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>
This commit is contained in:
Elias Naur 2018-06-25 21:10:44 +02:00 committed by Nigel Tao
parent af66defab9
commit cc896f830c
3 changed files with 8 additions and 9 deletions

View File

@ -82,7 +82,7 @@ func TestFaceKern(t *testing.T) {
}
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)

View File

@ -1357,8 +1357,7 @@ func (f *Font) Kern(b *Buffer, x0, x1 GlyphIndex, ppem fixed.Int26_6, h font.Hin
// 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),
}

View File

@ -223,9 +223,9 @@ func TestMetrics(t *testing.T) {
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 {