Skip to content

Commit 7961201

Browse files
authored
Rework font stack (#248)
Only load default fonts once, rather than each time they are used. Reorder font preference in the order (Zebra), Linux, Windows, MacOS, Android.
1 parent 642ada2 commit 7961201

File tree

1 file changed

+45
-79
lines changed

1 file changed

+45
-79
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using SkiaSharp;
22
using System;
3-
using System.Diagnostics;
43
using System.Linq;
54

65
namespace BinaryKits.Zpl.Viewer.ElementDrawers
@@ -17,7 +16,7 @@ public class DrawerOptions
1716
/// Applies label over a white background after rendering all elements
1817
/// </summary>
1918
public bool OpaqueBackground { get; set; } = false;
20-
19+
2120
/// <summary>
2221
/// Renders the label as pdf
2322
/// </summary>
@@ -27,102 +26,69 @@ public class DrawerOptions
2726

2827
public bool Antialias { get; set; } = true;
2928

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+
{
3250
var skFontManager = SKFontManager.Default;
3351
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)
3657
{
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))
7459
{
75-
//let the system provide a fallback for Helvetica
76-
typeface = SKTypeface.FromFamilyName(
77-
"Helvetica",
60+
typeface0 = SKTypeface.FromFamilyName(
61+
familyName,
7862
SKFontStyleWeight.Bold,
7963
SKFontStyleWidth.SemiCondensed,
8064
SKFontStyleSlant.Upright
8165
);
66+
break;
8267
}
8368
}
84-
else
69+
70+
foreach (var familyName in fontStackA)
8571
{
86-
if (fontFamilies.Contains("DejaVu Sans Mono"))
72+
if (fontFamilies.Contains(familyName))
8773
{
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,
11876
SKFontStyleWeight.Normal,
11977
SKFontStyleWidth.Normal,
12078
SKFontStyleSlant.Upright
12179
);
80+
break;
12281
}
12382
}
83+
}
84+
85+
private static Func<string, SKTypeface> DefaultFontLoader = fontName => {
86+
if (fontName == "0")
87+
{
88+
return typeface0;
89+
}
12490

125-
return typeface;
91+
return typefaceA;
12692
};
12793
}
12894
}

0 commit comments

Comments
 (0)