Add algorithm for figuring out the recommended height of text
This commit is contained in:
parent
ef8944f2a6
commit
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
|
||||
}
|
Loading…
Reference in New Issue
Block a user