| 1 |
#include "consolidate.h" |
| 2 |
|
| 3 |
#include "support/util.h" |
| 4 |
#include "table/all.h" |
| 5 |
|
| 6 |
#include "otl/gsub-single.h" |
| 7 |
#include "otl/gsub-multi.h" |
| 8 |
#include "otl/gsub-ligature.h" |
| 9 |
#include "otl/gsub-reverse.h" |
| 10 |
#include "otl/gpos-single.h" |
| 11 |
#include "otl/gpos-pair.h" |
| 12 |
#include "otl/gpos-cursive.h" |
| 13 |
#include "otl/chaining.h" |
| 14 |
#include "otl/mark.h" |
| 15 |
#include "otl/GDEF.h" |
| 16 |
|
| 17 |
// Consolidation |
| 18 |
// Replace name entries in json to ids and do some check |
| 19 |
static int by_stem_pos(const glyf_PostscriptStemDef *a, const glyf_PostscriptStemDef *b) { |
| 20 |
if (a->position == b->position) { |
| 21 |
return (int)a->map - (int)b->map; |
| 22 |
} else if (a->position > b->position) { |
| 23 |
return 1; |
| 24 |
} else { |
| 25 |
return -1; |
| 26 |
} |
| 27 |
} |
| 28 |
static int by_mask_pointindex(const glyf_PostscriptHintMask *a, const glyf_PostscriptHintMask *b) { |
| 29 |
return a->contoursBefore == b->contoursBefore ? a->pointsBefore - b->pointsBefore |
| 30 |
: a->contoursBefore - b->contoursBefore; |
| 31 |
} |
| 32 |
|
| 33 |
static void consolidateGlyphContours(glyf_Glyph *g, const otfcc_Options *options) { |
| 34 |
shapeid_t nContoursConsolidated = 0; |
| 35 |
shapeid_t skip = 0; |
| 36 |
for (shapeid_t j = 0; j < g->contours.length; j++) { |
| 37 |
if (g->contours.items[j].length) { |
| 38 |
g->contours.items[j - skip] = g->contours.items[j]; |
| 39 |
nContoursConsolidated += 1; |
| 40 |
} else { |
| 41 |
glyf_iContourList.disposeItem(&g->contours, j); |
| 42 |
logWarning("[Consolidate] Removed empty contour #%d in glyph %s.\n", j, g->name); |
| 43 |
skip += 1; |
| 44 |
} |
| 45 |
} |
| 46 |
g->contours.length = nContoursConsolidated; |
| 47 |
} |
| 48 |
static void consolidateGlyphReferences(glyf_Glyph *g, otfcc_Font *font, |
| 49 |
const otfcc_Options *options) { |
| 50 |
shapeid_t nReferencesConsolidated = 0; |
| 51 |
shapeid_t skip = 0; |
| 52 |
for (shapeid_t j = 0; j < g->references.length; j++) { |
| 53 |
if (!GlyphOrder.consolidateHandle(font->glyph_order, &g->references.items[j].glyph)) { |
| 54 |
logWarning("[Consolidate] Ignored absent glyph component reference /%s within /%s.\n", |
| 55 |
g->references.items[j].glyph.name, g->name); |
| 56 |
glyf_iReferenceList.disposeItem(&(g->references), j); |
| 57 |
skip += 1; |
| 58 |
} else { |
| 59 |
g->references.items[j - skip] = g->references.items[j]; |
| 60 |
nReferencesConsolidated += 1; |
| 61 |
} |
| 62 |
} |
| 63 |
g->references.length = nReferencesConsolidated; |
| 64 |
} |
| 65 |
static void consolidateGlyphHints(glyf_Glyph *g, const otfcc_Options *options) { |
| 66 |
// sort stems |
| 67 |
if (g->stemH.length) { |
| 68 |
for (shapeid_t j = 0; j < g->stemH.length; j++) { |
| 69 |
g->stemH.items[j].map = j; |
| 70 |
} |
| 71 |
glyf_iStemDefList.sort(&g->stemH, by_stem_pos); |
| 72 |
} |
| 73 |
if (g->stemV.length) { |
| 74 |
for (shapeid_t j = 0; j < g->stemV.length; j++) { |
| 75 |
g->stemV.items[j].map = j; |
| 76 |
} |
| 77 |
glyf_iStemDefList.sort(&g->stemV, by_stem_pos); |
| 78 |
} |
| 79 |
shapeid_t *hmap; |
| 80 |
NEW(hmap, g->stemH.length); |
| 81 |
shapeid_t *vmap; |
| 82 |
NEW(vmap, g->stemV.length); |
| 83 |
for (shapeid_t j = 0; j < g->stemH.length; j++) { |
| 84 |
hmap[g->stemH.items[j].map] = j; |
| 85 |
} |
| 86 |
for (shapeid_t j = 0; j < g->stemV.length; j++) { |
| 87 |
vmap[g->stemV.items[j].map] = j; |
| 88 |
} |
| 89 |
// sort masks |
| 90 |
if (g->hintMasks.length) { |
| 91 |
glyf_iMaskList.sort(&g->hintMasks, by_mask_pointindex); |
| 92 |
for (shapeid_t j = 0; j < g->hintMasks.length; j++) { |
| 93 |
glyf_PostscriptHintMask oldmask = g->hintMasks.items[j]; // copy |
| 94 |
for (shapeid_t k = 0; k < g->stemH.length; k++) { |
| 95 |
g->hintMasks.items[j].maskH[k] = oldmask.maskH[hmap[k]]; |
| 96 |
} |
| 97 |
for (shapeid_t k = 0; k < g->stemV.length; k++) { |
| 98 |
g->hintMasks.items[j].maskV[k] = oldmask.maskV[vmap[k]]; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
if (g->contourMasks.length) { |
| 103 |
glyf_iMaskList.sort(&g->contourMasks, by_mask_pointindex); |
| 104 |
for (shapeid_t j = 0; j < g->contourMasks.length; j++) { |
| 105 |
glyf_PostscriptHintMask oldmask = g->contourMasks.items[j]; // copy |
| 106 |
for (shapeid_t k = 0; k < g->stemH.length; k++) { |
| 107 |
g->contourMasks.items[j].maskH[k] = oldmask.maskH[hmap[k]]; |
| 108 |
} |
| 109 |
for (shapeid_t k = 0; k < g->stemV.length; k++) { |
| 110 |
g->contourMasks.items[j].maskV[k] = oldmask.maskV[vmap[k]]; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
FREE(hmap); |
| 115 |
FREE(vmap); |
| 116 |
} |
| 117 |
static void consolidateFDSelect(fd_handle *h, table_CFF *cff, const otfcc_Options *options, |
| 118 |
const sds gname) { |
| 119 |
if (!cff || !cff->fdArray || !cff->fdArrayCount) return; |
| 120 |
// Consolidate fdSelect |
| 121 |
if (h->state == HANDLE_STATE_INDEX) { |
| 122 |
if (h->index >= cff->fdArrayCount) { h->index = 0; } |
| 123 |
Handle.consolidateTo(h, h->index, cff->fdArray[h->index]->fontName); |
| 124 |
} else if (h->name) { |
| 125 |
bool found = false; |
| 126 |
for (tableid_t j = 0; j < cff->fdArrayCount; j++) { |
| 127 |
if (strcmp(h->name, cff->fdArray[j]->fontName) == 0) { |
| 128 |
found = true; |
| 129 |
Handle.consolidateTo(h, j, cff->fdArray[j]->fontName); |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
if (!found) { |
| 134 |
logWarning("[Consolidate] CID Subfont %s is not defined. (in glyph /%s).\n", h->name, |
| 135 |
gname); |
| 136 |
Handle.dispose(h); |
| 137 |
} |
| 138 |
} else if (h->name) { |
| 139 |
Handle.dispose(h); |
| 140 |
} |
| 141 |
} |
| 142 |
static void consolidateGlyph(glyf_Glyph *g, otfcc_Font *font, const otfcc_Options *options) { |
| 143 |
consolidateGlyphContours(g, options); |
| 144 |
consolidateGlyphReferences(g, font, options); |
| 145 |
consolidateGlyphHints(g, options); |
| 146 |
consolidateFDSelect(&g->fdSelect, font->CFF_, options, g->name); |
| 147 |
} |
| 148 |
|
| 149 |
// Anchored reference consolidation |
| 150 |
bool consolidateAnchorRef(table_glyf *table, glyf_ComponentReference *gr, |
| 151 |
glyf_ComponentReference *rr, const otfcc_Options *options); |
| 152 |
static bool getPointCoordinates(table_glyf *table, glyf_ComponentReference *gr, shapeid_t n, |
| 153 |
shapeid_t *stated, VQ *x, VQ *y, const otfcc_Options *options) { |
| 154 |
glyphid_t j = gr->glyph.index; |
| 155 |
glyf_Glyph *g = table->items[j]; |
| 156 |
for (shapeid_t c = 0; c < g->contours.length; c++) { |
| 157 |
for (shapeid_t pj = 0; pj < g->contours.items[c].length; pj++) { |
| 158 |
if (*stated == n) { |
| 159 |
glyf_Point *p = &(g->contours.items[c].items[pj]); |
| 160 |
iVQ.replace(x, iVQ.pointLinearTfm(gr->x, gr->a, p->x, gr->b, p->y)); |
| 161 |
iVQ.replace(y, iVQ.pointLinearTfm(gr->y, gr->c, p->x, gr->d, p->y)); |
| 162 |
return true; |
| 163 |
} |
| 164 |
*stated += 1; |
| 165 |
} |
| 166 |
} |
| 167 |
for (shapeid_t r = 0; r < g->references.length; r++) { |
| 168 |
glyf_ComponentReference *rr = &(g->references.items[r]); |
| 169 |
|
| 170 |
consolidateAnchorRef(table, gr, rr, options); |
| 171 |
|
| 172 |
// composite affine transformations |
| 173 |
glyf_ComponentReference ref = glyf_iComponentReference.empty(); |
| 174 |
ref.glyph = Handle.fromIndex(g->references.items[r].glyph.index); |
| 175 |
ref.a = gr->a * rr->a + rr->b * gr->c; |
| 176 |
ref.b = rr->a * gr->b + rr->b * gr->d; |
| 177 |
ref.c = gr->a * rr->c + gr->c * rr->d; |
| 178 |
ref.d = gr->b * rr->c + rr->d * gr->d; |
| 179 |
iVQ.replace(&ref.x, iVQ.pointLinearTfm(rr->x, rr->a, gr->x, rr->b, gr->y)); |
| 180 |
iVQ.replace(&ref.y, iVQ.pointLinearTfm(rr->y, rr->c, gr->x, rr->d, gr->y)); |
| 181 |
|
| 182 |
bool success = getPointCoordinates(table, &ref, n, stated, x, y, options); |
| 183 |
glyf_iComponentReference.dispose(&ref); |
| 184 |
if (success) return true; |
| 185 |
} |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
bool consolidateAnchorRef(table_glyf *table, glyf_ComponentReference *gr, |
| 190 |
glyf_ComponentReference *rr, const otfcc_Options *options) { |
| 191 |
if (rr->isAnchored == REF_ANCHOR_CONSOLIDATED || rr->isAnchored == REF_XY) return true; |
| 192 |
if (rr->isAnchored == REF_ANCHOR_CONSOLIDATING_ANCHOR || |
| 193 |
rr->isAnchored == REF_ANCHOR_CONSOLIDATING_XY) { |
| 194 |
logWarning( |
| 195 |
"Found circular reference of out-of-range point reference in anchored reference."); |
| 196 |
rr->isAnchored = REF_XY; |
| 197 |
return false; |
| 198 |
} |
| 199 |
if (rr->isAnchored == REF_ANCHOR_ANCHOR) { |
| 200 |
rr->isAnchored = REF_ANCHOR_CONSOLIDATING_ANCHOR; |
| 201 |
} else { |
| 202 |
rr->isAnchored = REF_ANCHOR_CONSOLIDATING_XY; |
| 203 |
} |
| 204 |
VQ innerX = iVQ.neutral(), outerX = iVQ.neutral(); |
| 205 |
VQ innerY = iVQ.neutral(), outerY = iVQ.neutral(); |
| 206 |
shapeid_t innerCounter = 0, outerCounter = 0; |
| 207 |
|
| 208 |
glyf_ComponentReference rr1 = glyf_iComponentReference.empty(); |
| 209 |
rr1.glyph = Handle.fromIndex(rr->glyph.index); |
| 210 |
|
| 211 |
bool s1 = getPointCoordinates(table, gr, rr->outer, &outerCounter, &outerX, &outerY, options); |
| 212 |
bool s2 = getPointCoordinates(table, &rr1, rr->inner, &innerCounter, &innerX, &innerY, options); |
| 213 |
if (!s1) { logWarning("Failed to access point %d in outer glyph.", rr->outer); } |
| 214 |
if (!s2) { |
| 215 |
logWarning("Failed to access point %d in reference to %s.", rr->outer, rr->glyph.name); |
| 216 |
} |
| 217 |
|
| 218 |
VQ rrx = iVQ.pointLinearTfm(outerX, -rr->a, innerX, -rr->b, innerY); |
| 219 |
VQ rry = iVQ.pointLinearTfm(outerY, -rr->c, innerX, -rr->d, innerY); |
| 220 |
|
| 221 |
if (rr->isAnchored == REF_ANCHOR_CONSOLIDATING_ANCHOR) { |
| 222 |
iVQ.replace(&rr->x, rrx); |
| 223 |
iVQ.replace(&rr->y, rry); |
| 224 |
rr->isAnchored = REF_ANCHOR_CONSOLIDATED; |
| 225 |
} else { |
| 226 |
if (fabs(iVQ.getStill(rr->x) - iVQ.getStill(rrx)) > 0.5 && |
| 227 |
fabs(iVQ.getStill(rr->y) - iVQ.getStill(rry)) > 0.5) { |
| 228 |
logWarning("Anchored reference to %s does not match its X/Y offset data.", |
| 229 |
rr->glyph.name); |
| 230 |
} |
| 231 |
rr->isAnchored = REF_ANCHOR_CONSOLIDATED; |
| 232 |
iVQ.dispose(&rrx), iVQ.dispose(&rry); |
| 233 |
} |
| 234 |
glyf_iComponentReference.dispose(&rr1); |
| 235 |
iVQ.dispose(&innerX), iVQ.dispose(&innerY); |
| 236 |
iVQ.dispose(&outerX), iVQ.dispose(&outerY); |
| 237 |
return false; |
| 238 |
} |
| 239 |
|
| 240 |
static void consolidateGlyf(otfcc_Font *font, const otfcc_Options *options) { |
| 241 |
if (!font->glyph_order || !font->glyf) return; |
| 242 |
for (glyphid_t j = 0; j < font->glyf->length; j++) { |
| 243 |
if (font->glyf->items[j]) { |
| 244 |
consolidateGlyph(font->glyf->items[j], font, options); |
| 245 |
} else { |
| 246 |
font->glyf->items[j] = otfcc_newGlyf_glyph(); |
| 247 |
} |
| 248 |
} |
| 249 |
for (glyphid_t j = 0; j < font->glyf->length; j++) { |
| 250 |
glyf_Glyph *g = font->glyf->items[j]; |
| 251 |
loggedStep("%s", g->name) { |
| 252 |
glyf_ComponentReference gr = glyf_iComponentReference.empty(); |
| 253 |
gr.glyph = Handle.fromIndex(j); |
| 254 |
|
| 255 |
for (shapeid_t r = 0; r < g->references.length; r++) { |
| 256 |
glyf_ComponentReference *rr = &(g->references.items[r]); |
| 257 |
consolidateAnchorRef(font->glyf, &gr, rr, options); |
| 258 |
} |
| 259 |
glyf_iComponentReference.dispose(&gr); |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
static void consolidateCmap(otfcc_Font *font, const otfcc_Options *options) { |
| 265 |
if (font->glyph_order && font->cmap) { |
| 266 |
cmap_Entry *item; |
| 267 |
foreach_hash(item, font->cmap->unicodes) { |
| 268 |
if (!GlyphOrder.consolidateHandle(font->glyph_order, &item->glyph)) { |
| 269 |
logWarning("[Consolidate] Ignored mapping U+%04X to non-existent glyph /%s.\n", |
| 270 |
item->unicode, item->glyph.name); |
| 271 |
Handle.dispose(&item->glyph); |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
if (font->glyph_order && font->cmap) { |
| 276 |
cmap_UVS_Entry *item; |
| 277 |
foreach_hash(item, font->cmap->uvs) { |
| 278 |
if (!GlyphOrder.consolidateHandle(font->glyph_order, &item->glyph)) { |
| 279 |
logWarning("[Consolidate] Ignored UVS mapping [U+%04X U+%04X] to non-existent " |
| 280 |
"glyph /%s.\n", |
| 281 |
item->key.unicode, item->key.selector, item->glyph.name); |
| 282 |
Handle.dispose(&item->glyph); |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
typedef bool (*otl_consolidation_function)(otfcc_Font *, table_OTL *, otl_Subtable *, |
| 289 |
const otfcc_Options *); |
| 290 |
typedef void (*subtable_remover)(otl_Subtable *); |
| 291 |
#define LOOKUP_CONSOLIDATOR(llt, fn, fndel) \ |
| 292 |
__declare_otl_consolidation(llt, fn, (subtable_remover)fndel, font, table, lookup, options); |
| 293 |
|
| 294 |
static void __declare_otl_consolidation(otl_LookupType type, otl_consolidation_function fn, |
| 295 |
subtable_remover fndel, otfcc_Font *font, table_OTL *table, |
| 296 |
otl_Lookup *lookup, const otfcc_Options *options) { |
| 297 |
if (!lookup || !lookup->subtables.length || lookup->type != type) return; |
| 298 |
loggedStep("%s", lookup->name) { |
| 299 |
for (tableid_t j = 0; j < lookup->subtables.length; j++) { |
| 300 |
if (!lookup->subtables.items[j]) { |
| 301 |
logWarning("[Consolidate] Ignored empty subtable %d of lookup %s.\n", j, |
| 302 |
lookup->name); |
| 303 |
continue; |
| 304 |
} |
| 305 |
bool subtableRemoved; |
| 306 |
// loggedStep("Subtable %d", j) { |
| 307 |
subtableRemoved = fn(font, table, lookup->subtables.items[j], options); |
| 308 |
//} |
| 309 |
if (subtableRemoved) { |
| 310 |
fndel(lookup->subtables.items[j]); |
| 311 |
lookup->subtables.items[j] = NULL; |
| 312 |
logWarning("[Consolidate] Ignored empty subtable %d of lookup %s.\n", j, |
| 313 |
lookup->name); |
| 314 |
} |
| 315 |
} |
| 316 |
tableid_t k = 0; |
| 317 |
for (tableid_t j = 0; j < lookup->subtables.length; j++) { |
| 318 |
if (lookup->subtables.items[j]) { |
| 319 |
lookup->subtables.items[k++] = lookup->subtables.items[j]; |
| 320 |
} |
| 321 |
} |
| 322 |
lookup->subtables.length = k; |
| 323 |
if (!k) { |
| 324 |
logWarning("[Consolidate] Lookup %s is empty and will be removed.\n", lookup->name); |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
static void otfcc_consolidate_lookup(otfcc_Font *font, table_OTL *table, otl_Lookup *lookup, |
| 330 |
const otfcc_Options *options) { |
| 331 |
LOOKUP_CONSOLIDATOR(otl_type_gsub_single, consolidate_gsub_single, iSubtable_gsub_single.free); |
| 332 |
LOOKUP_CONSOLIDATOR(otl_type_gsub_multiple, consolidate_gsub_multi, iSubtable_gsub_multi.free); |
| 333 |
LOOKUP_CONSOLIDATOR(otl_type_gsub_alternate, consolidate_gsub_alternative, |
| 334 |
iSubtable_gsub_multi.free); |
| 335 |
LOOKUP_CONSOLIDATOR(otl_type_gsub_ligature, consolidate_gsub_ligature, |
| 336 |
iSubtable_gsub_ligature.free); |
| 337 |
LOOKUP_CONSOLIDATOR(otl_type_gsub_chaining, consolidate_chaining, iSubtable_chaining.free); |
| 338 |
LOOKUP_CONSOLIDATOR(otl_type_gsub_reverse, consolidate_gsub_reverse, |
| 339 |
iSubtable_gsub_reverse.free); |
| 340 |
LOOKUP_CONSOLIDATOR(otl_type_gpos_single, consolidate_gpos_single, iSubtable_gpos_single.free); |
| 341 |
LOOKUP_CONSOLIDATOR(otl_type_gpos_pair, consolidate_gpos_pair, iSubtable_gpos_pair.free); |
| 342 |
LOOKUP_CONSOLIDATOR(otl_type_gpos_cursive, consolidate_gpos_cursive, |
| 343 |
iSubtable_gpos_cursive.free); |
| 344 |
LOOKUP_CONSOLIDATOR(otl_type_gpos_chaining, consolidate_chaining, iSubtable_chaining.free); |
| 345 |
LOOKUP_CONSOLIDATOR(otl_type_gpos_markToBase, consolidate_mark_to_single, |
| 346 |
iSubtable_gpos_markToSingle.free); |
| 347 |
LOOKUP_CONSOLIDATOR(otl_type_gpos_markToMark, consolidate_mark_to_single, |
| 348 |
iSubtable_gpos_markToSingle.free); |
| 349 |
LOOKUP_CONSOLIDATOR(otl_type_gpos_markToLigature, consolidate_mark_to_ligature, |
| 350 |
iSubtable_gpos_markToLigature.free); |
| 351 |
} |
| 352 |
|
| 353 |
static bool lookupRefIsNotEmpty(const otl_LookupRef *rLut, void *env) { |
| 354 |
return rLut && *rLut && (*rLut)->subtables.length > 0; |
| 355 |
} |
| 356 |
static bool featureRefIsNotEmpty(const otl_FeatureRef *rFeat, void *env) { |
| 357 |
return rFeat && *rFeat && (*rFeat)->lookups.length > 0; |
| 358 |
} |
| 359 |
static bool lookupIsNotEmpty(const otl_LookupPtr *rLut, void *env) { |
| 360 |
return rLut && *rLut && (*rLut)->subtables.length > 0; |
| 361 |
} |
| 362 |
static bool featureIsNotEmpty(const otl_FeaturePtr *rFeat, void *env) { |
| 363 |
return rFeat && *rFeat && (*rFeat)->lookups.length > 0; |
| 364 |
} |
| 365 |
|
| 366 |
static void consolidateOTLTable(otfcc_Font *font, table_OTL *table, const otfcc_Options *options) { |
| 367 |
if (!font->glyph_order || !table) return; |
| 368 |
do { |
| 369 |
tableid_t featN = table->features.length; |
| 370 |
tableid_t lutN = table->lookups.length; |
| 371 |
|
| 372 |
// Perform consolidation |
| 373 |
for (tableid_t j = 0; j < table->lookups.length; j++) { |
| 374 |
otfcc_consolidate_lookup(font, table, table->lookups.items[j], options); |
| 375 |
} |
| 376 |
// remove empty features |
| 377 |
for (tableid_t j = 0; j < table->features.length; j++) { |
| 378 |
otl_Feature *feature = table->features.items[j]; |
| 379 |
otl_iLookupRefList.filterEnv(&feature->lookups, lookupRefIsNotEmpty, NULL); |
| 380 |
} |
| 381 |
// remove empty lookups |
| 382 |
for (tableid_t j = 0; j < table->languages.length; j++) { |
| 383 |
otl_LanguageSystem *lang = table->languages.items[j]; |
| 384 |
otl_iFeatureRefList.filterEnv(&lang->features, featureRefIsNotEmpty, NULL); |
| 385 |
} |
| 386 |
otl_iLookupList.filterEnv(&table->lookups, lookupIsNotEmpty, NULL); |
| 387 |
otl_iFeatureList.filterEnv(&table->features, featureIsNotEmpty, NULL); |
| 388 |
|
| 389 |
tableid_t featN1 = table->features.length; |
| 390 |
tableid_t lutN1 = table->lookups.length; |
| 391 |
if (featN1 >= featN && lutN1 >= lutN) break; |
| 392 |
} while (true); |
| 393 |
} |
| 394 |
|
| 395 |
static void consolidateOTL(otfcc_Font *font, const otfcc_Options *options) { |
| 396 |
loggedStep("GSUB") { |
| 397 |
consolidateOTLTable(font, font->GSUB, options); |
| 398 |
} |
| 399 |
loggedStep("GPOS") { |
| 400 |
consolidateOTLTable(font, font->GPOS, options); |
| 401 |
} |
| 402 |
loggedStep("GDEF") { |
| 403 |
consolidate_GDEF(font, font->GDEF, options); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
static void consolidateCOLR(otfcc_Font *font, const otfcc_Options *options) { |
| 408 |
if (!font || !font->COLR || !font->glyph_order) return; |
| 409 |
table_COLR *consolidated = table_iCOLR.create(); |
| 410 |
foreach (colr_Mapping *mapping, *(font->COLR)) { |
| 411 |
if (!GlyphOrder.consolidateHandle(font->glyph_order, &mapping->glyph)) { |
| 412 |
logWarning("[Consolidate] Ignored missing glyph of /%s", mapping->glyph.name); |
| 413 |
continue; |
| 414 |
} |
| 415 |
colr_Mapping m; |
| 416 |
Handle.copy(&m.glyph, &mapping->glyph); |
| 417 |
colr_iLayerList.init(&m.layers); |
| 418 |
foreach (colr_Layer *layer, mapping->layers) { |
| 419 |
if (!GlyphOrder.consolidateHandle(font->glyph_order, &layer->glyph)) { |
| 420 |
logWarning("[Consolidate] Ignored missing glyph of /%s", layer->glyph.name); |
| 421 |
continue; |
| 422 |
} |
| 423 |
colr_Layer layer1; |
| 424 |
colr_iLayer.copy(&layer1, layer); |
| 425 |
colr_iLayerList.push(&m.layers, layer1); |
| 426 |
} |
| 427 |
if (mapping->layers.length) { |
| 428 |
table_iCOLR.push(consolidated, m); |
| 429 |
} else { |
| 430 |
logWarning("[Consolidate] COLR decomposition for /%s is empth", mapping->glyph.name); |
| 431 |
colr_iMapping.dispose(&m); |
| 432 |
} |
| 433 |
} |
| 434 |
table_iCOLR.free(font->COLR); |
| 435 |
font->COLR = consolidated; |
| 436 |
} |
| 437 |
|
| 438 |
static int compareTSIEntry(const tsi_Entry *a, const tsi_Entry *b) { |
| 439 |
if (a->type != b->type) return a->type - b->type; |
| 440 |
return a->glyph.index - b->glyph.index; |
| 441 |
} |
| 442 |
|
| 443 |
static void consolidateTSI(otfcc_Font *font, table_TSI **_tsi, const otfcc_Options *options) { |
| 444 |
table_TSI *tsi = *_tsi; |
| 445 |
if (!font || !font->glyf || !tsi || !font->glyph_order) return; |
| 446 |
table_TSI *consolidated = table_iTSI.create(); |
| 447 |
sds *gidEntries; |
| 448 |
NEW_CLEAN_N(gidEntries, font->glyf->length); |
| 449 |
|
| 450 |
foreach (tsi_Entry *entry, *tsi) { |
| 451 |
if (entry->type == TSI_GLYPH) { |
| 452 |
if (GlyphOrder.consolidateHandle(font->glyph_order, &entry->glyph)) { |
| 453 |
if (gidEntries[entry->glyph.index]) sdsfree(gidEntries[entry->glyph.index]); |
| 454 |
gidEntries[entry->glyph.index] = entry->content; |
| 455 |
entry->content = NULL; |
| 456 |
} else { |
| 457 |
logWarning("[Consolidate] Ignored missing glyph of /%s", entry->glyph.name); |
| 458 |
} |
| 459 |
} else { |
| 460 |
tsi_Entry e; |
| 461 |
tsi_iEntry.copy(&e, entry); |
| 462 |
table_iTSI.push(consolidated, e); |
| 463 |
} |
| 464 |
} |
| 465 |
for (glyphid_t j = 0; j < font->glyf->length; j++) { |
| 466 |
tsi_Entry e; |
| 467 |
e.type = TSI_GLYPH; |
| 468 |
e.glyph = Handle.fromIndex(j); |
| 469 |
GlyphOrder.consolidateHandle(font->glyph_order, &e.glyph); |
| 470 |
e.content = gidEntries[j] ? gidEntries[j] : sdsempty(); |
| 471 |
table_iTSI.push(consolidated, e); |
| 472 |
} |
| 473 |
table_iTSI.free(tsi); |
| 474 |
FREE(gidEntries); |
| 475 |
table_iTSI.sort(consolidated, compareTSIEntry); |
| 476 |
*_tsi = consolidated; |
| 477 |
} |
| 478 |
|
| 479 |
void otfcc_consolidateFont(otfcc_Font *font, const otfcc_Options *options) { |
| 480 |
// In case we don’t have a glyph order, make one. |
| 481 |
if (font->glyf && !font->glyph_order) { |
| 482 |
otfcc_GlyphOrder *go = GlyphOrder.create(); |
| 483 |
for (glyphid_t j = 0; j < font->glyf->length; j++) { |
| 484 |
sds name; |
| 485 |
sds glyfName = font->glyf->items[j]->name; |
| 486 |
if (glyfName) { |
| 487 |
name = sdsdup(glyfName); |
| 488 |
} else { |
| 489 |
name = sdscatprintf(sdsempty(), "$$gid%d", j); |
| 490 |
font->glyf->items[j]->name = sdsdup(name); |
| 491 |
} |
| 492 |
if (!GlyphOrder.setByName(go, name, j)) { |
| 493 |
logWarning("[Consolidate] Glyph name %s is already in use.", name); |
| 494 |
uint32_t suffix = 2; |
| 495 |
bool success = false; |
| 496 |
do { |
| 497 |
sds newname = sdscatfmt(sdsempty(), "%s_%u", name, suffix); |
| 498 |
success = GlyphOrder.setByName(go, newname, j); |
| 499 |
if (!success) { |
| 500 |
sdsfree(newname); |
| 501 |
suffix += 1; |
| 502 |
} else { |
| 503 |
logWarning("[Consolidate] Glyph %s is renamed into %s.", name, newname); |
| 504 |
sdsfree(font->glyf->items[j]->name); |
| 505 |
font->glyf->items[j]->name = sdsdup(newname); |
| 506 |
} |
| 507 |
} while (!success); |
| 508 |
sdsfree(name); |
| 509 |
} |
| 510 |
} |
| 511 |
font->glyph_order = go; |
| 512 |
} |
| 513 |
loggedStep("glyf") { |
| 514 |
consolidateGlyf(font, options); |
| 515 |
} |
| 516 |
loggedStep("cmap") { |
| 517 |
consolidateCmap(font, options); |
| 518 |
} |
| 519 |
if (font->glyf) consolidateOTL(font, options); |
| 520 |
loggedStep("COLR") { |
| 521 |
consolidateCOLR(font, options); |
| 522 |
} |
| 523 |
loggedStep("TSI_01") { |
| 524 |
consolidateTSI(font, &font->TSI_01, options); |
| 525 |
} |
| 526 |
loggedStep("TSI_23") { |
| 527 |
consolidateTSI(font, &font->TSI_23, options); |
| 528 |
} |
| 529 |
loggedStep("TSI5") { |
| 530 |
fontop_consolidateClassDef(font, font->TSI5, options); |
| 531 |
} |
| 532 |
} |