fccharset.c 29.1 KB
Newer Older
Keith Packard's avatar
Keith Packard committed
1
/*
2
 * fontconfig/src/fccharset.c
Keith Packard's avatar
Keith Packard committed
3
 *
4
 * Copyright © 2001 Keith Packard
Keith Packard's avatar
Keith Packard committed
5 6 7 8 9
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
10
 * documentation, and that the name of the author(s) not be used in
Keith Packard's avatar
Keith Packard committed
11
 * advertising or publicity pertaining to distribution of the software without
12
 * specific, written prior permission.  The authors make no
Keith Packard's avatar
Keith Packard committed
13 14 15
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
16
 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
Keith Packard's avatar
Keith Packard committed
17
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18
 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
Keith Packard's avatar
Keith Packard committed
19 20 21 22 23 24 25
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#include "fcint.h"
26
#include <stdlib.h>
Keith Packard's avatar
Keith Packard committed
27 28 29 30 31 32 33 34 35 36 37

/* #define CHECK */

FcCharSet *
FcCharSetCreate (void)
{
    FcCharSet	*fcs;

    fcs = (FcCharSet *) malloc (sizeof (FcCharSet));
    if (!fcs)
	return 0;
38
    FcRefInit (&fcs->ref, 1);
39
    fcs->num = 0;
40 41
    fcs->leaves_offset = 0;
    fcs->numbers_offset = 0;
Keith Packard's avatar
Keith Packard committed
42 43 44
    return fcs;
}

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
FcCharSet *
FcCharSetPromote (FcValuePromotionBuffer *vbuf)
{
    FcCharSet *fcs = (FcCharSet *) vbuf;

    FC_ASSERT_STATIC (sizeof (FcCharSet) <= sizeof (FcValuePromotionBuffer));

    FcRefSetConst (&fcs->ref);
    fcs->num = 0;
    fcs->leaves_offset = 0;
    fcs->numbers_offset = 0;

    return fcs;
}

Keith Packard's avatar
Keith Packard committed
60 61 62 63 64 65 66 67 68
FcCharSet *
FcCharSetNew (void)
{
    return FcCharSetCreate ();
}

void
FcCharSetDestroy (FcCharSet *fcs)
{
69
    int i;
70

71
    if (fcs)
72
    {
73
	if (FcRefIsConst (&fcs->ref))
74 75 76 77
	{
	    FcCacheObjectDereference (fcs);
	    return;
	}
78
	if (FcRefDec (&fcs->ref) != 1)
79 80 81 82 83 84 85 86 87
	    return;
	for (i = 0; i < fcs->num; i++)
	    free (FcCharSetLeaf (fcs, i));
	if (fcs->num)
	{
	    free (FcCharSetLeaves (fcs));
	    free (FcCharSetNumbers (fcs));
	}
	free (fcs);
88
    }
Keith Packard's avatar
Keith Packard committed
89 90 91
}

/*
Karl Tomlinson's avatar
Karl Tomlinson committed
92 93
 * Search for the leaf containing with the specified num.
 * Return its index if it exists, otherwise return negative of
94
 * the (position + 1) where it should be inserted
Keith Packard's avatar
Keith Packard committed
95 96
 */

Karl Tomlinson's avatar
Karl Tomlinson committed
97

98
static int
Karl Tomlinson's avatar
Karl Tomlinson committed
99
FcCharSetFindLeafForward (const FcCharSet *fcs, int start, FcChar16 num)
100
{
101
    FcChar16		*numbers = FcCharSetNumbers(fcs);
102
    FcChar16		page;
Karl Tomlinson's avatar
Karl Tomlinson committed
103
    int			low = start;
104 105 106 107 108 109 110 111
    int			high = fcs->num - 1;

    if (!numbers)
	return -1;
    while (low <= high)
    {
	int mid = (low + high) >> 1;
	page = numbers[mid];
Karl Tomlinson's avatar
Karl Tomlinson committed
112
	if (page == num)
113
	    return mid;
Karl Tomlinson's avatar
Karl Tomlinson committed
114
	if (page < num)
115 116 117 118
	    low = mid + 1;
	else
	    high = mid - 1;
    }
Karl Tomlinson's avatar
Karl Tomlinson committed
119
    if (high < 0 || (high < fcs->num && numbers[high] < num))
120 121 122 123
	high++;
    return -(high + 1);
}

Karl Tomlinson's avatar
Karl Tomlinson committed
124 125 126 127 128 129 130 131 132 133 134 135
/*
 * Locate the leaf containing the specified char, return
 * its index if it exists, otherwise return negative of
 * the (position + 1) where it should be inserted
 */

static int
FcCharSetFindLeafPos (const FcCharSet *fcs, FcChar32 ucs4)
{
    return FcCharSetFindLeafForward (fcs, 0, ucs4 >> 8);
}

Keith Packard's avatar
Keith Packard committed
136 137 138
static FcCharLeaf *
FcCharSetFindLeaf (const FcCharSet *fcs, FcChar32 ucs4)
{
139 140
    int	pos = FcCharSetFindLeafPos (fcs, ucs4);
    if (pos >= 0)
141
	return FcCharSetLeaf(fcs, pos);
142 143
    return 0;
}
Keith Packard's avatar
Keith Packard committed
144

145 146
#define FC_IS_ZERO_OR_POWER_OF_TWO(x) (!((x) & ((x)-1)))

147
static FcBool
148
FcCharSetPutLeaf (FcCharSet	*fcs,
149
		  FcChar32	ucs4,
150
		  FcCharLeaf	*leaf,
151 152
		  int		pos)
{
153 154
    intptr_t	*leaves = FcCharSetLeaves (fcs);
    FcChar16	*numbers = FcCharSetNumbers (fcs);
155 156 157 158

    ucs4 >>= 8;
    if (ucs4 >= 0x10000)
	return FcFalse;
159

160
    if (FC_IS_ZERO_OR_POWER_OF_TWO (fcs->num))
161
    {
162 163 164 165 166
      if (!fcs->num)
      {
        unsigned int alloced = 8;
	leaves = malloc (alloced * sizeof (*leaves));
	numbers = malloc (alloced * sizeof (*numbers));
167 168 169 170 171 172 173 174
	if (!leaves || !numbers)
	{
	    if (leaves)
		free (leaves);
	    if (numbers)
		free (numbers);
	    return FcFalse;
	}
175 176 177 178 179 180 181 182
      }
      else
      {
        unsigned int alloced = fcs->num;
	intptr_t *new_leaves, distance;

	alloced *= 2;
	new_leaves = realloc (leaves, alloced * sizeof (*leaves));
183 184
	if (!new_leaves)
	    return FcFalse;
185
	numbers = realloc (numbers, alloced * sizeof (*numbers));
186 187 188 189 190 191 192 193 194 195
	if (!numbers)
	{
	    /* Revert the reallocation of leaves */
	    leaves = realloc (new_leaves, (alloced / 2) * sizeof (*new_leaves));
	    /* unlikely to fail though */
	    if (!leaves)
		return FcFalse;
	    fcs->leaves_offset = FcPtrToOffset (fcs, leaves);
	    return FcFalse;
	}
196
	distance = (intptr_t) new_leaves - (intptr_t) leaves;
197 198 199 200 201 202 203
	if (new_leaves && distance)
	{
	    int i;
	    for (i = 0; i < fcs->num; i++)
		new_leaves[i] -= distance;
	}
	leaves = new_leaves;
204 205 206 207
      }

      fcs->leaves_offset = FcPtrToOffset (fcs, leaves);
      fcs->numbers_offset = FcPtrToOffset (fcs, numbers);
208
    }
209 210

    memmove (leaves + pos + 1, leaves + pos,
211 212 213 214 215
	     (fcs->num - pos) * sizeof (*leaves));
    memmove (numbers + pos + 1, numbers + pos,
	     (fcs->num - pos) * sizeof (*numbers));
    numbers[pos] = (FcChar16) ucs4;
    leaves[pos] = FcPtrToOffset (leaves, leaf);
216 217
    fcs->num++;
    return FcTrue;
Keith Packard's avatar
Keith Packard committed
218 219 220 221 222 223 224
}

/*
 * Locate the leaf containing the specified char, creating it
 * if desired
 */

225
FcCharLeaf *
Keith Packard's avatar
Keith Packard committed
226 227
FcCharSetFindLeafCreate (FcCharSet *fcs, FcChar32 ucs4)
{
228 229
    int			pos;
    FcCharLeaf		*leaf;
Keith Packard's avatar
Keith Packard committed
230

231 232
    pos = FcCharSetFindLeafPos (fcs, ucs4);
    if (pos >= 0)
233
	return FcCharSetLeaf(fcs, pos);
234

235 236 237
    leaf = calloc (1, sizeof (FcCharLeaf));
    if (!leaf)
	return 0;
238

239 240
    pos = -pos - 1;
    if (!FcCharSetPutLeaf (fcs, ucs4, leaf, pos))
Keith Packard's avatar
Keith Packard committed
241
    {
242 243
	free (leaf);
	return 0;
Keith Packard's avatar
Keith Packard committed
244
    }
245 246 247 248 249 250 251 252 253 254
    return leaf;
}

static FcBool
FcCharSetInsertLeaf (FcCharSet *fcs, FcChar32 ucs4, FcCharLeaf *leaf)
{
    int		    pos;

    pos = FcCharSetFindLeafPos (fcs, ucs4);
    if (pos >= 0)
Keith Packard's avatar
Keith Packard committed
255
    {
256 257 258
	free (FcCharSetLeaf (fcs, pos));
	FcCharSetLeaves(fcs)[pos] = FcPtrToOffset (FcCharSetLeaves(fcs),
						   leaf);
259
	return FcTrue;
Keith Packard's avatar
Keith Packard committed
260
    }
261 262
    pos = -pos - 1;
    return FcCharSetPutLeaf (fcs, ucs4, leaf, pos);
Keith Packard's avatar
Keith Packard committed
263 264 265 266 267 268 269
}

FcBool
FcCharSetAddChar (FcCharSet *fcs, FcChar32 ucs4)
{
    FcCharLeaf	*leaf;
    FcChar32	*b;
270

271
    if (fcs == NULL || FcRefIsConst (&fcs->ref))
Keith Packard's avatar
Keith Packard committed
272 273 274 275 276 277 278 279 280
	return FcFalse;
    leaf = FcCharSetFindLeafCreate (fcs, ucs4);
    if (!leaf)
	return FcFalse;
    b = &leaf->map[(ucs4 & 0xff) >> 5];
    *b |= (1 << (ucs4 & 0x1f));
    return FcTrue;
}

281 282 283 284 285 286
FcBool
FcCharSetDelChar (FcCharSet *fcs, FcChar32 ucs4)
{
    FcCharLeaf	*leaf;
    FcChar32	*b;

287
    if (fcs == NULL || FcRefIsConst (&fcs->ref))
288 289 290 291 292 293 294 295 296 297
	return FcFalse;
    leaf = FcCharSetFindLeaf (fcs, ucs4);
    if (!leaf)
	return FcTrue;
    b = &leaf->map[(ucs4 & 0xff) >> 5];
    *b &= ~(1 << (ucs4 & 0x1f));
    /* We don't bother removing the leaf if it's empty */
    return FcTrue;
}

Keith Packard's avatar
Keith Packard committed
298 299 300 301 302 303 304
/*
 * An iterator for the leaves of a charset
 */

typedef struct _fcCharSetIter {
    FcCharLeaf	    *leaf;
    FcChar32	    ucs4;
305
    int		    pos;
Keith Packard's avatar
Keith Packard committed
306 307 308
} FcCharSetIter;

/*
309
 * Set iter->leaf to the leaf containing iter->ucs4 or higher
Keith Packard's avatar
Keith Packard committed
310 311 312 313 314
 */

static void
FcCharSetIterSet (const FcCharSet *fcs, FcCharSetIter *iter)
{
315
    int		    pos = FcCharSetFindLeafPos (fcs, iter->ucs4);
316

317
    if (pos < 0)
318
    {
319 320
	pos = -pos - 1;
	if (pos == fcs->num)
321
	{
322 323 324
	    iter->ucs4 = ~0;
	    iter->leaf = 0;
	    return;
325
	}
326
        iter->ucs4 = (FcChar32) FcCharSetNumbers(fcs)[pos] << 8;
327
    }
328
    iter->leaf = FcCharSetLeaf(fcs, pos);
329
    iter->pos = pos;
Keith Packard's avatar
Keith Packard committed
330 331 332 333 334
}

static void
FcCharSetIterNext (const FcCharSet *fcs, FcCharSetIter *iter)
{
335 336
    int	pos = iter->pos + 1;
    if (pos >= fcs->num)
337 338 339 340
    {
	iter->ucs4 = ~0;
	iter->leaf = 0;
    }
341
    else
342
    {
343 344
	iter->ucs4 = (FcChar32) FcCharSetNumbers(fcs)[pos] << 8;
	iter->leaf = FcCharSetLeaf(fcs, pos);
345
	iter->pos = pos;
346 347 348
    }
}

Keith Packard's avatar
Keith Packard committed
349 350 351 352 353

static void
FcCharSetIterStart (const FcCharSet *fcs, FcCharSetIter *iter)
{
    iter->ucs4 = 0;
Patrick Lam's avatar
Patrick Lam committed
354
    iter->pos = 0;
Keith Packard's avatar
Keith Packard committed
355 356 357 358 359 360
    FcCharSetIterSet (fcs, iter);
}

FcCharSet *
FcCharSetCopy (FcCharSet *src)
{
361 362
    if (src)
    {
363 364
	if (!FcRefIsConst (&src->ref))
	    FcRefInc (&src->ref);
365 366 367
	else
	    FcCacheObjectReference (src);
    }
Keith Packard's avatar
Keith Packard committed
368 369 370 371 372 373 374 375
    return src;
}

FcBool
FcCharSetEqual (const FcCharSet *a, const FcCharSet *b)
{
    FcCharSetIter   ai, bi;
    int		    i;
376

Keith Packard's avatar
Keith Packard committed
377 378
    if (a == b)
	return FcTrue;
379 380
    if (!a || !b)
	return FcFalse;
Keith Packard's avatar
Keith Packard committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
    for (FcCharSetIterStart (a, &ai), FcCharSetIterStart (b, &bi);
	 ai.leaf && bi.leaf;
	 FcCharSetIterNext (a, &ai), FcCharSetIterNext (b, &bi))
    {
	if (ai.ucs4 != bi.ucs4)
	    return FcFalse;
	for (i = 0; i < 256/32; i++)
	    if (ai.leaf->map[i] != bi.leaf->map[i])
		return FcFalse;
    }
    return ai.leaf == bi.leaf;
}

static FcBool
FcCharSetAddLeaf (FcCharSet	*fcs,
		  FcChar32	ucs4,
		  FcCharLeaf	*leaf)
{
    FcCharLeaf   *new = FcCharSetFindLeafCreate (fcs, ucs4);
    if (!new)
	return FcFalse;
    *new = *leaf;
    return FcTrue;
}

static FcCharSet *
FcCharSetOperate (const FcCharSet   *a,
		  const FcCharSet   *b,
		  FcBool	    (*overlap) (FcCharLeaf	    *result,
						const FcCharLeaf    *al,
						const FcCharLeaf    *bl),
		  FcBool	aonly,
		  FcBool	bonly)
{
    FcCharSet	    *fcs;
    FcCharSetIter   ai, bi;

418 419
    if (!a || !b)
	goto bail0;
Keith Packard's avatar
Keith Packard committed
420 421 422 423 424
    fcs = FcCharSetCreate ();
    if (!fcs)
	goto bail0;
    FcCharSetIterStart (a, &ai);
    FcCharSetIterStart (b, &bi);
425
    while ((ai.leaf || (bonly && bi.leaf)) && (bi.leaf || (aonly && ai.leaf)))
Keith Packard's avatar
Keith Packard committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
    {
	if (ai.ucs4 < bi.ucs4)
	{
	    if (aonly)
	    {
		if (!FcCharSetAddLeaf (fcs, ai.ucs4, ai.leaf))
		    goto bail1;
		FcCharSetIterNext (a, &ai);
	    }
	    else
	    {
		ai.ucs4 = bi.ucs4;
		FcCharSetIterSet (a, &ai);
	    }
	}
	else if (bi.ucs4 < ai.ucs4 )
	{
	    if (bonly)
	    {
		if (!FcCharSetAddLeaf (fcs, bi.ucs4, bi.leaf))
		    goto bail1;
		FcCharSetIterNext (b, &bi);
	    }
	    else
	    {
		bi.ucs4 = ai.ucs4;
		FcCharSetIterSet (b, &bi);
	    }
	}
	else
	{
	    FcCharLeaf  leaf;

	    if ((*overlap) (&leaf, ai.leaf, bi.leaf))
	    {
		if (!FcCharSetAddLeaf (fcs, ai.ucs4, &leaf))
		    goto bail1;
	    }
	    FcCharSetIterNext (a, &ai);
	    FcCharSetIterNext (b, &bi);
	}
    }
    return fcs;
bail1:
    FcCharSetDestroy (fcs);
bail0:
    return 0;
}

static FcBool
FcCharSetIntersectLeaf (FcCharLeaf *result,
			const FcCharLeaf *al,
			const FcCharLeaf *bl)
{
    int	    i;
    FcBool  nonempty = FcFalse;

    for (i = 0; i < 256/32; i++)
	if ((result->map[i] = al->map[i] & bl->map[i]))
	    nonempty = FcTrue;
    return nonempty;
}

FcCharSet *
FcCharSetIntersect (const FcCharSet *a, const FcCharSet *b)
{
    return FcCharSetOperate (a, b, FcCharSetIntersectLeaf, FcFalse, FcFalse);
}

static FcBool
FcCharSetUnionLeaf (FcCharLeaf *result,
		    const FcCharLeaf *al,
		    const FcCharLeaf *bl)
{
    int	i;

    for (i = 0; i < 256/32; i++)
	result->map[i] = al->map[i] | bl->map[i];
    return FcTrue;
}

FcCharSet *
FcCharSetUnion (const FcCharSet *a, const FcCharSet *b)
{
    return FcCharSetOperate (a, b, FcCharSetUnionLeaf, FcTrue, FcTrue);
}

Karl Tomlinson's avatar
Karl Tomlinson committed
513 514
FcBool
FcCharSetMerge (FcCharSet *a, const FcCharSet *b, FcBool *changed)
515
{
Karl Tomlinson's avatar
Karl Tomlinson committed
516 517
    int		ai = 0, bi = 0;
    FcChar16	an, bn;
518

519 520 521
    if (!a || !b)
	return FcFalse;

522
    if (FcRefIsConst (&a->ref)) {
523 524
	if (changed)
	    *changed = FcFalse;
Karl Tomlinson's avatar
Karl Tomlinson committed
525
	return FcFalse;
526
    }
527

Karl Tomlinson's avatar
Karl Tomlinson committed
528 529 530 531 532
    if (changed) {
	*changed = !FcCharSetIsSubset(b, a);
	if (!*changed)
	    return FcTrue;
    }
533

Karl Tomlinson's avatar
Karl Tomlinson committed
534
    while (bi < b->num)
535
    {
Karl Tomlinson's avatar
Karl Tomlinson committed
536 537
	an = ai < a->num ? FcCharSetNumbers(a)[ai] : ~0;
	bn = FcCharSetNumbers(b)[bi];
538

Karl Tomlinson's avatar
Karl Tomlinson committed
539
	if (an < bn)
540
	{
Karl Tomlinson's avatar
Karl Tomlinson committed
541 542 543
	    ai = FcCharSetFindLeafForward (a, ai + 1, bn);
	    if (ai < 0)
		ai = -ai - 1;
544 545 546
	}
	else
	{
Karl Tomlinson's avatar
Karl Tomlinson committed
547 548
	    FcCharLeaf *bl = FcCharSetLeaf(b, bi);
	    if (bn < an)
549
	    {
Karl Tomlinson's avatar
Karl Tomlinson committed
550 551 552 553 554 555 556
		if (!FcCharSetAddLeaf (a, bn << 8, bl))
		    return FcFalse;
	    }
	    else
	    {
		FcCharLeaf *al = FcCharSetLeaf(a, ai);
		FcCharSetUnionLeaf (al, al, bl);
557 558
	    }

Karl Tomlinson's avatar
Karl Tomlinson committed
559 560
	    ai++;
	    bi++;
561 562 563
	}
    }

Karl Tomlinson's avatar
Karl Tomlinson committed
564
    return FcTrue;
565 566
}

Keith Packard's avatar
Keith Packard committed
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
static FcBool
FcCharSetSubtractLeaf (FcCharLeaf *result,
		       const FcCharLeaf *al,
		       const FcCharLeaf *bl)
{
    int	    i;
    FcBool  nonempty = FcFalse;

    for (i = 0; i < 256/32; i++)
	if ((result->map[i] = al->map[i] & ~bl->map[i]))
	    nonempty = FcTrue;
    return nonempty;
}

FcCharSet *
FcCharSetSubtract (const FcCharSet *a, const FcCharSet *b)
{
    return FcCharSetOperate (a, b, FcCharSetSubtractLeaf, FcTrue, FcFalse);
}

FcBool
FcCharSetHasChar (const FcCharSet *fcs, FcChar32 ucs4)
{
590 591 592 593 594
    FcCharLeaf	*leaf;

    if (!fcs)
	return FcFalse;
    leaf = FcCharSetFindLeaf (fcs, ucs4);
Keith Packard's avatar
Keith Packard committed
595 596 597 598 599 600 601 602
    if (!leaf)
	return FcFalse;
    return (leaf->map[(ucs4 & 0xff) >> 5] & (1 << (ucs4 & 0x1f))) != 0;
}

static FcChar32
FcCharSetPopCount (FcChar32 c1)
{
603 604 605
#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
    return __builtin_popcount (c1);
#else
Keith Packard's avatar
Keith Packard committed
606 607 608 609
    /* hackmem 169 */
    FcChar32	c2 = (c1 >> 1) & 033333333333;
    c2 = c1 - c2 - ((c2 >> 1) & 033333333333);
    return (((c2 + (c2 >> 3)) & 030707070707) % 077);
610
#endif
Keith Packard's avatar
Keith Packard committed
611 612 613 614 615 616 617
}

FcChar32
FcCharSetIntersectCount (const FcCharSet *a, const FcCharSet *b)
{
    FcCharSetIter   ai, bi;
    FcChar32	    count = 0;
618

619
    if (a && b)
Keith Packard's avatar
Keith Packard committed
620
    {
621 622 623
	FcCharSetIterStart (a, &ai);
	FcCharSetIterStart (b, &bi);
	while (ai.leaf && bi.leaf)
Keith Packard's avatar
Keith Packard committed
624
	{
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
	    if (ai.ucs4 == bi.ucs4)
	    {
		FcChar32	*am = ai.leaf->map;
		FcChar32	*bm = bi.leaf->map;
		int		i = 256/32;
		while (i--)
		    count += FcCharSetPopCount (*am++ & *bm++);
		FcCharSetIterNext (a, &ai);
	    }
	    else if (ai.ucs4 < bi.ucs4)
	    {
		ai.ucs4 = bi.ucs4;
		FcCharSetIterSet (a, &ai);
	    }
	    if (bi.ucs4 < ai.ucs4)
	    {
		bi.ucs4 = ai.ucs4;
		FcCharSetIterSet (b, &bi);
	    }
Keith Packard's avatar
Keith Packard committed
644 645 646 647 648 649 650 651 652 653
	}
    }
    return count;
}

FcChar32
FcCharSetCount (const FcCharSet *a)
{
    FcCharSetIter   ai;
    FcChar32	    count = 0;
654

655
    if (a)
Keith Packard's avatar
Keith Packard committed
656
    {
657 658 659 660
	for (FcCharSetIterStart (a, &ai); ai.leaf; FcCharSetIterNext (a, &ai))
	{
	    int		    i = 256/32;
	    FcChar32	    *am = ai.leaf->map;
Keith Packard's avatar
Keith Packard committed
661

662 663 664
	    while (i--)
		count += FcCharSetPopCount (*am++);
	}
Keith Packard's avatar
Keith Packard committed
665 666 667 668 669 670 671 672 673
    }
    return count;
}

FcChar32
FcCharSetSubtractCount (const FcCharSet *a, const FcCharSet *b)
{
    FcCharSetIter   ai, bi;
    FcChar32	    count = 0;
674

675
    if (a && b)
Keith Packard's avatar
Keith Packard committed
676
    {
677 678 679
	FcCharSetIterStart (a, &ai);
	FcCharSetIterStart (b, &bi);
	while (ai.leaf)
Keith Packard's avatar
Keith Packard committed
680
	{
681
	    if (ai.ucs4 <= bi.ucs4)
Keith Packard's avatar
Keith Packard committed
682
	    {
683 684 685 686 687 688 689 690 691 692 693 694 695 696
		FcChar32	*am = ai.leaf->map;
		int		i = 256/32;
		if (ai.ucs4 == bi.ucs4)
		{
		    FcChar32	*bm = bi.leaf->map;
		    while (i--)
			count += FcCharSetPopCount (*am++ & ~*bm++);
		}
		else
		{
		    while (i--)
			count += FcCharSetPopCount (*am++);
		}
		FcCharSetIterNext (a, &ai);
Keith Packard's avatar
Keith Packard committed
697
	    }
698
	    else if (bi.leaf)
Keith Packard's avatar
Keith Packard committed
699
	    {
700 701
		bi.ucs4 = ai.ucs4;
		FcCharSetIterSet (b, &bi);
Keith Packard's avatar
Keith Packard committed
702 703 704 705 706 707
	    }
	}
    }
    return count;
}

708 709 710 711 712 713
/*
 * return FcTrue iff a is a subset of b
 */
FcBool
FcCharSetIsSubset (const FcCharSet *a, const FcCharSet *b)
{
714 715
    int		ai, bi;
    FcChar16	an, bn;
716

717 718 719 720
    if (a == b)
	return FcTrue;
    if (!a || !b)
	return FcFalse;
721 722 723
    bi = 0;
    ai = 0;
    while (ai < a->num && bi < b->num)
724
    {
725 726
	an = FcCharSetNumbers(a)[ai];
	bn = FcCharSetNumbers(b)[bi];
727 728 729
	/*
	 * Check matching pages
	 */
730
	if (an == bn)
731
	{
732 733
	    FcChar32	*am = FcCharSetLeaf(a, ai)->map;
	    FcChar32	*bm = FcCharSetLeaf(b, bi)->map;
734
	
735
	    if (am != bm)
736
	    {
737
		int	i = 256/32;
738 739 740
		/*
		 * Does am have any bits not in bm?
		 */
741 742 743
		while (i--)
		    if (*am++ & ~*bm++)
			return FcFalse;
744
	    }
745 746 747
	    ai++;
	    bi++;
	}
748 749 750
	/*
	 * Does a have any pages not in b?
	 */
751 752 753 754
	else if (an < bn)
	    return FcFalse;
	else
	{
Karl Tomlinson's avatar
Karl Tomlinson committed
755 756 757
	    bi = FcCharSetFindLeafForward (b, bi + 1, an);
	    if (bi < 0)
		bi = -bi - 1;
758 759
	}
    }
760 761 762
    /*
     * did we look at every page?
     */
763
    return ai >= a->num;
764 765
}

766 767 768 769 770
/*
 * These two functions efficiently walk the entire charmap for
 * other software (like pango) that want their own copy
 */

771
FcChar32
772
FcCharSetNextPage (const FcCharSet  *a,
773 774
		   FcChar32	    map[FC_CHARSET_MAP_SIZE],
		   FcChar32	    *next)
775 776
{
    FcCharSetIter   ai;
777
    FcChar32	    page;
778

779 780
    if (!a)
	return FC_CHARSET_DONE;
781
    ai.ucs4 = *next;
782 783
    FcCharSetIterSet (a, &ai);
    if (!ai.leaf)
784
	return FC_CHARSET_DONE;
785

786 787 788 789 790 791 792 793 794 795 796
    /*
     * Save current information
     */
    page = ai.ucs4;
    memcpy (map, ai.leaf->map, sizeof (ai.leaf->map));
    /*
     * Step to next page
     */
    FcCharSetIterNext (a, &ai);
    *next = ai.ucs4;

797 798 799
    return page;
}

800
FcChar32
801
FcCharSetFirstPage (const FcCharSet *a,
802 803 804 805 806 807 808
		    FcChar32	    map[FC_CHARSET_MAP_SIZE],
		    FcChar32	    *next)
{
    *next = 0;
    return FcCharSetNextPage (a, map, next);
}

809 810 811
/*
 * old coverage API, rather hard to use correctly
 */
812

813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
FcChar32
FcCharSetCoverage (const FcCharSet *a, FcChar32 page, FcChar32 *result)
{
    FcCharSetIter   ai;

    ai.ucs4 = page;
    FcCharSetIterSet (a, &ai);
    if (!ai.leaf)
    {
	memset (result, '\0', 256 / 8);
	page = 0;
    }
    else
    {
	memcpy (result, ai.leaf->map, sizeof (ai.leaf->map));
	FcCharSetIterNext (a, &ai);
	page = ai.ucs4;
    }
    return page;
}

Keith Packard's avatar
Keith Packard committed
834
static FcBool
835 836 837 838 839 840
FcNameParseRange (FcChar8 **string, FcChar32 *pfirst, FcChar32 *plast)
{
	char *s = (char *) *string;
	char *t;
	long first, last;

Behdad Esfahbod's avatar
Minor  
Behdad Esfahbod committed
841
	while (isspace(*s))
842 843 844 845 846 847
	    s++;
	t = s;
	errno = 0;
	first = last = strtol (s, &s, 16);
	if (errno)
	    return FcFalse;
Behdad Esfahbod's avatar
Minor  
Behdad Esfahbod committed
848
	while (isspace(*s))
849 850
	    s++;
	if (*s == '-')
Keith Packard's avatar
Keith Packard committed
851
	{
852 853 854 855
	    s++;
	    errno = 0;
	    last = strtol (s, &s, 16);
	    if (errno)
Keith Packard's avatar
Keith Packard committed
856
		return FcFalse;
857 858 859 860 861 862 863 864 865
	}

	if (s == t || first < 0 || last < 0 || last < first || last > 0x10ffff)
	     return FcFalse;

	*string = (FcChar8 *) s;
	*pfirst = first;
	*plast = last;
	return FcTrue;
Keith Packard's avatar
Keith Packard committed
866 867
}

868 869 870 871
FcCharSet *
FcNameParseCharSet (FcChar8 *string)
{
    FcCharSet	*c;
872
    FcChar32	first, last;
873 874 875 876 877 878

    c = FcCharSetCreate ();
    if (!c)
	goto bail0;
    while (*string)
    {
879 880 881
	FcChar32 u;

	if (!FcNameParseRange (&string, &first, &last))
882
		goto bail1;
883 884 885

	for (u = first; u < last + 1; u++)
	    FcCharSetAddChar (c, u);
886 887 888
    }
    return c;
bail1:
889
    FcCharSetDestroy (c);
890 891 892 893
bail0:
    return NULL;
}

894 895 896 897 898 899 900 901
static void
FcNameUnparseUnicode (FcStrBuf *buf, FcChar32 u)
{
    FcChar8	    buf_static[64];
    snprintf ((char *) buf_static, sizeof (buf_static), "%x", u);
    FcStrBufString (buf, buf_static);
}

902 903 904 905
FcBool
FcNameUnparseCharSet (FcStrBuf *buf, const FcCharSet *c)
{
    FcCharSetIter   ci;
906
    FcChar32	    first, last;
907 908 909 910 911
    int		    i;
#ifdef CHECK
    int		    len = buf->len;
#endif

912 913
    first = last = 0x7FFFFFFF;

914 915 916 917 918
    for (FcCharSetIterStart (c, &ci);
	 ci.leaf;
	 FcCharSetIterNext (c, &ci))
    {
	for (i = 0; i < 256/32; i++)
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
	{
	    FcChar32 bits = ci.leaf->map[i];
	    FcChar32 u = ci.ucs4 + i * 32;

	    while (bits)
	    {
		if (bits & 1)
		{
			if (u != last + 1)
			{
			    if (last != first)
			    {
				FcStrBufChar (buf, '-');
				FcNameUnparseUnicode (buf, last);
			    }
			    if (last != 0x7FFFFFFF)
935
				FcStrBufChar (buf, ' ');
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
			    /* Start new range. */
			    first = u;
			    FcNameUnparseUnicode (buf, u);
			}
			last = u;
		}
		bits >>= 1;
		u++;
	    }
	}
    }
    if (last != first)
    {
	FcStrBufChar (buf, '-');
	FcNameUnparseUnicode (buf, last);
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
    }
#ifdef CHECK
    {
	FcCharSet	*check;
	FcChar32	missing;
	FcCharSetIter	ci, checki;
	
	/* null terminate for parser */
	FcStrBufChar (buf, '\0');
	/* step back over null for life after test */
	buf->len--;
	check = FcNameParseCharSet (buf->buf + len);
	FcCharSetIterStart (c, &ci);
	FcCharSetIterStart (check, &checki);
	while (ci.leaf || checki.leaf)
	{
	    if (ci.ucs4 < checki.ucs4)
	    {
		printf ("Missing leaf node at 0x%x\n", ci.ucs4);
		FcCharSetIterNext (c, &ci);
	    }
	    else if (checki.ucs4 < ci.ucs4)
	    {
		printf ("Extra leaf node at 0x%x\n", checki.ucs4);
		FcCharSetIterNext (check, &checki);
	    }
	    else
	    {
		int	    i = 256/32;
		FcChar32    *cm = ci.leaf->map;
		FcChar32    *checkm = checki.leaf->map;

		for (i = 0; i < 256; i += 32)
		{
		    if (*cm != *checkm)
			printf ("Mismatching sets at 0x%08x: 0x%08x != 0x%08x\n",
				ci.ucs4 + i, *cm, *checkm);
		    cm++;
		    checkm++;
		}
		FcCharSetIterNext (c, &ci);
		FcCharSetIterNext (check, &checki);
	    }
	}
	if ((missing = FcCharSetSubtractCount (c, check)))
	    printf ("%d missing in reparsed result\n", missing);
	if ((missing = FcCharSetSubtractCount (check, c)))
	    printf ("%d extra in reparsed result\n", missing);
	FcCharSetDestroy (check);
    }
#endif
1002

1003 1004 1005
    return FcTrue;
}

1006 1007 1008 1009 1010 1011 1012 1013 1014
typedef struct _FcCharLeafEnt FcCharLeafEnt;

struct _FcCharLeafEnt {
    FcCharLeafEnt   *next;
    FcChar32	    hash;
    FcCharLeaf	    leaf;
};

#define FC_CHAR_LEAF_BLOCK	(4096 / sizeof (FcCharLeafEnt))
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
#define FC_CHAR_LEAF_HASH_SIZE	257

typedef struct _FcCharSetEnt FcCharSetEnt;

struct _FcCharSetEnt {
    FcCharSetEnt	*next;
    FcChar32		hash;
    FcCharSet		set;
};

typedef struct _FcCharSetOrigEnt FcCharSetOrigEnt;

struct _FcCharSetOrigEnt {
    FcCharSetOrigEnt	*next;
    const FcCharSet    	*orig;
    const FcCharSet    	*frozen;
};

#define FC_CHAR_SET_HASH_SIZE    67

struct _FcCharSetFreezer {
    FcCharLeafEnt   *leaf_hash_table[FC_CHAR_LEAF_HASH_SIZE];
    FcCharLeafEnt   **leaf_blocks;
    int		    leaf_block_count;
    FcCharSetEnt    *set_hash_table[FC_CHAR_SET_HASH_SIZE];
    FcCharSetOrigEnt	*orig_hash_table[FC_CHAR_SET_HASH_SIZE];
    FcCharLeafEnt   *current_block;
    int		    leaf_remain;
    int		    leaves_seen;
    int		    charsets_seen;
    int		    leaves_allocated;
    int		    charsets_allocated;
};
1048 1049

static FcCharLeafEnt *
1050
FcCharLeafEntCreate (FcCharSetFreezer *freezer)
1051
{
1052
    if (!freezer->leaf_remain)
1053
    {
1054 1055
	FcCharLeafEnt **newBlocks;

1056 1057
	freezer->leaf_block_count++;
	newBlocks = realloc (freezer->leaf_blocks, freezer->leaf_block_count * sizeof (FcCharLeafEnt *));
1058 1059
	if (!newBlocks)
	    return 0;
1060 1061 1062
	freezer->leaf_blocks = newBlocks;
	freezer->current_block = freezer->leaf_blocks[freezer->leaf_block_count-1] = malloc (FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
	if (!freezer->current_block)
1063
	    return 0;
1064
	freezer->leaf_remain = FC_CHAR_LEAF_BLOCK;
1065
    }
1066 1067 1068
    freezer->leaf_remain--;
    freezer->leaves_allocated++;
    return freezer->current_block++;
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
}

static FcChar32
FcCharLeafHash (FcCharLeaf *leaf)
{
    FcChar32	hash = 0;
    int		i;

    for (i = 0; i < 256/32; i++)
	hash = ((hash << 1) | (hash >> 31)) ^ leaf->map[i];
    return hash;
}

static FcCharLeaf *
1083
FcCharSetFreezeLeaf (FcCharSetFreezer *freezer, FcCharLeaf *leaf)
1084 1085
{
    FcChar32			hash = FcCharLeafHash (leaf);
1086
    FcCharLeafEnt		**bucket = &freezer->leaf_hash_table[hash % FC_CHAR_LEAF_HASH_SIZE];
1087
    FcCharLeafEnt		*ent;
1088

1089 1090 1091 1092 1093 1094
    for (ent = *bucket; ent; ent = ent->next)
    {
	if (ent->hash == hash && !memcmp (&ent->leaf, leaf, sizeof (FcCharLeaf)))
	    return &ent->leaf;
    }

1095
    ent = FcCharLeafEntCreate(freezer);
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
    if (!ent)
	return 0;
    ent->leaf = *leaf;
    ent->hash = hash;
    ent->next = *bucket;
    *bucket = ent;
    return &ent->leaf;
}

static FcChar32
FcCharSetHash (FcCharSet *fcs)
{
    FcChar32	hash = 0;
    int		i;

    /* hash in leaves */
1112 1113
    for (i = 0; i < fcs->num; i++)
	hash = ((hash << 1) | (hash >> 31)) ^ FcCharLeafHash (FcCharSetLeaf(fcs,i));
1114 1115
    /* hash in numbers */
    for (i = 0; i < fcs->num; i++)
1116
	hash = ((hash << 1) | (hash >> 31)) ^ FcCharSetNumbers(fcs)[i];
1117 1118 1119
    return hash;
}

1120 1121 1122 1123 1124
static FcBool
FcCharSetFreezeOrig (FcCharSetFreezer *freezer, const FcCharSet *orig, const FcCharSet *frozen)
{
    FcCharSetOrigEnt	**bucket = &freezer->orig_hash_table[((uintptr_t) orig) & FC_CHAR_SET_HASH_SIZE];
    FcCharSetOrigEnt	*ent;
1125

1126 1127 1128 1129 1130 1131 1132 1133 1134
    ent = malloc (sizeof (FcCharSetOrigEnt));
    if (!ent)
	return FcFalse;
    ent->orig = orig;
    ent->frozen = frozen;
    ent->next = *bucket;
    *bucket = ent;
    return FcTrue;
}
1135

1136
static FcCharSet *
1137
FcCharSetFreezeBase (FcCharSetFreezer *freezer, FcCharSet *fcs)
1138 1139
{
    FcChar32		hash = FcCharSetHash (fcs);
1140
    FcCharSetEnt	**bucket = &freezer->set_hash_table[hash % FC_CHAR_SET_HASH_SIZE];
1141
    FcCharSetEnt	*ent;
1142
    int			size;
1143
    int			i;
1144 1145 1146 1147 1148

    for (ent = *bucket; ent; ent = ent->next)
    {
	if (ent->hash == hash &&
	    ent->set.num == fcs->num &&
1149
	    !memcmp (FcCharSetNumbers(&ent->set),
1150
		     FcCharSetNumbers(fcs),
1151 1152
		     fcs->num * sizeof (FcChar16)))
	{
1153 1154 1155 1156
	    FcBool ok = FcTrue;
	    int i;

	    for (i = 0; i < fcs->num; i++)
1157
		if (FcCharSetLeaf(&ent->set, i) != FcCharSetLeaf(fcs, i))
1158 1159 1160
		    ok = FcFalse;
	    if (ok)
		return &ent->set;
1161 1162 1163
	}
    }

1164 1165 1166 1167
    size = (sizeof (FcCharSetEnt) +
	    fcs->num * sizeof (FcCharLeaf *) +
	    fcs->num * sizeof (FcChar16));
    ent = malloc (size);
1168 1169
    if (!ent)
	return 0;
1170

1171
    freezer->charsets_allocated++;
1172

1173
    FcRefSetConst (&ent->set.ref);
1174
    ent->set.num = fcs->num;
1175
    if (fcs->num)
1176
    {
1177 1178 1179 1180 1181
	intptr_t    *ent_leaves;

	ent->set.leaves_offset = sizeof (ent->set);
	ent->set.numbers_offset = (ent->set.leaves_offset +
				   fcs->num * sizeof (intptr_t));
1182

1183 1184 1185 1186
	ent_leaves = FcCharSetLeaves (&ent->set);
	for (i = 0; i < fcs->num; i++)
	    ent_leaves[i] = FcPtrToOffset (ent_leaves,
					   FcCharSetLeaf (fcs, i));
1187 1188
	memcpy (FcCharSetNumbers (&ent->set),
		FcCharSetNumbers (fcs),
1189
		fcs->num * sizeof (FcChar16));
1190 1191 1192
    }
    else
    {
1193 1194
	ent->set.leaves_offset = 0;
	ent->set.numbers_offset = 0;
1195
    }
1196 1197 1198 1199

    ent->hash = hash;
    ent->next = *bucket;
    *bucket = ent;
1200

1201 1202 1203
    return &ent->set;
}

1204 1205
static const FcCharSet *
FcCharSetFindFrozen (FcCharSetFreezer *freezer, const FcCharSet *orig)
1206
{
1207 1208
    FcCharSetOrigEnt    **bucket = &freezer->orig_hash_table[((uintptr_t) orig) & FC_CHAR_SET_HASH_SIZE];
    FcCharSetOrigEnt	*ent;
1209

1210 1211 1212 1213
    for (ent = *bucket; ent; ent = ent->next)
	if (ent->orig == orig)
	    return ent->frozen;
    return NULL;
1214 1215
}

1216
const FcCharSet *
1217
FcCharSetFreeze (FcCharSetFreezer *freezer, const FcCharSet *fcs)
1218
{
1219 1220 1221 1222
    FcCharSet	    *b;
    const FcCharSet *n = 0;
    FcCharLeaf	    *l;
    int		    i;
1223 1224 1225 1226 1227 1228

    b = FcCharSetCreate ();
    if (!b)
	goto bail0;
    for (i = 0; i < fcs->num; i++)
    {
1229
	l = FcCharSetFreezeLeaf (freezer, FcCharSetLeaf(fcs, i));
1230 1231
	if (!l)
	    goto bail1;
1232
	if (!FcCharSetInsertLeaf (b, FcCharSetNumbers(fcs)[i] << 8, l))
1233 1234
	    goto bail1;
    }
1235
    n = FcCharSetFreezeBase (freezer, b);
1236 1237 1238 1239 1240 1241 1242
    if (!FcCharSetFreezeOrig (freezer, fcs, n))
    {
	n = NULL;
	goto bail1;
    }
    freezer->charsets_seen++;
    freezer->leaves_seen += fcs->num;
1243
bail1:
1244
    if (b->num)
1245
	free (FcCharSetLeaves (b));
1246
    if (b->num)
1247
	free (FcCharSetNumbers (b));
1248 1249 1250 1251 1252
    free (b);
bail0:
    return n;
}

1253
FcCharSetFreezer *
1254
FcCharSetFreezerCreate (void)
Keith Packard's avatar
Keith Packard committed
1255
{
1256
    FcCharSetFreezer	*freezer;
Keith Packard's avatar
Keith Packard committed
1257

1258 1259
    freezer = calloc (1, sizeof (FcCharSetFreezer));
    return freezer;
Keith Packard's avatar
Keith Packard committed
1260 1261
}

1262 1263
void
FcCharSetFreezerDestroy (FcCharSetFreezer *freezer)
Keith Packard's avatar
Keith Packard committed
1264
{
1265
    int i;
Keith Packard's avatar
Keith Packard committed
1266

1267
    if (FcDebug() & FC_DBG_CACHE)
Keith Packard's avatar
Keith Packard committed
1268
    {
1269 1270 1271
	printf ("\ncharsets %d -> %d leaves %d -> %d\n",
		freezer->charsets_seen, freezer->charsets_allocated,
		freezer->leaves_seen, freezer->leaves_allocated);
Keith Packard's avatar
Keith Packard committed
1272
    }
1273
    for (i = 0; i < FC_CHAR_SET_HASH_SIZE; i++)
Keith Packard's avatar
Keith Packard committed
1274
    {
1275 1276
	FcCharSetEnt	*ent, *next;
	for (ent = freezer->set_hash_table[i]; ent; ent = next)
Keith Packard's avatar
Keith Packard committed
1277
	{
1278 1279 1280 1281
	    next = ent->next;
	    free (ent);
	}
    }
Keith Packard's avatar
Keith Packard committed
1282

1283 1284 1285 1286 1287 1288 1289
    for (i = 0; i < FC_CHAR_SET_HASH_SIZE; i++)
    {
	FcCharSetOrigEnt	*ent, *next;
	for (ent = freezer->orig_hash_table[i]; ent; ent = next)
	{
	    next = ent->next;
	    free (ent);
Keith Packard's avatar
Keith Packard committed
1290 1291
	}
    }
1292 1293 1294 1295 1296 1297

    for (i = 0; i < freezer->leaf_block_count; i++)
	free (freezer->leaf_blocks[i]);

    free (freezer->leaf_blocks);
    free (freezer);
Keith Packard's avatar
Keith Packard committed
1298
}
1299

1300 1301
FcBool
FcCharSetSerializeAlloc (FcSerialize *serialize, const FcCharSet *cs)
1302
{
1303 1304
    intptr_t	    *leaves;
    FcChar16	    *numbers;
1305
    int		    i;
1306

1307
    if (!FcRefIsConst (&cs->ref))
1308 1309 1310 1311 1312 1313 1314
    {
	if (!serialize->cs_freezer)
	{
	    serialize->cs_freezer = FcCharSetFreezerCreate ();
	    if (!serialize->cs_freezer)
		return FcFalse;
	}
1315 1316
	if (FcCharSetFindFrozen (serialize->cs_freezer, cs))
	    return FcTrue;
1317

1318 1319
        cs = FcCharSetFreeze (serialize->cs_freezer, cs);
    }
1320

1321 1322
    leaves = FcCharSetLeaves (cs);
    numbers = FcCharSetNumbers (cs);
1323

1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
    if (!FcSerializeAlloc (serialize, cs, sizeof (FcCharSet)))
	return FcFalse;
    if (!FcSerializeAlloc (serialize, leaves, cs->num * sizeof (intptr_t)))
	return FcFalse;
    if (!FcSerializeAlloc (serialize, numbers, cs->num * sizeof (FcChar16)))
	return FcFalse;
    for (i = 0; i < cs->num; i++)
	if (!FcSerializeAlloc (serialize, FcCharSetLeaf(cs, i),
			       sizeof (FcCharLeaf)))
	    return FcFalse;
1334 1335
    return FcTrue;
}
1336

1337 1338
FcCharSet *
FcCharSetSerialize(FcSerialize *serialize, const FcCharSet *cs)
1339
{
1340
    FcCharSet	*cs_serialized;
1341 1342 1343 1344
    intptr_t	*leaves, *leaves_serialized;
    FcChar16	*numbers, *numbers_serialized;
    FcCharLeaf	*leaf, *leaf_serialized;
    int		i;
1345

1346
    if (!FcRefIsConst (&cs->ref) && serialize->cs_freezer)