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 },
71 { FC_FONTFORMAT, FcTypeString },
72 { FC_EMBOLDEN, FcTypeBool },
75 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
77 typedef struct _FcObjectTypeList FcObjectTypeList;
79 struct _FcObjectTypeList {
80 const FcObjectTypeList *next;
81 const FcObjectType *types;
85 static const FcObjectTypeList _FcBaseObjectTypesList = {
91 static const FcObjectTypeList *_FcObjectTypes = &_FcBaseObjectTypesList;
94 FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
98 l = (FcObjectTypeList *) malloc (sizeof (FcObjectTypeList));
101 FcMemAlloc (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
104 l->next = _FcObjectTypes;
110 FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
112 const FcObjectTypeList *l, **prev;
114 for (prev = &_FcObjectTypes;
116 prev = (const FcObjectTypeList **) &(l->next))
118 if (l->types == types && l->ntypes == ntypes)
121 FcMemFree (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
130 FcNameGetObjectType (const char *object)
133 const FcObjectTypeList *l;
134 const FcObjectType *t;
136 for (l = _FcObjectTypes; l; l = l->next)
138 for (i = 0; i < l->ntypes; i++)
141 if (!strcmp (object, t->object))
148 static int objectptr_count = 1;
149 static int objectptr_alloc = 0;
150 static int * objectptr_indices = 0;
153 FcObjectNewBank(void)
157 objectptr_indices = 0;
160 // XXX todo: introduce a hashtable for faster lookup
162 FcObjectToPtr (const char * object)
165 const FcObjectTypeList *l;
166 const FcObjectType *t;
168 for (l = _FcObjectTypes; l; l = l->next)
170 for (i = 0; i < l->ntypes; i++)
173 if (!strcmp (object, t->object))
182 FcObjectPtrU (FcObjectPtr si)
184 return _FcObjectTypes->types[si].object;
188 FcObjectNeededBytes (FcObjectPtr si)
194 FcObjectDistributeBytes (FcCache * metadata, void * block_ptr)
200 FcObjectSerialize (FcObjectPtr si)
206 FcObjectUnserialize (FcCache metadata, FcConfig * config, void *block_ptr)
211 FcObjectPtrCompare (const FcObjectPtr a, const FcObjectPtr b)
216 static const FcConstant _FcBaseConstants[] = {
217 { (FcChar8 *) "thin", "weight", FC_WEIGHT_THIN, },
218 { (FcChar8 *) "extralight", "weight", FC_WEIGHT_EXTRALIGHT, },
219 { (FcChar8 *) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT, },
220 { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, },
221 { (FcChar8 *) "book", "weight", FC_WEIGHT_BOOK, },
222 { (FcChar8 *) "regular", "weight", FC_WEIGHT_REGULAR, },
223 { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, },
224 { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, },
225 { (FcChar8 *) "semibold", "weight", FC_WEIGHT_DEMIBOLD, },
226 { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, },
227 { (FcChar8 *) "extrabold", "weight", FC_WEIGHT_EXTRABOLD, },
228 { (FcChar8 *) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD, },
229 { (FcChar8 *) "black", "weight", FC_WEIGHT_BLACK, },
231 { (FcChar8 *) "roman", "slant", FC_SLANT_ROMAN, },
232 { (FcChar8 *) "italic", "slant", FC_SLANT_ITALIC, },
233 { (FcChar8 *) "oblique", "slant", FC_SLANT_OBLIQUE, },
235 { (FcChar8 *) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED },
236 { (FcChar8 *) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED },
237 { (FcChar8 *) "condensed", "width", FC_WIDTH_CONDENSED },
238 { (FcChar8 *) "semicondensed", "width", FC_WIDTH_SEMICONDENSED },
239 { (FcChar8 *) "normal", "width", FC_WIDTH_NORMAL },
240 { (FcChar8 *) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED },
241 { (FcChar8 *) "expanded", "width", FC_WIDTH_EXPANDED },
242 { (FcChar8 *) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED },
243 { (FcChar8 *) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED },
245 { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, },
246 { (FcChar8 *) "dual", "spacing", FC_DUAL, },
247 { (FcChar8 *) "mono", "spacing", FC_MONO, },
248 { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, },
250 { (FcChar8 *) "unknown", "rgba", FC_RGBA_UNKNOWN },
251 { (FcChar8 *) "rgb", "rgba", FC_RGBA_RGB, },
252 { (FcChar8 *) "bgr", "rgba", FC_RGBA_BGR, },
253 { (FcChar8 *) "vrgb", "rgba", FC_RGBA_VRGB },
254 { (FcChar8 *) "vbgr", "rgba", FC_RGBA_VBGR },
255 { (FcChar8 *) "none", "rgba", FC_RGBA_NONE },
257 { (FcChar8 *) "hintnone", "hintstyle", FC_HINT_NONE },
258 { (FcChar8 *) "hintslight", "hintstyle", FC_HINT_SLIGHT },
259 { (FcChar8 *) "hintmedium", "hintstyle", FC_HINT_MEDIUM },
260 { (FcChar8 *) "hintfull", "hintstyle", FC_HINT_FULL },
263 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
265 typedef struct _FcConstantList FcConstantList;
267 struct _FcConstantList {
268 const FcConstantList *next;
269 const FcConstant *consts;
273 static const FcConstantList _FcBaseConstantList = {
279 static const FcConstantList *_FcConstants = &_FcBaseConstantList;
282 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
286 l = (FcConstantList *) malloc (sizeof (FcConstantList));
289 FcMemAlloc (FC_MEM_CONSTANT, sizeof (FcConstantList));
291 l->nconsts = nconsts;
292 l->next = _FcConstants;
298 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
300 const FcConstantList *l, **prev;
302 for (prev = &_FcConstants;
304 prev = (const FcConstantList **) &(l->next))
306 if (l->consts == consts && l->nconsts == nconsts)
309 FcMemFree (FC_MEM_CONSTANT, sizeof (FcConstantList));
318 FcNameGetConstant (FcChar8 *string)
320 const FcConstantList *l;
323 for (l = _FcConstants; l; l = l->next)
325 for (i = 0; i < l->nconsts; i++)
326 if (!FcStrCmpIgnoreCase (string, l->consts[i].name))
327 return &l->consts[i];
333 FcNameConstant (FcChar8 *string, int *result)
337 if ((c = FcNameGetConstant(string)))
346 FcNameBool (const FcChar8 *v, FcBool *result)
352 if (c0 == 't' || c0 == 'y' || c0 == '1')
357 if (c0 == 'f' || c0 == 'n' || c0 == '0')
381 FcNameConvert (FcType type, FcChar8 *string, FcMatrix *m)
388 if (!FcNameConstant (string, &v.u.i))
389 v.u.i = atoi ((char *) string);
392 v.u.s = FcObjectStaticName(string);
395 if (!FcNameBool (string, &v.u.b))
399 v.u.d = strtod ((char *) string, 0);
403 sscanf ((char *) string, "%lg %lg %lg %lg", &m->xx, &m->xy, &m->yx, &m->yy);
406 v.u.c = FcNameParseCharSet (string);
409 v.u.l = FcNameParseLangSet (string);
417 static const FcChar8 *
418 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
430 else if (strchr (delim, c))
443 FcNameParse (const FcChar8 *name)
452 const FcObjectType *t;
456 save = malloc (strlen ((char *) name) + 1);
459 pat = FcPatternCreate ();
465 name = FcNameFindNext (name, "-,:", save, &delim);
468 if (!FcPatternAddString (pat, FC_FAMILY, save))
478 name = FcNameFindNext (name, "-,:", save, &delim);
479 d = strtod ((char *) save, (char **) &e);
482 if (!FcPatternAddDouble (pat, FC_SIZE, d))
491 name = FcNameFindNext (name, "=_:", save, &delim);
494 if (delim == '=' || delim == '_')
496 t = FcNameGetObjectType ((char *) save);
499 name = FcNameFindNext (name, ":,", save, &delim);
502 v = FcNameConvert (t->type, save, &m);
503 if (!FcPatternAdd (pat, t->object, v, FcTrue))
507 FcCharSetDestroy ((FcCharSet *) v.u.c);
510 FcLangSetDestroy ((FcLangSet *) v.u.l);
519 FcCharSetDestroy ((FcCharSet *) v.u.c);
522 FcLangSetDestroy ((FcLangSet *) v.u.l);
534 if ((c = FcNameGetConstant (save)))
536 if (!FcPatternAddInteger (pat, c->object, c->value))
547 FcPatternDestroy (pat);
554 FcNameUnparseString (FcStrBuf *buf,
555 const FcChar8 *string,
556 const FcChar8 *escape)
559 while ((c = *string++))
561 if (escape && strchr ((char *) escape, (char) c))
563 if (!FcStrBufChar (buf, escape[0]))
566 if (!FcStrBufChar (buf, c))
573 FcNameUnparseValue (FcStrBuf *buf,
579 FcValue v = FcValueCanonicalize(v0);
585 sprintf ((char *) temp, "%d", v.u.i);
586 return FcNameUnparseString (buf, temp, 0);
588 sprintf ((char *) temp, "%g", v.u.d);
589 return FcNameUnparseString (buf, temp, 0);
591 return FcNameUnparseString (buf, v.u.s, escape);
593 return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
595 sprintf ((char *) temp, "%g %g %g %g",
596 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
597 return FcNameUnparseString (buf, temp, 0);
599 return FcNameUnparseCharSet (buf, v.u.c);
601 return FcNameUnparseLangSet (buf, v.u.l);
609 FcNameUnparseValueList (FcStrBuf *buf,
613 while (FcValueListPtrU(v))
615 if (!FcNameUnparseValue (buf, v.bank, &FcValueListPtrU(v)->value, escape))
617 if (FcValueListPtrU(v = FcValueListPtrU(v)->next))
618 if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
624 #define FC_ESCAPE_FIXED "\\-:,"
625 #define FC_ESCAPE_VARIABLE "\\=_:,"
628 FcNameUnparse (FcPattern *pat)
631 FcChar8 buf_static[8192];
634 const FcObjectTypeList *l;
635 const FcObjectType *o;
637 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
638 e = FcPatternFindElt (pat, FC_FAMILY);
641 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
644 e = FcPatternFindElt (pat, FC_SIZE);
647 if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
649 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
652 for (l = _FcObjectTypes; l; l = l->next)
654 for (i = 0; i < l->ntypes; i++)
657 if (!strcmp (o->object, FC_FAMILY) ||
658 !strcmp (o->object, FC_SIZE) ||
659 !strcmp (o->object, FC_FILE))
662 e = FcPatternFindElt (pat, o->object);
665 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
667 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, (FcChar8 *) FC_ESCAPE_VARIABLE))
669 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
671 if (!FcNameUnparseValueList (&buf, e->values,
672 (FcChar8 *) FC_ESCAPE_VARIABLE))
677 return FcStrBufDone (&buf);
679 FcStrBufDestroy (&buf);