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_HINTING
, FcTypeBool
, },
45 { FC_VERTICAL_LAYOUT
, FcTypeBool
, },
46 { FC_AUTOHINT
, FcTypeBool
, },
47 { FC_GLOBAL_ADVANCE
, FcTypeBool
, },
48 /* { FC_XLFD, FcTypeString, }, */
49 { FC_FILE
, FcTypeString
, },
50 { FC_INDEX
, FcTypeInteger
, },
51 { FC_RASTERIZER
, FcTypeString
, },
52 { FC_OUTLINE
, FcTypeBool
, },
53 { FC_SCALABLE
, FcTypeBool
, },
54 { FC_DPI
, FcTypeDouble
},
55 { FC_RGBA
, FcTypeInteger
, },
56 { FC_SCALE
, FcTypeDouble
, },
57 /* { FC_RENDER, FcTypeBool, },*/
58 { FC_MINSPACE
, FcTypeBool
, },
59 { FC_CHAR_WIDTH
, FcTypeInteger
},
60 { FC_CHAR_HEIGHT
, FcTypeInteger
},
61 { FC_MATRIX
, FcTypeMatrix
},
62 { FC_CHARSET
, FcTypeCharSet
},
63 { FC_LANG
, FcTypeLangSet
},
64 { FC_FONTVERSION
, FcTypeInteger
},
67 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
69 typedef struct _FcObjectTypeList FcObjectTypeList
;
71 struct _FcObjectTypeList
{
72 const FcObjectTypeList
*next
;
73 const FcObjectType
*types
;
77 static const FcObjectTypeList _FcBaseObjectTypesList
= {
83 static const FcObjectTypeList
*_FcObjectTypes
= &_FcBaseObjectTypesList
;
86 FcNameRegisterObjectTypes (const FcObjectType
*types
, int ntypes
)
90 l
= (FcObjectTypeList
*) malloc (sizeof (FcObjectTypeList
));
93 FcMemAlloc (FC_MEM_OBJECTTYPE
, sizeof (FcObjectTypeList
));
96 l
->next
= _FcObjectTypes
;
102 FcNameUnregisterObjectTypes (const FcObjectType
*types
, int ntypes
)
104 const FcObjectTypeList
*l
, **prev
;
106 for (prev
= &_FcObjectTypes
;
108 prev
= (const FcObjectTypeList
**) &(l
->next
))
110 if (l
->types
== types
&& l
->ntypes
== ntypes
)
113 FcMemFree (FC_MEM_OBJECTTYPE
, sizeof (FcObjectTypeList
));
122 FcNameGetObjectType (const char *object
)
125 const FcObjectTypeList
*l
;
126 const FcObjectType
*t
;
128 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
130 for (i
= 0; i
< l
->ntypes
; i
++)
133 if (!strcmp (object
, t
->object
))
140 static const FcConstant _FcBaseConstants
[] = {
141 { (FcChar8
*) "thin", "weight", FC_WEIGHT_THIN
, },
142 { (FcChar8
*) "extralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
143 { (FcChar8
*) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
144 { (FcChar8
*) "light", "weight", FC_WEIGHT_LIGHT
, },
145 { (FcChar8
*) "book", "weight", FC_WEIGHT_BOOK
, },
146 { (FcChar8
*) "regular", "weight", FC_WEIGHT_REGULAR
, },
147 { (FcChar8
*) "medium", "weight", FC_WEIGHT_MEDIUM
, },
148 { (FcChar8
*) "demibold", "weight", FC_WEIGHT_DEMIBOLD
, },
149 { (FcChar8
*) "semibold", "weight", FC_WEIGHT_DEMIBOLD
, },
150 { (FcChar8
*) "bold", "weight", FC_WEIGHT_BOLD
, },
151 { (FcChar8
*) "extrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
152 { (FcChar8
*) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
153 { (FcChar8
*) "black", "weight", FC_WEIGHT_BLACK
, },
155 { (FcChar8
*) "roman", "slant", FC_SLANT_ROMAN
, },
156 { (FcChar8
*) "italic", "slant", FC_SLANT_ITALIC
, },
157 { (FcChar8
*) "oblique", "slant", FC_SLANT_OBLIQUE
, },
159 { (FcChar8
*) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED
},
160 { (FcChar8
*) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED
},
161 { (FcChar8
*) "condensed", "width", FC_WIDTH_CONDENSED
},
162 { (FcChar8
*) "semicondensed", "width", FC_WIDTH_SEMICONDENSED
},
163 { (FcChar8
*) "normal", "width", FC_WIDTH_NORMAL
},
164 { (FcChar8
*) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED
},
165 { (FcChar8
*) "expanded", "width", FC_WIDTH_EXPANDED
},
166 { (FcChar8
*) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED
},
167 { (FcChar8
*) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED
},
169 { (FcChar8
*) "proportional", "spacing", FC_PROPORTIONAL
, },
170 { (FcChar8
*) "dual", "spacing", FC_DUAL
, },
171 { (FcChar8
*) "mono", "spacing", FC_MONO
, },
172 { (FcChar8
*) "charcell", "spacing", FC_CHARCELL
, },
174 { (FcChar8
*) "unknown", "rgba", FC_RGBA_UNKNOWN
},
175 { (FcChar8
*) "rgb", "rgba", FC_RGBA_RGB
, },
176 { (FcChar8
*) "bgr", "rgba", FC_RGBA_BGR
, },
177 { (FcChar8
*) "vrgb", "rgba", FC_RGBA_VRGB
},
178 { (FcChar8
*) "vbgr", "rgba", FC_RGBA_VBGR
},
179 { (FcChar8
*) "none", "rgba", FC_RGBA_NONE
},
182 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
184 typedef struct _FcConstantList FcConstantList
;
186 struct _FcConstantList
{
187 const FcConstantList
*next
;
188 const FcConstant
*consts
;
192 static const FcConstantList _FcBaseConstantList
= {
198 static const FcConstantList
*_FcConstants
= &_FcBaseConstantList
;
201 FcNameRegisterConstants (const FcConstant
*consts
, int nconsts
)
205 l
= (FcConstantList
*) malloc (sizeof (FcConstantList
));
208 FcMemAlloc (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
210 l
->nconsts
= nconsts
;
211 l
->next
= _FcConstants
;
217 FcNameUnregisterConstants (const FcConstant
*consts
, int nconsts
)
219 const FcConstantList
*l
, **prev
;
221 for (prev
= &_FcConstants
;
223 prev
= (const FcConstantList
**) &(l
->next
))
225 if (l
->consts
== consts
&& l
->nconsts
== nconsts
)
228 FcMemFree (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
237 FcNameGetConstant (FcChar8
*string
)
239 const FcConstantList
*l
;
242 for (l
= _FcConstants
; l
; l
= l
->next
)
244 for (i
= 0; i
< l
->nconsts
; i
++)
245 if (!FcStrCmpIgnoreCase (string
, l
->consts
[i
].name
))
246 return &l
->consts
[i
];
252 FcNameConstant (FcChar8
*string
, int *result
)
256 if ((c
= FcNameGetConstant(string
)))
265 FcNameBool (FcChar8
*v
, FcBool
*result
)
271 if (c0
== 't' || c0
== 'y' || c0
== '1')
276 if (c0
== 'f' || c0
== 'n' || c0
== '0')
300 FcNameConvert (FcType type
, FcChar8
*string
, FcMatrix
*m
)
307 if (!FcNameConstant (string
, &v
.u
.i
))
308 v
.u
.i
= atoi ((char *) string
);
314 if (!FcNameBool (string
, &v
.u
.b
))
318 v
.u
.d
= strtod ((char *) string
, 0);
322 sscanf ((char *) string
, "%lg %lg %lg %lg", &m
->xx
, &m
->xy
, &m
->yx
, &m
->yy
);
325 v
.u
.c
= FcNameParseCharSet (string
);
328 v
.u
.l
= FcNameParseLangSet (string
);
336 static const FcChar8
*
337 FcNameFindNext (const FcChar8
*cur
, const char *delim
, FcChar8
*save
, FcChar8
*last
)
349 else if (strchr (delim
, c
))
362 FcNameParse (const FcChar8
*name
)
371 const FcObjectType
*t
;
375 save
= malloc (strlen ((char *) name
) + 1);
378 pat
= FcPatternCreate ();
384 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
387 if (!FcPatternAddString (pat
, FC_FAMILY
, save
))
397 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
398 d
= strtod ((char *) save
, (char **) &e
);
401 if (!FcPatternAddDouble (pat
, FC_SIZE
, d
))
410 name
= FcNameFindNext (name
, "=_:", save
, &delim
);
413 if (delim
== '=' || delim
== '_')
415 t
= FcNameGetObjectType ((char *) save
);
418 name
= FcNameFindNext (name
, ":,", save
, &delim
);
421 v
= FcNameConvert (t
->type
, save
, &m
);
422 if (!FcPatternAdd (pat
, t
->object
, v
, FcTrue
))
426 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
429 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
438 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
441 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
453 if ((c
= FcNameGetConstant (save
)))
455 if (!FcPatternAddInteger (pat
, c
->object
, c
->value
))
466 FcPatternDestroy (pat
);
473 FcNameUnparseString (FcStrBuf
*buf
,
474 const FcChar8
*string
,
475 const FcChar8
*escape
)
478 while ((c
= *string
++))
480 if (escape
&& strchr ((char *) escape
, (char) c
))
482 if (!FcStrBufChar (buf
, escape
[0]))
485 if (!FcStrBufChar (buf
, c
))
492 FcNameUnparseValue (FcStrBuf
*buf
,
502 sprintf ((char *) temp
, "%d", v
.u
.i
);
503 return FcNameUnparseString (buf
, temp
, 0);
505 sprintf ((char *) temp
, "%g", v
.u
.d
);
506 return FcNameUnparseString (buf
, temp
, 0);
508 return FcNameUnparseString (buf
, v
.u
.s
, escape
);
510 return FcNameUnparseString (buf
, v
.u
.b
? (FcChar8
*) "True" : (FcChar8
*) "False", 0);
512 sprintf ((char *) temp
, "%g %g %g %g",
513 v
.u
.m
->xx
, v
.u
.m
->xy
, v
.u
.m
->yx
, v
.u
.m
->yy
);
514 return FcNameUnparseString (buf
, temp
, 0);
516 return FcNameUnparseCharSet (buf
, v
.u
.c
);
518 return FcNameUnparseLangSet (buf
, v
.u
.l
);
526 FcNameUnparseValueList (FcStrBuf
*buf
,
532 if (!FcNameUnparseValue (buf
, v
->value
, escape
))
535 if (!FcNameUnparseString (buf
, (FcChar8
*) ",", 0))
541 #define FC_ESCAPE_FIXED "\\-:,"
542 #define FC_ESCAPE_VARIABLE "\\=_:,"
545 FcNameUnparse (FcPattern
*pat
)
548 FcChar8 buf_static
[8192];
551 const FcObjectTypeList
*l
;
552 const FcObjectType
*o
;
554 FcStrBufInit (&buf
, buf_static
, sizeof (buf_static
));
555 e
= FcPatternFindElt (pat
, FC_FAMILY
);
558 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
561 e
= FcPatternFindElt (pat
, FC_SIZE
);
564 if (!FcNameUnparseString (&buf
, (FcChar8
*) "-", 0))
566 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
569 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
571 for (i
= 0; i
< l
->ntypes
; i
++)
574 if (!strcmp (o
->object
, FC_FAMILY
) ||
575 !strcmp (o
->object
, FC_SIZE
) ||
576 !strcmp (o
->object
, FC_FILE
))
579 e
= FcPatternFindElt (pat
, o
->object
);
582 if (!FcNameUnparseString (&buf
, (FcChar8
*) ":", 0))
584 if (!FcNameUnparseString (&buf
, (FcChar8
*) o
->object
, (FcChar8
*) FC_ESCAPE_VARIABLE
))
586 if (!FcNameUnparseString (&buf
, (FcChar8
*) "=", 0))
588 if (!FcNameUnparseValueList (&buf
, e
->values
,
589 (FcChar8
*) FC_ESCAPE_VARIABLE
))
594 return FcStrBufDone (&buf
);
596 FcStrBufDestroy (&buf
);