| 1 |
#include "_TSI.h" |
| 2 |
#include "support/util.h" |
| 3 |
|
| 4 |
static INLINE void initTSIEntry(tsi_Entry *entry) { |
| 5 |
Handle.init(&entry->glyph); |
| 6 |
entry->type = TSI_GLYPH; |
| 7 |
entry->content = NULL; |
| 8 |
} |
| 9 |
static INLINE void copyTSIEntry(tsi_Entry *dst, const tsi_Entry *src) { |
| 10 |
Handle.copy(&dst->glyph, &src->glyph); |
| 11 |
dst->type = src->type; |
| 12 |
dst->content = sdsdup(src->content); |
| 13 |
} |
| 14 |
static INLINE void disposeTSIEntry(tsi_Entry *entry) { |
| 15 |
Handle.dispose(&entry->glyph); |
| 16 |
sdsfree(entry->content); |
| 17 |
} |
| 18 |
caryll_standardType(tsi_Entry, tsi_iEntry, initTSIEntry, copyTSIEntry, disposeTSIEntry); |
| 19 |
caryll_standardVectorImpl(table_TSI, tsi_Entry, tsi_iEntry, table_iTSI); |
| 20 |
|
| 21 |
static INLINE bool isValidGID(uint16_t gid, uint32_t tagIndex) { |
| 22 |
if (tagIndex == OTFCC_CHR('T','S','I','0')) { |
| 23 |
return gid != 0xFFFE && gid != 0xFFFC; |
| 24 |
} else { |
| 25 |
return gid < 0xFFFA; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
table_TSI *otfcc_readTSI(const otfcc_Packet packet, const otfcc_Options *options, uint32_t tagIndex, |
| 30 |
uint32_t tagText) { |
| 31 |
otfcc_PacketPiece textPart; |
| 32 |
textPart.tag = 0; |
| 33 |
otfcc_PacketPiece indexPart; |
| 34 |
indexPart.tag = 0; |
| 35 |
FOR_TABLE(tagIndex, tableIx) { |
| 36 |
indexPart = tableIx; |
| 37 |
} |
| 38 |
FOR_TABLE(tagText, tableTx) { |
| 39 |
textPart = tableTx; |
| 40 |
} |
| 41 |
if (!textPart.tag || !indexPart.tag) return NULL; |
| 42 |
|
| 43 |
table_TSI *tsi = table_iTSI.create(); |
| 44 |
for (uint32_t j = 0; j * 8 < indexPart.length; j++) { |
| 45 |
uint16_t gid = read_16u(indexPart.data + j * 8); |
| 46 |
uint32_t textLength = read_16u(indexPart.data + j * 8 + 2); |
| 47 |
uint32_t textOffset = read_32u(indexPart.data + j * 8 + 4); |
| 48 |
if (!isValidGID(gid, tagIndex) || textOffset >= textPart.length || !textLength) continue; |
| 49 |
// acquire next valid entry to retreve length of text of this entry |
| 50 |
uint32_t predictedTextLength = textPart.length - textOffset; |
| 51 |
for (glyphid_t k = j + 1; k * 8 < indexPart.length; k++) { |
| 52 |
uint16_t gidK = read_16u(indexPart.data + k * 8); |
| 53 |
uint32_t textOffsetK = read_32u(indexPart.data + k * 8 + 4); |
| 54 |
if (gidK != 0xFFFE && textOffsetK < textPart.length && textOffsetK > textOffset) { |
| 55 |
predictedTextLength = textOffsetK - textOffset; |
| 56 |
break; |
| 57 |
} |
| 58 |
} |
| 59 |
if (textLength >= 0x8000) { textLength = predictedTextLength; } |
| 60 |
// push entry |
| 61 |
tsi_Entry entry; |
| 62 |
switch (gid) { |
| 63 |
case 0xFFFA: |
| 64 |
entry.type = TSI_PREP; |
| 65 |
Handle.init(&entry.glyph); |
| 66 |
break; |
| 67 |
case 0xFFFB: |
| 68 |
entry.type = TSI_CVT; |
| 69 |
Handle.init(&entry.glyph); |
| 70 |
break; |
| 71 |
case 0xFFFD: |
| 72 |
entry.type = TSI_FPGM; |
| 73 |
Handle.init(&entry.glyph); |
| 74 |
break; |
| 75 |
default: |
| 76 |
entry.type = TSI_GLYPH; |
| 77 |
entry.glyph = Handle.fromIndex(gid); |
| 78 |
break; |
| 79 |
} |
| 80 |
entry.content = sdsnewlen(textPart.data + textOffset, textLength); |
| 81 |
table_iTSI.push(tsi, entry); |
| 82 |
} |
| 83 |
return tsi; |
| 84 |
} |
| 85 |
|
| 86 |
void otfcc_dumpTSI(const table_TSI *tsi, json_value *root, const otfcc_Options *options, |
| 87 |
const char *tag) { |
| 88 |
if (!tsi) return; |
| 89 |
loggedStep("%s", tag) { |
| 90 |
json_value *_tsi = json_object_new(2); |
| 91 |
json_value *_glyphs = json_object_new(tsi->length); |
| 92 |
foreach (tsi_Entry *entry, *tsi) { |
| 93 |
if (entry->type != TSI_GLYPH) continue; |
| 94 |
json_object_push( |
| 95 |
_glyphs, entry->glyph.name, |
| 96 |
json_string_new_length((uint32_t)sdslen(entry->content), entry->content)); |
| 97 |
} |
| 98 |
|
| 99 |
json_value *_extra = json_object_new(tsi->length); |
| 100 |
foreach (tsi_Entry *entry, *tsi) { |
| 101 |
if (entry->type == TSI_GLYPH) continue; |
| 102 |
const char *extraKey; |
| 103 |
switch (entry->type) { |
| 104 |
case TSI_CVT: |
| 105 |
extraKey = "cvt"; |
| 106 |
break; |
| 107 |
case TSI_FPGM: |
| 108 |
extraKey = "fpgm"; |
| 109 |
break; |
| 110 |
break; |
| 111 |
case TSI_PREP: |
| 112 |
extraKey = "prep"; |
| 113 |
break; |
| 114 |
default: |
| 115 |
extraKey = "reserved"; |
| 116 |
break; |
| 117 |
} |
| 118 |
json_object_push( |
| 119 |
_extra, extraKey, |
| 120 |
json_string_new_length((uint32_t)sdslen(entry->content), entry->content)); |
| 121 |
} |
| 122 |
json_object_push(_tsi, "glyphs", _glyphs); |
| 123 |
json_object_push(_tsi, "extra", _extra); |
| 124 |
json_object_push(root, tag, _tsi); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
table_TSI *otfcc_parseTSI(const json_value *root, const otfcc_Options *options, const char *tag) { |
| 129 |
json_value *_tsi = NULL; |
| 130 |
if (!(_tsi = json_obj_get_type(root, tag, json_object))) return NULL; |
| 131 |
table_TSI *tsi = table_iTSI.create(); |
| 132 |
loggedStep("%s", tag) { |
| 133 |
json_value *_glyphs = json_obj_get_type(_tsi, "glyphs", json_object); |
| 134 |
if (_glyphs) { |
| 135 |
for (uint32_t j = 0; j < _glyphs->u.object.length; j++) { |
| 136 |
char *_gid = _glyphs->u.object.values[j].name; |
| 137 |
size_t _gidlen = _glyphs->u.object.values[j].name_length; |
| 138 |
json_value *_content = _glyphs->u.object.values[j].value; |
| 139 |
if (!_content || _content->type != json_string) continue; |
| 140 |
table_iTSI.push(tsi, (tsi_Entry){.type = TSI_GLYPH, |
| 141 |
.glyph = Handle.fromName(sdsnewlen(_gid, _gidlen)), |
| 142 |
.content = sdsnewlen(_content->u.string.ptr, |
| 143 |
_content->u.string.length)}); |
| 144 |
} |
| 145 |
} |
| 146 |
json_value *_extra = json_obj_get_type(_tsi, "extra", json_object); |
| 147 |
if (_extra) { |
| 148 |
for (uint32_t j = 0; j < _extra->u.object.length; j++) { |
| 149 |
char *_key = _extra->u.object.values[j].name; |
| 150 |
json_value *_content = _extra->u.object.values[j].value; |
| 151 |
if (!_content || _content->type != json_string) continue; |
| 152 |
if (strcmp(_key, "cvt") == 0) { |
| 153 |
table_iTSI.push(tsi, |
| 154 |
(tsi_Entry){.type = TSI_CVT, |
| 155 |
.glyph = Handle.empty(), |
| 156 |
.content = sdsnewlen(_content->u.string.ptr, |
| 157 |
_content->u.string.length)}); |
| 158 |
} else if (strcmp(_key, "fpgm") == 0) { |
| 159 |
table_iTSI.push(tsi, |
| 160 |
(tsi_Entry){.type = TSI_FPGM, |
| 161 |
.glyph = Handle.empty(), |
| 162 |
.content = sdsnewlen(_content->u.string.ptr, |
| 163 |
_content->u.string.length)}); |
| 164 |
} else if (strcmp(_key, "prep") == 0) { |
| 165 |
table_iTSI.push(tsi, |
| 166 |
(tsi_Entry){.type = TSI_PREP, |
| 167 |
.glyph = Handle.empty(), |
| 168 |
.content = sdsnewlen(_content->u.string.ptr, |
| 169 |
_content->u.string.length)}); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
return tsi; |
| 175 |
} |
| 176 |
|
| 177 |
static glyphid_t propergid(tsi_Entry *entry, const tsi_EntryType type) { |
| 178 |
switch (type) { |
| 179 |
case TSI_CVT: |
| 180 |
return 0xFFFB; |
| 181 |
case TSI_FPGM: |
| 182 |
return 0xFFFD; |
| 183 |
case TSI_PREP: |
| 184 |
return 0xFFFA; |
| 185 |
case TSI_RESERVED_FFFC: |
| 186 |
return 0xFFFC; |
| 187 |
case TSI_GLYPH: |
| 188 |
return entry->glyph.index; |
| 189 |
default: |
| 190 |
fprintf(stderr,"! warning: propergid: unknown EntryType %u. Return 0x0.\n",type); |
| 191 |
return 0x0; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
static void pushTSIEntries(tsi_BuildTarget *target, const table_TSI *tsi, const tsi_EntryType type, |
| 196 |
const glyphid_t minN) { |
| 197 |
glyphid_t itemsPushed = 0; |
| 198 |
foreach (tsi_Entry *entry, *tsi) { |
| 199 |
if (entry->type != type) continue; |
| 200 |
size_t lengthSofar = target->textPart->cursor; |
| 201 |
bufwrite_sds(target->textPart, entry->content); |
| 202 |
size_t lengthAfter = target->textPart->cursor; |
| 203 |
bufwrite16b(target->indexPart, propergid(entry, type)); |
| 204 |
if (lengthAfter - lengthSofar < 0x8000) { |
| 205 |
bufwrite16b(target->indexPart, lengthAfter - lengthSofar); |
| 206 |
} else { |
| 207 |
bufwrite16b(target->indexPart, 0x8000); |
| 208 |
} |
| 209 |
bufwrite32b(target->indexPart, (uint32_t)lengthSofar); |
| 210 |
itemsPushed += 1; |
| 211 |
} |
| 212 |
while (itemsPushed < minN) { |
| 213 |
bufwrite16b(target->indexPart, propergid(NULL, type)); |
| 214 |
bufwrite16b(target->indexPart, 0x0000); |
| 215 |
bufwrite32b(target->indexPart, (uint32_t)target->textPart->cursor); |
| 216 |
itemsPushed += 1; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
tsi_BuildTarget otfcc_buildTSI(const table_TSI *tsi, const otfcc_Options *options) { |
| 221 |
|
| 222 |
tsi_BuildTarget target; |
| 223 |
if (!tsi) { |
| 224 |
target.textPart = NULL; |
| 225 |
target.indexPart = NULL; |
| 226 |
} else { |
| 227 |
target.textPart = bufnew(); |
| 228 |
target.indexPart = bufnew(); |
| 229 |
|
| 230 |
pushTSIEntries(&target, tsi, TSI_GLYPH, 0); |
| 231 |
// magic |
| 232 |
bufwrite16b(target.indexPart, 0xFFFE); |
| 233 |
bufwrite16b(target.indexPart, 0x0000); |
| 234 |
bufwrite32b(target.indexPart, 0xABFC1F34); |
| 235 |
pushTSIEntries(&target, tsi, TSI_PREP, 1); |
| 236 |
pushTSIEntries(&target, tsi, TSI_CVT, 1); |
| 237 |
pushTSIEntries(&target, tsi, TSI_RESERVED_FFFC, 1); |
| 238 |
pushTSIEntries(&target, tsi, TSI_FPGM, 1); |
| 239 |
} |
| 240 |
return target; |
| 241 |
} |