reed@google.com | 17aa07d | 2012-02-23 14:51:10 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkFont.h" |
| 9 | #include "include/core/SkPaint.h" |
| 10 | #include "include/core/SkStream.h" |
| 11 | #include "include/core/SkTypeface.h" |
| 12 | #include "src/core/SkAutoMalloc.h" |
| 13 | #include "src/core/SkEndian.h" |
| 14 | #include "src/core/SkFontStream.h" |
| 15 | #include "src/core/SkOSFile.h" |
| 16 | #include "tests/Test.h" |
| 17 | #include "tools/Resources.h" |
reed@google.com | 17aa07d | 2012-02-23 14:51:10 +0000 | [diff] [blame] | 18 | |
| 19 | //#define DUMP_TABLES |
reed@google.com | ed268bf | 2013-03-11 20:13:36 +0000 | [diff] [blame] | 20 | //#define DUMP_TTC_TABLES |
reed@google.com | 17aa07d | 2012-02-23 14:51:10 +0000 | [diff] [blame] | 21 | |
| 22 | #define kFontTableTag_head SkSetFourByteTag('h', 'e', 'a', 'd') |
| 23 | #define kFontTableTag_hhea SkSetFourByteTag('h', 'h', 'e', 'a') |
reed@google.com | 17aa07d | 2012-02-23 14:51:10 +0000 | [diff] [blame] | 24 | #define kFontTableTag_maxp SkSetFourByteTag('m', 'a', 'x', 'p') |
| 25 | |
| 26 | static const struct TagSize { |
| 27 | SkFontTableTag fTag; |
| 28 | size_t fSize; |
| 29 | } gKnownTableSizes[] = { |
| 30 | { kFontTableTag_head, 54 }, |
| 31 | { kFontTableTag_hhea, 36 }, |
reed@google.com | 17aa07d | 2012-02-23 14:51:10 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
reed@google.com | a262eea | 2013-03-21 15:20:00 +0000 | [diff] [blame] | 34 | // Test that getUnitsPerEm() agrees with a direct lookup in the 'head' table |
bungeman@google.com | 7bdd614 | 2013-07-15 19:42:57 +0000 | [diff] [blame] | 35 | // (if that table is available). |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 36 | static void test_unitsPerEm(skiatest::Reporter* reporter, const sk_sp<SkTypeface>& face) { |
bungeman@google.com | 7bdd614 | 2013-07-15 19:42:57 +0000 | [diff] [blame] | 37 | int nativeUPEM = face->getUnitsPerEm(); |
reed@google.com | 4b2af9c | 2012-07-31 17:24:44 +0000 | [diff] [blame] | 38 | |
reed@google.com | a262eea | 2013-03-21 15:20:00 +0000 | [diff] [blame] | 39 | int tableUPEM = -1; |
reed@google.com | 4b2af9c | 2012-07-31 17:24:44 +0000 | [diff] [blame] | 40 | size_t size = face->getTableSize(kFontTableTag_head); |
| 41 | if (size) { |
reed@google.com | 4b2af9c | 2012-07-31 17:24:44 +0000 | [diff] [blame] | 42 | // unitsPerEm is at offset 18 into the 'head' table. |
bungeman@google.com | 7bdd614 | 2013-07-15 19:42:57 +0000 | [diff] [blame] | 43 | uint16_t rawUPEM; |
| 44 | face->getTableData(kFontTableTag_head, 18, sizeof(rawUPEM), &rawUPEM); |
| 45 | tableUPEM = SkEndian_SwapBE16(rawUPEM); |
reed@google.com | a262eea | 2013-03-21 15:20:00 +0000 | [diff] [blame] | 46 | } |
reed@google.com | a262eea | 2013-03-21 15:20:00 +0000 | [diff] [blame] | 47 | |
| 48 | if (tableUPEM >= 0) { |
| 49 | REPORTER_ASSERT(reporter, tableUPEM == nativeUPEM); |
reed@google.com | 4b2af9c | 2012-07-31 17:24:44 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
bungeman@google.com | 7bdd614 | 2013-07-15 19:42:57 +0000 | [diff] [blame] | 53 | // Test that countGlyphs() agrees with a direct lookup in the 'maxp' table |
| 54 | // (if that table is available). |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 55 | static void test_countGlyphs(skiatest::Reporter* reporter, const sk_sp<SkTypeface>& face) { |
bungeman@google.com | 7bdd614 | 2013-07-15 19:42:57 +0000 | [diff] [blame] | 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); |
bungeman@google.com | 7bdd614 | 2013-07-15 19:42:57 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
bungeman@google.com | 72b8cb2 | 2013-10-25 17:49:08 +0000 | [diff] [blame] | 71 | |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 72 | static void test_fontstream(skiatest::Reporter* reporter, SkStream* stream, int ttcIndex) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 73 | int n = SkFontStream::GetTableTags(stream, ttcIndex, nullptr); |
reed@google.com | ed268bf | 2013-03-11 20:13:36 +0000 | [diff] [blame] | 74 | SkAutoTArray<SkFontTableTag> array(n); |
skia.committer@gmail.com | 2e71f16 | 2013-03-12 07:12:32 +0000 | [diff] [blame] | 75 | |
reed@google.com | ed268bf | 2013-03-11 20:13:36 +0000 | [diff] [blame] | 76 | int n2 = SkFontStream::GetTableTags(stream, ttcIndex, array.get()); |
| 77 | REPORTER_ASSERT(reporter, n == n2); |
skia.committer@gmail.com | 2e71f16 | 2013-03-12 07:12:32 +0000 | [diff] [blame] | 78 | |
reed@google.com | ed268bf | 2013-03-11 20:13:36 +0000 | [diff] [blame] | 79 | for (int i = 0; i < n; ++i) { |
| 80 | #ifdef DUMP_TTC_TABLES |
| 81 | SkString str; |
| 82 | SkFontTableTag t = array[i]; |
| 83 | str.appendUnichar((t >> 24) & 0xFF); |
| 84 | str.appendUnichar((t >> 16) & 0xFF); |
| 85 | str.appendUnichar((t >> 8) & 0xFF); |
| 86 | str.appendUnichar((t >> 0) & 0xFF); |
| 87 | SkDebugf("[%d:%d] '%s'\n", ttcIndex, i, str.c_str()); |
| 88 | #endif |
| 89 | size_t size = SkFontStream::GetTableSize(stream, ttcIndex, array[i]); |
| 90 | for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) { |
| 91 | if (gKnownTableSizes[j].fTag == array[i]) { |
| 92 | REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size); |
| 93 | } |
skia.committer@gmail.com | 2e71f16 | 2013-03-12 07:12:32 +0000 | [diff] [blame] | 94 | } |
reed@google.com | ed268bf | 2013-03-11 20:13:36 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 98 | static void test_fontstream(skiatest::Reporter* reporter) { |
Hal Canary | 53e5e7d | 2017-12-08 14:25:14 -0500 | [diff] [blame] | 99 | std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream("fonts/test.ttc")); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 100 | if (!stream) { |
| 101 | SkDebugf("Skipping FontHostTest::test_fontstream\n"); |
| 102 | return; |
| 103 | } |
| 104 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 105 | int count = SkFontStream::CountTTCEntries(stream.get()); |
reed@google.com | ed268bf | 2013-03-11 20:13:36 +0000 | [diff] [blame] | 106 | #ifdef DUMP_TTC_TABLES |
| 107 | SkDebugf("CountTTCEntries %d\n", count); |
| 108 | #endif |
| 109 | for (int i = 0; i < count; ++i) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 110 | test_fontstream(reporter, stream.get(), i); |
reed@google.com | ed268bf | 2013-03-11 20:13:36 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Mike Reed | f9e824b | 2018-12-13 10:22:39 -0500 | [diff] [blame] | 114 | // Exercise this rare cmap format (platform 3, encoding 0) |
bungeman | 726cf90 | 2015-06-05 13:38:12 -0700 | [diff] [blame] | 115 | static void test_symbolfont(skiatest::Reporter* reporter) { |
Mike Reed | f9e824b | 2018-12-13 10:22:39 -0500 | [diff] [blame] | 116 | auto tf = MakeResourceAsTypeface("fonts/SpiderSymbol.ttf"); |
| 117 | if (tf) { |
| 118 | SkUnichar c = 0xf021; |
| 119 | uint16_t g = SkFont(tf).unicharToGlyph(c); |
| 120 | REPORTER_ASSERT(reporter, g == 3); |
| 121 | } else { |
| 122 | // not all platforms support data fonts, so we just note that failure |
bungeman | 726cf90 | 2015-06-05 13:38:12 -0700 | [diff] [blame] | 123 | SkDebugf("Skipping FontHostTest::test_symbolfont\n"); |
bungeman | 726cf90 | 2015-06-05 13:38:12 -0700 | [diff] [blame] | 124 | } |
bungeman | 726cf90 | 2015-06-05 13:38:12 -0700 | [diff] [blame] | 125 | } |
| 126 | |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 127 | static void test_tables(skiatest::Reporter* reporter, const sk_sp<SkTypeface>& face) { |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 128 | if (false) { // avoid bit rot, suppress warning |
robertphillips@google.com | 5b33211 | 2013-01-30 20:33:12 +0000 | [diff] [blame] | 129 | SkFontID fontID = face->uniqueID(); |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 130 | REPORTER_ASSERT(reporter, fontID); |
| 131 | } |
reed@google.com | 17aa07d | 2012-02-23 14:51:10 +0000 | [diff] [blame] | 132 | |
reed@google.com | 8b0a335 | 2012-04-19 18:52:39 +0000 | [diff] [blame] | 133 | int count = face->countTables(); |
reed@google.com | 17aa07d | 2012-02-23 14:51:10 +0000 | [diff] [blame] | 134 | |
| 135 | SkAutoTMalloc<SkFontTableTag> storage(count); |
| 136 | SkFontTableTag* tags = storage.get(); |
reed@google.com | fbd033d | 2012-02-23 16:15:58 +0000 | [diff] [blame] | 137 | |
reed@google.com | 8b0a335 | 2012-04-19 18:52:39 +0000 | [diff] [blame] | 138 | int count2 = face->getTableTags(tags); |
reed@google.com | fbd033d | 2012-02-23 16:15:58 +0000 | [diff] [blame] | 139 | REPORTER_ASSERT(reporter, count2 == count); |
reed@google.com | 17aa07d | 2012-02-23 14:51:10 +0000 | [diff] [blame] | 140 | |
| 141 | for (int i = 0; i < count; ++i) { |
reed@google.com | 8b0a335 | 2012-04-19 18:52:39 +0000 | [diff] [blame] | 142 | size_t size = face->getTableSize(tags[i]); |
reed@google.com | 17aa07d | 2012-02-23 14:51:10 +0000 | [diff] [blame] | 143 | REPORTER_ASSERT(reporter, size > 0); |
| 144 | |
| 145 | #ifdef DUMP_TABLES |
| 146 | char name[5]; |
| 147 | name[0] = (tags[i] >> 24) & 0xFF; |
| 148 | name[1] = (tags[i] >> 16) & 0xFF; |
| 149 | name[2] = (tags[i] >> 8) & 0xFF; |
| 150 | name[3] = (tags[i] >> 0) & 0xFF; |
| 151 | name[4] = 0; |
| 152 | SkDebugf("%s %d\n", name, size); |
| 153 | #endif |
| 154 | |
| 155 | for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) { |
| 156 | if (gKnownTableSizes[j].fTag == tags[i]) { |
| 157 | REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size); |
| 158 | } |
| 159 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 160 | |
reed@google.com | fbd033d | 2012-02-23 16:15:58 +0000 | [diff] [blame] | 161 | // do we get the same size from GetTableData and GetTableSize |
| 162 | { |
| 163 | SkAutoMalloc data(size); |
reed@google.com | 8b0a335 | 2012-04-19 18:52:39 +0000 | [diff] [blame] | 164 | size_t size2 = face->getTableData(tags[i], 0, size, data.get()); |
reed@google.com | fbd033d | 2012-02-23 16:15:58 +0000 | [diff] [blame] | 165 | REPORTER_ASSERT(reporter, size2 == size); |
| 166 | } |
reed@google.com | 17aa07d | 2012-02-23 14:51:10 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | static void test_tables(skiatest::Reporter* reporter) { |
| 171 | static const char* const gNames[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 172 | nullptr, // default font |
bungeman | 7b09aab | 2014-09-24 11:04:41 -0700 | [diff] [blame] | 173 | "Helvetica", "Arial", |
| 174 | "Times", "Times New Roman", |
| 175 | "Courier", "Courier New", |
| 176 | "Terminal", "MS Sans Serif", |
| 177 | "Hiragino Mincho ProN", "MS PGothic", |
reed@google.com | 17aa07d | 2012-02-23 14:51:10 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) { |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 181 | sk_sp<SkTypeface> face(SkTypeface::MakeFromName(gNames[i], SkFontStyle())); |
reed@google.com | 17aa07d | 2012-02-23 14:51:10 +0000 | [diff] [blame] | 182 | if (face) { |
| 183 | #ifdef DUMP_TABLES |
| 184 | SkDebugf("%s\n", gNames[i]); |
| 185 | #endif |
| 186 | test_tables(reporter, face); |
reed@google.com | 4b2af9c | 2012-07-31 17:24:44 +0000 | [diff] [blame] | 187 | test_unitsPerEm(reporter, face); |
bungeman@google.com | 7bdd614 | 2013-07-15 19:42:57 +0000 | [diff] [blame] | 188 | test_countGlyphs(reporter, face); |
reed@google.com | 17aa07d | 2012-02-23 14:51:10 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
bungeman@google.com | 34f1026 | 2012-03-23 18:11:47 +0000 | [diff] [blame] | 193 | /* |
| 194 | * Verifies that the advance values returned by generateAdvance and |
| 195 | * generateMetrics match. |
| 196 | */ |
| 197 | static void test_advances(skiatest::Reporter* reporter) { |
| 198 | static const char* const faces[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 199 | nullptr, // default font |
bungeman@google.com | 34f1026 | 2012-03-23 18:11:47 +0000 | [diff] [blame] | 200 | "Arial", "Times", "Times New Roman", "Helvetica", "Courier", |
| 201 | "Courier New", "Verdana", "monospace", |
| 202 | }; |
| 203 | |
| 204 | static const struct { |
Mike Reed | 9edbf42 | 2018-11-07 19:54:33 -0500 | [diff] [blame] | 205 | SkFontHinting hinting; |
Mike Reed | e5f9cfa | 2019-01-10 13:55:35 -0500 | [diff] [blame] | 206 | bool linear; |
| 207 | bool subpixel; |
bungeman@google.com | 34f1026 | 2012-03-23 18:11:47 +0000 | [diff] [blame] | 208 | } settings[] = { |
Ben Wagner | 5785e4a | 2019-05-07 16:50:29 -0400 | [diff] [blame] | 209 | { SkFontHinting::kNone, false, false }, |
| 210 | { SkFontHinting::kNone, true, false }, |
| 211 | { SkFontHinting::kNone, false, true }, |
| 212 | { SkFontHinting::kSlight, false, false }, |
| 213 | { SkFontHinting::kSlight, true, false }, |
| 214 | { SkFontHinting::kSlight, false, true }, |
| 215 | { SkFontHinting::kNormal, false, false }, |
| 216 | { SkFontHinting::kNormal, true, false }, |
| 217 | { SkFontHinting::kNormal, false, true }, |
bungeman@google.com | 34f1026 | 2012-03-23 18:11:47 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
reed@google.com | d074c37 | 2012-07-18 13:45:58 +0000 | [diff] [blame] | 220 | static const struct { |
| 221 | SkScalar fScaleX; |
| 222 | SkScalar fSkewX; |
| 223 | } gScaleRec[] = { |
| 224 | { SK_Scalar1, 0 }, |
| 225 | { SK_Scalar1/2, 0 }, |
| 226 | // these two exercise obliquing (skew) |
| 227 | { SK_Scalar1, -SK_Scalar1/4 }, |
| 228 | { SK_Scalar1/2, -SK_Scalar1/4 }, |
| 229 | }; |
| 230 | |
Mike Reed | f9e824b | 2018-12-13 10:22:39 -0500 | [diff] [blame] | 231 | SkFont font; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 232 | char const * const txt = "long.text.with.lots.of.dots."; |
| 233 | size_t textLen = strlen(txt); |
bungeman@google.com | 34f1026 | 2012-03-23 18:11:47 +0000 | [diff] [blame] | 234 | |
| 235 | for (size_t i = 0; i < SK_ARRAY_COUNT(faces); i++) { |
Mike Reed | f9e824b | 2018-12-13 10:22:39 -0500 | [diff] [blame] | 236 | font.setTypeface(SkTypeface::MakeFromName(faces[i], SkFontStyle())); |
bungeman@google.com | 34f1026 | 2012-03-23 18:11:47 +0000 | [diff] [blame] | 237 | |
| 238 | for (size_t j = 0; j < SK_ARRAY_COUNT(settings); j++) { |
Mike Reed | f9e824b | 2018-12-13 10:22:39 -0500 | [diff] [blame] | 239 | font.setHinting(settings[j].hinting); |
Mike Reed | e5f9cfa | 2019-01-10 13:55:35 -0500 | [diff] [blame] | 240 | font.setLinearMetrics(settings[j].linear); |
| 241 | font.setSubpixel(settings[j].subpixel); |
bungeman@google.com | 34f1026 | 2012-03-23 18:11:47 +0000 | [diff] [blame] | 242 | |
reed@google.com | d074c37 | 2012-07-18 13:45:58 +0000 | [diff] [blame] | 243 | for (size_t k = 0; k < SK_ARRAY_COUNT(gScaleRec); ++k) { |
Mike Reed | f9e824b | 2018-12-13 10:22:39 -0500 | [diff] [blame] | 244 | font.setScaleX(gScaleRec[k].fScaleX); |
| 245 | font.setSkewX(gScaleRec[k].fSkewX); |
bungeman@google.com | 34f1026 | 2012-03-23 18:11:47 +0000 | [diff] [blame] | 246 | |
reed@google.com | d074c37 | 2012-07-18 13:45:58 +0000 | [diff] [blame] | 247 | SkRect bounds; |
bungeman@google.com | 34f1026 | 2012-03-23 18:11:47 +0000 | [diff] [blame] | 248 | |
reed@google.com | d074c37 | 2012-07-18 13:45:58 +0000 | [diff] [blame] | 249 | // For no hinting and light hinting this should take the |
| 250 | // optimized generateAdvance path. |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 251 | SkScalar width1 = font.measureText(txt, textLen, SkTextEncoding::kUTF8); |
bungeman@google.com | 34f1026 | 2012-03-23 18:11:47 +0000 | [diff] [blame] | 252 | |
reed@google.com | d074c37 | 2012-07-18 13:45:58 +0000 | [diff] [blame] | 253 | // Requesting the bounds forces a generateMetrics call. |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 254 | SkScalar width2 = font.measureText(txt, textLen, SkTextEncoding::kUTF8, &bounds); |
bungeman@google.com | 34f1026 | 2012-03-23 18:11:47 +0000 | [diff] [blame] | 255 | |
reed@google.com | d074c37 | 2012-07-18 13:45:58 +0000 | [diff] [blame] | 256 | // SkDebugf("Font: %s, generateAdvance: %f, generateMetrics: %f\n", |
| 257 | // faces[i], SkScalarToFloat(width1), SkScalarToFloat(width2)); |
| 258 | |
| 259 | REPORTER_ASSERT(reporter, width1 == width2); |
| 260 | } |
bungeman@google.com | 34f1026 | 2012-03-23 18:11:47 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 265 | DEF_TEST(FontHost, reporter) { |
reed@google.com | 17aa07d | 2012-02-23 14:51:10 +0000 | [diff] [blame] | 266 | test_tables(reporter); |
reed@google.com | ed268bf | 2013-03-11 20:13:36 +0000 | [diff] [blame] | 267 | test_fontstream(reporter); |
bungeman@google.com | 34f1026 | 2012-03-23 18:11:47 +0000 | [diff] [blame] | 268 | test_advances(reporter); |
bungeman | 726cf90 | 2015-06-05 13:38:12 -0700 | [diff] [blame] | 269 | test_symbolfont(reporter); |
reed@google.com | 17aa07d | 2012-02-23 14:51:10 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // need tests for SkStrSearch |