Skip to content

Commit 6113239

Browse files
zodimoandydotxyz
authored andcommitted
fix oneline overflow
1 parent eb6b289 commit 6113239

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

shaping/wrapping.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package shaping
22

33
import (
4+
"math"
45
"sort"
56

67
"github.com/go-text/typesetting/di"
@@ -784,6 +785,10 @@ func (l *LineWrapper) Prepare(config WrapConfig, paragraph []rune, runs RunItera
784785
//
785786
// See also [WrapParagraphF] which supports a decimal [maxWidth].
786787
func (l *LineWrapper) WrapParagraph(config WrapConfig, maxWidth int, paragraph []rune, runs RunIterator) (_ []Line, truncated int) {
788+
maxFixed := math.MaxInt32 >> 6
789+
if maxWidth > maxFixed {
790+
maxWidth = maxFixed
791+
}
787792
return l.WrapParagraphF(config, fixed.I(maxWidth), paragraph, runs)
788793
}
789794

shaping/wrapping_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package shaping
33
import (
44
"bytes"
55
"fmt"
6+
"math"
67
"os"
78
"reflect"
89
"sort"
@@ -3587,3 +3588,35 @@ func TestMaxWidthRouding(t *testing.T) {
35873588
line, _ = wr.WrapNextLineF(run.Advance)
35883589
tu.Assert(t, line.NextLine == 13)
35893590
}
3591+
3592+
func TestWrapping_oneLine_overflow_bug(t *testing.T) {
3593+
3594+
maxWidth := math.MaxInt
3595+
3596+
textInput := []rune("Lorem ipsum") // a simple input that fits on one line
3597+
face := benchEnFace
3598+
var shaper HarfbuzzShaper
3599+
out := []Output{shaper.Shape(Input{
3600+
Text: textInput,
3601+
RunStart: 0,
3602+
RunEnd: len(textInput),
3603+
Direction: di.DirectionLTR,
3604+
Face: face,
3605+
Size: fixed.I(16),
3606+
Script: language.Latin,
3607+
Language: language.NewLanguage("EN"),
3608+
})}
3609+
iter := NewSliceIterator(out)
3610+
var l LineWrapper
3611+
3612+
outs, _ := l.WrapParagraph(WrapConfig{BreakPolicy: Never}, maxWidth, textInput, iter)
3613+
if len(outs) != 1 {
3614+
t.Errorf("expected one line, got %d", len(outs))
3615+
}
3616+
3617+
// the run in iter should have been consumed
3618+
outs, _ = l.WrapParagraph(WrapConfig{BreakPolicy: Never}, maxWidth, textInput, iter)
3619+
if len(outs) != 0 {
3620+
t.Errorf("expected no line, got %d", len(outs))
3621+
}
3622+
}

0 commit comments

Comments
 (0)