Skip to content

Commit

Permalink
Add Field Justification
Browse files Browse the repository at this point in the history
Add field justification parameter for `^FW`, `^FO`, and `^FT`.
  • Loading branch information
primo-ppcg committed May 28, 2024
1 parent a11ccd5 commit cbcf6e3
Show file tree
Hide file tree
Showing 15 changed files with 157 additions and 24 deletions.
17 changes: 17 additions & 0 deletions src/BinaryKits.Zpl.Label/Elements/ZplElementBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ public string RenderFieldOrientation(FieldOrientation fieldOrientation)
throw new NotImplementedException("Unknown Field Orientation");
}

public string RenderFieldJustification(FieldJustification fieldJustification)
{
switch (fieldJustification)
{
case FieldJustification.None:
return string.Empty;
case FieldJustification.Left:
return "0";
case FieldJustification.Right:
return "1";
case FieldJustification.Auto:
return "2";
}

throw new NotImplementedException("Unknown Field Justification");
}

public string RenderLineColor(LineColor lineColor)
{
switch (lineColor)
Expand Down
11 changes: 9 additions & 2 deletions src/BinaryKits.Zpl.Label/Elements/ZplFieldOrientation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@ namespace BinaryKits.Zpl.Label.Elements
public class ZplFieldOrientation : ZplElementBase
{
public FieldOrientation FieldOrientation { get; private set; }
public FieldJustification FieldJustification { get; private set; }

public ZplFieldOrientation(FieldOrientation fieldOrientation)
/// <summary>
/// Field Orientation
/// </summary>
/// <param name="fieldOrientation"></param>
/// <param name="fieldJustification"></param>
public ZplFieldOrientation(FieldOrientation fieldOrientation, FieldJustification fieldJustification = FieldJustification.None)
{
this.FieldOrientation = fieldOrientation;
this.FieldJustification = fieldJustification;
}

///<inheritdoc/>
public override IEnumerable<string> Render(ZplRenderOptions context)
{
return new[] { $"^FW{RenderFieldOrientation(this.FieldOrientation)}" };
return new[] { $"^FW{RenderFieldOrientation(this.FieldOrientation)},{RenderFieldJustification(this.FieldJustification)}" };
}

}
Expand Down
13 changes: 8 additions & 5 deletions src/BinaryKits.Zpl.Label/Elements/ZplFieldOrigin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,26 @@ public class ZplFieldOrigin : ZplElementBase
{
public int PositionX { get; protected set; }
public int PositionY { get; protected set; }
public FieldJustification FieldJustification { get; protected set; }

/// <summary>
/// Field Origin
/// </summary>
/// <param name="positionX">X Position (0-32000)</param>
/// <param name="positionY">Y Position (0-32000)</param>
public ZplFieldOrigin(int positionX, int positionY)
/// <param name="fieldJustification"></param>
public ZplFieldOrigin(int positionX, int positionY, FieldJustification fieldJustification = FieldJustification.None)
{
PositionX = positionX;
PositionY = positionY;
this.PositionX = positionX;
this.PositionY = positionY;
this.FieldJustification = fieldJustification;
}

///<inheritdoc/>
public override IEnumerable<string> Render(ZplRenderOptions context)
{
//^FO50,50
return new string[] { $"^FO{context.Scale(PositionX)},{context.Scale(PositionY)}" };
return new string[] { $"^FO{context.Scale(this.PositionX)},{context.Scale(this.PositionY)},{RenderFieldJustification(this.FieldJustification)}" };
}

/// <summary>
Expand All @@ -41,7 +44,7 @@ public override IEnumerable<string> Render(ZplRenderOptions context)
/// <returns></returns>
public ZplFieldOrigin Offset(int offsetX, int offsetY)
{
return new ZplFieldOrigin(PositionX + offsetX, PositionY + offsetY);
return new ZplFieldOrigin(this.PositionX + offsetX, this.PositionY + offsetY, this.FieldJustification);
}
}
}
13 changes: 8 additions & 5 deletions src/BinaryKits.Zpl.Label/Elements/ZplFieldTypeset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,26 @@ public class ZplFieldTypeset : ZplElementBase
{
public int PositionX { get; protected set; }
public int PositionY { get; protected set; }
public FieldJustification FieldJustification { get; protected set; }

/// <summary>
/// Field Typeset
/// </summary>
/// <param name="positionX">X Position (0-32000)</param>
/// <param name="positionY">Y Position (0-32000)</param>
public ZplFieldTypeset(int positionX, int positionY)
/// <param name="fieldJustification"></param>
public ZplFieldTypeset(int positionX, int positionY, FieldJustification fieldJustification = FieldJustification.None)
{
PositionX = positionX;
PositionY = positionY;
this.PositionX = positionX;
this.PositionY = positionY;
this.FieldJustification = fieldJustification;
}

///<inheritdoc/>
public override IEnumerable<string> Render(ZplRenderOptions context)
{
//^FO50,50
return new string[] { $"^FT{context.Scale(PositionX)},{context.Scale(PositionY)}" };
return new string[] { $"^FT{context.Scale(this.PositionX)},{context.Scale(this.PositionY)},{RenderFieldJustification(this.FieldJustification)}" };
}

/// <summary>
Expand All @@ -43,7 +46,7 @@ public override IEnumerable<string> Render(ZplRenderOptions context)
/// <returns></returns>
public ZplFieldTypeset Offset(int offsetX, int offsetY)
{
return new ZplFieldTypeset(PositionX + offsetX, PositionY + offsetY);
return new ZplFieldTypeset(this.PositionX + offsetX, this.PositionY + offsetY, this.FieldJustification);
}
}
}
7 changes: 4 additions & 3 deletions src/BinaryKits.Zpl.Label/Elements/ZplPositionedElementBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ public abstract class ZplPositionedElementBase : ZplElementBase
/// <param name="positionX"></param>
/// <param name="positionY"></param>
/// <param name="bottomToTop">Use FieldTypeset</param>
public ZplPositionedElementBase(int positionX, int positionY, bool bottomToTop = false) : base()
/// <param name="fieldJustification"></param>
public ZplPositionedElementBase(int positionX, int positionY, bool bottomToTop = false, FieldJustification fieldJustification = FieldJustification.None) : base()
{
if (bottomToTop)
{
FieldTypeset = new ZplFieldTypeset(positionX, positionY);
FieldTypeset = new ZplFieldTypeset(positionX, positionY, fieldJustification);
PositionX = positionX;
PositionY = positionY;
return;
}

FieldOrigin = new ZplFieldOrigin(positionX, positionY);
FieldOrigin = new ZplFieldOrigin(positionX, positionY, fieldJustification);
PositionX = positionX;
PositionY = positionY;
}
Expand Down
6 changes: 4 additions & 2 deletions src/BinaryKits.Zpl.Label/Elements/ZplTextField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ZplTextField : ZplPositionedElementBase, IFormatElement
/// <param name="useHexadecimalIndicator"></param>
/// <param name="reversePrint"></param>
/// <param name="bottomToTop"></param>
/// <param name="fieldJustification"></param>
public ZplTextField(
string text,
int positionX,
Expand All @@ -38,8 +39,9 @@ public ZplTextField(
NewLineConversionMethod newLineConversion = NewLineConversionMethod.ToSpace,
bool useHexadecimalIndicator = true,
bool reversePrint = false,
bool bottomToTop = false)
: base(positionX, positionY, bottomToTop)
bool bottomToTop = false,
FieldJustification fieldJustification = FieldJustification.None)
: base(positionX, positionY, bottomToTop, fieldJustification)
{
Text = text;
Font = font;
Expand Down
25 changes: 25 additions & 0 deletions src/BinaryKits.Zpl.Label/FieldJustification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace BinaryKits.Zpl.Label
{
/// <summary>
/// Text Alignment
/// </summary>
public enum FieldJustification
{
/// <summary>
/// None
/// </summary>
None,
/// <summary>
/// Left
/// </summary>
Left,
/// <summary>
/// Right
/// </summary>
Right,
/// <summary>
/// Auto (script dependent)
/// </summary>
Auto
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public override ZplElementBase Analyze(string zplCommand)
int x = 0;
int y = 0;
bool bottomToTop = false;
var fieldJustification = this.VirtualPrinter.NextElementFieldJustification;
if (fieldJustification == FieldJustification.None)
{
fieldJustification = this.VirtualPrinter.FieldJustification;
}

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

return new ZplTextField(text, x, y, font, reversePrint: reversePrint, bottomToTop: bottomToTop);
return new ZplTextField(text, x, y, font, reversePrint: reversePrint, bottomToTop: bottomToTop, fieldJustification: fieldJustification);
}

private (ErrorCorrectionLevel, string) ParseQrCodeFieldData(QrCodeBarcodeFieldData qrCode, string text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public override ZplElementBase Analyze(string zplCommand)
this.VirtualPrinter.SetFieldOrientation(fieldOrientation);
}

if (zplDataParts.Length > 1)
{
var fieldJustification = ConvertFieldJustification(zplDataParts[1]);
this.VirtualPrinter.SetFieldJustification(fieldJustification);
}

return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public override ZplElementBase Analyze(string zplCommand)
y = tmpint;
}

if (zplDataParts.Length > 2)
{
var fieldJustification = ConvertFieldJustification(zplDataParts[2]);
this.VirtualPrinter.SetNextElementFieldJustification(fieldJustification);
}

if (this.VirtualPrinter.LabelHomePosition != null)
{
x += this.VirtualPrinter.LabelHomePosition.X;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public override ZplElementBase Analyze(string zplCommand)
this.VirtualPrinter.ClearNextElementFieldData();
this.VirtualPrinter.ClearNextElementFieldReverse();
this.VirtualPrinter.ClearNextElementFieldUseHexadecimalIndicator();
this.VirtualPrinter.ClearNextElementFieldJustification();
this.VirtualPrinter.ClearNextFont();
this.VirtualPrinter.ClearComments();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public override ZplElementBase Analyze(string zplCommand)
y = tmpint;
}

if (zplDataParts.Length > 2)
{
var fieldJustification = ConvertFieldJustification(zplDataParts[2]);
this.VirtualPrinter.SetNextElementFieldJustification(fieldJustification);
}

if (this.VirtualPrinter.LabelHomePosition != null)
{
x += this.VirtualPrinter.LabelHomePosition.X;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ protected FieldOrientation ConvertFieldOrientation(string fieldOrientation)
};
}

protected FieldJustification ConvertFieldJustification(string fieldJustification)
{
return fieldJustification switch
{
"0" => FieldJustification.Left,
"1" => FieldJustification.Right,
"2" => FieldJustification.Auto,
_ => this.VirtualPrinter.FieldJustification,
};
}

protected ErrorCorrectionLevel ConvertErrorCorrectionLevel(string errorCorrection)
{
return errorCorrection switch
Expand Down
23 changes: 23 additions & 0 deletions src/BinaryKits.Zpl.Viewer/ElementDrawers/TextFieldElementDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public override void Draw(ZplElementBase element, DrawerOptions options)
{
float x = textField.PositionX;
float y = textField.PositionY;
var fieldJustification = Label.FieldJustification.None;

var font = textField.Font;

Expand Down Expand Up @@ -84,6 +85,7 @@ public override void Draw(ZplElementBase element, DrawerOptions options)
case Label.FieldOrientation.Normal:
break;
}
fieldJustification = textField.FieldOrigin.FieldJustification;
}
else
{
Expand All @@ -101,6 +103,7 @@ public override void Draw(ZplElementBase element, DrawerOptions options)
case Label.FieldOrientation.Normal:
break;
}
fieldJustification = textField.FieldTypeset.FieldJustification;
}

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

if (fieldJustification == Label.FieldJustification.Left)
{
skPaint.TextAlign = SKTextAlign.Left;
}
else if (fieldJustification == Label.FieldJustification.Right)
{
skPaint.TextAlign = SKTextAlign.Right;
}
else if (fieldJustification == Label.FieldJustification.Auto)
{
var buffer = new HarfBuzzSharp.Buffer();
buffer.AddUtf16(displayText);
buffer.GuessSegmentProperties();
if (buffer.Direction == HarfBuzzSharp.Direction.RightToLeft)
{
skPaint.TextAlign = SKTextAlign.Right;
}
}

this._skCanvas.DrawShapedText(displayText, x, y, skPaint);

}
}
}
Expand Down
Loading

0 comments on commit cbcf6e3

Please sign in to comment.