blob: ff275e2ad5ddbc83f6d80846985f35d281a181cb [file] [log] [blame]
reed@google.com17aa07d2012-02-23 14:51:10 +00001/*
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
tfarinabcbc1782014-06-18 14:32:48 -07008#include "Resources.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00009#include "SkEndian.h"
reed@google.comed268bf2013-03-11 20:13:36 +000010#include "SkFontStream.h"
bungeman@google.com3c996f82013-10-24 21:39:35 +000011#include "SkOSFile.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000012#include "SkPaint.h"
reed@google.comed268bf2013-03-11 20:13:36 +000013#include "SkStream.h"
reed@google.com17aa07d2012-02-23 14:51:10 +000014#include "SkTypeface.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000015#include "Test.h"
reed@google.com17aa07d2012-02-23 14:51:10 +000016
17//#define DUMP_TABLES
reed@google.comed268bf2013-03-11 20:13:36 +000018//#define DUMP_TTC_TABLES
reed@google.com17aa07d2012-02-23 14:51:10 +000019
20#define kFontTableTag_head SkSetFourByteTag('h', 'e', 'a', 'd')
21#define kFontTableTag_hhea SkSetFourByteTag('h', 'h', 'e', 'a')
reed@google.com17aa07d2012-02-23 14:51:10 +000022#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 },
reed@google.com17aa07d2012-02-23 14:51:10 +000030};
31
reed@google.coma262eea2013-03-21 15:20:00 +000032// Test that getUnitsPerEm() agrees with a direct lookup in the 'head' table
bungeman@google.com7bdd6142013-07-15 19:42:57 +000033// (if that table is available).
reed@google.com4b2af9c2012-07-31 17:24:44 +000034static void test_unitsPerEm(skiatest::Reporter* reporter, SkTypeface* face) {
bungeman@google.com7bdd6142013-07-15 19:42:57 +000035 int nativeUPEM = face->getUnitsPerEm();
reed@google.com4b2af9c2012-07-31 17:24:44 +000036
reed@google.coma262eea2013-03-21 15:20:00 +000037 int tableUPEM = -1;
reed@google.com4b2af9c2012-07-31 17:24:44 +000038 size_t size = face->getTableSize(kFontTableTag_head);
39 if (size) {
reed@google.com4b2af9c2012-07-31 17:24:44 +000040 // unitsPerEm is at offset 18 into the 'head' table.
bungeman@google.com7bdd6142013-07-15 19:42:57 +000041 uint16_t rawUPEM;
42 face->getTableData(kFontTableTag_head, 18, sizeof(rawUPEM), &rawUPEM);
43 tableUPEM = SkEndian_SwapBE16(rawUPEM);
reed@google.coma262eea2013-03-21 15:20:00 +000044 }
reed@google.coma262eea2013-03-21 15:20:00 +000045
46 if (tableUPEM >= 0) {
47 REPORTER_ASSERT(reporter, tableUPEM == nativeUPEM);
reed@google.com4b2af9c2012-07-31 17:24:44 +000048 }
49}
50
bungeman@google.com7bdd6142013-07-15 19:42:57 +000051// Test that countGlyphs() agrees with a direct lookup in the 'maxp' table
52// (if that table is available).
53static 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);
bungeman@google.com7bdd6142013-07-15 19:42:57 +000067 }
68}
bungeman@google.com72b8cb22013-10-25 17:49:08 +000069
bungeman@google.com3c996f82013-10-24 21:39:35 +000070// The following three are all the same code points in various encodings.
bungeman7b09aab2014-09-24 11:04:41 -070071// a中Яיו𝄞a𠮟
72static uint8_t utf8Chars[] = { 0x61, 0xE4,0xB8,0xAD, 0xD0,0xAF, 0xD7,0x99, 0xD7,0x95, 0xF0,0x9D,0x84,0x9E, 0x61, 0xF0,0xA0,0xAE,0x9F };
73static uint16_t utf16Chars[] = { 0x0061, 0x4E2D, 0x042F, 0x05D9, 0x05D5, 0xD834,0xDD1E, 0x0061, 0xD842,0xDF9F };
74static uint32_t utf32Chars[] = { 0x00000061, 0x00004E2D, 0x0000042F, 0x000005D9, 0x000005D5, 0x0001D11E, 0x00000061, 0x00020B9F };
bungeman@google.com3c996f82013-10-24 21:39:35 +000075
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[] = {
bungeman7b09aab2014-09-24 11:04:41 -070083 { 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" },
bungeman@google.com3c996f82013-10-24 21:39:35 +000086};
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;
skia.committer@gmail.com7dc4fd02013-10-25 07:02:14 +0000107 a.appendf("%s, paintGlyphIds[%d] = %d, faceGlyphIds[%d] = %d, face = %s",
bungeman@google.com3c996f82013-10-24 21:39:35 +0000108 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
bungeman3ffa1262015-04-30 17:12:58 -0400114static void test_fontstream(skiatest::Reporter* reporter, SkStream* stream, int ttcIndex) {
reed@google.comed268bf2013-03-11 20:13:36 +0000115 int n = SkFontStream::GetTableTags(stream, ttcIndex, NULL);
116 SkAutoTArray<SkFontTableTag> array(n);
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000117
reed@google.comed268bf2013-03-11 20:13:36 +0000118 int n2 = SkFontStream::GetTableTags(stream, ttcIndex, array.get());
119 REPORTER_ASSERT(reporter, n == n2);
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000120
reed@google.comed268bf2013-03-11 20:13:36 +0000121 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 }
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000136 }
reed@google.comed268bf2013-03-11 20:13:36 +0000137 }
138}
139
bungeman3ffa1262015-04-30 17:12:58 -0400140static 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
reed@google.comed268bf2013-03-11 20:13:36 +0000147 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
reed@google.com17aa07d2012-02-23 14:51:10 +0000156static void test_tables(skiatest::Reporter* reporter, SkTypeface* face) {
caryclark@google.com42639cd2012-06-06 12:03:39 +0000157 if (false) { // avoid bit rot, suppress warning
robertphillips@google.com5b332112013-01-30 20:33:12 +0000158 SkFontID fontID = face->uniqueID();
caryclark@google.com42639cd2012-06-06 12:03:39 +0000159 REPORTER_ASSERT(reporter, fontID);
160 }
reed@google.com17aa07d2012-02-23 14:51:10 +0000161
reed@google.com8b0a3352012-04-19 18:52:39 +0000162 int count = face->countTables();
reed@google.com17aa07d2012-02-23 14:51:10 +0000163
164 SkAutoTMalloc<SkFontTableTag> storage(count);
165 SkFontTableTag* tags = storage.get();
reed@google.comfbd033d2012-02-23 16:15:58 +0000166
reed@google.com8b0a3352012-04-19 18:52:39 +0000167 int count2 = face->getTableTags(tags);
reed@google.comfbd033d2012-02-23 16:15:58 +0000168 REPORTER_ASSERT(reporter, count2 == count);
reed@google.com17aa07d2012-02-23 14:51:10 +0000169
170 for (int i = 0; i < count; ++i) {
reed@google.com8b0a3352012-04-19 18:52:39 +0000171 size_t size = face->getTableSize(tags[i]);
reed@google.com17aa07d2012-02-23 14:51:10 +0000172 REPORTER_ASSERT(reporter, size > 0);
173
174#ifdef DUMP_TABLES
175 char name[5];
176 name[0] = (tags[i] >> 24) & 0xFF;
177 name[1] = (tags[i] >> 16) & 0xFF;
178 name[2] = (tags[i] >> 8) & 0xFF;
179 name[3] = (tags[i] >> 0) & 0xFF;
180 name[4] = 0;
181 SkDebugf("%s %d\n", name, size);
182#endif
183
184 for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) {
185 if (gKnownTableSizes[j].fTag == tags[i]) {
186 REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size);
187 }
188 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000189
reed@google.comfbd033d2012-02-23 16:15:58 +0000190 // do we get the same size from GetTableData and GetTableSize
191 {
192 SkAutoMalloc data(size);
reed@google.com8b0a3352012-04-19 18:52:39 +0000193 size_t size2 = face->getTableData(tags[i], 0, size, data.get());
reed@google.comfbd033d2012-02-23 16:15:58 +0000194 REPORTER_ASSERT(reporter, size2 == size);
195 }
reed@google.com17aa07d2012-02-23 14:51:10 +0000196 }
197}
198
199static void test_tables(skiatest::Reporter* reporter) {
200 static const char* const gNames[] = {
201 NULL, // default font
bungeman7b09aab2014-09-24 11:04:41 -0700202 "Helvetica", "Arial",
203 "Times", "Times New Roman",
204 "Courier", "Courier New",
205 "Terminal", "MS Sans Serif",
206 "Hiragino Mincho ProN", "MS PGothic",
reed@google.com17aa07d2012-02-23 14:51:10 +0000207 };
208
209 for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
bungeman@google.com3c996f82013-10-24 21:39:35 +0000210 SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(gNames[i], SkTypeface::kNormal));
reed@google.com17aa07d2012-02-23 14:51:10 +0000211 if (face) {
212#ifdef DUMP_TABLES
213 SkDebugf("%s\n", gNames[i]);
214#endif
215 test_tables(reporter, face);
reed@google.com4b2af9c2012-07-31 17:24:44 +0000216 test_unitsPerEm(reporter, face);
bungeman@google.com7bdd6142013-07-15 19:42:57 +0000217 test_countGlyphs(reporter, face);
bungeman@google.com72b8cb22013-10-25 17:49:08 +0000218 test_charsToGlyphs(reporter, face);
reed@google.com17aa07d2012-02-23 14:51:10 +0000219 }
220 }
221}
222
bungeman@google.com34f10262012-03-23 18:11:47 +0000223/*
224 * Verifies that the advance values returned by generateAdvance and
225 * generateMetrics match.
226 */
227static void test_advances(skiatest::Reporter* reporter) {
228 static const char* const faces[] = {
229 NULL, // default font
230 "Arial", "Times", "Times New Roman", "Helvetica", "Courier",
231 "Courier New", "Verdana", "monospace",
232 };
233
234 static const struct {
235 SkPaint::Hinting hinting;
236 unsigned flags;
237 } settings[] = {
238 { SkPaint::kNo_Hinting, 0 },
239 { SkPaint::kNo_Hinting, SkPaint::kLinearText_Flag },
240 { SkPaint::kNo_Hinting, SkPaint::kSubpixelText_Flag },
241 { SkPaint::kSlight_Hinting, 0 },
242 { SkPaint::kSlight_Hinting, SkPaint::kLinearText_Flag },
243 { SkPaint::kSlight_Hinting, SkPaint::kSubpixelText_Flag },
244 { SkPaint::kNormal_Hinting, 0 },
245 { SkPaint::kNormal_Hinting, SkPaint::kLinearText_Flag },
246 { SkPaint::kNormal_Hinting, SkPaint::kSubpixelText_Flag },
247 };
248
reed@google.comd074c372012-07-18 13:45:58 +0000249 static const struct {
250 SkScalar fScaleX;
251 SkScalar fSkewX;
252 } gScaleRec[] = {
253 { SK_Scalar1, 0 },
254 { SK_Scalar1/2, 0 },
255 // these two exercise obliquing (skew)
256 { SK_Scalar1, -SK_Scalar1/4 },
257 { SK_Scalar1/2, -SK_Scalar1/4 },
258 };
259
bungeman@google.com34f10262012-03-23 18:11:47 +0000260 SkPaint paint;
261 char txt[] = "long.text.with.lots.of.dots.";
262
263 for (size_t i = 0; i < SK_ARRAY_COUNT(faces); i++) {
bungeman@google.com94471032013-02-25 15:55:13 +0000264 SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(faces[i], SkTypeface::kNormal));
bungeman@google.com34f10262012-03-23 18:11:47 +0000265 paint.setTypeface(face);
266
267 for (size_t j = 0; j < SK_ARRAY_COUNT(settings); j++) {
reed@google.comd074c372012-07-18 13:45:58 +0000268 paint.setHinting(settings[j].hinting);
269 paint.setLinearText((settings[j].flags & SkPaint::kLinearText_Flag) != 0);
270 paint.setSubpixelText((settings[j].flags & SkPaint::kSubpixelText_Flag) != 0);
bungeman@google.com34f10262012-03-23 18:11:47 +0000271
reed@google.comd074c372012-07-18 13:45:58 +0000272 for (size_t k = 0; k < SK_ARRAY_COUNT(gScaleRec); ++k) {
273 paint.setTextScaleX(gScaleRec[k].fScaleX);
274 paint.setTextSkewX(gScaleRec[k].fSkewX);
bungeman@google.com34f10262012-03-23 18:11:47 +0000275
reed@google.comd074c372012-07-18 13:45:58 +0000276 SkRect bounds;
bungeman@google.com34f10262012-03-23 18:11:47 +0000277
reed@google.comd074c372012-07-18 13:45:58 +0000278 // For no hinting and light hinting this should take the
279 // optimized generateAdvance path.
280 SkScalar width1 = paint.measureText(txt, strlen(txt));
bungeman@google.com34f10262012-03-23 18:11:47 +0000281
reed@google.comd074c372012-07-18 13:45:58 +0000282 // Requesting the bounds forces a generateMetrics call.
283 SkScalar width2 = paint.measureText(txt, strlen(txt), &bounds);
bungeman@google.com34f10262012-03-23 18:11:47 +0000284
reed@google.comd074c372012-07-18 13:45:58 +0000285 // SkDebugf("Font: %s, generateAdvance: %f, generateMetrics: %f\n",
286 // faces[i], SkScalarToFloat(width1), SkScalarToFloat(width2));
287
288 REPORTER_ASSERT(reporter, width1 == width2);
289 }
bungeman@google.com34f10262012-03-23 18:11:47 +0000290 }
291 }
292}
293
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000294DEF_TEST(FontHost, reporter) {
reed@google.com17aa07d2012-02-23 14:51:10 +0000295 test_tables(reporter);
reed@google.comed268bf2013-03-11 20:13:36 +0000296 test_fontstream(reporter);
bungeman@google.com34f10262012-03-23 18:11:47 +0000297 test_advances(reporter);
reed@google.com17aa07d2012-02-23 14:51:10 +0000298}
299
300// need tests for SkStrSearch