| 1 |
#include "gsub-ligature.h" |
| 2 |
|
| 3 |
static void deleteGsubLigatureEntry(otl_GsubLigatureEntry *entry) { |
| 4 |
Handle.dispose(&entry->to); |
| 5 |
DELETE(Coverage.free, entry->from); |
| 6 |
} |
| 7 |
static caryll_ElementInterface(otl_GsubLigatureEntry) gss_typeinfo = { |
| 8 |
.init = NULL, .copy = NULL, .dispose = deleteGsubLigatureEntry}; |
| 9 |
|
| 10 |
caryll_standardVectorImpl(subtable_gsub_ligature, otl_GsubLigatureEntry, gss_typeinfo, |
| 11 |
iSubtable_gsub_ligature); |
| 12 |
|
| 13 |
otl_Subtable *otl_read_gsub_ligature(const font_file_pointer data, uint32_t tableLength, |
| 14 |
uint32_t offset, const glyphid_t maxGlyphs, |
| 15 |
const otfcc_Options *options) { |
| 16 |
subtable_gsub_ligature *subtable = iSubtable_gsub_ligature.create(); |
| 17 |
checkLength(offset + 6); |
| 18 |
|
| 19 |
otl_Coverage *startCoverage = |
| 20 |
Coverage.read(data, tableLength, offset + read_16u(data + offset + 2)); |
| 21 |
if (!startCoverage) goto FAIL; |
| 22 |
glyphid_t setCount = read_16u(data + offset + 4); |
| 23 |
if (setCount != startCoverage->numGlyphs) goto FAIL; |
| 24 |
checkLength(offset + 6 + setCount * 2); |
| 25 |
|
| 26 |
uint32_t ligatureCount = 0; |
| 27 |
for (glyphid_t j = 0; j < setCount; j++) { |
| 28 |
uint32_t setOffset = offset + read_16u(data + offset + 6 + j * 2); |
| 29 |
checkLength(setOffset + 2); |
| 30 |
ligatureCount += read_16u(data + setOffset); |
| 31 |
checkLength(setOffset + 2 + read_16u(data + setOffset) * 2); |
| 32 |
} |
| 33 |
|
| 34 |
for (glyphid_t j = 0; j < setCount; j++) { |
| 35 |
uint32_t setOffset = offset + read_16u(data + offset + 6 + j * 2); |
| 36 |
glyphid_t lc = read_16u(data + setOffset); |
| 37 |
for (glyphid_t k = 0; k < lc; k++) { |
| 38 |
uint32_t ligOffset = setOffset + read_16u(data + setOffset + 2 + k * 2); |
| 39 |
checkLength(ligOffset + 4); |
| 40 |
glyphid_t ligComponents = read_16u(data + ligOffset + 2); |
| 41 |
checkLength(ligOffset + 2 + ligComponents * 2); |
| 42 |
|
| 43 |
otl_Coverage *cov = Coverage.create(); |
| 44 |
Coverage.push(cov, Handle.fromIndex(startCoverage->glyphs[j].index)); |
| 45 |
for (glyphid_t m = 1; m < ligComponents; m++) { |
| 46 |
Coverage.push(cov, Handle.fromIndex(read_16u(data + ligOffset + 2 + m * 2))); |
| 47 |
} |
| 48 |
iSubtable_gsub_ligature.push( |
| 49 |
subtable, ((otl_GsubLigatureEntry){ |
| 50 |
.from = cov, .to = Handle.fromIndex(read_16u(data + ligOffset)), |
| 51 |
})); |
| 52 |
} |
| 53 |
} |
| 54 |
Coverage.free(startCoverage); |
| 55 |
return (otl_Subtable *)subtable; |
| 56 |
FAIL: |
| 57 |
iSubtable_gsub_ligature.free(subtable); |
| 58 |
return NULL; |
| 59 |
} |
| 60 |
|
| 61 |
json_value *otl_gsub_dump_ligature(const otl_Subtable *_subtable) { |
| 62 |
const subtable_gsub_ligature *subtable = &(_subtable->gsub_ligature); |
| 63 |
json_value *st = json_array_new(subtable->length); |
| 64 |
for (glyphid_t j = 0; j < subtable->length; j++) { |
| 65 |
json_value *entry = json_object_new(2); |
| 66 |
json_object_push(entry, "from", Coverage.dump(subtable->items[j].from)); |
| 67 |
json_object_push(entry, "to", |
| 68 |
json_string_new_length((uint32_t)sdslen(subtable->items[j].to.name), |
| 69 |
subtable->items[j].to.name)); |
| 70 |
json_array_push(st, preserialize(entry)); |
| 71 |
} |
| 72 |
json_value *ret = json_object_new(1); |
| 73 |
json_object_push(ret, "substitutions", st); |
| 74 |
return ret; |
| 75 |
} |
| 76 |
|
| 77 |
otl_Subtable *otl_gsub_parse_ligature(const json_value *_subtable, const otfcc_Options *options) { |
| 78 |
if (json_obj_get_type(_subtable, "substitutions", json_array)) { |
| 79 |
_subtable = json_obj_get_type(_subtable, "substitutions", json_array); |
| 80 |
|
| 81 |
subtable_gsub_ligature *st = iSubtable_gsub_ligature.create(); |
| 82 |
glyphid_t n = _subtable->u.array.length; |
| 83 |
|
| 84 |
for (glyphid_t k = 0; k < n; k++) { |
| 85 |
json_value *entry = _subtable->u.array.values[k]; |
| 86 |
json_value *_from = json_obj_get_type(entry, "from", json_array); |
| 87 |
json_value *_to = json_obj_get_type(entry, "to", json_string); |
| 88 |
if (!_from || !_to) continue; |
| 89 |
iSubtable_gsub_ligature.push( |
| 90 |
st, ((otl_GsubLigatureEntry){ |
| 91 |
.to = Handle.fromName(sdsnewlen(_to->u.string.ptr, _to->u.string.length)), |
| 92 |
.from = Coverage.parse(_from), |
| 93 |
})); |
| 94 |
} |
| 95 |
return (otl_Subtable *)st; |
| 96 |
} else { |
| 97 |
subtable_gsub_ligature *st = iSubtable_gsub_ligature.create(); |
| 98 |
glyphid_t n = _subtable->u.array.length; |
| 99 |
|
| 100 |
for (glyphid_t k = 0; k < n; k++) { |
| 101 |
json_value *_from = _subtable->u.object.values[k].value; |
| 102 |
if (!_from || _from->type != json_array) continue; |
| 103 |
iSubtable_gsub_ligature.push( |
| 104 |
st, ((otl_GsubLigatureEntry){ |
| 105 |
.to = Handle.fromName(sdsnewlen(_subtable->u.object.values[k].name, |
| 106 |
_subtable->u.object.values[k].name_length)), |
| 107 |
.from = Coverage.parse(_from), |
| 108 |
})); |
| 109 |
} |
| 110 |
return (otl_Subtable *)st; |
| 111 |
} |
| 112 |
return NULL; |
| 113 |
} |
| 114 |
|
| 115 |
typedef struct { |
| 116 |
int gid; |
| 117 |
UT_hash_handle hh; |
| 118 |
} ligature_aggerator; |
| 119 |
static int by_gid(ligature_aggerator *a, ligature_aggerator *b) { |
| 120 |
return a->gid - b->gid; |
| 121 |
} |
| 122 |
|
| 123 |
caryll_Buffer *otfcc_build_gsub_ligature_subtable(const otl_Subtable *_subtable, otl_BuildHeuristics heuristics) { |
| 124 |
const subtable_gsub_ligature *subtable = &(_subtable->gsub_ligature); |
| 125 |
|
| 126 |
ligature_aggerator *h = NULL, *s, *tmp; |
| 127 |
glyphid_t nLigatures = subtable->length; |
| 128 |
for (glyphid_t j = 0; j < nLigatures; j++) { |
| 129 |
int sgid = subtable->items[j].from->glyphs[0].index; |
| 130 |
HASH_FIND_INT(h, &sgid, s); |
| 131 |
if (!s) { |
| 132 |
NEW(s); |
| 133 |
s->gid = sgid; |
| 134 |
HASH_ADD_INT(h, gid, s); |
| 135 |
} |
| 136 |
} |
| 137 |
HASH_SORT(h, by_gid); |
| 138 |
|
| 139 |
otl_Coverage *startcov = Coverage.create(); |
| 140 |
|
| 141 |
foreach_hash(s, h) { |
| 142 |
Coverage.push(startcov, Handle.fromIndex(s->gid)); |
| 143 |
} |
| 144 |
|
| 145 |
bk_Block *root = bk_new_Block(b16, 1, // format |
| 146 |
p16, bk_newBlockFromBuffer(Coverage.build(startcov)), // coverage |
| 147 |
b16, startcov->numGlyphs, // LigSetCount |
| 148 |
bkover); |
| 149 |
|
| 150 |
foreach_hash(s, h) { |
| 151 |
glyphid_t nLigsHere = 0; |
| 152 |
for (glyphid_t j = 0; j < nLigatures; j++) |
| 153 |
if (subtable->items[j].from->glyphs[0].index == s->gid) nLigsHere++; |
| 154 |
bk_Block *ligset = bk_new_Block(b16, nLigsHere, bkover); |
| 155 |
|
| 156 |
for (glyphid_t j = 0; j < nLigatures; j++) { |
| 157 |
if (subtable->items[j].from->glyphs[0].index == s->gid) { |
| 158 |
bk_Block *ligdef = |
| 159 |
bk_new_Block(b16, subtable->items[j].to.index, // ligGlyph |
| 160 |
b16, subtable->items[j].from->numGlyphs, // compCount |
| 161 |
bkover); |
| 162 |
for (glyphid_t m = 1; m < subtable->items[j].from->numGlyphs; m++) { |
| 163 |
bk_push(ligdef, b16, subtable->items[j].from->glyphs[m].index, bkover); |
| 164 |
} |
| 165 |
bk_push(ligset, p16, ligdef, bkover); |
| 166 |
} |
| 167 |
} |
| 168 |
bk_push(root, p16, ligset, bkover); |
| 169 |
} |
| 170 |
|
| 171 |
Coverage.free(startcov); |
| 172 |
HASH_ITER(hh, h, s, tmp) { |
| 173 |
HASH_DEL(h, s); |
| 174 |
FREE(s); |
| 175 |
} |
| 176 |
return bk_build_Block(root); |
| 177 |
} |