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
)
305 if (type
== FcTypeLangSet
|| type
== FcTypeString
)
319 FcObjectFromName (const char * name
)
321 FcObjectType
*o
= FcObjectFindByName (name
, FcTrue
);
324 return FcObjectId (o
);
336 FcObjectsInited
= FcTrue
;
337 for (i
= 0; i
< NUM_OBJECT_TYPES
; i
++)
338 if (!FcObjectHashInsert (&_FcBaseObjectTypes
[i
], FcFalse
))
347 FcObjectBucket
*b
, *next
;
349 for (i
= 0; i
< OBJECT_HASH_SIZE
; i
++)
351 for (b
= FcObjectBuckets
[i
]; b
; b
= next
)
356 FcObjectBuckets
[i
] = 0;
358 for (i
= 0; i
< FcObjectsNumber
; i
++)
359 if (FcObjects
[i
].type
== -1)
360 free ((void*) FcObjects
[i
].object
);
361 if (FcObjects
!= _FcBaseObjectTypes
)
363 FcObjects
= (FcObjectType
*) _FcBaseObjectTypes
;
364 FcObjectsNumber
= NUM_OBJECT_TYPES
;
366 FcObjectsInited
= FcFalse
;
370 FcObjectName (FcObject object
)
372 FcObjectType
*o
= FcObjectFindById (object
);
379 static const FcConstant _FcBaseConstants
[] = {
380 { (FcChar8
*) "thin", "weight", FC_WEIGHT_THIN
, },
381 { (FcChar8
*) "extralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
382 { (FcChar8
*) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
383 { (FcChar8
*) "light", "weight", FC_WEIGHT_LIGHT
, },
384 { (FcChar8
*) "book", "weight", FC_WEIGHT_BOOK
, },
385 { (FcChar8
*) "regular", "weight", FC_WEIGHT_REGULAR
, },
386 { (FcChar8
*) "medium", "weight", FC_WEIGHT_MEDIUM
, },
387 { (FcChar8
*) "demibold", "weight", FC_WEIGHT_DEMIBOLD
, },
388 { (FcChar8
*) "semibold", "weight", FC_WEIGHT_DEMIBOLD
, },
389 { (FcChar8
*) "bold", "weight", FC_WEIGHT_BOLD
, },
390 { (FcChar8
*) "extrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
391 { (FcChar8
*) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
392 { (FcChar8
*) "black", "weight", FC_WEIGHT_BLACK
, },
394 { (FcChar8
*) "roman", "slant", FC_SLANT_ROMAN
, },
395 { (FcChar8
*) "italic", "slant", FC_SLANT_ITALIC
, },
396 { (FcChar8
*) "oblique", "slant", FC_SLANT_OBLIQUE
, },
398 { (FcChar8
*) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED
},
399 { (FcChar8
*) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED
},
400 { (FcChar8
*) "condensed", "width", FC_WIDTH_CONDENSED
},
401 { (FcChar8
*) "semicondensed", "width", FC_WIDTH_SEMICONDENSED
},
402 { (FcChar8
*) "normal", "width", FC_WIDTH_NORMAL
},
403 { (FcChar8
*) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED
},
404 { (FcChar8
*) "expanded", "width", FC_WIDTH_EXPANDED
},
405 { (FcChar8
*) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED
},
406 { (FcChar8
*) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED
},
408 { (FcChar8
*) "proportional", "spacing", FC_PROPORTIONAL
, },
409 { (FcChar8
*) "dual", "spacing", FC_DUAL
, },
410 { (FcChar8
*) "mono", "spacing", FC_MONO
, },
411 { (FcChar8
*) "charcell", "spacing", FC_CHARCELL
, },
413 { (FcChar8
*) "unknown", "rgba", FC_RGBA_UNKNOWN
},
414 { (FcChar8
*) "rgb", "rgba", FC_RGBA_RGB
, },
415 { (FcChar8
*) "bgr", "rgba", FC_RGBA_BGR
, },
416 { (FcChar8
*) "vrgb", "rgba", FC_RGBA_VRGB
},
417 { (FcChar8
*) "vbgr", "rgba", FC_RGBA_VBGR
},
418 { (FcChar8
*) "none", "rgba", FC_RGBA_NONE
},
420 { (FcChar8
*) "hintnone", "hintstyle", FC_HINT_NONE
},
421 { (FcChar8
*) "hintslight", "hintstyle", FC_HINT_SLIGHT
},
422 { (FcChar8
*) "hintmedium", "hintstyle", FC_HINT_MEDIUM
},
423 { (FcChar8
*) "hintfull", "hintstyle", FC_HINT_FULL
},
426 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
428 typedef struct _FcConstantList FcConstantList
;
430 struct _FcConstantList
{
431 const FcConstantList
*next
;
432 const FcConstant
*consts
;
436 static const FcConstantList _FcBaseConstantList
= {
442 static const FcConstantList
*_FcConstants
= &_FcBaseConstantList
;
445 FcNameRegisterConstants (const FcConstant
*consts
, int nconsts
)
449 l
= (FcConstantList
*) malloc (sizeof (FcConstantList
));
452 FcMemAlloc (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
454 l
->nconsts
= nconsts
;
455 l
->next
= _FcConstants
;
461 FcNameUnregisterConstants (const FcConstant
*consts
, int nconsts
)
463 const FcConstantList
*l
, **prev
;
465 for (prev
= &_FcConstants
;
467 prev
= (const FcConstantList
**) &(l
->next
))
469 if (l
->consts
== consts
&& l
->nconsts
== nconsts
)
472 FcMemFree (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
481 FcNameGetConstant (FcChar8
*string
)
483 const FcConstantList
*l
;
486 for (l
= _FcConstants
; l
; l
= l
->next
)
488 for (i
= 0; i
< l
->nconsts
; i
++)
489 if (!FcStrCmpIgnoreCase (string
, l
->consts
[i
].name
))
490 return &l
->consts
[i
];
496 FcNameConstant (FcChar8
*string
, int *result
)
500 if ((c
= FcNameGetConstant(string
)))
509 FcNameBool (const FcChar8
*v
, FcBool
*result
)
515 if (c0
== 't' || c0
== 'y' || c0
== '1')
520 if (c0
== 'f' || c0
== 'n' || c0
== '0')
544 FcNameConvert (FcType type
, FcChar8
*string
, FcMatrix
*m
)
551 if (!FcNameConstant (string
, &v
.u
.i
))
552 v
.u
.i
= atoi ((char *) string
);
555 v
.u
.s
= FcStrStaticName(string
);
558 if (!FcNameBool (string
, &v
.u
.b
))
562 v
.u
.d
= strtod ((char *) string
, 0);
566 sscanf ((char *) string
, "%lg %lg %lg %lg", &m
->xx
, &m
->xy
, &m
->yx
, &m
->yy
);
569 v
.u
.c
= FcNameParseCharSet (string
);
572 v
.u
.l
= FcNameParseLangSet (string
);
580 static const FcChar8
*
581 FcNameFindNext (const FcChar8
*cur
, const char *delim
, FcChar8
*save
, FcChar8
*last
)
593 else if (strchr (delim
, c
))
606 FcNameParse (const FcChar8
*name
)
615 const FcObjectType
*t
;
619 save
= malloc (strlen ((char *) name
) + 1);
622 pat
= FcPatternCreate ();
628 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
631 if (!FcPatternAddString (pat
, FC_FAMILY
, save
))
641 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
642 d
= strtod ((char *) save
, (char **) &e
);
645 if (!FcPatternAddDouble (pat
, FC_SIZE
, d
))
654 name
= FcNameFindNext (name
, "=_:", save
, &delim
);
657 if (delim
== '=' || delim
== '_')
659 t
= FcNameGetObjectType ((char *) save
);
662 name
= FcNameFindNext (name
, ":,", save
, &delim
);
663 if (t
&& strcmp (t
->object
, _FcBaseObjectTypes
[0].object
))
665 v
= FcNameConvert (t
->type
, save
, &m
);
666 if (!FcPatternAdd (pat
, t
->object
, v
, FcTrue
))
670 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
673 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
682 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
685 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
697 if ((c
= FcNameGetConstant (save
)))
699 if (!FcPatternAddInteger (pat
, c
->object
, c
->value
))
710 FcPatternDestroy (pat
);
717 FcNameUnparseString (FcStrBuf
*buf
,
718 const FcChar8
*string
,
719 const FcChar8
*escape
)
722 while ((c
= *string
++))
724 if (escape
&& strchr ((char *) escape
, (char) c
))
726 if (!FcStrBufChar (buf
, escape
[0]))
729 if (!FcStrBufChar (buf
, c
))
736 FcNameUnparseValue (FcStrBuf
*buf
,
741 FcValue v
= FcValueCanonicalize(v0
);
747 sprintf ((char *) temp
, "%d", v
.u
.i
);
748 return FcNameUnparseString (buf
, temp
, 0);
750 sprintf ((char *) temp
, "%g", v
.u
.d
);
751 return FcNameUnparseString (buf
, temp
, 0);
753 return FcNameUnparseString (buf
, v
.u
.s
, escape
);
755 return FcNameUnparseString (buf
, v
.u
.b
? (FcChar8
*) "True" : (FcChar8
*) "False", 0);
757 sprintf ((char *) temp
, "%g %g %g %g",
758 v
.u
.m
->xx
, v
.u
.m
->xy
, v
.u
.m
->yx
, v
.u
.m
->yy
);
759 return FcNameUnparseString (buf
, temp
, 0);
761 return FcNameUnparseCharSet (buf
, v
.u
.c
);
763 return FcNameUnparseLangSet (buf
, v
.u
.l
);
771 FcNameUnparseValueList (FcStrBuf
*buf
,
777 if (!FcNameUnparseValue (buf
, &v
->value
, escape
))
779 if ((v
= FcValueListNext(v
)) != NULL
)
780 if (!FcNameUnparseString (buf
, (FcChar8
*) ",", 0))
786 #define FC_ESCAPE_FIXED "\\-:,"
787 #define FC_ESCAPE_VARIABLE "\\=_:,"
790 FcNameUnparse (FcPattern
*pat
)
792 return FcNameUnparseEscaped (pat
, FcTrue
);
796 FcNameUnparseEscaped (FcPattern
*pat
, FcBool escape
)
799 FcChar8 buf_static
[8192];
802 const FcObjectTypeList
*l
;
803 const FcObjectType
*o
;
805 FcStrBufInit (&buf
, buf_static
, sizeof (buf_static
));
806 e
= FcPatternObjectFindElt (pat
, FC_FAMILY_OBJECT
);
809 if (!FcNameUnparseValueList (&buf
, FcPatternEltValues(e
), escape
? (FcChar8
*) FC_ESCAPE_FIXED
: 0))
812 e
= FcPatternObjectFindElt (pat
, FC_SIZE_OBJECT
);
815 if (!FcNameUnparseString (&buf
, (FcChar8
*) "-", 0))
817 if (!FcNameUnparseValueList (&buf
, FcPatternEltValues(e
), escape
? (FcChar8
*) FC_ESCAPE_FIXED
: 0))
820 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
822 for (i
= 0; i
< l
->ntypes
; i
++)
825 if (!strcmp (o
->object
, FC_FAMILY
) ||
826 !strcmp (o
->object
, FC_SIZE
) ||
827 !strcmp (o
->object
, FC_FILE
))
830 e
= FcPatternObjectFindElt (pat
, FcObjectFromName (o
->object
));
833 if (!FcNameUnparseString (&buf
, (FcChar8
*) ":", 0))
835 if (!FcNameUnparseString (&buf
, (FcChar8
*) o
->object
, escape
? (FcChar8
*) FC_ESCAPE_VARIABLE
: 0))
837 if (!FcNameUnparseString (&buf
, (FcChar8
*) "=", 0))
839 if (!FcNameUnparseValueList (&buf
, FcPatternEltValues(e
), escape
?
840 (FcChar8
*) FC_ESCAPE_VARIABLE
: 0))
845 return FcStrBufDone (&buf
);
847 FcStrBufDestroy (&buf
);