2 * $RCSId: xc/lib/fontconfig/src/fcname.c,v 1.15 2002/09/26 00:17:28 keithp Exp $
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 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.
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.
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 },
74 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
76 typedef struct _FcObjectTypeList FcObjectTypeList;
78 struct _FcObjectTypeList {
79 const FcObjectTypeList *next;
80 const FcObjectType *types;
84 static const FcObjectTypeList _FcBaseObjectTypesList = {
90 static const FcObjectTypeList *_FcObjectTypes = &_FcBaseObjectTypesList;
93 FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
97 l = (FcObjectTypeList *) malloc (sizeof (FcObjectTypeList));
100 FcMemAlloc (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
103 l->next = _FcObjectTypes;
109 FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
111 const FcObjectTypeList *l, **prev;
113 for (prev = &_FcObjectTypes;
115 prev = (const FcObjectTypeList **) &(l->next))
117 if (l->types == types && l->ntypes == ntypes)
120 FcMemFree (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
129 FcNameGetObjectType (const char *object)
132 const FcObjectTypeList *l;
133 const FcObjectType *t;
135 for (l = _FcObjectTypes; l; l = l->next)
137 for (i = 0; i < l->ntypes; i++)
140 if (!strcmp (object, t->object))
147 static const FcConstant _FcBaseConstants[] = {
148 { (FcChar8 *) "thin", "weight", FC_WEIGHT_THIN, },
149 { (FcChar8 *) "extralight", "weight", FC_WEIGHT_EXTRALIGHT, },
150 { (FcChar8 *) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT, },
151 { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, },
152 { (FcChar8 *) "book", "weight", FC_WEIGHT_BOOK, },
153 { (FcChar8 *) "regular", "weight", FC_WEIGHT_REGULAR, },
154 { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, },
155 { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, },
156 { (FcChar8 *) "semibold", "weight", FC_WEIGHT_DEMIBOLD, },
157 { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, },
158 { (FcChar8 *) "extrabold", "weight", FC_WEIGHT_EXTRABOLD, },
159 { (FcChar8 *) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD, },
160 { (FcChar8 *) "black", "weight", FC_WEIGHT_BLACK, },
162 { (FcChar8 *) "roman", "slant", FC_SLANT_ROMAN, },
163 { (FcChar8 *) "italic", "slant", FC_SLANT_ITALIC, },
164 { (FcChar8 *) "oblique", "slant", FC_SLANT_OBLIQUE, },
166 { (FcChar8 *) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED },
167 { (FcChar8 *) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED },
168 { (FcChar8 *) "condensed", "width", FC_WIDTH_CONDENSED },
169 { (FcChar8 *) "semicondensed", "width", FC_WIDTH_SEMICONDENSED },
170 { (FcChar8 *) "normal", "width", FC_WIDTH_NORMAL },
171 { (FcChar8 *) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED },
172 { (FcChar8 *) "expanded", "width", FC_WIDTH_EXPANDED },
173 { (FcChar8 *) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED },
174 { (FcChar8 *) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED },
176 { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, },
177 { (FcChar8 *) "dual", "spacing", FC_DUAL, },
178 { (FcChar8 *) "mono", "spacing", FC_MONO, },
179 { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, },
181 { (FcChar8 *) "unknown", "rgba", FC_RGBA_UNKNOWN },
182 { (FcChar8 *) "rgb", "rgba", FC_RGBA_RGB, },
183 { (FcChar8 *) "bgr", "rgba", FC_RGBA_BGR, },
184 { (FcChar8 *) "vrgb", "rgba", FC_RGBA_VRGB },
185 { (FcChar8 *) "vbgr", "rgba", FC_RGBA_VBGR },
186 { (FcChar8 *) "none", "rgba", FC_RGBA_NONE },
188 { (FcChar8 *) "hintnone", "hintstyle", FC_HINT_NONE },
189 { (FcChar8 *) "hintslight", "hintstyle", FC_HINT_SLIGHT },
190 { (FcChar8 *) "hintmedium", "hintstyle", FC_HINT_MEDIUM },
191 { (FcChar8 *) "hintfull", "hintstyle", FC_HINT_FULL },
194 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
196 typedef struct _FcConstantList FcConstantList;
198 struct _FcConstantList {
199 const FcConstantList *next;
200 const FcConstant *consts;
204 static const FcConstantList _FcBaseConstantList = {
210 static const FcConstantList *_FcConstants = &_FcBaseConstantList;
213 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
217 l = (FcConstantList *) malloc (sizeof (FcConstantList));
220 FcMemAlloc (FC_MEM_CONSTANT, sizeof (FcConstantList));
222 l->nconsts = nconsts;
223 l->next = _FcConstants;
229 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
231 const FcConstantList *l, **prev;
233 for (prev = &_FcConstants;
235 prev = (const FcConstantList **) &(l->next))
237 if (l->consts == consts && l->nconsts == nconsts)
240 FcMemFree (FC_MEM_CONSTANT, sizeof (FcConstantList));
249 FcNameGetConstant (FcChar8 *string)
251 const FcConstantList *l;
254 for (l = _FcConstants; l; l = l->next)
256 for (i = 0; i < l->nconsts; i++)
257 if (!FcStrCmpIgnoreCase (string, l->consts[i].name))
258 return &l->consts[i];
264 FcNameConstant (FcChar8 *string, int *result)
268 if ((c = FcNameGetConstant(string)))
277 FcNameBool (const FcChar8 *v, FcBool *result)
283 if (c0 == 't' || c0 == 'y' || c0 == '1')
288 if (c0 == 'f' || c0 == 'n' || c0 == '0')
312 FcNameConvert (FcType type, FcChar8 *string, FcMatrix *m)
319 if (!FcNameConstant (string, &v.u.i))
320 v.u.i = atoi ((char *) string);
326 if (!FcNameBool (string, &v.u.b))
330 v.u.d = strtod ((char *) string, 0);
334 sscanf ((char *) string, "%lg %lg %lg %lg", &m->xx, &m->xy, &m->yx, &m->yy);
337 v.u.c = FcNameParseCharSet (string);
340 v.u.l = FcNameParseLangSet (string);
348 static const FcChar8 *
349 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
361 else if (strchr (delim, c))
374 FcNameParse (const FcChar8 *name)
383 const FcObjectType *t;
387 save = malloc (strlen ((char *) name) + 1);
390 pat = FcPatternCreate ();
396 name = FcNameFindNext (name, "-,:", save, &delim);
399 if (!FcPatternAddString (pat, FC_FAMILY, save))
409 name = FcNameFindNext (name, "-,:", save, &delim);
410 d = strtod ((char *) save, (char **) &e);
413 if (!FcPatternAddDouble (pat, FC_SIZE, d))
422 name = FcNameFindNext (name, "=_:", save, &delim);
425 if (delim == '=' || delim == '_')
427 t = FcNameGetObjectType ((char *) save);
430 name = FcNameFindNext (name, ":,", save, &delim);
433 v = FcNameConvert (t->type, save, &m);
434 if (!FcPatternAdd (pat, t->object, v, FcTrue))
438 FcCharSetDestroy ((FcCharSet *) v.u.c);
441 FcLangSetDestroy ((FcLangSet *) v.u.l);
450 FcCharSetDestroy ((FcCharSet *) v.u.c);
453 FcLangSetDestroy ((FcLangSet *) v.u.l);
465 if ((c = FcNameGetConstant (save)))
467 if (!FcPatternAddInteger (pat, c->object, c->value))
478 FcPatternDestroy (pat);
485 FcNameUnparseString (FcStrBuf *buf,
486 const FcChar8 *string,
487 const FcChar8 *escape)
490 while ((c = *string++))
492 if (escape && strchr ((char *) escape, (char) c))
494 if (!FcStrBufChar (buf, escape[0]))
497 if (!FcStrBufChar (buf, c))
504 FcNameUnparseValue (FcStrBuf *buf,
514 sprintf ((char *) temp, "%d", v.u.i);
515 return FcNameUnparseString (buf, temp, 0);
517 sprintf ((char *) temp, "%g", v.u.d);
518 return FcNameUnparseString (buf, temp, 0);
520 return FcNameUnparseString (buf, v.u.s, escape);
522 return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
524 sprintf ((char *) temp, "%g %g %g %g",
525 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
526 return FcNameUnparseString (buf, temp, 0);
528 return FcNameUnparseCharSet (buf, v.u.c);
530 return FcNameUnparseLangSet (buf, v.u.l);
538 FcNameUnparseValueList (FcStrBuf *buf,
544 if (!FcNameUnparseValue (buf, v->value, escape))
547 if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
553 #define FC_ESCAPE_FIXED "\\-:,"
554 #define FC_ESCAPE_VARIABLE "\\=_:,"
557 FcNameUnparse (FcPattern *pat)
560 FcChar8 buf_static[8192];
563 const FcObjectTypeList *l;
564 const FcObjectType *o;
566 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
567 e = FcPatternFindElt (pat, FC_FAMILY);
570 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
573 e = FcPatternFindElt (pat, FC_SIZE);
576 if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
578 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
581 for (l = _FcObjectTypes; l; l = l->next)
583 for (i = 0; i < l->ntypes; i++)
586 if (!strcmp (o->object, FC_FAMILY) ||
587 !strcmp (o->object, FC_SIZE) ||
588 !strcmp (o->object, FC_FILE))
591 e = FcPatternFindElt (pat, o->object);
594 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
596 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, (FcChar8 *) FC_ESCAPE_VARIABLE))
598 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
600 if (!FcNameUnparseValueList (&buf, e->values,
601 (FcChar8 *) FC_ESCAPE_VARIABLE))
606 return FcStrBufDone (&buf);
608 FcStrBufDestroy (&buf);