diff --git a/shaping/wrapping.go b/shaping/wrapping.go index f03ed51..1ebc535 100644 --- a/shaping/wrapping.go +++ b/shaping/wrapping.go @@ -1,6 +1,7 @@ package shaping import ( + "math" "sort" "github.com/go-text/typesetting/di" @@ -784,6 +785,10 @@ func (l *LineWrapper) Prepare(config WrapConfig, paragraph []rune, runs RunItera // // See also [WrapParagraphF] which supports a decimal [maxWidth]. func (l *LineWrapper) WrapParagraph(config WrapConfig, maxWidth int, paragraph []rune, runs RunIterator) (_ []Line, truncated int) { + maxFixed := math.MaxInt32 >> 6 + if maxWidth > maxFixed { + maxWidth = maxFixed + } return l.WrapParagraphF(config, fixed.I(maxWidth), paragraph, runs) } diff --git a/shaping/wrapping_test.go b/shaping/wrapping_test.go index e11c86d..9f1bb94 100644 --- a/shaping/wrapping_test.go +++ b/shaping/wrapping_test.go @@ -3,6 +3,7 @@ package shaping import ( "bytes" "fmt" + "math" "os" "reflect" "sort" @@ -3587,3 +3588,35 @@ func TestMaxWidthRouding(t *testing.T) { line, _ = wr.WrapNextLineF(run.Advance) tu.Assert(t, line.NextLine == 13) } + +func TestWrapping_oneLine_overflow_bug(t *testing.T) { + + maxWidth := math.MaxInt + + textInput := []rune("Lorem ipsum") // a simple input that fits on one line + face := benchEnFace + var shaper HarfbuzzShaper + out := []Output{shaper.Shape(Input{ + Text: textInput, + RunStart: 0, + RunEnd: len(textInput), + Direction: di.DirectionLTR, + Face: face, + Size: fixed.I(16), + Script: language.Latin, + Language: language.NewLanguage("EN"), + })} + iter := NewSliceIterator(out) + var l LineWrapper + + outs, _ := l.WrapParagraph(WrapConfig{BreakPolicy: Never}, maxWidth, textInput, iter) + if len(outs) != 1 { + t.Errorf("expected one line, got %d", len(outs)) + } + + // the run in iter should have been consumed + outs, _ = l.WrapParagraph(WrapConfig{BreakPolicy: Never}, maxWidth, textInput, iter) + if len(outs) != 0 { + t.Errorf("expected no line, got %d", len(outs)) + } +}