font/sfnt,font/opentype: correct font height computation

According to https://www.freetype.org/freetype2/docs/glyphs/glyphs-3.html
the line spacing of a font is computed as

line space = ascent - descent + linegap

Use that formula to compute the Height field of a Font's Metrics.

Change-Id: I1c44eca9f662e0aae68716cd8859670fe0b394d7
Reviewed-on: https://go-review.googlesource.com/120815
Reviewed-by: Nigel Tao <nigeltao@golang.org>
diff --git a/font/opentype/face_test.go b/font/opentype/face_test.go
index 224b0f2..524a7f6 100644
--- a/font/opentype/face_test.go
+++ b/font/opentype/face_test.go
@@ -82,7 +82,7 @@
 }
 
 func TestFaceMetrics(t *testing.T) {
-	want := font.Metrics{Height: 768, Ascent: 726, Descent: 162}
+	want := font.Metrics{Height: 888, Ascent: 726, Descent: 162}
 	got := regular.Metrics()
 	if got != want {
 		t.Fatalf("metrics failed. got=%#v. want=%#v", got, want)
diff --git a/font/sfnt/sfnt.go b/font/sfnt/sfnt.go
index cc4ceac..21a7927 100644
--- a/font/sfnt/sfnt.go
+++ b/font/sfnt/sfnt.go
@@ -1357,8 +1357,7 @@
 // Metrics returns the metrics of this font.
 func (f *Font) Metrics(b *Buffer, ppem fixed.Int26_6, h font.Hinting) (font.Metrics, error) {
 	m := font.Metrics{
-		// TODO: is adding lineGap correct?
-		Height:  ppem + scale(fixed.Int26_6(f.cached.lineGap)*ppem, f.cached.unitsPerEm),
+		Height:  scale(fixed.Int26_6(f.cached.ascent-f.cached.descent+f.cached.lineGap)*ppem, f.cached.unitsPerEm),
 		Ascent:  +scale(fixed.Int26_6(f.cached.ascent)*ppem, f.cached.unitsPerEm),
 		Descent: -scale(fixed.Int26_6(f.cached.descent)*ppem, f.cached.unitsPerEm),
 	}
diff --git a/font/sfnt/sfnt_test.go b/font/sfnt/sfnt_test.go
index b9b66a7..ca52fba 100644
--- a/font/sfnt/sfnt_test.go
+++ b/font/sfnt/sfnt_test.go
@@ -223,9 +223,9 @@
 		font []byte
 		want font.Metrics
 	}{
-		"goregular": {goregular.TTF, font.Metrics{Height: 2048, Ascent: 1935, Descent: 432}},
+		"goregular": {goregular.TTF, font.Metrics{Height: 2367, Ascent: 1935, Descent: 432}},
 		// cmapTest.ttf has a non-zero lineGap.
-		"cmapTest": {cmapFont, font.Metrics{Height: 2232, Ascent: 1365, Descent: 0}},
+		"cmapTest": {cmapFont, font.Metrics{Height: 1549, Ascent: 1365, Descent: 0}},
 	}
 	var b Buffer
 	for name, tc := range testCases {
@@ -574,11 +574,11 @@
 		lineTo(136, 1297),
 		lineTo(136, 68),
 	}, {
-	// .null
-	// Empty glyph.
+		// .null
+		// Empty glyph.
 	}, {
-	// nonmarkingreturn
-	// Empty glyph.
+		// nonmarkingreturn
+		// Empty glyph.
 	}, {
 		// zero
 		// - contour #0