bungeman@google.com | 8ec9956 | 2012-02-07 21:30:21 +0000 | [diff] [blame] | 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 | |
John Stiles | c88a6ac | 2020-07-27 08:41:27 -0400 | [diff] [blame] | 8 | #include <array> |
| 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/sfnt/SkOTUtils.h" |
Hal Canary | 4a851ca | 2017-11-09 11:09:34 -0500 | [diff] [blame] | 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
bungeman@google.com | 8ec9956 | 2012-02-07 21:30:21 +0000 | [diff] [blame] | 21 | |
bungeman@google.com | a544f29 | 2012-11-20 18:52:23 +0000 | [diff] [blame] | 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 |
skia.committer@gmail.com | b0a327e | 2012-11-21 02:02:25 +0000 | [diff] [blame] | 28 | |
bungeman@google.com | a544f29 | 2012-11-20 18:52:23 +0000 | [diff] [blame] | 29 | 0x0,0x0, //SkOTTableGlyphDataInstructions::length |
| 30 | }; |
| 31 | |
bungeman@google.com | 8ec9956 | 2012-02-07 21:30:21 +0000 | [diff] [blame] | 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 | } |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 40 | |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 41 | SkData* SkOTUtils::RenameFont(SkStreamAsset* fontData, const char* fontName, int fontNameLen) { |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 42 | |
| 43 | // Get the sfnt header. |
| 44 | SkSFNTHeader sfntHeader; |
| 45 | if (fontData->read(&sfntHeader, sizeof(sfntHeader)) < sizeof(sfntHeader)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 46 | return nullptr; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // Find the existing 'name' table. |
| 50 | int tableIndex; |
bungeman@google.com | a544f29 | 2012-11-20 18:52:23 +0000 | [diff] [blame] | 51 | SkSFNTHeader::TableDirectoryEntry tableEntry; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 52 | int numTables = SkEndian_SwapBE16(sfntHeader.numTables); |
| 53 | for (tableIndex = 0; tableIndex < numTables; ++tableIndex) { |
| 54 | if (fontData->read(&tableEntry, sizeof(tableEntry)) < sizeof(tableEntry)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 55 | return nullptr; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 56 | } |
bungeman@google.com | ec95a4a | 2012-06-25 14:26:48 +0000 | [diff] [blame] | 57 | if (SkOTTableName::TAG == tableEntry.tag) { |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 58 | break; |
| 59 | } |
| 60 | } |
| 61 | if (tableIndex == numTables) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 62 | return nullptr; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | if (!fontData->rewind()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 66 | return nullptr; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // The required 'name' record types: Family, Style, Unique, Full and PostScript. |
Ben Wagner | 1dac7bc | 2019-08-07 13:47:09 -0400 | [diff] [blame] | 70 | static constexpr std::array<SkOTTableName::Record::NameID::Predefined::Value, 5> names{{ |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 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, |
Ben Wagner | 1dac7bc | 2019-08-07 13:47:09 -0400 | [diff] [blame] | 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 | }}; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 83 | |
| 84 | // Copy the data, leaving out the old name table. |
| 85 | // In theory, we could also remove the DSIG table if it exists. |
Ben Wagner | 1dac7bc | 2019-08-07 13:47:09 -0400 | [diff] [blame] | 86 | size_t nameTableLogicalSize = sizeof(SkOTTableName) |
| 87 | + (encodings.size() * names.size() * sizeof(SkOTTableName::Record)) |
| 88 | + (fontNameLen * sizeof(SK_OT_USHORT)); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 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; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 97 | |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 98 | auto rewrittenFontData = SkData::MakeUninitialized(newDataSize); |
reed | 33a3050 | 2014-09-11 08:42:36 -0700 | [diff] [blame] | 99 | SK_OT_BYTE* data = static_cast<SK_OT_BYTE*>(rewrittenFontData->writable_data()); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 100 | |
| 101 | if (fontData->read(data, oldNameTableOffset) < oldNameTableOffset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 102 | return nullptr; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 103 | } |
| 104 | if (fontData->skip(oldNameTablePhysicalSize) < oldNameTablePhysicalSize) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 105 | return nullptr; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 106 | } |
| 107 | if (fontData->read(data + oldNameTableOffset, originalDataSize - oldNameTableOffset) < originalDataSize - oldNameTableOffset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 108 | return nullptr; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | //Fix up the offsets of the directory entries after the old 'name' table entry. |
bungeman@google.com | a544f29 | 2012-11-20 18:52:23 +0000 | [diff] [blame] | 112 | SkSFNTHeader::TableDirectoryEntry* currentEntry = reinterpret_cast<SkSFNTHeader::TableDirectoryEntry*>(data + sizeof(SkSFNTHeader)); |
| 113 | SkSFNTHeader::TableDirectoryEntry* endEntry = currentEntry + numTables; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 114 | SkSFNTHeader::TableDirectoryEntry* headTableEntry = nullptr; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 115 | for (; currentEntry < endEntry; ++currentEntry) { |
| 116 | uint32_t oldOffset = SkEndian_SwapBE32(currentEntry->offset); |
| 117 | if (oldOffset > oldNameTableOffset) { |
commit-bot@chromium.org | f117781 | 2014-04-23 19:19:44 +0000 | [diff] [blame] | 118 | currentEntry->offset = SkEndian_SwapBE32(SkToU32(oldOffset - oldNameTablePhysicalSize)); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 119 | } |
Ben Wagner | 1dac7bc | 2019-08-07 13:47:09 -0400 | [diff] [blame] | 120 | |
bungeman@google.com | caa3a3b | 2012-11-09 17:07:42 +0000 | [diff] [blame] | 121 | if (SkOTTableHead::TAG == currentEntry->tag) { |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 122 | headTableEntry = currentEntry; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Make the table directory entry point to the new 'name' table. |
bungeman@google.com | a544f29 | 2012-11-20 18:52:23 +0000 | [diff] [blame] | 127 | SkSFNTHeader::TableDirectoryEntry* nameTableEntry = reinterpret_cast<SkSFNTHeader::TableDirectoryEntry*>(data + sizeof(SkSFNTHeader)) + tableIndex; |
commit-bot@chromium.org | f117781 | 2014-04-23 19:19:44 +0000 | [diff] [blame] | 128 | nameTableEntry->logicalLength = SkEndian_SwapBE32(SkToU32(nameTableLogicalSize)); |
| 129 | nameTableEntry->offset = SkEndian_SwapBE32(SkToU32(originalDataSize)); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 130 | |
| 131 | // Write the new 'name' table after the original font data. |
| 132 | SkOTTableName* nameTable = reinterpret_cast<SkOTTableName*>(data + originalDataSize); |
Ben Wagner | 1dac7bc | 2019-08-07 13:47:09 -0400 | [diff] [blame] | 133 | unsigned short stringOffset = sizeof(SkOTTableName) + (encodings.size() * names.size() * sizeof(SkOTTableName::Record)); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 134 | nameTable->format = SkOTTableName::format_0; |
Ben Wagner | 1dac7bc | 2019-08-07 13:47:09 -0400 | [diff] [blame] | 135 | nameTable->count = SkEndian_SwapBE16(encodings.size() * names.size()); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 136 | nameTable->stringOffset = SkEndian_SwapBE16(stringOffset); |
| 137 | |
Ben Wagner | 1dac7bc | 2019-08-07 13:47:09 -0400 | [diff] [blame] | 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 | } |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 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 | |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 176 | return rewrittenFontData.release(); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 177 | } |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 178 | |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 179 | sk_sp<SkOTUtils::LocalizedStrings_NameTable> |
| 180 | SkOTUtils::LocalizedStrings_NameTable::Make(const SkTypeface& typeface, |
| 181 | SK_OT_USHORT types[], |
| 182 | int typesCount) |
| 183 | { |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 184 | static const SkFontTableTag nameTag = SkSetFourByteTag('n','a','m','e'); |
| 185 | size_t nameTableSize = typeface.getTableSize(nameTag); |
| 186 | if (0 == nameTableSize) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 187 | return nullptr; |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 188 | } |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 189 | std::unique_ptr<uint8_t[]> nameTableData(new uint8_t[nameTableSize]); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 190 | size_t copied = typeface.getTableData(nameTag, 0, nameTableSize, nameTableData.get()); |
| 191 | if (copied != nameTableSize) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 192 | return nullptr; |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 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)); |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 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 | |
Ben Wagner | ad031f5 | 2018-08-20 13:45:57 -0400 | [diff] [blame] | 223 | SK_OT_USHORT SkOTUtils::LocalizedStrings_NameTable::familyNameTypes[3] = { |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 224 | SkOTTableName::Record::NameID::Predefined::FontFamilyName, |
| 225 | SkOTTableName::Record::NameID::Predefined::PreferredFamily, |
| 226 | SkOTTableName::Record::NameID::Predefined::WWSFamilyName, |
| 227 | }; |
Hal Canary | 4a851ca | 2017-11-09 11:09:34 -0500 | [diff] [blame] | 228 | |
Hal Canary | a865d25 | 2017-11-09 16:16:09 -0500 | [diff] [blame] | 229 | void SkOTUtils::SetAdvancedTypefaceFlags(SkOTTableOS2_V4::Type fsType, |
Hal Canary | 4a851ca | 2017-11-09 11:09:34 -0500 | [diff] [blame] | 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 | } |