-
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathFieldBlockElementDrawer.cs
More file actions
247 lines (213 loc) · 9.65 KB
/
FieldBlockElementDrawer.cs
File metadata and controls
247 lines (213 loc) · 9.65 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using BinaryKits.Zpl.Label;
using BinaryKits.Zpl.Label.Elements;
using BinaryKits.Zpl.Viewer.Helpers;
using SkiaSharp;
using SkiaSharp.HarfBuzz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BinaryKits.Zpl.Viewer.ElementDrawers
{
/// <summary>
/// Drawer for Field Block elements
/// </summary>
public class FieldBlockElementDrawer : ElementDrawerBase
{
///<inheritdoc/>
public override bool CanDraw(ZplElementBase element)
{
return element is ZplFieldBlock;
}
///<inheritdoc/>
public override bool IsReverseDraw(ZplElementBase element)
{
if (element is ZplFieldBlock fieldBlock)
{
return fieldBlock.ReversePrint;
}
return false;
}
///<inheritdoc/>
public override SKPoint Draw(ZplElementBase element, DrawerOptions options, SKPoint currentPosition, InternationalFont internationalFont, int printDensityDpmm)
{
if (element is ZplFieldBlock fieldBlock)
{
ZplFont font = fieldBlock.Font;
SKTypeface typeface = options.FontManager.GetFont(font.FontName, FallbackFont, out bool found);
string fontName = found ? font.FontName : FallbackFont;
(float fontSize, float scaleX) = FontScale.GetFontScaling(fontName, font.FontHeight, font.FontWidth, printDensityDpmm);
string text = fieldBlock.Text;
if (fieldBlock.HexadecimalIndicator is char hexIndicator)
{
text = text.ReplaceHexEscapes(hexIndicator, internationalFont);
}
if (fontName == "0")
{
if (options.ReplaceDashWithEnDash)
{
text = text.Replace("-", " \u2013 ");
}
if (options.ReplaceUnderscoreWithEnSpace)
{
text = text.Replace('_', '\u2002');
}
}
SKFont skFont = new(typeface, fontSize, scaleX);
using SKPaint skPaint = new()
{
IsAntialias = options.Antialias
};
skFont.MeasureText("X", out SKRect textBoundBaseline);
float x = fieldBlock.PositionX;
float y = fieldBlock.PositionY + textBoundBaseline.Height;
if (fieldBlock.UseDefaultPosition)
{
x = currentPosition.X;
y = currentPosition.Y + textBoundBaseline.Height;
}
IEnumerable<string> textLines = WordWrap(text, skFont, fieldBlock.Width);
int hangingIndent = 0;
float lineHeight = fontSize + fieldBlock.LineSpace;
// actual ZPL printer does not include trailing line spacing in total height
float totalHeight = lineHeight * fieldBlock.MaxLineCount - fieldBlock.LineSpace;
// labelary
//var totalHeight = lineHeight * fieldBlock.MaxLineCount;
if (fieldBlock.FieldTypeset != null)
{
totalHeight = lineHeight * (fieldBlock.MaxLineCount - 1) + textBoundBaseline.Height;
y -= totalHeight;
}
using (new SKAutoCanvasRestore(this.skCanvas))
{
SKMatrix matrix = SKMatrix.Empty;
if (fieldBlock.FieldOrigin != null)
{
switch (fieldBlock.Font.FieldOrientation)
{
case FieldOrientation.Rotated90:
matrix = SKMatrix.CreateRotationDegrees(90, fieldBlock.PositionX + totalHeight / 2, fieldBlock.PositionY + totalHeight / 2);
break;
case FieldOrientation.Rotated180:
matrix = SKMatrix.CreateRotationDegrees(180, fieldBlock.PositionX + fieldBlock.Width / 2, fieldBlock.PositionY + totalHeight / 2);
break;
case FieldOrientation.Rotated270:
matrix = SKMatrix.CreateRotationDegrees(270, fieldBlock.PositionX + fieldBlock.Width / 2, fieldBlock.PositionY + fieldBlock.Width / 2);
break;
case FieldOrientation.Normal:
break;
}
}
else
{
switch (fieldBlock.Font.FieldOrientation)
{
case FieldOrientation.Rotated90:
matrix = SKMatrix.CreateRotationDegrees(90, fieldBlock.PositionX, fieldBlock.PositionY);
break;
case FieldOrientation.Rotated180:
matrix = SKMatrix.CreateRotationDegrees(180, fieldBlock.PositionX, fieldBlock.PositionY);
break;
case FieldOrientation.Rotated270:
matrix = SKMatrix.CreateRotationDegrees(270, fieldBlock.PositionX, fieldBlock.PositionY);
break;
case FieldOrientation.Normal:
break;
}
}
if (matrix != SKMatrix.Empty)
{
SKMatrix currentMatrix = this.skCanvas.TotalMatrix;
SKMatrix concatMatrix = SKMatrix.Concat(currentMatrix, matrix);
this.skCanvas.SetMatrix(concatMatrix);
}
foreach (string textLine in textLines)
{
x = fieldBlock.PositionX + hangingIndent;
skFont.MeasureText(textLine, out SKRect textBounds);
float diff = fieldBlock.Width - textBounds.Width;
switch (fieldBlock.TextJustification)
{
case TextJustification.Center:
x += diff / 2 - textBounds.Left;
break;
case TextJustification.Right:
x += diff - textBounds.Left * 2;
hangingIndent = -fieldBlock.HangingIndent;
break;
case TextJustification.Left:
case TextJustification.Justified:
default:
hangingIndent = fieldBlock.HangingIndent;
break;
}
if (fieldBlock.ReversePrint)
{
skPaint.BlendMode = SKBlendMode.Xor;
}
this.skCanvas.DrawShapedText(textLine, x, y, skFont, skPaint);
y += lineHeight;
}
return this.CalculateNextDefaultPosition(fieldBlock.PositionX, fieldBlock.PositionY, fieldBlock.Width, totalHeight, fieldBlock.FieldOrigin != null, fieldBlock.Font.FieldOrientation, currentPosition);
}
}
return currentPosition;
}
private static List<string> WordWrap(string text, SKFont font, int maxWidth)
{
float spaceWidth = font.MeasureText(" ");
List<string> lines = [];
Stack<string> words = new(text.Split([' '], StringSplitOptions.None).AsEnumerable().Reverse());
StringBuilder line = new();
float width = 0;
while (words.Count != 0)
{
string word = words.Pop();
if (word.Contains(@"\&"))
{
string[] subwords = word.Split([@"\&"], 2, StringSplitOptions.None);
word = subwords[0];
words.Push(subwords[1]);
float wordWidth = font.MeasureText(word);
if (width + wordWidth <= maxWidth)
{
line.Append(word);
lines.Add(line.ToString());
line = new StringBuilder();
width = 0;
}
else
{
if (line.Length > 0)
{
lines.Add(line.ToString().Trim());
}
lines.Add(word.ToString());
line = new StringBuilder();
width = 0;
}
}
else
{
float wordWidth = font.MeasureText(word);
if (width + wordWidth <= maxWidth)
{
line.Append(word + " ");
width += wordWidth + spaceWidth;
}
else
{
if (line.Length > 0)
{
lines.Add(line.ToString().Trim());
}
line = new StringBuilder(word + " ");
width = wordWidth + spaceWidth;
}
}
}
lines.Add(line.ToString().Trim());
return lines;
}
}
}