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