FontHostTest.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. #include "include/core/SkFont.h"
  8. #include "include/core/SkPaint.h"
  9. #include "include/core/SkStream.h"
  10. #include "include/core/SkTypeface.h"
  11. #include "src/core/SkAutoMalloc.h"
  12. #include "src/core/SkEndian.h"
  13. #include "src/core/SkFontStream.h"
  14. #include "src/core/SkOSFile.h"
  15. #include "tests/Test.h"
  16. #include "tools/Resources.h"
  17. //#define DUMP_TABLES
  18. //#define DUMP_TTC_TABLES
  19. #define kFontTableTag_head SkSetFourByteTag('h', 'e', 'a', 'd')
  20. #define kFontTableTag_hhea SkSetFourByteTag('h', 'h', 'e', 'a')
  21. #define kFontTableTag_maxp SkSetFourByteTag('m', 'a', 'x', 'p')
  22. static const struct TagSize {
  23. SkFontTableTag fTag;
  24. size_t fSize;
  25. } gKnownTableSizes[] = {
  26. { kFontTableTag_head, 54 },
  27. { kFontTableTag_hhea, 36 },
  28. };
  29. // Test that getUnitsPerEm() agrees with a direct lookup in the 'head' table
  30. // (if that table is available).
  31. static void test_unitsPerEm(skiatest::Reporter* reporter, const sk_sp<SkTypeface>& face) {
  32. int nativeUPEM = face->getUnitsPerEm();
  33. int tableUPEM = -1;
  34. size_t size = face->getTableSize(kFontTableTag_head);
  35. if (size) {
  36. // unitsPerEm is at offset 18 into the 'head' table.
  37. uint16_t rawUPEM;
  38. face->getTableData(kFontTableTag_head, 18, sizeof(rawUPEM), &rawUPEM);
  39. tableUPEM = SkEndian_SwapBE16(rawUPEM);
  40. }
  41. if (tableUPEM >= 0) {
  42. REPORTER_ASSERT(reporter, tableUPEM == nativeUPEM);
  43. }
  44. }
  45. // Test that countGlyphs() agrees with a direct lookup in the 'maxp' table
  46. // (if that table is available).
  47. static void test_countGlyphs(skiatest::Reporter* reporter, const sk_sp<SkTypeface>& face) {
  48. int nativeGlyphs = face->countGlyphs();
  49. int tableGlyphs = -1;
  50. size_t size = face->getTableSize(kFontTableTag_maxp);
  51. if (size) {
  52. // glyphs is at offset 4 into the 'maxp' table.
  53. uint16_t rawGlyphs;
  54. face->getTableData(kFontTableTag_maxp, 4, sizeof(rawGlyphs), &rawGlyphs);
  55. tableGlyphs = SkEndian_SwapBE16(rawGlyphs);
  56. }
  57. if (tableGlyphs >= 0) {
  58. REPORTER_ASSERT(reporter, tableGlyphs == nativeGlyphs);
  59. }
  60. }
  61. static void test_fontstream(skiatest::Reporter* reporter, SkStream* stream, int ttcIndex) {
  62. int n = SkFontStream::GetTableTags(stream, ttcIndex, nullptr);
  63. SkAutoTArray<SkFontTableTag> array(n);
  64. int n2 = SkFontStream::GetTableTags(stream, ttcIndex, array.get());
  65. REPORTER_ASSERT(reporter, n == n2);
  66. for (int i = 0; i < n; ++i) {
  67. #ifdef DUMP_TTC_TABLES
  68. SkString str;
  69. SkFontTableTag t = array[i];
  70. str.appendUnichar((t >> 24) & 0xFF);
  71. str.appendUnichar((t >> 16) & 0xFF);
  72. str.appendUnichar((t >> 8) & 0xFF);
  73. str.appendUnichar((t >> 0) & 0xFF);
  74. SkDebugf("[%d:%d] '%s'\n", ttcIndex, i, str.c_str());
  75. #endif
  76. size_t size = SkFontStream::GetTableSize(stream, ttcIndex, array[i]);
  77. for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) {
  78. if (gKnownTableSizes[j].fTag == array[i]) {
  79. REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size);
  80. }
  81. }
  82. }
  83. }
  84. static void test_fontstream(skiatest::Reporter* reporter) {
  85. std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream("fonts/test.ttc"));
  86. if (!stream) {
  87. SkDebugf("Skipping FontHostTest::test_fontstream\n");
  88. return;
  89. }
  90. int count = SkFontStream::CountTTCEntries(stream.get());
  91. #ifdef DUMP_TTC_TABLES
  92. SkDebugf("CountTTCEntries %d\n", count);
  93. #endif
  94. for (int i = 0; i < count; ++i) {
  95. test_fontstream(reporter, stream.get(), i);
  96. }
  97. }
  98. // Exercise this rare cmap format (platform 3, encoding 0)
  99. static void test_symbolfont(skiatest::Reporter* reporter) {
  100. auto tf = MakeResourceAsTypeface("fonts/SpiderSymbol.ttf");
  101. if (tf) {
  102. SkUnichar c = 0xf021;
  103. uint16_t g = SkFont(tf).unicharToGlyph(c);
  104. REPORTER_ASSERT(reporter, g == 3);
  105. } else {
  106. // not all platforms support data fonts, so we just note that failure
  107. SkDebugf("Skipping FontHostTest::test_symbolfont\n");
  108. }
  109. }
  110. static void test_tables(skiatest::Reporter* reporter, const sk_sp<SkTypeface>& face) {
  111. if (false) { // avoid bit rot, suppress warning
  112. SkFontID fontID = face->uniqueID();
  113. REPORTER_ASSERT(reporter, fontID);
  114. }
  115. int count = face->countTables();
  116. SkAutoTMalloc<SkFontTableTag> storage(count);
  117. SkFontTableTag* tags = storage.get();
  118. int count2 = face->getTableTags(tags);
  119. REPORTER_ASSERT(reporter, count2 == count);
  120. for (int i = 0; i < count; ++i) {
  121. size_t size = face->getTableSize(tags[i]);
  122. REPORTER_ASSERT(reporter, size > 0);
  123. #ifdef DUMP_TABLES
  124. char name[5];
  125. name[0] = (tags[i] >> 24) & 0xFF;
  126. name[1] = (tags[i] >> 16) & 0xFF;
  127. name[2] = (tags[i] >> 8) & 0xFF;
  128. name[3] = (tags[i] >> 0) & 0xFF;
  129. name[4] = 0;
  130. SkDebugf("%s %d\n", name, size);
  131. #endif
  132. for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) {
  133. if (gKnownTableSizes[j].fTag == tags[i]) {
  134. REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size);
  135. }
  136. }
  137. // do we get the same size from GetTableData and GetTableSize
  138. {
  139. SkAutoMalloc data(size);
  140. size_t size2 = face->getTableData(tags[i], 0, size, data.get());
  141. REPORTER_ASSERT(reporter, size2 == size);
  142. sk_sp<SkData> data2 = face->copyTableData(tags[i]);
  143. REPORTER_ASSERT(reporter, size == data2->size());
  144. REPORTER_ASSERT(reporter, !memcmp(data.get(), data2->data(), size));
  145. }
  146. }
  147. }
  148. static void test_tables(skiatest::Reporter* reporter) {
  149. static const char* const gNames[] = {
  150. nullptr, // default font
  151. "Helvetica", "Arial",
  152. "Times", "Times New Roman",
  153. "Courier", "Courier New",
  154. "Terminal", "MS Sans Serif",
  155. "Hiragino Mincho ProN", "MS PGothic",
  156. };
  157. for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
  158. sk_sp<SkTypeface> face(SkTypeface::MakeFromName(gNames[i], SkFontStyle()));
  159. if (face) {
  160. #ifdef DUMP_TABLES
  161. SkDebugf("%s\n", gNames[i]);
  162. #endif
  163. test_tables(reporter, face);
  164. test_unitsPerEm(reporter, face);
  165. test_countGlyphs(reporter, face);
  166. }
  167. }
  168. }
  169. /*
  170. * Verifies that the advance values returned by generateAdvance and
  171. * generateMetrics match.
  172. */
  173. static void test_advances(skiatest::Reporter* reporter) {
  174. static const char* const faces[] = {
  175. nullptr, // default font
  176. "Arial", "Times", "Times New Roman", "Helvetica", "Courier",
  177. "Courier New", "Verdana", "monospace",
  178. };
  179. static const struct {
  180. SkFontHinting hinting;
  181. bool linear;
  182. bool subpixel;
  183. } settings[] = {
  184. { SkFontHinting::kNone, false, false },
  185. { SkFontHinting::kNone, true, false },
  186. { SkFontHinting::kNone, false, true },
  187. { SkFontHinting::kSlight, false, false },
  188. { SkFontHinting::kSlight, true, false },
  189. { SkFontHinting::kSlight, false, true },
  190. { SkFontHinting::kNormal, false, false },
  191. { SkFontHinting::kNormal, true, false },
  192. { SkFontHinting::kNormal, false, true },
  193. };
  194. static const struct {
  195. SkScalar fScaleX;
  196. SkScalar fSkewX;
  197. } gScaleRec[] = {
  198. { SK_Scalar1, 0 },
  199. { SK_Scalar1/2, 0 },
  200. // these two exercise obliquing (skew)
  201. { SK_Scalar1, -SK_Scalar1/4 },
  202. { SK_Scalar1/2, -SK_Scalar1/4 },
  203. };
  204. SkFont font;
  205. char const * const txt = "long.text.with.lots.of.dots.";
  206. size_t textLen = strlen(txt);
  207. for (size_t i = 0; i < SK_ARRAY_COUNT(faces); i++) {
  208. font.setTypeface(SkTypeface::MakeFromName(faces[i], SkFontStyle()));
  209. for (size_t j = 0; j < SK_ARRAY_COUNT(settings); j++) {
  210. font.setHinting(settings[j].hinting);
  211. font.setLinearMetrics(settings[j].linear);
  212. font.setSubpixel(settings[j].subpixel);
  213. for (size_t k = 0; k < SK_ARRAY_COUNT(gScaleRec); ++k) {
  214. font.setScaleX(gScaleRec[k].fScaleX);
  215. font.setSkewX(gScaleRec[k].fSkewX);
  216. SkRect bounds;
  217. // For no hinting and light hinting this should take the
  218. // optimized generateAdvance path.
  219. SkScalar width1 = font.measureText(txt, textLen, SkTextEncoding::kUTF8);
  220. // Requesting the bounds forces a generateMetrics call.
  221. SkScalar width2 = font.measureText(txt, textLen, SkTextEncoding::kUTF8, &bounds);
  222. // SkDebugf("Font: %s, generateAdvance: %f, generateMetrics: %f\n",
  223. // faces[i], SkScalarToFloat(width1), SkScalarToFloat(width2));
  224. REPORTER_ASSERT(reporter, width1 == width2);
  225. }
  226. }
  227. }
  228. }
  229. DEF_TEST(FontHost, reporter) {
  230. test_tables(reporter);
  231. test_fontstream(reporter);
  232. test_advances(reporter);
  233. test_symbolfont(reporter);
  234. }
  235. // need tests for SkStrSearch