git clone https://git.lucas.co/go_mono.git
font: add Face.Metrics.
Fixes golang/go#14885
Change-Id: I3aa2d323c97c76cc78d981d4bf6ea9e95c9bf9d2
Reviewed-on: https://go-review.googlesource.com/21035
Reviewed-by: Nigel Tao <[email protected]>
font/basicfont/basicfont.go | 8 ++++++++
font/font.go | 14 +++++++++++++-
font/plan9font/plan9font.go | 14 ++++++++++++++
3 files changed, 35 insertions(+), 1 deletion(-)
diff --git a/font/basicfont/basicfont.go b/font/basicfont/basicfont.go
index 3f61d4c..ed05fa2 100644
--- a/font/basicfont/basicfont.go
+++ b/font/basicfont/basicfont.go
@@ -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) {
diff --git a/font/font.go b/font/font.go
index 9cf43ff..3e3608e 100644
--- a/font/font.go
+++ b/font/font.go
@@ -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?
diff --git a/font/plan9font/plan9font.go b/font/plan9font/plan9font.go
index da56ee8..b7948d3 100644
--- a/font/plan9font/plan9font.go
+++ b/font/plan9font/plan9font.go
@@ -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) {