#3575·react-pdf

Expose textkit's excludeRects on <Text> (and fix an off-by-one in layoutParagraph's line widths)

Author: lonnieezellCreated Sep 25, 2026Updated Sep 25, 2026

textkit can already flow text around rects (container.excludeRects → generateLineRects). That's enough for floats, drop caps and text wrapping around images (#573). But @react-pdf/layout never sets excludeRects, so nothing in the public API can reach it. And once it's reachable, layoutParagraph narrows one line too many.

1. Pass excludeRects through from <Text>

getContainer in @react-pdf/layout builds the container without it. Adding the prop is a one-line change:

javascript
const getContainer = (width, height, node) => ({
  x: 0, y: 0, width, maxLines, height: height || Infinity, truncateMode: textOverflow,
  excludeRects: node.props?.excludeRects, // rects in the text box's own coordinates
});

TextProps would also need excludeRects?: { x: number; y: number; width: number; height: number }[].

2. Off-by-one in layoutParagraph's widths

javascript
const rects = generateLineRects(container, height$1);
const availableWidths = rects.map((r) => r.width);
availableWidths.unshift(availableWidths[0] - indent);

With no exclusions, rects is one rect, which gives [w - indent, w]. That's correct. With exclusions, rects is one rect per narrowed line plus the rest. The unshift adds an extra entry, so the linebreaker breaks line N + 1 at the narrow width, while layoutLines places it in the full-width rest rect. It then gets justified across the full width with huge gaps. Replacing the entry keeps both cases right:

javascript
const availableWidths = [
  rects[0].width - indent,
  ...rects.slice(1).map((r) => r.width),
  rects[rects.length - 1].width, // the linebreaker reads the last entry only for lines past the list
];

Repro (with change 1 applied)

javascript
<Text style={{ fontSize: 10, lineHeight: 1.5, textAlign: 'justify' }}
      excludeRects={[{ x: 0, y: 0, width: 120, height: 30 }]}>
  {'word '.repeat(120)}
</Text>

Lines 1–2 should be narrowed and line 3 onward full width. Without change 2, line 3 holds as many words as a narrowed line, spread across the full width.

We carry both as patch-package patches (layout 4.6.1, textkit 6.3.0) to draw real drop caps in PDF exports. The first letter is an absolutely positioned <Text>, and the paragraph flows around its rect. I'm happy to open a PR if this direction works for you.