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
*) "regular", "weight", FC_WEIGHT_REGULAR
, },
146 { (FcChar8
*) "medium", "weight", FC_WEIGHT_MEDIUM
, },
147 { (FcChar8
*) "demibold", "weight", FC_WEIGHT_DEMIBOLD
, },
148 { (FcChar8
*) "semibold", "weight", FC_WEIGHT_DEMIBOLD
, },
149 { (FcChar8
*) "bold", "weight", FC_WEIGHT_BOLD
, },
150 { (FcChar8
*) "extrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
151 { (FcChar8
*) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
152 { (FcChar8
*) "black", "weight", FC_WEIGHT_BLACK
, },
154 { (FcChar8
*) "roman", "slant", FC_SLANT_ROMAN
, },
155 { (FcChar8
*) "italic", "slant", FC_SLANT_ITALIC
, },
156 { (FcChar8
*) "oblique", "slant", FC_SLANT_OBLIQUE
, },
158 { (FcChar8
*) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED
},
159 { (FcChar8
*) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED
},
160 { (FcChar8
*) "condensed", "width", FC_WIDTH_CONDENSED
},
161 { (FcChar8
*) "semicondensed", "width", FC_WIDTH_SEMICONDENSED
},
162 { (FcChar8
*) "normal", "width", FC_WIDTH_NORMAL
},
163 { (FcChar8
*) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED
},
164 { (FcChar8
*) "expanded", "width", FC_WIDTH_EXPANDED
},
165 { (FcChar8
*) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED
},
166 { (FcChar8
*) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED
},
168 { (FcChar8
*) "proportional", "spacing", FC_PROPORTIONAL
, },
169 { (FcChar8
*) "mono", "spacing", FC_MONO
, },
170 { (FcChar8
*) "charcell", "spacing", FC_CHARCELL
, },
172 { (FcChar8
*) "unknown", "rgba", FC_RGBA_UNKNOWN
},
173 { (FcChar8
*) "rgb", "rgba", FC_RGBA_RGB
, },
174 { (FcChar8
*) "bgr", "rgba", FC_RGBA_BGR
, },
175 { (FcChar8
*) "vrgb", "rgba", FC_RGBA_VRGB
},
176 { (FcChar8
*) "vbgr", "rgba", FC_RGBA_VBGR
},
177 { (FcChar8
*) "none", "rgba", FC_RGBA_NONE
},
180 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
182 typedef struct _FcConstantList FcConstantList
;
184 struct _FcConstantList
{
185 const FcConstantList
*next
;
186 const FcConstant
*consts
;
190 static const FcConstantList _FcBaseConstantList
= {
196 static const FcConstantList
*_FcConstants
= &_FcBaseConstantList
;
199 FcNameRegisterConstants (const FcConstant
*consts
, int nconsts
)
203 l
= (FcConstantList
*) malloc (sizeof (FcConstantList
));
206 FcMemAlloc (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
208 l
->nconsts
= nconsts
;
209 l
->next
= _FcConstants
;
215 FcNameUnregisterConstants (const FcConstant
*consts
, int nconsts
)
217 const FcConstantList
*l
, **prev
;
219 for (prev
= &_FcConstants
;
221 prev
= (const FcConstantList
**) &(l
->next
))
223 if (l
->consts
== consts
&& l
->nconsts
== nconsts
)
226 FcMemFree (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
235 FcNameGetConstant (FcChar8
*string
)
237 const FcConstantList
*l
;
240 for (l
= _FcConstants
; l
; l
= l
->next
)
242 for (i
= 0; i
< l
->nconsts
; i
++)
243 if (!FcStrCmpIgnoreCase (string
, l
->consts
[i
].name
))
244 return &l
->consts
[i
];
250 FcNameConstant (FcChar8
*string
, int *result
)
254 if ((c
= FcNameGetConstant(string
)))
263 FcNameBool (FcChar8
*v
, FcBool
*result
)
269 if (c0
== 't' || c0
== 'y' || c0
== '1')
274 if (c0
== 'f' || c0
== 'n' || c0
== '0')
298 FcNameConvert (FcType type
, FcChar8
*string
, FcMatrix
*m
)
305 if (!FcNameConstant (string
, &v
.u
.i
))
306 v
.u
.i
= atoi ((char *) string
);
312 if (!FcNameBool (string
, &v
.u
.b
))
316 v
.u
.d
= strtod ((char *) string
, 0);
320 sscanf ((char *) string
, "%lg %lg %lg %lg", &m
->xx
, &m
->xy
, &m
->yx
, &m
->yy
);
323 v
.u
.c
= FcNameParseCharSet (string
);
326 v
.u
.l
= FcNameParseLangSet (string
);
334 static const FcChar8
*
335 FcNameFindNext (const FcChar8
*cur
, const char *delim
, FcChar8
*save
, FcChar8
*last
)
347 else if (strchr (delim
, c
))
360 FcNameParse (const FcChar8
*name
)
369 const FcObjectType
*t
;
373 save
= malloc (strlen ((char *) name
) + 1);
376 pat
= FcPatternCreate ();
382 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
385 if (!FcPatternAddString (pat
, FC_FAMILY
, save
))
395 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
396 d
= strtod ((char *) save
, (char **) &e
);
399 if (!FcPatternAddDouble (pat
, FC_SIZE
, d
))
408 name
= FcNameFindNext (name
, "=_:", save
, &delim
);
411 if (delim
== '=' || delim
== '_')
413 t
= FcNameGetObjectType ((char *) save
);
416 name
= FcNameFindNext (name
, ":,", save
, &delim
);
419 v
= FcNameConvert (t
->type
, save
, &m
);
420 if (!FcPatternAdd (pat
, t
->object
, v
, FcTrue
))
424 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
427 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
436 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
439 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
451 if ((c
= FcNameGetConstant (save
)))
453 if (!FcPatternAddInteger (pat
, c
->object
, c
->value
))
464 FcPatternDestroy (pat
);
471 FcNameUnparseString (FcStrBuf
*buf
,
472 const FcChar8
*string
,
473 const FcChar8
*escape
)
476 while ((c
= *string
++))
478 if (escape
&& strchr ((char *) escape
, (char) c
))
480 if (!FcStrBufChar (buf
, escape
[0]))
483 if (!FcStrBufChar (buf
, c
))
490 FcNameUnparseValue (FcStrBuf
*buf
,
500 sprintf ((char *) temp
, "%d", v
.u
.i
);
501 return FcNameUnparseString (buf
, temp
, 0);
503 sprintf ((char *) temp
, "%g", v
.u
.d
);
504 return FcNameUnparseString (buf
, temp
, 0);
506 return FcNameUnparseString (buf
, v
.u
.s
, escape
);
508 return FcNameUnparseString (buf
, v
.u
.b
? (FcChar8
*) "True" : (FcChar8
*) "False", 0);
510 sprintf ((char *) temp
, "%g %g %g %g",
511 v
.u
.m
->xx
, v
.u
.m
->xy
, v
.u
.m
->yx
, v
.u
.m
->yy
);
512 return FcNameUnparseString (buf
, temp
, 0);
514 return FcNameUnparseCharSet (buf
, v
.u
.c
);
516 return FcNameUnparseLangSet (buf
, v
.u
.l
);
524 FcNameUnparseValueList (FcStrBuf
*buf
,
530 if (!FcNameUnparseValue (buf
, v
->value
, escape
))
533 if (!FcNameUnparseString (buf
, (FcChar8
*) ",", 0))
539 #define FC_ESCAPE_FIXED "\\-:,"
540 #define FC_ESCAPE_VARIABLE "\\=_:,"
543 FcNameUnparse (FcPattern
*pat
)
546 FcChar8 buf_static
[8192];
549 const FcObjectTypeList
*l
;
550 const FcObjectType
*o
;
552 FcStrBufInit (&buf
, buf_static
, sizeof (buf_static
));
553 e
= FcPatternFindElt (pat
, FC_FAMILY
);
556 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
559 e
= FcPatternFindElt (pat
, FC_SIZE
);
562 if (!FcNameUnparseString (&buf
, (FcChar8
*) "-", 0))
564 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
567 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
569 for (i
= 0; i
< l
->ntypes
; i
++)
572 if (!strcmp (o
->object
, FC_FAMILY
) ||
573 !strcmp (o
->object
, FC_SIZE
) ||
574 !strcmp (o
->object
, FC_FILE
))
577 e
= FcPatternFindElt (pat
, o
->object
);
580 if (!FcNameUnparseString (&buf
, (FcChar8
*) ":", 0))
582 if (!FcNameUnparseString (&buf
, (FcChar8
*) o
->object
, (FcChar8
*) FC_ESCAPE_VARIABLE
))
584 if (!FcNameUnparseString (&buf
, (FcChar8
*) "=", 0))
586 if (!FcNameUnparseValueList (&buf
, e
->values
,
587 (FcChar8
*) FC_ESCAPE_VARIABLE
))
592 return FcStrBufDone (&buf
);
594 FcStrBufDestroy (&buf
);