blob: 7358b1089a62b5047a7f2f48d596502ec59a8c57 [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"
11#include "SkStream.h"
reed@google.com17aa07d2012-02-23 14:51:10 +000012#include "SkTypeface.h"
reed@google.com4b2af9c2012-07-31 17:24:44 +000013#include "SkEndian.h"
reed@google.com17aa07d2012-02-23 14:51:10 +000014
15//#define DUMP_TABLES
reed@google.comed268bf2013-03-11 20:13:36 +000016//#define DUMP_TTC_TABLES
reed@google.com17aa07d2012-02-23 14:51:10 +000017
18#define kFontTableTag_head SkSetFourByteTag('h', 'e', 'a', 'd')
19#define kFontTableTag_hhea SkSetFourByteTag('h', 'h', 'e', 'a')
reed@google.com17aa07d2012-02-23 14:51:10 +000020#define kFontTableTag_maxp SkSetFourByteTag('m', 'a', 'x', 'p')
21
22static const struct TagSize {
23 SkFontTableTag fTag;
24 size_t fSize;
25} gKnownTableSizes[] = {
26 { kFontTableTag_head, 54 },
27 { kFontTableTag_hhea, 36 },
reed@google.com17aa07d2012-02-23 14:51:10 +000028 { kFontTableTag_maxp, 32 },
29};
30
reed@google.coma262eea2013-03-21 15:20:00 +000031// Test that getUnitsPerEm() agrees with a direct lookup in the 'head' table
bungeman@google.com7bdd6142013-07-15 19:42:57 +000032// (if that table is available).
reed@google.com4b2af9c2012-07-31 17:24:44 +000033static void test_unitsPerEm(skiatest::Reporter* reporter, SkTypeface* face) {
bungeman@google.com7bdd6142013-07-15 19:42:57 +000034 int nativeUPEM = face->getUnitsPerEm();
reed@google.com4b2af9c2012-07-31 17:24:44 +000035
reed@google.coma262eea2013-03-21 15:20:00 +000036 int tableUPEM = -1;
reed@google.com4b2af9c2012-07-31 17:24:44 +000037 size_t size = face->getTableSize(kFontTableTag_head);
38 if (size) {
reed@google.com4b2af9c2012-07-31 17:24:44 +000039 // unitsPerEm is at offset 18 into the 'head' table.
bungeman@google.com7bdd6142013-07-15 19:42:57 +000040 uint16_t rawUPEM;
41 face->getTableData(kFontTableTag_head, 18, sizeof(rawUPEM), &rawUPEM);
42 tableUPEM = SkEndian_SwapBE16(rawUPEM);
reed@google.coma262eea2013-03-21 15:20:00 +000043 }
reed@google.coma262eea2013-03-21 15:20:00 +000044
45 if (tableUPEM >= 0) {
46 REPORTER_ASSERT(reporter, tableUPEM == nativeUPEM);
47 } else {
48 // not sure this is a bug, but lets report it for now as info.
49 SkDebugf("--- typeface returned 0 upem [%X]\n", face->uniqueID());
reed@google.com4b2af9c2012-07-31 17:24:44 +000050 }
51}
52
bungeman@google.com7bdd6142013-07-15 19:42:57 +000053// Test that countGlyphs() agrees with a direct lookup in the 'maxp' table
54// (if that table is available).
55static void test_countGlyphs(skiatest::Reporter* reporter, SkTypeface* face) {
56 int nativeGlyphs = face->countGlyphs();
57
58 int tableGlyphs = -1;
59 size_t size = face->getTableSize(kFontTableTag_maxp);
60 if (size) {
61 // glyphs is at offset 4 into the 'maxp' table.
62 uint16_t rawGlyphs;
63 face->getTableData(kFontTableTag_maxp, 4, sizeof(rawGlyphs), &rawGlyphs);
64 tableGlyphs = SkEndian_SwapBE16(rawGlyphs);
65 }
66
67 if (tableGlyphs >= 0) {
68 REPORTER_ASSERT(reporter, tableGlyphs == nativeGlyphs);
69 } else {
70 // not sure this is a bug, but lets report it for now as info.
71 SkDebugf("--- typeface returned 0 glyphs [%X]\n", face->uniqueID());
72 }
73}
74
reed@google.comed268bf2013-03-11 20:13:36 +000075static void test_fontstream(skiatest::Reporter* reporter,
76 SkStream* stream, int ttcIndex) {
77 int n = SkFontStream::GetTableTags(stream, ttcIndex, NULL);
78 SkAutoTArray<SkFontTableTag> array(n);
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +000079
reed@google.comed268bf2013-03-11 20:13:36 +000080 int n2 = SkFontStream::GetTableTags(stream, ttcIndex, array.get());
81 REPORTER_ASSERT(reporter, n == n2);
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +000082
reed@google.comed268bf2013-03-11 20:13:36 +000083 for (int i = 0; i < n; ++i) {
84#ifdef DUMP_TTC_TABLES
85 SkString str;
86 SkFontTableTag t = array[i];
87 str.appendUnichar((t >> 24) & 0xFF);
88 str.appendUnichar((t >> 16) & 0xFF);
89 str.appendUnichar((t >> 8) & 0xFF);
90 str.appendUnichar((t >> 0) & 0xFF);
91 SkDebugf("[%d:%d] '%s'\n", ttcIndex, i, str.c_str());
92#endif
93 size_t size = SkFontStream::GetTableSize(stream, ttcIndex, array[i]);
94 for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) {
95 if (gKnownTableSizes[j].fTag == array[i]) {
96 REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size);
97 }
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +000098 }
reed@google.comed268bf2013-03-11 20:13:36 +000099 }
100}
101
102static void test_fontstream(skiatest::Reporter* reporter, SkStream* stream) {
103 int count = SkFontStream::CountTTCEntries(stream);
104#ifdef DUMP_TTC_TABLES
105 SkDebugf("CountTTCEntries %d\n", count);
106#endif
107 for (int i = 0; i < count; ++i) {
108 test_fontstream(reporter, stream, i);
109 }
110}
111
112static void test_fontstream(skiatest::Reporter* reporter) {
113 // TODO: replace when we get a tools/resources/fonts/test.ttc
114 const char* name = "/AmericanTypewriter.ttc";
115 SkFILEStream stream(name);
116 if (stream.isValid()) {
117 test_fontstream(reporter, &stream);
118 }
119}
120
reed@google.com17aa07d2012-02-23 14:51:10 +0000121static void test_tables(skiatest::Reporter* reporter, SkTypeface* face) {
caryclark@google.com42639cd2012-06-06 12:03:39 +0000122 if (false) { // avoid bit rot, suppress warning
robertphillips@google.com5b332112013-01-30 20:33:12 +0000123 SkFontID fontID = face->uniqueID();
caryclark@google.com42639cd2012-06-06 12:03:39 +0000124 REPORTER_ASSERT(reporter, fontID);
125 }
reed@google.com17aa07d2012-02-23 14:51:10 +0000126
reed@google.com8b0a3352012-04-19 18:52:39 +0000127 int count = face->countTables();
reed@google.com17aa07d2012-02-23 14:51:10 +0000128
129 SkAutoTMalloc<SkFontTableTag> storage(count);
130 SkFontTableTag* tags = storage.get();
reed@google.comfbd033d2012-02-23 16:15:58 +0000131
reed@google.com8b0a3352012-04-19 18:52:39 +0000132 int count2 = face->getTableTags(tags);
reed@google.comfbd033d2012-02-23 16:15:58 +0000133 REPORTER_ASSERT(reporter, count2 == count);
reed@google.com17aa07d2012-02-23 14:51:10 +0000134
135 for (int i = 0; i < count; ++i) {
reed@google.com8b0a3352012-04-19 18:52:39 +0000136 size_t size = face->getTableSize(tags[i]);
reed@google.com17aa07d2012-02-23 14:51:10 +0000137 REPORTER_ASSERT(reporter, size > 0);
138
139#ifdef DUMP_TABLES
140 char name[5];
141 name[0] = (tags[i] >> 24) & 0xFF;
142 name[1] = (tags[i] >> 16) & 0xFF;
143 name[2] = (tags[i] >> 8) & 0xFF;
144 name[3] = (tags[i] >> 0) & 0xFF;
145 name[4] = 0;
146 SkDebugf("%s %d\n", name, size);
147#endif
148
149 for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) {
150 if (gKnownTableSizes[j].fTag == tags[i]) {
151 REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size);
152 }
153 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000154
reed@google.comfbd033d2012-02-23 16:15:58 +0000155 // do we get the same size from GetTableData and GetTableSize
156 {
157 SkAutoMalloc data(size);
reed@google.com8b0a3352012-04-19 18:52:39 +0000158 size_t size2 = face->getTableData(tags[i], 0, size, data.get());
reed@google.comfbd033d2012-02-23 16:15:58 +0000159 REPORTER_ASSERT(reporter, size2 == size);
160 }
reed@google.com17aa07d2012-02-23 14:51:10 +0000161 }
162}
163
164static void test_tables(skiatest::Reporter* reporter) {
165 static const char* const gNames[] = {
166 NULL, // default font
167 "Arial", "Times", "Times New Roman", "Helvetica", "Courier",
bungeman@google.comc0d3f2f2012-07-31 21:39:05 +0000168 "Courier New", "Terminal", "MS Sans Serif",
reed@google.com17aa07d2012-02-23 14:51:10 +0000169 };
170
171 for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
172 SkTypeface* face = SkTypeface::CreateFromName(gNames[i],
173 SkTypeface::kNormal);
174 if (face) {
175#ifdef DUMP_TABLES
176 SkDebugf("%s\n", gNames[i]);
177#endif
178 test_tables(reporter, face);
reed@google.com4b2af9c2012-07-31 17:24:44 +0000179 test_unitsPerEm(reporter, face);
bungeman@google.com7bdd6142013-07-15 19:42:57 +0000180 test_countGlyphs(reporter, face);
reed@google.com17aa07d2012-02-23 14:51:10 +0000181 face->unref();
182 }
183 }
184}
185
bungeman@google.com34f10262012-03-23 18:11:47 +0000186/*
187 * Verifies that the advance values returned by generateAdvance and
188 * generateMetrics match.
189 */
190static void test_advances(skiatest::Reporter* reporter) {
191 static const char* const faces[] = {
192 NULL, // default font
193 "Arial", "Times", "Times New Roman", "Helvetica", "Courier",
194 "Courier New", "Verdana", "monospace",
195 };
196
197 static const struct {
198 SkPaint::Hinting hinting;
199 unsigned flags;
200 } settings[] = {
201 { SkPaint::kNo_Hinting, 0 },
202 { SkPaint::kNo_Hinting, SkPaint::kLinearText_Flag },
203 { SkPaint::kNo_Hinting, SkPaint::kSubpixelText_Flag },
204 { SkPaint::kSlight_Hinting, 0 },
205 { SkPaint::kSlight_Hinting, SkPaint::kLinearText_Flag },
206 { SkPaint::kSlight_Hinting, SkPaint::kSubpixelText_Flag },
207 { SkPaint::kNormal_Hinting, 0 },
208 { SkPaint::kNormal_Hinting, SkPaint::kLinearText_Flag },
209 { SkPaint::kNormal_Hinting, SkPaint::kSubpixelText_Flag },
210 };
211
reed@google.comd074c372012-07-18 13:45:58 +0000212 static const struct {
213 SkScalar fScaleX;
214 SkScalar fSkewX;
215 } gScaleRec[] = {
216 { SK_Scalar1, 0 },
217 { SK_Scalar1/2, 0 },
218 // these two exercise obliquing (skew)
219 { SK_Scalar1, -SK_Scalar1/4 },
220 { SK_Scalar1/2, -SK_Scalar1/4 },
221 };
222
bungeman@google.com34f10262012-03-23 18:11:47 +0000223 SkPaint paint;
224 char txt[] = "long.text.with.lots.of.dots.";
225
226 for (size_t i = 0; i < SK_ARRAY_COUNT(faces); i++) {
bungeman@google.com94471032013-02-25 15:55:13 +0000227 SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(faces[i], SkTypeface::kNormal));
bungeman@google.com34f10262012-03-23 18:11:47 +0000228 paint.setTypeface(face);
229
230 for (size_t j = 0; j < SK_ARRAY_COUNT(settings); j++) {
reed@google.comd074c372012-07-18 13:45:58 +0000231 paint.setHinting(settings[j].hinting);
232 paint.setLinearText((settings[j].flags & SkPaint::kLinearText_Flag) != 0);
233 paint.setSubpixelText((settings[j].flags & SkPaint::kSubpixelText_Flag) != 0);
bungeman@google.com34f10262012-03-23 18:11:47 +0000234
reed@google.comd074c372012-07-18 13:45:58 +0000235 for (size_t k = 0; k < SK_ARRAY_COUNT(gScaleRec); ++k) {
236 paint.setTextScaleX(gScaleRec[k].fScaleX);
237 paint.setTextSkewX(gScaleRec[k].fSkewX);
bungeman@google.com34f10262012-03-23 18:11:47 +0000238
reed@google.comd074c372012-07-18 13:45:58 +0000239 SkRect bounds;
bungeman@google.com34f10262012-03-23 18:11:47 +0000240
reed@google.comd074c372012-07-18 13:45:58 +0000241 // For no hinting and light hinting this should take the
242 // optimized generateAdvance path.
243 SkScalar width1 = paint.measureText(txt, strlen(txt));
bungeman@google.com34f10262012-03-23 18:11:47 +0000244
reed@google.comd074c372012-07-18 13:45:58 +0000245 // Requesting the bounds forces a generateMetrics call.
246 SkScalar width2 = paint.measureText(txt, strlen(txt), &bounds);
bungeman@google.com34f10262012-03-23 18:11:47 +0000247
reed@google.comd074c372012-07-18 13:45:58 +0000248 // SkDebugf("Font: %s, generateAdvance: %f, generateMetrics: %f\n",
249 // faces[i], SkScalarToFloat(width1), SkScalarToFloat(width2));
250
251 REPORTER_ASSERT(reporter, width1 == width2);
252 }
bungeman@google.com34f10262012-03-23 18:11:47 +0000253 }
254 }
255}
256
reed@google.com17aa07d2012-02-23 14:51:10 +0000257static void TestFontHost(skiatest::Reporter* reporter) {
258 test_tables(reporter);
reed@google.comed268bf2013-03-11 20:13:36 +0000259 test_fontstream(reporter);
bungeman@google.com34f10262012-03-23 18:11:47 +0000260 test_advances(reporter);
reed@google.com17aa07d2012-02-23 14:51:10 +0000261}
262
263// need tests for SkStrSearch
264
265#include "TestClassDef.h"
266DEFINE_TESTCLASS("FontHost", FontHostTestClass, TestFontHost)