2 * $XFree86: xc/lib/fontconfig/src/fcname.c,v 1.7 2002/06/03 08:31:15 keithp Exp $
4 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
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_STYLE, FcTypeString, },
34 { FC_SLANT, FcTypeInteger, },
35 { FC_WEIGHT, FcTypeInteger, },
36 { FC_SIZE, FcTypeDouble, },
37 { FC_ASPECT, FcTypeDouble, },
38 { FC_PIXEL_SIZE, FcTypeDouble, },
39 { FC_SPACING, FcTypeInteger, },
40 { FC_FOUNDRY, FcTypeString, },
41 /* { FC_CORE, FcTypeBool, }, */
42 { FC_ANTIALIAS, FcTypeBool, },
43 /* { FC_XLFD, FcTypeString, }, */
44 { FC_FILE, FcTypeString, },
45 { FC_INDEX, FcTypeInteger, },
46 { FC_RASTERIZER, FcTypeString, },
47 { FC_OUTLINE, FcTypeBool, },
48 { FC_SCALABLE, FcTypeBool, },
49 { FC_RGBA, FcTypeInteger, },
50 { FC_SCALE, FcTypeDouble, },
51 /* { FC_RENDER, FcTypeBool, },*/
52 { FC_MINSPACE, FcTypeBool, },
53 { FC_CHAR_WIDTH, FcTypeInteger },
54 { FC_CHAR_HEIGHT, FcTypeInteger },
55 { FC_MATRIX, FcTypeMatrix },
56 { FC_CHARSET, FcTypeCharSet },
57 { FC_LANG, FcTypeString },
60 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
62 typedef struct _FcObjectTypeList FcObjectTypeList;
64 struct _FcObjectTypeList {
65 const FcObjectTypeList *next;
66 const FcObjectType *types;
70 static const FcObjectTypeList _FcBaseObjectTypesList = {
76 static const FcObjectTypeList *_FcObjectTypes = &_FcBaseObjectTypesList;
79 FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
83 l = (FcObjectTypeList *) malloc (sizeof (FcObjectTypeList));
88 l->next = _FcObjectTypes;
94 FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
96 const FcObjectTypeList *l, **prev;
98 for (prev = &_FcObjectTypes;
100 prev = (const FcObjectTypeList **) &(l->next))
102 if (l->types == types && l->ntypes == ntypes)
113 FcNameGetObjectType (const char *object)
116 const FcObjectTypeList *l;
117 const FcObjectType *t;
119 for (l = _FcObjectTypes; l; l = l->next)
121 for (i = 0; i < l->ntypes; i++)
124 if (!strcmp (object, t->object))
131 static const FcConstant _FcBaseConstants[] = {
132 { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, },
133 { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, },
134 { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, },
135 { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, },
136 { (FcChar8 *) "black", "weight", FC_WEIGHT_BLACK, },
138 { (FcChar8 *) "roman", "slant", FC_SLANT_ROMAN, },
139 { (FcChar8 *) "italic", "slant", FC_SLANT_ITALIC, },
140 { (FcChar8 *) "oblique", "slant", FC_SLANT_OBLIQUE, },
142 { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, },
143 { (FcChar8 *) "mono", "spacing", FC_MONO, },
144 { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, },
146 { (FcChar8 *) "none", "rgba", FC_RGBA_NONE },
147 { (FcChar8 *) "rgb", "rgba", FC_RGBA_RGB, },
148 { (FcChar8 *) "bgr", "rgba", FC_RGBA_BGR, },
149 { (FcChar8 *) "vrgb", "rgba", FC_RGBA_VRGB },
150 { (FcChar8 *) "vbgr", "rgba", FC_RGBA_VBGR },
153 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
155 typedef struct _FcConstantList FcConstantList;
157 struct _FcConstantList {
158 const FcConstantList *next;
159 const FcConstant *consts;
163 static const FcConstantList _FcBaseConstantList = {
169 static const FcConstantList *_FcConstants = &_FcBaseConstantList;
172 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
176 l = (FcConstantList *) malloc (sizeof (FcConstantList));
180 l->nconsts = nconsts;
181 l->next = _FcConstants;
187 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
189 const FcConstantList *l, **prev;
191 for (prev = &_FcConstants;
193 prev = (const FcConstantList **) &(l->next))
195 if (l->consts == consts && l->nconsts == nconsts)
206 FcNameGetConstant (FcChar8 *string)
208 const FcConstantList *l;
211 for (l = _FcConstants; l; l = l->next)
213 for (i = 0; i < l->nconsts; i++)
214 if (!FcStrCmpIgnoreCase (string, l->consts[i].name))
215 return &l->consts[i];
221 FcNameConstant (FcChar8 *string, int *result)
225 if ((c = FcNameGetConstant(string)))
234 FcNameBool (FcChar8 *v, FcBool *result)
241 if (c0 == 't' || c0 == 'y' || c0 == '1')
246 if (c0 == 'f' || c0 == 'n' || c0 == '0')
271 FcNameConvert (FcType type, FcChar8 *string, FcMatrix *m)
278 if (!FcNameConstant (string, &v.u.i))
279 v.u.i = atoi ((char *) string);
285 if (!FcNameBool (string, &v.u.b))
289 v.u.d = strtod ((char *) string, 0);
293 sscanf ((char *) string, "%lg %lg %lg %lg", &m->xx, &m->xy, &m->yx, &m->yy);
296 v.u.c = FcNameParseCharSet (string);
304 static const FcChar8 *
305 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
317 else if (strchr (delim, c))
330 FcNameParse (const FcChar8 *name)
339 const FcObjectType *t;
342 save = malloc (strlen ((char *) name) + 1);
345 pat = FcPatternCreate ();
351 name = FcNameFindNext (name, "-,:", save, &delim);
354 if (!FcPatternAddString (pat, FC_FAMILY, save))
364 name = FcNameFindNext (name, "-,:", save, &delim);
365 d = strtod ((char *) save, (char **) &e);
368 if (!FcPatternAddDouble (pat, FC_SIZE, d))
377 name = FcNameFindNext (name, "=_:", save, &delim);
380 if (delim == '=' || delim == '_')
382 t = FcNameGetObjectType ((char *) save);
385 name = FcNameFindNext (name, ":,", save, &delim);
388 v = FcNameConvert (t->type, save, &m);
389 if (!FcPatternAdd (pat, t->object, v, FcTrue))
391 if (v.type == FcTypeCharSet)
392 FcCharSetDestroy ((FcCharSet *) v.u.c);
395 if (v.type == FcTypeCharSet)
396 FcCharSetDestroy ((FcCharSet *) v.u.c);
404 if ((c = FcNameGetConstant (save)))
406 if (!FcPatternAddInteger (pat, c->object, c->value))
417 FcPatternDestroy (pat);
424 FcNameUnparseString (FcStrBuf *buf,
425 const FcChar8 *string,
426 const FcChar8 *escape)
429 while ((c = *string++))
431 if (escape && strchr ((char *) escape, (char) c))
433 if (!FcStrBufChar (buf, escape[0]))
436 if (!FcStrBufChar (buf, c))
443 FcNameUnparseValue (FcStrBuf *buf,
453 sprintf ((char *) temp, "%d", v.u.i);
454 return FcNameUnparseString (buf, temp, 0);
456 sprintf ((char *) temp, "%g", v.u.d);
457 return FcNameUnparseString (buf, temp, 0);
459 return FcNameUnparseString (buf, v.u.s, escape);
461 return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
463 sprintf ((char *) temp, "%g %g %g %g",
464 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
465 return FcNameUnparseString (buf, temp, 0);
467 return FcNameUnparseCharSet (buf, v.u.c);
475 FcNameUnparseValueList (FcStrBuf *buf,
481 if (!FcNameUnparseValue (buf, v->value, escape))
484 if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
490 #define FC_ESCAPE_FIXED "\\-:,"
491 #define FC_ESCAPE_VARIABLE "\\=_:,"
494 FcNameUnparse (FcPattern *pat)
497 FcChar8 buf_static[8192];
500 const FcObjectTypeList *l;
501 const FcObjectType *o;
503 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
504 e = FcPatternFindElt (pat, FC_FAMILY);
507 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
510 e = FcPatternFindElt (pat, FC_SIZE);
513 if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
515 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
518 for (l = _FcObjectTypes; l; l = l->next)
520 for (i = 0; i < l->ntypes; i++)
523 if (!strcmp (o->object, FC_FAMILY) ||
524 !strcmp (o->object, FC_SIZE) ||
525 !strcmp (o->object, FC_FILE))
528 e = FcPatternFindElt (pat, o->object);
531 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
533 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, (FcChar8 *) FC_ESCAPE_VARIABLE))
535 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
537 if (!FcNameUnparseValueList (&buf, e->values,
538 (FcChar8 *) FC_ESCAPE_VARIABLE))
543 return FcStrBufDone (&buf);
545 FcStrBufDestroy (&buf);