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