getBoundsForRenderBox method

Rect? getBoundsForRenderBox(
  1. RenderBox targetBox
)

Get the bounding rectangle for a specific inline element across all line fragments.

Implementation

Rect? getBoundsForRenderBox(RenderBox targetBox) {
  // Paragraph path: bounds for atomic inline placeholders
  final idx = _placeholderOrder.indexOf(targetBox);
  if (idx >= 0 && idx < _placeholderBoxes.length) {
    final r = _placeholderBoxes[idx];
    final double left = r.left - (_paragraphMinLeft.isFinite ? _paragraphMinLeft : 0.0);
    return Rect.fromLTWH(left, r.top, r.right - r.left, r.bottom - r.top);
  }
  // For inline element ranges (non-atomic), approximate using getBoxesForRange if recorded
  if (targetBox is RenderBoxModel) {
    final range = _elementRanges[targetBox];
    if (range != null && _paragraph != null) {
      List<ui.TextBox> rects = _paragraph!.getBoxesForRange(range.$1, range.$2);
      bool synthesized = false;
      if (rects.isEmpty) {
        rects = _synthesizeRectsForEmptySpan(targetBox);
        synthesized = rects.isNotEmpty;
      }
      if (rects.isNotEmpty) {
        if (!synthesized) {
          double? minX, minY, maxX, maxY;
          final double offsetX = (_paragraphMinLeft.isFinite ? _paragraphMinLeft : 0.0);
          for (final tb in rects) {
            final double adjLeft = tb.left - offsetX;
            final double adjRight = tb.right - offsetX;
            minX = (minX == null) ? adjLeft : math.min(minX, adjLeft);
            minY = (minY == null) ? tb.top : math.min(minY, tb.top);
            maxX = (maxX == null) ? adjRight : math.max(maxX, adjRight);
            maxY = (maxY == null) ? tb.bottom : math.max(maxY, tb.bottom);
          }
          return Rect.fromLTRB(minX!, minY!, maxX!, maxY!);
        } else {
          // For synthesized empty spans, include effective line-height vertically to match visual area
          final style = targetBox.renderStyle;
          final padT = style.paddingTop.computedValue;
          final bT = style.effectiveBorderTopWidth.computedValue;
          final padB = style.paddingBottom.computedValue;
          final bB = style.effectiveBorderBottomWidth.computedValue;
          final lh = _effectiveLineHeightPx(style);
          final tb = rects.first;
          final double offsetX = (_paragraphMinLeft.isFinite ? _paragraphMinLeft : 0.0);
          final double left = tb.left - offsetX;
          final double right = tb.right - offsetX;
          final double top = tb.top - (lh + padT + bT);
          final double bottom = tb.top + (padB + bB);
          return Rect.fromLTRB(left, top, right, bottom);
        }
      }
    }
  }
  return null;
}