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 
24 static const struct TagSize {
25     SkFontTableTag  fTag;
26     size_t          fSize;
27 } gKnownTableSizes[] = {
28     {   kFontTableTag_head,         54 },
29     {   kFontTableTag_hhea,         36 },
30 };
31 
32 // Test that getUnitsPerEm() agrees with a direct lookup in the 'head' table
33 // (if that table is available).
test_unitsPerEm(skiatest::Reporter * reporter,SkTypeface * face)34 static void test_unitsPerEm(skiatest::Reporter* reporter, SkTypeface* face) {
35     int nativeUPEM = face->getUnitsPerEm();
36 
37     int tableUPEM = -1;
38     size_t size = face->getTableSize(kFontTableTag_head);
39     if (size) {
40         // unitsPerEm is at offset 18 into the 'head' table.
41         uint16_t rawUPEM;
42         face->getTableData(kFontTableTag_head, 18, sizeof(rawUPEM), &rawUPEM);
43         tableUPEM = SkEndian_SwapBE16(rawUPEM);
44     }
45 
46     if (tableUPEM >= 0) {
47         REPORTER_ASSERT(reporter, tableUPEM == nativeUPEM);
48     }
49 }
50 
51 // Test that countGlyphs() agrees with a direct lookup in the 'maxp' table
52 // (if that table is available).
test_countGlyphs(skiatest::Reporter * reporter,SkTypeface * face)53 static void test_countGlyphs(skiatest::Reporter* reporter, SkTypeface* face) {
54     int nativeGlyphs = face->countGlyphs();
55 
56     int tableGlyphs = -1;
57     size_t size = face->getTableSize(kFontTableTag_maxp);
58     if (size) {
59         // glyphs is at offset 4 into the 'maxp' table.
60         uint16_t rawGlyphs;
61         face->getTableData(kFontTableTag_maxp, 4, sizeof(rawGlyphs), &rawGlyphs);
62         tableGlyphs = SkEndian_SwapBE16(rawGlyphs);
63     }
64 
65     if (tableGlyphs >= 0) {
66         REPORTER_ASSERT(reporter, tableGlyphs == nativeGlyphs);
67     }
68 }
69 
70 // The following three are all the same code points in various encodings.
71 // a中Яיו��a��
72 static uint8_t utf8Chars[] = { 0x61, 0xE4,0xB8,0xAD, 0xD0,0xAF, 0xD7,0x99, 0xD7,0x95, 0xF0,0x9D,0x84,0x9E, 0x61, 0xF0,0xA0,0xAE,0x9F };
73 static uint16_t utf16Chars[] = { 0x0061, 0x4E2D, 0x042F, 0x05D9, 0x05D5, 0xD834,0xDD1E, 0x0061, 0xD842,0xDF9F };
74 static uint32_t utf32Chars[] = { 0x00000061, 0x00004E2D, 0x0000042F, 0x000005D9, 0x000005D5, 0x0001D11E, 0x00000061, 0x00020B9F };
75 
76 struct 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, 8, sizeof(utf8Chars), SkTypeface::kUTF8_Encoding, "Simple UTF-8" },
84     { utf16Chars, 8, sizeof(utf16Chars), SkTypeface::kUTF16_Encoding, "Simple UTF-16" },
85     { utf32Chars, 8, sizeof(utf32Chars), SkTypeface::kUTF32_Encoding, "Simple UTF-32" },
86 };
87 
88 // Test that SkPaint::textToGlyphs agrees with SkTypeface::charsToGlyphs.
test_charsToGlyphs(skiatest::Reporter * reporter,SkTypeface * face)89 static 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 
test_fontstream(skiatest::Reporter * reporter,SkStream * stream,int ttcIndex)114 static void test_fontstream(skiatest::Reporter* reporter, SkStream* stream, int ttcIndex) {
115     int n = SkFontStream::GetTableTags(stream, ttcIndex, nullptr);
116     SkAutoTArray<SkFontTableTag> array(n);
117 
118     int n2 = SkFontStream::GetTableTags(stream, ttcIndex, array.get());
119     REPORTER_ASSERT(reporter, n == n2);
120 
121     for (int i = 0; i < n; ++i) {
122 #ifdef DUMP_TTC_TABLES
123         SkString str;
124         SkFontTableTag t = array[i];
125         str.appendUnichar((t >> 24) & 0xFF);
126         str.appendUnichar((t >> 16) & 0xFF);
127         str.appendUnichar((t >>  8) & 0xFF);
128         str.appendUnichar((t >>  0) & 0xFF);
129         SkDebugf("[%d:%d] '%s'\n", ttcIndex, i, str.c_str());
130 #endif
131         size_t size = SkFontStream::GetTableSize(stream, ttcIndex, array[i]);
132         for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) {
133             if (gKnownTableSizes[j].fTag == array[i]) {
134                 REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size);
135             }
136         }
137     }
138 }
139 
test_fontstream(skiatest::Reporter * reporter)140 static void test_fontstream(skiatest::Reporter* reporter) {
141     SkAutoTDelete<SkStreamAsset> stream(GetResourceAsStream("/fonts/test.ttc"));
142     if (!stream) {
143         SkDebugf("Skipping FontHostTest::test_fontstream\n");
144         return;
145     }
146 
147     int count = SkFontStream::CountTTCEntries(stream);
148 #ifdef DUMP_TTC_TABLES
149     SkDebugf("CountTTCEntries %d\n", count);
150 #endif
151     for (int i = 0; i < count; ++i) {
152         test_fontstream(reporter, stream, i);
153     }
154 }
155 
test_symbolfont(skiatest::Reporter * reporter)156 static void test_symbolfont(skiatest::Reporter* reporter) {
157     SkAutoTUnref<SkTypeface> typeface(GetResourceAsTypeface("/fonts/SpiderSymbol.ttf"));
158     if (!typeface) {
159         SkDebugf("Skipping FontHostTest::test_symbolfont\n");
160         return;
161     }
162 
163     SkUnichar c = 0xf021;
164     uint16_t g;
165     SkPaint paint;
166     paint.setTypeface(typeface);
167     paint.setTextEncoding(SkPaint::kUTF32_TextEncoding);
168     paint.textToGlyphs(&c, 4, &g);
169     REPORTER_ASSERT(reporter, g == 3);
170 }
171 
test_tables(skiatest::Reporter * reporter,SkTypeface * face)172 static void test_tables(skiatest::Reporter* reporter, SkTypeface* face) {
173     if (false) { // avoid bit rot, suppress warning
174         SkFontID fontID = face->uniqueID();
175         REPORTER_ASSERT(reporter, fontID);
176     }
177 
178     int count = face->countTables();
179 
180     SkAutoTMalloc<SkFontTableTag> storage(count);
181     SkFontTableTag* tags = storage.get();
182 
183     int count2 = face->getTableTags(tags);
184     REPORTER_ASSERT(reporter, count2 == count);
185 
186     for (int i = 0; i < count; ++i) {
187         size_t size = face->getTableSize(tags[i]);
188         REPORTER_ASSERT(reporter, size > 0);
189 
190 #ifdef DUMP_TABLES
191         char name[5];
192         name[0] = (tags[i] >> 24) & 0xFF;
193         name[1] = (tags[i] >> 16) & 0xFF;
194         name[2] = (tags[i] >>  8) & 0xFF;
195         name[3] = (tags[i] >>  0) & 0xFF;
196         name[4] = 0;
197         SkDebugf("%s %d\n", name, size);
198 #endif
199 
200         for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) {
201             if (gKnownTableSizes[j].fTag == tags[i]) {
202                 REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size);
203             }
204         }
205 
206         // do we get the same size from GetTableData and GetTableSize
207         {
208             SkAutoMalloc data(size);
209             size_t size2 = face->getTableData(tags[i], 0, size, data.get());
210             REPORTER_ASSERT(reporter, size2 == size);
211         }
212     }
213 }
214 
test_tables(skiatest::Reporter * reporter)215 static void test_tables(skiatest::Reporter* reporter) {
216     static const char* const gNames[] = {
217         nullptr,   // default font
218         "Helvetica", "Arial",
219         "Times", "Times New Roman",
220         "Courier", "Courier New",
221         "Terminal", "MS Sans Serif",
222         "Hiragino Mincho ProN", "MS PGothic",
223     };
224 
225     for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
226         SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(gNames[i], SkTypeface::kNormal));
227         if (face) {
228 #ifdef DUMP_TABLES
229             SkDebugf("%s\n", gNames[i]);
230 #endif
231             test_tables(reporter, face);
232             test_unitsPerEm(reporter, face);
233             test_countGlyphs(reporter, face);
234             test_charsToGlyphs(reporter, face);
235         }
236     }
237 }
238 
239 /*
240  * Verifies that the advance values returned by generateAdvance and
241  * generateMetrics match.
242  */
test_advances(skiatest::Reporter * reporter)243 static void test_advances(skiatest::Reporter* reporter) {
244     static const char* const faces[] = {
245         nullptr,   // default font
246         "Arial", "Times", "Times New Roman", "Helvetica", "Courier",
247         "Courier New", "Verdana", "monospace",
248     };
249 
250     static const struct {
251         SkPaint::Hinting    hinting;
252         unsigned            flags;
253     } settings[] = {
254         { SkPaint::kNo_Hinting,     0                               },
255         { SkPaint::kNo_Hinting,     SkPaint::kLinearText_Flag       },
256         { SkPaint::kNo_Hinting,     SkPaint::kSubpixelText_Flag     },
257         { SkPaint::kSlight_Hinting, 0                               },
258         { SkPaint::kSlight_Hinting, SkPaint::kLinearText_Flag       },
259         { SkPaint::kSlight_Hinting, SkPaint::kSubpixelText_Flag     },
260         { SkPaint::kNormal_Hinting, 0                               },
261         { SkPaint::kNormal_Hinting, SkPaint::kLinearText_Flag       },
262         { SkPaint::kNormal_Hinting, SkPaint::kSubpixelText_Flag     },
263     };
264 
265     static const struct {
266         SkScalar    fScaleX;
267         SkScalar    fSkewX;
268     } gScaleRec[] = {
269         { SK_Scalar1, 0 },
270         { SK_Scalar1/2, 0 },
271         // these two exercise obliquing (skew)
272         { SK_Scalar1, -SK_Scalar1/4 },
273         { SK_Scalar1/2, -SK_Scalar1/4 },
274     };
275 
276     SkPaint paint;
277     char txt[] = "long.text.with.lots.of.dots.";
278 
279     for (size_t i = 0; i < SK_ARRAY_COUNT(faces); i++) {
280         SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(faces[i], SkTypeface::kNormal));
281         paint.setTypeface(face);
282 
283         for (size_t j = 0; j  < SK_ARRAY_COUNT(settings); j++) {
284             paint.setHinting(settings[j].hinting);
285             paint.setLinearText((settings[j].flags & SkPaint::kLinearText_Flag) != 0);
286             paint.setSubpixelText((settings[j].flags & SkPaint::kSubpixelText_Flag) != 0);
287 
288             for (size_t k = 0; k < SK_ARRAY_COUNT(gScaleRec); ++k) {
289                 paint.setTextScaleX(gScaleRec[k].fScaleX);
290                 paint.setTextSkewX(gScaleRec[k].fSkewX);
291 
292                 SkRect bounds;
293 
294                 // For no hinting and light hinting this should take the
295                 // optimized generateAdvance path.
296                 SkScalar width1 = paint.measureText(txt, strlen(txt));
297 
298                 // Requesting the bounds forces a generateMetrics call.
299                 SkScalar width2 = paint.measureText(txt, strlen(txt), &bounds);
300 
301                 // SkDebugf("Font: %s, generateAdvance: %f, generateMetrics: %f\n",
302                 //    faces[i], SkScalarToFloat(width1), SkScalarToFloat(width2));
303 
304                 REPORTER_ASSERT(reporter, width1 == width2);
305             }
306         }
307     }
308 }
309 
DEF_TEST(FontHost,reporter)310 DEF_TEST(FontHost, reporter) {
311     test_tables(reporter);
312     test_fontstream(reporter);
313     test_advances(reporter);
314     test_symbolfont(reporter);
315 }
316 
317 // need tests for SkStrSearch
318