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 { "__DUMMY__", FcTypeVoid
, },
34 { FC_FAMILY
, FcTypeString
, },
35 { FC_FAMILYLANG
, FcTypeString
, },
36 { FC_STYLE
, FcTypeString
, },
37 { FC_STYLELANG
, FcTypeString
, },
38 { FC_FULLNAME
, FcTypeString
, },
39 { FC_FULLNAMELANG
, FcTypeString
, },
40 { FC_SLANT
, FcTypeInteger
, },
41 { FC_WEIGHT
, FcTypeInteger
, },
42 { FC_WIDTH
, FcTypeInteger
, },
43 { FC_SIZE
, FcTypeDouble
, },
44 { FC_ASPECT
, FcTypeDouble
, },
45 { FC_PIXEL_SIZE
, FcTypeDouble
, },
46 { FC_SPACING
, FcTypeInteger
, },
47 { FC_FOUNDRY
, FcTypeString
, },
48 /* { FC_CORE, FcTypeBool, }, */
49 { FC_ANTIALIAS
, FcTypeBool
, },
50 { FC_HINT_STYLE
, FcTypeInteger
, },
51 { FC_HINTING
, FcTypeBool
, },
52 { FC_VERTICAL_LAYOUT
, FcTypeBool
, },
53 { FC_AUTOHINT
, FcTypeBool
, },
54 { FC_GLOBAL_ADVANCE
, FcTypeBool
, },
55 /* { FC_XLFD, FcTypeString, }, */
56 { FC_FILE
, FcTypeString
, },
57 { FC_INDEX
, FcTypeInteger
, },
58 { FC_RASTERIZER
, FcTypeString
, },
59 { FC_OUTLINE
, FcTypeBool
, },
60 { FC_SCALABLE
, FcTypeBool
, },
61 { FC_DPI
, FcTypeDouble
},
62 { FC_RGBA
, FcTypeInteger
, },
63 { FC_SCALE
, FcTypeDouble
, },
64 { FC_MINSPACE
, FcTypeBool
, },
65 /* { FC_RENDER, FcTypeBool, },*/
66 { FC_CHAR_WIDTH
, FcTypeInteger
},
67 { FC_CHAR_HEIGHT
, FcTypeInteger
},
68 { FC_MATRIX
, FcTypeMatrix
},
69 { FC_CHARSET
, FcTypeCharSet
},
70 { FC_LANG
, FcTypeLangSet
},
71 { FC_FONTVERSION
, FcTypeInteger
},
72 { FC_CAPABILITY
, FcTypeString
},
73 { FC_FONTFORMAT
, FcTypeString
},
74 { FC_EMBOLDEN
, FcTypeBool
},
77 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
79 static FcObjectType
* _FcUserObjectNames
= 0;
80 static int user_obj_alloc
= 0;
81 static int next_basic_offset
= NUM_OBJECT_TYPES
;
83 typedef struct _FcObjectTypeList FcObjectTypeList
;
85 struct _FcObjectTypeList
{
86 const FcObjectTypeList
*next
;
87 const FcObjectType
*types
;
92 static const FcObjectTypeList _FcBaseObjectTypesList
= {
98 static const FcObjectTypeList
*_FcObjectTypes
= &_FcBaseObjectTypesList
;
101 FcNameRegisterObjectTypes (const FcObjectType
*types
, int ntypes
)
105 l
= (FcObjectTypeList
*) malloc (sizeof (FcObjectTypeList
));
108 FcMemAlloc (FC_MEM_OBJECTTYPE
, sizeof (FcObjectTypeList
));
111 l
->next
= _FcObjectTypes
;
112 l
->basic_offset
= next_basic_offset
;
113 next_basic_offset
+= ntypes
;
119 FcNameUnregisterObjectTypesFree (const FcObjectType
*types
, int ntypes
,
122 const FcObjectTypeList
*l
, **prev
;
124 for (prev
= &_FcObjectTypes
;
126 prev
= (const FcObjectTypeList
**) &(l
->next
))
128 if (l
->types
== types
&& l
->ntypes
== ntypes
)
132 FcMemFree (FC_MEM_OBJECTTYPE
, sizeof (FcObjectTypeList
));
142 FcNameUnregisterObjectTypes (const FcObjectType
*types
, int ntypes
)
144 return FcNameUnregisterObjectTypesFree (types
, ntypes
, FcTrue
);
148 FcNameGetObjectType (const char *object
)
151 const FcObjectTypeList
*l
;
152 const FcObjectType
*t
;
154 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
156 for (i
= 0; i
< l
->ntypes
; i
++)
159 if (!strcmp (object
, t
->object
))
166 #define OBJECT_HASH_SIZE 31
167 struct objectBucket
{
168 struct objectBucket
*next
;
172 static struct objectBucket
*FcObjectBuckets
[OBJECT_HASH_SIZE
];
174 /* Design constraint: biggest_known_ntypes must never change
175 * after any call to FcNameRegisterObjectTypes. */
176 static const FcObjectType
*biggest_known_types
= _FcBaseObjectTypes
;
177 static FcBool allocated_biggest_known_types
;
178 static int biggest_known_ntypes
= NUM_OBJECT_TYPES
;
179 static int biggest_known_count
= 0;
180 static char * biggest_ptr
;
184 FcObjectToPtrLookup (const char * object
)
186 FcObjectPtr i
= 0, n
;
187 const FcObjectTypeList
*l
;
188 FcObjectType
*t
= _FcUserObjectNames
;
190 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
192 for (i
= 0; i
< l
->ntypes
; i
++)
194 t
= (FcObjectType
*)&l
->types
[i
];
195 if (!strcmp (object
, t
->object
))
197 if (l
== (FcObjectTypeList
*)_FcUserObjectNames
)
200 return l
->basic_offset
+ i
;
205 /* We didn't match. Look for the correct FcObjectTypeList
206 * to replace it in-place. */
207 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
209 if (l
->types
== _FcUserObjectNames
)
213 if (!_FcUserObjectNames
||
214 (l
&& l
->types
== _FcUserObjectNames
&& user_obj_alloc
< l
->ntypes
))
216 int nt
= user_obj_alloc
+ 4;
217 FcObjectType
* t
= realloc (_FcUserObjectNames
,
218 nt
* sizeof (FcObjectType
));
221 _FcUserObjectNames
= t
;
225 if (l
&& l
->types
== _FcUserObjectNames
)
228 FcNameUnregisterObjectTypesFree (l
->types
, l
->ntypes
, FcFalse
);
233 FcNameRegisterObjectTypes (_FcUserObjectNames
, n
+1);
235 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
237 if (l
->types
== _FcUserObjectNames
)
239 t
= (FcObjectType
*)l
->types
;
247 t
[n
].object
= object
;
248 t
[n
].type
= FcTypeVoid
;
254 FcObjectToPtr (const char * name
)
256 FcChar32 hash
= FcStringHash ((const FcChar8
*) name
);
257 struct objectBucket
**p
;
258 struct objectBucket
*b
;
261 for (p
= &FcObjectBuckets
[hash
% OBJECT_HASH_SIZE
]; (b
= *p
); p
= &(b
->next
)
263 if (b
->hash
== hash
&& !strcmp (name
, (char *) (b
+ 1)))
265 size
= sizeof (struct objectBucket
) + strlen (name
) + 1;
267 FcMemAlloc (FC_MEM_STATICSTR
, size
);
272 b
->id
= FcObjectToPtrLookup (name
);
273 strcpy ((char *) (b
+ 1), name
);
279 FcObjectStaticNameFini (void)
282 struct objectBucket
*b
, *next
;
285 for (i
= 0; i
< OBJECT_HASH_SIZE
; i
++)
287 for (b
= FcObjectBuckets
[i
]; b
; b
= next
)
290 name
= (char *) (b
+ 1);
291 size
= sizeof (struct objectBucket
) + strlen (name
) + 1;
292 FcMemFree (FC_MEM_STATICSTR
, size
);
295 FcObjectBuckets
[i
] = 0;
300 FcObjectPtrU (FcObjectPtr si
)
302 const FcObjectTypeList
*l
;
307 if (si
< biggest_known_ntypes
)
308 return biggest_known_types
[si
].object
;
311 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
312 for (i
= 0; i
< l
->ntypes
; i
++, j
++)
314 return l
->types
[i
].object
;
317 return _FcUserObjectNames
[-si
].object
;
321 FcObjectNeededBytes ()
324 for (i
= 0; i
< biggest_known_ntypes
; i
++)
326 const char * t
= biggest_known_types
[i
].object
;
327 num
= num
+ strlen(t
) + 1;
329 biggest_known_count
= num
;
330 return num
+ sizeof(int);
334 FcObjectDistributeBytes (FcCache
* metadata
, void * block_ptr
)
336 *(int *)block_ptr
= biggest_known_ntypes
;
337 block_ptr
= (int *) block_ptr
+ 1;
338 biggest_ptr
= block_ptr
;
339 block_ptr
= (char *) block_ptr
+ biggest_known_count
;
347 for (i
= 0; i
< biggest_known_ntypes
; i
++)
349 const char * t
= biggest_known_types
[i
].object
;
350 strcpy (biggest_ptr
, t
);
351 biggest_ptr
= biggest_ptr
+ strlen(t
) + 1;
356 FcObjectUnserialize (FcCache metadata
, void *block_ptr
)
359 new_biggest
= *(int *)block_ptr
;
360 block_ptr
= (int *) block_ptr
+ 1;
361 if (biggest_known_ntypes
< new_biggest
)
364 char * bp
= (char *)block_ptr
;
366 FcObjectTypeList
* bnl
;
368 bn
= malloc (sizeof (const FcObjectType
) * (new_biggest
+ 1));
372 bnl
= malloc (sizeof (FcObjectTypeList
));
379 for (i
= 0; i
< new_biggest
; i
++)
381 const FcObjectType
* t
= FcNameGetObjectType(bp
);
383 bn
[i
].type
= t
->type
;
385 bn
[i
].type
= FcTypeVoid
;
387 bp
= bp
+ strlen(bp
) + 1;
390 FcNameUnregisterObjectTypesFree (biggest_known_types
, biggest_known_ntypes
, FcFalse
);
391 if (allocated_biggest_known_types
)
393 free ((FcObjectTypeList
*)biggest_known_types
);
396 allocated_biggest_known_types
= FcTrue
;
398 FcNameRegisterObjectTypes (bn
, new_biggest
);
399 biggest_known_ntypes
= new_biggest
;
400 biggest_known_types
= (const FcObjectType
*)bn
;
402 block_ptr
= (char *) block_ptr
+ biggest_known_count
;
407 FcObjectPtrCompare (const FcObjectPtr a
, const FcObjectPtr b
)
412 static const FcConstant _FcBaseConstants
[] = {
413 { (FcChar8
*) "thin", "weight", FC_WEIGHT_THIN
, },
414 { (FcChar8
*) "extralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
415 { (FcChar8
*) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
416 { (FcChar8
*) "light", "weight", FC_WEIGHT_LIGHT
, },
417 { (FcChar8
*) "book", "weight", FC_WEIGHT_BOOK
, },
418 { (FcChar8
*) "regular", "weight", FC_WEIGHT_REGULAR
, },
419 { (FcChar8
*) "medium", "weight", FC_WEIGHT_MEDIUM
, },
420 { (FcChar8
*) "demibold", "weight", FC_WEIGHT_DEMIBOLD
, },
421 { (FcChar8
*) "semibold", "weight", FC_WEIGHT_DEMIBOLD
, },
422 { (FcChar8
*) "bold", "weight", FC_WEIGHT_BOLD
, },
423 { (FcChar8
*) "extrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
424 { (FcChar8
*) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
425 { (FcChar8
*) "black", "weight", FC_WEIGHT_BLACK
, },
427 { (FcChar8
*) "roman", "slant", FC_SLANT_ROMAN
, },
428 { (FcChar8
*) "italic", "slant", FC_SLANT_ITALIC
, },
429 { (FcChar8
*) "oblique", "slant", FC_SLANT_OBLIQUE
, },
431 { (FcChar8
*) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED
},
432 { (FcChar8
*) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED
},
433 { (FcChar8
*) "condensed", "width", FC_WIDTH_CONDENSED
},
434 { (FcChar8
*) "semicondensed", "width", FC_WIDTH_SEMICONDENSED
},
435 { (FcChar8
*) "normal", "width", FC_WIDTH_NORMAL
},
436 { (FcChar8
*) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED
},
437 { (FcChar8
*) "expanded", "width", FC_WIDTH_EXPANDED
},
438 { (FcChar8
*) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED
},
439 { (FcChar8
*) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED
},
441 { (FcChar8
*) "proportional", "spacing", FC_PROPORTIONAL
, },
442 { (FcChar8
*) "dual", "spacing", FC_DUAL
, },
443 { (FcChar8
*) "mono", "spacing", FC_MONO
, },
444 { (FcChar8
*) "charcell", "spacing", FC_CHARCELL
, },
446 { (FcChar8
*) "unknown", "rgba", FC_RGBA_UNKNOWN
},
447 { (FcChar8
*) "rgb", "rgba", FC_RGBA_RGB
, },
448 { (FcChar8
*) "bgr", "rgba", FC_RGBA_BGR
, },
449 { (FcChar8
*) "vrgb", "rgba", FC_RGBA_VRGB
},
450 { (FcChar8
*) "vbgr", "rgba", FC_RGBA_VBGR
},
451 { (FcChar8
*) "none", "rgba", FC_RGBA_NONE
},
453 { (FcChar8
*) "hintnone", "hintstyle", FC_HINT_NONE
},
454 { (FcChar8
*) "hintslight", "hintstyle", FC_HINT_SLIGHT
},
455 { (FcChar8
*) "hintmedium", "hintstyle", FC_HINT_MEDIUM
},
456 { (FcChar8
*) "hintfull", "hintstyle", FC_HINT_FULL
},
459 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
461 typedef struct _FcConstantList FcConstantList
;
463 struct _FcConstantList
{
464 const FcConstantList
*next
;
465 const FcConstant
*consts
;
469 static const FcConstantList _FcBaseConstantList
= {
475 static const FcConstantList
*_FcConstants
= &_FcBaseConstantList
;
478 FcNameRegisterConstants (const FcConstant
*consts
, int nconsts
)
482 l
= (FcConstantList
*) malloc (sizeof (FcConstantList
));
485 FcMemAlloc (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
487 l
->nconsts
= nconsts
;
488 l
->next
= _FcConstants
;
494 FcNameUnregisterConstants (const FcConstant
*consts
, int nconsts
)
496 const FcConstantList
*l
, **prev
;
498 for (prev
= &_FcConstants
;
500 prev
= (const FcConstantList
**) &(l
->next
))
502 if (l
->consts
== consts
&& l
->nconsts
== nconsts
)
505 FcMemFree (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
514 FcNameGetConstant (FcChar8
*string
)
516 const FcConstantList
*l
;
519 for (l
= _FcConstants
; l
; l
= l
->next
)
521 for (i
= 0; i
< l
->nconsts
; i
++)
522 if (!FcStrCmpIgnoreCase (string
, l
->consts
[i
].name
))
523 return &l
->consts
[i
];
529 FcNameConstant (FcChar8
*string
, int *result
)
533 if ((c
= FcNameGetConstant(string
)))
542 FcNameBool (const FcChar8
*v
, FcBool
*result
)
548 if (c0
== 't' || c0
== 'y' || c0
== '1')
553 if (c0
== 'f' || c0
== 'n' || c0
== '0')
577 FcNameConvert (FcType type
, FcChar8
*string
, FcMatrix
*m
)
584 if (!FcNameConstant (string
, &v
.u
.i
))
585 v
.u
.i
= atoi ((char *) string
);
588 v
.u
.s
= FcStrStaticName(string
);
591 if (!FcNameBool (string
, &v
.u
.b
))
595 v
.u
.d
= strtod ((char *) string
, 0);
599 sscanf ((char *) string
, "%lg %lg %lg %lg", &m
->xx
, &m
->xy
, &m
->yx
, &m
->yy
);
602 v
.u
.c
= FcNameParseCharSet (string
);
605 v
.u
.l
= FcNameParseLangSet (string
);
613 static const FcChar8
*
614 FcNameFindNext (const FcChar8
*cur
, const char *delim
, FcChar8
*save
, FcChar8
*last
)
626 else if (strchr (delim
, c
))
639 FcNameParse (const FcChar8
*name
)
648 const FcObjectType
*t
;
652 save
= malloc (strlen ((char *) name
) + 1);
655 pat
= FcPatternCreate ();
661 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
664 if (!FcPatternAddString (pat
, FC_FAMILY
, save
))
674 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
675 d
= strtod ((char *) save
, (char **) &e
);
678 if (!FcPatternAddDouble (pat
, FC_SIZE
, d
))
687 name
= FcNameFindNext (name
, "=_:", save
, &delim
);
690 if (delim
== '=' || delim
== '_')
692 t
= FcNameGetObjectType ((char *) save
);
695 name
= FcNameFindNext (name
, ":,", save
, &delim
);
698 v
= FcNameConvert (t
->type
, save
, &m
);
699 if (!FcPatternAdd (pat
, t
->object
, v
, FcTrue
))
703 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
706 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
715 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
718 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
730 if ((c
= FcNameGetConstant (save
)))
732 if (!FcPatternAddInteger (pat
, c
->object
, c
->value
))
743 FcPatternDestroy (pat
);
750 FcNameUnparseString (FcStrBuf
*buf
,
751 const FcChar8
*string
,
752 const FcChar8
*escape
)
755 while ((c
= *string
++))
757 if (escape
&& strchr ((char *) escape
, (char) c
))
759 if (!FcStrBufChar (buf
, escape
[0]))
762 if (!FcStrBufChar (buf
, c
))
769 FcNameUnparseValue (FcStrBuf
*buf
,
775 FcValue v
= FcValueCanonicalize(v0
);
781 sprintf ((char *) temp
, "%d", v
.u
.i
);
782 return FcNameUnparseString (buf
, temp
, 0);
784 sprintf ((char *) temp
, "%g", v
.u
.d
);
785 return FcNameUnparseString (buf
, temp
, 0);
787 return FcNameUnparseString (buf
, v
.u
.s
, escape
);
789 return FcNameUnparseString (buf
, v
.u
.b
? (FcChar8
*) "True" : (FcChar8
*) "False", 0);
791 sprintf ((char *) temp
, "%g %g %g %g",
792 v
.u
.m
->xx
, v
.u
.m
->xy
, v
.u
.m
->yx
, v
.u
.m
->yy
);
793 return FcNameUnparseString (buf
, temp
, 0);
795 return FcNameUnparseCharSet (buf
, v
.u
.c
);
797 return FcNameUnparseLangSet (buf
, v
.u
.l
);
805 FcNameUnparseValueList (FcStrBuf
*buf
,
809 while (FcValueListPtrU(v
))
811 if (!FcNameUnparseValue (buf
, v
.bank
, &FcValueListPtrU(v
)->value
, escape
))
813 if (FcValueListPtrU(v
= FcValueListPtrU(v
)->next
))
814 if (!FcNameUnparseString (buf
, (FcChar8
*) ",", 0))
820 #define FC_ESCAPE_FIXED "\\-:,"
821 #define FC_ESCAPE_VARIABLE "\\=_:,"
824 FcNameUnparse (FcPattern
*pat
)
827 FcChar8 buf_static
[8192];
830 const FcObjectTypeList
*l
;
831 const FcObjectType
*o
;
833 FcStrBufInit (&buf
, buf_static
, sizeof (buf_static
));
834 e
= FcPatternFindElt (pat
, FC_FAMILY
);
837 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
840 e
= FcPatternFindElt (pat
, FC_SIZE
);
843 if (!FcNameUnparseString (&buf
, (FcChar8
*) "-", 0))
845 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
848 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
850 for (i
= 0; i
< l
->ntypes
; i
++)
853 if (!strcmp (o
->object
, FC_FAMILY
) ||
854 !strcmp (o
->object
, FC_SIZE
) ||
855 !strcmp (o
->object
, FC_FILE
))
858 e
= FcPatternFindElt (pat
, o
->object
);
861 if (!FcNameUnparseString (&buf
, (FcChar8
*) ":", 0))
863 if (!FcNameUnparseString (&buf
, (FcChar8
*) o
->object
, (FcChar8
*) FC_ESCAPE_VARIABLE
))
865 if (!FcNameUnparseString (&buf
, (FcChar8
*) "=", 0))
867 if (!FcNameUnparseValueList (&buf
, e
->values
,
868 (FcChar8
*) FC_ESCAPE_VARIABLE
))
873 return FcStrBufDone (&buf
);
875 FcStrBufDestroy (&buf
);