| 1 |
#include "gsub-reverse.h" |
| 2 |
|
| 3 |
static INLINE void initGsubReverse(subtable_gsub_reverse *subtable) { |
| 4 |
subtable->match = NULL; |
| 5 |
subtable->to = NULL; |
| 6 |
} |
| 7 |
static INLINE void disposeGsubReverse(subtable_gsub_reverse *subtable) { |
| 8 |
if (subtable->match) |
| 9 |
for (tableid_t j = 0; j < subtable->matchCount; j++) { |
| 10 |
Coverage.free(subtable->match[j]); |
| 11 |
} |
| 12 |
if (subtable->to) Coverage.free(subtable->to); |
| 13 |
} |
| 14 |
|
| 15 |
caryll_standardRefType(subtable_gsub_reverse, iSubtable_gsub_reverse, initGsubReverse, |
| 16 |
disposeGsubReverse); |
| 17 |
|
| 18 |
static void reverseBacktracks(otl_Coverage **match, tableid_t inputIndex) { |
| 19 |
if (inputIndex > 0) { |
| 20 |
tableid_t start = 0; |
| 21 |
tableid_t end = inputIndex - 1; |
| 22 |
while (end > start) { |
| 23 |
otl_Coverage *tmp = match[start]; |
| 24 |
match[start] = match[end]; |
| 25 |
match[end] = tmp; |
| 26 |
end--, start++; |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
otl_Subtable *otl_read_gsub_reverse(const font_file_pointer data, uint32_t tableLength, |
| 32 |
uint32_t offset, const glyphid_t maxGlyphs, |
| 33 |
const otfcc_Options *options) { |
| 34 |
subtable_gsub_reverse *subtable = iSubtable_gsub_reverse.create(); |
| 35 |
checkLength(offset + 6); |
| 36 |
|
| 37 |
tableid_t nBacktrack = read_16u(data + offset + 4); |
| 38 |
checkLength(offset + 6 + nBacktrack * 2); |
| 39 |
|
| 40 |
tableid_t nForward = read_16u(data + offset + 6 + nBacktrack * 2); |
| 41 |
checkLength(offset + 8 + (nBacktrack + nForward) * 2); |
| 42 |
|
| 43 |
tableid_t nReplacement = read_16u(data + offset + 8 + (nBacktrack + nForward) * 2); |
| 44 |
checkLength(offset + 10 + (nBacktrack + nForward + nReplacement) * 2); |
| 45 |
|
| 46 |
subtable->matchCount = nBacktrack + nForward + 1; |
| 47 |
NEW(subtable->match, subtable->matchCount); |
| 48 |
subtable->inputIndex = nBacktrack; |
| 49 |
|
| 50 |
for (tableid_t j = 0; j < nBacktrack; j++) { |
| 51 |
uint32_t covOffset = offset + read_16u(data + offset + 6 + j * 2); |
| 52 |
subtable->match[j] = Coverage.read(data, tableLength, covOffset); |
| 53 |
} |
| 54 |
{ |
| 55 |
uint32_t covOffset = offset + read_16u(data + offset + 2); |
| 56 |
subtable->match[subtable->inputIndex] = Coverage.read(data, tableLength, covOffset); |
| 57 |
if (nReplacement != subtable->match[subtable->inputIndex]->numGlyphs) goto FAIL; |
| 58 |
} |
| 59 |
for (tableid_t j = 0; j < nForward; j++) { |
| 60 |
uint32_t covOffset = offset + read_16u(data + offset + 8 + nBacktrack * 2 + j * 2); |
| 61 |
subtable->match[nBacktrack + 1 + j] = Coverage.read(data, tableLength, covOffset); |
| 62 |
} |
| 63 |
|
| 64 |
NEW(subtable->to); |
| 65 |
subtable->to->numGlyphs = nReplacement; |
| 66 |
NEW(subtable->to->glyphs, nReplacement); |
| 67 |
for (tableid_t j = 0; j < nReplacement; j++) { |
| 68 |
subtable->to->glyphs[j] = |
| 69 |
Handle.fromIndex(read_16u(data + offset + 10 + (nBacktrack + nForward + j) * 2)); |
| 70 |
} |
| 71 |
reverseBacktracks(subtable->match, subtable->inputIndex); |
| 72 |
return (otl_Subtable *)subtable; |
| 73 |
|
| 74 |
FAIL: |
| 75 |
iSubtable_gsub_reverse.free(subtable); |
| 76 |
return NULL; |
| 77 |
} |
| 78 |
|
| 79 |
json_value *otl_gsub_dump_reverse(const otl_Subtable *_subtable) { |
| 80 |
const subtable_gsub_reverse *subtable = &(_subtable->gsub_reverse); |
| 81 |
json_value *_st = json_object_new(3); |
| 82 |
json_value *_match = json_array_new(subtable->matchCount); |
| 83 |
for (tableid_t j = 0; j < subtable->matchCount; j++) { |
| 84 |
json_array_push(_match, Coverage.dump(subtable->match[j])); |
| 85 |
} |
| 86 |
json_object_push(_st, "match", _match); |
| 87 |
json_object_push(_st, "to", Coverage.dump(subtable->to)); |
| 88 |
json_object_push(_st, "inputIndex", json_integer_new(subtable->inputIndex)); |
| 89 |
return _st; |
| 90 |
} |
| 91 |
|
| 92 |
otl_Subtable *otl_gsub_parse_reverse(const json_value *_subtable, const otfcc_Options *options) { |
| 93 |
json_value *_match = json_obj_get_type(_subtable, "match", json_array); |
| 94 |
json_value *_to = json_obj_get_type(_subtable, "to", json_array); |
| 95 |
if (!_match || !_to) return NULL; |
| 96 |
|
| 97 |
subtable_gsub_reverse *subtable = iSubtable_gsub_reverse.create(); |
| 98 |
|
| 99 |
subtable->matchCount = _match->u.array.length; |
| 100 |
NEW(subtable->match, subtable->matchCount); |
| 101 |
|
| 102 |
subtable->inputIndex = json_obj_getnum_fallback(_subtable, "inputIndex", 0); |
| 103 |
|
| 104 |
for (tableid_t j = 0; j < subtable->matchCount; j++) { |
| 105 |
subtable->match[j] = Coverage.parse(_match->u.array.values[j]); |
| 106 |
} |
| 107 |
subtable->to = Coverage.parse(_to); |
| 108 |
return (otl_Subtable *)subtable; |
| 109 |
} |
| 110 |
|
| 111 |
caryll_Buffer *otfcc_build_gsub_reverse(const otl_Subtable *_subtable, otl_BuildHeuristics heuristics) { |
| 112 |
const subtable_gsub_reverse *subtable = &(_subtable->gsub_reverse); |
| 113 |
reverseBacktracks(subtable->match, subtable->inputIndex); |
| 114 |
|
| 115 |
bk_Block *root = bk_new_Block(b16, 1, // format |
| 116 |
p16, bk_newBlockFromBuffer(Coverage.build( |
| 117 |
subtable->match[subtable->inputIndex])), // coverage |
| 118 |
bkover); |
| 119 |
bk_push(root, b16, subtable->inputIndex, bkover); |
| 120 |
for (tableid_t j = 0; j < subtable->inputIndex; j++) { |
| 121 |
bk_push(root, p16, bk_newBlockFromBuffer(Coverage.build(subtable->match[j])), bkover); |
| 122 |
} |
| 123 |
bk_push(root, b16, subtable->matchCount - subtable->inputIndex - 1, bkover); |
| 124 |
for (tableid_t j = subtable->inputIndex + 1; j < subtable->matchCount; j++) { |
| 125 |
bk_push(root, p16, bk_newBlockFromBuffer(Coverage.build(subtable->match[j])), bkover); |
| 126 |
} |
| 127 |
bk_push(root, b16, subtable->to->numGlyphs, bkover); |
| 128 |
for (tableid_t j = 0; j < subtable->to->numGlyphs; j++) { |
| 129 |
bk_push(root, b16, subtable->to->glyphs[j].index, bkover); |
| 130 |
} |
| 131 |
|
| 132 |
return bk_build_Block(root); |
| 133 |
} |