2 * $XFree86: xc/lib/Fc/xftname.c,v 1.10 2001/03/30 18:50:18 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 (!FcStrCmpIgnoreCase (object
, t
->object
))
130 static const FcConstant _FcBaseConstants
[] = {
131 { "light", "weight", FC_WEIGHT_LIGHT
, },
132 { "medium", "weight", FC_WEIGHT_MEDIUM
, },
133 { "demibold", "weight", FC_WEIGHT_DEMIBOLD
, },
134 { "bold", "weight", FC_WEIGHT_BOLD
, },
135 { "black", "weight", FC_WEIGHT_BLACK
, },
137 { "roman", "slant", FC_SLANT_ROMAN
, },
138 { "italic", "slant", FC_SLANT_ITALIC
, },
139 { "oblique", "slant", FC_SLANT_OBLIQUE
, },
141 { "proportional", "spacing", FC_PROPORTIONAL
, },
142 { "mono", "spacing", FC_MONO
, },
143 { "charcell", "spacing", FC_CHARCELL
, },
145 { "rgb", "rgba", FC_RGBA_RGB
, },
146 { "bgr", "rgba", FC_RGBA_BGR
, },
147 { "vrgb", "rgba", FC_RGBA_VRGB
},
148 { "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 (char *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 (char *string
, int *result
)
223 if ((c
= FcNameGetConstant(string
)))
232 FcNameBool (char *v
, FcBool
*result
)
239 if (c0
== 't' || c0
== 'y' || c0
== '1')
244 if (c0
== 'f' || c0
== 'n' || c0
== '0')
269 FcNameConvert (FcType type
, char *string
, FcMatrix
*m
)
276 if (!FcNameConstant (string
, &v
.u
.i
))
277 v
.u
.i
= atoi (string
);
283 if (!FcNameBool (string
, &v
.u
.b
))
287 v
.u
.d
= strtod (string
, 0);
291 sscanf (string
, "%lg %lg %lg %lg", &m
->xx
, &m
->xy
, &m
->yx
, &m
->yy
);
294 v
.u
.c
= FcNameParseCharSet (string
);
303 FcNameFindNext (const char *cur
, const char *delim
, char *save
, char *last
)
315 else if (strchr (delim
, c
))
328 FcNameParse (const char *name
)
337 const FcObjectType
*t
;
340 save
= malloc (strlen (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 (save
, &e
);
366 if (!FcPatternAddDouble (pat
, FC_SIZE
, d
))
375 name
= FcNameFindNext (name
, "=_:", save
, &delim
);
378 if (delim
== '=' || delim
== '_')
380 t
= FcNameGetObjectType (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
);
423 FcNameBufInit (FcNameBuf
*buf
, FcChar8
*init
, int size
)
426 buf
->allocated
= FcFalse
;
427 buf
->failed
= FcFalse
;
433 FcNameBufDestroy (FcNameBuf
*buf
)
440 FcNameBufDone (FcNameBuf
*buf
)
444 ret
= malloc (buf
->len
+ 1);
447 memcpy (ret
, buf
->buf
, buf
->len
);
448 ret
[buf
->len
] = '\0';
450 FcNameBufDestroy (buf
);
455 FcNameBufChar (FcNameBuf
*buf
, FcChar8 c
)
457 if (buf
->len
== buf
->size
)
464 size
= buf
->size
* 2;
465 new = realloc (buf
->buf
, size
);
469 size
= buf
->size
+ 1024;
473 buf
->allocated
= FcTrue
;
474 memcpy (new, buf
->buf
, buf
->len
);
479 buf
->failed
= FcTrue
;
485 buf
->buf
[buf
->len
++] = c
;
490 FcNameBufString (FcNameBuf
*buf
, const FcChar8
*s
)
494 if (!FcNameBufChar (buf
, c
))
500 FcNameUnparseString (FcNameBuf
*buf
,
501 const FcChar8
*string
,
502 const FcChar8
*escape
)
505 while ((c
= *string
++))
507 if (escape
&& strchr ((char *) escape
, (char) c
))
509 if (!FcNameBufChar (buf
, escape
[0]))
512 if (!FcNameBufChar (buf
, c
))
519 FcNameUnparseValue (FcNameBuf
*buf
,
529 sprintf ((char *) temp
, "%d", v
.u
.i
);
530 return FcNameUnparseString (buf
, temp
, 0);
532 sprintf ((char *) temp
, "%g", v
.u
.d
);
533 return FcNameUnparseString (buf
, temp
, 0);
535 return FcNameUnparseString (buf
, v
.u
.s
, escape
);
537 return FcNameUnparseString (buf
, v
.u
.b
? "True" : "False", 0);
539 sprintf ((char *) temp
, "%g %g %g %g",
540 v
.u
.m
->xx
, v
.u
.m
->xy
, v
.u
.m
->yx
, v
.u
.m
->yy
);
541 return FcNameUnparseString (buf
, temp
, 0);
543 return FcNameUnparseCharSet (buf
, v
.u
.c
);
549 FcNameUnparseValueList (FcNameBuf
*buf
,
555 if (!FcNameUnparseValue (buf
, v
->value
, escape
))
558 if (!FcNameUnparseString (buf
, ",", 0))
564 #define FC_ESCAPE_FIXED "\\-:,"
565 #define FC_ESCAPE_VARIABLE "\\=_:,"
568 FcNameUnparse (FcPattern
*pat
)
571 FcChar8 buf_static
[8192];
574 const FcObjectTypeList
*l
;
575 const FcObjectType
*o
;
577 FcNameBufInit (&buf
, buf_static
, sizeof (buf_static
));
578 e
= FcPatternFind (pat
, FC_FAMILY
, FcFalse
);
581 if (!FcNameUnparseValueList (&buf
, e
->values
, FC_ESCAPE_FIXED
))
584 e
= FcPatternFind (pat
, FC_SIZE
, FcFalse
);
587 if (!FcNameUnparseString (&buf
, "-", 0))
589 if (!FcNameUnparseValueList (&buf
, e
->values
, FC_ESCAPE_FIXED
))
592 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
594 for (i
= 0; i
< l
->ntypes
; i
++)
597 if (!strcmp (o
->object
, FC_FAMILY
) ||
598 !strcmp (o
->object
, FC_SIZE
) ||
599 !strcmp (o
->object
, FC_FILE
))
602 e
= FcPatternFind (pat
, o
->object
, FcFalse
);
605 if (!FcNameUnparseString (&buf
, ":", 0))
607 if (!FcNameUnparseString (&buf
, o
->object
, FC_ESCAPE_VARIABLE
))
609 if (!FcNameUnparseString (&buf
, "=", 0))
611 if (!FcNameUnparseValueList (&buf
, e
->values
,
617 return FcNameBufDone (&buf
);
619 FcNameBufDestroy (&buf
);