diff --git a/lib/src/generator.dart b/lib/src/generator.dart index f811f7c..c5be7ed 100644 --- a/lib/src/generator.dart +++ b/lib/src/generator.dart @@ -7,18 +7,17 @@ import 'package:flutter_esc_pos_utils/flutter_esc_pos_utils.dart'; import 'commands.dart'; class Generator { - Generator(this._paperSize, this._profile, - {this.spaceBetweenRows = 5, this.codec = latin1}); + Generator(this._paperSize, this._profile, {this.spaceBetweenRows = 5, this.codec = latin1}); // Ticket config final PaperSize _paperSize; - CapabilityProfile _profile; + final CapabilityProfile _profile; int? _maxCharsPerLine; // Global styles String? _codeTable; PosFontType? _font; // Current styles - PosStyles _styles = PosStyles(); + PosStyles _styles = const PosStyles(); final Codec codec; int spaceBetweenRows; @@ -53,8 +52,7 @@ class Generator { if (styles.fontType != null) { charsPerLine = _getMaxCharsPerLine(styles.fontType); } else { - charsPerLine = - _maxCharsPerLine ?? _getMaxCharsPerLine(_styles.fontType); + charsPerLine = _maxCharsPerLine ?? _getMaxCharsPerLine(_styles.fontType); } } return charsPerLine; @@ -62,12 +60,7 @@ class Generator { Uint8List _encode(String text, {bool isKanji = false}) { // replace some non-ascii characters - text = text - .replaceAll("’", "'") - .replaceAll("´", "'") - .replaceAll("»", '"') - .replaceAll(" ", ' ') - .replaceAll("•", '.'); + text = text.replaceAll("’", "'").replaceAll("´", "'").replaceAll("»", '"').replaceAll(" ", ' ').replaceAll("•", '.'); if (!isKanji) { return codec.encode(text); } else { @@ -114,8 +107,7 @@ class Generator { throw Exception('Can only output 1-4 bytes'); } if (value < 0 || value > maxInput) { - throw Exception( - 'Number is too large. Can only output up to $maxInput in $bytesNb bytes'); + throw Exception('Number is too large. Can only output up to $maxInput in $bytesNb bytes'); } final List res = []; @@ -139,8 +131,7 @@ class Generator { final int heightPx = image.height; // Create a black bottom layer - final biggerImage = copyResize(image, - width: widthPx, height: heightPx, interpolation: Interpolation.linear); + final biggerImage = copyResize(image, width: widthPx, height: heightPx, interpolation: Interpolation.linear); fill(biggerImage, 0); // Insert source image into bigger one drawImage(biggerImage, image, dstX: 0, dstY: 0); @@ -210,8 +201,7 @@ class Generator { /// Replaces a single bit in a 32-bit unsigned integer. int _transformUint32Bool(int uint32, int shift, bool newValue) { - return ((0xFFFFFFFF ^ (0x1 << shift)) & uint32) | - ((newValue ? 1 : 0) << shift); + return ((0xFFFFFFFF ^ (0x1 << shift)) & uint32) | ((newValue ? 1 : 0) << shift); } // ************************ (end) Internal helpers ************************ @@ -220,7 +210,7 @@ class Generator { List reset() { List bytes = []; bytes += cInit.codeUnits; - _styles = PosStyles(); + _styles = const PosStyles(); bytes += setGlobalCodeTable(_codeTable); bytes += setGlobalFont(_font); return bytes; @@ -228,8 +218,7 @@ class Generator { /// Clear the buffer and reset text styles List clearStyle() { - return setStyles( - const PosStyles(height: PosTextSize.size1, width: PosTextSize.size1)); + return setStyles(const PosStyles(height: PosTextSize.size1, width: PosTextSize.size1)); } /// Set global code table which will be used instead of the default printer's code table @@ -261,17 +250,24 @@ class Generator { List setStyles(PosStyles styles, {bool isKanji = false}) { List bytes = []; - if (styles.align != _styles.align) { - bytes += codec.encode(styles.align == PosAlign.left - ? cAlignLeft - : (styles.align == PosAlign.center ? cAlignCenter : cAlignRight)); - _styles = _styles.copyWith(align: styles.align); - } - if (styles.bold != _styles.bold) { - bytes += styles.bold ? cBoldOn.codeUnits : cBoldOff.codeUnits; - _styles = _styles.copyWith(bold: styles.bold); - } + /// NOTE TO DEVELOPER: the first if statement it checks for is not working + /// + /// HOTFIX: the alignment was not working but removing the condition worked + /// but the next condition (bold) didn't work + /// + /// Not sure why, but it is now working. + /// if you could, please fix this in a proper way :) + + // if (styles.align != _styles.align) { + bytes += codec.encode(styles.align == PosAlign.left ? cAlignLeft : (styles.align == PosAlign.center ? cAlignCenter : cAlignRight)); + _styles = _styles.copyWith(align: styles.align); + // } + + // if (styles.bold != _styles.bold) { + bytes += styles.bold ? cBoldOn.codeUnits : cBoldOff.codeUnits; + _styles = _styles.copyWith(bold: styles.bold); + // } if (styles.turn90 != _styles.turn90) { bytes += styles.turn90 ? cTurn90On.codeUnits : cTurn90Off.codeUnits; _styles = _styles.copyWith(turn90: styles.turn90); @@ -281,16 +277,13 @@ class Generator { _styles = _styles.copyWith(reverse: styles.reverse); } if (styles.underline != _styles.underline) { - bytes += - styles.underline ? cUnderline1dot.codeUnits : cUnderlineOff.codeUnits; + bytes += styles.underline ? cUnderline1dot.codeUnits : cUnderlineOff.codeUnits; _styles = _styles.copyWith(underline: styles.underline); } // Set font if (styles.fontType != null && styles.fontType != _styles.fontType) { - bytes += styles.fontType == PosFontType.fontB - ? cFontB.codeUnits - : cFontA.codeUnits; + bytes += styles.fontType == PosFontType.fontB ? cFontB.codeUnits : cFontA.codeUnits; _styles = _styles.copyWith(fontType: styles.fontType); } else if (_font != null && _font != _styles.fontType) { bytes += _font == PosFontType.fontB ? cFontB.codeUnits : cFontA.codeUnits; @@ -298,11 +291,9 @@ class Generator { } // Characters size - if (styles.height.value != _styles.height.value || - styles.width.value != _styles.width.value) { + if (styles.height.value != _styles.height.value || styles.width.value != _styles.width.value) { bytes += Uint8List.fromList( - List.from(cSizeGSn.codeUnits) - ..add(PosTextSize.decSize(styles.height, styles.width)), + List.from(cSizeGSn.codeUnits)..add(PosTextSize.decSize(styles.height, styles.width)), ); _styles = _styles.copyWith(height: styles.height, width: styles.width); } @@ -317,15 +308,12 @@ class Generator { // Set local code table if (styles.codeTable != null) { bytes += Uint8List.fromList( - List.from(cCodeTable.codeUnits) - ..add(_profile.getCodePageId(styles.codeTable)), + List.from(cCodeTable.codeUnits)..add(_profile.getCodePageId(styles.codeTable)), ); - _styles = - _styles.copyWith(align: styles.align, codeTable: styles.codeTable); + _styles = _styles.copyWith(align: styles.align, codeTable: styles.codeTable); } else if (_codeTable != null) { bytes += Uint8List.fromList( - List.from(cCodeTable.codeUnits) - ..add(_profile.getCodePageId(_codeTable)), + List.from(cCodeTable.codeUnits)..add(_profile.getCodePageId(_codeTable)), ); _styles = _styles.copyWith(align: styles.align, codeTable: _codeTable); } @@ -428,8 +416,7 @@ class Generator { /// Beeps [n] times /// /// Beep [duration] could be between 50 and 450 ms. - List beep( - {int n = 3, PosBeepDuration duration = PosBeepDuration.beep450ms}) { + List beep({int n = 3, PosBeepDuration duration = PosBeepDuration.beep450ms}) { List bytes = []; if (n <= 0) { return []; @@ -471,37 +458,28 @@ class Generator { List nextRow = []; for (int i = 0; i < cols.length; ++i) { - int colInd = - cols.sublist(0, i).fold(0, (int sum, col) => sum + col.width); + int colInd = cols.sublist(0, i).fold(0, (int sum, col) => sum + col.width); double charWidth = _getCharWidth(cols[i].styles); double fromPos = _colIndToPosition(colInd); - final double toPos = - _colIndToPosition(colInd + cols[i].width) - spaceBetweenRows; + final double toPos = _colIndToPosition(colInd + cols[i].width) - spaceBetweenRows; int maxCharactersNb = ((toPos - fromPos) / charWidth).floor(); if (!cols[i].containsChinese) { // CASE 1: containsChinese = false - Uint8List encodedToPrint = cols[i].textEncoded != null - ? cols[i].textEncoded! - : _encode(cols[i].text); + Uint8List encodedToPrint = cols[i].textEncoded != null ? cols[i].textEncoded! : _encode(cols[i].text); // If the col's content is too long, split it to the next row if (multiLine) { int realCharactersNb = encodedToPrint.length; if (realCharactersNb > maxCharactersNb) { // Print max possible and split to the next row - Uint8List encodedToPrintNextRow = - encodedToPrint.sublist(maxCharactersNb); + Uint8List encodedToPrintNextRow = encodedToPrint.sublist(maxCharactersNb); encodedToPrint = encodedToPrint.sublist(0, maxCharactersNb); isNextRow = true; - nextRow.add(PosColumn( - textEncoded: encodedToPrintNextRow, - width: cols[i].width, - styles: cols[i].styles)); + nextRow.add(PosColumn(textEncoded: encodedToPrintNextRow, width: cols[i].width, styles: cols[i].styles)); } else { // Insert an empty col - nextRow.add(PosColumn( - text: '', width: cols[i].width, styles: cols[i].styles)); + nextRow.add(PosColumn(text: '', width: cols[i].width, styles: cols[i].styles)); } } // end rows splitting @@ -529,15 +507,10 @@ class Generator { if (toPrintNextRow.isNotEmpty) { isNextRow = true; - nextRow.add(PosColumn( - text: toPrintNextRow, - containsChinese: true, - width: cols[i].width, - styles: cols[i].styles)); + nextRow.add(PosColumn(text: toPrintNextRow, containsChinese: true, width: cols[i].width, styles: cols[i].styles)); } else { // Insert an empty col - nextRow.add(PosColumn( - text: '', width: cols[i].width, styles: cols[i].styles)); + nextRow.add(PosColumn(text: '', width: cols[i].width, styles: cols[i].styles)); } // Print current row @@ -572,11 +545,10 @@ class Generator { /// Print an image using (ESC *) command /// /// [image] is an instance of class from [Image library](https://pub.dev/packages/image) - List image(Image imgSrc, - {PosAlign align = PosAlign.center, bool isDoubleDensity = true}) { + 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) { @@ -587,8 +559,7 @@ class Generator { size = 503 ~/ 2; } - image = - copyResize(imgSrc, width: size, interpolation: Interpolation.linear); + image = copyResize(imgSrc, width: size, interpolation: Interpolation.linear); } else { image = Image.from(imgSrc); // make a copy } @@ -611,8 +582,7 @@ class Generator { } final int heightPx = imageRotated.height; - int densityByte = - (highDensityHorizontal ? 1 : 0) + (highDensityVertical ? 32 : 0); + int densityByte = (highDensityHorizontal ? 1 : 0) + (highDensityVertical ? 32 : 0); final List header = List.from(cBitImg.codeUnits); header.add(densityByte); @@ -642,7 +612,7 @@ class Generator { }) { List bytes = []; // Image alignment - bytes += setStyles(PosStyles().copyWith(align: align)); + bytes += setStyles(const PosStyles().copyWith(align: align)); final int widthPx = image.width; final int heightPx = image.height; @@ -651,8 +621,7 @@ class Generator { if (imageFn == PosImageFn.bitImageRaster) { // GS v 0 - final int densityByte = - (highDensityVertical ? 0 : 1) + (highDensityHorizontal ? 0 : 2); + final int densityByte = (highDensityVertical ? 0 : 1) + (highDensityHorizontal ? 0 : 2); final List header = List.from(cRasterImg2.codeUnits); header.add(densityByte); // m @@ -694,7 +663,7 @@ class Generator { }) { List bytes = []; // Set alignment - bytes += setStyles(PosStyles().copyWith(align: align)); + bytes += setStyles(const PosStyles().copyWith(align: align)); // Set text position bytes += cBarcodeSelectPos.codeUnits + [textPos.value]; @@ -734,7 +703,7 @@ class Generator { }) { List bytes = []; // Set alignment - bytes += setStyles(PosStyles().copyWith(align: align)); + bytes += setStyles(const PosStyles().copyWith(align: align)); QRCode qr = QRCode(text, size, cor); bytes += qr.bytes; return bytes; @@ -800,15 +769,13 @@ class Generator { }) { List bytes = []; if (colInd != null) { - double charWidth = - _getCharWidth(styles, maxCharsPerLine: maxCharsPerLine); + double charWidth = _getCharWidth(styles, maxCharsPerLine: maxCharsPerLine); double fromPos = _colIndToPosition(colInd); // Align if (colWidth != 12) { // Update fromPos - final double toPos = - _colIndToPosition(colInd + colWidth) - spaceBetweenRows; + final double toPos = _colIndToPosition(colInd + colWidth) - spaceBetweenRows; final double textLen = textBytes.length * charWidth; if (styles.align == PosAlign.right) {