| 1 |
#include "private.h" |
| 2 |
|
| 3 |
typedef enum { LOOKUP_ORDER_FORCE, LOOKUP_ORDER_FILE } lookup_order_type; |
| 4 |
|
| 5 |
typedef struct { |
| 6 |
char *name; |
| 7 |
otl_Lookup *lookup; |
| 8 |
UT_hash_handle hh; |
| 9 |
lookup_order_type orderType; |
| 10 |
uint16_t orderVal; |
| 11 |
} lookup_hash; |
| 12 |
|
| 13 |
typedef struct { |
| 14 |
char *name; |
| 15 |
bool alias; |
| 16 |
otl_Feature *feature; |
| 17 |
UT_hash_handle hh; |
| 18 |
} feature_hash; |
| 19 |
|
| 20 |
typedef struct { |
| 21 |
char *name; |
| 22 |
otl_LanguageSystem *language; |
| 23 |
UT_hash_handle hh; |
| 24 |
} language_hash; |
| 25 |
static bool _declareLookupParser(const char *lt, otl_LookupType llt, |
| 26 |
otl_Subtable *(*parser)(const json_value *, |
| 27 |
const otfcc_Options *options), |
| 28 |
json_value *_lookup, char *lookupName, |
| 29 |
const otfcc_Options *options, lookup_hash **lh); |
| 30 |
|
| 31 |
#define LOOKUP_PARSER(llt, parser) \ |
| 32 |
if (!parsed) { \ |
| 33 |
parsed = \ |
| 34 |
_declareLookupParser(tableNames[llt], llt, parser, lookup, lookupName, options, lh); \ |
| 35 |
} |
| 36 |
|
| 37 |
static bool _parse_lookup(json_value *lookup, char *lookupName, const otfcc_Options *options, |
| 38 |
lookup_hash **lh) { |
| 39 |
bool parsed = false; |
| 40 |
LOOKUP_PARSER(otl_type_gsub_single, otl_gsub_parse_single); |
| 41 |
LOOKUP_PARSER(otl_type_gsub_multiple, otl_gsub_parse_multi); |
| 42 |
LOOKUP_PARSER(otl_type_gsub_alternate, otl_gsub_parse_multi); |
| 43 |
LOOKUP_PARSER(otl_type_gsub_ligature, otl_gsub_parse_ligature); |
| 44 |
LOOKUP_PARSER(otl_type_gsub_chaining, otl_parse_chaining); |
| 45 |
LOOKUP_PARSER(otl_type_gsub_reverse, otl_gsub_parse_reverse); |
| 46 |
LOOKUP_PARSER(otl_type_gpos_single, otl_gpos_parse_single); |
| 47 |
LOOKUP_PARSER(otl_type_gpos_pair, otl_gpos_parse_pair); |
| 48 |
LOOKUP_PARSER(otl_type_gpos_cursive, otl_gpos_parse_cursive); |
| 49 |
LOOKUP_PARSER(otl_type_gpos_chaining, otl_parse_chaining); |
| 50 |
LOOKUP_PARSER(otl_type_gpos_markToBase, otl_gpos_parse_markToSingle); |
| 51 |
LOOKUP_PARSER(otl_type_gpos_markToMark, otl_gpos_parse_markToSingle); |
| 52 |
LOOKUP_PARSER(otl_type_gpos_markToLigature, otl_gpos_parse_markToLigature); |
| 53 |
return parsed; |
| 54 |
} |
| 55 |
|
| 56 |
static bool _declareLookupParser(const char *lt, otl_LookupType llt, |
| 57 |
otl_Subtable *(*parser)(const json_value *, |
| 58 |
const otfcc_Options *options), |
| 59 |
json_value *_lookup, char *lookupName, |
| 60 |
const otfcc_Options *options, lookup_hash **lh) { |
| 61 |
|
| 62 |
// detect a valid type field exists |
| 63 |
json_value *type = json_obj_get_type(_lookup, "type", json_string); |
| 64 |
if (!type || strcmp(type->u.string.ptr, lt)) { |
| 65 |
if (!type) logWarning("Lookup %s does not have a valid 'type' field.", lookupName); |
| 66 |
return false; |
| 67 |
} |
| 68 |
// no duplicate lookup has been parsed |
| 69 |
lookup_hash *item = NULL; |
| 70 |
HASH_FIND_STR(*lh, lookupName, item); |
| 71 |
if (item) { |
| 72 |
logWarning("Lookup %s already exists.", lookupName); |
| 73 |
return false; |
| 74 |
} |
| 75 |
// detect valid subtables array |
| 76 |
json_value *_subtables = json_obj_get_type(_lookup, "subtables", json_array); |
| 77 |
if (!_subtables) { |
| 78 |
logWarning("Lookup %s does not have a valid subtable list.", lookupName); |
| 79 |
return false; |
| 80 |
} |
| 81 |
// init this lookup |
| 82 |
otl_Lookup *lookup; |
| 83 |
otl_iLookupPtr.init(&lookup); |
| 84 |
lookup->type = llt; |
| 85 |
lookup->flags = otfcc_parse_flags(json_obj_get(_lookup, "flags"), lookupFlagsLabels); |
| 86 |
uint16_t markAttachmentType = json_obj_getint(_lookup, "markAttachmentType"); |
| 87 |
if (markAttachmentType) { lookup->flags |= markAttachmentType << 8; } |
| 88 |
// start parse subtables |
| 89 |
tableid_t subtableCount = _subtables->u.array.length; |
| 90 |
loggedStep("%s", lookupName) { |
| 91 |
for (tableid_t j = 0; j < subtableCount; j++) { |
| 92 |
json_value *_subtable = _subtables->u.array.values[j]; |
| 93 |
if (_subtable && _subtable->type == json_object) { |
| 94 |
otl_Subtable *_st = parser(_subtable, options); |
| 95 |
otl_iSubtableList.push(&lookup->subtables, _st); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
if (!lookup->subtables.length) { |
| 100 |
logWarning("Lookup %s does not have any subtables.", lookupName); |
| 101 |
otfcc_delete_lookup(lookup); |
| 102 |
return false; |
| 103 |
} |
| 104 |
// we've found a valid lookup, put it into the hash. |
| 105 |
NEW(item); |
| 106 |
item->name = sdsnew(lookupName); |
| 107 |
lookup->name = sdsdup(item->name); |
| 108 |
item->lookup = lookup; |
| 109 |
item->orderType = LOOKUP_ORDER_FILE; |
| 110 |
item->orderVal = HASH_COUNT(*lh); |
| 111 |
HASH_ADD_STR(*lh, name, item); |
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
static lookup_hash *figureOutLookupsFromJSON(json_value *lookups, const otfcc_Options *options) { |
| 116 |
lookup_hash *lh = NULL; |
| 117 |
|
| 118 |
for (uint32_t j = 0; j < lookups->u.object.length; j++) { |
| 119 |
char *lookupName = lookups->u.object.values[j].name; |
| 120 |
if (lookups->u.object.values[j].value->type == json_object) { |
| 121 |
bool parsed = |
| 122 |
_parse_lookup(lookups->u.object.values[j].value, lookupName, options, &lh); |
| 123 |
if (!parsed) { |
| 124 |
logWarning("[OTFCC-fea] Ignoring invalid or unsupported lookup %s.\n", lookupName); |
| 125 |
} |
| 126 |
} else if (lookups->u.object.values[j].value->type == json_string) { |
| 127 |
char *thatname = lookups->u.object.values[j].value->u.string.ptr; |
| 128 |
lookup_hash *s = NULL; |
| 129 |
HASH_FIND_STR(lh, thatname, s); |
| 130 |
if (s) { |
| 131 |
lookup_hash *dup; |
| 132 |
NEW(dup); |
| 133 |
dup->name = sdsnew(lookupName); |
| 134 |
dup->lookup = s->lookup; |
| 135 |
dup->orderType = LOOKUP_ORDER_FILE; |
| 136 |
dup->orderVal = HASH_COUNT(lh); |
| 137 |
HASH_ADD_STR(lh, name, dup); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
return lh; |
| 142 |
} |
| 143 |
|
| 144 |
static void feature_merger_activate(json_value *d, const bool sametag, const char *objtype, |
| 145 |
const otfcc_Options *options) { |
| 146 |
for (uint32_t j = 0; j < d->u.object.length; j++) { |
| 147 |
json_value *jthis = d->u.object.values[j].value; |
| 148 |
char *kthis = d->u.object.values[j].name; |
| 149 |
uint32_t nkthis = d->u.object.values[j].name_length; |
| 150 |
if (jthis->type != json_array && jthis->type != json_object) continue; |
| 151 |
for (uint32_t k = j + 1; k < d->u.object.length; k++) { |
| 152 |
json_value *jthat = d->u.object.values[k].value; |
| 153 |
char *kthat = d->u.object.values[k].name; |
| 154 |
if (json_ident(jthis, jthat) && (sametag ? strncmp(kthis, kthat, 4) == 0 : true)) { |
| 155 |
json_value_free(jthat); |
| 156 |
json_value *v = json_string_new_length(nkthis, kthis); |
| 157 |
v->parent = d; |
| 158 |
d->u.object.values[k].value = v; |
| 159 |
logNotice("[OTFCC-fea] Merged duplicate %s '%s' into '%s'.\n", objtype, kthat, |
| 160 |
kthis); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
static feature_hash *figureOutFeaturesFromJSON(json_value *features, lookup_hash *lh, |
| 167 |
const char *tag, const otfcc_Options *options) { |
| 168 |
feature_hash *fh = NULL; |
| 169 |
// Remove duplicates |
| 170 |
if (options->merge_features) { feature_merger_activate(features, true, "feature", options); } |
| 171 |
// Resolve features |
| 172 |
for (uint32_t j = 0; j < features->u.object.length; j++) { |
| 173 |
char *featureName = features->u.object.values[j].name; |
| 174 |
json_value *_feature = features->u.object.values[j].value; |
| 175 |
if (_feature->type == json_array) { |
| 176 |
otl_LookupRefList al; |
| 177 |
otl_iLookupRefList.init(&al); |
| 178 |
for (tableid_t k = 0; k < _feature->u.array.length; k++) { |
| 179 |
json_value *term = _feature->u.array.values[k]; |
| 180 |
if (term->type != json_string) continue; |
| 181 |
lookup_hash *item = NULL; |
| 182 |
HASH_FIND_STR(lh, term->u.string.ptr, item); |
| 183 |
if (item) { |
| 184 |
otl_iLookupRefList.push(&al, item->lookup); |
| 185 |
} else { |
| 186 |
logWarning("Lookup assignment %s for feature [%s/%s] is missing or invalid.", |
| 187 |
term->u.string.ptr, tag, featureName) |
| 188 |
} |
| 189 |
} |
| 190 |
if (al.length > 0) { |
| 191 |
feature_hash *s = NULL; |
| 192 |
HASH_FIND_STR(fh, featureName, s); |
| 193 |
if (!s) { |
| 194 |
NEW(s); |
| 195 |
s->name = sdsnew(featureName); |
| 196 |
s->alias = false; |
| 197 |
otl_iFeaturePtr.init(&s->feature); |
| 198 |
s->feature->name = sdsdup(s->name); |
| 199 |
otl_iLookupRefList.replace(&s->feature->lookups, al); |
| 200 |
HASH_ADD_STR(fh, name, s); |
| 201 |
} else { |
| 202 |
logWarning("[OTFCC-fea] Duplicate feature for [%s/%s]. This feature will " |
| 203 |
"be ignored.\n", |
| 204 |
tag, featureName); |
| 205 |
otl_iLookupRefList.dispose(&al); |
| 206 |
} |
| 207 |
} else { |
| 208 |
logWarning("[OTFCC-fea] There is no valid lookup " |
| 209 |
"assignments for [%s/%s]. This feature will be " |
| 210 |
"ignored.\n", |
| 211 |
tag, featureName); |
| 212 |
otl_iLookupRefList.dispose(&al); |
| 213 |
} |
| 214 |
} else if (_feature->type == json_string) { |
| 215 |
feature_hash *s = NULL; |
| 216 |
char *target = _feature->u.string.ptr; |
| 217 |
HASH_FIND_STR(fh, target, s); |
| 218 |
if (s) { |
| 219 |
feature_hash *dup; |
| 220 |
NEW(dup); |
| 221 |
dup->alias = true; |
| 222 |
dup->name = sdsnew(featureName); |
| 223 |
dup->feature = s->feature; |
| 224 |
HASH_ADD_STR(fh, name, dup); |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
return fh; |
| 229 |
} |
| 230 |
static bool isValidLanguageName(const char *name, const size_t length) { |
| 231 |
return length == 9 && name[4] == SCRIPT_LANGUAGE_SEPARATOR; |
| 232 |
} |
| 233 |
static language_hash *figureOutLanguagesFromJson(json_value *languages, feature_hash *fh, |
| 234 |
const char *tag, const otfcc_Options *options) { |
| 235 |
language_hash *sh = NULL; |
| 236 |
// languages |
| 237 |
for (uint32_t j = 0; j < languages->u.object.length; j++) { |
| 238 |
char *languageName = languages->u.object.values[j].name; |
| 239 |
size_t languageNameLen = languages->u.object.values[j].name_length; |
| 240 |
json_value *_language = languages->u.object.values[j].value; |
| 241 |
if (isValidLanguageName(languageName, languageNameLen) && _language->type == json_object) { |
| 242 |
otl_Feature *requiredFeature = NULL; |
| 243 |
json_value *_rf = json_obj_get_type(_language, "requiredFeature", json_string); |
| 244 |
if (_rf) { |
| 245 |
// required feature term |
| 246 |
feature_hash *rf = NULL; |
| 247 |
HASH_FIND_STR(fh, _rf->u.string.ptr, rf); |
| 248 |
if (rf) { requiredFeature = rf->feature; } |
| 249 |
} |
| 250 |
otl_FeatureRefList af; |
| 251 |
otl_iFeatureRefList.init(&af); |
| 252 |
json_value *_features = json_obj_get_type(_language, "features", json_array); |
| 253 |
if (_features) { |
| 254 |
for (tableid_t k = 0; k < _features->u.array.length; k++) { |
| 255 |
json_value *term = _features->u.array.values[k]; |
| 256 |
if (term->type == json_string) { |
| 257 |
feature_hash *item = NULL; |
| 258 |
HASH_FIND_STR(fh, term->u.string.ptr, item); |
| 259 |
if (item) { otl_iFeatureRefList.push(&af, item->feature); } |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
if (requiredFeature || (af.length > 0)) { |
| 264 |
language_hash *s = NULL; |
| 265 |
HASH_FIND_STR(sh, languageName, s); |
| 266 |
if (!s) { |
| 267 |
NEW(s); |
| 268 |
s->name = sdsnew(languageName); |
| 269 |
otl_iLanguageSystem.init(&s->language); |
| 270 |
s->language->name = sdsdup(s->name); |
| 271 |
s->language->requiredFeature = requiredFeature; |
| 272 |
otl_iFeatureRefList.replace(&s->language->features, af); |
| 273 |
HASH_ADD_STR(sh, name, s); |
| 274 |
} else { |
| 275 |
logWarning("[OTFCC-fea] Duplicate language item [%s/%s]. This language " |
| 276 |
"term will be ignored.\n", |
| 277 |
tag, languageName); |
| 278 |
otl_iFeatureRefList.dispose(&af); |
| 279 |
} |
| 280 |
} else { |
| 281 |
logWarning("[OTFCC-fea] There is no valid feature " |
| 282 |
"assignments for [%s/%s]. This language term " |
| 283 |
"will be ignored.\n", |
| 284 |
tag, languageName); |
| 285 |
otl_iFeatureRefList.dispose(&af); |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
return sh; |
| 290 |
} |
| 291 |
|
| 292 |
static int by_lookup_order(lookup_hash *a, lookup_hash *b) { |
| 293 |
if (a->orderType == b->orderType) { |
| 294 |
return a->orderVal - b->orderVal; |
| 295 |
} else { |
| 296 |
return a->orderType - b->orderType; |
| 297 |
} |
| 298 |
} |
| 299 |
static int by_feature_name(feature_hash *a, feature_hash *b) { |
| 300 |
return strcmp(a->name, b->name); |
| 301 |
} |
| 302 |
static int by_language_name(language_hash *a, language_hash *b) { |
| 303 |
return strcmp(a->name, b->name); |
| 304 |
} |
| 305 |
table_OTL *otfcc_parseOtl(const json_value *root, const otfcc_Options *options, const char *tag) { |
| 306 |
table_OTL *otl = NULL; |
| 307 |
json_value *table = json_obj_get_type(root, tag, json_object); |
| 308 |
if (!table) goto FAIL; |
| 309 |
otl = table_iOTL.create(); |
| 310 |
json_value *languages = json_obj_get_type(table, "languages", json_object); |
| 311 |
json_value *features = json_obj_get_type(table, "features", json_object); |
| 312 |
json_value *lookups = json_obj_get_type(table, "lookups", json_object); |
| 313 |
if (!languages || !features || !lookups) goto FAIL; |
| 314 |
|
| 315 |
loggedStep("%s", tag) { |
| 316 |
lookup_hash *lh = figureOutLookupsFromJSON(lookups, options); |
| 317 |
json_value *lookupOrder = json_obj_get_type(table, "lookupOrder", json_array); |
| 318 |
if (lookupOrder) { |
| 319 |
for (tableid_t j = 0; j < lookupOrder->u.array.length; j++) { |
| 320 |
json_value *_ln = lookupOrder->u.array.values[j]; |
| 321 |
if (_ln && _ln->type == json_string) { |
| 322 |
lookup_hash *item = NULL; |
| 323 |
HASH_FIND_STR(lh, _ln->u.string.ptr, item); |
| 324 |
if (item) { |
| 325 |
item->orderType = LOOKUP_ORDER_FORCE; |
| 326 |
item->orderVal = j; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
HASH_SORT(lh, by_lookup_order); |
| 332 |
feature_hash *fh = figureOutFeaturesFromJSON(features, lh, tag, options); |
| 333 |
HASH_SORT(fh, by_feature_name); |
| 334 |
language_hash *sh = figureOutLanguagesFromJson(languages, fh, tag, options); |
| 335 |
HASH_SORT(sh, by_language_name); |
| 336 |
if (!HASH_COUNT(lh) || !HASH_COUNT(fh) || !HASH_COUNT(sh)) { |
| 337 |
options->logger->dedent(options->logger); |
| 338 |
goto FAIL; |
| 339 |
} |
| 340 |
|
| 341 |
{ |
| 342 |
lookup_hash *s, *tmp; |
| 343 |
HASH_ITER(hh, lh, s, tmp) { |
| 344 |
otl_iLookupList.push(&otl->lookups, s->lookup); |
| 345 |
HASH_DEL(lh, s); |
| 346 |
sdsfree(s->name); |
| 347 |
FREE(s); |
| 348 |
} |
| 349 |
} |
| 350 |
{ |
| 351 |
feature_hash *s, *tmp; |
| 352 |
HASH_ITER(hh, fh, s, tmp) { |
| 353 |
if (!s->alias) { otl_iFeatureList.push(&otl->features, s->feature); } |
| 354 |
HASH_DEL(fh, s); |
| 355 |
sdsfree(s->name); |
| 356 |
FREE(s); |
| 357 |
} |
| 358 |
} |
| 359 |
{ |
| 360 |
language_hash *s, *tmp; |
| 361 |
HASH_ITER(hh, sh, s, tmp) { |
| 362 |
otl_iLangSystemList.push(&otl->languages, s->language); |
| 363 |
HASH_DEL(sh, s); |
| 364 |
sdsfree(s->name); |
| 365 |
FREE(s); |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
return otl; |
| 370 |
FAIL: |
| 371 |
if (otl) { |
| 372 |
logWarning("[OTFCC-fea] Ignoring invalid or incomplete OTL table %s.\n", tag); |
| 373 |
table_iOTL.free(otl); |
| 374 |
} |
| 375 |
return NULL; |
| 376 |
} |