From b537ed5100852def0ac546f51a9ff46cf33bed94 Mon Sep 17 00:00:00 2001 From: Wendelin Niesl Date: Fri, 21 Jun 2024 14:07:56 +0200 Subject: [PATCH] Add ANSI Codabar Command Analyzer, Field Data and element drawers --- .../DrawerTest.cs | 2 +- .../AnsiCodabarBarcodeZplCommandAnalyzer.cs | 67 +++++++++++++++++++ .../FieldDataZplCommandAnalyzer.cs | 7 +- .../BarcodeAnsiCodabarElementDrawer.cs | 49 ++++++++++++++ .../Models/AnsiCodabarFieldData.cs | 14 ++++ src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs | 1 + src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs | 3 +- 7 files changed, 138 insertions(+), 5 deletions(-) create mode 100644 src/BinaryKits.Zpl.Viewer/CommandAnalyzers/AnsiCodabarBarcodeZplCommandAnalyzer.cs create mode 100644 src/BinaryKits.Zpl.Viewer/ElementDrawers/BarcodeAnsiCodabarElementDrawer.cs create mode 100644 src/BinaryKits.Zpl.Viewer/Models/AnsiCodabarFieldData.cs diff --git a/src/BinaryKits.Zpl.Viewer.UnitTest/DrawerTest.cs b/src/BinaryKits.Zpl.Viewer.UnitTest/DrawerTest.cs index 84fce35..fbda544 100644 --- a/src/BinaryKits.Zpl.Viewer.UnitTest/DrawerTest.cs +++ b/src/BinaryKits.Zpl.Viewer.UnitTest/DrawerTest.cs @@ -52,4 +52,4 @@ public void InvertColor() Common.DefaultPrint(test2, "inverted2.png"); } } -} \ No newline at end of file +} diff --git a/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/AnsiCodabarBarcodeZplCommandAnalyzer.cs b/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/AnsiCodabarBarcodeZplCommandAnalyzer.cs new file mode 100644 index 0000000..cd12cdb --- /dev/null +++ b/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/AnsiCodabarBarcodeZplCommandAnalyzer.cs @@ -0,0 +1,67 @@ +using BinaryKits.Zpl.Label.Elements; +using BinaryKits.Zpl.Viewer.Models; + +namespace BinaryKits.Zpl.Viewer.CommandAnalyzers; + +public class AnsiCodabarBarcodeZplCommandAnalyzer : ZplCommandAnalyzerBase +{ + public AnsiCodabarBarcodeZplCommandAnalyzer(VirtualPrinter virtualPrinter) : base("^BK", virtualPrinter) { } + + public override ZplElementBase Analyze(string zplCommand) + { + var zplDataParts = this.SplitCommand(zplCommand); + + bool checkDigit = false; + int height = this.VirtualPrinter.BarcodeInfo.Height; + bool printInterpretationLine = true; + bool printInterpretationLineAboveCode = false; + char startCharacter = 'A'; + char stopCharacter = 'A'; + + var fieldOrientation = this.ConvertFieldOrientation(zplDataParts[0]); + + if (zplDataParts.Length > 1) + { + checkDigit = this.ConvertBoolean(zplDataParts[1], "Y"); + } + + if (zplDataParts.Length > 2 && int.TryParse(zplDataParts[2], out int tmpint)) + { + height = tmpint; + } + + if (zplDataParts.Length > 3) + { + printInterpretationLine = this.ConvertBoolean(zplDataParts[3], "Y"); + } + + if (zplDataParts.Length > 4) + { + printInterpretationLineAboveCode = this.ConvertBoolean(zplDataParts[4]); + } + + if (zplDataParts.Length > 5 && char.TryParse(zplDataParts[6], out char startChar)) + { + startCharacter = startChar; + } + + if (zplDataParts.Length > 6 && char.TryParse(zplDataParts[6], out char stopChar)) + { + stopCharacter = stopChar; + } + + //The field data are processing in the FieldDataZplCommandAnalyzer + this.VirtualPrinter.SetNextElementFieldData(new AnsiCodabarFieldData + { + FieldOrientation = fieldOrientation, + StartCharacter = startCharacter, + StopCharacter = stopCharacter, + Height = height, + CheckDigit = checkDigit, + PrintInterpretationLine = printInterpretationLine, + PrintInterpretationLineAboveCode = printInterpretationLineAboveCode, + }); + + return null; + } +} diff --git a/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldDataZplCommandAnalyzer.cs b/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldDataZplCommandAnalyzer.cs index e58d47b..eb9464f 100644 --- a/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldDataZplCommandAnalyzer.cs +++ b/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldDataZplCommandAnalyzer.cs @@ -1,12 +1,9 @@ using System; -using System.Linq; using System.Text; using System.Text.RegularExpressions; using BinaryKits.Zpl.Label; using BinaryKits.Zpl.Label.Elements; using BinaryKits.Zpl.Viewer.Models; -using ZXing; -using ZXing.Datamatrix.Encoder; namespace BinaryKits.Zpl.Viewer.CommandAnalyzers { @@ -98,6 +95,10 @@ public override ZplElementBase Analyze(string zplCommand) { return new ZplAztecBarcode(text, x, y, aztec.MagnificationFactor, aztec.ExtendedChannel, aztec.ErrorControl, aztec.MenuSymbol, aztec.SymbolCount, aztec.IdField, useHexadecimalIndicator, aztec.FieldOrientation, bottomToTop); } + if (this.VirtualPrinter.NextElementFieldData is AnsiCodabarFieldData codabar) + { + return new ZplBarcodeAnsiCodabar(text, codabar.StartCharacter, codabar.StopCharacter, x, y, codabar.Height, moduleWidth, wideBarToNarrowBarWidthRatio, codabar.FieldOrientation, codabar.PrintInterpretationLine, codabar.PrintInterpretationLineAboveCode, codabar.CheckDigit, bottomToTop); + } } var font = this.GetFontFromVirtualPrinter(); diff --git a/src/BinaryKits.Zpl.Viewer/ElementDrawers/BarcodeAnsiCodabarElementDrawer.cs b/src/BinaryKits.Zpl.Viewer/ElementDrawers/BarcodeAnsiCodabarElementDrawer.cs new file mode 100644 index 0000000..0a99793 --- /dev/null +++ b/src/BinaryKits.Zpl.Viewer/ElementDrawers/BarcodeAnsiCodabarElementDrawer.cs @@ -0,0 +1,49 @@ +using BinaryKits.Zpl.Label.Elements; +using SkiaSharp; +using System; +using ZXing.OneD; + +namespace BinaryKits.Zpl.Viewer.ElementDrawers +{ + /// + /// Drawer for Code 39 Barcode elements + /// + public class BarcodeAnsiCodabarElementDrawer : BarcodeDrawerBase + { + /// + public override bool CanDraw(ZplElementBase element) + { + return element is ZplBarcodeAnsiCodabar; + } + + /// + public override void Draw(ZplElementBase element, DrawerOptions options) + { + if (element is ZplBarcodeAnsiCodabar barcode) + { + float x = barcode.PositionX; + float y = barcode.PositionY; + + var content = barcode.Content.Trim('*'); + var interpretation = string.Format("*{0}*", content); + + var writer = new CodaBarWriter(); + var result = writer.encode(content); + int narrow = barcode.ModuleWidth; + int wide = (int)Math.Floor(barcode.WideBarToNarrowBarWidthRatio * narrow); + result = this.AdjustWidths(result, wide, narrow); + using var resizedImage = this.BoolArrayToSKBitmap(result, barcode.Height); + var 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 = Math.Min(barcode.ModuleWidth * 10f, 100f); + var labelTypeFace = options.FontLoader("A"); + var labelFont = new SKFont(labelTypeFace, labelFontSize); + this.DrawInterpretationLine(interpretation, labelFont, x, y, resizedImage.Width, resizedImage.Height, barcode.FieldOrigin != null, barcode.FieldOrientation, barcode.PrintInterpretationLineAboveCode, options); + } + } + } + } +} diff --git a/src/BinaryKits.Zpl.Viewer/Models/AnsiCodabarFieldData.cs b/src/BinaryKits.Zpl.Viewer/Models/AnsiCodabarFieldData.cs new file mode 100644 index 0000000..99cc3be --- /dev/null +++ b/src/BinaryKits.Zpl.Viewer/Models/AnsiCodabarFieldData.cs @@ -0,0 +1,14 @@ +using BinaryKits.Zpl.Label; + +namespace BinaryKits.Zpl.Viewer.Models; + +public class AnsiCodabarFieldData : FieldDataBase +{ + public FieldOrientation FieldOrientation { get; set; } + public int Height { get; set; } + public bool PrintInterpretationLine { get; set; } + public bool PrintInterpretationLineAboveCode { get; set; } + public bool CheckDigit { get; set; } + public char StartCharacter { get; set; } + public char StopCharacter { get; set; } +} \ No newline at end of file diff --git a/src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs b/src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs index 2b31075..9387952 100644 --- a/src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs +++ b/src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs @@ -71,6 +71,7 @@ public AnalyzeInfo Analyze(string zplData) new RecallFormatCommandAnalyzer(this._virtualPrinter), new RecallGraphicZplCommandAnalyzer(this._virtualPrinter), new ScalableBitmappedFontZplCommandAnalyzer(this._virtualPrinter), + new AnsiCodabarBarcodeZplCommandAnalyzer(this._virtualPrinter), }; var labelInfos = new List(); diff --git a/src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs b/src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs index 70f2c5d..5be6fb8 100644 --- a/src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs +++ b/src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs @@ -40,7 +40,8 @@ public ZplElementDrawer(IPrinterStorage printerStorage, DrawerOptions drawerOpti new Pdf417ElementDrawer(), new QrCodeElementDrawer(), new RecallGraphicElementDrawer(), - new TextFieldElementDrawer() + new TextFieldElementDrawer(), + new BarcodeAnsiCodabarElementDrawer(), }; }