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