Initial commit

Initial commit til Git.
V2 er deployed
This commit is contained in:
2026-06-13 17:31:50 +02:00
parent 9fcd2b145e
commit 41e23b6184
375 changed files with 15956 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
using ESCPOS_NET.Emitters.BaseCommandValues;
namespace ESCPOS_NET.Emitters
{
public abstract partial class BaseCommandEmitter : ICommandEmitter
{
/* Action Commands */
public virtual byte[] FullCut() => new byte[] { Cmd.GS, Ops.PaperCut, Functions.PaperCutFullCut };
public virtual byte[] PartialCut() => new byte[] { Cmd.GS, Ops.PaperCut, Functions.PaperCutPartialCut };
public virtual byte[] FullCutAfterFeed(int lineCount) => new byte[] { Cmd.GS, Ops.PaperCut, Functions.PaperCutFullCutWithFeed, (byte)lineCount };
public virtual byte[] PartialCutAfterFeed(int lineCount) => new byte[] { Cmd.GS, Ops.PaperCut, Functions.PaperCutPartialCutWithFeed, (byte)lineCount };
}
}

View File

@@ -0,0 +1,115 @@
using ESCPOS_NET.DataValidation;
using ESCPOS_NET.Emitters.BaseCommandValues;
using ESCPOS_NET.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ESCPOS_NET.Emitters
{
public abstract partial class BaseCommandEmitter : ICommandEmitter
{
/* Barcode Commands */
public virtual byte[] PrintBarcode(BarcodeType type, string barcode, BarcodeCode code = BarcodeCode.CODE_B)
{
DataValidator.ValidateBarcode(type, code, barcode);
return BarcodeBytes(type, barcode, code);
}
protected virtual byte[] BarcodeBytes(BarcodeType type, string barcode, BarcodeCode code)
{
// For CODE128, prepend the first 2 characters as 0x7B and the CODE type, and escape 0x7B characters.
if (type == BarcodeType.CODE128)
{
if (code == BarcodeCode.CODE_C)
{
byte[] b = Encoding.ASCII.GetBytes(barcode);
byte[] ob = new byte[b.Length / 2];
for (int i = 0, obc = 0; i < b.Length; i += 2)
{
ob[obc++] = (byte)(((b[i] - '0') * 10) + (b[i + 1] - '0'));
}
barcode = Encoding.ASCII.GetString(ob);
}
barcode = barcode.Replace("{", "{{");
barcode = $"{(char)0x7B}{(char)code}" + barcode;
}
var command = new List<byte> { Cmd.GS, Barcodes.PrintBarcode, (byte)type, (byte)barcode.Length };
command.AddRange(barcode.ToCharArray().Select(x => (byte)x));
return command.ToArray();
}
public virtual byte[] PrintQRCode(string data, TwoDimensionCodeType type = TwoDimensionCodeType.QRCODE_MODEL2, Size2DCode size = Size2DCode.NORMAL, CorrectionLevel2DCode correction = CorrectionLevel2DCode.PERCENT_7)
{
if (type == TwoDimensionCodeType.PDF417)
{
throw new ArgumentException($"{nameof(TwoDimensionCodeType.PDF417)} is not a valid QRCode type. Please use {nameof(Print2DCode)} method", nameof(type));
}
return Print2DCode(type, data, size, correction);
}
public virtual byte[] Print2DCode(TwoDimensionCodeType type, string data, Size2DCode size = Size2DCode.NORMAL, CorrectionLevel2DCode correction = CorrectionLevel2DCode.PERCENT_7)
{
DataValidator.Validate2DCode(type, data);
return TwoDimensionCodeBytes(type, data, size, correction);
}
protected virtual byte[] TwoDimensionCodeBytes(TwoDimensionCodeType type, string data, Size2DCode size, CorrectionLevel2DCode correction)
{
List<byte> command = new List<byte>();
byte[] initial = { Cmd.GS, Barcodes.Set2DCode, Barcodes.PrintBarcode };
switch (type)
{
case TwoDimensionCodeType.PDF417:
command.AddRange(initial, Barcodes.SetPDF417NumberOfColumns, Barcodes.AutoEnding);
command.AddRange(initial, Barcodes.SetPDF417NumberOfRows, Barcodes.AutoEnding);
command.AddRange(initial, Barcodes.SetPDF417DotSize, (byte)size);
command.AddRange(initial, Barcodes.SetPDF417CorrectionLevel, (byte)correction);
// k = (pL + pH * 256) - 3 --> But pH is always 0.
int k = data.Length;
int l = k + 3;
command.AddRange(initial, (byte)l, Barcodes.StorePDF417Data);
command.AddRange(data.ToCharArray().Select(x => (byte)x));
// Prints stored PDF417
command.AddRange(initial, Barcodes.PrintPDF417);
break;
case TwoDimensionCodeType.QRCODE_MODEL1:
case TwoDimensionCodeType.QRCODE_MODEL2:
case TwoDimensionCodeType.QRCODE_MICRO:
command.AddRange(initial, Barcodes.SelectQRCodeModel, (byte)type, Barcodes.AutoEnding);
command.AddRange(initial, Barcodes.SetQRCodeDotSize, (byte)size);
command.AddRange(initial, Barcodes.SetQRCodeCorrectionLevel, (byte)correction);
int num = data.Length + 3;
int pL = num % 256;
int pH = num / 256;
command.AddRange(initial, (byte)pL, (byte)pH, Barcodes.StoreQRCodeData);
command.AddRange(data.ToCharArray().Select(x => (byte)x));
// Prints stored QRCode
command.AddRange(initial, Barcodes.PrintQRCode);
break;
default:
throw new NotImplementedException($"2D Code '{type}' was not implemented yet.");
}
return command.ToArray();
}
public virtual byte[] SetBarcodeHeightInDots(int height) => new byte[] { Cmd.GS, Barcodes.SetBarcodeHeightInDots, (byte)height };
public virtual byte[] SetBarWidth(BarWidth width) => new byte[] { Cmd.GS, Barcodes.SetBarWidth, (byte)width };
public virtual byte[] SetBarLabelPosition(BarLabelPrintPosition position) => new byte[] { Cmd.GS, Barcodes.SetBarLabelPosition, (byte)position };
public virtual byte[] SetBarLabelFontB(bool fontB) => new byte[] { Cmd.GS, Barcodes.SetBarLabelFont, (byte)(fontB ? 1 : 0) };
}
}

View File

@@ -0,0 +1,16 @@
using ESCPOS_NET.Emitters.BaseCommandValues;
namespace ESCPOS_NET.Emitters
{
public abstract partial class BaseCommandEmitter : ICommandEmitter
{
/* Cash Drawer Commands */
public virtual byte[] CashDrawerOpenPin2(int pulseOnTimeMs = 120, int pulseOffTimeMs = 240) {
return new byte[] { Cmd.ESC, Ops.CashDrawerPulse, 0x00, (byte) (pulseOnTimeMs / 2), (byte) (pulseOffTimeMs / 2) };
}
public virtual byte[] CashDrawerOpenPin5(int pulseOnTimeMs = 120, int pulseOffTimeMs = 240) {
return new byte[] { Cmd.ESC, Ops.CashDrawerPulse, 0x01, (byte) (pulseOnTimeMs / 2), (byte) (pulseOffTimeMs / 2) };
}
}
}

View File

@@ -0,0 +1,22 @@
using ESCPOS_NET.Emitters.BaseCommandValues;
namespace ESCPOS_NET.Emitters
{
public abstract partial class BaseCommandEmitter : ICommandEmitter
{
/* Character Commands */
public virtual byte[] SetStyles(PrintStyle style) => new byte[] { Cmd.ESC, Chars.StyleMode, (byte)style };
public virtual byte[] LeftAlign() => new byte[] { Cmd.ESC, Chars.Alignment, (byte)Align.Left };
public virtual byte[] CenterAlign() => new byte[] { Cmd.ESC, Chars.Alignment, (byte)Align.Center };
public virtual byte[] RightAlign() => new byte[] { Cmd.ESC, Chars.Alignment, (byte)Align.Right };
public virtual byte[] RightCharacterSpacing(int spaceCount) => new byte[] { Cmd.ESC, Chars.RightCharacterSpacing, (byte)spaceCount };
public virtual byte[] CodePage(CodePage codePage) => new byte[] { Cmd.ESC, Chars.CodePage, (byte)codePage };
public virtual byte[] Color(Color color) => new byte[] { Cmd.ESC, Chars.Color, (byte)color };
}
}

View File

@@ -0,0 +1,10 @@
using ESCPOS_NET.Emitters.BaseCommandValues;
namespace ESCPOS_NET.Emitters
{
public abstract partial class BaseCommandEmitter : ICommandEmitter
{
/* Display Commands */
public virtual byte[] Clear() => new byte[] { Display.CLR };
}
}

View File

@@ -0,0 +1,122 @@
using ESCPOS_NET.Emitters.BaseCommandValues;
using ESCPOS_NET.Utilities;
using SixLabors.ImageSharp;
namespace ESCPOS_NET.Emitters
{
public abstract partial class BaseCommandEmitter : ICommandEmitter
{
private byte[] GetImageHeader(int commandLength)
{
byte[] lengths = new byte[4];
int i = 0;
while (commandLength > 0)
{
lengths[i] = (byte)(commandLength & 0xFF);
commandLength >>= 8;
i++;
}
if (i >= 3)
{
return new byte[] { Cmd.GS, Images.ImageCmd8, Images.ImageCmdL, lengths[0], lengths[1], lengths[2], lengths[3] };
}
else
{
return new byte[] { Cmd.GS, Images.ImageCmdParen, Images.ImageCmdL, lengths[0], lengths[1] };
}
}
/* Image Commands */
public virtual byte[] SetImageDensity(bool isHiDPI)
{
ByteArrayBuilder builder = new ByteArrayBuilder();
byte dpiSetting = isHiDPI ? (byte)0x33 : (byte)0x32; // TODO: is this right??
byte[] baseCommand = new byte[] { 0x30, 0x31, dpiSetting, dpiSetting };
builder.Append(GetImageHeader(baseCommand.Length));
builder.Append(baseCommand);
return builder.ToArray();
}
public virtual byte[] BufferImage(byte[] image, int maxWidth = -1, bool isLegacy = false, int color = 1)
{
ByteArrayBuilder imageCommand = new ByteArrayBuilder();
byte colorByte;
switch (color)
{
case 2:
colorByte = 0x32;
break;
case 3:
colorByte = 0x33;
break;
default:
colorByte = 0x31;
break;
}
int width;
int height;
byte[] imageData;
using (var img = Image.Load(image))
{
imageData = img.ToSingleBitPixelByteArray(maxWidth: maxWidth == -1 ? (int?)null : maxWidth);
height = img.Height;
width = img.Width;
}
byte heightL = (byte)height;
byte heightH = (byte)(height >> 8);
if (isLegacy)
{
var byteWidth = (width + 7 & -8) / 8;
byte widthL = (byte)byteWidth;
byte widthH = (byte)(byteWidth >> 8);
imageCommand.Append(new byte[] { Cmd.GS, Images.ImageCmdLegacy, 0x30, 0x00, widthL, widthH, heightL, heightH });
}
else
{
byte widthL = (byte)width;
byte widthH = (byte)(width >> 8);
imageCommand.Append(new byte[] { 0x30, 0x70, 0x30, 0x01, 0x01, colorByte, widthL, widthH, heightL, heightH });
}
imageCommand.Append(imageData);
// Load image to print buffer
ByteArrayBuilder response = new ByteArrayBuilder();
byte[] imageCommandBytes = imageCommand.ToArray();
if (!isLegacy)
{
response.Append(GetImageHeader(imageCommandBytes.Length));
}
response.Append(imageCommandBytes);
return response.ToArray();
}
public virtual byte[] WriteImageFromBuffer()
{
// Print image that's already buffered.
ByteArrayBuilder response = new ByteArrayBuilder();
byte[] printCommandBytes = new byte[] { 0x30, 0x32 };
response.Append(GetImageHeader(printCommandBytes.Length));
response.Append(printCommandBytes);
return response.ToArray();
}
public virtual byte[] PrintImage(byte[] image, bool isHiDPI, bool isLegacy = false, int maxWidth = -1, int color = 1)
{
if (isLegacy)
{
return ByteSplicer.Combine(BufferImage(image, maxWidth, isLegacy));
}
else
{
return ByteSplicer.Combine(SetImageDensity(isHiDPI), BufferImage(image, maxWidth, isLegacy, color), WriteImageFromBuffer());
}
}
}
}

View File

@@ -0,0 +1,11 @@
using ESCPOS_NET.Emitters.BaseCommandValues;
namespace ESCPOS_NET.Emitters
{
public abstract partial class BaseCommandEmitter : ICommandEmitter
{
public virtual byte[] ResetLineSpacing() => new byte[] { Cmd.ESC, Whitespace.ResetLineSpacing };
public virtual byte[] SetLineSpacingInDots(int dots) => new byte[] { Cmd.ESC, Whitespace.LineSpacingInDots, (byte)dots };
}
}

View File

@@ -0,0 +1,14 @@
using ESCPOS_NET.Emitters.BaseCommandValues;
namespace ESCPOS_NET.Emitters
{
public abstract partial class BaseCommandEmitter : ICommandEmitter
{
/* Operational Commands */
public virtual byte[] Initialize() => new byte[] { Cmd.ESC, Ops.Initialize };
public virtual byte[] Enable() => new byte[] { Cmd.ESC, Ops.EnableDisable, 1 };
public virtual byte[] Disable() => new byte[] { Cmd.ESC, Ops.EnableDisable, 0 };
}
}

View File

@@ -0,0 +1,39 @@
using System.Linq;
using ESCPOS_NET.Emitters.BaseCommandValues;
namespace ESCPOS_NET.Emitters
{
public abstract partial class BaseCommandEmitter : ICommandEmitter
{
/* Printing Commands */
public virtual byte[] Print(string data)
{
// Fix OSX or Windows-style newlines
data = data.Replace("\r\n", "\n");
data = data.Replace("\r", "\n");
// TODO: Sanitize...
return data.ToCharArray().Select(x => (byte)x).ToArray();
}
public virtual byte[] PrintLine(string line)
{
if (line == null)
{
return Print("\n");
}
return Print(line.Replace("\r", string.Empty).Replace("\n", string.Empty) + "\n");
}
public virtual byte[] FeedLines(int lineCount) => new byte[] { Cmd.ESC, Whitespace.FeedLines, (byte)lineCount };
public virtual byte[] FeedLinesReverse(int lineCount) => new byte[] { Cmd.ESC, Whitespace.FeedLinesReverse, (byte)lineCount };
public virtual byte[] FeedDots(int dotCount) => new byte[] { Cmd.ESC, Whitespace.FeedDots, (byte)dotCount };
public virtual byte[] ReverseMode(bool enable) => new byte[] { Cmd.GS, Chars.ReversePrintMode, enable ? (byte)0x01 : (byte)0x00 };
public virtual byte[] UpsideDownMode(bool enable) => new byte[] { Cmd.ESC, Chars.UpsideDownMode, enable ? (byte)0x01 : (byte)0x00 };
}
}

View File

@@ -0,0 +1,18 @@
using ESCPOS_NET.Emitters.BaseCommandValues;
namespace ESCPOS_NET.Emitters
{
public abstract partial class BaseCommandEmitter : ICommandEmitter
{
/* Status Commands */
public virtual byte[] EnableAutomaticStatusBack() => new byte[] { Cmd.GS, Status.AutomaticStatusBack, 0xFF };
public virtual byte[] EnableAutomaticInkStatusBack() => new byte[] { Cmd.GS, Status.AutomaticInkStatusBack, 0xFF };
public virtual byte[] DisableAutomaticStatusBack() => new byte[] { Cmd.GS, Status.AutomaticStatusBack, 0x00 };
public virtual byte[] DisableAutomaticInkStatusBack() => new byte[] { Cmd.GS, Status.AutomaticInkStatusBack, 0x00 };
public virtual byte[] RequestPaperStatus() => new byte[] { Cmd.GS, Status.RequestStatus, 0x31 };
public virtual byte[] RequestDrawerStatus() => new byte[] { Cmd.GS, Status.RequestStatus, 0x32 };
public virtual byte[] RequestInkStatus() => new byte[] { Cmd.GS, Status.RequestStatus, 0x34 };
}
}