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) {
  if (text == null) {
    return;
  }
  textStyle = textStyle.extendsStyle(this.config!.fontStyle!);

  var fontSize = getFontWidth(textStyle);
  var fontName = getFontName(textStyle);
  TextStyle style = TextStyle(color: Color(0xFF000000));
  TextAlign textAlign;

  switch (textStyle.align) {
    case AlignEnum.start:
      textAlign = TextAlign.left;
      break;
    case AlignEnum.center:
      textAlign = TextAlign.center;
      break;
    case AlignEnum.end:
      textAlign = TextAlign.right;
      break;
    default:
      textAlign = TextAlign.left;
  }

  ParagraphBuilder paragraphBuilder1 = ParagraphBuilder(ParagraphStyle(
      fontFamily: fontName,
      textAlign: textAlign,
      fontSize: fontSize.toDouble(),
      fontWeight: textStyle.fontBlod! ? FontWeight.bold : FontWeight.normal,
      maxLines: 1,
      strutStyle: StrutStyle(
        fontFamily: fontName,
        fontSize: fontSize.toDouble(),
        forceStrutHeight: true,
        height: textStyle.lineHeight,
      )));
  paragraphBuilder1.pushStyle(style);
  paragraphBuilder1.addText(text);
  var paragraph1 = paragraphBuilder1.build()
    ..layout(ParagraphConstraints(width: size.width!));

  canvas!.drawParagraph(paragraph1, Offset(offset.x!, offset.y!));
}