Debian Codegraph @

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

raw
1 #include "name.h"
2
3 #include "support/util.h"
4 #include "support/unicodeconv/unicodeconv.h"
5
6 #ifndef MAIN_VER
7 #define MAIN_VER 0
8 #endif
9 #ifndef SECONDARY_VER
10 #define SECONDARY_VER 0
11 #endif
12 #ifndef PATCH_VER
13 #define PATCH_VER 0
14 #endif
15
16 #define COPYRIGHT_LEN 32
17
18 static void nameRecordDtor(otfcc_NameRecord *entry) {
19 DELETE(sdsfree, entry->nameString);
20 }
21 caryll_standardType(otfcc_NameRecord, otfcc_iNameRecord, nameRecordDtor);
22 caryll_standardVectorImpl(table_name, otfcc_NameRecord, otfcc_iNameRecord, table_iName);
23
24 static bool shouldDecodeAsUTF16(const otfcc_NameRecord *record) {
25 return (record->platformID == 0) // Unicode, all
26 || (record->platformID == 2 && record->encodingID == 1) // ISO, 1
27 || (record->platformID == 3 && // Microsoft, 0, 1, 10
28 (record->encodingID == 0 || record->encodingID == 1 || record->encodingID == 10));
29 }
30 static bool shouldDecodeAsBytes(const otfcc_NameRecord *record) {
31 return record->platformID == 1 && record->encodingID == 0 &&
32 record->languageID == 0; // Mac Roman English - I hope
33 }
34
35 table_name *otfcc_readName(const otfcc_Packet packet, const otfcc_Options *options) {
36 FOR_TABLE(OTFCC_CHR('n','a','m','e'), table) {
37 table_name *name = NULL;
38 font_file_pointer data = table.data;
39
40 uint32_t length = table.length;
41 if (length < 6) goto TABLE_NAME_CORRUPTED;
42 uint32_t count = read_16u(data + 2);
43 uint32_t stringOffset = read_16u(data + 4);
44 if (length < 6 + 12 * count) goto TABLE_NAME_CORRUPTED;
45
46 name = table_iName.create();
47
48 for (uint16_t j = 0; j < count; j++) {
49 otfcc_NameRecord record;
50 record.platformID = read_16u(data + 6 + j * 12);
51 record.encodingID = read_16u(data + 6 + j * 12 + 2);
52 record.languageID = read_16u(data + 6 + j * 12 + 4);
53 record.nameID = read_16u(data + 6 + j * 12 + 6);
54 record.nameString = NULL;
55 uint16_t length = read_16u(data + 6 + j * 12 + 8);
56 uint16_t offset = read_16u(data + 6 + j * 12 + 10);
57
58 if (shouldDecodeAsBytes(&record)) {
59 // Mac Roman. Note that this is not very correct, but works for most fonts
60 sds nameString = sdsnewlen(data + stringOffset + offset, length);
61 record.nameString = nameString;
62 } else if (shouldDecodeAsUTF16(&record)) {
63 sds nameString = utf16be_to_utf8(data + stringOffset + offset, length);
64 record.nameString = nameString;
65 } else {
66 size_t len = 0;
67 uint8_t *buf = base64_encode(data + stringOffset + offset, length, &len);
68 record.nameString = sdsnewlen(buf, len);
69 FREE(buf);
70 }
71 table_iName.push(name, record);
72 }
73 return name;
74 TABLE_NAME_CORRUPTED:
75 logWarning("table 'name' corrupted.\n");
76 if (name) { DELETE(table_iName.free, name); }
77 }
78 return NULL;
79 }
80
81 void otfcc_dumpName(const table_name *name, json_value *root, const otfcc_Options *options) {
82 if (!name) return;
83 loggedStep("name") {
84 json_value *_name = json_array_new(name->length);
85 for (uint16_t j = 0; j < name->length; j++) {
86 otfcc_NameRecord *r = &(name->items[j]);
87 json_value *record = json_object_new(5);
88 json_object_push(record, "platformID", json_integer_new(r->platformID));
89 json_object_push(record, "encodingID", json_integer_new(r->encodingID));
90 json_object_push(record, "languageID", json_integer_new(r->languageID));
91 json_object_push(record, "nameID", json_integer_new(r->nameID));
92 json_object_push(
93 record, "nameString",
94 json_string_new_length((uint32_t)sdslen(r->nameString), r->nameString));
95 json_array_push(_name, record);
96 }
97 json_object_push(root, "name", _name);
98 }
99 }
100 static int name_record_sort(const otfcc_NameRecord *a, const otfcc_NameRecord *b) {
101 if (a->platformID != b->platformID) return a->platformID - b->platformID;
102 if (a->encodingID != b->encodingID) return a->encodingID - b->encodingID;
103 if (a->languageID != b->languageID) return a->languageID - b->languageID;
104 return a->nameID - b->nameID;
105 }
106 table_name *otfcc_parseName(const json_value *root, const otfcc_Options *options) {
107 table_name *name = table_iName.create();
108 json_value *table = NULL;
109 if ((table = json_obj_get_type(root, "name", json_array))) {
110 loggedStep("name") {
111
112 for (uint32_t j = 0; j < table->u.array.length; j++) {
113 if (!(table->u.array.values[j] && table->u.array.values[j]->type == json_object))
114 continue;
115 json_value *_record = table->u.array.values[j];
116 if (!json_obj_get_type(_record, "platformID", json_integer)) {
117 logWarning("Missing or invalid platformID for name entry %d\n", j);
118 continue;
119 }
120 if (!json_obj_get_type(_record, "encodingID", json_integer)) {
121 logWarning("Missing or invalid encodingID for name entry %d\n", j);
122 continue;
123 }
124 if (!json_obj_get_type(_record, "languageID", json_integer)) {
125 logWarning("Missing or invalid languageID for name entry %d\n", j);
126 continue;
127 }
128 if (!json_obj_get_type(_record, "nameID", json_integer)) {
129 logWarning("Missing or invalid nameID for name entry %d\n", j);
130 continue;
131 }
132 if (!json_obj_get_type(_record, "nameString", json_string)) {
133 logWarning("Missing or invalid name string for name entry %d\n", j);
134 continue;
135 }
136 otfcc_NameRecord record;
137 record.platformID = json_obj_getint(_record, "platformID");
138 record.encodingID = json_obj_getint(_record, "encodingID");
139 record.languageID = json_obj_getint(_record, "languageID");
140 record.nameID = json_obj_getint(_record, "nameID");
141
142 json_value *str = json_obj_get_type(_record, "nameString", json_string);
143 record.nameString = sdsnewlen(str->u.string.ptr, str->u.string.length);
144 table_iName.push(name, record);
145 }
146
147 table_iName.sort(name, name_record_sort);
148 }
149 }
150 return name;
151 }
152 caryll_Buffer *otfcc_buildName(const table_name *name, const otfcc_Options *options) {
153 if (!name) return NULL;
154 caryll_Buffer *buf = bufnew();
155 bufwrite16b(buf, 0);
156 bufwrite16b(buf, name->length);
157 bufwrite16b(buf, 0); // fill later
158 caryll_Buffer *strings = bufnew();
159 for (uint16_t j = 0; j < name->length; j++) {
160 otfcc_NameRecord *record = &(name->items[j]);
161 bufwrite16b(buf, record->platformID);
162 bufwrite16b(buf, record->encodingID);
163 bufwrite16b(buf, record->languageID);
164 bufwrite16b(buf, record->nameID);
165 size_t cbefore = strings->cursor;
166 if (shouldDecodeAsUTF16(record)) {
167 size_t words;
168 uint8_t *u16 = utf8toutf16be(record->nameString, &words);
169 bufwrite_bytes(strings, words, u16);
170 FREE(u16);
171 } else if (shouldDecodeAsBytes(record)) {
172 bufwrite_bytes(strings, sdslen(record->nameString), (uint8_t *)record->nameString);
173 } else {
174 size_t length;
175 uint8_t *decoded =
176 base64_decode((uint8_t *)record->nameString, sdslen(record->nameString), &length);
177 bufwrite_bytes(strings, length, decoded);
178 FREE(decoded);
179 }
180 size_t cafter = strings->cursor;
181 bufwrite16b(buf, cafter - cbefore);
182 bufwrite16b(buf, cbefore);
183 }
184
185 // write copyright info
186 sds copyright =
187 sdscatprintf(sdsempty(), "-- By OTFCC %d.%d.%d --", MAIN_VER, SECONDARY_VER, PATCH_VER);
188 sdsgrowzero(copyright, COPYRIGHT_LEN);
189 bufwrite_bytes(strings, COPYRIGHT_LEN, (uint8_t *)copyright);
190 sdsfree(copyright);
191
192 size_t stringsOffset = buf->cursor;
193 bufwrite_buf(buf, strings);
194 bufseek(buf, 4);
195 bufwrite16b(buf, stringsOffset);
196 buffree(strings);
197 return buf;
198 }