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_STYLE, FcTypeString, },
34 { FC_SLANT, FcTypeInteger, },
35 { FC_WEIGHT, FcTypeInteger, },
36 { FC_WIDTH, FcTypeInteger, },
37 { FC_SIZE, FcTypeDouble, },
38 { FC_ASPECT, FcTypeDouble, },
39 { FC_PIXEL_SIZE, FcTypeDouble, },
40 { FC_SPACING, FcTypeInteger, },
41 { FC_FOUNDRY, FcTypeString, },
42 /* { FC_CORE, FcTypeBool, }, */
43 { FC_ANTIALIAS, FcTypeBool, },
44 { FC_HINTING, FcTypeBool, },
45 { FC_VERTICAL_LAYOUT, FcTypeBool, },
46 { FC_AUTOHINT, FcTypeBool, },
47 { FC_GLOBAL_ADVANCE, FcTypeBool, },
48 /* { FC_XLFD, FcTypeString, }, */
49 { FC_FILE, FcTypeString, },
50 { FC_INDEX, FcTypeInteger, },
51 { FC_RASTERIZER, FcTypeString, },
52 { FC_OUTLINE, FcTypeBool, },
53 { FC_SCALABLE, FcTypeBool, },
54 { FC_DPI, FcTypeDouble },
55 { FC_RGBA, FcTypeInteger, },
56 { FC_SCALE, FcTypeDouble, },
57 /* { FC_RENDER, FcTypeBool, },*/
58 { FC_MINSPACE, FcTypeBool, },
59 { FC_CHAR_WIDTH, FcTypeInteger },
60 { FC_CHAR_HEIGHT, FcTypeInteger },
61 { FC_MATRIX, FcTypeMatrix },
62 { FC_CHARSET, FcTypeCharSet },
63 { FC_LANG, FcTypeLangSet },
64 { FC_FONTVERSION, FcTypeInteger },
67 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
69 typedef struct _FcObjectTypeList FcObjectTypeList;
71 struct _FcObjectTypeList {
72 const FcObjectTypeList *next;
73 const FcObjectType *types;
77 static const FcObjectTypeList _FcBaseObjectTypesList = {
83 static const FcObjectTypeList *_FcObjectTypes = &_FcBaseObjectTypesList;
86 FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
90 l = (FcObjectTypeList *) malloc (sizeof (FcObjectTypeList));
93 FcMemAlloc (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
96 l->next = _FcObjectTypes;
102 FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
104 const FcObjectTypeList *l, **prev;
106 for (prev = &_FcObjectTypes;
108 prev = (const FcObjectTypeList **) &(l->next))
110 if (l->types == types && l->ntypes == ntypes)
113 FcMemFree (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
122 FcNameGetObjectType (const char *object)
125 const FcObjectTypeList *l;
126 const FcObjectType *t;
128 for (l = _FcObjectTypes; l; l = l->next)
130 for (i = 0; i < l->ntypes; i++)
133 if (!strcmp (object, t->object))
140 static const FcConstant _FcBaseConstants[] = {
141 { (FcChar8 *) "thin", "weight", FC_WEIGHT_THIN, },
142 { (FcChar8 *) "extralight", "weight", FC_WEIGHT_EXTRALIGHT, },
143 { (FcChar8 *) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT, },
144 { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, },
145 { (FcChar8 *) "book", "weight", FC_WEIGHT_BOOK, },
146 { (FcChar8 *) "regular", "weight", FC_WEIGHT_REGULAR, },
147 { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, },
148 { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, },
149 { (FcChar8 *) "semibold", "weight", FC_WEIGHT_DEMIBOLD, },
150 { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, },
151 { (FcChar8 *) "extrabold", "weight", FC_WEIGHT_EXTRABOLD, },
152 { (FcChar8 *) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD, },
153 { (FcChar8 *) "black", "weight", FC_WEIGHT_BLACK, },
155 { (FcChar8 *) "roman", "slant", FC_SLANT_ROMAN, },
156 { (FcChar8 *) "italic", "slant", FC_SLANT_ITALIC, },
157 { (FcChar8 *) "oblique", "slant", FC_SLANT_OBLIQUE, },
159 { (FcChar8 *) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED },
160 { (FcChar8 *) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED },
161 { (FcChar8 *) "condensed", "width", FC_WIDTH_CONDENSED },
162 { (FcChar8 *) "semicondensed", "width", FC_WIDTH_SEMICONDENSED },
163 { (FcChar8 *) "normal", "width", FC_WIDTH_NORMAL },
164 { (FcChar8 *) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED },
165 { (FcChar8 *) "expanded", "width", FC_WIDTH_EXPANDED },
166 { (FcChar8 *) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED },
167 { (FcChar8 *) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED },
169 { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, },
170 { (FcChar8 *) "mono", "spacing", FC_MONO, },
171 { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, },
173 { (FcChar8 *) "unknown", "rgba", FC_RGBA_UNKNOWN },
174 { (FcChar8 *) "rgb", "rgba", FC_RGBA_RGB, },
175 { (FcChar8 *) "bgr", "rgba", FC_RGBA_BGR, },
176 { (FcChar8 *) "vrgb", "rgba", FC_RGBA_VRGB },
177 { (FcChar8 *) "vbgr", "rgba", FC_RGBA_VBGR },
178 { (FcChar8 *) "none", "rgba", FC_RGBA_NONE },
181 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
183 typedef struct _FcConstantList FcConstantList;
185 struct _FcConstantList {
186 const FcConstantList *next;
187 const FcConstant *consts;
191 static const FcConstantList _FcBaseConstantList = {
197 static const FcConstantList *_FcConstants = &_FcBaseConstantList;
200 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
204 l = (FcConstantList *) malloc (sizeof (FcConstantList));
207 FcMemAlloc (FC_MEM_CONSTANT, sizeof (FcConstantList));
209 l->nconsts = nconsts;
210 l->next = _FcConstants;
216 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
218 const FcConstantList *l, **prev;
220 for (prev = &_FcConstants;
222 prev = (const FcConstantList **) &(l->next))
224 if (l->consts == consts && l->nconsts == nconsts)
227 FcMemFree (FC_MEM_CONSTANT, sizeof (FcConstantList));
236 FcNameGetConstant (FcChar8 *string)
238 const FcConstantList *l;
241 for (l = _FcConstants; l; l = l->next)
243 for (i = 0; i < l->nconsts; i++)
244 if (!FcStrCmpIgnoreCase (string, l->consts[i].name))
245 return &l->consts[i];
251 FcNameConstant (FcChar8 *string, int *result)
255 if ((c = FcNameGetConstant(string)))
264 FcNameBool (FcChar8 *v, FcBool *result)
270 if (c0 == 't' || c0 == 'y' || c0 == '1')
275 if (c0 == 'f' || c0 == 'n' || c0 == '0')
299 FcNameConvert (FcType type, FcChar8 *string, FcMatrix *m)
306 if (!FcNameConstant (string, &v.u.i))
307 v.u.i = atoi ((char *) string);
313 if (!FcNameBool (string, &v.u.b))
317 v.u.d = strtod ((char *) string, 0);
321 sscanf ((char *) string, "%lg %lg %lg %lg", &m->xx, &m->xy, &m->yx, &m->yy);
324 v.u.c = FcNameParseCharSet (string);
327 v.u.l = FcNameParseLangSet (string);
335 static const FcChar8 *
336 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
348 else if (strchr (delim, c))
361 FcNameParse (const FcChar8 *name)
370 const FcObjectType *t;
374 save = malloc (strlen ((char *) name) + 1);
377 pat = FcPatternCreate ();
383 name = FcNameFindNext (name, "-,:", save, &delim);
386 if (!FcPatternAddString (pat, FC_FAMILY, save))
396 name = FcNameFindNext (name, "-,:", save, &delim);
397 d = strtod ((char *) save, (char **) &e);
400 if (!FcPatternAddDouble (pat, FC_SIZE, d))
409 name = FcNameFindNext (name, "=_:", save, &delim);
412 if (delim == '=' || delim == '_')
414 t = FcNameGetObjectType ((char *) save);
417 name = FcNameFindNext (name, ":,", save, &delim);
420 v = FcNameConvert (t->type, save, &m);
421 if (!FcPatternAdd (pat, t->object, v, FcTrue))
425 FcCharSetDestroy ((FcCharSet *) v.u.c);
428 FcLangSetDestroy ((FcLangSet *) v.u.l);
437 FcCharSetDestroy ((FcCharSet *) v.u.c);
440 FcLangSetDestroy ((FcLangSet *) v.u.l);
452 if ((c = FcNameGetConstant (save)))
454 if (!FcPatternAddInteger (pat, c->object, c->value))
465 FcPatternDestroy (pat);
472 FcNameUnparseString (FcStrBuf *buf,
473 const FcChar8 *string,
474 const FcChar8 *escape)
477 while ((c = *string++))
479 if (escape && strchr ((char *) escape, (char) c))
481 if (!FcStrBufChar (buf, escape[0]))
484 if (!FcStrBufChar (buf, c))
491 FcNameUnparseValue (FcStrBuf *buf,
501 sprintf ((char *) temp, "%d", v.u.i);
502 return FcNameUnparseString (buf, temp, 0);
504 sprintf ((char *) temp, "%g", v.u.d);
505 return FcNameUnparseString (buf, temp, 0);
507 return FcNameUnparseString (buf, v.u.s, escape);
509 return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
511 sprintf ((char *) temp, "%g %g %g %g",
512 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
513 return FcNameUnparseString (buf, temp, 0);
515 return FcNameUnparseCharSet (buf, v.u.c);
517 return FcNameUnparseLangSet (buf, v.u.l);
525 FcNameUnparseValueList (FcStrBuf *buf,
531 if (!FcNameUnparseValue (buf, v->value, escape))
534 if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
540 #define FC_ESCAPE_FIXED "\\-:,"
541 #define FC_ESCAPE_VARIABLE "\\=_:,"
544 FcNameUnparse (FcPattern *pat)
547 FcChar8 buf_static[8192];
550 const FcObjectTypeList *l;
551 const FcObjectType *o;
553 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
554 e = FcPatternFindElt (pat, FC_FAMILY);
557 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
560 e = FcPatternFindElt (pat, FC_SIZE);
563 if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
565 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
568 for (l = _FcObjectTypes; l; l = l->next)
570 for (i = 0; i < l->ntypes; i++)
573 if (!strcmp (o->object, FC_FAMILY) ||
574 !strcmp (o->object, FC_SIZE) ||
575 !strcmp (o->object, FC_FILE))
578 e = FcPatternFindElt (pat, o->object);
581 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
583 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, (FcChar8 *) FC_ESCAPE_VARIABLE))
585 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
587 if (!FcNameUnparseValueList (&buf, e->values,
588 (FcChar8 *) FC_ESCAPE_VARIABLE))
593 return FcStrBufDone (&buf);
595 FcStrBufDestroy (&buf);