blob: 5c94b3c0d8662b769c322bab782e5ce29fbea06a [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"
9#include "SkTypeface.h"
10#include "SkFontHost.h"
11
12//#define DUMP_TABLES
13
14#define kFontTableTag_head SkSetFourByteTag('h', 'e', 'a', 'd')
15#define kFontTableTag_hhea SkSetFourByteTag('h', 'h', 'e', 'a')
16#define kFontTableTag_os_slash_2 SkSetFourByteTag('O', 'S', '/', '2')
17#define kFontTableTag_maxp SkSetFourByteTag('m', 'a', 'x', 'p')
18
19static const struct TagSize {
20 SkFontTableTag fTag;
21 size_t fSize;
22} gKnownTableSizes[] = {
23 { kFontTableTag_head, 54 },
24 { kFontTableTag_hhea, 36 },
25 { kFontTableTag_os_slash_2, 96 },
26 { kFontTableTag_maxp, 32 },
27};
28
29static void test_tables(skiatest::Reporter* reporter, SkTypeface* face) {
30 SkFontID fontID = face->uniqueID();
31
32 int count = SkFontHost::CountTables(fontID);
33 REPORTER_ASSERT(reporter, count > 0);
34
35 SkAutoTMalloc<SkFontTableTag> storage(count);
36 SkFontTableTag* tags = storage.get();
37 SkFontHost::GetTableTags(fontID, tags);
38
39 for (int i = 0; i < count; ++i) {
40 size_t size = SkFontHost::GetTableSize(fontID, tags[i]);
41 REPORTER_ASSERT(reporter, size > 0);
42
43#ifdef DUMP_TABLES
44 char name[5];
45 name[0] = (tags[i] >> 24) & 0xFF;
46 name[1] = (tags[i] >> 16) & 0xFF;
47 name[2] = (tags[i] >> 8) & 0xFF;
48 name[3] = (tags[i] >> 0) & 0xFF;
49 name[4] = 0;
50 SkDebugf("%s %d\n", name, size);
51#endif
52
53 for (size_t j = 0; j < SK_ARRAY_COUNT(gKnownTableSizes); ++j) {
54 if (gKnownTableSizes[j].fTag == tags[i]) {
55 REPORTER_ASSERT(reporter, gKnownTableSizes[j].fSize == size);
56 }
57 }
58 }
59}
60
61static void test_tables(skiatest::Reporter* reporter) {
62 static const char* const gNames[] = {
63 NULL, // default font
64 "Arial", "Times", "Times New Roman", "Helvetica", "Courier",
65 "Courier New",
66 };
67
68 for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
69 SkTypeface* face = SkTypeface::CreateFromName(gNames[i],
70 SkTypeface::kNormal);
71 if (face) {
72#ifdef DUMP_TABLES
73 SkDebugf("%s\n", gNames[i]);
74#endif
75 test_tables(reporter, face);
76 face->unref();
77 }
78 }
79}
80
81static void TestFontHost(skiatest::Reporter* reporter) {
82 test_tables(reporter);
83}
84
85// need tests for SkStrSearch
86
87#include "TestClassDef.h"
88DEFINE_TESTCLASS("FontHost", FontHostTestClass, TestFontHost)