| 1 |
#include "cmap.h" |
| 2 |
|
| 3 |
#include "support/util.h" |
| 4 |
#include "bk/bkgraph.h" |
| 5 |
|
| 6 |
// PART I, type definition |
| 7 |
|
| 8 |
static INLINE void initCmap(table_cmap *cmap) { |
| 9 |
cmap->unicodes = NULL; |
| 10 |
cmap->uvs = NULL; |
| 11 |
} |
| 12 |
static INLINE void disposeCmap(table_cmap *cmap) { |
| 13 |
{ // Unicode |
| 14 |
cmap_Entry *s, *tmp; |
| 15 |
HASH_ITER(hh, cmap->unicodes, s, tmp) { |
| 16 |
// delete and free all cmap entries |
| 17 |
Handle.dispose(&s->glyph); |
| 18 |
HASH_DEL(cmap->unicodes, s); |
| 19 |
FREE(s); |
| 20 |
} |
| 21 |
} |
| 22 |
{ // UVS |
| 23 |
cmap_UVS_Entry *s, *tmp; |
| 24 |
HASH_ITER(hh, cmap->uvs, s, tmp) { |
| 25 |
// delete and free all cmap entries |
| 26 |
Handle.dispose(&s->glyph); |
| 27 |
HASH_DEL(cmap->uvs, s); |
| 28 |
FREE(s); |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
caryll_standardRefTypeFn(table_cmap, initCmap, disposeCmap); |
| 33 |
|
| 34 |
static bool otfcc_encodeCmapByIndex(table_cmap *cmap, int c, uint16_t gid) { |
| 35 |
cmap_Entry *s; |
| 36 |
HASH_FIND_INT(cmap->unicodes, &c, s); |
| 37 |
if (s == NULL) { |
| 38 |
NEW(s); |
| 39 |
s->glyph = Handle.fromIndex(gid); |
| 40 |
s->unicode = c; |
| 41 |
HASH_ADD_INT(cmap->unicodes, unicode, s); |
| 42 |
return true; |
| 43 |
} else { |
| 44 |
return false; |
| 45 |
} |
| 46 |
} |
| 47 |
static bool otfcc_encodeCmapByName(table_cmap *cmap, int c, sds name) { |
| 48 |
cmap_Entry *s; |
| 49 |
HASH_FIND_INT(cmap->unicodes, &c, s); |
| 50 |
if (s == NULL) { |
| 51 |
NEW(s); |
| 52 |
s->glyph = Handle.fromName(name); |
| 53 |
s->unicode = c; |
| 54 |
HASH_ADD_INT(cmap->unicodes, unicode, s); |
| 55 |
return true; |
| 56 |
} else { |
| 57 |
return false; |
| 58 |
} |
| 59 |
} |
| 60 |
static bool otfcc_unmapCmap(table_cmap *cmap, int c) { |
| 61 |
cmap_Entry *s; |
| 62 |
HASH_FIND_INT(cmap->unicodes, &c, s); |
| 63 |
if (s) { |
| 64 |
Handle.dispose(&s->glyph); |
| 65 |
HASH_DEL(cmap->unicodes, s); |
| 66 |
FREE(s); |
| 67 |
return true; |
| 68 |
} else { |
| 69 |
return false; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
static otfcc_GlyphHandle *otfcc_cmapLookup(const table_cmap *cmap, int c) { |
| 74 |
cmap_Entry *s; |
| 75 |
HASH_FIND_INT(cmap->unicodes, &c, s); |
| 76 |
if (s) { |
| 77 |
return &(s->glyph); |
| 78 |
} else { |
| 79 |
return NULL; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
static bool otfcc_encodeCmapUVSByIndex(table_cmap *cmap, cmap_UVS_key c, uint16_t gid) { |
| 84 |
cmap_UVS_Entry *s; |
| 85 |
HASH_FIND(hh, cmap->uvs, &c, sizeof(cmap_UVS_key), s); |
| 86 |
if (s == NULL) { |
| 87 |
NEW(s); |
| 88 |
s->glyph = Handle.fromIndex(gid); |
| 89 |
s->key = c; |
| 90 |
HASH_ADD(hh, cmap->uvs, key, sizeof(cmap_UVS_key), s); |
| 91 |
return true; |
| 92 |
} else { |
| 93 |
return false; |
| 94 |
} |
| 95 |
} |
| 96 |
static bool otfcc_encodeCmapUVSByName(table_cmap *cmap, cmap_UVS_key c, sds name) { |
| 97 |
cmap_UVS_Entry *s; |
| 98 |
HASH_FIND(hh, cmap->uvs, &c, sizeof(cmap_UVS_key), s); |
| 99 |
if (s == NULL) { |
| 100 |
NEW(s); |
| 101 |
s->glyph = Handle.fromName(name); |
| 102 |
s->key = c; |
| 103 |
HASH_ADD(hh, cmap->uvs, key, sizeof(cmap_UVS_key), s); |
| 104 |
return true; |
| 105 |
} else { |
| 106 |
return false; |
| 107 |
} |
| 108 |
} |
| 109 |
static bool otfcc_unmapCmapUVS(table_cmap *cmap, cmap_UVS_key c) { |
| 110 |
cmap_Entry *s; |
| 111 |
HASH_FIND(hh, cmap->uvs, &c, sizeof(cmap_UVS_key), s); |
| 112 |
if (s) { |
| 113 |
Handle.dispose(&s->glyph); |
| 114 |
HASH_DEL(cmap->uvs, s); |
| 115 |
FREE(s); |
| 116 |
return true; |
| 117 |
} else { |
| 118 |
return false; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
static otfcc_GlyphHandle *otfcc_cmapLookupUVS(const table_cmap *cmap, cmap_UVS_key c) { |
| 123 |
cmap_Entry *s; |
| 124 |
HASH_FIND(hh, cmap->uvs, &c, sizeof(cmap_UVS_key), s); |
| 125 |
if (s) { |
| 126 |
return &(s->glyph); |
| 127 |
} else { |
| 128 |
return NULL; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
caryll_ElementInterfaceOf(table_cmap) table_iCmap = {caryll_standardRefTypeMethods(table_cmap), |
| 133 |
.encodeByIndex = otfcc_encodeCmapByIndex, |
| 134 |
.encodeByName = otfcc_encodeCmapByName, |
| 135 |
.unmap = otfcc_unmapCmap, |
| 136 |
.lookup = otfcc_cmapLookup, |
| 137 |
.encodeUVSByIndex = otfcc_encodeCmapUVSByIndex, |
| 138 |
.encodeUVSByName = otfcc_encodeCmapUVSByName, |
| 139 |
.unmapUVS = otfcc_unmapCmapUVS, |
| 140 |
.lookupUVS = otfcc_cmapLookupUVS}; |
| 141 |
|
| 142 |
// PART II, reading and writing |
| 143 |
|
| 144 |
static void readFormat12(font_file_pointer start, uint32_t lengthLimit, table_cmap *cmap) { |
| 145 |
if (lengthLimit < 16) return; |
| 146 |
uint32_t nGroups = read_32u(start + 12); |
| 147 |
if (lengthLimit < 16 + 12 * nGroups) return; |
| 148 |
for (uint32_t j = 0; j < nGroups; j++) { |
| 149 |
uint32_t startCode = read_32u(start + 16 + 12 * j); |
| 150 |
uint32_t endCode = read_32u(start + 16 + 12 * j + 4); |
| 151 |
uint32_t startGID = read_32u(start + 16 + 12 * j + 8); |
| 152 |
for (uint32_t c = startCode; c <= endCode; c++) { |
| 153 |
otfcc_encodeCmapByIndex(cmap, c, (c - startCode) + startGID); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
static void readFormat4(font_file_pointer start, uint32_t lengthLimit, table_cmap *cmap) { |
| 159 |
if (lengthLimit < 14) return; |
| 160 |
uint16_t segmentsCount = read_16u(start + 6) / 2; |
| 161 |
if (lengthLimit < 16 + segmentsCount * 8) return; |
| 162 |
for (uint16_t j = 0; j < segmentsCount; j++) { |
| 163 |
uint16_t endCode = read_16u(start + 14 + j * 2); |
| 164 |
uint16_t startCode = read_16u(start + 14 + segmentsCount * 2 + 2 + j * 2); |
| 165 |
int16_t idDelta = read_16u(start + 14 + segmentsCount * 4 + 2 + j * 2); |
| 166 |
uint32_t idRangeOffsetOffset = 14 + segmentsCount * 6 + 2 + j * 2; |
| 167 |
uint16_t idRangeOffset = read_16u(start + idRangeOffsetOffset); |
| 168 |
if (idRangeOffset == 0) { |
| 169 |
for (uint32_t c = startCode; c < 0xFFFF && c <= endCode; c++) { |
| 170 |
uint16_t gid = (c + idDelta) & 0xFFFF; |
| 171 |
otfcc_encodeCmapByIndex(cmap, c, gid); |
| 172 |
} |
| 173 |
} else { |
| 174 |
for (uint32_t c = startCode; c < 0xFFFF && c <= endCode; c++) { |
| 175 |
uint32_t glyphOffset = idRangeOffset + (c - startCode) * 2 + idRangeOffsetOffset; |
| 176 |
if (glyphOffset + 2 > lengthLimit) continue; // ignore this encoding slot when o-o-r |
| 177 |
uint16_t gid = (read_16u(start + glyphOffset) + idDelta) & 0xFFFF; |
| 178 |
otfcc_encodeCmapByIndex(cmap, c, gid); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
static void readUVSDefault(font_file_pointer start, uint32_t lengthLimit, unicode_t selector, |
| 184 |
table_cmap *cmap) { |
| 185 |
if (lengthLimit < 4) return; |
| 186 |
uint32_t numUnicodeValueRanges = read_32u(start); |
| 187 |
if (lengthLimit < 4 + 4 * numUnicodeValueRanges) return; |
| 188 |
for (uint32_t j = 0; j < numUnicodeValueRanges; j++) { |
| 189 |
font_file_pointer vsr = start + 4 + 4 * j; |
| 190 |
unicode_t startUnicodeValue = read_24u(vsr); |
| 191 |
uint8_t additionalCount = read_8u(vsr + 3); |
| 192 |
for (unicode_t u = startUnicodeValue; u <= startUnicodeValue + additionalCount; u++) { |
| 193 |
otfcc_GlyphHandle *g = table_iCmap.lookup(cmap, (int)u); |
| 194 |
if (!g) continue; |
| 195 |
table_iCmap.encodeUVSByIndex(cmap, (cmap_UVS_key){.unicode = u, .selector = selector}, |
| 196 |
g->index); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
static void readUVSNonDefault(font_file_pointer start, uint32_t lengthLimit, unicode_t selector, |
| 201 |
table_cmap *cmap) { |
| 202 |
if (lengthLimit < 4) return; |
| 203 |
uint32_t numUVSMappings = read_32u(start); |
| 204 |
if (lengthLimit < 4 + 5 * numUVSMappings) return; |
| 205 |
for (uint32_t j = 0; j < numUVSMappings; j++) { |
| 206 |
font_file_pointer vsr = start + 4 + 5 * j; |
| 207 |
unicode_t unicodeValue = read_24u(vsr); |
| 208 |
glyphid_t glyphID = read_16u(vsr + 3); |
| 209 |
table_iCmap.encodeUVSByIndex( |
| 210 |
cmap, (cmap_UVS_key){.unicode = unicodeValue, .selector = selector}, glyphID); |
| 211 |
} |
| 212 |
} |
| 213 |
static void readFormat14(font_file_pointer start, uint32_t lengthLimit, table_cmap *cmap) { |
| 214 |
if (lengthLimit < 10) return; |
| 215 |
uint32_t nGroups = read_32u(start + 6); |
| 216 |
if (lengthLimit < 11 + 11 * nGroups) return; |
| 217 |
for (uint32_t j = 0; j < nGroups; j++) { |
| 218 |
font_file_pointer vsr = start + 10 + 11 * j; |
| 219 |
unicode_t selector = read_24u(vsr); |
| 220 |
uint32_t defaultUVSOffset = read_32u(vsr + 3); |
| 221 |
uint32_t nonDefaultUVSOffset = read_32u(vsr + 7); |
| 222 |
|
| 223 |
if (defaultUVSOffset) { |
| 224 |
readUVSDefault(start + defaultUVSOffset, lengthLimit - defaultUVSOffset, selector, |
| 225 |
cmap); |
| 226 |
} |
| 227 |
if (nonDefaultUVSOffset) { |
| 228 |
readUVSNonDefault(start + nonDefaultUVSOffset, lengthLimit - nonDefaultUVSOffset, |
| 229 |
selector, cmap); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
static void readCmapMappingTable(font_file_pointer start, uint32_t lengthLimit, table_cmap *cmap, |
| 235 |
tableid_t requiredFormat) { |
| 236 |
uint16_t format = read_16u(start); |
| 237 |
if (format == requiredFormat) { |
| 238 |
if (format == 4) { |
| 239 |
readFormat4(start, lengthLimit, cmap); |
| 240 |
} else if (format == 12) { |
| 241 |
readFormat12(start, lengthLimit, cmap); |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
static void readCmapMappingTableUVS(font_file_pointer start, uint32_t lengthLimit, |
| 247 |
table_cmap *cmap) { |
| 248 |
uint16_t format = read_16u(start); |
| 249 |
if (format == 14) { readFormat14(start, lengthLimit, cmap); } |
| 250 |
} |
| 251 |
|
| 252 |
static int by_unicode(cmap_Entry *a, cmap_Entry *b) { |
| 253 |
return (a->unicode - b->unicode); |
| 254 |
} |
| 255 |
|
| 256 |
static int by_uvs_key(cmap_UVS_Entry *a, cmap_UVS_Entry *b) { |
| 257 |
if (a->key.unicode == b->key.unicode) { |
| 258 |
return a->key.selector - b->key.selector; |
| 259 |
} else { |
| 260 |
return a->key.unicode - b->key.unicode; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
static INLINE bool isValidCmapEncoding(uint16_t platform, uint16_t encoding) { |
| 265 |
return (platform == 0 && encoding == 3) || (platform == 0 && encoding == 4) || |
| 266 |
(platform == 0 && encoding == 5) || (platform == 3 && encoding == 1) || |
| 267 |
(platform == 3 && encoding == 10); |
| 268 |
} |
| 269 |
|
| 270 |
// Note: we do not support Apple's format 0 subtable |
| 271 |
// since we not never support legacy fonts. |
| 272 |
const tableid_t formatPriorities[] = {12, 4, 0}; |
| 273 |
|
| 274 |
// OTFCC will not support all `cmap` mappings. |
| 275 |
table_cmap *otfcc_readCmap(const otfcc_Packet packet, const otfcc_Options *options) { |
| 276 |
// the map is a reference to a hash table |
| 277 |
table_cmap *cmap = NULL; |
| 278 |
FOR_TABLE(OTFCC_CHR('c','m','a','p'), table) { |
| 279 |
font_file_pointer data = table.data; |
| 280 |
uint32_t length = table.length; |
| 281 |
if (length < 4) goto CMAP_CORRUPTED; |
| 282 |
|
| 283 |
cmap = table_iCmap.create(); // intialize to empty hashtable |
| 284 |
uint16_t numTables = read_16u(data + 2); |
| 285 |
if (length < 4 + 8 * numTables) goto CMAP_CORRUPTED; |
| 286 |
|
| 287 |
// step 1 : read format 12. The results are prioritized |
| 288 |
for (size_t kSubtableType = 0; formatPriorities[kSubtableType]; kSubtableType++) { |
| 289 |
for (uint16_t j = 0; j < numTables; j++) { |
| 290 |
uint16_t platform = read_16u(data + 4 + 8 * j); |
| 291 |
uint16_t encoding = read_16u(data + 4 + 8 * j + 2); |
| 292 |
if (!isValidCmapEncoding(platform, encoding)) continue; |
| 293 |
|
| 294 |
uint32_t tableOffset = read_32u(data + 4 + 8 * j + 4); |
| 295 |
readCmapMappingTable(data + tableOffset, length - tableOffset, cmap, |
| 296 |
formatPriorities[kSubtableType]); |
| 297 |
}; |
| 298 |
} |
| 299 |
HASH_SORT(cmap->unicodes, by_unicode); |
| 300 |
|
| 301 |
// step 3 : read format 14 |
| 302 |
for (uint16_t j = 0; j < numTables; j++) { |
| 303 |
uint16_t platform = read_16u(data + 4 + 8 * j); |
| 304 |
uint16_t encoding = read_16u(data + 4 + 8 * j + 2); |
| 305 |
if (!isValidCmapEncoding(platform, encoding)) continue; |
| 306 |
|
| 307 |
uint32_t tableOffset = read_32u(data + 4 + 8 * j + 4); |
| 308 |
readCmapMappingTableUVS(data + tableOffset, length - tableOffset, cmap); |
| 309 |
}; |
| 310 |
HASH_SORT(cmap->uvs, by_uvs_key); |
| 311 |
return cmap; |
| 312 |
|
| 313 |
CMAP_CORRUPTED: |
| 314 |
logWarning("table 'cmap' corrupted.\n"); |
| 315 |
if (cmap != NULL) { FREE(cmap), cmap = NULL; } |
| 316 |
} |
| 317 |
return NULL; |
| 318 |
} |
| 319 |
|
| 320 |
void otfcc_dumpCmap(const table_cmap *table, json_value *root, const otfcc_Options *options) { |
| 321 |
if (!table) return; |
| 322 |
loggedStep("cmap") { |
| 323 |
if (table->unicodes) { |
| 324 |
json_value *cmap = json_object_new(HASH_COUNT(table->unicodes)); |
| 325 |
cmap_Entry *item; |
| 326 |
foreach_hash(item, table->unicodes) if (item->glyph.name) { |
| 327 |
sds key; |
| 328 |
if (options->decimal_cmap) { |
| 329 |
key = sdsfromlonglong(item->unicode); |
| 330 |
} else { |
| 331 |
key = sdscatprintf(sdsempty(), "U+%04X", item->unicode); |
| 332 |
} |
| 333 |
json_object_push( |
| 334 |
cmap, key, |
| 335 |
json_string_new_length((uint32_t)sdslen(item->glyph.name), item->glyph.name)); |
| 336 |
sdsfree(key); |
| 337 |
} |
| 338 |
json_object_push(root, "cmap", cmap); |
| 339 |
} |
| 340 |
if (table->uvs) { |
| 341 |
json_value *uvs = json_object_new(HASH_COUNT(table->uvs)); |
| 342 |
cmap_UVS_Entry *item; |
| 343 |
foreach_hash(item, table->uvs) if (item->glyph.name) { |
| 344 |
sds key; |
| 345 |
if (options->decimal_cmap) { |
| 346 |
key = sdscatprintf(sdsempty(), "%d %d", item->key.unicode, item->key.selector); |
| 347 |
} else { |
| 348 |
key = sdscatprintf(sdsempty(), "U+%04X U+%04X", item->key.unicode, |
| 349 |
item->key.selector); |
| 350 |
} |
| 351 |
json_object_push( |
| 352 |
uvs, key, |
| 353 |
json_string_new_length((uint32_t)sdslen(item->glyph.name), item->glyph.name)); |
| 354 |
sdsfree(key); |
| 355 |
} |
| 356 |
json_object_push(root, "cmap_uvs", uvs); |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
static INLINE unicode_t parseUnicode(const sds unicodeStr) { |
| 362 |
if (sdslen(unicodeStr) > 2 && unicodeStr[0] == 'U' && unicodeStr[1] == '+') { |
| 363 |
return strtol(unicodeStr + 2, NULL, 16); |
| 364 |
} else { |
| 365 |
return atoi(unicodeStr); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
static void parseCmapUnicodes(table_cmap *cmap, const json_value *table, |
| 370 |
const otfcc_Options *options) { |
| 371 |
if (!table || table->type != json_object) return; |
| 372 |
for (uint32_t j = 0; j < table->u.object.length; j++) { |
| 373 |
sds unicodeStr = |
| 374 |
sdsnewlen(table->u.object.values[j].name, table->u.object.values[j].name_length); |
| 375 |
json_value *item = table->u.object.values[j].value; |
| 376 |
unicode_t unicode = parseUnicode(unicodeStr); |
| 377 |
sdsfree(unicodeStr); |
| 378 |
if (item->type == json_string && unicode > 0 && unicode <= 0x10FFFF) { |
| 379 |
sds gname = sdsnewlen(item->u.string.ptr, item->u.string.length); |
| 380 |
if (!otfcc_encodeCmapByName(cmap, unicode, gname)) { |
| 381 |
glyph_handle *currentMap = otfcc_cmapLookup(cmap, unicode); |
| 382 |
logWarning("U+%04X is already mapped to %s. Assignment to %s is ignored.", unicode, |
| 383 |
currentMap->name, gname); |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
static INLINE cmap_UVS_key parseUVSKey(const sds uvsStr) { |
| 390 |
size_t len = sdslen(uvsStr); |
| 391 |
cmap_UVS_key k = {.unicode = 0, .selector = 0}; |
| 392 |
for (sds scan = uvsStr; scan < uvsStr + len; scan++) { |
| 393 |
if (*scan == ' ') { |
| 394 |
k.unicode = parseUnicode(uvsStr); |
| 395 |
k.selector = parseUnicode(scan + 1); |
| 396 |
return k; |
| 397 |
} |
| 398 |
} |
| 399 |
return k; |
| 400 |
} |
| 401 |
|
| 402 |
static void parseCmapUVS(table_cmap *cmap, const json_value *table, const otfcc_Options *options) { |
| 403 |
if (!table || table->type != json_object) return; |
| 404 |
for (uint32_t j = 0; j < table->u.object.length; j++) { |
| 405 |
sds uvsStr = |
| 406 |
sdsnewlen(table->u.object.values[j].name, table->u.object.values[j].name_length); |
| 407 |
cmap_UVS_key k = parseUVSKey(uvsStr); |
| 408 |
json_value *item = table->u.object.values[j].value; |
| 409 |
if (item->type == json_string && k.unicode > 0 && k.unicode <= 0x10FFFF && k.selector > 0 && |
| 410 |
k.selector <= 0x10FFFF) { |
| 411 |
sds gname = sdsnewlen(item->u.string.ptr, item->u.string.length); |
| 412 |
if (!otfcc_encodeCmapUVSByName(cmap, k, gname)) { |
| 413 |
glyph_handle *currentMap = otfcc_cmapLookupUVS(cmap, k); |
| 414 |
logWarning( |
| 415 |
"UVS U+%04X U+%04X is already mapped to %s. Assignment to %s is ignored.", |
| 416 |
k.unicode, k.selector, currentMap->name, gname); |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
table_cmap *otfcc_parseCmap(const json_value *root, const otfcc_Options *options) { |
| 423 |
if (root->type != json_object) return NULL; |
| 424 |
table_cmap *cmap = table_iCmap.create(); |
| 425 |
loggedStep("cmap") { |
| 426 |
parseCmapUnicodes(cmap, json_obj_get_type(root, "cmap", json_object), options); |
| 427 |
} |
| 428 |
loggedStep("cmap_uvs") { |
| 429 |
parseCmapUVS(cmap, json_obj_get_type(root, "cmap_uvs", json_object), options); |
| 430 |
} |
| 431 |
|
| 432 |
HASH_SORT(cmap->unicodes, by_unicode); |
| 433 |
HASH_SORT(cmap->uvs, by_uvs_key); |
| 434 |
|
| 435 |
return cmap; |
| 436 |
} |
| 437 |
// writing tables |
| 438 |
#define FLUSH_SEQUENCE_FORMAT_4 \ |
| 439 |
bufwrite16b(endCount, lastUnicodeEnd); \ |
| 440 |
bufwrite16b(startCount, lastUnicodeStart); \ |
| 441 |
if (isSequencial) { \ |
| 442 |
bufwrite16b(idDelta, lastGIDStart - lastUnicodeStart); \ |
| 443 |
bufwrite16b(idRangeOffset, 0); \ |
| 444 |
} else { \ |
| 445 |
bufwrite16b(idDelta, 0); \ |
| 446 |
bufwrite16b(idRangeOffset, lastGlyphIdArrayOffset + 1); \ |
| 447 |
} \ |
| 448 |
segmentsCount += 1; |
| 449 |
|
| 450 |
static caryll_Buffer *otfcc_buildCmap_format4(const table_cmap *cmap) { |
| 451 |
caryll_Buffer *buf = bufnew(); |
| 452 |
caryll_Buffer *endCount = bufnew(); |
| 453 |
caryll_Buffer *startCount = bufnew(); |
| 454 |
caryll_Buffer *idDelta = bufnew(); |
| 455 |
caryll_Buffer *idRangeOffset = bufnew(); |
| 456 |
caryll_Buffer *glyphIdArray = bufnew(); |
| 457 |
|
| 458 |
bool started = false; |
| 459 |
int lastUnicodeStart = 0xFFFFFF; |
| 460 |
int lastUnicodeEnd = 0xFFFFFF; |
| 461 |
int lastGIDStart = 0xFFFFFF; |
| 462 |
int lastGIDEnd = 0xFFFFFF; |
| 463 |
size_t lastGlyphIdArrayOffset = 0; |
| 464 |
bool isSequencial = true; |
| 465 |
uint16_t segmentsCount = 0; |
| 466 |
|
| 467 |
cmap_Entry *item; |
| 468 |
foreach_hash(item, cmap->unicodes) if (item->unicode <= 0xFFFF) { |
| 469 |
if (!started) { |
| 470 |
started = true; |
| 471 |
lastUnicodeStart = lastUnicodeEnd = item->unicode; |
| 472 |
lastGIDStart = lastGIDEnd = item->glyph.index; |
| 473 |
isSequencial = true; |
| 474 |
} else { |
| 475 |
if (item->unicode == lastUnicodeEnd + 1 && |
| 476 |
!(item->glyph.index != lastGIDEnd + 1 && isSequencial && |
| 477 |
lastGIDEnd - lastGIDStart >= 4)) { |
| 478 |
if (isSequencial && !(item->glyph.index == lastGIDEnd + 1)) { |
| 479 |
lastGlyphIdArrayOffset = glyphIdArray->cursor; |
| 480 |
// oops, sequencial glyphid broken |
| 481 |
for (int j = lastGIDStart; j <= lastGIDEnd; j++) { |
| 482 |
bufwrite16b(glyphIdArray, j); |
| 483 |
} |
| 484 |
} |
| 485 |
lastUnicodeEnd = item->unicode; |
| 486 |
isSequencial = isSequencial && (item->glyph.index == lastGIDEnd + 1); |
| 487 |
lastGIDEnd = item->glyph.index; |
| 488 |
if (!isSequencial) { bufwrite16b(glyphIdArray, lastGIDEnd); } |
| 489 |
} else { |
| 490 |
// we have a segment |
| 491 |
FLUSH_SEQUENCE_FORMAT_4; |
| 492 |
|
| 493 |
lastUnicodeStart = lastUnicodeEnd = item->unicode; |
| 494 |
lastGIDStart = lastGIDEnd = item->glyph.index; |
| 495 |
isSequencial = true; |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
FLUSH_SEQUENCE_FORMAT_4; |
| 501 |
if (lastGIDEnd < 0xFFFF) { |
| 502 |
// Add a padding segment to end this subtable |
| 503 |
bufwrite16b(endCount, 0xFFFF); |
| 504 |
bufwrite16b(startCount, 0xFFFF); |
| 505 |
bufwrite16b(idDelta, 1); |
| 506 |
bufwrite16b(idRangeOffset, 0); |
| 507 |
segmentsCount += 1; |
| 508 |
} |
| 509 |
|
| 510 |
for (int j = 0; j < segmentsCount; j++) { |
| 511 |
// rewrite idRangeOffset |
| 512 |
uint16_t ro = read_16u((uint8_t *)idRangeOffset->data + j * 2); |
| 513 |
if (ro) { |
| 514 |
ro -= 1; |
| 515 |
ro += 2 * (segmentsCount - j); |
| 516 |
bufseek(idRangeOffset, 2 * j); |
| 517 |
bufwrite16b(idRangeOffset, ro); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
bufwrite16b(buf, 4); |
| 522 |
bufwrite16b(buf, 0); // fill later |
| 523 |
bufwrite16b(buf, 0); |
| 524 |
bufwrite16b(buf, segmentsCount << 1); |
| 525 |
uint32_t i; |
| 526 |
uint32_t j; |
| 527 |
for (j = 0, i = 1; i <= segmentsCount; ++j) { |
| 528 |
i <<= 1; |
| 529 |
} |
| 530 |
bufwrite16b(buf, i); |
| 531 |
bufwrite16b(buf, j - 1); |
| 532 |
bufwrite16b(buf, 2 * segmentsCount - i); |
| 533 |
bufwrite_buf(buf, endCount); |
| 534 |
bufwrite16b(buf, 0); |
| 535 |
bufwrite_buf(buf, startCount); |
| 536 |
bufwrite_buf(buf, idDelta); |
| 537 |
bufwrite_buf(buf, idRangeOffset); |
| 538 |
bufwrite_buf(buf, glyphIdArray); |
| 539 |
|
| 540 |
bufseek(buf, 2); |
| 541 |
bufwrite16b(buf, buflen(buf)); |
| 542 |
|
| 543 |
buffree(endCount); |
| 544 |
buffree(startCount); |
| 545 |
buffree(idDelta); |
| 546 |
buffree(idRangeOffset); |
| 547 |
buffree(glyphIdArray); |
| 548 |
return buf; |
| 549 |
} |
| 550 |
|
| 551 |
static caryll_Buffer *otfcc_tryBuildCmap_format4(const table_cmap *cmap) { |
| 552 |
caryll_Buffer *buf = otfcc_buildCmap_format4(cmap); |
| 553 |
if (buflen(buf) > UINT16_MAX) { |
| 554 |
// this cmap subtable is broken |
| 555 |
buffree(buf); |
| 556 |
return NULL; |
| 557 |
} else |
| 558 |
return buf; |
| 559 |
} |
| 560 |
|
| 561 |
static caryll_Buffer *otfcc_buildCmap_format12(const table_cmap *cmap) { |
| 562 |
caryll_Buffer *buf = bufnew(); |
| 563 |
bufwrite16b(buf, 12); |
| 564 |
bufwrite16b(buf, 0); |
| 565 |
bufwrite32b(buf, 0); // fill later |
| 566 |
bufwrite32b(buf, 0); |
| 567 |
bufwrite32b(buf, 0); // fill later |
| 568 |
|
| 569 |
uint32_t nGroups = 0; |
| 570 |
bool started = false; |
| 571 |
int lastUnicodeStart = 0xFFFFFF; |
| 572 |
int lastUnicodeEnd = 0xFFFFFF; |
| 573 |
int lastGIDStart = 0xFFFFFF; |
| 574 |
int lastGIDEnd = 0xFFFFFF; |
| 575 |
cmap_Entry *item; |
| 576 |
foreach_hash(item, cmap->unicodes) { |
| 577 |
if (!started) { |
| 578 |
started = true; |
| 579 |
lastUnicodeStart = lastUnicodeEnd = item->unicode; |
| 580 |
lastGIDStart = lastGIDEnd = item->glyph.index; |
| 581 |
} else if (item->unicode == lastUnicodeEnd + 1 && item->glyph.index == lastGIDEnd + 1) { |
| 582 |
lastUnicodeEnd = item->unicode; |
| 583 |
lastGIDEnd = item->glyph.index; |
| 584 |
} else { |
| 585 |
bufwrite32b(buf, lastUnicodeStart); |
| 586 |
bufwrite32b(buf, lastUnicodeEnd); |
| 587 |
bufwrite32b(buf, lastGIDStart); |
| 588 |
nGroups += 1; |
| 589 |
lastUnicodeStart = lastUnicodeEnd = item->unicode; |
| 590 |
lastGIDStart = lastGIDEnd = item->glyph.index; |
| 591 |
} |
| 592 |
} |
| 593 |
bufwrite32b(buf, lastUnicodeStart); |
| 594 |
bufwrite32b(buf, lastUnicodeEnd); |
| 595 |
bufwrite32b(buf, lastGIDStart); |
| 596 |
nGroups += 1; |
| 597 |
|
| 598 |
bufseek(buf, 4); |
| 599 |
bufwrite32b(buf, (uint32_t)buflen(buf)); |
| 600 |
bufseek(buf, 12); |
| 601 |
bufwrite32b(buf, nGroups); |
| 602 |
return buf; |
| 603 |
} |
| 604 |
|
| 605 |
#define MAX_UNICODE 0x110001 |
| 606 |
#define HAS_DEFAULT 1 |
| 607 |
#define HAS_NON_DEFAULT 2 |
| 608 |
|
| 609 |
static INLINE void writeDefaultRange(caryll_Buffer *dflt, uint32_t *nRanges, unicode_t start, |
| 610 |
unicode_t end) { |
| 611 |
// start and end are inclusive |
| 612 |
while (end - start > 0xFF) { |
| 613 |
bufwrite24b(dflt, start); |
| 614 |
bufwrite8(dflt, 0xFF); |
| 615 |
start += 0x100; |
| 616 |
*nRanges += 1; |
| 617 |
} |
| 618 |
bufwrite24b(dflt, start); |
| 619 |
bufwrite8(dflt, end - start); |
| 620 |
*nRanges += 1; |
| 621 |
} |
| 622 |
|
| 623 |
static uint8_t buildFormat14ForSelector(const table_cmap *cmap, unicode_t selector, |
| 624 |
caryll_Buffer *dflt, caryll_Buffer *nondflt) { |
| 625 |
glyphid_t *defaults, *nonDefaults; |
| 626 |
NEW_CLEAN_N(defaults, MAX_UNICODE); |
| 627 |
NEW_CLEAN_N(nonDefaults, MAX_UNICODE); |
| 628 |
for (unicode_t s = 0; s < MAX_UNICODE; s++) { |
| 629 |
defaults[s] = 0xFFFF; |
| 630 |
nonDefaults[s] = 0xFFFF; |
| 631 |
} |
| 632 |
cmap_UVS_Entry *item; |
| 633 |
foreach_hash(item, cmap->uvs) { |
| 634 |
unicode_t u = item->key.unicode; |
| 635 |
if (item->key.selector != selector || u >= MAX_UNICODE) continue; |
| 636 |
if (!item->glyph.name) continue; |
| 637 |
glyphid_t uvsGID = item->glyph.index; |
| 638 |
otfcc_GlyphHandle *g = table_iCmap.lookup(cmap, (int)u); |
| 639 |
if (!g) { |
| 640 |
nonDefaults[u] = uvsGID; |
| 641 |
} else if (uvsGID == g->index) { |
| 642 |
defaults[u] = uvsGID; |
| 643 |
} else { |
| 644 |
nonDefaults[u] = uvsGID; |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
defaults[0] = nonDefaults[0] = 0xFFFF; // clear U+0000 |
| 649 |
defaults[MAX_UNICODE - 1] = nonDefaults[MAX_UNICODE - 1] = 0xFFFF; // clear U+110000 * |
| 650 |
|
| 651 |
uint32_t numUnicodeValueRanges = 0; |
| 652 |
unicode_t startUnicodeValue = 0; |
| 653 |
uint32_t numUVSMappings = 0; |
| 654 |
bufwrite32b(dflt, 0); |
| 655 |
bufwrite32b(nondflt, 0); |
| 656 |
for (unicode_t u = 1; u < MAX_UNICODE; u++) { |
| 657 |
if (defaults[u] != 0xFFFF && defaults[u - 1] == 0xFFFF) { startUnicodeValue = u; } |
| 658 |
if (defaults[u] == 0xFFFF && defaults[u - 1] != 0xFFFF) { |
| 659 |
writeDefaultRange(dflt, &numUnicodeValueRanges, startUnicodeValue, u - 1); |
| 660 |
} |
| 661 |
// non-defaults |
| 662 |
if (nonDefaults[u] != 0xFFFF) { |
| 663 |
bufwrite24b(nondflt, u); |
| 664 |
bufwrite16b(nondflt, nonDefaults[u]); |
| 665 |
numUVSMappings++; |
| 666 |
} |
| 667 |
} |
| 668 |
bufseek(dflt, 0); |
| 669 |
bufwrite32b(dflt, numUnicodeValueRanges); |
| 670 |
bufseek(nondflt, 0); |
| 671 |
bufwrite32b(nondflt, numUVSMappings); |
| 672 |
|
| 673 |
FREE(defaults); |
| 674 |
FREE(nonDefaults); |
| 675 |
|
| 676 |
return (numUnicodeValueRanges ? HAS_DEFAULT : 0) | (numUVSMappings ? HAS_NON_DEFAULT : 0); |
| 677 |
} |
| 678 |
|
| 679 |
static caryll_Buffer *otfcc_buildCmap_format14(const table_cmap *cmap) { |
| 680 |
bool *validSelectors; |
| 681 |
NEW_CLEAN_N(validSelectors, MAX_UNICODE); |
| 682 |
cmap_UVS_Entry *item; |
| 683 |
foreach_hash(item, cmap->uvs) { |
| 684 |
if (item->key.selector < MAX_UNICODE) { validSelectors[item->key.selector] = true; } |
| 685 |
} |
| 686 |
uint32_t nSelectors = 0; |
| 687 |
for (unicode_t selector = 0; selector < MAX_UNICODE; selector++) { |
| 688 |
if (!validSelectors[selector]) continue; |
| 689 |
nSelectors++; |
| 690 |
} |
| 691 |
|
| 692 |
bk_Block *st = bk_new_Block(b16, 14, // format |
| 693 |
b32, 0, // length -- fill later |
| 694 |
b32, nSelectors, // selector quantity |
| 695 |
bkover); |
| 696 |
|
| 697 |
for (unicode_t selector = 0; selector < MAX_UNICODE; selector++) { |
| 698 |
if (!validSelectors[selector]) continue; |
| 699 |
caryll_Buffer *dflt = bufnew(); |
| 700 |
caryll_Buffer *nondflt = bufnew(); |
| 701 |
uint8_t results = buildFormat14ForSelector(cmap, selector, dflt, nondflt); |
| 702 |
if (!(results & HAS_DEFAULT)) { |
| 703 |
buffree(dflt); |
| 704 |
dflt = NULL; |
| 705 |
} |
| 706 |
if (!(results & HAS_NON_DEFAULT)) { |
| 707 |
buffree(nondflt); |
| 708 |
nondflt = NULL; |
| 709 |
} |
| 710 |
bk_push(st, b8, (selector >> 16) & 0xFF, // selector, first byte |
| 711 |
b8, (selector >> 8) & 0xFF, // selector, first byte |
| 712 |
b8, (selector >> 0) & 0xFF, // selector, first byte |
| 713 |
p32, bk_newBlockFromBuffer(dflt), // default offset |
| 714 |
p32, bk_newBlockFromBuffer(nondflt), // non-default offset |
| 715 |
bkover); |
| 716 |
} |
| 717 |
|
| 718 |
caryll_Buffer *buf = bk_build_Block(st); |
| 719 |
bufseek(buf, 2); |
| 720 |
bufwrite32b(buf, (uint32_t)buflen(buf)); |
| 721 |
return buf; |
| 722 |
} |
| 723 |
|
| 724 |
caryll_Buffer *otfcc_buildCmap(const table_cmap *cmap, const otfcc_Options *options) { |
| 725 |
if (!cmap || !cmap->unicodes) return NULL; |
| 726 |
|
| 727 |
cmap_Entry *entry; |
| 728 |
bool requiresFormat12 = false; |
| 729 |
bool hasUVS = cmap->uvs && (HASH_COUNT(cmap->uvs) > 0); |
| 730 |
foreach_hash(entry, cmap->unicodes) { |
| 731 |
if (entry->unicode > 0xFFFF) { requiresFormat12 = true; } |
| 732 |
} |
| 733 |
|
| 734 |
caryll_Buffer *format4 = NULL; |
| 735 |
if (!requiresFormat12 || !options->stub_cmap4) { |
| 736 |
format4 = otfcc_tryBuildCmap_format4(cmap); |
| 737 |
if (!format4) |
| 738 |
requiresFormat12 = true; |
| 739 |
} |
| 740 |
|
| 741 |
uint8_t nTables = requiresFormat12 ? 4 : 2; |
| 742 |
if (hasUVS) nTables += 1; |
| 743 |
|
| 744 |
if (!format4) { |
| 745 |
// Write a dummy |
| 746 |
format4 = bufnew(); |
| 747 |
bufwrite16b(format4, 4); // format |
| 748 |
bufwrite16b(format4, 32); // length |
| 749 |
bufwrite16b(format4, 0); // language |
| 750 |
bufwrite16b(format4, 4); // segCountX2 |
| 751 |
bufwrite16b(format4, 4); // searchRange |
| 752 |
bufwrite16b(format4, 1); // entrySelector |
| 753 |
bufwrite16b(format4, 0); // rangeShift |
| 754 |
bufwrite16b(format4, 0); // endCount |
| 755 |
bufwrite16b(format4, 0xFFFF); // endCount |
| 756 |
bufwrite16b(format4, 0); // endCount |
| 757 |
bufwrite16b(format4, 0); // startCount |
| 758 |
bufwrite16b(format4, 0xFFFF); // startCount |
| 759 |
bufwrite16b(format4, 0); // idDelta |
| 760 |
bufwrite16b(format4, 1); // idDelta |
| 761 |
bufwrite16b(format4, 0); // idRangeOffset |
| 762 |
bufwrite16b(format4, 0); // idRangeOffset |
| 763 |
} |
| 764 |
|
| 765 |
caryll_Buffer *format12 = otfcc_buildCmap_format12(cmap); |
| 766 |
|
| 767 |
bk_Block *root = bk_new_Block(b16, 0, // version |
| 768 |
b16, nTables, // nTables |
| 769 |
bkover); |
| 770 |
bk_push(root, b16, 0, // unicode |
| 771 |
b16, 3, // BMP |
| 772 |
p32, bk_newBlockFromBufferCopy(format4), // table |
| 773 |
bkover); |
| 774 |
if (requiresFormat12) { |
| 775 |
bk_push(root, b16, 0, // unicode |
| 776 |
b16, 4, // full |
| 777 |
p32, bk_newBlockFromBufferCopy(format12), // table |
| 778 |
bkover); |
| 779 |
} |
| 780 |
if (hasUVS) { |
| 781 |
caryll_Buffer *format14 = otfcc_buildCmap_format14(cmap); |
| 782 |
bk_push(root, b16, 0, // Unicode |
| 783 |
b16, 5, // Variation |
| 784 |
p32, bk_newBlockFromBuffer(format14), // table |
| 785 |
bkover); |
| 786 |
} |
| 787 |
bk_push(root, b16, 3, // Windows |
| 788 |
b16, 1, // Unicode BMP |
| 789 |
p32, bk_newBlockFromBufferCopy(format4), // table |
| 790 |
bkover); |
| 791 |
if (requiresFormat12) { |
| 792 |
bk_push(root, b16, 3, // Windows |
| 793 |
b16, 10, // Unicode Full |
| 794 |
p32, bk_newBlockFromBufferCopy(format12), // table |
| 795 |
bkover); |
| 796 |
} |
| 797 |
|
| 798 |
buffree(format4); |
| 799 |
buffree(format12); |
| 800 |
return bk_build_Block(root); |
| 801 |
} |