git.lucas.co / go_mono
git clone https://git.lucas.co/go_mono.git

commit14f8673644be7d4acb1747d3b3d1faa7cef3b290
parent4cff43048f
authorNigel Tao <nigeltao@golang.org>
date2016-04-10 13:06
font: add a MeasureString function, not just a method.

It can be useful to measure some text without having to set up a Drawer.

Change-Id: I18b7b1fecc32ca69b7644d598ed21462e7c41edd
Reviewed-on: https://go-review.googlesource.com/21785
Reviewed-by: Andrew Gerrand <adg@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>

 font/font.go | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/font/font.go b/font/font.go
index 31e48c9..a089e77 100644
--- a/font/font.go
+++ b/font/font.go
@@ -145,12 +145,17 @@ func (d *Drawer) DrawString(s string) {
 
 // MeasureString returns how far dot would advance by drawing s.
 func (d *Drawer) MeasureString(s string) (advance fixed.Int26_6) {
+	return MeasureString(d.Face, s)
+}
+
+// MeasureString returns how far dot would advance by drawing s with f.
+func MeasureString(f Face, s string) (advance fixed.Int26_6) {
 	var prevC rune
 	for i, c := range s {
 		if i != 0 {
-			advance += d.Face.Kern(prevC, c)
+			advance += f.Kern(prevC, c)
 		}
-		a, ok := d.Face.GlyphAdvance(c)
+		a, ok := f.GlyphAdvance(c)
 		if !ok {
 			// TODO: is falling back on the U+FFFD glyph the responsibility of
 			// the Drawer or the Face?