2 * $XFree86: xc/lib/fontconfig/src/fcname.c,v 1.12 2002/08/19 19:32:05 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, FcTypeLangSet },
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);
304 v.u.l = FcNameParseLangSet (string);
312 static const FcChar8 *
313 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
325 else if (strchr (delim, c))
338 FcNameParse (const FcChar8 *name)
347 const FcObjectType *t;
350 save = malloc (strlen ((char *) name) + 1);
353 pat = FcPatternCreate ();
359 name = FcNameFindNext (name, "-,:", save, &delim);
362 if (!FcPatternAddString (pat, FC_FAMILY, save))
372 name = FcNameFindNext (name, "-,:", save, &delim);
373 d = strtod ((char *) save, (char **) &e);
376 if (!FcPatternAddDouble (pat, FC_SIZE, d))
385 name = FcNameFindNext (name, "=_:", save, &delim);
388 if (delim == '=' || delim == '_')
390 t = FcNameGetObjectType ((char *) save);
393 name = FcNameFindNext (name, ":,", save, &delim);
396 v = FcNameConvert (t->type, save, &m);
397 if (!FcPatternAdd (pat, t->object, v, FcTrue))
401 FcCharSetDestroy ((FcCharSet *) v.u.c);
404 FcLangSetDestroy ((FcLangSet *) v.u.l);
413 FcCharSetDestroy ((FcCharSet *) v.u.c);
416 FcLangSetDestroy ((FcLangSet *) v.u.l);
428 if ((c = FcNameGetConstant (save)))
430 if (!FcPatternAddInteger (pat, c->object, c->value))
441 FcPatternDestroy (pat);
448 FcNameUnparseString (FcStrBuf *buf,
449 const FcChar8 *string,
450 const FcChar8 *escape)
453 while ((c = *string++))
455 if (escape && strchr ((char *) escape, (char) c))
457 if (!FcStrBufChar (buf, escape[0]))
460 if (!FcStrBufChar (buf, c))
467 FcNameUnparseValue (FcStrBuf *buf,
477 sprintf ((char *) temp, "%d", v.u.i);
478 return FcNameUnparseString (buf, temp, 0);
480 sprintf ((char *) temp, "%g", v.u.d);
481 return FcNameUnparseString (buf, temp, 0);
483 return FcNameUnparseString (buf, v.u.s, escape);
485 return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
487 sprintf ((char *) temp, "%g %g %g %g",
488 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
489 return FcNameUnparseString (buf, temp, 0);
491 return FcNameUnparseCharSet (buf, v.u.c);
493 return FcNameUnparseLangSet (buf, v.u.l);
501 FcNameUnparseValueList (FcStrBuf *buf,
507 if (!FcNameUnparseValue (buf, v->value, escape))
510 if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
516 #define FC_ESCAPE_FIXED "\\-:,"
517 #define FC_ESCAPE_VARIABLE "\\=_:,"
520 FcNameUnparse (FcPattern *pat)
523 FcChar8 buf_static[8192];
526 const FcObjectTypeList *l;
527 const FcObjectType *o;
529 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
530 e = FcPatternFindElt (pat, FC_FAMILY);
533 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
536 e = FcPatternFindElt (pat, FC_SIZE);
539 if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
541 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
544 for (l = _FcObjectTypes; l; l = l->next)
546 for (i = 0; i < l->ntypes; i++)
549 if (!strcmp (o->object, FC_FAMILY) ||
550 !strcmp (o->object, FC_SIZE) ||
551 !strcmp (o->object, FC_FILE))
554 e = FcPatternFindElt (pat, o->object);
557 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
559 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, (FcChar8 *) FC_ESCAPE_VARIABLE))
561 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
563 if (!FcNameUnparseValueList (&buf, e->values,
564 (FcChar8 *) FC_ESCAPE_VARIABLE))
569 return FcStrBufDone (&buf);
571 FcStrBufDestroy (&buf);