Skip to content

Fix visual order of nested bidi embeddings - #296

Merged
benoitkugler merged 2 commits into
go-text:mainfrom
egonelbre:fix/bidi-levels
Oct 1, 2026
Merged

benoitkugler merged 2 commits into
go-text:mainfrom
egonelbre:fix/bidi-levels

Conversation

@egonelbre

Copy link
Copy Markdown
Contributor

Nested bidi embeddings came out in the wrong visual order. Two bugs stacked up to cause it, one in bidi and one in shaping.

bidi.buildRuns only closed a run when the direction changed, so level 0 text followed by level 2 text inside an LRE landed in one run and Run.Level reported 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, computeBidiOrdering turned out to be wrong too. It reversed each L2 sequence by swapping VisualIndex of line[i] with line[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 TestComputeBidiOrdering expected the scrambled output. They now expect what UAX #9 L2 specifies.

Behaviour change for callers: splitByBidi emits 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.

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.

@benoitkugler benoitkugler left a comment

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.

Good catch, thank you !

@benoitkugler
benoitkugler merged commit 5052c25 into go-text:main Oct 1, 2026
7 checks passed
@egonelbre
egonelbre deleted the fix/bidi-levels branch October 1, 2026 14:48
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.

2 participants