2 * $XFree86: xc/lib/fontconfig/src/fcname.c,v 1.13 2002/08/22 07:36:45 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_HINTING
, FcTypeBool
, },
44 { FC_VERTICAL_LAYOUT
, FcTypeBool
, },
45 { FC_AUTOHINT
, FcTypeBool
, },
46 { FC_GLOBAL_ADVANCE
, FcTypeBool
, },
47 /* { FC_XLFD, FcTypeString, }, */
48 { FC_FILE
, FcTypeString
, },
49 { FC_INDEX
, FcTypeInteger
, },
50 { FC_RASTERIZER
, FcTypeString
, },
51 { FC_OUTLINE
, FcTypeBool
, },
52 { FC_SCALABLE
, FcTypeBool
, },
53 { FC_DPI
, FcTypeDouble
},
54 { FC_RGBA
, FcTypeInteger
, },
55 { FC_SCALE
, FcTypeDouble
, },
56 /* { FC_RENDER, FcTypeBool, },*/
57 { FC_MINSPACE
, FcTypeBool
, },
58 { FC_CHAR_WIDTH
, FcTypeInteger
},
59 { FC_CHAR_HEIGHT
, FcTypeInteger
},
60 { FC_MATRIX
, FcTypeMatrix
},
61 { FC_CHARSET
, FcTypeCharSet
},
62 { FC_LANG
, FcTypeLangSet
},
65 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
67 typedef struct _FcObjectTypeList FcObjectTypeList
;
69 struct _FcObjectTypeList
{
70 const FcObjectTypeList
*next
;
71 const FcObjectType
*types
;
75 static const FcObjectTypeList _FcBaseObjectTypesList
= {
81 static const FcObjectTypeList
*_FcObjectTypes
= &_FcBaseObjectTypesList
;
84 FcNameRegisterObjectTypes (const FcObjectType
*types
, int ntypes
)
88 l
= (FcObjectTypeList
*) malloc (sizeof (FcObjectTypeList
));
91 FcMemAlloc (FC_MEM_OBJECTTYPE
, sizeof (FcObjectTypeList
));
94 l
->next
= _FcObjectTypes
;
100 FcNameUnregisterObjectTypes (const FcObjectType
*types
, int ntypes
)
102 const FcObjectTypeList
*l
, **prev
;
104 for (prev
= &_FcObjectTypes
;
106 prev
= (const FcObjectTypeList
**) &(l
->next
))
108 if (l
->types
== types
&& l
->ntypes
== ntypes
)
111 FcMemFree (FC_MEM_OBJECTTYPE
, sizeof (FcObjectTypeList
));
120 FcNameGetObjectType (const char *object
)
123 const FcObjectTypeList
*l
;
124 const FcObjectType
*t
;
126 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
128 for (i
= 0; i
< l
->ntypes
; i
++)
131 if (!strcmp (object
, t
->object
))
138 static const FcConstant _FcBaseConstants
[] = {
139 { (FcChar8
*) "light", "weight", FC_WEIGHT_LIGHT
, },
140 { (FcChar8
*) "medium", "weight", FC_WEIGHT_MEDIUM
, },
141 { (FcChar8
*) "demibold", "weight", FC_WEIGHT_DEMIBOLD
, },
142 { (FcChar8
*) "bold", "weight", FC_WEIGHT_BOLD
, },
143 { (FcChar8
*) "black", "weight", FC_WEIGHT_BLACK
, },
145 { (FcChar8
*) "roman", "slant", FC_SLANT_ROMAN
, },
146 { (FcChar8
*) "italic", "slant", FC_SLANT_ITALIC
, },
147 { (FcChar8
*) "oblique", "slant", FC_SLANT_OBLIQUE
, },
149 { (FcChar8
*) "proportional", "spacing", FC_PROPORTIONAL
, },
150 { (FcChar8
*) "mono", "spacing", FC_MONO
, },
151 { (FcChar8
*) "charcell", "spacing", FC_CHARCELL
, },
153 { (FcChar8
*) "none", "rgba", FC_RGBA_NONE
},
154 { (FcChar8
*) "rgb", "rgba", FC_RGBA_RGB
, },
155 { (FcChar8
*) "bgr", "rgba", FC_RGBA_BGR
, },
156 { (FcChar8
*) "vrgb", "rgba", FC_RGBA_VRGB
},
157 { (FcChar8
*) "vbgr", "rgba", FC_RGBA_VBGR
},
160 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
162 typedef struct _FcConstantList FcConstantList
;
164 struct _FcConstantList
{
165 const FcConstantList
*next
;
166 const FcConstant
*consts
;
170 static const FcConstantList _FcBaseConstantList
= {
176 static const FcConstantList
*_FcConstants
= &_FcBaseConstantList
;
179 FcNameRegisterConstants (const FcConstant
*consts
, int nconsts
)
183 l
= (FcConstantList
*) malloc (sizeof (FcConstantList
));
186 FcMemAlloc (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
188 l
->nconsts
= nconsts
;
189 l
->next
= _FcConstants
;
195 FcNameUnregisterConstants (const FcConstant
*consts
, int nconsts
)
197 const FcConstantList
*l
, **prev
;
199 for (prev
= &_FcConstants
;
201 prev
= (const FcConstantList
**) &(l
->next
))
203 if (l
->consts
== consts
&& l
->nconsts
== nconsts
)
206 FcMemFree (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
215 FcNameGetConstant (FcChar8
*string
)
217 const FcConstantList
*l
;
220 for (l
= _FcConstants
; l
; l
= l
->next
)
222 for (i
= 0; i
< l
->nconsts
; i
++)
223 if (!FcStrCmpIgnoreCase (string
, l
->consts
[i
].name
))
224 return &l
->consts
[i
];
230 FcNameConstant (FcChar8
*string
, int *result
)
234 if ((c
= FcNameGetConstant(string
)))
243 FcNameBool (FcChar8
*v
, FcBool
*result
)
250 if (c0
== 't' || c0
== 'y' || c0
== '1')
255 if (c0
== 'f' || c0
== 'n' || c0
== '0')
280 FcNameConvert (FcType type
, FcChar8
*string
, FcMatrix
*m
)
287 if (!FcNameConstant (string
, &v
.u
.i
))
288 v
.u
.i
= atoi ((char *) string
);
294 if (!FcNameBool (string
, &v
.u
.b
))
298 v
.u
.d
= strtod ((char *) string
, 0);
302 sscanf ((char *) string
, "%lg %lg %lg %lg", &m
->xx
, &m
->xy
, &m
->yx
, &m
->yy
);
305 v
.u
.c
= FcNameParseCharSet (string
);
308 v
.u
.l
= FcNameParseLangSet (string
);
316 static const FcChar8
*
317 FcNameFindNext (const FcChar8
*cur
, const char *delim
, FcChar8
*save
, FcChar8
*last
)
329 else if (strchr (delim
, c
))
342 FcNameParse (const FcChar8
*name
)
351 const FcObjectType
*t
;
355 save
= malloc (strlen ((char *) name
) + 1);
358 pat
= FcPatternCreate ();
364 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
367 if (!FcPatternAddString (pat
, FC_FAMILY
, save
))
377 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
378 d
= strtod ((char *) save
, (char **) &e
);
381 if (!FcPatternAddDouble (pat
, FC_SIZE
, d
))
390 name
= FcNameFindNext (name
, "=_:", save
, &delim
);
393 if (delim
== '=' || delim
== '_')
395 t
= FcNameGetObjectType ((char *) save
);
398 name
= FcNameFindNext (name
, ":,", save
, &delim
);
401 v
= FcNameConvert (t
->type
, save
, &m
);
402 if (!FcPatternAdd (pat
, t
->object
, v
, FcTrue
))
406 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
409 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
418 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
421 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
433 if ((c
= FcNameGetConstant (save
)))
435 if (!FcPatternAddInteger (pat
, c
->object
, c
->value
))
446 FcPatternDestroy (pat
);
453 FcNameUnparseString (FcStrBuf
*buf
,
454 const FcChar8
*string
,
455 const FcChar8
*escape
)
458 while ((c
= *string
++))
460 if (escape
&& strchr ((char *) escape
, (char) c
))
462 if (!FcStrBufChar (buf
, escape
[0]))
465 if (!FcStrBufChar (buf
, c
))
472 FcNameUnparseValue (FcStrBuf
*buf
,
482 sprintf ((char *) temp
, "%d", v
.u
.i
);
483 return FcNameUnparseString (buf
, temp
, 0);
485 sprintf ((char *) temp
, "%g", v
.u
.d
);
486 return FcNameUnparseString (buf
, temp
, 0);
488 return FcNameUnparseString (buf
, v
.u
.s
, escape
);
490 return FcNameUnparseString (buf
, v
.u
.b
? (FcChar8
*) "True" : (FcChar8
*) "False", 0);
492 sprintf ((char *) temp
, "%g %g %g %g",
493 v
.u
.m
->xx
, v
.u
.m
->xy
, v
.u
.m
->yx
, v
.u
.m
->yy
);
494 return FcNameUnparseString (buf
, temp
, 0);
496 return FcNameUnparseCharSet (buf
, v
.u
.c
);
498 return FcNameUnparseLangSet (buf
, v
.u
.l
);
506 FcNameUnparseValueList (FcStrBuf
*buf
,
512 if (!FcNameUnparseValue (buf
, v
->value
, escape
))
515 if (!FcNameUnparseString (buf
, (FcChar8
*) ",", 0))
521 #define FC_ESCAPE_FIXED "\\-:,"
522 #define FC_ESCAPE_VARIABLE "\\=_:,"
525 FcNameUnparse (FcPattern
*pat
)
528 FcChar8 buf_static
[8192];
531 const FcObjectTypeList
*l
;
532 const FcObjectType
*o
;
534 FcStrBufInit (&buf
, buf_static
, sizeof (buf_static
));
535 e
= FcPatternFindElt (pat
, FC_FAMILY
);
538 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
541 e
= FcPatternFindElt (pat
, FC_SIZE
);
544 if (!FcNameUnparseString (&buf
, (FcChar8
*) "-", 0))
546 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
549 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
551 for (i
= 0; i
< l
->ntypes
; i
++)
554 if (!strcmp (o
->object
, FC_FAMILY
) ||
555 !strcmp (o
->object
, FC_SIZE
) ||
556 !strcmp (o
->object
, FC_FILE
))
559 e
= FcPatternFindElt (pat
, o
->object
);
562 if (!FcNameUnparseString (&buf
, (FcChar8
*) ":", 0))
564 if (!FcNameUnparseString (&buf
, (FcChar8
*) o
->object
, (FcChar8
*) FC_ESCAPE_VARIABLE
))
566 if (!FcNameUnparseString (&buf
, (FcChar8
*) "=", 0))
568 if (!FcNameUnparseValueList (&buf
, e
->values
,
569 (FcChar8
*) FC_ESCAPE_VARIABLE
))
574 return FcStrBufDone (&buf
);
576 FcStrBufDestroy (&buf
);