drawText method

  1. @override
dynamic drawText(
  1. String? text,
  2. PrinterOffset offset,
  3. PrinterSize size,
  4. PrinterTextStyle textStyle,
)
override

打印文字

text 内容

offset 偏移位置

size 大小

textStyle 文字样式

Implementation

@override
drawText(String? text, PrinterOffset offset, PrinterSize size, PrinterTextStyle textStyle) {

  textStyle = textStyle.extendsStyle(this.config!.fontStyle!);

  var fontHeight = getFontHeight(textStyle);
  var height = fontHeight.toDouble() * (textStyle.lineHeight ?? 1);

  if (text == null || text.trim().isEmpty) {
    return offset.y! + height;
  }

  var textLeft =
      this.calcTextLeft(text, size.width!.toInt(), textStyle.align!, textStyle);
  if (textLeft < 0) {
    text = splitLine(text, size.width!.toInt(), textStyle)[0];
    textLeft = this
        .calcTextLeft(text, size.width!.toInt(), textStyle.align!, textStyle);
  }
  var x = offset.x! + textLeft;
  var y = offset.y! + (height - fontHeight) ~/ 2;

  if (text.trim().isNotEmpty) {
    this.addCommand(
        'TEXT ${x.toInt()},${y.toInt()},"${textStyle.fontType}",0,${textStyle.fontSize},${textStyle.fontSize},"$text"');
  }
}