blob: e9961551e10c4221db3e844b30434f199dbb8869 [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.com4b2af9c2012-07-31 17:24:44 +000031static void test_unitsPerEm(skiatest::Reporter* reporter, SkTypeface* face) {
32 int upem = face->getUnitsPerEm();
bungeman@google.comc0d3f2f2012-07-31 21:39:05 +000033 if (0 == upem) return;
reed@google.com4b2af9c2012-07-31 17:24:44 +000034
35 size_t size = face->getTableSize(kFontTableTag_head);
36 if (size) {
37 SkAutoMalloc storage(size);
38 char* ptr = (char*)storage.get();
39 face->getTableData(kFontTableTag_head, 0, size, ptr);
40 // unitsPerEm is at offset 18 into the 'head' table.
41 int upem2 = SkEndian_SwapBE16(*(uint16_t*)&ptr[18]);
42 REPORTER_ASSERT(reporter, upem2 == upem);
43 }
44}
45
reed@google.comed268bf2013-03-11 20:13:36 +000046static void test_fontstream(skiatest::Reporter* reporter,
47 SkStream* stream, int ttcIndex) {
48 int n = SkFontStream::GetTableTags(stream, ttcIndex, NULL);
49 SkAutoTArray<SkFontTableTag> array(n);
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +000050
reed@google.comed268bf2013-03-11 20:13:36 +000051 int n2 = SkFontStream::GetTableTags(stream, ttcIndex, array.get());
52 REPORTER_ASSERT(reporter, n == n2);
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +000053
reed@google.comed268bf2013-03-11 20:13:36 +000054 for (int i = 0; i < n; ++i) {
55#ifdef DUMP_TTC_TABLES
56 SkString str;
57 SkFontTableTag t = array[i];
58 str.appendUnichar((t >> 24) & 0xFF);
59 str.appendUnichar((t >> 16) & 0xFF);
60 str.appendUnichar((t >> 8) & 0xFF);
61 str.appendUnichar((t >> 0) & 0xFF);
62 SkDebugf("[%d:%d] '%s'\n", ttcIndex, i, str.c_str());
63#endif
64 size_t size = SkFontStream::GetTableSize(stream, ttcIndex, array[i]);
65 for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) {
66 if (gKnownTableSizes[j].fTag == array[i]) {
67 REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size);
68 }
skia.committer@gmail.com2e71f162013-03-12 07:12:32 +000069 }
reed@google.comed268bf2013-03-11 20:13:36 +000070 }
71}
72
73static void test_fontstream(skiatest::Reporter* reporter, SkStream* stream) {
74 int count = SkFontStream::CountTTCEntries(stream);
75#ifdef DUMP_TTC_TABLES
76 SkDebugf("CountTTCEntries %d\n", count);
77#endif
78 for (int i = 0; i < count; ++i) {
79 test_fontstream(reporter, stream, i);
80 }
81}
82
83static void test_fontstream(skiatest::Reporter* reporter) {
84 // TODO: replace when we get a tools/resources/fonts/test.ttc
85 const char* name = "/AmericanTypewriter.ttc";
86 SkFILEStream stream(name);
87 if (stream.isValid()) {
88 test_fontstream(reporter, &stream);
89 }
90}
91
reed@google.com17aa07d2012-02-23 14:51:10 +000092static void test_tables(skiatest::Reporter* reporter, SkTypeface* face) {
caryclark@google.com42639cd2012-06-06 12:03:39 +000093 if (false) { // avoid bit rot, suppress warning
robertphillips@google.com5b332112013-01-30 20:33:12 +000094 SkFontID fontID = face->uniqueID();
caryclark@google.com42639cd2012-06-06 12:03:39 +000095 REPORTER_ASSERT(reporter, fontID);
96 }
reed@google.com17aa07d2012-02-23 14:51:10 +000097
reed@google.com8b0a3352012-04-19 18:52:39 +000098 int count = face->countTables();
reed@google.com17aa07d2012-02-23 14:51:10 +000099
100 SkAutoTMalloc<SkFontTableTag> storage(count);
101 SkFontTableTag* tags = storage.get();
reed@google.comfbd033d2012-02-23 16:15:58 +0000102
reed@google.com8b0a3352012-04-19 18:52:39 +0000103 int count2 = face->getTableTags(tags);
reed@google.comfbd033d2012-02-23 16:15:58 +0000104 REPORTER_ASSERT(reporter, count2 == count);
reed@google.com17aa07d2012-02-23 14:51:10 +0000105
106 for (int i = 0; i < count; ++i) {
reed@google.com8b0a3352012-04-19 18:52:39 +0000107 size_t size = face->getTableSize(tags[i]);
reed@google.com17aa07d2012-02-23 14:51:10 +0000108 REPORTER_ASSERT(reporter, size > 0);
109
110#ifdef DUMP_TABLES
111 char name[5];
112 name[0] = (tags[i] >> 24) & 0xFF;
113 name[1] = (tags[i] >> 16) & 0xFF;
114 name[2] = (tags[i] >> 8) & 0xFF;
115 name[3] = (tags[i] >> 0) & 0xFF;
116 name[4] = 0;
117 SkDebugf("%s %d\n", name, size);
118#endif
119
120 for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) {
121 if (gKnownTableSizes[j].fTag == tags[i]) {
122 REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size);
123 }
124 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000125
reed@google.comfbd033d2012-02-23 16:15:58 +0000126 // do we get the same size from GetTableData and GetTableSize
127 {
128 SkAutoMalloc data(size);
reed@google.com8b0a3352012-04-19 18:52:39 +0000129 size_t size2 = face->getTableData(tags[i], 0, size, data.get());
reed@google.comfbd033d2012-02-23 16:15:58 +0000130 REPORTER_ASSERT(reporter, size2 == size);
131 }
reed@google.com17aa07d2012-02-23 14:51:10 +0000132 }
133}
134
135static void test_tables(skiatest::Reporter* reporter) {
136 static const char* const gNames[] = {
137 NULL, // default font
138 "Arial", "Times", "Times New Roman", "Helvetica", "Courier",
bungeman@google.comc0d3f2f2012-07-31 21:39:05 +0000139 "Courier New", "Terminal", "MS Sans Serif",
reed@google.com17aa07d2012-02-23 14:51:10 +0000140 };
141
142 for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
143 SkTypeface* face = SkTypeface::CreateFromName(gNames[i],
144 SkTypeface::kNormal);
145 if (face) {
146#ifdef DUMP_TABLES
147 SkDebugf("%s\n", gNames[i]);
148#endif
149 test_tables(reporter, face);
reed@google.com4b2af9c2012-07-31 17:24:44 +0000150 test_unitsPerEm(reporter, face);
reed@google.com17aa07d2012-02-23 14:51:10 +0000151 face->unref();
152 }
153 }
154}
155
bungeman@google.com34f10262012-03-23 18:11:47 +0000156/*
157 * Verifies that the advance values returned by generateAdvance and
158 * generateMetrics match.
159 */
160static void test_advances(skiatest::Reporter* reporter) {
161 static const char* const faces[] = {
162 NULL, // default font
163 "Arial", "Times", "Times New Roman", "Helvetica", "Courier",
164 "Courier New", "Verdana", "monospace",
165 };
166
167 static const struct {
168 SkPaint::Hinting hinting;
169 unsigned flags;
170 } settings[] = {
171 { SkPaint::kNo_Hinting, 0 },
172 { SkPaint::kNo_Hinting, SkPaint::kLinearText_Flag },
173 { SkPaint::kNo_Hinting, SkPaint::kSubpixelText_Flag },
174 { SkPaint::kSlight_Hinting, 0 },
175 { SkPaint::kSlight_Hinting, SkPaint::kLinearText_Flag },
176 { SkPaint::kSlight_Hinting, SkPaint::kSubpixelText_Flag },
177 { SkPaint::kNormal_Hinting, 0 },
178 { SkPaint::kNormal_Hinting, SkPaint::kLinearText_Flag },
179 { SkPaint::kNormal_Hinting, SkPaint::kSubpixelText_Flag },
180 };
181
reed@google.comd074c372012-07-18 13:45:58 +0000182 static const struct {
183 SkScalar fScaleX;
184 SkScalar fSkewX;
185 } gScaleRec[] = {
186 { SK_Scalar1, 0 },
187 { SK_Scalar1/2, 0 },
188 // these two exercise obliquing (skew)
189 { SK_Scalar1, -SK_Scalar1/4 },
190 { SK_Scalar1/2, -SK_Scalar1/4 },
191 };
192
bungeman@google.com34f10262012-03-23 18:11:47 +0000193 SkPaint paint;
194 char txt[] = "long.text.with.lots.of.dots.";
195
196 for (size_t i = 0; i < SK_ARRAY_COUNT(faces); i++) {
bungeman@google.com94471032013-02-25 15:55:13 +0000197 SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(faces[i], SkTypeface::kNormal));
bungeman@google.com34f10262012-03-23 18:11:47 +0000198 paint.setTypeface(face);
199
200 for (size_t j = 0; j < SK_ARRAY_COUNT(settings); j++) {
reed@google.comd074c372012-07-18 13:45:58 +0000201 paint.setHinting(settings[j].hinting);
202 paint.setLinearText((settings[j].flags & SkPaint::kLinearText_Flag) != 0);
203 paint.setSubpixelText((settings[j].flags & SkPaint::kSubpixelText_Flag) != 0);
bungeman@google.com34f10262012-03-23 18:11:47 +0000204
reed@google.comd074c372012-07-18 13:45:58 +0000205 for (size_t k = 0; k < SK_ARRAY_COUNT(gScaleRec); ++k) {
206 paint.setTextScaleX(gScaleRec[k].fScaleX);
207 paint.setTextSkewX(gScaleRec[k].fSkewX);
bungeman@google.com34f10262012-03-23 18:11:47 +0000208
reed@google.comd074c372012-07-18 13:45:58 +0000209 SkRect bounds;
bungeman@google.com34f10262012-03-23 18:11:47 +0000210
reed@google.comd074c372012-07-18 13:45:58 +0000211 // For no hinting and light hinting this should take the
212 // optimized generateAdvance path.
213 SkScalar width1 = paint.measureText(txt, strlen(txt));
bungeman@google.com34f10262012-03-23 18:11:47 +0000214
reed@google.comd074c372012-07-18 13:45:58 +0000215 // Requesting the bounds forces a generateMetrics call.
216 SkScalar width2 = paint.measureText(txt, strlen(txt), &bounds);
bungeman@google.com34f10262012-03-23 18:11:47 +0000217
reed@google.comd074c372012-07-18 13:45:58 +0000218 // SkDebugf("Font: %s, generateAdvance: %f, generateMetrics: %f\n",
219 // faces[i], SkScalarToFloat(width1), SkScalarToFloat(width2));
220
221 REPORTER_ASSERT(reporter, width1 == width2);
222 }
bungeman@google.com34f10262012-03-23 18:11:47 +0000223 }
224 }
225}
226
reed@google.com17aa07d2012-02-23 14:51:10 +0000227static void TestFontHost(skiatest::Reporter* reporter) {
228 test_tables(reporter);
reed@google.comed268bf2013-03-11 20:13:36 +0000229 test_fontstream(reporter);
bungeman@google.com34f10262012-03-23 18:11:47 +0000230 test_advances(reporter);
reed@google.com17aa07d2012-02-23 14:51:10 +0000231}
232
233// need tests for SkStrSearch
234
235#include "TestClassDef.h"
236DEFINE_TESTCLASS("FontHost", FontHostTestClass, TestFontHost)