Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions shaping/wrapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down
4 changes: 2 additions & 2 deletions shaping/wrapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
Loading