From c207eadfb87eca7751a8b77d53a4a9dd433b7c8b Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 15 Jul 2023 20:19:35 -0400 Subject: [PATCH] Typeset now only uses SetWrap instead of relying zero values --- layout.go | 23 ++++++++--------------- setter.go | 37 +++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 35 deletions(-) diff --git a/layout.go b/layout.go index dbcaf9f..6a14459 100644 --- a/layout.go +++ b/layout.go @@ -108,10 +108,10 @@ type LineLayout struct { // DoLine consumes exactly one line from the given string, and produces a line // layout according to the given font. It returns the remaining text as well. If -// maxWidth is greater than zero, this function will stop processing words once -// the limit is crossed. The word which would have crossed over the limit will -// not be processed. -func DoLine (text []rune, face font.Face, maxWidth fixed.Int26_6) (line LineLayout, remaining []rune) { +// 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) { remaining = text x := fixed.Int26_6(0) lastWord := WordLayout { } @@ -123,8 +123,8 @@ func DoLine (text []rune, face font.Face, maxWidth fixed.Int26_6) (line LineLayo x += word.Width // if we have gone over the maximum width, stop processing - // words (if maxWidth is even specified) - if !isFirstWord && maxWidth > 0 && x > maxWidth { + // words (if wrap is enabled) + if !isFirstWord && wrap && x > maxWidth { break } @@ -149,16 +149,9 @@ func DoLine (text []rune, face font.Face, maxWidth fixed.Int26_6) (line LineLayo } // set the width of the line's content. + line.Width = maxWidth line.ContentWidth = lastWord.X + lastWord.Width - - // set the line's width. this is subject to be overridden by the - // TypeSetter to match the longest line. - if maxWidth > 0 { - line.Width = maxWidth - } else { - line.Width = line.ContentWidth - line.SpaceAfter = lastWord.SpaceAfter - } + line.SpaceAfter = lastWord.SpaceAfter return } diff --git a/setter.go b/setter.go index f74ad7d..d92f67c 100644 --- a/setter.go +++ b/setter.go @@ -18,6 +18,7 @@ type TypeSetter struct { face font.Face maxWidth int maxHeight int + wrap bool minWidth fixed.Int26_6 layoutBounds image.Rectangle @@ -46,16 +47,17 @@ func (setter *TypeSetter) needLayout () { for len(remaining) > 0 { // process one line line, remainingFromLine := DoLine ( - remaining, setter.face, fixed.I(setter.maxWidth)) + remaining, setter.face, setter.wrap, + fixed.I(setter.maxWidth)) remaining = remainingFromLine // add the line line.Y = y y += metrics.Height - if line.Width > horizontalExtent { - horizontalExtent = line.Width + if line.ContentWidth > horizontalExtent { + horizontalExtent = line.ContentWidth } - lineWidthSpace := line.Width + line.SpaceAfter + lineWidthSpace := line.ContentWidth + line.SpaceAfter if lineWidthSpace > horizontalExtentSpace { horizontalExtentSpace = lineWidthSpace } @@ -63,18 +65,8 @@ func (setter *TypeSetter) needLayout () { } setter.minWidth = horizontalExtentSpace - // set all line widths to horizontalExtent if we don't have a specified - // maximum width - if setter.maxWidth == 0 { - for index := range setter.lines { - setter.lines[index].Width = horizontalExtent - } - setter.layoutBounds.Max.X = horizontalExtent.Round() - setter.layoutBoundsSpace.Max.X = horizontalExtentSpace.Round() - } else { - setter.layoutBounds.Max.X = setter.maxWidth - setter.layoutBoundsSpace.Max.X = setter.maxWidth - } + setter.layoutBounds.Max.X = setter.maxWidth + setter.layoutBoundsSpace.Max.X = setter.maxWidth y -= metrics.Height if setter.maxHeight == 0 { @@ -106,6 +98,13 @@ func (setter *TypeSetter) needAlignedLayout () { } } +// SetWrap sets whether or not the text wraps around and forms new lines. +func (setter *TypeSetter) SetWrap (wrap bool) { + if setter.wrap == wrap { return } + setter.layoutClean = false + setter.wrap = wrap +} + // SetAlign sets the alignment method of the typesetter. func (setter *TypeSetter) SetAlign (align Align) { if setter.align == align { return } @@ -128,9 +127,7 @@ func (setter *TypeSetter) SetFace (face font.Face) { setter.face = face } -// SetMaxWidth sets the maximum width of the typesetter. If the maximum width -// is greater than zero, the text will wrap to that width. If the maximum width -// is zero, the text will not wrap and instead extend as far as it needs to. +// SetMaxWidth sets the maximum width of the typesetter. func (setter *TypeSetter) SetMaxWidth (width int) { if setter.maxWidth == width { return } setter.layoutClean = false @@ -303,7 +300,7 @@ func (setter *TypeSetter) LayoutBoundsSpace () (image.Rectangle) { // wrapping is enabled, this method will return (Em(), 0) func (setter *TypeSetter) MinimumSize () image.Point { setter.needLayout() - if setter.maxWidth != 0 { + if setter.wrap { return image.Pt(setter.Em().Round(), 0) }