From 197346d7302f6ad0db84cc08177b458f090c5025 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 19 Sep 2024 23:54:08 -0400 Subject: [PATCH] Add algorithm for figuring out the recommended height of text --- recommend.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 recommend.go diff --git a/recommend.go b/recommend.go new file mode 100644 index 0000000..db9d14e --- /dev/null +++ b/recommend.go @@ -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 +}