blob: a7ecc5a9dcf1578f0aff9dac360bf9665734dc7d [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
8#include "Test.h"
reed@google.com8b0a3352012-04-19 18:52:39 +00009#include "SkPaint.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"
reed@google.comed268bf2013-03-11 20:13:36 +000012#include "SkStream.h"
reed@google.com17aa07d2012-02-23 14:51:10 +000013#include "SkTypeface.h"
reed@google.com4b2af9c2012-07-31 17:24:44 +000014#include "SkEndian.h"
reed@google.com17aa07d2012-02-23 14:51:10 +000015
16//#define DUMP_TABLES
reed@google.comed268bf2013-03-11 20:13:36 +000017//#define DUMP_TTC_TABLES
reed@google.com17aa07d2012-02-23 14:51:10 +000018
19#define kFontTableTag_head SkSetFourByteTag('h', 'e', 'a', 'd')
20#define kFontTableTag_hhea SkSetFourByteTag('h', 'h', 'e', 'a')
reed@google.com17aa07d2012-02-23 14:51:10 +000021#define kFontTableTag_maxp SkSetFourByteTag('m', 'a', 'x', 'p')
22
23static const struct TagSize {
24 SkFontTableTag fTag;
25 size_t fSize;
26} gKnownTableSizes[] = {
27 { kFontTableTag_head, 54 },
28 { kFontTableTag_hhea, 36 },
reed@google.com17aa07d2012-02-23 14:51:10 +000029 { kFontTableTag_maxp, 32 },
30};
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);
48 } else {
49 // not sure this is a bug, but lets report it for now as info.
50 SkDebugf("--- typeface returned 0 upem [%X]\n", face->uniqueID());
reed@google.com4b2af9c2012-07-31 17:24:44 +000051 }
52}
53
bungeman@google.com7bdd6142013-07-15 19:42:57 +000054// Test that countGlyphs() agrees with a direct lookup in the 'maxp' table
55// (if that table is available).
56static void test_countGlyphs(skiatest::Reporter* reporter, SkTypeface* face) {
57 int nativeGlyphs = face->countGlyphs();
58
59 int tableGlyphs = -1;
60 size_t size = face->getTableSize(kFontTableTag_maxp);
61 if (size) {
62 // glyphs is at offset 4 into the 'maxp' table.
63 uint16_t rawGlyphs;
64 face->getTableData(kFontTableTag_maxp, 4, sizeof(rawGlyphs), &rawGlyphs);
65 tableGlyphs = SkEndian_SwapBE16(rawGlyphs);
66 }
67
68 if (tableGlyphs >= 0) {
69 REPORTER_ASSERT(reporter, tableGlyphs == nativeGlyphs);
70 } else {
71 // not sure this is a bug, but lets report it for now as info.
72 SkDebugf("--- typeface returned 0 glyphs [%X]\n", face->uniqueID());
73 }
74}
bungeman@google.com72b8cb22013-10-25 17:49:08 +000075
bungeman@google.com3c996f82013-10-24 21:39:35 +000076// The following three are all the same code points in various encodings.
77static uint8_t utf8Chars[] = { 0x61, 0xE4, 0xB8, 0xAD, 0xD0, 0xAF, 0xD7, 0x99, 0xD7, 0x95, 0xF0, 0x9D, 0x84, 0x9E, 0x61 };
78static uint16_t utf16Chars[] = { 0x0061, 0x4E2D, 0x042F, 0x05D9, 0x05D5, 0xD834, 0xDD1E, 0x0061 };
79static uint32_t utf32Chars[] = { 0x00000061, 0x00004E2D, 0x0000042F, 0x000005D9, 0x000005D5, 0x0001D11E, 0x00000061 };
80
81struct CharsToGlyphs_TestData {
82 const void* chars;
83 int charCount;
84 size_t charsByteLength;
85 SkTypeface::Encoding typefaceEncoding;
86 const char* name;
87} static charsToGlyphs_TestData[] = {
88 { utf8Chars, 7, sizeof(utf8Chars), SkTypeface::kUTF8_Encoding, "Simple UTF-8" },
89 { utf16Chars, 7, sizeof(utf16Chars), SkTypeface::kUTF16_Encoding, "Simple UTF-16" },
90 { utf32Chars, 7, sizeof(utf32Chars), SkTypeface::kUTF32_Encoding, "Simple UTF-32" },
91};
92
93// Test that SkPaint::textToGlyphs agrees with SkTypeface::charsToGlyphs.
94static void test_charsToGlyphs(skiatest::Reporter* reporter, SkTypeface* face) {
95 uint16_t paintGlyphIds[256];
96 uint16_t faceGlyphIds[256];
97
98 for (size_t testIndex = 0; testIndex < SK_ARRAY_COUNT(charsToGlyphs_TestData); ++testIndex) {
99 CharsToGlyphs_TestData& test = charsToGlyphs_TestData[testIndex];
100
101 SkPaint paint;
102 paint.setTypeface(face);
103 paint.setTextEncoding((SkPaint::TextEncoding)test.typefaceEncoding);
104 paint.textToGlyphs(test.chars, test.charsByteLength, paintGlyphIds);
105
106 face->charsToGlyphs(test.chars, test.typefaceEncoding, faceGlyphIds, test.charCount);
107
108 for (int i = 0; i < test.charCount; ++i) {
109 SkString name;
110 face->getFamilyName(&name);
111 SkString a;
skia.committer@gmail.com7dc4fd02013-10-25 07:02:14 +0000112 a.appendf("%s, paintGlyphIds[%d] = %d, faceGlyphIds[%d] = %d, face = %s",
bungeman@google.com3c996f82013-10-24 21:39:35 +0000113 test.name, i, (int)paintGlyphIds[i], i, (int)faceGlyphIds[i], name.c_str());
114 REPORTER_ASSERT_MESSAGE(reporter, paintGlyphIds[i] == faceGlyphIds[i], a.c_str());
115 }
116 }
117}
118
reed@google.comed268bf2013-03-11 20:13:36 +0000119static void test_fontstream(skiatest::Reporter* reporter,
120 SkStream* stream, int ttcIndex) {
121 int n = SkFontStream::GetTableTags(stream, ttcIndex, NULL);
122 SkAutoTArray<SkFontTableTag> array(n);
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000123
reed@google.comed268bf2013-03-11 20:13:36 +0000124 int n2 = SkFontStream::GetTableTags(stream, ttcIndex, array.get());
125 REPORTER_ASSERT(reporter, n == n2);
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000126
reed@google.comed268bf2013-03-11 20:13:36 +0000127 for (int i = 0; i < n; ++i) {
128#ifdef DUMP_TTC_TABLES
129 SkString str;
130 SkFontTableTag t = array[i];
131 str.appendUnichar((t >> 24) & 0xFF);
132 str.appendUnichar((t >> 16) & 0xFF);
133 str.appendUnichar((t >> 8) & 0xFF);
134 str.appendUnichar((t >> 0) & 0xFF);
135 SkDebugf("[%d:%d] '%s'\n", ttcIndex, i, str.c_str());
136#endif
137 size_t size = SkFontStream::GetTableSize(stream, ttcIndex, array[i]);
138 for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) {
139 if (gKnownTableSizes[j].fTag == array[i]) {
140 REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size);
141 }
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +0000142 }
reed@google.comed268bf2013-03-11 20:13:36 +0000143 }
144}
145
146static void test_fontstream(skiatest::Reporter* reporter, SkStream* stream) {
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
156static void test_fontstream(skiatest::Reporter* reporter) {
bungeman@google.com3c996f82013-10-24 21:39:35 +0000157 // This test cannot run if there is no resource path.
158 SkString resourcePath = skiatest::Test::GetResourcePath();
159 if (resourcePath.isEmpty()) {
160 SkDebugf("Could not run fontstream test because resourcePath not specified.");
161 return;
162 }
163 SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), "test.ttc");
164
165 SkFILEStream stream(filename.c_str());
reed@google.comed268bf2013-03-11 20:13:36 +0000166 if (stream.isValid()) {
167 test_fontstream(reporter, &stream);
bungeman@google.com3c996f82013-10-24 21:39:35 +0000168 } else {
169 SkDebugf("Could not run fontstream test because test.ttc not found.");
reed@google.comed268bf2013-03-11 20:13:36 +0000170 }
171}
172
reed@google.com17aa07d2012-02-23 14:51:10 +0000173static void test_tables(skiatest::Reporter* reporter, SkTypeface* face) {
caryclark@google.com42639cd2012-06-06 12:03:39 +0000174 if (false) { // avoid bit rot, suppress warning
robertphillips@google.com5b332112013-01-30 20:33:12 +0000175 SkFontID fontID = face->uniqueID();
caryclark@google.com42639cd2012-06-06 12:03:39 +0000176 REPORTER_ASSERT(reporter, fontID);
177 }
reed@google.com17aa07d2012-02-23 14:51:10 +0000178
reed@google.com8b0a3352012-04-19 18:52:39 +0000179 int count = face->countTables();
reed@google.com17aa07d2012-02-23 14:51:10 +0000180
181 SkAutoTMalloc<SkFontTableTag> storage(count);
182 SkFontTableTag* tags = storage.get();
reed@google.comfbd033d2012-02-23 16:15:58 +0000183
reed@google.com8b0a3352012-04-19 18:52:39 +0000184 int count2 = face->getTableTags(tags);
reed@google.comfbd033d2012-02-23 16:15:58 +0000185 REPORTER_ASSERT(reporter, count2 == count);
reed@google.com17aa07d2012-02-23 14:51:10 +0000186
187 for (int i = 0; i < count; ++i) {
reed@google.com8b0a3352012-04-19 18:52:39 +0000188 size_t size = face->getTableSize(tags[i]);
reed@google.com17aa07d2012-02-23 14:51:10 +0000189 REPORTER_ASSERT(reporter, size > 0);
190
191#ifdef DUMP_TABLES
192 char name[5];
193 name[0] = (tags[i] >> 24) & 0xFF;
194 name[1] = (tags[i] >> 16) & 0xFF;
195 name[2] = (tags[i] >> 8) & 0xFF;
196 name[3] = (tags[i] >> 0) & 0xFF;
197 name[4] = 0;
198 SkDebugf("%s %d\n", name, size);
199#endif
200
201 for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) {
202 if (gKnownTableSizes[j].fTag == tags[i]) {
203 REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size);
204 }
205 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000206
reed@google.comfbd033d2012-02-23 16:15:58 +0000207 // do we get the same size from GetTableData and GetTableSize
208 {
209 SkAutoMalloc data(size);
reed@google.com8b0a3352012-04-19 18:52:39 +0000210 size_t size2 = face->getTableData(tags[i], 0, size, data.get());
reed@google.comfbd033d2012-02-23 16:15:58 +0000211 REPORTER_ASSERT(reporter, size2 == size);
212 }
reed@google.com17aa07d2012-02-23 14:51:10 +0000213 }
214}
215
216static void test_tables(skiatest::Reporter* reporter) {
217 static const char* const gNames[] = {
218 NULL, // default font
219 "Arial", "Times", "Times New Roman", "Helvetica", "Courier",
bungeman@google.comc0d3f2f2012-07-31 21:39:05 +0000220 "Courier New", "Terminal", "MS Sans Serif",
reed@google.com17aa07d2012-02-23 14:51:10 +0000221 };
222
223 for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
bungeman@google.com3c996f82013-10-24 21:39:35 +0000224 SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(gNames[i], SkTypeface::kNormal));
reed@google.com17aa07d2012-02-23 14:51:10 +0000225 if (face) {
226#ifdef DUMP_TABLES
227 SkDebugf("%s\n", gNames[i]);
228#endif
229 test_tables(reporter, face);
reed@google.com4b2af9c2012-07-31 17:24:44 +0000230 test_unitsPerEm(reporter, face);
bungeman@google.com7bdd6142013-07-15 19:42:57 +0000231 test_countGlyphs(reporter, face);
bungeman@google.com72b8cb22013-10-25 17:49:08 +0000232 test_charsToGlyphs(reporter, face);
reed@google.com17aa07d2012-02-23 14:51:10 +0000233 }
234 }
235}
236
bungeman@google.com34f10262012-03-23 18:11:47 +0000237/*
238 * Verifies that the advance values returned by generateAdvance and
239 * generateMetrics match.
240 */
241static void test_advances(skiatest::Reporter* reporter) {
242 static const char* const faces[] = {
243 NULL, // default font
244 "Arial", "Times", "Times New Roman", "Helvetica", "Courier",
245 "Courier New", "Verdana", "monospace",
246 };
247
248 static const struct {
249 SkPaint::Hinting hinting;
250 unsigned flags;
251 } settings[] = {
252 { SkPaint::kNo_Hinting, 0 },
253 { SkPaint::kNo_Hinting, SkPaint::kLinearText_Flag },
254 { SkPaint::kNo_Hinting, SkPaint::kSubpixelText_Flag },
255 { SkPaint::kSlight_Hinting, 0 },
256 { SkPaint::kSlight_Hinting, SkPaint::kLinearText_Flag },
257 { SkPaint::kSlight_Hinting, SkPaint::kSubpixelText_Flag },
258 { SkPaint::kNormal_Hinting, 0 },
259 { SkPaint::kNormal_Hinting, SkPaint::kLinearText_Flag },
260 { SkPaint::kNormal_Hinting, SkPaint::kSubpixelText_Flag },
261 };
262
reed@google.comd074c372012-07-18 13:45:58 +0000263 static const struct {
264 SkScalar fScaleX;
265 SkScalar fSkewX;
266 } gScaleRec[] = {
267 { SK_Scalar1, 0 },
268 { SK_Scalar1/2, 0 },
269 // these two exercise obliquing (skew)
270 { SK_Scalar1, -SK_Scalar1/4 },
271 { SK_Scalar1/2, -SK_Scalar1/4 },
272 };
273
bungeman@google.com34f10262012-03-23 18:11:47 +0000274 SkPaint paint;
275 char txt[] = "long.text.with.lots.of.dots.";
276
277 for (size_t i = 0; i < SK_ARRAY_COUNT(faces); i++) {
bungeman@google.com94471032013-02-25 15:55:13 +0000278 SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(faces[i], SkTypeface::kNormal));
bungeman@google.com34f10262012-03-23 18:11:47 +0000279 paint.setTypeface(face);
280
281 for (size_t j = 0; j < SK_ARRAY_COUNT(settings); j++) {
reed@google.comd074c372012-07-18 13:45:58 +0000282 paint.setHinting(settings[j].hinting);
283 paint.setLinearText((settings[j].flags & SkPaint::kLinearText_Flag) != 0);
284 paint.setSubpixelText((settings[j].flags & SkPaint::kSubpixelText_Flag) != 0);
bungeman@google.com34f10262012-03-23 18:11:47 +0000285
reed@google.comd074c372012-07-18 13:45:58 +0000286 for (size_t k = 0; k < SK_ARRAY_COUNT(gScaleRec); ++k) {
287 paint.setTextScaleX(gScaleRec[k].fScaleX);
288 paint.setTextSkewX(gScaleRec[k].fSkewX);
bungeman@google.com34f10262012-03-23 18:11:47 +0000289
reed@google.comd074c372012-07-18 13:45:58 +0000290 SkRect bounds;
bungeman@google.com34f10262012-03-23 18:11:47 +0000291
reed@google.comd074c372012-07-18 13:45:58 +0000292 // For no hinting and light hinting this should take the
293 // optimized generateAdvance path.
294 SkScalar width1 = paint.measureText(txt, strlen(txt));
bungeman@google.com34f10262012-03-23 18:11:47 +0000295
reed@google.comd074c372012-07-18 13:45:58 +0000296 // Requesting the bounds forces a generateMetrics call.
297 SkScalar width2 = paint.measureText(txt, strlen(txt), &bounds);
bungeman@google.com34f10262012-03-23 18:11:47 +0000298
reed@google.comd074c372012-07-18 13:45:58 +0000299 // SkDebugf("Font: %s, generateAdvance: %f, generateMetrics: %f\n",
300 // faces[i], SkScalarToFloat(width1), SkScalarToFloat(width2));
301
302 REPORTER_ASSERT(reporter, width1 == width2);
303 }
bungeman@google.com34f10262012-03-23 18:11:47 +0000304 }
305 }
306}
307
reed@google.com17aa07d2012-02-23 14:51:10 +0000308static void TestFontHost(skiatest::Reporter* reporter) {
309 test_tables(reporter);
reed@google.comed268bf2013-03-11 20:13:36 +0000310 test_fontstream(reporter);
bungeman@google.com34f10262012-03-23 18:11:47 +0000311 test_advances(reporter);
reed@google.com17aa07d2012-02-23 14:51:10 +0000312}
313
314// need tests for SkStrSearch
315
316#include "TestClassDef.h"
317DEFINE_TESTCLASS("FontHost", FontHostTestClass, TestFontHost)