Debian Codegraph @

src/fcname.c

raw
1 /*
2 * fontconfig/src/fcname.c
3 *
4 * Copyright © 2000 Keith Packard
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
23 */
24
25 #include "fcint.h"
26
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 static const FcObjectType FcObjects[] = {
33 #define FC_OBJECT(NAME, Type, Cmp) { FC_##NAME, Type },
34 #include "fcobjs.h"
35 #undef FC_OBJECT
36 };
37
38 #define NUM_OBJECT_TYPES ((int)(sizeof FcObjects / sizeof FcObjects[0]))
39
40 static const FcObjectType *
41 FcObjectFindById (FcObject object)
42 {
43 if (1 <= object && object <= NUM_OBJECT_TYPES)
44 return &FcObjects[object - 1];
45 return FcObjectLookupOtherTypeById (object);
46 }
47
48 FcBool
49 FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
50 {
51 /* Deprecated. */
52 return FcFalse;
53 }
54
55 FcBool
56 FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
57 {
58 /* Deprecated. */
59 return FcFalse;
60 }
61
62 const FcObjectType *
63 FcNameGetObjectType (const char *object)
64 {
65 int id = FcObjectLookupBuiltinIdByName (object);
66
67 if (!id)
68 return FcObjectLookupOtherTypeByName (object);
69
70 return &FcObjects[id - 1];
71 }
72
73 FcBool
74 FcObjectValidType (FcObject object, FcType type)
75 {
76 const FcObjectType *t = FcObjectFindById (object);
77
78 if (t) {
79 switch ((int)t->type) {
80 case FcTypeUnknown:
81 return FcTrue;
82 case FcTypeDouble:
83 case FcTypeInteger:
84 if (type == FcTypeDouble || type == FcTypeInteger)
85 return FcTrue;
86 break;
87 case FcTypeLangSet:
88 if (type == FcTypeLangSet || type == FcTypeString)
89 return FcTrue;
90 break;
91 case FcTypeRange:
92 if (type == FcTypeRange ||
93 type == FcTypeDouble ||
94 type == FcTypeInteger)
95 return FcTrue;
96 break;
97 default:
98 if (type == t->type)
99 return FcTrue;
100 break;
101 }
102 return FcFalse;
103 }
104 return FcTrue;
105 }
106
107 FcObject
108 FcObjectFromName (const char *name)
109 {
110 return FcObjectLookupIdByName (name);
111 }
112
113 FcObjectSet *
114 FcObjectGetSet (void)
115 {
116 int i;
117 FcObjectSet *os = NULL;
118
119 os = FcObjectSetCreate();
120 for (i = 0; i < NUM_OBJECT_TYPES; i++)
121 FcObjectSetAdd (os, FcObjects[i].object);
122
123 return os;
124 }
125
126 const char *
127 FcObjectName (FcObject object)
128 {
129 const FcObjectType *o = FcObjectFindById (object);
130
131 if (o)
132 return o->object;
133
134 return FcObjectLookupOtherNameById (object);
135 }
136
137 typedef FcChar8 *FC8;
138
139 static const FcConstant _FcBaseConstants[] = {
140 { (FC8) "thin", "weight", FC_WEIGHT_THIN },
141 { (FC8) "extralight", "weight", FC_WEIGHT_EXTRALIGHT },
142 { (FC8) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT },
143 { (FC8) "demilight", "weight", FC_WEIGHT_DEMILIGHT },
144 { (FC8) "semilight", "weight", FC_WEIGHT_DEMILIGHT },
145 { (FC8) "light", "weight", FC_WEIGHT_LIGHT },
146 { (FC8) "book", "weight", FC_WEIGHT_BOOK },
147 { (FC8) "regular", "weight", FC_WEIGHT_REGULAR },
148 { (FC8) "normal", "weight", FC_WEIGHT_NORMAL },
149 { (FC8) "medium", "weight", FC_WEIGHT_MEDIUM },
150 { (FC8) "demibold", "weight", FC_WEIGHT_DEMIBOLD },
151 { (FC8) "semibold", "weight", FC_WEIGHT_DEMIBOLD },
152 { (FC8) "bold", "weight", FC_WEIGHT_BOLD },
153 { (FC8) "extrabold", "weight", FC_WEIGHT_EXTRABOLD },
154 { (FC8) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD },
155 { (FC8) "black", "weight", FC_WEIGHT_BLACK },
156 { (FC8) "heavy", "weight", FC_WEIGHT_HEAVY },
157 { (FC8) "extrablack", "weight", FC_WEIGHT_EXTRABLACK },
158 { (FC8) "ultrablack", "weight", FC_WEIGHT_ULTRABLACK },
159
160 { (FC8) "roman", "slant", FC_SLANT_ROMAN },
161 { (FC8) "italic", "slant", FC_SLANT_ITALIC },
162 { (FC8) "oblique", "slant", FC_SLANT_OBLIQUE },
163
164 { (FC8) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED },
165 { (FC8) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED },
166 { (FC8) "condensed", "width", FC_WIDTH_CONDENSED },
167 { (FC8) "semicondensed", "width", FC_WIDTH_SEMICONDENSED },
168 { (FC8) "normal", "width", FC_WIDTH_NORMAL },
169 { (FC8) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED },
170 { (FC8) "expanded", "width", FC_WIDTH_EXPANDED },
171 { (FC8) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED },
172 { (FC8) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED },
173
174 { (FC8) "proportional", "spacing", FC_PROPORTIONAL },
175 { (FC8) "dual", "spacing", FC_DUAL },
176 { (FC8) "mono", "spacing", FC_MONO },
177 { (FC8) "charcell", "spacing", FC_CHARCELL },
178
179 { (FC8) "unknown", "rgba", FC_RGBA_UNKNOWN },
180 { (FC8) "rgb", "rgba", FC_RGBA_RGB },
181 { (FC8) "bgr", "rgba", FC_RGBA_BGR },
182 { (FC8) "vrgb", "rgba", FC_RGBA_VRGB },
183 { (FC8) "vbgr", "rgba", FC_RGBA_VBGR },
184 { (FC8) "none", "rgba", FC_RGBA_NONE },
185
186 { (FC8) "hintnone", "hintstyle", FC_HINT_NONE },
187 { (FC8) "hintslight", "hintstyle", FC_HINT_SLIGHT },
188 { (FC8) "hintmedium", "hintstyle", FC_HINT_MEDIUM },
189 { (FC8) "hintfull", "hintstyle", FC_HINT_FULL },
190
191 { (FC8) "antialias", "antialias", FcTrue },
192 { (FC8) "hinting", "hinting", FcTrue },
193 { (FC8) "verticallayout", "verticallayout", FcTrue },
194 { (FC8) "autohint", "autohint", FcTrue },
195 { (FC8) "globaladvance", "globaladvance", FcTrue }, /* deprecated */
196 { (FC8) "outline", "outline", FcTrue },
197 { (FC8) "scalable", "scalable", FcTrue },
198 { (FC8) "minspace", "minspace", FcTrue },
199 { (FC8) "embolden", "embolden", FcTrue },
200 { (FC8) "embeddedbitmap", "embeddedbitmap", FcTrue },
201 { (FC8) "decorative", "decorative", FcTrue },
202 { (FC8) "lcdnone", "lcdfilter", FC_LCD_NONE },
203 { (FC8) "lcddefault", "lcdfilter", FC_LCD_DEFAULT },
204 { (FC8) "lcdlight", "lcdfilter", FC_LCD_LIGHT },
205 { (FC8) "lcdlegacy", "lcdfilter", FC_LCD_LEGACY },
206 };
207
208 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants / sizeof _FcBaseConstants[0])
209
210 FcBool
211 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
212 {
213 /* Deprecated. */
214 return FcFalse;
215 }
216
217 FcBool
218 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
219 {
220 /* Deprecated. */
221 return FcFalse;
222 }
223
224 const FcConstant *
225 FcNameGetConstant (const FcChar8 *string)
226 {
227 unsigned int i;
228
229 for (i = 0; i < NUM_FC_CONSTANTS; i++)
230 if (!FcStrCmpIgnoreCase (string, _FcBaseConstants[i].name))
231 return &_FcBaseConstants[i];
232
233 return 0;
234 }
235
236 const FcConstant *
237 FcNameGetConstantFor (const FcChar8 *string, const char *object)
238 {
239 unsigned int i;
240
241 for (i = 0; i < NUM_FC_CONSTANTS; i++)
242 if (!FcStrCmpIgnoreCase (string, _FcBaseConstants[i].name) &&
243 !FcStrCmpIgnoreCase ((const FcChar8 *)object, (const FcChar8 *)_FcBaseConstants[i].object))
244 return &_FcBaseConstants[i];
245
246 return 0;
247 }
248
249 FcBool
250 FcNameConstant (const FcChar8 *string, int *result)
251 {
252 const FcConstant *c;
253
254 if ((c = FcNameGetConstant (string))) {
255 *result = c->value;
256 return FcTrue;
257 }
258 return FcFalse;
259 }
260
261 FcBool
262 FcNameConstantWithObjectCheck (const FcChar8 *string, const char *object, int *result)
263 {
264 const FcConstant *c;
265
266 if ((c = FcNameGetConstantFor (string, object))) {
267 *result = c->value;
268 return FcTrue;
269 } else if ((c = FcNameGetConstant (string))) {
270 if (strcmp (c->object, object) != 0) {
271 fprintf (stderr, "Fontconfig error: Unexpected constant name `%s' used for object `%s': should be `%s'\n", string, object, c->object);
272 return FcFalse;
273 }
274 /* Unlikely to reach out */
275 *result = c->value;
276 return FcTrue;
277 }
278 return FcFalse;
279 }
280
281 FcBool
282 FcNameBool (const FcChar8 *v, FcBool *result)
283 {
284 char c0, c1;
285
286 c0 = *v;
287 c0 = FcToLower (c0);
288 if (c0 == 't' || c0 == 'y' || c0 == '1') {
289 *result = FcTrue;
290 return FcTrue;
291 }
292 if (c0 == 'f' || c0 == 'n' || c0 == '0') {
293 *result = FcFalse;
294 return FcTrue;
295 }
296 if (c0 == 'd' || c0 == 'x' || c0 == '2') {
297 *result = FcDontCare;
298 return FcTrue;
299 }
300 if (c0 == 'o') {
301 c1 = v[1];
302 c1 = FcToLower (c1);
303 if (c1 == 'n') {
304 *result = FcTrue;
305 return FcTrue;
306 }
307 if (c1 == 'f') {
308 *result = FcFalse;
309 return FcTrue;
310 }
311 if (c1 == 'r') {
312 *result = FcDontCare;
313 return FcTrue;
314 }
315 }
316 return FcFalse;
317 }
318
319 static FcValue
320 FcNameConvert (FcType type, const char *object, FcChar8 *string)
321 {
322 FcValue v;
323 FcMatrix m;
324 double b, e;
325 char *p;
326
327 v.type = type;
328 switch ((int)v.type) {
329 case FcTypeInteger:
330 if (!FcNameConstantWithObjectCheck (string, object, &v.u.i))
331 v.u.i = atoi ((char *)string);
332 break;
333 case FcTypeString:
334 v.u.s = FcStrdup (string);
335 if (!v.u.s)
336 v.type = FcTypeVoid;
337 break;
338 case FcTypeBool:
339 if (!FcNameBool (string, &v.u.b))
340 v.u.b = FcFalse;
341 break;
342 case FcTypeDouble:
343 v.u.d = strtod ((char *)string, 0);
344 break;
345 case FcTypeMatrix:
346 FcMatrixInit (&m);
347 sscanf ((char *)string, "%lg %lg %lg %lg", &m.xx, &m.xy, &m.yx, &m.yy);
348 v.u.m = FcMatrixCopy (&m);
349 break;
350 case FcTypeCharSet:
351 v.u.c = FcNameParseCharSet (string);
352 if (!v.u.c)
353 v.type = FcTypeVoid;
354 break;
355 case FcTypeLangSet:
356 v.u.l = FcNameParseLangSet (string);
357 if (!v.u.l)
358 v.type = FcTypeVoid;
359 break;
360 case FcTypeRange:
361 if (sscanf ((char *)string, "[%lg %lg]", &b, &e) != 2) {
362 char *sc, *ec;
363 size_t len = strlen ((const char *)string);
364 int si, ei;
365
366 sc = malloc (len + 1);
367 ec = malloc (len + 1);
368 if (sc && ec && sscanf ((char *)string, "[%s %[^]]]", sc, ec) == 2) {
369 if (FcNameConstantWithObjectCheck ((const FcChar8 *)sc, object, &si) &&
370 FcNameConstantWithObjectCheck ((const FcChar8 *)ec, object, &ei))
371 v.u.r = FcRangeCreateDouble (si, ei);
372 else
373 goto bail1;
374 } else {
375 bail1:
376 v.type = FcTypeDouble;
377 if (FcNameConstantWithObjectCheck (string, object, &si)) {
378 v.u.d = (double)si;
379 } else {
380 v.u.d = strtod ((char *)string, &p);
381 if (p != NULL && p[0] != 0)
382 v.type = FcTypeVoid;
383 }
384 }
385 if (sc)
386 free (sc);
387 if (ec)
388 free (ec);
389 } else
390 v.u.r = FcRangeCreateDouble (b, e);
391 break;
392 default:
393 /* No valid type to convert */
394 v.type = FcTypeVoid;
395 break;
396 }
397 return v;
398 }
399
400 static const FcChar8 *
401 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
402 {
403 FcChar8 c;
404
405 while ((c = *cur)) {
406 if (!isspace (c))
407 break;
408 ++cur;
409 }
410 while ((c = *cur)) {
411 if (c == '\\') {
412 ++cur;
413 if (!(c = *cur))
414 break;
415 } else if (strchr (delim, c))
416 break;
417 ++cur;
418 *save++ = c;
419 }
420 *save = 0;
421 *last = *cur;
422 if (*cur)
423 cur++;
424 return cur;
425 }
426
427 FcPattern *
428 FcNameParse (const FcChar8 *name)
429 {
430 FcChar8 *save;
431 FcPattern *pat;
432 double d;
433 FcChar8 *e;
434 FcChar8 delim;
435 FcValue v;
436 const FcObjectType *t;
437 const FcConstant *c;
438
439 /* freed below */
440 save = malloc (strlen ((char *)name) + 1);
441 if (!save)
442 goto bail0;
443 pat = FcPatternCreate();
444 if (!pat)
445 goto bail1;
446
447 for (;;) {
448 name = FcNameFindNext (name, "-,:", save, &delim);
449 if (save[0]) {
450 if (!FcPatternObjectAddString (pat, FC_FAMILY_OBJECT, save))
451 goto bail2;
452 }
453 if (delim != ',')
454 break;
455 }
456 if (delim == '-') {
457 for (;;) {
458 name = FcNameFindNext (name, "-,:", save, &delim);
459 d = strtod ((char *)save, (char **)&e);
460 if (e != save) {
461 if (!FcPatternObjectAddDouble (pat, FC_SIZE_OBJECT, d))
462 goto bail2;
463 }
464 if (delim != ',')
465 break;
466 }
467 }
468 while (delim == ':') {
469 name = FcNameFindNext (name, "=_:", save, &delim);
470 if (save[0]) {
471 if (delim == '=' || delim == '_') {
472 t = FcNameGetObjectType ((char *)save);
473 for (;;) {
474 name = FcNameFindNext (name, ":,", save, &delim);
475 if (t) {
476 v = FcNameConvert (t->type, t->object, save);
477 if (!FcPatternAdd (pat, t->object, v, FcTrue)) {
478 FcValueDestroy (v);
479 goto bail2;
480 }
481 FcValueDestroy (v);
482 }
483 if (delim != ',')
484 break;
485 }
486 } else {
487 if ((c = FcNameGetConstant (save))) {
488 t = FcNameGetObjectType ((char *)c->object);
489 if (t == NULL)
490 goto bail2;
491 switch ((int)t->type) {
492 case FcTypeInteger:
493 case FcTypeDouble:
494 if (!FcPatternAddInteger (pat, c->object, c->value))
495 goto bail2;
496 break;
497 case FcTypeBool:
498 if (!FcPatternAddBool (pat, c->object, c->value))
499 goto bail2;
500 break;
501 case FcTypeRange:
502 if (!FcPatternAddInteger (pat, c->object, c->value))
503 goto bail2;
504 break;
505 default:
506 break;
507 }
508 }
509 }
510 }
511 }
512
513 free (save);
514 return pat;
515
516 bail2:
517 FcPatternDestroy (pat);
518 bail1:
519 free (save);
520 bail0:
521 return 0;
522 }
523 static FcBool
524 FcNameUnparseString (FcStrBuf *buf,
525 const FcChar8 *string,
526 const FcChar8 *escape)
527 {
528 FcChar8 c;
529 while ((c = *string++)) {
530 if (escape && strchr ((char *)escape, (char)c)) {
531 if (!FcStrBufChar (buf, escape[0]))
532 return FcFalse;
533 }
534 if (!FcStrBufChar (buf, c))
535 return FcFalse;
536 }
537 return FcTrue;
538 }
539
540 FcBool
541 FcNameUnparseValue (FcStrBuf *buf,
542 FcValue *v0,
543 FcChar8 *escape)
544 {
545 FcChar8 temp[1024];
546 FcValue v = FcValueCanonicalize (v0);
547
548 switch (v.type) {
549 case FcTypeUnknown:
550 case FcTypeVoid:
551 return FcTrue;
552 case FcTypeInteger:
553 sprintf ((char *)temp, "%d", v.u.i);
554 return FcNameUnparseString (buf, temp, 0);
555 case FcTypeDouble:
556 sprintf ((char *)temp, "%g", v.u.d);
557 return FcNameUnparseString (buf, temp, 0);
558 case FcTypeString:
559 return FcNameUnparseString (buf, v.u.s, escape);
560 case FcTypeBool:
561 return FcNameUnparseString (buf,
562 v.u.b == FcTrue ? (FcChar8 *)"True" : v.u.b == FcFalse ? (FcChar8 *)"False"
563 : (FcChar8 *)"DontCare",
564 0);
565 case FcTypeMatrix:
566 sprintf ((char *)temp, "%g %g %g %g",
567 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
568 return FcNameUnparseString (buf, temp, 0);
569 case FcTypeCharSet:
570 return FcNameUnparseCharSet (buf, v.u.c);
571 case FcTypeLangSet:
572 return FcNameUnparseLangSet (buf, v.u.l);
573 case FcTypeFTFace:
574 return FcTrue;
575 case FcTypeRange:
576 sprintf ((char *)temp, "[%g %g]", v.u.r->begin, v.u.r->end);
577 return FcNameUnparseString (buf, temp, 0);
578 }
579 return FcFalse;
580 }
581
582 FcBool
583 FcNameUnparseValueList (FcStrBuf *buf,
584 FcValueListPtr v,
585 FcChar8 *escape)
586 {
587 while (v) {
588 if (!FcNameUnparseValue (buf, &v->value, escape))
589 return FcFalse;
590 if ((v = FcValueListNext (v)) != NULL)
591 if (!FcNameUnparseString (buf, (FcChar8 *)",", 0))
592 return FcFalse;
593 }
594 return FcTrue;
595 }
596
597 #define FC_ESCAPE_FIXED "\\-:,"
598 #define FC_ESCAPE_VARIABLE "\\=_:,"
599
600 FcChar8 *
601 FcNameUnparse (FcPattern *pat)
602 {
603 return FcNameUnparseEscaped (pat, FcTrue);
604 }
605
606 FcChar8 *
607 FcNameUnparseEscaped (FcPattern *pat, FcBool escape)
608 {
609 FcStrBuf buf, buf2;
610 FcChar8 buf_static[8192], buf2_static[256];
611 int i;
612 FcPatternElt *e;
613
614 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
615 FcStrBufInit (&buf2, buf2_static, sizeof (buf2_static));
616 e = FcPatternObjectFindElt (pat, FC_FAMILY_OBJECT);
617 if (e) {
618 if (!FcNameUnparseValueList (&buf, FcPatternEltValues (e), escape ? (FcChar8 *)FC_ESCAPE_FIXED : 0))
619 goto bail0;
620 }
621 e = FcPatternObjectFindElt (pat, FC_SIZE_OBJECT);
622 if (e) {
623 FcChar8 *p;
624
625 if (!FcNameUnparseString (&buf2, (FcChar8 *)"-", 0))
626 goto bail0;
627 if (!FcNameUnparseValueList (&buf2, FcPatternEltValues (e), escape ? (FcChar8 *)FC_ESCAPE_FIXED : 0))
628 goto bail0;
629 p = FcStrBufDoneStatic (&buf2);
630 FcStrBufDestroy (&buf2);
631 if (strlen ((const char *)p) > 1)
632 if (!FcStrBufString (&buf, p))
633 goto bail0;
634 }
635 for (i = 0; i < NUM_OBJECT_TYPES; i++) {
636 FcObject id = i + 1;
637 const FcObjectType *o;
638 o = &FcObjects[i];
639 if (!strcmp (o->object, FC_FAMILY) ||
640 !strcmp (o->object, FC_SIZE))
641 continue;
642
643 e = FcPatternObjectFindElt (pat, id);
644 if (e) {
645 if (!FcNameUnparseString (&buf, (FcChar8 *)":", 0))
646 goto bail0;
647 if (!FcNameUnparseString (&buf, (FcChar8 *)o->object, escape ? (FcChar8 *)FC_ESCAPE_VARIABLE : 0))
648 goto bail0;
649 if (!FcNameUnparseString (&buf, (FcChar8 *)"=", 0))
650 goto bail0;
651 if (!FcNameUnparseValueList (&buf, FcPatternEltValues (e), escape ? (FcChar8 *)FC_ESCAPE_VARIABLE : 0))
652 goto bail0;
653 }
654 }
655 return FcStrBufDone (&buf);
656 bail0:
657 FcStrBufDestroy (&buf);
658 return 0;
659 }
660 #define __fcname__
661 #include "fcaliastail.h"
662 #undef __fcname__