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 | |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 8 | #include "SkData.h" |
| 9 | #include "SkEndian.h" |
| 10 | #include "SkSFNTHeader.h" |
| 11 | #include "SkStream.h" |
| 12 | #include "SkOTTable_head.h" |
| 13 | #include "SkOTTable_name.h" |
bungeman@google.com | 8ec9956 | 2012-02-07 21:30:21 +0000 | [diff] [blame] | 14 | #include "SkOTTableTypes.h" |
| 15 | #include "SkOTUtils.h" |
| 16 | |
bungeman@google.com | a544f29 | 2012-11-20 18:52:23 +0000 | [diff] [blame] | 17 | extern const uint8_t SK_OT_GlyphData_NoOutline[] = { |
| 18 | 0x0,0x0, //SkOTTableGlyphData::numberOfContours |
| 19 | 0x0,0x0, //SkOTTableGlyphData::xMin |
| 20 | 0x0,0x0, //SkOTTableGlyphData::yMin |
| 21 | 0x0,0x0, //SkOTTableGlyphData::xMax |
| 22 | 0x0,0x0, //SkOTTableGlyphData::yMax |
skia.committer@gmail.com | b0a327e | 2012-11-21 02:02:25 +0000 | [diff] [blame] | 23 | |
bungeman@google.com | a544f29 | 2012-11-20 18:52:23 +0000 | [diff] [blame] | 24 | 0x0,0x0, //SkOTTableGlyphDataInstructions::length |
| 25 | }; |
| 26 | |
bungeman@google.com | 8ec9956 | 2012-02-07 21:30:21 +0000 | [diff] [blame] | 27 | uint32_t SkOTUtils::CalcTableChecksum(SK_OT_ULONG *data, size_t length) { |
| 28 | uint32_t sum = 0; |
| 29 | SK_OT_ULONG *dataEnd = data + ((length + 3) & ~3) / sizeof(SK_OT_ULONG); |
| 30 | for (; data < dataEnd; ++data) { |
| 31 | sum += SkEndian_SwapBE32(*data); |
| 32 | } |
| 33 | return sum; |
| 34 | } |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 35 | |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 36 | SkData* SkOTUtils::RenameFont(SkStreamAsset* fontData, const char* fontName, int fontNameLen) { |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 37 | |
| 38 | // Get the sfnt header. |
| 39 | SkSFNTHeader sfntHeader; |
| 40 | if (fontData->read(&sfntHeader, sizeof(sfntHeader)) < sizeof(sfntHeader)) { |
| 41 | return NULL; |
| 42 | } |
| 43 | |
| 44 | // Find the existing 'name' table. |
| 45 | int tableIndex; |
bungeman@google.com | a544f29 | 2012-11-20 18:52:23 +0000 | [diff] [blame] | 46 | SkSFNTHeader::TableDirectoryEntry tableEntry; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 47 | int numTables = SkEndian_SwapBE16(sfntHeader.numTables); |
| 48 | for (tableIndex = 0; tableIndex < numTables; ++tableIndex) { |
| 49 | if (fontData->read(&tableEntry, sizeof(tableEntry)) < sizeof(tableEntry)) { |
| 50 | return NULL; |
| 51 | } |
bungeman@google.com | ec95a4a | 2012-06-25 14:26:48 +0000 | [diff] [blame] | 52 | if (SkOTTableName::TAG == tableEntry.tag) { |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 53 | break; |
| 54 | } |
| 55 | } |
| 56 | if (tableIndex == numTables) { |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | if (!fontData->rewind()) { |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | // The required 'name' record types: Family, Style, Unique, Full and PostScript. |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 65 | const SkOTTableName::Record::NameID::Predefined::Value namesToCreate[] = { |
| 66 | SkOTTableName::Record::NameID::Predefined::FontFamilyName, |
| 67 | SkOTTableName::Record::NameID::Predefined::FontSubfamilyName, |
| 68 | SkOTTableName::Record::NameID::Predefined::UniqueFontIdentifier, |
| 69 | SkOTTableName::Record::NameID::Predefined::FullFontName, |
| 70 | SkOTTableName::Record::NameID::Predefined::PostscriptName, |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 71 | }; |
| 72 | const int namesCount = SK_ARRAY_COUNT(namesToCreate); |
| 73 | |
| 74 | // Copy the data, leaving out the old name table. |
| 75 | // In theory, we could also remove the DSIG table if it exists. |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 76 | size_t nameTableLogicalSize = sizeof(SkOTTableName) + (namesCount * sizeof(SkOTTableName::Record)) + (fontNameLen * sizeof(wchar_t)); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 77 | size_t nameTablePhysicalSize = (nameTableLogicalSize + 3) & ~3; // Rounded up to a multiple of 4. |
| 78 | |
| 79 | size_t oldNameTablePhysicalSize = (SkEndian_SwapBE32(tableEntry.logicalLength) + 3) & ~3; // Rounded up to a multiple of 4. |
| 80 | size_t oldNameTableOffset = SkEndian_SwapBE32(tableEntry.offset); |
| 81 | |
| 82 | //originalDataSize is the size of the original data without the name table. |
| 83 | size_t originalDataSize = fontData->getLength() - oldNameTablePhysicalSize; |
| 84 | size_t newDataSize = originalDataSize + nameTablePhysicalSize; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 85 | |
reed | 33a3050 | 2014-09-11 08:42:36 -0700 | [diff] [blame] | 86 | SkAutoTUnref<SkData> rewrittenFontData(SkData::NewUninitialized(newDataSize)); |
| 87 | 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] | 88 | |
| 89 | if (fontData->read(data, oldNameTableOffset) < oldNameTableOffset) { |
| 90 | return NULL; |
| 91 | } |
| 92 | if (fontData->skip(oldNameTablePhysicalSize) < oldNameTablePhysicalSize) { |
| 93 | return NULL; |
| 94 | } |
| 95 | if (fontData->read(data + oldNameTableOffset, originalDataSize - oldNameTableOffset) < originalDataSize - oldNameTableOffset) { |
| 96 | return NULL; |
| 97 | } |
| 98 | |
| 99 | //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] | 100 | SkSFNTHeader::TableDirectoryEntry* currentEntry = reinterpret_cast<SkSFNTHeader::TableDirectoryEntry*>(data + sizeof(SkSFNTHeader)); |
| 101 | SkSFNTHeader::TableDirectoryEntry* endEntry = currentEntry + numTables; |
| 102 | SkSFNTHeader::TableDirectoryEntry* headTableEntry = NULL; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 103 | for (; currentEntry < endEntry; ++currentEntry) { |
| 104 | uint32_t oldOffset = SkEndian_SwapBE32(currentEntry->offset); |
| 105 | if (oldOffset > oldNameTableOffset) { |
commit-bot@chromium.org | f117781 | 2014-04-23 19:19:44 +0000 | [diff] [blame] | 106 | currentEntry->offset = SkEndian_SwapBE32(SkToU32(oldOffset - oldNameTablePhysicalSize)); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 107 | } |
bungeman@google.com | caa3a3b | 2012-11-09 17:07:42 +0000 | [diff] [blame] | 108 | if (SkOTTableHead::TAG == currentEntry->tag) { |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 109 | headTableEntry = currentEntry; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Make the table directory entry point to the new 'name' table. |
bungeman@google.com | a544f29 | 2012-11-20 18:52:23 +0000 | [diff] [blame] | 114 | 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] | 115 | nameTableEntry->logicalLength = SkEndian_SwapBE32(SkToU32(nameTableLogicalSize)); |
| 116 | nameTableEntry->offset = SkEndian_SwapBE32(SkToU32(originalDataSize)); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 117 | |
| 118 | // Write the new 'name' table after the original font data. |
| 119 | SkOTTableName* nameTable = reinterpret_cast<SkOTTableName*>(data + originalDataSize); |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 120 | unsigned short stringOffset = sizeof(SkOTTableName) + (namesCount * sizeof(SkOTTableName::Record)); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 121 | nameTable->format = SkOTTableName::format_0; |
| 122 | nameTable->count = SkEndian_SwapBE16(namesCount); |
| 123 | nameTable->stringOffset = SkEndian_SwapBE16(stringOffset); |
| 124 | |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 125 | SkOTTableName::Record* nameRecords = reinterpret_cast<SkOTTableName::Record*>(data + originalDataSize + sizeof(SkOTTableName)); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 126 | for (int i = 0; i < namesCount; ++i) { |
bungeman@google.com | 07a69f8 | 2013-04-02 14:12:38 +0000 | [diff] [blame] | 127 | nameRecords[i].platformID.value = SkOTTableName::Record::PlatformID::Windows; |
| 128 | nameRecords[i].encodingID.windows.value = SkOTTableName::Record::EncodingID::Windows::UnicodeBMPUCS2; |
| 129 | nameRecords[i].languageID.windows.value = SkOTTableName::Record::LanguageID::Windows::English_UnitedStates; |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 130 | nameRecords[i].nameID.predefined.value = namesToCreate[i]; |
| 131 | nameRecords[i].offset = SkEndian_SwapBE16(0); |
bsalomon | ef3fcd8 | 2014-12-12 08:51:38 -0800 | [diff] [blame] | 132 | nameRecords[i].length = SkEndian_SwapBE16(SkToU16(fontNameLen * sizeof(wchar_t))); |
bungeman@google.com | a550199 | 2012-05-18 19:06:41 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | SK_OT_USHORT* nameString = reinterpret_cast<SK_OT_USHORT*>(data + originalDataSize + stringOffset); |
| 136 | for (int i = 0; i < fontNameLen; ++i) { |
| 137 | nameString[i] = SkEndian_SwapBE16(fontName[i]); |
| 138 | } |
| 139 | |
| 140 | unsigned char* logical = data + originalDataSize + nameTableLogicalSize; |
| 141 | unsigned char* physical = data + originalDataSize + nameTablePhysicalSize; |
| 142 | for (; logical < physical; ++logical) { |
| 143 | *logical = 0; |
| 144 | } |
| 145 | |
| 146 | // Update the table checksum in the directory entry. |
| 147 | nameTableEntry->checksum = SkEndian_SwapBE32(SkOTUtils::CalcTableChecksum(reinterpret_cast<SK_OT_ULONG*>(nameTable), nameTableLogicalSize)); |
| 148 | |
| 149 | // Update the checksum adjustment in the head table. |
| 150 | if (headTableEntry) { |
| 151 | size_t headTableOffset = SkEndian_SwapBE32(headTableEntry->offset); |
| 152 | if (headTableOffset + sizeof(SkOTTableHead) < originalDataSize) { |
| 153 | SkOTTableHead* headTable = reinterpret_cast<SkOTTableHead*>(data + headTableOffset); |
| 154 | headTable->checksumAdjustment = SkEndian_SwapBE32(0); |
| 155 | uint32_t unadjustedFontChecksum = SkOTUtils::CalcTableChecksum(reinterpret_cast<SK_OT_ULONG*>(data), originalDataSize + nameTablePhysicalSize); |
| 156 | headTable->checksumAdjustment = SkEndian_SwapBE32(SkOTTableHead::fontChecksum - unadjustedFontChecksum); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return rewrittenFontData.detach(); |
| 161 | } |
bungeman@google.com | a980269 | 2013-08-07 02:45:25 +0000 | [diff] [blame] | 162 | |
| 163 | |
| 164 | SkOTUtils::LocalizedStrings_NameTable* |
| 165 | SkOTUtils::LocalizedStrings_NameTable::CreateForFamilyNames(const SkTypeface& typeface) { |
| 166 | static const SkFontTableTag nameTag = SkSetFourByteTag('n','a','m','e'); |
| 167 | size_t nameTableSize = typeface.getTableSize(nameTag); |
| 168 | if (0 == nameTableSize) { |
| 169 | return NULL; |
| 170 | } |
| 171 | SkAutoTDeleteArray<uint8_t> nameTableData(new uint8_t[nameTableSize]); |
| 172 | size_t copied = typeface.getTableData(nameTag, 0, nameTableSize, nameTableData.get()); |
| 173 | if (copied != nameTableSize) { |
| 174 | return NULL; |
| 175 | } |
| 176 | |
| 177 | return new SkOTUtils::LocalizedStrings_NameTable((SkOTTableName*)nameTableData.detach(), |
| 178 | SkOTUtils::LocalizedStrings_NameTable::familyNameTypes, |
| 179 | SK_ARRAY_COUNT(SkOTUtils::LocalizedStrings_NameTable::familyNameTypes)); |
| 180 | } |
| 181 | |
| 182 | bool SkOTUtils::LocalizedStrings_NameTable::next(SkTypeface::LocalizedString* localizedString) { |
| 183 | do { |
| 184 | SkOTTableName::Iterator::Record record; |
| 185 | if (fFamilyNameIter.next(record)) { |
| 186 | localizedString->fString = record.name; |
| 187 | localizedString->fLanguage = record.language; |
| 188 | return true; |
| 189 | } |
| 190 | if (fTypesCount == fTypesIndex + 1) { |
| 191 | return false; |
| 192 | } |
| 193 | ++fTypesIndex; |
| 194 | fFamilyNameIter.reset(fTypes[fTypesIndex]); |
| 195 | } while (true); |
| 196 | } |
| 197 | |
| 198 | SkOTTableName::Record::NameID::Predefined::Value |
| 199 | SkOTUtils::LocalizedStrings_NameTable::familyNameTypes[3] = { |
| 200 | SkOTTableName::Record::NameID::Predefined::FontFamilyName, |
| 201 | SkOTTableName::Record::NameID::Predefined::PreferredFamily, |
| 202 | SkOTTableName::Record::NameID::Predefined::WWSFamilyName, |
| 203 | }; |