2 * $XFree86: xc/lib/fontconfig/src/fcname.c,v 1.5 2002/06/02 20:52:06 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_ASPECT
, FcTypeDouble
, },
38 { FC_PIXEL_SIZE
, FcTypeDouble
, },
39 { FC_SPACING
, FcTypeInteger
, },
40 { FC_FOUNDRY
, FcTypeString
, },
41 /* { FC_CORE, FcTypeBool, }, */
42 { FC_ANTIALIAS
, FcTypeBool
, },
43 /* { FC_XLFD, FcTypeString, }, */
44 { FC_FILE
, FcTypeString
, },
45 { FC_INDEX
, FcTypeInteger
, },
46 { FC_RASTERIZER
, FcTypeString
, },
47 { FC_OUTLINE
, FcTypeBool
, },
48 { FC_SCALABLE
, FcTypeBool
, },
49 { FC_RGBA
, FcTypeInteger
, },
50 { FC_SCALE
, FcTypeDouble
, },
51 /* { FC_RENDER, FcTypeBool, },*/
52 { FC_MINSPACE
, FcTypeBool
, },
53 { FC_CHAR_WIDTH
, FcTypeInteger
},
54 { FC_CHAR_HEIGHT
, FcTypeInteger
},
55 { FC_MATRIX
, FcTypeMatrix
},
56 { FC_CHARSET
, FcTypeCharSet
},
57 { FC_LANG
, FcTypeString
},
60 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
62 typedef struct _FcObjectTypeList FcObjectTypeList
;
64 struct _FcObjectTypeList
{
65 const FcObjectTypeList
*next
;
66 const FcObjectType
*types
;
70 static const FcObjectTypeList _FcBaseObjectTypesList
= {
76 static const FcObjectTypeList
*_FcObjectTypes
= &_FcBaseObjectTypesList
;
79 FcNameRegisterObjectTypes (const FcObjectType
*types
, int ntypes
)
83 l
= (FcObjectTypeList
*) malloc (sizeof (FcObjectTypeList
));
88 l
->next
= _FcObjectTypes
;
94 FcNameUnregisterObjectTypes (const FcObjectType
*types
, int ntypes
)
96 const FcObjectTypeList
*l
, **prev
;
98 for (prev
= &_FcObjectTypes
;
100 prev
= (const FcObjectTypeList
**) &(l
->next
))
102 if (l
->types
== types
&& l
->ntypes
== ntypes
)
113 FcNameGetObjectType (const char *object
)
116 const FcObjectTypeList
*l
;
117 const FcObjectType
*t
;
119 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
121 for (i
= 0; i
< l
->ntypes
; i
++)
124 if (!strcmp (object
, t
->object
))
131 static const FcConstant _FcBaseConstants
[] = {
132 { (FcChar8
*) "light", "weight", FC_WEIGHT_LIGHT
, },
133 { (FcChar8
*) "medium", "weight", FC_WEIGHT_MEDIUM
, },
134 { (FcChar8
*) "demibold", "weight", FC_WEIGHT_DEMIBOLD
, },
135 { (FcChar8
*) "bold", "weight", FC_WEIGHT_BOLD
, },
136 { (FcChar8
*) "black", "weight", FC_WEIGHT_BLACK
, },
138 { (FcChar8
*) "roman", "slant", FC_SLANT_ROMAN
, },
139 { (FcChar8
*) "italic", "slant", FC_SLANT_ITALIC
, },
140 { (FcChar8
*) "oblique", "slant", FC_SLANT_OBLIQUE
, },
142 { (FcChar8
*) "proportional", "spacing", FC_PROPORTIONAL
, },
143 { (FcChar8
*) "mono", "spacing", FC_MONO
, },
144 { (FcChar8
*) "charcell", "spacing", FC_CHARCELL
, },
146 { (FcChar8
*) "rgb", "rgba", FC_RGBA_RGB
, },
147 { (FcChar8
*) "bgr", "rgba", FC_RGBA_BGR
, },
148 { (FcChar8
*) "vrgb", "rgba", FC_RGBA_VRGB
},
149 { (FcChar8
*) "vbgr", "rgba", FC_RGBA_VBGR
},
152 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
154 typedef struct _FcConstantList FcConstantList
;
156 struct _FcConstantList
{
157 const FcConstantList
*next
;
158 const FcConstant
*consts
;
162 static const FcConstantList _FcBaseConstantList
= {
168 static const FcConstantList
*_FcConstants
= &_FcBaseConstantList
;
171 FcNameRegisterConstants (const FcConstant
*consts
, int nconsts
)
175 l
= (FcConstantList
*) malloc (sizeof (FcConstantList
));
179 l
->nconsts
= nconsts
;
180 l
->next
= _FcConstants
;
186 FcNameUnregisterConstants (const FcConstant
*consts
, int nconsts
)
188 const FcConstantList
*l
, **prev
;
190 for (prev
= &_FcConstants
;
192 prev
= (const FcConstantList
**) &(l
->next
))
194 if (l
->consts
== consts
&& l
->nconsts
== nconsts
)
205 FcNameGetConstant (FcChar8
*string
)
207 const FcConstantList
*l
;
210 for (l
= _FcConstants
; l
; l
= l
->next
)
212 for (i
= 0; i
< l
->nconsts
; i
++)
213 if (!FcStrCmpIgnoreCase (string
, l
->consts
[i
].name
))
214 return &l
->consts
[i
];
220 FcNameConstant (FcChar8
*string
, int *result
)
224 if ((c
= FcNameGetConstant(string
)))
233 FcNameBool (FcChar8
*v
, FcBool
*result
)
240 if (c0
== 't' || c0
== 'y' || c0
== '1')
245 if (c0
== 'f' || c0
== 'n' || c0
== '0')
270 FcNameConvert (FcType type
, FcChar8
*string
, FcMatrix
*m
)
277 if (!FcNameConstant (string
, &v
.u
.i
))
278 v
.u
.i
= atoi ((char *) string
);
284 if (!FcNameBool (string
, &v
.u
.b
))
288 v
.u
.d
= strtod ((char *) string
, 0);
292 sscanf ((char *) string
, "%lg %lg %lg %lg", &m
->xx
, &m
->xy
, &m
->yx
, &m
->yy
);
295 v
.u
.c
= FcNameParseCharSet (string
);
303 static const FcChar8
*
304 FcNameFindNext (const FcChar8
*cur
, const char *delim
, FcChar8
*save
, FcChar8
*last
)
316 else if (strchr (delim
, c
))
329 FcNameParse (const FcChar8
*name
)
338 const FcObjectType
*t
;
341 save
= malloc (strlen ((char *) name
) + 1);
344 pat
= FcPatternCreate ();
350 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
353 if (!FcPatternAddString (pat
, FC_FAMILY
, save
))
363 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
364 d
= strtod ((char *) save
, (char **) &e
);
367 if (!FcPatternAddDouble (pat
, FC_SIZE
, d
))
376 name
= FcNameFindNext (name
, "=_:", save
, &delim
);
379 if (delim
== '=' || delim
== '_')
381 t
= FcNameGetObjectType ((char *) save
);
384 name
= FcNameFindNext (name
, ":,", save
, &delim
);
387 v
= FcNameConvert (t
->type
, save
, &m
);
388 if (!FcPatternAdd (pat
, t
->object
, v
, FcTrue
))
390 if (v
.type
== FcTypeCharSet
)
391 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
394 if (v
.type
== FcTypeCharSet
)
395 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
403 if ((c
= FcNameGetConstant (save
)))
405 if (!FcPatternAddInteger (pat
, c
->object
, c
->value
))
416 FcPatternDestroy (pat
);
423 FcNameUnparseString (FcStrBuf
*buf
,
424 const FcChar8
*string
,
425 const FcChar8
*escape
)
428 while ((c
= *string
++))
430 if (escape
&& strchr ((char *) escape
, (char) c
))
432 if (!FcStrBufChar (buf
, escape
[0]))
435 if (!FcStrBufChar (buf
, c
))
442 FcNameUnparseValue (FcStrBuf
*buf
,
452 sprintf ((char *) temp
, "%d", v
.u
.i
);
453 return FcNameUnparseString (buf
, temp
, 0);
455 sprintf ((char *) temp
, "%g", v
.u
.d
);
456 return FcNameUnparseString (buf
, temp
, 0);
458 return FcNameUnparseString (buf
, v
.u
.s
, escape
);
460 return FcNameUnparseString (buf
, v
.u
.b
? (FcChar8
*) "True" : (FcChar8
*) "False", 0);
462 sprintf ((char *) temp
, "%g %g %g %g",
463 v
.u
.m
->xx
, v
.u
.m
->xy
, v
.u
.m
->yx
, v
.u
.m
->yy
);
464 return FcNameUnparseString (buf
, temp
, 0);
466 return FcNameUnparseCharSet (buf
, v
.u
.c
);
474 FcNameUnparseValueList (FcStrBuf
*buf
,
480 if (!FcNameUnparseValue (buf
, v
->value
, escape
))
483 if (!FcNameUnparseString (buf
, (FcChar8
*) ",", 0))
489 #define FC_ESCAPE_FIXED "\\-:,"
490 #define FC_ESCAPE_VARIABLE "\\=_:,"
493 FcNameUnparse (FcPattern
*pat
)
496 FcChar8 buf_static
[8192];
499 const FcObjectTypeList
*l
;
500 const FcObjectType
*o
;
502 FcStrBufInit (&buf
, buf_static
, sizeof (buf_static
));
503 e
= FcPatternFind (pat
, FC_FAMILY
, FcFalse
);
506 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
509 e
= FcPatternFind (pat
, FC_SIZE
, FcFalse
);
512 if (!FcNameUnparseString (&buf
, (FcChar8
*) "-", 0))
514 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
517 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
519 for (i
= 0; i
< l
->ntypes
; i
++)
522 if (!strcmp (o
->object
, FC_FAMILY
) ||
523 !strcmp (o
->object
, FC_SIZE
) ||
524 !strcmp (o
->object
, FC_FILE
))
527 e
= FcPatternFind (pat
, o
->object
, FcFalse
);
530 if (!FcNameUnparseString (&buf
, (FcChar8
*) ":", 0))
532 if (!FcNameUnparseString (&buf
, (FcChar8
*) o
->object
, (FcChar8
*) FC_ESCAPE_VARIABLE
))
534 if (!FcNameUnparseString (&buf
, (FcChar8
*) "=", 0))
536 if (!FcNameUnparseValueList (&buf
, e
->values
,
537 (FcChar8
*) FC_ESCAPE_VARIABLE
))
542 return FcStrBufDone (&buf
);
544 FcStrBufDestroy (&buf
);