| 1 |
#include "GDEF.h" |
| 2 |
|
| 3 |
#include "otl/private.h" |
| 4 |
|
| 5 |
|
| 6 |
caryll_ElementInterfaceOf(otl_CaretValue) otl_iCaretValue = { |
| 7 |
.init = NULL, |
| 8 |
.copy = NULL, |
| 9 |
.dispose = NULL, |
| 10 |
}; |
| 11 |
caryll_standardVectorImpl(otl_CaretValueList, otl_CaretValue, otl_iCaretValue, otl_iCaretValueList); |
| 12 |
|
| 13 |
static INLINE void initGdefLigCaretRec(otl_CaretValueRecord *v) { |
| 14 |
v->glyph = Handle.empty(); |
| 15 |
otl_iCaretValueList.init(&v->carets); |
| 16 |
} |
| 17 |
static void deleteGdefLigCaretRec(otl_CaretValueRecord *v) { |
| 18 |
Handle.dispose(&v->glyph); |
| 19 |
otl_iCaretValueList.dispose(&v->carets); |
| 20 |
} |
| 21 |
caryll_ElementInterfaceOf(otl_CaretValueRecord) otl_iCaretValueRecord = { |
| 22 |
.init = initGdefLigCaretRec, |
| 23 |
.copy = NULL, |
| 24 |
.dispose = deleteGdefLigCaretRec, |
| 25 |
}; |
| 26 |
caryll_standardVectorImpl(otl_LigCaretTable, otl_CaretValueRecord, otl_iCaretValueRecord, |
| 27 |
otl_iLigCaretTable); |
| 28 |
|
| 29 |
static INLINE void initGDEF(table_GDEF *gdef) { |
| 30 |
gdef->glyphClassDef = NULL; |
| 31 |
gdef->markAttachClassDef = NULL; |
| 32 |
otl_iLigCaretTable.init(&gdef->ligCarets); |
| 33 |
} |
| 34 |
static INLINE void disposeGDEF(table_GDEF *gdef) { |
| 35 |
if (!gdef) return; |
| 36 |
if (gdef->glyphClassDef) ClassDef.free(gdef->glyphClassDef); |
| 37 |
if (gdef->markAttachClassDef) ClassDef.free(gdef->markAttachClassDef); |
| 38 |
otl_iLigCaretTable.dispose(&gdef->ligCarets); |
| 39 |
} |
| 40 |
|
| 41 |
caryll_standardRefType(table_GDEF, table_iGDEF, initGDEF, disposeGDEF); |
| 42 |
|
| 43 |
static otl_CaretValue readCaretValue(const font_file_pointer data, uint32_t tableLength, |
| 44 |
uint32_t offset) { |
| 45 |
otl_CaretValue v; |
| 46 |
v.format = 0; |
| 47 |
v.coordiante = 0; |
| 48 |
v.pointIndex = 0xFFFF; |
| 49 |
checkLength(offset + 4); |
| 50 |
|
| 51 |
v.format = read_16u(data + offset); |
| 52 |
if (v.format == 2) { // attach to glyph point |
| 53 |
v.pointIndex = read_16u(data + offset + 2); |
| 54 |
} else { |
| 55 |
v.coordiante = (pos_t)read_16u(data + offset + 2); |
| 56 |
} |
| 57 |
FAIL: |
| 58 |
return v; |
| 59 |
} |
| 60 |
static otl_CaretValueRecord readLigCaretRecord(const font_file_pointer data, uint32_t tableLength, |
| 61 |
uint32_t offset) { |
| 62 |
otl_CaretValueRecord g; |
| 63 |
otl_iCaretValueRecord.init(&g); |
| 64 |
checkLength(offset + 2); |
| 65 |
shapeid_t caretCount = read_16u(data + offset); |
| 66 |
checkLength(offset + 2 + caretCount * 2); |
| 67 |
|
| 68 |
for (glyphid_t j = 0; j < caretCount; j++) { |
| 69 |
otl_iCaretValueList.push( |
| 70 |
&g.carets, |
| 71 |
readCaretValue(data, tableLength, offset + read_16u(data + offset + 2 + j * 2))); |
| 72 |
} |
| 73 |
FAIL:; |
| 74 |
return g; |
| 75 |
} |
| 76 |
|
| 77 |
table_GDEF *otfcc_readGDEF(const otfcc_Packet packet, const otfcc_Options *options) { |
| 78 |
table_GDEF *gdef = NULL; |
| 79 |
FOR_TABLE(OTFCC_CHR('G','D','E','F'), table) { |
| 80 |
font_file_pointer data = table.data; |
| 81 |
uint32_t tableLength = table.length; |
| 82 |
checkLength(12); |
| 83 |
gdef = table_iGDEF.create(); |
| 84 |
uint16_t classdefOffset = read_16u(data + 4); |
| 85 |
if (classdefOffset) { |
| 86 |
gdef->glyphClassDef = ClassDef.read(data, tableLength, classdefOffset); |
| 87 |
} |
| 88 |
uint16_t ligCaretOffset = read_16u(data + 8); |
| 89 |
if (ligCaretOffset) { |
| 90 |
checkLength(ligCaretOffset + 4); |
| 91 |
otl_Coverage *cov = |
| 92 |
Coverage.read(data, tableLength, ligCaretOffset + read_16u(data + ligCaretOffset)); |
| 93 |
if (!cov || cov->numGlyphs != read_16u(data + ligCaretOffset + 2)) goto FAIL; |
| 94 |
checkLength(ligCaretOffset + 4 + cov->numGlyphs * 2); |
| 95 |
for (glyphid_t j = 0; j < cov->numGlyphs; j++) { |
| 96 |
otl_CaretValueRecord v = readLigCaretRecord( |
| 97 |
data, tableLength, |
| 98 |
ligCaretOffset + read_16u(data + ligCaretOffset + 4 + j * 2)); |
| 99 |
v.glyph = Handle.dup(cov->glyphs[j]); |
| 100 |
otl_iLigCaretTable.push(&gdef->ligCarets, v); |
| 101 |
} |
| 102 |
Coverage.free(cov); |
| 103 |
} |
| 104 |
uint16_t markAttachDefOffset = read_16u(data + 10); |
| 105 |
if (markAttachDefOffset) { |
| 106 |
gdef->markAttachClassDef = ClassDef.read(data, tableLength, markAttachDefOffset); |
| 107 |
} |
| 108 |
return gdef; |
| 109 |
|
| 110 |
FAIL: |
| 111 |
DELETE(table_iGDEF.free, gdef); |
| 112 |
} |
| 113 |
return gdef; |
| 114 |
} |
| 115 |
|
| 116 |
static json_value *dumpGDEFLigCarets(const table_GDEF *gdef) { |
| 117 |
json_value *_carets = json_object_new(gdef->ligCarets.length); |
| 118 |
for (glyphid_t j = 0; j < gdef->ligCarets.length; j++) { |
| 119 |
sds name = gdef->ligCarets.items[j].glyph.name; |
| 120 |
json_value *_record = json_array_new(gdef->ligCarets.items[j].carets.length); |
| 121 |
|
| 122 |
for (glyphid_t k = 0; k < gdef->ligCarets.items[j].carets.length; k++) { |
| 123 |
json_value *_cv = json_object_new(1); |
| 124 |
if (gdef->ligCarets.items[j].carets.items[k].format == 2) { |
| 125 |
json_object_push( |
| 126 |
_cv, "atPoint", |
| 127 |
json_integer_new(gdef->ligCarets.items[j].carets.items[k].pointIndex)); |
| 128 |
} else { |
| 129 |
json_object_push( |
| 130 |
_cv, "at", |
| 131 |
json_integer_new(gdef->ligCarets.items[j].carets.items[k].coordiante)); |
| 132 |
} |
| 133 |
json_array_push(_record, _cv); |
| 134 |
} |
| 135 |
json_object_push(_carets, name, preserialize(_record)); |
| 136 |
} |
| 137 |
return _carets; |
| 138 |
} |
| 139 |
|
| 140 |
void otfcc_dumpGDEF(const table_GDEF *gdef, json_value *root, const otfcc_Options *options) { |
| 141 |
if (!gdef) return; |
| 142 |
loggedStep("GDEF") { |
| 143 |
json_value *_gdef = json_object_new(4); |
| 144 |
if (gdef->glyphClassDef) { |
| 145 |
json_object_push(_gdef, "glyphClassDef", ClassDef.dump(gdef->glyphClassDef)); |
| 146 |
} |
| 147 |
if (gdef->markAttachClassDef) { |
| 148 |
json_object_push(_gdef, "markAttachClassDef", ClassDef.dump(gdef->markAttachClassDef)); |
| 149 |
} |
| 150 |
if (gdef->ligCarets.length) { |
| 151 |
json_object_push(_gdef, "ligCarets", dumpGDEFLigCarets(gdef)); |
| 152 |
} |
| 153 |
json_object_push(root, "GDEF", _gdef); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
static void ligCaretFromJson(const json_value *_carets, otl_LigCaretTable *lc) { |
| 158 |
if (!_carets || _carets->type != json_object) return; |
| 159 |
|
| 160 |
for (glyphid_t j = 0; j < _carets->u.object.length; j++) { |
| 161 |
json_value *a = _carets->u.object.values[j].value; |
| 162 |
if (!a || a->type != json_array) continue; |
| 163 |
otl_CaretValueRecord v; |
| 164 |
otl_iCaretValueRecord.init(&v); |
| 165 |
v.glyph = Handle.fromName( |
| 166 |
sdsnewlen(_carets->u.object.values[j].name, _carets->u.object.values[j].name_length)); |
| 167 |
shapeid_t caretCount = a->u.array.length; |
| 168 |
for (glyphid_t k = 0; k < caretCount; k++) { |
| 169 |
otl_CaretValue caret; |
| 170 |
caret.format = 1; |
| 171 |
caret.coordiante = 0; |
| 172 |
caret.pointIndex = 0xFFFF; |
| 173 |
json_value *_caret = a->u.array.values[k]; |
| 174 |
if (_caret && _caret->type == json_object) { |
| 175 |
if (json_obj_get_type(_caret, "atPoint", json_integer)) { |
| 176 |
caret.format = 2; |
| 177 |
caret.pointIndex = json_obj_getint(_caret, "atPoint"); |
| 178 |
} else { |
| 179 |
caret.coordiante = json_obj_getnum(_caret, "at"); |
| 180 |
} |
| 181 |
} |
| 182 |
otl_iCaretValueList.push(&v.carets, caret); |
| 183 |
} |
| 184 |
otl_iLigCaretTable.push(lc, v); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
table_GDEF *otfcc_parseGDEF(const json_value *root, const otfcc_Options *options) { |
| 189 |
table_GDEF *gdef = NULL; |
| 190 |
json_value *table = NULL; |
| 191 |
if ((table = json_obj_get_type(root, "GDEF", json_object))) { |
| 192 |
loggedStep("GDEF") { |
| 193 |
gdef = table_iGDEF.create(); |
| 194 |
gdef->glyphClassDef = ClassDef.parse(json_obj_get(table, "glyphClassDef")); |
| 195 |
gdef->markAttachClassDef = ClassDef.parse(json_obj_get(table, "markAttachClassDef")); |
| 196 |
ligCaretFromJson(json_obj_get(table, "ligCarets"), &gdef->ligCarets); |
| 197 |
} |
| 198 |
} |
| 199 |
return gdef; |
| 200 |
} |
| 201 |
|
| 202 |
static bk_Block *writeLigCaretRec(otl_CaretValueRecord *cr) { |
| 203 |
bk_Block *bcr = bk_new_Block(b16, cr->carets.length, // CaretCount |
| 204 |
bkover); |
| 205 |
for (glyphid_t j = 0; j < cr->carets.length; j++) { |
| 206 |
bk_push(bcr, p16, |
| 207 |
bk_new_Block(b16, cr->carets.items[j].format, // format |
| 208 |
b16, |
| 209 |
cr->carets.items[j].format == 2 |
| 210 |
? cr->carets.items[j].pointIndex // Point index |
| 211 |
: (int16_t)cr->carets.items[j].coordiante, // X coordinate |
| 212 |
bkover), // CaretValue |
| 213 |
bkover); |
| 214 |
} |
| 215 |
return bcr; |
| 216 |
} |
| 217 |
|
| 218 |
static bk_Block *writeLigCarets(const otl_LigCaretTable *lc) { |
| 219 |
otl_Coverage *cov = Coverage.create(); |
| 220 |
for (glyphid_t j = 0; j < lc->length; j++) { |
| 221 |
Coverage.push(cov, Handle.dup(lc->items[j].glyph)); |
| 222 |
} |
| 223 |
bk_Block *lct = bk_new_Block(p16, bk_newBlockFromBuffer(Coverage.build(cov)), // Coverage |
| 224 |
b16, lc->length, // LigGlyphCount |
| 225 |
bkover); |
| 226 |
for (glyphid_t j = 0; j < lc->length; j++) { |
| 227 |
bk_push(lct, p16, writeLigCaretRec(&(lc->items[j])), bkover); |
| 228 |
} |
| 229 |
Coverage.free(cov); |
| 230 |
return lct; |
| 231 |
} |
| 232 |
|
| 233 |
caryll_Buffer *otfcc_buildGDEF(const table_GDEF *gdef, const otfcc_Options *options) { |
| 234 |
if (!gdef) return NULL; |
| 235 |
bk_Block *bGlyphClassDef = NULL; |
| 236 |
bk_Block *bAttachList = NULL; |
| 237 |
bk_Block *bLigCaretList = NULL; |
| 238 |
bk_Block *bMarkAttachClassDef = NULL; |
| 239 |
|
| 240 |
if (gdef->glyphClassDef) { |
| 241 |
bGlyphClassDef = bk_newBlockFromBuffer(ClassDef.build(gdef->glyphClassDef)); |
| 242 |
} |
| 243 |
if (gdef->ligCarets.length) { bLigCaretList = writeLigCarets(&gdef->ligCarets); } |
| 244 |
if (gdef->markAttachClassDef) { |
| 245 |
bMarkAttachClassDef = bk_newBlockFromBuffer(ClassDef.build(gdef->markAttachClassDef)); |
| 246 |
} |
| 247 |
bk_Block *root = bk_new_Block(b32, 0x10000, // Version |
| 248 |
p16, bGlyphClassDef, // GlyphClassDef |
| 249 |
p16, bAttachList, // AttachList |
| 250 |
p16, bLigCaretList, // LigCaretList |
| 251 |
p16, bMarkAttachClassDef, // MarkAttachClassDef |
| 252 |
bkover); |
| 253 |
return bk_build_Block(root); |
| 254 |
} |