Skip to content

Commit

Permalink
Merge pull request #82 from remco1271/master
Browse files Browse the repository at this point in the history
Add support for Field Hexadecimal Indicator(^FH)
  • Loading branch information
YipingRuan committed Mar 7, 2022
2 parents ba3da45 + b8a6147 commit c1593b3
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
@@ -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;
}
}
}
@@ -1,5 +1,6 @@
using BinaryKits.Zpl.Label;
using BinaryKits.Zpl.Label.Elements;
using BinaryKits.Zpl.Viewer.Helpers;
using SkiaSharp;
using System;

Expand Down Expand Up @@ -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)
{
Expand Down
@@ -1,4 +1,5 @@
using BinaryKits.Zpl.Label.Elements;
using BinaryKits.Zpl.Viewer.Helpers;
using SkiaSharp;

namespace BinaryKits.Zpl.Viewer.ElementDrawers
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
43 changes: 43 additions & 0 deletions 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
{
/// <summary>
/// The hexadecimal indicator from the ^FH
/// </summary>
public static char ReplaceChar { get; set; } = '_';

/// <summary>
/// Search for the Hexadecimal indicator and replaces it with the HEX char
/// </summary>
/// <param name="text">String to search in</param>
/// <returns>Output string</returns>
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();
}
}
}
1 change: 1 addition & 0 deletions src/BinaryKits.Zpl.Viewer/ZplAnalyzer.cs
Expand Up @@ -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),
Expand Down

0 comments on commit c1593b3

Please sign in to comment.