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

Add ANSI Codabar to BinaryKits.Zpl.Viewer #258

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/BinaryKits.Zpl.Viewer.UnitTest/DrawerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ public void InvertColor()
Common.DefaultPrint(test2, "inverted2.png");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using BinaryKits.Zpl.Label.Elements;
using SkiaSharp;
using System;
using ZXing.OneD;

namespace BinaryKits.Zpl.Viewer.ElementDrawers
{
/// <summary>
/// Drawer for Code 39 Barcode elements
/// </summary>
public class BarcodeAnsiCodabarElementDrawer : BarcodeDrawerBase
{
///<inheritdoc/>
public override bool CanDraw(ZplElementBase element)
{
return element is ZplBarcodeAnsiCodabar;
}

///<inheritdoc/>
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);
}
}
}
}
}
14 changes: 14 additions & 0 deletions src/BinaryKits.Zpl.Viewer/Models/AnsiCodabarFieldData.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
1 change: 1 addition & 0 deletions src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LabelInfo>();
Expand Down
3 changes: 2 additions & 1 deletion src/BinaryKits.Zpl.Viewer/ZplElementDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public ZplElementDrawer(IPrinterStorage printerStorage, DrawerOptions drawerOpti
new Pdf417ElementDrawer(),
new QrCodeElementDrawer(),
new RecallGraphicElementDrawer(),
new TextFieldElementDrawer()
new TextFieldElementDrawer(),
new BarcodeAnsiCodabarElementDrawer(),
};
}

Expand Down
Loading