| 1 |
#include "support/util.h" |
| 2 |
#include "otfcc/font.h" |
| 3 |
#include "table/all.h" |
| 4 |
|
| 5 |
static otfcc_font_subtype otfcc_decideFontSubtypeFromJson(const json_value *root) { |
| 6 |
if (json_obj_get_type(root, "CFF_", json_object) != NULL) { |
| 7 |
return FONTTYPE_CFF; |
| 8 |
} else { |
| 9 |
return FONTTYPE_TTF; |
| 10 |
} |
| 11 |
} |
| 12 |
|
| 13 |
// The default glyph_order object is completed using a two-step construction |
| 14 |
enum { ORD_GLYPHORDER = 1, ORD_NOTDEF = 2, ORD_CMAP = 3, ORD_GLYF = 4 }; |
| 15 |
|
| 16 |
// Register a name->(orderType, orderEntry) map. |
| 17 |
static void setOrderByName(otfcc_GlyphOrder *go, sds name, uint8_t orderType, uint32_t orderEntry) { |
| 18 |
otfcc_GlyphOrderEntry *s = NULL; |
| 19 |
HASH_FIND(hhName, go->byName, name, sdslen(name), s); |
| 20 |
if (!s) { |
| 21 |
NEW(s); |
| 22 |
s->gid = -1; |
| 23 |
s->name = name; |
| 24 |
s->orderType = orderType; |
| 25 |
s->orderEntry = orderEntry; |
| 26 |
HASH_ADD(hhName, go->byName, name[0], sdslen(s->name), s); |
| 27 |
} else if (s->orderType > orderType) { |
| 28 |
s->orderType = orderType; |
| 29 |
s->orderEntry = orderEntry; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
static int _byOrder(otfcc_GlyphOrderEntry *a, otfcc_GlyphOrderEntry *b) { |
| 34 |
if (a->orderType < b->orderType) return (-1); |
| 35 |
if (a->orderType > b->orderType) return (1); |
| 36 |
if (a->orderEntry < b->orderEntry) return (-1); |
| 37 |
if (a->orderEntry > b->orderEntry) return (1); |
| 38 |
return 0; |
| 39 |
} |
| 40 |
|
| 41 |
// Complete ClyphOrder |
| 42 |
static void orderGlyphs(otfcc_GlyphOrder *go) { |
| 43 |
HASH_SRT(hhName, go->byName, _byOrder); |
| 44 |
otfcc_GlyphOrderEntry *current, *temp; |
| 45 |
glyphid_t gid = 0; |
| 46 |
HASH_ITER(hhName, go->byName, current, temp) { |
| 47 |
current->gid = gid; |
| 48 |
HASH_ADD(hhID, go->byGID, gid, sizeof(glyphid_t), current); |
| 49 |
gid += 1; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
static void escalateGlyphOrderByName(otfcc_GlyphOrder *go, sds name, uint8_t orderType, |
| 54 |
uint32_t orderEntry) { |
| 55 |
otfcc_GlyphOrderEntry *s = NULL; |
| 56 |
HASH_FIND(hhName, go->byName, name, sdslen(name), s); |
| 57 |
if (s && s->orderType > orderType) { |
| 58 |
s->orderType = orderType; |
| 59 |
s->orderEntry = orderEntry; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
static void placeOrderEntriesFromGlyf(json_value *table, otfcc_GlyphOrder *go) { |
| 64 |
for (uint32_t j = 0; j < table->u.object.length; j++) { |
| 65 |
sds gname = |
| 66 |
sdsnewlen(table->u.object.values[j].name, table->u.object.values[j].name_length); |
| 67 |
if (strcmp(gname, ".notdef") == 0) { |
| 68 |
setOrderByName(go, gname, ORD_NOTDEF, 0); |
| 69 |
} else if (strcmp(gname, ".null") == 0) { |
| 70 |
setOrderByName(go, gname, ORD_NOTDEF, 1); |
| 71 |
} else { |
| 72 |
setOrderByName(go, gname, ORD_GLYF, j); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
static void placeOrderEntriesFromCmap(json_value *table, otfcc_GlyphOrder *go) { |
| 77 |
for (uint32_t j = 0; j < table->u.object.length; j++) { |
| 78 |
sds unicodeStr = |
| 79 |
sdsnewlen(table->u.object.values[j].name, table->u.object.values[j].name_length); |
| 80 |
json_value *item = table->u.object.values[j].value; |
| 81 |
int32_t unicode; |
| 82 |
if (sdslen(unicodeStr) > 2 && unicodeStr[0] == 'U' && unicodeStr[1] == '+') { |
| 83 |
unicode = strtol(unicodeStr + 2, NULL, 16); |
| 84 |
} else { |
| 85 |
unicode = atoi(unicodeStr); |
| 86 |
} |
| 87 |
sdsfree(unicodeStr); |
| 88 |
if (item->type == json_string && unicode > 0 && |
| 89 |
unicode <= 0x10FFFF) { // a valid unicode codepoint |
| 90 |
sds gname = sdsnewlen(item->u.string.ptr, item->u.string.length); |
| 91 |
escalateGlyphOrderByName(go, gname, ORD_CMAP, unicode); |
| 92 |
sdsfree(gname); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
static void placeOrderEntriesFromSubtable(json_value *table, otfcc_GlyphOrder *go, bool zeroOnly) { |
| 97 |
uint32_t uplimit = table->u.array.length; |
| 98 |
if (uplimit >= 1 && zeroOnly) { uplimit = 1; } |
| 99 |
for (uint32_t j = 0; j < uplimit; j++) { |
| 100 |
json_value *item = table->u.array.values[j]; |
| 101 |
if (item->type == json_string) { |
| 102 |
sds gname = sdsnewlen(item->u.string.ptr, item->u.string.length); |
| 103 |
escalateGlyphOrderByName(go, gname, ORD_GLYPHORDER, j); |
| 104 |
sdsfree(gname); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
static otfcc_GlyphOrder *parseGlyphOrder(const json_value *root, const otfcc_Options *options) { |
| 110 |
otfcc_GlyphOrder *go = GlyphOrder.create(); |
| 111 |
if (root->type != json_object) return go; |
| 112 |
json_value *table; |
| 113 |
|
| 114 |
if ((table = json_obj_get_type(root, "glyf", json_object))) { |
| 115 |
placeOrderEntriesFromGlyf(table, go); |
| 116 |
if ((table = json_obj_get_type(root, "cmap", json_object))) { |
| 117 |
placeOrderEntriesFromCmap(table, go); |
| 118 |
} |
| 119 |
if ((table = json_obj_get_type(root, "glyph_order", json_array))) { |
| 120 |
bool ignoreGlyphOrder = options->ignore_glyph_order; |
| 121 |
if (ignoreGlyphOrder && !!json_obj_get_type(root, "SVG_", json_array)) { |
| 122 |
logNotice("OpenType SVG table detected. Glyph order is preserved."); |
| 123 |
ignoreGlyphOrder = false; |
| 124 |
} |
| 125 |
placeOrderEntriesFromSubtable(table, go, ignoreGlyphOrder); |
| 126 |
} |
| 127 |
} |
| 128 |
orderGlyphs(go); |
| 129 |
return go; |
| 130 |
} |
| 131 |
|
| 132 |
static otfcc_Font *readJson(void *_root, uint32_t index, const otfcc_Options *options) { |
| 133 |
const json_value *root = (json_value *)_root; |
| 134 |
otfcc_Font *font = otfcc_iFont.create(); |
| 135 |
if (!font) return NULL; |
| 136 |
font->subtype = otfcc_decideFontSubtypeFromJson(root); |
| 137 |
font->glyph_order = parseGlyphOrder(root, options); |
| 138 |
font->glyf = otfcc_parseGlyf(root, font->glyph_order, options); |
| 139 |
font->CFF_ = otfcc_parseCFF(root, options); |
| 140 |
font->head = otfcc_parseHead(root, options); |
| 141 |
font->hhea = otfcc_parseHhea(root, options); |
| 142 |
font->OS_2 = otfcc_parseOS_2(root, options); |
| 143 |
font->maxp = otfcc_parseMaxp(root, options); |
| 144 |
font->post = otfcc_parsePost(root, options); |
| 145 |
font->name = otfcc_parseName(root, options); |
| 146 |
font->meta = otfcc_parseMeta(root, options); |
| 147 |
font->cmap = otfcc_parseCmap(root, options); |
| 148 |
if (!options->ignore_hints) { |
| 149 |
font->fpgm = otfcc_parseFpgmPrep(root, options, "fpgm"); |
| 150 |
font->prep = otfcc_parseFpgmPrep(root, options, "prep"); |
| 151 |
font->cvt_ = otfcc_parseCvt(root, options, "cvt_"); |
| 152 |
font->gasp = otfcc_parseGasp(root, options); |
| 153 |
} |
| 154 |
font->VDMX = otfcc_parseVDMX(root, options); |
| 155 |
font->vhea = otfcc_parseVhea(root, options); |
| 156 |
if (font->glyf) { |
| 157 |
font->GSUB = otfcc_parseOtl(root, options, "GSUB"); |
| 158 |
font->GPOS = otfcc_parseOtl(root, options, "GPOS"); |
| 159 |
font->GDEF = otfcc_parseGDEF(root, options); |
| 160 |
} |
| 161 |
font->BASE = otfcc_parseBASE(root, options); |
| 162 |
font->CPAL = otfcc_parseCPAL(root, options); |
| 163 |
font->COLR = otfcc_parseCOLR(root, options); |
| 164 |
font->SVG_ = otfcc_parseSVG(root, options); |
| 165 |
|
| 166 |
font->TSI_01 = otfcc_parseTSI(root, options, "TSI_01"); |
| 167 |
font->TSI_23 = otfcc_parseTSI(root, options, "TSI_23"); |
| 168 |
font->TSI5 = otfcc_parseTSI5(root, options); |
| 169 |
|
| 170 |
return font; |
| 171 |
} |
| 172 |
static INLINE void freeReader(otfcc_IFontBuilder *self) { |
| 173 |
free(self); |
| 174 |
} |
| 175 |
otfcc_IFontBuilder *otfcc_newJsonReader() { |
| 176 |
otfcc_IFontBuilder *reader; |
| 177 |
NEW(reader); |
| 178 |
reader->read = readJson; |
| 179 |
reader->free = freeReader; |
| 180 |
return reader; |
| 181 |
} |