drawStyledContent function

void drawStyledContent(
  1. Canvas canvas,
  2. String content,
  3. int startX,
  4. int startY,
  5. UvStyle bgStyle, {
  6. bool transparent = false,
  7. int? contentWidth,
  8. int? contentHeight,
})

Implementation

void drawStyledContent(
  Canvas canvas,
  String content,
  int startX,
  int startY,
  UvStyle bgStyle, {
  bool transparent = false,
  int? contentWidth,
  int? contentHeight,
}) {
  final span = TuiTrace.begin(
    '_drawStyledContent',
    tag: TraceTag.paint,
    extra:
        'startX=$startX startY=$startY canvasW=${canvas.width()} canvasH=${canvas.height()}',
  );
  final styled = StyledString(content);
  final (styledWidth, styledHeight) = switch ((contentWidth, contentHeight)) {
    (final int width, final int height) => (width, height),
    _ => () {
      final bounds = styled.bounds();
      return (bounds.width, bounds.height);
    }(),
  };
  final tempCanvas = Canvas(styledWidth, styledHeight);
  styled.draw(tempCanvas, tempCanvas.bounds());

  for (var y = 0; y < styledHeight; y++) {
    for (var x = 0; x < styledWidth; x++) {
      final destX = startX + x;
      final destY = startY + y;

      if (destX < 0 || destY < 0) continue;
      if (destX >= canvas.width() || destY >= canvas.height()) continue;

      final srcCell = tempCanvas.cellAt(x, y);
      if (srcCell == null || srcCell.isZero) continue;
      final normalizedStyle = srcCell.style;
      final isSingleWidthSpace = srcCell.content == ' ' && srcCell.width == 1;

      // Skip layout/padding spaces that have no visible styling of their own so
      // the destination background remains visible. This covers both plain empty
      // cells and spaces that only carried a foreground/default-background style
      // after ANSI round-tripping.
      //
      // We unconditionally skip empty cells and transparent spaces — regardless
      // of whether the current container has a background color — so that
      // Layout.place/pad trailing-space padding never overwrites background fill
      // cells laid down by an inner widget. Without this, a no-bg intermediate
      // container (e.g. Container(width: 58) with no color) would pass empty
      // Layout.place spaces through to the parent canvas, overwriting the inner
      // widget's bg=highlight fill cells.
      final hasVisibleSpaceAttrs =
          (normalizedStyle.attrs & 32) != 0; // 32 == Attr.reverse
      final isTransparentSpace =
          isSingleWidthSpace &&
          normalizedStyle.fg == null &&
          normalizedStyle.bg == null &&
          normalizedStyle.underlineColor == null &&
          normalizedStyle.underline == UnderlineStyle.none &&
          !hasVisibleSpaceAttrs &&
          srcCell.link.isZero;
      if (srcCell.isEmpty || isTransparentSpace) {
        continue;
      }

      final mergedStyle = normalizedStyle.bg == null && bgStyle.bg != null
          ? normalizedStyle.copyWith(bg: bgStyle.bg)
          : normalizedStyle;

      canvas.setCell(
        destX,
        destY,
        Cell(
          content: srcCell.content,
          width: srcCell.width,
          style: mergedStyle,
          link: srcCell.link,
        ),
      );
    }
  }
  span.end(extra: 'bounds=${styledWidth}x$styledHeight');
}