Skip to content

Commit 3d6b3e6

Browse files
committed
Rework font resolution so that it doesn't rely on knowing what font families are available ahead of time.
1 parent 257bcfe commit 3d6b3e6

9 files changed

Lines changed: 76 additions & 32 deletions

File tree

‎Source/HtmlRenderer.PdfSharp/Adapters/FontAdapter.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// like the days and months;
77
// they die and are reborn,
88
// like the four seasons."
9-
//
9+
//
1010
// - Sun Tsu,
1111
// "The Art of War"
1212

@@ -53,6 +53,8 @@ public FontAdapter(XFont font)
5353
_font = font;
5454
}
5555

56+
public override string Family => _font.Name;
57+
5658
/// <summary>
5759
/// the underline win-forms font.
5860
/// </summary>

‎Source/HtmlRenderer.PdfSharp/Adapters/PdfSharpAdapter.cs‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,6 @@ private PdfSharpAdapter()
4141
{
4242
AddFontFamilyMapping("monospace", "Courier New");
4343
AddFontFamilyMapping("Helvetica", "Arial");
44-
45-
// This doesn't work on non-Windows systems since it uses System.Drawing.Common.
46-
//var families = new InstalledFontCollection();
47-
48-
//foreach (var family in families.Families)
49-
//{
50-
// AddFontFamily(new FontFamilyAdapter(new XFontFamily(family.Name)));
51-
//}
5244
}
5345

5446
/// <summary>
@@ -109,6 +101,9 @@ protected override RImage ImageFromStreamInt(Stream memoryStream)
109101
return new ImageAdapter(XImage.FromStream(() => memoryStream));
110102
}
111103

104+
public override RFontFamily CreateFontFamily(string family)
105+
=> new FontFamilyAdapter(new XFontFamily(family));
106+
112107
protected override RFont CreateFontInt(string family, double size, RFontStyle style)
113108
{
114109
var fontStyle = (XFontStyle)((int)style);
@@ -118,9 +113,7 @@ protected override RFont CreateFontInt(string family, double size, RFontStyle st
118113

119114
protected override RFont CreateFontInt(RFontFamily family, double size, RFontStyle style)
120115
{
121-
var fontStyle = (XFontStyle)((int)style);
122-
var xFont = new XFont(((FontFamilyAdapter)family).FontFamily.Name, size, fontStyle, new XPdfFontOptions(PdfFontEncoding.Unicode));
123-
return new FontAdapter(xFont);
116+
return CreateFontInt(((FontFamilyAdapter)family).FontFamily.Name, size, style);
124117
}
125118
}
126119
}

‎Source/HtmlRenderer.WPF/Adapters/FontAdapter.cs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// like the days and months;
77
// they die and are reborn,
88
// like the four seasons."
9-
//
9+
//
1010
// - Sun Tsu,
1111
// "The Art of War"
1212

@@ -20,6 +20,7 @@ namespace TheArtOfDev.HtmlRenderer.WPF.Adapters
2020
/// </summary>
2121
internal sealed class FontAdapter : RFont
2222
{
23+
2324
#region Fields and Consts
2425

2526
/// <summary>
@@ -58,8 +59,9 @@ internal sealed class FontAdapter : RFont
5859
/// <summary>
5960
/// Init.
6061
/// </summary>
61-
public FontAdapter(Typeface font, double size)
62+
public FontAdapter(string family, Typeface font, double size)
6263
{
64+
Family = family;
6365
_font = font;
6466
_size = size;
6567
_height = 96d / 72d * _size * _font.FontFamily.LineSpacing;
@@ -80,6 +82,8 @@ public FontAdapter(Typeface font, double size)
8082
}
8183
}
8284

85+
public override string Family { get; }
86+
8387
/// <summary>
8488
/// the underline win-forms font.
8589
/// </summary>

‎Source/HtmlRenderer.WPF/Adapters/WpfAdapter.cs‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// like the days and months;
77
// they die and are reborn,
88
// like the four seasons."
9-
//
9+
//
1010
// - Sun Tsu,
1111
// "The Art of War"
1212

@@ -39,7 +39,7 @@ internal sealed class WpfAdapter : RAdapter
3939
/// <summary>
4040
/// List of valid predefined color names in lower-case
4141
/// </summary>
42-
private static readonly List<string> ValidColorNamesLc;
42+
private static readonly List<string> ValidColorNamesLc;
4343

4444
#endregion
4545

@@ -129,15 +129,18 @@ protected override RImage ImageFromStreamInt(Stream memoryStream)
129129
return new ImageAdapter(bitmap);
130130
}
131131

132+
public override RFontFamily CreateFontFamily(string family)
133+
=> new FontFamilyAdapter(new FontFamily(family));
134+
132135
protected override RFont CreateFontInt(string family, double size, RFontStyle style)
133136
{
134137
var fontFamily = (FontFamily)new FontFamilyConverter().ConvertFromString(family) ?? new FontFamily();
135-
return new FontAdapter(new Typeface(fontFamily, GetFontStyle(style), GetFontWidth(style), FontStretches.Normal), size);
138+
return new FontAdapter(family, new Typeface(fontFamily, GetFontStyle(style), GetFontWidth(style), FontStretches.Normal), size);
136139
}
137140

138141
protected override RFont CreateFontInt(RFontFamily family, double size, RFontStyle style)
139142
{
140-
return new FontAdapter(new Typeface(((FontFamilyAdapter)family).FontFamily, GetFontStyle(style), GetFontWidth(style), FontStretches.Normal), size);
143+
return new FontAdapter(family.Name, new Typeface(((FontFamilyAdapter)family).FontFamily, GetFontStyle(style), GetFontWidth(style), FontStretches.Normal), size);
141144
}
142145

143146
protected override object GetClipboardDataObjectInt(string html, string plainText)

‎Source/HtmlRenderer.WinForms/Adapters/FontAdapter.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// like the days and months;
77
// they die and are reborn,
88
// like the four seasons."
9-
//
9+
//
1010
// - Sun Tsu,
1111
// "The Art of War"
1212

@@ -59,6 +59,8 @@ public FontAdapter(Font font)
5959
_font = font;
6060
}
6161

62+
public override string Family => Font.FontFamily.Name;
63+
6264
/// <summary>
6365
/// the underline win-forms font.
6466
/// </summary>

‎Source/HtmlRenderer.WinForms/Adapters/WinFormsAdapter.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// like the days and months;
77
// they die and are reborn,
88
// like the four seasons."
9-
//
9+
//
1010
// - Sun Tsu,
1111
// "The Art of War"
1212

@@ -98,6 +98,9 @@ protected override RImage ImageFromStreamInt(Stream memoryStream)
9898
return new ImageAdapter(Image.FromStream(memoryStream));
9999
}
100100

101+
public override RFontFamily CreateFontFamily(string family)
102+
=> new FontFamilyAdapter(new FontFamily(family));
103+
101104
protected override RFont CreateFontInt(string family, double size, RFontStyle style)
102105
{
103106
var fontStyle = (FontStyle)((int)style);

‎Source/HtmlRenderer/Adapters/RAdapter.cs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// like the days and months;
77
// they die and are reborn,
88
// like the four seasons."
9-
//
9+
//
1010
// - Sun Tsu,
1111
// "The Art of War"
1212

@@ -24,7 +24,7 @@ namespace TheArtOfDev.HtmlRenderer.Adapters
2424
{
2525
/// <summary>
2626
/// Platform adapter to bridge platform specific objects to HTML Renderer core library.<br/>
27-
/// Core uses abstract renderer objects (RAdapter/RControl/REtc...) to access platform specific functionality, the concrete platforms
27+
/// Core uses abstract renderer objects (RAdapter/RControl/REtc...) to access platform specific functionality, the concrete platforms
2828
/// implements those objects to provide concrete platform implementation. Those allowing the core library to be platform agnostic.
2929
/// <para>
3030
/// Platforms: WinForms, WPF, Metro, PDF renders, etc.<br/>
@@ -184,7 +184,7 @@ public void AddFontFamily(RFontFamily fontFamily)
184184

185185
/// <summary>
186186
/// Adds a font mapping from <paramref name="fromFamily"/> to <paramref name="toFamily"/> iff the <paramref name="fromFamily"/> is not found.<br/>
187-
/// When the <paramref name="fromFamily"/> font is used in rendered html and is not found in existing
187+
/// When the <paramref name="fromFamily"/> font is used in rendered html and is not found in existing
188188
/// fonts (installed or added) it will be replaced by <paramref name="toFamily"/>.<br/>
189189
/// </summary>
190190
/// <param name="fromFamily">the font family to replace</param>
@@ -301,6 +301,8 @@ public void SaveToFile(RImage image, string name, string extension, RControl con
301301
SaveToFileInt(image, name, extension, control);
302302
}
303303

304+
public abstract RFontFamily CreateFontFamily(string family);
305+
304306
/// <summary>
305307
/// Get font instance by given font family name, size and style.
306308
/// </summary>

‎Source/HtmlRenderer/Adapters/RFont.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ namespace TheArtOfDev.HtmlRenderer.Adapters
1717
/// </summary>
1818
public abstract class RFont
1919
{
20+
public abstract string Family { get; }
21+
2022
/// <summary>
2123
/// Gets the em-size of this Font measured in the units specified by the Unit property.
2224
/// </summary>

‎Source/HtmlRenderer/Core/Handlers/FontsHandler.cs‎

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// like the days and months;
77
// they die and are reborn,
88
// like the four seasons."
9-
//
9+
//
1010
// - Sun Tsu,
1111
// "The Art of War"
1212

@@ -26,7 +26,7 @@ internal sealed class FontsHandler
2626
#region Fields and Consts
2727

2828
/// <summary>
29-
///
29+
///
3030
/// </summary>
3131
private readonly RAdapter _adapter;
3232

@@ -65,16 +65,44 @@ public FontsHandler(RAdapter adapter)
6565
/// <returns>true - font exists by given family name, false - otherwise</returns>
6666
public bool IsFontExists(string family)
6767
{
68-
bool exists = _existingFontFamilies.ContainsKey(family);
69-
if (!exists)
68+
if (_existingFontFamilies.ContainsKey(family))
7069
{
71-
string mappedFamily;
72-
if (_fontsMapping.TryGetValue(family, out mappedFamily))
70+
return true;
71+
}
72+
73+
if (_fontsMapping.TryGetValue(family, out var mappedFamily))
74+
{
75+
if (_existingFontFamilies.ContainsKey(mappedFamily))
7376
{
74-
exists = _existingFontFamilies.ContainsKey(mappedFamily);
77+
return true;
7578
}
79+
80+
// We want to resolve and store under the mapped family,
81+
// not the original one.
82+
family = mappedFamily;
83+
}
84+
85+
// Note that the size doesn't matter; specifying Regular for the
86+
// style gives us the best chance of a match (seems very unlikely
87+
// anyone would have italic or bold files installed but not the base
88+
// regular file).
89+
var font = CreateFont(family, 10.0, RFontStyle.Regular);
90+
if (font == null)
91+
{
92+
return false;
7693
}
77-
return exists;
94+
95+
// PdfSharpCore will return the first font it knows about if it can't
96+
// find the font you requested, so let's check we actually got what
97+
// we asked for:
98+
if (font.Family != family)
99+
{
100+
return false;
101+
}
102+
103+
AddFontFamily(_adapter.CreateFontFamily(family));
104+
105+
return true;
78106
}
79107

80108
/// <summary>
@@ -86,11 +114,16 @@ public void AddFontFamily(RFontFamily fontFamily)
86114
ArgChecker.AssertArgNotNull(fontFamily, "family");
87115

88116
_existingFontFamilies[fontFamily.Name] = fontFamily;
117+
118+
if (fontFamily.Name != fontFamily.Name.ToLowerInvariant())
119+
{
120+
_existingFontFamilies[fontFamily.Name.ToLowerInvariant()] = fontFamily;
121+
}
89122
}
90123

91124
/// <summary>
92125
/// Adds a font mapping from <paramref name="fromFamily"/> to <paramref name="toFamily"/> iff the <paramref name="fromFamily"/> is not found.<br/>
93-
/// When the <paramref name="fromFamily"/> font is used in rendered html and is not found in existing
126+
/// When the <paramref name="fromFamily"/> font is used in rendered html and is not found in existing
94127
/// fonts (installed or added) it will be replaced by <paramref name="toFamily"/>.<br/>
95128
/// </summary>
96129
/// <param name="fromFamily">the font family to replace</param>

0 commit comments

Comments
 (0)