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