1/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Resources.h"
9#include "SkEndian.h"
10#include "SkFontStream.h"
11#include "SkOSFile.h"
12#include "SkPaint.h"
13#include "SkStream.h"
14#include "SkTypeface.h"
15#include "Test.h"
16
17//#define DUMP_TABLES
18//#define DUMP_TTC_TABLES
19
20#define kFontTableTag_head          SkSetFourByteTag('h', 'e', 'a', 'd')
21#define kFontTableTag_hhea          SkSetFourByteTag('h', 'h', 'e', 'a')
22#define kFontTableTag_maxp          SkSetFourByteTag('m', 'a', 'x', 'p')
23
24static const struct TagSize {
25    SkFontTableTag  fTag;
26    size_t          fSize;
27} gKnownTableSizes[] = {
28    {   kFontTableTag_head,         54 },
29    {   kFontTableTag_hhea,         36 },
30    {   kFontTableTag_maxp,         32 },
31};
32
33// Test that getUnitsPerEm() agrees with a direct lookup in the 'head' table
34// (if that table is available).
35static void test_unitsPerEm(skiatest::Reporter* reporter, SkTypeface* face) {
36    int nativeUPEM = face->getUnitsPerEm();
37
38    int tableUPEM = -1;
39    size_t size = face->getTableSize(kFontTableTag_head);
40    if (size) {
41        // unitsPerEm is at offset 18 into the 'head' table.
42        uint16_t rawUPEM;
43        face->getTableData(kFontTableTag_head, 18, sizeof(rawUPEM), &rawUPEM);
44        tableUPEM = SkEndian_SwapBE16(rawUPEM);
45    }
46
47    if (tableUPEM >= 0) {
48        REPORTER_ASSERT(reporter, tableUPEM == nativeUPEM);
49    }
50}
51
52// Test that countGlyphs() agrees with a direct lookup in the 'maxp' table
53// (if that table is available).
54static void test_countGlyphs(skiatest::Reporter* reporter, SkTypeface* face) {
55    int nativeGlyphs = face->countGlyphs();
56
57    int tableGlyphs = -1;
58    size_t size = face->getTableSize(kFontTableTag_maxp);
59    if (size) {
60        // glyphs is at offset 4 into the 'maxp' table.
61        uint16_t rawGlyphs;
62        face->getTableData(kFontTableTag_maxp, 4, sizeof(rawGlyphs), &rawGlyphs);
63        tableGlyphs = SkEndian_SwapBE16(rawGlyphs);
64    }
65
66    if (tableGlyphs >= 0) {
67        REPORTER_ASSERT(reporter, tableGlyphs == nativeGlyphs);
68    }
69}
70
71// The following three are all the same code points in various encodings.
72static uint8_t utf8Chars[] = { 0x61, 0xE4, 0xB8, 0xAD, 0xD0, 0xAF, 0xD7, 0x99, 0xD7, 0x95, 0xF0, 0x9D, 0x84, 0x9E, 0x61 };
73static uint16_t utf16Chars[] = { 0x0061, 0x4E2D, 0x042F, 0x05D9, 0x05D5, 0xD834, 0xDD1E, 0x0061 };
74static uint32_t utf32Chars[] = { 0x00000061, 0x00004E2D, 0x0000042F, 0x000005D9, 0x000005D5, 0x0001D11E, 0x00000061 };
75
76struct CharsToGlyphs_TestData {
77    const void* chars;
78    int charCount;
79    size_t charsByteLength;
80    SkTypeface::Encoding typefaceEncoding;
81    const char* name;
82} static charsToGlyphs_TestData[] = {
83    { utf8Chars, 7, sizeof(utf8Chars), SkTypeface::kUTF8_Encoding, "Simple UTF-8" },
84    { utf16Chars, 7, sizeof(utf16Chars), SkTypeface::kUTF16_Encoding, "Simple UTF-16" },
85    { utf32Chars, 7, sizeof(utf32Chars), SkTypeface::kUTF32_Encoding, "Simple UTF-32" },
86};
87
88// Test that SkPaint::textToGlyphs agrees with SkTypeface::charsToGlyphs.
89static void test_charsToGlyphs(skiatest::Reporter* reporter, SkTypeface* face) {
90    uint16_t paintGlyphIds[256];
91    uint16_t faceGlyphIds[256];
92
93    for (size_t testIndex = 0; testIndex < SK_ARRAY_COUNT(charsToGlyphs_TestData); ++testIndex) {
94        CharsToGlyphs_TestData& test = charsToGlyphs_TestData[testIndex];
95
96        SkPaint paint;
97        paint.setTypeface(face);
98        paint.setTextEncoding((SkPaint::TextEncoding)test.typefaceEncoding);
99        paint.textToGlyphs(test.chars, test.charsByteLength, paintGlyphIds);
100
101        face->charsToGlyphs(test.chars, test.typefaceEncoding, faceGlyphIds, test.charCount);
102
103        for (int i = 0; i < test.charCount; ++i) {
104            SkString name;
105            face->getFamilyName(&name);
106            SkString a;
107            a.appendf("%s, paintGlyphIds[%d] = %d, faceGlyphIds[%d] = %d, face = %s",
108                      test.name, i, (int)paintGlyphIds[i], i, (int)faceGlyphIds[i], name.c_str());
109            REPORTER_ASSERT_MESSAGE(reporter, paintGlyphIds[i] == faceGlyphIds[i], a.c_str());
110        }
111    }
112}
113
114static void test_fontstream(skiatest::Reporter* reporter,
115                            SkStream* stream, int ttcIndex) {
116    int n = SkFontStream::GetTableTags(stream, ttcIndex, NULL);
117    SkAutoTArray<SkFontTableTag> array(n);
118
119    int n2 = SkFontStream::GetTableTags(stream, ttcIndex, array.get());
120    REPORTER_ASSERT(reporter, n == n2);
121
122    for (int i = 0; i < n; ++i) {
123#ifdef DUMP_TTC_TABLES
124        SkString str;
125        SkFontTableTag t = array[i];
126        str.appendUnichar((t >> 24) & 0xFF);
127        str.appendUnichar((t >> 16) & 0xFF);
128        str.appendUnichar((t >>  8) & 0xFF);
129        str.appendUnichar((t >>  0) & 0xFF);
130        SkDebugf("[%d:%d] '%s'\n", ttcIndex, i, str.c_str());
131#endif
132        size_t size = SkFontStream::GetTableSize(stream, ttcIndex, array[i]);
133        for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) {
134            if (gKnownTableSizes[j].fTag == array[i]) {
135                REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size);
136            }
137        }
138    }
139}
140
141static void test_fontstream(skiatest::Reporter* reporter, SkStream* stream) {
142    int count = SkFontStream::CountTTCEntries(stream);
143#ifdef DUMP_TTC_TABLES
144    SkDebugf("CountTTCEntries %d\n", count);
145#endif
146    for (int i = 0; i < count; ++i) {
147        test_fontstream(reporter, stream, i);
148    }
149}
150
151static void test_fontstream(skiatest::Reporter* reporter) {
152    // This test cannot run if there is no resource path.
153    SkString resourcePath = GetResourcePath();
154    if (resourcePath.isEmpty()) {
155        SkDebugf("Could not run fontstream test because resourcePath not specified.");
156        return;
157    }
158    SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), "test.ttc");
159
160    SkFILEStream stream(filename.c_str());
161    if (stream.isValid()) {
162        test_fontstream(reporter, &stream);
163    } else {
164        SkDebugf("Could not run fontstream test because test.ttc not found.");
165    }
166}
167
168static void test_tables(skiatest::Reporter* reporter, SkTypeface* face) {
169    if (false) { // avoid bit rot, suppress warning
170        SkFontID fontID = face->uniqueID();
171        REPORTER_ASSERT(reporter, fontID);
172    }
173
174    int count = face->countTables();
175
176    SkAutoTMalloc<SkFontTableTag> storage(count);
177    SkFontTableTag* tags = storage.get();
178
179    int count2 = face->getTableTags(tags);
180    REPORTER_ASSERT(reporter, count2 == count);
181
182    for (int i = 0; i < count; ++i) {
183        size_t size = face->getTableSize(tags[i]);
184        REPORTER_ASSERT(reporter, size > 0);
185
186#ifdef DUMP_TABLES
187        char name[5];
188        name[0] = (tags[i] >> 24) & 0xFF;
189        name[1] = (tags[i] >> 16) & 0xFF;
190        name[2] = (tags[i] >>  8) & 0xFF;
191        name[3] = (tags[i] >>  0) & 0xFF;
192        name[4] = 0;
193        SkDebugf("%s %d\n", name, size);
194#endif
195
196        for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) {
197            if (gKnownTableSizes[j].fTag == tags[i]) {
198                REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size);
199            }
200        }
201
202        // do we get the same size from GetTableData and GetTableSize
203        {
204            SkAutoMalloc data(size);
205            size_t size2 = face->getTableData(tags[i], 0, size, data.get());
206            REPORTER_ASSERT(reporter, size2 == size);
207        }
208    }
209}
210
211static void test_tables(skiatest::Reporter* reporter) {
212    static const char* const gNames[] = {
213        NULL,   // default font
214        "Arial", "Times", "Times New Roman", "Helvetica", "Courier",
215        "Courier New", "Terminal", "MS Sans Serif",
216    };
217
218    for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
219        SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(gNames[i], SkTypeface::kNormal));
220        if (face) {
221#ifdef DUMP_TABLES
222            SkDebugf("%s\n", gNames[i]);
223#endif
224            test_tables(reporter, face);
225            test_unitsPerEm(reporter, face);
226            test_countGlyphs(reporter, face);
227            test_charsToGlyphs(reporter, face);
228        }
229    }
230}
231
232/*
233 * Verifies that the advance values returned by generateAdvance and
234 * generateMetrics match.
235 */
236static void test_advances(skiatest::Reporter* reporter) {
237    static const char* const faces[] = {
238        NULL,   // default font
239        "Arial", "Times", "Times New Roman", "Helvetica", "Courier",
240        "Courier New", "Verdana", "monospace",
241    };
242
243    static const struct {
244        SkPaint::Hinting    hinting;
245        unsigned            flags;
246    } settings[] = {
247        { SkPaint::kNo_Hinting,     0                               },
248        { SkPaint::kNo_Hinting,     SkPaint::kLinearText_Flag       },
249        { SkPaint::kNo_Hinting,     SkPaint::kSubpixelText_Flag     },
250        { SkPaint::kSlight_Hinting, 0                               },
251        { SkPaint::kSlight_Hinting, SkPaint::kLinearText_Flag       },
252        { SkPaint::kSlight_Hinting, SkPaint::kSubpixelText_Flag     },
253        { SkPaint::kNormal_Hinting, 0                               },
254        { SkPaint::kNormal_Hinting, SkPaint::kLinearText_Flag       },
255        { SkPaint::kNormal_Hinting, SkPaint::kSubpixelText_Flag     },
256    };
257
258    static const struct {
259        SkScalar    fScaleX;
260        SkScalar    fSkewX;
261    } gScaleRec[] = {
262        { SK_Scalar1, 0 },
263        { SK_Scalar1/2, 0 },
264        // these two exercise obliquing (skew)
265        { SK_Scalar1, -SK_Scalar1/4 },
266        { SK_Scalar1/2, -SK_Scalar1/4 },
267    };
268
269    SkPaint paint;
270    char txt[] = "long.text.with.lots.of.dots.";
271
272    for (size_t i = 0; i < SK_ARRAY_COUNT(faces); i++) {
273        SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(faces[i], SkTypeface::kNormal));
274        paint.setTypeface(face);
275
276        for (size_t j = 0; j  < SK_ARRAY_COUNT(settings); j++) {
277            paint.setHinting(settings[j].hinting);
278            paint.setLinearText((settings[j].flags & SkPaint::kLinearText_Flag) != 0);
279            paint.setSubpixelText((settings[j].flags & SkPaint::kSubpixelText_Flag) != 0);
280
281            for (size_t k = 0; k < SK_ARRAY_COUNT(gScaleRec); ++k) {
282                paint.setTextScaleX(gScaleRec[k].fScaleX);
283                paint.setTextSkewX(gScaleRec[k].fSkewX);
284
285                SkRect bounds;
286
287                // For no hinting and light hinting this should take the
288                // optimized generateAdvance path.
289                SkScalar width1 = paint.measureText(txt, strlen(txt));
290
291                // Requesting the bounds forces a generateMetrics call.
292                SkScalar width2 = paint.measureText(txt, strlen(txt), &bounds);
293
294                // SkDebugf("Font: %s, generateAdvance: %f, generateMetrics: %f\n",
295                //    faces[i], SkScalarToFloat(width1), SkScalarToFloat(width2));
296
297                REPORTER_ASSERT(reporter, width1 == width2);
298            }
299        }
300    }
301}
302
303DEF_TEST(FontHost, reporter) {
304    test_tables(reporter);
305    test_fontstream(reporter);
306    test_advances(reporter);
307}
308
309// need tests for SkStrSearch
310