Skip to content

Commit a034c4b

Browse files
authored
Add Field Justification (#247)
Add field justification parameter for `^FW`, `^FO`, and `^FT`. Fixes #245
1 parent a11ccd5 commit a034c4b

15 files changed

+157
-24
lines changed

src/BinaryKits.Zpl.Label/Elements/ZplElementBase.cs

+17
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ public string RenderFieldOrientation(FieldOrientation fieldOrientation)
6161
throw new NotImplementedException("Unknown Field Orientation");
6262
}
6363

64+
public string RenderFieldJustification(FieldJustification fieldJustification)
65+
{
66+
switch (fieldJustification)
67+
{
68+
case FieldJustification.None:
69+
return string.Empty;
70+
case FieldJustification.Left:
71+
return "0";
72+
case FieldJustification.Right:
73+
return "1";
74+
case FieldJustification.Auto:
75+
return "2";
76+
}
77+
78+
throw new NotImplementedException("Unknown Field Justification");
79+
}
80+
6481
public string RenderLineColor(LineColor lineColor)
6582
{
6683
switch (lineColor)

src/BinaryKits.Zpl.Label/Elements/ZplFieldOrientation.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,23 @@ namespace BinaryKits.Zpl.Label.Elements
88
public class ZplFieldOrientation : ZplElementBase
99
{
1010
public FieldOrientation FieldOrientation { get; private set; }
11+
public FieldJustification FieldJustification { get; private set; }
1112

12-
public ZplFieldOrientation(FieldOrientation fieldOrientation)
13+
/// <summary>
14+
/// Field Orientation
15+
/// </summary>
16+
/// <param name="fieldOrientation"></param>
17+
/// <param name="fieldJustification"></param>
18+
public ZplFieldOrientation(FieldOrientation fieldOrientation, FieldJustification fieldJustification = FieldJustification.None)
1319
{
1420
this.FieldOrientation = fieldOrientation;
21+
this.FieldJustification = fieldJustification;
1522
}
1623

1724
///<inheritdoc/>
1825
public override IEnumerable<string> Render(ZplRenderOptions context)
1926
{
20-
return new[] { $"^FW{RenderFieldOrientation(this.FieldOrientation)}" };
27+
return new[] { $"^FW{RenderFieldOrientation(this.FieldOrientation)},{RenderFieldJustification(this.FieldJustification)}".TrimEnd(',') };
2128
}
2229

2330
}

src/BinaryKits.Zpl.Label/Elements/ZplFieldOrigin.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,26 @@ public class ZplFieldOrigin : ZplElementBase
1414
{
1515
public int PositionX { get; protected set; }
1616
public int PositionY { get; protected set; }
17+
public FieldJustification FieldJustification { get; protected set; }
1718

1819
/// <summary>
1920
/// Field Origin
2021
/// </summary>
2122
/// <param name="positionX">X Position (0-32000)</param>
2223
/// <param name="positionY">Y Position (0-32000)</param>
23-
public ZplFieldOrigin(int positionX, int positionY)
24+
/// <param name="fieldJustification"></param>
25+
public ZplFieldOrigin(int positionX, int positionY, FieldJustification fieldJustification = FieldJustification.None)
2426
{
25-
PositionX = positionX;
26-
PositionY = positionY;
27+
this.PositionX = positionX;
28+
this.PositionY = positionY;
29+
this.FieldJustification = fieldJustification;
2730
}
2831

2932
///<inheritdoc/>
3033
public override IEnumerable<string> Render(ZplRenderOptions context)
3134
{
3235
//^FO50,50
33-
return new string[] { $"^FO{context.Scale(PositionX)},{context.Scale(PositionY)}" };
36+
return new string[] { $"^FO{context.Scale(this.PositionX)},{context.Scale(this.PositionY)},{RenderFieldJustification(this.FieldJustification)}".TrimEnd(',') };
3437
}
3538

3639
/// <summary>
@@ -41,7 +44,7 @@ public override IEnumerable<string> Render(ZplRenderOptions context)
4144
/// <returns></returns>
4245
public ZplFieldOrigin Offset(int offsetX, int offsetY)
4346
{
44-
return new ZplFieldOrigin(PositionX + offsetX, PositionY + offsetY);
47+
return new ZplFieldOrigin(this.PositionX + offsetX, this.PositionY + offsetY, this.FieldJustification);
4548
}
4649
}
4750
}

src/BinaryKits.Zpl.Label/Elements/ZplFieldTypeset.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,26 @@ public class ZplFieldTypeset : ZplElementBase
1616
{
1717
public int PositionX { get; protected set; }
1818
public int PositionY { get; protected set; }
19+
public FieldJustification FieldJustification { get; protected set; }
1920

2021
/// <summary>
2122
/// Field Typeset
2223
/// </summary>
2324
/// <param name="positionX">X Position (0-32000)</param>
2425
/// <param name="positionY">Y Position (0-32000)</param>
25-
public ZplFieldTypeset(int positionX, int positionY)
26+
/// <param name="fieldJustification"></param>
27+
public ZplFieldTypeset(int positionX, int positionY, FieldJustification fieldJustification = FieldJustification.None)
2628
{
27-
PositionX = positionX;
28-
PositionY = positionY;
29+
this.PositionX = positionX;
30+
this.PositionY = positionY;
31+
this.FieldJustification = fieldJustification;
2932
}
3033

3134
///<inheritdoc/>
3235
public override IEnumerable<string> Render(ZplRenderOptions context)
3336
{
3437
//^FO50,50
35-
return new string[] { $"^FT{context.Scale(PositionX)},{context.Scale(PositionY)}" };
38+
return new string[] { $"^FT{context.Scale(this.PositionX)},{context.Scale(this.PositionY)},{RenderFieldJustification(this.FieldJustification)}".TrimEnd(',') };
3639
}
3740

3841
/// <summary>
@@ -43,7 +46,7 @@ public override IEnumerable<string> Render(ZplRenderOptions context)
4346
/// <returns></returns>
4447
public ZplFieldTypeset Offset(int offsetX, int offsetY)
4548
{
46-
return new ZplFieldTypeset(PositionX + offsetX, PositionY + offsetY);
49+
return new ZplFieldTypeset(this.PositionX + offsetX, this.PositionY + offsetY, this.FieldJustification);
4750
}
4851
}
4952
}

src/BinaryKits.Zpl.Label/Elements/ZplPositionedElementBase.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ public abstract class ZplPositionedElementBase : ZplElementBase
1616
/// <param name="positionX"></param>
1717
/// <param name="positionY"></param>
1818
/// <param name="bottomToTop">Use FieldTypeset</param>
19-
public ZplPositionedElementBase(int positionX, int positionY, bool bottomToTop = false) : base()
19+
/// <param name="fieldJustification"></param>
20+
public ZplPositionedElementBase(int positionX, int positionY, bool bottomToTop = false, FieldJustification fieldJustification = FieldJustification.None) : base()
2021
{
2122
if (bottomToTop)
2223
{
23-
FieldTypeset = new ZplFieldTypeset(positionX, positionY);
24+
FieldTypeset = new ZplFieldTypeset(positionX, positionY, fieldJustification);
2425
PositionX = positionX;
2526
PositionY = positionY;
2627
return;
2728
}
2829

29-
FieldOrigin = new ZplFieldOrigin(positionX, positionY);
30+
FieldOrigin = new ZplFieldOrigin(positionX, positionY, fieldJustification);
3031
PositionX = positionX;
3132
PositionY = positionY;
3233
}

src/BinaryKits.Zpl.Label/Elements/ZplTextField.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class ZplTextField : ZplPositionedElementBase, IFormatElement
3030
/// <param name="useHexadecimalIndicator"></param>
3131
/// <param name="reversePrint"></param>
3232
/// <param name="bottomToTop"></param>
33+
/// <param name="fieldJustification"></param>
3334
public ZplTextField(
3435
string text,
3536
int positionX,
@@ -38,8 +39,9 @@ public ZplTextField(
3839
NewLineConversionMethod newLineConversion = NewLineConversionMethod.ToSpace,
3940
bool useHexadecimalIndicator = true,
4041
bool reversePrint = false,
41-
bool bottomToTop = false)
42-
: base(positionX, positionY, bottomToTop)
42+
bool bottomToTop = false,
43+
FieldJustification fieldJustification = FieldJustification.None)
44+
: base(positionX, positionY, bottomToTop, fieldJustification)
4345
{
4446
Text = text;
4547
Font = font;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace BinaryKits.Zpl.Label
2+
{
3+
/// <summary>
4+
/// Text Alignment
5+
/// </summary>
6+
public enum FieldJustification
7+
{
8+
/// <summary>
9+
/// None
10+
/// </summary>
11+
None,
12+
/// <summary>
13+
/// Left
14+
/// </summary>
15+
Left,
16+
/// <summary>
17+
/// Right
18+
/// </summary>
19+
Right,
20+
/// <summary>
21+
/// Auto (script dependent)
22+
/// </summary>
23+
Auto
24+
}
25+
}

src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldDataZplCommandAnalyzer.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public override ZplElementBase Analyze(string zplCommand)
3434
int x = 0;
3535
int y = 0;
3636
bool bottomToTop = false;
37+
var fieldJustification = this.VirtualPrinter.NextElementFieldJustification;
38+
if (fieldJustification == FieldJustification.None)
39+
{
40+
fieldJustification = this.VirtualPrinter.FieldJustification;
41+
}
3742

3843
if (this.VirtualPrinter.NextElementPosition != null)
3944
{
@@ -110,7 +115,7 @@ public override ZplElementBase Analyze(string zplCommand)
110115
return new ZplFieldBlock(text, x, y, width, font, maxLineCount, lineSpace, textJustification, hangingIndent, reversePrint: reversePrint, bottomToTop: bottomToTop);
111116
}
112117

113-
return new ZplTextField(text, x, y, font, reversePrint: reversePrint, bottomToTop: bottomToTop);
118+
return new ZplTextField(text, x, y, font, reversePrint: reversePrint, bottomToTop: bottomToTop, fieldJustification: fieldJustification);
114119
}
115120

116121
private (ErrorCorrectionLevel, string) ParseQrCodeFieldData(QrCodeBarcodeFieldData qrCode, string text)

src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldOrientationZplCommandAnalyzer.cs

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ public override ZplElementBase Analyze(string zplCommand)
1616
this.VirtualPrinter.SetFieldOrientation(fieldOrientation);
1717
}
1818

19+
if (zplDataParts.Length > 1)
20+
{
21+
var fieldJustification = ConvertFieldJustification(zplDataParts[1]);
22+
this.VirtualPrinter.SetFieldJustification(fieldJustification);
23+
}
24+
1925
return null;
2026
}
2127
}

src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldOriginZplCommandAnalzer.cs

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public override ZplElementBase Analyze(string zplCommand)
2727
y = tmpint;
2828
}
2929

30+
if (zplDataParts.Length > 2)
31+
{
32+
var fieldJustification = ConvertFieldJustification(zplDataParts[2]);
33+
this.VirtualPrinter.SetNextElementFieldJustification(fieldJustification);
34+
}
35+
3036
if (this.VirtualPrinter.LabelHomePosition != null)
3137
{
3238
x += this.VirtualPrinter.LabelHomePosition.X;

src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldSeparatorZplCommandAnalyzer.cs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public override ZplElementBase Analyze(string zplCommand)
3131
this.VirtualPrinter.ClearNextElementFieldData();
3232
this.VirtualPrinter.ClearNextElementFieldReverse();
3333
this.VirtualPrinter.ClearNextElementFieldUseHexadecimalIndicator();
34+
this.VirtualPrinter.ClearNextElementFieldJustification();
3435
this.VirtualPrinter.ClearNextFont();
3536
this.VirtualPrinter.ClearComments();
3637

src/BinaryKits.Zpl.Viewer/CommandAnalyzers/FieldTypesetZplCommandAnalyzer.cs

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public override ZplElementBase Analyze(string zplCommand)
2727
y = tmpint;
2828
}
2929

30+
if (zplDataParts.Length > 2)
31+
{
32+
var fieldJustification = ConvertFieldJustification(zplDataParts[2]);
33+
this.VirtualPrinter.SetNextElementFieldJustification(fieldJustification);
34+
}
35+
3036
if (this.VirtualPrinter.LabelHomePosition != null)
3137
{
3238
x += this.VirtualPrinter.LabelHomePosition.X;

src/BinaryKits.Zpl.Viewer/CommandAnalyzers/ZplCommandAnalyzerBase.cs

+11
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ protected FieldOrientation ConvertFieldOrientation(string fieldOrientation)
4141
};
4242
}
4343

44+
protected FieldJustification ConvertFieldJustification(string fieldJustification)
45+
{
46+
return fieldJustification switch
47+
{
48+
"0" => FieldJustification.Left,
49+
"1" => FieldJustification.Right,
50+
"2" => FieldJustification.Auto,
51+
_ => this.VirtualPrinter.FieldJustification,
52+
};
53+
}
54+
4455
protected ErrorCorrectionLevel ConvertErrorCorrectionLevel(string errorCorrection)
4556
{
4657
return errorCorrection switch

src/BinaryKits.Zpl.Viewer/ElementDrawers/TextFieldElementDrawer.cs

+23
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public override void Draw(ZplElementBase element, DrawerOptions options)
3030
{
3131
float x = textField.PositionX;
3232
float y = textField.PositionY;
33+
var fieldJustification = Label.FieldJustification.None;
3334

3435
var font = textField.Font;
3536

@@ -84,6 +85,7 @@ public override void Draw(ZplElementBase element, DrawerOptions options)
8485
case Label.FieldOrientation.Normal:
8586
break;
8687
}
88+
fieldJustification = textField.FieldOrigin.FieldJustification;
8789
}
8890
else
8991
{
@@ -101,6 +103,7 @@ public override void Draw(ZplElementBase element, DrawerOptions options)
101103
case Label.FieldOrientation.Normal:
102104
break;
103105
}
106+
fieldJustification = textField.FieldTypeset.FieldJustification;
104107
}
105108

106109
if (matrix != SKMatrix.Empty)
@@ -118,7 +121,27 @@ public override void Draw(ZplElementBase element, DrawerOptions options)
118121
skPaint.BlendMode = SKBlendMode.Xor;
119122
}
120123

124+
if (fieldJustification == Label.FieldJustification.Left)
125+
{
126+
skPaint.TextAlign = SKTextAlign.Left;
127+
}
128+
else if (fieldJustification == Label.FieldJustification.Right)
129+
{
130+
skPaint.TextAlign = SKTextAlign.Right;
131+
}
132+
else if (fieldJustification == Label.FieldJustification.Auto)
133+
{
134+
var buffer = new HarfBuzzSharp.Buffer();
135+
buffer.AddUtf16(displayText);
136+
buffer.GuessSegmentProperties();
137+
if (buffer.Direction == HarfBuzzSharp.Direction.RightToLeft)
138+
{
139+
skPaint.TextAlign = SKTextAlign.Right;
140+
}
141+
}
142+
121143
this._skCanvas.DrawShapedText(displayText, x, y, skPaint);
144+
122145
}
123146
}
124147
}

0 commit comments

Comments
 (0)