-
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathBarcode39ElementDrawer.cs
More file actions
69 lines (57 loc) · 2.78 KB
/
Barcode39ElementDrawer.cs
File metadata and controls
69 lines (57 loc) · 2.78 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
using BinaryKits.Zpl.Label;
using BinaryKits.Zpl.Label.Elements;
using BinaryKits.Zpl.Viewer.Helpers;
using SkiaSharp;
using System;
using ZXing.OneD;
namespace BinaryKits.Zpl.Viewer.ElementDrawers
{
/// <summary>
/// Drawer for Code 39 Barcode elements
/// </summary>
public class Barcode39ElementDrawer : BarcodeDrawerBase
{
///<inheritdoc/>
public override bool CanDraw(ZplElementBase element)
{
return element is ZplBarcode39;
}
///<inheritdoc/>
public override SKPoint Draw(ZplElementBase element, DrawerOptions options, SKPoint currentPosition, InternationalFont internationalFont, int printDensityDpmm)
{
if (element is ZplBarcode39 barcode)
{
float x = barcode.PositionX;
float y = barcode.PositionY;
if (barcode.UseDefaultPosition)
{
x = currentPosition.X;
y = currentPosition.Y;
}
string content = barcode.Content.Trim('*');
if (barcode.HexadecimalIndicator is char hexIndicator)
{
content = content.ReplaceHexEscapes(hexIndicator, internationalFont);
}
string interpretation = string.Format("*{0}*", content);
Code39Writer writer = new();
bool[] result = writer.encode(content);
int narrow = barcode.ModuleWidth;
int wide = (int)Math.Floor(barcode.WideBarToNarrowBarWidthRatio * narrow);
result = AdjustWidths(result, wide, narrow);
using SKBitmap resizedImage = BoolArrayToSKBitmap(result, barcode.Height);
byte[] png = resizedImage.Encode(SKEncodedImageFormat.Png, 100).ToArray();
this.DrawBarcode(png, x, y, resizedImage.Width, resizedImage.Height, barcode.FieldOrigin != null, barcode.FieldOrientation);
if (barcode.PrintInterpretationLine)
{
float labelFontSize = FontScale.GetBitmappedFontSize("A", Math.Min(barcode.ModuleWidth, 10), printDensityDpmm).Value;
SKTypeface labelTypeFace = options.FontManager.GetFont("A");
SKFont labelFont = new(labelTypeFace, labelFontSize);
this.DrawInterpretationLine(interpretation, labelFont, x, y, resizedImage.Width, resizedImage.Height, barcode.FieldOrigin != null, barcode.FieldOrientation, barcode.PrintInterpretationLineAboveCode, options);
}
return this.CalculateNextDefaultPosition(x, y, resizedImage.Width, resizedImage.Height, barcode.FieldOrigin != null, barcode.FieldOrientation, currentPosition);
}
return currentPosition;
}
}
}