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 /* Please do not revoke any of these bindings. */
32 static const FcObjectType _FcBaseObjectTypes
[] = {
33 { FC_FAMILY
, FcTypeString
, },
34 { FC_FAMILYLANG
, FcTypeString
, },
35 { FC_STYLE
, FcTypeString
, },
36 { FC_STYLELANG
, FcTypeString
, },
37 { FC_FULLNAME
, FcTypeString
, },
38 { FC_FULLNAMELANG
, FcTypeString
, },
39 { FC_SLANT
, FcTypeInteger
, },
40 { FC_WEIGHT
, FcTypeInteger
, },
41 { FC_WIDTH
, FcTypeInteger
, },
42 { FC_SIZE
, FcTypeDouble
, },
43 { FC_ASPECT
, FcTypeDouble
, },
44 { FC_PIXEL_SIZE
, FcTypeDouble
, },
45 { FC_SPACING
, FcTypeInteger
, },
46 { FC_FOUNDRY
, FcTypeString
, },
47 /* { FC_CORE, FcTypeBool, }, */
48 { FC_ANTIALIAS
, FcTypeBool
, },
49 { FC_HINT_STYLE
, FcTypeInteger
, },
50 { FC_HINTING
, FcTypeBool
, },
51 { FC_VERTICAL_LAYOUT
, FcTypeBool
, },
52 { FC_AUTOHINT
, FcTypeBool
, },
53 { FC_GLOBAL_ADVANCE
, FcTypeBool
, },
54 /* { FC_XLFD, FcTypeString, }, */
55 { FC_FILE
, FcTypeString
, },
56 { FC_INDEX
, FcTypeInteger
, },
57 { FC_RASTERIZER
, FcTypeString
, },
58 { FC_OUTLINE
, FcTypeBool
, },
59 { FC_SCALABLE
, FcTypeBool
, },
60 { FC_DPI
, FcTypeDouble
},
61 { FC_RGBA
, FcTypeInteger
, },
62 { FC_SCALE
, FcTypeDouble
, },
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
},
73 { FC_RENDER
, FcTypeBool
, },
76 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
78 typedef struct _FcObjectTypeList FcObjectTypeList
;
80 struct _FcObjectTypeList
{
81 const FcObjectTypeList
*next
;
82 const FcObjectType
*types
;
86 static const FcObjectTypeList _FcBaseObjectTypesList
= {
92 static const FcObjectTypeList
*_FcObjectTypes
= &_FcBaseObjectTypesList
;
95 FcNameRegisterObjectTypes (const FcObjectType
*types
, int ntypes
)
99 l
= (FcObjectTypeList
*) malloc (sizeof (FcObjectTypeList
));
102 FcMemAlloc (FC_MEM_OBJECTTYPE
, sizeof (FcObjectTypeList
));
105 l
->next
= _FcObjectTypes
;
111 FcNameUnregisterObjectTypesFree (const FcObjectType
*types
, int ntypes
,
114 const FcObjectTypeList
*l
, **prev
;
116 for (prev
= &_FcObjectTypes
;
118 prev
= (const FcObjectTypeList
**) &(l
->next
))
120 if (l
->types
== types
&& l
->ntypes
== ntypes
)
124 FcMemFree (FC_MEM_OBJECTTYPE
, sizeof (FcObjectTypeList
));
134 FcNameUnregisterObjectTypes (const FcObjectType
*types
, int ntypes
)
136 return FcNameUnregisterObjectTypesFree (types
, ntypes
, FcTrue
);
140 FcNameGetObjectType (const char *object
)
143 const FcObjectTypeList
*l
;
144 const FcObjectType
*t
;
146 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
148 for (i
= 0; i
< l
->ntypes
; i
++)
151 if (!strcmp (object
, t
->object
))
159 FcObjectToPtrLookup (const char * object
)
162 const FcObjectTypeList
*l
;
163 const FcObjectType
*t
;
165 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
167 for (i
= 0; i
< l
->ntypes
; i
++)
170 if (!strcmp (object
, t
->object
))
177 #define OBJECT_HASH_SIZE 31
178 struct objectBucket
{
179 struct objectBucket
*next
;
183 static struct objectBucket
*FcObjectBuckets
[OBJECT_HASH_SIZE
];
185 static const FcObjectType
*biggest_known_types
= _FcBaseObjectTypes
;
186 static FcBool allocated_biggest_known_types
;
187 static int biggest_known_ntypes
= NUM_OBJECT_TYPES
;
188 static int biggest_known_count
= 0;
189 static char * biggest_ptr
;
192 FcObjectToPtr (const char * name
)
194 FcChar32 hash
= FcStringHash ((const FcChar8
*) name
);
195 struct objectBucket
**p
;
196 struct objectBucket
*b
;
199 for (p
= &FcObjectBuckets
[hash
% OBJECT_HASH_SIZE
]; (b
= *p
); p
= &(b
->next
)
201 if (b
->hash
== hash
&& !strcmp (name
, (char *) (b
+ 1)))
203 size
= sizeof (struct objectBucket
) + strlen (name
) + 1;
205 FcMemAlloc (FC_MEM_STATICSTR
, size
);
210 b
->id
= FcObjectToPtrLookup (name
);
211 strcpy ((char *) (b
+ 1), name
);
217 FcObjectStaticNameFini (void)
220 struct objectBucket
*b
, *next
;
223 for (i
= 0; i
< OBJECT_HASH_SIZE
; i
++)
225 for (b
= FcObjectBuckets
[i
]; b
; b
= next
)
228 name
= (char *) (b
+ 1);
229 size
= sizeof (struct objectBucket
) + strlen (name
) + 1;
230 FcMemFree (FC_MEM_STATICSTR
, size
);
233 FcObjectBuckets
[i
] = 0;
238 FcObjectPtrU (FcObjectPtr si
)
240 return biggest_known_types
[si
].object
;
244 FcObjectNeededBytes ()
247 for (i
= 0; i
< biggest_known_ntypes
; i
++)
249 const char * t
= biggest_known_types
[i
].object
;
250 num
= num
+ strlen(t
) + 1;
252 biggest_known_count
= num
;
253 return num
+ sizeof(int);
257 FcObjectDistributeBytes (FcCache
* metadata
, void * block_ptr
)
259 *(int *)block_ptr
= biggest_known_ntypes
;
260 block_ptr
= (int *) block_ptr
+ 1;
261 biggest_ptr
= block_ptr
;
262 block_ptr
= (char *) block_ptr
+ biggest_known_count
;
270 for (i
= 0; i
< biggest_known_ntypes
; i
++)
272 const char * t
= biggest_known_types
[i
].object
;
273 strcpy (biggest_ptr
, t
);
274 biggest_ptr
= biggest_ptr
+ strlen(t
) + 1;
279 FcObjectUnserialize (FcCache metadata
, void *block_ptr
)
282 new_biggest
= *(int *)block_ptr
;
283 block_ptr
= (int *) block_ptr
+ 1;
284 if (biggest_known_ntypes
< new_biggest
)
287 char * bp
= (char *)block_ptr
;
289 FcObjectTypeList
* bnl
;
291 bn
= malloc (sizeof (const FcObjectType
) * (new_biggest
+ 1));
295 bnl
= malloc (sizeof (FcObjectTypeList
));
302 for (i
= 0; i
< new_biggest
; i
++)
304 const FcObjectType
* t
= FcNameGetObjectType(bp
);
306 bn
[i
].type
= t
->type
;
308 bn
[i
].type
= FcTypeVoid
;
310 bp
= bp
+ strlen(bp
) + 1;
313 FcNameUnregisterObjectTypesFree (biggest_known_types
, biggest_known_ntypes
, FcFalse
);
314 if (allocated_biggest_known_types
)
316 free ((FcObjectTypeList
*)biggest_known_types
);
319 allocated_biggest_known_types
= FcTrue
;
321 FcNameRegisterObjectTypes (bn
, new_biggest
);
322 biggest_known_ntypes
= new_biggest
;
323 biggest_known_types
= (const FcObjectType
*)bn
;
325 block_ptr
= (char *) block_ptr
+ biggest_known_count
;
330 FcObjectPtrCompare (const FcObjectPtr a
, const FcObjectPtr b
)
335 static const FcConstant _FcBaseConstants
[] = {
336 { (FcChar8
*) "thin", "weight", FC_WEIGHT_THIN
, },
337 { (FcChar8
*) "extralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
338 { (FcChar8
*) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
339 { (FcChar8
*) "light", "weight", FC_WEIGHT_LIGHT
, },
340 { (FcChar8
*) "book", "weight", FC_WEIGHT_BOOK
, },
341 { (FcChar8
*) "regular", "weight", FC_WEIGHT_REGULAR
, },
342 { (FcChar8
*) "medium", "weight", FC_WEIGHT_MEDIUM
, },
343 { (FcChar8
*) "demibold", "weight", FC_WEIGHT_DEMIBOLD
, },
344 { (FcChar8
*) "semibold", "weight", FC_WEIGHT_DEMIBOLD
, },
345 { (FcChar8
*) "bold", "weight", FC_WEIGHT_BOLD
, },
346 { (FcChar8
*) "extrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
347 { (FcChar8
*) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
348 { (FcChar8
*) "black", "weight", FC_WEIGHT_BLACK
, },
350 { (FcChar8
*) "roman", "slant", FC_SLANT_ROMAN
, },
351 { (FcChar8
*) "italic", "slant", FC_SLANT_ITALIC
, },
352 { (FcChar8
*) "oblique", "slant", FC_SLANT_OBLIQUE
, },
354 { (FcChar8
*) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED
},
355 { (FcChar8
*) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED
},
356 { (FcChar8
*) "condensed", "width", FC_WIDTH_CONDENSED
},
357 { (FcChar8
*) "semicondensed", "width", FC_WIDTH_SEMICONDENSED
},
358 { (FcChar8
*) "normal", "width", FC_WIDTH_NORMAL
},
359 { (FcChar8
*) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED
},
360 { (FcChar8
*) "expanded", "width", FC_WIDTH_EXPANDED
},
361 { (FcChar8
*) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED
},
362 { (FcChar8
*) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED
},
364 { (FcChar8
*) "proportional", "spacing", FC_PROPORTIONAL
, },
365 { (FcChar8
*) "dual", "spacing", FC_DUAL
, },
366 { (FcChar8
*) "mono", "spacing", FC_MONO
, },
367 { (FcChar8
*) "charcell", "spacing", FC_CHARCELL
, },
369 { (FcChar8
*) "unknown", "rgba", FC_RGBA_UNKNOWN
},
370 { (FcChar8
*) "rgb", "rgba", FC_RGBA_RGB
, },
371 { (FcChar8
*) "bgr", "rgba", FC_RGBA_BGR
, },
372 { (FcChar8
*) "vrgb", "rgba", FC_RGBA_VRGB
},
373 { (FcChar8
*) "vbgr", "rgba", FC_RGBA_VBGR
},
374 { (FcChar8
*) "none", "rgba", FC_RGBA_NONE
},
376 { (FcChar8
*) "hintnone", "hintstyle", FC_HINT_NONE
},
377 { (FcChar8
*) "hintslight", "hintstyle", FC_HINT_SLIGHT
},
378 { (FcChar8
*) "hintmedium", "hintstyle", FC_HINT_MEDIUM
},
379 { (FcChar8
*) "hintfull", "hintstyle", FC_HINT_FULL
},
382 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
384 typedef struct _FcConstantList FcConstantList
;
386 struct _FcConstantList
{
387 const FcConstantList
*next
;
388 const FcConstant
*consts
;
392 static const FcConstantList _FcBaseConstantList
= {
398 static const FcConstantList
*_FcConstants
= &_FcBaseConstantList
;
401 FcNameRegisterConstants (const FcConstant
*consts
, int nconsts
)
405 l
= (FcConstantList
*) malloc (sizeof (FcConstantList
));
408 FcMemAlloc (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
410 l
->nconsts
= nconsts
;
411 l
->next
= _FcConstants
;
417 FcNameUnregisterConstants (const FcConstant
*consts
, int nconsts
)
419 const FcConstantList
*l
, **prev
;
421 for (prev
= &_FcConstants
;
423 prev
= (const FcConstantList
**) &(l
->next
))
425 if (l
->consts
== consts
&& l
->nconsts
== nconsts
)
428 FcMemFree (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
437 FcNameGetConstant (FcChar8
*string
)
439 const FcConstantList
*l
;
442 for (l
= _FcConstants
; l
; l
= l
->next
)
444 for (i
= 0; i
< l
->nconsts
; i
++)
445 if (!FcStrCmpIgnoreCase (string
, l
->consts
[i
].name
))
446 return &l
->consts
[i
];
452 FcNameConstant (FcChar8
*string
, int *result
)
456 if ((c
= FcNameGetConstant(string
)))
465 FcNameBool (const FcChar8
*v
, FcBool
*result
)
471 if (c0
== 't' || c0
== 'y' || c0
== '1')
476 if (c0
== 'f' || c0
== 'n' || c0
== '0')
500 FcNameConvert (FcType type
, FcChar8
*string
, FcMatrix
*m
)
507 if (!FcNameConstant (string
, &v
.u
.i
))
508 v
.u
.i
= atoi ((char *) string
);
511 v
.u
.s
= FcStrStaticName(string
);
514 if (!FcNameBool (string
, &v
.u
.b
))
518 v
.u
.d
= strtod ((char *) string
, 0);
522 sscanf ((char *) string
, "%lg %lg %lg %lg", &m
->xx
, &m
->xy
, &m
->yx
, &m
->yy
);
525 v
.u
.c
= FcNameParseCharSet (string
);
528 v
.u
.l
= FcNameParseLangSet (string
);
536 static const FcChar8
*
537 FcNameFindNext (const FcChar8
*cur
, const char *delim
, FcChar8
*save
, FcChar8
*last
)
549 else if (strchr (delim
, c
))
562 FcNameParse (const FcChar8
*name
)
571 const FcObjectType
*t
;
575 save
= malloc (strlen ((char *) name
) + 1);
578 pat
= FcPatternCreate ();
584 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
587 if (!FcPatternAddString (pat
, FC_FAMILY
, save
))
597 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
598 d
= strtod ((char *) save
, (char **) &e
);
601 if (!FcPatternAddDouble (pat
, FC_SIZE
, d
))
610 name
= FcNameFindNext (name
, "=_:", save
, &delim
);
613 if (delim
== '=' || delim
== '_')
615 t
= FcNameGetObjectType ((char *) save
);
618 name
= FcNameFindNext (name
, ":,", save
, &delim
);
621 v
= FcNameConvert (t
->type
, save
, &m
);
622 if (!FcPatternAdd (pat
, t
->object
, v
, FcTrue
))
626 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
629 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
638 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
641 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
653 if ((c
= FcNameGetConstant (save
)))
655 if (!FcPatternAddInteger (pat
, c
->object
, c
->value
))
666 FcPatternDestroy (pat
);
673 FcNameUnparseString (FcStrBuf
*buf
,
674 const FcChar8
*string
,
675 const FcChar8
*escape
)
678 while ((c
= *string
++))
680 if (escape
&& strchr ((char *) escape
, (char) c
))
682 if (!FcStrBufChar (buf
, escape
[0]))
685 if (!FcStrBufChar (buf
, c
))
692 FcNameUnparseValue (FcStrBuf
*buf
,
698 FcValue v
= FcValueCanonicalize(v0
);
704 sprintf ((char *) temp
, "%d", v
.u
.i
);
705 return FcNameUnparseString (buf
, temp
, 0);
707 sprintf ((char *) temp
, "%g", v
.u
.d
);
708 return FcNameUnparseString (buf
, temp
, 0);
710 return FcNameUnparseString (buf
, v
.u
.s
, escape
);
712 return FcNameUnparseString (buf
, v
.u
.b
? (FcChar8
*) "True" : (FcChar8
*) "False", 0);
714 sprintf ((char *) temp
, "%g %g %g %g",
715 v
.u
.m
->xx
, v
.u
.m
->xy
, v
.u
.m
->yx
, v
.u
.m
->yy
);
716 return FcNameUnparseString (buf
, temp
, 0);
718 return FcNameUnparseCharSet (buf
, v
.u
.c
);
720 return FcNameUnparseLangSet (buf
, v
.u
.l
);
728 FcNameUnparseValueList (FcStrBuf
*buf
,
732 while (FcValueListPtrU(v
))
734 if (!FcNameUnparseValue (buf
, v
.bank
, &FcValueListPtrU(v
)->value
, escape
))
736 if (FcValueListPtrU(v
= FcValueListPtrU(v
)->next
))
737 if (!FcNameUnparseString (buf
, (FcChar8
*) ",", 0))
743 #define FC_ESCAPE_FIXED "\\-:,"
744 #define FC_ESCAPE_VARIABLE "\\=_:,"
747 FcNameUnparse (FcPattern
*pat
)
750 FcChar8 buf_static
[8192];
753 const FcObjectTypeList
*l
;
754 const FcObjectType
*o
;
756 FcStrBufInit (&buf
, buf_static
, sizeof (buf_static
));
757 e
= FcPatternFindElt (pat
, FC_FAMILY
);
760 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
763 e
= FcPatternFindElt (pat
, FC_SIZE
);
766 if (!FcNameUnparseString (&buf
, (FcChar8
*) "-", 0))
768 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
771 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
773 for (i
= 0; i
< l
->ntypes
; i
++)
776 if (!strcmp (o
->object
, FC_FAMILY
) ||
777 !strcmp (o
->object
, FC_SIZE
) ||
778 !strcmp (o
->object
, FC_FILE
))
781 e
= FcPatternFindElt (pat
, o
->object
);
784 if (!FcNameUnparseString (&buf
, (FcChar8
*) ":", 0))
786 if (!FcNameUnparseString (&buf
, (FcChar8
*) o
->object
, (FcChar8
*) FC_ESCAPE_VARIABLE
))
788 if (!FcNameUnparseString (&buf
, (FcChar8
*) "=", 0))
790 if (!FcNameUnparseValueList (&buf
, e
->values
,
791 (FcChar8
*) FC_ESCAPE_VARIABLE
))
796 return FcStrBufDone (&buf
);
798 FcStrBufDestroy (&buf
);