measure static method

CellMetrics measure(
  1. TextStyle style, {
  2. int sample = 20,
  3. double offsetX = 0,
  4. double offsetY = 0,
})

Measure cell size for style.

contentHeight = primaryLineHeight(style) * (style.height ?? 1.0)
cellHeight    = contentHeight + offsetY
cellWidth     = avg('W') + offsetX

Implementation

static CellMetrics measure(
  TextStyle style, {
  int sample = 20,
  double offsetX = 0,
  double offsetY = 0,
}) {
  final fontSize = style.fontSize ?? 14.0;
  final heightMul = style.height ?? 1.0;

  final ascii = TextPainter(
    text: TextSpan(
      text: 'W' * sample,
      style: TextStyle(
        fontFamily: style.fontFamily,
        fontSize: fontSize,
        fontWeight: style.fontWeight,
        fontStyle: style.fontStyle,
      ),
    ),
    textDirection: TextDirection.ltr,
  )..layout();

  // Floor so a broken face cannot yield a zero-height grid.
  final safeContent = math.max(primaryLineHeight(style) * heightMul, 1.0);

  return CellMetrics(
    ascii.width / sample + offsetX,
    safeContent + offsetY,
    contentHeight: safeContent,
  );
}