]> git.wh0rd.org - fontconfig.git/blob - src/fcname.c
Overhaul the serialization system to create one mmapable file per directory
[fontconfig.git] / src / fcname.c
1 /*
2 * $RCSId: xc/lib/fontconfig/src/fcname.c,v 1.15 2002/09/26 00:17:28 keithp Exp $
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 Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes 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 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD 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 <ctype.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include "fcint.h"
30
31 static const FcObjectType _FcBaseObjectTypes[] = {
32 { FC_FAMILY, FcTypeString, },
33 { FC_FAMILYLANG, FcTypeString, },
34 { FC_STYLE, FcTypeString, },
35 { FC_STYLELANG, FcTypeString, },
36 { FC_FULLNAME, FcTypeString, },
37 { FC_FULLNAMELANG, FcTypeString, },
38 { FC_SLANT, FcTypeInteger, },
39 { FC_WEIGHT, FcTypeInteger, },
40 { FC_WIDTH, FcTypeInteger, },
41 { FC_SIZE, FcTypeDouble, },
42 { FC_ASPECT, FcTypeDouble, },
43 { FC_PIXEL_SIZE, FcTypeDouble, },
44 { FC_SPACING, FcTypeInteger, },
45 { FC_FOUNDRY, FcTypeString, },
46 /* { FC_CORE, FcTypeBool, }, */
47 { FC_ANTIALIAS, FcTypeBool, },
48 { FC_HINT_STYLE, FcTypeInteger, },
49 { FC_HINTING, FcTypeBool, },
50 { FC_VERTICAL_LAYOUT, FcTypeBool, },
51 { FC_AUTOHINT, FcTypeBool, },
52 { FC_GLOBAL_ADVANCE, FcTypeBool, },
53 /* { FC_XLFD, FcTypeString, }, */
54 { FC_FILE, FcTypeString, },
55 { FC_INDEX, FcTypeInteger, },
56 { FC_RASTERIZER, FcTypeString, },
57 { FC_OUTLINE, FcTypeBool, },
58 { FC_SCALABLE, FcTypeBool, },
59 { FC_DPI, FcTypeDouble },
60 { FC_RGBA, FcTypeInteger, },
61 { FC_SCALE, FcTypeDouble, },
62 /* { FC_RENDER, FcTypeBool, },*/
63 { FC_MINSPACE, FcTypeBool, },
64 { FC_CHAR_WIDTH, FcTypeInteger },
65 { FC_CHAR_HEIGHT, FcTypeInteger },
66 { FC_MATRIX, FcTypeMatrix },
67 { FC_CHARSET, FcTypeCharSet },
68 { FC_LANG, FcTypeLangSet },
69 { FC_FONTVERSION, FcTypeInteger },
70 { FC_CAPABILITY, FcTypeString },
71 { FC_FONTFORMAT, FcTypeString },
72 { FC_EMBOLDEN, FcTypeBool },
73 };
74
75 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
76
77 typedef struct _FcObjectTypeList FcObjectTypeList;
78
79 struct _FcObjectTypeList {
80 const FcObjectTypeList *next;
81 const FcObjectType *types;
82 int ntypes;
83 };
84
85 static const FcObjectTypeList _FcBaseObjectTypesList = {
86 0,
87 _FcBaseObjectTypes,
88 NUM_OBJECT_TYPES
89 };
90
91 static const FcObjectTypeList *_FcObjectTypes = &_FcBaseObjectTypesList;
92
93 FcBool
94 FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
95 {
96 FcObjectTypeList *l;
97
98 l = (FcObjectTypeList *) malloc (sizeof (FcObjectTypeList));
99 if (!l)
100 return FcFalse;
101 FcMemAlloc (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
102 l->types = types;
103 l->ntypes = ntypes;
104 l->next = _FcObjectTypes;
105 _FcObjectTypes = l;
106 return FcTrue;
107 }
108
109 FcBool
110 FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
111 {
112 const FcObjectTypeList *l, **prev;
113
114 for (prev = &_FcObjectTypes;
115 (l = *prev);
116 prev = (const FcObjectTypeList **) &(l->next))
117 {
118 if (l->types == types && l->ntypes == ntypes)
119 {
120 *prev = l->next;
121 FcMemFree (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
122 free ((void *) l);
123 return FcTrue;
124 }
125 }
126 return FcFalse;
127 }
128
129 const FcObjectType *
130 FcNameGetObjectType (const char *object)
131 {
132 int i;
133 const FcObjectTypeList *l;
134 const FcObjectType *t;
135
136 for (l = _FcObjectTypes; l; l = l->next)
137 {
138 for (i = 0; i < l->ntypes; i++)
139 {
140 t = &l->types[i];
141 if (!strcmp (object, t->object))
142 return t;
143 }
144 }
145 return 0;
146 }
147
148 static int objectptr_count = 1;
149 static int objectptr_alloc = 0;
150 static int * objectptr_indices = 0;
151
152 void
153 FcObjectNewBank(void)
154 {
155 objectptr_count = 1;
156 objectptr_alloc = 0;
157 objectptr_indices = 0;
158 }
159
160 // XXX todo: introduce a hashtable for faster lookup
161 FcObjectPtr
162 FcObjectToPtr (const char * object)
163 {
164 int i;
165 const FcObjectTypeList *l;
166 const FcObjectType *t;
167
168 for (l = _FcObjectTypes; l; l = l->next)
169 {
170 for (i = 0; i < l->ntypes; i++)
171 {
172 t = &l->types[i];
173 if (!strcmp (object, t->object))
174 return i;
175 }
176 }
177 abort();
178 return 0;
179 }
180
181 const char *
182 FcObjectPtrU (FcObjectPtr si)
183 {
184 return _FcObjectTypes->types[si].object;
185 }
186
187 int
188 FcObjectNeededBytes (FcObjectPtr si)
189 {
190 return 0;
191 }
192
193 void *
194 FcObjectDistributeBytes (FcCache * metadata, void * block_ptr)
195 {
196 return block_ptr;
197 }
198
199 FcObjectPtr
200 FcObjectSerialize (FcObjectPtr si)
201 {
202 return si;
203 }
204
205 void
206 FcObjectUnserialize (FcCache metadata, FcConfig * config, void *block_ptr)
207 {
208 }
209
210 int
211 FcObjectPtrCompare (const FcObjectPtr a, const FcObjectPtr b)
212 {
213 return a - b;
214 }
215
216 static const FcConstant _FcBaseConstants[] = {
217 { (FcChar8 *) "thin", "weight", FC_WEIGHT_THIN, },
218 { (FcChar8 *) "extralight", "weight", FC_WEIGHT_EXTRALIGHT, },
219 { (FcChar8 *) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT, },
220 { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, },
221 { (FcChar8 *) "book", "weight", FC_WEIGHT_BOOK, },
222 { (FcChar8 *) "regular", "weight", FC_WEIGHT_REGULAR, },
223 { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, },
224 { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, },
225 { (FcChar8 *) "semibold", "weight", FC_WEIGHT_DEMIBOLD, },
226 { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, },
227 { (FcChar8 *) "extrabold", "weight", FC_WEIGHT_EXTRABOLD, },
228 { (FcChar8 *) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD, },
229 { (FcChar8 *) "black", "weight", FC_WEIGHT_BLACK, },
230
231 { (FcChar8 *) "roman", "slant", FC_SLANT_ROMAN, },
232 { (FcChar8 *) "italic", "slant", FC_SLANT_ITALIC, },
233 { (FcChar8 *) "oblique", "slant", FC_SLANT_OBLIQUE, },
234
235 { (FcChar8 *) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED },
236 { (FcChar8 *) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED },
237 { (FcChar8 *) "condensed", "width", FC_WIDTH_CONDENSED },
238 { (FcChar8 *) "semicondensed", "width", FC_WIDTH_SEMICONDENSED },
239 { (FcChar8 *) "normal", "width", FC_WIDTH_NORMAL },
240 { (FcChar8 *) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED },
241 { (FcChar8 *) "expanded", "width", FC_WIDTH_EXPANDED },
242 { (FcChar8 *) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED },
243 { (FcChar8 *) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED },
244
245 { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, },
246 { (FcChar8 *) "dual", "spacing", FC_DUAL, },
247 { (FcChar8 *) "mono", "spacing", FC_MONO, },
248 { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, },
249
250 { (FcChar8 *) "unknown", "rgba", FC_RGBA_UNKNOWN },
251 { (FcChar8 *) "rgb", "rgba", FC_RGBA_RGB, },
252 { (FcChar8 *) "bgr", "rgba", FC_RGBA_BGR, },
253 { (FcChar8 *) "vrgb", "rgba", FC_RGBA_VRGB },
254 { (FcChar8 *) "vbgr", "rgba", FC_RGBA_VBGR },
255 { (FcChar8 *) "none", "rgba", FC_RGBA_NONE },
256
257 { (FcChar8 *) "hintnone", "hintstyle", FC_HINT_NONE },
258 { (FcChar8 *) "hintslight", "hintstyle", FC_HINT_SLIGHT },
259 { (FcChar8 *) "hintmedium", "hintstyle", FC_HINT_MEDIUM },
260 { (FcChar8 *) "hintfull", "hintstyle", FC_HINT_FULL },
261 };
262
263 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
264
265 typedef struct _FcConstantList FcConstantList;
266
267 struct _FcConstantList {
268 const FcConstantList *next;
269 const FcConstant *consts;
270 int nconsts;
271 };
272
273 static const FcConstantList _FcBaseConstantList = {
274 0,
275 _FcBaseConstants,
276 NUM_FC_CONSTANTS
277 };
278
279 static const FcConstantList *_FcConstants = &_FcBaseConstantList;
280
281 FcBool
282 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
283 {
284 FcConstantList *l;
285
286 l = (FcConstantList *) malloc (sizeof (FcConstantList));
287 if (!l)
288 return FcFalse;
289 FcMemAlloc (FC_MEM_CONSTANT, sizeof (FcConstantList));
290 l->consts = consts;
291 l->nconsts = nconsts;
292 l->next = _FcConstants;
293 _FcConstants = l;
294 return FcTrue;
295 }
296
297 FcBool
298 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
299 {
300 const FcConstantList *l, **prev;
301
302 for (prev = &_FcConstants;
303 (l = *prev);
304 prev = (const FcConstantList **) &(l->next))
305 {
306 if (l->consts == consts && l->nconsts == nconsts)
307 {
308 *prev = l->next;
309 FcMemFree (FC_MEM_CONSTANT, sizeof (FcConstantList));
310 free ((void *) l);
311 return FcTrue;
312 }
313 }
314 return FcFalse;
315 }
316
317 const FcConstant *
318 FcNameGetConstant (FcChar8 *string)
319 {
320 const FcConstantList *l;
321 int i;
322
323 for (l = _FcConstants; l; l = l->next)
324 {
325 for (i = 0; i < l->nconsts; i++)
326 if (!FcStrCmpIgnoreCase (string, l->consts[i].name))
327 return &l->consts[i];
328 }
329 return 0;
330 }
331
332 FcBool
333 FcNameConstant (FcChar8 *string, int *result)
334 {
335 const FcConstant *c;
336
337 if ((c = FcNameGetConstant(string)))
338 {
339 *result = c->value;
340 return FcTrue;
341 }
342 return FcFalse;
343 }
344
345 FcBool
346 FcNameBool (const FcChar8 *v, FcBool *result)
347 {
348 char c0, c1;
349
350 c0 = *v;
351 c0 = FcToLower (c0);
352 if (c0 == 't' || c0 == 'y' || c0 == '1')
353 {
354 *result = FcTrue;
355 return FcTrue;
356 }
357 if (c0 == 'f' || c0 == 'n' || c0 == '0')
358 {
359 *result = FcFalse;
360 return FcTrue;
361 }
362 if (c0 == 'o')
363 {
364 c1 = v[1];
365 c1 = FcToLower (c1);
366 if (c1 == 'n')
367 {
368 *result = FcTrue;
369 return FcTrue;
370 }
371 if (c1 == 'f')
372 {
373 *result = FcFalse;
374 return FcTrue;
375 }
376 }
377 return FcFalse;
378 }
379
380 static FcValue
381 FcNameConvert (FcType type, FcChar8 *string, FcMatrix *m)
382 {
383 FcValue v;
384
385 v.type = type;
386 switch (v.type) {
387 case FcTypeInteger:
388 if (!FcNameConstant (string, &v.u.i))
389 v.u.i = atoi ((char *) string);
390 break;
391 case FcTypeString:
392 v.u.s = FcObjectStaticName(string);
393 break;
394 case FcTypeBool:
395 if (!FcNameBool (string, &v.u.b))
396 v.u.b = FcFalse;
397 break;
398 case FcTypeDouble:
399 v.u.d = strtod ((char *) string, 0);
400 break;
401 case FcTypeMatrix:
402 v.u.m = m;
403 sscanf ((char *) string, "%lg %lg %lg %lg", &m->xx, &m->xy, &m->yx, &m->yy);
404 break;
405 case FcTypeCharSet:
406 v.u.c = FcNameParseCharSet (string);
407 break;
408 case FcTypeLangSet:
409 v.u.l = FcNameParseLangSet (string);
410 break;
411 default:
412 break;
413 }
414 return v;
415 }
416
417 static const FcChar8 *
418 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
419 {
420 FcChar8 c;
421
422 while ((c = *cur))
423 {
424 if (c == '\\')
425 {
426 ++cur;
427 if (!(c = *cur))
428 break;
429 }
430 else if (strchr (delim, c))
431 break;
432 ++cur;
433 *save++ = c;
434 }
435 *save = 0;
436 *last = *cur;
437 if (*cur)
438 cur++;
439 return cur;
440 }
441
442 FcPattern *
443 FcNameParse (const FcChar8 *name)
444 {
445 FcChar8 *save;
446 FcPattern *pat;
447 double d;
448 FcChar8 *e;
449 FcChar8 delim;
450 FcValue v;
451 FcMatrix m;
452 const FcObjectType *t;
453 const FcConstant *c;
454
455 /* freed below */
456 save = malloc (strlen ((char *) name) + 1);
457 if (!save)
458 goto bail0;
459 pat = FcPatternCreate ();
460 if (!pat)
461 goto bail1;
462
463 for (;;)
464 {
465 name = FcNameFindNext (name, "-,:", save, &delim);
466 if (save[0])
467 {
468 if (!FcPatternAddString (pat, FC_FAMILY, save))
469 goto bail2;
470 }
471 if (delim != ',')
472 break;
473 }
474 if (delim == '-')
475 {
476 for (;;)
477 {
478 name = FcNameFindNext (name, "-,:", save, &delim);
479 d = strtod ((char *) save, (char **) &e);
480 if (e != save)
481 {
482 if (!FcPatternAddDouble (pat, FC_SIZE, d))
483 goto bail2;
484 }
485 if (delim != ',')
486 break;
487 }
488 }
489 while (delim == ':')
490 {
491 name = FcNameFindNext (name, "=_:", save, &delim);
492 if (save[0])
493 {
494 if (delim == '=' || delim == '_')
495 {
496 t = FcNameGetObjectType ((char *) save);
497 for (;;)
498 {
499 name = FcNameFindNext (name, ":,", save, &delim);
500 if (t)
501 {
502 v = FcNameConvert (t->type, save, &m);
503 if (!FcPatternAdd (pat, t->object, v, FcTrue))
504 {
505 switch (v.type) {
506 case FcTypeCharSet:
507 FcCharSetDestroy ((FcCharSet *) v.u.c);
508 break;
509 case FcTypeLangSet:
510 FcLangSetDestroy ((FcLangSet *) v.u.l);
511 break;
512 default:
513 break;
514 }
515 goto bail2;
516 }
517 switch (v.type) {
518 case FcTypeCharSet:
519 FcCharSetDestroy ((FcCharSet *) v.u.c);
520 break;
521 case FcTypeLangSet:
522 FcLangSetDestroy ((FcLangSet *) v.u.l);
523 break;
524 default:
525 break;
526 }
527 }
528 if (delim != ',')
529 break;
530 }
531 }
532 else
533 {
534 if ((c = FcNameGetConstant (save)))
535 {
536 if (!FcPatternAddInteger (pat, c->object, c->value))
537 goto bail2;
538 }
539 }
540 }
541 }
542
543 free (save);
544 return pat;
545
546 bail2:
547 FcPatternDestroy (pat);
548 bail1:
549 free (save);
550 bail0:
551 return 0;
552 }
553 static FcBool
554 FcNameUnparseString (FcStrBuf *buf,
555 const FcChar8 *string,
556 const FcChar8 *escape)
557 {
558 FcChar8 c;
559 while ((c = *string++))
560 {
561 if (escape && strchr ((char *) escape, (char) c))
562 {
563 if (!FcStrBufChar (buf, escape[0]))
564 return FcFalse;
565 }
566 if (!FcStrBufChar (buf, c))
567 return FcFalse;
568 }
569 return FcTrue;
570 }
571
572 static FcBool
573 FcNameUnparseValue (FcStrBuf *buf,
574 int bank,
575 FcValue *v0,
576 FcChar8 *escape)
577 {
578 FcChar8 temp[1024];
579 FcValue v = FcValueCanonicalize(v0);
580
581 switch (v.type) {
582 case FcTypeVoid:
583 return FcTrue;
584 case FcTypeInteger:
585 sprintf ((char *) temp, "%d", v.u.i);
586 return FcNameUnparseString (buf, temp, 0);
587 case FcTypeDouble:
588 sprintf ((char *) temp, "%g", v.u.d);
589 return FcNameUnparseString (buf, temp, 0);
590 case FcTypeString:
591 return FcNameUnparseString (buf, v.u.s, escape);
592 case FcTypeBool:
593 return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
594 case FcTypeMatrix:
595 sprintf ((char *) temp, "%g %g %g %g",
596 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
597 return FcNameUnparseString (buf, temp, 0);
598 case FcTypeCharSet:
599 return FcNameUnparseCharSet (buf, v.u.c);
600 case FcTypeLangSet:
601 return FcNameUnparseLangSet (buf, v.u.l);
602 case FcTypeFTFace:
603 return FcTrue;
604 }
605 return FcFalse;
606 }
607
608 static FcBool
609 FcNameUnparseValueList (FcStrBuf *buf,
610 FcValueListPtr v,
611 FcChar8 *escape)
612 {
613 while (FcValueListPtrU(v))
614 {
615 if (!FcNameUnparseValue (buf, v.bank, &FcValueListPtrU(v)->value, escape))
616 return FcFalse;
617 if (FcValueListPtrU(v = FcValueListPtrU(v)->next))
618 if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
619 return FcFalse;
620 }
621 return FcTrue;
622 }
623
624 #define FC_ESCAPE_FIXED "\\-:,"
625 #define FC_ESCAPE_VARIABLE "\\=_:,"
626
627 FcChar8 *
628 FcNameUnparse (FcPattern *pat)
629 {
630 FcStrBuf buf;
631 FcChar8 buf_static[8192];
632 int i;
633 FcPatternElt *e;
634 const FcObjectTypeList *l;
635 const FcObjectType *o;
636
637 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
638 e = FcPatternFindElt (pat, FC_FAMILY);
639 if (e)
640 {
641 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
642 goto bail0;
643 }
644 e = FcPatternFindElt (pat, FC_SIZE);
645 if (e)
646 {
647 if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
648 goto bail0;
649 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
650 goto bail0;
651 }
652 for (l = _FcObjectTypes; l; l = l->next)
653 {
654 for (i = 0; i < l->ntypes; i++)
655 {
656 o = &l->types[i];
657 if (!strcmp (o->object, FC_FAMILY) ||
658 !strcmp (o->object, FC_SIZE) ||
659 !strcmp (o->object, FC_FILE))
660 continue;
661
662 e = FcPatternFindElt (pat, o->object);
663 if (e)
664 {
665 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
666 goto bail0;
667 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, (FcChar8 *) FC_ESCAPE_VARIABLE))
668 goto bail0;
669 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
670 goto bail0;
671 if (!FcNameUnparseValueList (&buf, e->values,
672 (FcChar8 *) FC_ESCAPE_VARIABLE))
673 goto bail0;
674 }
675 }
676 }
677 return FcStrBufDone (&buf);
678 bail0:
679 FcStrBufDestroy (&buf);
680 return 0;
681 }