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