1212
1313using PdfSharp . Drawing ;
1414using PdfSharp . Pdf ;
15- using System . Drawing ;
16- using System . Drawing . Text ;
15+ using System ;
1716using System . IO ;
17+ using FontResolver . PdfSharp ;
1818using TheArtOfDev . HtmlRenderer . Adapters ;
1919using TheArtOfDev . HtmlRenderer . Adapters . Entities ;
2020using TheArtOfDev . HtmlRenderer . PdfSharp . Utilities ;
@@ -41,14 +41,16 @@ internal sealed class PdfSharpAdapter : RAdapter
4141 /// </summary>
4242 private PdfSharpAdapter ( )
4343 {
44+ FontResolverPdfSharp . Register ( ) ;
45+
4446 AddFontFamilyMapping ( "monospace" , "Courier New" ) ;
4547 AddFontFamilyMapping ( "Helvetica" , "Arial" ) ;
4648
47- var families = new InstalledFontCollection ( ) ;
48-
49- foreach ( var family in families . Families )
49+ var fontFamilies = FontResolver . FontResolver . DiscoverFontFamilies ( ) ;
50+
51+ foreach ( var fontFamily in fontFamilies )
5052 {
51- AddFontFamily ( new FontFamilyAdapter ( new XFontFamily ( family . Name ) ) ) ;
53+ AddFontFamily ( new FontFamilyAdapter ( new XFontFamily ( fontFamily ) ) ) ;
5254 }
5355 }
5456
@@ -64,8 +66,23 @@ protected override RColor GetColorInt(string colorName)
6466 {
6567 try
6668 {
67- var color = Color . FromKnownColor ( ( KnownColor ) System . Enum . Parse ( typeof ( KnownColor ) , colorName , true ) ) ;
68- return Utils . Convert ( color ) ;
69+ var colorResourceManager = new XColorResourceManager ( ) ;
70+
71+ var knownColors = XColorResourceManager . GetKnownColors ( true ) ;
72+
73+ foreach ( var knownColor in knownColors )
74+ {
75+ var name = colorResourceManager . ToColorName ( knownColor ) ;
76+ if ( ! string . Equals ( name , colorName , StringComparison . OrdinalIgnoreCase ) )
77+ {
78+ continue ;
79+ }
80+
81+ var xColor = XColor . FromKnownColor ( knownColor ) ;
82+ return xColor . IsEmpty ? RColor . Empty : Utils . Convert ( xColor ) ;
83+ }
84+
85+ return RColor . Empty ;
6986 }
7087 catch
7188 {
@@ -119,14 +136,14 @@ protected override RImage ImageFromStreamInt(Stream memoryStream)
119136
120137 protected override RFont CreateFontInt ( string family , double size , RFontStyle style )
121138 {
122- var fontStyle = ( XFontStyle ) ( ( int ) style ) ;
139+ var fontStyle = Utils . Convert ( style ) ;
123140 var xFont = new XFont ( family , size , fontStyle , new XPdfFontOptions ( PdfFontEncoding . Unicode ) ) ;
124141 return new FontAdapter ( xFont ) ;
125142 }
126143
127144 protected override RFont CreateFontInt ( RFontFamily family , double size , RFontStyle style )
128145 {
129- var fontStyle = ( XFontStyle ) ( ( int ) style ) ;
146+ var fontStyle = Utils . Convert ( style ) ;
130147 var xFont = new XFont ( ( ( FontFamilyAdapter ) family ) . FontFamily . Name , size , fontStyle , new XPdfFontOptions ( PdfFontEncoding . Unicode ) ) ;
131148 return new FontAdapter ( xFont ) ;
132149 }
0 commit comments