font: add Face.Metrics.

Fixes golang/go#14885

Change-Id: I3aa2d323c97c76cc78d981d4bf6ea9e95c9bf9d2
Reviewed-on: https://go-review.googlesource.com/21035
Reviewed-by: Nigel Tao <nigeltao@golang.org>
This commit is contained in:
Ethan Burns 2016-03-23 06:48:38 -04:00 committed by Nigel Tao
parent 7c492694a6
commit 7b1c29e1d6
3 changed files with 35 additions and 1 deletions

View File

@ -10,6 +10,7 @@ package basicfont // import "golang.org/x/image/font/basicfont"
import (
"image"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
@ -70,6 +71,13 @@ type Face struct {
func (f *Face) Close() error { return nil }
func (f *Face) Kern(r0, r1 rune) fixed.Int26_6 { return 0 }
func (f *Face) Metrics() font.Metrics {
return font.Metrics{
Height: fixed.I(f.Height),
Ascent: fixed.I(f.Ascent),
}
}
func (f *Face) Glyph(dot fixed.Point26_6, r rune) (
dr image.Rectangle, mask image.Image, maskp image.Point, advance fixed.Int26_6, ok bool) {

View File

@ -64,11 +64,23 @@ type Face interface {
// positive kern means to move the glyphs further apart.
Kern(r0, r1 rune) fixed.Int26_6
// TODO: per-font Metrics.
// Metrics returns the metrics for this Face.
Metrics() Metrics
// TODO: ColoredGlyph for various emoji?
// TODO: Ligatures? Shaping?
}
// Metrics holds the metrics for a Face. A visual depiction is at
// https://developer.apple.com/library/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/Art/glyph_metrics_2x.png
type Metrics struct {
// Ascent is the distance from the top of a line to its baseline.
Ascent fixed.Int26_6
// Height is the recommended amount of vertical space between two lines of text.
Height fixed.Int26_6
}
// TODO: Drawer.Layout or Drawer.Measure methods to measure text without
// drawing?

View File

@ -65,6 +65,13 @@ type subface struct {
func (f *subface) Close() error { return nil }
func (f *subface) Kern(r0, r1 rune) fixed.Int26_6 { return 0 }
func (f *subface) Metrics() font.Metrics {
return font.Metrics{
Height: fixed.I(f.height),
Ascent: fixed.I(f.ascent),
}
}
func (f *subface) Glyph(dot fixed.Point26_6, r rune) (
dr image.Rectangle, mask image.Image, maskp image.Point, advance fixed.Int26_6, ok bool) {
@ -139,6 +146,13 @@ type face struct {
func (f *face) Close() error { return nil }
func (f *face) Kern(r0, r1 rune) fixed.Int26_6 { return 0 }
func (f *face) Metrics() font.Metrics {
return font.Metrics{
Height: fixed.I(f.height),
Ascent: fixed.I(f.ascent),
}
}
func (f *face) Glyph(dot fixed.Point26_6, r rune) (
dr image.Rectangle, mask image.Image, maskp image.Point, advance fixed.Int26_6, ok bool) {