Compare commits
2 Commits
ef8944f2a6
...
4be5154df0
Author | SHA1 | Date | |
---|---|---|---|
4be5154df0 | |||
197346d730 |
37
recommend.go
Normal file
37
recommend.go
Normal file
@ -0,0 +1,37 @@
|
||||
package typeset
|
||||
|
||||
import "golang.org/x/image/font"
|
||||
import "golang.org/x/image/math/fixed"
|
||||
|
||||
func recommendHeight (tokens []token, face font.Face, width fixed.Int26_6) fixed.Int26_6 {
|
||||
metrics := face.Metrics()
|
||||
var dot fixed.Point26_6
|
||||
|
||||
newline := func () {
|
||||
dot.Y += metrics.Height
|
||||
dot.X = 0
|
||||
}
|
||||
|
||||
sawLineBreak := false
|
||||
for _, token := range tokens {
|
||||
// demarcate lines
|
||||
if sawLineBreak {
|
||||
newline()
|
||||
sawLineBreak = false
|
||||
}
|
||||
if token.kind == tokenKindLineBreak {
|
||||
sawLineBreak = true
|
||||
} else {
|
||||
needWrap :=
|
||||
token.kind == tokenKindWord &&
|
||||
dot.X + token.width > width
|
||||
if needWrap {
|
||||
newline()
|
||||
}
|
||||
dot.X += token.width
|
||||
}
|
||||
}
|
||||
newline()
|
||||
|
||||
return dot.Y
|
||||
}
|
@ -175,6 +175,14 @@ func (this *TypeSetter) PositionAt (index int) fixed.Point26_6 {
|
||||
return position
|
||||
}
|
||||
|
||||
// ReccomendedHeightFor returns the reccomended max height if the text were to
|
||||
// have its maximum width set to the given width. This does not actually move
|
||||
// any text, it only simulates it.
|
||||
func (this *TypeSetter) RecommendedHeight (width fixed.Int26_6) fixed.Int26_6 {
|
||||
this.needMeasurement()
|
||||
return recommendHeight(this.tokens, this.face, width)
|
||||
}
|
||||
|
||||
// SetAlign sets the horizontal and vertical alignment of the text.
|
||||
func (this *TypeSetter) SetAlign (x, y Align) {
|
||||
if this.xAlign == x && this.yAlign == y { return }
|
||||
|
Loading…
Reference in New Issue
Block a user