fccharset.c 32.6 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 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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
 * documentation, and that the name of Keith Packard not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Keith Packard makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * 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 38 39

/* #define CHECK */

FcCharSet *
FcCharSetCreate (void)
{
    FcCharSet	*fcs;

    fcs = (FcCharSet *) malloc (sizeof (FcCharSet));
    if (!fcs)
	return 0;
    FcMemAlloc (FC_MEM_CHARSET, sizeof (FcCharSet));
    fcs->ref = 1;
40
    fcs->num = 0;
41 42
    fcs->leaves_offset = 0;
    fcs->numbers_offset = 0;
Keith Packard's avatar
Keith Packard committed
43 44 45 46 47 48 49 50 51 52 53 54
    return fcs;
}

FcCharSet *
FcCharSetNew (void)
{
    return FcCharSetCreate ();
}

void
FcCharSetDestroy (FcCharSet *fcs)
{
55
    int i;
56
    
57
    if (fcs->ref == FC_REF_CONSTANT)
58 59
    {
	FcCacheObjectDereference (fcs);
60
	return;
61
    }
62 63
    if (--fcs->ref > 0)
	return;
64
    for (i = 0; i < fcs->num; i++)
65
    {
66 67 68
	FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
	free (FcCharSetLeaf (fcs, i));
    }
69
    if (fcs->num)
70 71 72 73
    {
	FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (intptr_t));
	free (FcCharSetLeaves (fcs));
    }
74
    if (fcs->num)
75 76 77
    {
	FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcChar16));
	free (FcCharSetNumbers (fcs));
78 79 80
    }
    FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
    free (fcs);
Keith Packard's avatar
Keith Packard committed
81 82 83
}

/*
84 85 86
 * 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
Keith Packard's avatar
Keith Packard committed
87 88
 */

89 90 91
static int
FcCharSetFindLeafPos (const FcCharSet *fcs, FcChar32 ucs4)
{
92
    FcChar16		*numbers = FcCharSetNumbers(fcs);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    FcChar16		page;
    int			low = 0;
    int			high = fcs->num - 1;

    if (!numbers)
	return -1;
    ucs4 >>= 8;
    while (low <= high)
    {
	int mid = (low + high) >> 1;
	page = numbers[mid];
	if (page == ucs4)
	    return mid;
	if (page < ucs4)
	    low = mid + 1;
	else
	    high = mid - 1;
    }
111
    if (high < 0 || (high < fcs->num && numbers[high] < ucs4))
112 113 114 115
	high++;
    return -(high + 1);
}

Keith Packard's avatar
Keith Packard committed
116 117 118
static FcCharLeaf *
FcCharSetFindLeaf (const FcCharSet *fcs, FcChar32 ucs4)
{
119 120
    int	pos = FcCharSetFindLeafPos (fcs, ucs4);
    if (pos >= 0)
121
	return FcCharSetLeaf(fcs, pos);
122 123
    return 0;
}
Keith Packard's avatar
Keith Packard committed
124

125 126 127 128 129 130
static FcBool
FcCharSetPutLeaf (FcCharSet	*fcs, 
		  FcChar32	ucs4,
		  FcCharLeaf	*leaf, 
		  int		pos)
{
131 132
    intptr_t	*leaves = FcCharSetLeaves (fcs);
    FcChar16	*numbers = FcCharSetNumbers (fcs);
133 134 135 136

    ucs4 >>= 8;
    if (ucs4 >= 0x10000)
	return FcFalse;
137
    if (!fcs->num)
138 139
	leaves = malloc (sizeof (*leaves));
    else
140
    {
141 142 143 144 145 146 147
	intptr_t    *new_leaves = realloc (leaves, (fcs->num + 1) * 
					   sizeof (*leaves));
	intptr_t    distance = (intptr_t) new_leaves - (intptr_t) leaves;
	
	if (new_leaves && distance)
	{
	    int i;
148

149 150 151 152
	    for (i = 0; i < fcs->num; i++)
		new_leaves[i] -= distance;
	}
	leaves = new_leaves;
153
    }
154 155 156 157 158 159 160 161
    if (!leaves)
	return FcFalse;
    
    if (fcs->num)
	FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (intptr_t));
    FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (intptr_t));
    fcs->leaves_offset = FcPtrToOffset (fcs, leaves);
    
162
    if (!fcs->num)
163
	numbers = malloc (sizeof (FcChar16));
164
    else
165 166 167 168 169 170 171 172
	numbers = realloc (numbers, (fcs->num + 1) * sizeof (FcChar16));
    if (!numbers)
	return FcFalse;
    
    if (fcs->num)
	FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcChar16));
    FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcChar16));
    fcs->numbers_offset = FcPtrToOffset (fcs, numbers);
173
    
174 175 176 177 178 179
    memmove (leaves + pos + 1, leaves + pos, 
	     (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);
180 181
    fcs->num++;
    return FcTrue;
Keith Packard's avatar
Keith Packard committed
182 183 184 185 186 187 188
}

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

189
FcCharLeaf *
Keith Packard's avatar
Keith Packard committed
190 191
FcCharSetFindLeafCreate (FcCharSet *fcs, FcChar32 ucs4)
{
192 193
    int			pos;
    FcCharLeaf		*leaf;
Keith Packard's avatar
Keith Packard committed
194

195 196
    pos = FcCharSetFindLeafPos (fcs, ucs4);
    if (pos >= 0)
197
	return FcCharSetLeaf(fcs, pos);
198 199 200 201 202 203 204
    
    leaf = calloc (1, sizeof (FcCharLeaf));
    if (!leaf)
	return 0;
    
    pos = -pos - 1;
    if (!FcCharSetPutLeaf (fcs, ucs4, leaf, pos))
Keith Packard's avatar
Keith Packard committed
205
    {
206 207
	free (leaf);
	return 0;
Keith Packard's avatar
Keith Packard committed
208
    }
209
    FcMemAlloc (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
210 211 212 213 214 215 216 217 218 219
    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
220
    {
221
	FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
222 223 224
	free (FcCharSetLeaf (fcs, pos));
	FcCharSetLeaves(fcs)[pos] = FcPtrToOffset (FcCharSetLeaves(fcs),
						   leaf);
225
	return FcTrue;
Keith Packard's avatar
Keith Packard committed
226
    }
227 228
    pos = -pos - 1;
    return FcCharSetPutLeaf (fcs, ucs4, leaf, pos);
Keith Packard's avatar
Keith Packard committed
229 230 231 232 233 234 235 236
}

FcBool
FcCharSetAddChar (FcCharSet *fcs, FcChar32 ucs4)
{
    FcCharLeaf	*leaf;
    FcChar32	*b;
    
237
    if (fcs->ref == FC_REF_CONSTANT)
Keith Packard's avatar
Keith Packard committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
	return FcFalse;
    leaf = FcCharSetFindLeafCreate (fcs, ucs4);
    if (!leaf)
	return FcFalse;
    b = &leaf->map[(ucs4 & 0xff) >> 5];
    *b |= (1 << (ucs4 & 0x1f));
    return FcTrue;
}

/*
 * An iterator for the leaves of a charset
 */

typedef struct _fcCharSetIter {
    FcCharLeaf	    *leaf;
    FcChar32	    ucs4;
254
    int		    pos;
Keith Packard's avatar
Keith Packard committed
255 256 257
} FcCharSetIter;

/*
258
 * Set iter->leaf to the leaf containing iter->ucs4 or higher
Keith Packard's avatar
Keith Packard committed
259 260 261 262 263
 */

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

266
    if (pos < 0)
267
    {
268 269
	pos = -pos - 1;
	if (pos == fcs->num)
270
	{
271 272 273
	    iter->ucs4 = ~0;
	    iter->leaf = 0;
	    return;
274
	}
275
        iter->ucs4 = (FcChar32) FcCharSetNumbers(fcs)[pos] << 8;
276
    }
277
    iter->leaf = FcCharSetLeaf(fcs, pos);
278
    iter->pos = pos;
Keith Packard's avatar
Keith Packard committed
279 280 281 282 283
}

static void
FcCharSetIterNext (const FcCharSet *fcs, FcCharSetIter *iter)
{
284 285
    int	pos = iter->pos + 1;
    if (pos >= fcs->num)
286 287 288 289
    {
	iter->ucs4 = ~0;
	iter->leaf = 0;
    }
290
    else
291
    {
292 293
	iter->ucs4 = (FcChar32) FcCharSetNumbers(fcs)[pos] << 8;
	iter->leaf = FcCharSetLeaf(fcs, pos);
294
	iter->pos = pos;
295 296 297
    }
}

Keith Packard's avatar
Keith Packard committed
298 299 300 301 302

static void
FcCharSetIterStart (const FcCharSet *fcs, FcCharSetIter *iter)
{
    iter->ucs4 = 0;
Patrick Lam's avatar
Patrick Lam committed
303
    iter->pos = 0;
Keith Packard's avatar
Keith Packard committed
304 305 306 307 308 309
    FcCharSetIterSet (fcs, iter);
}

FcCharSet *
FcCharSetCopy (FcCharSet *src)
{
310 311
    if (src->ref != FC_REF_CONSTANT)
	src->ref++;
312 313
    else
	FcCacheObjectReference (src);
Keith Packard's avatar
Keith Packard committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    return src;
}

FcBool
FcCharSetEqual (const FcCharSet *a, const FcCharSet *b)
{
    FcCharSetIter   ai, bi;
    int		    i;
    
    if (a == b)
	return FcTrue;
    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;

    fcs = FcCharSetCreate ();
    if (!fcs)
	goto bail0;
    FcCharSetIterStart (a, &ai);
    FcCharSetIterStart (b, &bi);
367
    while ((ai.leaf || (bonly && bi.leaf)) && (bi.leaf || (aonly && ai.leaf)))
Keith Packard's avatar
Keith Packard committed
368 369 370 371 372 373 374 375 376 377 378 379 380 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 418 419 420 421 422 423 424 425 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
    {
	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);
}

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 513 514 515 516
FcCharSet *
FcCharSetMerge (FcCharSet *a, const FcCharSet *b)
{
    FcCharSet	    *fcs;
    FcCharSetIter    ai, bi;

    if (a == NULL) {
	return FcCharSetCopy ((FcCharSet *) b);
    } else if (a->ref == FC_REF_CONSTANT) {
	fcs = FcCharSetCreate ();
	if (fcs == NULL)
	    return NULL;
    } else
	fcs = a;

    FcCharSetIterStart (a, &ai);
    FcCharSetIterStart (b, &bi);
    while (ai.leaf || bi.leaf)
    {
	if (ai.ucs4 < bi.ucs4)
	{
	    if (!FcCharSetAddLeaf (fcs, ai.ucs4, ai.leaf))
		goto bail;

	    FcCharSetIterNext (a, &ai);
	}
	else if (bi.ucs4 < ai.ucs4)
	{
	    if (!FcCharSetAddLeaf (fcs, bi.ucs4, bi.leaf))
		goto bail;

	    FcCharSetIterNext (b, &bi);
	}
	else
	{
	    FcCharLeaf  leaf;

	    if (FcCharSetUnionLeaf (&leaf, ai.leaf, bi.leaf))
	    {
		if (!FcCharSetAddLeaf (fcs, ai.ucs4, &leaf))
		    goto bail;
	    }

	    FcCharSetIterNext (a, &ai);
	    FcCharSetIterNext (b, &bi);
	}
    }

    if (fcs != a)
	FcCharSetDestroy (a);

    return fcs;

bail:
    FcCharSetDestroy (fcs);

    if (fcs != a)
	FcCharSetDestroy (a);

    return NULL;
}

Keith Packard's avatar
Keith Packard committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
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)
{
    FcCharLeaf	*leaf = FcCharSetFindLeaf (fcs, ucs4);
    if (!leaf)
	return FcFalse;
    return (leaf->map[(ucs4 & 0xff) >> 5] & (1 << (ucs4 & 0x1f))) != 0;
}

static FcChar32
FcCharSetPopCount (FcChar32 c1)
{
    /* hackmem 169 */
    FcChar32	c2 = (c1 >> 1) & 033333333333;
    c2 = c1 - c2 - ((c2 >> 1) & 033333333333);
    return (((c2 + (c2 >> 3)) & 030707070707) % 077);
}

FcChar32
FcCharSetIntersectCount (const FcCharSet *a, const FcCharSet *b)
{
    FcCharSetIter   ai, bi;
    FcChar32	    count = 0;
    
    FcCharSetIterStart (a, &ai);
    FcCharSetIterStart (b, &bi);
    while (ai.leaf && bi.leaf)
    {
	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);
	}
    }
    return count;
}

FcChar32
FcCharSetCount (const FcCharSet *a)
{
    FcCharSetIter   ai;
    FcChar32	    count = 0;
    
    for (FcCharSetIterStart (a, &ai); ai.leaf; FcCharSetIterNext (a, &ai))
    {
	int		    i = 256/32;
	FcChar32	    *am = ai.leaf->map;

	while (i--)
	    count += FcCharSetPopCount (*am++);
    }
    return count;
}

FcChar32
FcCharSetSubtractCount (const FcCharSet *a, const FcCharSet *b)
{
    FcCharSetIter   ai, bi;
    FcChar32	    count = 0;
    
    FcCharSetIterStart (a, &ai);
    FcCharSetIterStart (b, &bi);
    while (ai.leaf)
    {
	if (ai.ucs4 <= bi.ucs4)
	{
	    FcChar32	*am = ai.leaf->map;
	    int		i = 256/32;
	    if (ai.ucs4 == bi.ucs4)
	    {
621
		FcChar32	*bm = bi.leaf->map;
Keith Packard's avatar
Keith Packard committed
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
		while (i--)
		    count += FcCharSetPopCount (*am++ & ~*bm++);
	    }
	    else
	    {
		while (i--)
		    count += FcCharSetPopCount (*am++);
	    }
	    FcCharSetIterNext (a, &ai);
	}
	else if (bi.leaf)
	{
	    bi.ucs4 = ai.ucs4;
	    FcCharSetIterSet (b, &bi);
	}
    }
    return count;
}

641 642 643 644 645 646
/*
 * return FcTrue iff a is a subset of b
 */
FcBool
FcCharSetIsSubset (const FcCharSet *a, const FcCharSet *b)
{
647 648 649 650 651 652 653
    int		ai, bi;
    FcChar16	an, bn;
    
    if (a == b) return FcTrue;
    bi = 0;
    ai = 0;
    while (ai < a->num && bi < b->num)
654
    {
655 656
	an = FcCharSetNumbers(a)[ai];
	bn = FcCharSetNumbers(b)[bi];
657 658 659
	/*
	 * Check matching pages
	 */
660
	if (an == bn)
661
	{
662 663
	    FcChar32	*am = FcCharSetLeaf(a, ai)->map;
	    FcChar32	*bm = FcCharSetLeaf(b, bi)->map;
664 665
	    
	    if (am != bm)
666
	    {
667
		int	i = 256/32;
668 669 670
		/*
		 * Does am have any bits not in bm?
		 */
671 672 673
		while (i--)
		    if (*am++ & ~*bm++)
			return FcFalse;
674
	    }
675 676 677
	    ai++;
	    bi++;
	}
678 679 680
	/*
	 * Does a have any pages not in b?
	 */
681 682 683 684 685 686 687
	else if (an < bn)
	    return FcFalse;
	else
	{
	    int	    low = bi + 1;
	    int	    high = b->num - 1;

688 689 690
	    /*
	     * Search for page 'an' in 'b'
	     */
691
	    while (low <= high)
692
	    {
693
		int mid = (low + high) >> 1;
694
		bn = FcCharSetNumbers(b)[mid];
695 696 697
		if (bn == an)
		{
		    high = mid;
698
		    break;
699 700 701 702 703
		}
		if (bn < an)
		    low = mid + 1;
		else
		    high = mid - 1;
704
	    }
705
	    bi = high;
706
	    while (bi < b->num && FcCharSetNumbers(b)[bi] < an)
707
		bi++;
708 709
	}
    }
710 711 712
    /*
     * did we look at every page?
     */
713
    return ai >= a->num;
714 715
}

716 717 718 719 720
/*
 * These two functions efficiently walk the entire charmap for
 * other software (like pango) that want their own copy
 */

721
FcChar32
722 723 724
FcCharSetNextPage (const FcCharSet  *a, 
		   FcChar32	    map[FC_CHARSET_MAP_SIZE],
		   FcChar32	    *next)
725 726
{
    FcCharSetIter   ai;
727
    FcChar32	    page;
728

729
    ai.ucs4 = *next;
730 731
    FcCharSetIterSet (a, &ai);
    if (!ai.leaf)
732 733 734 735 736 737 738 739 740 741 742 743 744
	return FC_CHARSET_DONE;
    
    /*
     * 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;

745 746 747
    return page;
}

748 749 750 751 752 753 754 755 756
FcChar32
FcCharSetFirstPage (const FcCharSet *a, 
		    FcChar32	    map[FC_CHARSET_MAP_SIZE],
		    FcChar32	    *next)
{
    *next = 0;
    return FcCharSetNextPage (a, map, next);
}

757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
/*
 * old coverage API, rather hard to use correctly
 */
    
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
782 783 784 785 786 787 788 789 790 791
/*
 * ASCII representation of charsets.
 * 
 * Each leaf is represented as 9 32-bit values, the code of the first character followed
 * by 8 32 bit values for the leaf itself.  Each value is encoded as 5 ASCII characters,
 * only 85 different values are used to avoid control characters as well as the other
 * characters used to encode font names.  85**5 > 2^32 so things work out, but
 * it's not exactly human readable output.  As a special case, 0 is encoded as a space
 */

792
static const unsigned char	charToValue[256] = {
Keith Packard's avatar
Keith Packard committed
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
    /*     "" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /*   "\b" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /* "\020" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /* "\030" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /*    " " */ 0xff,  0x00,  0xff,  0x01,  0x02,  0x03,  0x04,  0xff, 
    /*    "(" */ 0x05,  0x06,  0x07,  0x08,  0xff,  0xff,  0x09,  0x0a, 
    /*    "0" */ 0x0b,  0x0c,  0x0d,  0x0e,  0x0f,  0x10,  0x11,  0x12, 
    /*    "8" */ 0x13,  0x14,  0xff,  0x15,  0x16,  0xff,  0x17,  0x18, 
    /*    "@" */ 0x19,  0x1a,  0x1b,  0x1c,  0x1d,  0x1e,  0x1f,  0x20, 
    /*    "H" */ 0x21,  0x22,  0x23,  0x24,  0x25,  0x26,  0x27,  0x28, 
    /*    "P" */ 0x29,  0x2a,  0x2b,  0x2c,  0x2d,  0x2e,  0x2f,  0x30, 
    /*    "X" */ 0x31,  0x32,  0x33,  0x34,  0xff,  0x35,  0x36,  0xff, 
    /*    "`" */ 0xff,  0x37,  0x38,  0x39,  0x3a,  0x3b,  0x3c,  0x3d, 
    /*    "h" */ 0x3e,  0x3f,  0x40,  0x41,  0x42,  0x43,  0x44,  0x45, 
    /*    "p" */ 0x46,  0x47,  0x48,  0x49,  0x4a,  0x4b,  0x4c,  0x4d, 
    /*    "x" */ 0x4e,  0x4f,  0x50,  0x51,  0x52,  0x53,  0x54,  0xff, 
    /* "\200" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /* "\210" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /* "\220" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /* "\230" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /* "\240" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /* "\250" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /* "\260" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /* "\270" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /* "\300" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /* "\310" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /* "\320" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /* "\330" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /* "\340" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /* "\350" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /* "\360" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
    /* "\370" */ 0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff,  0xff, 
};

827
static const FcChar8 valueToChar[0x55] = {
Keith Packard's avatar
Keith Packard committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
    /* 0x00 */ '!', '#', '$', '%', '&', '(', ')', '*',
    /* 0x08 */ '+', '.', '/', '0', '1', '2', '3', '4',
    /* 0x10 */ '5', '6', '7', '8', '9', ';', '<', '>',
    /* 0x18 */ '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
    /* 0x20 */ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
    /* 0x28 */ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
    /* 0x30 */ 'W', 'X', 'Y', 'Z', '[', ']', '^', 'a',
    /* 0x38 */ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
    /* 0x40 */ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
    /* 0x48 */ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
    /* 0x50 */ 'z', '{', '|', '}', '~',
};

static FcChar8 *
FcCharSetParseValue (FcChar8 *string, FcChar32 *value)
{
    int		i;
    FcChar32	v;
846
    FcChar32	c;
Keith Packard's avatar
Keith Packard committed
847 848 849 850 851 852 853 854 855 856 857
    
    if (*string == ' ')
    {
	v = 0;
	string++;
    }
    else
    {
	v = 0;
	for (i = 0; i < 5; i++)
	{
858
	    if (!(c = (FcChar32) (unsigned char) *string++))
Keith Packard's avatar
Keith Packard committed
859 860 861 862 863 864 865 866 867 868 869 870
		return 0;
	    c = charToValue[c];
	    if (c == 0xff)
		return 0;
	    v = v * 85 + c;
	}
    }
    *value = v;
    return string;
}

static FcBool
871
FcCharSetUnparseValue (FcStrBuf *buf, FcChar32 value)
Keith Packard's avatar
Keith Packard committed
872 873 874 875
{
    int	    i;
    if (value == 0)
    {
876
	return FcStrBufChar (buf, ' ');
Keith Packard's avatar
Keith Packard committed
877 878 879 880 881 882 883 884 885 886 887 888
    }
    else
    {
	FcChar8	string[6];
	FcChar8	*s = string + 5;
	string[5] = '\0';
	for (i = 0; i < 5; i++)
	{
	    *--s = valueToChar[value % 85];
	    value /= 85;
	}
	for (i = 0; i < 5; i++)
889
	    if (!FcStrBufChar (buf, *s++))
Keith Packard's avatar
Keith Packard committed
890 891 892 893 894
		return FcFalse;
    }
    return FcTrue;
}

895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 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 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
FcCharSet *
FcNameParseCharSet (FcChar8 *string)
{
    FcCharSet	*c;
    FcChar32	ucs4;
    FcCharLeaf	*leaf;
    FcCharLeaf	temp;
    FcChar32	bits;
    int		i;

    c = FcCharSetCreate ();
    if (!c)
	goto bail0;
    while (*string)
    {
	string = FcCharSetParseValue (string, &ucs4);
	if (!string)
	    goto bail1;
	bits = 0;
	for (i = 0; i < 256/32; i++)
	{
	    string = FcCharSetParseValue (string, &temp.map[i]);
	    if (!string)
		goto bail1;
	    bits |= temp.map[i];
	}
	if (bits)
	{
	    leaf = malloc (sizeof (FcCharLeaf));
	    if (!leaf)
		goto bail1;
	    *leaf = temp;
	    if (!FcCharSetInsertLeaf (c, ucs4, leaf))
		goto bail1;
	}
    }
    return c;
bail1:
    if (c->num)
    {
	FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcCharLeaf *));
	free (FcCharSetLeaves (c));
    }
    if (c->num)
    {
	FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcChar16));
	free (FcCharSetNumbers (c));
    }
    FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
    free (c);
bail0:
    return NULL;
}

FcBool
FcNameUnparseCharSet (FcStrBuf *buf, const FcCharSet *c)
{
    FcCharSetIter   ci;
    int		    i;
#ifdef CHECK
    int		    len = buf->len;
#endif

    for (FcCharSetIterStart (c, &ci);
	 ci.leaf;
	 FcCharSetIterNext (c, &ci))
    {
	if (!FcCharSetUnparseValue (buf, ci.ucs4))
	    return FcFalse;
	for (i = 0; i < 256/32; i++)
	    if (!FcCharSetUnparseValue (buf, ci.leaf->map[i]))
		return FcFalse;
    }
#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
    
    return FcTrue;
}

1022 1023 1024 1025 1026 1027 1028 1029 1030
typedef struct _FcCharLeafEnt FcCharLeafEnt;

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

#define FC_CHAR_LEAF_BLOCK	(4096 / sizeof (FcCharLeafEnt))
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
#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;
};
1064 1065

static FcCharLeafEnt *
1066
FcCharLeafEntCreate (FcCharSetFreezer *freezer)
1067
{
1068
    if (!freezer->leaf_remain)
1069
    {
1070 1071
	FcCharLeafEnt **newBlocks;

1072 1073
	freezer->leaf_block_count++;
	newBlocks = realloc (freezer->leaf_blocks, freezer->leaf_block_count * sizeof (FcCharLeafEnt *));
1074 1075
	if (!newBlocks)
	    return 0;
1076 1077 1078
	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)
1079
	    return 0;
1080
	FcMemAlloc (FC_MEM_CHARLEAF, FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
1081
	freezer->leaf_remain = FC_CHAR_LEAF_BLOCK;
1082
    }
1083 1084 1085
    freezer->leaf_remain--;
    freezer->leaves_allocated++;
    return freezer->current_block++;
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
}

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 *
1100
FcCharSetFreezeLeaf (FcCharSetFreezer *freezer, FcCharLeaf *leaf)
1101 1102
{
    FcChar32			hash = FcCharLeafHash (leaf);
1103
    FcCharLeafEnt		**bucket = &freezer->leaf_hash_table[hash % FC_CHAR_LEAF_HASH_SIZE];
1104 1105 1106 1107 1108 1109 1110 1111
    FcCharLeafEnt		*ent;
    
    for (ent = *bucket; ent; ent = ent->next)
    {
	if (ent->hash == hash && !memcmp (&ent->leaf, leaf, sizeof (FcCharLeaf)))
	    return &ent->leaf;
    }

1112
    ent = FcCharLeafEntCreate(freezer);
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
    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 */
1129 1130
    for (i = 0; i < fcs->num; i++)
	hash = ((hash << 1) | (hash >> 31)) ^ FcCharLeafHash (FcCharSetLeaf(fcs,i));
1131 1132
    /* hash in numbers */
    for (i = 0; i < fcs->num; i++)
1133
	hash = ((hash << 1) | (hash >> 31)) ^ *FcCharSetNumbers(fcs);
1134 1135 1136
    return hash;
}

1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
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;
    
    ent = malloc (sizeof (FcCharSetOrigEnt));
    if (!ent)
	return FcFalse;
    ent->orig = orig;
    ent->frozen = frozen;
    ent->next = *bucket;
    *bucket = ent;
    return FcTrue;
}
1152

1153
static FcCharSet *
1154
FcCharSetFreezeBase (FcCharSetFreezer *freezer, FcCharSet *fcs, const FcCharSet *orig)
1155 1156
{
    FcChar32		hash = FcCharSetHash (fcs);
1157
    FcCharSetEnt	**bucket = &freezer->set_hash_table[hash % FC_CHAR_SET_HASH_SIZE];
1158
    FcCharSetEnt	*ent;
1159
    int			size;
1160
    int			i;
1161 1162 1163 1164 1165

    for (ent = *bucket; ent; ent = ent->next)
    {
	if (ent->hash == hash &&
	    ent->set.num == fcs->num &&
1166 1167
	    !memcmp (FcCharSetNumbers(&ent->set), 
		     FcCharSetNumbers(fcs),
1168 1169
		     fcs->num * sizeof (FcChar16)))
	{
1170 1171 1172 1173
	    FcBool ok = FcTrue;
	    int i;

	    for (i = 0; i < fcs->num; i++)
1174
		if (FcCharSetLeaf(&ent->set, i) != FcCharSetLeaf(fcs, i))
1175 1176 1177
		    ok = FcFalse;
	    if (ok)
		return &ent->set;
1178 1179 1180
	}
    }

1181 1182 1183 1184
    size = (sizeof (FcCharSetEnt) +
	    fcs->num * sizeof (FcCharLeaf *) +
	    fcs->num * sizeof (FcChar16));
    ent = malloc (size);
1185 1186
    if (!ent)
	return 0;
1187
    FcMemAlloc (FC_MEM_CHARSET, size);
1188 1189
    
    freezer->charsets_allocated++;
1190
    
1191
    ent->set.ref = FC_REF_CONSTANT;
1192
    ent->set.num = fcs->num;
1193
    if (fcs->num)
1194
    {
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
	intptr_t    *ent_leaves;

	ent->set.leaves_offset = sizeof (ent->set);
	ent->set.numbers_offset = (ent->set.leaves_offset +
				   fcs->num * sizeof (intptr_t));
    
	ent_leaves = FcCharSetLeaves (&ent->set);
	for (i = 0; i < fcs->num; i++)
	    ent_leaves[i] = FcPtrToOffset (ent_leaves,
					   FcCharSetLeaf (fcs, i));
	memcpy (FcCharSetNumbers (&ent->set), 
		FcCharSetNumbers (fcs), 
		fcs->num * sizeof (FcChar16));
1208 1209 1210
    }
    else
    {
1211 1212
	ent->set.leaves_offset = 0;
	ent->set.numbers_offset = 0;
1213
    }
1214 1215 1216 1217

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

1219 1220 1221
    return &ent->set;
}

1222 1223
static const FcCharSet *
FcCharSetFindFrozen (FcCharSetFreezer *freezer, const FcCharSet *orig)
1224
{
1225 1226 1227 1228 1229 1230 1231
    FcCharSetOrigEnt    **bucket = &freezer->orig_hash_table[((uintptr_t) orig) & FC_CHAR_SET_HASH_SIZE];
    FcCharSetOrigEnt	*ent;
    
    for (ent = *bucket; ent; ent = ent->next)
	if (ent->orig == orig)
	    return ent->frozen;
    return NULL;
1232 1233
}

1234
const FcCharSet *
1235
FcCharSetFreeze (FcCharSetFreezer *freezer, const FcCharSet *fcs)
1236
{
1237 1238 1239 1240
    FcCharSet	    *b;
    const FcCharSet *n = 0;
    FcCharLeaf	    *l;
    int		    i;
1241 1242 1243 1244 1245 1246

    b = FcCharSetCreate ();
    if (!b)
	goto bail0;
    for (i = 0; i < fcs->num; i++)
    {
1247
	l = FcCharSetFreezeLeaf (freezer, FcCharSetLeaf(fcs, i));
1248 1249
	if (!l)
	    goto bail1;
1250
	if (!FcCharSetInsertLeaf (b, FcCharSetNumbers(fcs)[i] << 8, l))
1251 1252
	    goto bail1;
    }
1253 1254 1255 1256 1257 1258 1259 1260
    n = FcCharSetFreezeBase (freezer, b, fcs);
    if (!FcCharSetFreezeOrig (freezer, fcs, n))
    {
	n = NULL;
	goto bail1;
    }
    freezer->charsets_seen++;
    freezer->leaves_seen += fcs->num;
1261
bail1:
1262
    if (b->num)
1263
    {
1264 1265 1266
	FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcCharLeaf *));
	free (FcCharSetLeaves (b));
    }
1267
    if (b->num)
1268 1269 1270
    {
	FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcChar16));
	free (FcCharSetNumbers (b));
1271 1272
    }
    FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
1273 1274 1275 1276 1277
    free (b);
bail0:
    return n;
}

1278
FcCharSetFreezer *
1279
FcCharSetFreezerCreate (void)
Keith Packard's avatar
Keith Packard committed
1280
{
1281
    FcCharSetFreezer	*freezer;
Keith Packard's avatar
Keith Packard committed
1282

1283 1284
    freezer = calloc (1, sizeof (FcCharSetFreezer));
    return freezer;
Keith Packard's avatar
Keith Packard committed
1285 1286
}

1287 1288
void
FcCharSetFreezerDestroy (FcCharSetFreezer *freezer)
Keith Packard's avatar
Keith Packard committed
1289
{
1290
    int i;
Keith Packard's avatar
Keith Packard committed
1291

1292
    if (FcDebug() & FC_DBG_CACHE)
Keith Packard's avatar
Keith Packard committed
1293
    {
1294 1295 1296
	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
1297
    }
1298
    for (i = 0; i < FC_CHAR_SET_HASH_SIZE; i++)
Keith Packard's avatar
Keith Packard committed
1299
    {
1300 1301
	FcCharSetEnt	*ent, *next;
	for (ent = freezer->set_hash_table[i]; ent; ent = next)
Keith Packard's avatar
Keith Packard committed
1302
	{
1303
	    next = ent->next;
1304 1305 1306
	    FcMemFree (FC_MEM_CHARSET, (sizeof (FcCharSetEnt) +
					ent->set.num * sizeof (FcCharLeaf *) +
					ent->set.num * sizeof (FcChar16)));
1307 1308 1309
	    free (ent);
	}
    }
Keith Packard's avatar
Keith Packard committed
1310

1311 1312 1313 1314 1315 1316 1317
    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
1318 1319
	}
    }
1320 1321

    for (i = 0; i < freezer->leaf_block_count; i++)
1322
    {
1323
	free (freezer->leaf_blocks[i]);
1324 1325
	FcMemFree (FC_MEM_CHARLEAF, FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
    }
1326 1327 1328

    free (freezer->leaf_blocks);
    free (freezer);
Keith Packard's avatar
Keith Packard committed
1329
}
1330

1331 1332
FcBool
FcCharSetSerializeAlloc (FcSerialize *serialize, const FcCharSet *cs)
1333
{
1334 1335
    intptr_t	    *leaves;
    FcChar16	    *numbers;
1336 1337
    int		    i;
    
1338 1339 1340 1341 1342 1343 1344 1345
    if (cs->ref != FC_REF_CONSTANT)
    {
	if (!serialize->cs_freezer)
	{
	    serialize->cs_freezer = FcCharSetFreezerCreate ();
	    if (!serialize->cs_freezer)
		return FcFalse;
	}
1346 1347 1348
	if (FcCharSetFindFrozen (serialize->cs_freezer, cs))
	    return FcTrue;
    
1349 1350 1351 1352 1353 1354
        cs = FcCharSetFreeze (serialize->cs_freezer, cs);
    }
    
    leaves = FcCharSetLeaves (cs);
    numbers = FcCharSetNumbers (cs);
    
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
    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;
1365 1366
    return FcTrue;
}
1367 1368 1369
    
FcCharSet *
FcCharSetSerialize(FcSerialize *serialize, const FcCharSet *cs)
1370
{
1371
    FcCharSet	*cs_serialized;
1372 1373 1374 1375
    intptr_t	*leaves, *leaves_serialized;
    FcChar16	*numbers, *numbers_serialized;
    FcCharLeaf	*leaf, *leaf_serialized;
    int		i;
1376

1377 1378 1379 1380 1381 1382 1383 1384
    if (cs->ref != FC_REF_CONSTANT && serialize->cs_freezer)
    {
	cs = FcCharSetFindFrozen (serialize->cs_freezer, cs);
	if (!cs)
	    return NULL;
    }
		    
    cs_serialized = FcSerializePtr (serialize, cs);
1385 1386 1387 1388 1389
    if (!cs_serialized)
	return NULL;
    
    cs_serialized->ref = FC_REF_CONSTANT;
    cs_serialized->num = cs->num;
1390

1391
    if (cs->num)
1392
    {
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
	leaves = FcCharSetLeaves (cs);
	leaves_serialized = FcSerializePtr (serialize, leaves);
	if (!leaves_serialized)
	    return NULL;
    
	cs_serialized->leaves_offset = FcPtrToOffset (cs_serialized,
						      leaves_serialized);
	
	numbers = FcCharSetNumbers (cs);
	numbers_serialized = FcSerializePtr (serialize, numbers);
	if (!numbers)
1404
	    return NULL;
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
    
	cs_serialized->numbers_offset = FcPtrToOffset (cs_serialized,
						       numbers_serialized);
    
	for (i = 0; i < cs->num; i++)
	{
	    leaf = FcCharSetLeaf (cs, i);
	    leaf_serialized = FcSerializePtr (serialize, leaf);
	    if (!leaf_serialized)
		return NULL;
	    *leaf_serialized = *leaf;
	    leaves_serialized[i] = FcPtrToOffset (leaves_serialized, 
						  leaf_serialized);
	    numbers_serialized[i] = numbers[i];
	}
    }
    else
    {
	cs_serialized->leaves_offset = 0;
	cs_serialized->numbers_offset = 0;
1425
    }
1426 1427
    
    return cs_serialized;
1428
}
1429 1430 1431
#define __fccharset__
#include "fcaliastail.h"
#undef __fccharset__