TextDrawer is able to return a reccomended max height.
This commit is contained in:
parent
18b0ef1159
commit
466fdb8472
@ -16,6 +16,7 @@ type characterLayout struct {
|
|||||||
type wordLayout struct {
|
type wordLayout struct {
|
||||||
position image.Point
|
position image.Point
|
||||||
width int
|
width int
|
||||||
|
spaceAfter int
|
||||||
text []characterLayout
|
text []characterLayout
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,6 +161,25 @@ func (drawer *TextDrawer) LineHeight () (height fixed.Int26_6) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
// drawer's state.
|
||||||
|
func (drawer *TextDrawer) ReccomendedHeightFor (width int) (height int) {
|
||||||
|
if !drawer.layoutClean { drawer.recalculate() }
|
||||||
|
metrics := drawer.face.Metrics()
|
||||||
|
dot := fixed.Point26_6 { 0, 0 }
|
||||||
|
for _, word := range drawer.layout {
|
||||||
|
dot.X += fixed.Int26_6((word.width + word.spaceAfter) << 6)
|
||||||
|
|
||||||
|
if word.width + word.position.X > width && word.position.X > 0 {
|
||||||
|
dot.Y += metrics.Height
|
||||||
|
dot.X = fixed.Int26_6(word.width << 6)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dot.Y.Round()
|
||||||
|
}
|
||||||
|
|
||||||
func (drawer *TextDrawer) recalculate () {
|
func (drawer *TextDrawer) recalculate () {
|
||||||
drawer.layoutClean = true
|
drawer.layoutClean = true
|
||||||
drawer.layout = nil
|
drawer.layout = nil
|
||||||
@ -219,9 +239,6 @@ func (drawer *TextDrawer) recalculate () {
|
|||||||
dot.X = wordWidth
|
dot.X = wordWidth
|
||||||
}
|
}
|
||||||
|
|
||||||
// add the word to the layout
|
|
||||||
drawer.layout = append(drawer.layout, word)
|
|
||||||
|
|
||||||
// skip over whitespace, going onto a new line if there is a
|
// skip over whitespace, going onto a new line if there is a
|
||||||
// newline character
|
// newline character
|
||||||
for index < len(drawer.runes) && unicode.IsSpace(drawer.runes[index]) {
|
for index < len(drawer.runes) && unicode.IsSpace(drawer.runes[index]) {
|
||||||
@ -233,6 +250,7 @@ func (drawer *TextDrawer) recalculate () {
|
|||||||
index ++
|
index ++
|
||||||
} else {
|
} else {
|
||||||
_, advance, ok := drawer.face.GlyphBounds(character)
|
_, advance, ok := drawer.face.GlyphBounds(character)
|
||||||
|
word.spaceAfter = advance.Round()
|
||||||
index ++
|
index ++
|
||||||
if !ok { continue }
|
if !ok { continue }
|
||||||
|
|
||||||
@ -246,6 +264,9 @@ func (drawer *TextDrawer) recalculate () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add the word to the layout
|
||||||
|
drawer.layout = append(drawer.layout, word)
|
||||||
|
|
||||||
// if there is a set maximum height, and we have crossed it,
|
// if there is a set maximum height, and we have crossed it,
|
||||||
// stop processing more words. and remove any words that have
|
// stop processing more words. and remove any words that have
|
||||||
// also crossed the line.
|
// also crossed the line.
|
||||||
|
Reference in New Issue
Block a user