From 388a113a019e509dac00778506c4dd67cb7b87d9 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 14 Sep 2023 20:28:32 -0400 Subject: [PATCH] Text bounds are now returned correctly --- layout.go | 14 +++++++++----- setter.go | 26 ++++++++++++++------------ 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/layout.go b/layout.go index 9816493..2d5c547 100644 --- a/layout.go +++ b/layout.go @@ -111,7 +111,7 @@ type LineLayout struct { // wrap is set to true, this function will stop processing words once maxWidth // is crossed. The word which would have crossed over the limit will not be // processed. -func DoLine (text []rune, face font.Face, wrap bool, maxWidth fixed.Int26_6) (line LineLayout, remaining []rune) { +func DoLine (text []rune, face font.Face, wrap bool, width fixed.Int26_6) (line LineLayout, remaining []rune) { remaining = text x := fixed.Int26_6(0) lastWord := WordLayout { } @@ -122,9 +122,9 @@ func DoLine (text []rune, face font.Face, wrap bool, maxWidth fixed.Int26_6) (li word.X = x x += word.Width - // if we have gone over the maximum width, stop processing + // if we have gone over the preferred width, stop processing // words (if wrap is enabled) - if !isFirstWord && wrap && x > maxWidth { + if !isFirstWord && wrap && x > width { break } @@ -149,9 +149,13 @@ func DoLine (text []rune, face font.Face, wrap bool, maxWidth fixed.Int26_6) (li } // set the width of the line's content. - line.Width = maxWidth line.ContentWidth = lastWord.X + lastWord.Width - line.SpaceAfter = lastWord.SpaceAfter + if wrap { + line.Width = width + } else { + line.Width = line.ContentWidth + } + line.SpaceAfter = lastWord.SpaceAfter return } diff --git a/setter.go b/setter.go index 6197a75..522704a 100644 --- a/setter.go +++ b/setter.go @@ -55,7 +55,7 @@ func (setter *TypeSetter) needLayout () { setter.lines = append(setter.lines, line) } - // process every line + // process every line until there are no more remaining runes for len(remaining) > 0 { line, remainingFromLine := DoLine ( remaining, setter.face, setter.wrap, @@ -73,8 +73,8 @@ func (setter *TypeSetter) needLayout () { // calculate layout boundaries setter.minWidth = horizontalExtentSpace - setter.layoutBounds.Max.X = setter.maxWidth - setter.layoutBoundsSpace.Max.X = setter.maxWidth + setter.layoutBounds.Max.X = horizontalExtent.Round() + setter.layoutBoundsSpace.Max.X = horizontalExtentSpace.Round() y -= metrics.Height if setter.maxHeight == 0 { @@ -138,7 +138,10 @@ func (setter *TypeSetter) SetFace (face font.Face) { setter.face = face } -// SetMaxWidth sets the maximum width of the typesetter. +// TODO rename to SetWidth and SetHeight + +// SetWidth sets the width of the typesetter. Text will still be able +// to overflow outside of this width if wrapping is disabled. func (setter *TypeSetter) SetMaxWidth (width int) { if setter.maxWidth == width { return } setter.layoutClean = false @@ -146,9 +149,9 @@ func (setter *TypeSetter) SetMaxWidth (width int) { setter.maxWidth = width } -// SetMaxHeight sets the maximum height of the typesetter. If the maximum height -// is greater than zero, no lines will be laid out past that point. If the -// maximum height is zero, the text's maximum height will not be constrained. +// SetHeight sets the height of the typesetter. If the height is greater than +// zero, no lines will be laid out past it. If the height is zero, the text's +// maximum height will not be constrained. func (setter *TypeSetter) SetMaxHeight (heignt int) { if setter.maxHeight == heignt { return } setter.layoutClean = false @@ -170,14 +173,13 @@ func (setter *TypeSetter) LineHeight () fixed.Int26_6 { return setter.face.Metrics().Height } -// MaxWidth returns the maximum width of the typesetter as set by SetMaxWidth. -func (setter *TypeSetter) MaxWidth () int { +// Width returns the height of the typesetter as set by SetWidth. +func (setter *TypeSetter) Width () int { return setter.maxWidth } -// MaxHeight returns the maximum height of the typesetter as set by -// SetMaxHeight. -func (setter *TypeSetter) MaxHeight () int { +// Height returns the height of the typesetter as set by SetHeight. +func (setter *TypeSetter) Height () int { return setter.maxHeight }