Skip to content

Commit

Permalink
Merge pull request #241 from BinaryKits/develop
Browse files Browse the repository at this point in the history
Merge develop branch
  • Loading branch information
primo-ppcg authored May 20, 2024
2 parents d6b7903 + 92fe196 commit a11ccd5
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/BinaryKits.Zpl.Label/BinaryKits.Zpl.Label.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net472;netstandard2.0;net6.0</TargetFrameworks>
<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>
<Company>Binary Kits Pte. Ltd.</Company>
<Version>3.2.0</Version>
<Version>3.2.1</Version>
<Authors>Binary Kits Pte. Ltd.</Authors>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageTags>Zebra ZPL ZPL2 Printer Label</PackageTags>
Expand Down
24 changes: 24 additions & 0 deletions src/BinaryKits.Zpl.Label/Elements/ZplFieldOrientation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;

namespace BinaryKits.Zpl.Label.Elements
{
/// <summary>
/// ^FW - Field Orientation
/// </summary>
public class ZplFieldOrientation : ZplElementBase
{
public FieldOrientation FieldOrientation { get; private set; }

public ZplFieldOrientation(FieldOrientation fieldOrientation)
{
this.FieldOrientation = fieldOrientation;
}

///<inheritdoc/>
public override IEnumerable<string> Render(ZplRenderOptions context)
{
return new[] { $"^FW{RenderFieldOrientation(this.FieldOrientation)}" };
}

}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
^XA

^LH0,0
^FWI

^A0N,50,0
^A0,50,0
^FO10,0
^FDFont1 Demo Text^FS

^A1N,50,0
^A1,50,0
^FO10,100
^FDFont2 Demo Text^FS

^AAN,80,0
^AA,80,0
^FO10,200
^FDFont3 Demo Text^FS

^ABN,50,0
^AB,50,0
^FO10,300
^FDFont4 Demo Text^FS

^ACN,20,0
^AC,20,0
^FO10,400
^FDFont5 Demo Text^FS

^ADN,0,20
^AD,0,20
^FO10,500
^FDFont6 Demo Text^FS

^ADN,20,20
^AD,20,20
^FO10,600
^FDFont7 Demo Text^FS

Expand Down
11 changes: 11 additions & 0 deletions src/BinaryKits.Zpl.Viewer.WebApi/Labels/Test/Format2-54x86.zpl2
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
^XA
^DFFORMAT^FS
^BY2,3.0^FO40,40^BCN,80,Y,,,A^FR^FN1^FS
^BY2,3.0^FO40,160^BCN,80,Y,,,A^FR^FN2^FS
^FN1^FD12345^FS
^XZ

^XA
^XFFORMAT.ZPL
^FN2^FDABC12345^FS
^XZ
2 changes: 1 addition & 1 deletion src/BinaryKits.Zpl.Viewer/BinaryKits.Zpl.Viewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net472;netstandard2.0;net6.0</TargetFrameworks>
<Description>This package provides a rendering logic for ZPL data, as an alternative to labelary.com.</Description>
<Company>Binary Kits Pte. Ltd.</Company>
<Version>1.2.0</Version>
<Version>1.2.1</Version>
<Authors>Binary Kits Pte. Ltd.</Authors>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageTags>Zebra ZPL ZPL2 ZPLEmulator ZPLVirtualPrinter ZPLViewer ZPLParser</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ private ZplFont GetFontFromVirtualPrinter()
int fontWidth = this.VirtualPrinter.FontWidth;
int fontHeight = this.VirtualPrinter.FontHeight;
string fontName = this.VirtualPrinter.FontName;
var fieldOrientation = this.VirtualPrinter.FieldOrientation;

return new ZplFont(fontWidth, fontHeight, fontName);
return new ZplFont(fontWidth, fontHeight, fontName, fieldOrientation);
}

private ZplFont GetNextFontFromVirtualPrinter()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using BinaryKits.Zpl.Label.Elements;

namespace BinaryKits.Zpl.Viewer.CommandAnalyzers
{
public class FieldOrientationZplCommandAnalyzer : ZplCommandAnalyzerBase
{
public FieldOrientationZplCommandAnalyzer(VirtualPrinter virtualPrinter) : base("^FW", virtualPrinter) { }

///<inheritdoc/>
public override ZplElementBase Analyze(string zplCommand)
{
var zplDataParts = this.SplitCommand(zplCommand);
if (zplDataParts.Length > 0)
{
var fieldOrientation = ConvertFieldOrientation(zplDataParts[0]);
this.VirtualPrinter.SetFieldOrientation(fieldOrientation);
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected FieldOrientation ConvertFieldOrientation(string fieldOrientation)
"R" => FieldOrientation.Rotated90,
"I" => FieldOrientation.Rotated180,
"B" => FieldOrientation.Rotated270,
_ => FieldOrientation.Normal,
_ => this.VirtualPrinter.FieldOrientation,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ public override void Draw(ZplElementBase element, DrawerOptions options)
}
else if (barcode.Mode == "D")
{
codeSet = Code128CodeSet.Code128C;
codeSet = Code128CodeSet.Code128;
gs1 = true;
}
else if (barcode.Mode == "U")
{
codeSet = Code128CodeSet.Code128C;
gs1 = true;
content = content.PadLeft(19, '0').Substring(0, 19);
int checksum = 0;
for (int i = 0; i < 19; i++)
Expand Down
21 changes: 16 additions & 5 deletions src/BinaryKits.Zpl.Viewer/FormatMerger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,27 @@ private static List<ZplElementBase> GetMergedElements(
{
var elements = new List<ZplElementBase>();

foreach (ZplElementBase element in rawLabelInfo.ZplElements)
foreach (ZplElementBase zplElement in rawLabelInfo.ZplElements)
{
if (element is ZplRecallFormat recallFormat)
if (zplElement is ZplRecallFormat recallFormat)
{
LabelInfo formatLabelInfo = GetFormatLabelInfo(recallFormat, templateFormats);
elements.AddRange(GetMergedElements(rawLabelInfo, formatLabelInfo));
}
else if (element is not ZplRecallFieldNumber)
else if (zplElement is ZplRecallFieldNumber recallFieldNumber)
{
elements.Add(element);
for(int i = 0; i < elements.Count; i++)
{
if (elements[i] is ZplFieldNumber fieldNumber && fieldNumber.Number == recallFieldNumber.Number)
{
((IFormatElement)fieldNumber.FormatElement).SetTemplateContent(recallFieldNumber.Text);
elements[i] = fieldNumber.FormatElement;
}
}
}
else
{
elements.Add(zplElement);
}
}

Expand Down Expand Up @@ -103,4 +114,4 @@ private static IEnumerable<ZplElementBase> GetMergedElements(
}
}
}
}
}
97 changes: 89 additions & 8 deletions src/BinaryKits.Zpl.Viewer/Symologies/ZplCode128Symbology.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public class ZplCode128Symbology
private static readonly Regex startCRegex = new Regex(@"^\d{4}", RegexOptions.Compiled);
private static readonly Regex digitPairRegex = new Regex(@"^\d\d", RegexOptions.Compiled);

// GS1 specific
private static readonly Regex gs1SwitchCRegex = new Regex(@"^\d\d(?:\d\d|>8)+(?!\d)", RegexOptions.Compiled);
private static readonly Regex gs1StartCRegex = new Regex(@"^(?:\d\d|>8){2}", RegexOptions.Compiled);
private static readonly Regex gs1DigitPairRegex = new Regex(@"^(?:\d\d|>8)", RegexOptions.Compiled);
private static readonly Regex fnc1Regex = new Regex(@"^>8", RegexOptions.Compiled);

private static readonly Regex startCodeRegex = new Regex(@"^(>[9:;])(.+)$", RegexOptions.Compiled);

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

private static readonly Dictionary<Code128CodeSet, string> codeSetCodeMap = new Dictionary<Code128CodeSet, string>() {
{ Code128CodeSet.Code128A, CODE_A },
{ Code128CodeSet.Code128B, CODE_B },
{ Code128CodeSet.Code128C, CODE_C }
};

private const string FNC_1 = "FNC_1";
private const string FNC_2 = "FNC_2";
private const string FNC_3 = "FNC_3";
Expand Down Expand Up @@ -208,7 +220,20 @@ static ZplCode128Symbology() {
public static (List<bool>, string) Encode(string content, Code128CodeSet initialCodeSet, bool gs1)
{
List<bool> result = new List<bool>();
var (data, interpretation) = Analyze(content, initialCodeSet);
List<int> data;
string interpretation;
if (gs1)
{
(data, interpretation) = AnalyzeGS1(content);
}
else if (initialCodeSet == Code128CodeSet.Code128)
{
(data, interpretation) = AnalyzeAuto(content);
}
else
{
(data, interpretation) = Analyze(content, initialCodeSet);
}

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

if (codeSet == Code128CodeSet.Code128)
{
return AnalyzeAuto(content);
}

(var codeChars, var codeMap) = codeMaps[codeSet];

data.Add((int)codeSet);
Expand Down Expand Up @@ -303,8 +323,14 @@ private static (List<int>, string) Analyze(string content, Code128CodeSet initia
}
else if (startCodeMap.ContainsKey(symbol))
{
codeSet = startCodeMap[symbol];
(codeChars, codeMap) = codeMaps[codeSet];
Code128CodeSet newCodeSet = startCodeMap[symbol];
if (newCodeSet != codeSet)
{
int value = codeMap[codeSetCodeMap[newCodeSet]];
data.Add(value);
codeSet = newCodeSet;
(codeChars, codeMap) = codeMaps[codeSet];
}
}
else
{
Expand All @@ -322,6 +348,8 @@ private static (List<int>, string) Analyze(string content, Code128CodeSet initia
}
else
{
int value = codeMap[CODE_B];
data.Add(value);
codeSet = Code128CodeSet.Code128B;
(codeChars, codeMap) = codeMaps[codeSet];
}
Expand Down Expand Up @@ -387,5 +415,58 @@ private static (List<int>, string) AnalyzeAuto(string content)
return (data, interpretation);
}

private static (List<int>, string) AnalyzeGS1(string content)
{
List<int> data = new List<int>();
string interpretation = "";
Code128CodeSet codeSet = Code128CodeSet.Code128B;
var codeMap = codeBMap;
if (gs1StartCRegex.IsMatch(content))
{
codeSet = Code128CodeSet.Code128C;
codeMap = codeCMap;
}
data.Add((int)codeSet);

while (content.Length > 0)
{
if (codeSet != Code128CodeSet.Code128C && gs1SwitchCRegex.IsMatch(content))
{
data.Add(codeMap[CODE_C]);
codeSet = Code128CodeSet.Code128C;
codeMap = codeCMap;
}
else if (codeSet == Code128CodeSet.Code128C && !gs1DigitPairRegex.IsMatch(content))
{
data.Add(codeMap[CODE_B]);
codeSet = Code128CodeSet.Code128B;
codeMap = codeBMap;
}
else if (fnc1Regex.IsMatch(content))
{
content = content.Substring(2);
data.Add(codeMap[FNC_1]);
}
else
{
string symbol = content[0].ToString();
if (codeSet == Code128CodeSet.Code128C)
{
symbol += content[1];
content = content.Substring(2);
}
else
{
content = content.Substring(1);
}

data.Add(codeMap[symbol]);
interpretation += symbol;
}
}

return (data, interpretation);
}

}
}
9 changes: 9 additions & 0 deletions src/BinaryKits.Zpl.Viewer/VirtualPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class VirtualPrinter
public LabelPosition NextElementPosition { get; private set; }
public FieldDataBase NextElementFieldData { get; private set; }
public FieldBlock NextElementFieldBlock { get; private set; }
public FieldOrientation FieldOrientation { get; private set; } = FieldOrientation.Normal;
public int FontWidth { get; private set; } = 0;
public int FontHeight { get; private set; } = 10;
public string FontName { get; private set; } = "0";
Expand Down Expand Up @@ -103,6 +104,14 @@ public void ClearNextElementFieldUseHexadecimalIndicator()
this.NextElementFieldUseHexadecimalIndicator = false;
}

public void SetFieldOrientation(FieldOrientation fieldOrientation) {
this.FieldOrientation = fieldOrientation;
if (this.NextFont != null)
{
this.SetNextFont(this.NextFont.FontName, fieldOrientation, this.NextFont.FontWidth, this.NextFont.FontHeight);
}
}

public void SetFontWidth(int fontWidth)
{
this.FontWidth = fontWidth;
Expand Down
1 change: 1 addition & 0 deletions src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public AnalyzeInfo Analyze(string zplData)
new DownloadObjectsZplCommandAnaylzer(this._virtualPrinter, this._printerStorage),
new FieldBlockZplCommandAnalyzer(this._virtualPrinter),
new FieldHexadecimalZplCommandAnalyzer(this._virtualPrinter),
new FieldOrientationZplCommandAnalyzer(this._virtualPrinter),
new FieldNumberCommandAnalyzer(this._virtualPrinter),
new FieldVariableZplCommandAnalyzer(this._virtualPrinter),
new FieldReversePrintZplCommandAnalyzer(this._virtualPrinter),
Expand Down

0 comments on commit a11ccd5

Please sign in to comment.