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 },
70 { FC_CAPABILITY, FcTypeString },
73 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
75 typedef struct _FcObjectTypeList FcObjectTypeList;
77 struct _FcObjectTypeList {
78 const FcObjectTypeList *next;
79 const FcObjectType *types;
83 static const FcObjectTypeList _FcBaseObjectTypesList = {
89 static const FcObjectTypeList *_FcObjectTypes = &_FcBaseObjectTypesList;
92 FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
96 l = (FcObjectTypeList *) malloc (sizeof (FcObjectTypeList));
99 FcMemAlloc (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
102 l->next = _FcObjectTypes;
108 FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
110 const FcObjectTypeList *l, **prev;
112 for (prev = &_FcObjectTypes;
114 prev = (const FcObjectTypeList **) &(l->next))
116 if (l->types == types && l->ntypes == ntypes)
119 FcMemFree (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
128 FcNameGetObjectType (const char *object)
131 const FcObjectTypeList *l;
132 const FcObjectType *t;
134 for (l = _FcObjectTypes; l; l = l->next)
136 for (i = 0; i < l->ntypes; i++)
139 if (!strcmp (object, t->object))
146 static const FcConstant _FcBaseConstants[] = {
147 { (FcChar8 *) "thin", "weight", FC_WEIGHT_THIN, },
148 { (FcChar8 *) "extralight", "weight", FC_WEIGHT_EXTRALIGHT, },
149 { (FcChar8 *) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT, },
150 { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, },
151 { (FcChar8 *) "book", "weight", FC_WEIGHT_BOOK, },
152 { (FcChar8 *) "regular", "weight", FC_WEIGHT_REGULAR, },
153 { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, },
154 { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, },
155 { (FcChar8 *) "semibold", "weight", FC_WEIGHT_DEMIBOLD, },
156 { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, },
157 { (FcChar8 *) "extrabold", "weight", FC_WEIGHT_EXTRABOLD, },
158 { (FcChar8 *) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD, },
159 { (FcChar8 *) "black", "weight", FC_WEIGHT_BLACK, },
161 { (FcChar8 *) "roman", "slant", FC_SLANT_ROMAN, },
162 { (FcChar8 *) "italic", "slant", FC_SLANT_ITALIC, },
163 { (FcChar8 *) "oblique", "slant", FC_SLANT_OBLIQUE, },
165 { (FcChar8 *) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED },
166 { (FcChar8 *) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED },
167 { (FcChar8 *) "condensed", "width", FC_WIDTH_CONDENSED },
168 { (FcChar8 *) "semicondensed", "width", FC_WIDTH_SEMICONDENSED },
169 { (FcChar8 *) "normal", "width", FC_WIDTH_NORMAL },
170 { (FcChar8 *) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED },
171 { (FcChar8 *) "expanded", "width", FC_WIDTH_EXPANDED },
172 { (FcChar8 *) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED },
173 { (FcChar8 *) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED },
175 { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, },
176 { (FcChar8 *) "dual", "spacing", FC_DUAL, },
177 { (FcChar8 *) "mono", "spacing", FC_MONO, },
178 { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, },
180 { (FcChar8 *) "unknown", "rgba", FC_RGBA_UNKNOWN },
181 { (FcChar8 *) "rgb", "rgba", FC_RGBA_RGB, },
182 { (FcChar8 *) "bgr", "rgba", FC_RGBA_BGR, },
183 { (FcChar8 *) "vrgb", "rgba", FC_RGBA_VRGB },
184 { (FcChar8 *) "vbgr", "rgba", FC_RGBA_VBGR },
185 { (FcChar8 *) "none", "rgba", FC_RGBA_NONE },
187 { (FcChar8 *) "hintnone", "hintstyle", FC_HINT_NONE },
188 { (FcChar8 *) "hintslight", "hintstyle", FC_HINT_SLIGHT },
189 { (FcChar8 *) "hintmedium", "hintstyle", FC_HINT_MEDIUM },
190 { (FcChar8 *) "hintfull", "hintstyle", FC_HINT_FULL },
193 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
195 typedef struct _FcConstantList FcConstantList;
197 struct _FcConstantList {
198 const FcConstantList *next;
199 const FcConstant *consts;
203 static const FcConstantList _FcBaseConstantList = {
209 static const FcConstantList *_FcConstants = &_FcBaseConstantList;
212 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
216 l = (FcConstantList *) malloc (sizeof (FcConstantList));
219 FcMemAlloc (FC_MEM_CONSTANT, sizeof (FcConstantList));
221 l->nconsts = nconsts;
222 l->next = _FcConstants;
228 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
230 const FcConstantList *l, **prev;
232 for (prev = &_FcConstants;
234 prev = (const FcConstantList **) &(l->next))
236 if (l->consts == consts && l->nconsts == nconsts)
239 FcMemFree (FC_MEM_CONSTANT, sizeof (FcConstantList));
248 FcNameGetConstant (FcChar8 *string)
250 const FcConstantList *l;
253 for (l = _FcConstants; l; l = l->next)
255 for (i = 0; i < l->nconsts; i++)
256 if (!FcStrCmpIgnoreCase (string, l->consts[i].name))
257 return &l->consts[i];
263 FcNameConstant (FcChar8 *string, int *result)
267 if ((c = FcNameGetConstant(string)))
276 FcNameBool (FcChar8 *v, FcBool *result)
282 if (c0 == 't' || c0 == 'y' || c0 == '1')
287 if (c0 == 'f' || c0 == 'n' || c0 == '0')
311 FcNameConvert (FcType type, FcChar8 *string, FcMatrix *m)
318 if (!FcNameConstant (string, &v.u.i))
319 v.u.i = atoi ((char *) string);
325 if (!FcNameBool (string, &v.u.b))
329 v.u.d = strtod ((char *) string, 0);
333 sscanf ((char *) string, "%lg %lg %lg %lg", &m->xx, &m->xy, &m->yx, &m->yy);
336 v.u.c = FcNameParseCharSet (string);
339 v.u.l = FcNameParseLangSet (string);
347 static const FcChar8 *
348 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
360 else if (strchr (delim, c))
373 FcNameParse (const FcChar8 *name)
382 const FcObjectType *t;
386 save = malloc (strlen ((char *) name) + 1);
389 pat = FcPatternCreate ();
395 name = FcNameFindNext (name, "-,:", save, &delim);
398 if (!FcPatternAddString (pat, FC_FAMILY, save))
408 name = FcNameFindNext (name, "-,:", save, &delim);
409 d = strtod ((char *) save, (char **) &e);
412 if (!FcPatternAddDouble (pat, FC_SIZE, d))
421 name = FcNameFindNext (name, "=_:", save, &delim);
424 if (delim == '=' || delim == '_')
426 t = FcNameGetObjectType ((char *) save);
429 name = FcNameFindNext (name, ":,", save, &delim);
432 v = FcNameConvert (t->type, save, &m);
433 if (!FcPatternAdd (pat, t->object, v, FcTrue))
437 FcCharSetDestroy ((FcCharSet *) v.u.c);
440 FcLangSetDestroy ((FcLangSet *) v.u.l);
449 FcCharSetDestroy ((FcCharSet *) v.u.c);
452 FcLangSetDestroy ((FcLangSet *) v.u.l);
464 if ((c = FcNameGetConstant (save)))
466 if (!FcPatternAddInteger (pat, c->object, c->value))
477 FcPatternDestroy (pat);
484 FcNameUnparseString (FcStrBuf *buf,
485 const FcChar8 *string,
486 const FcChar8 *escape)
489 while ((c = *string++))
491 if (escape && strchr ((char *) escape, (char) c))
493 if (!FcStrBufChar (buf, escape[0]))
496 if (!FcStrBufChar (buf, c))
503 FcNameUnparseValue (FcStrBuf *buf,
513 sprintf ((char *) temp, "%d", v.u.i);
514 return FcNameUnparseString (buf, temp, 0);
516 sprintf ((char *) temp, "%g", v.u.d);
517 return FcNameUnparseString (buf, temp, 0);
519 return FcNameUnparseString (buf, v.u.s, escape);
521 return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
523 sprintf ((char *) temp, "%g %g %g %g",
524 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
525 return FcNameUnparseString (buf, temp, 0);
527 return FcNameUnparseCharSet (buf, v.u.c);
529 return FcNameUnparseLangSet (buf, v.u.l);
537 FcNameUnparseValueList (FcStrBuf *buf,
543 if (!FcNameUnparseValue (buf, v->value, escape))
546 if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
552 #define FC_ESCAPE_FIXED "\\-:,"
553 #define FC_ESCAPE_VARIABLE "\\=_:,"
556 FcNameUnparse (FcPattern *pat)
559 FcChar8 buf_static[8192];
562 const FcObjectTypeList *l;
563 const FcObjectType *o;
565 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
566 e = FcPatternFindElt (pat, FC_FAMILY);
569 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
572 e = FcPatternFindElt (pat, FC_SIZE);
575 if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
577 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
580 for (l = _FcObjectTypes; l; l = l->next)
582 for (i = 0; i < l->ntypes; i++)
585 if (!strcmp (o->object, FC_FAMILY) ||
586 !strcmp (o->object, FC_SIZE) ||
587 !strcmp (o->object, FC_FILE))
590 e = FcPatternFindElt (pat, o->object);
593 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
595 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, (FcChar8 *) FC_ESCAPE_VARIABLE))
597 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
599 if (!FcNameUnparseValueList (&buf, e->values,
600 (FcChar8 *) FC_ESCAPE_VARIABLE))
605 return FcStrBufDone (&buf);
607 FcStrBufDestroy (&buf);