2 * fontconfig/src/fcname.c
4 * Copyright © 2000 Keith Packard
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.
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.
32 * Please do not change this list, it is used to initialize the object
33 * list in this order to match the FC_foo_OBJECT constants. Those
34 * constants are written into cache files.
37 static const FcObjectType _FcBaseObjectTypes[] = {
38 { FC_FAMILY, FcTypeString, }, /* 1 */
39 { FC_FAMILYLANG, FcTypeString, },
40 { FC_STYLE, FcTypeString, },
41 { FC_STYLELANG, FcTypeString, },
42 { FC_FULLNAME, FcTypeString, },
43 { FC_FULLNAMELANG, FcTypeString, },
44 { FC_SLANT, FcTypeInteger, },
45 { FC_WEIGHT, FcTypeInteger, },
46 { FC_WIDTH, FcTypeInteger, },
47 { FC_SIZE, FcTypeDouble, },
48 { FC_ASPECT, FcTypeDouble, },
49 { FC_PIXEL_SIZE, FcTypeDouble, },
50 { FC_SPACING, FcTypeInteger, },
51 { FC_FOUNDRY, FcTypeString, },
52 { FC_ANTIALIAS, FcTypeBool, },
53 { FC_HINT_STYLE, FcTypeInteger, },
54 { FC_HINTING, FcTypeBool, },
55 { FC_VERTICAL_LAYOUT, FcTypeBool, },
56 { FC_AUTOHINT, FcTypeBool, },
57 { FC_GLOBAL_ADVANCE, FcTypeBool, },
58 { FC_FILE, FcTypeString, },
59 { FC_INDEX, FcTypeInteger, },
60 { FC_RASTERIZER, FcTypeString, },
61 { FC_OUTLINE, FcTypeBool, },
62 { FC_SCALABLE, FcTypeBool, },
63 { FC_DPI, FcTypeDouble },
64 { FC_RGBA, FcTypeInteger, },
65 { FC_SCALE, FcTypeDouble, },
66 { FC_MINSPACE, FcTypeBool, },
67 { FC_CHAR_WIDTH, FcTypeInteger },
68 { FC_CHAR_HEIGHT, FcTypeInteger },
69 { FC_MATRIX, FcTypeMatrix },
70 { FC_CHARSET, FcTypeCharSet },
71 { FC_LANG, FcTypeLangSet },
72 { FC_FONTVERSION, FcTypeInteger },
73 { FC_CAPABILITY, FcTypeString },
74 { FC_FONTFORMAT, FcTypeString },
75 { FC_EMBOLDEN, FcTypeBool },
76 { FC_EMBEDDED_BITMAP, FcTypeBool },
77 { FC_DECORATIVE, FcTypeBool },
78 { FC_LCD_FILTER, FcTypeInteger }, /* 41 */
81 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
83 typedef struct _FcObjectTypeList FcObjectTypeList;
85 struct _FcObjectTypeList {
86 const FcObjectTypeList *next;
87 const FcObjectType *types;
91 static const FcObjectTypeList _FcBaseObjectTypesList = {
97 static const FcObjectTypeList *_FcObjectTypes = &_FcBaseObjectTypesList;
99 #define OBJECT_HASH_SIZE 31
101 typedef struct _FcObjectBucket {
102 struct _FcObjectBucket *next;
107 static FcObjectBucket *FcObjectBuckets[OBJECT_HASH_SIZE];
109 static FcObjectType *FcObjects = (FcObjectType *) _FcBaseObjectTypes;
110 static int FcObjectsNumber = NUM_OBJECT_TYPES;
111 static int FcObjectsSize = 0;
112 static FcBool FcObjectsInited;
114 static FcObjectType *
115 FcObjectInsert (const char *name, FcType type)
118 if (FcObjectsNumber >= FcObjectsSize)
120 int newsize = FcObjectsNumber * 2;
121 FcObjectType *newobjects;
124 newobjects = realloc (FcObjects, newsize * sizeof (FcObjectType));
127 newobjects = malloc (newsize * sizeof (FcObjectType));
129 memcpy (newobjects, FcObjects,
130 FcObjectsNumber * sizeof (FcObjectType));
134 FcObjects = newobjects;
135 FcObjectsSize = newsize;
137 o = &FcObjects[FcObjectsNumber];
145 FcObjectId (FcObjectType *o)
147 return o - FcObjects + 1;
150 static FcObjectType *
151 FcObjectFindByName (const char *object, FcBool insert)
153 FcChar32 hash = FcStringHash ((const FcChar8 *) object);
158 if (!FcObjectsInited)
160 for (p = &FcObjectBuckets[hash%OBJECT_HASH_SIZE]; (b = *p); p = &(b->next))
162 o = FcObjects + b->id - 1;
163 if (b->hash == hash && !strcmp (object, (o->object)))
169 * Hook it into the hash chain
171 b = malloc (sizeof(FcObjectBucket));
174 object = (const char *) FcStrCopy ((FcChar8 *) object);
179 o = FcObjectInsert (object, -1);
182 b->id = FcObjectId (o);
187 static FcObjectType *
188 FcObjectFindById (FcObject object)
190 if (1 <= object && object <= FcObjectsNumber)
191 return FcObjects + object - 1;
196 FcObjectHashInsert (const FcObjectType *object, FcBool copy)
198 FcChar32 hash = FcStringHash ((const FcChar8 *) object->object);
203 if (!FcObjectsInited)
205 for (p = &FcObjectBuckets[hash%OBJECT_HASH_SIZE]; (b = *p); p = &(b->next))
207 o = FcObjects + b->id - 1;
208 if (b->hash == hash && !strcmp (object->object, o->object))
212 * Hook it into the hash chain
214 b = malloc (sizeof(FcObjectBucket));
219 o = FcObjectInsert (object->object, object->type);
227 o = (FcObjectType *) object;
230 b->id = FcObjectId (o);
236 FcObjectHashRemove (const FcObjectType *object, FcBool cleanobj)
238 FcChar32 hash = FcStringHash ((const FcChar8 *) object->object);
243 if (!FcObjectsInited)
245 for (p = &FcObjectBuckets[hash%OBJECT_HASH_SIZE]; (b = *p); p = &(b->next))
247 o = FcObjects + b->id - 1;
248 if (b->hash == hash && !strcmp (object->object, o->object))
254 /* Clean up object array */
257 while (FcObjects[FcObjectsNumber-1].object == NULL)
266 FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
270 for (i = 0; i < ntypes; i++)
271 if (!FcObjectHashInsert (&types[i], FcTrue))
277 FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
281 for (i = 0; i < ntypes; i++)
282 FcObjectHashRemove (&types[i], FcTrue);
287 FcNameGetObjectType (const char *object)
289 return FcObjectFindByName (object, FcFalse);
293 FcObjectValidType (FcObject object, FcType type)
295 FcObjectType *t = FcObjectFindById (object);
301 if (type == FcTypeDouble || type == FcTypeInteger)
305 if (type == FcTypeLangSet || type == FcTypeString)
309 if (t->type == -1 || type == t->type)
319 FcObjectFromName (const char * name)
321 FcObjectType *o = FcObjectFindByName (name, FcTrue);
324 return FcObjectId (o);
329 FcObjectGetSet (void)
332 FcObjectSet *os = NULL;
335 os = FcObjectSetCreate ();
336 for (i = 0; i < FcObjectsNumber; i++)
337 FcObjectSetAdd (os, FcObjects[i].object);
350 FcObjectsInited = FcTrue;
351 for (i = 0; i < NUM_OBJECT_TYPES; i++)
352 if (!FcObjectHashInsert (&_FcBaseObjectTypes[i], FcFalse))
361 FcObjectBucket *b, *next;
363 for (i = 0; i < OBJECT_HASH_SIZE; i++)
365 for (b = FcObjectBuckets[i]; b; b = next)
370 FcObjectBuckets[i] = 0;
372 for (i = 0; i < FcObjectsNumber; i++)
373 if (FcObjects[i].type == -1)
374 free ((void*) FcObjects[i].object);
375 if (FcObjects != _FcBaseObjectTypes)
377 FcObjects = (FcObjectType *) _FcBaseObjectTypes;
378 FcObjectsNumber = NUM_OBJECT_TYPES;
380 FcObjectsInited = FcFalse;
384 FcObjectName (FcObject object)
386 FcObjectType *o = FcObjectFindById (object);
393 static const FcConstant _FcBaseConstants[] = {
394 { (FcChar8 *) "thin", "weight", FC_WEIGHT_THIN, },
395 { (FcChar8 *) "extralight", "weight", FC_WEIGHT_EXTRALIGHT, },
396 { (FcChar8 *) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT, },
397 { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, },
398 { (FcChar8 *) "book", "weight", FC_WEIGHT_BOOK, },
399 { (FcChar8 *) "regular", "weight", FC_WEIGHT_REGULAR, },
400 { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, },
401 { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, },
402 { (FcChar8 *) "semibold", "weight", FC_WEIGHT_DEMIBOLD, },
403 { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, },
404 { (FcChar8 *) "extrabold", "weight", FC_WEIGHT_EXTRABOLD, },
405 { (FcChar8 *) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD, },
406 { (FcChar8 *) "black", "weight", FC_WEIGHT_BLACK, },
407 { (FcChar8 *) "heavy", "weight", FC_WEIGHT_HEAVY, },
409 { (FcChar8 *) "roman", "slant", FC_SLANT_ROMAN, },
410 { (FcChar8 *) "italic", "slant", FC_SLANT_ITALIC, },
411 { (FcChar8 *) "oblique", "slant", FC_SLANT_OBLIQUE, },
413 { (FcChar8 *) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED },
414 { (FcChar8 *) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED },
415 { (FcChar8 *) "condensed", "width", FC_WIDTH_CONDENSED },
416 { (FcChar8 *) "semicondensed", "width", FC_WIDTH_SEMICONDENSED },
417 { (FcChar8 *) "normal", "width", FC_WIDTH_NORMAL },
418 { (FcChar8 *) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED },
419 { (FcChar8 *) "expanded", "width", FC_WIDTH_EXPANDED },
420 { (FcChar8 *) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED },
421 { (FcChar8 *) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED },
423 { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, },
424 { (FcChar8 *) "dual", "spacing", FC_DUAL, },
425 { (FcChar8 *) "mono", "spacing", FC_MONO, },
426 { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, },
428 { (FcChar8 *) "unknown", "rgba", FC_RGBA_UNKNOWN },
429 { (FcChar8 *) "rgb", "rgba", FC_RGBA_RGB, },
430 { (FcChar8 *) "bgr", "rgba", FC_RGBA_BGR, },
431 { (FcChar8 *) "vrgb", "rgba", FC_RGBA_VRGB },
432 { (FcChar8 *) "vbgr", "rgba", FC_RGBA_VBGR },
433 { (FcChar8 *) "none", "rgba", FC_RGBA_NONE },
435 { (FcChar8 *) "hintnone", "hintstyle", FC_HINT_NONE },
436 { (FcChar8 *) "hintslight", "hintstyle", FC_HINT_SLIGHT },
437 { (FcChar8 *) "hintmedium", "hintstyle", FC_HINT_MEDIUM },
438 { (FcChar8 *) "hintfull", "hintstyle", FC_HINT_FULL },
440 { (FcChar8 *) "antialias", "antialias", FcTrue },
441 { (FcChar8 *) "hinting", "hinting", FcTrue },
442 { (FcChar8 *) "verticallayout", "verticallayout", FcTrue },
443 { (FcChar8 *) "autohint", "autohint", FcTrue },
444 { (FcChar8 *) "globaladvance", "globaladvance", FcTrue },
445 { (FcChar8 *) "outline", "outline", FcTrue },
446 { (FcChar8 *) "scalable", "scalable", FcTrue },
447 { (FcChar8 *) "minspace", "minspace", FcTrue },
448 { (FcChar8 *) "embolden", "embolden", FcTrue },
449 { (FcChar8 *) "embeddedbitmap", "embeddedbitmap", FcTrue },
450 { (FcChar8 *) "decorative", "decorative", FcTrue },
451 { (FcChar8 *) "lcdnone", "lcdfilter", FC_LCD_NONE },
452 { (FcChar8 *) "lcddefault", "lcdfilter", FC_LCD_DEFAULT },
453 { (FcChar8 *) "lcdlight", "lcdfilter", FC_LCD_LIGHT },
454 { (FcChar8 *) "lcdlegacy", "lcdfilter", FC_LCD_LEGACY },
457 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
459 typedef struct _FcConstantList FcConstantList;
461 struct _FcConstantList {
462 const FcConstantList *next;
463 const FcConstant *consts;
467 static const FcConstantList _FcBaseConstantList = {
473 static const FcConstantList *_FcConstants = &_FcBaseConstantList;
476 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
480 l = (FcConstantList *) malloc (sizeof (FcConstantList));
483 FcMemAlloc (FC_MEM_CONSTANT, sizeof (FcConstantList));
485 l->nconsts = nconsts;
486 l->next = _FcConstants;
492 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
494 const FcConstantList *l, **prev;
496 for (prev = &_FcConstants;
498 prev = (const FcConstantList **) &(l->next))
500 if (l->consts == consts && l->nconsts == nconsts)
503 FcMemFree (FC_MEM_CONSTANT, sizeof (FcConstantList));
512 FcNameGetConstant (const FcChar8 *string)
514 const FcConstantList *l;
517 for (l = _FcConstants; l; l = l->next)
519 for (i = 0; i < l->nconsts; i++)
520 if (!FcStrCmpIgnoreCase (string, l->consts[i].name))
521 return &l->consts[i];
527 FcNameConstant (const FcChar8 *string, int *result)
531 if ((c = FcNameGetConstant(string)))
540 FcNameBool (const FcChar8 *v, FcBool *result)
546 if (c0 == 't' || c0 == 'y' || c0 == '1')
551 if (c0 == 'f' || c0 == 'n' || c0 == '0')
575 FcNameConvert (FcType type, FcChar8 *string)
583 if (!FcNameConstant (string, &v.u.i))
584 v.u.i = atoi ((char *) string);
587 v.u.s = FcSharedStr (string);
592 if (!FcNameBool (string, &v.u.b))
596 v.u.d = strtod ((char *) string, 0);
599 sscanf ((char *) string, "%lg %lg %lg %lg", &m.xx, &m.xy, &m.yx, &m.yy);
600 v.u.m = FcMatrixCopy (&m);
603 v.u.c = FcNameParseCharSet (string);
608 v.u.l = FcNameParseLangSet (string);
618 static const FcChar8 *
619 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
631 else if (strchr (delim, c))
644 FcNameParse (const FcChar8 *name)
652 const FcObjectType *t;
656 save = malloc (strlen ((char *) name) + 1);
659 pat = FcPatternCreate ();
665 name = FcNameFindNext (name, "-,:", save, &delim);
668 if (!FcPatternAddString (pat, FC_FAMILY, save))
678 name = FcNameFindNext (name, "-,:", save, &delim);
679 d = strtod ((char *) save, (char **) &e);
682 if (!FcPatternAddDouble (pat, FC_SIZE, d))
691 name = FcNameFindNext (name, "=_:", save, &delim);
694 if (delim == '=' || delim == '_')
696 t = FcNameGetObjectType ((char *) save);
699 name = FcNameFindNext (name, ":,", save, &delim);
702 v = FcNameConvert (t->type, save);
703 if (!FcPatternAdd (pat, t->object, v, FcTrue))
716 if ((c = FcNameGetConstant (save)))
718 t = FcNameGetObjectType ((char *) c->object);
722 if (!FcPatternAddInteger (pat, c->object, c->value))
726 if (!FcPatternAddBool (pat, c->object, c->value))
741 FcPatternDestroy (pat);
748 FcNameUnparseString (FcStrBuf *buf,
749 const FcChar8 *string,
750 const FcChar8 *escape)
753 while ((c = *string++))
755 if (escape && strchr ((char *) escape, (char) c))
757 if (!FcStrBufChar (buf, escape[0]))
760 if (!FcStrBufChar (buf, c))
767 FcNameUnparseValue (FcStrBuf *buf,
772 FcValue v = FcValueCanonicalize(v0);
778 sprintf ((char *) temp, "%d", v.u.i);
779 return FcNameUnparseString (buf, temp, 0);
781 sprintf ((char *) temp, "%g", v.u.d);
782 return FcNameUnparseString (buf, temp, 0);
784 return FcNameUnparseString (buf, v.u.s, escape);
786 return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
788 sprintf ((char *) temp, "%g %g %g %g",
789 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
790 return FcNameUnparseString (buf, temp, 0);
792 return FcNameUnparseCharSet (buf, v.u.c);
794 return FcNameUnparseLangSet (buf, v.u.l);
802 FcNameUnparseValueList (FcStrBuf *buf,
808 if (!FcNameUnparseValue (buf, &v->value, escape))
810 if ((v = FcValueListNext(v)) != NULL)
811 if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
817 #define FC_ESCAPE_FIXED "\\-:,"
818 #define FC_ESCAPE_VARIABLE "\\=_:,"
821 FcNameUnparse (FcPattern *pat)
823 return FcNameUnparseEscaped (pat, FcTrue);
827 FcNameUnparseEscaped (FcPattern *pat, FcBool escape)
830 FcChar8 buf_static[8192];
833 const FcObjectTypeList *l;
834 const FcObjectType *o;
836 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
837 e = FcPatternObjectFindElt (pat, FC_FAMILY_OBJECT);
840 if (!FcNameUnparseValueList (&buf, FcPatternEltValues(e), escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
843 e = FcPatternObjectFindElt (pat, FC_SIZE_OBJECT);
846 if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
848 if (!FcNameUnparseValueList (&buf, FcPatternEltValues(e), escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
851 for (l = _FcObjectTypes; l; l = l->next)
853 for (i = 0; i < l->ntypes; i++)
856 if (!strcmp (o->object, FC_FAMILY) ||
857 !strcmp (o->object, FC_SIZE))
860 e = FcPatternObjectFindElt (pat, FcObjectFromName (o->object));
863 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
865 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, escape ? (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
867 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
869 if (!FcNameUnparseValueList (&buf, FcPatternEltValues(e), escape ?
870 (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
875 return FcStrBufDone (&buf);
877 FcStrBufDestroy (&buf);
881 #include "fcaliastail.h"