fccharset.c 35 KB
Newer Older
Keith Packard's avatar
Keith Packard committed
1
/*
Keith Packard's avatar
Keith Packard committed
2
 * $RCSId: xc/lib/fontconfig/src/fccharset.c,v 1.18 2002/08/22 07:36:44 keithp Exp $
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 26 27 28 29
 *
 * 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 <stdlib.h>
#include "fcint.h"

/* #define CHECK */

30
/* #define CHATTY */
Keith Packard's avatar
Keith Packard committed
31

32 33 34
static FcCharSet ** charsets = 0;
static FcChar16 ** numbers = 0;
static int charset_bank_count = 0, charset_ptr, charset_count;
35
static int charset_numbers_ptr, charset_numbers_count;
36
static FcCharLeaf ** leaves = 0;
37
static int charset_leaf_ptr, charset_leaf_count;
38
static int ** leaf_idx = 0;
39 40
static int charset_leaf_idx_ptr, charset_leaf_idx_count;

41 42 43 44 45 46 47 48 49 50
extern const FcChar16 *langBankNumbers;
extern const FcCharLeaf	*langBankLeaves;
extern const int *langBankLeafIdx;

static FcBool
FcCharSetEnsureBank (int bi);

void
FcLangCharSetPopulate (void)
{
51
    int bi = FcCacheBankToIndexMTF (FC_BANK_LANGS);
52 53 54 55 56 57 58
    FcCharSetEnsureBank (bi);
    charsets[bi] = 0;
    numbers[bi] = (FcChar16 *)&langBankNumbers;
    leaves[bi] = (FcCharLeaf *)&langBankLeaves;
    leaf_idx[bi] = (int *)&langBankLeafIdx;
}

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

    fcs = (FcCharSet *) malloc (sizeof (FcCharSet));
    if (!fcs)
	return 0;
    FcMemAlloc (FC_MEM_CHARSET, sizeof (FcCharSet));
    fcs->ref = 1;
69
    fcs->num = 0;
70
    fcs->bank = FC_BANK_DYNAMIC;
71 72
    fcs->u.dyn.leaves = 0;
    fcs->u.dyn.numbers = 0;
Keith Packard's avatar
Keith Packard committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    return fcs;
}

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

void
FcCharSetDestroy (FcCharSet *fcs)
{
88 89 90 91 92
    int i;
    if (fcs->ref == FC_REF_CONSTANT)
	return;
    if (--fcs->ref > 0)
	return;
93
    if (fcs->bank == FC_BANK_DYNAMIC)
94
    {
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	for (i = 0; i < fcs->num; i++)
	{
	    FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
	    free (fcs->u.dyn.leaves[i]);
	}
	if (fcs->u.dyn.leaves)
	{
	    FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcCharLeaf *));
	    free (fcs->u.dyn.leaves);
	}
	if (fcs->u.dyn.numbers)
	{
	    FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcChar16));
	    free (fcs->u.dyn.numbers);
	}
110 111 112
    }
    FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
    free (fcs);
Keith Packard's avatar
Keith Packard committed
113 114 115
}

/*
116 117 118
 * 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
119 120
 */

121 122 123
static int
FcCharSetFindLeafPos (const FcCharSet *fcs, FcChar32 ucs4)
{
124
    FcChar16		*numbers = FcCharSetGetNumbers(fcs);
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    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;
    }
143
    if (high < 0 || (high < fcs->num && numbers[high] < ucs4))
144 145 146 147
	high++;
    return -(high + 1);
}

Keith Packard's avatar
Keith Packard committed
148 149 150
static FcCharLeaf *
FcCharSetFindLeaf (const FcCharSet *fcs, FcChar32 ucs4)
{
151 152
    int	pos = FcCharSetFindLeafPos (fcs, ucs4);
    if (pos >= 0)
153
	return FcCharSetGetLeaf(fcs, pos);
154 155
    return 0;
}
Keith Packard's avatar
Keith Packard committed
156

157 158 159 160 161 162 163 164 165 166 167 168
static FcBool
FcCharSetPutLeaf (FcCharSet	*fcs, 
		  FcChar32	ucs4,
		  FcCharLeaf	*leaf, 
		  int		pos)
{
    FcCharLeaf	**leaves;
    FcChar16	*numbers;

    ucs4 >>= 8;
    if (ucs4 >= 0x10000)
	return FcFalse;
169
    if (fcs->bank != FC_BANK_DYNAMIC)
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    {
	int i;

	leaves = malloc ((fcs->num + 1) * sizeof (FcCharLeaf *));
	if (!leaves)
	    return FcFalse;
	FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcCharLeaf *));
	numbers = malloc ((fcs->num + 1) * sizeof (FcChar16));
	if (!numbers)
	    return FcFalse;
	FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcChar16));

	for (i = 0; i < fcs->num; i++)
	    leaves[i] = FcCharSetGetLeaf(fcs, i);
	memcpy (numbers, FcCharSetGetNumbers(fcs), 
		fcs->num * sizeof (FcChar16));
    }
187
    else
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    {
	if (!fcs->u.dyn.leaves)
	    leaves = malloc (sizeof (FcCharLeaf *));
	else
	    leaves = realloc (fcs->u.dyn.leaves, (fcs->num + 1) * sizeof (FcCharLeaf *));
	if (!leaves)
	    return FcFalse;
	if (fcs->num)
	    FcMemFree (FC_MEM_CHARSET, fcs->num * sizeof (FcCharLeaf *));
	FcMemAlloc (FC_MEM_CHARSET, (fcs->num + 1) * sizeof (FcCharLeaf *));
	fcs->u.dyn.leaves = leaves;
	if (!fcs->u.dyn.numbers)
	    numbers = malloc (sizeof (FcChar16));
	else
	    numbers = realloc (fcs->u.dyn.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->u.dyn.numbers = numbers;
    }
210
    
211
    memmove (fcs->u.dyn.leaves + pos + 1, fcs->u.dyn.leaves + pos, 
212
	     (fcs->num - pos) * sizeof (FcCharLeaf *));
213
    memmove (fcs->u.dyn.numbers + pos + 1, fcs->u.dyn.numbers + pos,
214
	     (fcs->num - pos) * sizeof (FcChar16));
215 216
    fcs->u.dyn.numbers[pos] = (FcChar16) ucs4;
    fcs->u.dyn.leaves[pos] = leaf;
217 218
    fcs->num++;
    return FcTrue;
Keith Packard's avatar
Keith Packard committed
219 220 221 222 223 224 225
}

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

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

232 233
    pos = FcCharSetFindLeafPos (fcs, ucs4);
    if (pos >= 0)
234
	return FcCharSetGetLeaf(fcs, pos);
235 236 237 238 239 240 241
    
    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
242
    {
243 244
	free (leaf);
	return 0;
Keith Packard's avatar
Keith Packard committed
245
    }
246
    FcMemAlloc (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
247 248 249 250 251 252 253 254 255 256
    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
257
    {
258
	FcMemFree (FC_MEM_CHARLEAF, sizeof (FcCharLeaf));
259
	if (fcs->bank == FC_BANK_DYNAMIC)
260 261 262 263 264 265
	{
	    free (fcs->u.dyn.leaves[pos]);
	    fcs->u.dyn.leaves[pos] = leaf;
	}
	else
	{
266
	    leaves[fcs->bank][leaf_idx[fcs->bank][fcs->u.stat.leafidx_offset]+pos] = *leaf;
267
	}
268
	return FcTrue;
Keith Packard's avatar
Keith Packard committed
269
    }
270 271
    pos = -pos - 1;
    return FcCharSetPutLeaf (fcs, ucs4, leaf, pos);
Keith Packard's avatar
Keith Packard committed
272 273 274 275 276 277 278 279
}

FcBool
FcCharSetAddChar (FcCharSet *fcs, FcChar32 ucs4)
{
    FcCharLeaf	*leaf;
    FcChar32	*b;
    
280
    if (fcs->ref == FC_REF_CONSTANT)
Keith Packard's avatar
Keith Packard committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
	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;
297
    int		    pos;
Keith Packard's avatar
Keith Packard committed
298 299 300
} FcCharSetIter;

/*
301
 * Set iter->leaf to the leaf containing iter->ucs4 or higher
Keith Packard's avatar
Keith Packard committed
302 303 304 305 306
 */

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

309
    if (pos < 0)
310
    {
311 312
	pos = -pos - 1;
	if (pos == fcs->num)
313
	{
314 315 316
	    iter->ucs4 = ~0;
	    iter->leaf = 0;
	    return;
317
	}
318
        iter->ucs4 = (FcChar32) FcCharSetGetNumbers(fcs)[pos] << 8;
319
    }
320
    iter->leaf = FcCharSetGetLeaf(fcs, pos);
321 322 323
    iter->pos = pos;
#ifdef CHATTY
    printf ("set %08x: %08x\n", iter->ucs4, (FcChar32) iter->leaf);
324
#endif
Keith Packard's avatar
Keith Packard committed
325 326 327 328 329
}

static void
FcCharSetIterNext (const FcCharSet *fcs, FcCharSetIter *iter)
{
330 331
    int	pos = iter->pos + 1;
    if (pos >= fcs->num)
332 333 334 335
    {
	iter->ucs4 = ~0;
	iter->leaf = 0;
    }
336
    else
337
    {
338 339
	iter->ucs4 = (FcChar32) FcCharSetGetNumbers(fcs)[pos] << 8;
	iter->leaf = FcCharSetGetLeaf(fcs, pos);
340
	iter->pos = pos;
341 342 343
    }
}

344
#ifdef CHATTY
345
static void
346
FcCharSetDump (const FcCharSet *fcs)
347
{
348 349 350 351
    int		pos;

    printf ("fcs %08x:\n", (FcChar32) fcs);
    for (pos = 0; pos < fcs->num; pos++)
352
    {
353 354 355 356
	FcCharLeaf	*leaf = fcs->leaves[pos];
	FcChar32	ucs4 = (FcChar32) fcs->numbers[pos] << 8;
	
	printf ("    %08x: %08x\n", ucs4, (FcChar32) leaf);
357
    }
Keith Packard's avatar
Keith Packard committed
358
}
359
#endif
Keith Packard's avatar
Keith Packard committed
360 361 362 363

static void
FcCharSetIterStart (const FcCharSet *fcs, FcCharSetIter *iter)
{
364 365
#ifdef CHATTY
    FcCharSetDump (fcs);
366
#endif
Keith Packard's avatar
Keith Packard committed
367
    iter->ucs4 = 0;
Patrick Lam's avatar
Patrick Lam committed
368
    iter->pos = 0;
Keith Packard's avatar
Keith Packard committed
369 370 371 372 373 374
    FcCharSetIterSet (fcs, iter);
}

FcCharSet *
FcCharSetCopy (FcCharSet *src)
{
375 376
    if (src->ref != FC_REF_CONSTANT)
	src->ref++;
Keith Packard's avatar
Keith Packard committed
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
    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);
430
    while ((ai.leaf || (bonly && bi.leaf)) && (bi.leaf || (aonly && ai.leaf)))
Keith Packard's avatar
Keith Packard committed
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 513 514 515 516 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 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
    {
	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);
}

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)
	    {
		FcChar32	*bm = bi.leaf->map;;
		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;
}

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

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

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

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

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

746 747 748
    return page;
}

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

758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
/*
 * old coverage API, rather hard to use correctly
 */
FcChar32
FcCharSetCoverage (const FcCharSet *a, FcChar32 page, FcChar32 *result);
    
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
785 786 787 788 789 790 791 792 793 794
/*
 * 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
 */

795
static const unsigned char	charToValue[256] = {
Keith Packard's avatar
Keith Packard committed
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 827 828 829
    /*     "" */ 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, 
};

830
static const FcChar8 valueToChar[0x55] = {
Keith Packard's avatar
Keith Packard committed
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
    /* 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;
849
    FcChar32	c;
Keith Packard's avatar
Keith Packard committed
850 851 852 853 854 855 856 857 858 859 860
    
    if (*string == ' ')
    {
	v = 0;
	string++;
    }
    else
    {
	v = 0;
	for (i = 0; i < 5; i++)
	{
861
	    if (!(c = (FcChar32) (unsigned char) *string++))
Keith Packard's avatar
Keith Packard committed
862 863 864 865 866 867 868 869 870 871 872 873
		return 0;
	    c = charToValue[c];
	    if (c == 0xff)
		return 0;
	    v = v * 85 + c;
	}
    }
    *value = v;
    return string;
}

static FcBool
874
FcCharSetUnparseValue (FcStrBuf *buf, FcChar32 value)
Keith Packard's avatar
Keith Packard committed
875 876 877 878
{
    int	    i;
    if (value == 0)
    {
879
	return FcStrBufChar (buf, ' ');
Keith Packard's avatar
Keith Packard committed
880 881 882 883 884 885 886 887 888 889 890 891
    }
    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++)
892
	    if (!FcStrBufChar (buf, *s++))
Keith Packard's avatar
Keith Packard committed
893 894 895 896 897
		return FcFalse;
    }
    return FcTrue;
}

898 899 900 901 902 903 904 905 906
typedef struct _FcCharLeafEnt FcCharLeafEnt;

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

#define FC_CHAR_LEAF_BLOCK	(4096 / sizeof (FcCharLeafEnt))
907 908
static FcCharLeafEnt **FcCharLeafBlocks;
static int FcCharLeafBlockCount;
909 910 911 912 913 914 915 916 917

static FcCharLeafEnt *
FcCharLeafEntCreate (void)
{
    static FcCharLeafEnt    *block;
    static int		    remain;

    if (!remain)
    {
918 919 920 921 922 923 924 925
	FcCharLeafEnt **newBlocks;

	FcCharLeafBlockCount++;
	newBlocks = realloc (FcCharLeafBlocks, FcCharLeafBlockCount * sizeof (FcCharLeafEnt *));
	if (!newBlocks)
	    return 0;
	FcCharLeafBlocks = newBlocks;
	block = FcCharLeafBlocks[FcCharLeafBlockCount-1] = malloc (FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
926 927
	if (!block)
	    return 0;
928
	FcMemAlloc (FC_MEM_CHARLEAF, FC_CHAR_LEAF_BLOCK * sizeof (FcCharLeafEnt));
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
	remain = FC_CHAR_LEAF_BLOCK;
    }
    remain--;
    return block++;
}

#define FC_CHAR_LEAF_HASH_SIZE	257

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 int	FcCharLeafTotal;
static int	FcCharLeafUsed;

951 952
static FcCharLeafEnt	*FcCharLeafHashTable[FC_CHAR_LEAF_HASH_SIZE];

953
static FcCharLeaf *
954
FcCharSetFreezeLeaf (FcCharLeaf *leaf)
955 956
{
    FcChar32			hash = FcCharLeafHash (leaf);
957
    FcCharLeafEnt		**bucket = &FcCharLeafHashTable[hash % FC_CHAR_LEAF_HASH_SIZE];
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
    FcCharLeafEnt		*ent;
    
    FcCharLeafTotal++;
    for (ent = *bucket; ent; ent = ent->next)
    {
	if (ent->hash == hash && !memcmp (&ent->leaf, leaf, sizeof (FcCharLeaf)))
	    return &ent->leaf;
    }

    ent = FcCharLeafEntCreate();
    if (!ent)
	return 0;
    FcCharLeafUsed++;
    ent->leaf = *leaf;
    ent->hash = hash;
    ent->next = *bucket;
    *bucket = ent;
    return &ent->leaf;
}

978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
static void
FcCharSetThawAllLeaf (void)
{
    int i;

    for (i = 0; i < FC_CHAR_LEAF_HASH_SIZE; i++)
	FcCharLeafHashTable[i] = 0;

    FcCharLeafTotal = 0;
    FcCharLeafUsed = 0;

    for (i = 0; i < FcCharLeafBlockCount; i++)
	free (FcCharLeafBlocks[i]);

    free (FcCharLeafBlocks);
    FcCharLeafBlocks = 0;
    FcCharLeafBlockCount = 0;
}

997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
typedef struct _FcCharSetEnt FcCharSetEnt;

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

#define FC_CHAR_SET_HASH_SIZE    67

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

    /* hash in leaves */
Patrick Lam's avatar
Patrick Lam committed
1014
    for (i = 0; i < fcs->num * (int) (sizeof (FcCharLeaf *) / sizeof (FcChar32)); i++)
1015
	hash = ((hash << 1) | (hash >> 31)) ^ (FcChar32)(FcCharSetGetLeaf(fcs, i)->map);
1016 1017
    /* hash in numbers */
    for (i = 0; i < fcs->num; i++)
1018
	hash = ((hash << 1) | (hash >> 31)) ^ *FcCharSetGetNumbers(fcs);
1019 1020 1021 1022 1023 1024 1025
    return hash;
}

static int	FcCharSetTotal;
static int	FcCharSetUsed;
static int	FcCharSetTotalEnts, FcCharSetUsedEnts;

1026 1027
static FcCharSetEnt	*FcCharSetHashTable[FC_CHAR_SET_HASH_SIZE];

1028
static FcCharSet *
1029
FcCharSetFreezeBase (FcCharSet *fcs)
1030 1031
{
    FcChar32		hash = FcCharSetHash (fcs);
1032
    FcCharSetEnt	**bucket = &FcCharSetHashTable[hash % FC_CHAR_SET_HASH_SIZE];
1033
    FcCharSetEnt	*ent;
1034
    int			size;
1035 1036 1037 1038 1039 1040 1041

    FcCharSetTotal++;
    FcCharSetTotalEnts += fcs->num;
    for (ent = *bucket; ent; ent = ent->next)
    {
	if (ent->hash == hash &&
	    ent->set.num == fcs->num &&
1042 1043
	    !memcmp (FcCharSetGetNumbers(&ent->set), 
		     FcCharSetGetNumbers(fcs),
1044 1045
		     fcs->num * sizeof (FcChar16)))
	{
1046 1047 1048 1049 1050 1051 1052 1053
	    FcBool ok = FcTrue;
	    int i;

	    for (i = 0; i < fcs->num; i++)
		if (FcCharSetGetLeaf(&ent->set, i) != FcCharSetGetLeaf(fcs, i))
		    ok = FcFalse;
	    if (ok)
		return &ent->set;
1054 1055 1056
	}
    }

1057 1058 1059 1060
    size = (sizeof (FcCharSetEnt) +
	    fcs->num * sizeof (FcCharLeaf *) +
	    fcs->num * sizeof (FcChar16));
    ent = malloc (size);
1061 1062
    if (!ent)
	return 0;
1063
    FcMemAlloc (FC_MEM_CHARSET, size);
1064 1065 1066
    FcCharSetUsed++;
    FcCharSetUsedEnts += fcs->num;
    
1067
    ent->set.ref = FC_REF_CONSTANT;
1068
    ent->set.num = fcs->num;
1069 1070
    ent->set.bank = fcs->bank;
    if (fcs->bank == FC_BANK_DYNAMIC)
1071
    {
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
	if (fcs->num)
	{
	    ent->set.u.dyn.leaves = (FcCharLeaf **) (ent + 1);
	    ent->set.u.dyn.numbers = (FcChar16 *) (ent->set.u.dyn.leaves + fcs->num);
	    memcpy (ent->set.u.dyn.leaves, fcs->u.dyn.leaves, fcs->num * sizeof (FcCharLeaf *));
	    memcpy (ent->set.u.dyn.numbers, fcs->u.dyn.numbers, fcs->num * sizeof (FcChar16));
	}
	else
	{
	    ent->set.u.dyn.leaves = 0;
	    ent->set.u.dyn.numbers = 0;
	}
1084 1085 1086
    }
    else
    {
1087 1088
	ent->set.u.stat.leafidx_offset = fcs->u.stat.leafidx_offset;
	ent->set.u.stat.numbers_offset = fcs->u.stat.numbers_offset;
1089
    }
1090 1091 1092 1093 1094 1095 1096

    ent->hash = hash;
    ent->next = *bucket;
    *bucket = ent;
    return &ent->set;
}

1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
void
FcCharSetThawAll (void)
{
    int i;
    FcCharSetEnt	*ent, *next;

    for (i = 0; i < FC_CHAR_SET_HASH_SIZE; i++)
    {
	for (ent = FcCharSetHashTable[i]; ent; ent = next)
	{
	    next = ent->next;
	    free (ent);
	}
	FcCharSetHashTable[i] = 0;
    }

    FcCharSetTotal = 0;
    FcCharSetTotalEnts = 0;
    FcCharSetUsed = 0;
    FcCharSetUsedEnts = 0;

    FcCharSetThawAllLeaf ();
}

1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
FcCharSet *
FcCharSetFreeze (FcCharSet *fcs)
{
    FcCharSet	*b;
    FcCharSet	*n = 0;
    FcCharLeaf	*l;
    int		i;

    b = FcCharSetCreate ();
    if (!b)
	goto bail0;
    for (i = 0; i < fcs->num; i++)
    {
1134
	l = FcCharSetFreezeLeaf (FcCharSetGetLeaf(fcs, i));
1135 1136
	if (!l)
	    goto bail1;
1137
	if (!FcCharSetInsertLeaf (b, FcCharSetGetNumbers(fcs)[i] << 8, l))
1138 1139 1140 1141
	    goto bail1;
    }
    n = FcCharSetFreezeBase (b);
bail1:
1142
    if (b->bank == FC_BANK_DYNAMIC)
1143
    {
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
	if (b->u.dyn.leaves)
	{
	    FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcCharLeaf *));
	    free (b->u.dyn.leaves);
	}
	if (b->u.dyn.numbers)
	{
	    FcMemFree (FC_MEM_CHARSET, b->num * sizeof (FcChar16));
	    free (b->u.dyn.numbers);
	}
1154 1155
    }
    FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
1156 1157 1158 1159 1160
    free (b);
bail0:
    return n;
}

Keith Packard's avatar
Keith Packard committed
1161 1162 1163
FcCharSet *
FcNameParseCharSet (FcChar8 *string)
{
1164
    FcCharSet	*c, *n = 0;
Keith Packard's avatar
Keith Packard committed
1165 1166
    FcChar32	ucs4;
    FcCharLeaf	*leaf;
1167 1168
    FcCharLeaf	temp;
    FcChar32	bits;
Keith Packard's avatar
Keith Packard committed
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
    int		i;

    c = FcCharSetCreate ();
    if (!c)
	goto bail0;
    while (*string)
    {
	string = FcCharSetParseValue (string, &ucs4);
	if (!string)
	    goto bail1;
1179
	bits = 0;
Keith Packard's avatar
Keith Packard committed
1180 1181
	for (i = 0; i < 256/32; i++)
	{
1182
	    string = FcCharSetParseValue (string, &temp.map[i]);
Keith Packard's avatar
Keith Packard committed
1183 1184
	    if (!string)
		goto bail1;
1185 1186 1187 1188
	    bits |= temp.map[i];
	}
	if (bits)
	{
1189
	    leaf = FcCharSetFreezeLeaf (&temp);
1190 1191 1192 1193
	    if (!leaf)
		goto bail1;
	    if (!FcCharSetInsertLeaf (c, ucs4, leaf))
		goto bail1;
Keith Packard's avatar
Keith Packard committed
1194 1195
	}
    }
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
#ifdef CHATTY
    printf ("          %8s %8s %8s %8s\n", "total", "totalmem", "new", "newmem");
    printf ("Leaves:   %8d %8d %8d %8d\n",
	    FcCharLeafTotal, sizeof (FcCharLeaf) * FcCharLeafTotal,
	    FcCharLeafUsed, sizeof (FcCharLeaf) * FcCharLeafUsed);
    printf ("Charsets: %8d %8d %8d %8d\n",
	    FcCharSetTotal, sizeof (FcCharSet) * FcCharSetTotal,
	    FcCharSetUsed, sizeof (FcCharSet) * FcCharSetUsed);
    printf ("Tables:   %8d %8d %8d %8d\n",
	    FcCharSetTotalEnts, FcCharSetTotalEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)),
	    FcCharSetUsedEnts, FcCharSetUsedEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)));
    printf ("Total:    %8s %8d %8s %8d\n", 
	    "", 
	    sizeof (FcCharLeaf) * FcCharLeafTotal +
	    sizeof (FcCharSet) * FcCharSetTotal +
	    FcCharSetTotalEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)),
	    "",
	    sizeof (FcCharLeaf) * FcCharLeafUsed +
	    sizeof (FcCharSet) * FcCharSetUsed +
	    FcCharSetUsedEnts * (sizeof (FcCharLeaf *) + sizeof (FcChar16)));
#endif
1217
    n = FcCharSetFreezeBase (c);
Keith Packard's avatar
Keith Packard committed
1218
bail1:
1219
    if (c->bank == FC_BANK_DYNAMIC)
1220
    {
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
	if (c->u.dyn.leaves)
	{
	    FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcCharLeaf *));
	    free (c->u.dyn.leaves);
	}
	if (c->u.dyn.numbers)
	{
	    FcMemFree (FC_MEM_CHARSET, c->num * sizeof (FcChar16));
	    free (c->u.dyn.numbers);
	}
	FcMemFree (FC_MEM_CHARSET, sizeof (FcCharSet));
1232
    }
1233
    free (c);
Keith Packard's avatar
Keith Packard committed
1234
bail0:
1235
    return n;
Keith Packard's avatar
Keith Packard committed
1236 1237 1238
}

FcBool
1239
FcNameUnparseCharSet (FcStrBuf *buf, const FcCharSet *c)
Keith Packard's avatar
Keith Packard committed
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
{
    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 */
1264
	FcStrBufChar (buf, '\0');
Keith Packard's avatar
Keith Packard committed
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
	/* 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;
}
1310

1311 1312
void
FcCharSetNewBank(void)
1313
{
1314 1315 1316 1317
    charset_count = 0;
    charset_numbers_count = 0;
    charset_leaf_count = 0;
    charset_leaf_idx_count = 0;
1318 1319
}

1320 1321
int
FcCharSetNeededBytes (const FcCharSet *c)
1322
{
1323
    /* yes, there's redundancy */
1324 1325 1326 1327
    charset_count++;
    charset_leaf_idx_count++;
    charset_leaf_count += c->num;
    charset_numbers_count += c->num;
1328 1329 1330 1331 1332 1333
    return sizeof (FcCharSet) + 
	sizeof (int) + 			/* leaf_idx */
	sizeof (FcCharLeaf) * c->num + 	/* leaf */
	sizeof (FcChar16) * c->num; 	/* number */
}

1334 1335 1336 1337 1338 1339 1340
int
FcCharSetNeededBytesAlign (void)
{
    return __alignof__ (FcCharSet) + __alignof__ (int) + 
	__alignof__ (FcCharLeaf) + __alignof__ (FcChar16);
}

1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
static FcBool
FcCharSetEnsureBank (int bi)
{
    if (!charsets || charset_bank_count <= bi)
    {
	int new_count = charset_bank_count + 2;
	FcCharSet ** cs;
	FcChar16 ** n;
	FcCharLeaf ** lvs;
	int ** lvi;
	int i;
	
	cs = realloc(charsets, sizeof(FcCharSet*) * new_count);
	if (!cs) return 0;
	n = realloc(numbers, sizeof(FcChar16*) * new_count);
	if (!n) return 0;
	lvs = realloc(leaves, sizeof(FcCharLeaf*) * new_count);
	if (!lvs) return 0;
	lvi = realloc(leaf_idx, sizeof(int*) * new_count);
	if (!lvi) return 0;

	charsets = cs; numbers = n; leaves = lvs; leaf_idx = lvi;
	for (i = charset_bank_count; i < new_count; i++)
	{
	    charsets[i] = 0;
	    numbers[i] = 0;
	    leaves[i] = 0;
	    leaf_idx[i] = 0;
	}
	charset_bank_count = new_count;
    }
1372 1373 1374
    return FcTrue;
}

1375 1376 1377 1378 1379 1380 1381
void *
FcCharSetDistributeBytes (FcCache * metadata, void * block_ptr)
{
    int bi = FcCacheBankToIndex(metadata->bank);
    if (!FcCharSetEnsureBank(bi))
	return 0;

1382
    block_ptr = ALIGN (block_ptr, FcCharSet);
1383
    charsets[bi] = (FcCharSet *)block_ptr;
1384 1385
    block_ptr = (void *)((char *)block_ptr + 
		     (sizeof (FcCharSet) * charset_count));
1386
    block_ptr = ALIGN (block_ptr, FcChar16);
1387
    numbers[bi] = (FcChar16 *)block_ptr;
1388 1389
    block_ptr = (void *)((char *)block_ptr + 
		     (sizeof(FcChar16) * charset_numbers_count));
1390
    block_ptr = ALIGN (block_ptr, FcCharLeaf);
1391
    leaves[bi] = (FcCharLeaf *)block_ptr;
1392 1393
    block_ptr = (void *)((char *)block_ptr +
		     (sizeof(FcCharLeaf) * charset_leaf_count));
1394
    block_ptr = ALIGN (block_ptr, int);
1395
    leaf_idx[bi] = (int *)block_ptr;
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
    block_ptr = (void *)((char *)block_ptr +
		     (sizeof(int) * charset_leaf_idx_count));

    metadata->charset_count = charset_count;
    metadata->charset_numbers_count = charset_numbers_count;
    metadata->charset_leaf_count = charset_leaf_count;
    metadata->charset_leaf_idx_count = charset_leaf_idx_count;
    charset_ptr = 0; charset_leaf_ptr = 0;
    charset_leaf_idx_ptr = 0; charset_numbers_ptr = 0;
    return block_ptr;
}

FcCharSet *
FcCharSetSerialize(int bank, FcCharSet *c)
1410 1411 1412
{
    int i;
    FcCharSet new;
1413
    int bi = FcCacheBankToIndex(bank), cp = charset_ptr;
1414

1415
    new.ref = FC_REF_CONSTANT;
1416
    new.bank = bank;
1417
    new.u.stat.leafidx_offset = charset_leaf_idx_ptr;
1418
    new.u.stat.numbers_offset = charset_numbers_ptr;
1419
    new.num = c->num;
1420

1421
    charsets[bi][charset_ptr++] = new;
1422

1423
    leaf_idx[bi][charset_leaf_idx_ptr++] = charset_leaf_ptr;
1424 1425
    for (i = 0; i < c->num; i++)
    {
1426
	memcpy (&leaves[bi][charset_leaf_ptr++], 
1427
		c->u.dyn.leaves[i], sizeof(FcCharLeaf));
1428
	numbers[bi][charset_numbers_ptr++] = c->u.dyn.numbers[i];
1429 1430
    }

1431
    return &charsets[bi][cp];
1432 1433
}

1434
void *
1435
FcCharSetUnserialize (FcCache *metadata, void *block_ptr)
1436
{
1437
    int bi = FcCacheBankToIndex(metadata->bank);
1438 1439
    if (!FcCharSetEnsureBank(bi))
	return 0;
1440

1441
    block_ptr = ALIGN (block_ptr, FcCharSet);
1442 1443
    charsets[bi] = (FcCharSet *)block_ptr;
    block_ptr = (void *)((char *)block_ptr + 
1444
		     (sizeof (FcCharSet) * metadata->charset_count));
1445
    block_ptr = ALIGN (block_ptr, FcChar16);
1446 1447
    numbers[bi] = (FcChar16 *)block_ptr;
    block_ptr = (void *)((char *)block_ptr + 
1448
		     (sizeof(FcChar16) * metadata->charset_numbers_count));
1449
    block_ptr = ALIGN (block_ptr, FcCharLeaf);
1450 1451
    leaves[bi] = (FcCharLeaf *)block_ptr;
    block_ptr = (void *)((char *)block_ptr +
1452
		     (sizeof(FcCharLeaf) * metadata->charset_leaf_count));
1453
    block_ptr = ALIGN (block_ptr, int);
1454 1455
    leaf_idx[bi] = (int *)block_ptr;
    block_ptr = (void *)((char *)block_ptr +
1456
		     (sizeof(int) * metadata->charset_leaf_idx_count));
1457 1458

    return block_ptr;
1459 1460
}

1461 1462 1463
FcCharLeaf *
FcCharSetGetLeaf(const FcCharSet *c, int i)
{
1464 1465
    int bi;
    if (c->bank == FC_BANK_DYNAMIC)
1466
	return c->u.dyn.leaves[i];
1467 1468 1469
    bi = FcCacheBankToIndex(c->bank);

    return &leaves[bi][leaf_idx[bi][c->u.stat.leafidx_offset]+i];
1470 1471 1472 1473 1474
}

FcChar16 *
FcCharSetGetNumbers(const FcCharSet *c)
{
1475 1476
    int bi;
    if (c->bank == FC_BANK_DYNAMIC)
1477
	return c->u.dyn.numbers;
1478 1479 1480
    bi = FcCacheBankToIndex(c->bank);

    return &numbers[bi][c->u.stat.numbers_offset];
1481 1482
}