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