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
},
78 { FC_LCD_FILTER
, FcTypeInteger
}, /* 41 */
81 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
83 typedef struct _FcObjectTypeList FcObjectTypeList
;
85 struct _FcObjectTypeList
{
86 const FcObjectTypeList
*next
;
87 const FcObjectType
*types
;
91 static const FcObjectTypeList _FcBaseObjectTypesList
= {
97 static const FcObjectTypeList
*_FcObjectTypes
= &_FcBaseObjectTypesList
;
99 #define OBJECT_HASH_SIZE 31
101 typedef struct _FcObjectBucket
{
102 struct _FcObjectBucket
*next
;
107 static FcObjectBucket
*FcObjectBuckets
[OBJECT_HASH_SIZE
];
109 static FcObjectType
*FcObjects
= (FcObjectType
*) _FcBaseObjectTypes
;
110 static int FcObjectsNumber
= NUM_OBJECT_TYPES
;
111 static int FcObjectsSize
= 0;
112 static FcBool FcObjectsInited
;
114 static FcObjectType
*
115 FcObjectInsert (const char *name
, FcType type
)
118 if (FcObjectsNumber
>= FcObjectsSize
)
120 int newsize
= FcObjectsNumber
* 2;
121 FcObjectType
*newobjects
;
124 newobjects
= realloc (FcObjects
, newsize
* sizeof (FcObjectType
));
127 newobjects
= malloc (newsize
* sizeof (FcObjectType
));
129 memcpy (newobjects
, FcObjects
,
130 FcObjectsNumber
* sizeof (FcObjectType
));
134 FcObjects
= newobjects
;
135 FcObjectsSize
= newsize
;
137 o
= &FcObjects
[FcObjectsNumber
];
145 FcObjectId (FcObjectType
*o
)
147 return o
- FcObjects
+ 1;
150 static FcObjectType
*
151 FcObjectFindByName (const char *object
, FcBool insert
)
153 FcChar32 hash
= FcStringHash ((const FcChar8
*) object
);
158 if (!FcObjectsInited
)
160 for (p
= &FcObjectBuckets
[hash
%OBJECT_HASH_SIZE
]; (b
= *p
); p
= &(b
->next
))
162 o
= FcObjects
+ b
->id
- 1;
163 if (b
->hash
== hash
&& !strcmp (object
, (o
->object
)))
169 * Hook it into the hash chain
171 b
= malloc (sizeof(FcObjectBucket
));
174 object
= (const char *) FcStrCopy ((FcChar8
*) object
);
179 o
= FcObjectInsert (object
, -1);
182 b
->id
= FcObjectId (o
);
187 static FcObjectType
*
188 FcObjectFindById (FcObject object
)
190 if (1 <= object
&& object
<= FcObjectsNumber
)
191 return FcObjects
+ object
- 1;
196 FcObjectHashInsert (const FcObjectType
*object
, FcBool copy
)
198 FcChar32 hash
= FcStringHash ((const FcChar8
*) object
->object
);
203 if (!FcObjectsInited
)
205 for (p
= &FcObjectBuckets
[hash
%OBJECT_HASH_SIZE
]; (b
= *p
); p
= &(b
->next
))
207 o
= FcObjects
+ b
->id
- 1;
208 if (b
->hash
== hash
&& !strcmp (object
->object
, o
->object
))
212 * Hook it into the hash chain
214 b
= malloc (sizeof(FcObjectBucket
));
219 o
= FcObjectInsert (object
->object
, object
->type
);
227 o
= (FcObjectType
*) object
;
230 b
->id
= FcObjectId (o
);
236 FcObjectHashRemove (const FcObjectType
*object
, FcBool cleanobj
)
238 FcChar32 hash
= FcStringHash ((const FcChar8
*) object
->object
);
243 if (!FcObjectsInited
)
245 for (p
= &FcObjectBuckets
[hash
%OBJECT_HASH_SIZE
]; (b
= *p
); p
= &(b
->next
))
247 o
= FcObjects
+ b
->id
- 1;
248 if (b
->hash
== hash
&& !strcmp (object
->object
, o
->object
))
254 /* Clean up object array */
257 while (FcObjects
[FcObjectsNumber
-1].object
== NULL
)
266 FcNameRegisterObjectTypes (const FcObjectType
*types
, int ntypes
)
270 for (i
= 0; i
< ntypes
; i
++)
271 if (!FcObjectHashInsert (&types
[i
], FcTrue
))
277 FcNameUnregisterObjectTypes (const FcObjectType
*types
, int ntypes
)
281 for (i
= 0; i
< ntypes
; i
++)
282 FcObjectHashRemove (&types
[i
], FcTrue
);
287 FcNameGetObjectType (const char *object
)
289 return FcObjectFindByName (object
, FcFalse
);
293 FcObjectValidType (FcObject object
, FcType type
)
295 FcObjectType
*t
= FcObjectFindById (object
);
303 if (type
== FcTypeDouble
|| type
== FcTypeInteger
)
307 if (type
== FcTypeLangSet
|| type
== FcTypeString
)
321 FcObjectFromName (const char * name
)
323 FcObjectType
*o
= FcObjectFindByName (name
, FcTrue
);
326 return FcObjectId (o
);
338 FcObjectsInited
= FcTrue
;
339 for (i
= 0; i
< NUM_OBJECT_TYPES
; i
++)
340 if (!FcObjectHashInsert (&_FcBaseObjectTypes
[i
], FcFalse
))
349 FcObjectBucket
*b
, *next
;
351 for (i
= 0; i
< OBJECT_HASH_SIZE
; i
++)
353 for (b
= FcObjectBuckets
[i
]; b
; b
= next
)
358 FcObjectBuckets
[i
] = 0;
360 for (i
= 0; i
< FcObjectsNumber
; i
++)
361 if (FcObjects
[i
].type
== -1)
362 free ((void*) FcObjects
[i
].object
);
363 if (FcObjects
!= _FcBaseObjectTypes
)
365 FcObjects
= (FcObjectType
*) _FcBaseObjectTypes
;
366 FcObjectsNumber
= NUM_OBJECT_TYPES
;
368 FcObjectsInited
= FcFalse
;
372 FcObjectName (FcObject object
)
374 FcObjectType
*o
= FcObjectFindById (object
);
381 static const FcConstant _FcBaseConstants
[] = {
382 { (FcChar8
*) "thin", "weight", FC_WEIGHT_THIN
, },
383 { (FcChar8
*) "extralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
384 { (FcChar8
*) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
385 { (FcChar8
*) "light", "weight", FC_WEIGHT_LIGHT
, },
386 { (FcChar8
*) "book", "weight", FC_WEIGHT_BOOK
, },
387 { (FcChar8
*) "regular", "weight", FC_WEIGHT_REGULAR
, },
388 { (FcChar8
*) "medium", "weight", FC_WEIGHT_MEDIUM
, },
389 { (FcChar8
*) "demibold", "weight", FC_WEIGHT_DEMIBOLD
, },
390 { (FcChar8
*) "semibold", "weight", FC_WEIGHT_DEMIBOLD
, },
391 { (FcChar8
*) "bold", "weight", FC_WEIGHT_BOLD
, },
392 { (FcChar8
*) "extrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
393 { (FcChar8
*) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
394 { (FcChar8
*) "black", "weight", FC_WEIGHT_BLACK
, },
395 { (FcChar8
*) "heavy", "weight", FC_WEIGHT_HEAVY
, },
397 { (FcChar8
*) "roman", "slant", FC_SLANT_ROMAN
, },
398 { (FcChar8
*) "italic", "slant", FC_SLANT_ITALIC
, },
399 { (FcChar8
*) "oblique", "slant", FC_SLANT_OBLIQUE
, },
401 { (FcChar8
*) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED
},
402 { (FcChar8
*) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED
},
403 { (FcChar8
*) "condensed", "width", FC_WIDTH_CONDENSED
},
404 { (FcChar8
*) "semicondensed", "width", FC_WIDTH_SEMICONDENSED
},
405 { (FcChar8
*) "normal", "width", FC_WIDTH_NORMAL
},
406 { (FcChar8
*) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED
},
407 { (FcChar8
*) "expanded", "width", FC_WIDTH_EXPANDED
},
408 { (FcChar8
*) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED
},
409 { (FcChar8
*) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED
},
411 { (FcChar8
*) "proportional", "spacing", FC_PROPORTIONAL
, },
412 { (FcChar8
*) "dual", "spacing", FC_DUAL
, },
413 { (FcChar8
*) "mono", "spacing", FC_MONO
, },
414 { (FcChar8
*) "charcell", "spacing", FC_CHARCELL
, },
416 { (FcChar8
*) "unknown", "rgba", FC_RGBA_UNKNOWN
},
417 { (FcChar8
*) "rgb", "rgba", FC_RGBA_RGB
, },
418 { (FcChar8
*) "bgr", "rgba", FC_RGBA_BGR
, },
419 { (FcChar8
*) "vrgb", "rgba", FC_RGBA_VRGB
},
420 { (FcChar8
*) "vbgr", "rgba", FC_RGBA_VBGR
},
421 { (FcChar8
*) "none", "rgba", FC_RGBA_NONE
},
423 { (FcChar8
*) "hintnone", "hintstyle", FC_HINT_NONE
},
424 { (FcChar8
*) "hintslight", "hintstyle", FC_HINT_SLIGHT
},
425 { (FcChar8
*) "hintmedium", "hintstyle", FC_HINT_MEDIUM
},
426 { (FcChar8
*) "hintfull", "hintstyle", FC_HINT_FULL
},
428 { (FcChar8
*) "antialias", "antialias", FcTrue
},
429 { (FcChar8
*) "hinting", "hinting", FcTrue
},
430 { (FcChar8
*) "verticallayout", "verticallayout", FcTrue
},
431 { (FcChar8
*) "autohint", "autohint", FcTrue
},
432 { (FcChar8
*) "globaladvance", "globaladvance", FcTrue
},
433 { (FcChar8
*) "outline", "outline", FcTrue
},
434 { (FcChar8
*) "scalable", "scalable", FcTrue
},
435 { (FcChar8
*) "minspace", "minspace", FcTrue
},
436 { (FcChar8
*) "embolden", "embolden", FcTrue
},
437 { (FcChar8
*) "embeddedbitmap", "embeddedbitmap", FcTrue
},
438 { (FcChar8
*) "decorative", "decorative", FcTrue
},
439 { (FcChar8
*) "lcdnone", "lcdfilter", FC_LCD_NONE
},
440 { (FcChar8
*) "lcddefault", "lcdfilter", FC_LCD_DEFAULT
},
441 { (FcChar8
*) "lcdlight", "lcdfilter", FC_LCD_LIGHT
},
442 { (FcChar8
*) "lcdlegacy", "lcdfilter", FC_LCD_LEGACY
},
445 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
447 typedef struct _FcConstantList FcConstantList
;
449 struct _FcConstantList
{
450 const FcConstantList
*next
;
451 const FcConstant
*consts
;
455 static const FcConstantList _FcBaseConstantList
= {
461 static const FcConstantList
*_FcConstants
= &_FcBaseConstantList
;
464 FcNameRegisterConstants (const FcConstant
*consts
, int nconsts
)
468 l
= (FcConstantList
*) malloc (sizeof (FcConstantList
));
471 FcMemAlloc (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
473 l
->nconsts
= nconsts
;
474 l
->next
= _FcConstants
;
480 FcNameUnregisterConstants (const FcConstant
*consts
, int nconsts
)
482 const FcConstantList
*l
, **prev
;
484 for (prev
= &_FcConstants
;
486 prev
= (const FcConstantList
**) &(l
->next
))
488 if (l
->consts
== consts
&& l
->nconsts
== nconsts
)
491 FcMemFree (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
500 FcNameGetConstant (FcChar8
*string
)
502 const FcConstantList
*l
;
505 for (l
= _FcConstants
; l
; l
= l
->next
)
507 for (i
= 0; i
< l
->nconsts
; i
++)
508 if (!FcStrCmpIgnoreCase (string
, l
->consts
[i
].name
))
509 return &l
->consts
[i
];
515 FcNameConstant (FcChar8
*string
, int *result
)
519 if ((c
= FcNameGetConstant(string
)))
528 FcNameBool (const FcChar8
*v
, FcBool
*result
)
534 if (c0
== 't' || c0
== 'y' || c0
== '1')
539 if (c0
== 'f' || c0
== 'n' || c0
== '0')
563 FcNameConvert (FcType type
, FcChar8
*string
, FcMatrix
*m
)
570 if (!FcNameConstant (string
, &v
.u
.i
))
571 v
.u
.i
= atoi ((char *) string
);
574 v
.u
.s
= FcStrStaticName(string
);
579 if (!FcNameBool (string
, &v
.u
.b
))
583 v
.u
.d
= strtod ((char *) string
, 0);
587 sscanf ((char *) string
, "%lg %lg %lg %lg", &m
->xx
, &m
->xy
, &m
->yx
, &m
->yy
);
590 v
.u
.c
= FcNameParseCharSet (string
);
595 v
.u
.l
= FcNameParseLangSet (string
);
605 static const FcChar8
*
606 FcNameFindNext (const FcChar8
*cur
, const char *delim
, FcChar8
*save
, FcChar8
*last
)
618 else if (strchr (delim
, c
))
631 FcNameParse (const FcChar8
*name
)
640 const FcObjectType
*t
;
644 save
= malloc (strlen ((char *) name
) + 1);
647 pat
= FcPatternCreate ();
653 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
656 if (!FcPatternAddString (pat
, FC_FAMILY
, save
))
666 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
667 d
= strtod ((char *) save
, (char **) &e
);
670 if (!FcPatternAddDouble (pat
, FC_SIZE
, d
))
679 name
= FcNameFindNext (name
, "=_:", save
, &delim
);
682 if (delim
== '=' || delim
== '_')
684 t
= FcNameGetObjectType ((char *) save
);
687 name
= FcNameFindNext (name
, ":,", save
, &delim
);
690 v
= FcNameConvert (t
->type
, save
, &m
);
691 if (!FcPatternAdd (pat
, t
->object
, v
, FcTrue
))
695 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
698 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
707 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
710 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
722 if ((c
= FcNameGetConstant (save
)))
724 t
= FcNameGetObjectType ((char *) c
->object
);
728 if (!FcPatternAddInteger (pat
, c
->object
, c
->value
))
732 if (!FcPatternAddBool (pat
, c
->object
, c
->value
))
747 FcPatternDestroy (pat
);
754 FcNameUnparseString (FcStrBuf
*buf
,
755 const FcChar8
*string
,
756 const FcChar8
*escape
)
759 while ((c
= *string
++))
761 if (escape
&& strchr ((char *) escape
, (char) c
))
763 if (!FcStrBufChar (buf
, escape
[0]))
766 if (!FcStrBufChar (buf
, c
))
773 FcNameUnparseValue (FcStrBuf
*buf
,
778 FcValue v
= FcValueCanonicalize(v0
);
784 sprintf ((char *) temp
, "%d", v
.u
.i
);
785 return FcNameUnparseString (buf
, temp
, 0);
787 sprintf ((char *) temp
, "%g", v
.u
.d
);
788 return FcNameUnparseString (buf
, temp
, 0);
790 return FcNameUnparseString (buf
, v
.u
.s
, escape
);
792 return FcNameUnparseString (buf
, v
.u
.b
? (FcChar8
*) "True" : (FcChar8
*) "False", 0);
794 sprintf ((char *) temp
, "%g %g %g %g",
795 v
.u
.m
->xx
, v
.u
.m
->xy
, v
.u
.m
->yx
, v
.u
.m
->yy
);
796 return FcNameUnparseString (buf
, temp
, 0);
798 return FcNameUnparseCharSet (buf
, v
.u
.c
);
800 return FcNameUnparseLangSet (buf
, v
.u
.l
);
808 FcNameUnparseValueList (FcStrBuf
*buf
,
814 if (!FcNameUnparseValue (buf
, &v
->value
, escape
))
816 if ((v
= FcValueListNext(v
)) != NULL
)
817 if (!FcNameUnparseString (buf
, (FcChar8
*) ",", 0))
823 #define FC_ESCAPE_FIXED "\\-:,"
824 #define FC_ESCAPE_VARIABLE "\\=_:,"
827 FcNameUnparse (FcPattern
*pat
)
829 return FcNameUnparseEscaped (pat
, FcTrue
);
833 FcNameUnparseEscaped (FcPattern
*pat
, FcBool escape
)
836 FcChar8 buf_static
[8192];
839 const FcObjectTypeList
*l
;
840 const FcObjectType
*o
;
842 FcStrBufInit (&buf
, buf_static
, sizeof (buf_static
));
843 e
= FcPatternObjectFindElt (pat
, FC_FAMILY_OBJECT
);
846 if (!FcNameUnparseValueList (&buf
, FcPatternEltValues(e
), escape
? (FcChar8
*) FC_ESCAPE_FIXED
: 0))
849 e
= FcPatternObjectFindElt (pat
, FC_SIZE_OBJECT
);
852 if (!FcNameUnparseString (&buf
, (FcChar8
*) "-", 0))
854 if (!FcNameUnparseValueList (&buf
, FcPatternEltValues(e
), escape
? (FcChar8
*) FC_ESCAPE_FIXED
: 0))
857 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
859 for (i
= 0; i
< l
->ntypes
; i
++)
862 if (!strcmp (o
->object
, FC_FAMILY
) ||
863 !strcmp (o
->object
, FC_SIZE
) ||
864 !strcmp (o
->object
, FC_FILE
))
867 e
= FcPatternObjectFindElt (pat
, FcObjectFromName (o
->object
));
870 if (!FcNameUnparseString (&buf
, (FcChar8
*) ":", 0))
872 if (!FcNameUnparseString (&buf
, (FcChar8
*) o
->object
, escape
? (FcChar8
*) FC_ESCAPE_VARIABLE
: 0))
874 if (!FcNameUnparseString (&buf
, (FcChar8
*) "=", 0))
876 if (!FcNameUnparseValueList (&buf
, FcPatternEltValues(e
), escape
?
877 (FcChar8
*) FC_ESCAPE_VARIABLE
: 0))
882 return FcStrBufDone (&buf
);
884 FcStrBufDestroy (&buf
);
888 #include "fcaliastail.h"