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_FAMILYLANG
, FcTypeString
, },
34 { FC_STYLE
, FcTypeString
, },
35 { FC_STYLELANG
, FcTypeString
, },
36 { FC_FULLNAME
, FcTypeString
, },
37 { FC_FULLNAMELANG
, FcTypeString
, },
38 { FC_SLANT
, FcTypeInteger
, },
39 { FC_WEIGHT
, FcTypeInteger
, },
40 { FC_WIDTH
, FcTypeInteger
, },
41 { FC_SIZE
, FcTypeDouble
, },
42 { FC_ASPECT
, FcTypeDouble
, },
43 { FC_PIXEL_SIZE
, FcTypeDouble
, },
44 { FC_SPACING
, FcTypeInteger
, },
45 { FC_FOUNDRY
, FcTypeString
, },
46 /* { FC_CORE, FcTypeBool, }, */
47 { FC_ANTIALIAS
, FcTypeBool
, },
48 { FC_HINT_STYLE
, FcTypeInteger
, },
49 { FC_HINTING
, FcTypeBool
, },
50 { FC_VERTICAL_LAYOUT
, FcTypeBool
, },
51 { FC_AUTOHINT
, FcTypeBool
, },
52 { FC_GLOBAL_ADVANCE
, FcTypeBool
, },
53 /* { FC_XLFD, FcTypeString, }, */
54 { FC_FILE
, FcTypeString
, },
55 { FC_INDEX
, FcTypeInteger
, },
56 { FC_RASTERIZER
, FcTypeString
, },
57 { FC_OUTLINE
, FcTypeBool
, },
58 { FC_SCALABLE
, FcTypeBool
, },
59 { FC_DPI
, FcTypeDouble
},
60 { FC_RGBA
, FcTypeInteger
, },
61 { FC_SCALE
, FcTypeDouble
, },
62 /* { FC_RENDER, FcTypeBool, },*/
63 { FC_MINSPACE
, FcTypeBool
, },
64 { FC_CHAR_WIDTH
, FcTypeInteger
},
65 { FC_CHAR_HEIGHT
, FcTypeInteger
},
66 { FC_MATRIX
, FcTypeMatrix
},
67 { FC_CHARSET
, FcTypeCharSet
},
68 { FC_LANG
, FcTypeLangSet
},
69 { FC_FONTVERSION
, FcTypeInteger
},
70 { FC_CAPABILITY
, FcTypeString
},
71 { FC_FONTFORMAT
, FcTypeString
},
72 { FC_EMBOLDEN
, FcTypeBool
},
75 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
77 typedef struct _FcObjectTypeList FcObjectTypeList
;
79 struct _FcObjectTypeList
{
80 const FcObjectTypeList
*next
;
81 const FcObjectType
*types
;
85 static const FcObjectTypeList _FcBaseObjectTypesList
= {
91 static const FcObjectTypeList
*_FcObjectTypes
= &_FcBaseObjectTypesList
;
94 FcNameRegisterObjectTypes (const FcObjectType
*types
, int ntypes
)
98 l
= (FcObjectTypeList
*) malloc (sizeof (FcObjectTypeList
));
101 FcMemAlloc (FC_MEM_OBJECTTYPE
, sizeof (FcObjectTypeList
));
104 l
->next
= _FcObjectTypes
;
110 FcNameUnregisterObjectTypes (const FcObjectType
*types
, int ntypes
)
112 const FcObjectTypeList
*l
, **prev
;
114 for (prev
= &_FcObjectTypes
;
116 prev
= (const FcObjectTypeList
**) &(l
->next
))
118 if (l
->types
== types
&& l
->ntypes
== ntypes
)
121 FcMemFree (FC_MEM_OBJECTTYPE
, sizeof (FcObjectTypeList
));
130 FcNameGetObjectType (const char *object
)
133 const FcObjectTypeList
*l
;
134 const FcObjectType
*t
;
136 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
138 for (i
= 0; i
< l
->ntypes
; i
++)
141 if (!strcmp (object
, t
->object
))
148 static const FcConstant _FcBaseConstants
[] = {
149 { (FcChar8
*) "thin", "weight", FC_WEIGHT_THIN
, },
150 { (FcChar8
*) "extralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
151 { (FcChar8
*) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT
, },
152 { (FcChar8
*) "light", "weight", FC_WEIGHT_LIGHT
, },
153 { (FcChar8
*) "book", "weight", FC_WEIGHT_BOOK
, },
154 { (FcChar8
*) "regular", "weight", FC_WEIGHT_REGULAR
, },
155 { (FcChar8
*) "medium", "weight", FC_WEIGHT_MEDIUM
, },
156 { (FcChar8
*) "demibold", "weight", FC_WEIGHT_DEMIBOLD
, },
157 { (FcChar8
*) "semibold", "weight", FC_WEIGHT_DEMIBOLD
, },
158 { (FcChar8
*) "bold", "weight", FC_WEIGHT_BOLD
, },
159 { (FcChar8
*) "extrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
160 { (FcChar8
*) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD
, },
161 { (FcChar8
*) "black", "weight", FC_WEIGHT_BLACK
, },
163 { (FcChar8
*) "roman", "slant", FC_SLANT_ROMAN
, },
164 { (FcChar8
*) "italic", "slant", FC_SLANT_ITALIC
, },
165 { (FcChar8
*) "oblique", "slant", FC_SLANT_OBLIQUE
, },
167 { (FcChar8
*) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED
},
168 { (FcChar8
*) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED
},
169 { (FcChar8
*) "condensed", "width", FC_WIDTH_CONDENSED
},
170 { (FcChar8
*) "semicondensed", "width", FC_WIDTH_SEMICONDENSED
},
171 { (FcChar8
*) "normal", "width", FC_WIDTH_NORMAL
},
172 { (FcChar8
*) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED
},
173 { (FcChar8
*) "expanded", "width", FC_WIDTH_EXPANDED
},
174 { (FcChar8
*) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED
},
175 { (FcChar8
*) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED
},
177 { (FcChar8
*) "proportional", "spacing", FC_PROPORTIONAL
, },
178 { (FcChar8
*) "dual", "spacing", FC_DUAL
, },
179 { (FcChar8
*) "mono", "spacing", FC_MONO
, },
180 { (FcChar8
*) "charcell", "spacing", FC_CHARCELL
, },
182 { (FcChar8
*) "unknown", "rgba", FC_RGBA_UNKNOWN
},
183 { (FcChar8
*) "rgb", "rgba", FC_RGBA_RGB
, },
184 { (FcChar8
*) "bgr", "rgba", FC_RGBA_BGR
, },
185 { (FcChar8
*) "vrgb", "rgba", FC_RGBA_VRGB
},
186 { (FcChar8
*) "vbgr", "rgba", FC_RGBA_VBGR
},
187 { (FcChar8
*) "none", "rgba", FC_RGBA_NONE
},
189 { (FcChar8
*) "hintnone", "hintstyle", FC_HINT_NONE
},
190 { (FcChar8
*) "hintslight", "hintstyle", FC_HINT_SLIGHT
},
191 { (FcChar8
*) "hintmedium", "hintstyle", FC_HINT_MEDIUM
},
192 { (FcChar8
*) "hintfull", "hintstyle", FC_HINT_FULL
},
195 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
197 typedef struct _FcConstantList FcConstantList
;
199 struct _FcConstantList
{
200 const FcConstantList
*next
;
201 const FcConstant
*consts
;
205 static const FcConstantList _FcBaseConstantList
= {
211 static const FcConstantList
*_FcConstants
= &_FcBaseConstantList
;
214 FcNameRegisterConstants (const FcConstant
*consts
, int nconsts
)
218 l
= (FcConstantList
*) malloc (sizeof (FcConstantList
));
221 FcMemAlloc (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
223 l
->nconsts
= nconsts
;
224 l
->next
= _FcConstants
;
230 FcNameUnregisterConstants (const FcConstant
*consts
, int nconsts
)
232 const FcConstantList
*l
, **prev
;
234 for (prev
= &_FcConstants
;
236 prev
= (const FcConstantList
**) &(l
->next
))
238 if (l
->consts
== consts
&& l
->nconsts
== nconsts
)
241 FcMemFree (FC_MEM_CONSTANT
, sizeof (FcConstantList
));
250 FcNameGetConstant (FcChar8
*string
)
252 const FcConstantList
*l
;
255 for (l
= _FcConstants
; l
; l
= l
->next
)
257 for (i
= 0; i
< l
->nconsts
; i
++)
258 if (!FcStrCmpIgnoreCase (string
, l
->consts
[i
].name
))
259 return &l
->consts
[i
];
265 FcNameConstant (FcChar8
*string
, int *result
)
269 if ((c
= FcNameGetConstant(string
)))
278 FcNameBool (const FcChar8
*v
, FcBool
*result
)
284 if (c0
== 't' || c0
== 'y' || c0
== '1')
289 if (c0
== 'f' || c0
== 'n' || c0
== '0')
313 FcNameConvert (FcType type
, FcChar8
*string
, FcMatrix
*m
)
320 if (!FcNameConstant (string
, &v
.u
.i
))
321 v
.u
.i
= atoi ((char *) string
);
327 if (!FcNameBool (string
, &v
.u
.b
))
331 v
.u
.d
= strtod ((char *) string
, 0);
335 sscanf ((char *) string
, "%lg %lg %lg %lg", &m
->xx
, &m
->xy
, &m
->yx
, &m
->yy
);
338 v
.u
.c
= FcNameParseCharSet (string
);
341 v
.u
.l
= FcNameParseLangSet (string
);
349 static const FcChar8
*
350 FcNameFindNext (const FcChar8
*cur
, const char *delim
, FcChar8
*save
, FcChar8
*last
)
362 else if (strchr (delim
, c
))
375 FcNameParse (const FcChar8
*name
)
384 const FcObjectType
*t
;
388 save
= malloc (strlen ((char *) name
) + 1);
391 pat
= FcPatternCreate ();
397 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
400 if (!FcPatternAddString (pat
, FC_FAMILY
, save
))
410 name
= FcNameFindNext (name
, "-,:", save
, &delim
);
411 d
= strtod ((char *) save
, (char **) &e
);
414 if (!FcPatternAddDouble (pat
, FC_SIZE
, d
))
423 name
= FcNameFindNext (name
, "=_:", save
, &delim
);
426 if (delim
== '=' || delim
== '_')
428 t
= FcNameGetObjectType ((char *) save
);
431 name
= FcNameFindNext (name
, ":,", save
, &delim
);
434 v
= FcNameConvert (t
->type
, save
, &m
);
435 if (!FcPatternAdd (pat
, t
->object
, v
, FcTrue
))
439 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
442 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
451 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
454 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
466 if ((c
= FcNameGetConstant (save
)))
468 if (!FcPatternAddInteger (pat
, c
->object
, c
->value
))
479 FcPatternDestroy (pat
);
486 FcNameUnparseString (FcStrBuf
*buf
,
487 const FcChar8
*string
,
488 const FcChar8
*escape
)
491 while ((c
= *string
++))
493 if (escape
&& strchr ((char *) escape
, (char) c
))
495 if (!FcStrBufChar (buf
, escape
[0]))
498 if (!FcStrBufChar (buf
, c
))
505 FcNameUnparseValue (FcStrBuf
*buf
,
515 sprintf ((char *) temp
, "%d", v
.u
.i
);
516 return FcNameUnparseString (buf
, temp
, 0);
518 sprintf ((char *) temp
, "%g", v
.u
.d
);
519 return FcNameUnparseString (buf
, temp
, 0);
521 return FcNameUnparseString (buf
, v
.u
.s
, escape
);
523 return FcNameUnparseString (buf
, v
.u
.b
? (FcChar8
*) "True" : (FcChar8
*) "False", 0);
525 sprintf ((char *) temp
, "%g %g %g %g",
526 v
.u
.m
->xx
, v
.u
.m
->xy
, v
.u
.m
->yx
, v
.u
.m
->yy
);
527 return FcNameUnparseString (buf
, temp
, 0);
529 return FcNameUnparseCharSet (buf
, v
.u
.c
);
531 return FcNameUnparseLangSet (buf
, v
.u
.l
);
539 FcNameUnparseValueList (FcStrBuf
*buf
,
545 if (!FcNameUnparseValue (buf
, v
->value
, escape
))
548 if (!FcNameUnparseString (buf
, (FcChar8
*) ",", 0))
554 #define FC_ESCAPE_FIXED "\\-:,"
555 #define FC_ESCAPE_VARIABLE "\\=_:,"
558 FcNameUnparse (FcPattern
*pat
)
561 FcChar8 buf_static
[8192];
564 const FcObjectTypeList
*l
;
565 const FcObjectType
*o
;
567 FcStrBufInit (&buf
, buf_static
, sizeof (buf_static
));
568 e
= FcPatternFindElt (pat
, FC_FAMILY
);
571 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
574 e
= FcPatternFindElt (pat
, FC_SIZE
);
577 if (!FcNameUnparseString (&buf
, (FcChar8
*) "-", 0))
579 if (!FcNameUnparseValueList (&buf
, e
->values
, (FcChar8
*) FC_ESCAPE_FIXED
))
582 for (l
= _FcObjectTypes
; l
; l
= l
->next
)
584 for (i
= 0; i
< l
->ntypes
; i
++)
587 if (!strcmp (o
->object
, FC_FAMILY
) ||
588 !strcmp (o
->object
, FC_SIZE
) ||
589 !strcmp (o
->object
, FC_FILE
))
592 e
= FcPatternFindElt (pat
, o
->object
);
595 if (!FcNameUnparseString (&buf
, (FcChar8
*) ":", 0))
597 if (!FcNameUnparseString (&buf
, (FcChar8
*) o
->object
, (FcChar8
*) FC_ESCAPE_VARIABLE
))
599 if (!FcNameUnparseString (&buf
, (FcChar8
*) "=", 0))
601 if (!FcNameUnparseValueList (&buf
, e
->values
,
602 (FcChar8
*) FC_ESCAPE_VARIABLE
))
607 return FcStrBufDone (&buf
);
609 FcStrBufDestroy (&buf
);