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 | |
8 | #include <array> |
9 | |
10 | #include "src/sfnt/SkOTUtils.h" |
11 | |
12 | #include "include/core/SkData.h" |
13 | #include "include/core/SkStream.h" |
14 | #include "include/private/SkTo.h" |
15 | #include "src/core/SkAdvancedTypefaceMetrics.h" |
16 | #include "src/core/SkEndian.h" |
17 | #include "src/sfnt/SkOTTableTypes.h" |
18 | #include "src/sfnt/SkOTTable_head.h" |
19 | #include "src/sfnt/SkOTTable_name.h" |
20 | #include "src/sfnt/SkSFNTHeader.h" |
21 | |
22 | extern const uint8_t SK_OT_GlyphData_NoOutline[] = { |
23 | 0x0,0x0, //SkOTTableGlyphData::numberOfContours |
24 | 0x0,0x0, //SkOTTableGlyphData::xMin |
25 | 0x0,0x0, //SkOTTableGlyphData::yMin |
26 | 0x0,0x0, //SkOTTableGlyphData::xMax |
27 | 0x0,0x0, //SkOTTableGlyphData::yMax |
28 | |
29 | 0x0,0x0, //SkOTTableGlyphDataInstructions::length |
30 | }; |
31 | |
32 | uint32_t SkOTUtils::CalcTableChecksum(SK_OT_ULONG *data, size_t length) { |
33 | uint32_t sum = 0; |
34 | SK_OT_ULONG *dataEnd = data + ((length + 3) & ~3) / sizeof(SK_OT_ULONG); |
35 | for (; data < dataEnd; ++data) { |
36 | sum += SkEndian_SwapBE32(*data); |
37 | } |
38 | return sum; |
39 | } |
40 | |
41 | SkData* SkOTUtils::RenameFont(SkStreamAsset* fontData, const char* fontName, int fontNameLen) { |
42 | |
43 | // Get the sfnt header. |
44 | SkSFNTHeader ; |
45 | if (fontData->read(&sfntHeader, sizeof(sfntHeader)) < sizeof(sfntHeader)) { |
46 | return nullptr; |
47 | } |
48 | |
49 | // Find the existing 'name' table. |
50 | int tableIndex; |
51 | SkSFNTHeader::TableDirectoryEntry tableEntry; |
52 | int numTables = SkEndian_SwapBE16(sfntHeader.numTables); |
53 | for (tableIndex = 0; tableIndex < numTables; ++tableIndex) { |
54 | if (fontData->read(&tableEntry, sizeof(tableEntry)) < sizeof(tableEntry)) { |
55 | return nullptr; |
56 | } |
57 | if (SkOTTableName::TAG == tableEntry.tag) { |
58 | break; |
59 | } |
60 | } |
61 | if (tableIndex == numTables) { |
62 | return nullptr; |
63 | } |
64 | |
65 | if (!fontData->rewind()) { |
66 | return nullptr; |
67 | } |
68 | |
69 | // The required 'name' record types: Family, Style, Unique, Full and PostScript. |
70 | static constexpr std::array<SkOTTableName::Record::NameID::Predefined::Value, 5> names{{ |
71 | SkOTTableName::Record::NameID::Predefined::FontFamilyName, |
72 | SkOTTableName::Record::NameID::Predefined::FontSubfamilyName, |
73 | SkOTTableName::Record::NameID::Predefined::UniqueFontIdentifier, |
74 | SkOTTableName::Record::NameID::Predefined::FullFontName, |
75 | SkOTTableName::Record::NameID::Predefined::PostscriptName, |
76 | }}; |
77 | |
78 | // GDI will not use a Symbol cmap table if there is no Symbol encoded name. |
79 | static constexpr std::array<SkOTTableName::Record::EncodingID::Windows::Value, 2> encodings{{ |
80 | SkOTTableName::Record::EncodingID::Windows::Symbol, |
81 | SkOTTableName::Record::EncodingID::Windows::UnicodeBMPUCS2, |
82 | }}; |
83 | |
84 | // Copy the data, leaving out the old name table. |
85 | // In theory, we could also remove the DSIG table if it exists. |
86 | size_t nameTableLogicalSize = sizeof(SkOTTableName) |
87 | + (encodings.size() * names.size() * sizeof(SkOTTableName::Record)) |
88 | + (fontNameLen * sizeof(SK_OT_USHORT)); |
89 | size_t nameTablePhysicalSize = (nameTableLogicalSize + 3) & ~3; // Rounded up to a multiple of 4. |
90 | |
91 | size_t oldNameTablePhysicalSize = (SkEndian_SwapBE32(tableEntry.logicalLength) + 3) & ~3; // Rounded up to a multiple of 4. |
92 | size_t oldNameTableOffset = SkEndian_SwapBE32(tableEntry.offset); |
93 | |
94 | //originalDataSize is the size of the original data without the name table. |
95 | size_t originalDataSize = fontData->getLength() - oldNameTablePhysicalSize; |
96 | size_t newDataSize = originalDataSize + nameTablePhysicalSize; |
97 | |
98 | auto rewrittenFontData = SkData::MakeUninitialized(newDataSize); |
99 | SK_OT_BYTE* data = static_cast<SK_OT_BYTE*>(rewrittenFontData->writable_data()); |
100 | |
101 | if (fontData->read(data, oldNameTableOffset) < oldNameTableOffset) { |
102 | return nullptr; |
103 | } |
104 | if (fontData->skip(oldNameTablePhysicalSize) < oldNameTablePhysicalSize) { |
105 | return nullptr; |
106 | } |
107 | if (fontData->read(data + oldNameTableOffset, originalDataSize - oldNameTableOffset) < originalDataSize - oldNameTableOffset) { |
108 | return nullptr; |
109 | } |
110 | |
111 | //Fix up the offsets of the directory entries after the old 'name' table entry. |
112 | SkSFNTHeader::TableDirectoryEntry* currentEntry = reinterpret_cast<SkSFNTHeader::TableDirectoryEntry*>(data + sizeof(SkSFNTHeader)); |
113 | SkSFNTHeader::TableDirectoryEntry* endEntry = currentEntry + numTables; |
114 | SkSFNTHeader::TableDirectoryEntry* headTableEntry = nullptr; |
115 | for (; currentEntry < endEntry; ++currentEntry) { |
116 | uint32_t oldOffset = SkEndian_SwapBE32(currentEntry->offset); |
117 | if (oldOffset > oldNameTableOffset) { |
118 | currentEntry->offset = SkEndian_SwapBE32(SkToU32(oldOffset - oldNameTablePhysicalSize)); |
119 | } |
120 | |
121 | if (SkOTTableHead::TAG == currentEntry->tag) { |
122 | headTableEntry = currentEntry; |
123 | } |
124 | } |
125 | |
126 | // Make the table directory entry point to the new 'name' table. |
127 | SkSFNTHeader::TableDirectoryEntry* nameTableEntry = reinterpret_cast<SkSFNTHeader::TableDirectoryEntry*>(data + sizeof(SkSFNTHeader)) + tableIndex; |
128 | nameTableEntry->logicalLength = SkEndian_SwapBE32(SkToU32(nameTableLogicalSize)); |
129 | nameTableEntry->offset = SkEndian_SwapBE32(SkToU32(originalDataSize)); |
130 | |
131 | // Write the new 'name' table after the original font data. |
132 | SkOTTableName* nameTable = reinterpret_cast<SkOTTableName*>(data + originalDataSize); |
133 | unsigned short stringOffset = sizeof(SkOTTableName) + (encodings.size() * names.size() * sizeof(SkOTTableName::Record)); |
134 | nameTable->format = SkOTTableName::format_0; |
135 | nameTable->count = SkEndian_SwapBE16(encodings.size() * names.size()); |
136 | nameTable->stringOffset = SkEndian_SwapBE16(stringOffset); |
137 | |
138 | SkOTTableName::Record* nameRecord = reinterpret_cast<SkOTTableName::Record*>(data + originalDataSize + sizeof(SkOTTableName)); |
139 | for (const auto& encoding : encodings) { |
140 | for (const auto& name : names) { |
141 | nameRecord->platformID.value = SkOTTableName::Record::PlatformID::Windows; |
142 | nameRecord->encodingID.windows.value = encoding; |
143 | nameRecord->languageID.windows.value = SkOTTableName::Record::LanguageID::Windows::English_UnitedStates; |
144 | nameRecord->nameID.predefined.value = name; |
145 | nameRecord->offset = SkEndian_SwapBE16(0); |
146 | nameRecord->length = SkEndian_SwapBE16(SkToU16(fontNameLen * sizeof(SK_OT_USHORT))); |
147 | ++nameRecord; |
148 | } |
149 | } |
150 | |
151 | SK_OT_USHORT* nameString = reinterpret_cast<SK_OT_USHORT*>(data + originalDataSize + stringOffset); |
152 | for (int i = 0; i < fontNameLen; ++i) { |
153 | nameString[i] = SkEndian_SwapBE16(fontName[i]); |
154 | } |
155 | |
156 | unsigned char* logical = data + originalDataSize + nameTableLogicalSize; |
157 | unsigned char* physical = data + originalDataSize + nameTablePhysicalSize; |
158 | for (; logical < physical; ++logical) { |
159 | *logical = 0; |
160 | } |
161 | |
162 | // Update the table checksum in the directory entry. |
163 | nameTableEntry->checksum = SkEndian_SwapBE32(SkOTUtils::CalcTableChecksum(reinterpret_cast<SK_OT_ULONG*>(nameTable), nameTableLogicalSize)); |
164 | |
165 | // Update the checksum adjustment in the head table. |
166 | if (headTableEntry) { |
167 | size_t headTableOffset = SkEndian_SwapBE32(headTableEntry->offset); |
168 | if (headTableOffset + sizeof(SkOTTableHead) < originalDataSize) { |
169 | SkOTTableHead* headTable = reinterpret_cast<SkOTTableHead*>(data + headTableOffset); |
170 | headTable->checksumAdjustment = SkEndian_SwapBE32(0); |
171 | uint32_t unadjustedFontChecksum = SkOTUtils::CalcTableChecksum(reinterpret_cast<SK_OT_ULONG*>(data), originalDataSize + nameTablePhysicalSize); |
172 | headTable->checksumAdjustment = SkEndian_SwapBE32(SkOTTableHead::fontChecksum - unadjustedFontChecksum); |
173 | } |
174 | } |
175 | |
176 | return rewrittenFontData.release(); |
177 | } |
178 | |
179 | sk_sp<SkOTUtils::LocalizedStrings_NameTable> |
180 | SkOTUtils::LocalizedStrings_NameTable::Make(const SkTypeface& typeface, |
181 | SK_OT_USHORT types[], |
182 | int typesCount) |
183 | { |
184 | static const SkFontTableTag nameTag = SkSetFourByteTag('n','a','m','e'); |
185 | size_t nameTableSize = typeface.getTableSize(nameTag); |
186 | if (0 == nameTableSize) { |
187 | return nullptr; |
188 | } |
189 | std::unique_ptr<uint8_t[]> nameTableData(new uint8_t[nameTableSize]); |
190 | size_t copied = typeface.getTableData(nameTag, 0, nameTableSize, nameTableData.get()); |
191 | if (copied != nameTableSize) { |
192 | return nullptr; |
193 | } |
194 | |
195 | return sk_sp<SkOTUtils::LocalizedStrings_NameTable>( |
196 | new SkOTUtils::LocalizedStrings_NameTable(std::move(nameTableData), nameTableSize, |
197 | types, typesCount)); |
198 | } |
199 | |
200 | sk_sp<SkOTUtils::LocalizedStrings_NameTable> |
201 | SkOTUtils::LocalizedStrings_NameTable::MakeForFamilyNames(const SkTypeface& typeface) { |
202 | return Make(typeface, |
203 | SkOTUtils::LocalizedStrings_NameTable::familyNameTypes, |
204 | SK_ARRAY_COUNT(SkOTUtils::LocalizedStrings_NameTable::familyNameTypes)); |
205 | } |
206 | |
207 | bool SkOTUtils::LocalizedStrings_NameTable::next(SkTypeface::LocalizedString* localizedString) { |
208 | do { |
209 | SkOTTableName::Iterator::Record record; |
210 | if (fFamilyNameIter.next(record)) { |
211 | localizedString->fString = record.name; |
212 | localizedString->fLanguage = record.language; |
213 | return true; |
214 | } |
215 | if (fTypesCount == fTypesIndex + 1) { |
216 | return false; |
217 | } |
218 | ++fTypesIndex; |
219 | fFamilyNameIter.reset(fTypes[fTypesIndex]); |
220 | } while (true); |
221 | } |
222 | |
223 | SK_OT_USHORT SkOTUtils::LocalizedStrings_NameTable::familyNameTypes[3] = { |
224 | SkOTTableName::Record::NameID::Predefined::FontFamilyName, |
225 | SkOTTableName::Record::NameID::Predefined::PreferredFamily, |
226 | SkOTTableName::Record::NameID::Predefined::WWSFamilyName, |
227 | }; |
228 | |
229 | void SkOTUtils::SetAdvancedTypefaceFlags(SkOTTableOS2_V4::Type fsType, |
230 | SkAdvancedTypefaceMetrics* info) { |
231 | SkASSERT(info); |
232 | // The logic should be identical to SkTypeface_FreeType::onGetAdvancedMetrics(). |
233 | if (fsType.raw.value != 0) { |
234 | if (SkToBool(fsType.field.Restricted) || SkToBool(fsType.field.Bitmap)) { |
235 | info->fFlags |= SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag; |
236 | } |
237 | if (SkToBool(fsType.field.NoSubsetting)) { |
238 | info->fFlags |= SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag; |
239 | } |
240 | } |
241 | } |
242 | |