2 * $RCSId: xc/lib/fontconfig/src/fcname.c,v 1.15 2002/09/26 00:17:28 keithp Exp $
4 * Copyright © 2000 Keith Packard
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_WIDTH
, FcTypeInteger
, },
37 { FC_SIZE
, FcTypeDouble
, },
38 { FC_ASPECT
, FcTypeDouble
, },
39 { FC_PIXEL_SIZE
, FcTypeDouble
, },
40 { FC_SPACING
, FcTypeInteger
, },
41 { FC_FOUNDRY
, FcTypeString
, },
42 /* { FC_CORE, FcTypeBool, }, */
43 { FC_ANTIALIAS
, FcTypeBool
, },
44 { FC_HINT_STYLE
, FcTypeInteger
, },
45 { FC_HINTING
, FcTypeBool
, },
46 { FC_VERTICAL_LAYOUT
, FcTypeBool
, },
47 { FC_AUTOHINT
, FcTypeBool
, },
48 { FC_GLOBAL_ADVANCE
, FcTypeBool
, },
49 /* { FC_XLFD, FcTypeString, }, */
50 { FC_FILE
, FcTypeString
, },
51 { FC_INDEX
, FcTypeInteger
, },
52 { FC_RASTERIZER
, FcTypeString
, },
53 { FC_OUTLINE
, FcTypeBool
, },
54 { FC_SCALABLE
, FcTypeBool
, },
55 { FC_DPI
, FcTypeDouble
},
56 { FC_RGBA
, FcTypeInteger
, },
57 { FC_SCALE
, FcTypeDouble
, },
58 /* { FC_RENDER, FcTypeBool, },*/
59 { FC_MINSPACE
, FcTypeBool
, },
60 { FC_CHAR_WIDTH
, FcTypeInteger
},
61 { FC_CHAR_HEIGHT
, FcTypeInteger
},
62 { FC_MATRIX
, FcTypeMatrix
},
63 { FC_CHARSET
, FcTypeCharSet
},
64 { FC_LANG
, FcTypeLangSet
},
65 { FC_FONTVERSION
, FcTypeInteger
},
68 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
70 typedef struct _FcObjectTypeList FcObjectTypeList
;
72 struct _FcObjectTypeList
{
73 const FcObjectTypeList
*next
;
74 const FcObjectType
*types
;
78 static const FcObjectTypeList _FcBaseObjectTypesList
= {
84 static const FcObjectTypeList
*_FcObjectTypes
= &_FcBaseObjectTypesList
;
87 FcNameRegisterObjectTypes (const FcObjectType
*types
, int ntypes
)
91 l
= (FcObjectTypeList
*) malloc (sizeof (FcObjectTypeList
));
94 FcMemAlloc (FC_MEM_OBJECTTYPE
, sizeof (FcObjectTypeList
));
97 l
->next
= _FcObjectTypes
;
103 FcNameUnregisterObjectTypes (const FcObjectType
*types
, int ntypes
)
105 const FcObjectTypeList
*l
, **prev
;
107 for (prev
= &_FcObjectTypes
;
109 prev
= (const FcObjectTypeList
**) &(l
->next
))
111 if (l
->types
== types
&& l
->ntypes
== ntypes
)
114 FcMemFree (FC_MEM_OBJECTTYPE
, sizeof (FcObjectTypeList
));
123 FcNameGetObjectType (const char *object
)
126 const FcObjectTypeList
*l
;
127 const FcObjectType
*t
;
129 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
131 for (i
= 0; i
< l
->ntypes
; i
++)
134 if (!strcmp (object
, t
->object
))
141 static const FcConstant _FcBaseConstants
[] = {
142 { (FcChar8
*) "thin", "weight", FC_WEIGHT_THIN
, },
143 { (FcChar8
*) "extralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
144 { (FcChar8
*) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
145 { (FcChar8
*) "light", "weight", FC_WEIGHT_LIGHT
, },
146 { (FcChar8
*) "book", "weight", FC_WEIGHT_BOOK
, },
147 { (FcChar8
*) "regular", "weight", FC_WEIGHT_REGULAR
, },
148 { (FcChar8
*) "medium", "weight", FC_WEIGHT_MEDIUM
, },
149 { (FcChar8
*) "demibold", "weight", FC_WEIGHT_DEMIBOLD
, },
150 { (FcChar8
*) "semibold", "weight", FC_WEIGHT_DEMIBOLD
, },
151 { (FcChar8
*) "bold", "weight", FC_WEIGHT_BOLD
, },
152 { (FcChar8
*) "extrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
153 { (FcChar8
*) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
154 { (FcChar8
*) "black", "weight", FC_WEIGHT_BLACK
, },
156 { (FcChar8
*) "roman", "slant", FC_SLANT_ROMAN
, },
157 { (FcChar8
*) "italic", "slant", FC_SLANT_ITALIC
, },
158 { (FcChar8
*) "oblique", "slant", FC_SLANT_OBLIQUE
, },
160 { (FcChar8
*) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED
},
161 { (FcChar8
*) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED
},
162 { (FcChar8
*) "condensed", "width", FC_WIDTH_CONDENSED
},
163 { (FcChar8
*) "semicondensed", "width", FC_WIDTH_SEMICONDENSED
},
164 { (FcChar8
*) "normal", "width", FC_WIDTH_NORMAL
},
165 { (FcChar8
*) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED
},
166 { (FcChar8
*) "expanded", "width", FC_WIDTH_EXPANDED
},
167 { (FcChar8
*) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED
},
168 { (FcChar8
*) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED
},
170 { (FcChar8
*) "proportional", "spacing", FC_PROPORTIONAL
, },
171 { (FcChar8
*) "dual", "spacing", FC_DUAL
, },
172 { (FcChar8
*) "mono", "spacing", FC_MONO
, },
173 { (FcChar8
*) "charcell", "spacing", FC_CHARCELL
, },
175 { (FcChar8
*) "unknown", "rgba", FC_RGBA_UNKNOWN
},
176 { (FcChar8
*) "rgb", "rgba", FC_RGBA_RGB
, },
177 { (FcChar8
*) "bgr", "rgba", FC_RGBA_BGR
, },
178 { (FcChar8
*) "vrgb", "rgba", FC_RGBA_VRGB
},
179 { (FcChar8
*) "vbgr", "rgba", FC_RGBA_VBGR
},
180 { (FcChar8
*) "none", "rgba", FC_RGBA_NONE
},
182 { (FcChar8
*) "hintnone", "hintstyle", FC_HINT_NONE
},
183 { (FcChar8
*) "hintslight", "hintstyle", FC_HINT_SLIGHT
},
184 { (FcChar8
*) "hintmedium", "hintstyle", FC_HINT_MEDIUM
},
185 { (FcChar8
*) "hintfull", "hintstyle", FC_HINT_FULL
},
188 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
190 typedef struct _FcConstantList FcConstantList
;
192 struct _FcConstantList
{
193 const FcConstantList
*next
;
194 const FcConstant
*consts
;
198 static const FcConstantList _FcBaseConstantList
= {
204 static const FcConstantList
*_FcConstants
= &_FcBaseConstantList
;
207 FcNameRegisterConstants (const FcConstant
*consts
, int nconsts
)
211 l
= (FcConstantList
*) malloc (sizeof (FcConstantList
));
214 FcMemAlloc (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
216 l
->nconsts
= nconsts
;
217 l
->next
= _FcConstants
;
223 FcNameUnregisterConstants (const FcConstant
*consts
, int nconsts
)
225 const FcConstantList
*l
, **prev
;
227 for (prev
= &_FcConstants
;
229 prev
= (const FcConstantList
**) &(l
->next
))
231 if (l
->consts
== consts
&& l
->nconsts
== nconsts
)
234 FcMemFree (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
243 FcNameGetConstant (FcChar8
*string
)
245 const FcConstantList
*l
;
248 for (l
= _FcConstants
; l
; l
= l
->next
)
250 for (i
= 0; i
< l
->nconsts
; i
++)
251 if (!FcStrCmpIgnoreCase (string
, l
->consts
[i
].name
))
252 return &l
->consts
[i
];
258 FcNameConstant (FcChar8
*string
, int *result
)
262 if ((c
= FcNameGetConstant(string
)))
271 FcNameBool (FcChar8
*v
, FcBool
*result
)
277 if (c0
== 't' || c0
== 'y' || c0
== '1')
282 if (c0
== 'f' || c0
== 'n' || c0
== '0')
306 FcNameConvert (FcType type
, FcChar8
*string
, FcMatrix
*m
)
313 if (!FcNameConstant (string
, &v
.u
.i
))
314 v
.u
.i
= atoi ((char *) string
);
320 if (!FcNameBool (string
, &v
.u
.b
))
324 v
.u
.d
= strtod ((char *) string
, 0);
328 sscanf ((char *) string
, "%lg %lg %lg %lg", &m
->xx
, &m
->xy
, &m
->yx
, &m
->yy
);
331 v
.u
.c
= FcNameParseCharSet (string
);
334 v
.u
.l
= FcNameParseLangSet (string
);
342 static const FcChar8
*
343 FcNameFindNext (const FcChar8
*cur
, const char *delim
, FcChar8
*save
, FcChar8
*last
)
355 else if (strchr (delim
, c
))
368 FcNameParse (const FcChar8
*name
)
377 const FcObjectType
*t
;
381 save
= malloc (strlen ((char *) name
) + 1);
384 pat
= FcPatternCreate ();
390 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
393 if (!FcPatternAddString (pat
, FC_FAMILY
, save
))
403 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
404 d
= strtod ((char *) save
, (char **) &e
);
407 if (!FcPatternAddDouble (pat
, FC_SIZE
, d
))
416 name
= FcNameFindNext (name
, "=_:", save
, &delim
);
419 if (delim
== '=' || delim
== '_')
421 t
= FcNameGetObjectType ((char *) save
);
424 name
= FcNameFindNext (name
, ":,", save
, &delim
);
427 v
= FcNameConvert (t
->type
, save
, &m
);
428 if (!FcPatternAdd (pat
, t
->object
, v
, FcTrue
))
432 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
435 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
444 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
447 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
459 if ((c
= FcNameGetConstant (save
)))
461 if (!FcPatternAddInteger (pat
, c
->object
, c
->value
))
472 FcPatternDestroy (pat
);
479 FcNameUnparseString (FcStrBuf
*buf
,
480 const FcChar8
*string
,
481 const FcChar8
*escape
)
484 while ((c
= *string
++))
486 if (escape
&& strchr ((char *) escape
, (char) c
))
488 if (!FcStrBufChar (buf
, escape
[0]))
491 if (!FcStrBufChar (buf
, c
))
498 FcNameUnparseValue (FcStrBuf
*buf
,
508 sprintf ((char *) temp
, "%d", v
.u
.i
);
509 return FcNameUnparseString (buf
, temp
, 0);
511 sprintf ((char *) temp
, "%g", v
.u
.d
);
512 return FcNameUnparseString (buf
, temp
, 0);
514 return FcNameUnparseString (buf
, v
.u
.s
, escape
);
516 return FcNameUnparseString (buf
, v
.u
.b
? (FcChar8
*) "True" : (FcChar8
*) "False", 0);
518 sprintf ((char *) temp
, "%g %g %g %g",
519 v
.u
.m
->xx
, v
.u
.m
->xy
, v
.u
.m
->yx
, v
.u
.m
->yy
);
520 return FcNameUnparseString (buf
, temp
, 0);
522 return FcNameUnparseCharSet (buf
, v
.u
.c
);
524 return FcNameUnparseLangSet (buf
, v
.u
.l
);
532 FcNameUnparseValueList (FcStrBuf
*buf
,
538 if (!FcNameUnparseValue (buf
, v
->value
, escape
))
541 if (!FcNameUnparseString (buf
, (FcChar8
*) ",", 0))
547 #define FC_ESCAPE_FIXED "\\-:,"
548 #define FC_ESCAPE_VARIABLE "\\=_:,"
551 FcNameUnparse (FcPattern
*pat
)
554 FcChar8 buf_static
[8192];
557 const FcObjectTypeList
*l
;
558 const FcObjectType
*o
;
560 FcStrBufInit (&buf
, buf_static
, sizeof (buf_static
));
561 e
= FcPatternFindElt (pat
, FC_FAMILY
);
564 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
567 e
= FcPatternFindElt (pat
, FC_SIZE
);
570 if (!FcNameUnparseString (&buf
, (FcChar8
*) "-", 0))
572 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
575 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
577 for (i
= 0; i
< l
->ntypes
; i
++)
580 if (!strcmp (o
->object
, FC_FAMILY
) ||
581 !strcmp (o
->object
, FC_SIZE
) ||
582 !strcmp (o
->object
, FC_FILE
))
585 e
= FcPatternFindElt (pat
, o
->object
);
588 if (!FcNameUnparseString (&buf
, (FcChar8
*) ":", 0))
590 if (!FcNameUnparseString (&buf
, (FcChar8
*) o
->object
, (FcChar8
*) FC_ESCAPE_VARIABLE
))
592 if (!FcNameUnparseString (&buf
, (FcChar8
*) "=", 0))
594 if (!FcNameUnparseValueList (&buf
, e
->values
,
595 (FcChar8
*) FC_ESCAPE_VARIABLE
))
600 return FcStrBufDone (&buf
);
602 FcStrBufDestroy (&buf
);