measureText method

Point<num> measureText(
  1. BitmapFont font,
  2. String text
)

Measures the bounding box of a given text string when rendered with the specified BitmapFont.

This method accounts for character advances, tracking, and line breaks (\n).

  • font: The BitmapFont to use for measurement.
  • text: The string to measure.

Returns a Point where x is the total width and y is the total height.

Implementation

Point measureText(BitmapFont font, String text) {
  var x = 0.0;
  var w = 0.0;
  var h = font.leading;
  for (int charCode in text.runes) {
    var glyph = font.glyphs[charCode];
    if (glyph != null) {
      x += glyph.advance * font.tracking;
      if (x > w) w = x;
    }
    if (charCode == 10) {
      h += font.leading;
      x = 0;
    }
  }
  return Point(w, h);
}