| 1 |
#include "private.h" |
| 2 |
|
| 3 |
#define LOOKUP_READER(llt, fn) \ |
| 4 |
case llt: \ |
| 5 |
return fn(data, tableLength, subtableOffset, maxGlyphs, options); |
| 6 |
|
| 7 |
otl_Subtable *otfcc_readOtl_subtable(font_file_pointer data, uint32_t tableLength, |
| 8 |
uint32_t subtableOffset, otl_LookupType lookupType, |
| 9 |
const glyphid_t maxGlyphs, const otfcc_Options *options) { |
| 10 |
switch (lookupType) { |
| 11 |
LOOKUP_READER(otl_type_gsub_single, otl_read_gsub_single); |
| 12 |
LOOKUP_READER(otl_type_gsub_multiple, otl_read_gsub_multi); |
| 13 |
LOOKUP_READER(otl_type_gsub_alternate, otl_read_gsub_multi); |
| 14 |
LOOKUP_READER(otl_type_gsub_ligature, otl_read_gsub_ligature); |
| 15 |
LOOKUP_READER(otl_type_gsub_chaining, otl_read_chaining); |
| 16 |
LOOKUP_READER(otl_type_gsub_reverse, otl_read_gsub_reverse); |
| 17 |
LOOKUP_READER(otl_type_gpos_chaining, otl_read_chaining); |
| 18 |
LOOKUP_READER(otl_type_gsub_context, otl_read_contextual); |
| 19 |
LOOKUP_READER(otl_type_gpos_context, otl_read_contextual); |
| 20 |
LOOKUP_READER(otl_type_gpos_single, otl_read_gpos_single); |
| 21 |
LOOKUP_READER(otl_type_gpos_pair, otl_read_gpos_pair); |
| 22 |
LOOKUP_READER(otl_type_gpos_cursive, otl_read_gpos_cursive); |
| 23 |
LOOKUP_READER(otl_type_gpos_markToBase, otl_read_gpos_markToSingle); |
| 24 |
LOOKUP_READER(otl_type_gpos_markToMark, otl_read_gpos_markToSingle); |
| 25 |
LOOKUP_READER(otl_type_gpos_markToLigature, otl_read_gpos_markToLigature); |
| 26 |
LOOKUP_READER(otl_type_gsub_extend, otfcc_readOtl_gsub_extend); |
| 27 |
LOOKUP_READER(otl_type_gpos_extend, otfcc_readOtl_gpos_extend); |
| 28 |
default: |
| 29 |
return NULL; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
static void parseLanguage(font_file_pointer data, uint32_t tableLength, uint32_t base, |
| 34 |
otl_LanguageSystem *lang, otl_FeatureList *features) { |
| 35 |
checkLength(base + 6); |
| 36 |
tableid_t rid = read_16u(data + base + 2); |
| 37 |
if (rid < features->length) { |
| 38 |
lang->requiredFeature = features->items[rid]; |
| 39 |
} else { |
| 40 |
lang->requiredFeature = NULL; |
| 41 |
} |
| 42 |
tableid_t featureCount = read_16u(data + base + 4); |
| 43 |
for (tableid_t j = 0; j < featureCount; j++) { |
| 44 |
tableid_t featureIndex = read_16u(data + base + 6 + 2 * j); |
| 45 |
if (featureIndex < features->length) { |
| 46 |
otl_iFeatureRefList.push(&lang->features, features->items[featureIndex]); |
| 47 |
} |
| 48 |
} |
| 49 |
return; |
| 50 |
FAIL: |
| 51 |
otl_iFeatureRefList.dispose(&lang->features); |
| 52 |
lang->requiredFeature = NULL; |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
static table_OTL *otfcc_readOtl_common(font_file_pointer data, uint32_t tableLength, |
| 57 |
otl_LookupType lookup_type_base, |
| 58 |
const otfcc_Options *options) { |
| 59 |
table_OTL *table = table_iOTL.create(); |
| 60 |
if (!table) goto FAIL; |
| 61 |
checkLength(10); |
| 62 |
uint32_t scriptListOffset = read_16u(data + 4); |
| 63 |
checkLength(scriptListOffset + 2); |
| 64 |
uint32_t featureListOffset = read_16u(data + 6); |
| 65 |
checkLength(featureListOffset + 2); |
| 66 |
uint32_t lookupListOffset = read_16u(data + 8); |
| 67 |
checkLength(lookupListOffset + 2); |
| 68 |
|
| 69 |
// parse lookup list |
| 70 |
{ |
| 71 |
tableid_t lookupCount = read_16u(data + lookupListOffset); |
| 72 |
checkLength(lookupListOffset + 2 + lookupCount * 2); |
| 73 |
for (tableid_t j = 0; j < lookupCount; j++) { |
| 74 |
otl_Lookup *lookup; |
| 75 |
otl_iLookupPtr.init(&lookup); |
| 76 |
lookup->_offset = lookupListOffset + read_16u(data + lookupListOffset + 2 + 2 * j); |
| 77 |
checkLength(lookup->_offset + 6); |
| 78 |
lookup->type = read_16u(data + lookup->_offset) + lookup_type_base; |
| 79 |
otl_iLookupList.push(&table->lookups, lookup); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
// parse feature list |
| 84 |
{ |
| 85 |
tableid_t featureCount = read_16u(data + featureListOffset); |
| 86 |
checkLength(featureListOffset + 2 + featureCount * 6); |
| 87 |
tableid_t lnk = 0; |
| 88 |
for (tableid_t j = 0; j < featureCount; j++) { |
| 89 |
otl_Feature *feature; |
| 90 |
otl_iFeaturePtr.init(&feature); |
| 91 |
uint32_t tag = read_32u(data + featureListOffset + 2 + j * 6); |
| 92 |
if (options->glyph_name_prefix) { |
| 93 |
feature->name = sdscatprintf(sdsempty(), "%c%c%c%c_%s_%05d", (tag >> 24) & 0xFF, |
| 94 |
(tag >> 16) & 0xFF, (tag >> 8) & 0xff, tag & 0xff, |
| 95 |
options->glyph_name_prefix, j); |
| 96 |
} else { |
| 97 |
feature->name = sdscatprintf(sdsempty(), "%c%c%c%c_%05d", (tag >> 24) & 0xFF, |
| 98 |
(tag >> 16) & 0xFF, (tag >> 8) & 0xff, tag & 0xff, j); |
| 99 |
} |
| 100 |
uint32_t featureOffset = |
| 101 |
featureListOffset + read_16u(data + featureListOffset + 2 + j * 6 + 4); |
| 102 |
|
| 103 |
checkLength(featureOffset + 4); |
| 104 |
tableid_t lookupCount = read_16u(data + featureOffset + 2); |
| 105 |
checkLength(featureOffset + 4 + lookupCount * 2); |
| 106 |
for (tableid_t k = 0; k < lookupCount; k++) { |
| 107 |
tableid_t lookupid = read_16u(data + featureOffset + 4 + k * 2); |
| 108 |
if (lookupid < table->lookups.length) { |
| 109 |
otl_Lookup *lookup = table->lookups.items[lookupid]; |
| 110 |
if (!lookup->name) { |
| 111 |
if (options->glyph_name_prefix) { |
| 112 |
lookup->name = sdscatprintf(sdsempty(), "lookup_%s_%c%c%c%c_%d", |
| 113 |
options->glyph_name_prefix, |
| 114 |
(tag >> 24) & 0xFF, (tag >> 16) & 0xFF, |
| 115 |
(tag >> 8) & 0xff, tag & 0xff, lnk++); |
| 116 |
} else { |
| 117 |
lookup->name = sdscatprintf(sdsempty(), "lookup_%c%c%c%c_%d", |
| 118 |
(tag >> 24) & 0xFF, (tag >> 16) & 0xFF, |
| 119 |
(tag >> 8) & 0xff, tag & 0xff, lnk++); |
| 120 |
} |
| 121 |
} |
| 122 |
otl_iLookupRefList.push(&feature->lookups, lookup); |
| 123 |
} |
| 124 |
} |
| 125 |
otl_iFeatureList.push(&table->features, feature); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
// parse script list |
| 130 |
{ |
| 131 |
tableid_t scriptCount = read_16u(data + scriptListOffset); |
| 132 |
checkLength(scriptListOffset + 2 + 6 * scriptCount); |
| 133 |
|
| 134 |
uint32_t nLanguageCombinations = 0; |
| 135 |
for (tableid_t j = 0; j < scriptCount; j++) { |
| 136 |
uint32_t scriptOffset = |
| 137 |
scriptListOffset + read_16u(data + scriptListOffset + 2 + 6 * j + 4); |
| 138 |
checkLength(scriptOffset + 4); |
| 139 |
|
| 140 |
tableid_t defaultLangSystem = read_16u(data + scriptOffset); |
| 141 |
nLanguageCombinations += |
| 142 |
(defaultLangSystem ? 1 : 0) + read_16u(data + scriptOffset + 2); |
| 143 |
} |
| 144 |
|
| 145 |
for (tableid_t j = 0; j < scriptCount; j++) { |
| 146 |
uint32_t tag = read_32u(data + scriptListOffset + 2 + 6 * j); |
| 147 |
uint32_t scriptOffset = |
| 148 |
scriptListOffset + read_16u(data + scriptListOffset + 2 + 6 * j + 4); |
| 149 |
tableid_t defaultLangSystem = read_16u(data + scriptOffset); |
| 150 |
if (defaultLangSystem) { |
| 151 |
otl_LanguageSystem *lang; |
| 152 |
otl_iLanguageSystem.init(&lang); |
| 153 |
lang->name = sdscatprintf(sdsempty(), "%c%c%c%c%cDFLT", (tag >> 24) & 0xFF, |
| 154 |
(tag >> 16) & 0xFF, (tag >> 8) & 0xff, tag & 0xff, |
| 155 |
SCRIPT_LANGUAGE_SEPARATOR); |
| 156 |
parseLanguage(data, tableLength, scriptOffset + defaultLangSystem, lang, |
| 157 |
&table->features); |
| 158 |
otl_iLangSystemList.push(&table->languages, lang); |
| 159 |
} |
| 160 |
tableid_t langSysCount = read_16u(data + scriptOffset + 2); |
| 161 |
for (tableid_t k = 0; k < langSysCount; k++) { |
| 162 |
uint32_t langTag = read_32u(data + scriptOffset + 4 + 6 * k); |
| 163 |
tableid_t langSys = read_16u(data + scriptOffset + 4 + 6 * k + 4); |
| 164 |
otl_LanguageSystem *lang; |
| 165 |
otl_iLanguageSystem.init(&lang); |
| 166 |
lang->name = |
| 167 |
sdscatprintf(sdsempty(), "%c%c%c%c%c%c%c%c%c", (tag >> 24) & 0xFF, |
| 168 |
(tag >> 16) & 0xFF, (tag >> 8) & 0xff, tag & 0xff, |
| 169 |
SCRIPT_LANGUAGE_SEPARATOR, (langTag >> 24) & 0xFF, |
| 170 |
(langTag >> 16) & 0xFF, (langTag >> 8) & 0xff, langTag & 0xff); |
| 171 |
parseLanguage(data, tableLength, scriptOffset + langSys, lang, &table->features); |
| 172 |
otl_iLangSystemList.push(&table->languages, lang); |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
// name all lookups |
| 178 |
for (tableid_t j = 0; j < table->lookups.length; j++) { |
| 179 |
if (!table->lookups.items[j]->name) { |
| 180 |
if (options->glyph_name_prefix) { |
| 181 |
table->lookups.items[j]->name = |
| 182 |
sdscatprintf(sdsempty(), "lookup_%s_%02x_%d", options->glyph_name_prefix, |
| 183 |
table->lookups.items[j]->type, j); |
| 184 |
} else { |
| 185 |
table->lookups.items[j]->name = |
| 186 |
sdscatprintf(sdsempty(), "lookup_%02x_%d", table->lookups.items[j]->type, j); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
return table; |
| 191 |
FAIL: |
| 192 |
if (table) table_iOTL.free(table); |
| 193 |
return NULL; |
| 194 |
} |
| 195 |
|
| 196 |
static void otfcc_readOtl_lookup(font_file_pointer data, uint32_t tableLength, otl_Lookup *lookup, |
| 197 |
glyphid_t maxGlyphs, const otfcc_Options *options) { |
| 198 |
lookup->flags = read_16u(data + lookup->_offset + 2); |
| 199 |
tableid_t subtableCount = read_16u(data + lookup->_offset + 4); |
| 200 |
if (!subtableCount || tableLength < lookup->_offset + 6 + 2 * subtableCount) { |
| 201 |
lookup->type = otl_type_unknown; |
| 202 |
return; |
| 203 |
} |
| 204 |
for (tableid_t j = 0; j < subtableCount; j++) { |
| 205 |
uint32_t subtableOffset = lookup->_offset + read_16u(data + lookup->_offset + 6 + j * 2); |
| 206 |
otl_Subtable *subtable = otfcc_readOtl_subtable(data, tableLength, subtableOffset, |
| 207 |
lookup->type, maxGlyphs, options); |
| 208 |
otl_iSubtableList.push(&lookup->subtables, subtable); |
| 209 |
} |
| 210 |
if (lookup->type == otl_type_gsub_extend || lookup->type == otl_type_gpos_extend) { |
| 211 |
lookup->type = 0; |
| 212 |
for (tableid_t j = 0; j < lookup->subtables.length; j++) { |
| 213 |
if (lookup->subtables.items[j]) { |
| 214 |
lookup->type = lookup->subtables.items[j]->extend.type; |
| 215 |
break; |
| 216 |
} |
| 217 |
} |
| 218 |
if (lookup->type) { |
| 219 |
for (tableid_t j = 0; j < lookup->subtables.length; j++) { |
| 220 |
if (lookup->subtables.items[j] && |
| 221 |
lookup->subtables.items[j]->extend.type == lookup->type) { |
| 222 |
// this subtable is valid |
| 223 |
otl_Subtable *st = lookup->subtables.items[j]->extend.subtable; |
| 224 |
FREE(lookup->subtables.items[j]); |
| 225 |
lookup->subtables.items[j] = st; |
| 226 |
} else if (lookup->subtables.items[j]) { |
| 227 |
// type mismatch, delete this subtable |
| 228 |
otl_Lookup *temp; |
| 229 |
otl_iLookupPtr.init(&temp); |
| 230 |
temp->type = lookup->subtables.items[j]->extend.type; |
| 231 |
otl_iSubtableList.push(&temp->subtables, |
| 232 |
lookup->subtables.items[j]->extend.subtable); |
| 233 |
DELETE(otfcc_delete_lookup, temp); |
| 234 |
FREE(lookup->subtables.items[j]); |
| 235 |
} |
| 236 |
} |
| 237 |
} else { |
| 238 |
otl_iSubtableList.disposeDependent(&lookup->subtables, lookup); |
| 239 |
return; |
| 240 |
} |
| 241 |
} |
| 242 |
if (lookup->type == otl_type_gsub_context) lookup->type = otl_type_gsub_chaining; |
| 243 |
if (lookup->type == otl_type_gpos_context) lookup->type = otl_type_gpos_chaining; |
| 244 |
} |
| 245 |
|
| 246 |
table_OTL *otfcc_readOtl(otfcc_Packet packet, const otfcc_Options *options, uint32_t tag, |
| 247 |
glyphid_t maxGlyphs) { |
| 248 |
table_OTL *otl = NULL; |
| 249 |
FOR_TABLE(tag, table) { |
| 250 |
font_file_pointer data = table.data; |
| 251 |
uint32_t length = table.length; |
| 252 |
otl = otfcc_readOtl_common(data, length, |
| 253 |
(tag == OTFCC_CHR('G','S','U','B') |
| 254 |
? otl_type_gsub_unknown |
| 255 |
: tag == OTFCC_CHR('G','P','O','S') ? otl_type_gpos_unknown : otl_type_unknown), |
| 256 |
options); |
| 257 |
if (!otl) goto FAIL; |
| 258 |
for (tableid_t j = 0; j < otl->lookups.length; j++) { |
| 259 |
otfcc_readOtl_lookup(data, length, otl->lookups.items[j], maxGlyphs, options); |
| 260 |
} |
| 261 |
return otl; |
| 262 |
FAIL: |
| 263 |
if (otl) table_iOTL.free(otl); |
| 264 |
otl = NULL; |
| 265 |
} |
| 266 |
return NULL; |
| 267 |
} |