Repository navigation
Fix visual order of nested bidi embeddings - #296
Merged
Merged
Conversation
buildRuns closed a run only when the direction changed, so level 0 text followed by level 2 text inside an LRE landed in one run, and Run.Level reported the first level for all of it. A run now ends whenever the level changes. "a\u202abא\u202cב" gives four runs at levels 0, 2, 3 and 1. Callers see more runs. buildRuns ends a run on any level change, not only on a direction change, so shaping.splitByBidi emits separate runs for same-direction embeddings and isolates. "a" followed by LRE "b" becomes two LTR runs, and kerning or ligatures no longer apply across an LRE or LRI boundary. Pango and ICU itemize the same way.
computeBidiOrdering reversed each sequence by swapping the VisualIndex
of line[i] with that of line[end-1-i]. That reverses the sequence only
while its indices are still in logical order. Once a higher level has
already reversed an inner sequence, the swap moves the wrong values
and the visual order comes out scrambled. Levels {0,2,2,1,1,1,0}
(an LTR paragraph, an RTL span, two LTR runs inside it) rendered as
if the two LTR runs were swapped and the RTL runs shuffled.
Reversals at higher levels only permute within a sequence, so the runs
of a sequence always hold its range of positions in some order.
Reflecting each index about the middle of that range reverses the
sequence whatever its current permutation is.
The two "bidi example" cases with nested levels expected the swapped
output and now expect the order UAX go-text#9 L2 specifies.
egonelbre
requested review from
andydotxyz,
benoitkugler and
whereswaldon
as code owners
October 1, 2026 13:21
benoitkugler
approved these changes
Oct 1, 2026
benoitkugler
left a comment
Contributor
There was a problem hiding this comment.
Good catch, thank you !
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.
Nested bidi embeddings came out in the wrong visual order. Two bugs stacked up to cause it, one in
bidiand one inshaping.bidi.buildRunsonly closed a run when the direction changed, so level 0 text followed by level 2 text inside an LRE landed in one run andRun.Levelreported level 0 for all of it. The shaper never saw the real levels. A run now ends whenever the level changes."a\u202abא\u202cב"gives four runs at levels 0, 2, 3 and 1 instead of two.With real levels flowing through,
computeBidiOrderingturned out to be wrong too. It reversed each L2 sequence by swappingVisualIndexofline[i]withline[end-1-i]. That only works while the indices are still in logical order. Once a higher level has reversed an inner sequence, the swap moves the wrong values. Levels{0,2,2,1,1,1,0}, an RTL span with two LTR runs inside an LTR paragraph, rendered with the LTR runs swapped and the RTL runs shuffled. Reversals at higher levels only permute within a sequence, so each sequence always owns a contiguous range of positions. Reflecting every index about the middle of that range reverses the sequence no matter how it is currently permuted.Two existing "bidi example" cases in
TestComputeBidiOrderingexpected the scrambled output. They now expect whatUAX #9 L2specifies.Behaviour change for callers:
splitByBidiemits separate runs for same-direction embeddings and isolates."a"followed by LRE"b"becomes two LTR runs, so kerning and ligatures no longer apply across an LRE or LRI boundary. Pango and ICU itemize the same way.