11using System . Collections . Generic ;
22using System . Windows . Media ;
33using System . Windows ;
4+ using System . Reflection ;
45
56namespace SVGImage . SVG
67{
7- class TextRender
8+ static class TextRender
89 {
10+ private static int dpiX = 0 ;
11+ private static int dpiY = 0 ;
12+
913 static public GeometryGroup BuildTextGeometry ( TextShape shape )
1014 {
1115 return BuildGlyphRun ( shape , 0 , 0 ) ;
@@ -76,6 +80,15 @@ static Geometry BuildGlyphRun(TextStyle textStyle, string text, double x, double
7680 if ( string . IsNullOrEmpty ( text ) )
7781 return new GeometryGroup ( ) ;
7882
83+ if ( dpiX == 0 || dpiY == 0 )
84+ {
85+ var sysPara = typeof ( SystemParameters ) ;
86+ var dpiXProperty = sysPara . GetProperty ( "DpiX" , BindingFlags . NonPublic | BindingFlags . Static ) ;
87+ var dpiYProperty = sysPara . GetProperty ( "Dpi" , BindingFlags . NonPublic | BindingFlags . Static ) ;
88+
89+ dpiX = ( int ) dpiXProperty . GetValue ( null , null ) ;
90+ dpiY = ( int ) dpiYProperty . GetValue ( null , null ) ;
91+ }
7992 double fontSize = textStyle . FontSize ;
8093 GlyphRun glyphs = null ;
8194 Typeface font = new Typeface ( new FontFamily ( textStyle . FontFamily ) ,
@@ -87,7 +100,13 @@ static Geometry BuildGlyphRun(TextStyle textStyle, string text, double x, double
87100 double baseline = y ;
88101 if ( font . TryGetGlyphTypeface ( out glyphFace ) )
89102 {
103+ #if DOTNET40 || DOTNET45 || DOTNET46
90104 glyphs = new GlyphRun ( ) ;
105+ #else
106+ var dpiScale = new DpiScale ( dpiX , dpiY ) ;
107+
108+ glyphs = new GlyphRun ( ( float ) dpiScale . PixelsPerDip ) ;
109+ #endif
91110 ( ( System . ComponentModel . ISupportInitialize ) glyphs ) . BeginInit ( ) ;
92111 glyphs . GlyphTypeface = glyphFace ;
93112 glyphs . FontRenderingEmSize = fontSize ;
0 commit comments