Debian Codegraph @

podofo/3rdparty/adobe/afdko/source/nameread/nameread.c

raw
1 /* Copyright 2016 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved.
2 This software is licensed as OpenSource, under the Apache License, Version 2.0.
3 This license is available at: http://opensource.org/licenses/Apache-2.0. */
4
5 #include "ctlshare.h"
6 #include "nameread.h"
7 #include "varread.h"
8 #include "dynarr.h"
9 #include "sha1.h"
10 #include "supportexcept.h"
11
12 #include <stdlib.h>
13 #include <ctype.h>
14 #include <string.h>
15 #include <math.h>
16
17 /* --------------------------- Constants --------------------------- */
18
19 #define MAX_FAMILY_NAME_LENGTH 64
20 #define MAX_COORD_STRING_LENGTH 16
21
22 /* --------------------------- type Definitions --------------------------- */
23
24 typedef struct /* bsearch id match record */
25 {
26 unsigned short platformId;
27 unsigned short platspecId;
28 unsigned short languageId;
29 unsigned short nameId;
30 } MatchIds;
31
32 struct nam_name_ /* name table */
33 {
34 unsigned short format; /* =0 */
35 unsigned short count;
36 unsigned short stringOffset;
37 dnaDCL(nam_NameRecord, records);
38 };
39
40 /* --------------------------- Functions --------------------------- */
41
42 /* Load the name table. */
43 nam_name nam_loadname(sfrCtx sfr, ctlSharedStmCallbacks *sscb) {
44 nam_name nameTbl = NULL;
45 int success = 0;
46 int i;
47
48 sfrTable *table = sfrGetTableByTag(sfr, CTL_TAG('n', 'a', 'm', 'e'));
49 if (table == NULL) {
50 sscb->message(sscb, "name table missing");
51 return NULL;
52 }
53 DURING
54 sscb->seek(sscb, table->offset);
55
56 nameTbl = (nam_name)sscb->memNew(sscb, sizeof(*nameTbl));
57 if (!nameTbl)
58 goto cleanup;
59
60 /* Read and validate table format */
61 nameTbl->format = sscb->read2(sscb);
62 if (nameTbl->format != 0) {
63 sscb->message(sscb, "invalid name table format");
64 goto cleanup;
65 }
66
67 /* Read rest of header */
68 nameTbl->count = sscb->read2(sscb);
69 nameTbl->stringOffset = sscb->read2(sscb);
70
71 /* Read name records */
72 dnaINIT(sscb->dna, nameTbl->records, nameTbl->count, nameTbl->count);
73 dnaSET_CNT(nameTbl->records, nameTbl->count);
74 for (i = 0; i < nameTbl->records.cnt; i++) {
75 nam_NameRecord *rec = &nameTbl->records.array[i];
76 rec->platformId = sscb->read2(sscb);
77 rec->platspecId = sscb->read2(sscb);
78 rec->languageId = sscb->read2(sscb);
79 rec->nameId = sscb->read2(sscb);
80 rec->length = sscb->read2(sscb);
81 rec->offset = table->offset + nameTbl->stringOffset + sscb->read2(sscb);
82 }
83 success = 1;
84
85 cleanup:;
86 HANDLER
87 END_HANDLER
88
89 if (!success) {
90 nam_freename(sscb, nameTbl);
91 nameTbl = NULL;
92 }
93
94 return nameTbl;
95 }
96
97 void nam_freename(ctlSharedStmCallbacks *sscb, nam_name tbl) {
98 if (tbl) {
99 dnaFREE(tbl->records);
100 sscb->memFree(sscb, tbl);
101 }
102 }
103
104 /* Match ids. */
105 static int CTL_CDECL matchIds(const void *key, const void *value) {
106 MatchIds *a = (MatchIds *)key;
107 nam_NameRecord *b = (nam_NameRecord *)value;
108 if (a->platformId < b->platformId)
109 return -1;
110 else if (a->platformId > b->platformId)
111 return 1;
112 else if (a->platspecId < b->platspecId)
113 return -1;
114 else if (a->platspecId > b->platspecId)
115 return 1;
116 else if (a->languageId < b->languageId)
117 return -1;
118 else if (a->languageId > b->languageId)
119 return 1;
120 else if (a->nameId < b->nameId)
121 return -1;
122 else if (a->nameId > b->nameId)
123 return 1;
124 else
125 return 0;
126 }
127
128 /* Return name record match specified ids or NULL if no match. */
129 nam_NameRecord *nam_nameFind(nam_name tbl,
130 unsigned short platformId,
131 unsigned short platspecId,
132 unsigned short languageId,
133 unsigned short nameId) {
134 MatchIds match;
135
136 if (tbl->records.cnt == 0)
137 return NULL; /* name table missing */
138
139 /* Initialize match record */
140 match.platformId = platformId;
141 match.platspecId = platspecId;
142 match.languageId = languageId;
143 match.nameId = nameId;
144
145 return (nam_NameRecord *)
146 bsearch(&match, tbl->records.array, tbl->records.cnt,
147 sizeof(nam_NameRecord), matchIds);
148 }
149
150 static int checkNameChar(unsigned char ch, int isPS) {
151 if (isPS) {
152 switch (ch) {
153 case '[':
154 case ']':
155 case '(':
156 case ')':
157 case '{':
158 case '}':
159 case '<':
160 case '>':
161 case '/':
162 case '%':
163 return 0;
164 default:
165 return (ch >= 33 && ch <= 126);
166 }
167 } else
168 return (ch >= 32) && (ch <= 126);
169 }
170
171 long nam_getASCIIName(nam_name nameTbl, ctlSharedStmCallbacks *sscb, char *buffer, unsigned long bufferLen, unsigned short nameId, int isPS) {
172 unsigned long i;
173 long nameLen;
174 unsigned char ch1, ch2;
175 nam_NameRecord *rec;
176
177 if (!nameTbl)
178 return NAME_READ_NAME_NOT_FOUND;
179 rec = nam_nameFind(nameTbl, NAME_WIN_PLATFORM, NAME_WIN_UGL, NAME_WIN_ENGLISH, nameId);
180 if (rec && rec->length > 0) {
181 nameLen = 0;
182 sscb->seek(sscb, rec->offset);
183 for (i = 0; i + 1 < (unsigned long)rec->length; i += 2) {
184 ch1 = sscb->read1(sscb);
185 ch2 = sscb->read1(sscb);
186 if (ch1 == 0 && checkNameChar(ch2, isPS)) {
187 if ((unsigned long)nameLen + 1 >= bufferLen) {
188 sscb->message(sscb, "a name in the name table is longer than the given buffer");
189 return NAME_READ_NAME_TOO_LONG;
190 }
191 buffer[nameLen++] = (char)ch2;
192 }
193 }
194 buffer[nameLen] = 0;
195 return nameLen;
196 }
197 rec = nam_nameFind(nameTbl, NAME_MAC_PLATFORM, NAME_MAC_ROMAN, NAME_MAC_ENGLISH, nameId);
198 if (rec) {
199 nameLen = 0;
200 sscb->seek(sscb, rec->offset);
201 for (i = 0; i < (unsigned long)rec->length; i++) {
202 ch1 = sscb->read1(sscb);
203 if (checkNameChar(ch1, isPS)) {
204 if ((unsigned long)nameLen + 1 >= bufferLen) {
205 sscb->message(sscb, "a name in the name table is longer than the given buffer");
206 return NAME_READ_NAME_TOO_LONG;
207 }
208 buffer[nameLen++] = (char)ch1;
209 }
210 }
211 buffer[nameLen] = 0;
212 return nameLen;
213 }
214 return NAME_READ_NAME_NOT_FOUND;
215 }
216
217 static long getPSName(nam_name nameTbl,
218 ctlSharedStmCallbacks *sscb,
219 char *nameBuffer, unsigned long nameBufferLen) {
220 return nam_getASCIIName(nameTbl, sscb, nameBuffer, nameBufferLen, NAME_ID_POSTSCRIPT, 1);
221 }
222
223 static unsigned long removeNonAlphanumChar(char *buffer, unsigned long len) {
224 unsigned long i, j;
225
226 for (i = j = 0; i < len; i++) {
227 if (isalnum((unsigned char)buffer[i])) {
228 buffer[j++] = buffer[i];
229 }
230 }
231 buffer[j] = 0;
232 return j;
233 }
234
235 /* get the family name prefix for a variable font instance name */
236 long nam_getFamilyNamePrefix(nam_name nameTbl,
237 ctlSharedStmCallbacks *sscb,
238 char *buffer, unsigned long bufferLen) {
239 long nameLen;
240
241 nameLen = nam_getASCIIName(nameTbl, sscb, buffer, bufferLen, NAME_ID_POSTSCRIPT_PREFIX, 1);
242 if (nameLen == NAME_READ_NAME_NOT_FOUND)
243 nameLen = nam_getASCIIName(nameTbl, sscb, buffer, bufferLen, NAME_ID_TYPO_FAMILY, 1);
244 if (nameLen == NAME_READ_NAME_NOT_FOUND)
245 nameLen = nam_getASCIIName(nameTbl, sscb, buffer, bufferLen, NAME_ID_FAMILY, 1);
246 if (nameLen > 0)
247 nameLen = removeNonAlphanumChar(buffer, nameLen);
248
249 if (nameLen > MAX_FAMILY_NAME_LENGTH) {
250 sscb->message(sscb, "too long family name prefix");
251 nameLen = NAME_READ_NAME_TOO_LONG;
252 }
253 return nameLen;
254 }
255
256 /* allocate a temporary buffer long enough to hold a generated instance name */
257 static char *allocNameBuffer(ctlSharedStmCallbacks *sscb, long axisCount, unsigned long *bufferLen) {
258 char *buffer;
259
260 *bufferLen = MAX_FAMILY_NAME_LENGTH + 10 + (MAX_COORD_STRING_LENGTH * axisCount);
261 buffer = (char *)sscb->memNew(sscb, *bufferLen);
262 if (buffer == 0) {
263 sscb->message(sscb, "failed to allocate memory");
264 return NULL;
265 }
266 return buffer;
267 }
268
269 long nam_getNamedInstancePSName(nam_name nameTbl,
270 var_axes axesTbl,
271 ctlSharedStmCallbacks *sscb,
272 float *coords,
273 unsigned short axisCount,
274 int instanceIndex,
275 char *instanceName, unsigned long instanceNameLen) {
276 unsigned long familyNameLen = 0;
277 long nameLen = NAME_READ_NAME_NOT_FOUND;
278 unsigned short subfamilyID = 0, postscriptID = 0;
279 char *buffer = 0;
280 unsigned long bufferLen;
281
282 /* if the font is not a variable font or no coordinates are specified, return the default PS name */
283 if (!axesTbl || !coords || !axisCount) {
284 return getPSName(nameTbl, sscb, instanceName, instanceNameLen);
285 }
286
287 /* find a matching named instance in the fvar table */
288 if (instanceIndex < 0)
289 instanceIndex = var_findInstance(axesTbl, coords, axisCount, &subfamilyID, &postscriptID);
290 if ((instanceIndex >= 0) && (postscriptID == 6 || ((postscriptID > 255) && (postscriptID < 32768)))) {
291 nameLen = nam_getASCIIName(nameTbl, sscb, instanceName, instanceNameLen, postscriptID, 1);
292 if (nameLen > 0)
293 return nameLen;
294 }
295
296 /* allocate a temporary buffer long enough to hold a generated instance name */
297 buffer = allocNameBuffer(sscb, axisCount, &bufferLen);
298 if (buffer == NULL)
299 return NAME_READ_FAILED;
300
301 /* get the family name prefix */
302 nameLen = nam_getFamilyNamePrefix(nameTbl, sscb, buffer, bufferLen);
303 if (nameLen <= 0)
304 goto cleanup;
305
306 familyNameLen = (unsigned long)nameLen;
307
308 /* append the style name from the instance if there is a matching one */
309 if (instanceIndex >= 0) {
310 char *styleName = &buffer[nameLen];
311 unsigned long styleBufferLen = bufferLen - nameLen;
312 long styleLen;
313
314 styleLen = nam_getASCIIName(nameTbl, sscb, styleName, styleBufferLen, subfamilyID, 0);
315 if (styleLen < 0)
316 nameLen = styleLen;
317 else {
318 styleLen = removeNonAlphanumChar(styleName, styleLen);
319 nameLen = nameLen + styleLen;
320 if ((unsigned long)nameLen + 1 > instanceNameLen)
321 nameLen = NAME_READ_NAME_TOO_LONG;
322 else
323 STRCPY_S(instanceName, instanceNameLen, buffer);
324 }
325 } else
326 nameLen = NAME_READ_NAME_NOT_FOUND;
327
328 cleanup:
329 if (buffer)
330 sscb->memFree(sscb, buffer);
331
332 return nameLen;
333 }
334
335 /* convert a fractional number to a string in the buffer.
336 * function returns the length of the string.
337 * the buffer is assumed to be long enough.
338 */
339 static unsigned long stringizeNum(char *buffer, unsigned long bufferLen, float value) {
340 unsigned long i = 0;
341 float eps = 0.5f / (1 << 16);
342 float hi, lo;
343 int intPart;
344 float fractPart;
345 int neg = 0;
346 int power10;
347 int digit;
348
349 if (value < 0.0f) {
350 neg = 1;
351 value = -value;
352 }
353
354 hi = value + eps;
355 lo = value - eps;
356
357 intPart = (int)floor(hi);
358 if (intPart != (int)floor(lo)) {
359 fractPart = 0.0f;
360 } else {
361 fractPart = hi - intPart;
362 }
363
364 if ((intPart == 0) && (fractPart == 0.0f)) {
365 buffer[i++] = '0';
366 return i;
367 }
368
369 if (neg) {
370 if (i + 1 >= bufferLen) return 0;
371 buffer[i++] = '-';
372 }
373
374 /* integer part */
375 for (digit = 0, power10 = 1; intPart >= power10;) {
376 digit++;
377 power10 *= 10;
378 }
379 while (digit-- > 0) {
380 power10 /= 10;
381 buffer[i++] = (char)('0' + (intPart / power10));
382 intPart %= power10;
383 }
384
385 /* fractional part */
386 if (fractPart >= eps) {
387 buffer[i++] = '.';
388
389 do {
390 fractPart *= 10;
391 eps *= 10;
392 intPart = (int)fractPart;
393 buffer[i++] = (char)('0' + intPart);
394 fractPart -= intPart;
395 } while (fractPart >= eps);
396 }
397
398 return i;
399 }
400
401 long nam_generateArbitraryInstancePSName(nam_name nameTbl,
402 var_axes axesTbl,
403 ctlSharedStmCallbacks *sscb,
404 float *coords,
405 unsigned short axisCount,
406 char *instanceName, unsigned long instanceNameLen) {
407 unsigned long familyNameLen = 0;
408 long nameLen;
409 unsigned short axis;
410 char *buffer = 0;
411 unsigned long bufferLen;
412
413 /* if the font is not a variable font or no coordinates are specified, return the default PS name */
414 if (!axesTbl || !coords || !axisCount) {
415 return getPSName(nameTbl, sscb, instanceName, instanceNameLen);
416 }
417
418 /* allocate a temporary buffer long enough to hold a generated instance name */
419 buffer = allocNameBuffer(sscb, axisCount, &bufferLen);
420 if (buffer == NULL)
421 return NAME_READ_FAILED;
422
423 /* get the family name prefix */
424 nameLen = nam_getFamilyNamePrefix(nameTbl, sscb, buffer, bufferLen);
425 if (nameLen <= 0)
426 goto cleanup;
427
428 familyNameLen = (unsigned long)nameLen;
429
430 /* create an arbitrary instance name from the coordinates */
431 for (axis = 0; axis < axisCount; axis++) {
432 unsigned long coordStrLen;
433 int i;
434 unsigned long tag;
435
436 /* value range check */
437 if (fabs(coords[axis]) > 32768.0f) {
438 sscb->message(sscb, "coordinates value out of range: %f", coords[axis]);
439 nameLen = 0;
440 goto cleanup;
441 }
442
443 /* convert a coordinates value to string */
444 buffer[nameLen++] = '_';
445 coordStrLen = stringizeNum(&buffer[nameLen], bufferLen - nameLen, coords[axis]);
446 nameLen += coordStrLen;
447
448 /* append a axis name */
449 if (var_getAxis(axesTbl, axis, &tag, NULL, NULL, NULL, NULL)) {
450 sscb->message(sscb, "failed to get axis information");
451 nameLen = 0;
452 goto cleanup;
453 }
454 for (i = 3; i >= 0; i--) {
455 char ch = (char)((tag >> (i * 8)) & 0xff);
456 if (ch < 32 || ch > 126) {
457 sscb->message(sscb, "invalid character found in an axis tag");
458 continue;
459 }
460 if (ch == ' ') continue;
461 buffer[nameLen++] = ch;
462 }
463 }
464 buffer[nameLen++] = 0;
465 if (nameLen < (long)instanceNameLen) {
466 STRNCPY_S(instanceName, instanceNameLen, buffer, nameLen);
467 instanceName[nameLen] = 0;
468 } else
469 nameLen = NAME_READ_NAME_TOO_LONG;
470
471 cleanup:
472 if (buffer)
473 sscb->memFree(sscb, buffer);
474
475 return nameLen;
476 }
477
478 static void *nam_sha1_malloc(size_t size, void *hook) {
479 ctlSharedStmCallbacks *sscb = (ctlSharedStmCallbacks *)hook;
480 return sscb->memNew(sscb, size);
481 }
482
483 static void nam_sha1_free(sha1_pctx ctx, void *hook) {
484 ctlSharedStmCallbacks *sscb = (ctlSharedStmCallbacks *)hook;
485 sscb->memFree(sscb, ctx);
486 }
487
488 long nam_generateLastResortInstancePSName(nam_name nameTbl,
489 var_axes axesTbl,
490 ctlSharedStmCallbacks *sscb,
491 float *coords,
492 unsigned short axisCount,
493 char *instanceName, unsigned long instanceNameLen) {
494 unsigned long familyNameLen = 0;
495 long nameLen;
496 char *buffer = 0;
497 unsigned long bufferLen;
498 unsigned long hashLen;
499 unsigned long i;
500
501 /* if the font is not a variable font or no coordinates are specified, return the default PS name */
502 if (!axesTbl || !coords || !axisCount) {
503 return getPSName(nameTbl, sscb, instanceName, instanceNameLen);
504 }
505
506 /* allocate a temporary buffer long enough to hold a generated instance name */
507 buffer = allocNameBuffer(sscb, axisCount, &bufferLen);
508 if (buffer == NULL)
509 return NAME_READ_FAILED;
510
511 /* get the family name prefix */
512 nameLen = nam_getFamilyNamePrefix(nameTbl, sscb, buffer, bufferLen);
513 if (nameLen <= 0)
514 goto cleanup;
515
516 familyNameLen = (unsigned long)nameLen;
517
518 /* generate a SHA1 value of the generated name as the identifier in a last resort name.
519 */
520 hashLen = sizeof(sha1_hash);
521 if (familyNameLen + hashLen * 2 + 5 >= instanceNameLen) {
522 hashLen /= 2; /* give it another chance by halving the hash length */
523 if (familyNameLen + hashLen * 2 + 5 >= instanceNameLen) {
524 sscb->message(sscb, "name buffer not long enough to generate a last resort variable font instance name");
525 nameLen = -2;
526 goto cleanup;
527 }
528 }
529 nameLen = familyNameLen;
530 STRNCPY_S(instanceName, instanceNameLen, buffer, nameLen);
531 instanceName[nameLen++] = '-';
532
533 {
534 sha1_pctx sha1_ctx = sha1_init(nam_sha1_malloc, sscb);
535 sha1_hash hash;
536 int error;
537
538 if (!sha1_ctx) {
539 nameLen = 0;
540 goto cleanup;
541 }
542
543 error = sha1_update(sha1_ctx, (unsigned char *)buffer, nameLen - 1);
544 error |= sha1_finalize(sha1_ctx, nam_sha1_free, hash, sscb);
545
546 if (error) {
547 sscb->message(sscb, "failed to generate hash during a last resort variable font instance name generation");
548 nameLen = 0;
549 goto cleanup;
550 }
551
552 /* append the hash value has a hex string */
553 for (i = 0; i < hashLen; i++) {
554 static const char hexChar[] = "0123456789ABCDEF";
555 unsigned char byte = hash[i];
556 instanceName[nameLen++] = hexChar[byte >> 4];
557 instanceName[nameLen++] = hexChar[byte & 0xF];
558 }
559
560 instanceName[nameLen++] = '.';
561 instanceName[nameLen++] = '.';
562 instanceName[nameLen++] = '.';
563 instanceName[nameLen++] = 0;
564
565 sscb->message(sscb, "last resort variable font instance name %s generated for %s",
566 instanceName, buffer);
567 }
568
569 cleanup:
570 if (buffer)
571 sscb->memFree(sscb, buffer);
572
573 return nameLen;
574 }