computeFullTextSizeForWidth method

Size computeFullTextSizeForWidth(
  1. double maxWidth
)

Implementation

Size computeFullTextSizeForWidth(double maxWidth) {
  // Reuse span building to keep style consistent with actual painting.
  final span = _buildTextSpan();
  // Configure strut and text height behavior to honor CSS line-height.
  StrutStyle? _strut;
  final lh = renderStyle.lineHeight;
  if (lh.type != CSSLengthType.NORMAL) {
    final double fs = renderStyle.fontSize.computedValue;
    final double multiple = lh.computedValue / fs;
    if (multiple.isFinite && multiple > 0) {
      _strut = StrutStyle(
        fontSize: fs,
        height: multiple,
        fontFamilyFallback: renderStyle.fontFamily,
        fontStyle: renderStyle.fontStyle,
        fontWeight: renderStyle.fontWeight,
        // Minimum line-box height like CSS; allow expansion if content larger.
        forceStrutHeight: false,
      );
    }
  }
  const TextHeightBehavior _thb = TextHeightBehavior(
    applyHeightToFirstAscent: true,
    applyHeightToLastDescent: true,
    leadingDistribution: TextLeadingDistribution.even,
  );

  // Compute effective maxLines consistent with layout.
  final bool _nowrap = renderStyle.whiteSpace == WhiteSpace.nowrap;
  final bool _ellipsis = renderStyle.effectiveTextOverflow == TextOverflow.ellipsis;
  final int? _effectiveMaxLines = renderStyle.lineClamp ?? (_nowrap && _ellipsis ? 1 : null);

  final tp = TextPainter(
    text: span,
    textAlign: renderStyle.textAlign,
    textDirection: renderStyle.direction,
    ellipsis: _ellipsis ? '…' : null,
    maxLines: _effectiveMaxLines, // honor line-clamp or nowrap+ellipsis
    strutStyle: _strut,
    textHeightBehavior: _thb,
  );
  tp.layout(minWidth: 0, maxWidth: maxWidth.isFinite ? maxWidth : double.infinity);
  return Size(tp.width, tp.height);
}