Fixes to MinimumSize

This commit is contained in:
Sasha Koshka 2023-07-15 20:03:19 -04:00
parent ca27e2af88
commit 290d3ab366

View File

@ -19,8 +19,7 @@ type TypeSetter struct {
maxWidth int
maxHeight int
horizontalExtent fixed.Int26_6
horizontalExtentSpace fixed.Int26_6
minWidth fixed.Int26_6
layoutBounds image.Rectangle
layoutBoundsSpace image.Rectangle
}
@ -34,11 +33,12 @@ func (setter *TypeSetter) needLayout () {
setter.lines = nil
setter.layoutBounds = image.Rectangle { }
setter.layoutBoundsSpace = image.Rectangle { }
setter.minWidth = 0
if len(setter.text) == 0 { return }
if setter.face == nil { return }
setter.horizontalExtent = fixed.Int26_6(0)
setter.horizontalExtentSpace = fixed.Int26_6(0)
horizontalExtent := fixed.Int26_6(0)
horizontalExtentSpace := fixed.Int26_6(0)
metrics := setter.face.Metrics()
remaining := setter.text
@ -52,24 +52,25 @@ func (setter *TypeSetter) needLayout () {
// add the line
line.Y = y
y += metrics.Height
if line.Width > setter.horizontalExtent {
setter.horizontalExtent = line.Width
if line.Width > horizontalExtent {
horizontalExtent = line.Width
}
lineWidthSpace := line.Width + line.SpaceAfter
if lineWidthSpace > setter.horizontalExtentSpace {
setter.horizontalExtentSpace = lineWidthSpace
if lineWidthSpace > horizontalExtentSpace {
horizontalExtentSpace = lineWidthSpace
}
setter.lines = append(setter.lines, line)
}
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 = setter.horizontalExtent
setter.lines[index].Width = horizontalExtent
}
setter.layoutBounds.Max.X = setter.horizontalExtent.Round()
setter.layoutBoundsSpace.Max.X = setter.horizontalExtentSpace.Round()
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
@ -302,13 +303,14 @@ 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.maxWidth != 0 {
return image.Pt(setter.Em().Round(), 0)
}
return image.Pt (
setter.horizontalExtentSpace.Round(),
(fixed.I(len(setter.lines)) * setter.LineHeight()).Round())
width := setter.minWidth
height := fixed.Int26_6(len(setter.lines)) * setter.LineHeight()
return image.Pt(width.Round(), height.Round())
}
// ReccomendedHeightFor returns the reccomended max height if the text were to