Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added: Barcode93 support #226

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/BinaryKits.Zpl.Label.UnitTest/BarcodeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ public void Barcode39()
Assert.AreEqual("^XA\n^LH0,0\n^CI28\n\n^FO100,100\n^BY2,3\n^B3N,N,100,Y,N\n^FD123ABC^FS\n^XZ", output);
}

[TestMethod]
public void Barcode93()
{
var elements = new List<ZplElementBase>
{
new ZplBarcode93("123ABC", 100, 300)
};

var renderEngine = new ZplEngine(elements);
var output = renderEngine.ToZplString(new ZplRenderOptions { AddEmptyLineBeforeElementStart = true });

Debug.WriteLine(output);
Assert.IsNotNull(output);
Assert.AreEqual("^XA\n^LH0,0\n^CI28\n\n^FO100,300\n^BY2,3\n^BAN,100,Y,N,N\n^FD123ABC^FS\n^XZ", output);
}

[TestMethod]
public void Barcode128()
{
Expand Down
70 changes: 70 additions & 0 deletions src/BinaryKits.Zpl.Label/Elements/ZplBarcode93.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Collections.Generic;

namespace BinaryKits.Zpl.Label.Elements
{
/// <summary>
/// Code 93 Barcode
/// </summary>
public class ZplBarcode93 : ZplBarcode
{
public bool CheckDigit { get; private set; }

/// <summary>
/// Code 93 Barcode
/// </summary>
/// <param name="content"></param>
/// <param name="positionX"></param>
/// <param name="positionY"></param>
/// <param name="height"></param>
/// <param name="moduleWidth"></param>
/// <param name="wideBarToNarrowBarWidthRatio"></param>
/// <param name="fieldOrientation"></param>
/// <param name="printInterpretationLine"></param>
/// <param name="printInterpretationLineAboveCode"></param>
/// <param name="bottomToTop"></param>
/// <param name="checkDigit"></param>
/// <param name="mode"></param>

Check warning on line 26 in src/BinaryKits.Zpl.Label/Elements/ZplBarcode93.cs

View workflow job for this annotation

GitHub Actions / build-windows

XML comment has a param tag for 'mode', but there is no parameter by that name

Check warning on line 26 in src/BinaryKits.Zpl.Label/Elements/ZplBarcode93.cs

View workflow job for this annotation

GitHub Actions / build-windows

XML comment has a param tag for 'mode', but there is no parameter by that name

Check warning on line 26 in src/BinaryKits.Zpl.Label/Elements/ZplBarcode93.cs

View workflow job for this annotation

GitHub Actions / build-windows

XML comment has a param tag for 'mode', but there is no parameter by that name

Check warning on line 26 in src/BinaryKits.Zpl.Label/Elements/ZplBarcode93.cs

View workflow job for this annotation

GitHub Actions / build-linux

XML comment has a param tag for 'mode', but there is no parameter by that name

Check warning on line 26 in src/BinaryKits.Zpl.Label/Elements/ZplBarcode93.cs

View workflow job for this annotation

GitHub Actions / build-linux

XML comment has a param tag for 'mode', but there is no parameter by that name

Check warning on line 26 in src/BinaryKits.Zpl.Label/Elements/ZplBarcode93.cs

View workflow job for this annotation

GitHub Actions / build-linux

XML comment has a param tag for 'mode', but there is no parameter by that name
public ZplBarcode93(
string content,
int positionX,
int positionY,
int height = 100,
int moduleWidth = 2,
double wideBarToNarrowBarWidthRatio = 3,
FieldOrientation fieldOrientation = FieldOrientation.Normal,
bool printInterpretationLine = true,
bool printInterpretationLineAboveCode = false,
bool checkDigit = false,
bool bottomToTop = false)
: base(content,
positionX,
positionY,
height,
moduleWidth,
wideBarToNarrowBarWidthRatio,
fieldOrientation,
printInterpretationLine,
printInterpretationLineAboveCode,
bottomToTop)
{
this.CheckDigit = checkDigit;
}

///<inheritdoc/>
public override IEnumerable<string> Render(ZplRenderOptions context)
{
//TODO:Add 'mode'

//^FO100,100 ^ BY3
//^BAN,100,Y,N,N
//^FD123456 ^ FS
var result = new List<string>();
result.AddRange(RenderPosition(context));
result.Add(RenderModuleWidth());
result.Add($"^BA{RenderFieldOrientation()},{context.Scale(Height)},{RenderPrintInterpretationLine()},{RenderPrintInterpretationLineAboveCode()},{(CheckDigit ? "Y" : "N")}");
result.Add($"^FD{Content}^FS");

return result;
}
}
}
11 changes: 6 additions & 5 deletions src/BinaryKits.Zpl.TestConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ static string RenderLabel3()
{
new ZplBarcode128("Barcode128", 10, 0),
new ZplBarcode39("Barcode39", 10, 150),
new ZplBarcodeAnsiCodabar("123456", 'a', 'd', 10, 300, 100),
new ZplBarcodeEan13("123456789", 10, 450),
new ZplBarcodeInterleaved2of5("123456789", 10, 600),
new ZplQrCode("BinaryKits ZplUtility BinaryKits ZplUtility BinaryKits ZplUtility", 10, 800, magnificationFactor: 6)
new ZplBarcode93("Barcode93", 10, 300),
new ZplBarcodeAnsiCodabar("123456", 'a', 'd', 10, 450, 100),
new ZplBarcodeEan13("123456789", 10, 600),
new ZplBarcodeInterleaved2of5("123456789", 10, 750),
new ZplQrCode("BinaryKits ZplUtility BinaryKits ZplUtility BinaryKits ZplUtility", 10, 950, magnificationFactor: 6)
};

var renderEngine = new ZplEngine(elements);
Expand Down Expand Up @@ -192,7 +193,7 @@ static string RenderLabel6()
new ZplTextBlock(text, 10, 120, 400, 100, font1, NewLineConversionMethod.ToSpace),

new ZplTextBlock(text, 10, 240, 400, 100, font2, NewLineConversionMethod.ToEmpty),

new ZplFieldBlock(text, 10, 360, 400, font1, 4)
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
^XA

^FO10,10
^BY3,2
^BAN,100,Y,N
^FD123ABC^FS

^FO10,160
^BY4,2
^BAN,100,Y,N
^FD123ABC^FS

^FO10,320
^BY5,2
^BAN,100,Y,N
^FD123ABC^FS

^XZ
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using BinaryKits.Zpl.Label.Elements;
using BinaryKits.Zpl.Viewer.Models;

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

///<inheritdoc/>
public override ZplElementBase Analyze(string zplCommand)
{
var zplDataParts = this.SplitCommand(zplCommand);

int tmpint;
bool printCheckDigit = false;
int height = this.VirtualPrinter.BarcodeInfo.Height;
bool printInterpretationLine = true;
bool printInterpretationLineAboveCode = false;

var fieldOrientation = this.ConvertFieldOrientation(zplDataParts[0]);
if (zplDataParts.Length > 1 && int.TryParse(zplDataParts[1], out tmpint))
{
height = tmpint;
}

if (zplDataParts.Length > 2)
{
printInterpretationLine = this.ConvertBoolean(zplDataParts[2], "Y");
}

if (zplDataParts.Length > 3)
{
printInterpretationLineAboveCode = this.ConvertBoolean(zplDataParts[3]);
}

if (zplDataParts.Length > 4)
{
printCheckDigit = this.ConvertBoolean(zplDataParts[4]);
}

//The field data are processing in the FieldDataZplCommandAnalyzer
this.VirtualPrinter.SetNextElementFieldData(new Code93BarcodeFieldData
{
FieldOrientation = fieldOrientation,
Height = height,
PrintInterpretationLine = printInterpretationLine,
PrintInterpretationLineAboveCode = printInterpretationLineAboveCode,
PrintCheckDigit = printCheckDigit
});

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class FieldDataZplCommandAnalyzer : ZplCommandAnalyzerBase
private static readonly Regex qrCodeFieldDataMixedRegex = new Regex(@"^D\d{4}[0-9A-F-a-f]{2},(?<correction>[HQML])(?<input>[AM]),(?<data>.+)$", RegexOptions.Compiled);
private static readonly Regex qrCodeFieldDataModeRegex = new Regex(@"^(?:[ANK]|(?:B(?<count>\d{4})))(?<data>.+)$", RegexOptions.Compiled);

public FieldDataZplCommandAnalyzer(VirtualPrinter virtualPrinter, string prefix="^FD") : base(prefix, virtualPrinter) { }
public FieldDataZplCommandAnalyzer(VirtualPrinter virtualPrinter, string prefix = "^FD") : base(prefix, virtualPrinter) { }

///<inheritdoc/>
public override ZplElementBase Analyze(string zplCommand)
Expand Down Expand Up @@ -53,6 +53,10 @@ public override ZplElementBase Analyze(string zplCommand)
{
return new ZplBarcode39(text, x, y, code39.Height, moduleWidth, wideBarToNarrowBarWidthRatio, code39.FieldOrientation, code39.PrintInterpretationLine, code39.PrintInterpretationLineAboveCode, code39.Mod43CheckDigit, bottomToTop: bottomToTop);
}
if (this.VirtualPrinter.NextElementFieldData is Code93BarcodeFieldData code93)
{
return new ZplBarcode93(text, x, y, code93.Height, moduleWidth, wideBarToNarrowBarWidthRatio, code93.FieldOrientation, code93.PrintInterpretationLine, code93.PrintInterpretationLineAboveCode, code93.PrintCheckDigit, bottomToTop: bottomToTop);
}
if (this.VirtualPrinter.NextElementFieldData is Code128BarcodeFieldData code128)
{
return new ZplBarcode128(text, x, y, code128.Height, moduleWidth, wideBarToNarrowBarWidthRatio, code128.FieldOrientation, code128.PrintInterpretationLine, code128.PrintInterpretationLineAboveCode, bottomToTop, code128.Mode);
Expand Down Expand Up @@ -103,7 +107,7 @@ public override ZplElementBase Analyze(string zplCommand)
int lineSpace = this.VirtualPrinter.NextElementFieldBlock.AddOrDeleteSpaceBetweenLines;
int hangingIndent = this.VirtualPrinter.NextElementFieldBlock.HangingIndentOfTheSecondAndRemainingLines;

return new ZplFieldBlock(text, x, y, width, font, maxLineCount, lineSpace, textJustification, hangingIndent, reversePrint : reversePrint, bottomToTop: bottomToTop);
return new ZplFieldBlock(text, x, y, width, font, maxLineCount, lineSpace, textJustification, hangingIndent, reversePrint: reversePrint, bottomToTop: bottomToTop);
}

return new ZplTextField(text, x, y, font, reversePrint: reversePrint, bottomToTop: bottomToTop);
Expand Down
56 changes: 56 additions & 0 deletions src/BinaryKits.Zpl.Viewer/ElementDrawers/Barcode93ElementDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using BarcodeLib;
using BinaryKits.Zpl.Label.Elements;
using BinaryKits.Zpl.Viewer.Helpers;
using SkiaSharp;
using System;
using System.Drawing;

namespace BinaryKits.Zpl.Viewer.ElementDrawers
{
public class Barcode93ElementDrawer : BarcodeDrawerBase
{
///<inheritdoc/>
public override bool CanDraw(ZplElementBase element)
{
return element is ZplBarcode93;
}

///<inheritdoc/>
public override void Draw(ZplElementBase element)
{
Draw(element, new DrawerOptions());
}

///<inheritdoc/>
public override void Draw(ZplElementBase element, DrawerOptions options)
{
if (element is ZplBarcode93 barcode)
{
float x = barcode.PositionX;
float y = barcode.PositionY;

var content = barcode.Content;

float labelFontSize = Math.Min(barcode.ModuleWidth * 7.2f, 72f);
var labelTypeFace = options.FontLoader("A");
var labelFont = new SKFont(labelTypeFace, labelFontSize).ToSystemDrawingFont();
int labelHeight = barcode.PrintInterpretationLine ? labelFont.Height : 0;
int labelHeightOffset = barcode.PrintInterpretationLineAboveCode ? labelHeight : 0;

var barcodeElement = new Barcode
{
BarWidth = barcode.ModuleWidth,
BackColor = Color.Transparent,
Height = barcode.Height + labelHeight,
IncludeLabel = barcode.PrintInterpretationLine,
LabelPosition = barcode.PrintInterpretationLineAboveCode ? LabelPositions.TOPCENTER : LabelPositions.BOTTOMCENTER,
LabelFont = labelFont,
AlternateLabel = content
};

using var image = barcodeElement.Encode(TYPE.CODE93, content);
this.DrawBarcode(this.GetImageData(image), barcode.Height, image.Width, barcode.FieldOrigin != null, x, y, labelHeightOffset, barcode.FieldOrientation);
}
}
}
}
13 changes: 13 additions & 0 deletions src/BinaryKits.Zpl.Viewer/Models/Code93BarcodeFieldData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using BinaryKits.Zpl.Label;

namespace BinaryKits.Zpl.Viewer.Models
{
internal class Code93BarcodeFieldData : FieldDataBase
{
public FieldOrientation FieldOrientation { get; set; }
public int Height { get; set; }
public bool PrintInterpretationLine { get; set; }
public bool PrintInterpretationLineAboveCode { get; set; }
public bool PrintCheckDigit { get; set; }
}
}
1 change: 1 addition & 0 deletions src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public AnalyzeInfo Analyze(string zplData)
new BarCodeFieldDefaultZplCommandAnalyzer(this._virtualPrinter),
new ChangeAlphanumericDefaultFontZplCommandAnalyzer(this._virtualPrinter),
new Code39BarcodeZplCommandAnalyzer(this._virtualPrinter),
new Code93BarcodeZplCommandAnalyzer(this._virtualPrinter),
new Code128BarcodeZplCommandAnalyzer(this._virtualPrinter),
new CodeEAN13BarcodeZplCommandAnalyzer(this._virtualPrinter),
new CommentZplCommandAnalyzer(this._virtualPrinter),
Expand Down
Loading
Loading