]>
Commit | Line | Data |
---|---|---|
24330d27 | 1 | /* |
94421e40 | 2 | * $RCSId: xc/lib/fontconfig/src/fcname.c,v 1.15 2002/09/26 00:17:28 keithp Exp $ |
24330d27 | 3 | * |
46b51147 | 4 | * Copyright © 2000 Keith Packard |
24330d27 KP |
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 | ||
7f37423d | 31 | /* Please do not revoke any of these bindings. */ |
5fe09702 PL |
32 | /* The __DUMMY__ object enables callers to distinguish the error return |
33 | * of FcObjectToPtrLookup from FC_FAMILY's FcObjectPtr, which would | |
34 | * otherwise be 0. */ | |
24330d27 | 35 | static const FcObjectType _FcBaseObjectTypes[] = { |
5fe09702 | 36 | { "__DUMMY__", FcTypeVoid, }, |
24330d27 | 37 | { FC_FAMILY, FcTypeString, }, |
4f27c1c0 | 38 | { FC_FAMILYLANG, FcTypeString, }, |
24330d27 | 39 | { FC_STYLE, FcTypeString, }, |
4f27c1c0 KP |
40 | { FC_STYLELANG, FcTypeString, }, |
41 | { FC_FULLNAME, FcTypeString, }, | |
42 | { FC_FULLNAMELANG, FcTypeString, }, | |
24330d27 KP |
43 | { FC_SLANT, FcTypeInteger, }, |
44 | { FC_WEIGHT, FcTypeInteger, }, | |
81fa16c3 | 45 | { FC_WIDTH, FcTypeInteger, }, |
24330d27 | 46 | { FC_SIZE, FcTypeDouble, }, |
2a41214a | 47 | { FC_ASPECT, FcTypeDouble, }, |
24330d27 KP |
48 | { FC_PIXEL_SIZE, FcTypeDouble, }, |
49 | { FC_SPACING, FcTypeInteger, }, | |
50 | { FC_FOUNDRY, FcTypeString, }, | |
51 | /* { FC_CORE, FcTypeBool, }, */ | |
52 | { FC_ANTIALIAS, FcTypeBool, }, | |
f077d662 | 53 | { FC_HINT_STYLE, FcTypeInteger, }, |
4c003605 KP |
54 | { FC_HINTING, FcTypeBool, }, |
55 | { FC_VERTICAL_LAYOUT, FcTypeBool, }, | |
56 | { FC_AUTOHINT, FcTypeBool, }, | |
57 | { FC_GLOBAL_ADVANCE, FcTypeBool, }, | |
24330d27 KP |
58 | /* { FC_XLFD, FcTypeString, }, */ |
59 | { FC_FILE, FcTypeString, }, | |
60 | { FC_INDEX, FcTypeInteger, }, | |
61 | { FC_RASTERIZER, FcTypeString, }, | |
62 | { FC_OUTLINE, FcTypeBool, }, | |
63 | { FC_SCALABLE, FcTypeBool, }, | |
4c003605 | 64 | { FC_DPI, FcTypeDouble }, |
24330d27 KP |
65 | { FC_RGBA, FcTypeInteger, }, |
66 | { FC_SCALE, FcTypeDouble, }, | |
24330d27 | 67 | { FC_MINSPACE, FcTypeBool, }, |
13cdf607 | 68 | /* { FC_RENDER, FcTypeBool, },*/ |
24330d27 KP |
69 | { FC_CHAR_WIDTH, FcTypeInteger }, |
70 | { FC_CHAR_HEIGHT, FcTypeInteger }, | |
71 | { FC_MATRIX, FcTypeMatrix }, | |
72 | { FC_CHARSET, FcTypeCharSet }, | |
d8d73958 | 73 | { FC_LANG, FcTypeLangSet }, |
a342e87d | 74 | { FC_FONTVERSION, FcTypeInteger }, |
dbf68dd5 | 75 | { FC_CAPABILITY, FcTypeString }, |
537e3d23 | 76 | { FC_FONTFORMAT, FcTypeString }, |
414f7202 | 77 | { FC_EMBOLDEN, FcTypeBool }, |
720298e7 | 78 | { FC_EMBEDDED_BITMAP, FcTypeBool }, |
24330d27 KP |
79 | }; |
80 | ||
81 | #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0]) | |
82 | ||
13cdf607 PL |
83 | static FcObjectType * _FcUserObjectNames = 0; |
84 | static int user_obj_alloc = 0; | |
14143250 | 85 | static int next_basic_offset = NUM_OBJECT_TYPES; |
13cdf607 | 86 | |
24330d27 KP |
87 | typedef struct _FcObjectTypeList FcObjectTypeList; |
88 | ||
89 | struct _FcObjectTypeList { | |
90 | const FcObjectTypeList *next; | |
91 | const FcObjectType *types; | |
92 | int ntypes; | |
14143250 | 93 | int basic_offset; |
24330d27 KP |
94 | }; |
95 | ||
96 | static const FcObjectTypeList _FcBaseObjectTypesList = { | |
97 | 0, | |
98 | _FcBaseObjectTypes, | |
9ab79bdf PL |
99 | NUM_OBJECT_TYPES, |
100 | 0 | |
24330d27 KP |
101 | }; |
102 | ||
103 | static const FcObjectTypeList *_FcObjectTypes = &_FcBaseObjectTypesList; | |
104 | ||
105 | FcBool | |
106 | FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes) | |
107 | { | |
108 | FcObjectTypeList *l; | |
109 | ||
110 | l = (FcObjectTypeList *) malloc (sizeof (FcObjectTypeList)); | |
111 | if (!l) | |
112 | return FcFalse; | |
9dac3c59 | 113 | FcMemAlloc (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList)); |
24330d27 KP |
114 | l->types = types; |
115 | l->ntypes = ntypes; | |
116 | l->next = _FcObjectTypes; | |
14143250 PL |
117 | l->basic_offset = next_basic_offset; |
118 | next_basic_offset += ntypes; | |
24330d27 KP |
119 | _FcObjectTypes = l; |
120 | return FcTrue; | |
121 | } | |
122 | ||
7f37423d PL |
123 | static FcBool |
124 | FcNameUnregisterObjectTypesFree (const FcObjectType *types, int ntypes, | |
125 | FcBool do_free) | |
24330d27 KP |
126 | { |
127 | const FcObjectTypeList *l, **prev; | |
128 | ||
129 | for (prev = &_FcObjectTypes; | |
130 | (l = *prev); | |
131 | prev = (const FcObjectTypeList **) &(l->next)) | |
132 | { | |
133 | if (l->types == types && l->ntypes == ntypes) | |
134 | { | |
135 | *prev = l->next; | |
7f37423d PL |
136 | if (do_free) { |
137 | FcMemFree (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList)); | |
138 | free ((void *) l); | |
139 | } | |
24330d27 KP |
140 | return FcTrue; |
141 | } | |
142 | } | |
143 | return FcFalse; | |
144 | } | |
145 | ||
7f37423d PL |
146 | FcBool |
147 | FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes) | |
148 | { | |
149 | return FcNameUnregisterObjectTypesFree (types, ntypes, FcTrue); | |
150 | } | |
151 | ||
24330d27 KP |
152 | const FcObjectType * |
153 | FcNameGetObjectType (const char *object) | |
154 | { | |
155 | int i; | |
156 | const FcObjectTypeList *l; | |
157 | const FcObjectType *t; | |
158 | ||
159 | for (l = _FcObjectTypes; l; l = l->next) | |
160 | { | |
c6103dfb PL |
161 | if (l == (FcObjectTypeList*)_FcUserObjectNames) |
162 | continue; | |
163 | ||
24330d27 KP |
164 | for (i = 0; i < l->ntypes; i++) |
165 | { | |
166 | t = &l->types[i]; | |
bc9469ba | 167 | if (!strcmp (object, t->object)) |
24330d27 KP |
168 | return t; |
169 | } | |
170 | } | |
171 | return 0; | |
172 | } | |
173 | ||
13cdf607 PL |
174 | #define OBJECT_HASH_SIZE 31 |
175 | struct objectBucket { | |
176 | struct objectBucket *next; | |
177 | FcChar32 hash; | |
178 | int id; | |
179 | }; | |
180 | static struct objectBucket *FcObjectBuckets[OBJECT_HASH_SIZE]; | |
181 | ||
14143250 PL |
182 | /* Design constraint: biggest_known_ntypes must never change |
183 | * after any call to FcNameRegisterObjectTypes. */ | |
13cdf607 PL |
184 | static const FcObjectType *biggest_known_types = _FcBaseObjectTypes; |
185 | static FcBool allocated_biggest_known_types; | |
186 | static int biggest_known_ntypes = NUM_OBJECT_TYPES; | |
187 | static int biggest_known_count = 0; | |
188 | static char * biggest_ptr; | |
189 | ||
190 | ||
7f37423d PL |
191 | static FcObjectPtr |
192 | FcObjectToPtrLookup (const char * object) | |
4262e0b3 | 193 | { |
13cdf607 | 194 | FcObjectPtr i = 0, n; |
4262e0b3 | 195 | const FcObjectTypeList *l; |
a56e89ab PL |
196 | FcObjectType *t = _FcUserObjectNames, *origNames; |
197 | FcBool replace; | |
7f37423d | 198 | |
4262e0b3 PL |
199 | for (l = _FcObjectTypes; l; l = l->next) |
200 | { | |
201 | for (i = 0; i < l->ntypes; i++) | |
202 | { | |
13cdf607 | 203 | t = (FcObjectType *)&l->types[i]; |
4262e0b3 | 204 | if (!strcmp (object, t->object)) |
13cdf607 PL |
205 | { |
206 | if (l == (FcObjectTypeList*)_FcUserObjectNames) | |
14143250 | 207 | return -i; |
13cdf607 | 208 | else |
14143250 | 209 | return l->basic_offset + i; |
13cdf607 | 210 | } |
4262e0b3 PL |
211 | } |
212 | } | |
4262e0b3 | 213 | |
5fe09702 PL |
214 | /* We didn't match. Look for the application's FcObjectTypeList |
215 | * and replace it in-place. */ | |
a56e89ab | 216 | origNames = _FcUserObjectNames; |
13cdf607 PL |
217 | for (l = _FcObjectTypes; l; l = l->next) |
218 | { | |
219 | if (l->types == _FcUserObjectNames) | |
220 | break; | |
221 | } | |
7f37423d | 222 | |
a56e89ab | 223 | replace = l && l->types == _FcUserObjectNames; |
13cdf607 | 224 | if (!_FcUserObjectNames || |
a56e89ab | 225 | (replace && user_obj_alloc <= l->ntypes)) |
13cdf607 PL |
226 | { |
227 | int nt = user_obj_alloc + 4; | |
a56e89ab | 228 | FcObjectType * tt = realloc (_FcUserObjectNames, |
13cdf607 | 229 | nt * sizeof (FcObjectType)); |
a56e89ab | 230 | if (!tt) |
14143250 | 231 | return 0; |
a56e89ab | 232 | _FcUserObjectNames = tt; |
13cdf607 PL |
233 | user_obj_alloc = nt; |
234 | } | |
235 | ||
a56e89ab | 236 | if (replace) |
13cdf607 PL |
237 | { |
238 | n = l->ntypes; | |
239 | FcNameUnregisterObjectTypesFree (l->types, l->ntypes, FcFalse); | |
240 | } | |
241 | else | |
242 | n = 0; | |
243 | ||
244 | FcNameRegisterObjectTypes (_FcUserObjectNames, n+1); | |
245 | ||
246 | for (l = _FcObjectTypes; l; l = l->next) | |
247 | { | |
a56e89ab | 248 | if (l->types == origNames) |
13cdf607 PL |
249 | { |
250 | t = (FcObjectType *)l->types; | |
251 | break; | |
252 | } | |
253 | } | |
254 | ||
255 | if (!t) | |
256 | return 0; | |
257 | ||
258 | t[n].object = object; | |
259 | t[n].type = FcTypeVoid; | |
260 | ||
14143250 | 261 | return -n; |
13cdf607 | 262 | } |
7f37423d PL |
263 | |
264 | FcObjectPtr | |
265 | FcObjectToPtr (const char * name) | |
266 | { | |
267 | FcChar32 hash = FcStringHash ((const FcChar8 *) name); | |
268 | struct objectBucket **p; | |
269 | struct objectBucket *b; | |
270 | int size; | |
271 | ||
272 | for (p = &FcObjectBuckets[hash % OBJECT_HASH_SIZE]; (b = *p); p = &(b->next) | |
273 | ) | |
274 | if (b->hash == hash && !strcmp (name, (char *) (b + 1))) | |
275 | return b->id; | |
276 | size = sizeof (struct objectBucket) + strlen (name) + 1; | |
23787a8f PL |
277 | /* workaround glibc bug which reads strlen in groups of 4 */ |
278 | b = malloc (size + sizeof (int)); | |
279 | FcMemAlloc (FC_MEM_STATICSTR, size + sizeof(int)); | |
7f37423d | 280 | if (!b) |
14143250 | 281 | return 0; |
7f37423d PL |
282 | b->next = 0; |
283 | b->hash = hash; | |
284 | b->id = FcObjectToPtrLookup (name); | |
285 | strcpy ((char *) (b + 1), name); | |
286 | *p = b; | |
287 | return b->id; | |
288 | } | |
289 | ||
290 | void | |
291 | FcObjectStaticNameFini (void) | |
292 | { | |
293 | int i, size; | |
294 | struct objectBucket *b, *next; | |
295 | char *name; | |
296 | ||
297 | for (i = 0; i < OBJECT_HASH_SIZE; i++) | |
298 | { | |
299 | for (b = FcObjectBuckets[i]; b; b = next) | |
300 | { | |
301 | next = b->next; | |
302 | name = (char *) (b + 1); | |
303 | size = sizeof (struct objectBucket) + strlen (name) + 1; | |
304 | FcMemFree (FC_MEM_STATICSTR, size); | |
305 | free (b); | |
306 | } | |
307 | FcObjectBuckets[i] = 0; | |
308 | } | |
309 | } | |
310 | ||
4262e0b3 PL |
311 | const char * |
312 | FcObjectPtrU (FcObjectPtr si) | |
313 | { | |
14143250 PL |
314 | const FcObjectTypeList *l; |
315 | int i, j; | |
316 | ||
317 | if (si > 0) | |
318 | { | |
319 | if (si < biggest_known_ntypes) | |
320 | return biggest_known_types[si].object; | |
321 | ||
322 | j = 0; | |
323 | for (l = _FcObjectTypes; l; l = l->next) | |
324 | for (i = 0; i < l->ntypes; i++, j++) | |
325 | if (j == si) | |
326 | return l->types[i].object; | |
327 | } | |
328 | ||
329 | return _FcUserObjectNames[-si].object; | |
4262e0b3 PL |
330 | } |
331 | ||
332 | int | |
7f37423d | 333 | FcObjectNeededBytes () |
4262e0b3 | 334 | { |
7f37423d PL |
335 | int num = 0, i; |
336 | for (i = 0; i < biggest_known_ntypes; i++) | |
337 | { | |
338 | const char * t = biggest_known_types[i].object; | |
339 | num = num + strlen(t) + 1; | |
340 | } | |
341 | biggest_known_count = num; | |
342 | return num + sizeof(int); | |
4262e0b3 PL |
343 | } |
344 | ||
7fd7221e PL |
345 | int |
346 | FcObjectNeededBytesAlign (void) | |
347 | { | |
44415a07 | 348 | return fc_alignof (int) + fc_alignof (char); |
7fd7221e PL |
349 | } |
350 | ||
4262e0b3 PL |
351 | void * |
352 | FcObjectDistributeBytes (FcCache * metadata, void * block_ptr) | |
353 | { | |
7fd7221e | 354 | block_ptr = ALIGN (block_ptr, int); |
1c5b6345 | 355 | *(int *)block_ptr = biggest_known_ntypes; |
7f37423d | 356 | block_ptr = (int *) block_ptr + 1; |
7fd7221e | 357 | block_ptr = ALIGN (block_ptr, char); |
1c5b6345 | 358 | biggest_ptr = block_ptr; |
7f37423d | 359 | block_ptr = (char *) block_ptr + biggest_known_count; |
4262e0b3 PL |
360 | return block_ptr; |
361 | } | |
362 | ||
7f37423d | 363 | void |
d8951c0c | 364 | FcObjectSerialize (void) |
4262e0b3 | 365 | { |
7f37423d PL |
366 | int i; |
367 | for (i = 0; i < biggest_known_ntypes; i++) | |
368 | { | |
369 | const char * t = biggest_known_types[i].object; | |
370 | strcpy (biggest_ptr, t); | |
371 | biggest_ptr = biggest_ptr + strlen(t) + 1; | |
372 | } | |
4262e0b3 PL |
373 | } |
374 | ||
7f37423d | 375 | void * |
61571f3f | 376 | FcObjectUnserialize (FcCache * metadata, void *block_ptr) |
4262e0b3 | 377 | { |
7f37423d | 378 | int new_biggest; |
1c5b6345 | 379 | block_ptr = ALIGN (block_ptr, int); |
ac001094 | 380 | new_biggest = *(int *)block_ptr; |
7f37423d PL |
381 | block_ptr = (int *) block_ptr + 1; |
382 | if (biggest_known_ntypes < new_biggest) | |
383 | { | |
384 | int i; | |
385 | char * bp = (char *)block_ptr; | |
386 | FcObjectType * bn; | |
7f37423d PL |
387 | |
388 | bn = malloc (sizeof (const FcObjectType) * (new_biggest + 1)); | |
389 | if (!bn) | |
390 | return 0; | |
391 | ||
7f37423d PL |
392 | for (i = 0; i < new_biggest; i++) |
393 | { | |
394 | const FcObjectType * t = FcNameGetObjectType(bp); | |
395 | if (t) | |
396 | bn[i].type = t->type; | |
397 | else | |
398 | bn[i].type = FcTypeVoid; | |
399 | bn[i].object = bp; | |
400 | bp = bp + strlen(bp) + 1; | |
401 | } | |
402 | ||
403 | FcNameUnregisterObjectTypesFree (biggest_known_types, biggest_known_ntypes, FcFalse); | |
404 | if (allocated_biggest_known_types) | |
405 | { | |
406 | free ((FcObjectTypeList *)biggest_known_types); | |
407 | } | |
408 | else | |
409 | allocated_biggest_known_types = FcTrue; | |
410 | ||
411 | FcNameRegisterObjectTypes (bn, new_biggest); | |
412 | biggest_known_ntypes = new_biggest; | |
413 | biggest_known_types = (const FcObjectType *)bn; | |
414 | } | |
1c5b6345 | 415 | block_ptr = ALIGN (block_ptr, char); |
7f37423d PL |
416 | block_ptr = (char *) block_ptr + biggest_known_count; |
417 | return block_ptr; | |
4262e0b3 PL |
418 | } |
419 | ||
24330d27 | 420 | static const FcConstant _FcBaseConstants[] = { |
81fa16c3 KP |
421 | { (FcChar8 *) "thin", "weight", FC_WEIGHT_THIN, }, |
422 | { (FcChar8 *) "extralight", "weight", FC_WEIGHT_EXTRALIGHT, }, | |
423 | { (FcChar8 *) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT, }, | |
ccb3e93b | 424 | { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, }, |
1f71c4d8 | 425 | { (FcChar8 *) "book", "weight", FC_WEIGHT_BOOK, }, |
81fa16c3 | 426 | { (FcChar8 *) "regular", "weight", FC_WEIGHT_REGULAR, }, |
ccb3e93b KP |
427 | { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, }, |
428 | { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, }, | |
81fa16c3 | 429 | { (FcChar8 *) "semibold", "weight", FC_WEIGHT_DEMIBOLD, }, |
ccb3e93b | 430 | { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, }, |
81fa16c3 KP |
431 | { (FcChar8 *) "extrabold", "weight", FC_WEIGHT_EXTRABOLD, }, |
432 | { (FcChar8 *) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD, }, | |
ccb3e93b KP |
433 | { (FcChar8 *) "black", "weight", FC_WEIGHT_BLACK, }, |
434 | ||
435 | { (FcChar8 *) "roman", "slant", FC_SLANT_ROMAN, }, | |
436 | { (FcChar8 *) "italic", "slant", FC_SLANT_ITALIC, }, | |
437 | { (FcChar8 *) "oblique", "slant", FC_SLANT_OBLIQUE, }, | |
438 | ||
81fa16c3 KP |
439 | { (FcChar8 *) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED }, |
440 | { (FcChar8 *) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED }, | |
441 | { (FcChar8 *) "condensed", "width", FC_WIDTH_CONDENSED }, | |
442 | { (FcChar8 *) "semicondensed", "width", FC_WIDTH_SEMICONDENSED }, | |
443 | { (FcChar8 *) "normal", "width", FC_WIDTH_NORMAL }, | |
444 | { (FcChar8 *) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED }, | |
445 | { (FcChar8 *) "expanded", "width", FC_WIDTH_EXPANDED }, | |
446 | { (FcChar8 *) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED }, | |
447 | { (FcChar8 *) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED }, | |
448 | ||
ccb3e93b | 449 | { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, }, |
a05d257f | 450 | { (FcChar8 *) "dual", "spacing", FC_DUAL, }, |
ccb3e93b KP |
451 | { (FcChar8 *) "mono", "spacing", FC_MONO, }, |
452 | { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, }, | |
453 | ||
1852d490 | 454 | { (FcChar8 *) "unknown", "rgba", FC_RGBA_UNKNOWN }, |
ccb3e93b KP |
455 | { (FcChar8 *) "rgb", "rgba", FC_RGBA_RGB, }, |
456 | { (FcChar8 *) "bgr", "rgba", FC_RGBA_BGR, }, | |
457 | { (FcChar8 *) "vrgb", "rgba", FC_RGBA_VRGB }, | |
458 | { (FcChar8 *) "vbgr", "rgba", FC_RGBA_VBGR }, | |
1852d490 | 459 | { (FcChar8 *) "none", "rgba", FC_RGBA_NONE }, |
f077d662 OT |
460 | |
461 | { (FcChar8 *) "hintnone", "hintstyle", FC_HINT_NONE }, | |
462 | { (FcChar8 *) "hintslight", "hintstyle", FC_HINT_SLIGHT }, | |
463 | { (FcChar8 *) "hintmedium", "hintstyle", FC_HINT_MEDIUM }, | |
464 | { (FcChar8 *) "hintfull", "hintstyle", FC_HINT_FULL }, | |
24330d27 KP |
465 | }; |
466 | ||
467 | #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0]) | |
468 | ||
469 | typedef struct _FcConstantList FcConstantList; | |
470 | ||
471 | struct _FcConstantList { | |
472 | const FcConstantList *next; | |
473 | const FcConstant *consts; | |
474 | int nconsts; | |
475 | }; | |
476 | ||
477 | static const FcConstantList _FcBaseConstantList = { | |
478 | 0, | |
479 | _FcBaseConstants, | |
480 | NUM_FC_CONSTANTS | |
481 | }; | |
482 | ||
483 | static const FcConstantList *_FcConstants = &_FcBaseConstantList; | |
484 | ||
485 | FcBool | |
486 | FcNameRegisterConstants (const FcConstant *consts, int nconsts) | |
487 | { | |
488 | FcConstantList *l; | |
489 | ||
490 | l = (FcConstantList *) malloc (sizeof (FcConstantList)); | |
491 | if (!l) | |
492 | return FcFalse; | |
9dac3c59 | 493 | FcMemAlloc (FC_MEM_CONSTANT, sizeof (FcConstantList)); |
24330d27 KP |
494 | l->consts = consts; |
495 | l->nconsts = nconsts; | |
496 | l->next = _FcConstants; | |
497 | _FcConstants = l; | |
498 | return FcTrue; | |
499 | } | |
500 | ||
501 | FcBool | |
502 | FcNameUnregisterConstants (const FcConstant *consts, int nconsts) | |
503 | { | |
504 | const FcConstantList *l, **prev; | |
505 | ||
506 | for (prev = &_FcConstants; | |
507 | (l = *prev); | |
508 | prev = (const FcConstantList **) &(l->next)) | |
509 | { | |
510 | if (l->consts == consts && l->nconsts == nconsts) | |
511 | { | |
512 | *prev = l->next; | |
9dac3c59 | 513 | FcMemFree (FC_MEM_CONSTANT, sizeof (FcConstantList)); |
24330d27 KP |
514 | free ((void *) l); |
515 | return FcTrue; | |
516 | } | |
517 | } | |
518 | return FcFalse; | |
519 | } | |
520 | ||
521 | const FcConstant * | |
ccb3e93b | 522 | FcNameGetConstant (FcChar8 *string) |
24330d27 KP |
523 | { |
524 | const FcConstantList *l; | |
525 | int i; | |
94421e40 | 526 | |
24330d27 KP |
527 | for (l = _FcConstants; l; l = l->next) |
528 | { | |
529 | for (i = 0; i < l->nconsts; i++) | |
530 | if (!FcStrCmpIgnoreCase (string, l->consts[i].name)) | |
531 | return &l->consts[i]; | |
532 | } | |
533 | return 0; | |
534 | } | |
535 | ||
536 | FcBool | |
ccb3e93b | 537 | FcNameConstant (FcChar8 *string, int *result) |
24330d27 KP |
538 | { |
539 | const FcConstant *c; | |
540 | ||
541 | if ((c = FcNameGetConstant(string))) | |
542 | { | |
543 | *result = c->value; | |
544 | return FcTrue; | |
545 | } | |
546 | return FcFalse; | |
547 | } | |
548 | ||
549 | FcBool | |
ca60d2b5 | 550 | FcNameBool (const FcChar8 *v, FcBool *result) |
24330d27 KP |
551 | { |
552 | char c0, c1; | |
553 | ||
554 | c0 = *v; | |
94421e40 | 555 | c0 = FcToLower (c0); |
24330d27 KP |
556 | if (c0 == 't' || c0 == 'y' || c0 == '1') |
557 | { | |
558 | *result = FcTrue; | |
559 | return FcTrue; | |
560 | } | |
561 | if (c0 == 'f' || c0 == 'n' || c0 == '0') | |
562 | { | |
563 | *result = FcFalse; | |
564 | return FcTrue; | |
565 | } | |
566 | if (c0 == 'o') | |
567 | { | |
568 | c1 = v[1]; | |
94421e40 | 569 | c1 = FcToLower (c1); |
24330d27 KP |
570 | if (c1 == 'n') |
571 | { | |
572 | *result = FcTrue; | |
573 | return FcTrue; | |
574 | } | |
575 | if (c1 == 'f') | |
576 | { | |
577 | *result = FcFalse; | |
578 | return FcTrue; | |
579 | } | |
580 | } | |
581 | return FcFalse; | |
582 | } | |
583 | ||
584 | static FcValue | |
ccb3e93b | 585 | FcNameConvert (FcType type, FcChar8 *string, FcMatrix *m) |
24330d27 KP |
586 | { |
587 | FcValue v; | |
588 | ||
589 | v.type = type; | |
590 | switch (v.type) { | |
591 | case FcTypeInteger: | |
592 | if (!FcNameConstant (string, &v.u.i)) | |
ccb3e93b | 593 | v.u.i = atoi ((char *) string); |
24330d27 KP |
594 | break; |
595 | case FcTypeString: | |
7f37423d | 596 | v.u.s = FcStrStaticName(string); |
24330d27 KP |
597 | break; |
598 | case FcTypeBool: | |
599 | if (!FcNameBool (string, &v.u.b)) | |
600 | v.u.b = FcFalse; | |
601 | break; | |
602 | case FcTypeDouble: | |
ccb3e93b | 603 | v.u.d = strtod ((char *) string, 0); |
24330d27 KP |
604 | break; |
605 | case FcTypeMatrix: | |
4262e0b3 | 606 | v.u.m = m; |
ccb3e93b | 607 | sscanf ((char *) string, "%lg %lg %lg %lg", &m->xx, &m->xy, &m->yx, &m->yy); |
24330d27 KP |
608 | break; |
609 | case FcTypeCharSet: | |
4262e0b3 | 610 | v.u.c = FcNameParseCharSet (string); |
24330d27 | 611 | break; |
d8d73958 | 612 | case FcTypeLangSet: |
4262e0b3 | 613 | v.u.l = FcNameParseLangSet (string); |
d8d73958 | 614 | break; |
24330d27 KP |
615 | default: |
616 | break; | |
617 | } | |
618 | return v; | |
619 | } | |
620 | ||
ccb3e93b KP |
621 | static const FcChar8 * |
622 | FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last) | |
24330d27 | 623 | { |
ccb3e93b | 624 | FcChar8 c; |
24330d27 KP |
625 | |
626 | while ((c = *cur)) | |
627 | { | |
628 | if (c == '\\') | |
629 | { | |
630 | ++cur; | |
631 | if (!(c = *cur)) | |
632 | break; | |
633 | } | |
634 | else if (strchr (delim, c)) | |
635 | break; | |
636 | ++cur; | |
637 | *save++ = c; | |
638 | } | |
639 | *save = 0; | |
640 | *last = *cur; | |
641 | if (*cur) | |
642 | cur++; | |
643 | return cur; | |
644 | } | |
645 | ||
646 | FcPattern * | |
ccb3e93b | 647 | FcNameParse (const FcChar8 *name) |
24330d27 | 648 | { |
ccb3e93b | 649 | FcChar8 *save; |
24330d27 KP |
650 | FcPattern *pat; |
651 | double d; | |
ccb3e93b KP |
652 | FcChar8 *e; |
653 | FcChar8 delim; | |
24330d27 KP |
654 | FcValue v; |
655 | FcMatrix m; | |
656 | const FcObjectType *t; | |
657 | const FcConstant *c; | |
658 | ||
9dac3c59 | 659 | /* freed below */ |
ccb3e93b | 660 | save = malloc (strlen ((char *) name) + 1); |
24330d27 KP |
661 | if (!save) |
662 | goto bail0; | |
663 | pat = FcPatternCreate (); | |
664 | if (!pat) | |
665 | goto bail1; | |
666 | ||
667 | for (;;) | |
668 | { | |
669 | name = FcNameFindNext (name, "-,:", save, &delim); | |
670 | if (save[0]) | |
671 | { | |
672 | if (!FcPatternAddString (pat, FC_FAMILY, save)) | |
673 | goto bail2; | |
674 | } | |
675 | if (delim != ',') | |
676 | break; | |
677 | } | |
678 | if (delim == '-') | |
679 | { | |
680 | for (;;) | |
681 | { | |
682 | name = FcNameFindNext (name, "-,:", save, &delim); | |
ccb3e93b | 683 | d = strtod ((char *) save, (char **) &e); |
24330d27 KP |
684 | if (e != save) |
685 | { | |
686 | if (!FcPatternAddDouble (pat, FC_SIZE, d)) | |
687 | goto bail2; | |
688 | } | |
689 | if (delim != ',') | |
690 | break; | |
691 | } | |
692 | } | |
693 | while (delim == ':') | |
694 | { | |
695 | name = FcNameFindNext (name, "=_:", save, &delim); | |
696 | if (save[0]) | |
697 | { | |
698 | if (delim == '=' || delim == '_') | |
699 | { | |
ccb3e93b | 700 | t = FcNameGetObjectType ((char *) save); |
24330d27 KP |
701 | for (;;) |
702 | { | |
703 | name = FcNameFindNext (name, ":,", save, &delim); | |
5fe09702 | 704 | if (t && strcmp (t->object, _FcBaseObjectTypes[0].object)) |
24330d27 KP |
705 | { |
706 | v = FcNameConvert (t->type, save, &m); | |
707 | if (!FcPatternAdd (pat, t->object, v, FcTrue)) | |
708 | { | |
d8d73958 KP |
709 | switch (v.type) { |
710 | case FcTypeCharSet: | |
4262e0b3 | 711 | FcCharSetDestroy ((FcCharSet *) v.u.c); |
d8d73958 KP |
712 | break; |
713 | case FcTypeLangSet: | |
4262e0b3 | 714 | FcLangSetDestroy ((FcLangSet *) v.u.l); |
d8d73958 KP |
715 | break; |
716 | default: | |
717 | break; | |
718 | } | |
24330d27 KP |
719 | goto bail2; |
720 | } | |
d8d73958 KP |
721 | switch (v.type) { |
722 | case FcTypeCharSet: | |
4262e0b3 | 723 | FcCharSetDestroy ((FcCharSet *) v.u.c); |
d8d73958 KP |
724 | break; |
725 | case FcTypeLangSet: | |
4262e0b3 | 726 | FcLangSetDestroy ((FcLangSet *) v.u.l); |
d8d73958 KP |
727 | break; |
728 | default: | |
729 | break; | |
730 | } | |
24330d27 KP |
731 | } |
732 | if (delim != ',') | |
733 | break; | |
734 | } | |
735 | } | |
736 | else | |
737 | { | |
738 | if ((c = FcNameGetConstant (save))) | |
739 | { | |
740 | if (!FcPatternAddInteger (pat, c->object, c->value)) | |
741 | goto bail2; | |
742 | } | |
743 | } | |
744 | } | |
745 | } | |
746 | ||
747 | free (save); | |
748 | return pat; | |
749 | ||
750 | bail2: | |
751 | FcPatternDestroy (pat); | |
752 | bail1: | |
753 | free (save); | |
754 | bail0: | |
755 | return 0; | |
756 | } | |
24330d27 | 757 | static FcBool |
c2e7c611 | 758 | FcNameUnparseString (FcStrBuf *buf, |
24330d27 KP |
759 | const FcChar8 *string, |
760 | const FcChar8 *escape) | |
761 | { | |
762 | FcChar8 c; | |
763 | while ((c = *string++)) | |
764 | { | |
765 | if (escape && strchr ((char *) escape, (char) c)) | |
766 | { | |
c2e7c611 | 767 | if (!FcStrBufChar (buf, escape[0])) |
24330d27 KP |
768 | return FcFalse; |
769 | } | |
c2e7c611 | 770 | if (!FcStrBufChar (buf, c)) |
24330d27 KP |
771 | return FcFalse; |
772 | } | |
773 | return FcTrue; | |
774 | } | |
775 | ||
776 | static FcBool | |
c2e7c611 | 777 | FcNameUnparseValue (FcStrBuf *buf, |
4262e0b3 | 778 | FcValue *v0, |
24330d27 KP |
779 | FcChar8 *escape) |
780 | { | |
781 | FcChar8 temp[1024]; | |
4262e0b3 | 782 | FcValue v = FcValueCanonicalize(v0); |
24330d27 KP |
783 | |
784 | switch (v.type) { | |
785 | case FcTypeVoid: | |
786 | return FcTrue; | |
787 | case FcTypeInteger: | |
788 | sprintf ((char *) temp, "%d", v.u.i); | |
789 | return FcNameUnparseString (buf, temp, 0); | |
790 | case FcTypeDouble: | |
791 | sprintf ((char *) temp, "%g", v.u.d); | |
792 | return FcNameUnparseString (buf, temp, 0); | |
793 | case FcTypeString: | |
4262e0b3 | 794 | return FcNameUnparseString (buf, v.u.s, escape); |
24330d27 | 795 | case FcTypeBool: |
ccb3e93b | 796 | return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0); |
24330d27 KP |
797 | case FcTypeMatrix: |
798 | sprintf ((char *) temp, "%g %g %g %g", | |
4262e0b3 | 799 | v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy); |
24330d27 KP |
800 | return FcNameUnparseString (buf, temp, 0); |
801 | case FcTypeCharSet: | |
4262e0b3 | 802 | return FcNameUnparseCharSet (buf, v.u.c); |
d8d73958 | 803 | case FcTypeLangSet: |
4262e0b3 | 804 | return FcNameUnparseLangSet (buf, v.u.l); |
88c747e2 KP |
805 | case FcTypeFTFace: |
806 | return FcTrue; | |
24330d27 KP |
807 | } |
808 | return FcFalse; | |
809 | } | |
810 | ||
811 | static FcBool | |
c2e7c611 | 812 | FcNameUnparseValueList (FcStrBuf *buf, |
cd2ec1a9 | 813 | FcValueListPtr v, |
ccb3e93b | 814 | FcChar8 *escape) |
24330d27 | 815 | { |
cd2ec1a9 | 816 | while (FcValueListPtrU(v)) |
24330d27 | 817 | { |
9ab79bdf | 818 | if (!FcNameUnparseValue (buf, &FcValueListPtrU(v)->value, escape)) |
24330d27 | 819 | return FcFalse; |
cd2ec1a9 | 820 | if (FcValueListPtrU(v = FcValueListPtrU(v)->next)) |
ccb3e93b | 821 | if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0)) |
24330d27 KP |
822 | return FcFalse; |
823 | } | |
824 | return FcTrue; | |
825 | } | |
826 | ||
827 | #define FC_ESCAPE_FIXED "\\-:," | |
828 | #define FC_ESCAPE_VARIABLE "\\=_:," | |
829 | ||
830 | FcChar8 * | |
831 | FcNameUnparse (FcPattern *pat) | |
2fa3f27e PL |
832 | { |
833 | return FcNameUnparseEscaped (pat, FcTrue); | |
834 | } | |
835 | ||
836 | FcChar8 * | |
837 | FcNameUnparseEscaped (FcPattern *pat, FcBool escape) | |
24330d27 | 838 | { |
c2e7c611 | 839 | FcStrBuf buf; |
24330d27 KP |
840 | FcChar8 buf_static[8192]; |
841 | int i; | |
842 | FcPatternElt *e; | |
843 | const FcObjectTypeList *l; | |
844 | const FcObjectType *o; | |
845 | ||
c2e7c611 | 846 | FcStrBufInit (&buf, buf_static, sizeof (buf_static)); |
e9be9cd1 | 847 | e = FcPatternFindElt (pat, FC_FAMILY); |
24330d27 KP |
848 | if (e) |
849 | { | |
2fa3f27e | 850 | if (!FcNameUnparseValueList (&buf, e->values, escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0)) |
24330d27 KP |
851 | goto bail0; |
852 | } | |
e9be9cd1 | 853 | e = FcPatternFindElt (pat, FC_SIZE); |
24330d27 KP |
854 | if (e) |
855 | { | |
ccb3e93b | 856 | if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0)) |
24330d27 | 857 | goto bail0; |
2fa3f27e | 858 | if (!FcNameUnparseValueList (&buf, e->values, escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0)) |
24330d27 KP |
859 | goto bail0; |
860 | } | |
861 | for (l = _FcObjectTypes; l; l = l->next) | |
862 | { | |
863 | for (i = 0; i < l->ntypes; i++) | |
864 | { | |
865 | o = &l->types[i]; | |
866 | if (!strcmp (o->object, FC_FAMILY) || | |
867 | !strcmp (o->object, FC_SIZE) || | |
868 | !strcmp (o->object, FC_FILE)) | |
869 | continue; | |
870 | ||
e9be9cd1 | 871 | e = FcPatternFindElt (pat, o->object); |
24330d27 KP |
872 | if (e) |
873 | { | |
ccb3e93b | 874 | if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0)) |
24330d27 | 875 | goto bail0; |
2fa3f27e | 876 | if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, escape ? (FcChar8 *) FC_ESCAPE_VARIABLE : 0)) |
24330d27 | 877 | goto bail0; |
ccb3e93b | 878 | if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0)) |
24330d27 | 879 | goto bail0; |
2fa3f27e PL |
880 | if (!FcNameUnparseValueList (&buf, e->values, escape ? |
881 | (FcChar8 *) FC_ESCAPE_VARIABLE : 0)) | |
24330d27 KP |
882 | goto bail0; |
883 | } | |
884 | } | |
885 | } | |
c2e7c611 | 886 | return FcStrBufDone (&buf); |
24330d27 | 887 | bail0: |
c2e7c611 | 888 | FcStrBufDestroy (&buf); |
24330d27 KP |
889 | return 0; |
890 | } |