-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElasticLabel.cs
More file actions
237 lines (192 loc) · 9.01 KB
/
ElasticLabel.cs
File metadata and controls
237 lines (192 loc) · 9.01 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
using QRCoder;
using System.Buffers.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace SolidMatrix.ElasticLabels;
public class ElasticLabel
{
[JsonInclude]
public Guid Guid { get; internal set; }
[JsonInclude]
public int Width { get; internal set; }
[JsonInclude]
public int Height { get; internal set; }
[JsonInclude]
public float WidthCm { get; internal set; }
[JsonInclude]
public float HeightCm { get; internal set; }
[JsonInclude]
public List<ElasticField> Fields { get; internal set; }
[JsonConstructor]
internal ElasticLabel()
{
Fields = [];
}
public ElasticLabel(int width, int height, float res_cm)
{
Guid = Guid.NewGuid();
Width = width;
Height = height;
WidthCm = width / res_cm;
HeightCm = height / res_cm;
Fields = [];
}
public ElasticLabel(int width, int height, float width_cm, float height_cm)
{
Guid = Guid.NewGuid();
Width = width;
Height = height;
WidthCm = width_cm;
HeightCm = height_cm;
Fields = [];
}
public static byte[] Encode(ElasticLabel label)
{
return JsonSerializer.SerializeToUtf8Bytes(label);
}
public static void Save(ElasticLabel label, string path)
{
File.WriteAllBytes(path, Encode(label));
}
public static ElasticLabel Decode(string path)
{
return Decode(File.ReadAllBytes(path));
}
public static ElasticLabel Decode(ReadOnlySpan<byte> bytes)
{
return JsonSerializer.Deserialize<ElasticLabel>(bytes) ?? throw new Exception("failed to decode");
}
internal static StringAlignment AlignmentSwitch(Alignment alignment)
{
return alignment switch
{
Alignment.Center => StringAlignment.Center,
Alignment.Left => StringAlignment.Near,
Alignment.Right => StringAlignment.Far,
_ => throw new Exception("unsupported alignment"),
};
}
internal static void RenderText(Graphics g, ElasticField field)
{
var fontSize = field.FontSize == 0 ? field.Rect.Height / 2.0f : field.FontSize;
//var fontStyle = fontSize >= 30 ? FontStyle.Bold : FontStyle.Regular;
using var font = new Font(FontFamily.GenericSerif, fontSize, FontStyle.Bold);
using var format = StringFormat.GenericTypographic;
format.LineAlignment = StringAlignment.Center;
format.Alignment = AlignmentSwitch(field.Alignment);
format.FormatFlags = StringFormatFlags.NoWrap;
g.DrawString(field.Value, font, Brushes.Black, field.Rect, format);
}
internal static void RenderImage(Graphics g, ElasticField field)
{
var bytes = Base64Url.DecodeFromChars(field.Value);
using var ms = new MemoryStream(bytes);
using var bmp = new Bitmap(ms);
int width;
int height;
double rx = (double)bmp.Width / field.Rect.Width;
double ry = (double)bmp.Height / field.Rect.Height;
if (rx >= ry)
{
width = field.Rect.Width;
height = (int)Math.Round((double)field.Rect.Width * bmp.Height / bmp.Width);
}
else
{
width = (int)Math.Round((double)field.Rect.Height * bmp.Width / bmp.Height);
height = field.Rect.Height;
}
int x = field.Rect.X + field.Rect.Width / 2 - width / 2;
int y = field.Rect.Y + field.Rect.Height / 2 - height / 2;
g.DrawImage(bmp, new Rectangle(x, y, width, height), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
}
internal static void RenderRect(Graphics g, ElasticField field)
{
var width = int.Parse(field.Value);
using var pen = new Pen(Color.Black, width);
g.DrawRectangle(pen, field.Rect);
}
internal static void RenderTextSlotPreview(Graphics g, ElasticField field)
{
var fontSize = field.FontSize == 0 ? field.Rect.Height / 2.0f : field.FontSize;
using var font = new Font(FontFamily.GenericSerif, fontSize, FontStyle.Bold);
using var format = StringFormat.GenericTypographic;
format.LineAlignment = StringAlignment.Center;
format.Alignment = AlignmentSwitch(field.Alignment);
format.FormatFlags = StringFormatFlags.NoWrap;
g.FillRectangle(Brushes.LightGray, field.Rect);
g.DrawString(field.Value, font, Brushes.Black, field.Rect, format);
}
internal static void RenderTextSlot(Graphics g, ElasticField field, string? value)
{
if (value == null) return;
var fontSize = field.FontSize == 0 ? field.Rect.Height / 2.0f : field.FontSize;
using var font = new Font(FontFamily.GenericSerif, fontSize, FontStyle.Bold);
using var format = StringFormat.GenericTypographic;
format.LineAlignment = StringAlignment.Center;
format.Alignment = AlignmentSwitch(field.Alignment);
format.FormatFlags = StringFormatFlags.NoWrap;
g.DrawString(value, font, Brushes.Black, field.Rect, format);
}
internal static void RenderQrCodeSlotPreview(Graphics g, ElasticField field)
{
using var qrCodeGenerator = new QRCodeGenerator();
using var qrCodeData = qrCodeGenerator.CreateQrCode(field.Value, QRCodeGenerator.ECCLevel.L);
using var qrCode = new QRCode(qrCodeData);
using var bmp = qrCode.GetGraphic(1, Color.Black, Color.White, false);
var size = Math.Min(field.Rect.Width, field.Rect.Height);
int x = field.Rect.X + field.Rect.Width / 2 - size / 2;
int y = field.Rect.Y + field.Rect.Height / 2 - size / 2;
g.DrawImage(bmp, new Rectangle(x, y, size, size), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
}
internal static void RenderQrCodeSlot(Graphics g, ElasticField field, string? value)
{
if (value == null) return;
using var qrCodeGenerator = new QRCodeGenerator();
using var qrCodeData = qrCodeGenerator.CreateQrCode(value, QRCodeGenerator.ECCLevel.L);
using var qrCode = new QRCode(qrCodeData);
using var bmp = qrCode.GetGraphic(1, Color.Black, Color.White, false);
var size = Math.Min(field.Rect.Width, field.Rect.Height);
int x = field.Rect.X + field.Rect.Width / 2 - size / 2;
int y = field.Rect.Y + field.Rect.Height / 2 - size / 2;
g.DrawImage(bmp, new Rectangle(x, y, size, size), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
}
internal static void RenderPreviewOnGraphics(ElasticLabel label, Graphics g, int width, int height)
{
g.ScaleTransform((float)width / label.Width, (float)height / label.Height);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.Clear(Color.White);
foreach (var field in label.Fields.Where(field => field.Type == ElasticFieldType.Text)) RenderText(g, field);
foreach (var field in label.Fields.Where(field => field.Type == ElasticFieldType.Image)) RenderImage(g, field);
foreach (var field in label.Fields.Where(field => field.Type == ElasticFieldType.TextSlot)) RenderTextSlotPreview(g, field);
foreach (var field in label.Fields.Where(field => field.Type == ElasticFieldType.QrCodeSlot)) RenderQrCodeSlotPreview(g, field);
foreach (var field in label.Fields.Where(field => field.Type == ElasticFieldType.Rect)) RenderRect(g, field);
}
internal static void RenderOnGraphics(ElasticLabel label, Graphics g, int width, int height, IDictionary<string, string> slotValues)
{
g.ScaleTransform((float)width / label.Width, (float)height / label.Height);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.Clear(Color.White);
foreach (var field in label.Fields.Where(field => field.Type == ElasticFieldType.Text)) RenderText(g, field);
foreach (var field in label.Fields.Where(field => field.Type == ElasticFieldType.Image)) RenderImage(g, field);
foreach (var field in label.Fields.Where(field => field.Type == ElasticFieldType.TextSlot))
{
if (slotValues.TryGetValue(field.Entry, out var value))
{
RenderTextSlot(g, field, value);
}
}
foreach (var field in label.Fields.Where(field => field.Type == ElasticFieldType.QrCodeSlot))
{
if (slotValues.TryGetValue(field.Entry, out var value))
{
RenderQrCodeSlot(g, field, value);
}
}
foreach (var field in label.Fields.Where(field => field.Type == ElasticFieldType.Rect)) RenderRect(g, field);
}
}