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.
32 * Please do not change this list, it is used to initialize the object
33 * list in this order to match the FC_foo_OBJECT constants. Those
34 * constants are written into cache files.
37 static const FcObjectType _FcBaseObjectTypes
[] = {
38 { FC_FAMILY
, FcTypeString
, }, /* 1 */
39 { FC_FAMILYLANG
, FcTypeString
, },
40 { FC_STYLE
, FcTypeString
, },
41 { FC_STYLELANG
, FcTypeString
, },
42 { FC_FULLNAME
, FcTypeString
, },
43 { FC_FULLNAMELANG
, FcTypeString
, },
44 { FC_SLANT
, FcTypeInteger
, },
45 { FC_WEIGHT
, FcTypeInteger
, },
46 { FC_WIDTH
, FcTypeInteger
, },
47 { FC_SIZE
, FcTypeDouble
, },
48 { FC_ASPECT
, FcTypeDouble
, },
49 { FC_PIXEL_SIZE
, FcTypeDouble
, },
50 { FC_SPACING
, FcTypeInteger
, },
51 { FC_FOUNDRY
, FcTypeString
, },
52 { FC_ANTIALIAS
, FcTypeBool
, },
53 { FC_HINT_STYLE
, FcTypeInteger
, },
54 { FC_HINTING
, FcTypeBool
, },
55 { FC_VERTICAL_LAYOUT
, FcTypeBool
, },
56 { FC_AUTOHINT
, FcTypeBool
, },
57 { FC_GLOBAL_ADVANCE
, FcTypeBool
, },
58 { FC_FILE
, FcTypeString
, },
59 { FC_INDEX
, FcTypeInteger
, },
60 { FC_RASTERIZER
, FcTypeString
, },
61 { FC_OUTLINE
, FcTypeBool
, },
62 { FC_SCALABLE
, FcTypeBool
, },
63 { FC_DPI
, FcTypeDouble
},
64 { FC_RGBA
, FcTypeInteger
, },
65 { FC_SCALE
, FcTypeDouble
, },
66 { FC_MINSPACE
, FcTypeBool
, },
67 { FC_CHAR_WIDTH
, FcTypeInteger
},
68 { FC_CHAR_HEIGHT
, FcTypeInteger
},
69 { FC_MATRIX
, FcTypeMatrix
},
70 { FC_CHARSET
, FcTypeCharSet
},
71 { FC_LANG
, FcTypeLangSet
},
72 { FC_FONTVERSION
, FcTypeInteger
},
73 { FC_CAPABILITY
, FcTypeString
},
74 { FC_FONTFORMAT
, FcTypeString
},
75 { FC_EMBOLDEN
, FcTypeBool
},
76 { FC_EMBEDDED_BITMAP
, FcTypeBool
},
77 { FC_DECORATIVE
, FcTypeBool
}, /* 40 */
80 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
82 typedef struct _FcObjectTypeList FcObjectTypeList
;
84 struct _FcObjectTypeList
{
85 const FcObjectTypeList
*next
;
86 const FcObjectType
*types
;
90 static const FcObjectTypeList _FcBaseObjectTypesList
= {
96 static const FcObjectTypeList
*_FcObjectTypes
= &_FcBaseObjectTypesList
;
98 #define OBJECT_HASH_SIZE 31
100 typedef struct _FcObjectBucket
{
101 struct _FcObjectBucket
*next
;
106 static FcObjectBucket
*FcObjectBuckets
[OBJECT_HASH_SIZE
];
108 static FcObjectType
*FcObjects
= (FcObjectType
*) _FcBaseObjectTypes
;
109 static int FcObjectsNumber
= NUM_OBJECT_TYPES
;
110 static int FcObjectsSize
= 0;
111 static FcBool FcObjectsInited
;
113 static FcObjectType
*
114 FcObjectInsert (const char *name
, FcType type
)
117 if (FcObjectsNumber
>= FcObjectsSize
)
119 int newsize
= FcObjectsNumber
* 2;
120 FcObjectType
*newobjects
;
123 newobjects
= realloc (FcObjects
, newsize
* sizeof (FcObjectType
));
126 newobjects
= malloc (newsize
* sizeof (FcObjectType
));
128 memcpy (newobjects
, FcObjects
,
129 FcObjectsNumber
* sizeof (FcObjectType
));
133 FcObjects
= newobjects
;
134 FcObjectsSize
= newsize
;
136 o
= &FcObjects
[FcObjectsNumber
];
144 FcObjectId (FcObjectType
*o
)
146 return o
- FcObjects
+ 1;
149 static FcObjectType
*
150 FcObjectFindByName (const char *object
, FcBool insert
)
152 FcChar32 hash
= FcStringHash ((const FcChar8
*) object
);
157 if (!FcObjectsInited
)
159 for (p
= &FcObjectBuckets
[hash
%OBJECT_HASH_SIZE
]; (b
= *p
); p
= &(b
->next
))
161 o
= FcObjects
+ b
->id
- 1;
162 if (b
->hash
== hash
&& !strcmp (object
, (o
->object
)))
168 * Hook it into the hash chain
170 b
= malloc (sizeof(FcObjectBucket
));
173 object
= (const char *) FcStrCopy ((FcChar8
*) object
);
178 o
= FcObjectInsert (object
, -1);
181 b
->id
= FcObjectId (o
);
186 static FcObjectType
*
187 FcObjectFindById (FcObject object
)
189 if (1 <= object
&& object
<= FcObjectsNumber
)
190 return FcObjects
+ object
- 1;
195 FcObjectHashInsert (const FcObjectType
*object
, FcBool copy
)
197 FcChar32 hash
= FcStringHash ((const FcChar8
*) object
->object
);
202 if (!FcObjectsInited
)
204 for (p
= &FcObjectBuckets
[hash
%OBJECT_HASH_SIZE
]; (b
= *p
); p
= &(b
->next
))
206 o
= FcObjects
+ b
->id
- 1;
207 if (b
->hash
== hash
&& !strcmp (object
->object
, o
->object
))
211 * Hook it into the hash chain
213 b
= malloc (sizeof(FcObjectBucket
));
218 o
= FcObjectInsert (object
->object
, object
->type
);
226 o
= (FcObjectType
*) object
;
229 b
->id
= FcObjectId (o
);
235 FcObjectHashRemove (const FcObjectType
*object
, FcBool cleanobj
)
237 FcChar32 hash
= FcStringHash ((const FcChar8
*) object
->object
);
242 if (!FcObjectsInited
)
244 for (p
= &FcObjectBuckets
[hash
%OBJECT_HASH_SIZE
]; (b
= *p
); p
= &(b
->next
))
246 o
= FcObjects
+ b
->id
- 1;
247 if (b
->hash
== hash
&& !strcmp (object
->object
, o
->object
))
253 /* Clean up object array */
256 while (FcObjects
[FcObjectsNumber
-1].object
== NULL
)
265 FcNameRegisterObjectTypes (const FcObjectType
*types
, int ntypes
)
269 for (i
= 0; i
< ntypes
; i
++)
270 if (!FcObjectHashInsert (&types
[i
], FcTrue
))
276 FcNameUnregisterObjectTypes (const FcObjectType
*types
, int ntypes
)
280 for (i
= 0; i
< ntypes
; i
++)
281 FcObjectHashRemove (&types
[i
], FcTrue
);
286 FcNameGetObjectType (const char *object
)
288 return FcObjectFindByName (object
, FcFalse
);
292 FcObjectValidType (FcObject object
, FcType type
)
294 FcObjectType
*t
= FcObjectFindById (object
);
302 if (type
== FcTypeDouble
|| type
== FcTypeInteger
)
306 if (type
== FcTypeLangSet
|| type
== FcTypeString
)
320 FcObjectFromName (const char * name
)
322 FcObjectType
*o
= FcObjectFindByName (name
, FcTrue
);
325 return FcObjectId (o
);
337 FcObjectsInited
= FcTrue
;
338 for (i
= 0; i
< NUM_OBJECT_TYPES
; i
++)
339 if (!FcObjectHashInsert (&_FcBaseObjectTypes
[i
], FcFalse
))
348 FcObjectBucket
*b
, *next
;
350 for (i
= 0; i
< OBJECT_HASH_SIZE
; i
++)
352 for (b
= FcObjectBuckets
[i
]; b
; b
= next
)
357 FcObjectBuckets
[i
] = 0;
359 for (i
= 0; i
< FcObjectsNumber
; i
++)
360 if (FcObjects
[i
].type
== -1)
361 free ((void*) FcObjects
[i
].object
);
362 if (FcObjects
!= _FcBaseObjectTypes
)
364 FcObjects
= (FcObjectType
*) _FcBaseObjectTypes
;
365 FcObjectsNumber
= NUM_OBJECT_TYPES
;
367 FcObjectsInited
= FcFalse
;
371 FcObjectName (FcObject object
)
373 FcObjectType
*o
= FcObjectFindById (object
);
380 static const FcConstant _FcBaseConstants
[] = {
381 { (FcChar8
*) "thin", "weight", FC_WEIGHT_THIN
, },
382 { (FcChar8
*) "extralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
383 { (FcChar8
*) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
384 { (FcChar8
*) "light", "weight", FC_WEIGHT_LIGHT
, },
385 { (FcChar8
*) "book", "weight", FC_WEIGHT_BOOK
, },
386 { (FcChar8
*) "regular", "weight", FC_WEIGHT_REGULAR
, },
387 { (FcChar8
*) "medium", "weight", FC_WEIGHT_MEDIUM
, },
388 { (FcChar8
*) "demibold", "weight", FC_WEIGHT_DEMIBOLD
, },
389 { (FcChar8
*) "semibold", "weight", FC_WEIGHT_DEMIBOLD
, },
390 { (FcChar8
*) "bold", "weight", FC_WEIGHT_BOLD
, },
391 { (FcChar8
*) "extrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
392 { (FcChar8
*) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
393 { (FcChar8
*) "black", "weight", FC_WEIGHT_BLACK
, },
394 { (FcChar8
*) "heavy", "weight", FC_WEIGHT_HEAVY
, },
396 { (FcChar8
*) "roman", "slant", FC_SLANT_ROMAN
, },
397 { (FcChar8
*) "italic", "slant", FC_SLANT_ITALIC
, },
398 { (FcChar8
*) "oblique", "slant", FC_SLANT_OBLIQUE
, },
400 { (FcChar8
*) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED
},
401 { (FcChar8
*) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED
},
402 { (FcChar8
*) "condensed", "width", FC_WIDTH_CONDENSED
},
403 { (FcChar8
*) "semicondensed", "width", FC_WIDTH_SEMICONDENSED
},
404 { (FcChar8
*) "normal", "width", FC_WIDTH_NORMAL
},
405 { (FcChar8
*) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED
},
406 { (FcChar8
*) "expanded", "width", FC_WIDTH_EXPANDED
},
407 { (FcChar8
*) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED
},
408 { (FcChar8
*) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED
},
410 { (FcChar8
*) "proportional", "spacing", FC_PROPORTIONAL
, },
411 { (FcChar8
*) "dual", "spacing", FC_DUAL
, },
412 { (FcChar8
*) "mono", "spacing", FC_MONO
, },
413 { (FcChar8
*) "charcell", "spacing", FC_CHARCELL
, },
415 { (FcChar8
*) "unknown", "rgba", FC_RGBA_UNKNOWN
},
416 { (FcChar8
*) "rgb", "rgba", FC_RGBA_RGB
, },
417 { (FcChar8
*) "bgr", "rgba", FC_RGBA_BGR
, },
418 { (FcChar8
*) "vrgb", "rgba", FC_RGBA_VRGB
},
419 { (FcChar8
*) "vbgr", "rgba", FC_RGBA_VBGR
},
420 { (FcChar8
*) "none", "rgba", FC_RGBA_NONE
},
422 { (FcChar8
*) "hintnone", "hintstyle", FC_HINT_NONE
},
423 { (FcChar8
*) "hintslight", "hintstyle", FC_HINT_SLIGHT
},
424 { (FcChar8
*) "hintmedium", "hintstyle", FC_HINT_MEDIUM
},
425 { (FcChar8
*) "hintfull", "hintstyle", FC_HINT_FULL
},
427 { (FcChar8
*) "antialias", "antialias", FcTrue
},
428 { (FcChar8
*) "hinting", "hinting", FcTrue
},
429 { (FcChar8
*) "verticallayout", "verticallayout", FcTrue
},
430 { (FcChar8
*) "autohint", "autohint", FcTrue
},
431 { (FcChar8
*) "globaladvance", "globaladvance", FcTrue
},
432 { (FcChar8
*) "outline", "outline", FcTrue
},
433 { (FcChar8
*) "scalable", "scalable", FcTrue
},
434 { (FcChar8
*) "minspace", "minspace", FcTrue
},
435 { (FcChar8
*) "embolden", "embolden", FcTrue
},
436 { (FcChar8
*) "embeddedbitmap", "embeddedbitmap", FcTrue
},
437 { (FcChar8
*) "decorative", "decorative", FcTrue
},
440 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
442 typedef struct _FcConstantList FcConstantList
;
444 struct _FcConstantList
{
445 const FcConstantList
*next
;
446 const FcConstant
*consts
;
450 static const FcConstantList _FcBaseConstantList
= {
456 static const FcConstantList
*_FcConstants
= &_FcBaseConstantList
;
459 FcNameRegisterConstants (const FcConstant
*consts
, int nconsts
)
463 l
= (FcConstantList
*) malloc (sizeof (FcConstantList
));
466 FcMemAlloc (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
468 l
->nconsts
= nconsts
;
469 l
->next
= _FcConstants
;
475 FcNameUnregisterConstants (const FcConstant
*consts
, int nconsts
)
477 const FcConstantList
*l
, **prev
;
479 for (prev
= &_FcConstants
;
481 prev
= (const FcConstantList
**) &(l
->next
))
483 if (l
->consts
== consts
&& l
->nconsts
== nconsts
)
486 FcMemFree (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
495 FcNameGetConstant (FcChar8
*string
)
497 const FcConstantList
*l
;
500 for (l
= _FcConstants
; l
; l
= l
->next
)
502 for (i
= 0; i
< l
->nconsts
; i
++)
503 if (!FcStrCmpIgnoreCase (string
, l
->consts
[i
].name
))
504 return &l
->consts
[i
];
510 FcNameConstant (FcChar8
*string
, int *result
)
514 if ((c
= FcNameGetConstant(string
)))
523 FcNameBool (const FcChar8
*v
, FcBool
*result
)
529 if (c0
== 't' || c0
== 'y' || c0
== '1')
534 if (c0
== 'f' || c0
== 'n' || c0
== '0')
558 FcNameConvert (FcType type
, FcChar8
*string
, FcMatrix
*m
)
565 if (!FcNameConstant (string
, &v
.u
.i
))
566 v
.u
.i
= atoi ((char *) string
);
569 v
.u
.s
= FcStrStaticName(string
);
574 if (!FcNameBool (string
, &v
.u
.b
))
578 v
.u
.d
= strtod ((char *) string
, 0);
582 sscanf ((char *) string
, "%lg %lg %lg %lg", &m
->xx
, &m
->xy
, &m
->yx
, &m
->yy
);
585 v
.u
.c
= FcNameParseCharSet (string
);
590 v
.u
.l
= FcNameParseLangSet (string
);
600 static const FcChar8
*
601 FcNameFindNext (const FcChar8
*cur
, const char *delim
, FcChar8
*save
, FcChar8
*last
)
613 else if (strchr (delim
, c
))
626 FcNameParse (const FcChar8
*name
)
635 const FcObjectType
*t
;
639 save
= malloc (strlen ((char *) name
) + 1);
642 pat
= FcPatternCreate ();
648 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
651 if (!FcPatternAddString (pat
, FC_FAMILY
, save
))
661 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
662 d
= strtod ((char *) save
, (char **) &e
);
665 if (!FcPatternAddDouble (pat
, FC_SIZE
, d
))
674 name
= FcNameFindNext (name
, "=_:", save
, &delim
);
677 if (delim
== '=' || delim
== '_')
679 t
= FcNameGetObjectType ((char *) save
);
682 name
= FcNameFindNext (name
, ":,", save
, &delim
);
685 v
= FcNameConvert (t
->type
, save
, &m
);
686 if (!FcPatternAdd (pat
, t
->object
, v
, FcTrue
))
690 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
693 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
702 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
705 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
717 if ((c
= FcNameGetConstant (save
)))
719 t
= FcNameGetObjectType ((char *) c
->object
);
723 if (!FcPatternAddInteger (pat
, c
->object
, c
->value
))
727 if (!FcPatternAddBool (pat
, c
->object
, c
->value
))
742 FcPatternDestroy (pat
);
749 FcNameUnparseString (FcStrBuf
*buf
,
750 const FcChar8
*string
,
751 const FcChar8
*escape
)
754 while ((c
= *string
++))
756 if (escape
&& strchr ((char *) escape
, (char) c
))
758 if (!FcStrBufChar (buf
, escape
[0]))
761 if (!FcStrBufChar (buf
, c
))
768 FcNameUnparseValue (FcStrBuf
*buf
,
773 FcValue v
= FcValueCanonicalize(v0
);
779 sprintf ((char *) temp
, "%d", v
.u
.i
);
780 return FcNameUnparseString (buf
, temp
, 0);
782 sprintf ((char *) temp
, "%g", v
.u
.d
);
783 return FcNameUnparseString (buf
, temp
, 0);
785 return FcNameUnparseString (buf
, v
.u
.s
, escape
);
787 return FcNameUnparseString (buf
, v
.u
.b
? (FcChar8
*) "True" : (FcChar8
*) "False", 0);
789 sprintf ((char *) temp
, "%g %g %g %g",
790 v
.u
.m
->xx
, v
.u
.m
->xy
, v
.u
.m
->yx
, v
.u
.m
->yy
);
791 return FcNameUnparseString (buf
, temp
, 0);
793 return FcNameUnparseCharSet (buf
, v
.u
.c
);
795 return FcNameUnparseLangSet (buf
, v
.u
.l
);
803 FcNameUnparseValueList (FcStrBuf
*buf
,
809 if (!FcNameUnparseValue (buf
, &v
->value
, escape
))
811 if ((v
= FcValueListNext(v
)) != NULL
)
812 if (!FcNameUnparseString (buf
, (FcChar8
*) ",", 0))
818 #define FC_ESCAPE_FIXED "\\-:,"
819 #define FC_ESCAPE_VARIABLE "\\=_:,"
822 FcNameUnparse (FcPattern
*pat
)
824 return FcNameUnparseEscaped (pat
, FcTrue
);
828 FcNameUnparseEscaped (FcPattern
*pat
, FcBool escape
)
831 FcChar8 buf_static
[8192];
834 const FcObjectTypeList
*l
;
835 const FcObjectType
*o
;
837 FcStrBufInit (&buf
, buf_static
, sizeof (buf_static
));
838 e
= FcPatternObjectFindElt (pat
, FC_FAMILY_OBJECT
);
841 if (!FcNameUnparseValueList (&buf
, FcPatternEltValues(e
), escape
? (FcChar8
*) FC_ESCAPE_FIXED
: 0))
844 e
= FcPatternObjectFindElt (pat
, FC_SIZE_OBJECT
);
847 if (!FcNameUnparseString (&buf
, (FcChar8
*) "-", 0))
849 if (!FcNameUnparseValueList (&buf
, FcPatternEltValues(e
), escape
? (FcChar8
*) FC_ESCAPE_FIXED
: 0))
852 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
854 for (i
= 0; i
< l
->ntypes
; i
++)
857 if (!strcmp (o
->object
, FC_FAMILY
) ||
858 !strcmp (o
->object
, FC_SIZE
) ||
859 !strcmp (o
->object
, FC_FILE
))
862 e
= FcPatternObjectFindElt (pat
, FcObjectFromName (o
->object
));
865 if (!FcNameUnparseString (&buf
, (FcChar8
*) ":", 0))
867 if (!FcNameUnparseString (&buf
, (FcChar8
*) o
->object
, escape
? (FcChar8
*) FC_ESCAPE_VARIABLE
: 0))
869 if (!FcNameUnparseString (&buf
, (FcChar8
*) "=", 0))
871 if (!FcNameUnparseValueList (&buf
, FcPatternEltValues(e
), escape
?
872 (FcChar8
*) FC_ESCAPE_VARIABLE
: 0))
877 return FcStrBufDone (&buf
);
879 FcStrBufDestroy (&buf
);
883 #include "fcaliastail.h"