From cb8b70e2b494c573f8ce221a3ddbe14bf41656b9 Mon Sep 17 00:00:00 2001 From: primo-ppcg Date: Mon, 20 May 2024 17:56:48 +0700 Subject: [PATCH] Implement `^FW` (#240) --- .../Elements/ZplFieldOrientation.cs | 24 +++++++++++++++++++ .../Labels/Test/FieldDataText2-102x152.zpl2 | 15 ++++++------ .../FieldDataZplCommandAnalyzer.cs | 3 ++- .../FieldOrientationZplCommandAnalyzer.cs | 22 +++++++++++++++++ .../ZplCommandAnalyzerBase.cs | 2 +- src/BinaryKits.Zpl.Viewer/VirtualPrinter.cs | 9 +++++++ src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs | 1 + 7 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 src/BinaryKits.Zpl.Label/Elements/ZplFieldOrientation.cs create mode 100644 src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldOrientationZplCommandAnalyzer.cs diff --git a/src/BinaryKits.Zpl.Label/Elements/ZplFieldOrientation.cs b/src/BinaryKits.Zpl.Label/Elements/ZplFieldOrientation.cs new file mode 100644 index 00000000..e7290894 --- /dev/null +++ b/src/BinaryKits.Zpl.Label/Elements/ZplFieldOrientation.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; + +namespace BinaryKits.Zpl.Label.Elements +{ + /// + /// ^FW - Field Orientation + /// + public class ZplFieldOrientation : ZplElementBase + { + public FieldOrientation FieldOrientation { get; private set; } + + public ZplFieldOrientation(FieldOrientation fieldOrientation) + { + this.FieldOrientation = fieldOrientation; + } + + /// + public override IEnumerable Render(ZplRenderOptions context) + { + return new[] { $"^FW{RenderFieldOrientation(this.FieldOrientation)}" }; + } + + } +} diff --git a/src/BinaryKits.Zpl.Viewer.WebApi/Labels/Test/FieldDataText2-102x152.zpl2 b/src/BinaryKits.Zpl.Viewer.WebApi/Labels/Test/FieldDataText2-102x152.zpl2 index aad65dbc..f7585e61 100644 --- a/src/BinaryKits.Zpl.Viewer.WebApi/Labels/Test/FieldDataText2-102x152.zpl2 +++ b/src/BinaryKits.Zpl.Viewer.WebApi/Labels/Test/FieldDataText2-102x152.zpl2 @@ -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 diff --git a/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldDataZplCommandAnalyzer.cs b/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldDataZplCommandAnalyzer.cs index 3f0b9f68..1f7406a6 100644 --- a/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldDataZplCommandAnalyzer.cs +++ b/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldDataZplCommandAnalyzer.cs @@ -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() diff --git a/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldOrientationZplCommandAnalyzer.cs b/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldOrientationZplCommandAnalyzer.cs new file mode 100644 index 00000000..c98a8cbc --- /dev/null +++ b/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldOrientationZplCommandAnalyzer.cs @@ -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) { } + + /// + 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; + } + } +} diff --git a/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/ZplCommandAnalyzerBase.cs b/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/ZplCommandAnalyzerBase.cs index ac7c7e8d..6ad19467 100644 --- a/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/ZplCommandAnalyzerBase.cs +++ b/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/ZplCommandAnalyzerBase.cs @@ -37,7 +37,7 @@ protected FieldOrientation ConvertFieldOrientation(string fieldOrientation) "R" => FieldOrientation.Rotated90, "I" => FieldOrientation.Rotated180, "B" => FieldOrientation.Rotated270, - _ => FieldOrientation.Normal, + _ => this.VirtualPrinter.FieldOrientation, }; } diff --git a/src/BinaryKits.Zpl.Viewer/VirtualPrinter.cs b/src/BinaryKits.Zpl.Viewer/VirtualPrinter.cs index 2bfe8964..5b119927 100644 --- a/src/BinaryKits.Zpl.Viewer/VirtualPrinter.cs +++ b/src/BinaryKits.Zpl.Viewer/VirtualPrinter.cs @@ -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"; @@ -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; diff --git a/src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs b/src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs index e816499d..fb79d976 100644 --- a/src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs +++ b/src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs @@ -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),