| 1 |
#include "gpos-single.h" |
| 2 |
#include "gpos-common.h" |
| 3 |
|
| 4 |
static void deleteGposSingleEntry(otl_GposSingleEntry *entry) { |
| 5 |
Handle.dispose(&entry->target); |
| 6 |
} |
| 7 |
|
| 8 |
static caryll_ElementInterface(otl_GposSingleEntry) gss_typeinfo = { |
| 9 |
.init = NULL, .copy = NULL, .dispose = deleteGposSingleEntry}; |
| 10 |
|
| 11 |
caryll_standardVectorImpl(subtable_gpos_single, otl_GposSingleEntry, gss_typeinfo, |
| 12 |
iSubtable_gpos_single); |
| 13 |
|
| 14 |
otl_Subtable *otl_read_gpos_single(const font_file_pointer data, uint32_t tableLength, |
| 15 |
uint32_t offset, const glyphid_t maxGlyphs, |
| 16 |
const otfcc_Options *options) { |
| 17 |
subtable_gpos_single *subtable = iSubtable_gpos_single.create(); |
| 18 |
otl_Coverage *targets = NULL; |
| 19 |
|
| 20 |
checkLength(offset + 6); |
| 21 |
|
| 22 |
uint16_t subtableFormat = read_16u(data + offset); |
| 23 |
targets = Coverage.read(data, tableLength, offset + read_16u(data + offset + 2)); |
| 24 |
if (!targets || targets->numGlyphs == 0) goto FAIL; |
| 25 |
|
| 26 |
if (subtableFormat == 1) { |
| 27 |
otl_PositionValue v = |
| 28 |
read_gpos_value(data, tableLength, offset + 6, read_16u(data + offset + 4)); |
| 29 |
for (glyphid_t j = 0; j < targets->numGlyphs; j++) { |
| 30 |
iSubtable_gpos_single.push(subtable, |
| 31 |
((otl_GposSingleEntry){ |
| 32 |
.target = Handle.dup(targets->glyphs[j]), .value = v, |
| 33 |
})); |
| 34 |
} |
| 35 |
} else { |
| 36 |
uint16_t valueFormat = read_16u(data + offset + 4); |
| 37 |
uint16_t valueCount = read_16u(data + offset + 6); |
| 38 |
checkLength(offset + 8 + position_format_length(valueFormat) * valueCount); |
| 39 |
if (valueCount != targets->numGlyphs) goto FAIL; |
| 40 |
|
| 41 |
for (glyphid_t j = 0; j < targets->numGlyphs; j++) { |
| 42 |
iSubtable_gpos_single.push( |
| 43 |
subtable, |
| 44 |
((otl_GposSingleEntry){ |
| 45 |
.target = Handle.dup(targets->glyphs[j]), |
| 46 |
.value = read_gpos_value(data, tableLength, |
| 47 |
offset + 8 + j * position_format_length(valueFormat), |
| 48 |
valueFormat), |
| 49 |
})); |
| 50 |
} |
| 51 |
} |
| 52 |
if (targets) Coverage.free(targets); |
| 53 |
return (otl_Subtable *)subtable; |
| 54 |
|
| 55 |
FAIL: |
| 56 |
if (targets) Coverage.free(targets); |
| 57 |
iSubtable_gpos_single.free(subtable); |
| 58 |
return NULL; |
| 59 |
} |
| 60 |
|
| 61 |
json_value *otl_gpos_dump_single(const otl_Subtable *_subtable) { |
| 62 |
const subtable_gpos_single *subtable = &(_subtable->gpos_single); |
| 63 |
json_value *st = json_object_new(subtable->length); |
| 64 |
for (glyphid_t j = 0; j < subtable->length; j++) { |
| 65 |
json_object_push(st, subtable->items[j].target.name, |
| 66 |
gpos_dump_value(subtable->items[j].value)); |
| 67 |
} |
| 68 |
return st; |
| 69 |
} |
| 70 |
otl_Subtable *otl_gpos_parse_single(const json_value *_subtable, const otfcc_Options *options) { |
| 71 |
subtable_gpos_single *subtable = iSubtable_gpos_single.create(); |
| 72 |
for (glyphid_t j = 0; j < _subtable->u.object.length; j++) { |
| 73 |
if (_subtable->u.object.values[j].value && |
| 74 |
_subtable->u.object.values[j].value->type == json_object) { |
| 75 |
sds gname = sdsnewlen(_subtable->u.object.values[j].name, |
| 76 |
_subtable->u.object.values[j].name_length); |
| 77 |
iSubtable_gpos_single.push( |
| 78 |
subtable, ((otl_GposSingleEntry){ |
| 79 |
.target = Handle.fromName(gname), |
| 80 |
.value = gpos_parse_value(_subtable->u.object.values[j].value), |
| 81 |
})); |
| 82 |
} |
| 83 |
} |
| 84 |
return (otl_Subtable *)subtable; |
| 85 |
} |
| 86 |
|
| 87 |
caryll_Buffer *otfcc_build_gpos_single(const otl_Subtable *_subtable, otl_BuildHeuristics heuristics) { |
| 88 |
const subtable_gpos_single *subtable = &(_subtable->gpos_single); |
| 89 |
bool isConst = subtable->length > 0; |
| 90 |
uint16_t format = 0; |
| 91 |
if (subtable->length > 0) { |
| 92 |
for (glyphid_t j = 0; j < subtable->length; j++) { |
| 93 |
isConst = isConst && (subtable->items[j].value.dx == subtable->items[0].value.dx) && |
| 94 |
(subtable->items[j].value.dy == subtable->items[0].value.dy) && |
| 95 |
(subtable->items[j].value.dWidth == subtable->items[0].value.dWidth) && |
| 96 |
(subtable->items[j].value.dHeight == subtable->items[0].value.dHeight); |
| 97 |
format |= required_position_format(subtable->items[j].value); |
| 98 |
} |
| 99 |
} |
| 100 |
otl_Coverage *cov = Coverage.create(); |
| 101 |
for (glyphid_t j = 0; j < subtable->length; j++) { |
| 102 |
Coverage.push(cov, Handle.dup(subtable->items[j].target)); |
| 103 |
} |
| 104 |
|
| 105 |
caryll_Buffer *coverageBuf = Coverage.build(cov); |
| 106 |
|
| 107 |
if (isConst) { |
| 108 |
bk_Block *b = |
| 109 |
(bk_new_Block(b16, 1, // Format |
| 110 |
p16, bk_newBlockFromBuffer(coverageBuf), // coverage |
| 111 |
b16, format, // format |
| 112 |
bkembed, bk_gpos_value(subtable->items[0].value, format), // value |
| 113 |
bkover)); |
| 114 |
Coverage.free(cov); |
| 115 |
return bk_build_Block(b); |
| 116 |
} else { |
| 117 |
bk_Block *b = bk_new_Block(b16, 2, // Format |
| 118 |
p16, bk_newBlockFromBuffer(coverageBuf), // coverage |
| 119 |
b16, format, // format |
| 120 |
b16, subtable->length, // quantity |
| 121 |
bkover); |
| 122 |
for (glyphid_t k = 0; k < subtable->length; k++) { |
| 123 |
bk_push(b, bkembed, bk_gpos_value(subtable->items[k].value, format), // value |
| 124 |
bkover); |
| 125 |
} |
| 126 |
Coverage.free(cov); |
| 127 |
return bk_build_Block(b); |
| 128 |
} |
| 129 |
} |