2 * $XFree86: xc/lib/fontconfig/src/fcname.c,v 1.9 2002/06/26 22:14:08 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_HINTING, FcTypeBool, },
44 { FC_VERTICAL_LAYOUT, FcTypeBool, },
45 { FC_AUTOHINT, FcTypeBool, },
46 { FC_GLOBAL_ADVANCE, FcTypeBool, },
47 /* { FC_XLFD, FcTypeString, }, */
48 { FC_FILE, FcTypeString, },
49 { FC_INDEX, FcTypeInteger, },
50 { FC_RASTERIZER, FcTypeString, },
51 { FC_OUTLINE, FcTypeBool, },
52 { FC_SCALABLE, FcTypeBool, },
53 { FC_DPI, FcTypeDouble },
54 { FC_RGBA, FcTypeInteger, },
55 { FC_SCALE, FcTypeDouble, },
56 /* { FC_RENDER, FcTypeBool, },*/
57 { FC_MINSPACE, FcTypeBool, },
58 { FC_CHAR_WIDTH, FcTypeInteger },
59 { FC_CHAR_HEIGHT, FcTypeInteger },
60 { FC_MATRIX, FcTypeMatrix },
61 { FC_CHARSET, FcTypeCharSet },
62 { FC_LANG, FcTypeString },
65 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
67 typedef struct _FcObjectTypeList FcObjectTypeList;
69 struct _FcObjectTypeList {
70 const FcObjectTypeList *next;
71 const FcObjectType *types;
75 static const FcObjectTypeList _FcBaseObjectTypesList = {
81 static const FcObjectTypeList *_FcObjectTypes = &_FcBaseObjectTypesList;
84 FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
88 l = (FcObjectTypeList *) malloc (sizeof (FcObjectTypeList));
93 l->next = _FcObjectTypes;
99 FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
101 const FcObjectTypeList *l, **prev;
103 for (prev = &_FcObjectTypes;
105 prev = (const FcObjectTypeList **) &(l->next))
107 if (l->types == types && l->ntypes == ntypes)
118 FcNameGetObjectType (const char *object)
121 const FcObjectTypeList *l;
122 const FcObjectType *t;
124 for (l = _FcObjectTypes; l; l = l->next)
126 for (i = 0; i < l->ntypes; i++)
129 if (!strcmp (object, t->object))
136 static const FcConstant _FcBaseConstants[] = {
137 { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, },
138 { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, },
139 { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, },
140 { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, },
141 { (FcChar8 *) "black", "weight", FC_WEIGHT_BLACK, },
143 { (FcChar8 *) "roman", "slant", FC_SLANT_ROMAN, },
144 { (FcChar8 *) "italic", "slant", FC_SLANT_ITALIC, },
145 { (FcChar8 *) "oblique", "slant", FC_SLANT_OBLIQUE, },
147 { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, },
148 { (FcChar8 *) "mono", "spacing", FC_MONO, },
149 { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, },
151 { (FcChar8 *) "none", "rgba", FC_RGBA_NONE },
152 { (FcChar8 *) "rgb", "rgba", FC_RGBA_RGB, },
153 { (FcChar8 *) "bgr", "rgba", FC_RGBA_BGR, },
154 { (FcChar8 *) "vrgb", "rgba", FC_RGBA_VRGB },
155 { (FcChar8 *) "vbgr", "rgba", FC_RGBA_VBGR },
158 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
160 typedef struct _FcConstantList FcConstantList;
162 struct _FcConstantList {
163 const FcConstantList *next;
164 const FcConstant *consts;
168 static const FcConstantList _FcBaseConstantList = {
174 static const FcConstantList *_FcConstants = &_FcBaseConstantList;
177 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
181 l = (FcConstantList *) malloc (sizeof (FcConstantList));
185 l->nconsts = nconsts;
186 l->next = _FcConstants;
192 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
194 const FcConstantList *l, **prev;
196 for (prev = &_FcConstants;
198 prev = (const FcConstantList **) &(l->next))
200 if (l->consts == consts && l->nconsts == nconsts)
211 FcNameGetConstant (FcChar8 *string)
213 const FcConstantList *l;
216 for (l = _FcConstants; l; l = l->next)
218 for (i = 0; i < l->nconsts; i++)
219 if (!FcStrCmpIgnoreCase (string, l->consts[i].name))
220 return &l->consts[i];
226 FcNameConstant (FcChar8 *string, int *result)
230 if ((c = FcNameGetConstant(string)))
239 FcNameBool (FcChar8 *v, FcBool *result)
246 if (c0 == 't' || c0 == 'y' || c0 == '1')
251 if (c0 == 'f' || c0 == 'n' || c0 == '0')
276 FcNameConvert (FcType type, FcChar8 *string, FcMatrix *m)
283 if (!FcNameConstant (string, &v.u.i))
284 v.u.i = atoi ((char *) string);
290 if (!FcNameBool (string, &v.u.b))
294 v.u.d = strtod ((char *) string, 0);
298 sscanf ((char *) string, "%lg %lg %lg %lg", &m->xx, &m->xy, &m->yx, &m->yy);
301 v.u.c = FcNameParseCharSet (string);
309 static const FcChar8 *
310 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
322 else if (strchr (delim, c))
335 FcNameParse (const FcChar8 *name)
344 const FcObjectType *t;
347 save = malloc (strlen ((char *) name) + 1);
350 pat = FcPatternCreate ();
356 name = FcNameFindNext (name, "-,:", save, &delim);
359 if (!FcPatternAddString (pat, FC_FAMILY, save))
369 name = FcNameFindNext (name, "-,:", save, &delim);
370 d = strtod ((char *) save, (char **) &e);
373 if (!FcPatternAddDouble (pat, FC_SIZE, d))
382 name = FcNameFindNext (name, "=_:", save, &delim);
385 if (delim == '=' || delim == '_')
387 t = FcNameGetObjectType ((char *) save);
390 name = FcNameFindNext (name, ":,", save, &delim);
393 v = FcNameConvert (t->type, save, &m);
394 if (!FcPatternAdd (pat, t->object, v, FcTrue))
396 if (v.type == FcTypeCharSet)
397 FcCharSetDestroy ((FcCharSet *) v.u.c);
400 if (v.type == FcTypeCharSet)
401 FcCharSetDestroy ((FcCharSet *) v.u.c);
409 if ((c = FcNameGetConstant (save)))
411 if (!FcPatternAddInteger (pat, c->object, c->value))
422 FcPatternDestroy (pat);
429 FcNameUnparseString (FcStrBuf *buf,
430 const FcChar8 *string,
431 const FcChar8 *escape)
434 while ((c = *string++))
436 if (escape && strchr ((char *) escape, (char) c))
438 if (!FcStrBufChar (buf, escape[0]))
441 if (!FcStrBufChar (buf, c))
448 FcNameUnparseValue (FcStrBuf *buf,
458 sprintf ((char *) temp, "%d", v.u.i);
459 return FcNameUnparseString (buf, temp, 0);
461 sprintf ((char *) temp, "%g", v.u.d);
462 return FcNameUnparseString (buf, temp, 0);
464 return FcNameUnparseString (buf, v.u.s, escape);
466 return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
468 sprintf ((char *) temp, "%g %g %g %g",
469 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
470 return FcNameUnparseString (buf, temp, 0);
472 return FcNameUnparseCharSet (buf, v.u.c);
480 FcNameUnparseValueList (FcStrBuf *buf,
486 if (!FcNameUnparseValue (buf, v->value, escape))
489 if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
495 #define FC_ESCAPE_FIXED "\\-:,"
496 #define FC_ESCAPE_VARIABLE "\\=_:,"
499 FcNameUnparse (FcPattern *pat)
502 FcChar8 buf_static[8192];
505 const FcObjectTypeList *l;
506 const FcObjectType *o;
508 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
509 e = FcPatternFindElt (pat, FC_FAMILY);
512 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
515 e = FcPatternFindElt (pat, FC_SIZE);
518 if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
520 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
523 for (l = _FcObjectTypes; l; l = l->next)
525 for (i = 0; i < l->ntypes; i++)
528 if (!strcmp (o->object, FC_FAMILY) ||
529 !strcmp (o->object, FC_SIZE) ||
530 !strcmp (o->object, FC_FILE))
533 e = FcPatternFindElt (pat, o->object);
536 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
538 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, (FcChar8 *) FC_ESCAPE_VARIABLE))
540 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
542 if (!FcNameUnparseValueList (&buf, e->values,
543 (FcChar8 *) FC_ESCAPE_VARIABLE))
548 return FcStrBufDone (&buf);
550 FcStrBufDestroy (&buf);