]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * fontconfig/src/fccharset.c | |
3 | * | |
4 | * Copyright © 2001 Keith Packard | |
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 "fcint.h" | |
26 | #include <stdlib.h> | |
27 | ||
28 | /* #define CHECK */ | |
29 | ||
30 | FcCharSet * | |
31 | FcCharSetCreate (void) | |
32 | { | |
33 | FcCharSet *fcs; | |
34 | ||
35 | fcs = (FcCharSet *) malloc (sizeof (FcCharSet)); | |
36 | if (!fcs) | |
37 | return 0; | |
38 | FcMemAlloc (FC_MEM_CHARSET, sizeof (FcCharSet)); | |
39 | fcs->ref = 1; | |
40 | fcs->num = 0; | |
41 | fcs->leaves_offset = 0; | |
42 | fcs->numbers_offset = 0; | |
43 | return fcs; | |
44 | } | |
45 | ||
46 | FcCharSet * | |
47 | FcCharSetNew (void) | |
48 | { | |
49 | return FcCharSetCreate (); | |
50 | } | |
51 | ||
52 | void | |
53 | FcCharSetDestroy (FcCharSet *fcs) | |
54 | { | |
55 | int i; | |
56 | ||
57 | if (fcs->ref == FC_REF_CONSTANT) | |
58 | { | |
59 | FcCacheObjectDereference (fcs); | |
60 | return; | |
61 | } | |
62 | if (--fcs->ref > 0) | |
63 | return; | |
64 | for (i = 0; i < fcs->num; i++) | |
65 | { | |
66 | FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf)); | |
67 | free (FcCharSetLeaf (fcs, i)); | |
68 | } | |
69 | if (fcs->num) | |
70 | { | |
71 | FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (intptr_t)); | |
72 | free (FcCharSetLeaves (fcs)); | |
73 | } | |
74 | if (fcs->num) | |
75 | { | |
76 | FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcChar16)); | |
77 | free (FcCharSetNumbers (fcs)); | |
78 | } | |
79 | FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet)); | |
80 | free (fcs); | |
81 | } | |
82 | ||
83 | /* | |
84 | * Search for the leaf containing with the specified num. | |
85 | * Return its index if it exists, otherwise return negative of | |
86 | * the (position + 1) where it should be inserted | |
87 | */ | |
88 | ||
89 | ||
90 | static int | |
91 | FcCharSetFindLeafForward (const FcCharSet *fcs, int start, FcChar16 num) | |
92 | { | |
93 | FcChar16 *numbers = FcCharSetNumbers(fcs); | |
94 | FcChar16 page; | |
95 | int low = start; | |
96 | int high = fcs->num - 1; | |
97 | ||
98 | if (!numbers) | |
99 | return -1; | |
100 | while (low <= high) | |
101 | { | |
102 | int mid = (low + high) >> 1; | |
103 | page = numbers[mid]; | |
104 | if (page == num) | |
105 | return mid; | |
106 | if (page < num) | |
107 | low = mid + 1; | |
108 | else | |
109 | high = mid - 1; | |
110 | } | |
111 | if (high < 0 || (high < fcs->num && numbers[high] < num)) | |
112 | high++; | |
113 | return -(high + 1); | |
114 | } | |
115 | ||
116 | /* | |
117 | * Locate the leaf containing the specified char, return | |
118 | * its index if it exists, otherwise return negative of | |
119 | * the (position + 1) where it should be inserted | |
120 | */ | |
121 | ||
122 | static int | |
123 | FcCharSetFindLeafPos (const FcCharSet *fcs, FcChar32 ucs4) | |
124 | { | |
125 | return FcCharSetFindLeafForward (fcs, 0, ucs4 >> 8); | |
126 | } | |
127 | ||
128 | static FcCharLeaf * | |
129 | FcCharSetFindLeaf (const FcCharSet *fcs, FcChar32 ucs4) | |
130 | { | |
131 | int pos = FcCharSetFindLeafPos (fcs, ucs4); | |
132 | if (pos >= 0) | |
133 | return FcCharSetLeaf(fcs, pos); | |
134 | return 0; | |
135 | } | |
136 | ||
137 | static FcBool | |
138 | FcCharSetPutLeaf (FcCharSet *fcs, | |
139 | FcChar32 ucs4, | |
140 | FcCharLeaf *leaf, | |
141 | int pos) | |
142 | { | |
143 | intptr_t *leaves = FcCharSetLeaves (fcs); | |
144 | FcChar16 *numbers = FcCharSetNumbers (fcs); | |
145 | ||
146 | ucs4 >>= 8; | |
147 | if (ucs4 >= 0x10000) | |
148 | return FcFalse; | |
149 | if (!fcs->num) | |
150 | leaves = malloc (sizeof (*leaves)); | |
151 | else | |
152 | { | |
153 | intptr_t *new_leaves = realloc (leaves, (fcs->num + 1) * | |
154 | sizeof (*leaves)); | |
155 | intptr_t distance = (intptr_t) new_leaves - (intptr_t) leaves; | |
156 | ||
157 | if (new_leaves && distance) | |
158 | { | |
159 | int i; | |
160 | ||
161 | for (i = 0; i < fcs->num; i++) | |
162 | new_leaves[i] -= distance; | |
163 | } | |
164 | leaves = new_leaves; | |
165 | } | |
166 | if (!leaves) | |
167 | return FcFalse; | |
168 | ||
169 | if (fcs->num) | |
170 | FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (intptr_t)); | |
171 | FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (intptr_t)); | |
172 | fcs->leaves_offset = FcPtrToOffset (fcs, leaves); | |
173 | ||
174 | if (!fcs->num) | |
175 | numbers = malloc (sizeof (FcChar16)); | |
176 | else | |
177 | numbers = realloc (numbers, (fcs->num + 1) * sizeof (FcChar16)); | |
178 | if (!numbers) | |
179 | return FcFalse; | |
180 | ||
181 | if (fcs->num) | |
182 | FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcChar16)); | |
183 | FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcChar16)); | |
184 | fcs->numbers_offset = FcPtrToOffset (fcs, numbers); | |
185 | ||
186 | memmove (leaves + pos + 1, leaves + pos, | |
187 | (fcs->num - pos) * sizeof (*leaves)); | |
188 | memmove (numbers + pos + 1, numbers + pos, | |
189 | (fcs->num - pos) * sizeof (*numbers)); | |
190 | numbers[pos] = (FcChar16) ucs4; | |
191 | leaves[pos] = FcPtrToOffset (leaves, leaf); | |
192 | fcs->num++; | |
193 | return FcTrue; | |
194 | } | |
195 | ||
196 | /* | |
197 | * Locate the leaf containing the specified char, creating it | |
198 | * if desired | |
199 | */ | |
200 | ||
201 | FcCharLeaf * | |
202 | FcCharSetFindLeafCreate (FcCharSet *fcs, FcChar32 ucs4) | |
203 | { | |
204 | int pos; | |
205 | FcCharLeaf *leaf; | |
206 | ||
207 | pos = FcCharSetFindLeafPos (fcs, ucs4); | |
208 | if (pos >= 0) | |
209 | return FcCharSetLeaf(fcs, pos); | |
210 | ||
211 | leaf = calloc (1, sizeof (FcCharLeaf)); | |
212 | if (!leaf) | |
213 | return 0; | |
214 | ||
215 | pos = -pos - 1; | |
216 | if (!FcCharSetPutLeaf (fcs, ucs4, leaf, pos)) | |
217 | { | |
218 | free (leaf); | |
219 | return 0; | |
220 | } | |
221 | FcMemAlloc (FC_MEM_CHARLEAF, sizeof (FcCharLeaf)); | |
222 | return leaf; | |
223 | } | |
224 | ||
225 | static FcBool | |
226 | FcCharSetInsertLeaf (FcCharSet *fcs, FcChar32 ucs4, FcCharLeaf *leaf) | |
227 | { | |
228 | int pos; | |
229 | ||
230 | pos = FcCharSetFindLeafPos (fcs, ucs4); | |
231 | if (pos >= 0) | |
232 | { | |
233 | FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf)); | |
234 | free (FcCharSetLeaf (fcs, pos)); | |
235 | FcCharSetLeaves(fcs)[pos] = FcPtrToOffset (FcCharSetLeaves(fcs), | |
236 | leaf); | |
237 | return FcTrue; | |
238 | } | |
239 | pos = -pos - 1; | |
240 | return FcCharSetPutLeaf (fcs, ucs4, leaf, pos); | |
241 | } | |
242 | ||
243 | FcBool | |
244 | FcCharSetAddChar (FcCharSet *fcs, FcChar32 ucs4) | |
245 | { | |
246 | FcCharLeaf *leaf; | |
247 | FcChar32 *b; | |
248 | ||
249 | if (fcs->ref == FC_REF_CONSTANT) | |
250 | return FcFalse; | |
251 | leaf = FcCharSetFindLeafCreate (fcs, ucs4); | |
252 | if (!leaf) | |
253 | return FcFalse; | |
254 | b = &leaf->map[(ucs4 & 0xff) >> 5]; | |
255 | *b |= (1 << (ucs4 & 0x1f)); | |
256 | return FcTrue; | |
257 | } | |
258 | ||
259 | /* | |
260 | * An iterator for the leaves of a charset | |
261 | */ | |
262 | ||
263 | typedef struct _fcCharSetIter { | |
264 | FcCharLeaf *leaf; | |
265 | FcChar32 ucs4; | |
266 | int pos; | |
267 | } FcCharSetIter; | |
268 | ||
269 | /* | |
270 | * Set iter->leaf to the leaf containing iter->ucs4 or higher | |
271 | */ | |
272 | ||
273 | static void | |
274 | FcCharSetIterSet (const FcCharSet *fcs, FcCharSetIter *iter) | |
275 | { | |
276 | int pos = FcCharSetFindLeafPos (fcs, iter->ucs4); | |
277 | ||
278 | if (pos < 0) | |
279 | { | |
280 | pos = -pos - 1; | |
281 | if (pos == fcs->num) | |
282 | { | |
283 | iter->ucs4 = ~0; | |
284 | iter->leaf = 0; | |
285 | return; | |
286 | } | |
287 | iter->ucs4 = (FcChar32) FcCharSetNumbers(fcs)[pos] << 8; | |
288 | } | |
289 | iter->leaf = FcCharSetLeaf(fcs, pos); | |
290 | iter->pos = pos; | |
291 | } | |
292 | ||
293 | static void | |
294 | FcCharSetIterNext (const FcCharSet *fcs, FcCharSetIter *iter) | |
295 | { | |
296 | int pos = iter->pos + 1; | |
297 | if (pos >= fcs->num) | |
298 | { | |
299 | iter->ucs4 = ~0; | |
300 | iter->leaf = 0; | |
301 | } | |
302 | else | |
303 | { | |
304 | iter->ucs4 = (FcChar32) FcCharSetNumbers(fcs)[pos] << 8; | |
305 | iter->leaf = FcCharSetLeaf(fcs, pos); | |
306 | iter->pos = pos; | |
307 | } | |
308 | } | |
309 | ||
310 | ||
311 | static void | |
312 | FcCharSetIterStart (const FcCharSet *fcs, FcCharSetIter *iter) | |
313 | { | |
314 | iter->ucs4 = 0; | |
315 | iter->pos = 0; | |
316 | FcCharSetIterSet (fcs, iter); | |
317 | } | |
318 | ||
319 | FcCharSet * | |
320 | FcCharSetCopy (FcCharSet *src) | |
321 | { | |
322 | if (src->ref != FC_REF_CONSTANT) | |
323 | src->ref++; | |
324 | else | |
325 | FcCacheObjectReference (src); | |
326 | return src; | |
327 | } | |
328 | ||
329 | FcBool | |
330 | FcCharSetEqual (const FcCharSet *a, const FcCharSet *b) | |
331 | { | |
332 | FcCharSetIter ai, bi; | |
333 | int i; | |
334 | ||
335 | if (a == b) | |
336 | return FcTrue; | |
337 | for (FcCharSetIterStart (a, &ai), FcCharSetIterStart (b, &bi); | |
338 | ai.leaf && bi.leaf; | |
339 | FcCharSetIterNext (a, &ai), FcCharSetIterNext (b, &bi)) | |
340 | { | |
341 | if (ai.ucs4 != bi.ucs4) | |
342 | return FcFalse; | |
343 | for (i = 0; i < 256/32; i++) | |
344 | if (ai.leaf->map[i] != bi.leaf->map[i]) | |
345 | return FcFalse; | |
346 | } | |
347 | return ai.leaf == bi.leaf; | |
348 | } | |
349 | ||
350 | static FcBool | |
351 | FcCharSetAddLeaf (FcCharSet *fcs, | |
352 | FcChar32 ucs4, | |
353 | FcCharLeaf *leaf) | |
354 | { | |
355 | FcCharLeaf *new = FcCharSetFindLeafCreate (fcs, ucs4); | |
356 | if (!new) | |
357 | return FcFalse; | |
358 | *new = *leaf; | |
359 | return FcTrue; | |
360 | } | |
361 | ||
362 | static FcCharSet * | |
363 | FcCharSetOperate (const FcCharSet *a, | |
364 | const FcCharSet *b, | |
365 | FcBool (*overlap) (FcCharLeaf *result, | |
366 | const FcCharLeaf *al, | |
367 | const FcCharLeaf *bl), | |
368 | FcBool aonly, | |
369 | FcBool bonly) | |
370 | { | |
371 | FcCharSet *fcs; | |
372 | FcCharSetIter ai, bi; | |
373 | ||
374 | fcs = FcCharSetCreate (); | |
375 | if (!fcs) | |
376 | goto bail0; | |
377 | FcCharSetIterStart (a, &ai); | |
378 | FcCharSetIterStart (b, &bi); | |
379 | while ((ai.leaf || (bonly && bi.leaf)) && (bi.leaf || (aonly && ai.leaf))) | |
380 | { | |
381 | if (ai.ucs4 < bi.ucs4) | |
382 | { | |
383 | if (aonly) | |
384 | { | |
385 | if (!FcCharSetAddLeaf (fcs, ai.ucs4, ai.leaf)) | |
386 | goto bail1; | |
387 | FcCharSetIterNext (a, &ai); | |
388 | } | |
389 | else | |
390 | { | |
391 | ai.ucs4 = bi.ucs4; | |
392 | FcCharSetIterSet (a, &ai); | |
393 | } | |
394 | } | |
395 | else if (bi.ucs4 < ai.ucs4 ) | |
396 | { | |
397 | if (bonly) | |
398 | { | |
399 | if (!FcCharSetAddLeaf (fcs, bi.ucs4, bi.leaf)) | |
400 | goto bail1; | |
401 | FcCharSetIterNext (b, &bi); | |
402 | } | |
403 | else | |
404 | { | |
405 | bi.ucs4 = ai.ucs4; | |
406 | FcCharSetIterSet (b, &bi); | |
407 | } | |
408 | } | |
409 | else | |
410 | { | |
411 | FcCharLeaf leaf; | |
412 | ||
413 | if ((*overlap) (&leaf, ai.leaf, bi.leaf)) | |
414 | { | |
415 | if (!FcCharSetAddLeaf (fcs, ai.ucs4, &leaf)) | |
416 | goto bail1; | |
417 | } | |
418 | FcCharSetIterNext (a, &ai); | |
419 | FcCharSetIterNext (b, &bi); | |
420 | } | |
421 | } | |
422 | return fcs; | |
423 | bail1: | |
424 | FcCharSetDestroy (fcs); | |
425 | bail0: | |
426 | return 0; | |
427 | } | |
428 | ||
429 | static FcBool | |
430 | FcCharSetIntersectLeaf (FcCharLeaf *result, | |
431 | const FcCharLeaf *al, | |
432 | const FcCharLeaf *bl) | |
433 | { | |
434 | int i; | |
435 | FcBool nonempty = FcFalse; | |
436 | ||
437 | for (i = 0; i < 256/32; i++) | |
438 | if ((result->map[i] = al->map[i] & bl->map[i])) | |
439 | nonempty = FcTrue; | |
440 | return nonempty; | |
441 | } | |
442 | ||
443 | FcCharSet * | |
444 | FcCharSetIntersect (const FcCharSet *a, const FcCharSet *b) | |
445 | { | |
446 | return FcCharSetOperate (a, b, FcCharSetIntersectLeaf, FcFalse, FcFalse); | |
447 | } | |
448 | ||
449 | static FcBool | |
450 | FcCharSetUnionLeaf (FcCharLeaf *result, | |
451 | const FcCharLeaf *al, | |
452 | const FcCharLeaf *bl) | |
453 | { | |
454 | int i; | |
455 | ||
456 | for (i = 0; i < 256/32; i++) | |
457 | result->map[i] = al->map[i] | bl->map[i]; | |
458 | return FcTrue; | |
459 | } | |
460 | ||
461 | FcCharSet * | |
462 | FcCharSetUnion (const FcCharSet *a, const FcCharSet *b) | |
463 | { | |
464 | return FcCharSetOperate (a, b, FcCharSetUnionLeaf, FcTrue, FcTrue); | |
465 | } | |
466 | ||
467 | FcBool | |
468 | FcCharSetMerge (FcCharSet *a, const FcCharSet *b, FcBool *changed) | |
469 | { | |
470 | int ai = 0, bi = 0; | |
471 | FcChar16 an, bn; | |
472 | ||
473 | if (a->ref == FC_REF_CONSTANT) | |
474 | return FcFalse; | |
475 | ||
476 | if (changed) { | |
477 | *changed = !FcCharSetIsSubset(b, a); | |
478 | if (!*changed) | |
479 | return FcTrue; | |
480 | } | |
481 | ||
482 | while (bi < b->num) | |
483 | { | |
484 | an = ai < a->num ? FcCharSetNumbers(a)[ai] : ~0; | |
485 | bn = FcCharSetNumbers(b)[bi]; | |
486 | ||
487 | if (an < bn) | |
488 | { | |
489 | ai = FcCharSetFindLeafForward (a, ai + 1, bn); | |
490 | if (ai < 0) | |
491 | ai = -ai - 1; | |
492 | } | |
493 | else | |
494 | { | |
495 | FcCharLeaf *bl = FcCharSetLeaf(b, bi); | |
496 | if (bn < an) | |
497 | { | |
498 | if (!FcCharSetAddLeaf (a, bn << 8, bl)) | |
499 | return FcFalse; | |
500 | } | |
501 | else | |
502 | { | |
503 | FcCharLeaf *al = FcCharSetLeaf(a, ai); | |
504 | FcCharSetUnionLeaf (al, al, bl); | |
505 | } | |
506 | ||
507 | ai++; | |
508 | bi++; | |
509 | } | |
510 | } | |
511 | ||
512 | return FcTrue; | |
513 | } | |
514 | ||
515 | static FcBool | |
516 | FcCharSetSubtractLeaf (FcCharLeaf *result, | |
517 | const FcCharLeaf *al, | |
518 | const FcCharLeaf *bl) | |
519 | { | |
520 | int i; | |
521 | FcBool nonempty = FcFalse; | |
522 | ||
523 | for (i = 0; i < 256/32; i++) | |
524 | if ((result->map[i] = al->map[i] & ~bl->map[i])) | |
525 | nonempty = FcTrue; | |
526 | return nonempty; | |
527 | } | |
528 | ||
529 | FcCharSet * | |
530 | FcCharSetSubtract (const FcCharSet *a, const FcCharSet *b) | |
531 | { | |
532 | return FcCharSetOperate (a, b, FcCharSetSubtractLeaf, FcTrue, FcFalse); | |
533 | } | |
534 | ||
535 | FcBool | |
536 | FcCharSetHasChar (const FcCharSet *fcs, FcChar32 ucs4) | |
537 | { | |
538 | FcCharLeaf *leaf = FcCharSetFindLeaf (fcs, ucs4); | |
539 | if (!leaf) | |
540 | return FcFalse; | |
541 | return (leaf->map[(ucs4 & 0xff) >> 5] & (1 << (ucs4 & 0x1f))) != 0; | |
542 | } | |
543 | ||
544 | static FcChar32 | |
545 | FcCharSetPopCount (FcChar32 c1) | |
546 | { | |
547 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) | |
548 | return __builtin_popcount (c1); | |
549 | #else | |
550 | /* hackmem 169 */ | |
551 | FcChar32 c2 = (c1 >> 1) & 033333333333; | |
552 | c2 = c1 - c2 - ((c2 >> 1) & 033333333333); | |
553 | return (((c2 + (c2 >> 3)) & 030707070707) % 077); | |
554 | #endif | |
555 | } | |
556 | ||
557 | FcChar32 | |
558 | FcCharSetIntersectCount (const FcCharSet *a, const FcCharSet *b) | |
559 | { | |
560 | FcCharSetIter ai, bi; | |
561 | FcChar32 count = 0; | |
562 | ||
563 | FcCharSetIterStart (a, &ai); | |
564 | FcCharSetIterStart (b, &bi); | |
565 | while (ai.leaf && bi.leaf) | |
566 | { | |
567 | if (ai.ucs4 == bi.ucs4) | |
568 | { | |
569 | FcChar32 *am = ai.leaf->map; | |
570 | FcChar32 *bm = bi.leaf->map; | |
571 | int i = 256/32; | |
572 | while (i--) | |
573 | count += FcCharSetPopCount (*am++ & *bm++); | |
574 | FcCharSetIterNext (a, &ai); | |
575 | } | |
576 | else if (ai.ucs4 < bi.ucs4) | |
577 | { | |
578 | ai.ucs4 = bi.ucs4; | |
579 | FcCharSetIterSet (a, &ai); | |
580 | } | |
581 | if (bi.ucs4 < ai.ucs4) | |
582 | { | |
583 | bi.ucs4 = ai.ucs4; | |
584 | FcCharSetIterSet (b, &bi); | |
585 | } | |
586 | } | |
587 | return count; | |
588 | } | |
589 | ||
590 | FcChar32 | |
591 | FcCharSetCount (const FcCharSet *a) | |
592 | { | |
593 | FcCharSetIter ai; | |
594 | FcChar32 count = 0; | |
595 | ||
596 | for (FcCharSetIterStart (a, &ai); ai.leaf; FcCharSetIterNext (a, &ai)) | |
597 | { | |
598 | int i = 256/32; | |
599 | FcChar32 *am = ai.leaf->map; | |
600 | ||
601 | while (i--) | |
602 | count += FcCharSetPopCount (*am++); | |
603 | } | |
604 | return count; | |
605 | } | |
606 | ||
607 | FcChar32 | |
608 | FcCharSetSubtractCount (const FcCharSet *a, const FcCharSet *b) | |
609 | { | |
610 | FcCharSetIter ai, bi; | |
611 | FcChar32 count = 0; | |
612 | ||
613 | FcCharSetIterStart (a, &ai); | |
614 | FcCharSetIterStart (b, &bi); | |
615 | while (ai.leaf) | |
616 | { | |
617 | if (ai.ucs4 <= bi.ucs4) | |
618 | { | |
619 | FcChar32 *am = ai.leaf->map; | |
620 | int i = 256/32; | |
621 | if (ai.ucs4 == bi.ucs4) | |
622 | { | |
623 | FcChar32 *bm = bi.leaf->map; | |
624 | while (i--) | |
625 | count += FcCharSetPopCount (*am++ & ~*bm++); | |
626 | } | |
627 | else | |
628 | { | |
629 | while (i--) | |
630 | count += FcCharSetPopCount (*am++); | |
631 | } | |
632 | FcCharSetIterNext (a, &ai); | |
633 | } | |
634 | else if (bi.leaf) | |
635 | { | |
636 | bi.ucs4 = ai.ucs4; | |
637 | FcCharSetIterSet (b, &bi); | |
638 | } | |
639 | } | |
640 | return count; | |
641 | } | |
642 | ||
643 | /* | |
644 | * return FcTrue iff a is a subset of b | |
645 | */ | |
646 | FcBool | |
647 | FcCharSetIsSubset (const FcCharSet *a, const FcCharSet *b) | |
648 | { | |
649 | int ai, bi; | |
650 | FcChar16 an, bn; | |
651 | ||
652 | if (a == b) return FcTrue; | |
653 | bi = 0; | |
654 | ai = 0; | |
655 | while (ai < a->num && bi < b->num) | |
656 | { | |
657 | an = FcCharSetNumbers(a)[ai]; | |
658 | bn = FcCharSetNumbers(b)[bi]; | |
659 | /* | |
660 | * Check matching pages | |
661 | */ | |
662 | if (an == bn) | |
663 | { | |
664 | FcChar32 *am = FcCharSetLeaf(a, ai)->map; | |
665 | FcChar32 *bm = FcCharSetLeaf(b, bi)->map; | |
666 | ||
667 | if (am != bm) | |
668 | { | |
669 | int i = 256/32; | |
670 | /* | |
671 | * Does am have any bits not in bm? | |
672 | */ | |
673 | while (i--) | |
674 | if (*am++ & ~*bm++) | |
675 | return FcFalse; | |
676 | } | |
677 | ai++; | |
678 | bi++; | |
679 | } | |
680 | /* | |
681 | * Does a have any pages not in b? | |
682 | */ | |
683 | else if (an < bn) | |
684 | return FcFalse; | |
685 | else | |
686 | { | |
687 | bi = FcCharSetFindLeafForward (b, bi + 1, an); | |
688 | if (bi < 0) | |
689 | bi = -bi - 1; | |
690 | } | |
691 | } | |
692 | /* | |
693 | * did we look at every page? | |
694 | */ | |
695 | return ai >= a->num; | |
696 | } | |
697 | ||
698 | /* | |
699 | * These two functions efficiently walk the entire charmap for | |
700 | * other software (like pango) that want their own copy | |
701 | */ | |
702 | ||
703 | FcChar32 | |
704 | FcCharSetNextPage (const FcCharSet *a, | |
705 | FcChar32 map[FC_CHARSET_MAP_SIZE], | |
706 | FcChar32 *next) | |
707 | { | |
708 | FcCharSetIter ai; | |
709 | FcChar32 page; | |
710 | ||
711 | ai.ucs4 = *next; | |
712 | FcCharSetIterSet (a, &ai); | |
713 | if (!ai.leaf) | |
714 | return FC_CHARSET_DONE; | |
715 | ||
716 | /* | |
717 | * Save current information | |
718 | */ | |
719 | page = ai.ucs4; | |
720 | memcpy (map, ai.leaf->map, sizeof (ai.leaf->map)); | |
721 | /* | |
722 | * Step to next page | |
723 | */ | |
724 | FcCharSetIterNext (a, &ai); | |
725 | *next = ai.ucs4; | |
726 | ||
727 | return page; | |
728 | } | |
729 | ||
730 | FcChar32 | |
731 | FcCharSetFirstPage (const FcCharSet *a, | |
732 | FcChar32 map[FC_CHARSET_MAP_SIZE], | |
733 | FcChar32 *next) | |
734 | { | |
735 | *next = 0; | |
736 | return FcCharSetNextPage (a, map, next); | |
737 | } | |
738 | ||
739 | /* | |
740 | * old coverage API, rather hard to use correctly | |
741 | */ | |
742 | ||
743 | FcChar32 | |
744 | FcCharSetCoverage (const FcCharSet *a, FcChar32 page, FcChar32 *result) | |
745 | { | |
746 | FcCharSetIter ai; | |
747 | ||
748 | ai.ucs4 = page; | |
749 | FcCharSetIterSet (a, &ai); | |
750 | if (!ai.leaf) | |
751 | { | |
752 | memset (result, '\0', 256 / 8); | |
753 | page = 0; | |
754 | } | |
755 | else | |
756 | { | |
757 | memcpy (result, ai.leaf->map, sizeof (ai.leaf->map)); | |
758 | FcCharSetIterNext (a, &ai); | |
759 | page = ai.ucs4; | |
760 | } | |
761 | return page; | |
762 | } | |
763 | ||
764 | /* | |
765 | * ASCII representation of charsets. | |
766 | * | |
767 | * Each leaf is represented as 9 32-bit values, the code of the first character followed | |
768 | * by 8 32 bit values for the leaf itself. Each value is encoded as 5 ASCII characters, | |
769 | * only 85 different values are used to avoid control characters as well as the other | |
770 | * characters used to encode font names. 85**5 > 2^32 so things work out, but | |
771 | * it's not exactly human readable output. As a special case, 0 is encoded as a space | |
772 | */ | |
773 | ||
774 | static const unsigned char charToValue[256] = { | |
775 | /* "" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
776 | /* "\b" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
777 | /* "\020" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
778 | /* "\030" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
779 | /* " " */ 0xff, 0x00, 0xff, 0x01, 0x02, 0x03, 0x04, 0xff, | |
780 | /* "(" */ 0x05, 0x06, 0x07, 0x08, 0xff, 0xff, 0x09, 0x0a, | |
781 | /* "0" */ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, | |
782 | /* "8" */ 0x13, 0x14, 0xff, 0x15, 0x16, 0xff, 0x17, 0x18, | |
783 | /* "@" */ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, | |
784 | /* "H" */ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, | |
785 | /* "P" */ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, | |
786 | /* "X" */ 0x31, 0x32, 0x33, 0x34, 0xff, 0x35, 0x36, 0xff, | |
787 | /* "`" */ 0xff, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, | |
788 | /* "h" */ 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, | |
789 | /* "p" */ 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, | |
790 | /* "x" */ 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0xff, | |
791 | /* "\200" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
792 | /* "\210" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
793 | /* "\220" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
794 | /* "\230" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
795 | /* "\240" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
796 | /* "\250" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
797 | /* "\260" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
798 | /* "\270" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
799 | /* "\300" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
800 | /* "\310" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
801 | /* "\320" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
802 | /* "\330" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
803 | /* "\340" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
804 | /* "\350" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
805 | /* "\360" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
806 | /* "\370" */ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
807 | }; | |
808 | ||
809 | static const FcChar8 valueToChar[0x55] = { | |
810 | /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*', | |
811 | /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4', | |
812 | /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>', | |
813 | /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', | |
814 | /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', | |
815 | /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', | |
816 | /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a', | |
817 | /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', | |
818 | /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', | |
819 | /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', | |
820 | /* 0x50 */ 'z', '{', '|', '}', '~', | |
821 | }; | |
822 | ||
823 | static FcChar8 * | |
824 | FcCharSetParseValue (FcChar8 *string, FcChar32 *value) | |
825 | { | |
826 | int i; | |
827 | FcChar32 v; | |
828 | FcChar32 c; | |
829 | ||
830 | if (*string == ' ') | |
831 | { | |
832 | v = 0; | |
833 | string++; | |
834 | } | |
835 | else | |
836 | { | |
837 | v = 0; | |
838 | for (i = 0; i < 5; i++) | |
839 | { | |
840 | if (!(c = (FcChar32) (unsigned char) *string++)) | |
841 | return 0; | |
842 | c = charToValue[c]; | |
843 | if (c == 0xff) | |
844 | return 0; | |
845 | v = v * 85 + c; | |
846 | } | |
847 | } | |
848 | *value = v; | |
849 | return string; | |
850 | } | |
851 | ||
852 | static FcBool | |
853 | FcCharSetUnparseValue (FcStrBuf *buf, FcChar32 value) | |
854 | { | |
855 | int i; | |
856 | if (value == 0) | |
857 | { | |
858 | return FcStrBufChar (buf, ' '); | |
859 | } | |
860 | else | |
861 | { | |
862 | FcChar8 string[6]; | |
863 | FcChar8 *s = string + 5; | |
864 | string[5] = '\0'; | |
865 | for (i = 0; i < 5; i++) | |
866 | { | |
867 | *--s = valueToChar[value % 85]; | |
868 | value /= 85; | |
869 | } | |
870 | for (i = 0; i < 5; i++) | |
871 | if (!FcStrBufChar (buf, *s++)) | |
872 | return FcFalse; | |
873 | } | |
874 | return FcTrue; | |
875 | } | |
876 | ||
877 | FcCharSet * | |
878 | FcNameParseCharSet (FcChar8 *string) | |
879 | { | |
880 | FcCharSet *c; | |
881 | FcChar32 ucs4; | |
882 | FcCharLeaf *leaf; | |
883 | FcCharLeaf temp; | |
884 | FcChar32 bits; | |
885 | int i; | |
886 | ||
887 | c = FcCharSetCreate (); | |
888 | if (!c) | |
889 | goto bail0; | |
890 | while (*string) | |
891 | { | |
892 | string = FcCharSetParseValue (string, &ucs4); | |
893 | if (!string) | |
894 | goto bail1; | |
895 | bits = 0; | |
896 | for (i = 0; i < 256/32; i++) | |
897 | { | |
898 | string = FcCharSetParseValue (string, &temp.map[i]); | |
899 | if (!string) | |
900 | goto bail1; | |
901 | bits |= temp.map[i]; | |
902 | } | |
903 | if (bits) | |
904 | { | |
905 | leaf = malloc (sizeof (FcCharLeaf)); | |
906 | if (!leaf) | |
907 | goto bail1; | |
908 | *leaf = temp; | |
909 | if (!FcCharSetInsertLeaf (c, ucs4, leaf)) | |
910 | goto bail1; | |
911 | } | |
912 | } | |
913 | return c; | |
914 | bail1: | |
915 | if (c->num) | |
916 | { | |
917 | FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcCharLeaf *)); | |
918 | free (FcCharSetLeaves (c)); | |
919 | } | |
920 | if (c->num) | |
921 | { | |
922 | FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcChar16)); | |
923 | free (FcCharSetNumbers (c)); | |
924 | } | |
925 | FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet)); | |
926 | free (c); | |
927 | bail0: | |
928 | return NULL; | |
929 | } | |
930 | ||
931 | FcBool | |
932 | FcNameUnparseCharSet (FcStrBuf *buf, const FcCharSet *c) | |
933 | { | |
934 | FcCharSetIter ci; | |
935 | int i; | |
936 | #ifdef CHECK | |
937 | int len = buf->len; | |
938 | #endif | |
939 | ||
940 | for (FcCharSetIterStart (c, &ci); | |
941 | ci.leaf; | |
942 | FcCharSetIterNext (c, &ci)) | |
943 | { | |
944 | if (!FcCharSetUnparseValue (buf, ci.ucs4)) | |
945 | return FcFalse; | |
946 | for (i = 0; i < 256/32; i++) | |
947 | if (!FcCharSetUnparseValue (buf, ci.leaf->map[i])) | |
948 | return FcFalse; | |
949 | } | |
950 | #ifdef CHECK | |
951 | { | |
952 | FcCharSet *check; | |
953 | FcChar32 missing; | |
954 | FcCharSetIter ci, checki; | |
955 | ||
956 | /* null terminate for parser */ | |
957 | FcStrBufChar (buf, '\0'); | |
958 | /* step back over null for life after test */ | |
959 | buf->len--; | |
960 | check = FcNameParseCharSet (buf->buf + len); | |
961 | FcCharSetIterStart (c, &ci); | |
962 | FcCharSetIterStart (check, &checki); | |
963 | while (ci.leaf || checki.leaf) | |
964 | { | |
965 | if (ci.ucs4 < checki.ucs4) | |
966 | { | |
967 | printf ("Missing leaf node at 0x%x\n", ci.ucs4); | |
968 | FcCharSetIterNext (c, &ci); | |
969 | } | |
970 | else if (checki.ucs4 < ci.ucs4) | |
971 | { | |
972 | printf ("Extra leaf node at 0x%x\n", checki.ucs4); | |
973 | FcCharSetIterNext (check, &checki); | |
974 | } | |
975 | else | |
976 | { | |
977 | int i = 256/32; | |
978 | FcChar32 *cm = ci.leaf->map; | |
979 | FcChar32 *checkm = checki.leaf->map; | |
980 | ||
981 | for (i = 0; i < 256; i += 32) | |
982 | { | |
983 | if (*cm != *checkm) | |
984 | printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n", | |
985 | ci.ucs4 + i, *cm, *checkm); | |
986 | cm++; | |
987 | checkm++; | |
988 | } | |
989 | FcCharSetIterNext (c, &ci); | |
990 | FcCharSetIterNext (check, &checki); | |
991 | } | |
992 | } | |
993 | if ((missing = FcCharSetSubtractCount (c, check))) | |
994 | printf ("%d missing in reparsed result\n", missing); | |
995 | if ((missing = FcCharSetSubtractCount (check, c))) | |
996 | printf ("%d extra in reparsed result\n", missing); | |
997 | FcCharSetDestroy (check); | |
998 | } | |
999 | #endif | |
1000 | ||
1001 | return FcTrue; | |
1002 | } | |
1003 | ||
1004 | typedef struct _FcCharLeafEnt FcCharLeafEnt; | |
1005 | ||
1006 | struct _FcCharLeafEnt { | |
1007 | FcCharLeafEnt *next; | |
1008 | FcChar32 hash; | |
1009 | FcCharLeaf leaf; | |
1010 | }; | |
1011 | ||
1012 | #define FC_CHAR_LEAF_BLOCK (4096 / sizeof (FcCharLeafEnt)) | |
1013 | #define FC_CHAR_LEAF_HASH_SIZE 257 | |
1014 | ||
1015 | typedef struct _FcCharSetEnt FcCharSetEnt; | |
1016 | ||
1017 | struct _FcCharSetEnt { | |
1018 | FcCharSetEnt *next; | |
1019 | FcChar32 hash; | |
1020 | FcCharSet set; | |
1021 | }; | |
1022 | ||
1023 | typedef struct _FcCharSetOrigEnt FcCharSetOrigEnt; | |
1024 | ||
1025 | struct _FcCharSetOrigEnt { | |
1026 | FcCharSetOrigEnt *next; | |
1027 | const FcCharSet *orig; | |
1028 | const FcCharSet *frozen; | |
1029 | }; | |
1030 | ||
1031 | #define FC_CHAR_SET_HASH_SIZE 67 | |
1032 | ||
1033 | struct _FcCharSetFreezer { | |
1034 | FcCharLeafEnt *leaf_hash_table[FC_CHAR_LEAF_HASH_SIZE]; | |
1035 | FcCharLeafEnt **leaf_blocks; | |
1036 | int leaf_block_count; | |
1037 | FcCharSetEnt *set_hash_table[FC_CHAR_SET_HASH_SIZE]; | |
1038 | FcCharSetOrigEnt *orig_hash_table[FC_CHAR_SET_HASH_SIZE]; | |
1039 | FcCharLeafEnt *current_block; | |
1040 | int leaf_remain; | |
1041 | int leaves_seen; | |
1042 | int charsets_seen; | |
1043 | int leaves_allocated; | |
1044 | int charsets_allocated; | |
1045 | }; | |
1046 | ||
1047 | static FcCharLeafEnt * | |
1048 | FcCharLeafEntCreate (FcCharSetFreezer *freezer) | |
1049 | { | |
1050 | if (!freezer->leaf_remain) | |
1051 | { | |
1052 | FcCharLeafEnt **newBlocks; | |
1053 | ||
1054 | freezer->leaf_block_count++; | |
1055 | newBlocks = realloc (freezer->leaf_blocks, freezer->leaf_block_count * sizeof (FcCharLeafEnt *)); | |
1056 | if (!newBlocks) | |
1057 | return 0; | |
1058 | freezer->leaf_blocks = newBlocks; | |
1059 | freezer->current_block = freezer->leaf_blocks[freezer->leaf_block_count-1] = malloc (FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt)); | |
1060 | if (!freezer->current_block) | |
1061 | return 0; | |
1062 | FcMemAlloc (FC_MEM_CHARLEAF, FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt)); | |
1063 | freezer->leaf_remain = FC_CHAR_LEAF_BLOCK; | |
1064 | } | |
1065 | freezer->leaf_remain--; | |
1066 | freezer->leaves_allocated++; | |
1067 | return freezer->current_block++; | |
1068 | } | |
1069 | ||
1070 | static FcChar32 | |
1071 | FcCharLeafHash (FcCharLeaf *leaf) | |
1072 | { | |
1073 | FcChar32 hash = 0; | |
1074 | int i; | |
1075 | ||
1076 | for (i = 0; i < 256/32; i++) | |
1077 | hash = ((hash << 1) | (hash >> 31)) ^ leaf->map[i]; | |
1078 | return hash; | |
1079 | } | |
1080 | ||
1081 | static FcCharLeaf * | |
1082 | FcCharSetFreezeLeaf (FcCharSetFreezer *freezer, FcCharLeaf *leaf) | |
1083 | { | |
1084 | FcChar32 hash = FcCharLeafHash (leaf); | |
1085 | FcCharLeafEnt **bucket = &freezer->leaf_hash_table[hash % FC_CHAR_LEAF_HASH_SIZE]; | |
1086 | FcCharLeafEnt *ent; | |
1087 | ||
1088 | for (ent = *bucket; ent; ent = ent->next) | |
1089 | { | |
1090 | if (ent->hash == hash && !memcmp (&ent->leaf, leaf, sizeof (FcCharLeaf))) | |
1091 | return &ent->leaf; | |
1092 | } | |
1093 | ||
1094 | ent = FcCharLeafEntCreate(freezer); | |
1095 | if (!ent) | |
1096 | return 0; | |
1097 | ent->leaf = *leaf; | |
1098 | ent->hash = hash; | |
1099 | ent->next = *bucket; | |
1100 | *bucket = ent; | |
1101 | return &ent->leaf; | |
1102 | } | |
1103 | ||
1104 | static FcChar32 | |
1105 | FcCharSetHash (FcCharSet *fcs) | |
1106 | { | |
1107 | FcChar32 hash = 0; | |
1108 | int i; | |
1109 | ||
1110 | /* hash in leaves */ | |
1111 | for (i = 0; i < fcs->num; i++) | |
1112 | hash = ((hash << 1) | (hash >> 31)) ^ FcCharLeafHash (FcCharSetLeaf(fcs,i)); | |
1113 | /* hash in numbers */ | |
1114 | for (i = 0; i < fcs->num; i++) | |
1115 | hash = ((hash << 1) | (hash >> 31)) ^ *FcCharSetNumbers(fcs); | |
1116 | return hash; | |
1117 | } | |
1118 | ||
1119 | static FcBool | |
1120 | FcCharSetFreezeOrig (FcCharSetFreezer *freezer, const FcCharSet *orig, const FcCharSet *frozen) | |
1121 | { | |
1122 | FcCharSetOrigEnt **bucket = &freezer->orig_hash_table[((uintptr_t) orig) & FC_CHAR_SET_HASH_SIZE]; | |
1123 | FcCharSetOrigEnt *ent; | |
1124 | ||
1125 | ent = malloc (sizeof (FcCharSetOrigEnt)); | |
1126 | if (!ent) | |
1127 | return FcFalse; | |
1128 | ent->orig = orig; | |
1129 | ent->frozen = frozen; | |
1130 | ent->next = *bucket; | |
1131 | *bucket = ent; | |
1132 | return FcTrue; | |
1133 | } | |
1134 | ||
1135 | static FcCharSet * | |
1136 | FcCharSetFreezeBase (FcCharSetFreezer *freezer, FcCharSet *fcs, const FcCharSet *orig) | |
1137 | { | |
1138 | FcChar32 hash = FcCharSetHash (fcs); | |
1139 | FcCharSetEnt **bucket = &freezer->set_hash_table[hash % FC_CHAR_SET_HASH_SIZE]; | |
1140 | FcCharSetEnt *ent; | |
1141 | int size; | |
1142 | int i; | |
1143 | ||
1144 | for (ent = *bucket; ent; ent = ent->next) | |
1145 | { | |
1146 | if (ent->hash == hash && | |
1147 | ent->set.num == fcs->num && | |
1148 | !memcmp (FcCharSetNumbers(&ent->set), | |
1149 | FcCharSetNumbers(fcs), | |
1150 | fcs->num * sizeof (FcChar16))) | |
1151 | { | |
1152 | FcBool ok = FcTrue; | |
1153 | int i; | |
1154 | ||
1155 | for (i = 0; i < fcs->num; i++) | |
1156 | if (FcCharSetLeaf(&ent->set, i) != FcCharSetLeaf(fcs, i)) | |
1157 | ok = FcFalse; | |
1158 | if (ok) | |
1159 | return &ent->set; | |
1160 | } | |
1161 | } | |
1162 | ||
1163 | size = (sizeof (FcCharSetEnt) + | |
1164 | fcs->num * sizeof (FcCharLeaf *) + | |
1165 | fcs->num * sizeof (FcChar16)); | |
1166 | ent = malloc (size); | |
1167 | if (!ent) | |
1168 | return 0; | |
1169 | FcMemAlloc (FC_MEM_CHARSET, size); | |
1170 | ||
1171 | freezer->charsets_allocated++; | |
1172 | ||
1173 | ent->set.ref = FC_REF_CONSTANT; | |
1174 | ent->set.num = fcs->num; | |
1175 | if (fcs->num) | |
1176 | { | |
1177 | intptr_t *ent_leaves; | |
1178 | ||
1179 | ent->set.leaves_offset = sizeof (ent->set); | |
1180 | ent->set.numbers_offset = (ent->set.leaves_offset + | |
1181 | fcs->num * sizeof (intptr_t)); | |
1182 | ||
1183 | ent_leaves = FcCharSetLeaves (&ent->set); | |
1184 | for (i = 0; i < fcs->num; i++) | |
1185 | ent_leaves[i] = FcPtrToOffset (ent_leaves, | |
1186 | FcCharSetLeaf (fcs, i)); | |
1187 | memcpy (FcCharSetNumbers (&ent->set), | |
1188 | FcCharSetNumbers (fcs), | |
1189 | fcs->num * sizeof (FcChar16)); | |
1190 | } | |
1191 | else | |
1192 | { | |
1193 | ent->set.leaves_offset = 0; | |
1194 | ent->set.numbers_offset = 0; | |
1195 | } | |
1196 | ||
1197 | ent->hash = hash; | |
1198 | ent->next = *bucket; | |
1199 | *bucket = ent; | |
1200 | ||
1201 | return &ent->set; | |
1202 | } | |
1203 | ||
1204 | static const FcCharSet * | |
1205 | FcCharSetFindFrozen (FcCharSetFreezer *freezer, const FcCharSet *orig) | |
1206 | { | |
1207 | FcCharSetOrigEnt **bucket = &freezer->orig_hash_table[((uintptr_t) orig) & FC_CHAR_SET_HASH_SIZE]; | |
1208 | FcCharSetOrigEnt *ent; | |
1209 | ||
1210 | for (ent = *bucket; ent; ent = ent->next) | |
1211 | if (ent->orig == orig) | |
1212 | return ent->frozen; | |
1213 | return NULL; | |
1214 | } | |
1215 | ||
1216 | const FcCharSet * | |
1217 | FcCharSetFreeze (FcCharSetFreezer *freezer, const FcCharSet *fcs) | |
1218 | { | |
1219 | FcCharSet *b; | |
1220 | const FcCharSet *n = 0; | |
1221 | FcCharLeaf *l; | |
1222 | int i; | |
1223 | ||
1224 | b = FcCharSetCreate (); | |
1225 | if (!b) | |
1226 | goto bail0; | |
1227 | for (i = 0; i < fcs->num; i++) | |
1228 | { | |
1229 | l = FcCharSetFreezeLeaf (freezer, FcCharSetLeaf(fcs, i)); | |
1230 | if (!l) | |
1231 | goto bail1; | |
1232 | if (!FcCharSetInsertLeaf (b, FcCharSetNumbers(fcs)[i] << 8, l)) | |
1233 | goto bail1; | |
1234 | } | |
1235 | n = FcCharSetFreezeBase (freezer, b, fcs); | |
1236 | if (!FcCharSetFreezeOrig (freezer, fcs, n)) | |
1237 | { | |
1238 | n = NULL; | |
1239 | goto bail1; | |
1240 | } | |
1241 | freezer->charsets_seen++; | |
1242 | freezer->leaves_seen += fcs->num; | |
1243 | bail1: | |
1244 | if (b->num) | |
1245 | { | |
1246 | FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcCharLeaf *)); | |
1247 | free (FcCharSetLeaves (b)); | |
1248 | } | |
1249 | if (b->num) | |
1250 | { | |
1251 | FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcChar16)); | |
1252 | free (FcCharSetNumbers (b)); | |
1253 | } | |
1254 | FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet)); | |
1255 | free (b); | |
1256 | bail0: | |
1257 | return n; | |
1258 | } | |
1259 | ||
1260 | FcCharSetFreezer * | |
1261 | FcCharSetFreezerCreate (void) | |
1262 | { | |
1263 | FcCharSetFreezer *freezer; | |
1264 | ||
1265 | freezer = calloc (1, sizeof (FcCharSetFreezer)); | |
1266 | return freezer; | |
1267 | } | |
1268 | ||
1269 | void | |
1270 | FcCharSetFreezerDestroy (FcCharSetFreezer *freezer) | |
1271 | { | |
1272 | int i; | |
1273 | ||
1274 | if (FcDebug() & FC_DBG_CACHE) | |
1275 | { | |
1276 | printf ("\ncharsets %d -> %d leaves %d -> %d\n", | |
1277 | freezer->charsets_seen, freezer->charsets_allocated, | |
1278 | freezer->leaves_seen, freezer->leaves_allocated); | |
1279 | } | |
1280 | for (i = 0; i < FC_CHAR_SET_HASH_SIZE; i++) | |
1281 | { | |
1282 | FcCharSetEnt *ent, *next; | |
1283 | for (ent = freezer->set_hash_table[i]; ent; ent = next) | |
1284 | { | |
1285 | next = ent->next; | |
1286 | FcMemFree (FC_MEM_CHARSET, (sizeof (FcCharSetEnt) + | |
1287 | ent->set.num * sizeof (FcCharLeaf *) + | |
1288 | ent->set.num * sizeof (FcChar16))); | |
1289 | free (ent); | |
1290 | } | |
1291 | } | |
1292 | ||
1293 | for (i = 0; i < FC_CHAR_SET_HASH_SIZE; i++) | |
1294 | { | |
1295 | FcCharSetOrigEnt *ent, *next; | |
1296 | for (ent = freezer->orig_hash_table[i]; ent; ent = next) | |
1297 | { | |
1298 | next = ent->next; | |
1299 | free (ent); | |
1300 | } | |
1301 | } | |
1302 | ||
1303 | for (i = 0; i < freezer->leaf_block_count; i++) | |
1304 | { | |
1305 | free (freezer->leaf_blocks[i]); | |
1306 | FcMemFree (FC_MEM_CHARLEAF, FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt)); | |
1307 | } | |
1308 | ||
1309 | free (freezer->leaf_blocks); | |
1310 | free (freezer); | |
1311 | } | |
1312 | ||
1313 | FcBool | |
1314 | FcCharSetSerializeAlloc (FcSerialize *serialize, const FcCharSet *cs) | |
1315 | { | |
1316 | intptr_t *leaves; | |
1317 | FcChar16 *numbers; | |
1318 | int i; | |
1319 | ||
1320 | if (cs->ref != FC_REF_CONSTANT) | |
1321 | { | |
1322 | if (!serialize->cs_freezer) | |
1323 | { | |
1324 | serialize->cs_freezer = FcCharSetFreezerCreate (); | |
1325 | if (!serialize->cs_freezer) | |
1326 | return FcFalse; | |
1327 | } | |
1328 | if (FcCharSetFindFrozen (serialize->cs_freezer, cs)) | |
1329 | return FcTrue; | |
1330 | ||
1331 | cs = FcCharSetFreeze (serialize->cs_freezer, cs); | |
1332 | } | |
1333 | ||
1334 | leaves = FcCharSetLeaves (cs); | |
1335 | numbers = FcCharSetNumbers (cs); | |
1336 | ||
1337 | if (!FcSerializeAlloc (serialize, cs, sizeof (FcCharSet))) | |
1338 | return FcFalse; | |
1339 | if (!FcSerializeAlloc (serialize, leaves, cs->num * sizeof (intptr_t))) | |
1340 | return FcFalse; | |
1341 | if (!FcSerializeAlloc (serialize, numbers, cs->num * sizeof (FcChar16))) | |
1342 | return FcFalse; | |
1343 | for (i = 0; i < cs->num; i++) | |
1344 | if (!FcSerializeAlloc (serialize, FcCharSetLeaf(cs, i), | |
1345 | sizeof (FcCharLeaf))) | |
1346 | return FcFalse; | |
1347 | return FcTrue; | |
1348 | } | |
1349 | ||
1350 | FcCharSet * | |
1351 | FcCharSetSerialize(FcSerialize *serialize, const FcCharSet *cs) | |
1352 | { | |
1353 | FcCharSet *cs_serialized; | |
1354 | intptr_t *leaves, *leaves_serialized; | |
1355 | FcChar16 *numbers, *numbers_serialized; | |
1356 | FcCharLeaf *leaf, *leaf_serialized; | |
1357 | int i; | |
1358 | ||
1359 | if (cs->ref != FC_REF_CONSTANT && serialize->cs_freezer) | |
1360 | { | |
1361 | cs = FcCharSetFindFrozen (serialize->cs_freezer, cs); | |
1362 | if (!cs) | |
1363 | return NULL; | |
1364 | } | |
1365 | ||
1366 | cs_serialized = FcSerializePtr (serialize, cs); | |
1367 | if (!cs_serialized) | |
1368 | return NULL; | |
1369 | ||
1370 | cs_serialized->ref = FC_REF_CONSTANT; | |
1371 | cs_serialized->num = cs->num; | |
1372 | ||
1373 | if (cs->num) | |
1374 | { | |
1375 | leaves = FcCharSetLeaves (cs); | |
1376 | leaves_serialized = FcSerializePtr (serialize, leaves); | |
1377 | if (!leaves_serialized) | |
1378 | return NULL; | |
1379 | ||
1380 | cs_serialized->leaves_offset = FcPtrToOffset (cs_serialized, | |
1381 | leaves_serialized); | |
1382 | ||
1383 | numbers = FcCharSetNumbers (cs); | |
1384 | numbers_serialized = FcSerializePtr (serialize, numbers); | |
1385 | if (!numbers) | |
1386 | return NULL; | |
1387 | ||
1388 | cs_serialized->numbers_offset = FcPtrToOffset (cs_serialized, | |
1389 | numbers_serialized); | |
1390 | ||
1391 | for (i = 0; i < cs->num; i++) | |
1392 | { | |
1393 | leaf = FcCharSetLeaf (cs, i); | |
1394 | leaf_serialized = FcSerializePtr (serialize, leaf); | |
1395 | if (!leaf_serialized) | |
1396 | return NULL; | |
1397 | *leaf_serialized = *leaf; | |
1398 | leaves_serialized[i] = FcPtrToOffset (leaves_serialized, | |
1399 | leaf_serialized); | |
1400 | numbers_serialized[i] = numbers[i]; | |
1401 | } | |
1402 | } | |
1403 | else | |
1404 | { | |
1405 | cs_serialized->leaves_offset = 0; | |
1406 | cs_serialized->numbers_offset = 0; | |
1407 | } | |
1408 | ||
1409 | return cs_serialized; | |
1410 | } | |
1411 | #define __fccharset__ | |
1412 | #include "fcaliastail.h" | |
1413 | #undef __fccharset__ |