summaryrefslogtreecommitdiff
path: root/render/glyph.c
blob: 6d09a0e521497140df99958f7518f87965d4c4f8 (plain)
1
2
3
4
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
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
367
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
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
/*
 *
 * Copyright © 2000 SuSE, Inc.
 *
 * 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 SuSE not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  SuSE makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
 * 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.
 *
 * Author:  Keith Packard, SuSE, Inc.
 */

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include "misc.h"
#include "scrnintstr.h"
#include "os.h"
#include "regionstr.h"
#include "validate.h"
#include "windowstr.h"
#include "input.h"
#include "resource.h"
#include "colormapst.h"
#include "cursorstr.h"
#include "dixstruct.h"
#include "gcstruct.h"
#include "servermd.h"
#include "picturestr.h"
#include "glyphstr.h"

/*
 * From Knuth -- a good choice for hash/rehash values is p, p-2 where
 * p and p-2 are both prime.  These tables are sized to have an extra 10%
 * free to avoid exponential performance degradation as the hash table fills
 */
static GlyphHashSetRec glyphHashSets[] = {
    { 32,		43,		41        },
    { 64,		73,		71        },
    { 128,		151,		149       },
    { 256,		283,		281       },
    { 512,		571,		569       },
    { 1024,		1153,		1151      },
    { 2048,		2269,		2267      },
    { 4096,		4519,		4517      },
    { 8192,		9013,		9011      },
    { 16384,		18043,		18041     },
    { 32768,		36109,		36107     },
    { 65536,		72091,		72089     },
    { 131072,		144409,		144407    },
    { 262144,		288361,		288359    },
    { 524288,		576883,		576881    },
    { 1048576,		1153459,	1153457   },
    { 2097152,		2307163,	2307161   },
    { 4194304,		4613893,	4613891   },
    { 8388608,		9227641,	9227639   },
    { 16777216,		18455029,	18455027  },
    { 33554432,		36911011,	36911009  },
    { 67108864,		73819861,	73819859  },
    { 134217728,	147639589,	147639587 },
    { 268435456,	295279081,	295279079 },
    { 536870912,	590559793,	590559791 }
};

#define NGLYPHHASHSETS	(sizeof(glyphHashSets)/sizeof(glyphHashSets[0]))

const CARD8	glyphDepths[GlyphFormatNum] = { 1, 4, 8, 16, 32 };

GlyphHashRec	globalGlyphs[GlyphFormatNum];

int		globalTotalGlyphPrivateSize = 0;

static int	glyphPrivateCount = 0;

void
ResetGlyphPrivates ()
{
    glyphPrivateCount = 0;
}

int
AllocateGlyphPrivateIndex ()
{
    return glyphPrivateCount++;
}

Bool
AllocateGlyphPrivate (ScreenPtr pScreen,
		      int	index2,
		      unsigned	amount)
{
    PictureScreenPtr ps;
    unsigned	     oldamount;

    ps = GetPictureScreenIfSet (pScreen);
    if (!ps)
	return FALSE;

    /* Round up sizes for proper alignment */
    amount = ((amount + (sizeof (DevUnion) - 1)) / sizeof (DevUnion)) *
	sizeof (DevUnion);

    if (index2 >= ps->glyphPrivateLen)
    {
	unsigned *nsizes;
	nsizes = (unsigned *) xrealloc (ps->glyphPrivateSizes,
					(index2 + 1) * sizeof (unsigned));
	if (!nsizes)
	    return FALSE;
	
	while (ps->glyphPrivateLen <= index2)
	{
	    nsizes[ps->glyphPrivateLen++] = 0;
	    ps->totalGlyphPrivateSize += sizeof (DevUnion);
	}
	ps->glyphPrivateSizes = nsizes;
    }
    oldamount = ps->glyphPrivateSizes[index2];
    if (amount > oldamount)
    {
	ps->glyphPrivateSizes[index2] = amount;
	ps->totalGlyphPrivateSize += (amount - oldamount);
    }
    ps->totalGlyphPrivateSize = BitmapBytePad (ps->totalGlyphPrivateSize * 8);
    
    return TRUE;
}

static void
SetGlyphScreenPrivateOffsets (void)
{
    PictureScreenPtr ps;
    int		     offset = 0;
    int		     i;

    for (i = 0; i < screenInfo.numScreens; i++)
    {
	ps = GetPictureScreenIfSet (screenInfo.screens[i]);
	if (ps && ps->totalGlyphPrivateSize)
	{
	    ps->glyphPrivateOffset = offset;
	    offset += ps->totalGlyphPrivateSize / sizeof (DevUnion);
	}
    }
}

static void
SetGlyphPrivatePointers (GlyphPtr glyph)
{
    PictureScreenPtr ps;
    int		     i;
    char	     *ptr;
    DevUnion         *ppriv;
    unsigned         *sizes;
    unsigned         size;
    int		     len;

    for (i = 0; i < screenInfo.numScreens; i++)
    {
	ps = GetPictureScreenIfSet (screenInfo.screens[i]);
	if (ps && ps->totalGlyphPrivateSize)
	{
	    ppriv = glyph->devPrivates + ps->glyphPrivateOffset;
	    sizes = ps->glyphPrivateSizes;
	    ptr = (char *) (ppriv + ps->glyphPrivateLen);
	    for (len = ps->glyphPrivateLen; --len >= 0; ppriv++, sizes++)
	    {
		if ((size = *sizes) != 0)
		{
		    ppriv->ptr = (pointer) ptr;
		    ptr += size;
		}
		else
		    ppriv->ptr = (pointer) 0;
	    }
	}
    }
}

static Bool
ReallocGlobalGlyphPrivate (GlyphPtr glyph)
{
    PictureScreenPtr ps;
    DevUnion         *devPrivates;
    char	     *ptr;
    int		     i;

    devPrivates = xalloc (globalTotalGlyphPrivateSize);
    if (!devPrivates)
	return FALSE;

    ptr = (char *) devPrivates;
    for (i = 0; i < screenInfo.numScreens; i++)
    {
	ps = GetPictureScreenIfSet (screenInfo.screens[i]);
	if (ps && ps->totalGlyphPrivateSize)
	{
	    if (ps->glyphPrivateOffset != -1)
	    {
		memcpy (ptr, glyph->devPrivates + ps->glyphPrivateOffset,
			ps->totalGlyphPrivateSize);
	    }
	    else if (ps->totalGlyphPrivateSize)
	    {
		memset (ptr, 0, ps->totalGlyphPrivateSize);
	    }
	    
	    ptr += ps->totalGlyphPrivateSize;
	}
    }

    if (glyph->devPrivates)
	xfree (glyph->devPrivates);
    
    glyph->devPrivates = devPrivates;

    return TRUE;
}

Bool
GlyphInit (ScreenPtr pScreen)
{
    PictureScreenPtr ps = GetPictureScreen (pScreen);
    
    ps->totalGlyphPrivateSize = 0;
    ps->glyphPrivateSizes = 0;
    ps->glyphPrivateLen = 0;
    ps->glyphPrivateOffset = -1;
    
    return TRUE;
}

Bool
GlyphFinishInit (ScreenPtr pScreen)
{
    PictureScreenPtr ps = GetPictureScreen (pScreen);

    if (ps->totalGlyphPrivateSize)
    {
	GlyphPtr glyph;
	int	 fdepth, i;
	
	globalTotalGlyphPrivateSize += ps->totalGlyphPrivateSize;
	
	for (fdepth = 0; fdepth < GlyphFormatNum; fdepth++)
	{
	    if (!globalGlyphs[fdepth].hashSet)
		continue;
		
	    for (i = 0; i < globalGlyphs[fdepth].hashSet->size; i++)
	    {
		glyph = globalGlyphs[fdepth].table[i].glyph;
		if (glyph && glyph != DeletedGlyph)
		{
		    if (!ReallocGlobalGlyphPrivate (glyph))
			return FALSE;
		}
	    }
	}

	SetGlyphScreenPrivateOffsets ();

	for (fdepth = 0; fdepth < GlyphFormatNum; fdepth++)
	{
	    if (!globalGlyphs[fdepth].hashSet)
		continue;
		
	    for (i = 0; i < globalGlyphs[fdepth].hashSet->size; i++)
	    {
		glyph = globalGlyphs[fdepth].table[i].glyph;
		if (glyph && glyph != DeletedGlyph)
		{
		    SetGlyphPrivatePointers (glyph);
			
		    if (!(*ps->RealizeGlyph) (pScreen, glyph))
			return FALSE;
		}
	    }
	}
    }
    else
	ps->glyphPrivateOffset = 0;
    
    return TRUE;
}

void
GlyphUninit (ScreenPtr pScreen)
{
    PictureScreenPtr ps = GetPictureScreen (pScreen);
    GlyphPtr	     glyph;
    int		     fdepth, i;

    globalTotalGlyphPrivateSize -= ps->totalGlyphPrivateSize;

    for (fdepth = 0; fdepth < GlyphFormatNum; fdepth++)
    {
	if (!globalGlyphs[fdepth].hashSet)
	    continue;
	
	for (i = 0; i < globalGlyphs[fdepth].hashSet->size; i++)
	{
	    glyph = globalGlyphs[fdepth].table[i].glyph;
	    if (glyph && glyph != DeletedGlyph)
	    {
		(*ps->UnrealizeGlyph) (pScreen, glyph);
		
		if (globalTotalGlyphPrivateSize)
		{
		    if (!ReallocGlobalGlyphPrivate (glyph))
			return;
		}
		else
		{
		    if (glyph->devPrivates)
			xfree (glyph->devPrivates);
		    glyph->devPrivates = NULL;
		}
	    }
	}
    }

    if (globalTotalGlyphPrivateSize)
	SetGlyphScreenPrivateOffsets ();

    for (fdepth = 0; fdepth < GlyphFormatNum; fdepth++)
    {
	if (!globalGlyphs[fdepth].hashSet)
	    continue;
	
	for (i = 0; i < globalGlyphs[fdepth].hashSet->size; i++)
	{
	    glyph = globalGlyphs[fdepth].table[i].glyph;    
	    if (glyph && glyph != DeletedGlyph)
	    {
		if (globalTotalGlyphPrivateSize)
		    SetGlyphPrivatePointers (glyph);
	    }
	}
    }

    if (ps->glyphPrivateSizes)
	xfree (ps->glyphPrivateSizes);
}

GlyphHashSetPtr
FindGlyphHashSet (CARD32 filled)
{
    int	i;

    for (i = 0; i < NGLYPHHASHSETS; i++)
	if (glyphHashSets[i].entries >= filled)
	    return &glyphHashSets[i];
    return 0;
}

static int _GlyphSetPrivateAllocateIndex = 0;

int
AllocateGlyphSetPrivateIndex (void)
{
    return _GlyphSetPrivateAllocateIndex++;
}

void
ResetGlyphSetPrivateIndex (void)
{
    _GlyphSetPrivateAllocateIndex = 0;
}

Bool
_GlyphSetSetNewPrivate (GlyphSetPtr glyphSet, int n, pointer ptr)
{
    pointer *new;

    if (n > glyphSet->maxPrivate) {
	if (glyphSet->devPrivates &&
	    glyphSet->devPrivates != (pointer)(&glyphSet[1])) {
	    new = (pointer *) xrealloc (glyphSet->devPrivates,
					(n + 1) * sizeof (pointer));
	    if (!new)
		return FALSE;
	} else {
	    new = (pointer *) xalloc ((n + 1) * sizeof (pointer));
	    if (!new)
		return FALSE;
	    if (glyphSet->devPrivates)
		memcpy (new,
			glyphSet->devPrivates,
			(glyphSet->maxPrivate + 1) * sizeof (pointer));
	}
	glyphSet->devPrivates = new;
	/* Zero out new, uninitialize privates */
	while (++glyphSet->maxPrivate < n)
	    glyphSet->devPrivates[glyphSet->maxPrivate] = (pointer)0;
    }
    glyphSet->devPrivates[n] = ptr;
    return TRUE;
}

GlyphRefPtr
FindGlyphRef (GlyphHashPtr hash, CARD32 signature, Bool match, GlyphPtr compare)
{
    CARD32	elt, step, s;
    GlyphPtr	glyph;
    GlyphRefPtr	table, gr, del;
    CARD32	tableSize = hash->hashSet->size;

    table = hash->table;
    elt = signature % tableSize;
    step = 0;
    del = 0;
    for (;;)
    {
	gr = &table[elt];
	s = gr->signature;
	glyph = gr->glyph;
	if (!glyph)
	{
	    if (del)
		gr = del;
	    break;
	}
	if (glyph == DeletedGlyph)
	{
	    if (!del)
		del = gr;
	    else if (gr == del)
		break;
	}
	else if (s == signature &&
		 (!match || 
		  memcmp (&compare->info, &glyph->info, compare->size) == 0))
	{
	    break;
	}
	if (!step)
	{
	    step = signature % hash->hashSet->rehash;
	    if (!step)
		step = 1;
	}
	elt += step;
	if (elt >= tableSize)
	    elt -= tableSize;
    }
    return gr;
}

CARD32
HashGlyph (GlyphPtr glyph)
{
    CARD32  *bits = (CARD32 *) &(glyph->info);
    CARD32  hash;
    int	    n = glyph->size / sizeof (CARD32);

    hash = 0;
    while (n--)
	hash ^= *bits++;
    return hash;
}

#ifdef CHECK_DUPLICATES
void
DuplicateRef (GlyphPtr glyph, char *where)
{
    ErrorF ("Duplicate Glyph 0x%x from %s\n", glyph, where);
}

void
CheckDuplicates (GlyphHashPtr hash, char *where)
{
    GlyphPtr	g;
    int		i, j;

    for (i = 0; i < hash->hashSet->size; i++)
    {
	g = hash->table[i].glyph;
	if (!g || g == DeletedGlyph)
	    continue;
	for (j = i + 1; j < hash->hashSet->size; j++)
	    if (hash->table[j].glyph == g)
		DuplicateRef (g, where);
    }
}
#else
#define CheckDuplicates(a,b)
#define DuplicateRef(a,b)
#endif

void
FreeGlyph (GlyphPtr glyph, int format)
{
    CheckDuplicates (&globalGlyphs[format], "FreeGlyph");
    if (--glyph->refcnt == 0)
    {
	PictureScreenPtr ps;
	GlyphRefPtr      gr;
	int	         i;
	int	         first;

	first = -1;
	for (i = 0; i < globalGlyphs[format].hashSet->size; i++)
	    if (globalGlyphs[format].table[i].glyph == glyph)
	    {
		if (first != -1)
		    DuplicateRef (glyph, "FreeGlyph check");
		first = i;
	    }

	gr = FindGlyphRef (&globalGlyphs[format],
			   HashGlyph (glyph), TRUE, glyph);
	if (gr - globalGlyphs[format].table != first)
	    DuplicateRef (glyph, "Found wrong one");
	if (gr->glyph && gr->glyph != DeletedGlyph)
	{
	    gr->glyph = DeletedGlyph;
	    gr->signature = 0;
	    globalGlyphs[format].tableEntries--;
	}

	for (i = 0; i < screenInfo.numScreens; i++)
	{
	    ps = GetPictureScreenIfSet (screenInfo.screens[i]);
	    if (ps)
		(*ps->UnrealizeGlyph) (screenInfo.screens[i], glyph);
	}
	
	if (glyph->devPrivates)
	    xfree (glyph->devPrivates);
	xfree (glyph);
    }
}

void
AddGlyph (GlyphSetPtr glyphSet, GlyphPtr glyph, Glyph id)
{
    GlyphRefPtr	    gr;
    CARD32	    hash;

    CheckDuplicates (&globalGlyphs[glyphSet->fdepth], "AddGlyph top global");
    /* Locate existing matching glyph */
    hash = HashGlyph (glyph);
    gr = FindGlyphRef (&globalGlyphs[glyphSet->fdepth], hash, TRUE, glyph);
    if (gr->glyph && gr->glyph != DeletedGlyph)
    {
	PictureScreenPtr ps;
	int              i;
	
	for (i = 0; i < screenInfo.numScreens; i++)
	{
	    ps = GetPictureScreenIfSet (screenInfo.screens[i]);
	    if (ps)
		(*ps->UnrealizeGlyph) (screenInfo.screens[i], glyph);
	}
	if (glyph->devPrivates)
	    xfree (glyph->devPrivates);
	xfree (glyph);
	glyph = gr->glyph;
    }
    else
    {
	gr->glyph = glyph;
	gr->signature = hash;
	globalGlyphs[glyphSet->fdepth].tableEntries++;
    }
    
    /* Insert/replace glyphset value */
    gr = FindGlyphRef (&glyphSet->hash, id, FALSE, 0);
    ++glyph->refcnt;
    if (gr->glyph && gr->glyph != DeletedGlyph)
	FreeGlyph (gr->glyph, glyphSet->fdepth);
    else
	glyphSet->hash.tableEntries++;
    gr->glyph = glyph;
    gr->signature = id;
    CheckDuplicates (&globalGlyphs[glyphSet->fdepth], "AddGlyph bottom");
}

Bool
DeleteGlyph (GlyphSetPtr glyphSet, Glyph id)
{
    GlyphRefPtr     gr;
    GlyphPtr	    glyph;

    gr = FindGlyphRef (&glyphSet->hash, id, FALSE, 0);
    glyph = gr->glyph;
    if (glyph && glyph != DeletedGlyph)
    {
	gr->glyph = DeletedGlyph;
	glyphSet->hash.tableEntries--;
	FreeGlyph (glyph, glyphSet->fdepth);
	return TRUE;
    }
    return FALSE;
}

GlyphPtr
FindGlyph (GlyphSetPtr glyphSet, Glyph id)
{
    GlyphPtr        glyph;

    glyph = FindGlyphRef (&glyphSet->hash, id, FALSE, 0)->glyph;
    if (glyph == DeletedGlyph)
	glyph = 0;
    return glyph;
}

GlyphPtr
AllocateGlyph (xGlyphInfo *gi, int fdepth)
{
    PictureScreenPtr ps;
    int		     size;
    GlyphPtr	     glyph;
    int		     i;

    size = gi->height * PixmapBytePad (gi->width, glyphDepths[fdepth]);
    glyph = (GlyphPtr) xalloc (size + sizeof (GlyphRec));
    if (!glyph)
	return 0;
    glyph->refcnt = 0;
    glyph->size = size + sizeof (xGlyphInfo);
    glyph->info = *gi;

    if (globalTotalGlyphPrivateSize)
    {
	glyph->devPrivates = xalloc (globalTotalGlyphPrivateSize);
	if (!glyph->devPrivates)
	    return 0;

	SetGlyphPrivatePointers (glyph);
    } else
	glyph->devPrivates = NULL;

    for (i = 0; i < screenInfo.numScreens; i++)
    {
	ps = GetPictureScreenIfSet (screenInfo.screens[i]);
	if (ps)
	{
	    if (!(*ps->RealizeGlyph) (screenInfo.screens[i], glyph))
	    {
		while (i--)
		{
		    ps = GetPictureScreenIfSet (screenInfo.screens[i]);
		    if (ps)
			(*ps->UnrealizeGlyph) (screenInfo.screens[i], glyph);
		}
		
		if (glyph->devPrivates)
		    xfree (glyph->devPrivates);
		xfree (glyph);
		return 0;
	    }
	}
    }
    
    return glyph;
}
    
Bool
AllocateGlyphHash (GlyphHashPtr hash, GlyphHashSetPtr hashSet)
{
    hash->table = (GlyphRefPtr) xalloc (hashSet->size * sizeof (GlyphRefRec));
    if (!hash->table)
	return FALSE;
    memset (hash->table, 0, hashSet->size * sizeof (GlyphRefRec));
    hash->hashSet = hashSet;
    hash->tableEntries = 0;
    return TRUE;
}

Bool
ResizeGlyphHash (GlyphHashPtr hash, CARD32 change, Bool global)
{
    CARD32	    tableEntries;
    GlyphHashSetPtr hashSet;
    GlyphHashRec    newHash;
    GlyphRefPtr	    gr;
    GlyphPtr	    glyph;
    int		    i;
    int		    oldSize;
    CARD32	    s;

    tableEntries = hash->tableEntries + change;
    hashSet = FindGlyphHashSet (tableEntries);
    if (hashSet == hash->hashSet)
	return TRUE;
    if (global)
	CheckDuplicates (hash, "ResizeGlyphHash top");
    if (!AllocateGlyphHash (&newHash, hashSet))
	return FALSE;
    if (hash->table)
    {
	oldSize = hash->hashSet->size;
	for (i = 0; i < oldSize; i++)
	{
	    glyph = hash->table[i].glyph;
	    if (glyph && glyph != DeletedGlyph)
	    {
		s = hash->table[i].signature;
		gr = FindGlyphRef (&newHash, s, global, glyph);
		gr->signature = s;
		gr->glyph = glyph;
		++newHash.tableEntries;
	    }
	}
	xfree (hash->table);
    }
    *hash = newHash;
    if (global)
	CheckDuplicates (hash, "ResizeGlyphHash bottom");
    return TRUE;
}

Bool
ResizeGlyphSet (GlyphSetPtr glyphSet, CARD32 change)
{
    return (ResizeGlyphHash (&glyphSet->hash, change, FALSE) &&
	    ResizeGlyphHash (&globalGlyphs[glyphSet->fdepth], change, TRUE));
}
			    
GlyphSetPtr
AllocateGlyphSet (int fdepth, PictFormatPtr format)
{
    GlyphSetPtr	glyphSet;
    int size;
    
    if (!globalGlyphs[fdepth].hashSet)
    {
	if (!AllocateGlyphHash (&globalGlyphs[fdepth], &glyphHashSets[0]))
	    return FALSE;
    }

    size = (sizeof (GlyphSetRec) +
	    (sizeof (pointer) * _GlyphSetPrivateAllocateIndex));
    glyphSet = xalloc (size);
    if (!glyphSet)
	return FALSE;
    bzero((char *)glyphSet, size);
    glyphSet->maxPrivate = _GlyphSetPrivateAllocateIndex - 1;
    if (_GlyphSetPrivateAllocateIndex)
	glyphSet->devPrivates = (pointer)(&glyphSet[1]);

    if (!AllocateGlyphHash (&glyphSet->hash, &glyphHashSets[0]))
    {
	xfree (glyphSet);
	return FALSE;
    }
    glyphSet->refcnt = 1;
    glyphSet->fdepth = fdepth;
    glyphSet->format = format;
    return glyphSet;	
}

int
FreeGlyphSet (pointer	value,
	      XID       gid)
{
    GlyphSetPtr	glyphSet = (GlyphSetPtr) value;
    
    if (--glyphSet->refcnt == 0)
    {
	CARD32	    i, tableSize = glyphSet->hash.hashSet->size;
	GlyphRefPtr table = glyphSet->hash.table;
	GlyphPtr    glyph;
    
	for (i = 0; i < tableSize; i++)
	{
	    glyph = table[i].glyph;
	    if (glyph && glyph != DeletedGlyph)
		FreeGlyph (glyph, glyphSet->fdepth);
	}
	if (!globalGlyphs[glyphSet->fdepth].tableEntries)
	{
	    xfree (globalGlyphs[glyphSet->fdepth].table);
	    globalGlyphs[glyphSet->fdepth].table = 0;
	    globalGlyphs[glyphSet->fdepth].hashSet = 0;
	}
	else
	    ResizeGlyphHash (&globalGlyphs[glyphSet->fdepth], 0, TRUE);
	xfree (table);

	if (glyphSet->devPrivates &&
	    glyphSet->devPrivates != (pointer)(&glyphSet[1]))
	    xfree(glyphSet->devPrivates);

	xfree (glyphSet);
    }
    return Success;
}