printer-api/Instructions/TextInstruction.cs
2022-02-28 01:25:55 +01:00

78 lines
1.6 KiB
C#

using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using ESCPOS_NET.Emitters;
public class TextInstruction : BaseInstruction
{
[DataMember(IsRequired = true)]
public string? text { get; set; }
[JsonConverter(typeof(JsonStringEnumConverter))]
public Alignment alignment {get; set; } = Alignment.Left;
public bool underline {get; set; } = false;
public bool bold {get; set; } = false;
public bool doubleHeight {get; set; } = false;
public bool doubleWidth {get; set; } = false;
public bool fontB {get; set; } = false;
public int lineSpacing {get; set; } = -1;
public bool newLine { get; set; } = true;
public override List<byte[]> GetInstructions()
{
var commands = new List<byte[]>();
commands.Add(alignmentCommand(alignment));
commands.AddRange(setStyles());
if (lineSpacing >= 0){
commands.Add(e.SetLineSpacingInDots(lineSpacing));
}
if (newLine)
{
commands.Add(e.PrintLine(text));
}
else
{
commands.Add(e.Print(text));
}
if (underline || bold || doubleHeight || doubleWidth || fontB){
commands.Add(e.SetStyles(PrintStyle.None));
}
if (lineSpacing >= 0){
commands.Add(e.ResetLineSpacing());
}
return commands;
}
private List<byte[]> setStyles(){
var commands = new List<byte[]>();
if (underline){
commands.Add(e.SetStyles(PrintStyle.Underline));
}
if (bold){
commands.Add(e.SetStyles(PrintStyle.Bold));
}
if (doubleHeight){
commands.Add(e.SetStyles(PrintStyle.DoubleHeight));
}
if (doubleWidth){
commands.Add(e.SetStyles(PrintStyle.DoubleWidth));
}
if (fontB){
commands.Add(e.SetStyles(PrintStyle.FontB));
}
return commands;
}
}