Skip to content

Commit

Permalink
[shaping] Fix trailing spaces (#164)
Browse files Browse the repository at this point in the history
* properly collapse trailing white spaces

* do not reuse the same Output twice in tests
  • Loading branch information
benoitkugler authored May 5, 2024
1 parent 94e638b commit 6b7f526
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
5 changes: 3 additions & 2 deletions shaping/wrapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,12 +838,13 @@ func (l *LineWrapper) postProcessLine(finalLine Line, done bool) (WrappedLine, b
// zero trailing whitespace advance,
// to be coherent with Output.advanceSpaceAware
if L := len(finalRun.Glyphs); L != 0 {
g := &finalRun.Glyphs[L-1]
if finalRun.Direction.IsVertical() {
if g := &finalRun.Glyphs[L-1]; g.Height == 0 {
if g.Height == 0 {
g.YAdvance = 0
}
} else { // horizontal
if g := finalRun.Glyphs[L-1]; g.Width == 0 {
if g.Width == 0 {
g.XAdvance = 0
}
}
Expand Down
16 changes: 13 additions & 3 deletions shaping/wrapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2196,7 +2196,7 @@ func TestTruncationWithBreaking(t *testing.T) {
BreakPolicy: tc.policy,
TruncateAfterLines: 1,
Truncator: truncator,
}, tc.width, inputText, NewSliceIterator([]Output{output}))
}, tc.width, inputText, NewSliceIterator([]Output{output.copy()}))
if truncated != tc.truncated {
t.Errorf("expected %d truncated runes, got %d (total %d)", tc.truncated, truncated, len(inputText))
}
Expand Down Expand Up @@ -2233,7 +2233,11 @@ func TestGraphemeBreakingRegression(t *testing.T) {
// Wrap at increasingly unreasonable widths, checking for sane wrapping decisions.
for maxWidth := maxWidth; maxWidth > 0; maxWidth-- {
t.Run(fmt.Sprintf("maxWidth=%d", maxWidth), func(t *testing.T) {
lines, truncated := wrapper.WrapParagraph(WrapConfig{BreakPolicy: Always}, maxWidth, bidiText2, NewSliceIterator(shaped))
runs := make([]Output, len(shaped))
for i, run := range shaped {
runs[i] = run.copy()
}
lines, truncated := wrapper.WrapParagraph(WrapConfig{BreakPolicy: Always}, maxWidth, bidiText2, NewSliceIterator(runs))
checkRuneCounts(t, bidiText2, lines, truncated)

for i, baseLine := range lines[:len(lines)-1] {
Expand Down Expand Up @@ -2642,6 +2646,12 @@ func TestGraphemeBreakLigature(t *testing.T) {
}
}

func (out Output) copy() Output {
copy := out
copy.Glyphs = append([]Glyph(nil), out.Glyphs...)
return copy
}

func TestLineWrapperBreakSpecific(t *testing.T) {
type expectation struct {
name string
Expand Down Expand Up @@ -2974,7 +2984,7 @@ func TestLineWrapperBreakSpecific(t *testing.T) {
for _, expec := range tc.expectations {
t.Run(expec.name, func(t *testing.T) {
expec.config.Truncator = shapedTrunc
lines, truncated := w.WrapParagraph(expec.config, tc.maxWidth, tc.paragraph, NewSliceIterator([]Output{out}))
lines, truncated := w.WrapParagraph(expec.config, tc.maxWidth, tc.paragraph, NewSliceIterator([]Output{out.copy()}))
actualCounts := checkRuneCounts(t, tc.paragraph, lines, truncated)
if truncated != expec.truncatedCount {
t.Errorf("expected %d truncated runes, got %d", expec.truncatedCount, truncated)
Expand Down

0 comments on commit 6b7f526

Please sign in to comment.