Commit 35f1a446 authored by Andrey's avatar Andrey

Added height/width size. Added cut mode. Fixed feed and reverse feed bugs.

parent 2d8cc467
import 'package:esc_pos_printer/esc_pos_printer.dart'; import 'package:esc_pos_printer/esc_pos_printer.dart';
import 'package:esc_pos_printer/src/printer.dart';
main() { main() {
Printer.connect('192.168.0.123').then((printer) { Printer.connect('192.168.0.123').then((printer) {
printer.reset();
printer.println('Normal text'); printer.println('Normal text');
printer.println('Bold text', bold: true); printer.println('Bold text', bold: true);
printer.println('Reverse text', reverse: true); printer.println('Reverse text', reverse: true);
printer.println('Underlined text', underline: true); printer.println('Underlined text', underline: true);
printer.println('Align left', align: TextAlign.left); printer.println('Align left', align: PosTextAlign.left);
printer.println('Align center', align: TextAlign.center); printer.println('Align center', align: PosTextAlign.center);
printer.println('Align right', align: TextAlign.right); printer.println('Align right', align: PosTextAlign.right);
printer.cut(); printer.cut();
......
...@@ -12,7 +12,8 @@ const cCutPart = '${esc}m'; // Execute paper partial cut. ...@@ -12,7 +12,8 @@ const cCutPart = '${esc}m'; // Execute paper partial cut.
// Character // Character
const cReverseOn = '${gs}B1'; // Turn white/black reverse print mode on const cReverseOn = '${gs}B1'; // Turn white/black reverse print mode on
const cReverseOff = '${gs}B0'; // Turn white/black reverse print mode off const cReverseOff = '${gs}B0'; // Turn white/black reverse print mode off
const cSizeN = '$gs!'; // Select character size [N] const cSizeGSn = '$gs!'; // Select character size [N]
const cSizeESCn = '$esc!'; // Select character size [N]
const cUnderlineOff = '$esc-0'; // Turns off underline mode const cUnderlineOff = '$esc-0'; // Turns off underline mode
const cUnderline1dot = '$esc-1'; // Turns on underline mode (1-dot thick) const cUnderline1dot = '$esc-1'; // Turns on underline mode (1-dot thick)
const cUnderline2dots = '$esc-2'; // Turns on underline mode (2-dots thick) const cUnderline2dots = '$esc-2'; // Turns on underline mode (2-dots thick)
......
import 'dart:io'; import 'dart:io';
import 'dart:typed_data';
import 'commands.dart'; import 'commands.dart';
enum TextAlign { left, center, right } enum PosTextAlign { left, center, right }
enum CutMode { normal, partial, full } enum PosCutMode { normal, partial, full }
class PosTextSizeHeight {
final value;
const PosTextSizeHeight._internal(this.value);
static const normal = const PosTextSizeHeight._internal(0x00);
static const double = const PosTextSizeHeight._internal(0x10);
}
class PosTextSizeWidth {
final value;
const PosTextSizeWidth._internal(this.value);
static const normal = const PosTextSizeWidth._internal(0x00);
static const double = const PosTextSizeWidth._internal(0x20);
}
/// Abstract printer. /// Abstract printer.
class Printer { class Printer {
...@@ -46,22 +61,24 @@ class Printer { ...@@ -46,22 +61,24 @@ class Printer {
bool bold = false, bool bold = false,
bool reverse = false, bool reverse = false,
bool underline = false, bool underline = false,
TextAlign align = TextAlign.left, PosTextAlign align = PosTextAlign.left,
int charHeight = -1, // TODO replace by font size PosTextSizeHeight height = PosTextSizeHeight.normal,
int charWidth = -1, PosTextSizeWidth width = PosTextSizeWidth.normal,
int linesAfter = 0, int linesAfter = 0,
}) { }) {
_socket.write(bold ? cBoldOn : cBoldOff); _socket.write(bold ? cBoldOn : cBoldOff);
_socket.write(reverse ? cReverseOn : cReverseOff); _socket.write(reverse ? cReverseOn : cReverseOff);
_socket.write(underline ? cUnderline1dot : cUnderlineOff); _socket.write(underline ? cUnderline1dot : cUnderlineOff);
_socket.write(align == TextAlign.left _socket.write(align == PosTextAlign.left
? cAlignLeft ? cAlignLeft
: (align == TextAlign.center ? cAlignCenter : cAlignRight)); : (align == PosTextAlign.center ? cAlignCenter : cAlignRight));
if (charHeight > 0 && charWidth > 0) { // Font size
var n = 16 * (charWidth - 1) + (charHeight - 1); _socket.add(
_socket.writeAll([cSizeN, n]); Uint8List.fromList(
} List.from(cSizeESCn.codeUnits)..add(height.value + width.value),
),
);
_socket.writeln(text); _socket.writeln(text);
emptyLines(linesAfter); emptyLines(linesAfter);
...@@ -80,19 +97,27 @@ class Printer { ...@@ -80,19 +97,27 @@ class Printer {
void feed(int n) { void feed(int n) {
if (n >= 0 && n <= 255) { if (n >= 0 && n <= 255) {
_socket.writeAll([cFeedN, n.toString()]); _socket.add(
Uint8List.fromList(
List.from(cFeedN.codeUnits)..add(n),
),
);
} }
} }
void reverseFeed(int n) { void reverseFeed(int n) {
_socket.writeAll([cReverseFeedN, n.toString()]); _socket.add(
Uint8List.fromList(
List.from(cReverseFeedN.codeUnits)..add(n),
),
);
} }
void cut({CutMode mode = CutMode.normal}) { void cut({PosCutMode mode = PosCutMode.normal}) {
_socket.write('\n\n\n\n\n'); _socket.write('\n\n\n\n\n');
if (mode == CutMode.partial) { if (mode == PosCutMode.partial) {
_socket.write(cCutPart); _socket.write(cCutPart);
} else if (mode == CutMode.full) { } else if (mode == PosCutMode.full) {
_socket.write(cCutFull); _socket.write(cCutFull);
} else { } else {
_socket.write(cCut); _socket.write(cCut);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment