Debian Codegraph @

texk/web2c/mfluadir/otfcc/lib/table/post.c

raw
1 #include "post.h"
2
3 #include "support/util.h"
4
5 // clang-format off
6 static const char *standardMacNames[258] = {".notdef", ".null", "nonmarkingreturn", "space", "exclam", "quotedbl", "numbersign", "dollar", "percent", "ampersand", "quotesingle", "parenleft", "parenright", "asterisk", "plus", "comma", "hyphen", "period", "slash", "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "colon", "semicolon", "less", "equal", "greater", "question", "at", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "bracketleft", "backslash", "bracketright", "asciicircum", "underscore", "grave", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "braceleft", "bar", "braceright", "asciitilde", "Adieresis", "Aring", "Ccedilla", "Eacute", "Ntilde", "Odieresis", "Udieresis", "aacute", "agrave", "acircumflex", "adieresis", "atilde", "aring", "ccedilla", "eacute", "egrave", "ecircumflex", "edieresis", "iacute", "igrave", "icircumflex", "idieresis", "ntilde", "oacute", "ograve", "ocircumflex", "odieresis", "otilde", "uacute", "ugrave", "ucircumflex", "udieresis", "dagger", "degree", "cent", "sterling", "section", "bullet", "paragraph", "germandbls", "registered", "copyright", "trademark", "acute", "dieresis", "notequal", "AE", "Oslash", "infinity", "plusminus", "lessequal", "greaterequal", "yen", "mu", "partialdiff", "summation", "product", "pi", "integral", "ordfeminine", "ordmasculine", "Omega", "ae", "oslash", "questiondown", "exclamdown", "logicalnot", "radical", "florin", "approxequal", "Delta", "guillemotleft", "guillemotright", "ellipsis", "nonbreakingspace", "Agrave", "Atilde", "Otilde", "OE", "oe", "endash", "emdash", "quotedblleft", "quotedblright", "quoteleft", "quoteright", "divide", "lozenge", "ydieresis", "Ydieresis", "fraction", "currency", "guilsinglleft", "guilsinglright", "fi", "fl", "daggerdbl", "periodcentered", "quotesinglbase", "quotedblbase", "perthousand", "Acircumflex", "Ecircumflex", "Aacute", "Edieresis", "Egrave", "Iacute", "Icircumflex", "Idieresis", "Igrave", "Oacute", "Ocircumflex", "apple", "Ograve", "Uacute", "Ucircumflex", "Ugrave", "dotlessi", "circumflex", "tilde", "macron", "breve", "dotaccent", "ring", "cedilla", "hungarumlaut", "ogonek", "caron", "Lslash", "lslash", "Scaron", "scaron", "Zcaron", "zcaron", "brokenbar", "Eth", "eth", "Yacute", "yacute", "Thorn", "thorn", "minus", "multiply", "onesuperior", "twosuperior", "threesuperior", "onehalf", "onequarter", "threequarters", "franc", "Gbreve", "gbreve", "Idotaccent", "Scedilla", "scedilla", "Cacute", "cacute", "Ccaron", "ccaron", "dcroat"};
7
8 // clang-format on
9
10 static INLINE void initPost(table_post *post) {
11 memset(post, 0, sizeof(*post));
12 post->version = 0x30000;
13 }
14 static INLINE void disposePost(MOVE table_post *post) {
15 if (post->post_name_map != NULL) { GlyphOrder.free(post->post_name_map); }
16 }
17
18 caryll_standardRefType(table_post, iTable_post, initPost, disposePost);
19
20 table_post *otfcc_readPost(const otfcc_Packet packet, const otfcc_Options *options) {
21 FOR_TABLE(OTFCC_CHR('p','o','s','t'), table) {
22 font_file_pointer data = table.data;
23
24 table_post *post = iTable_post.create();
25 post->version = read_32s(data);
26 post->italicAngle = read_32u(data + 4);
27 post->underlinePosition = read_16u(data + 8);
28 post->underlineThickness = read_16u(data + 10);
29 post->isFixedPitch = read_32u(data + 12);
30 post->minMemType42 = read_32u(data + 16);
31 post->maxMemType42 = read_32u(data + 20);
32 post->minMemType1 = read_32u(data + 24);
33 post->maxMemType1 = read_32u(data + 28);
34 post->post_name_map = NULL;
35 // Foamt 2 additional glyph names
36 if (post->version == 0x20000) {
37 otfcc_GlyphOrder *map = GlyphOrder.create();
38
39 sds pendingNames[0x10000];
40 memset(pendingNames, 0, sizeof(pendingNames));
41 uint16_t numberGlyphs = read_16u(data + 32);
42 uint32_t offset = 34 + 2 * numberGlyphs;
43 uint16_t pendingNameIndex = 0;
44 while (pendingNameIndex <= 0xFFFF && offset < table.length) {
45 uint8_t len = data[offset];
46 sds s;
47 if (len > 0) {
48 s = sdsnewlen(data + offset + 1, len);
49 } else {
50 s = sdsempty();
51 }
52 offset += len + 1;
53 pendingNames[pendingNameIndex] = s;
54 pendingNameIndex += 1;
55 }
56 for (uint16_t j = 0; j < numberGlyphs; j++) {
57 uint16_t nameMap = read_16u(data + 34 + 2 * j);
58 if (nameMap >= 258) { // Custom glyph name
59 GlyphOrder.setByGID(map, j, sdsdup(pendingNames[nameMap - 258]));
60 } else { // Standard Macintosh glyph name
61 GlyphOrder.setByGID(map, j, sdsnew(standardMacNames[nameMap]));
62 }
63 }
64 for (uint32_t j = 0; j < pendingNameIndex; j++) {
65 sdsfree(pendingNames[j]);
66 }
67 post->post_name_map = map;
68 }
69 return post;
70 }
71 return NULL;
72 }
73
74 void otfcc_dumpPost(const table_post *table, json_value *root, const otfcc_Options *options) {
75 if (!table) return;
76 loggedStep("post") {
77 json_value *post = json_object_new(10);
78 json_object_push(post, "version", json_double_new(otfcc_from_fixed(table->version)));
79 json_object_push(post, "italicAngle",
80 json_integer_new(otfcc_from_fixed(table->italicAngle)));
81 json_object_push(post, "underlinePosition", json_integer_new(table->underlinePosition));
82 json_object_push(post, "underlineThickness", json_integer_new(table->underlineThickness));
83 json_object_push(post, "isFixedPitch", json_boolean_new(table->isFixedPitch));
84 json_object_push(post, "minMemType42", json_integer_new(table->minMemType42));
85 json_object_push(post, "maxMemType42", json_integer_new(table->maxMemType42));
86 json_object_push(post, "minMemType1", json_integer_new(table->minMemType1));
87 json_object_push(post, "maxMemType1", json_integer_new(table->maxMemType1));
88 json_object_push(root, "post", post);
89 }
90 }
91 table_post *otfcc_parsePost(const json_value *root, const otfcc_Options *options) {
92 table_post *post = iTable_post.create();
93 json_value *table = NULL;
94 if ((table = json_obj_get_type(root, "post", json_object))) {
95 loggedStep("post") {
96 if (options->short_post) {
97 post->version = 0x30000;
98 } else {
99 post->version = otfcc_to_fixed(json_obj_getnum(table, "version"));
100 }
101 post->italicAngle = otfcc_to_fixed(json_obj_getnum(table, "italicAngle"));
102 post->underlinePosition = json_obj_getnum(table, "underlinePosition");
103 post->underlineThickness = json_obj_getnum(table, "underlineThickness");
104 post->isFixedPitch = json_obj_getbool(table, "isFixedPitch");
105 post->minMemType42 = json_obj_getnum(table, "minMemType42");
106 post->maxMemType42 = json_obj_getnum(table, "maxMemType42");
107 post->minMemType1 = json_obj_getnum(table, "minMemType1");
108 post->maxMemType1 = json_obj_getnum(table, "maxMemType1");
109 }
110 }
111 return post;
112 }
113 caryll_Buffer *otfcc_buildPost(const table_post *post, otfcc_GlyphOrder *glyphorder,
114 const otfcc_Options *options) {
115 if (!post) return NULL;
116 caryll_Buffer *buf = bufnew();
117 bufwrite32b(buf, post->version);
118 bufwrite32b(buf, post->italicAngle);
119 bufwrite16b(buf, post->underlinePosition);
120 bufwrite16b(buf, post->underlineThickness);
121 bufwrite32b(buf, post->isFixedPitch);
122 bufwrite32b(buf, post->minMemType42);
123 bufwrite32b(buf, post->maxMemType42);
124 bufwrite32b(buf, post->minMemType1);
125 bufwrite32b(buf, post->maxMemType1);
126 if (post->version == 0x20000) {
127 bufwrite16b(buf, HASH_CNT(hhName, glyphorder->byName));
128 // Since the glyphorder is already sorted using the "real" glyph order
129 // we can simply write down the glyph names.
130 otfcc_GlyphOrderEntry *s, *tmp;
131 HASH_ITER(hhName, glyphorder->byName, s, tmp) {
132 bufwrite16b(buf, 258 + s->gid);
133 }
134 HASH_ITER(hhName, glyphorder->byName, s, tmp) {
135 bufwrite8(buf, sdslen(s->name));
136 bufwrite_sds(buf, s->name);
137 }
138 }
139 return buf;
140 }