estimateLength method

int estimateLength(
  1. String text,
  2. int contentWidth,
  3. PrinterTextStyle style
)

获取text限制在contentWidth后的长度

text 文字

contentWidth 限制范围

style 文字样式

Implementation

int estimateLength(String text, int contentWidth, PrinterTextStyle style) {
  int ascCharWidth = this.getFontAsciiWidth(style);
  int cnCharWidth = this.getFontWidth(style);
  int width = 0;
  int i = 0;
  List<int> codeUnits = text.codeUnits;
  for (int len = codeUnits.length; i < len; i++) {
    int charCode = codeUnits[i];
    var charWidth =
        (0 <= charCode && charCode <= 127) ? ascCharWidth : cnCharWidth;

    if (charWidth + width <= contentWidth) {
      width += charWidth;
    } else {
      break;
    }
  }
  return i;
}