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>
This commit is contained in:
Nigel Tao 2016-04-10 13:06:53 +10:00
parent 4cff43048f
commit 14f8673644

View File

@ -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?