-
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathFontScale.cs
More file actions
176 lines (156 loc) · 5.53 KB
/
FontScale.cs
File metadata and controls
176 lines (156 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Collections.Generic;
namespace BinaryKits.Zpl.Viewer.Helpers
{
using FontScaleDictionary = Dictionary<string, (int height, int width)>;
internal static class FontScale
{
private static readonly FontScaleDictionary fontScales6mm = new()
{
["A"] = (9, 5),
["B"] = (11, 7),
["C"] = (18, 10),
["D"] = (18, 10),
["E"] = (21, 10),
["F"] = (26, 13),
["G"] = (60, 40),
["H"] = (17, 11),
["GS"] = (24, 24)
};
private static readonly FontScaleDictionary fontScales8mm = new()
{
["A"] = (9, 5),
["B"] = (11, 7),
["C"] = (18, 10),
["D"] = (18, 10),
["E"] = (28, 15),
["F"] = (26, 13),
["G"] = (60, 40),
["H"] = (21, 13),
["GS"] = (24, 24),
["P"] = (20, 18),
["Q"] = (28, 24),
["R"] = (35, 31),
["S"] = (40, 35),
["T"] = (48, 42),
["U"] = (59, 53),
["V"] = (80, 71)
};
private static readonly FontScaleDictionary fontScales12mm = new()
{
["A"] = (9, 5),
["B"] = (11, 7),
["C"] = (18, 10),
["D"] = (18, 10),
["E"] = (42, 20),
["F"] = (26, 13),
["G"] = (60, 40),
["H"] = (34, 22),
["GS"] = (24, 24),
["P"] = (20, 18),
["Q"] = (28, 24),
["R"] = (35, 31),
["S"] = (40, 35),
["T"] = (48, 42),
["U"] = (59, 53),
["V"] = (80, 71)
};
private static readonly FontScaleDictionary fontScales24mm = new()
{
["A"] = (9, 5),
["B"] = (11, 7),
["C"] = (18, 10),
["D"] = (18, 10),
["E"] = (42, 20),
["F"] = (26, 13),
["G"] = (60, 40),
["H"] = (34, 22),
["GS"] = (24, 24),
["P"] = (20, 18),
["Q"] = (28, 24),
["R"] = (35, 31),
["S"] = (40, 35),
["T"] = (48, 42),
["U"] = (59, 53),
["V"] = (80, 71)
};
private static (int height, int width)? GetFontScale(string fontName, int printDensityDpmm)
{
FontScaleDictionary dict;
switch (printDensityDpmm)
{
case 6:
dict = fontScales6mm;
break;
case 8:
dict = fontScales8mm;
break;
case 12:
dict = fontScales12mm;
break;
case 24:
dict = fontScales24mm;
break;
default:
return null;
}
if (dict.TryGetValue(fontName, out (int height, int width) value))
{
return value;
}
return null;
}
private static readonly (int height, int width) defaultScalingFontScale = (15, 12);
// This is a corrective used to match labelary.com scale for text fields' font size
// TODO: implement a proper solution based on font metrics
private const float heightScale = 1.1f;
public static float? GetBitmappedFontSize(string fontName, int scalingFactor, int printDensityDpmm)
{
return GetFontScale(fontName, printDensityDpmm)?.height * scalingFactor * heightScale;
}
public static (float fontSize, float scaleX) GetFontScaling(string fontName, int fontHeight, int fontWidth, int printDensityDpmm)
{
(int height, int width)? fontScale = GetFontScale(fontName, printDensityDpmm);
if (fontScale != null)
{
(int height, int width) = fontScale.Value;
if (fontHeight > 0)
{
double heightRatio = (double)fontHeight / height;
int intHeightRatio = (int)Math.Max(1, Math.Round(heightRatio));
float emSize = height * intHeightRatio;
if (!fontName.Equals("GS"))
{
emSize *= heightScale;
}
if (fontWidth == 0)
{
return (emSize, 1.0f);
}
double widthRatio = (double)fontWidth / width;
int intWidthRatio = (int)Math.Max(1, Math.Round(widthRatio));
return (emSize, (float)intWidthRatio / intHeightRatio);
}
else if (fontWidth > 0)
{
double widthRatio = (double)fontWidth / width;
int intWidthRatio = (int)Math.Max(1, Math.Round(widthRatio));
return fontName.Equals("GS")
? (height * intWidthRatio, 1.0f)
: (height * intWidthRatio * heightScale, 1.0f);
}
else
{
return (height * heightScale, 1.0f);
}
}
float fontSize = fontHeight > 0 ? fontHeight : fontWidth > 0 ? fontWidth : defaultScalingFontScale.height;
float scaleX = 1.0f;
if (fontWidth != 0 && fontWidth != fontSize)
{
scaleX = fontWidth / fontSize;
}
return (fontSize, scaleX);
}
}
}