Skip to content

Commit

Permalink
Implement delegate for circumventing the WPF embedded font resource m…
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor Lee authored and zone117x committed Feb 3, 2018
1 parent 6111e2d commit 17e37cc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
10 changes: 6 additions & 4 deletions LiteHtmlSharp.Wpf/FontInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Media;
Expand All @@ -20,11 +20,13 @@ public class FontInfo

public TextDecorationCollection Decorations = new TextDecorationCollection();

public FontInfo(string faceName, FontStyle style, FontWeight weight, int size)
public FontInfo(string faceName, FontStyle style, FontWeight weight, int size, FontFamily fontFamily = null)
{
// Using the pack URI means we can look inside resources in addition to system fonts.
// Using the pack URI means we can look inside resources in addition to system fonts - this is only supported for backwards compatibility, as it causes memory leaks.
// https://stackoverflow.com/questions/31452443/wpf-textblock-memory-leak-when-using-font
// FontFamily will allow comma separated font names, but does not support quotes of any kind (so don't quote it in your HTML/CSS!).
Family = new FontFamily(new Uri("pack://application:,,,/Fonts/"), faceName);

Family = fontFamily ?? new FontFamily(new Uri("pack://application:,,,/Fonts/"), faceName);

TypeFace = new Typeface(Family, style, weight, new FontStretch());
Size = size;
Expand Down
9 changes: 7 additions & 2 deletions LiteHtmlSharp.Wpf/WPFContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ public IntPoint(int x, int y)
}
}

public delegate FontFamily FontAbsolutePathDelegate(string fontName);

public class WpfContainer : ViewportContainer
{
IResourceLoader _loader;

public FontAbsolutePathDelegate FontAbsolutePathDelegate;

class ResourceLoader : IResourceLoader
{
Func<string, string> _getStringResource;
Expand Down Expand Up @@ -282,8 +286,9 @@ protected override void DrawText(string text, UIntPtr font, ref web_color color,

protected override UIntPtr CreateFont(string faceName, int size, int weight, font_style italic, font_decoration decoration, ref font_metrics fm)
{
var fontweight = FontWeight.FromOpenTypeWeight(weight);
FontInfo font = new FontInfo(faceName, italic == font_style.fontStyleItalic ? FontStyles.Italic : FontStyles.Normal, fontweight, size);
var fontweight = FontWeight.FromOpenTypeWeight(weight);
FontInfo font = new FontInfo(family, italic == font_style.fontStyleItalic ? FontStyles.Italic : FontStyles.Normal, fontweight, size, FontAbsolutePathDelegate?.Invoke(family));

if ((decoration & font_decoration.font_decoration_underline) != 0)
{
font.Decorations.Add(TextDecorations.Underline);
Expand Down

0 comments on commit 17e37cc

Please sign in to comment.