1
1
using SkiaSharp ;
2
2
using System ;
3
- using System . Diagnostics ;
4
3
using System . Linq ;
5
4
6
5
namespace BinaryKits . Zpl . Viewer . ElementDrawers
@@ -17,7 +16,7 @@ public class DrawerOptions
17
16
/// Applies label over a white background after rendering all elements
18
17
/// </summary>
19
18
public bool OpaqueBackground { get ; set ; } = false ;
20
-
19
+
21
20
/// <summary>
22
21
/// Renders the label as pdf
23
22
/// </summary>
@@ -27,102 +26,69 @@ public class DrawerOptions
27
26
28
27
public bool Antialias { get ; set ; } = true ;
29
28
30
- public static Func < string , SKTypeface > DefaultFontLoader = fontName => {
31
- var typeface = SKTypeface . Default ;
29
+ private static readonly string [ ] fontStack0 = new string [ ] {
30
+ "Swis721 Cn BT" ,
31
+ "Nimbus Sans" ,
32
+ "Arial" ,
33
+ "Helvetica Neue" ,
34
+ "Roboto Condensed"
35
+ } ;
36
+
37
+ private static readonly string [ ] fontStackA = new string [ ] {
38
+ "DejaVu Sans Mono" ,
39
+ "Cascadia Code" ,
40
+ "Consolas" ,
41
+ "SF Mono" ,
42
+ "Droid Sans Mono"
43
+ } ;
44
+
45
+ private static readonly SKTypeface typeface0 ;
46
+ private static readonly SKTypeface typefaceA ;
47
+
48
+ static DrawerOptions ( )
49
+ {
32
50
var skFontManager = SKFontManager . Default ;
33
51
var fontFamilies = skFontManager . FontFamilies ;
34
-
35
- if ( fontName == "0" )
52
+
53
+ typeface0 = SKTypeface . Default ;
54
+ typefaceA = SKTypeface . Default ;
55
+
56
+ foreach ( var familyName in fontStack0 )
36
57
{
37
- if ( fontFamilies . Contains ( "Helvetica" ) )
38
- {
39
- typeface = SKTypeface . FromFamilyName (
40
- "Helvetica" ,
41
- SKFontStyleWeight . Bold ,
42
- SKFontStyleWidth . SemiCondensed ,
43
- SKFontStyleSlant . Upright
44
- ) ;
45
- }
46
- else if ( fontFamilies . Contains ( "Roboto Condensed" ) )
47
- {
48
- typeface = SKTypeface . FromFamilyName (
49
- "Roboto Condensed" ,
50
- SKFontStyleWeight . Bold ,
51
- SKFontStyleWidth . Normal ,
52
- SKFontStyleSlant . Upright
53
- ) ;
54
- }
55
- else if ( fontFamilies . Contains ( "Swis721 BT" ) )
56
- {
57
- //Note: Zebra Swis721 BT (tt0003m_.ttf) is not as bold and condensed as Labelary
58
- //Note: swiss-721-bt-bold.ttf is not as condensed as Labelary
59
- //typeface = SKTypeface.FromFile(@"swiss-721-black-bt.ttf");
60
- typeface = SKTypeface . FromFamilyName (
61
- "Swis721 BT"
62
- ) ;
63
- }
64
- else if ( fontFamilies . Contains ( "Arial" ) )
65
- {
66
- typeface = SKTypeface . FromFamilyName (
67
- "Arial" ,
68
- SKFontStyleWeight . Bold ,
69
- SKFontStyleWidth . Condensed ,
70
- SKFontStyleSlant . Upright
71
- ) ;
72
- }
73
- else
58
+ if ( fontFamilies . Contains ( familyName ) )
74
59
{
75
- //let the system provide a fallback for Helvetica
76
- typeface = SKTypeface . FromFamilyName (
77
- "Helvetica" ,
60
+ typeface0 = SKTypeface . FromFamilyName (
61
+ familyName ,
78
62
SKFontStyleWeight . Bold ,
79
63
SKFontStyleWidth . SemiCondensed ,
80
64
SKFontStyleSlant . Upright
81
65
) ;
66
+ break ;
82
67
}
83
68
}
84
- else
69
+
70
+ foreach ( var familyName in fontStackA )
85
71
{
86
- if ( fontFamilies . Contains ( "DejaVu Sans Mono" ) )
72
+ if ( fontFamilies . Contains ( familyName ) )
87
73
{
88
- typeface = SKTypeface . FromFamilyName (
89
- "DejaVu Sans Mono" ,
90
- SKFontStyleWeight . Normal ,
91
- SKFontStyleWidth . Normal ,
92
- SKFontStyleSlant . Upright
93
- ) ;
94
- }
95
- else if ( fontFamilies . Contains ( "Courier New" ) )
96
- {
97
- typeface = SKTypeface . FromFamilyName (
98
- "Courier New" ,
99
- SKFontStyleWeight . Medium ,
100
- SKFontStyleWidth . Normal ,
101
- SKFontStyleSlant . Upright
102
- ) ;
103
- }
104
- else if ( fontFamilies . Contains ( "Courier" ) )
105
- {
106
- typeface = SKTypeface . FromFamilyName (
107
- "Courier" ,
108
- SKFontStyleWeight . Medium ,
109
- SKFontStyleWidth . Normal ,
110
- SKFontStyleSlant . Upright
111
- ) ;
112
- }
113
- else
114
- {
115
- //let the system provide a fallback for DejaVu
116
- typeface = SKTypeface . FromFamilyName (
117
- "DejaVu Sans Mono" ,
74
+ typefaceA = SKTypeface . FromFamilyName (
75
+ familyName ,
118
76
SKFontStyleWeight . Normal ,
119
77
SKFontStyleWidth . Normal ,
120
78
SKFontStyleSlant . Upright
121
79
) ;
80
+ break ;
122
81
}
123
82
}
83
+ }
84
+
85
+ private static Func < string , SKTypeface > DefaultFontLoader = fontName => {
86
+ if ( fontName == "0" )
87
+ {
88
+ return typeface0 ;
89
+ }
124
90
125
- return typeface ;
91
+ return typefaceA ;
126
92
} ;
127
93
}
128
94
}
0 commit comments