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
}, /* 39 */
79 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
81 typedef struct _FcObjectTypeList FcObjectTypeList
;
83 struct _FcObjectTypeList
{
84 const FcObjectTypeList
*next
;
85 const FcObjectType
*types
;
89 static const FcObjectTypeList _FcBaseObjectTypesList
= {
95 static const FcObjectTypeList
*_FcObjectTypes
= &_FcBaseObjectTypesList
;
97 #define OBJECT_HASH_SIZE 31
99 typedef struct _FcObjectBucket
{
100 struct _FcObjectBucket
*next
;
105 static FcObjectBucket
*FcObjectBuckets
[OBJECT_HASH_SIZE
];
107 static FcObjectType
*FcObjects
= (FcObjectType
*) _FcBaseObjectTypes
;
108 static int FcObjectsNumber
= NUM_OBJECT_TYPES
;
109 static int FcObjectsSize
= 0;
110 static FcBool FcObjectsInited
;
112 static FcObjectType
*
113 FcObjectInsert (const char *name
, FcType type
)
116 if (FcObjectsNumber
>= FcObjectsSize
)
118 int newsize
= FcObjectsNumber
* 2;
119 FcObjectType
*newobjects
;
122 newobjects
= realloc (FcObjects
, newsize
* sizeof (FcObjectType
));
125 newobjects
= malloc (newsize
* sizeof (FcObjectType
));
127 memcpy (newobjects
, FcObjects
,
128 FcObjectsNumber
* sizeof (FcObjectType
));
132 FcObjects
= newobjects
;
133 FcObjectsSize
= newsize
;
135 o
= &FcObjects
[FcObjectsNumber
];
143 FcObjectId (FcObjectType
*o
)
145 return o
- FcObjects
+ 1;
148 static FcObjectType
*
149 FcObjectFindByName (const char *object
, FcBool insert
)
151 FcChar32 hash
= FcStringHash ((const FcChar8
*) object
);
156 if (!FcObjectsInited
)
158 for (p
= &FcObjectBuckets
[hash
%OBJECT_HASH_SIZE
]; (b
= *p
); p
= &(b
->next
))
160 o
= FcObjects
+ b
->id
- 1;
161 if (b
->hash
== hash
&& !strcmp (object
, (o
->object
)))
167 * Hook it into the hash chain
169 b
= malloc (sizeof(FcObjectBucket
));
172 object
= (const char *) FcStrCopy ((FcChar8
*) object
);
177 o
= FcObjectInsert (object
, -1);
180 b
->id
= FcObjectId (o
);
185 static FcObjectType
*
186 FcObjectFindById (FcObject object
)
188 if (1 <= object
&& object
<= FcObjectsNumber
)
189 return FcObjects
+ object
- 1;
194 FcObjectHashInsert (const FcObjectType
*object
, FcBool copy
)
196 FcChar32 hash
= FcStringHash ((const FcChar8
*) object
->object
);
201 if (!FcObjectsInited
)
203 for (p
= &FcObjectBuckets
[hash
%OBJECT_HASH_SIZE
]; (b
= *p
); p
= &(b
->next
))
205 o
= FcObjects
+ b
->id
- 1;
206 if (b
->hash
== hash
&& !strcmp (object
->object
, o
->object
))
210 * Hook it into the hash chain
212 b
= malloc (sizeof(FcObjectBucket
));
217 o
= FcObjectInsert (object
->object
, object
->type
);
225 o
= (FcObjectType
*) object
;
228 b
->id
= FcObjectId (o
);
234 FcObjectHashRemove (const FcObjectType
*object
, FcBool cleanobj
)
236 FcChar32 hash
= FcStringHash ((const FcChar8
*) object
->object
);
241 if (!FcObjectsInited
)
243 for (p
= &FcObjectBuckets
[hash
%OBJECT_HASH_SIZE
]; (b
= *p
); p
= &(b
->next
))
245 o
= FcObjects
+ b
->id
- 1;
246 if (b
->hash
== hash
&& !strcmp (object
->object
, o
->object
))
252 /* Clean up object array */
255 while (FcObjects
[FcObjectsNumber
-1].object
== NULL
)
264 FcNameRegisterObjectTypes (const FcObjectType
*types
, int ntypes
)
268 for (i
= 0; i
< ntypes
; i
++)
269 if (!FcObjectHashInsert (&types
[i
], FcTrue
))
275 FcNameUnregisterObjectTypes (const FcObjectType
*types
, int ntypes
)
279 for (i
= 0; i
< ntypes
; i
++)
280 FcObjectHashRemove (&types
[i
], FcTrue
);
285 FcNameGetObjectType (const char *object
)
287 return FcObjectFindByName (object
, FcFalse
);
291 FcObjectValidType (FcObject object
, FcType type
)
293 FcObjectType
*t
= FcObjectFindById (object
);
301 if (type
== FcTypeDouble
|| type
== FcTypeInteger
)
315 FcObjectFromName (const char * name
)
317 FcObjectType
*o
= FcObjectFindByName (name
, FcTrue
);
320 return FcObjectId (o
);
332 FcObjectsInited
= FcTrue
;
333 for (i
= 0; i
< NUM_OBJECT_TYPES
; i
++)
334 if (!FcObjectHashInsert (&_FcBaseObjectTypes
[i
], FcFalse
))
343 FcObjectBucket
*b
, *next
;
345 for (i
= 0; i
< OBJECT_HASH_SIZE
; i
++)
347 for (b
= FcObjectBuckets
[i
]; b
; b
= next
)
352 FcObjectBuckets
[i
] = 0;
354 for (i
= 0; i
< FcObjectsNumber
; i
++)
355 if (FcObjects
[i
].type
== -1)
356 free ((void*) FcObjects
[i
].object
);
357 if (FcObjects
!= _FcBaseObjectTypes
)
359 FcObjects
= (FcObjectType
*) _FcBaseObjectTypes
;
360 FcObjectsNumber
= NUM_OBJECT_TYPES
;
362 FcObjectsInited
= FcFalse
;
366 FcObjectName (FcObject object
)
368 FcObjectType
*o
= FcObjectFindById (object
);
375 static const FcConstant _FcBaseConstants
[] = {
376 { (FcChar8
*) "thin", "weight", FC_WEIGHT_THIN
, },
377 { (FcChar8
*) "extralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
378 { (FcChar8
*) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
379 { (FcChar8
*) "light", "weight", FC_WEIGHT_LIGHT
, },
380 { (FcChar8
*) "book", "weight", FC_WEIGHT_BOOK
, },
381 { (FcChar8
*) "regular", "weight", FC_WEIGHT_REGULAR
, },
382 { (FcChar8
*) "medium", "weight", FC_WEIGHT_MEDIUM
, },
383 { (FcChar8
*) "demibold", "weight", FC_WEIGHT_DEMIBOLD
, },
384 { (FcChar8
*) "semibold", "weight", FC_WEIGHT_DEMIBOLD
, },
385 { (FcChar8
*) "bold", "weight", FC_WEIGHT_BOLD
, },
386 { (FcChar8
*) "extrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
387 { (FcChar8
*) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
388 { (FcChar8
*) "black", "weight", FC_WEIGHT_BLACK
, },
390 { (FcChar8
*) "roman", "slant", FC_SLANT_ROMAN
, },
391 { (FcChar8
*) "italic", "slant", FC_SLANT_ITALIC
, },
392 { (FcChar8
*) "oblique", "slant", FC_SLANT_OBLIQUE
, },
394 { (FcChar8
*) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED
},
395 { (FcChar8
*) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED
},
396 { (FcChar8
*) "condensed", "width", FC_WIDTH_CONDENSED
},
397 { (FcChar8
*) "semicondensed", "width", FC_WIDTH_SEMICONDENSED
},
398 { (FcChar8
*) "normal", "width", FC_WIDTH_NORMAL
},
399 { (FcChar8
*) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED
},
400 { (FcChar8
*) "expanded", "width", FC_WIDTH_EXPANDED
},
401 { (FcChar8
*) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED
},
402 { (FcChar8
*) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED
},
404 { (FcChar8
*) "proportional", "spacing", FC_PROPORTIONAL
, },
405 { (FcChar8
*) "dual", "spacing", FC_DUAL
, },
406 { (FcChar8
*) "mono", "spacing", FC_MONO
, },
407 { (FcChar8
*) "charcell", "spacing", FC_CHARCELL
, },
409 { (FcChar8
*) "unknown", "rgba", FC_RGBA_UNKNOWN
},
410 { (FcChar8
*) "rgb", "rgba", FC_RGBA_RGB
, },
411 { (FcChar8
*) "bgr", "rgba", FC_RGBA_BGR
, },
412 { (FcChar8
*) "vrgb", "rgba", FC_RGBA_VRGB
},
413 { (FcChar8
*) "vbgr", "rgba", FC_RGBA_VBGR
},
414 { (FcChar8
*) "none", "rgba", FC_RGBA_NONE
},
416 { (FcChar8
*) "hintnone", "hintstyle", FC_HINT_NONE
},
417 { (FcChar8
*) "hintslight", "hintstyle", FC_HINT_SLIGHT
},
418 { (FcChar8
*) "hintmedium", "hintstyle", FC_HINT_MEDIUM
},
419 { (FcChar8
*) "hintfull", "hintstyle", FC_HINT_FULL
},
422 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
424 typedef struct _FcConstantList FcConstantList
;
426 struct _FcConstantList
{
427 const FcConstantList
*next
;
428 const FcConstant
*consts
;
432 static const FcConstantList _FcBaseConstantList
= {
438 static const FcConstantList
*_FcConstants
= &_FcBaseConstantList
;
441 FcNameRegisterConstants (const FcConstant
*consts
, int nconsts
)
445 l
= (FcConstantList
*) malloc (sizeof (FcConstantList
));
448 FcMemAlloc (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
450 l
->nconsts
= nconsts
;
451 l
->next
= _FcConstants
;
457 FcNameUnregisterConstants (const FcConstant
*consts
, int nconsts
)
459 const FcConstantList
*l
, **prev
;
461 for (prev
= &_FcConstants
;
463 prev
= (const FcConstantList
**) &(l
->next
))
465 if (l
->consts
== consts
&& l
->nconsts
== nconsts
)
468 FcMemFree (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
477 FcNameGetConstant (FcChar8
*string
)
479 const FcConstantList
*l
;
482 for (l
= _FcConstants
; l
; l
= l
->next
)
484 for (i
= 0; i
< l
->nconsts
; i
++)
485 if (!FcStrCmpIgnoreCase (string
, l
->consts
[i
].name
))
486 return &l
->consts
[i
];
492 FcNameConstant (FcChar8
*string
, int *result
)
496 if ((c
= FcNameGetConstant(string
)))
505 FcNameBool (const FcChar8
*v
, FcBool
*result
)
511 if (c0
== 't' || c0
== 'y' || c0
== '1')
516 if (c0
== 'f' || c0
== 'n' || c0
== '0')
540 FcNameConvert (FcType type
, FcChar8
*string
, FcMatrix
*m
)
547 if (!FcNameConstant (string
, &v
.u
.i
))
548 v
.u
.i
= atoi ((char *) string
);
551 v
.u
.s
= FcStrStaticName(string
);
554 if (!FcNameBool (string
, &v
.u
.b
))
558 v
.u
.d
= strtod ((char *) string
, 0);
562 sscanf ((char *) string
, "%lg %lg %lg %lg", &m
->xx
, &m
->xy
, &m
->yx
, &m
->yy
);
565 v
.u
.c
= FcNameParseCharSet (string
);
568 v
.u
.l
= FcNameParseLangSet (string
);
576 static const FcChar8
*
577 FcNameFindNext (const FcChar8
*cur
, const char *delim
, FcChar8
*save
, FcChar8
*last
)
589 else if (strchr (delim
, c
))
602 FcNameParse (const FcChar8
*name
)
611 const FcObjectType
*t
;
615 save
= malloc (strlen ((char *) name
) + 1);
618 pat
= FcPatternCreate ();
624 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
627 if (!FcPatternAddString (pat
, FC_FAMILY
, save
))
637 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
638 d
= strtod ((char *) save
, (char **) &e
);
641 if (!FcPatternAddDouble (pat
, FC_SIZE
, d
))
650 name
= FcNameFindNext (name
, "=_:", save
, &delim
);
653 if (delim
== '=' || delim
== '_')
655 t
= FcNameGetObjectType ((char *) save
);
658 name
= FcNameFindNext (name
, ":,", save
, &delim
);
659 if (t
&& strcmp (t
->object
, _FcBaseObjectTypes
[0].object
))
661 v
= FcNameConvert (t
->type
, save
, &m
);
662 if (!FcPatternAdd (pat
, t
->object
, v
, FcTrue
))
666 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
669 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
678 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
681 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
693 if ((c
= FcNameGetConstant (save
)))
695 if (!FcPatternAddInteger (pat
, c
->object
, c
->value
))
706 FcPatternDestroy (pat
);
713 FcNameUnparseString (FcStrBuf
*buf
,
714 const FcChar8
*string
,
715 const FcChar8
*escape
)
718 while ((c
= *string
++))
720 if (escape
&& strchr ((char *) escape
, (char) c
))
722 if (!FcStrBufChar (buf
, escape
[0]))
725 if (!FcStrBufChar (buf
, c
))
732 FcNameUnparseValue (FcStrBuf
*buf
,
737 FcValue v
= FcValueCanonicalize(v0
);
743 sprintf ((char *) temp
, "%d", v
.u
.i
);
744 return FcNameUnparseString (buf
, temp
, 0);
746 sprintf ((char *) temp
, "%g", v
.u
.d
);
747 return FcNameUnparseString (buf
, temp
, 0);
749 return FcNameUnparseString (buf
, v
.u
.s
, escape
);
751 return FcNameUnparseString (buf
, v
.u
.b
? (FcChar8
*) "True" : (FcChar8
*) "False", 0);
753 sprintf ((char *) temp
, "%g %g %g %g",
754 v
.u
.m
->xx
, v
.u
.m
->xy
, v
.u
.m
->yx
, v
.u
.m
->yy
);
755 return FcNameUnparseString (buf
, temp
, 0);
757 return FcNameUnparseCharSet (buf
, v
.u
.c
);
759 return FcNameUnparseLangSet (buf
, v
.u
.l
);
767 FcNameUnparseValueList (FcStrBuf
*buf
,
773 if (!FcNameUnparseValue (buf
, &v
->value
, escape
))
775 if ((v
= FcValueListNext(v
)) != NULL
)
776 if (!FcNameUnparseString (buf
, (FcChar8
*) ",", 0))
782 #define FC_ESCAPE_FIXED "\\-:,"
783 #define FC_ESCAPE_VARIABLE "\\=_:,"
786 FcNameUnparse (FcPattern
*pat
)
788 return FcNameUnparseEscaped (pat
, FcTrue
);
792 FcNameUnparseEscaped (FcPattern
*pat
, FcBool escape
)
795 FcChar8 buf_static
[8192];
798 const FcObjectTypeList
*l
;
799 const FcObjectType
*o
;
801 FcStrBufInit (&buf
, buf_static
, sizeof (buf_static
));
802 e
= FcPatternObjectFindElt (pat
, FC_FAMILY_OBJECT
);
805 if (!FcNameUnparseValueList (&buf
, FcPatternEltValues(e
), escape
? (FcChar8
*) FC_ESCAPE_FIXED
: 0))
808 e
= FcPatternObjectFindElt (pat
, FC_SIZE_OBJECT
);
811 if (!FcNameUnparseString (&buf
, (FcChar8
*) "-", 0))
813 if (!FcNameUnparseValueList (&buf
, FcPatternEltValues(e
), escape
? (FcChar8
*) FC_ESCAPE_FIXED
: 0))
816 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
818 for (i
= 0; i
< l
->ntypes
; i
++)
821 if (!strcmp (o
->object
, FC_FAMILY
) ||
822 !strcmp (o
->object
, FC_SIZE
) ||
823 !strcmp (o
->object
, FC_FILE
))
826 e
= FcPatternObjectFindElt (pat
, FcObjectFromName (o
->object
));
829 if (!FcNameUnparseString (&buf
, (FcChar8
*) ":", 0))
831 if (!FcNameUnparseString (&buf
, (FcChar8
*) o
->object
, escape
? (FcChar8
*) FC_ESCAPE_VARIABLE
: 0))
833 if (!FcNameUnparseString (&buf
, (FcChar8
*) "=", 0))
835 if (!FcNameUnparseValueList (&buf
, FcPatternEltValues(e
), escape
?
836 (FcChar8
*) FC_ESCAPE_VARIABLE
: 0))
841 return FcStrBufDone (&buf
);
843 FcStrBufDestroy (&buf
);