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