| 1 |
#include "support/util.h" |
| 2 |
#include "otfcc/table/otl/classdef.h" |
| 3 |
|
| 4 |
static INLINE void disposeClassDef(otl_ClassDef *cd) { |
| 5 |
if (cd->glyphs) { |
| 6 |
for (glyphid_t j = 0; j < cd->numGlyphs; j++) { |
| 7 |
Handle.dispose(&cd->glyphs[j]); |
| 8 |
} |
| 9 |
FREE(cd->glyphs); |
| 10 |
} |
| 11 |
FREE(cd->classes); |
| 12 |
} |
| 13 |
caryll_standardRefTypeFn(otl_ClassDef, disposeClassDef); |
| 14 |
|
| 15 |
static void growClassdef(otl_ClassDef *cd, uint32_t n) { |
| 16 |
if (!n) return; |
| 17 |
if (n > cd->capacity) { |
| 18 |
if (!cd->capacity) cd->capacity = 0x10; |
| 19 |
while (n > cd->capacity) |
| 20 |
cd->capacity += (cd->capacity >> 1) & 0xFFFFFF; |
| 21 |
RESIZE(cd->glyphs, cd->capacity); |
| 22 |
RESIZE(cd->classes, cd->capacity); |
| 23 |
} |
| 24 |
} |
| 25 |
static void pushClassDef(otl_ClassDef *cd, MOVE otfcc_GlyphHandle h, glyphclass_t cls) { |
| 26 |
cd->numGlyphs += 1; |
| 27 |
growClassdef(cd, cd->numGlyphs); |
| 28 |
cd->glyphs[cd->numGlyphs - 1] = h; |
| 29 |
cd->classes[cd->numGlyphs - 1] = cls; |
| 30 |
if (cls > cd->maxclass) cd->maxclass = cls; |
| 31 |
} |
| 32 |
|
| 33 |
typedef struct { |
| 34 |
int gid; |
| 35 |
int covIndex; |
| 36 |
UT_hash_handle hh; |
| 37 |
} coverage_entry; |
| 38 |
|
| 39 |
static int by_covIndex(coverage_entry *a, coverage_entry *b) { |
| 40 |
return a->covIndex - b->covIndex; |
| 41 |
} |
| 42 |
|
| 43 |
static otl_ClassDef *readClassDef(const uint8_t *data, uint32_t tableLength, uint32_t offset) { |
| 44 |
otl_ClassDef *cd = otl_iClassDef.create(); |
| 45 |
if (tableLength < offset + 4) return cd; |
| 46 |
uint16_t format = read_16u(data + offset); |
| 47 |
if (format == 1 && tableLength >= offset + 6) { |
| 48 |
glyphid_t startGID = read_16u(data + offset + 2); |
| 49 |
glyphid_t count = read_16u(data + offset + 4); |
| 50 |
if (count && tableLength >= offset + 6 + count * 2) { |
| 51 |
for (glyphid_t j = 0; j < count; j++) { |
| 52 |
pushClassDef(cd, Handle.fromIndex(startGID + j), read_16u(data + offset + 6 + j * 2)); |
| 53 |
} |
| 54 |
return cd; |
| 55 |
} |
| 56 |
} else if (format == 2) { |
| 57 |
// The ranges may overlap. |
| 58 |
// Use hashtable. |
| 59 |
uint16_t rangeCount = read_16u(data + offset + 2); |
| 60 |
if (tableLength < offset + 4 + rangeCount * 6) return cd; |
| 61 |
coverage_entry *hash = NULL; |
| 62 |
for (uint16_t j = 0; j < rangeCount; j++) { |
| 63 |
uint16_t start = read_16u(data + offset + 4 + 6 * j); |
| 64 |
uint16_t end = read_16u(data + offset + 4 + 6 * j + 2); |
| 65 |
uint16_t cls = read_16u(data + offset + 4 + 6 * j + 4); |
| 66 |
for (int k = start; k <= end; k++) { |
| 67 |
coverage_entry *item = NULL; |
| 68 |
HASH_FIND_INT(hash, &k, item); |
| 69 |
if (!item) { |
| 70 |
NEW(item); |
| 71 |
item->gid = k; |
| 72 |
item->covIndex = cls; |
| 73 |
HASH_ADD_INT(hash, gid, item); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
HASH_SORT(hash, by_covIndex); |
| 78 |
coverage_entry *e, *tmp; |
| 79 |
HASH_ITER(hh, hash, e, tmp) { |
| 80 |
pushClassDef(cd, Handle.fromIndex(e->gid), e->covIndex); |
| 81 |
HASH_DEL(hash, e); |
| 82 |
FREE(e); |
| 83 |
} |
| 84 |
return cd; |
| 85 |
} |
| 86 |
return cd; |
| 87 |
} |
| 88 |
|
| 89 |
static otl_ClassDef *expandClassDef(otl_Coverage *cov, otl_ClassDef *ocd) { |
| 90 |
otl_ClassDef *cd = otl_iClassDef.create(); |
| 91 |
coverage_entry *hash = NULL; |
| 92 |
for (glyphid_t j = 0; j < ocd->numGlyphs; j++) { |
| 93 |
int gid = ocd->glyphs[j].index; |
| 94 |
int cid = ocd->classes[j]; |
| 95 |
coverage_entry *item = NULL; |
| 96 |
HASH_FIND_INT(hash, &gid, item); |
| 97 |
if (!item) { |
| 98 |
NEW(item); |
| 99 |
item->gid = gid; |
| 100 |
item->covIndex = cid; |
| 101 |
HASH_ADD_INT(hash, gid, item); |
| 102 |
} |
| 103 |
} |
| 104 |
for (glyphid_t j = 0; j < cov->numGlyphs; j++) { |
| 105 |
int gid = cov->glyphs[j].index; |
| 106 |
coverage_entry *item = NULL; |
| 107 |
HASH_FIND_INT(hash, &gid, item); |
| 108 |
if (!item) { |
| 109 |
NEW(item); |
| 110 |
item->gid = gid; |
| 111 |
item->covIndex = 0; |
| 112 |
HASH_ADD_INT(hash, gid, item); |
| 113 |
} |
| 114 |
} |
| 115 |
coverage_entry *e, *tmp; |
| 116 |
HASH_ITER(hh, hash, e, tmp) { |
| 117 |
pushClassDef(cd, Handle.fromIndex(e->gid), e->covIndex); |
| 118 |
HASH_DEL(hash, e); |
| 119 |
FREE(e); |
| 120 |
} |
| 121 |
otl_iClassDef.free(ocd); |
| 122 |
return cd; |
| 123 |
} |
| 124 |
|
| 125 |
static json_value *dumpClassDef(const otl_ClassDef *cd) { |
| 126 |
json_value *a = json_object_new(cd->numGlyphs); |
| 127 |
for (glyphid_t j = 0; j < cd->numGlyphs; j++) { |
| 128 |
json_object_push(a, cd->glyphs[j].name, json_integer_new(cd->classes[j])); |
| 129 |
} |
| 130 |
return preserialize(a); |
| 131 |
} |
| 132 |
|
| 133 |
static otl_ClassDef *parseClassDef(const json_value *_cd) { |
| 134 |
if (!_cd || _cd->type != json_object) return NULL; |
| 135 |
otl_ClassDef *cd = otl_iClassDef.create(); |
| 136 |
for (glyphid_t j = 0; j < _cd->u.object.length; j++) { |
| 137 |
glyph_handle h = Handle.fromName(sdsnewlen(_cd->u.object.values[j].name, _cd->u.object.values[j].name_length)); |
| 138 |
json_value *_cid = _cd->u.object.values[j].value; |
| 139 |
glyphclass_t cls = 0; |
| 140 |
if (_cid->type == json_integer) { |
| 141 |
cls = _cid->u.integer; |
| 142 |
} else if (_cid->type == json_double) { |
| 143 |
cls = _cid->u.dbl; |
| 144 |
} |
| 145 |
pushClassDef(cd, h, cls); |
| 146 |
} |
| 147 |
return cd; |
| 148 |
} |
| 149 |
|
| 150 |
typedef struct { |
| 151 |
glyphid_t gid; |
| 152 |
glyphclass_t cid; |
| 153 |
} classdef_sortrecord; |
| 154 |
|
| 155 |
static int by_gid(const void *a, const void *b) { |
| 156 |
return ((const classdef_sortrecord *)a)->gid - ((const classdef_sortrecord *)b)->gid; |
| 157 |
} |
| 158 |
static caryll_Buffer *buildClassDef(const otl_ClassDef *cd) { |
| 159 |
caryll_Buffer *buf = bufnew(); |
| 160 |
bufwrite16b(buf, 2); |
| 161 |
if (!cd->numGlyphs) { // no glyphs, return a blank classdef |
| 162 |
bufwrite16b(buf, 0); |
| 163 |
return buf; |
| 164 |
} |
| 165 |
|
| 166 |
classdef_sortrecord *r; |
| 167 |
NEW(r, cd->numGlyphs); |
| 168 |
glyphid_t jj = 0; |
| 169 |
for (glyphid_t j = 0; j < cd->numGlyphs; j++) { |
| 170 |
if (cd->classes[j]) { |
| 171 |
r[jj].gid = cd->glyphs[j].index; |
| 172 |
r[jj].cid = cd->classes[j]; |
| 173 |
jj++; |
| 174 |
} |
| 175 |
} |
| 176 |
if (!jj) { // The classdef has only class 0 |
| 177 |
FREE(r); |
| 178 |
bufwrite16b(buf, 0); |
| 179 |
return buf; |
| 180 |
} |
| 181 |
qsort(r, jj, sizeof(classdef_sortrecord), by_gid); |
| 182 |
|
| 183 |
glyphid_t startGID = r[0].gid; |
| 184 |
glyphid_t endGID = startGID; |
| 185 |
glyphclass_t lastClass = r[0].cid; |
| 186 |
glyphid_t nRanges = 0; |
| 187 |
glyphid_t lastGID = startGID; |
| 188 |
caryll_Buffer *ranges = bufnew(); |
| 189 |
for (glyphid_t j = 1; j < jj; j++) { |
| 190 |
glyphid_t current = r[j].gid; |
| 191 |
if (current <= lastGID) continue; |
| 192 |
if (current == endGID + 1 && r[j].cid == lastClass) { |
| 193 |
endGID = current; |
| 194 |
} else { |
| 195 |
bufwrite16b(ranges, startGID); |
| 196 |
bufwrite16b(ranges, endGID); |
| 197 |
bufwrite16b(ranges, lastClass); |
| 198 |
nRanges += 1; |
| 199 |
startGID = endGID = current; |
| 200 |
lastClass = r[j].cid; |
| 201 |
} |
| 202 |
lastGID = current; |
| 203 |
} |
| 204 |
bufwrite16b(ranges, startGID); |
| 205 |
bufwrite16b(ranges, endGID); |
| 206 |
bufwrite16b(ranges, lastClass); |
| 207 |
nRanges += 1; |
| 208 |
bufwrite16b(buf, nRanges); |
| 209 |
bufwrite_bufdel(buf, ranges); |
| 210 |
FREE(r); |
| 211 |
return buf; |
| 212 |
} |
| 213 |
|
| 214 |
static void shrinkClassDef(otl_ClassDef *cd) { |
| 215 |
glyphid_t k = 0; |
| 216 |
for (glyphid_t j = 0; j < cd->numGlyphs; j++) { |
| 217 |
if (cd->glyphs[j].name) { |
| 218 |
cd->glyphs[k] = cd->glyphs[j]; |
| 219 |
cd->classes[k] = cd->classes[j]; |
| 220 |
k++; |
| 221 |
} else { |
| 222 |
Handle.dispose(&cd->glyphs[j]); |
| 223 |
} |
| 224 |
} |
| 225 |
cd->numGlyphs = k; |
| 226 |
} |
| 227 |
|
| 228 |
const struct __otfcc_IClassDef otl_iClassDef = { |
| 229 |
caryll_standardRefTypeMethods(otl_ClassDef), |
| 230 |
.read = readClassDef, |
| 231 |
.expand = expandClassDef, |
| 232 |
.dump = dumpClassDef, |
| 233 |
.parse = parseClassDef, |
| 234 |
.build = buildClassDef, |
| 235 |
.shrink = shrinkClassDef, |
| 236 |
.push = pushClassDef, |
| 237 |
}; |