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