2 * $XFree86: xc/lib/fontconfig/src/fcname.c,v 1.15 2002/09/26 00:17:28 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 },
63 { FC_FONTVERSION, FcTypeInteger },
66 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
68 typedef struct _FcObjectTypeList FcObjectTypeList;
70 struct _FcObjectTypeList {
71 const FcObjectTypeList *next;
72 const FcObjectType *types;
76 static const FcObjectTypeList _FcBaseObjectTypesList = {
82 static const FcObjectTypeList *_FcObjectTypes = &_FcBaseObjectTypesList;
85 FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
89 l = (FcObjectTypeList *) malloc (sizeof (FcObjectTypeList));
92 FcMemAlloc (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
95 l->next = _FcObjectTypes;
101 FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
103 const FcObjectTypeList *l, **prev;
105 for (prev = &_FcObjectTypes;
107 prev = (const FcObjectTypeList **) &(l->next))
109 if (l->types == types && l->ntypes == ntypes)
112 FcMemFree (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
121 FcNameGetObjectType (const char *object)
124 const FcObjectTypeList *l;
125 const FcObjectType *t;
127 for (l = _FcObjectTypes; l; l = l->next)
129 for (i = 0; i < l->ntypes; i++)
132 if (!strcmp (object, t->object))
139 static const FcConstant _FcBaseConstants[] = {
140 { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, },
141 { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, },
142 { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, },
143 { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, },
144 { (FcChar8 *) "black", "weight", FC_WEIGHT_BLACK, },
146 { (FcChar8 *) "roman", "slant", FC_SLANT_ROMAN, },
147 { (FcChar8 *) "italic", "slant", FC_SLANT_ITALIC, },
148 { (FcChar8 *) "oblique", "slant", FC_SLANT_OBLIQUE, },
150 { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, },
151 { (FcChar8 *) "mono", "spacing", FC_MONO, },
152 { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, },
154 { (FcChar8 *) "unknown", "rgba", FC_RGBA_UNKNOWN },
155 { (FcChar8 *) "rgb", "rgba", FC_RGBA_RGB, },
156 { (FcChar8 *) "bgr", "rgba", FC_RGBA_BGR, },
157 { (FcChar8 *) "vrgb", "rgba", FC_RGBA_VRGB },
158 { (FcChar8 *) "vbgr", "rgba", FC_RGBA_VBGR },
159 { (FcChar8 *) "none", "rgba", FC_RGBA_NONE },
162 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
164 typedef struct _FcConstantList FcConstantList;
166 struct _FcConstantList {
167 const FcConstantList *next;
168 const FcConstant *consts;
172 static const FcConstantList _FcBaseConstantList = {
178 static const FcConstantList *_FcConstants = &_FcBaseConstantList;
181 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
185 l = (FcConstantList *) malloc (sizeof (FcConstantList));
188 FcMemAlloc (FC_MEM_CONSTANT, sizeof (FcConstantList));
190 l->nconsts = nconsts;
191 l->next = _FcConstants;
197 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
199 const FcConstantList *l, **prev;
201 for (prev = &_FcConstants;
203 prev = (const FcConstantList **) &(l->next))
205 if (l->consts == consts && l->nconsts == nconsts)
208 FcMemFree (FC_MEM_CONSTANT, sizeof (FcConstantList));
217 FcNameGetConstant (FcChar8 *string)
219 const FcConstantList *l;
222 for (l = _FcConstants; l; l = l->next)
224 for (i = 0; i < l->nconsts; i++)
225 if (!FcStrCmpIgnoreCase (string, l->consts[i].name))
226 return &l->consts[i];
232 FcNameConstant (FcChar8 *string, int *result)
236 if ((c = FcNameGetConstant(string)))
245 FcNameBool (FcChar8 *v, FcBool *result)
252 if (c0 == 't' || c0 == 'y' || c0 == '1')
257 if (c0 == 'f' || c0 == 'n' || c0 == '0')
282 FcNameConvert (FcType type, FcChar8 *string, FcMatrix *m)
289 if (!FcNameConstant (string, &v.u.i))
290 v.u.i = atoi ((char *) string);
296 if (!FcNameBool (string, &v.u.b))
300 v.u.d = strtod ((char *) string, 0);
304 sscanf ((char *) string, "%lg %lg %lg %lg", &m->xx, &m->xy, &m->yx, &m->yy);
307 v.u.c = FcNameParseCharSet (string);
310 v.u.l = FcNameParseLangSet (string);
318 static const FcChar8 *
319 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
331 else if (strchr (delim, c))
344 FcNameParse (const FcChar8 *name)
353 const FcObjectType *t;
357 save = malloc (strlen ((char *) name) + 1);
360 pat = FcPatternCreate ();
366 name = FcNameFindNext (name, "-,:", save, &delim);
369 if (!FcPatternAddString (pat, FC_FAMILY, save))
379 name = FcNameFindNext (name, "-,:", save, &delim);
380 d = strtod ((char *) save, (char **) &e);
383 if (!FcPatternAddDouble (pat, FC_SIZE, d))
392 name = FcNameFindNext (name, "=_:", save, &delim);
395 if (delim == '=' || delim == '_')
397 t = FcNameGetObjectType ((char *) save);
400 name = FcNameFindNext (name, ":,", save, &delim);
403 v = FcNameConvert (t->type, save, &m);
404 if (!FcPatternAdd (pat, t->object, v, FcTrue))
408 FcCharSetDestroy ((FcCharSet *) v.u.c);
411 FcLangSetDestroy ((FcLangSet *) v.u.l);
420 FcCharSetDestroy ((FcCharSet *) v.u.c);
423 FcLangSetDestroy ((FcLangSet *) v.u.l);
435 if ((c = FcNameGetConstant (save)))
437 if (!FcPatternAddInteger (pat, c->object, c->value))
448 FcPatternDestroy (pat);
455 FcNameUnparseString (FcStrBuf *buf,
456 const FcChar8 *string,
457 const FcChar8 *escape)
460 while ((c = *string++))
462 if (escape && strchr ((char *) escape, (char) c))
464 if (!FcStrBufChar (buf, escape[0]))
467 if (!FcStrBufChar (buf, c))
474 FcNameUnparseValue (FcStrBuf *buf,
484 sprintf ((char *) temp, "%d", v.u.i);
485 return FcNameUnparseString (buf, temp, 0);
487 sprintf ((char *) temp, "%g", v.u.d);
488 return FcNameUnparseString (buf, temp, 0);
490 return FcNameUnparseString (buf, v.u.s, escape);
492 return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
494 sprintf ((char *) temp, "%g %g %g %g",
495 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
496 return FcNameUnparseString (buf, temp, 0);
498 return FcNameUnparseCharSet (buf, v.u.c);
500 return FcNameUnparseLangSet (buf, v.u.l);
508 FcNameUnparseValueList (FcStrBuf *buf,
514 if (!FcNameUnparseValue (buf, v->value, escape))
517 if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
523 #define FC_ESCAPE_FIXED "\\-:,"
524 #define FC_ESCAPE_VARIABLE "\\=_:,"
527 FcNameUnparse (FcPattern *pat)
530 FcChar8 buf_static[8192];
533 const FcObjectTypeList *l;
534 const FcObjectType *o;
536 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
537 e = FcPatternFindElt (pat, FC_FAMILY);
540 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
543 e = FcPatternFindElt (pat, FC_SIZE);
546 if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
548 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
551 for (l = _FcObjectTypes; l; l = l->next)
553 for (i = 0; i < l->ntypes; i++)
556 if (!strcmp (o->object, FC_FAMILY) ||
557 !strcmp (o->object, FC_SIZE) ||
558 !strcmp (o->object, FC_FILE))
561 e = FcPatternFindElt (pat, o->object);
564 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
566 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, (FcChar8 *) FC_ESCAPE_VARIABLE))
568 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
570 if (!FcNameUnparseValueList (&buf, e->values,
571 (FcChar8 *) FC_ESCAPE_VARIABLE))
576 return FcStrBufDone (&buf);
578 FcStrBufDestroy (&buf);