truetype_test.go 9.0 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2012 The Freetype-Go Authors. All rights reserved.
// Use of this source code is governed by your choice of either the
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.

package truetype

import (
9
	"bufio"
10
	"fmt"
11
	"io"
12
	"io/ioutil"
13
	"os"
14
	"strconv"
15
	"strings"
16
	"testing"
17

18
	"golang.org/x/exp/shiny/font"
19
	"golang.org/x/image/math/fixed"
20 21
)

22
func parseTestdataFont(name string) (f *Font, testdataIsOptional bool, err error) {
23
	b, err := ioutil.ReadFile(fmt.Sprintf("../testdata/%s.ttf", name))
24 25 26 27 28
	if err != nil {
		// The "x-foo" fonts are optional tests, as they are not checked
		// in for copyright or file size reasons.
		return nil, strings.HasPrefix(name, "x-"), fmt.Errorf("%s: ReadFile: %v", name, err)
	}
29
	f, err = Parse(b)
30 31 32
	if err != nil {
		return nil, true, fmt.Errorf("%s: Parse: %v", name, err)
	}
33
	return f, false, nil
34 35
}

36 37 38
// TestParse tests that the luxisr.ttf metrics and glyphs are parsed correctly.
// The numerical values can be manually verified by examining luxisr.ttx.
func TestParse(t *testing.T) {
39
	f, _, err := parseTestdataFont("luxisr")
40 41 42
	if err != nil {
		t.Fatal(err)
	}
43
	if got, want := f.FUnitsPerEm(), int32(2048); got != want {
44
		t.Errorf("FUnitsPerEm: got %v, want %v", got, want)
45
	}
46 47
	fupe := fixed.Int26_6(f.FUnitsPerEm())
	if got, want := f.Bounds(fupe), (Bounds{-441, -432, 2024, 2033}); got != want {
48
		t.Errorf("Bounds: got %v, want %v", got, want)
49 50
	}

51 52
	i0 := f.Index('A')
	i1 := f.Index('V')
53 54 55
	if i0 != 36 || i1 != 57 {
		t.Fatalf("Index: i0, i1 = %d, %d, want 36, 57", i0, i1)
	}
56
	if got, want := f.HMetric(fupe, i0), (HMetric{1366, 19}); got != want {
57 58
		t.Errorf("HMetric: got %v, want %v", got, want)
	}
59
	if got, want := f.VMetric(fupe, i0), (VMetric{2465, 553}); got != want {
60 61
		t.Errorf("VMetric: got %v, want %v", got, want)
	}
N
Nigel Tao 已提交
62 63
	if got, want := f.Kern(fupe, i0, i1), fixed.Int26_6(-144); got != want {
		t.Errorf("Kern: got %v, want %v", got, want)
64 65
	}

66
	g := &GlyphBuf{}
67
	err = g.Load(f, fupe, i0, font.HintingNone)
68 69 70
	if err != nil {
		t.Fatalf("Load: %v", err)
	}
71 72 73 74 75
	g0 := &GlyphBuf{
		B:     g.B,
		Point: g.Point,
		End:   g.End,
	}
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	g1 := &GlyphBuf{
		B: Bounds{19, 0, 1342, 1480},
		Point: []Point{
			{19, 0, 51},
			{581, 1480, 1},
			{789, 1480, 51},
			{1342, 0, 1},
			{1116, 0, 35},
			{962, 410, 3},
			{368, 410, 33},
			{214, 0, 3},
			{428, 566, 19},
			{904, 566, 33},
			{667, 1200, 3},
		},
		End: []int{8, 11},
	}
	if got, want := fmt.Sprint(g0), fmt.Sprint(g1); got != want {
		t.Errorf("GlyphBuf:\ngot  %v\nwant %v", got, want)
	}
}
97

98 99 100 101 102 103 104 105 106 107 108 109
func TestIndex(t *testing.T) {
	testCases := map[string]map[rune]Index{
		"luxisr": {
			' ':      3,
			'!':      4,
			'A':      36,
			'V':      57,
			'É':      101,
			'fl':      193,
			'\u22c5': 385,
			'中':      0,
		},
110 111 112 113

		// The x-etc test cases use those versions of the .ttf files provided
		// by Ubuntu 14.04. See testdata/make-other-hinting-txts.sh for details.

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
		"x-arial-bold": {
			' ':      3,
			'+':      14,
			'0':      19,
			'_':      66,
			'w':      90,
			'~':      97,
			'Ä':      98,
			'fl':      192,
			'½':      242,
			'σ':      305,
			'λ':      540,
			'ỹ':      1275,
			'\u04e9': 1319,
			'中':      0,
		},
		"x-deja-vu-sans-oblique": {
			' ':      3,
			'*':      13,
			'Œ':      276,
			'ω':      861,
			'‡':      2571,
136 137 138 139
			'⊕':      3110,
			'fl':      4728,
			'\ufb03': 4729,
			'\ufffd': 4813,
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
			// TODO: '\U0001f640': ???,
			'中': 0,
		},
		"x-droid-sans-japanese": {
			' ':      0,
			'\u3000': 3,
			'\u3041': 25,
			'\u30fe': 201,
			'\uff61': 202,
			'\uff67': 208,
			'\uff9e': 263,
			'\uff9f': 264,
			'\u4e00': 265,
			'\u557e': 1000,
			'\u61b6': 2024,
			'\u6ede': 3177,
			'\u7505': 3555,
			'\u81e3': 4602,
			'\u81e5': 4603,
			'\u81e7': 4604,
			'\u81e8': 4605,
			'\u81ea': 4606,
			'\u81ed': 4607,
			'\u81f3': 4608,
			'\u81f4': 4609,
			'\u91c7': 5796,
			'\u9fa0': 6620,
			'\u203e': 12584,
		},
		"x-times-new-roman": {
			' ':      3,
			':':      29,
			'fl':      192,
			'Ŀ':      273,
			'♠':      388,
			'Ŗ':      451,
			'Σ':      520,
			'\u200D': 745,
			'Ẽ':      1216,
			'\u04e9': 1319,
			'中':      0,
		},
	}
	for name, wants := range testCases {
184
		f, testdataIsOptional, err := parseTestdataFont(name)
185 186 187 188 189 190 191 192 193
		if err != nil {
			if testdataIsOptional {
				t.Log(err)
			} else {
				t.Fatal(err)
			}
			continue
		}
		for r, want := range wants {
194
			if got := f.Index(r); got != want {
195
				t.Errorf("%s: Index of %q, aka %U: got %d, want %d", name, r, r, got, want)
196 197 198 199 200
			}
		}
	}
}

201
type scalingTestData struct {
202
	advanceWidth fixed.Int26_6
203 204 205 206
	bounds       Bounds
	points       []Point
}

207
// scalingTestParse parses a line of points like
208
// 213 -22 -111 236 555;-22 -111 1, 178 555 1, 236 555 1, 36 -111 1
209
// The line will not have a trailing "\n".
210
func scalingTestParse(line string) (ret scalingTestData) {
211
	next := func(s string) (string, fixed.Int26_6) {
212 213 214 215 216
		t, i := "", strings.Index(s, " ")
		if i != -1 {
			s, t = s[:i], s[i+1:]
		}
		x, _ := strconv.Atoi(s)
217
		return t, fixed.Int26_6(x)
218
	}
219 220 221 222 223 224 225 226 227 228 229

	i := strings.Index(line, ";")
	prefix, line := line[:i], line[i+1:]

	prefix, ret.advanceWidth = next(prefix)
	prefix, ret.bounds.XMin = next(prefix)
	prefix, ret.bounds.YMin = next(prefix)
	prefix, ret.bounds.XMax = next(prefix)
	prefix, ret.bounds.YMax = next(prefix)

	ret.points = make([]Point, 0, 1+strings.Count(line, ","))
230 231 232 233 234 235 236 237 238 239
	for len(line) > 0 {
		s := line
		if i := strings.Index(line, ","); i != -1 {
			s, line = line[:i], line[i+1:]
			for len(line) > 0 && line[0] == ' ' {
				line = line[1:]
			}
		} else {
			line = ""
		}
240 241 242 243
		s, x := next(s)
		s, y := next(s)
		s, f := next(s)
		ret.points = append(ret.points, Point{X: x, Y: y, Flags: uint32(f)})
244
	}
245
	return ret
246 247 248
}

// scalingTestEquals is equivalent to, but faster than, calling
N
Nigel Tao 已提交
249 250 251 252
// reflect.DeepEquals(a, b), and also returns the index of the first non-equal
// element. It also treats a nil []Point and an empty non-nil []Point as equal.
// a and b must have equal length.
func scalingTestEquals(a, b []Point) (index int, equals bool) {
253 254
	for i, p := range a {
		if p != b[i] {
N
Nigel Tao 已提交
255
			return i, false
256 257
		}
	}
N
Nigel Tao 已提交
258
	return 0, true
259 260
}

261 262
var scalingTestCases = []struct {
	name string
263
	size int
264
}{
265 266 267 268 269
	{"luxisr", 12},
	{"x-arial-bold", 11},
	{"x-deja-vu-sans-oblique", 17},
	{"x-droid-sans-japanese", 9},
	{"x-times-new-roman", 13},
270
}
271

272
func testScaling(t *testing.T, h font.Hinting) {
273
	for _, tc := range scalingTestCases {
274
		f, testdataIsOptional, err := parseTestdataFont(tc.name)
275
		if err != nil {
276 277
			if testdataIsOptional {
				t.Log(err)
278
			} else {
279
				t.Error(err)
280
			}
281
			continue
282
		}
283
		hintingStr := "sans"
284
		if h != font.HintingNone {
285
			hintingStr = "with"
286
		}
287
		testFile, err := os.Open(fmt.Sprintf(
288
			"../testdata/%s-%dpt-%s-hinting.txt", tc.name, tc.size, hintingStr))
289 290
		if err != nil {
			t.Errorf("%s: Open: %v", tc.name, err)
291
			continue
292
		}
293
		defer testFile.Close()
294

295
		wants := []scalingTestData{}
296
		scanner := bufio.NewScanner(testFile)
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
		if scanner.Scan() {
			major, minor, patch := 0, 0, 0
			_, err := fmt.Sscanf(scanner.Text(), "freetype version %d.%d.%d", &major, &minor, &patch)
			if err != nil {
				t.Errorf("%s: version information: %v", tc.name, err)
			}
			if (major < 2) || (major == 2 && minor < 5) || (major == 2 && minor == 5 && patch < 1) {
				t.Errorf("%s: need freetype version >= 2.5.1.\n"+
					"Try setting LD_LIBRARY_PATH=/path/to/freetype_built_from_src/objs/.libs/\n"+
					"and re-running testdata/make-other-hinting-txts.sh",
					tc.name)
				continue
			}
		} else {
			t.Errorf("%s: no version information", tc.name)
			continue
		}
314
		for scanner.Scan() {
315
			wants = append(wants, scalingTestParse(scanner.Text()))
316
		}
317 318
		if err := scanner.Err(); err != nil && err != io.EOF {
			t.Errorf("%s: Scanner: %v", tc.name, err)
319
			continue
320
		}
321

322
		glyphBuf := &GlyphBuf{}
323
		for i, want := range wants {
324
			if err = glyphBuf.Load(f, fixed.I(tc.size), Index(i), h); err != nil {
325
				t.Errorf("%s: glyph #%d: Load: %v", tc.name, i, err)
326
				continue
327
			}
328
			got := scalingTestData{
329
				advanceWidth: glyphBuf.AdvanceWidth,
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
				bounds:       glyphBuf.B,
				points:       glyphBuf.Point,
			}

			if got.advanceWidth != want.advanceWidth {
				t.Errorf("%s: glyph #%d advance width:\ngot  %v\nwant %v",
					tc.name, i, got.advanceWidth, want.advanceWidth)
				continue
			}

			if got.bounds != want.bounds {
				t.Errorf("%s: glyph #%d bounds:\ngot  %v\nwant %v",
					tc.name, i, got.bounds, want.bounds)
				continue
			}

			for i := range got.points {
				got.points[i].Flags &= 0x01
348
			}
349
			if len(got.points) != len(want.points) {
N
Nigel Tao 已提交
350
				t.Errorf("%s: glyph #%d:\ngot  %v\nwant %v\ndifferent slice lengths: %d versus %d",
351
					tc.name, i, got.points, want.points, len(got.points), len(want.points))
N
Nigel Tao 已提交
352 353
				continue
			}
354
			if j, equals := scalingTestEquals(got.points, want.points); !equals {
N
Nigel Tao 已提交
355
				t.Errorf("%s: glyph #%d:\ngot  %v\nwant %v\nat index %d: %v versus %v",
356
					tc.name, i, got.points, want.points, j, got.points[j], want.points[j])
N
Nigel Tao 已提交
357
				continue
358
			}
359 360 361 362
		}
	}
}

363 364
func TestScalingHintingNone(t *testing.T) { testScaling(t, font.HintingNone) }
func TestScalingHintingFull(t *testing.T) { testScaling(t, font.HintingFull) }