Skip to content

fix(rendering): normalize RTL caption glyph offset to prevent bounding box overflow (#2284) - #2289

Open
Dewin wants to merge 2 commits into
CapSoftware:mainfrom
Dewin:fix/rtl-captions-bounding-box-2284
Open

fix(rendering): normalize RTL caption glyph offset to prevent bounding box overflow (#2284)#2289
Dewin wants to merge 2 commits into
CapSoftware:mainfrom
Dewin:fix/rtl-captions-bounding-box-2284

Conversation

@Dewin

@Dewin Dewin commented Sep 13, 2026

Copy link
Copy Markdown

What does this PR do?

Resolves issue #2284 where Right-to-Left (RTL) AI captions (e.g., Persian, Arabic) shifted horizontally and overflowed outside the black background bounding container.

Root Cause

When cosmic-text shapes RTL text within an unconstrained or wide line width, glyphs in the layout run are positioned with a non-zero positive starting offset (min_glyph_x > 0). When the renderer anchored the TextArea at text_left = draw_box_left + padding, the initial glyph offset shifted the rendered text to the right by that distance, causing the text to spill outside the bounding box and get clipped.

Changes

  • Track minimum glyph x-offset across layout runs in crates/rendering/src/layers/captions.rs.
  • Subtract the normalized glyph offset from text_left during TextArea preparation so both LTR and RTL text begin cleanly at the container's interior padding boundary.
  • Added regression unit tests for RTL layout offset normalization.

Verification

  • Self-reviewed against rendering coordinate pipeline.
  • Verified bounds calculation and text alignment.

RetriggerConfidence Score: 4/5

The rendering change appears behaviorally safe, but the explicit repository comments requirement must be satisfied before merging; strengthening the synthetic regression test is also recommended.

Findings

  1. P2 Test Skips Real RTL Shaping
  2. P2 Comments Merely Narrate Assertions
Fix with agent prompt
### Issue 1
crates/rendering/src/layers/captions.rs:1277
This test supplies a synthetic `min_glyph_x` and repeats the production coordinate formula without invoking RTL shaping or `LayoutRunIter`. It therefore cannot catch incorrect assumptions about real glyph offsets or scaling, reducing its value as regression coverage for the reported rendering failure. Exercise an actual RTL layout and assert its normalized glyph extent.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

### Issue 2
crates/rendering/src/layers/captions.rs:1309
The comments here and at line 1315 only restate what the following assertions check. This violates the repository directive to avoid comments that narrate the code, so these comments must be removed before merging; the assertions already communicate the behavior.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

  • Tracks the minimum glyph x-coordinate across layout runs.
  • Compensates TextArea.left while keeping clipping bounds anchored to the visible content area.
  • Adds helper-level regression assertions for offset normalization and bounds calculations.

Reviews (1) · Last reviewed commit: "fix(rendering): normalize RTL caption gl..."

assert_eq!(word_byte_range("missing", &words, 0, false), None);
}

#[test]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Test Skips Real RTL Shaping

This test supplies a synthetic min_glyph_x and repeats the production coordinate formula without invoking RTL shaping or LayoutRunIter. It therefore cannot catch incorrect assumptions about real glyph offsets or scaling, reducing its value as regression coverage for the reported rendering failure. Exercise an actual RTL layout and assert its normalized glyph extent.

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/rendering/src/layers/captions.rs
Line: 1277

Comment:
**Test Skips Real RTL Shaping**

This test supplies a synthetic `min_glyph_x` and repeats the production coordinate formula without invoking RTL shaping or `LayoutRunIter`. It therefore cannot catch incorrect assumptions about real glyph offsets or scaling, reducing its value as regression coverage for the reported rendering failure. Exercise an actual RTL layout and assert its normalized glyph extent.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed: added
tl_text_shaping_glyph_offset\ test invoking cosmic-text Buffer shaping with Persian/Arabic script and asserting layout run glyph offsets.

Comment thread crates/rendering/src/layers/captions.rs Outdated
bottom: (text_top + draw_text_height + 2.0).ceil() as i32,
};

// Rendered glyphs start flush at the interior left padding

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Comments Merely Narrate Assertions

The comments here and at line 1315 only restate what the following assertions check. This violates the repository directive to avoid comments that narrate the code, so these comments must be removed before merging; the assertions already communicate the behavior.

Context Used: CLAUDE.md (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/rendering/src/layers/captions.rs
Line: 1309

Comment:
**Comments Merely Narrate Assertions**

The comments here and at line 1315 only restate what the following assertions check. This violates the repository directive to avoid comments that narrate the code, so these comments must be removed before merging; the assertions already communicate the behavior.

**Context Used:** CLAUDE.md ([source](https://github.com/capsoftware/cap/blob/main/CLAUDE.md))

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed: removed narrating comments per CLAUDE.md guidelines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: RTL AI Captions (Persian/Arabic) overflow outside the background bounding box

1 participant