File size: 30,249 Bytes
fab29d7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 |
// Copyright (c) Dr. Dirk Lellinger. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using HtmlToFlowDocument.Dom;
using swd = System.Windows.Documents;
namespace HtmlToFlowDocument.Rendering
{
public class WpfRenderer
{
/// <summary>
/// Gets or sets a value indicating whether the renderer shold invert the colors of the text and the background.
/// </summary>
/// <value>
/// <c>true</c> if the renderer should invert colors; otherwise, <c>false</c>.
/// </value>
public bool InvertColors { get; set; }
/// <summary>
/// Gets or sets a value indicating whether text in a run should be split into separate Runs, each containing one word.
/// </summary>
/// <value>
/// <c>true</c> if text in a run should be split into separate Runs, each containing one word; otherwise, <c>false</c>.
/// </value>
public bool SplitIntoWords { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether text in a run should be split into separate Runs, each containing one sentence.
/// </summary>
/// <value>
/// <c>true</c> if text in a run should be split into separate Runs, each containing one sentence; otherwise, <c>false</c>.
/// </value>
public bool SplitIntoSentences { get; set; } = true;
/// <summary>
/// Gets or sets the font dictionary.
/// </summary>
/// <value>
/// The font dictionary. Key is the font name, value is the absolute path of the font file.
/// </value>
public Dictionary<string, string> FontDictionary { get; set; } = new Dictionary<string, string>();
/// <summary>
/// Gets or sets a value indicating whether during the conversion process the DOM elements are attached as tags to the UI elements.
/// This will of course increase the memory footprint, because the DOM elements then could not be reclaimed by the garbage collector.
/// </summary>
/// <value>
/// <c>true</c> if DOM elements should be attached as tags to the UI elements; otherwise, <c>false</c>.
/// </value>
public bool AttachDomAsTags { get; set; }
/// <summary>
/// Gets or sets the name of flow document.
/// </summary>
/// <value>
/// The name of flow document.
/// </value>
public string NameOfFlowDocument { get; set; } = "_guiFlowDocument";
/// <summary>
/// Gets or sets the template for the binding to get the column width (= viewport width) of the document.
/// Each time a binding to the viewport width is needed, a copy of this binding is made.
/// </summary>
/// <value>
/// The template for the binding to get the column width (= viewport width) of the document.
/// </value>
public Binding TemplateBindingViewportWidth { get; set; }
/// <summary>
/// Gets or sets the template for the binding to get the viewport height of the document.
/// Each time a binding to the viewport height is needed, a copy of this binding is made.
/// </summary>
/// <value>
/// The template for the binding to get the viewport height of the document.
/// </value>
public Binding TemplateBindingViewportHeight { get; set; }
/// <summary>
/// Renders the specified DOM flow document into a Wpf flow document.
/// </summary>
/// <param name="flowDocument">The DOM flow document to render.</param>
/// <returns></returns>
public swd.FlowDocument Render(FlowDocument flowDocument)
{
LoadFonts();
return (swd.FlowDocument)RenderRecursively(flowDocument);
}
/// <summary>
/// Renders the specified DOM section into a Wpf section
/// </summary>
/// <param name="section">The DOM section to render.</param>
/// <returns></returns>
public swd.Section Render(Section section)
{
return (swd.Section)RenderRecursively(section);
}
/// <summary>
/// Renders DOM text elements recursively, i.e. including their childs.
/// </summary>
/// <param name="e">The root DOM text element.</param>
/// <returns>The corresponding Wpf element.</returns>
/// <exception cref="System.InvalidOperationException">
/// </exception>
/// <exception cref="System.NotImplementedException"></exception>
public object RenderRecursively(TextElement e)
{
object wpf = null;
switch (e)
{
case BlockUIContainer buc:
{
wpf = new swd.BlockUIContainer();
}
break;
case FlowDocument flowDocument:
{
// make sure the standard colors were set
flowDocument.Foreground = ExCSS.Color.Black;
flowDocument.Background = ExCSS.Color.White;
var flowDocumente = new swd.FlowDocument() { Name = NameOfFlowDocument };
if (TemplateBindingViewportWidth is null)
TemplateBindingViewportWidth = new Binding("ColumnWidth") { Source = flowDocumente };
if (TemplateBindingViewportHeight is null)
TemplateBindingViewportHeight = new Binding("ColumnWidth") { Source = flowDocumente }; // Binding to ColumnWidth is not optimal, but better than nothing!
if (flowDocument.Background.HasValue)
flowDocumente.Background = GetBrushFromColor(flowDocument.Background.Value);
if (flowDocument.Foreground.HasValue)
flowDocumente.Foreground = GetBrushFromColor(flowDocument.Foreground.Value);
wpf = flowDocumente;
}
break;
case Hyperlink hl:
{
var hle = new swd.Hyperlink();
if (!string.IsNullOrEmpty(hl.NavigateUri))
{
if (System.Uri.TryCreate(hl.NavigateUri, UriKind.RelativeOrAbsolute, out var uri))
hle.NavigateUri = uri;
}
if (!string.IsNullOrEmpty(hl.TargetName))
{
hle.TargetName = hl.TargetName;
}
wpf = hle;
}
break;
case Image image:
{
var imagee = new System.Windows.Controls.Image();
if (!string.IsNullOrEmpty(image.Source))
{
imagee.SetBinding(System.Windows.Controls.Image.SourceProperty, $"ImageProvider[{image.Source}]");
}
if (image.Width == null && image.Height == null)
{
imagee.Stretch = System.Windows.Media.Stretch.Uniform;
var binding = new Binding() { RelativeSource = RelativeSource.Self, Path = new System.Windows.PropertyPath("Source") };
binding.Converter = ImageToImageWidthConverter.Instance;
imagee.SetBinding(System.Windows.Controls.Image.WidthProperty, binding);
}
else
{
imagee.Stretch = System.Windows.Media.Stretch.Uniform;
}
if (image.Width != null)
{
if (image.Width.IsPurelyAbsolute(out var widthPx))
{
imagee.Width = widthPx;
}
else
{
var multibinding = new MultiBinding();
multibinding.Bindings.Add(new Binding() { Source = TemplateBindingViewportWidth.Source, Path = TemplateBindingViewportWidth.Path });
multibinding.Bindings.Add(new Binding() { Source = TemplateBindingViewportHeight.Source, Path = TemplateBindingViewportHeight.Path });
multibinding.Converter = CompoundLengthConverter.Instance;
multibinding.ConverterParameter = GetCompoundLengthConverterParameters(image.Width);
imagee.SetBinding(System.Windows.Controls.Image.WidthProperty, multibinding);
}
}
if (image.Height != null)
{
if (image.Height.IsPurelyAbsolute(out var heightPx))
{
imagee.Height = heightPx;
}
else
{
var multibinding = new MultiBinding();
multibinding.Bindings.Add(new Binding() { Source = TemplateBindingViewportWidth.Source, Path = TemplateBindingViewportWidth.Path });
multibinding.Bindings.Add(new Binding() { Source = TemplateBindingViewportHeight.Source, Path = TemplateBindingViewportHeight.Path });
multibinding.Converter = CompoundLengthConverter.Instance;
multibinding.ConverterParameter = GetCompoundLengthConverterParameters(image.Height);
imagee.SetBinding(System.Windows.Controls.Image.HeightProperty, multibinding);
}
}
// set max-width and max-height
if (image.MaxWidth != null && image.MaxWidth.Value.IsAbsolute)
{
imagee.MaxWidth = image.MaxWidth.Value.ToPixel();
}
else if (image.MaxWidth == null || image.MaxWidth.Value.Type == ExCSS.Length.Unit.Vw)
{
double vwValue = image.MaxWidth.HasValue ? image.MaxWidth.Value.Value : 100;
var binding = new Binding() { Source = TemplateBindingViewportWidth.Source, Path = TemplateBindingViewportWidth.Path };
binding.Converter = RelativeSizeConverter.Instance;
binding.ConverterParameter = vwValue;
imagee.SetBinding(System.Windows.Controls.Image.MaxWidthProperty, binding);
}
else
{
throw new InvalidProgramException();
}
if (image.MaxHeight != null && image.MaxHeight.Value.IsAbsolute)
{
imagee.MaxHeight = image.MaxHeight.Value.ToPixel();
}
else if (image.MaxHeight == null || image.MaxHeight.Value.Type == ExCSS.Length.Unit.Vh)
{
double vhValue = image.MaxHeight.HasValue ? image.MaxHeight.Value.Value : 100;
var binding = new Binding() { Source = TemplateBindingViewportWidth.Source, Path = TemplateBindingViewportWidth.Path };
binding.Converter = RelativeSizeConverter.Instance;
binding.ConverterParameter = vhValue;
imagee.SetBinding(System.Windows.Controls.Image.MaxHeightProperty, binding);
}
else
{
throw new InvalidProgramException();
}
wpf = imagee;
}
break;
case InlineUIContainer iuc:
{
var inlineuiContainere = new swd.InlineUIContainer();
wpf = inlineuiContainere;
}
break;
case LineBreak lb:
{
wpf = new swd.LineBreak();
}
break;
case List list:
{
var liste = new swd.List();
if (list.MarkerStyle.HasValue)
{
liste.MarkerStyle = ToMarkerStyle(list.MarkerStyle.Value);
}
wpf = liste;
}
break;
case ListItem li:
{
wpf = new swd.ListItem();
}
break;
case Paragraph p:
{
var pe = new swd.Paragraph();
if (p.TextDecorations.HasValue)
{
pe.TextDecorations = ToTextDecorations(p.TextDecorations.Value);
}
if (p.TextIndent.HasValue)
{
pe.TextIndent = p.TextIndent.Value.IsAbsolute ? p.TextIndent.Value.ToPixel() : 0;
}
wpf = pe;
}
break;
case Run run:
{
if (SplitIntoWords)
wpf = CreateTextElement_SeparateWords(run.Text);
else if (SplitIntoSentences)
wpf = CreateTextElement_SeparateSentences(run.Text);
else
wpf = new swd.Run(run.Text);
}
break;
case Section s:
{
wpf = new swd.Section();
}
break;
case Span span:
{
wpf = new swd.Span();
}
break;
case Table tb:
{
var tbe = new swd.Table();
foreach (var c in tb.Columns)
{
if (c.Width.HasValue)
tbe.Columns.Add(new swd.TableColumn() { Width = new System.Windows.GridLength(c.Width.Value) });
else
tbe.Columns.Add(new swd.TableColumn());
}
wpf = tbe;
}
break;
case TableCell tc:
{
var tce = new swd.TableCell();
if (1 != tc.ColumnSpan)
{
tce.ColumnSpan = tc.ColumnSpan;
}
if (1 != tc.RowSpan)
{
tce.RowSpan = tc.RowSpan;
}
if (tc.BorderBrush.HasValue)
{
tce.BorderBrush = new System.Windows.Media.SolidColorBrush(ToColor(tc.BorderBrush.Value));
}
if (tc.BorderThickness.HasValue)
{
tce.BorderThickness = ToThickness(tc.BorderThickness.Value);
}
wpf = tce;
}
break;
case TableRow trow:
{
wpf = new swd.TableRow();
}
break;
case TableRowGroup trg:
{
wpf = new swd.TableRowGroup();
}
break;
default:
{
wpf = null;
}
break;
}
// Render TextElement properties
if (wpf is swd.TextElement te)
{
if (!string.IsNullOrEmpty(e.FontFamily))
{
te.FontFamily = GetFontFamily(e.FontFamily);
}
if (e.FontSize.HasValue)
{
var fs = e.FontSize.Value;
fs = Math.Max(0.004, fs);
te.FontSize = fs;
}
if (e.FontStyle.HasValue)
{
te.FontStyle = ToFontStyle(e.FontStyle.Value);
}
if (e.FontWeight.HasValue)
{
te.FontWeight = ToFontWeight(e.FontWeight.Value);
}
if (e.Foreground.HasValue && e.Foreground != e.ForegroundInheritedOnly)
{
te.Foreground = GetBrushFromColor(e.Foreground.Value);
}
if (e.Background.HasValue && e.Background != e.BackgroundInheritedOnly)
{
te.Background = GetBrushFromColor(e.Background.Value);
}
}
// now special properties
if (e is Block b && wpf is swd.Block be)
{
if (b.Margin.HasValue)
{
be.Margin = ToThickness(b.Margin.Value);
}
if (b.Padding.HasValue)
{
be.Padding = ToThickness(b.Padding.Value);
}
if (b.BorderBrush.HasValue)
{
be.BorderBrush = new System.Windows.Media.SolidColorBrush(ToColor(b.BorderBrush.Value));
}
if (b.BorderThickness.HasValue)
{
be.BorderThickness = ToThickness(b.BorderThickness.Value);
}
if (b.TextAlignment.HasValue)
{
be.TextAlignment = ToTextAlignment(b.TextAlignment.Value);
}
if (b.LineHeight.HasValue)
{
be.LineHeight = b.LineHeight.Value;
}
}
if (e is Inline i && wpf is swd.Inline ie)
{
if (i.VerticalAlignment.HasValue)
{
ie.BaselineAlignment = ToBaselineAlignment(i.VerticalAlignment.Value);
}
}
// finished rendering the attributes
// now, render all children
foreach (var child in e.Childs)
{
var childe = RenderRecursively(child);
switch (wpf)
{
case swd.Figure figure:
figure.Blocks.Add((swd.Block)childe);
break;
case swd.Floater floater:
floater.Blocks.Add((swd.Block)childe);
break;
case swd.FlowDocument flowDocument:
flowDocument.Blocks.Add((swd.Block)childe);
break;
case swd.List list:
list.ListItems.Add((swd.ListItem)childe);
break;
case swd.ListItem listItem:
listItem.Blocks.Add((swd.Block)childe);
break;
case swd.Section section:
section.Blocks.Add((swd.Block)childe);
break;
case swd.Table table:
table.RowGroups.Add((swd.TableRowGroup)childe);
break;
case swd.TableCell tableCell:
tableCell.Blocks.Add((swd.Block)childe);
break;
case swd.TableRow tableRow:
tableRow.Cells.Add((swd.TableCell)childe);
break;
case swd.TableRowGroup tableRowGroup:
tableRowGroup.Rows.Add((swd.TableRow)childe);
break;
// now elements that can contain inlines
case swd.Paragraph paragraph:
paragraph.Inlines.Add((swd.Inline)childe);
break;
case swd.Span span:
span.Inlines.Add((swd.Inline)childe);
break;
// now some specialties
case swd.InlineUIContainer inlineUIContainer:
if (inlineUIContainer.Child != null)
throw new InvalidOperationException($"{nameof(swd.InlineUIContainer)} can not contain more than one child");
inlineUIContainer.Child = (System.Windows.UIElement)childe;
break;
case swd.BlockUIContainer blockUIContainer:
if (blockUIContainer.Child != null)
throw new InvalidOperationException($"{nameof(swd.BlockUIContainer)} can not contain more than one child");
blockUIContainer.Child = (System.Windows.UIElement)childe;
break;
default:
throw new NotImplementedException();
}
}
if (AttachDomAsTags)
{
if (wpf is System.Windows.FrameworkContentElement conEle)
conEle.Tag = e;
else if (wpf is System.Windows.FrameworkElement uiEle)
uiEle.Tag = e;
}
return wpf;
}
private static object GetCompoundLengthConverterParameters(CompoundLength compoundLength)
{
var converterParameters = new List<(int, double)>();
foreach (var entry in compoundLength)
{
switch (entry.Key)
{
case ExCSS.Length.Unit.Px:
converterParameters.Add((0, entry.Value.Value));
break;
case ExCSS.Length.Unit.Vw:
converterParameters.Add((1, entry.Value.Value));
break;
case ExCSS.Length.Unit.Vh:
converterParameters.Add((2, entry.Value.Value));
break;
case ExCSS.Length.Unit.Vmin:
converterParameters.Add((3, entry.Value.Value));
break;
case ExCSS.Length.Unit.Vmax:
converterParameters.Add((4, entry.Value.Value));
break;
default:
throw new NotImplementedException();
}
}
return converterParameters;
}
private object CreateTextElement_SeparateSentences(string text)
{
if (SplitIntoSentences)
{
int prevIdx = 0;
List<int> list = null;
int numberOfWords = 0;
bool inWord = false;
for (int i = 0; i < text.Length; ++i)
{
char c = text[i];
if (char.IsWhiteSpace(text[i]))
{
inWord = false;
}
else
{
if (!inWord)
{
++numberOfWords;
}
inWord = true;
}
if (c == '.' || c == '!' || c == '?')
{
if (numberOfWords > 2 || (i - prevIdx) >= 4)
{
if (null == list)
{
list = new List<int>(text.Length / 5);
list.Add(0);
}
list.Add(i + 1);
prevIdx = i + 1;
}
}
}
if (null != list)
{
list.Add(text.Length);
var span = new swd.Span();
for (int i = 1; i < list.Count; ++i)
{
span.Inlines.Add(new swd.Run(text.Substring(list[i - 1], list[i] - list[i - 1])));
}
return span;
}
else
{
return new swd.Run(text);
}
}
else
{
return new swd.Run(text);
}
}
private object CreateTextElement_SeparateWords(string text)
{
var span = new swd.Span();
int prevIdx = 0;
int numberOfWords = 0;
bool inWord = false;
swd.Run previousRun = null;
int i;
for (i = 0; i < text.Length; ++i)
{
if (char.IsWhiteSpace(text[i]))
{
inWord = false;
}
else
{
if (!inWord)
{
++numberOfWords;
if (null != previousRun)
{
span.Inlines.Add(previousRun);
previousRun = null;
}
if (i > prevIdx)
{
previousRun = new swd.Run(text.Substring(prevIdx, i - prevIdx));
}
prevIdx = i;
}
inWord = true;
}
}
if (null != previousRun)
{
span.Inlines.Add(previousRun);
previousRun = null;
}
if (i > prevIdx)
{
previousRun = new swd.Run(text.Substring(prevIdx, i - prevIdx));
span.Inlines.Add(previousRun);
}
if (span.Inlines.Count > 0)
return span;
else
return previousRun ?? throw new InvalidOperationException();
}
#region Conversion helper
/// <summary>
/// Converts internal FontStyle enum to WPF fontstyle.
/// </summary>
/// <param name="fs">The internal fontstyle value.</param>
/// <returns>WPF fontstyle.</returns>
/// <exception cref="System.NotImplementedException"></exception>
public System.Windows.FontStyle ToFontStyle(FontStyle fs)
{
switch (fs)
{
case FontStyle.Normal:
return System.Windows.FontStyles.Normal;
case FontStyle.Oblique:
return System.Windows.FontStyles.Oblique;
case FontStyle.Italic:
return System.Windows.FontStyles.Italic;
default:
throw new NotImplementedException();
}
}
public System.Windows.FontWeight ToFontWeight(FontWeight fw)
{
switch (fw)
{
case FontWeight.Thin:
return System.Windows.FontWeights.Thin;
case FontWeight.ExtraLight:
return System.Windows.FontWeights.ExtraLight;
case FontWeight.Normal:
return System.Windows.FontWeights.Normal;
case FontWeight.Medium:
return System.Windows.FontWeights.Medium;
case FontWeight.DemiBold:
return System.Windows.FontWeights.DemiBold;
case FontWeight.Bold:
return System.Windows.FontWeights.Bold;
case FontWeight.ExtraBold:
return System.Windows.FontWeights.ExtraBold;
case FontWeight.Black:
return System.Windows.FontWeights.Black;
case FontWeight.ExtraBlack:
return System.Windows.FontWeights.ExtraBlack;
default:
throw new NotImplementedException();
}
}
public System.Windows.Media.Color ToColor(ExCSS.Color color)
{
if (InvertColors)
return System.Windows.Media.Color.FromArgb(color.A, (byte)(255 - color.R), (byte)(255 - color.G), (byte)(255 - color.B));
else
return System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B);
}
public System.Windows.Thickness ToThickness(Thickness t)
{
if (t.Left == t.Right && t.Left == t.Top && t.Left == t.Bottom && t.Left.IsAbsolute)
return new System.Windows.Thickness(t.Left.ToPixel());
else
return new System.Windows.Thickness(
t.Left.IsAbsolute ? Math.Max(0, t.Left.ToPixel()) : 0, // Note: we have to clamp to values >=0, since Wpf does not accept negative values
t.Top.IsAbsolute ? Math.Max(0, t.Top.ToPixel()) : 0,
t.Right.IsAbsolute ? Math.Max(0, t.Right.ToPixel()) : 0,
t.Bottom.IsAbsolute ? Math.Max(0, t.Bottom.ToPixel()) : 0
);
}
private System.Windows.TextAlignment ToTextAlignment(TextAlignment value)
{
switch (value)
{
case TextAlignment.Left:
return System.Windows.TextAlignment.Left;
case TextAlignment.Right:
return System.Windows.TextAlignment.Right;
case TextAlignment.Center:
return System.Windows.TextAlignment.Center;
case TextAlignment.Justify:
return System.Windows.TextAlignment.Justify;
default:
throw new NotImplementedException();
}
}
private System.Windows.BaselineAlignment ToBaselineAlignment(ExCSS.VerticalAlignment value)
{
switch (value)
{
case ExCSS.VerticalAlignment.Baseline:
return System.Windows.BaselineAlignment.Baseline;
case ExCSS.VerticalAlignment.Sub:
return System.Windows.BaselineAlignment.Subscript;
case ExCSS.VerticalAlignment.Super:
return System.Windows.BaselineAlignment.Superscript;
case ExCSS.VerticalAlignment.TextTop:
return System.Windows.BaselineAlignment.TextTop;
case ExCSS.VerticalAlignment.TextBottom:
return System.Windows.BaselineAlignment.Bottom;
case ExCSS.VerticalAlignment.Middle:
return System.Windows.BaselineAlignment.Center;
case ExCSS.VerticalAlignment.Top:
return System.Windows.BaselineAlignment.Top;
case ExCSS.VerticalAlignment.Bottom:
return System.Windows.BaselineAlignment.Bottom;
default:
throw new NotImplementedException();
}
}
private System.Windows.TextDecorationCollection ToTextDecorations(Dom.TextDecorations value)
{
switch (value)
{
case TextDecorations.None:
return null;
case TextDecorations.Underline:
return System.Windows.TextDecorations.Underline;
default:
throw new NotImplementedException();
}
}
private System.Windows.TextMarkerStyle ToMarkerStyle(ListMarkerStyle value)
{
switch (value)
{
case ListMarkerStyle.None:
return System.Windows.TextMarkerStyle.None;
case ListMarkerStyle.Square:
return System.Windows.TextMarkerStyle.Square;
case ListMarkerStyle.Box:
return System.Windows.TextMarkerStyle.Box;
case ListMarkerStyle.LowerLatin:
return System.Windows.TextMarkerStyle.LowerLatin;
case ListMarkerStyle.UpperLatin:
return System.Windows.TextMarkerStyle.UpperLatin;
case ListMarkerStyle.LowerRoman:
return System.Windows.TextMarkerStyle.LowerRoman;
case ListMarkerStyle.UpperRoman:
return System.Windows.TextMarkerStyle.UpperRoman;
case ListMarkerStyle.Decimal:
return System.Windows.TextMarkerStyle.Decimal;
case ListMarkerStyle.Disc:
return System.Windows.TextMarkerStyle.Disc;
case ListMarkerStyle.Circle:
return System.Windows.TextMarkerStyle.Circle;
default:
throw new NotImplementedException();
}
}
#endregion
Dictionary<string, System.Windows.Media.FontFamily> _resolvedFontFamilies = new Dictionary<string, System.Windows.Media.FontFamily>();
void LoadFonts()
{
_resolvedFontFamilies.Clear();
var alreadyTriedFolders = new HashSet<string>();
foreach (var entry in FontDictionary)
{
var folder = System.IO.Path.GetDirectoryName(entry.Value);
if (!alreadyTriedFolders.Contains(folder))
{
alreadyTriedFolders.Add(folder);
if (folder.StartsWith(@"\\?\"))
folder = folder.Substring(4);
folder = folder.Replace('\\', '/');
folder = "file:///" + folder;
if (!folder.EndsWith("/"))
folder += "/";
var families = System.Windows.Media.Fonts.GetFontFamilies(folder);
foreach (var family in families)
{
var src = family.Source;
var parts = src.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length >= 2)
_resolvedFontFamilies[parts[parts.Length - 1].ToLowerInvariant()] = family;
}
}
}
}
private static readonly Dictionary<string, string> _genericFontSubstitutes = new Dictionary<string, string>
{
["monospace"] = "Global Monospace",
["serif"] = "Global Serif",
["sans-serif"] = "Global Sans Serif",
["cursive"] = "Comic Sans MS",
["fantasy"] = "Impact",
};
System.Windows.Media.FontFamily GetFontFamily(string familyName)
{
familyName = familyName.ToLowerInvariant();
var parts = familyName.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
if (_resolvedFontFamilies.ContainsKey(parts[0].Trim()))
{
return _resolvedFontFamilies[parts[0].Trim()];
}
else
{
if (_genericFontSubstitutes.TryGetValue(familyName, out var substitute))
familyName = substitute;
return new System.Windows.Media.FontFamily(familyName);
}
}
Dictionary<ExCSS.Color, System.Windows.Media.Brush> _cachedSolidBrushes = new Dictionary<ExCSS.Color, System.Windows.Media.Brush>();
/// <summary>
/// Gets a solid color brush from the color specified.
/// </summary>
/// <param name="color">The color in RGBA format (A is the least significant byte).</param>
/// <returns>A solid color brush.</returns>
public System.Windows.Media.Brush GetBrushFromColor(ExCSS.Color color)
{
if (!_cachedSolidBrushes.TryGetValue(color, out var brush))
{
brush = new System.Windows.Media.SolidColorBrush(ToColor(color));
brush.Freeze();
_cachedSolidBrushes.Add(color, brush);
}
return brush;
}
}
}
|