diff --git a/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldHexadecimalZplCommandAnalyzer.cs b/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldHexadecimalZplCommandAnalyzer.cs new file mode 100644 index 0000000..9c406a4 --- /dev/null +++ b/src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldHexadecimalZplCommandAnalyzer.cs @@ -0,0 +1,24 @@ +using BinaryKits.Zpl.Label.Elements; +using BinaryKits.Zpl.Viewer.Helpers; + +namespace BinaryKits.Zpl.Viewer.CommandAnalyzers +{ + public class FieldHexadecimalZplCommandAnalyzer : ZplCommandAnalyzerBase + { + public FieldHexadecimalZplCommandAnalyzer(VirtualPrinter virtualPrinter) : base("^FH", virtualPrinter) + { } + public override ZplElementBase Analyze(string zplCommand) + { + var zplDataParts = this.SplitCommand(zplCommand); + + char Indicator = '_'; + + if (zplDataParts.Length > 0) + { + Indicator = zplDataParts[0][0]; + } + StringHelper.ReplaceChar = Indicator; + return null; + } + } +} \ No newline at end of file diff --git a/src/BinaryKits.Zpl.Viewer/ElementDrawers/FieldBlockElementDrawer.cs b/src/BinaryKits.Zpl.Viewer/ElementDrawers/FieldBlockElementDrawer.cs index 1351fb0..3039cb5 100644 --- a/src/BinaryKits.Zpl.Viewer/ElementDrawers/FieldBlockElementDrawer.cs +++ b/src/BinaryKits.Zpl.Viewer/ElementDrawers/FieldBlockElementDrawer.cs @@ -1,5 +1,6 @@ using BinaryKits.Zpl.Label; using BinaryKits.Zpl.Label.Elements; +using BinaryKits.Zpl.Viewer.Helpers; using SkiaSharp; using System; @@ -49,7 +50,7 @@ public override void Draw(ZplElementBase element) typeface = SKTypeface.FromFamilyName("Arial", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright); } - var textLines = fieldBlock.Text.Split(new[] { "\\&" }, StringSplitOptions.RemoveEmptyEntries); + var textLines = fieldBlock.Text.ReplaceSpecialChars().Split(new[] { "\\&" }, StringSplitOptions.RemoveEmptyEntries); foreach (var textLine in textLines) { diff --git a/src/BinaryKits.Zpl.Viewer/ElementDrawers/TextFieldElementDrawer.cs b/src/BinaryKits.Zpl.Viewer/ElementDrawers/TextFieldElementDrawer.cs index 1697074..8ffcd64 100644 --- a/src/BinaryKits.Zpl.Viewer/ElementDrawers/TextFieldElementDrawer.cs +++ b/src/BinaryKits.Zpl.Viewer/ElementDrawers/TextFieldElementDrawer.cs @@ -1,4 +1,5 @@ using BinaryKits.Zpl.Label.Elements; +using BinaryKits.Zpl.Viewer.Helpers; using SkiaSharp; namespace BinaryKits.Zpl.Viewer.ElementDrawers @@ -55,8 +56,9 @@ public override void Draw(ZplElementBase element) var textBounds = new SKRect(); var textBoundBaseline = new SKRect(); - skPaint.MeasureText(new string('A', textField.Text.Length), ref textBoundBaseline); - skPaint.MeasureText(textField.Text, ref textBounds); + string DisplayText = textField.Text.ReplaceSpecialChars(); + skPaint.MeasureText(new string('A', DisplayText.Length), ref textBoundBaseline); + skPaint.MeasureText(DisplayText, ref textBounds); if (textField.FieldTypeset != null) { @@ -117,7 +119,7 @@ public override void Draw(ZplElementBase element) this._skCanvas.SetMatrix(matrix); } - this._skCanvas.DrawText(textField.Text, x, y, new SKFont(typeface, fontSize, scaleX, 0), skPaint); + this._skCanvas.DrawText(DisplayText, x, y, new SKFont(typeface, fontSize, scaleX, 0), skPaint); } } } diff --git a/src/BinaryKits.Zpl.Viewer/Helpers/StringHelper.cs b/src/BinaryKits.Zpl.Viewer/Helpers/StringHelper.cs new file mode 100644 index 0000000..aec8b9a --- /dev/null +++ b/src/BinaryKits.Zpl.Viewer/Helpers/StringHelper.cs @@ -0,0 +1,43 @@ +using System; +using System.Globalization; +using System.Linq; +using System.Text; + +namespace BinaryKits.Zpl.Viewer.Helpers +{ + public static class StringHelper + { + /// + /// The hexadecimal indicator from the ^FH + /// + public static char ReplaceChar { get; set; } = '_'; + + /// + /// Search for the Hexadecimal indicator and replaces it with the HEX char + /// + /// String to search in + /// Output string + public static string ReplaceSpecialChars(this string text) + { + if (!text.Contains(ReplaceChar)) + return text; + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < text.Length; i++) + { + if (text[i] == ReplaceChar) + { + string temp = text.Substring(i + 1, 2); + sb.Append((char)Int16.Parse(temp, NumberStyles.AllowHexSpecifier)); + i = i + 2; + } + else + { + sb.Append(text[i]); + } + + } + return sb.ToString(); + } + } +} diff --git a/src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs b/src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs index 3fb4ac7..84b5692 100644 --- a/src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs +++ b/src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs @@ -39,6 +39,7 @@ public AnalyzeInfo Analyze(string zplData) new DownloadObjectsZplCommandAnaylzer(this._virtualPrinter, this._printerStorage), new FieldBlockZplCommandAnalyzer(this._virtualPrinter), new FieldDataZplCommandAnalyzer(this._virtualPrinter), + new FieldHexadecimalZplCommandAnalyzer(this._virtualPrinter), new FieldReversePrintZplCommandAnalyzer(this._virtualPrinter), new LabelReversePrintZplCommandAnalyzer(this._virtualPrinter), new FieldSeparatorZplCommandAnalyzer(this._virtualPrinter),