typeset/recommend.go

38 lines
691 B
Go
Raw Permalink Normal View History

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
}