getMetrics method

BitmapFontMetrics getMetrics(
  1. String string
)

Calculate the width and height in pixels of a text string

Implementation

BitmapFontMetrics getMetrics(String string) {
  if (string.isEmpty) {
    return const BitmapFontMetrics(0, 0);
  }

  var width = 0;
  var height = 0;
  final cu = string.codeUnits;

  for (var c in cu.sublist(0, cu.length - 1)) {
    if (!characters.containsKey(c)) {
      continue;
    }

    final ch = characters[c]!;
    width += ch.xAdvance;
    if (height < ch.height + ch.yOffset) {
      height = ch.height + ch.yOffset;
    }
  }

  final c = cu.last;
  if (characters.containsKey(c)) {
    final ch = characters[c]!;
    width += ch.width;
    if (height < ch.height + ch.yOffset) {
      height = ch.height + ch.yOffset;
    }
  }

  return BitmapFontMetrics(width, height);
}