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 /* The __DUMMY__ object enables callers to distinguish the error return
33 * of FcObjectToPtrLookup from FC_FAMILY's FcObject, which would
35 static const FcObjectType _FcBaseObjectTypes
[] = {
36 { "__DUMMY__", FcTypeVoid
, },
37 { FC_FAMILY
, FcTypeString
, },
38 { FC_FAMILYLANG
, FcTypeString
, },
39 { FC_STYLE
, FcTypeString
, },
40 { FC_STYLELANG
, FcTypeString
, },
41 { FC_FULLNAME
, FcTypeString
, },
42 { FC_FULLNAMELANG
, FcTypeString
, },
43 { FC_SLANT
, FcTypeInteger
, },
44 { FC_WEIGHT
, FcTypeInteger
, },
45 { FC_WIDTH
, FcTypeInteger
, },
46 { FC_SIZE
, FcTypeDouble
, },
47 { FC_ASPECT
, FcTypeDouble
, },
48 { FC_PIXEL_SIZE
, FcTypeDouble
, },
49 { FC_SPACING
, FcTypeInteger
, },
50 { FC_FOUNDRY
, FcTypeString
, },
51 { FC_ANTIALIAS
, FcTypeBool
, },
52 { FC_HINT_STYLE
, FcTypeInteger
, },
53 { FC_HINTING
, FcTypeBool
, },
54 { FC_VERTICAL_LAYOUT
, FcTypeBool
, },
55 { FC_AUTOHINT
, FcTypeBool
, },
56 { FC_GLOBAL_ADVANCE
, FcTypeBool
, },
57 { FC_FILE
, FcTypeString
, },
58 { FC_INDEX
, FcTypeInteger
, },
59 { FC_RASTERIZER
, FcTypeString
, },
60 { FC_OUTLINE
, FcTypeBool
, },
61 { FC_SCALABLE
, FcTypeBool
, },
62 { FC_DPI
, FcTypeDouble
},
63 { FC_RGBA
, FcTypeInteger
, },
64 { FC_SCALE
, FcTypeDouble
, },
65 { FC_MINSPACE
, 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
},
75 { FC_EMBEDDED_BITMAP
, FcTypeBool
},
78 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
80 static FcObjectType
* _FcUserObjectNames
= 0;
81 static int user_obj_alloc
= 0;
82 static int next_basic_offset
= NUM_OBJECT_TYPES
;
84 typedef struct _FcObjectTypeList FcObjectTypeList
;
86 struct _FcObjectTypeList
{
87 const FcObjectTypeList
*next
;
88 const FcObjectType
*types
;
93 static const FcObjectTypeList _FcBaseObjectTypesList
= {
100 static const FcObjectTypeList
*_FcObjectTypes
= &_FcBaseObjectTypesList
;
103 FcNameRegisterObjectTypes (const FcObjectType
*types
, int ntypes
)
107 l
= (FcObjectTypeList
*) malloc (sizeof (FcObjectTypeList
));
110 FcMemAlloc (FC_MEM_OBJECTTYPE
, sizeof (FcObjectTypeList
));
113 l
->next
= _FcObjectTypes
;
114 l
->basic_offset
= next_basic_offset
;
115 next_basic_offset
+= ntypes
;
121 FcNameUnregisterObjectTypesFree (const FcObjectType
*types
, int ntypes
,
124 const FcObjectTypeList
*l
, **prev
;
126 for (prev
= &_FcObjectTypes
;
128 prev
= (const FcObjectTypeList
**) &(l
->next
))
130 if (l
->types
== types
&& l
->ntypes
== ntypes
)
134 FcMemFree (FC_MEM_OBJECTTYPE
, sizeof (FcObjectTypeList
));
144 FcNameUnregisterObjectTypes (const FcObjectType
*types
, int ntypes
)
146 return FcNameUnregisterObjectTypesFree (types
, ntypes
, FcTrue
);
150 FcNameGetObjectType (const char *object
)
153 const FcObjectTypeList
*l
;
154 const FcObjectType
*t
;
156 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
158 if (l
== (FcObjectTypeList
*)_FcUserObjectNames
)
161 for (i
= 0; i
< l
->ntypes
; i
++)
164 if (!strcmp (object
, t
->object
))
171 #define OBJECT_HASH_SIZE 31
172 struct objectBucket
{
173 struct objectBucket
*next
;
177 static struct objectBucket
*FcObjectBuckets
[OBJECT_HASH_SIZE
];
179 /* Design constraint: biggest_known_ntypes must never change
180 * after any call to FcNameRegisterObjectTypes. */
181 static const FcObjectType
*biggest_known_types
= _FcBaseObjectTypes
;
182 static int biggest_known_ntypes
= NUM_OBJECT_TYPES
;
186 FcObjectToPtrLookup (const char * object
)
189 const FcObjectTypeList
*l
;
190 FcObjectType
*t
= _FcUserObjectNames
;
193 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
195 for (i
= 0; i
< l
->ntypes
; i
++)
197 t
= (FcObjectType
*)&l
->types
[i
];
198 if (!strcmp (object
, t
->object
))
200 if (l
->types
== _FcUserObjectNames
)
203 return l
->basic_offset
+ i
;
208 /* We didn't match. Look for the application's FcObjectTypeList
209 * and replace it in-place. */
210 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
212 if (l
->types
== _FcUserObjectNames
)
216 replace
= l
&& l
->types
== _FcUserObjectNames
;
217 if (!_FcUserObjectNames
||
218 (replace
&& user_obj_alloc
<= l
->ntypes
))
220 int nt
= user_obj_alloc
+ 4;
221 FcObjectType
* tt
= realloc (_FcUserObjectNames
,
222 nt
* sizeof (FcObjectType
));
225 _FcUserObjectNames
= tt
;
232 FcNameUnregisterObjectTypesFree (l
->types
, l
->ntypes
, FcFalse
);
237 FcNameRegisterObjectTypes (_FcUserObjectNames
, n
+1);
239 if (!_FcUserObjectNames
)
242 _FcUserObjectNames
[n
].object
= object
;
243 _FcUserObjectNames
[n
].type
= FcTypeVoid
;
249 FcObjectValidType (FcObject object
, FcType type
)
251 if (object
< NUM_OBJECT_TYPES
&& _FcBaseObjectTypes
[object
].type
!= type
)
257 FcObjectFromName (const char * name
)
259 FcChar32 hash
= FcStringHash ((const FcChar8
*) name
);
260 struct objectBucket
**p
;
261 struct objectBucket
*b
;
264 for (p
= &FcObjectBuckets
[hash
% OBJECT_HASH_SIZE
]; (b
= *p
); p
= &(b
->next
)
266 if (b
->hash
== hash
&& !strcmp (name
, (char *) (b
+ 1)))
268 size
= sizeof (struct objectBucket
) + strlen (name
) + 1;
269 /* workaround glibc bug which reads strlen in groups of 4 */
270 b
= malloc (size
+ sizeof (int));
271 FcMemAlloc (FC_MEM_STATICSTR
, size
+ sizeof(int));
276 b
->id
= FcObjectToPtrLookup (name
);
277 strcpy ((char *) (b
+ 1), name
);
283 FcObjectStaticNameFini (void)
286 struct objectBucket
*b
, *next
;
289 for (i
= 0; i
< OBJECT_HASH_SIZE
; i
++)
291 for (b
= FcObjectBuckets
[i
]; b
; b
= next
)
294 name
= (char *) (b
+ 1);
295 size
= sizeof (struct objectBucket
) + strlen (name
) + 1;
296 FcMemFree (FC_MEM_STATICSTR
, size
);
299 FcObjectBuckets
[i
] = 0;
304 FcObjectName (FcObject object
)
306 const FcObjectTypeList
*l
;
311 if (object
< biggest_known_ntypes
)
312 return biggest_known_types
[object
].object
;
315 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
316 for (i
= 0; i
< l
->ntypes
; i
++, j
++)
318 return l
->types
[i
].object
;
321 return _FcUserObjectNames
[-object
].object
;
324 static const FcConstant _FcBaseConstants
[] = {
325 { (FcChar8
*) "thin", "weight", FC_WEIGHT_THIN
, },
326 { (FcChar8
*) "extralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
327 { (FcChar8
*) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
328 { (FcChar8
*) "light", "weight", FC_WEIGHT_LIGHT
, },
329 { (FcChar8
*) "book", "weight", FC_WEIGHT_BOOK
, },
330 { (FcChar8
*) "regular", "weight", FC_WEIGHT_REGULAR
, },
331 { (FcChar8
*) "medium", "weight", FC_WEIGHT_MEDIUM
, },
332 { (FcChar8
*) "demibold", "weight", FC_WEIGHT_DEMIBOLD
, },
333 { (FcChar8
*) "semibold", "weight", FC_WEIGHT_DEMIBOLD
, },
334 { (FcChar8
*) "bold", "weight", FC_WEIGHT_BOLD
, },
335 { (FcChar8
*) "extrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
336 { (FcChar8
*) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
337 { (FcChar8
*) "black", "weight", FC_WEIGHT_BLACK
, },
339 { (FcChar8
*) "roman", "slant", FC_SLANT_ROMAN
, },
340 { (FcChar8
*) "italic", "slant", FC_SLANT_ITALIC
, },
341 { (FcChar8
*) "oblique", "slant", FC_SLANT_OBLIQUE
, },
343 { (FcChar8
*) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED
},
344 { (FcChar8
*) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED
},
345 { (FcChar8
*) "condensed", "width", FC_WIDTH_CONDENSED
},
346 { (FcChar8
*) "semicondensed", "width", FC_WIDTH_SEMICONDENSED
},
347 { (FcChar8
*) "normal", "width", FC_WIDTH_NORMAL
},
348 { (FcChar8
*) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED
},
349 { (FcChar8
*) "expanded", "width", FC_WIDTH_EXPANDED
},
350 { (FcChar8
*) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED
},
351 { (FcChar8
*) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED
},
353 { (FcChar8
*) "proportional", "spacing", FC_PROPORTIONAL
, },
354 { (FcChar8
*) "dual", "spacing", FC_DUAL
, },
355 { (FcChar8
*) "mono", "spacing", FC_MONO
, },
356 { (FcChar8
*) "charcell", "spacing", FC_CHARCELL
, },
358 { (FcChar8
*) "unknown", "rgba", FC_RGBA_UNKNOWN
},
359 { (FcChar8
*) "rgb", "rgba", FC_RGBA_RGB
, },
360 { (FcChar8
*) "bgr", "rgba", FC_RGBA_BGR
, },
361 { (FcChar8
*) "vrgb", "rgba", FC_RGBA_VRGB
},
362 { (FcChar8
*) "vbgr", "rgba", FC_RGBA_VBGR
},
363 { (FcChar8
*) "none", "rgba", FC_RGBA_NONE
},
365 { (FcChar8
*) "hintnone", "hintstyle", FC_HINT_NONE
},
366 { (FcChar8
*) "hintslight", "hintstyle", FC_HINT_SLIGHT
},
367 { (FcChar8
*) "hintmedium", "hintstyle", FC_HINT_MEDIUM
},
368 { (FcChar8
*) "hintfull", "hintstyle", FC_HINT_FULL
},
371 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
373 typedef struct _FcConstantList FcConstantList
;
375 struct _FcConstantList
{
376 const FcConstantList
*next
;
377 const FcConstant
*consts
;
381 static const FcConstantList _FcBaseConstantList
= {
387 static const FcConstantList
*_FcConstants
= &_FcBaseConstantList
;
390 FcNameRegisterConstants (const FcConstant
*consts
, int nconsts
)
394 l
= (FcConstantList
*) malloc (sizeof (FcConstantList
));
397 FcMemAlloc (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
399 l
->nconsts
= nconsts
;
400 l
->next
= _FcConstants
;
406 FcNameUnregisterConstants (const FcConstant
*consts
, int nconsts
)
408 const FcConstantList
*l
, **prev
;
410 for (prev
= &_FcConstants
;
412 prev
= (const FcConstantList
**) &(l
->next
))
414 if (l
->consts
== consts
&& l
->nconsts
== nconsts
)
417 FcMemFree (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
426 FcNameGetConstant (FcChar8
*string
)
428 const FcConstantList
*l
;
431 for (l
= _FcConstants
; l
; l
= l
->next
)
433 for (i
= 0; i
< l
->nconsts
; i
++)
434 if (!FcStrCmpIgnoreCase (string
, l
->consts
[i
].name
))
435 return &l
->consts
[i
];
441 FcNameConstant (FcChar8
*string
, int *result
)
445 if ((c
= FcNameGetConstant(string
)))
454 FcNameBool (const FcChar8
*v
, FcBool
*result
)
460 if (c0
== 't' || c0
== 'y' || c0
== '1')
465 if (c0
== 'f' || c0
== 'n' || c0
== '0')
489 FcNameConvert (FcType type
, FcChar8
*string
, FcMatrix
*m
)
496 if (!FcNameConstant (string
, &v
.u
.i
))
497 v
.u
.i
= atoi ((char *) string
);
500 v
.u
.s
= FcStrStaticName(string
);
503 if (!FcNameBool (string
, &v
.u
.b
))
507 v
.u
.d
= strtod ((char *) string
, 0);
511 sscanf ((char *) string
, "%lg %lg %lg %lg", &m
->xx
, &m
->xy
, &m
->yx
, &m
->yy
);
514 v
.u
.c
= FcNameParseCharSet (string
);
517 v
.u
.l
= FcNameParseLangSet (string
);
525 static const FcChar8
*
526 FcNameFindNext (const FcChar8
*cur
, const char *delim
, FcChar8
*save
, FcChar8
*last
)
538 else if (strchr (delim
, c
))
551 FcNameParse (const FcChar8
*name
)
560 const FcObjectType
*t
;
564 save
= malloc (strlen ((char *) name
) + 1);
567 pat
= FcPatternCreate ();
573 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
576 if (!FcPatternAddString (pat
, FC_FAMILY
, save
))
586 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
587 d
= strtod ((char *) save
, (char **) &e
);
590 if (!FcPatternAddDouble (pat
, FC_SIZE
, d
))
599 name
= FcNameFindNext (name
, "=_:", save
, &delim
);
602 if (delim
== '=' || delim
== '_')
604 t
= FcNameGetObjectType ((char *) save
);
607 name
= FcNameFindNext (name
, ":,", save
, &delim
);
608 if (t
&& strcmp (t
->object
, _FcBaseObjectTypes
[0].object
))
610 v
= FcNameConvert (t
->type
, save
, &m
);
611 if (!FcPatternAdd (pat
, t
->object
, v
, FcTrue
))
615 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
618 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
627 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
630 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
642 if ((c
= FcNameGetConstant (save
)))
644 if (!FcPatternAddInteger (pat
, c
->object
, c
->value
))
655 FcPatternDestroy (pat
);
662 FcNameUnparseString (FcStrBuf
*buf
,
663 const FcChar8
*string
,
664 const FcChar8
*escape
)
667 while ((c
= *string
++))
669 if (escape
&& strchr ((char *) escape
, (char) c
))
671 if (!FcStrBufChar (buf
, escape
[0]))
674 if (!FcStrBufChar (buf
, c
))
681 FcNameUnparseValue (FcStrBuf
*buf
,
686 FcValue v
= FcValueCanonicalize(v0
);
692 sprintf ((char *) temp
, "%d", v
.u
.i
);
693 return FcNameUnparseString (buf
, temp
, 0);
695 sprintf ((char *) temp
, "%g", v
.u
.d
);
696 return FcNameUnparseString (buf
, temp
, 0);
698 return FcNameUnparseString (buf
, v
.u
.s
, escape
);
700 return FcNameUnparseString (buf
, v
.u
.b
? (FcChar8
*) "True" : (FcChar8
*) "False", 0);
702 sprintf ((char *) temp
, "%g %g %g %g",
703 v
.u
.m
->xx
, v
.u
.m
->xy
, v
.u
.m
->yx
, v
.u
.m
->yy
);
704 return FcNameUnparseString (buf
, temp
, 0);
706 return FcNameUnparseCharSet (buf
, v
.u
.c
);
708 return FcNameUnparseLangSet (buf
, v
.u
.l
);
716 FcNameUnparseValueList (FcStrBuf
*buf
,
722 if (!FcNameUnparseValue (buf
, &v
->value
, escape
))
724 if ((v
= FcValueListNext(v
)) != NULL
)
725 if (!FcNameUnparseString (buf
, (FcChar8
*) ",", 0))
731 #define FC_ESCAPE_FIXED "\\-:,"
732 #define FC_ESCAPE_VARIABLE "\\=_:,"
735 FcNameUnparse (FcPattern
*pat
)
737 return FcNameUnparseEscaped (pat
, FcTrue
);
741 FcNameUnparseEscaped (FcPattern
*pat
, FcBool escape
)
744 FcChar8 buf_static
[8192];
747 const FcObjectTypeList
*l
;
748 const FcObjectType
*o
;
750 FcStrBufInit (&buf
, buf_static
, sizeof (buf_static
));
751 e
= FcPatternObjectFindElt (pat
, FC_FAMILY_OBJECT
);
754 if (!FcNameUnparseValueList (&buf
, FcPatternEltValues(e
), escape
? (FcChar8
*) FC_ESCAPE_FIXED
: 0))
757 e
= FcPatternObjectFindElt (pat
, FC_SIZE_OBJECT
);
760 if (!FcNameUnparseString (&buf
, (FcChar8
*) "-", 0))
762 if (!FcNameUnparseValueList (&buf
, FcPatternEltValues(e
), escape
? (FcChar8
*) FC_ESCAPE_FIXED
: 0))
765 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
767 for (i
= 0; i
< l
->ntypes
; i
++)
770 if (!strcmp (o
->object
, FC_FAMILY
) ||
771 !strcmp (o
->object
, FC_SIZE
) ||
772 !strcmp (o
->object
, FC_FILE
))
775 e
= FcPatternObjectFindElt (pat
, FcObjectFromName (o
->object
));
778 if (!FcNameUnparseString (&buf
, (FcChar8
*) ":", 0))
780 if (!FcNameUnparseString (&buf
, (FcChar8
*) o
->object
, escape
? (FcChar8
*) FC_ESCAPE_VARIABLE
: 0))
782 if (!FcNameUnparseString (&buf
, (FcChar8
*) "=", 0))
784 if (!FcNameUnparseValueList (&buf
, FcPatternEltValues(e
), escape
?
785 (FcChar8
*) FC_ESCAPE_VARIABLE
: 0))
790 return FcStrBufDone (&buf
);
792 FcStrBufDestroy (&buf
);