| 1 |
#include "gpos-pair.h" |
| 2 |
#include "gpos-common.h" |
| 3 |
|
| 4 |
static INLINE void initGposPair(subtable_gpos_pair *subtable) { |
| 5 |
subtable->first = NULL; |
| 6 |
subtable->second = NULL; |
| 7 |
subtable->firstValues = NULL; |
| 8 |
subtable->secondValues = NULL; |
| 9 |
} |
| 10 |
static INLINE void disposeGposPair(subtable_gpos_pair *subtable) { |
| 11 |
if (subtable->firstValues) { |
| 12 |
for (glyphclass_t j = 0; j <= subtable->first->maxclass; j++) { |
| 13 |
FREE(subtable->firstValues[j]); |
| 14 |
} |
| 15 |
FREE(subtable->firstValues); |
| 16 |
} |
| 17 |
if (subtable->secondValues) { |
| 18 |
for (glyphclass_t j = 0; j <= subtable->first->maxclass; j++) { |
| 19 |
FREE(subtable->secondValues[j]); |
| 20 |
} |
| 21 |
FREE(subtable->secondValues); |
| 22 |
} |
| 23 |
DELETE(ClassDef.free, subtable->first); |
| 24 |
DELETE(ClassDef.free, subtable->second); |
| 25 |
} |
| 26 |
|
| 27 |
caryll_standardRefType(subtable_gpos_pair, iSubtable_gpos_pair, initGposPair, disposeGposPair); |
| 28 |
|
| 29 |
typedef struct { |
| 30 |
int gid; |
| 31 |
int cid; |
| 32 |
UT_hash_handle hh; |
| 33 |
} pair_classifier_hash; |
| 34 |
|
| 35 |
otl_Subtable *otl_read_gpos_pair(const font_file_pointer data, uint32_t tableLength, |
| 36 |
uint32_t offset, const glyphid_t maxGlyphs, |
| 37 |
const otfcc_Options *options) { |
| 38 |
subtable_gpos_pair *subtable = iSubtable_gpos_pair.create(); |
| 39 |
|
| 40 |
checkLength(offset + 2); |
| 41 |
uint16_t subtableFormat = read_16u(data + offset); |
| 42 |
if (subtableFormat == 1) { |
| 43 |
// pair adjustment by individuals |
| 44 |
otl_Coverage *cov = Coverage.read(data, tableLength, offset + read_16u(data + offset + 2)); |
| 45 |
NEW(subtable->first); |
| 46 |
subtable->first->numGlyphs = cov->numGlyphs; |
| 47 |
subtable->first->maxclass = cov->numGlyphs - 1; |
| 48 |
subtable->first->glyphs = cov->glyphs; |
| 49 |
NEW(subtable->first->classes, cov->numGlyphs); |
| 50 |
for (glyphid_t j = 0; j < cov->numGlyphs; j++) { |
| 51 |
subtable->first->classes[j] = j; |
| 52 |
} |
| 53 |
FREE(cov); |
| 54 |
|
| 55 |
uint16_t format1 = read_16u(data + offset + 4); |
| 56 |
uint16_t format2 = read_16u(data + offset + 6); |
| 57 |
uint8_t len1 = position_format_length(format1); |
| 58 |
uint8_t len2 = position_format_length(format2); |
| 59 |
|
| 60 |
glyphid_t pairSetCount = read_16u(data + offset + 8); |
| 61 |
if (pairSetCount != subtable->first->numGlyphs) goto FAIL; |
| 62 |
checkLength(offset + 10 + 2 * pairSetCount); |
| 63 |
|
| 64 |
// pass 1: check length |
| 65 |
for (glyphid_t j = 0; j < pairSetCount; j++) { |
| 66 |
uint32_t pairSetOffset = offset + read_16u(data + offset + 10 + 2 * j); |
| 67 |
checkLength(pairSetOffset + 2); |
| 68 |
glyphid_t pairCount = read_16u(data + pairSetOffset); |
| 69 |
checkLength(pairSetOffset + 2 + (2 + len1 + len2) * pairCount); |
| 70 |
} |
| 71 |
// pass 2: make hashtable about second glyphs to construct a classdec |
| 72 |
pair_classifier_hash *h = NULL; |
| 73 |
for (glyphid_t j = 0; j < pairSetCount; j++) { |
| 74 |
uint32_t pairSetOffset = offset + read_16u(data + offset + 10 + 2 * j); |
| 75 |
glyphid_t pairCount = read_16u(data + pairSetOffset); |
| 76 |
for (glyphid_t k = 0; k < pairCount; k++) { |
| 77 |
int second = read_16u(data + pairSetOffset + 2 + (2 + len1 + len2) * k); |
| 78 |
pair_classifier_hash *s; |
| 79 |
HASH_FIND_INT(h, &second, s); |
| 80 |
if (!s) { |
| 81 |
NEW(s); |
| 82 |
s->gid = second; |
| 83 |
s->cid = HASH_COUNT(h) + 1; |
| 84 |
HASH_ADD_INT(h, gid, s); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
// pass 3: make the matrix |
| 89 |
NEW(subtable->second); |
| 90 |
subtable->second->numGlyphs = HASH_COUNT(h); |
| 91 |
subtable->second->maxclass = HASH_COUNT(h); |
| 92 |
NEW(subtable->second->classes, subtable->second->numGlyphs); |
| 93 |
NEW(subtable->second->glyphs, subtable->second->numGlyphs); |
| 94 |
glyphclass_t class2Count = subtable->second->maxclass + 1; |
| 95 |
|
| 96 |
NEW(subtable->firstValues, subtable->first->maxclass + 1); |
| 97 |
NEW(subtable->secondValues, subtable->first->maxclass + 1); |
| 98 |
for (glyphclass_t j = 0; j <= subtable->first->maxclass; j++) { |
| 99 |
NEW(subtable->firstValues[j], class2Count); |
| 100 |
NEW(subtable->secondValues[j], class2Count); |
| 101 |
for (glyphclass_t k = 0; k < class2Count; k++) { |
| 102 |
subtable->firstValues[j][k] = position_zero(); |
| 103 |
subtable->secondValues[j][k] = position_zero(); |
| 104 |
} |
| 105 |
}; |
| 106 |
for (glyphclass_t j = 0; j <= subtable->first->maxclass; j++) { |
| 107 |
uint32_t pairSetOffset = offset + read_16u(data + offset + 10 + 2 * j); |
| 108 |
glyphid_t pairCount = read_16u(data + pairSetOffset); |
| 109 |
for (glyphid_t k = 0; k < pairCount; k++) { |
| 110 |
int second = read_16u(data + pairSetOffset + 2 + (2 + len1 + len2) * k); |
| 111 |
pair_classifier_hash *s; |
| 112 |
HASH_FIND_INT(h, &second, s); |
| 113 |
if (s) { |
| 114 |
subtable->firstValues[j][s->cid] = read_gpos_value( |
| 115 |
data, tableLength, pairSetOffset + 2 + (2 + len1 + len2) * k + 2, format1); |
| 116 |
subtable->secondValues[j][s->cid] = read_gpos_value( |
| 117 |
data, tableLength, pairSetOffset + 2 + (2 + len1 + len2) * k + 2 + len1, |
| 118 |
format2); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
// pass 4: return |
| 123 |
pair_classifier_hash *s, *tmp; |
| 124 |
glyphid_t jj = 0; |
| 125 |
HASH_ITER(hh, h, s, tmp) { |
| 126 |
subtable->second->glyphs[jj] = Handle.fromIndex(s->gid); |
| 127 |
subtable->second->classes[jj] = s->cid; |
| 128 |
jj++; |
| 129 |
HASH_DEL(h, s); |
| 130 |
FREE(s); |
| 131 |
} |
| 132 |
return (otl_Subtable *)subtable; |
| 133 |
} else if (subtableFormat == 2) { |
| 134 |
// pair adjustment by classes |
| 135 |
checkLength(offset + 16); |
| 136 |
uint16_t format1 = read_16u(data + offset + 4); |
| 137 |
uint16_t format2 = read_16u(data + offset + 6); |
| 138 |
uint8_t len1 = position_format_length(format1); |
| 139 |
uint8_t len2 = position_format_length(format2); |
| 140 |
otl_Coverage *cov = Coverage.read(data, tableLength, offset + read_16u(data + offset + 2)); |
| 141 |
subtable->first = ClassDef.read(data, tableLength, offset + read_16u(data + offset + 8)); |
| 142 |
subtable->first = ClassDef.expand(cov, subtable->first); |
| 143 |
DELETE(Coverage.free, cov); |
| 144 |
subtable->second = ClassDef.read(data, tableLength, offset + read_16u(data + offset + 10)); |
| 145 |
if (!subtable->first || !subtable->second) goto FAIL; |
| 146 |
glyphclass_t class1Count = read_16u(data + offset + 12); |
| 147 |
glyphclass_t class2Count = read_16u(data + offset + 14); |
| 148 |
if (subtable->first->maxclass + 1 != class1Count) goto FAIL; |
| 149 |
if (subtable->second->maxclass + 1 != class2Count) goto FAIL; |
| 150 |
|
| 151 |
checkLength(offset + 16 + class1Count * class2Count * (len1 + len2)); |
| 152 |
// read the matrix |
| 153 |
NEW(subtable->firstValues, class1Count); |
| 154 |
NEW(subtable->secondValues, class1Count); |
| 155 |
|
| 156 |
for (glyphclass_t j = 0; j < class1Count; j++) { |
| 157 |
NEW(subtable->firstValues[j], class2Count); |
| 158 |
NEW(subtable->secondValues[j], class2Count); |
| 159 |
for (glyphclass_t k = 0; k < class2Count; k++) { |
| 160 |
subtable->firstValues[j][k] = |
| 161 |
read_gpos_value(data, tableLength, |
| 162 |
offset + 16 + (j * class2Count + k) * (len1 + len2), format1); |
| 163 |
subtable->secondValues[j][k] = read_gpos_value( |
| 164 |
data, tableLength, offset + 16 + (j * class2Count + k) * (len1 + len2) + len1, |
| 165 |
format2); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return (otl_Subtable *)subtable; |
| 170 |
} |
| 171 |
FAIL: |
| 172 |
iSubtable_gpos_pair.free(subtable); |
| 173 |
return NULL; |
| 174 |
} |
| 175 |
|
| 176 |
json_value *otl_gpos_dump_pair(const otl_Subtable *_subtable) { |
| 177 |
const subtable_gpos_pair *subtable = &(_subtable->gpos_pair); |
| 178 |
json_value *st = json_object_new(3); |
| 179 |
json_object_push(st, "first", ClassDef.dump(subtable->first)); |
| 180 |
json_object_push(st, "second", ClassDef.dump(subtable->second)); |
| 181 |
|
| 182 |
json_value *mat = json_array_new(subtable->first->maxclass + 1); |
| 183 |
for (glyphclass_t j = 0; j <= subtable->first->maxclass; j++) { |
| 184 |
json_value *row = json_array_new(subtable->second->maxclass + 1); |
| 185 |
for (glyphclass_t k = 0; k <= subtable->second->maxclass; k++) { |
| 186 |
uint8_t f1 = required_position_format(subtable->firstValues[j][k]); |
| 187 |
uint8_t f2 = required_position_format(subtable->secondValues[j][k]); |
| 188 |
if (f1 | f2) { |
| 189 |
if (f1 == FORMAT_DWIDTH && f2 == 0) { |
| 190 |
// simple kerning |
| 191 |
json_array_push(row, json_new_position(subtable->firstValues[j][k].dWidth)); |
| 192 |
} else { |
| 193 |
json_value *pair = json_object_new(2); |
| 194 |
if (f1) { |
| 195 |
json_object_push(pair, "first", |
| 196 |
gpos_dump_value(subtable->firstValues[j][k])); |
| 197 |
} |
| 198 |
if (f2) { |
| 199 |
json_object_push(pair, "second", |
| 200 |
gpos_dump_value(subtable->secondValues[j][k])); |
| 201 |
} |
| 202 |
json_array_push(row, pair); |
| 203 |
} |
| 204 |
} else { |
| 205 |
json_array_push(row, json_new_position(0)); |
| 206 |
} |
| 207 |
} |
| 208 |
json_array_push(mat, preserialize(row)); |
| 209 |
} |
| 210 |
json_object_push(st, "matrix", mat); |
| 211 |
return st; |
| 212 |
} |
| 213 |
otl_Subtable *otl_gpos_parse_pair(const json_value *_subtable, const otfcc_Options *options) { |
| 214 |
subtable_gpos_pair *subtable = iSubtable_gpos_pair.create(); |
| 215 |
|
| 216 |
json_value *_mat = json_obj_get_type(_subtable, "matrix", json_array); |
| 217 |
subtable->first = ClassDef.parse(json_obj_get_type(_subtable, "first", json_object)); |
| 218 |
subtable->second = ClassDef.parse(json_obj_get_type(_subtable, "second", json_object)); |
| 219 |
if (!_mat || !subtable->first || !subtable->second) goto FAIL; |
| 220 |
|
| 221 |
glyphclass_t class1Count = subtable->first->maxclass + 1; |
| 222 |
glyphclass_t class2Count = subtable->second->maxclass + 1; |
| 223 |
|
| 224 |
NEW(subtable->firstValues, class1Count); |
| 225 |
NEW(subtable->secondValues, class1Count); |
| 226 |
|
| 227 |
for (glyphclass_t j = 0; j < class1Count; j++) { |
| 228 |
NEW(subtable->firstValues[j], class2Count); |
| 229 |
NEW(subtable->secondValues[j], class2Count); |
| 230 |
for (glyphclass_t k = 0; k < class2Count; k++) { |
| 231 |
subtable->firstValues[j][k] = position_zero(); |
| 232 |
subtable->secondValues[j][k] = position_zero(); |
| 233 |
} |
| 234 |
} |
| 235 |
for (glyphclass_t j = 0; j < class1Count && j < _mat->u.array.length; j++) { |
| 236 |
json_value *_row = _mat->u.array.values[j]; |
| 237 |
if (!_row || _row->type != json_array) continue; |
| 238 |
for (glyphclass_t k = 0; k < class2Count && k < _row->u.array.length; k++) { |
| 239 |
json_value *_item = _row->u.array.values[k]; |
| 240 |
if (_item->type == json_integer) { |
| 241 |
subtable->firstValues[j][k].dWidth = _item->u.integer; |
| 242 |
} else if (_item->type == json_double) { |
| 243 |
subtable->firstValues[j][k].dWidth = _item->u.dbl; |
| 244 |
} else if (_item->type == json_object) { |
| 245 |
subtable->firstValues[j][k] = gpos_parse_value(json_obj_get(_item, "first")); |
| 246 |
subtable->secondValues[j][k] = gpos_parse_value(json_obj_get(_item, "second")); |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
return (otl_Subtable *)subtable; |
| 251 |
FAIL: |
| 252 |
iSubtable_gpos_pair.free(subtable); |
| 253 |
return NULL; |
| 254 |
} |
| 255 |
static otl_Coverage *covFromCD(otl_ClassDef *cd) { |
| 256 |
otl_Coverage *cov; |
| 257 |
NEW(cov); |
| 258 |
cov->numGlyphs = cd->numGlyphs; |
| 259 |
NEW(cov->glyphs, cd->numGlyphs); |
| 260 |
for (glyphid_t j = 0; j < cd->numGlyphs; j++) { |
| 261 |
cov->glyphs[j] = Handle.dup(cd->glyphs[j]); |
| 262 |
} |
| 263 |
return cov; |
| 264 |
} |
| 265 |
|
| 266 |
typedef struct { |
| 267 |
glyphid_t gid; |
| 268 |
otl_PositionValue *fv; |
| 269 |
otl_PositionValue *sv; |
| 270 |
} IndividualGposPair; |
| 271 |
|
| 272 |
static int by_pairSecondGlyph(const void *a, const void *b) { |
| 273 |
return ((const IndividualGposPair *)a)->gid - ((const IndividualGposPair *)b)->gid; |
| 274 |
} |
| 275 |
|
| 276 |
static bk_Block *otfcc_build_gpos_pair_individual(const otl_Subtable *_subtable) { |
| 277 |
const subtable_gpos_pair *subtable = &(_subtable->gpos_pair); |
| 278 |
uint16_t format1 = 0; |
| 279 |
uint16_t format2 = 0; |
| 280 |
glyphclass_t class1Count = subtable->first->maxclass + 1; |
| 281 |
glyphclass_t class2Count = subtable->second->maxclass + 1; |
| 282 |
|
| 283 |
for (glyphclass_t j = 0; j < class1Count; j++) { |
| 284 |
for (glyphclass_t k = 0; k < class2Count; k++) { |
| 285 |
format1 |= required_position_format(subtable->firstValues[j][k]); |
| 286 |
format2 |= required_position_format(subtable->secondValues[j][k]); |
| 287 |
} |
| 288 |
} |
| 289 |
glyphid_t *pairCounts; |
| 290 |
NEW(pairCounts, subtable->first->numGlyphs); |
| 291 |
for (glyphid_t j = 0; j < subtable->first->numGlyphs; j++) { |
| 292 |
pairCounts[j] = 0; |
| 293 |
for (glyphid_t k = 0; k < subtable->second->numGlyphs; k++) { |
| 294 |
glyphclass_t c1 = subtable->first->classes[j]; |
| 295 |
glyphclass_t c2 = subtable->second->classes[k]; |
| 296 |
if (required_position_format(subtable->firstValues[c1][c2]) | |
| 297 |
required_position_format(subtable->secondValues[c1][c2])) { |
| 298 |
pairCounts[j] += 1; |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
otl_Coverage *cov = covFromCD(subtable->first); |
| 303 |
Coverage.shrink(cov, true); |
| 304 |
bk_Block *root = bk_new_Block(b16, 1, // PosFormat |
| 305 |
p16, bk_newBlockFromBuffer(Coverage.build(cov)), // Coverage |
| 306 |
b16, format1, // ValueFormat1 |
| 307 |
b16, format2, // ValueFormat2 |
| 308 |
b16, subtable->first->numGlyphs, // PairSetCount |
| 309 |
bkover); |
| 310 |
|
| 311 |
for (glyphid_t j = 0; j < cov->numGlyphs; j++) { |
| 312 |
tableid_t currentPairCount = 0; |
| 313 |
glyphclass_t c1 = 0; |
| 314 |
for (glyphid_t k = 0; k < subtable->first->numGlyphs; k++) { |
| 315 |
if (subtable->first->glyphs[k].index == cov->glyphs[j].index) { |
| 316 |
// The coverage is sorted, not direct correspondence with subtable->first. |
| 317 |
c1 = subtable->first->classes[k]; |
| 318 |
currentPairCount = pairCounts[k]; |
| 319 |
} |
| 320 |
} |
| 321 |
bk_Block *pairSet = bk_new_Block(b16, currentPairCount, // PairValueCount |
| 322 |
bkover); |
| 323 |
IndividualGposPair *pairs; |
| 324 |
NEW_CLEAN_N(pairs, currentPairCount); |
| 325 |
size_t n = 0; |
| 326 |
for (glyphid_t k = 0; k < subtable->second->numGlyphs; k++) { |
| 327 |
glyphclass_t c2 = subtable->second->classes[k]; |
| 328 |
if (required_position_format(subtable->firstValues[c1][c2]) | |
| 329 |
required_position_format(subtable->secondValues[c1][c2])) { |
| 330 |
pairs[n].gid = subtable->second->glyphs[k].index; |
| 331 |
pairs[n].fv = &subtable->firstValues[c1][c2]; |
| 332 |
pairs[n].sv = &subtable->secondValues[c1][c2]; |
| 333 |
n++; |
| 334 |
} |
| 335 |
} |
| 336 |
qsort(pairs, currentPairCount, sizeof(IndividualGposPair), by_pairSecondGlyph); |
| 337 |
for (size_t n = 0; n < currentPairCount; n++) { |
| 338 |
bk_push(pairSet, b16, pairs[n].gid, // SecondGlyph |
| 339 |
bkembed, bk_gpos_value(*(pairs[n].fv), format1), // Value1 |
| 340 |
bkembed, bk_gpos_value(*(pairs[n].sv), format2), // Value2 |
| 341 |
bkover); |
| 342 |
} |
| 343 |
FREE(pairs); |
| 344 |
bk_push(root, p16, pairSet, bkover); |
| 345 |
} |
| 346 |
DELETE(Coverage.free, cov); |
| 347 |
FREE(pairCounts); |
| 348 |
return root; |
| 349 |
} |
| 350 |
static bk_Block *otfcc_build_gpos_pair_classes(const otl_Subtable *_subtable) { |
| 351 |
const subtable_gpos_pair *subtable = &(_subtable->gpos_pair); |
| 352 |
uint16_t format1 = 0; |
| 353 |
uint16_t format2 = 0; |
| 354 |
glyphclass_t class1Count = subtable->first->maxclass + 1; |
| 355 |
glyphclass_t class2Count = subtable->second->maxclass + 1; |
| 356 |
|
| 357 |
for (glyphclass_t j = 0; j < class1Count; j++) { |
| 358 |
for (glyphclass_t k = 0; k < class2Count; k++) { |
| 359 |
format1 |= required_position_format(subtable->firstValues[j][k]); |
| 360 |
format2 |= required_position_format(subtable->secondValues[j][k]); |
| 361 |
} |
| 362 |
} |
| 363 |
otl_Coverage *cov = covFromCD(subtable->first); |
| 364 |
bk_Block *root = |
| 365 |
bk_new_Block(b16, 2, // PosFormat |
| 366 |
p16, bk_newBlockFromBuffer(Coverage.build(cov)), // Coverage |
| 367 |
b16, format1, // ValueFormat1 |
| 368 |
b16, format2, // ValueFormat2 |
| 369 |
p16, bk_newBlockFromBuffer(ClassDef.build(subtable->first)), // ClassDef1 |
| 370 |
p16, bk_newBlockFromBuffer(ClassDef.build(subtable->second)), // ClassDef2 |
| 371 |
b16, class1Count, // Class1Count |
| 372 |
b16, class2Count, // Class2Count |
| 373 |
bkover); |
| 374 |
for (glyphclass_t j = 0; j < class1Count; j++) { |
| 375 |
for (glyphclass_t k = 0; k < class2Count; k++) { |
| 376 |
bk_push(root, bkembed, bk_gpos_value(subtable->firstValues[j][k], format1), // Value1 |
| 377 |
bkembed, bk_gpos_value(subtable->secondValues[j][k], format2), // Value2 |
| 378 |
bkover); |
| 379 |
} |
| 380 |
} |
| 381 |
DELETE(Coverage.free, cov); |
| 382 |
return root; |
| 383 |
} |
| 384 |
caryll_Buffer *otfcc_build_gpos_pair(const otl_Subtable *_subtable, otl_BuildHeuristics heuristics) { |
| 385 |
bk_Block *format1 = otfcc_build_gpos_pair_individual(_subtable); |
| 386 |
bk_Block *format2 = otfcc_build_gpos_pair_classes(_subtable); |
| 387 |
bk_Graph *g1 = bk_newGraphFromRootBlock(format1); |
| 388 |
bk_Graph *g2 = bk_newGraphFromRootBlock(format2); |
| 389 |
bk_minimizeGraph(g1); |
| 390 |
bk_minimizeGraph(g2); |
| 391 |
if (bk_estimateSizeOfGraph(g1) > bk_estimateSizeOfGraph(g2)) { |
| 392 |
// Choose pair adjustment by classes |
| 393 |
bk_delete_Graph(g1); |
| 394 |
bk_untangleGraph(g2); |
| 395 |
caryll_Buffer *buf = bk_build_Graph(g2); |
| 396 |
bk_delete_Graph(g2); |
| 397 |
return buf; |
| 398 |
} else { |
| 399 |
// Choose pair adjustment by individuals |
| 400 |
bk_delete_Graph(g2); |
| 401 |
bk_untangleGraph(g1); |
| 402 |
caryll_Buffer *buf = bk_build_Graph(g1); |
| 403 |
bk_delete_Graph(g1); |
| 404 |
return buf; |
| 405 |
} |
| 406 |
} |