2 * $XFree86: xc/lib/fontconfig/src/fcname.c,v 1.3 2002/02/18 22:29:28 keithp Exp $
4 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
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 static const FcObjectType _FcBaseObjectTypes
[] = {
32 { FC_FAMILY
, FcTypeString
, },
33 { FC_STYLE
, FcTypeString
, },
34 { FC_SLANT
, FcTypeInteger
, },
35 { FC_WEIGHT
, FcTypeInteger
, },
36 { FC_SIZE
, FcTypeDouble
, },
37 { FC_PIXEL_SIZE
, FcTypeDouble
, },
38 { FC_SPACING
, FcTypeInteger
, },
39 { FC_FOUNDRY
, FcTypeString
, },
40 /* { FC_CORE, FcTypeBool, }, */
41 { FC_ANTIALIAS
, FcTypeBool
, },
42 /* { FC_XLFD, FcTypeString, }, */
43 { FC_FILE
, FcTypeString
, },
44 { FC_INDEX
, FcTypeInteger
, },
45 { FC_RASTERIZER
, FcTypeString
, },
46 { FC_OUTLINE
, FcTypeBool
, },
47 { FC_SCALABLE
, FcTypeBool
, },
48 { FC_RGBA
, FcTypeInteger
, },
49 { FC_SCALE
, FcTypeDouble
, },
50 /* { FC_RENDER, FcTypeBool, },*/
51 { FC_MINSPACE
, FcTypeBool
, },
52 { FC_CHAR_WIDTH
, FcTypeInteger
},
53 { FC_CHAR_HEIGHT
, FcTypeInteger
},
54 { FC_MATRIX
, FcTypeMatrix
},
55 { FC_CHARSET
, FcTypeCharSet
},
56 { FC_LANG
, FcTypeString
},
59 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
61 typedef struct _FcObjectTypeList FcObjectTypeList
;
63 struct _FcObjectTypeList
{
64 const FcObjectTypeList
*next
;
65 const FcObjectType
*types
;
69 static const FcObjectTypeList _FcBaseObjectTypesList
= {
75 static const FcObjectTypeList
*_FcObjectTypes
= &_FcBaseObjectTypesList
;
78 FcNameRegisterObjectTypes (const FcObjectType
*types
, int ntypes
)
82 l
= (FcObjectTypeList
*) malloc (sizeof (FcObjectTypeList
));
87 l
->next
= _FcObjectTypes
;
93 FcNameUnregisterObjectTypes (const FcObjectType
*types
, int ntypes
)
95 const FcObjectTypeList
*l
, **prev
;
97 for (prev
= &_FcObjectTypes
;
99 prev
= (const FcObjectTypeList
**) &(l
->next
))
101 if (l
->types
== types
&& l
->ntypes
== ntypes
)
112 FcNameGetObjectType (const char *object
)
115 const FcObjectTypeList
*l
;
116 const FcObjectType
*t
;
118 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
120 for (i
= 0; i
< l
->ntypes
; i
++)
123 if (!strcmp (object
, t
->object
))
130 static const FcConstant _FcBaseConstants
[] = {
131 { (FcChar8
*) "light", "weight", FC_WEIGHT_LIGHT
, },
132 { (FcChar8
*) "medium", "weight", FC_WEIGHT_MEDIUM
, },
133 { (FcChar8
*) "demibold", "weight", FC_WEIGHT_DEMIBOLD
, },
134 { (FcChar8
*) "bold", "weight", FC_WEIGHT_BOLD
, },
135 { (FcChar8
*) "black", "weight", FC_WEIGHT_BLACK
, },
137 { (FcChar8
*) "roman", "slant", FC_SLANT_ROMAN
, },
138 { (FcChar8
*) "italic", "slant", FC_SLANT_ITALIC
, },
139 { (FcChar8
*) "oblique", "slant", FC_SLANT_OBLIQUE
, },
141 { (FcChar8
*) "proportional", "spacing", FC_PROPORTIONAL
, },
142 { (FcChar8
*) "mono", "spacing", FC_MONO
, },
143 { (FcChar8
*) "charcell", "spacing", FC_CHARCELL
, },
145 { (FcChar8
*) "rgb", "rgba", FC_RGBA_RGB
, },
146 { (FcChar8
*) "bgr", "rgba", FC_RGBA_BGR
, },
147 { (FcChar8
*) "vrgb", "rgba", FC_RGBA_VRGB
},
148 { (FcChar8
*) "vbgr", "rgba", FC_RGBA_VBGR
},
151 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
153 typedef struct _FcConstantList FcConstantList
;
155 struct _FcConstantList
{
156 const FcConstantList
*next
;
157 const FcConstant
*consts
;
161 static const FcConstantList _FcBaseConstantList
= {
167 static const FcConstantList
*_FcConstants
= &_FcBaseConstantList
;
170 FcNameRegisterConstants (const FcConstant
*consts
, int nconsts
)
174 l
= (FcConstantList
*) malloc (sizeof (FcConstantList
));
178 l
->nconsts
= nconsts
;
179 l
->next
= _FcConstants
;
185 FcNameUnregisterConstants (const FcConstant
*consts
, int nconsts
)
187 const FcConstantList
*l
, **prev
;
189 for (prev
= &_FcConstants
;
191 prev
= (const FcConstantList
**) &(l
->next
))
193 if (l
->consts
== consts
&& l
->nconsts
== nconsts
)
204 FcNameGetConstant (FcChar8
*string
)
206 const FcConstantList
*l
;
209 for (l
= _FcConstants
; l
; l
= l
->next
)
211 for (i
= 0; i
< l
->nconsts
; i
++)
212 if (!FcStrCmpIgnoreCase (string
, l
->consts
[i
].name
))
213 return &l
->consts
[i
];
219 FcNameConstant (FcChar8
*string
, int *result
)
223 if ((c
= FcNameGetConstant(string
)))
232 FcNameBool (FcChar8
*v
, FcBool
*result
)
239 if (c0
== 't' || c0
== 'y' || c0
== '1')
244 if (c0
== 'f' || c0
== 'n' || c0
== '0')
269 FcNameConvert (FcType type
, FcChar8
*string
, FcMatrix
*m
)
276 if (!FcNameConstant (string
, &v
.u
.i
))
277 v
.u
.i
= atoi ((char *) string
);
283 if (!FcNameBool (string
, &v
.u
.b
))
287 v
.u
.d
= strtod ((char *) string
, 0);
291 sscanf ((char *) string
, "%lg %lg %lg %lg", &m
->xx
, &m
->xy
, &m
->yx
, &m
->yy
);
294 v
.u
.c
= FcNameParseCharSet (string
);
302 static const FcChar8
*
303 FcNameFindNext (const FcChar8
*cur
, const char *delim
, FcChar8
*save
, FcChar8
*last
)
315 else if (strchr (delim
, c
))
328 FcNameParse (const FcChar8
*name
)
337 const FcObjectType
*t
;
340 save
= malloc (strlen ((char *) name
) + 1);
343 pat
= FcPatternCreate ();
349 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
352 if (!FcPatternAddString (pat
, FC_FAMILY
, save
))
362 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
363 d
= strtod ((char *) save
, (char **) &e
);
366 if (!FcPatternAddDouble (pat
, FC_SIZE
, d
))
375 name
= FcNameFindNext (name
, "=_:", save
, &delim
);
378 if (delim
== '=' || delim
== '_')
380 t
= FcNameGetObjectType ((char *) save
);
383 name
= FcNameFindNext (name
, ":,", save
, &delim
);
386 v
= FcNameConvert (t
->type
, save
, &m
);
387 if (!FcPatternAdd (pat
, t
->object
, v
, FcTrue
))
389 if (v
.type
== FcTypeCharSet
)
390 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
393 if (v
.type
== FcTypeCharSet
)
394 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
402 if ((c
= FcNameGetConstant (save
)))
404 if (!FcPatternAddInteger (pat
, c
->object
, c
->value
))
415 FcPatternDestroy (pat
);
422 FcNameUnparseString (FcStrBuf
*buf
,
423 const FcChar8
*string
,
424 const FcChar8
*escape
)
427 while ((c
= *string
++))
429 if (escape
&& strchr ((char *) escape
, (char) c
))
431 if (!FcStrBufChar (buf
, escape
[0]))
434 if (!FcStrBufChar (buf
, c
))
441 FcNameUnparseValue (FcStrBuf
*buf
,
451 sprintf ((char *) temp
, "%d", v
.u
.i
);
452 return FcNameUnparseString (buf
, temp
, 0);
454 sprintf ((char *) temp
, "%g", v
.u
.d
);
455 return FcNameUnparseString (buf
, temp
, 0);
457 return FcNameUnparseString (buf
, v
.u
.s
, escape
);
459 return FcNameUnparseString (buf
, v
.u
.b
? (FcChar8
*) "True" : (FcChar8
*) "False", 0);
461 sprintf ((char *) temp
, "%g %g %g %g",
462 v
.u
.m
->xx
, v
.u
.m
->xy
, v
.u
.m
->yx
, v
.u
.m
->yy
);
463 return FcNameUnparseString (buf
, temp
, 0);
465 return FcNameUnparseCharSet (buf
, v
.u
.c
);
471 FcNameUnparseValueList (FcStrBuf
*buf
,
477 if (!FcNameUnparseValue (buf
, v
->value
, escape
))
480 if (!FcNameUnparseString (buf
, (FcChar8
*) ",", 0))
486 #define FC_ESCAPE_FIXED "\\-:,"
487 #define FC_ESCAPE_VARIABLE "\\=_:,"
490 FcNameUnparse (FcPattern
*pat
)
493 FcChar8 buf_static
[8192];
496 const FcObjectTypeList
*l
;
497 const FcObjectType
*o
;
499 FcStrBufInit (&buf
, buf_static
, sizeof (buf_static
));
500 e
= FcPatternFind (pat
, FC_FAMILY
, FcFalse
);
503 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
506 e
= FcPatternFind (pat
, FC_SIZE
, FcFalse
);
509 if (!FcNameUnparseString (&buf
, (FcChar8
*) "-", 0))
511 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
514 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
516 for (i
= 0; i
< l
->ntypes
; i
++)
519 if (!strcmp (o
->object
, FC_FAMILY
) ||
520 !strcmp (o
->object
, FC_SIZE
) ||
521 !strcmp (o
->object
, FC_FILE
))
524 e
= FcPatternFind (pat
, o
->object
, FcFalse
);
527 if (!FcNameUnparseString (&buf
, (FcChar8
*) ":", 0))
529 if (!FcNameUnparseString (&buf
, (FcChar8
*) o
->object
, (FcChar8
*) FC_ESCAPE_VARIABLE
))
531 if (!FcNameUnparseString (&buf
, (FcChar8
*) "=", 0))
533 if (!FcNameUnparseValueList (&buf
, e
->values
,
534 (FcChar8
*) FC_ESCAPE_VARIABLE
))
539 return FcStrBufDone (&buf
);
541 FcStrBufDestroy (&buf
);