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_RENDER, FcTypeBool, },*/
65 { FC_CHAR_WIDTH
, FcTypeInteger
},
66 { FC_CHAR_HEIGHT
, FcTypeInteger
},
67 { FC_MATRIX
, FcTypeMatrix
},
68 { FC_CHARSET
, FcTypeCharSet
},
69 { FC_LANG
, FcTypeLangSet
},
70 { FC_FONTVERSION
, FcTypeInteger
},
71 { FC_CAPABILITY
, FcTypeString
},
72 { FC_FONTFORMAT
, FcTypeString
},
73 { FC_EMBOLDEN
, FcTypeBool
},
76 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
78 static FcObjectType
* _FcUserObjectNames
= 0;
79 static int user_obj_alloc
= 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
;
98 FcNameRegisterObjectTypes (const FcObjectType
*types
, int ntypes
)
102 l
= (FcObjectTypeList
*) malloc (sizeof (FcObjectTypeList
));
105 FcMemAlloc (FC_MEM_OBJECTTYPE
, sizeof (FcObjectTypeList
));
108 l
->next
= _FcObjectTypes
;
114 FcNameUnregisterObjectTypesFree (const FcObjectType
*types
, int ntypes
,
117 const FcObjectTypeList
*l
, **prev
;
119 for (prev
= &_FcObjectTypes
;
121 prev
= (const FcObjectTypeList
**) &(l
->next
))
123 if (l
->types
== types
&& l
->ntypes
== ntypes
)
127 FcMemFree (FC_MEM_OBJECTTYPE
, sizeof (FcObjectTypeList
));
137 FcNameUnregisterObjectTypes (const FcObjectType
*types
, int ntypes
)
139 return FcNameUnregisterObjectTypesFree (types
, ntypes
, FcTrue
);
143 FcNameGetObjectType (const char *object
)
146 const FcObjectTypeList
*l
;
147 const FcObjectType
*t
;
149 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
151 for (i
= 0; i
< l
->ntypes
; i
++)
154 if (!strcmp (object
, t
->object
))
161 #define OBJECT_HASH_SIZE 31
162 struct objectBucket
{
163 struct objectBucket
*next
;
167 static struct objectBucket
*FcObjectBuckets
[OBJECT_HASH_SIZE
];
169 static const FcObjectType
*biggest_known_types
= _FcBaseObjectTypes
;
170 static FcBool allocated_biggest_known_types
;
171 static int biggest_known_ntypes
= NUM_OBJECT_TYPES
;
172 static int biggest_known_count
= 0;
173 static char * biggest_ptr
;
177 FcObjectToPtrLookup (const char * object
)
179 FcObjectPtr i
= 0, n
;
180 const FcObjectTypeList
*l
;
181 FcObjectType
*t
= _FcUserObjectNames
;
183 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
185 for (i
= 0; i
< l
->ntypes
; i
++)
187 t
= (FcObjectType
*)&l
->types
[i
];
188 if (!strcmp (object
, t
->object
))
190 if (l
== (FcObjectTypeList
*)_FcUserObjectNames
)
191 return i
+ biggest_known_ntypes
;
198 /* We didn't match. Look for the correct FcObjectTypeList
199 * to replace it in-place. */
200 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
202 if (l
->types
== _FcUserObjectNames
)
206 if (!_FcUserObjectNames
||
207 (l
&& l
->types
== _FcUserObjectNames
&& user_obj_alloc
< l
->ntypes
))
209 int nt
= user_obj_alloc
+ 4;
210 FcObjectType
* t
= realloc (_FcUserObjectNames
,
211 nt
* sizeof (FcObjectType
));
214 _FcUserObjectNames
= t
;
218 if (l
&& l
->types
== _FcUserObjectNames
)
221 FcNameUnregisterObjectTypesFree (l
->types
, l
->ntypes
, FcFalse
);
226 FcNameRegisterObjectTypes (_FcUserObjectNames
, n
+1);
228 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
230 if (l
->types
== _FcUserObjectNames
)
232 t
= (FcObjectType
*)l
->types
;
240 t
[n
].object
= object
;
241 t
[n
].type
= FcTypeVoid
;
243 return n
+biggest_known_ntypes
;
247 FcObjectToPtr (const char * name
)
249 FcChar32 hash
= FcStringHash ((const FcChar8
*) name
);
250 struct objectBucket
**p
;
251 struct objectBucket
*b
;
254 for (p
= &FcObjectBuckets
[hash
% OBJECT_HASH_SIZE
]; (b
= *p
); p
= &(b
->next
)
256 if (b
->hash
== hash
&& !strcmp (name
, (char *) (b
+ 1)))
258 size
= sizeof (struct objectBucket
) + strlen (name
) + 1;
260 FcMemAlloc (FC_MEM_STATICSTR
, size
);
265 b
->id
= FcObjectToPtrLookup (name
);
266 strcpy ((char *) (b
+ 1), name
);
272 FcObjectStaticNameFini (void)
275 struct objectBucket
*b
, *next
;
278 for (i
= 0; i
< OBJECT_HASH_SIZE
; i
++)
280 for (b
= FcObjectBuckets
[i
]; b
; b
= next
)
283 name
= (char *) (b
+ 1);
284 size
= sizeof (struct objectBucket
) + strlen (name
) + 1;
285 FcMemFree (FC_MEM_STATICSTR
, size
);
288 FcObjectBuckets
[i
] = 0;
293 FcObjectPtrU (FcObjectPtr si
)
295 if (si
< biggest_known_ntypes
)
296 return biggest_known_types
[si
].object
;
298 return _FcUserObjectNames
[si
-biggest_known_ntypes
].object
;
302 FcObjectNeededBytes ()
305 for (i
= 0; i
< biggest_known_ntypes
; i
++)
307 const char * t
= biggest_known_types
[i
].object
;
308 num
= num
+ strlen(t
) + 1;
310 biggest_known_count
= num
;
311 return num
+ sizeof(int);
315 FcObjectDistributeBytes (FcCache
* metadata
, void * block_ptr
)
317 *(int *)block_ptr
= biggest_known_ntypes
;
318 block_ptr
= (int *) block_ptr
+ 1;
319 biggest_ptr
= block_ptr
;
320 block_ptr
= (char *) block_ptr
+ biggest_known_count
;
328 for (i
= 0; i
< biggest_known_ntypes
; i
++)
330 const char * t
= biggest_known_types
[i
].object
;
331 strcpy (biggest_ptr
, t
);
332 biggest_ptr
= biggest_ptr
+ strlen(t
) + 1;
337 FcObjectUnserialize (FcCache metadata
, void *block_ptr
)
340 new_biggest
= *(int *)block_ptr
;
341 block_ptr
= (int *) block_ptr
+ 1;
342 if (biggest_known_ntypes
< new_biggest
)
345 char * bp
= (char *)block_ptr
;
347 FcObjectTypeList
* bnl
;
349 bn
= malloc (sizeof (const FcObjectType
) * (new_biggest
+ 1));
353 bnl
= malloc (sizeof (FcObjectTypeList
));
360 for (i
= 0; i
< new_biggest
; i
++)
362 const FcObjectType
* t
= FcNameGetObjectType(bp
);
364 bn
[i
].type
= t
->type
;
366 bn
[i
].type
= FcTypeVoid
;
368 bp
= bp
+ strlen(bp
) + 1;
371 FcNameUnregisterObjectTypesFree (biggest_known_types
, biggest_known_ntypes
, FcFalse
);
372 if (allocated_biggest_known_types
)
374 free ((FcObjectTypeList
*)biggest_known_types
);
377 allocated_biggest_known_types
= FcTrue
;
379 FcNameRegisterObjectTypes (bn
, new_biggest
);
380 biggest_known_ntypes
= new_biggest
;
381 biggest_known_types
= (const FcObjectType
*)bn
;
383 block_ptr
= (char *) block_ptr
+ biggest_known_count
;
388 FcObjectPtrCompare (const FcObjectPtr a
, const FcObjectPtr b
)
393 static const FcConstant _FcBaseConstants
[] = {
394 { (FcChar8
*) "thin", "weight", FC_WEIGHT_THIN
, },
395 { (FcChar8
*) "extralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
396 { (FcChar8
*) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
397 { (FcChar8
*) "light", "weight", FC_WEIGHT_LIGHT
, },
398 { (FcChar8
*) "book", "weight", FC_WEIGHT_BOOK
, },
399 { (FcChar8
*) "regular", "weight", FC_WEIGHT_REGULAR
, },
400 { (FcChar8
*) "medium", "weight", FC_WEIGHT_MEDIUM
, },
401 { (FcChar8
*) "demibold", "weight", FC_WEIGHT_DEMIBOLD
, },
402 { (FcChar8
*) "semibold", "weight", FC_WEIGHT_DEMIBOLD
, },
403 { (FcChar8
*) "bold", "weight", FC_WEIGHT_BOLD
, },
404 { (FcChar8
*) "extrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
405 { (FcChar8
*) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
406 { (FcChar8
*) "black", "weight", FC_WEIGHT_BLACK
, },
408 { (FcChar8
*) "roman", "slant", FC_SLANT_ROMAN
, },
409 { (FcChar8
*) "italic", "slant", FC_SLANT_ITALIC
, },
410 { (FcChar8
*) "oblique", "slant", FC_SLANT_OBLIQUE
, },
412 { (FcChar8
*) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED
},
413 { (FcChar8
*) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED
},
414 { (FcChar8
*) "condensed", "width", FC_WIDTH_CONDENSED
},
415 { (FcChar8
*) "semicondensed", "width", FC_WIDTH_SEMICONDENSED
},
416 { (FcChar8
*) "normal", "width", FC_WIDTH_NORMAL
},
417 { (FcChar8
*) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED
},
418 { (FcChar8
*) "expanded", "width", FC_WIDTH_EXPANDED
},
419 { (FcChar8
*) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED
},
420 { (FcChar8
*) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED
},
422 { (FcChar8
*) "proportional", "spacing", FC_PROPORTIONAL
, },
423 { (FcChar8
*) "dual", "spacing", FC_DUAL
, },
424 { (FcChar8
*) "mono", "spacing", FC_MONO
, },
425 { (FcChar8
*) "charcell", "spacing", FC_CHARCELL
, },
427 { (FcChar8
*) "unknown", "rgba", FC_RGBA_UNKNOWN
},
428 { (FcChar8
*) "rgb", "rgba", FC_RGBA_RGB
, },
429 { (FcChar8
*) "bgr", "rgba", FC_RGBA_BGR
, },
430 { (FcChar8
*) "vrgb", "rgba", FC_RGBA_VRGB
},
431 { (FcChar8
*) "vbgr", "rgba", FC_RGBA_VBGR
},
432 { (FcChar8
*) "none", "rgba", FC_RGBA_NONE
},
434 { (FcChar8
*) "hintnone", "hintstyle", FC_HINT_NONE
},
435 { (FcChar8
*) "hintslight", "hintstyle", FC_HINT_SLIGHT
},
436 { (FcChar8
*) "hintmedium", "hintstyle", FC_HINT_MEDIUM
},
437 { (FcChar8
*) "hintfull", "hintstyle", FC_HINT_FULL
},
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
);
572 if (!FcNameBool (string
, &v
.u
.b
))
576 v
.u
.d
= strtod ((char *) string
, 0);
580 sscanf ((char *) string
, "%lg %lg %lg %lg", &m
->xx
, &m
->xy
, &m
->yx
, &m
->yy
);
583 v
.u
.c
= FcNameParseCharSet (string
);
586 v
.u
.l
= FcNameParseLangSet (string
);
594 static const FcChar8
*
595 FcNameFindNext (const FcChar8
*cur
, const char *delim
, FcChar8
*save
, FcChar8
*last
)
607 else if (strchr (delim
, c
))
620 FcNameParse (const FcChar8
*name
)
629 const FcObjectType
*t
;
633 save
= malloc (strlen ((char *) name
) + 1);
636 pat
= FcPatternCreate ();
642 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
645 if (!FcPatternAddString (pat
, FC_FAMILY
, save
))
655 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
656 d
= strtod ((char *) save
, (char **) &e
);
659 if (!FcPatternAddDouble (pat
, FC_SIZE
, d
))
668 name
= FcNameFindNext (name
, "=_:", save
, &delim
);
671 if (delim
== '=' || delim
== '_')
673 t
= FcNameGetObjectType ((char *) save
);
676 name
= FcNameFindNext (name
, ":,", save
, &delim
);
679 v
= FcNameConvert (t
->type
, save
, &m
);
680 if (!FcPatternAdd (pat
, t
->object
, v
, FcTrue
))
684 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
687 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
696 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
699 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
711 if ((c
= FcNameGetConstant (save
)))
713 if (!FcPatternAddInteger (pat
, c
->object
, c
->value
))
724 FcPatternDestroy (pat
);
731 FcNameUnparseString (FcStrBuf
*buf
,
732 const FcChar8
*string
,
733 const FcChar8
*escape
)
736 while ((c
= *string
++))
738 if (escape
&& strchr ((char *) escape
, (char) c
))
740 if (!FcStrBufChar (buf
, escape
[0]))
743 if (!FcStrBufChar (buf
, c
))
750 FcNameUnparseValue (FcStrBuf
*buf
,
756 FcValue v
= FcValueCanonicalize(v0
);
762 sprintf ((char *) temp
, "%d", v
.u
.i
);
763 return FcNameUnparseString (buf
, temp
, 0);
765 sprintf ((char *) temp
, "%g", v
.u
.d
);
766 return FcNameUnparseString (buf
, temp
, 0);
768 return FcNameUnparseString (buf
, v
.u
.s
, escape
);
770 return FcNameUnparseString (buf
, v
.u
.b
? (FcChar8
*) "True" : (FcChar8
*) "False", 0);
772 sprintf ((char *) temp
, "%g %g %g %g",
773 v
.u
.m
->xx
, v
.u
.m
->xy
, v
.u
.m
->yx
, v
.u
.m
->yy
);
774 return FcNameUnparseString (buf
, temp
, 0);
776 return FcNameUnparseCharSet (buf
, v
.u
.c
);
778 return FcNameUnparseLangSet (buf
, v
.u
.l
);
786 FcNameUnparseValueList (FcStrBuf
*buf
,
790 while (FcValueListPtrU(v
))
792 if (!FcNameUnparseValue (buf
, v
.bank
, &FcValueListPtrU(v
)->value
, escape
))
794 if (FcValueListPtrU(v
= FcValueListPtrU(v
)->next
))
795 if (!FcNameUnparseString (buf
, (FcChar8
*) ",", 0))
801 #define FC_ESCAPE_FIXED "\\-:,"
802 #define FC_ESCAPE_VARIABLE "\\=_:,"
805 FcNameUnparse (FcPattern
*pat
)
808 FcChar8 buf_static
[8192];
811 const FcObjectTypeList
*l
;
812 const FcObjectType
*o
;
814 FcStrBufInit (&buf
, buf_static
, sizeof (buf_static
));
815 e
= FcPatternFindElt (pat
, FC_FAMILY
);
818 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
821 e
= FcPatternFindElt (pat
, FC_SIZE
);
824 if (!FcNameUnparseString (&buf
, (FcChar8
*) "-", 0))
826 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
829 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
831 for (i
= 0; i
< l
->ntypes
; i
++)
834 if (!strcmp (o
->object
, FC_FAMILY
) ||
835 !strcmp (o
->object
, FC_SIZE
) ||
836 !strcmp (o
->object
, FC_FILE
))
839 e
= FcPatternFindElt (pat
, o
->object
);
842 if (!FcNameUnparseString (&buf
, (FcChar8
*) ":", 0))
844 if (!FcNameUnparseString (&buf
, (FcChar8
*) o
->object
, (FcChar8
*) FC_ESCAPE_VARIABLE
))
846 if (!FcNameUnparseString (&buf
, (FcChar8
*) "=", 0))
848 if (!FcNameUnparseValueList (&buf
, e
->values
,
849 (FcChar8
*) FC_ESCAPE_VARIABLE
))
854 return FcStrBufDone (&buf
);
856 FcStrBufDestroy (&buf
);