measureText static method
Measure the size of the given text
(or a single character by default) in
pixels using the given 2D context
and the given CSS font
string.
Implementation
static Vec2 measureText(html.CanvasRenderingContext2D context, String font,
[String? text]) {
text ??= 'a';
context.textBaseline = 'top';
context.font = font;
var metrics = context.measureText(String.fromCharCode(0x2588));
// get the width
int left = metrics.actualBoundingBoxLeft?.round() ?? 0;
int right = metrics.actualBoundingBoxRight?.round() ?? 0;
int width = math.max(metrics.width?.round() ?? 0, left.abs() + right.abs());
// get the height
int ascent = metrics.actualBoundingBoxAscent?.round() ?? 0;
int descent = metrics.actualBoundingBoxDescent?.round() ?? 0;
int height = ascent.abs() + descent.abs();
print(
'MEASURE: left $left, right $right, ascent $ascent, descent $descent');
print('CHAR width $width, height $height');
return Vec2(width, height);
}