Skip to content

Commit 528a051

Browse files
committed
[shaping] expose ComputeBidiOrdering
1 parent b5d89ff commit 528a051

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

shaping/wrapping.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -889,16 +889,20 @@ func swapVisualOrder(subline Line) {
889889
}
890890
}
891891

892-
// computeBidiOrdering resolves the [VisualIndex] of each run.
893-
func computeBidiOrdering(dir di.Direction, finalLine Line) {
892+
// ComputeBidiOrdering resolves the [VisualIndex] of each run.
893+
//
894+
// [paragraphDirection] describes the text layout of the overall paragraph, rather than
895+
// individual runs of text.
896+
func (finalLine Line) ComputeBidiOrdering(paragraphDirection di.Direction) {
897+
progression := paragraphDirection.Progression()
894898
bidiStart := -1
895899
for idx, run := range finalLine {
896900
basePosition := idx
897-
if dir.Progression() == di.TowardTopLeft {
901+
if progression == di.TowardTopLeft {
898902
basePosition = len(finalLine) - 1 - idx
899903
}
900904
finalLine[idx].VisualIndex = int32(basePosition)
901-
if run.Direction == dir {
905+
if run.Direction.Progression() == progression {
902906
if bidiStart != -1 {
903907
swapVisualOrder(finalLine[bidiStart:idx])
904908
bidiStart = -1
@@ -914,7 +918,7 @@ func computeBidiOrdering(dir di.Direction, finalLine Line) {
914918

915919
func (l *LineWrapper) postProcessLine(finalLine Line, done bool) (WrappedLine, bool) {
916920
if len(finalLine) > 0 {
917-
computeBidiOrdering(l.config.Direction, finalLine)
921+
finalLine.ComputeBidiOrdering(l.config.Direction)
918922
if !l.config.DisableTrailingWhitespaceTrim {
919923
// Here we find the last visual run in the line.
920924
goalIdx := len(finalLine) - 1
@@ -975,7 +979,7 @@ func (l *LineWrapper) postProcessLine(finalLine Line, done bool) (WrappedLine, b
975979
truncator.Runes.Offset = l.lineStartRune
976980
finalLine = append(finalLine, truncator)
977981
// We've just modified the line, we need to recompute the bidi ordering.
978-
computeBidiOrdering(l.config.Direction, finalLine)
982+
finalLine.ComputeBidiOrdering(l.config.Direction)
979983
}
980984
}
981985

shaping/wrapping_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3399,7 +3399,7 @@ func TestLineWrapPostProcess(t *testing.T) {
33993399
func TestComputeBidiOrdering(t *testing.T) {
34003400
type testcase struct {
34013401
name string
3402-
input []Output
3402+
input Line
34033403
direction di.Direction
34043404
expectedVisualOrder []int
34053405
}
@@ -3486,7 +3486,7 @@ func TestComputeBidiOrdering(t *testing.T) {
34863486
},
34873487
} {
34883488
t.Run(tc.name, func(t *testing.T) {
3489-
computeBidiOrdering(tc.direction, tc.input)
3489+
tc.input.ComputeBidiOrdering(tc.direction)
34903490
for visualIndex, logicalIndex := range tc.expectedVisualOrder {
34913491
if tc.input[logicalIndex].VisualIndex != int32(visualIndex) {
34923492
t.Errorf("line[%d]: expected visual index %v, got %v", logicalIndex, visualIndex, tc.input[logicalIndex].VisualIndex)

0 commit comments

Comments
 (0)