Skip to content

Commit a11ccd5

Browse files
authored
Merge pull request #241 from BinaryKits/develop
Merge develop branch
2 parents d6b7903 + 92fe196 commit a11ccd5

14 files changed

+186
-26
lines changed

src/BinaryKits.Zpl.Label/BinaryKits.Zpl.Label.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFrameworks>net472;netstandard2.0;net6.0</TargetFrameworks>
55
<Description>This package allows you to simply and reliably prepare labels complying with the Zebra programming language, using predefined class/typing. It also supports you with image conversion.</Description>
66
<Company>Binary Kits Pte. Ltd.</Company>
7-
<Version>3.2.0</Version>
7+
<Version>3.2.1</Version>
88
<Authors>Binary Kits Pte. Ltd.</Authors>
99
<PackageReleaseNotes></PackageReleaseNotes>
1010
<PackageTags>Zebra ZPL ZPL2 Printer Label</PackageTags>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Collections.Generic;
2+
3+
namespace BinaryKits.Zpl.Label.Elements
4+
{
5+
/// <summary>
6+
/// ^FW - Field Orientation
7+
/// </summary>
8+
public class ZplFieldOrientation : ZplElementBase
9+
{
10+
public FieldOrientation FieldOrientation { get; private set; }
11+
12+
public ZplFieldOrientation(FieldOrientation fieldOrientation)
13+
{
14+
this.FieldOrientation = fieldOrientation;
15+
}
16+
17+
///<inheritdoc/>
18+
public override IEnumerable<string> Render(ZplRenderOptions context)
19+
{
20+
return new[] { $"^FW{RenderFieldOrientation(this.FieldOrientation)}" };
21+
}
22+
23+
}
24+
}

src/BinaryKits.Zpl.Viewer.WebApi/Labels/Test/FieldDataText2-102x152.zpl2

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
^XA
22

33
^LH0,0
4+
^FWI
45

5-
^A0N,50,0
6+
^A0,50,0
67
^FO10,0
78
^FDFont1 Demo Text^FS
89

9-
^A1N,50,0
10+
^A1,50,0
1011
^FO10,100
1112
^FDFont2 Demo Text^FS
1213

13-
^AAN,80,0
14+
^AA,80,0
1415
^FO10,200
1516
^FDFont3 Demo Text^FS
1617

17-
^ABN,50,0
18+
^AB,50,0
1819
^FO10,300
1920
^FDFont4 Demo Text^FS
2021

21-
^ACN,20,0
22+
^AC,20,0
2223
^FO10,400
2324
^FDFont5 Demo Text^FS
2425

25-
^ADN,0,20
26+
^AD,0,20
2627
^FO10,500
2728
^FDFont6 Demo Text^FS
2829

29-
^ADN,20,20
30+
^AD,20,20
3031
^FO10,600
3132
^FDFont7 Demo Text^FS
3233

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
^XA
2+
^DFFORMAT^FS
3+
^BY2,3.0^FO40,40^BCN,80,Y,,,A^FR^FN1^FS
4+
^BY2,3.0^FO40,160^BCN,80,Y,,,A^FR^FN2^FS
5+
^FN1^FD12345^FS
6+
^XZ
7+
8+
^XA
9+
^XFFORMAT.ZPL
10+
^FN2^FDABC12345^FS
11+
^XZ

src/BinaryKits.Zpl.Viewer/BinaryKits.Zpl.Viewer.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFrameworks>net472;netstandard2.0;net6.0</TargetFrameworks>
55
<Description>This package provides a rendering logic for ZPL data, as an alternative to labelary.com.</Description>
66
<Company>Binary Kits Pte. Ltd.</Company>
7-
<Version>1.2.0</Version>
7+
<Version>1.2.1</Version>
88
<Authors>Binary Kits Pte. Ltd.</Authors>
99
<PackageReleaseNotes></PackageReleaseNotes>
1010
<PackageTags>Zebra ZPL ZPL2 ZPLEmulator ZPLVirtualPrinter ZPLViewer ZPLParser</PackageTags>

src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldDataZplCommandAnalyzer.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,9 @@ private ZplFont GetFontFromVirtualPrinter()
206206
int fontWidth = this.VirtualPrinter.FontWidth;
207207
int fontHeight = this.VirtualPrinter.FontHeight;
208208
string fontName = this.VirtualPrinter.FontName;
209+
var fieldOrientation = this.VirtualPrinter.FieldOrientation;
209210

210-
return new ZplFont(fontWidth, fontHeight, fontName);
211+
return new ZplFont(fontWidth, fontHeight, fontName, fieldOrientation);
211212
}
212213

213214
private ZplFont GetNextFontFromVirtualPrinter()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using BinaryKits.Zpl.Label.Elements;
2+
3+
namespace BinaryKits.Zpl.Viewer.CommandAnalyzers
4+
{
5+
public class FieldOrientationZplCommandAnalyzer : ZplCommandAnalyzerBase
6+
{
7+
public FieldOrientationZplCommandAnalyzer(VirtualPrinter virtualPrinter) : base("^FW", virtualPrinter) { }
8+
9+
///<inheritdoc/>
10+
public override ZplElementBase Analyze(string zplCommand)
11+
{
12+
var zplDataParts = this.SplitCommand(zplCommand);
13+
if (zplDataParts.Length > 0)
14+
{
15+
var fieldOrientation = ConvertFieldOrientation(zplDataParts[0]);
16+
this.VirtualPrinter.SetFieldOrientation(fieldOrientation);
17+
}
18+
19+
return null;
20+
}
21+
}
22+
}

src/BinaryKits.Zpl.Viewer/CommandAnalyzers/ZplCommandAnalyzerBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected FieldOrientation ConvertFieldOrientation(string fieldOrientation)
3737
"R" => FieldOrientation.Rotated90,
3838
"I" => FieldOrientation.Rotated180,
3939
"B" => FieldOrientation.Rotated270,
40-
_ => FieldOrientation.Normal,
40+
_ => this.VirtualPrinter.FieldOrientation,
4141
};
4242
}
4343

src/BinaryKits.Zpl.Viewer/ElementDrawers/Barcode128ElementDrawer.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,12 @@ public override void Draw(ZplElementBase element, DrawerOptions options)
3434
}
3535
else if (barcode.Mode == "D")
3636
{
37-
codeSet = Code128CodeSet.Code128C;
37+
codeSet = Code128CodeSet.Code128;
3838
gs1 = true;
3939
}
4040
else if (barcode.Mode == "U")
4141
{
4242
codeSet = Code128CodeSet.Code128C;
43-
gs1 = true;
4443
content = content.PadLeft(19, '0').Substring(0, 19);
4544
int checksum = 0;
4645
for (int i = 0; i < 19; i++)

src/BinaryKits.Zpl.Viewer/FormatMerger.cs

+16-5
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,27 @@ private static List<ZplElementBase> GetMergedElements(
4343
{
4444
var elements = new List<ZplElementBase>();
4545

46-
foreach (ZplElementBase element in rawLabelInfo.ZplElements)
46+
foreach (ZplElementBase zplElement in rawLabelInfo.ZplElements)
4747
{
48-
if (element is ZplRecallFormat recallFormat)
48+
if (zplElement is ZplRecallFormat recallFormat)
4949
{
5050
LabelInfo formatLabelInfo = GetFormatLabelInfo(recallFormat, templateFormats);
5151
elements.AddRange(GetMergedElements(rawLabelInfo, formatLabelInfo));
5252
}
53-
else if (element is not ZplRecallFieldNumber)
53+
else if (zplElement is ZplRecallFieldNumber recallFieldNumber)
5454
{
55-
elements.Add(element);
55+
for(int i = 0; i < elements.Count; i++)
56+
{
57+
if (elements[i] is ZplFieldNumber fieldNumber && fieldNumber.Number == recallFieldNumber.Number)
58+
{
59+
((IFormatElement)fieldNumber.FormatElement).SetTemplateContent(recallFieldNumber.Text);
60+
elements[i] = fieldNumber.FormatElement;
61+
}
62+
}
63+
}
64+
else
65+
{
66+
elements.Add(zplElement);
5667
}
5768
}
5869

@@ -103,4 +114,4 @@ private static IEnumerable<ZplElementBase> GetMergedElements(
103114
}
104115
}
105116
}
106-
}
117+
}

src/BinaryKits.Zpl.Viewer/Symologies/ZplCode128Symbology.cs

+89-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public class ZplCode128Symbology
2222
private static readonly Regex startCRegex = new Regex(@"^\d{4}", RegexOptions.Compiled);
2323
private static readonly Regex digitPairRegex = new Regex(@"^\d\d", RegexOptions.Compiled);
2424

25+
// GS1 specific
26+
private static readonly Regex gs1SwitchCRegex = new Regex(@"^\d\d(?:\d\d|>8)+(?!\d)", RegexOptions.Compiled);
27+
private static readonly Regex gs1StartCRegex = new Regex(@"^(?:\d\d|>8){2}", RegexOptions.Compiled);
28+
private static readonly Regex gs1DigitPairRegex = new Regex(@"^(?:\d\d|>8)", RegexOptions.Compiled);
29+
private static readonly Regex fnc1Regex = new Regex(@"^>8", RegexOptions.Compiled);
30+
2531
private static readonly Regex startCodeRegex = new Regex(@"^(>[9:;])(.+)$", RegexOptions.Compiled);
2632

2733
private static readonly Dictionary<string, int> invocationMap = new Dictionary<string, int>() {
@@ -44,6 +50,12 @@ public class ZplCode128Symbology
4450
{ ">;", Code128CodeSet.Code128C }
4551
};
4652

53+
private static readonly Dictionary<Code128CodeSet, string> codeSetCodeMap = new Dictionary<Code128CodeSet, string>() {
54+
{ Code128CodeSet.Code128A, CODE_A },
55+
{ Code128CodeSet.Code128B, CODE_B },
56+
{ Code128CodeSet.Code128C, CODE_C }
57+
};
58+
4759
private const string FNC_1 = "FNC_1";
4860
private const string FNC_2 = "FNC_2";
4961
private const string FNC_3 = "FNC_3";
@@ -208,7 +220,20 @@ static ZplCode128Symbology() {
208220
public static (List<bool>, string) Encode(string content, Code128CodeSet initialCodeSet, bool gs1)
209221
{
210222
List<bool> result = new List<bool>();
211-
var (data, interpretation) = Analyze(content, initialCodeSet);
223+
List<int> data;
224+
string interpretation;
225+
if (gs1)
226+
{
227+
(data, interpretation) = AnalyzeGS1(content);
228+
}
229+
else if (initialCodeSet == Code128CodeSet.Code128)
230+
{
231+
(data, interpretation) = AnalyzeAuto(content);
232+
}
233+
else
234+
{
235+
(data, interpretation) = Analyze(content, initialCodeSet);
236+
}
212237

213238
// TODO: magic constant FNC_1
214239
if (gs1 && data[1] != 102)
@@ -265,11 +290,6 @@ private static (List<int>, string) Analyze(string content, Code128CodeSet initia
265290
startCodeMatch = startCodeRegex.Match(content);
266291
}
267292

268-
if (codeSet == Code128CodeSet.Code128)
269-
{
270-
return AnalyzeAuto(content);
271-
}
272-
273293
(var codeChars, var codeMap) = codeMaps[codeSet];
274294

275295
data.Add((int)codeSet);
@@ -303,8 +323,14 @@ private static (List<int>, string) Analyze(string content, Code128CodeSet initia
303323
}
304324
else if (startCodeMap.ContainsKey(symbol))
305325
{
306-
codeSet = startCodeMap[symbol];
307-
(codeChars, codeMap) = codeMaps[codeSet];
326+
Code128CodeSet newCodeSet = startCodeMap[symbol];
327+
if (newCodeSet != codeSet)
328+
{
329+
int value = codeMap[codeSetCodeMap[newCodeSet]];
330+
data.Add(value);
331+
codeSet = newCodeSet;
332+
(codeChars, codeMap) = codeMaps[codeSet];
333+
}
308334
}
309335
else
310336
{
@@ -322,6 +348,8 @@ private static (List<int>, string) Analyze(string content, Code128CodeSet initia
322348
}
323349
else
324350
{
351+
int value = codeMap[CODE_B];
352+
data.Add(value);
325353
codeSet = Code128CodeSet.Code128B;
326354
(codeChars, codeMap) = codeMaps[codeSet];
327355
}
@@ -387,5 +415,58 @@ private static (List<int>, string) AnalyzeAuto(string content)
387415
return (data, interpretation);
388416
}
389417

418+
private static (List<int>, string) AnalyzeGS1(string content)
419+
{
420+
List<int> data = new List<int>();
421+
string interpretation = "";
422+
Code128CodeSet codeSet = Code128CodeSet.Code128B;
423+
var codeMap = codeBMap;
424+
if (gs1StartCRegex.IsMatch(content))
425+
{
426+
codeSet = Code128CodeSet.Code128C;
427+
codeMap = codeCMap;
428+
}
429+
data.Add((int)codeSet);
430+
431+
while (content.Length > 0)
432+
{
433+
if (codeSet != Code128CodeSet.Code128C && gs1SwitchCRegex.IsMatch(content))
434+
{
435+
data.Add(codeMap[CODE_C]);
436+
codeSet = Code128CodeSet.Code128C;
437+
codeMap = codeCMap;
438+
}
439+
else if (codeSet == Code128CodeSet.Code128C && !gs1DigitPairRegex.IsMatch(content))
440+
{
441+
data.Add(codeMap[CODE_B]);
442+
codeSet = Code128CodeSet.Code128B;
443+
codeMap = codeBMap;
444+
}
445+
else if (fnc1Regex.IsMatch(content))
446+
{
447+
content = content.Substring(2);
448+
data.Add(codeMap[FNC_1]);
449+
}
450+
else
451+
{
452+
string symbol = content[0].ToString();
453+
if (codeSet == Code128CodeSet.Code128C)
454+
{
455+
symbol += content[1];
456+
content = content.Substring(2);
457+
}
458+
else
459+
{
460+
content = content.Substring(1);
461+
}
462+
463+
data.Add(codeMap[symbol]);
464+
interpretation += symbol;
465+
}
466+
}
467+
468+
return (data, interpretation);
469+
}
470+
390471
}
391472
}

src/BinaryKits.Zpl.Viewer/VirtualPrinter.cs

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class VirtualPrinter
1010
public LabelPosition NextElementPosition { get; private set; }
1111
public FieldDataBase NextElementFieldData { get; private set; }
1212
public FieldBlock NextElementFieldBlock { get; private set; }
13+
public FieldOrientation FieldOrientation { get; private set; } = FieldOrientation.Normal;
1314
public int FontWidth { get; private set; } = 0;
1415
public int FontHeight { get; private set; } = 10;
1516
public string FontName { get; private set; } = "0";
@@ -103,6 +104,14 @@ public void ClearNextElementFieldUseHexadecimalIndicator()
103104
this.NextElementFieldUseHexadecimalIndicator = false;
104105
}
105106

107+
public void SetFieldOrientation(FieldOrientation fieldOrientation) {
108+
this.FieldOrientation = fieldOrientation;
109+
if (this.NextFont != null)
110+
{
111+
this.SetNextFont(this.NextFont.FontName, fieldOrientation, this.NextFont.FontWidth, this.NextFont.FontHeight);
112+
}
113+
}
114+
106115
public void SetFontWidth(int fontWidth)
107116
{
108117
this.FontWidth = fontWidth;

src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public AnalyzeInfo Analyze(string zplData)
5050
new DownloadObjectsZplCommandAnaylzer(this._virtualPrinter, this._printerStorage),
5151
new FieldBlockZplCommandAnalyzer(this._virtualPrinter),
5252
new FieldHexadecimalZplCommandAnalyzer(this._virtualPrinter),
53+
new FieldOrientationZplCommandAnalyzer(this._virtualPrinter),
5354
new FieldNumberCommandAnalyzer(this._virtualPrinter),
5455
new FieldVariableZplCommandAnalyzer(this._virtualPrinter),
5556
new FieldReversePrintZplCommandAnalyzer(this._virtualPrinter),

0 commit comments

Comments
 (0)