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