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