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