| 1 |
#include "gsub-single.h" |
| 2 |
|
| 3 |
static void gss_entry_ctor(MODIFY otl_GsubSingleEntry *entry) { |
| 4 |
entry->from = Handle.empty(); |
| 5 |
entry->to = Handle.empty(); |
| 6 |
} |
| 7 |
static void gss_entry_copyctor(MODIFY otl_GsubSingleEntry *dst, |
| 8 |
COPY const otl_GsubSingleEntry *src) { |
| 9 |
dst->from = Handle.dup(src->from); |
| 10 |
dst->to = Handle.dup(src->to); |
| 11 |
} |
| 12 |
static void gss_entry_dtor(MODIFY otl_GsubSingleEntry *entry) { |
| 13 |
Handle.dispose(&entry->from); |
| 14 |
Handle.dispose(&entry->to); |
| 15 |
} |
| 16 |
|
| 17 |
static caryll_ElementInterface(otl_GsubSingleEntry) gss_typeinfo = { |
| 18 |
.init = gss_entry_ctor, .copy = gss_entry_copyctor, .dispose = gss_entry_dtor}; |
| 19 |
|
| 20 |
caryll_standardVectorImpl(subtable_gsub_single, otl_GsubSingleEntry, gss_typeinfo, |
| 21 |
iSubtable_gsub_single); |
| 22 |
|
| 23 |
otl_Subtable *otl_read_gsub_single(const font_file_pointer data, uint32_t tableLength, |
| 24 |
uint32_t subtableOffset, const glyphid_t maxGlyphs, |
| 25 |
const otfcc_Options *options) { |
| 26 |
subtable_gsub_single *subtable = iSubtable_gsub_single.create(); |
| 27 |
otl_Coverage *from = NULL; |
| 28 |
otl_Coverage *to = NULL; |
| 29 |
if (tableLength < subtableOffset + 6) goto FAIL; |
| 30 |
|
| 31 |
uint16_t subtableFormat = read_16u(data + subtableOffset); |
| 32 |
from = Coverage.read(data, tableLength, subtableOffset + read_16u(data + subtableOffset + 2)); |
| 33 |
if (!from || from->numGlyphs == 0) goto FAIL; |
| 34 |
|
| 35 |
if (subtableFormat == 1) { |
| 36 |
NEW(to); |
| 37 |
to->numGlyphs = from->numGlyphs; |
| 38 |
NEW(to->glyphs, to->numGlyphs); |
| 39 |
|
| 40 |
uint16_t delta = read_16u(data + subtableOffset + 4); |
| 41 |
for (glyphid_t j = 0; j < from->numGlyphs; j++) { |
| 42 |
to->glyphs[j] = Handle.fromIndex(from->glyphs[j].index + delta); |
| 43 |
} |
| 44 |
} else { |
| 45 |
glyphid_t toglyphs = read_16u(data + subtableOffset + 4); |
| 46 |
if (tableLength < subtableOffset + 6 + toglyphs * 2 || toglyphs != from->numGlyphs) |
| 47 |
goto FAIL; |
| 48 |
NEW(to); |
| 49 |
to->numGlyphs = toglyphs; |
| 50 |
NEW(to->glyphs, to->numGlyphs); |
| 51 |
|
| 52 |
for (glyphid_t j = 0; j < to->numGlyphs; j++) { |
| 53 |
to->glyphs[j] = Handle.fromIndex(read_16u(data + subtableOffset + 6 + j * 2)); |
| 54 |
} |
| 55 |
} |
| 56 |
goto OK; |
| 57 |
FAIL: |
| 58 |
iSubtable_gsub_single.free(subtable); |
| 59 |
if (from) Coverage.free(from); |
| 60 |
if (to) Coverage.free(to); |
| 61 |
return NULL; |
| 62 |
OK: |
| 63 |
for (glyphid_t j = 0; j < from->numGlyphs; j++) { |
| 64 |
iSubtable_gsub_single.push(subtable, ((otl_GsubSingleEntry){ |
| 65 |
.from = Handle.dup(from->glyphs[j]), // from |
| 66 |
.to = Handle.dup(to->glyphs[j]), // to |
| 67 |
})); |
| 68 |
} |
| 69 |
if (from) Coverage.free(from); |
| 70 |
if (to) Coverage.free(to); |
| 71 |
return (otl_Subtable *)subtable; |
| 72 |
} |
| 73 |
|
| 74 |
json_value *otl_gsub_dump_single(const otl_Subtable *_subtable) { |
| 75 |
const subtable_gsub_single *subtable = &(_subtable->gsub_single); |
| 76 |
json_value *st = json_object_new(subtable->length); |
| 77 |
for (size_t j = 0; j < subtable->length; j++) { |
| 78 |
json_object_push(st, subtable->items[j].from.name, |
| 79 |
json_string_new(subtable->items[j].to.name)); |
| 80 |
} |
| 81 |
return st; |
| 82 |
} |
| 83 |
|
| 84 |
otl_Subtable *otl_gsub_parse_single(const json_value *_subtable, const otfcc_Options *options) { |
| 85 |
subtable_gsub_single *subtable = iSubtable_gsub_single.create(); |
| 86 |
for (glyphid_t j = 0; j < _subtable->u.object.length; j++) { |
| 87 |
if (_subtable->u.object.values[j].value && |
| 88 |
_subtable->u.object.values[j].value->type == json_string) { |
| 89 |
glyph_handle from = Handle.fromName(sdsnewlen( |
| 90 |
_subtable->u.object.values[j].name, _subtable->u.object.values[j].name_length)); |
| 91 |
glyph_handle to = |
| 92 |
Handle.fromName(sdsnewlen(_subtable->u.object.values[j].value->u.string.ptr, |
| 93 |
_subtable->u.object.values[j].value->u.string.length)); |
| 94 |
iSubtable_gsub_single.push(subtable, ((otl_GsubSingleEntry){.from = from, .to = to})); |
| 95 |
} |
| 96 |
} |
| 97 |
return (otl_Subtable *)subtable; |
| 98 |
}; |
| 99 |
|
| 100 |
caryll_Buffer *otfcc_build_gsub_single_subtable(const otl_Subtable *_subtable, otl_BuildHeuristics heuristics) { |
| 101 |
const subtable_gsub_single *subtable = &(_subtable->gsub_single); |
| 102 |
bool isConstantDifference = subtable->length > 0; |
| 103 |
if (isConstantDifference) { |
| 104 |
int32_t difference = subtable->items[0].to.index - subtable->items[0].from.index; |
| 105 |
isConstantDifference = isConstantDifference && difference < 0x8000 && difference > -0x8000; |
| 106 |
for (glyphid_t j = 1; j < subtable->length; j++) { |
| 107 |
int32_t diffJ = subtable->items[j].to.index - subtable->items[j].from.index; |
| 108 |
isConstantDifference = isConstantDifference && diffJ == difference |
| 109 |
&& diffJ < 0x8000 && diffJ > -0x8000; |
| 110 |
} |
| 111 |
} |
| 112 |
otl_Coverage *cov = Coverage.create(); |
| 113 |
for (glyphid_t j = 0; j < subtable->length; j++) { |
| 114 |
Coverage.push(cov, Handle.dup(subtable->items[j].from)); |
| 115 |
} |
| 116 |
|
| 117 |
caryll_Buffer *coverageBuf = Coverage.buildFormat(cov, heuristics & OTL_BH_GSUB_VERT ? 1 : 0); |
| 118 |
|
| 119 |
if (isConstantDifference && !(heuristics & OTL_BH_GSUB_VERT)) { |
| 120 |
bk_Block *b = |
| 121 |
bk_new_Block(b16, 1, // Format |
| 122 |
p16, bk_newBlockFromBuffer(coverageBuf), // coverage |
| 123 |
b16, |
| 124 |
subtable->items[0].to.index - subtable->items[0].from.index, // delta |
| 125 |
bkover); |
| 126 |
Coverage.free(cov); |
| 127 |
return bk_build_Block(b); |
| 128 |
} else { |
| 129 |
bk_Block *b = bk_new_Block(b16, 2, // Format |
| 130 |
p16, bk_newBlockFromBuffer(coverageBuf), // coverage |
| 131 |
b16, subtable->length, // quantity |
| 132 |
bkover); |
| 133 |
for (glyphid_t k = 0; k < subtable->length; k++) { |
| 134 |
bk_push(b, b16, subtable->items[k].to.index, bkover); |
| 135 |
} |
| 136 |
Coverage.free(cov); |
| 137 |
return bk_build_Block(b); |
| 138 |
} |
| 139 |
} |