Skip to content

Fix panic when slicing RTL text with non-monotonic clusters#1078

Open
StefanoD wants to merge 1 commit into
linebender:mainfrom
StefanoD:fix/1044-rtl-cluster-slice-panic
Open

Fix panic when slicing RTL text with non-monotonic clusters#1078
StefanoD wants to merge 1 commit into
linebender:mainfrom
StefanoD:fix/1044-rtl-cluster-slice-panic

Conversation

@StefanoD

Copy link
Copy Markdown

shape_text_with_font assumed the shaper returns glyph clusters of an RTL run in strictly descending order, computing a cluster's byte range as cluster[i]..cluster[i-1]. Some fonts/shapers violate this (e.g. some builds of Times New Roman shaping the Arabic text "الويب" return ascending clusters [0, 2, 4, 6, 8] for the RTL run), which made start > end and panicked when slicing the source text ("byte range starts at 2 but ends at 0").

Extract the boundary computation into glyph_cluster_byte_range and normalize the range so start <= end always holds. Both ends stay on cluster (char) boundaries, so the slice remains UTF-8 safe.

Add unit tests covering the LTR, normal RTL, and the non-monotonic RTL case from the bug report.

Fixes #1044

Generated by Claude


Note: I accidentally closed the original PR (#1060) by deleting my fork, which auto-closed it. This reopens the same change — the branch and commits are unchanged.

`shape_text_with_font` assumed the shaper returns glyph clusters of an
RTL run in strictly descending order, computing a cluster's byte range as
`cluster[i]..cluster[i-1]`. Some fonts/shapers violate this (e.g. some
builds of Times New Roman shaping the Arabic text "الويب" return ascending
clusters `[0, 2, 4, 6, 8]` for the RTL run), which made `start > end` and
panicked when slicing the source text ("byte range starts at 2 but ends
at 0").

Extract the boundary computation into `glyph_cluster_byte_range` and
normalize the range so `start <= end` always holds. Both ends stay on
cluster (char) boundaries, so the slice remains UTF-8 safe.

Add unit tests covering the LTR, normal RTL, and the non-monotonic RTL
case from the bug report.

Fixes linebender#1044

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.

thread '<unnamed>' panicked at crates\usvg\src\text\layout.rs:1505:35: begin > end (2 > 0) when slicing الويب

1 participant