Flutter Engine
The Flutter Engine
Classes | Static Public Member Functions | List of all members
SkOTUtils Struct Reference

#include <SkOTUtils.h>

Classes

class  LocalizedStrings_NameTable
 
class  LocalizedStrings_SingleName
 

Static Public Member Functions

static uint32_t CalcTableChecksum (SK_OT_ULONG *data, size_t length)
 
static SkDataRenameFont (SkStreamAsset *fontData, const char *fontName, int fontNameLen)
 
static void SetAdvancedTypefaceFlags (SkOTTableOS2_V4::Type fsType, SkAdvancedTypefaceMetrics *info)
 

Detailed Description

Definition at line 20 of file SkOTUtils.h.

Member Function Documentation

◆ CalcTableChecksum()

uint32_t SkOTUtils::CalcTableChecksum ( SK_OT_ULONG data,
size_t  length 
)
static

Calculates the OpenType checksum for data.

Definition at line 22 of file SkOTUtils.cpp.

22 {
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}
#define SkEndian_SwapBE32(n)
Definition: SkEndian.h:136
uint32_t SK_OT_ULONG
size_t length
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63

◆ RenameFont()

SkData * SkOTUtils::RenameFont ( SkStreamAsset fontData,
const char *  fontName,
int  fontNameLen 
)
static

Renames an sfnt font. On failure (invalid data or not an sfnt font) returns nullptr.

Essentially, this removes any existing 'name' table and replaces it with a new one in which FontFamilyName, FontSubfamilyName, UniqueFontIdentifier, FullFontName, and PostscriptName are fontName.

The new 'name' table records will be written with the Windows, UnicodeBMPUCS2, and English_UnitedStates settings.

fontName and fontNameLen must be specified in terms of ASCII chars.

Does not affect fontData's ownership.

Definition at line 31 of file SkOTUtils.cpp.

31 {
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;
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{{
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{{
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) {
132 nameRecord->encodingID.windows.value = encoding;
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);
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}
#define SkEndian_SwapBE16(n)
Definition: SkEndian.h:135
uint8_t SK_OT_BYTE
uint16_t SK_OT_USHORT
constexpr uint16_t SkToU16(S x)
Definition: SkTo.h:24
constexpr uint32_t SkToU32(S x)
Definition: SkTo.h:26
static sk_sp< SkData > MakeUninitialized(size_t length)
Definition: SkData.cpp:116
size_t getLength() const override=0
bool rewind() override=0
size_t skip(size_t size)
Definition: SkStream.h:51
virtual size_t read(void *buffer, size_t size)=0
static const char *const names[]
Definition: symbols.cc:24
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
SK_OT_ULONG checksumAdjustment
static const SK_OT_ULONG TAG
static const uint32_t fontChecksum
enum SkOTTableName::Record::EncodingID::Windows::Value value
enum SkOTTableName::Record::LanguageID::Windows::Value value
enum SkOTTableName::Record::NameID::Predefined::Value value
enum SkOTTableName::Record::PlatformID::Value value
struct SkOTTableName::Record::PlatformID platformID
union SkOTTableName::Record::NameID nameID
union SkOTTableName::Record::LanguageID languageID
union SkOTTableName::Record::EncodingID encodingID
static const SK_OT_USHORT format_0
SK_OT_USHORT stringOffset
SK_OT_USHORT format
static const SK_OT_ULONG TAG
SK_OT_USHORT count
static uint32_t CalcTableChecksum(SK_OT_ULONG *data, size_t length)
Definition: SkOTUtils.cpp:22
SK_SFNT_USHORT numTables
Definition: SkSFNTHeader.h:51
struct SkOTTableName::Record::EncodingID::Windows windows
struct SkOTTableName::Record::LanguageID::Windows windows
struct SkOTTableName::Record::NameID::Predefined predefined

◆ SetAdvancedTypefaceFlags()

void SkOTUtils::SetAdvancedTypefaceFlags ( SkOTTableOS2_V4::Type  fsType,
SkAdvancedTypefaceMetrics info 
)
static

Definition at line 219 of file SkOTUtils.cpp.

220 {
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)) {
226 }
227 if (SkToBool(fsType.field.NoSubsetting)) {
229 }
230 }
231}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
#define SkASSERT(cond)
Definition: SkAssert.h:116
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
@ kNotEmbeddable_FontFlag
May not be embedded.
@ kNotSubsettable_FontFlag
May not be subset.
struct SkOTTableOS2_V4::Type::Field field
struct SkOTTableOS2_V4::Type::Raw raw

The documentation for this struct was generated from the following files: