]> git.wh0rd.org - fontconfig.git/blob - src/fcname.c
Add strong/weak pattern value binding, add known charsets for automatic
[fontconfig.git] / src / fcname.c
1 /*
2 * $XFree86: xc/lib/fontconfig/src/fcname.c,v 1.9 2002/06/26 22:14:08 keithp Exp $
3 *
4 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
5 *
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.
15 *
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.
23 */
24
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include "fcint.h"
30
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, FcTypeString },
63 };
64
65 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
66
67 typedef struct _FcObjectTypeList FcObjectTypeList;
68
69 struct _FcObjectTypeList {
70 const FcObjectTypeList *next;
71 const FcObjectType *types;
72 int ntypes;
73 };
74
75 static const FcObjectTypeList _FcBaseObjectTypesList = {
76 0,
77 _FcBaseObjectTypes,
78 NUM_OBJECT_TYPES
79 };
80
81 static const FcObjectTypeList *_FcObjectTypes = &_FcBaseObjectTypesList;
82
83 FcBool
84 FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
85 {
86 FcObjectTypeList *l;
87
88 l = (FcObjectTypeList *) malloc (sizeof (FcObjectTypeList));
89 if (!l)
90 return FcFalse;
91 l->types = types;
92 l->ntypes = ntypes;
93 l->next = _FcObjectTypes;
94 _FcObjectTypes = l;
95 return FcTrue;
96 }
97
98 FcBool
99 FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
100 {
101 const FcObjectTypeList *l, **prev;
102
103 for (prev = &_FcObjectTypes;
104 (l = *prev);
105 prev = (const FcObjectTypeList **) &(l->next))
106 {
107 if (l->types == types && l->ntypes == ntypes)
108 {
109 *prev = l->next;
110 free ((void *) l);
111 return FcTrue;
112 }
113 }
114 return FcFalse;
115 }
116
117 const FcObjectType *
118 FcNameGetObjectType (const char *object)
119 {
120 int i;
121 const FcObjectTypeList *l;
122 const FcObjectType *t;
123
124 for (l = _FcObjectTypes; l; l = l->next)
125 {
126 for (i = 0; i < l->ntypes; i++)
127 {
128 t = &l->types[i];
129 if (!strcmp (object, t->object))
130 return t;
131 }
132 }
133 return 0;
134 }
135
136 static const FcConstant _FcBaseConstants[] = {
137 { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, },
138 { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, },
139 { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, },
140 { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, },
141 { (FcChar8 *) "black", "weight", FC_WEIGHT_BLACK, },
142
143 { (FcChar8 *) "roman", "slant", FC_SLANT_ROMAN, },
144 { (FcChar8 *) "italic", "slant", FC_SLANT_ITALIC, },
145 { (FcChar8 *) "oblique", "slant", FC_SLANT_OBLIQUE, },
146
147 { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, },
148 { (FcChar8 *) "mono", "spacing", FC_MONO, },
149 { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, },
150
151 { (FcChar8 *) "none", "rgba", FC_RGBA_NONE },
152 { (FcChar8 *) "rgb", "rgba", FC_RGBA_RGB, },
153 { (FcChar8 *) "bgr", "rgba", FC_RGBA_BGR, },
154 { (FcChar8 *) "vrgb", "rgba", FC_RGBA_VRGB },
155 { (FcChar8 *) "vbgr", "rgba", FC_RGBA_VBGR },
156 };
157
158 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
159
160 typedef struct _FcConstantList FcConstantList;
161
162 struct _FcConstantList {
163 const FcConstantList *next;
164 const FcConstant *consts;
165 int nconsts;
166 };
167
168 static const FcConstantList _FcBaseConstantList = {
169 0,
170 _FcBaseConstants,
171 NUM_FC_CONSTANTS
172 };
173
174 static const FcConstantList *_FcConstants = &_FcBaseConstantList;
175
176 FcBool
177 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
178 {
179 FcConstantList *l;
180
181 l = (FcConstantList *) malloc (sizeof (FcConstantList));
182 if (!l)
183 return FcFalse;
184 l->consts = consts;
185 l->nconsts = nconsts;
186 l->next = _FcConstants;
187 _FcConstants = l;
188 return FcTrue;
189 }
190
191 FcBool
192 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
193 {
194 const FcConstantList *l, **prev;
195
196 for (prev = &_FcConstants;
197 (l = *prev);
198 prev = (const FcConstantList **) &(l->next))
199 {
200 if (l->consts == consts && l->nconsts == nconsts)
201 {
202 *prev = l->next;
203 free ((void *) l);
204 return FcTrue;
205 }
206 }
207 return FcFalse;
208 }
209
210 const FcConstant *
211 FcNameGetConstant (FcChar8 *string)
212 {
213 const FcConstantList *l;
214 int i;
215
216 for (l = _FcConstants; l; l = l->next)
217 {
218 for (i = 0; i < l->nconsts; i++)
219 if (!FcStrCmpIgnoreCase (string, l->consts[i].name))
220 return &l->consts[i];
221 }
222 return 0;
223 }
224
225 FcBool
226 FcNameConstant (FcChar8 *string, int *result)
227 {
228 const FcConstant *c;
229
230 if ((c = FcNameGetConstant(string)))
231 {
232 *result = c->value;
233 return FcTrue;
234 }
235 return FcFalse;
236 }
237
238 FcBool
239 FcNameBool (FcChar8 *v, FcBool *result)
240 {
241 char c0, c1;
242
243 c0 = *v;
244 if (isupper (c0))
245 c0 = tolower (c0);
246 if (c0 == 't' || c0 == 'y' || c0 == '1')
247 {
248 *result = FcTrue;
249 return FcTrue;
250 }
251 if (c0 == 'f' || c0 == 'n' || c0 == '0')
252 {
253 *result = FcFalse;
254 return FcTrue;
255 }
256 if (c0 == 'o')
257 {
258 c1 = v[1];
259 if (isupper (c1))
260 c1 = tolower (c1);
261 if (c1 == 'n')
262 {
263 *result = FcTrue;
264 return FcTrue;
265 }
266 if (c1 == 'f')
267 {
268 *result = FcFalse;
269 return FcTrue;
270 }
271 }
272 return FcFalse;
273 }
274
275 static FcValue
276 FcNameConvert (FcType type, FcChar8 *string, FcMatrix *m)
277 {
278 FcValue v;
279
280 v.type = type;
281 switch (v.type) {
282 case FcTypeInteger:
283 if (!FcNameConstant (string, &v.u.i))
284 v.u.i = atoi ((char *) string);
285 break;
286 case FcTypeString:
287 v.u.s = string;
288 break;
289 case FcTypeBool:
290 if (!FcNameBool (string, &v.u.b))
291 v.u.b = FcFalse;
292 break;
293 case FcTypeDouble:
294 v.u.d = strtod ((char *) string, 0);
295 break;
296 case FcTypeMatrix:
297 v.u.m = m;
298 sscanf ((char *) string, "%lg %lg %lg %lg", &m->xx, &m->xy, &m->yx, &m->yy);
299 break;
300 case FcTypeCharSet:
301 v.u.c = FcNameParseCharSet (string);
302 break;
303 default:
304 break;
305 }
306 return v;
307 }
308
309 static const FcChar8 *
310 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
311 {
312 FcChar8 c;
313
314 while ((c = *cur))
315 {
316 if (c == '\\')
317 {
318 ++cur;
319 if (!(c = *cur))
320 break;
321 }
322 else if (strchr (delim, c))
323 break;
324 ++cur;
325 *save++ = c;
326 }
327 *save = 0;
328 *last = *cur;
329 if (*cur)
330 cur++;
331 return cur;
332 }
333
334 FcPattern *
335 FcNameParse (const FcChar8 *name)
336 {
337 FcChar8 *save;
338 FcPattern *pat;
339 double d;
340 FcChar8 *e;
341 FcChar8 delim;
342 FcValue v;
343 FcMatrix m;
344 const FcObjectType *t;
345 const FcConstant *c;
346
347 save = malloc (strlen ((char *) name) + 1);
348 if (!save)
349 goto bail0;
350 pat = FcPatternCreate ();
351 if (!pat)
352 goto bail1;
353
354 for (;;)
355 {
356 name = FcNameFindNext (name, "-,:", save, &delim);
357 if (save[0])
358 {
359 if (!FcPatternAddString (pat, FC_FAMILY, save))
360 goto bail2;
361 }
362 if (delim != ',')
363 break;
364 }
365 if (delim == '-')
366 {
367 for (;;)
368 {
369 name = FcNameFindNext (name, "-,:", save, &delim);
370 d = strtod ((char *) save, (char **) &e);
371 if (e != save)
372 {
373 if (!FcPatternAddDouble (pat, FC_SIZE, d))
374 goto bail2;
375 }
376 if (delim != ',')
377 break;
378 }
379 }
380 while (delim == ':')
381 {
382 name = FcNameFindNext (name, "=_:", save, &delim);
383 if (save[0])
384 {
385 if (delim == '=' || delim == '_')
386 {
387 t = FcNameGetObjectType ((char *) save);
388 for (;;)
389 {
390 name = FcNameFindNext (name, ":,", save, &delim);
391 if (t)
392 {
393 v = FcNameConvert (t->type, save, &m);
394 if (!FcPatternAdd (pat, t->object, v, FcTrue))
395 {
396 if (v.type == FcTypeCharSet)
397 FcCharSetDestroy ((FcCharSet *) v.u.c);
398 goto bail2;
399 }
400 if (v.type == FcTypeCharSet)
401 FcCharSetDestroy ((FcCharSet *) v.u.c);
402 }
403 if (delim != ',')
404 break;
405 }
406 }
407 else
408 {
409 if ((c = FcNameGetConstant (save)))
410 {
411 if (!FcPatternAddInteger (pat, c->object, c->value))
412 goto bail2;
413 }
414 }
415 }
416 }
417
418 free (save);
419 return pat;
420
421 bail2:
422 FcPatternDestroy (pat);
423 bail1:
424 free (save);
425 bail0:
426 return 0;
427 }
428 static FcBool
429 FcNameUnparseString (FcStrBuf *buf,
430 const FcChar8 *string,
431 const FcChar8 *escape)
432 {
433 FcChar8 c;
434 while ((c = *string++))
435 {
436 if (escape && strchr ((char *) escape, (char) c))
437 {
438 if (!FcStrBufChar (buf, escape[0]))
439 return FcFalse;
440 }
441 if (!FcStrBufChar (buf, c))
442 return FcFalse;
443 }
444 return FcTrue;
445 }
446
447 static FcBool
448 FcNameUnparseValue (FcStrBuf *buf,
449 FcValue v,
450 FcChar8 *escape)
451 {
452 FcChar8 temp[1024];
453
454 switch (v.type) {
455 case FcTypeVoid:
456 return FcTrue;
457 case FcTypeInteger:
458 sprintf ((char *) temp, "%d", v.u.i);
459 return FcNameUnparseString (buf, temp, 0);
460 case FcTypeDouble:
461 sprintf ((char *) temp, "%g", v.u.d);
462 return FcNameUnparseString (buf, temp, 0);
463 case FcTypeString:
464 return FcNameUnparseString (buf, v.u.s, escape);
465 case FcTypeBool:
466 return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
467 case FcTypeMatrix:
468 sprintf ((char *) temp, "%g %g %g %g",
469 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
470 return FcNameUnparseString (buf, temp, 0);
471 case FcTypeCharSet:
472 return FcNameUnparseCharSet (buf, v.u.c);
473 case FcTypeFTFace:
474 return FcTrue;
475 }
476 return FcFalse;
477 }
478
479 static FcBool
480 FcNameUnparseValueList (FcStrBuf *buf,
481 FcValueList *v,
482 FcChar8 *escape)
483 {
484 while (v)
485 {
486 if (!FcNameUnparseValue (buf, v->value, escape))
487 return FcFalse;
488 if ((v = v->next))
489 if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
490 return FcFalse;
491 }
492 return FcTrue;
493 }
494
495 #define FC_ESCAPE_FIXED "\\-:,"
496 #define FC_ESCAPE_VARIABLE "\\=_:,"
497
498 FcChar8 *
499 FcNameUnparse (FcPattern *pat)
500 {
501 FcStrBuf buf;
502 FcChar8 buf_static[8192];
503 int i;
504 FcPatternElt *e;
505 const FcObjectTypeList *l;
506 const FcObjectType *o;
507
508 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
509 e = FcPatternFindElt (pat, FC_FAMILY);
510 if (e)
511 {
512 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
513 goto bail0;
514 }
515 e = FcPatternFindElt (pat, FC_SIZE);
516 if (e)
517 {
518 if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
519 goto bail0;
520 if (!FcNameUnparseValueList (&buf, e->values, (FcChar8 *) FC_ESCAPE_FIXED))
521 goto bail0;
522 }
523 for (l = _FcObjectTypes; l; l = l->next)
524 {
525 for (i = 0; i < l->ntypes; i++)
526 {
527 o = &l->types[i];
528 if (!strcmp (o->object, FC_FAMILY) ||
529 !strcmp (o->object, FC_SIZE) ||
530 !strcmp (o->object, FC_FILE))
531 continue;
532
533 e = FcPatternFindElt (pat, o->object);
534 if (e)
535 {
536 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
537 goto bail0;
538 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, (FcChar8 *) FC_ESCAPE_VARIABLE))
539 goto bail0;
540 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
541 goto bail0;
542 if (!FcNameUnparseValueList (&buf, e->values,
543 (FcChar8 *) FC_ESCAPE_VARIABLE))
544 goto bail0;
545 }
546 }
547 }
548 return FcStrBufDone (&buf);
549 bail0:
550 FcStrBufDestroy (&buf);
551 return 0;
552 }