Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hotfix - text alignment not working. #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 53 additions & 86 deletions lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -53,21 +52,15 @@ class Generator {
if (styles.fontType != null) {
charsPerLine = _getMaxCharsPerLine(styles.fontType);
} else {
charsPerLine =
_maxCharsPerLine ?? _getMaxCharsPerLine(_styles.fontType);
charsPerLine = _maxCharsPerLine ?? _getMaxCharsPerLine(_styles.fontType);
}
}
return charsPerLine;
}

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 {
Expand Down Expand Up @@ -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<int> res = <int>[];
Expand All @@ -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);
Expand Down Expand Up @@ -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 ************************

Expand All @@ -220,16 +210,15 @@ class Generator {
List<int> reset() {
List<int> bytes = [];
bytes += cInit.codeUnits;
_styles = PosStyles();
_styles = const PosStyles();
bytes += setGlobalCodeTable(_codeTable);
bytes += setGlobalFont(_font);
return bytes;
}

/// Clear the buffer and reset text styles
List<int> 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
Expand Down Expand Up @@ -261,17 +250,24 @@ class Generator {

List<int> setStyles(PosStyles styles, {bool isKanji = false}) {
List<int> 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);
Expand All @@ -281,28 +277,23 @@ 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;
_styles = _styles.copyWith(fontType: _font);
}

// 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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -428,8 +416,7 @@ class Generator {
/// Beeps [n] times
///
/// Beep [duration] could be between 50 and 450 ms.
List<int> beep(
{int n = 3, PosBeepDuration duration = PosBeepDuration.beep450ms}) {
List<int> beep({int n = 3, PosBeepDuration duration = PosBeepDuration.beep450ms}) {
List<int> bytes = [];
if (n <= 0) {
return [];
Expand Down Expand Up @@ -471,37 +458,28 @@ class Generator {
List<PosColumn> nextRow = <PosColumn>[];

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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<int> image(Image imgSrc,
{PosAlign align = PosAlign.center, bool isDoubleDensity = true}) {
List<int> image(Image imgSrc, {PosAlign align = PosAlign.center, bool isDoubleDensity = true}) {
List<int> bytes = [];
// Image alignment
bytes += setStyles(PosStyles().copyWith(align: align));
bytes += setStyles(const PosStyles().copyWith(align: align));

Image image;
if (!isDoubleDensity) {
Expand All @@ -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
}
Expand All @@ -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<int> header = List.from(cBitImg.codeUnits);
header.add(densityByte);
Expand Down Expand Up @@ -642,7 +612,7 @@ class Generator {
}) {
List<int> 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;
Expand All @@ -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<int> header = List.from(cRasterImg2.codeUnits);
header.add(densityByte); // m
Expand Down Expand Up @@ -694,7 +663,7 @@ class Generator {
}) {
List<int> bytes = [];
// Set alignment
bytes += setStyles(PosStyles().copyWith(align: align));
bytes += setStyles(const PosStyles().copyWith(align: align));

// Set text position
bytes += cBarcodeSelectPos.codeUnits + [textPos.value];
Expand Down Expand Up @@ -734,7 +703,7 @@ class Generator {
}) {
List<int> 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;
Expand Down Expand Up @@ -800,15 +769,13 @@ class Generator {
}) {
List<int> 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) {
Expand Down