diff --git a/shaping/wrapping.go b/shaping/wrapping.go index 195536d9..feb2d080 100644 --- a/shaping/wrapping.go +++ b/shaping/wrapping.go @@ -889,16 +889,20 @@ func swapVisualOrder(subline Line) { } } -// computeBidiOrdering resolves the [VisualIndex] of each run. -func computeBidiOrdering(dir di.Direction, finalLine Line) { +// ComputeBidiOrdering resolves the [VisualIndex] of each run. +// +// [paragraphDirection] describes the text layout of the overall paragraph, rather than +// individual runs of text. +func (finalLine Line) ComputeBidiOrdering(paragraphDirection di.Direction) { + progression := paragraphDirection.Progression() bidiStart := -1 for idx, run := range finalLine { basePosition := idx - if dir.Progression() == di.TowardTopLeft { + if progression == di.TowardTopLeft { basePosition = len(finalLine) - 1 - idx } finalLine[idx].VisualIndex = int32(basePosition) - if run.Direction == dir { + if run.Direction.Progression() == progression { if bidiStart != -1 { swapVisualOrder(finalLine[bidiStart:idx]) bidiStart = -1 @@ -914,7 +918,7 @@ func computeBidiOrdering(dir di.Direction, finalLine Line) { func (l *LineWrapper) postProcessLine(finalLine Line, done bool) (WrappedLine, bool) { if len(finalLine) > 0 { - computeBidiOrdering(l.config.Direction, finalLine) + finalLine.ComputeBidiOrdering(l.config.Direction) if !l.config.DisableTrailingWhitespaceTrim { // Here we find the last visual run in the line. goalIdx := len(finalLine) - 1 @@ -975,7 +979,7 @@ func (l *LineWrapper) postProcessLine(finalLine Line, done bool) (WrappedLine, b truncator.Runes.Offset = l.lineStartRune finalLine = append(finalLine, truncator) // We've just modified the line, we need to recompute the bidi ordering. - computeBidiOrdering(l.config.Direction, finalLine) + finalLine.ComputeBidiOrdering(l.config.Direction) } } diff --git a/shaping/wrapping_test.go b/shaping/wrapping_test.go index 6e9eaf79..8a092c41 100644 --- a/shaping/wrapping_test.go +++ b/shaping/wrapping_test.go @@ -3399,7 +3399,7 @@ func TestLineWrapPostProcess(t *testing.T) { func TestComputeBidiOrdering(t *testing.T) { type testcase struct { name string - input []Output + input Line direction di.Direction expectedVisualOrder []int } @@ -3486,7 +3486,7 @@ func TestComputeBidiOrdering(t *testing.T) { }, } { t.Run(tc.name, func(t *testing.T) { - computeBidiOrdering(tc.direction, tc.input) + tc.input.ComputeBidiOrdering(tc.direction) for visualIndex, logicalIndex := range tc.expectedVisualOrder { if tc.input[logicalIndex].VisualIndex != int32(visualIndex) { t.Errorf("line[%d]: expected visual index %v, got %v", logicalIndex, visualIndex, tc.input[logicalIndex].VisualIndex)