OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkChecksum.h" | 8 #include "SkChecksum.h" |
9 #include "SkFontDescriptor.h" | 9 #include "SkFontDescriptor.h" |
10 #include "SkStream.h" | 10 #include "SkStream.h" |
11 #include "SkString.h" | 11 #include "SkString.h" |
12 #include "SkTypeface.h" | 12 #include "SkTypeface.h" |
13 #include "SkUtils.h" | 13 #include "SkUtils.h" |
| 14 #include "../sfnt/SkOTUtils.h" |
14 | 15 |
15 #include "SkWhitelistChecksums.cpp" | 16 #include "SkWhitelistChecksums.cpp" |
16 | 17 |
17 #define WHITELIST_DEBUG 0 | 18 #define WHITELIST_DEBUG 0 |
18 | 19 |
19 extern void WhitelistSerializeTypeface(const SkTypeface*, SkWStream* ); | 20 extern void WhitelistSerializeTypeface(const SkTypeface*, SkWStream* ); |
20 extern SkTypeface* WhitelistDeserializeTypeface(SkStream* ); | 21 extern SkTypeface* WhitelistDeserializeTypeface(SkStream* ); |
21 extern bool CheckChecksums(); | 22 extern bool CheckChecksums(); |
22 extern bool GenerateChecksums(); | 23 extern bool GenerateChecksums(); |
23 | 24 |
24 #if WHITELIST_DEBUG | 25 #if WHITELIST_DEBUG |
25 static bool timesNewRomanSerializedNameOnly = false; | 26 static bool timesNewRomanSerializedNameOnly = false; |
26 #endif | 27 #endif |
27 | 28 |
28 struct NameRecord { | |
29 unsigned short fPlatformID; | |
30 unsigned short fEncodingID; | |
31 unsigned short fLanguageID; | |
32 unsigned short fNameID; | |
33 unsigned short fLength; | |
34 unsigned short fOffset; | |
35 }; | |
36 | |
37 struct NameTable { | |
38 unsigned short fFormat; | |
39 unsigned short fCount; | |
40 unsigned short fStringOffset; | |
41 NameRecord fRecord[1]; | |
42 }; | |
43 | |
44 #define SUBNAME_PREFIX "sk_" | 29 #define SUBNAME_PREFIX "sk_" |
45 | 30 |
46 static unsigned short swizzle(unsigned short x) { | |
47 return x << 8 | (x >> 8 & 0xff); | |
48 } | |
49 | |
50 static bool font_name_is_local(const char* fontName, SkTypeface::Style style) { | 31 static bool font_name_is_local(const char* fontName, SkTypeface::Style style) { |
51 if (!strcmp(fontName, "DejaVu Sans")) { | 32 if (!strcmp(fontName, "DejaVu Sans")) { |
52 return true; | 33 return true; |
53 } | 34 } |
54 SkTypeface* defaultFace = SkTypeface::CreateFromName(nullptr, style); | 35 SkTypeface* defaultFace = SkTypeface::CreateFromName(nullptr, style); |
55 SkTypeface* foundFace = SkTypeface::CreateFromName(fontName, style); | 36 SkTypeface* foundFace = SkTypeface::CreateFromName(fontName, style); |
56 return defaultFace != foundFace; | 37 return defaultFace != foundFace; |
57 } | 38 } |
58 | 39 |
59 static int name_table(const NameTable* nameTable, int tableIndex, const char** s
tringLocPtr) { | 40 static int whitelist_name_index(const SkTypeface* tf) { |
60 int nameTableCount = swizzle(nameTable->fCount); | |
61 for (int i = 0; i < nameTableCount; ++i) { | |
62 const NameRecord* nameRecord = &nameTable->fRecord[i]; | |
63 int recordNameID = swizzle(nameRecord->fNameID); | |
64 if (recordNameID != tableIndex) { | |
65 continue; | |
66 } | |
67 int stringLen = swizzle(nameRecord->fLength); | |
68 if (!stringLen) { | |
69 break; | |
70 } | |
71 int recordOffset = swizzle(nameRecord->fOffset); | |
72 const char* stringLoc = (const char* ) nameTable + swizzle(nameTable->fS
tringOffset); | |
73 stringLoc += recordOffset; | |
74 *stringLocPtr = stringLoc; | |
75 return stringLen; | |
76 } | |
77 return -1; | |
78 } | |
79 | 41 |
80 static int whitelist_name_index(const SkTypeface* tf) { | |
81 static const SkFontTableTag nameTag = SkSetFourByteTag('n', 'a', 'm', 'e'); | |
82 size_t nameSize = tf->getTableSize(nameTag); | |
83 if (!nameSize) { | |
84 return -1; | |
85 } | |
86 SkTDArray<char> name; | |
87 name.setCount((int) nameSize); | |
88 tf->getTableData(nameTag, 0, nameSize, name.begin()); | |
89 const NameTable* nameTable = (const NameTable* ) name.begin(); | |
90 const char* stringLoc; | |
91 int stringLen = name_table(nameTable, 1, &stringLoc); | |
92 if (stringLen < 0) { | |
93 stringLen = name_table(nameTable, 16, &stringLoc); | |
94 } | |
95 if (stringLen < 0) { | |
96 stringLen = name_table(nameTable, 21, &stringLoc); | |
97 } | |
98 if (stringLen < 0) { | |
99 return -1; | |
100 } | |
101 SkString fontNameStr; | 42 SkString fontNameStr; |
102 if (!*stringLoc) { | 43 SkAutoTUnref<SkTypeface::LocalizedStrings> nameIter( |
103 stringLen /= 2; | 44 SkOTUtils::LocalizedStrings_NameTable::CreateForFamilyNames(*tf)); |
104 for (int i = 0; i < stringLen; ++i) { | 45 SkTypeface::LocalizedString familyNameLocalized; |
105 fontNameStr.appendUnichar(swizzle(((const uint16_t*) stringLoc)[i]))
; | 46 while (nameIter->next(&familyNameLocalized)) { |
106 } | 47 fontNameStr = familyNameLocalized.fString; |
107 } else { | 48 // check against permissible list of names |
108 fontNameStr.resize(stringLen); | 49 for (int i = 0; i < whitelistCount; ++i) { |
109 strncpy(fontNameStr.writable_str(), stringLoc, stringLen); | 50 if (fontNameStr.equals(whitelist[i].fFontName)) { |
110 } | 51 return i; |
111 // check against permissible list of names | 52 } |
112 for (int i = 0; i < whitelistCount; ++i) { | |
113 if (fontNameStr.equals(whitelist[i].fFontName)) { | |
114 return i; | |
115 } | |
116 } | |
117 for (int i = 0; i < whitelistCount; ++i) { | |
118 if (fontNameStr.startsWith(whitelist[i].fFontName)) { | |
119 #if WHITELIST_DEBUG | |
120 SkDebugf("partial match whitelist=\"%s\" fontName=\"%s\"\n", whiteli
st[i].fFontName, | |
121 fontNameStr.c_str()); | |
122 #endif | |
123 return -1; | |
124 } | 53 } |
125 } | 54 } |
126 #if WHITELIST_DEBUG | 55 #if WHITELIST_DEBUG |
127 SkDebugf("no match fontName=\"%s\"\n", fontNameStr.c_str()); | 56 SkAutoTUnref<SkTypeface::LocalizedStrings> debugIter( |
| 57 SkOTUtils::LocalizedStrings_NameTable::CreateForFamilyNames(*tf)); |
| 58 while (debugIter->next(&familyNameLocalized)) { |
| 59 SkDebugf("no match fontName=\"%s\"\n", familyNameLocalized.fString.c_str
()); |
| 60 } |
128 #endif | 61 #endif |
129 return -1; | 62 return -1; |
130 } | 63 } |
131 | 64 |
132 static uint32_t compute_checksum(const SkTypeface* tf) { | 65 static uint32_t compute_checksum(const SkTypeface* tf) { |
133 SkFontData* fontData = tf->createFontData(); | 66 SkFontData* fontData = tf->createFontData(); |
134 if (!fontData) { | 67 if (!fontData) { |
135 return 0; | 68 return 0; |
136 } | 69 } |
137 SkStreamAsset* fontStream = fontData->getStream(); | 70 SkStreamAsset* fontStream = fontData->getStream(); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 if (data) { | 189 if (data) { |
257 SkTypeface* typeface = SkTypeface::CreateFromFontData(data); | 190 SkTypeface* typeface = SkTypeface::CreateFromFontData(data); |
258 if (typeface) { | 191 if (typeface) { |
259 return typeface; | 192 return typeface; |
260 } | 193 } |
261 } | 194 } |
262 const char* familyName = desc.getFamilyName(); | 195 const char* familyName = desc.getFamilyName(); |
263 if (!strncmp(SUBNAME_PREFIX, familyName, sizeof(SUBNAME_PREFIX) - 1)) { | 196 if (!strncmp(SUBNAME_PREFIX, familyName, sizeof(SUBNAME_PREFIX) - 1)) { |
264 familyName += sizeof(SUBNAME_PREFIX) - 1; | 197 familyName += sizeof(SUBNAME_PREFIX) - 1; |
265 } | 198 } |
266 return SkTypeface::CreateFromName(desc.getFamilyName(), desc.getStyle()); | 199 return SkTypeface::CreateFromName(familyName, desc.getStyle()); |
267 } | 200 } |
268 | 201 |
269 bool CheckChecksums() { | 202 bool CheckChecksums() { |
270 for (int i = 0; i < whitelistCount; ++i) { | 203 for (int i = 0; i < whitelistCount; ++i) { |
271 const char* fontName = whitelist[i].fFontName; | 204 const char* fontName = whitelist[i].fFontName; |
272 SkTypeface* tf = SkTypeface::CreateFromName(fontName, SkTypeface::kNorma
l); | 205 SkTypeface* tf = SkTypeface::CreateFromName(fontName, SkTypeface::kNorma
l); |
273 uint32_t checksum = compute_checksum(tf); | 206 uint32_t checksum = compute_checksum(tf); |
274 if (whitelist[i].fChecksum != checksum) { | 207 if (whitelist[i].fChecksum != checksum) { |
275 return false; | 208 return false; |
276 } | 209 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 const char* fontName = whitelist[i].fFontName; | 259 const char* fontName = whitelist[i].fFontName; |
327 SkTypeface* tf = SkTypeface::CreateFromName(fontName, SkTypeface::kNorma
l); | 260 SkTypeface* tf = SkTypeface::CreateFromName(fontName, SkTypeface::kNorma
l); |
328 uint32_t checksum = compute_checksum(tf); | 261 uint32_t checksum = compute_checksum(tf); |
329 line.printf(checksumEntry, fontName, checksum); | 262 line.printf(checksumEntry, fontName, checksum); |
330 sk_fwrite(line.c_str(), line.size(), file); | 263 sk_fwrite(line.c_str(), line.size(), file); |
331 } | 264 } |
332 sk_fwrite(checksumTrailer, sizeof(checksumTrailer) - 1, file); | 265 sk_fwrite(checksumTrailer, sizeof(checksumTrailer) - 1, file); |
333 sk_fclose(file); | 266 sk_fclose(file); |
334 return true; | 267 return true; |
335 } | 268 } |
OLD | NEW |