setStyles method

List<int> setStyles(
  1. PosStyles styles, {
  2. bool isKanji = false,
})

Implementation

List<int> setStyles(PosStyles styles, {bool isKanji = false}) {
  List<int> bytes = [];
  if (styles.align != _styles.align) {
    bytes += encodeChars(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);
  }
  if (styles.reverse != _styles.reverse) {
    bytes += styles.reverse ? cReverseOn.codeUnits : cReverseOff.codeUnits;
    _styles = _styles.copyWith(reverse: styles.reverse);
  }
  if (styles.underline != _styles.underline) {
    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;
    _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) {
    bytes += Uint8List.fromList(
      List.from(cSizeGSn.codeUnits)
        ..add(PosTextSize.decSize(styles.height, styles.width)),
    );
    _styles = _styles.copyWith(height: styles.height, width: styles.width);
  }

  // Set Kanji mode
  if (isKanji) {
    bytes += cKanjiOn.codeUnits;
  } else {
    bytes += cKanjiOff.codeUnits;
  }

  // Set local code table
  if (styles.codeTable != null) {
    bytes += Uint8List.fromList(
      List.from(cCodeTable.codeUnits)
        ..add(_profile.getCodePageId(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)),
    );
    _styles = _styles.copyWith(align: styles.align, codeTable: _codeTable);
  }

  return bytes;
}