| 1 |
#include "../chaining.h" |
| 2 |
|
| 3 |
// Chaining substitution classifier |
| 4 |
// We will merge similar subtables. |
| 5 |
|
| 6 |
typedef struct { |
| 7 |
int gid; |
| 8 |
sds gname; |
| 9 |
int cls; |
| 10 |
UT_hash_handle hh; |
| 11 |
} classifier_hash; |
| 12 |
static int by_gid_clsh(classifier_hash *a, classifier_hash *b) { |
| 13 |
return a->gid - b->gid; |
| 14 |
} |
| 15 |
|
| 16 |
static int classCompatible(classifier_hash **h, otl_Coverage *cov, int *past) { |
| 17 |
// checks whether a coverage is compatible to a class hash. |
| 18 |
classifier_hash *s = NULL; |
| 19 |
if (cov->numGlyphs == 0) return 1; |
| 20 |
int gid = cov->glyphs[0].index; |
| 21 |
// check pass |
| 22 |
HASH_FIND_INT(*h, &gid, s); |
| 23 |
if (s) { |
| 24 |
// the coverage has been defined into a class |
| 25 |
classifier_hash *ss, *tmp; |
| 26 |
for (glyphid_t j = 1; j < cov->numGlyphs; j++) { |
| 27 |
int gid = cov->glyphs[j].index; |
| 28 |
HASH_FIND_INT(*h, &gid, ss); |
| 29 |
if (!ss || ss->cls != s->cls) return 0; |
| 30 |
} |
| 31 |
// reverse check: all glyphs classified are there in the coverage |
| 32 |
classifier_hash *revh = NULL; |
| 33 |
for (glyphid_t j = 0; j < cov->numGlyphs; j++) { |
| 34 |
int gid = cov->glyphs[j].index; |
| 35 |
classifier_hash *rss = NULL; |
| 36 |
HASH_FIND_INT(revh, &gid, rss); |
| 37 |
if (!rss) { |
| 38 |
NEW(rss); |
| 39 |
rss->gid = gid; |
| 40 |
rss->gname = cov->glyphs[j].name; |
| 41 |
rss->cls = s->cls; |
| 42 |
HASH_ADD_INT(revh, gid, rss); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
bool allcheck = true; |
| 47 |
foreach_hash(ss, *h) if (ss->cls == s->cls) { |
| 48 |
int gid = ss->gid; |
| 49 |
classifier_hash *rss; |
| 50 |
HASH_FIND_INT(revh, &gid, rss); |
| 51 |
if (!rss) { |
| 52 |
allcheck = false; |
| 53 |
break; |
| 54 |
} |
| 55 |
} |
| 56 |
HASH_ITER(hh, revh, ss, tmp) { |
| 57 |
HASH_DEL(revh, ss); |
| 58 |
FREE(ss); |
| 59 |
} |
| 60 |
return allcheck ? s->cls : 0; |
| 61 |
} else { |
| 62 |
// the coverage is not defined into a class. |
| 63 |
classifier_hash *ss; |
| 64 |
for (glyphid_t j = 1; j < cov->numGlyphs; j++) { |
| 65 |
int gid = cov->glyphs[j].index; |
| 66 |
HASH_FIND_INT(*h, &gid, ss); |
| 67 |
if (ss) return 0; |
| 68 |
} |
| 69 |
for (glyphid_t j = 0; j < cov->numGlyphs; j++) { |
| 70 |
int gid = cov->glyphs[j].index; |
| 71 |
classifier_hash *s = NULL; |
| 72 |
HASH_FIND_INT(*h, &gid, s); |
| 73 |
if (!s) { |
| 74 |
NEW(s); |
| 75 |
s->gid = cov->glyphs[j].index; |
| 76 |
s->gname = cov->glyphs[j].name; |
| 77 |
s->cls = *past + 1; |
| 78 |
HASH_ADD_INT(*h, gid, s); |
| 79 |
} |
| 80 |
} |
| 81 |
*past += 1; |
| 82 |
return 1; |
| 83 |
} |
| 84 |
} |
| 85 |
static otl_ChainingRule *buildRule(otl_ChainingRule *rule, classifier_hash *hb, classifier_hash *hi, |
| 86 |
classifier_hash *hf) { |
| 87 |
otl_ChainingRule *newRule; |
| 88 |
NEW(newRule); |
| 89 |
newRule->matchCount = rule->matchCount; |
| 90 |
newRule->inputBegins = rule->inputBegins; |
| 91 |
newRule->inputEnds = rule->inputEnds; |
| 92 |
NEW(newRule->match, newRule->matchCount); |
| 93 |
for (tableid_t m = 0; m < rule->matchCount; m++) { |
| 94 |
NEW(newRule->match[m]); |
| 95 |
newRule->match[m]->numGlyphs = 1; |
| 96 |
NEW(newRule->match[m]->glyphs); |
| 97 |
if (rule->match[m]->numGlyphs > 0) { |
| 98 |
classifier_hash *h = (m < rule->inputBegins ? hb : m < rule->inputEnds ? hi : hf); |
| 99 |
classifier_hash *s; |
| 100 |
int gid = rule->match[m]->glyphs[0].index; |
| 101 |
HASH_FIND_INT(h, &gid, s); |
| 102 |
newRule->match[m]->glyphs[0] = Handle.fromIndex(s->cls); |
| 103 |
} else { |
| 104 |
newRule->match[m]->glyphs[0] = Handle.fromIndex(0); |
| 105 |
} |
| 106 |
} |
| 107 |
newRule->applyCount = rule->applyCount; |
| 108 |
NEW(newRule->apply, newRule->applyCount); |
| 109 |
for (tableid_t j = 0; j < rule->applyCount; j++) { |
| 110 |
newRule->apply[j].index = rule->apply[j].index; |
| 111 |
newRule->apply[j].lookup = Handle.dup(rule->apply[j].lookup); |
| 112 |
} |
| 113 |
return newRule; |
| 114 |
} |
| 115 |
static otl_ClassDef *toClass(classifier_hash **h) { |
| 116 |
otl_ClassDef *cd = ClassDef.create(); |
| 117 |
classifier_hash *item; |
| 118 |
HASH_SORT(*h, by_gid_clsh); |
| 119 |
foreach_hash(item, *h) { |
| 120 |
ClassDef.push(cd, Handle.fromConsolidated(item->gid, item->gname), item->cls); |
| 121 |
} |
| 122 |
return cd; |
| 123 |
} |
| 124 |
static tableid_t tryClassifyAround(const otl_Lookup *lookup, tableid_t j, |
| 125 |
OUT subtable_chaining **classifiedST) { |
| 126 |
tableid_t compatibleCount = 0; |
| 127 |
classifier_hash *hb = NULL; |
| 128 |
classifier_hash *hi = NULL; |
| 129 |
classifier_hash *hf = NULL; |
| 130 |
// initialize the class hash |
| 131 |
subtable_chaining *subtable0 = &(lookup->subtables.items[j]->chaining); |
| 132 |
int classno_b = 0; |
| 133 |
int classno_i = 0; |
| 134 |
int classno_f = 0; |
| 135 |
|
| 136 |
otl_ChainingRule *rule0 = &subtable0->rule; |
| 137 |
for (tableid_t m = 0; m < rule0->matchCount; m++) { |
| 138 |
int check = 0; |
| 139 |
if (m < rule0->inputBegins) { |
| 140 |
check = classCompatible(&hb, rule0->match[m], &classno_b); |
| 141 |
} else if (m < rule0->inputEnds) { |
| 142 |
check = classCompatible(&hi, rule0->match[m], &classno_i); |
| 143 |
} else { |
| 144 |
check = classCompatible(&hf, rule0->match[m], &classno_f); |
| 145 |
} |
| 146 |
if (!check) { goto FAIL; } |
| 147 |
} |
| 148 |
for (tableid_t k = j + 1; k < lookup->subtables.length; k++) { |
| 149 |
otl_ChainingRule *rule = &lookup->subtables.items[k]->chaining.rule; |
| 150 |
bool allcheck = true; |
| 151 |
for (tableid_t m = 0; m < rule->matchCount; m++) { |
| 152 |
int check = 0; |
| 153 |
if (m < rule->inputBegins) { |
| 154 |
check = classCompatible(&hb, rule->match[m], &classno_b); |
| 155 |
} else if (m < rule->inputEnds) { |
| 156 |
check = classCompatible(&hi, rule->match[m], &classno_i); |
| 157 |
} else { |
| 158 |
check = classCompatible(&hf, rule->match[m], &classno_f); |
| 159 |
} |
| 160 |
if (!check) { |
| 161 |
allcheck = false; |
| 162 |
goto endcheck; |
| 163 |
} |
| 164 |
} |
| 165 |
if (allcheck) { compatibleCount += 1; } |
| 166 |
} |
| 167 |
endcheck: |
| 168 |
if (compatibleCount > 1) { |
| 169 |
// We've found multiple compatible subtables; |
| 170 |
NEW(subtable0); |
| 171 |
subtable0->rulesCount = compatibleCount + 1; |
| 172 |
NEW(subtable0->rules, compatibleCount + 1); |
| 173 |
|
| 174 |
subtable0->rules[0] = buildRule(rule0, hb, hi, hf); |
| 175 |
// write other rules |
| 176 |
tableid_t kk = 1; |
| 177 |
for (tableid_t k = j + 1; k < lookup->subtables.length && kk < compatibleCount + 1; k++) { |
| 178 |
otl_ChainingRule *rule = &lookup->subtables.items[k]->chaining.rule; |
| 179 |
subtable0->rules[kk] = buildRule(rule, hb, hi, hf); |
| 180 |
kk++; |
| 181 |
} |
| 182 |
|
| 183 |
subtable0->type = otl_chaining_classified; |
| 184 |
subtable0->bc = toClass(&hb); |
| 185 |
subtable0->ic = toClass(&hi); |
| 186 |
subtable0->fc = toClass(&hf); |
| 187 |
*classifiedST = subtable0; |
| 188 |
} |
| 189 |
FAIL:; |
| 190 |
if (hb) { |
| 191 |
classifier_hash *s, *tmp; |
| 192 |
HASH_ITER(hh, hb, s, tmp) { |
| 193 |
HASH_DEL(hb, s); |
| 194 |
FREE(s); |
| 195 |
} |
| 196 |
} |
| 197 |
if (hi) { |
| 198 |
classifier_hash *s, *tmp; |
| 199 |
HASH_ITER(hh, hi, s, tmp) { |
| 200 |
HASH_DEL(hi, s); |
| 201 |
FREE(s); |
| 202 |
} |
| 203 |
} |
| 204 |
if (hf) { |
| 205 |
classifier_hash *s, *tmp; |
| 206 |
HASH_ITER(hh, hf, s, tmp) { |
| 207 |
HASH_DEL(hf, s); |
| 208 |
FREE(s); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if (compatibleCount > 1) { |
| 213 |
return compatibleCount; |
| 214 |
} else { |
| 215 |
return 0; |
| 216 |
} |
| 217 |
} |
| 218 |
tableid_t otfcc_classifiedBuildChaining(const otl_Lookup *lookup, |
| 219 |
OUT caryll_Buffer ***subtableBuffers, |
| 220 |
MODIFY size_t *lastOffset) { |
| 221 |
bool isContextual = otfcc_chainingLookupIsContextualLookup(lookup); |
| 222 |
tableid_t subtablesWritten = 0; |
| 223 |
NEW(*subtableBuffers, lookup->subtables.length); |
| 224 |
for (tableid_t j = 0; j < lookup->subtables.length; j++) { |
| 225 |
subtable_chaining *st0 = &(lookup->subtables.items[j]->chaining); |
| 226 |
if (st0->type) continue; |
| 227 |
subtable_chaining *st = st0; |
| 228 |
// Try to classify subtables after j into j |
| 229 |
j += tryClassifyAround(lookup, j, &st); |
| 230 |
caryll_Buffer *buf = isContextual ? otfcc_build_contextual((otl_Subtable *)st) |
| 231 |
: otfcc_build_chaining((otl_Subtable *)st); |
| 232 |
if (st != st0) { iSubtable_chaining.free(st); } |
| 233 |
(*subtableBuffers)[subtablesWritten] = buf; |
| 234 |
*lastOffset += buf->size; |
| 235 |
subtablesWritten += 1; |
| 236 |
} |
| 237 |
return subtablesWritten; |
| 238 |
} |