2 * $XFree86: xc/lib/fontconfig/src/fcname.c,v 1.14 2002/08/31 22:17:32 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
},
63 { FC_FONTVERSION
, FcTypeInteger
},
66 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
68 typedef struct _FcObjectTypeList FcObjectTypeList
;
70 struct _FcObjectTypeList
{
71 const FcObjectTypeList
*next
;
72 const FcObjectType
*types
;
76 static const FcObjectTypeList _FcBaseObjectTypesList
= {
82 static const FcObjectTypeList
*_FcObjectTypes
= &_FcBaseObjectTypesList
;
85 FcNameRegisterObjectTypes (const FcObjectType
*types
, int ntypes
)
89 l
= (FcObjectTypeList
*) malloc (sizeof (FcObjectTypeList
));
92 FcMemAlloc (FC_MEM_OBJECTTYPE
, sizeof (FcObjectTypeList
));
95 l
->next
= _FcObjectTypes
;
101 FcNameUnregisterObjectTypes (const FcObjectType
*types
, int ntypes
)
103 const FcObjectTypeList
*l
, **prev
;
105 for (prev
= &_FcObjectTypes
;
107 prev
= (const FcObjectTypeList
**) &(l
->next
))
109 if (l
->types
== types
&& l
->ntypes
== ntypes
)
112 FcMemFree (FC_MEM_OBJECTTYPE
, sizeof (FcObjectTypeList
));
121 FcNameGetObjectType (const char *object
)
124 const FcObjectTypeList
*l
;
125 const FcObjectType
*t
;
127 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
129 for (i
= 0; i
< l
->ntypes
; i
++)
132 if (!strcmp (object
, t
->object
))
139 static const FcConstant _FcBaseConstants
[] = {
140 { (FcChar8
*) "light", "weight", FC_WEIGHT_LIGHT
, },
141 { (FcChar8
*) "medium", "weight", FC_WEIGHT_MEDIUM
, },
142 { (FcChar8
*) "demibold", "weight", FC_WEIGHT_DEMIBOLD
, },
143 { (FcChar8
*) "bold", "weight", FC_WEIGHT_BOLD
, },
144 { (FcChar8
*) "black", "weight", FC_WEIGHT_BLACK
, },
146 { (FcChar8
*) "roman", "slant", FC_SLANT_ROMAN
, },
147 { (FcChar8
*) "italic", "slant", FC_SLANT_ITALIC
, },
148 { (FcChar8
*) "oblique", "slant", FC_SLANT_OBLIQUE
, },
150 { (FcChar8
*) "proportional", "spacing", FC_PROPORTIONAL
, },
151 { (FcChar8
*) "mono", "spacing", FC_MONO
, },
152 { (FcChar8
*) "charcell", "spacing", FC_CHARCELL
, },
154 { (FcChar8
*) "none", "rgba", FC_RGBA_NONE
},
155 { (FcChar8
*) "rgb", "rgba", FC_RGBA_RGB
, },
156 { (FcChar8
*) "bgr", "rgba", FC_RGBA_BGR
, },
157 { (FcChar8
*) "vrgb", "rgba", FC_RGBA_VRGB
},
158 { (FcChar8
*) "vbgr", "rgba", FC_RGBA_VBGR
},
161 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
163 typedef struct _FcConstantList FcConstantList
;
165 struct _FcConstantList
{
166 const FcConstantList
*next
;
167 const FcConstant
*consts
;
171 static const FcConstantList _FcBaseConstantList
= {
177 static const FcConstantList
*_FcConstants
= &_FcBaseConstantList
;
180 FcNameRegisterConstants (const FcConstant
*consts
, int nconsts
)
184 l
= (FcConstantList
*) malloc (sizeof (FcConstantList
));
187 FcMemAlloc (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
189 l
->nconsts
= nconsts
;
190 l
->next
= _FcConstants
;
196 FcNameUnregisterConstants (const FcConstant
*consts
, int nconsts
)
198 const FcConstantList
*l
, **prev
;
200 for (prev
= &_FcConstants
;
202 prev
= (const FcConstantList
**) &(l
->next
))
204 if (l
->consts
== consts
&& l
->nconsts
== nconsts
)
207 FcMemFree (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
216 FcNameGetConstant (FcChar8
*string
)
218 const FcConstantList
*l
;
221 for (l
= _FcConstants
; l
; l
= l
->next
)
223 for (i
= 0; i
< l
->nconsts
; i
++)
224 if (!FcStrCmpIgnoreCase (string
, l
->consts
[i
].name
))
225 return &l
->consts
[i
];
231 FcNameConstant (FcChar8
*string
, int *result
)
235 if ((c
= FcNameGetConstant(string
)))
244 FcNameBool (FcChar8
*v
, FcBool
*result
)
251 if (c0
== 't' || c0
== 'y' || c0
== '1')
256 if (c0
== 'f' || c0
== 'n' || c0
== '0')
281 FcNameConvert (FcType type
, FcChar8
*string
, FcMatrix
*m
)
288 if (!FcNameConstant (string
, &v
.u
.i
))
289 v
.u
.i
= atoi ((char *) string
);
295 if (!FcNameBool (string
, &v
.u
.b
))
299 v
.u
.d
= strtod ((char *) string
, 0);
303 sscanf ((char *) string
, "%lg %lg %lg %lg", &m
->xx
, &m
->xy
, &m
->yx
, &m
->yy
);
306 v
.u
.c
= FcNameParseCharSet (string
);
309 v
.u
.l
= FcNameParseLangSet (string
);
317 static const FcChar8
*
318 FcNameFindNext (const FcChar8
*cur
, const char *delim
, FcChar8
*save
, FcChar8
*last
)
330 else if (strchr (delim
, c
))
343 FcNameParse (const FcChar8
*name
)
352 const FcObjectType
*t
;
356 save
= malloc (strlen ((char *) name
) + 1);
359 pat
= FcPatternCreate ();
365 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
368 if (!FcPatternAddString (pat
, FC_FAMILY
, save
))
378 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
379 d
= strtod ((char *) save
, (char **) &e
);
382 if (!FcPatternAddDouble (pat
, FC_SIZE
, d
))
391 name
= FcNameFindNext (name
, "=_:", save
, &delim
);
394 if (delim
== '=' || delim
== '_')
396 t
= FcNameGetObjectType ((char *) save
);
399 name
= FcNameFindNext (name
, ":,", save
, &delim
);
402 v
= FcNameConvert (t
->type
, save
, &m
);
403 if (!FcPatternAdd (pat
, t
->object
, v
, FcTrue
))
407 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
410 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
419 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
422 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
434 if ((c
= FcNameGetConstant (save
)))
436 if (!FcPatternAddInteger (pat
, c
->object
, c
->value
))
447 FcPatternDestroy (pat
);
454 FcNameUnparseString (FcStrBuf
*buf
,
455 const FcChar8
*string
,
456 const FcChar8
*escape
)
459 while ((c
= *string
++))
461 if (escape
&& strchr ((char *) escape
, (char) c
))
463 if (!FcStrBufChar (buf
, escape
[0]))
466 if (!FcStrBufChar (buf
, c
))
473 FcNameUnparseValue (FcStrBuf
*buf
,
483 sprintf ((char *) temp
, "%d", v
.u
.i
);
484 return FcNameUnparseString (buf
, temp
, 0);
486 sprintf ((char *) temp
, "%g", v
.u
.d
);
487 return FcNameUnparseString (buf
, temp
, 0);
489 return FcNameUnparseString (buf
, v
.u
.s
, escape
);
491 return FcNameUnparseString (buf
, v
.u
.b
? (FcChar8
*) "True" : (FcChar8
*) "False", 0);
493 sprintf ((char *) temp
, "%g %g %g %g",
494 v
.u
.m
->xx
, v
.u
.m
->xy
, v
.u
.m
->yx
, v
.u
.m
->yy
);
495 return FcNameUnparseString (buf
, temp
, 0);
497 return FcNameUnparseCharSet (buf
, v
.u
.c
);
499 return FcNameUnparseLangSet (buf
, v
.u
.l
);
507 FcNameUnparseValueList (FcStrBuf
*buf
,
513 if (!FcNameUnparseValue (buf
, v
->value
, escape
))
516 if (!FcNameUnparseString (buf
, (FcChar8
*) ",", 0))
522 #define FC_ESCAPE_FIXED "\\-:,"
523 #define FC_ESCAPE_VARIABLE "\\=_:,"
526 FcNameUnparse (FcPattern
*pat
)
529 FcChar8 buf_static
[8192];
532 const FcObjectTypeList
*l
;
533 const FcObjectType
*o
;
535 FcStrBufInit (&buf
, buf_static
, sizeof (buf_static
));
536 e
= FcPatternFindElt (pat
, FC_FAMILY
);
539 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
542 e
= FcPatternFindElt (pat
, FC_SIZE
);
545 if (!FcNameUnparseString (&buf
, (FcChar8
*) "-", 0))
547 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
550 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
552 for (i
= 0; i
< l
->ntypes
; i
++)
555 if (!strcmp (o
->object
, FC_FAMILY
) ||
556 !strcmp (o
->object
, FC_SIZE
) ||
557 !strcmp (o
->object
, FC_FILE
))
560 e
= FcPatternFindElt (pat
, o
->object
);
563 if (!FcNameUnparseString (&buf
, (FcChar8
*) ":", 0))
565 if (!FcNameUnparseString (&buf
, (FcChar8
*) o
->object
, (FcChar8
*) FC_ESCAPE_VARIABLE
))
567 if (!FcNameUnparseString (&buf
, (FcChar8
*) "=", 0))
569 if (!FcNameUnparseValueList (&buf
, e
->values
,
570 (FcChar8
*) FC_ESCAPE_VARIABLE
))
575 return FcStrBufDone (&buf
);
577 FcStrBufDestroy (&buf
);