Commit 0db38e74 authored by Andrey's avatar Andrey

Added PosString class. Updated methods and example.

parent bbf5ce66
...@@ -4,15 +4,15 @@ import 'package:esc_pos_printer/src/printer.dart'; ...@@ -4,15 +4,15 @@ import 'package:esc_pos_printer/src/printer.dart';
void main() { void main() {
Printer.connect('192.168.0.123').then((printer) { Printer.connect('192.168.0.123').then((printer) {
printer.reset(); printer.reset();
printer.println('Normal text'); printer.println(PosString('Normal text'));
printer.println('Bold text', bold: true); printer.println(PosString('Bold text', bold: true));
printer.println('Reverse text', reverse: true); printer.println(PosString('Reverse text', reverse: true));
printer.println('Underlined text', underline: true); printer.println(PosString('Underlined text', underline: true));
printer.println('Align left', align: PosTextAlign.left); printer.println(PosString('Align left', align: PosTextAlign.left));
printer.println('Align center', align: PosTextAlign.center); printer.println(PosString('Align center', align: PosTextAlign.center));
printer.println('Align right', align: PosTextAlign.right); printer.println(PosString('Align right', align: PosTextAlign.right));
printer.println('Text size 2', printer.println(PosString('Text size 200%',
height: PosTextSize.size2, width: PosTextSize.size2); height: PosTextSize.size2, width: PosTextSize.size2));
printer.cut(); printer.cut();
......
...@@ -8,4 +8,5 @@ library esc_pos_printer; ...@@ -8,4 +8,5 @@ library esc_pos_printer;
export './src/enums.dart'; export './src/enums.dart';
export './src/exceptions.dart'; export './src/exceptions.dart';
export './src/pos_string.dart';
export './src/printer.dart'; export './src/printer.dart';
...@@ -31,20 +31,3 @@ class PosBeepDuration { ...@@ -31,20 +31,3 @@ class PosBeepDuration {
static const beep400ms = PosBeepDuration._internal(8); static const beep400ms = PosBeepDuration._internal(8);
static const beep450ms = PosBeepDuration._internal(9); static const beep450ms = PosBeepDuration._internal(9);
} }
// class PosCol {
// const PosCol._internal(this.value);
// final int value;
// static const col1 = PosCol._internal(1);
// static const col2 = PosCol._internal(2);
// static const col3 = PosCol._internal(3);
// static const col4 = PosCol._internal(4);
// static const col5 = PosCol._internal(5);
// static const col6 = PosCol._internal(6);
// static const col7 = PosCol._internal(7);
// static const col8 = PosCol._internal(8);
// static const col9 = PosCol._internal(9);
// static const col10 = PosCol._internal(10);
// static const col11 = PosCol._internal(11);
// static const col12 = PosCol._internal(12);
// }
import 'enums.dart';
class PosString {
PosString(
this.text, {
this.bold = false,
this.reverse = false,
this.underline = false,
this.align = PosTextAlign.left,
this.height = PosTextSize.size1,
this.width = PosTextSize.size1,
this.fontType = PosFontType.fontA,
this.linesAfter = 0,
});
String text;
bool bold = false;
bool reverse = false;
bool underline = false;
PosTextAlign align = PosTextAlign.left;
PosTextSize height = PosTextSize.size1;
PosTextSize width = PosTextSize.size1;
PosFontType fontType = PosFontType.fontA;
int linesAfter = 0;
@override
String toString() {
return text;
}
}
...@@ -4,8 +4,9 @@ import 'package:hex/hex.dart'; ...@@ -4,8 +4,9 @@ import 'package:hex/hex.dart';
import 'commands.dart'; import 'commands.dart';
import 'enums.dart'; import 'enums.dart';
import 'exceptions.dart'; import 'exceptions.dart';
import 'pos_string.dart';
/// Printer. /// Network printer
class Printer { class Printer {
Printer(this._socket) { Printer(this._socket) {
reset(); reset();
...@@ -29,37 +30,28 @@ class Printer { ...@@ -29,37 +30,28 @@ class Printer {
_socket.destroy(); _socket.destroy();
} }
void println( void println(PosString data) {
String text, { _socket.write(data.bold ? cBoldOn : cBoldOff);
bool bold = false, _socket.write(data.reverse ? cReverseOn : cReverseOff);
bool reverse = false, _socket.write(data.underline ? cUnderline1dot : cUnderlineOff);
bool underline = false, _socket.write(data.align == PosTextAlign.left
PosTextAlign align = PosTextAlign.left,
PosTextSize height = PosTextSize.size1,
PosTextSize width = PosTextSize.size1,
PosFontType fontType = PosFontType.fontA,
int linesAfter = 0,
}) {
_socket.write(bold ? cBoldOn : cBoldOff);
_socket.write(reverse ? cReverseOn : cReverseOff);
_socket.write(underline ? cUnderline1dot : cUnderlineOff);
_socket.write(align == PosTextAlign.left
? cAlignLeft ? cAlignLeft
: (align == PosTextAlign.center ? cAlignCenter : cAlignRight)); : (data.align == PosTextAlign.center ? cAlignCenter : cAlignRight));
_socket.write(fontType == PosFontType.fontA ? cFontA : cFontB); _socket.write(data.fontType == PosFontType.fontA ? cFontA : cFontB);
// Text size // Text size
_socket.add( _socket.add(
Uint8List.fromList( Uint8List.fromList(
List.from(cSizeGSn.codeUnits)..add(PosTextSize.decSize(height, width)), List.from(cSizeGSn.codeUnits)
..add(PosTextSize.decSize(data.height, data.width)),
), ),
); );
_socket.writeln(text); _socket.writeln(data.text);
emptyLines(linesAfter); emptyLines(data.linesAfter);
reset(); reset();
} }
void printRow(List<int> cols, List<String> data) { void printRow(List<int> cols, List<PosString> data) {
final validRange = cols.every((val) => val >= 1 && val <= 12); final validRange = cols.every((val) => val >= 1 && val <= 12);
if (!validRange) { if (!validRange) {
throw PosRowException('Column width should be between 1..12'); throw PosRowException('Column width should be between 1..12');
...@@ -80,7 +72,7 @@ class Printer { ...@@ -80,7 +72,7 @@ class Printer {
_socket.writeln(); _socket.writeln();
} }
void _printCol(int i, String data) { void _printCol(int i, PosString data) {
final int pos = i == 0 ? 0 : (512 * i / 11 - 1).round(); final int pos = i == 0 ? 0 : (512 * i / 11 - 1).round();
final hexStr = pos.toRadixString(16).padLeft(3, '0'); final hexStr = pos.toRadixString(16).padLeft(3, '0');
final hexPair = HEX.decode(hexStr); final hexPair = HEX.decode(hexStr);
......
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