-
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathGraphicSymbolElementDrawer.cs
More file actions
122 lines (103 loc) · 4.85 KB
/
GraphicSymbolElementDrawer.cs
File metadata and controls
122 lines (103 loc) · 4.85 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
using BinaryKits.Zpl.Label;
using BinaryKits.Zpl.Label.Elements;
using BinaryKits.Zpl.Viewer.Helpers;
using SkiaSharp;
namespace BinaryKits.Zpl.Viewer.ElementDrawers
{
/// <summary>
/// Drawer for Text Field elements
/// </summary>
public class GraphicSymbolElementDrawer : ElementDrawerBase
{
///<inheritdoc/>
public override bool CanDraw(ZplElementBase element)
{
return element.GetType() == typeof(ZplGraphicSymbol);
}
///<inheritdoc/>
public override SKPoint Draw(ZplElementBase element, DrawerOptions options, SKPoint currentPosition, InternationalFont internationalFont, int printDensityDpmm)
{
if (element is ZplGraphicSymbol graphicSymbol)
{
float x = graphicSymbol.PositionX;
float y = graphicSymbol.PositionY;
FieldJustification fieldJustification = FieldJustification.None;
if (graphicSymbol.UseDefaultPosition)
{
x = currentPosition.X;
y = currentPosition.Y;
}
(float fontSize, float scaleX) = FontScale.GetFontScaling("GS", graphicSymbol.Height, graphicSymbol.Width, printDensityDpmm);
SKTypeface typeface = options.FontManager.TypefaceGS;
SKFont skFont = new(typeface, fontSize * 1.25f, scaleX);
using SKPaint skPaint = new()
{
IsAntialias = options.Antialias
};
string displayText = $"{(char?)graphicSymbol.Character}";
float totalWidth = skFont.MeasureText(displayText, out SKRect textBounds);
using (new SKAutoCanvasRestore(this.skCanvas))
{
SKMatrix matrix = SKMatrix.Empty;
if (graphicSymbol.FieldOrigin != null)
{
switch (graphicSymbol.FieldOrientation)
{
case FieldOrientation.Rotated90:
matrix = SKMatrix.CreateRotationDegrees(90, x + fontSize / 2, y + fontSize / 2);
break;
case FieldOrientation.Rotated180:
matrix = SKMatrix.CreateRotationDegrees(180, x + textBounds.Width / 2, y + fontSize / 2);
break;
case FieldOrientation.Rotated270:
matrix = SKMatrix.CreateRotationDegrees(270, x + textBounds.Width / 2, y + textBounds.Width / 2);
break;
case FieldOrientation.Normal:
break;
}
fieldJustification = graphicSymbol.FieldOrigin.FieldJustification;
}
else
{
switch (graphicSymbol.FieldOrientation)
{
case FieldOrientation.Rotated90:
matrix = SKMatrix.CreateRotationDegrees(90, x, y);
break;
case FieldOrientation.Rotated180:
matrix = SKMatrix.CreateRotationDegrees(180, x, y);
break;
case FieldOrientation.Rotated270:
matrix = SKMatrix.CreateRotationDegrees(270, x, y);
break;
case FieldOrientation.Normal:
break;
}
fieldJustification = graphicSymbol.FieldTypeset.FieldJustification;
}
if (matrix != SKMatrix.Empty)
{
this.skCanvas.Concat(matrix);
}
if (graphicSymbol.FieldTypeset == null)
{
y += fontSize;
}
SKTextAlign textAlign = SKTextAlign.Left;
if (fieldJustification == FieldJustification.Left)
{
textAlign = SKTextAlign.Left;
}
else if (fieldJustification == FieldJustification.Right)
{
textAlign = SKTextAlign.Right;
}
this.skCanvas.DrawText(displayText, x, y, textAlign, skFont, skPaint);
// Update the next default field position after rendering
return this.CalculateNextDefaultPosition(x, y, totalWidth, textBounds.Height, false, graphicSymbol.FieldOrientation, currentPosition);
}
}
return currentPosition;
}
}
}