]> git.wh0rd.org - fontconfig.git/blob - src/fcname.c
use FcToLower instead of tolower
[fontconfig.git] / src / fcname.c
1 /*
2 * $RCSId: xc/lib/fontconfig/src/fcname.c,v 1.15 2002/09/26 00:17:28 keithp Exp $
3 *
4 * Copyright © 2000 Keith Packard
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 { FC_FONTVERSION, FcTypeInteger },
64 };
65
66 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
67
68 typedef struct _FcObjectTypeList FcObjectTypeList;
69
70 struct _FcObjectTypeList {
71 const FcObjectTypeList *next;
72 const FcObjectType *types;
73 int ntypes;
74 };
75
76 static const FcObjectTypeList _FcBaseObjectTypesList = {
77 0,
78 _FcBaseObjectTypes,
79 NUM_OBJECT_TYPES
80 };
81
82 static const FcObjectTypeList *_FcObjectTypes = &_FcBaseObjectTypesList;
83
84 FcBool
85 FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
86 {
87 FcObjectTypeList *l;
88
89 l = (FcObjectTypeList *) malloc (sizeof (FcObjectTypeList));
90 if (!l)
91 return FcFalse;
92 FcMemAlloc (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
93 l->types = types;
94 l->ntypes = ntypes;
95 l->next = _FcObjectTypes;
96 _FcObjectTypes = l;
97 return FcTrue;
98 }
99
100 FcBool
101 FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
102 {
103 const FcObjectTypeList *l, **prev;
104
105 for (prev = &_FcObjectTypes;
106 (l = *prev);
107 prev = (const FcObjectTypeList **) &(l->next))
108 {
109 if (l->types == types && l->ntypes == ntypes)
110 {
111 *prev = l->next;
112 FcMemFree (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
113 free ((void *) l);
114 return FcTrue;
115 }
116 }
117 return FcFalse;
118 }
119
120 const FcObjectType *
121 FcNameGetObjectType (const char *object)
122 {
123 int i;
124 const FcObjectTypeList *l;
125 const FcObjectType *t;
126
127 for (l = _FcObjectTypes; l; l = l->next)
128 {
129 for (i = 0; i < l->ntypes; i++)
130 {
131 t = &l->types[i];
132 if (!strcmp (object, t->object))
133 return t;
134 }
135 }
136 return 0;
137 }
138
139 static const FcConstant _FcBaseConstants[] = {
140 { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, },
141 { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, },
142 { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, },
143 { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, },
144 { (FcChar8 *) "black", "weight", FC_WEIGHT_BLACK, },
145
146 { (FcChar8 *) "roman", "slant", FC_SLANT_ROMAN, },
147 { (FcChar8 *) "italic", "slant", FC_SLANT_ITALIC, },
148 { (FcChar8 *) "oblique", "slant", FC_SLANT_OBLIQUE, },
149
150 { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, },
151 { (FcChar8 *) "mono", "spacing", FC_MONO, },
152 { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, },
153
154 { (FcChar8 *) "unknown", "rgba", FC_RGBA_UNKNOWN },
155 { (FcChar8 *) "rgb", "rgba", FC_RGBA_RGB, },
156 { (FcChar8 *) "bgr", "rgba", FC_RGBA_BGR, },
157 { (FcChar8 *) "vrgb", "rgba", FC_RGBA_VRGB },
158 { (FcChar8 *) "vbgr", "rgba", FC_RGBA_VBGR },
159 { (FcChar8 *) "none", "rgba", FC_RGBA_NONE },
160 };
161
162 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
163
164 typedef struct _FcConstantList FcConstantList;
165
166 struct _FcConstantList {
167 const FcConstantList *next;
168 const FcConstant *consts;
169 int nconsts;
170 };
171
172 static const FcConstantList _FcBaseConstantList = {
173 0,
174 _FcBaseConstants,
175 NUM_FC_CONSTANTS
176 };
177
178 static const FcConstantList *_FcConstants = &_FcBaseConstantList;
179
180 FcBool
181 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
182 {
183 FcConstantList *l;
184
185 l = (FcConstantList *) malloc (sizeof (FcConstantList));
186 if (!l)
187 return FcFalse;
188 FcMemAlloc (FC_MEM_CONSTANT, sizeof (FcConstantList));
189 l->consts = consts;
190 l->nconsts = nconsts;
191 l->next = _FcConstants;
192 _FcConstants = l;
193 return FcTrue;
194 }
195
196 FcBool
197 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
198 {
199 const FcConstantList *l, **prev;
200
201 for (prev = &_FcConstants;
202 (l = *prev);
203 prev = (const FcConstantList **) &(l->next))
204 {
205 if (l->consts == consts && l->nconsts == nconsts)
206 {
207 *prev = l->next;
208 FcMemFree (FC_MEM_CONSTANT, sizeof (FcConstantList));
209 free ((void *) l);
210 return FcTrue;
211 }
212 }
213 return FcFalse;
214 }
215
216 const FcConstant *
217 FcNameGetConstant (FcChar8 *string)
218 {
219 const FcConstantList *l;
220 int i;
221
222 for (l = _FcConstants; l; l = l->next)
223 {
224 for (i = 0; i < l->nconsts; i++)
225 if (!FcStrCmpIgnoreCase (string, l->consts[i].name))
226 return &l->consts[i];
227 }
228 return 0;
229 }
230
231 FcBool
232 FcNameConstant (FcChar8 *string, int *result)
233 {
234 const FcConstant *c;
235
236 if ((c = FcNameGetConstant(string)))
237 {
238 *result = c->value;
239 return FcTrue;
240 }
241 return FcFalse;
242 }
243
244 FcBool
245 FcNameBool (FcChar8 *v, FcBool *result)
246 {
247 char c0, c1;
248
249 c0 = *v;
250 c0 = FcToLower (c0);
251 if (c0 == 't' || c0 == 'y' || c0 == '1')
252 {
253 *result = FcTrue;
254 return FcTrue;
255 }
256 if (c0 == 'f' || c0 == 'n' || c0 == '0')
257 {
258 *result = FcFalse;
259 return FcTrue;
260 }
261 if (c0 == 'o')
262 {
263 c1 = v[1];
264 c1 = FcToLower (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 }