2016-11-20 04:06:55 +01:00
|
|
|
// Copyright 2016 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package sfnt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-11-27 10:19:50 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2016-11-20 04:06:55 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/image/font/gofont/goregular"
|
2016-12-05 13:52:00 +01:00
|
|
|
"golang.org/x/image/math/fixed"
|
2016-11-20 04:06:55 +01:00
|
|
|
)
|
|
|
|
|
2016-12-05 13:52:00 +01:00
|
|
|
func moveTo(xa, ya int) Segment {
|
|
|
|
return Segment{
|
|
|
|
Op: SegmentOpMoveTo,
|
|
|
|
Args: [6]fixed.Int26_6{
|
|
|
|
0: fixed.I(xa),
|
|
|
|
1: fixed.I(ya),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func lineTo(xa, ya int) Segment {
|
|
|
|
return Segment{
|
|
|
|
Op: SegmentOpLineTo,
|
|
|
|
Args: [6]fixed.Int26_6{
|
|
|
|
0: fixed.I(xa),
|
|
|
|
1: fixed.I(ya),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-15 10:09:03 +01:00
|
|
|
func quadTo(xa, ya, xb, yb int) Segment {
|
|
|
|
return Segment{
|
|
|
|
Op: SegmentOpQuadTo,
|
|
|
|
Args: [6]fixed.Int26_6{
|
|
|
|
0: fixed.I(xa),
|
|
|
|
1: fixed.I(ya),
|
|
|
|
2: fixed.I(xb),
|
|
|
|
3: fixed.I(yb),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 13:52:00 +01:00
|
|
|
func cubeTo(xa, ya, xb, yb, xc, yc int) Segment {
|
|
|
|
return Segment{
|
|
|
|
Op: SegmentOpCubeTo,
|
|
|
|
Args: [6]fixed.Int26_6{
|
|
|
|
0: fixed.I(xa),
|
|
|
|
1: fixed.I(ya),
|
|
|
|
2: fixed.I(xb),
|
|
|
|
3: fixed.I(yb),
|
|
|
|
4: fixed.I(xc),
|
|
|
|
5: fixed.I(yc),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 10:19:50 +01:00
|
|
|
func TestTrueTypeParse(t *testing.T) {
|
2016-11-20 04:06:55 +01:00
|
|
|
f, err := Parse(goregular.TTF)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Parse: %v", err)
|
|
|
|
}
|
2016-11-27 10:19:50 +01:00
|
|
|
testTrueType(t, f)
|
2016-11-20 04:06:55 +01:00
|
|
|
}
|
|
|
|
|
2016-11-27 10:19:50 +01:00
|
|
|
func TestTrueTypeParseReaderAt(t *testing.T) {
|
2016-11-20 04:06:55 +01:00
|
|
|
f, err := ParseReaderAt(bytes.NewReader(goregular.TTF))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ParseReaderAt: %v", err)
|
|
|
|
}
|
2016-11-27 10:19:50 +01:00
|
|
|
testTrueType(t, f)
|
2016-11-20 04:06:55 +01:00
|
|
|
}
|
|
|
|
|
2016-11-27 10:19:50 +01:00
|
|
|
func testTrueType(t *testing.T, f *Font) {
|
2016-11-20 04:06:55 +01:00
|
|
|
if got, want := f.UnitsPerEm(), Units(2048); got != want {
|
|
|
|
t.Errorf("UnitsPerEm: got %d, want %d", got, want)
|
|
|
|
}
|
|
|
|
// The exact number of glyphs in goregular.TTF can vary, and future
|
|
|
|
// versions may add more glyphs, but https://blog.golang.org/go-fonts says
|
|
|
|
// that "The WGL4 character set... [has] more than 650 characters in all.
|
|
|
|
if got, want := f.NumGlyphs(), 650; got <= want {
|
|
|
|
t.Errorf("NumGlyphs: got %d, want > %d", got, want)
|
|
|
|
}
|
|
|
|
}
|
2016-11-27 10:19:50 +01:00
|
|
|
|
2017-02-04 04:43:53 +01:00
|
|
|
func TestGoRegularGlyphIndex(t *testing.T) {
|
|
|
|
f, err := Parse(goregular.TTF)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Parse: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
r rune
|
|
|
|
want GlyphIndex
|
|
|
|
}{
|
|
|
|
// Glyphs that aren't present in Go Regular.
|
|
|
|
{'\u001f', 0}, // U+001F <control>
|
|
|
|
{'\u0200', 0}, // U+0200 LATIN CAPITAL LETTER A WITH DOUBLE GRAVE
|
|
|
|
{'\u2000', 0}, // U+2000 EN QUAD
|
|
|
|
|
|
|
|
// The want values below can be verified by running the ttx tool on
|
|
|
|
// Go-Regular.ttf.
|
|
|
|
//
|
|
|
|
// The actual values are ad hoc, and result from whatever tools the
|
|
|
|
// Bigelow & Holmes type foundry used and the order in which they
|
|
|
|
// crafted the glyphs. They may change over time as newer versions of
|
|
|
|
// the font are released. In practice, though, running this test with
|
|
|
|
// coverage analysis suggests that it covers both the zero and non-zero
|
|
|
|
// cmapEntry16.offset cases for a format-4 cmap table.
|
|
|
|
|
|
|
|
{'\u0020', 3}, // U+0020 SPACE
|
|
|
|
{'\u0021', 4}, // U+0021 EXCLAMATION MARK
|
|
|
|
{'\u0022', 5}, // U+0022 QUOTATION MARK
|
|
|
|
{'\u0023', 6}, // U+0023 NUMBER SIGN
|
|
|
|
{'\u0024', 223}, // U+0024 DOLLAR SIGN
|
|
|
|
{'\u0025', 7}, // U+0025 PERCENT SIGN
|
|
|
|
{'\u0026', 8}, // U+0026 AMPERSAND
|
|
|
|
{'\u0027', 9}, // U+0027 APOSTROPHE
|
|
|
|
|
|
|
|
{'\u03bd', 423}, // U+03BD GREEK SMALL LETTER NU
|
|
|
|
{'\u03be', 424}, // U+03BE GREEK SMALL LETTER XI
|
|
|
|
{'\u03bf', 438}, // U+03BF GREEK SMALL LETTER OMICRON
|
|
|
|
{'\u03c0', 208}, // U+03C0 GREEK SMALL LETTER PI
|
|
|
|
{'\u03c1', 425}, // U+03C1 GREEK SMALL LETTER RHO
|
|
|
|
{'\u03c2', 426}, // U+03C2 GREEK SMALL LETTER FINAL SIGMA
|
|
|
|
}
|
|
|
|
|
|
|
|
var b Buffer
|
|
|
|
for _, tc := range testCases {
|
|
|
|
got, err := f.GlyphIndex(&b, tc.r)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("r=%q: %v", tc.r, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("r=%q: got %d, want %d", tc.r, got, tc.want)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 06:32:42 +01:00
|
|
|
func TestGlyphIndex(t *testing.T) {
|
2017-02-02 09:04:00 +01:00
|
|
|
data, err := ioutil.ReadFile(filepath.FromSlash("../testdata/cmapTest.ttf"))
|
2017-01-21 06:32:42 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-03 09:18:20 +01:00
|
|
|
for _, format := range []int{-1, 0, 4, 12} {
|
2017-01-21 06:32:42 +01:00
|
|
|
testGlyphIndex(t, data, format)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testGlyphIndex(t *testing.T, data []byte, cmapFormat int) {
|
|
|
|
if cmapFormat >= 0 {
|
|
|
|
originalSupportedCmapFormat := supportedCmapFormat
|
|
|
|
defer func() {
|
|
|
|
supportedCmapFormat = originalSupportedCmapFormat
|
|
|
|
}()
|
|
|
|
supportedCmapFormat = func(format, pid, psid uint16) bool {
|
|
|
|
return int(format) == cmapFormat && originalSupportedCmapFormat(format, pid, psid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := Parse(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cmapFormat=%d: %v", cmapFormat, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
r rune
|
|
|
|
want GlyphIndex
|
|
|
|
}{
|
2017-02-02 09:04:00 +01:00
|
|
|
// Glyphs that aren't present in cmapTest.ttf.
|
2017-01-21 06:32:42 +01:00
|
|
|
{'?', 0},
|
|
|
|
{'\ufffd', 0},
|
|
|
|
{'\U0001f4a9', 0},
|
2017-02-02 09:04:00 +01:00
|
|
|
|
|
|
|
// For a .TTF file, FontForge maps:
|
|
|
|
// - ".notdef" to glyph index 0.
|
|
|
|
// - ".null" to glyph index 1.
|
|
|
|
// - "nonmarkingreturn" to glyph index 2.
|
|
|
|
|
|
|
|
{'/', 0},
|
|
|
|
{'0', 3},
|
|
|
|
{'1', 4},
|
|
|
|
{'2', 5},
|
|
|
|
{'3', 0},
|
|
|
|
|
|
|
|
{'@', 0},
|
|
|
|
{'A', 6},
|
|
|
|
{'B', 7},
|
|
|
|
{'C', 0},
|
|
|
|
|
|
|
|
{'`', 0},
|
|
|
|
{'a', 8},
|
|
|
|
{'b', 0},
|
|
|
|
|
|
|
|
// Of the remaining runes, only U+00FF LATIN SMALL LETTER Y WITH
|
|
|
|
// DIAERESIS is in both the Mac Roman encoding and the cmapTest.ttf
|
|
|
|
// font file.
|
|
|
|
{'\u00fe', 0},
|
|
|
|
{'\u00ff', 9},
|
|
|
|
{'\u0100', 10},
|
|
|
|
{'\u0101', 11},
|
|
|
|
{'\u0102', 0},
|
|
|
|
|
|
|
|
{'\u4e2c', 0},
|
|
|
|
{'\u4e2d', 12},
|
|
|
|
{'\u4e2e', 0},
|
|
|
|
|
2017-02-03 09:18:20 +01:00
|
|
|
{'\U0001f0a0', 0},
|
|
|
|
{'\U0001f0a1', 13},
|
|
|
|
{'\U0001f0a2', 0},
|
2017-02-02 09:04:00 +01:00
|
|
|
|
2017-02-03 09:18:20 +01:00
|
|
|
{'\U0001f0b0', 0},
|
|
|
|
{'\U0001f0b1', 14},
|
|
|
|
{'\U0001f0b2', 15},
|
|
|
|
{'\U0001f0b3', 0},
|
2017-01-21 06:32:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var b Buffer
|
|
|
|
for _, tc := range testCases {
|
|
|
|
want := tc.want
|
2017-02-03 09:18:20 +01:00
|
|
|
switch {
|
|
|
|
case cmapFormat == 0 && tc.r > '\u007f' && tc.r != '\u00ff':
|
|
|
|
// cmap format 0, with the Macintosh Roman encoding, can only
|
|
|
|
// represent a limited set of non-ASCII runes, e.g. U+00FF.
|
|
|
|
want = 0
|
|
|
|
case cmapFormat == 4 && tc.r > '\uffff':
|
|
|
|
// cmap format 4 only supports the Basic Multilingual Plane (BMP).
|
2017-01-21 06:32:42 +01:00
|
|
|
want = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := f.GlyphIndex(&b, tc.r)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("cmapFormat=%d, r=%q: %v", cmapFormat, tc.r, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("cmapFormat=%d, r=%q: got %d, want %d", cmapFormat, tc.r, got, want)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-15 10:09:03 +01:00
|
|
|
func TestPostScriptSegments(t *testing.T) {
|
2016-12-05 13:52:00 +01:00
|
|
|
// wants' vectors correspond 1-to-1 to what's in the CFFTest.sfd file,
|
2016-12-12 22:13:29 +01:00
|
|
|
// although OpenType/CFF and FontForge's SFD have reversed orders.
|
|
|
|
// https://fontforge.github.io/validation.html says that "All paths must be
|
|
|
|
// drawn in a consistent direction. Clockwise for external paths,
|
|
|
|
// anti-clockwise for internal paths. (Actually PostScript requires the
|
|
|
|
// exact opposite, but FontForge reverses PostScript contours when it loads
|
|
|
|
// them so that everything is consistant internally -- and reverses them
|
|
|
|
// again when it saves them, of course)."
|
2016-11-27 10:19:50 +01:00
|
|
|
//
|
2016-12-05 13:52:00 +01:00
|
|
|
// The .notdef glyph isn't explicitly in the SFD file, but for some unknown
|
2017-01-15 10:09:03 +01:00
|
|
|
// reason, FontForge generates it in the OpenType/CFF file.
|
|
|
|
wants := [][]Segment{{
|
2016-12-05 13:52:00 +01:00
|
|
|
// .notdef
|
|
|
|
// - contour #0
|
|
|
|
moveTo(50, 0),
|
|
|
|
lineTo(450, 0),
|
|
|
|
lineTo(450, 533),
|
|
|
|
lineTo(50, 533),
|
|
|
|
// - contour #1
|
|
|
|
moveTo(100, 50),
|
|
|
|
lineTo(100, 483),
|
|
|
|
lineTo(400, 483),
|
|
|
|
lineTo(400, 50),
|
|
|
|
}, {
|
|
|
|
// zero
|
|
|
|
// - contour #0
|
|
|
|
moveTo(300, 700),
|
|
|
|
cubeTo(380, 700, 420, 580, 420, 500),
|
|
|
|
cubeTo(420, 350, 390, 100, 300, 100),
|
|
|
|
cubeTo(220, 100, 180, 220, 180, 300),
|
|
|
|
cubeTo(180, 450, 210, 700, 300, 700),
|
|
|
|
// - contour #1
|
|
|
|
moveTo(300, 800),
|
|
|
|
cubeTo(200, 800, 100, 580, 100, 400),
|
|
|
|
cubeTo(100, 220, 200, 0, 300, 0),
|
|
|
|
cubeTo(400, 0, 500, 220, 500, 400),
|
|
|
|
cubeTo(500, 580, 400, 800, 300, 800),
|
|
|
|
}, {
|
|
|
|
// one
|
|
|
|
// - contour #0
|
|
|
|
moveTo(100, 0),
|
|
|
|
lineTo(300, 0),
|
|
|
|
lineTo(300, 800),
|
|
|
|
lineTo(100, 800),
|
2016-12-09 12:46:30 +01:00
|
|
|
}, {
|
|
|
|
// Q
|
|
|
|
// - contour #0
|
|
|
|
moveTo(657, 237),
|
|
|
|
lineTo(289, 387),
|
|
|
|
lineTo(519, 615),
|
|
|
|
// - contour #1
|
|
|
|
moveTo(792, 169),
|
|
|
|
cubeTo(867, 263, 926, 502, 791, 665),
|
|
|
|
cubeTo(645, 840, 380, 831, 228, 673),
|
|
|
|
cubeTo(71, 509, 110, 231, 242, 93),
|
|
|
|
cubeTo(369, -39, 641, 18, 722, 93),
|
|
|
|
lineTo(802, 3),
|
|
|
|
lineTo(864, 83),
|
2016-12-05 13:52:00 +01:00
|
|
|
}, {
|
|
|
|
// uni4E2D
|
|
|
|
// - contour #0
|
|
|
|
moveTo(141, 520),
|
|
|
|
lineTo(137, 356),
|
|
|
|
lineTo(245, 400),
|
|
|
|
lineTo(331, 26),
|
|
|
|
lineTo(355, 414),
|
|
|
|
lineTo(463, 434),
|
|
|
|
lineTo(453, 620),
|
|
|
|
lineTo(341, 592),
|
|
|
|
lineTo(331, 758),
|
|
|
|
lineTo(243, 752),
|
|
|
|
lineTo(235, 562),
|
2017-01-15 10:09:03 +01:00
|
|
|
// TODO: explicitly (not implicitly) close these contours?
|
2016-12-05 13:52:00 +01:00
|
|
|
}}
|
|
|
|
|
2017-01-15 10:09:03 +01:00
|
|
|
testSegments(t, "CFFTest.otf", wants)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrueTypeSegments(t *testing.T) {
|
|
|
|
// wants' vectors correspond 1-to-1 to what's in the glyfTest.sfd file,
|
|
|
|
// although FontForge's SFD format stores quadratic Bézier curves as cubics
|
|
|
|
// with duplicated off-curve points. quadTo(bx, by, cx, cy) is stored as
|
|
|
|
// "bx by bx by cx cy".
|
|
|
|
//
|
|
|
|
// The .notdef, .null and nonmarkingreturn glyphs aren't explicitly in the
|
|
|
|
// SFD file, but for some unknown reason, FontForge generates them in the
|
|
|
|
// TrueType file.
|
|
|
|
wants := [][]Segment{{
|
|
|
|
// .notdef
|
|
|
|
// - contour #0
|
|
|
|
moveTo(68, 0),
|
|
|
|
lineTo(68, 1365),
|
|
|
|
lineTo(612, 1365),
|
|
|
|
lineTo(612, 0),
|
|
|
|
lineTo(68, 0),
|
|
|
|
// - contour #1
|
|
|
|
moveTo(136, 68),
|
|
|
|
lineTo(544, 68),
|
|
|
|
lineTo(544, 1297),
|
|
|
|
lineTo(136, 1297),
|
|
|
|
lineTo(136, 68),
|
|
|
|
}, {
|
|
|
|
// .null
|
|
|
|
// Empty glyph.
|
|
|
|
}, {
|
|
|
|
// nonmarkingreturn
|
|
|
|
// Empty glyph.
|
|
|
|
}, {
|
|
|
|
// zero
|
|
|
|
// - contour #0
|
|
|
|
moveTo(614, 1434),
|
|
|
|
quadTo(369, 1434, 369, 614),
|
|
|
|
quadTo(369, 471, 435, 338),
|
|
|
|
quadTo(502, 205, 614, 205),
|
|
|
|
quadTo(860, 205, 860, 1024),
|
|
|
|
quadTo(860, 1167, 793, 1300),
|
|
|
|
quadTo(727, 1434, 614, 1434),
|
|
|
|
// - contour #1
|
|
|
|
moveTo(614, 1638),
|
|
|
|
quadTo(1024, 1638, 1024, 819),
|
|
|
|
quadTo(1024, 0, 614, 0),
|
|
|
|
quadTo(205, 0, 205, 819),
|
|
|
|
quadTo(205, 1638, 614, 1638),
|
|
|
|
}, {
|
|
|
|
// one
|
|
|
|
// - contour #0
|
|
|
|
moveTo(205, 0),
|
|
|
|
lineTo(205, 1638),
|
|
|
|
lineTo(614, 1638),
|
|
|
|
lineTo(614, 0),
|
|
|
|
lineTo(205, 0),
|
|
|
|
}}
|
|
|
|
|
|
|
|
testSegments(t, "glyfTest.ttf", wants)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSegments(t *testing.T, filename string, wants [][]Segment) {
|
2017-01-21 06:32:42 +01:00
|
|
|
data, err := ioutil.ReadFile(filepath.FromSlash("../testdata/" + filename))
|
2017-01-15 10:09:03 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f, err := Parse(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-11-27 10:19:50 +01:00
|
|
|
if ng := f.NumGlyphs(); ng != len(wants) {
|
|
|
|
t.Fatalf("NumGlyphs: got %d, want %d", ng, len(wants))
|
|
|
|
}
|
2016-12-05 13:52:00 +01:00
|
|
|
var b Buffer
|
|
|
|
loop:
|
2016-11-27 10:19:50 +01:00
|
|
|
for i, want := range wants {
|
2016-12-17 02:44:26 +01:00
|
|
|
got, err := f.LoadGlyph(&b, GlyphIndex(i), nil)
|
|
|
|
if err != nil {
|
2016-12-05 13:52:00 +01:00
|
|
|
t.Errorf("i=%d: LoadGlyph: %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(got) != len(want) {
|
|
|
|
t.Errorf("i=%d: got %d elements, want %d\noverall:\ngot %v\nwant %v",
|
|
|
|
i, len(got), len(want), got, want)
|
2016-11-27 10:19:50 +01:00
|
|
|
continue
|
|
|
|
}
|
2016-12-05 13:52:00 +01:00
|
|
|
for j, g := range got {
|
|
|
|
if w := want[j]; g != w {
|
|
|
|
t.Errorf("i=%d: element %d:\ngot %v\nwant %v\noverall:\ngot %v\nwant %v",
|
|
|
|
i, j, g, w, got, want)
|
|
|
|
continue loop
|
|
|
|
}
|
2016-11-27 10:19:50 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-17 02:44:26 +01:00
|
|
|
if _, err := f.LoadGlyph(nil, 0xffff, nil); err != ErrNotFound {
|
|
|
|
t.Errorf("LoadGlyph(..., 0xffff, ...):\ngot %v\nwant %v", err, ErrNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
name, err := f.Name(nil, NameIDFamily)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Name: %v", err)
|
2017-01-15 10:09:03 +01:00
|
|
|
} else if want := filename[:len(filename)-len(".ttf")]; name != want {
|
2016-12-17 02:44:26 +01:00
|
|
|
t.Errorf("Name:\ngot %q\nwant %q", name, want)
|
|
|
|
}
|
2016-11-27 10:19:50 +01:00
|
|
|
}
|