getOrBuild method

TextPainter getOrBuild({
  1. required TextNodeSnapshot node,
  2. required TextStyle textStyle,
  3. required double maxWidth,
  4. TextDirection textDirection = TextDirection.ltr,
})

Implementation

TextPainter getOrBuild({
  required TextNodeSnapshot node,
  required TextStyle textStyle,
  required double maxWidth,
  TextDirection textDirection = TextDirection.ltr,
}) {
  final safeFontSize = clampPositiveFinite(node.fontSize, fallback: 24);
  final safeLineHeight =
      (node.lineHeight != null &&
          node.lineHeight!.isFinite &&
          node.lineHeight! > 0)
      ? node.lineHeight
      : null;
  final key = _TextLayoutKeyV2(
    text: node.text,
    fontSize: safeFontSize,
    fontFamily: node.fontFamily,
    isBold: node.isBold,
    isItalic: node.isItalic,
    isUnderline: node.isUnderline,
    align: node.align,
    lineHeight: safeLineHeight,
    maxWidth: clampNonNegativeFinite(maxWidth),
    color: textStyle.color ?? const Color(0xFF000000),
    textDirection: textDirection,
  );

  final cached = _entries.remove(key);
  if (cached != null) {
    _entries[key] = cached;
    _debugHitCount += 1;
    return cached;
  }

  final textPainter = TextPainter(
    text: TextSpan(text: node.text, style: textStyle),
    textAlign: node.align,
    textDirection: textDirection,
    maxLines: null,
  );
  textPainter.layout(maxWidth: key.maxWidth);
  _entries[key] = textPainter;
  _debugBuildCount += 1;
  _evictIfNeeded();
  return textPainter;
}