Added a MinimumSize method

This commit is contained in:
Sasha Koshka 2023-07-15 19:52:06 -04:00
parent 778cdb6827
commit ca27e2af88

View File

@ -18,7 +18,9 @@ type TypeSetter struct {
face font.Face
maxWidth int
maxHeight int
horizontalExtent fixed.Int26_6
horizontalExtentSpace fixed.Int26_6
layoutBounds image.Rectangle
layoutBoundsSpace image.Rectangle
}
@ -35,8 +37,8 @@ func (setter *TypeSetter) needLayout () {
if len(setter.text) == 0 { return }
if setter.face == nil { return }
horizontalExtent := fixed.Int26_6(0)
horizontalExtentSpace := fixed.Int26_6(0)
setter.horizontalExtent = fixed.Int26_6(0)
setter.horizontalExtentSpace = fixed.Int26_6(0)
metrics := setter.face.Metrics()
remaining := setter.text
@ -50,12 +52,12 @@ func (setter *TypeSetter) needLayout () {
// add the line
line.Y = y
y += metrics.Height
if line.Width > horizontalExtent {
horizontalExtent = line.Width
if line.Width > setter.horizontalExtent {
setter.horizontalExtent = line.Width
}
lineWidthSpace := line.Width + line.SpaceAfter
if lineWidthSpace > horizontalExtentSpace {
horizontalExtentSpace = lineWidthSpace
if lineWidthSpace > setter.horizontalExtentSpace {
setter.horizontalExtentSpace = lineWidthSpace
}
setter.lines = append(setter.lines, line)
}
@ -64,10 +66,10 @@ func (setter *TypeSetter) needLayout () {
// maximum width
if setter.maxWidth == 0 {
for index := range setter.lines {
setter.lines[index].Width = horizontalExtent
setter.lines[index].Width = setter.horizontalExtent
}
setter.layoutBounds.Max.X = horizontalExtent.Round()
setter.layoutBoundsSpace.Max.X = horizontalExtentSpace.Round()
setter.layoutBounds.Max.X = setter.horizontalExtent.Round()
setter.layoutBoundsSpace.Max.X = setter.horizontalExtentSpace.Round()
} else {
setter.layoutBounds.Max.X = setter.maxWidth
setter.layoutBoundsSpace.Max.X = setter.maxWidth
@ -296,6 +298,19 @@ func (setter *TypeSetter) LayoutBoundsSpace () (image.Rectangle) {
return setter.layoutBoundsSpace
}
// MinimumSize returns the minimum width and height needed to display text. If
// wrapping is enabled, this method will return (Em(), 0)
func (setter *TypeSetter) MinimumSize () image.Point {
setter.needLayout()
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())
}
// ReccomendedHeightFor returns the reccomended max height if the text were to
// have its maximum width set to the given width. This does not alter the
// typesetter's state.