From 14f8673644be7d4acb1747d3b3d1faa7cef3b290 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 10 Apr 2016 13:06:53 +1000 Subject: [PATCH] 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 Reviewed-by: David Crawshaw --- 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?