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_FAMILYLANG, FcTypeString, },
34 { FC_STYLE, FcTypeString, },
35 { FC_STYLELANG, FcTypeString, },
36 { FC_FULLNAME, FcTypeString, },
37 { FC_FULLNAMELANG, FcTypeString, },
38 { FC_SLANT, FcTypeInteger, },
39 { FC_WEIGHT, FcTypeInteger, },
40 { FC_WIDTH, FcTypeInteger, },
41 { FC_SIZE, FcTypeDouble, },
42 { FC_ASPECT, FcTypeDouble, },
43 { FC_PIXEL_SIZE, FcTypeDouble, },
44 { FC_SPACING, FcTypeInteger, },
45 { FC_FOUNDRY, FcTypeString, },
46 /* { FC_CORE, FcTypeBool, }, */
47 { FC_ANTIALIAS, FcTypeBool, },
48 { FC_HINT_STYLE, FcTypeInteger, },
49 { FC_HINTING, FcTypeBool, },
50 { FC_VERTICAL_LAYOUT, FcTypeBool, },
51 { FC_AUTOHINT, FcTypeBool, },
52 { FC_GLOBAL_ADVANCE, FcTypeBool, },
53 /* { FC_XLFD, FcTypeString, }, */
54 { FC_FILE, FcTypeString, },
55 { FC_INDEX, FcTypeInteger, },
56 { FC_RASTERIZER, FcTypeString, },
57 { FC_OUTLINE, FcTypeBool, },
58 { FC_SCALABLE, FcTypeBool, },
59 { FC_DPI, FcTypeDouble },
60 { FC_RGBA, FcTypeInteger, },
61 { FC_SCALE, FcTypeDouble, },
62 /* { FC_RENDER, FcTypeBool, },*/
63 { FC_MINSPACE, FcTypeBool, },
64 { FC_CHAR_WIDTH, FcTypeInteger },
65 { FC_CHAR_HEIGHT, FcTypeInteger },
66 { FC_MATRIX, FcTypeMatrix },
67 { FC_CHARSET, FcTypeCharSet },
68 { FC_LANG, FcTypeLangSet },
69 { FC_FONTVERSION, FcTypeInteger },
72 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
74 typedef struct _FcObjectTypeList FcObjectTypeList;
76 struct _FcObjectTypeList {
77 const FcObjectTypeList *next;
78 const FcObjectType *types;
82 static const FcObjectTypeList _FcBaseObjectTypesList = {
88 static const FcObjectTypeList *_FcObjectTypes = &_FcBaseObjectTypesList;
91 FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
95 l = (FcObjectTypeList *) malloc (sizeof (FcObjectTypeList));
98 FcMemAlloc (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
101 l->next = _FcObjectTypes;
107 FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
109 const FcObjectTypeList *l, **prev;
111 for (prev = &_FcObjectTypes;
113 prev = (const FcObjectTypeList **) &(l->next))
115 if (l->types == types && l->ntypes == ntypes)
118 FcMemFree (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
127 FcNameGetObjectType (const char *object)
130 const FcObjectTypeList *l;
131 const FcObjectType *t;
133 for (l = _FcObjectTypes; l; l = l->next)
135 for (i = 0; i < l->ntypes; i++)
138 if (!strcmp (object, t->object))
145 static const FcConstant _FcBaseConstants[] = {
146 { (FcChar8 *) "thin", "weight", FC_WEIGHT_THIN, },
147 { (FcChar8 *) "extralight", "weight", FC_WEIGHT_EXTRALIGHT, },
148 { (FcChar8 *) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT, },
149 { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, },
150 { (FcChar8 *) "book", "weight", FC_WEIGHT_BOOK, },
151 { (FcChar8 *) "regular", "weight", FC_WEIGHT_REGULAR, },
152 { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, },
153 { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, },
154 { (FcChar8 *) "semibold", "weight", FC_WEIGHT_DEMIBOLD, },
155 { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, },
156 { (FcChar8 *) "extrabold", "weight", FC_WEIGHT_EXTRABOLD, },
157 { (FcChar8 *) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD, },
158 { (FcChar8 *) "black", "weight", FC_WEIGHT_BLACK, },
160 { (FcChar8 *) "roman", "slant", FC_SLANT_ROMAN, },
161 { (FcChar8 *) "italic", "slant", FC_SLANT_ITALIC, },
162 { (FcChar8 *) "oblique", "slant", FC_SLANT_OBLIQUE, },
164 { (FcChar8 *) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED },
165 { (FcChar8 *) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED },
166 { (FcChar8 *) "condensed", "width", FC_WIDTH_CONDENSED },
167 { (FcChar8 *) "semicondensed", "width", FC_WIDTH_SEMICONDENSED },
168 { (FcChar8 *) "normal", "width", FC_WIDTH_NORMAL },
169 { (FcChar8 *) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED },
170 { (FcChar8 *) "expanded", "width", FC_WIDTH_EXPANDED },
171 { (FcChar8 *) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED },
172 { (FcChar8 *) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED },
174 { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, },
175 { (FcChar8 *) "dual", "spacing", FC_DUAL, },
176 { (FcChar8 *) "mono", "spacing", FC_MONO, },
177 { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, },
179 { (FcChar8 *) "unknown", "rgba", FC_RGBA_UNKNOWN },
180 { (FcChar8 *) "rgb", "rgba", FC_RGBA_RGB, },
181 { (FcChar8 *) "bgr", "rgba", FC_RGBA_BGR, },
182 { (FcChar8 *) "vrgb", "rgba", FC_RGBA_VRGB },
183 { (FcChar8 *) "vbgr", "rgba", FC_RGBA_VBGR },
184 { (FcChar8 *) "none", "rgba", FC_RGBA_NONE },
186 { (FcChar8 *) "hintnone", "hintstyle", FC_HINT_NONE },
187 { (FcChar8 *) "hintslight", "hintstyle", FC_HINT_SLIGHT },
188 { (FcChar8 *) "hintmedium", "hintstyle", FC_HINT_MEDIUM },
189 { (FcChar8 *) "hintfull", "hintstyle", FC_HINT_FULL },
192 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
194 typedef struct _FcConstantList FcConstantList;
196 struct _FcConstantList {
197 const FcConstantList *next;
198 const FcConstant *consts;
202 static const FcConstantList _FcBaseConstantList = {
208 static const FcConstantList *_FcConstants = &_FcBaseConstantList;
211 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
215 l = (FcConstantList *) malloc (sizeof (FcConstantList));
218 FcMemAlloc (FC_MEM_CONSTANT, sizeof (FcConstantList));
220 l->nconsts = nconsts;
221 l->next = _FcConstants;
227 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
229 const FcConstantList *l, **prev;
231 for (prev = &_FcConstants;
233 prev = (const FcConstantList **) &(l->next))
235 if (l->consts == consts && l->nconsts == nconsts)
238 FcMemFree (FC_MEM_CONSTANT, sizeof (FcConstantList));
247 FcNameGetConstant (FcChar8 *string)
249 const FcConstantList *l;
252 for (l = _FcConstants; l; l = l->next)
254 for (i = 0; i < l->nconsts; i++)
255 if (!FcStrCmpIgnoreCase (string, l->consts[i].name))
256 return &l->consts[i];
262 FcNameConstant (FcChar8 *string, int *result)
266 if ((c = FcNameGetConstant(string)))
275 FcNameBool (FcChar8 *v, FcBool *result)
281 if (c0 == 't' || c0 == 'y' || c0 == '1')
286 if (c0 == 'f' || c0 == 'n' || c0 == '0')
310 FcNameConvert (FcType type, FcChar8 *string, FcMatrix *m)
317 if (!FcNameConstant (string, &v.u.i))
318 v.u.i = atoi ((char *) string);
324 if (!FcNameBool (string, &v.u.b))
328 v.u.d = strtod ((char *) string, 0);
332 sscanf ((char *) string, "%lg %lg %lg %lg", &m->xx, &m->xy, &m->yx, &m->yy);
335 v.u.c = FcNameParseCharSet (string);
338 v.u.l = FcNameParseLangSet (string);
346 static const FcChar8 *
347 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
359 else if (strchr (delim, c))
372 FcNameParse (const FcChar8 *name)
381 const FcObjectType *t;
385 save = malloc (strlen ((char *) name) + 1);
388 pat = FcPatternCreate ();
394 name = FcNameFindNext (name, "-,:", save, &delim);
397 if (!FcPatternAddString (pat, FC_FAMILY, save))
407 name = FcNameFindNext (name, "-,:", save, &delim);
408 d = strtod ((char *) save, (char **) &e);
411 if (!FcPatternAddDouble (pat, FC_SIZE, d))
420 name = FcNameFindNext (name, "=_:", save, &delim);
423 if (delim == '=' || delim == '_')
425 t = FcNameGetObjectType ((char *) save);
428 name = FcNameFindNext (name, ":,", save, &delim);
431 v = FcNameConvert (t->type, save, &m);
432 if (!FcPatternAdd (pat, t->object, v, FcTrue))
436 FcCharSetDestroy ((FcCharSet *) v.u.c);
439 FcLangSetDestroy ((FcLangSet *) v.u.l);
448 FcCharSetDestroy ((FcCharSet *) v.u.c);
451 FcLangSetDestroy ((FcLangSet *) v.u.l);
463 if ((c = FcNameGetConstant (save)))
465 if (!FcPatternAddInteger (pat, c->object, c->value))
476 FcPatternDestroy (pat);
483 FcNameUnparseString (FcStrBuf *buf,
484 const FcChar8 *string,
485 const FcChar8 *escape)
488 while ((c = *string++))
490 if (escape && strchr ((char *) escape, (char) c))
492 if (!FcStrBufChar (buf, escape[0]))
495 if (!FcStrBufChar (buf, c))
502 FcNameUnparseValue (FcStrBuf *buf,
512 sprintf ((char *) temp, "%d", v.u.i);
513 return FcNameUnparseString (buf, temp, 0);
515 sprintf ((char *) temp, "%g", v.u.d);
516 return FcNameUnparseString (buf, temp, 0);
518 return FcNameUnparseString (buf, v.u.s, escape);
520 return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
522 sprintf ((char *) temp, "%g %g %g %g",
523 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
524 return FcNameUnparseString (buf, temp, 0);
526 return FcNameUnparseCharSet (buf, v.u.c);
528 return FcNameUnparseLangSet (buf, v.u.l);
536 FcNameUnparseValueList (FcStrBuf *buf,
542 if (!FcNameUnparseValue (buf, v->value, escape))
545 if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
551 #define FC_ESCAPE_FIXED "\\-:,"
552 #define FC_ESCAPE_VARIABLE "\\=_:,"
555 FcNameUnparse (FcPattern *pat)
558 FcChar8 buf_static[8192];
561 const FcObjectTypeList *l;
562 const FcObjectType *o;
564 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
565 e = FcPatternFindElt (pat, FC_FAMILY);
568 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
571 e = FcPatternFindElt (pat, FC_SIZE);
574 if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
576 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
579 for (l = _FcObjectTypes; l; l = l->next)
581 for (i = 0; i < l->ntypes; i++)
584 if (!strcmp (o->object, FC_FAMILY) ||
585 !strcmp (o->object, FC_SIZE) ||
586 !strcmp (o->object, FC_FILE))
589 e = FcPatternFindElt (pat, o->object);
592 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
594 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, (FcChar8 *) FC_ESCAPE_VARIABLE))
596 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
598 if (!FcNameUnparseValueList (&buf, e->values,
599 (FcChar8 *) FC_ESCAPE_VARIABLE))
604 return FcStrBufDone (&buf);
606 FcStrBufDestroy (&buf);