Fix panic when slicing RTL text with non-monotonic clusters#1078
Open
StefanoD wants to merge 1 commit into
Open
Fix panic when slicing RTL text with non-monotonic clusters#1078StefanoD wants to merge 1 commit into
StefanoD wants to merge 1 commit into
Conversation
`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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
shape_text_with_fontassumed the shaper returns glyph clusters of an RTL run in strictly descending order, computing a cluster's byte range ascluster[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 madestart > endand panicked when slicing the source text ("byte range starts at 2 but ends at 0").Extract the boundary computation into
glyph_cluster_byte_rangeand normalize the range sostart <= endalways 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.