git.lucas.co / go_mono
git clone https://git.lucas.co/go_mono.git

font/opentype/opentype_test.go (3.7K)

  1 // Copyright 2017 The Go Authors. All rights reserved.
  2 // Use of this source code is governed by a BSD-style
  3 // license that can be found in the LICENSE file.
  4 
  5 package opentype
  6 
  7 import (
  8 	"image"
  9 	"testing"
 10 
 11 	"golang.org/x/image/font"
 12 	"golang.org/x/image/font/gofont/goregular"
 13 	"golang.org/x/image/font/sfnt"
 14 	"golang.org/x/image/math/fixed"
 15 )
 16 
 17 var (
 18 	regular font.Face
 19 )
 20 
 21 func init() {
 22 	font, err := sfnt.Parse(goregular.TTF)
 23 	if err != nil {
 24 		panic(err)
 25 	}
 26 
 27 	regular, err = NewFace(font, defaultFaceOptions())
 28 	if err != nil {
 29 		panic(err)
 30 	}
 31 }
 32 
 33 var runeTests = []struct {
 34 	r       rune
 35 	advance fixed.Int26_6
 36 	dr      image.Rectangle
 37 }{
 38 	{' ', 213, image.Rect(0, 0, 0, 0)},
 39 	{'A', 512, image.Rect(0, -9, 8, 0)},
 40 	{'Á', 512, image.Rect(0, -12, 8, 0)},
 41 	{'Æ', 768, image.Rect(0, -9, 12, 0)},
 42 	{'i', 189, image.Rect(0, -9, 3, 0)},
 43 	{'x', 384, image.Rect(0, -7, 6, 0)},
 44 }
 45 
 46 func TestFaceGlyphAdvance(t *testing.T) {
 47 	for _, test := range runeTests {
 48 		got, ok := regular.GlyphAdvance(test.r)
 49 		if !ok {
 50 			t.Errorf("could not get glyph advance width for %q", test.r)
 51 			continue
 52 		}
 53 
 54 		if got != test.advance {
 55 			t.Errorf("%q: glyph advance width=%d. want=%d", test.r, got, test.advance)
 56 			continue
 57 		}
 58 	}
 59 }
 60 
 61 func TestFaceGlyphBounds(t *testing.T) {
 62 	for _, test := range runeTests {
 63 		bounds, advance, ok := regular.GlyphBounds(test.r)
 64 		if !ok {
 65 			t.Errorf("could not get glyph bounds for %q", test.r)
 66 			continue
 67 		}
 68 
 69 		// bounds must fit inside the draw rect.
 70 		testFixedBounds := fixed.R(test.dr.Min.X, test.dr.Min.Y,
 71 			test.dr.Max.X, test.dr.Max.Y)
 72 		if !bounds.In(testFixedBounds) {
 73 			t.Errorf("%q: glyph bounds %v must be inside %v", test.r, bounds, testFixedBounds)
 74 			continue
 75 		}
 76 		if advance != test.advance {
 77 			t.Errorf("%q: glyph advance width=%d. want=%d", test.r, advance, test.advance)
 78 			continue
 79 		}
 80 	}
 81 }
 82 
 83 func TestFaceGlyph(t *testing.T) {
 84 	dot := image.Pt(200, 500)
 85 	fixedDot := fixed.P(dot.X, dot.Y)
 86 
 87 	for _, test := range runeTests {
 88 		dr, mask, maskp, advance, ok := regular.Glyph(fixedDot, test.r)
 89 		if !ok {
 90 			t.Errorf("could not get glyph for %q", test.r)
 91 			continue
 92 		}
 93 		if got, want := dr, test.dr.Add(dot); got != want {
 94 			t.Errorf("%q: glyph draw rectangle=%d. want=%d", test.r, got, want)
 95 			continue
 96 		}
 97 		if got, want := mask.Bounds(), image.Rect(0, 0, dr.Dx(), dr.Dy()); got != want {
 98 			t.Errorf("%q: glyph mask rectangle=%d. want=%d", test.r, got, want)
 99 			continue
100 		}
101 		if maskp != (image.Point{}) {
102 			t.Errorf("%q: glyph maskp=%d. want=%d", test.r, maskp, image.Point{})
103 			continue
104 		}
105 		if advance != test.advance {
106 			t.Errorf("%q: glyph advance width=%d. want=%d", test.r, advance, test.advance)
107 			continue
108 		}
109 	}
110 }
111 
112 func BenchmarkFaceGlyph(b *testing.B) {
113 	fixedDot := fixed.P(200, 500)
114 	r := 'A'
115 
116 	b.ReportAllocs()
117 	b.ResetTimer()
118 	for i := 0; i < b.N; i++ {
119 		_, _, _, _, ok := regular.Glyph(fixedDot, r)
120 		if !ok {
121 			b.Fatalf("could not get glyph for %q", r)
122 		}
123 	}
124 }
125 
126 func TestFaceKern(t *testing.T) {
127 	// FIXME(sbinet) there is no kerning with gofont/goregular
128 	for _, test := range []struct {
129 		r1, r2 rune
130 		want   fixed.Int26_6
131 	}{
132 		{'A', 'A', 0},
133 		{'A', 'V', 0},
134 		{'V', 'A', 0},
135 		{'A', 'v', 0},
136 		{'W', 'a', 0},
137 		{'W', 'i', 0},
138 		{'Y', 'i', 0},
139 		{'f', '(', 0},
140 		{'f', 'f', 0},
141 		{'f', 'i', 0},
142 		{'T', 'a', 0},
143 		{'T', 'e', 0},
144 	} {
145 		got := regular.Kern(test.r1, test.r2)
146 		if got != test.want {
147 			t.Errorf("(%q, %q): glyph kerning=%d. want=%d", test.r1, test.r2, got, test.want)
148 			continue
149 		}
150 	}
151 }
152 
153 func TestFaceMetrics(t *testing.T) {
154 	want := font.Metrics{Height: 888, Ascent: 726, Descent: 162, XHeight: 407, CapHeight: 555,
155 		CaretSlope: image.Point{X: 0, Y: 1}}
156 	got := regular.Metrics()
157 	if got != want {
158 		t.Fatalf("metrics failed. got=%#v. want=%#v", got, want)
159 	}
160 }