From cc896f830cedae125428bc9fe1b0362aa91b3fb1 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Mon, 25 Jun 2018 21:10:44 +0200 Subject: [PATCH] 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 --- font/opentype/face_test.go | 2 +- font/sfnt/sfnt.go | 3 +-- font/sfnt/sfnt_test.go | 12 ++++++------ 3 files changed, 8 insertions(+), 9 deletions(-) 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 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) 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 @@ 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), } 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 @@ 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 { @@ -574,11 +574,11 @@ func TestTrueTypeSegments(t *testing.T) { lineTo(136, 1297), lineTo(136, 68), }, { - // .null - // Empty glyph. + // .null + // Empty glyph. }, { - // nonmarkingreturn - // Empty glyph. + // nonmarkingreturn + // Empty glyph. }, { // zero // - contour #0