layout method

void layout({
  1. double minHeight = 0.0,
  2. double maxHeight = double.infinity,
})

Computes the visual position of the glyphs for painting the text.

The text will layout with a height that's as close to its max intrinsic height (or its longest line, if textHeightBasis is set to TextHeightBasis.parent) as possible while still being greater than or equal to minHeight and less than or equal to maxHeight.

The text property must be non-null before this is called.

Implementation

void layout({double minHeight = 0.0, double maxHeight = double.infinity}) {
  assert(!maxHeight.isNaN);
  assert(!minHeight.isNaN);
  assert(() {
    _debugNeedsRelayout = false;
    return true;
  }());

  final _TextPainterLayoutCacheWithOffset? cachedLayout = _layoutCache;
  if (cachedLayout != null &&
      cachedLayout._resizeToFit(minHeight, maxHeight, textHeightBasis)) {
    return;
  }

  final TextSpan? text = this.text;
  if (text == null) {
    throw StateError(
        'MongolTextPainter.text must be set to a non-null value before using the MongolTextPainter.');
  }

  final double paintOffsetAlignment = _computePaintOffsetFraction(textAlign);
  // Try to avoid laying out the paragraph with maxHeight=double.infinity
  // when the text is not top-aligned, so we don't have to deal with an
  // infinite paint offset.
  final bool adjustMaxHeight =
      !maxHeight.isFinite && paintOffsetAlignment != 0;
  final double? adjustedMaxHeight = !adjustMaxHeight
      ? maxHeight
      : cachedLayout?.layout.maxIntrinsicLineExtent;
  _inputHeight = adjustedMaxHeight ?? maxHeight;

  // Only rebuild the paragraph when there're layout changes, even when
  // `_rebuildParagraphForPaint` is true. It's best to not eagerly rebuild
  // the paragraph to avoid the extra work, because:
  // 1. the text color could change again before `paint` is called (so one of
  //    the paragraph rebuilds is unnecessary)
  // 2. the user could be measuring the text layout so `paint` will never be
  //    called.
  final paragraph = (cachedLayout?.paragraph ?? _createParagraph(text))
    ..layout(MongolParagraphConstraints(height: _inputHeight));
  final newLayoutCache = _TextPainterLayoutCacheWithOffset(
    _MongolTextLayout._(paragraph),
    paintOffsetAlignment,
    minHeight,
    maxHeight,
    textHeightBasis,
  );
  // Call layout again if newLayoutCache had an infinite paint offset.
  // This is not as expensive as it seems, line breaking is relatively cheap
  // as compared to shaping.
  if (adjustedMaxHeight == null && minHeight.isFinite) {
    assert(maxHeight.isInfinite);
    final double newInputHeight =
        newLayoutCache.layout.maxIntrinsicLineExtent;
    paragraph.layout(MongolParagraphConstraints(height: newInputHeight));
    _inputHeight = newInputHeight;
  }
  _layoutCache = newLayoutCache;
}