diff --git a/example/example.dart b/example/example.dart index 85c0a1c..2ca13ad 100644 --- a/example/example.dart +++ b/example/example.dart @@ -42,7 +42,7 @@ Future main() async { width: 3, styles: PosStyles(align: PosAlign.center, underline: true), ), - ]); + ], false); bytes += generator.text('Text size 200%', styles: PosStyles( diff --git a/lib/src/capability_profile.dart b/lib/src/capability_profile.dart index 019bae2..c899362 100644 --- a/lib/src/capability_profile.dart +++ b/lib/src/capability_profile.dart @@ -7,7 +7,6 @@ */ import 'dart:convert' show json; -import 'dart:convert' show utf8; import 'package:flutter/services.dart' show rootBundle; class CodePage { diff --git a/lib/src/enums.dart b/lib/src/enums.dart index ac2f602..bb68efa 100644 --- a/lib/src/enums.dart +++ b/lib/src/enums.dart @@ -7,8 +7,11 @@ */ enum PosAlign { left, center, right } + enum PosCutMode { full, partial } + enum PosFontType { fontA, fontB } + enum PosDrawer { pin2, pin5 } /// Choose image printing function @@ -33,12 +36,14 @@ class PosTextSize { } class PaperSize { - const PaperSize._internal(this.value); + PaperSize._internal(this.value); final int value; - static const mm58 = PaperSize._internal(1); - static const mm80 = PaperSize._internal(2); + int? custom; + static var mm58 = PaperSize._internal(1); + static var mm80 = PaperSize._internal(2); - int get width => value == PaperSize.mm58.value ? 372 : 558; + int get width => + (custom != null ? custom! : (value == PaperSize.mm58.value ? 372 : 558)); } class PosBeepDuration { diff --git a/lib/src/generator.dart b/lib/src/generator.dart index 6b464a6..aa5915c 100644 --- a/lib/src/generator.dart +++ b/lib/src/generator.dart @@ -12,7 +12,6 @@ import 'package:hex/hex.dart'; import 'package:image/image.dart'; import 'package:gbk_codec/gbk_codec.dart'; import 'package:esc_pos_utils/esc_pos_utils.dart'; -import 'enums.dart'; import 'commands.dart'; class Generator { @@ -73,7 +72,10 @@ class Generator { .replaceAll("»", '"') .replaceAll(" ", ' ') .replaceAll("•", '.'); + if (!isKanji) { + text = text.replaceAll( + RegExp('[^A-Za-z0-9!"#\$%&\'\n()*+,./:;<=>?@\^_`{|}~-]'), ' '); return latin1.encode(text); } else { return Uint8List.fromList(gbk_bytes.encode(text)); @@ -83,6 +85,9 @@ class Generator { List _getLexemes(String text) { final List lexemes = []; final List isLexemeChinese = []; + if (text.isEmpty) { + return []; + } int start = 0; int end = 0; bool curLexemeChinese = _isChinese(text[0]); @@ -144,17 +149,23 @@ class Generator { final int heightPx = image.height; // Create a black bottom layer - final biggerImage = copyResize(image, width: widthPx, height: heightPx); - fill(biggerImage, 0); + final biggerImage = copyResize(image, + width: widthPx, height: heightPx, interpolation: Interpolation.linear); + //fill(biggerImage, color: ColorRgb8(0, 0, 0)); + fill(biggerImage, color: ColorRgb8(0, 0, 0)); // Insert source image into bigger one - drawImage(biggerImage, image, dstX: 0, dstY: 0); + compositeImage(biggerImage, image, dstX: 0, dstY: 0); int left = 0; final List> blobs = []; while (left < widthPx) { - final Image slice = copyCrop(biggerImage, left, 0, lineHeight, heightPx); - final Uint8List bytes = slice.getBytes(format: Format.luminance); + final Image slice = copyCrop(biggerImage, + x: left, y: 0, width: lineHeight, height: heightPx); + if (slice.numChannels > 2) grayscale(slice); + final imgBinary = + (slice.numChannels > 1) ? slice.convert(numChannels: 1) : slice; + final bytes = imgBinary.getBytes(); blobs.add(bytes); left += lineHeight; } @@ -173,7 +184,7 @@ class Generator { // R/G/B channels are same -> keep only one channel final List oneChannelBytes = []; - final List buffer = image.getBytes(format: Format.rgba); + final List buffer = image.getBytes(); for (int i = 0; i < buffer.length; i += 4) { oneChannelBytes.add(buffer[i]); } @@ -350,6 +361,8 @@ class Generator { }) { List bytes = []; if (!containsChinese) { + text = text.replaceAll( + RegExp('[^A-Za-z0-9!"#\$%&\'\n()*+,./:;<=>?@\^_`{|}~-]'), ' '); bytes += _text( _encode(text, isKanji: containsChinese), styles: styles, @@ -459,7 +472,7 @@ class Generator { /// /// A row contains up to 12 columns. A column has a width between 1 and 12. /// Total width of columns in one row must be equal 12. - List row(List cols) { + List row(List cols, bool containChineese) { List bytes = []; final isSumValid = cols.fold(0, (int sum, col) => sum + col.width) == 12; if (!isSumValid) { @@ -477,22 +490,38 @@ class Generator { _colIndToPosition(colInd + cols[i].width) - spaceBetweenRows; int maxCharactersNb = ((toPos - fromPos) / charWidth).floor(); - if (!cols[i].containsChinese) { + if (true) { // CASE 1: containsChinese = false Uint8List encodedToPrint = cols[i].textEncoded != null ? cols[i].textEncoded! - : _encode(cols[i].text); + : _encode(cols[i].text, isKanji: containChineese); // If the col's content is too long, split it to the next row int realCharactersNb = encodedToPrint.length; if (realCharactersNb > maxCharactersNb) { // Print max possible and split to the next row + try { + while (String.fromCharCodes( + encodedToPrint.sublist(0, maxCharactersNb))[ + String.fromCharCodes( + encodedToPrint.sublist(0, maxCharactersNb)) + .length - + 1] != + " ") { + maxCharactersNb--; + if (maxCharactersNb == 0) { + maxCharactersNb = ((toPos - fromPos) / charWidth).floor(); + break; + } + } + } catch (e) {} Uint8List encodedToPrintNextRow = encodedToPrint.sublist(maxCharactersNb); encodedToPrint = encodedToPrint.sublist(0, maxCharactersNb); isNextRow = true; nextRow.add(PosColumn( textEncoded: encodedToPrintNextRow, + containsChinese: containChineese, width: cols[i].width, styles: cols[i].styles)); } else { @@ -501,12 +530,11 @@ class Generator { text: '', width: cols[i].width, styles: cols[i].styles)); } // end rows splitting - bytes += _text( - encodedToPrint, - styles: cols[i].styles, - colInd: colInd, - colWidth: cols[i].width, - ); + bytes += _text(encodedToPrint, + styles: cols[i].styles, + colInd: colInd, + colWidth: cols[i].width, + isKanji: containChineese); } else { // CASE 1: containsChinese = true // Split text into multiple lines if it too long @@ -527,7 +555,7 @@ class Generator { isNextRow = true; nextRow.add(PosColumn( text: toPrintNextRow, - containsChinese: true, + containsChinese: containChineese, width: cols[i].width, styles: cols[i].styles)); } else { @@ -538,8 +566,8 @@ class Generator { // Print current row final list = _getLexemes(toPrint); - final List lexemes = list[0]; - final List isLexemeChinese = list[1]; + final List lexemes = list.isEmpty ? [] : list[0]; + final List isLexemeChinese = list.isEmpty ? [] : list[1]; // Print each lexeme using codetable OR kanji for (var j = 0; j < lexemes.length; ++j) { @@ -559,7 +587,7 @@ class Generator { bytes += emptyLines(1); if (isNextRow) { - row(nextRow); + bytes += row(nextRow, containChineese); } return bytes; } @@ -567,20 +595,35 @@ class Generator { /// Print an image using (ESC *) command /// /// [image] is an instanse of class from [Image library](https://pub.dev/packages/image) - List image(Image imgSrc, {PosAlign align = PosAlign.center}) { + List image(Image imgSrc, + {PosAlign align = PosAlign.center, bool isDoubleDensity = true}) { List bytes = []; // Image alignment - bytes += setStyles(PosStyles().copyWith(align: align)); + bytes += setStyles(const PosStyles().copyWith(align: align)); + + Image image; + if (!isDoubleDensity) { + int size = 558 ~/ 2; + if (_paperSize == PaperSize.mm58) { + size = 375 ~/ 2; + } else if (_paperSize == PaperSize.mm80) { + size = 503 ~/ 2; + } - final Image image = Image.from(imgSrc); // make a copy - const bool highDensityHorizontal = true; - const bool highDensityVertical = true; + image = + copyResize(imgSrc, width: size, interpolation: Interpolation.linear); + } else { + image = Image.from(imgSrc); // make a copy + } + + bool highDensityHorizontal = isDoubleDensity; + bool highDensityVertical = isDoubleDensity; invert(image); - flip(image, Flip.horizontal); - final Image imageRotated = copyRotate(image, 270); + flipHorizontal(image); + final Image imageRotated = copyRotate(image, angle: 270); - const int lineHeight = highDensityVertical ? 3 : 1; + int lineHeight = highDensityVertical ? 3 : 1; final List> blobs = _toColumnFormat(imageRotated, lineHeight * 8); // Compress according to line density @@ -592,7 +635,7 @@ class Generator { } final int heightPx = imageRotated.height; - const int densityByte = + int densityByte = (highDensityHorizontal ? 1 : 0) + (highDensityVertical ? 32 : 0); final List header = List.from(cBitImg.codeUnits); @@ -600,7 +643,7 @@ class Generator { header.addAll(_intLowHigh(heightPx, 2)); // Adjust line spacing (for 16-unit line feeds): ESC 3 0x10 (HEX: 0x1b 0x33 0x10) - bytes += [27, 51, 16]; + bytes += [27, 51, 0]; for (int i = 0; i < blobs.length; ++i) { bytes += List.from(header) ..addAll(blobs[i]) @@ -815,8 +858,8 @@ class Generator { }) { List bytes = []; final list = _getLexemes(text); - final List lexemes = list[0]; - final List isLexemeChinese = list[1]; + final List lexemes = list.isEmpty ? [] : list[0]; + final List isLexemeChinese = list.isEmpty ? [] : list[1]; // Print each lexeme using codetable OR kanji int? colInd = 0; diff --git a/pubspec.lock b/pubspec.lock index 3447480..4b16d40 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -5,72 +5,82 @@ packages: dependency: transitive description: name: archive - url: "https://pub.dartlang.org" + sha256: "22600aa1e926be775fa5fe7e6894e7fb3df9efda8891c73f70fb3262399a432d" + url: "https://pub.dev" source: hosted - version: "3.1.2" + version: "3.4.10" async: dependency: transitive description: name: async - url: "https://pub.dartlang.org" + sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c" + url: "https://pub.dev" source: hosted - version: "2.8.2" + version: "2.11.0" boolean_selector: dependency: transitive description: name: boolean_selector - url: "https://pub.dartlang.org" + sha256: "6cfb5af12253eaf2b368f07bacc5a80d1301a071c73360d746b7f2e32d762c66" + url: "https://pub.dev" source: hosted - version: "2.1.0" + version: "2.1.1" characters: dependency: transitive description: name: characters - url: "https://pub.dartlang.org" + sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605" + url: "https://pub.dev" source: hosted - version: "1.2.0" - charcode: - dependency: transitive - description: - name: charcode - url: "https://pub.dartlang.org" - source: hosted - version: "1.3.1" + version: "1.3.0" clock: dependency: transitive description: name: clock - url: "https://pub.dartlang.org" + sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf + url: "https://pub.dev" source: hosted - version: "1.1.0" + version: "1.1.1" collection: dependency: transitive description: name: collection - url: "https://pub.dartlang.org" + sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a + url: "https://pub.dev" + source: hosted + version: "1.18.0" + convert: + dependency: transitive + description: + name: convert + sha256: "0f08b14755d163f6e2134cb58222dd25ea2a2ee8a195e53983d57c075324d592" + url: "https://pub.dev" source: hosted - version: "1.15.0" + version: "3.1.1" crypto: dependency: transitive description: name: crypto - url: "https://pub.dartlang.org" + sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab + url: "https://pub.dev" source: hosted - version: "3.0.1" + version: "3.0.3" csslib: dependency: transitive description: name: csslib - url: "https://pub.dartlang.org" + sha256: f857285c8dc0b4f2f77b49a1c083ff8c75223a7549de20f3e607df58cf497a43 + url: "https://pub.dev" source: hosted version: "0.17.0" fake_async: dependency: transitive description: name: fake_async - url: "https://pub.dartlang.org" + sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78" + url: "https://pub.dev" source: hosted - version: "1.2.0" + version: "1.3.1" flutter: dependency: "direct main" description: flutter @@ -85,58 +95,90 @@ packages: dependency: "direct main" description: name: gbk_codec - url: "https://pub.dartlang.org" + sha256: "3af5311fc9393115e3650ae6023862adf998051a804a08fb804f042724999f61" + url: "https://pub.dev" source: hosted version: "0.4.0" hex: dependency: "direct main" description: name: hex - url: "https://pub.dartlang.org" + sha256: "4e7cd54e4b59ba026432a6be2dd9d96e4c5205725194997193bf871703b82c4a" + url: "https://pub.dev" source: hosted version: "0.2.0" html: dependency: transitive description: name: html - url: "https://pub.dartlang.org" + sha256: bfef906cbd4e78ef49ae511d9074aebd1d2251482ef601a280973e8b58b51bbf + url: "https://pub.dev" source: hosted version: "0.15.0" image: dependency: "direct main" description: name: image - url: "https://pub.dartlang.org" + sha256: "004a2e90ce080f8627b5a04aecb4cdfac87d2c3f3b520aa291260be5a32c033d" + url: "https://pub.dev" + source: hosted + version: "4.1.4" + js: + dependency: transitive + description: + name: js + sha256: "4186c61b32f99e60f011f7160e32c89a758ae9b1d0c6d28e2c02ef0382300e2b" + url: "https://pub.dev" source: hosted - version: "3.0.5" + version: "0.7.0" matcher: dependency: transitive description: name: matcher - url: "https://pub.dartlang.org" + sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" + url: "https://pub.dev" source: hosted - version: "0.12.11" + version: "0.12.16" + material_color_utilities: + dependency: transitive + description: + name: material_color_utilities + sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41" + url: "https://pub.dev" + source: hosted + version: "0.5.0" meta: dependency: transitive description: name: meta - url: "https://pub.dartlang.org" + sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e + url: "https://pub.dev" source: hosted - version: "1.7.0" + version: "1.10.0" path: dependency: transitive description: name: path - url: "https://pub.dartlang.org" + sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917" + url: "https://pub.dev" source: hosted - version: "1.8.0" + version: "1.8.3" petitparser: dependency: transitive description: name: petitparser - url: "https://pub.dartlang.org" + sha256: c15605cd28af66339f8eb6fbe0e541bfe2d1b72d5825efc6598f3e0a31b9ad27 + url: "https://pub.dev" + source: hosted + version: "6.0.2" + pointycastle: + dependency: transitive + description: + name: pointycastle + sha256: "43ac87de6e10afabc85c445745a7b799e04de84cebaa4fd7bf55a5e1e9604d29" + url: "https://pub.dev" source: hosted - version: "4.3.0" + version: "3.7.4" sky_engine: dependency: transitive description: flutter @@ -146,64 +188,81 @@ packages: dependency: transitive description: name: source_span - url: "https://pub.dartlang.org" + sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c" + url: "https://pub.dev" source: hosted - version: "1.8.1" + version: "1.10.0" stack_trace: dependency: transitive description: name: stack_trace - url: "https://pub.dartlang.org" + sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b" + url: "https://pub.dev" source: hosted - version: "1.10.0" + version: "1.11.1" stream_channel: dependency: transitive description: name: stream_channel - url: "https://pub.dartlang.org" + sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7 + url: "https://pub.dev" source: hosted - version: "2.1.0" + version: "2.1.2" string_scanner: dependency: transitive description: name: string_scanner - url: "https://pub.dartlang.org" + sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde" + url: "https://pub.dev" source: hosted - version: "1.1.0" + version: "1.2.0" term_glyph: dependency: transitive description: name: term_glyph - url: "https://pub.dartlang.org" + sha256: a29248a84fbb7c79282b40b8c72a1209db169a2e0542bce341da992fe1bc7e84 + url: "https://pub.dev" source: hosted - version: "1.2.0" + version: "1.2.1" test_api: dependency: transitive description: name: test_api - url: "https://pub.dartlang.org" + sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b" + url: "https://pub.dev" source: hosted - version: "0.4.3" + version: "0.6.1" typed_data: dependency: transitive description: name: typed_data - url: "https://pub.dartlang.org" + sha256: "53bdf7e979cfbf3e28987552fd72f637e63f3c8724c9e56d9246942dc2fa36ee" + url: "https://pub.dev" source: hosted version: "1.3.0" vector_math: dependency: transitive description: name: vector_math - url: "https://pub.dartlang.org" + sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803" + url: "https://pub.dev" + source: hosted + version: "2.1.4" + web: + dependency: transitive + description: + name: web + sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152 + url: "https://pub.dev" source: hosted - version: "2.1.0" + version: "0.3.0" xml: dependency: transitive description: name: xml - url: "https://pub.dartlang.org" + sha256: b015a8ad1c488f66851d762d3090a21c600e479dc75e68328c52774040cf9226 + url: "https://pub.dev" source: hosted - version: "5.3.0" + version: "6.5.0" sdks: - dart: ">=2.14.0 <3.0.0" + dart: ">=3.2.0 <4.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 5db09d0..437b569 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,7 @@ name: esc_pos_utils description: Basic Flutter/Dart classes for ESC/POS printing. Ticket class generates ESC/POS commands that can be sent to a thermal printer. version: 1.1.0 +publish_to: none homepage: https://github.com/andrey-ushakov/esc_pos_utils environment: @@ -11,7 +12,7 @@ dependencies: sdk: flutter hex: ^0.2.0 gbk_codec: ^0.4.0 - image: ^3.0.2 + image: ^4.0.9 dev_dependencies: flutter_test: diff --git a/test/esc_pos_utils_test.dart b/test/esc_pos_utils_test.dart index f50dbae..2f6b825 100644 --- a/test/esc_pos_utils_test.dart +++ b/test/esc_pos_utils_test.dart @@ -1,7 +1,5 @@ import 'package:flutter_test/flutter_test.dart'; -import 'package:esc_pos_utils/esc_pos_utils.dart'; - void main() { test('Tests not implemented', () { expect(1, 1);