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.
31 static const FcObjectType FcObjects[] = {
32 #define FC_OBJECT(NAME, Type, Cmp) { FC_##NAME, Type },
37 #define NUM_OBJECT_TYPES ((int) (sizeof FcObjects / sizeof FcObjects[0]))
39 static const FcObjectType *
40 FcObjectFindById (FcObject object)
42 if (1 <= object && object <= NUM_OBJECT_TYPES)
43 return &FcObjects[object - 1];
44 return FcObjectLookupOtherTypeById (object);
48 FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
55 FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
62 FcNameGetObjectType (const char *object)
64 int id = FcObjectLookupBuiltinIdByName (object);
67 return FcObjectLookupOtherTypeByName (object);
69 return &FcObjects[id - 1];
73 FcObjectValidType (FcObject object, FcType type)
75 const FcObjectType *t = FcObjectFindById (object);
78 switch ((int) t->type) {
83 if (type == FcTypeDouble || type == FcTypeInteger)
87 if (type == FcTypeLangSet || type == FcTypeString)
101 FcObjectFromName (const char * name)
103 return FcObjectLookupIdByName (name);
107 FcObjectGetSet (void)
110 FcObjectSet *os = NULL;
113 os = FcObjectSetCreate ();
114 for (i = 0; i < NUM_OBJECT_TYPES; i++)
115 FcObjectSetAdd (os, FcObjects[i].object);
121 FcObjectName (FcObject object)
123 const FcObjectType *o = FcObjectFindById (object);
128 return FcObjectLookupOtherNameById (object);
131 static const FcConstant _FcBaseConstants[] = {
132 { (FcChar8 *) "thin", "weight", FC_WEIGHT_THIN, },
133 { (FcChar8 *) "extralight", "weight", FC_WEIGHT_EXTRALIGHT, },
134 { (FcChar8 *) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT, },
135 { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, },
136 { (FcChar8 *) "book", "weight", FC_WEIGHT_BOOK, },
137 { (FcChar8 *) "regular", "weight", FC_WEIGHT_REGULAR, },
138 { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, },
139 { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, },
140 { (FcChar8 *) "semibold", "weight", FC_WEIGHT_DEMIBOLD, },
141 { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, },
142 { (FcChar8 *) "extrabold", "weight", FC_WEIGHT_EXTRABOLD, },
143 { (FcChar8 *) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD, },
144 { (FcChar8 *) "black", "weight", FC_WEIGHT_BLACK, },
145 { (FcChar8 *) "heavy", "weight", FC_WEIGHT_HEAVY, },
147 { (FcChar8 *) "roman", "slant", FC_SLANT_ROMAN, },
148 { (FcChar8 *) "italic", "slant", FC_SLANT_ITALIC, },
149 { (FcChar8 *) "oblique", "slant", FC_SLANT_OBLIQUE, },
151 { (FcChar8 *) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED },
152 { (FcChar8 *) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED },
153 { (FcChar8 *) "condensed", "width", FC_WIDTH_CONDENSED },
154 { (FcChar8 *) "semicondensed", "width", FC_WIDTH_SEMICONDENSED },
155 { (FcChar8 *) "normal", "width", FC_WIDTH_NORMAL },
156 { (FcChar8 *) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED },
157 { (FcChar8 *) "expanded", "width", FC_WIDTH_EXPANDED },
158 { (FcChar8 *) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED },
159 { (FcChar8 *) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED },
161 { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, },
162 { (FcChar8 *) "dual", "spacing", FC_DUAL, },
163 { (FcChar8 *) "mono", "spacing", FC_MONO, },
164 { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, },
166 { (FcChar8 *) "unknown", "rgba", FC_RGBA_UNKNOWN },
167 { (FcChar8 *) "rgb", "rgba", FC_RGBA_RGB, },
168 { (FcChar8 *) "bgr", "rgba", FC_RGBA_BGR, },
169 { (FcChar8 *) "vrgb", "rgba", FC_RGBA_VRGB },
170 { (FcChar8 *) "vbgr", "rgba", FC_RGBA_VBGR },
171 { (FcChar8 *) "none", "rgba", FC_RGBA_NONE },
173 { (FcChar8 *) "hintnone", "hintstyle", FC_HINT_NONE },
174 { (FcChar8 *) "hintslight", "hintstyle", FC_HINT_SLIGHT },
175 { (FcChar8 *) "hintmedium", "hintstyle", FC_HINT_MEDIUM },
176 { (FcChar8 *) "hintfull", "hintstyle", FC_HINT_FULL },
178 { (FcChar8 *) "antialias", "antialias", FcTrue },
179 { (FcChar8 *) "hinting", "hinting", FcTrue },
180 { (FcChar8 *) "verticallayout", "verticallayout", FcTrue },
181 { (FcChar8 *) "autohint", "autohint", FcTrue },
182 { (FcChar8 *) "globaladvance", "globaladvance", FcTrue }, /* deprecated */
183 { (FcChar8 *) "outline", "outline", FcTrue },
184 { (FcChar8 *) "scalable", "scalable", FcTrue },
185 { (FcChar8 *) "minspace", "minspace", FcTrue },
186 { (FcChar8 *) "embolden", "embolden", FcTrue },
187 { (FcChar8 *) "embeddedbitmap", "embeddedbitmap", FcTrue },
188 { (FcChar8 *) "decorative", "decorative", FcTrue },
189 { (FcChar8 *) "lcdnone", "lcdfilter", FC_LCD_NONE },
190 { (FcChar8 *) "lcddefault", "lcdfilter", FC_LCD_DEFAULT },
191 { (FcChar8 *) "lcdlight", "lcdfilter", FC_LCD_LIGHT },
192 { (FcChar8 *) "lcdlegacy", "lcdfilter", FC_LCD_LEGACY },
195 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
198 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
205 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
212 FcNameGetConstant (const FcChar8 *string)
216 for (i = 0; i < NUM_FC_CONSTANTS; i++)
217 if (!FcStrCmpIgnoreCase (string, _FcBaseConstants[i].name))
218 return &_FcBaseConstants[i];
224 FcNameConstant (const FcChar8 *string, int *result)
228 if ((c = FcNameGetConstant(string)))
237 FcNameBool (const FcChar8 *v, FcBool *result)
243 if (c0 == 't' || c0 == 'y' || c0 == '1')
248 if (c0 == 'f' || c0 == 'n' || c0 == '0')
272 FcNameConvert (FcType type, FcChar8 *string)
278 switch ((int) v.type) {
280 if (!FcNameConstant (string, &v.u.i))
281 v.u.i = atoi ((char *) string);
284 v.u.s = FcStrdup (string);
289 if (!FcNameBool (string, &v.u.b))
293 v.u.d = strtod ((char *) string, 0);
297 sscanf ((char *) string, "%lg %lg %lg %lg", &m.xx, &m.xy, &m.yx, &m.yy);
298 v.u.m = FcMatrixCopy (&m);
301 v.u.c = FcNameParseCharSet (string);
306 v.u.l = FcNameParseLangSet (string);
316 static const FcChar8 *
317 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
335 else if (strchr (delim, c))
348 FcNameParse (const FcChar8 *name)
356 const FcObjectType *t;
360 save = malloc (strlen ((char *) name) + 1);
363 pat = FcPatternCreate ();
369 name = FcNameFindNext (name, "-,:", save, &delim);
372 if (!FcPatternAddString (pat, FC_FAMILY, save))
382 name = FcNameFindNext (name, "-,:", save, &delim);
383 d = strtod ((char *) save, (char **) &e);
386 if (!FcPatternAddDouble (pat, FC_SIZE, d))
395 name = FcNameFindNext (name, "=_:", save, &delim);
398 if (delim == '=' || delim == '_')
400 t = FcNameGetObjectType ((char *) save);
403 name = FcNameFindNext (name, ":,", save, &delim);
406 v = FcNameConvert (t->type, save);
407 if (!FcPatternAdd (pat, t->object, v, FcTrue))
420 if ((c = FcNameGetConstant (save)))
422 t = FcNameGetObjectType ((char *) c->object);
425 switch ((int) t->type) {
428 if (!FcPatternAddInteger (pat, c->object, c->value))
432 if (!FcPatternAddBool (pat, c->object, c->value))
447 FcPatternDestroy (pat);
454 FcNameUnparseString (FcStrBuf *buf,
455 const FcChar8 *string,
456 const FcChar8 *escape)
459 while ((c = *string++))
461 if (escape && strchr ((char *) escape, (char) c))
463 if (!FcStrBufChar (buf, escape[0]))
466 if (!FcStrBufChar (buf, c))
473 FcNameUnparseValue (FcStrBuf *buf,
478 FcValue v = FcValueCanonicalize(v0);
485 sprintf ((char *) temp, "%d", v.u.i);
486 return FcNameUnparseString (buf, temp, 0);
488 sprintf ((char *) temp, "%g", v.u.d);
489 return FcNameUnparseString (buf, temp, 0);
491 return FcNameUnparseString (buf, v.u.s, escape);
493 return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
495 sprintf ((char *) temp, "%g %g %g %g",
496 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
497 return FcNameUnparseString (buf, temp, 0);
499 return FcNameUnparseCharSet (buf, v.u.c);
501 return FcNameUnparseLangSet (buf, v.u.l);
509 FcNameUnparseValueList (FcStrBuf *buf,
515 if (!FcNameUnparseValue (buf, &v->value, escape))
517 if ((v = FcValueListNext(v)) != NULL)
518 if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
524 #define FC_ESCAPE_FIXED "\\-:,"
525 #define FC_ESCAPE_VARIABLE "\\=_:,"
528 FcNameUnparse (FcPattern *pat)
530 return FcNameUnparseEscaped (pat, FcTrue);
534 FcNameUnparseEscaped (FcPattern *pat, FcBool escape)
537 FcChar8 buf_static[8192];
541 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
542 e = FcPatternObjectFindElt (pat, FC_FAMILY_OBJECT);
545 if (!FcNameUnparseValueList (&buf, FcPatternEltValues(e), escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
548 e = FcPatternObjectFindElt (pat, FC_SIZE_OBJECT);
551 if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
553 if (!FcNameUnparseValueList (&buf, FcPatternEltValues(e), escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
556 for (i = 0; i < NUM_OBJECT_TYPES; i++)
559 const FcObjectType *o;
561 if (!strcmp (o->object, FC_FAMILY) ||
562 !strcmp (o->object, FC_SIZE))
565 e = FcPatternObjectFindElt (pat, id);
568 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
570 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, escape ? (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
572 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
574 if (!FcNameUnparseValueList (&buf, FcPatternEltValues(e), escape ?
575 (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
579 return FcStrBufDone (&buf);
581 FcStrBufDestroy (&buf);
585 #include "fcaliastail.h"