aboutsummaryrefslogtreecommitdiff
path: root/libXft/src/xftdraw.c
blob: dfa97fbfc8621657f7ef2445dd6d3158f7e4a6b6 (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
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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
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
/*
 * Copyright © 2000 Keith Packard
 *
 * 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 "xftint.h"

/*
 * Ok, this is a pain.  To share source pictures across multiple destinations,
 * the screen for each drawable must be discovered.
 */

static int
_XftDrawScreen (Display *dpy, Drawable drawable, Visual *visual)
{
    int		    s;
    Window	    root;
    int		    x, y;
    unsigned int    width, height, borderWidth, depth;
    /* Special case the most common environment */
    if (ScreenCount (dpy) == 1)
	return 0;
    /*
     * If we've got a visual, look for the screen that points at it.
     * This requires no round trip.
     */
    if (visual)
    {
	for (s = 0; s < ScreenCount (dpy); s++)
	{
	    XVisualInfo	template, *ret;
	    int		nret;

	    template.visualid = visual->visualid;
	    template.screen = s;
	    ret = XGetVisualInfo (dpy, VisualIDMask|VisualScreenMask,
				  &template, &nret);
	    if (ret)
	    {
		XFree (ret);
		return s;
	    }
	}
    }
    /*
     * Otherwise, as the server for the drawable geometry and find
     * the screen from the root window.
     * This takes a round trip.
     */
    if (XGetGeometry (dpy, drawable, &root, &x, &y, &width, &height,
		      &borderWidth, &depth))
    {
	for (s = 0; s < ScreenCount (dpy); s++)
	{
	    if (RootWindow (dpy, s) == root)
		return s;
	}
    }
    /*
     * Make a guess -- it's probably wrong, but then the app probably
     * handed us a bogus drawable in this case
     */
    return 0;
}

_X_HIDDEN unsigned int
XftDrawDepth (XftDraw *draw)
{
    if (!draw->depth)
    {
	Window		    root;
	int		    x, y;
	unsigned int	    width, height, borderWidth, depth;
	if (XGetGeometry (draw->dpy, draw->drawable,
			  &root, &x, &y, &width, &height,
			  &borderWidth, &depth))
	    draw->depth = depth;
    }
    return draw->depth;
}

_X_HIDDEN unsigned int
XftDrawBitsPerPixel (XftDraw	*draw)
{
    if (!draw->bits_per_pixel)
    {
	XPixmapFormatValues *formats;
	int		    nformats;
	unsigned int	    depth;

	if ((depth = XftDrawDepth (draw)) &&
	    (formats = XListPixmapFormats (draw->dpy, &nformats)))
	{
	    int	i;

	    for (i = 0; i < nformats; i++)
	    {
		if (formats[i].depth == depth)
		{
		    draw->bits_per_pixel = formats[i].bits_per_pixel;
		    break;
		}
	    }
	    XFree (formats);
	}
    }
    return draw->bits_per_pixel;
}

_X_EXPORT XftDraw *
XftDrawCreate (Display   *dpy,
	       Drawable  drawable,
	       Visual    *visual,
	       Colormap  colormap)
{
    XftDraw	*draw;

    draw = (XftDraw *) malloc (sizeof (XftDraw));
    if (!draw)
	return NULL;

    draw->dpy = dpy;
    draw->drawable = drawable;
    draw->screen = _XftDrawScreen (dpy, drawable, visual);
    draw->depth = 0;		/* don't find out unless we need to know */
    draw->bits_per_pixel = 0;	/* don't find out unless we need to know */
    draw->visual = visual;
    draw->colormap = colormap;
    draw->render.pict = 0;
    draw->core.gc = NULL;
    draw->core.use_pixmap = 0;
    draw->clip_type = XftClipTypeNone;
    draw->subwindow_mode = ClipByChildren;
    XftMemAlloc (XFT_MEM_DRAW, sizeof (XftDraw));
    return draw;
}

_X_EXPORT XftDraw *
XftDrawCreateBitmap (Display	*dpy,
		     Pixmap	bitmap)
{
    XftDraw	*draw;

    draw = (XftDraw *) malloc (sizeof (XftDraw));
    if (!draw)
	return NULL;
    draw->dpy = dpy;
    draw->drawable = (Drawable) bitmap;
    draw->screen = _XftDrawScreen (dpy, bitmap, NULL);
    draw->depth = 1;
    draw->bits_per_pixel = 1;
    draw->visual = NULL;
    draw->colormap = 0;
    draw->render.pict = 0;
    draw->core.gc = NULL;
    draw->core.use_pixmap = 0;
    draw->clip_type = XftClipTypeNone;
    draw->subwindow_mode = ClipByChildren;
    XftMemAlloc (XFT_MEM_DRAW, sizeof (XftDraw));
    return draw;
}

_X_EXPORT XftDraw *
XftDrawCreateAlpha (Display *dpy,
		    Pixmap  pixmap,
		    int	    depth)
{
    XftDraw	*draw;

    draw = (XftDraw *) malloc (sizeof (XftDraw));
    if (!draw)
	return NULL;
    draw->dpy = dpy;
    draw->drawable = (Drawable) pixmap;
    draw->screen = _XftDrawScreen (dpy, pixmap, NULL);
    draw->depth = depth;
    draw->bits_per_pixel = 0;	/* don't find out until we need it */
    draw->visual = NULL;
    draw->colormap = 0;
    draw->render.pict = 0;
    draw->core.gc = NULL;
    draw->core.use_pixmap = 0;
    draw->clip_type = XftClipTypeNone;
    draw->subwindow_mode = ClipByChildren;
    XftMemAlloc (XFT_MEM_DRAW, sizeof (XftDraw));
    return draw;
}

static XRenderPictFormat *
_XftDrawFormat (XftDraw	*draw)
{
    XftDisplayInfo  *info = _XftDisplayInfoGet (draw->dpy, True);

    if (!info || !info->hasRender)
	return NULL;

    if (draw->visual == NULL)
    {
	XRenderPictFormat   pf;

	pf.type = PictTypeDirect;
	pf.depth = XftDrawDepth (draw);
	pf.direct.alpha = 0;
	pf.direct.alphaMask = (1 << pf.depth) - 1;
	return XRenderFindFormat (draw->dpy,
				  (PictFormatType|
				   PictFormatDepth|
				   PictFormatAlpha|
				   PictFormatAlphaMask),
				  &pf,
				  0);
    }
    else
	return XRenderFindVisualFormat (draw->dpy, draw->visual);
}

_X_EXPORT void
XftDrawChange (XftDraw	*draw,
	       Drawable	drawable)
{
    draw->drawable = drawable;
    if (draw->render.pict)
    {
	XRenderFreePicture (draw->dpy, draw->render.pict);
	draw->render.pict = 0;
    }
    if (draw->core.gc)
    {
	XFreeGC (draw->dpy, draw->core.gc);
	draw->core.gc = NULL;
    }
}

_X_EXPORT Display *
XftDrawDisplay (XftDraw *draw)
{
    return draw->dpy;
}

_X_EXPORT Drawable
XftDrawDrawable (XftDraw *draw)
{
    return draw->drawable;
}

_X_EXPORT Colormap
XftDrawColormap (XftDraw *draw)
{
    return draw->colormap;
}

_X_EXPORT Visual *
XftDrawVisual (XftDraw *draw)
{
    return draw->visual;
}

_X_EXPORT void
XftDrawDestroy (XftDraw	*draw)
{
    if (draw->render.pict)
	XRenderFreePicture (draw->dpy, draw->render.pict);
    if (draw->core.gc)
	XFreeGC (draw->dpy, draw->core.gc);
    switch (draw->clip_type) {
    case XftClipTypeRegion:
	XDestroyRegion (draw->clip.region);
	break;
    case XftClipTypeRectangles:
	free (draw->clip.rect);
	break;
    case XftClipTypeNone:
	break;
    }
    XftMemFree (XFT_MEM_DRAW, sizeof (XftDraw));
    free (draw);
}

_X_EXPORT Picture
XftDrawSrcPicture (XftDraw *draw, _Xconst XftColor *color)
{
    Display	    *dpy = draw->dpy;
    XftDisplayInfo  *info = _XftDisplayInfoGet (dpy, True);
    int		    i;
    XftColor	    bitmapColor;

    if (!info || !info->solidFormat)
	return 0;

    /*
     * Monochrome targets require special handling; the PictOp controls
     * the color, and the color must be opaque
     */
    if (!draw->visual && draw->depth == 1)
    {
	bitmapColor.color.alpha = 0xffff;
	bitmapColor.color.red   = 0xffff;
	bitmapColor.color.green = 0xffff;
	bitmapColor.color.blue  = 0xffff;
	color = &bitmapColor;
    }

    /*
     * See if there's one already available
     */
    for (i = 0; i < XFT_NUM_SOLID_COLOR; i++)
    {
	if (info->colors[i].pict &&
	    info->colors[i].screen == draw->screen &&
	    !memcmp ((void *) &color->color,
		     (void *) &info->colors[i].color,
		     sizeof (XRenderColor)))
	    return info->colors[i].pict;
    }
    /*
     * Pick one to replace at random
     */
    i = (unsigned int) rand () % XFT_NUM_SOLID_COLOR;

    if (info->hasSolid) {
	/*
	 * Free any existing entry
	 */
	if (info->colors[i].pict)
	    XRenderFreePicture (dpy, info->colors[i].pict);
	/*
	 * Create picture
	 */
	info->colors[i].pict = XRenderCreateSolidFill (draw->dpy, &color->color);
    } else {
	if (info->colors[i].screen != draw->screen && info->colors[i].pict)
	{
	    XRenderFreePicture (dpy, info->colors[i].pict);
	    info->colors[i].pict = 0;
	}
	/*
	 * Create picture if necessary
	 */
	if (!info->colors[i].pict)
	{
	    Pixmap			    pix;
	    XRenderPictureAttributes    pa;

	    pix = XCreatePixmap (dpy, RootWindow (dpy, draw->screen), 1, 1,
				 info->solidFormat->depth);
	    pa.repeat = True;
	    info->colors[i].pict = XRenderCreatePicture (draw->dpy,
							 pix,
							 info->solidFormat,
							 CPRepeat, &pa);
	    XFreePixmap (dpy, pix);
	}
	/*
	 * Set to the new color
	 */
	info->colors[i].color = color->color;
	info->colors[i].screen = draw->screen;
	XRenderFillRectangle (dpy, PictOpSrc,
			      info->colors[i].pict,
			      &color->color, 0, 0, 1, 1);
    }
    info->colors[i].color = color->color;
    info->colors[i].screen = draw->screen;

    return info->colors[i].pict;
}

static int
_XftDrawOp (_Xconst XftDraw *draw, _Xconst XftColor *color)
{
    if (draw->visual || draw->depth != 1)
	return PictOpOver;
    if (color->color.alpha >= 0x8000)
	return PictOpOver;
    return PictOpOutReverse;
}

static FcBool
_XftDrawRenderPrepare (XftDraw	*draw)
{
    if (!draw->render.pict)
    {
	XRenderPictFormat	    *format;
	XRenderPictureAttributes    pa;
	unsigned long		    mask = 0;

	format = _XftDrawFormat (draw);
	if (!format)
	    return FcFalse;

	if (draw->subwindow_mode == IncludeInferiors)
	{
	    pa.subwindow_mode = IncludeInferiors;
	    mask |= CPSubwindowMode;
	}
	draw->render.pict = XRenderCreatePicture (draw->dpy, draw->drawable,
						  format, mask, &pa);
	if (!draw->render.pict)
	    return FcFalse;
	switch (draw->clip_type) {
	case XftClipTypeRegion:
	    XRenderSetPictureClipRegion (draw->dpy, draw->render.pict,
					 draw->clip.region);
	    break;
	case XftClipTypeRectangles:
	    XRenderSetPictureClipRectangles (draw->dpy, draw->render.pict,
					     draw->clip.rect->xOrigin,
					     draw->clip.rect->yOrigin,
					     XftClipRects(draw->clip.rect),
					     draw->clip.rect->n);
	    break;
	case XftClipTypeNone:
	    break;
	}
    }
    return FcTrue;
}

static FcBool
_XftDrawCorePrepare (XftDraw *draw, _Xconst XftColor *color)
{
    if (!draw->core.gc)
    {
	XGCValues	gcv;
	unsigned long	mask = 0;
	if (draw->subwindow_mode == IncludeInferiors)
	{
	    gcv.subwindow_mode = IncludeInferiors;
	    mask |= GCSubwindowMode;
	}
	draw->core.gc = XCreateGC (draw->dpy, draw->drawable, mask, &gcv);
	if (!draw->core.gc)
	    return FcFalse;
	switch (draw->clip_type) {
	case XftClipTypeRegion:
	    XSetRegion (draw->dpy, draw->core.gc, draw->clip.region);
	    break;
	case XftClipTypeRectangles:
	    XSetClipRectangles (draw->dpy, draw->core.gc,
				draw->clip.rect->xOrigin,
				draw->clip.rect->yOrigin,
				XftClipRects (draw->clip.rect),
				draw->clip.rect->n,
				Unsorted);
	    break;
	case XftClipTypeNone:
	    break;
	}
    }
    XSetForeground (draw->dpy, draw->core.gc, color->pixel);
    return FcTrue;
}

_X_EXPORT Picture
XftDrawPicture (XftDraw *draw)
{
    if (!_XftDrawRenderPrepare (draw))
	return 0;
    return draw->render.pict;
}

#define NUM_LOCAL   1024

_X_EXPORT void
XftDrawGlyphs (XftDraw		*draw,
	       _Xconst XftColor	*color,
	       XftFont		*pub,
	       int		x,
	       int		y,
	       _Xconst FT_UInt	*glyphs,
	       int		nglyphs)
{
    XftFontInt	*font = (XftFontInt *) pub;

    if (font->format)
    {
	Picture	    src;

	if (_XftDrawRenderPrepare (draw) &&
	    (src = XftDrawSrcPicture (draw, color)))
	    XftGlyphRender (draw->dpy, _XftDrawOp (draw, color),
			     src, pub, draw->render.pict,
			     0, 0, x, y, glyphs, nglyphs);
    }
    else
    {
	if (_XftDrawCorePrepare (draw, color))
	    XftGlyphCore (draw, color, pub, x, y, glyphs, nglyphs);
    }
}

_X_EXPORT void
XftDrawString8 (XftDraw		    *draw,
		_Xconst XftColor    *color,
		XftFont		    *pub,
		int		    x,
		int		    y,
		_Xconst FcChar8	    *string,
		int		    len)
{
    FT_UInt	    *glyphs, glyphs_local[NUM_LOCAL];
    int		    i;

    if (XftDebug () & XFT_DBG_DRAW)
	printf ("DrawString \"%*.*s\"\n", len, len, string);

    if (len <= NUM_LOCAL)
	glyphs = glyphs_local;
    else
    {
	glyphs = malloc (len * sizeof (FT_UInt));
	if (!glyphs)
	    return;
    }
    for (i = 0; i < len; i++)
	glyphs[i] = XftCharIndex (draw->dpy, pub, string[i]);
    XftDrawGlyphs (draw, color, pub, x, y, glyphs, len);
    if (glyphs != glyphs_local)
	free (glyphs);
}

_X_EXPORT void
XftDrawString16 (XftDraw	    *draw,
		 _Xconst XftColor   *color,
		 XftFont	    *pub,
		 int		    x,
		 int		    y,
		 _Xconst FcChar16   *string,
		 int		    len)
{
    FT_UInt	    *glyphs, glyphs_local[NUM_LOCAL];
    int		    i;

    if (len <= NUM_LOCAL)
	glyphs = glyphs_local;
    else
    {
	glyphs = malloc (len * sizeof (FT_UInt));
	if (!glyphs)
	    return;
    }
    for (i = 0; i < len; i++)
	glyphs[i] = XftCharIndex (draw->dpy, pub, string[i]);

    XftDrawGlyphs (draw, color, pub, x, y, glyphs, len);
    if (glyphs != glyphs_local)
	free (glyphs);
}

_X_EXPORT void
XftDrawString32 (XftDraw	    *draw,
		 _Xconst XftColor   *color,
		 XftFont	    *pub,
		 int		    x,
		 int		    y,
		 _Xconst FcChar32   *string,
		 int		    len)
{
    FT_UInt	    *glyphs, glyphs_local[NUM_LOCAL];
    int		    i;

    if (len <= NUM_LOCAL)
	glyphs = glyphs_local;
    else
    {
	glyphs = malloc (len * sizeof (FT_UInt));
	if (!glyphs)
	    return;
    }
    for (i = 0; i < len; i++)
	glyphs[i] = XftCharIndex (draw->dpy, pub, string[i]);

    XftDrawGlyphs (draw, color, pub, x, y, glyphs, len);
    if (glyphs != glyphs_local)
	free (glyphs);
}

_X_EXPORT void
XftDrawStringUtf8 (XftDraw	    *draw,
		   _Xconst XftColor *color,
		   XftFont	    *pub,
		   int		    x,
		   int		    y,
		   _Xconst FcChar8  *string,
		   int		    len)
{
    FT_UInt	    *glyphs, *glyphs_new, glyphs_local[NUM_LOCAL];
    FcChar32	    ucs4;
    int		    i;
    int		    l;
    int		    size;

    i = 0;
    glyphs = glyphs_local;
    size = NUM_LOCAL;
    while (len && (l = FcUtf8ToUcs4 (string, &ucs4, len)) > 0)
    {
	if (i == size)
	{
	    glyphs_new = malloc (size * 2 * sizeof (FT_UInt));
	    if (!glyphs_new)
	    {
		if (glyphs != glyphs_local)
		    free (glyphs);
		return;
	    }
	    memcpy (glyphs_new, glyphs, size * sizeof (FT_UInt));
	    size *= 2;
	    if (glyphs != glyphs_local)
		free (glyphs);
	    glyphs = glyphs_new;
	}
	glyphs[i++] = XftCharIndex (draw->dpy, pub, ucs4);
	string += l;
	len -= l;
    }
    XftDrawGlyphs (draw, color, pub, x, y, glyphs, i);
    if (glyphs != glyphs_local)
	free (glyphs);
}

_X_EXPORT void
XftDrawStringUtf16 (XftDraw		*draw,
		    _Xconst XftColor	*color,
		    XftFont		*pub,
		    int			x,
		    int			y,
		    _Xconst FcChar8	*string,
		    FcEndian		endian,
		    int			len)
{
    FT_UInt	    *glyphs, *glyphs_new, glyphs_local[NUM_LOCAL];
    FcChar32	    ucs4;
    int		    i;
    int		    l;
    int		    size;

    i = 0;
    glyphs = glyphs_local;
    size = NUM_LOCAL;
    while (len && (l = FcUtf16ToUcs4 (string, endian, &ucs4, len)) > 0)
    {
	if (i == size)
	{
	    glyphs_new = malloc (size * 2 * sizeof (FT_UInt));
	    if (!glyphs_new)
	    {
		if (glyphs != glyphs_local)
		    free (glyphs);
		return;
	    }
	    memcpy (glyphs_new, glyphs, size * sizeof (FT_UInt));
	    size *= 2;
	    if (glyphs != glyphs_local)
		free (glyphs);
	    glyphs = glyphs_new;
	}
	glyphs[i++] = XftCharIndex (draw->dpy, pub, ucs4);
	string += l;
	len -= l;
    }
    XftDrawGlyphs (draw, color, pub, x, y, glyphs, i);
    if (glyphs != glyphs_local)
	free (glyphs);
}

_X_EXPORT void
XftDrawGlyphSpec (XftDraw		*draw,
		  _Xconst XftColor	*color,
		  XftFont		*pub,
		  _Xconst XftGlyphSpec	*glyphs,
		  int			len)
{
    XftFontInt	*font = (XftFontInt *) pub;

    if (font->format)
    {
	Picture	src;

	if (_XftDrawRenderPrepare (draw) &&
	    (src = XftDrawSrcPicture (draw, color)))
	{
	    XftGlyphSpecRender (draw->dpy, _XftDrawOp (draw, color),
				src, pub, draw->render.pict,
				0, 0, glyphs, len);
	}
    }
    else
    {
	if (_XftDrawCorePrepare (draw, color))
	    XftGlyphSpecCore (draw, color, pub, glyphs, len);
    }
}

_X_EXPORT void
XftDrawGlyphFontSpec (XftDraw			*draw,
		      _Xconst XftColor		*color,
		      _Xconst XftGlyphFontSpec	*glyphs,
		      int			len)
{
    int		i;
    int		start;

    i = 0;
    while (i < len)
    {
	start = i;
	if (((XftFontInt *) glyphs[i].font)->format)
	{
	    Picture	src;
	    while (i < len && ((XftFontInt *) glyphs[i].font)->format)
		i++;
	    if (_XftDrawRenderPrepare (draw) &&
		(src = XftDrawSrcPicture (draw, color)))
	    {
		XftGlyphFontSpecRender (draw->dpy, _XftDrawOp (draw, color),
					src, draw->render.pict,
					0, 0, glyphs + start , i - start);
	    }
	}
	else
	{
	    while (i < len && !((XftFontInt *) glyphs[i].font)->format)
		i++;
	    if (_XftDrawCorePrepare (draw, color))
		XftGlyphFontSpecCore (draw, color, glyphs + start, i - start);
	}
    }
}

_X_EXPORT void
XftDrawCharSpec (XftDraw		*draw,
		 _Xconst XftColor	*color,
		 XftFont		*pub,
		 _Xconst XftCharSpec	*chars,
		 int			len)
{
    XftGlyphSpec    *glyphs, glyphs_local[NUM_LOCAL];
    int		    i;

    if (len <= NUM_LOCAL)
	glyphs = glyphs_local;
    else
    {
	glyphs = malloc (len * sizeof (XftGlyphSpec));
	if (!glyphs)
	    return;
    }
    for (i = 0; i < len; i++)
    {
	glyphs[i].glyph = XftCharIndex(draw->dpy, pub, chars[i].ucs4);
	glyphs[i].x = chars[i].x;
	glyphs[i].y = chars[i].y;
    }

    XftDrawGlyphSpec (draw, color, pub, glyphs, len);
    if (glyphs != glyphs_local)
	free (glyphs);
}

_X_EXPORT void
XftDrawCharFontSpec (XftDraw			*draw,
		     _Xconst XftColor		*color,
		     _Xconst XftCharFontSpec	*chars,
		     int			len)
{
    XftGlyphFontSpec	*glyphs, glyphs_local[NUM_LOCAL];
    int			i;

    if (len <= NUM_LOCAL)
	glyphs = glyphs_local;
    else
    {
	glyphs = malloc (len * sizeof (XftGlyphFontSpec));
	if (!glyphs)
	    return;
    }
    for (i = 0; i < len; i++)
    {
	glyphs[i].font = chars[i].font;
	glyphs[i].glyph = XftCharIndex(draw->dpy, glyphs[i].font, chars[i].ucs4);
	glyphs[i].x = chars[i].x;
	glyphs[i].y = chars[i].y;
    }

    XftDrawGlyphFontSpec (draw, color, glyphs, len);
    if (glyphs != glyphs_local)
	free (glyphs);
}

_X_EXPORT void
XftDrawRect (XftDraw		*draw,
	     _Xconst XftColor	*color,
	     int		x,
	     int		y,
	     unsigned int	width,
	     unsigned int	height)
{
    if (_XftDrawRenderPrepare (draw))
    {
	XRenderFillRectangle (draw->dpy, PictOpSrc, draw->render.pict,
			      &color->color, x, y, width, height);
    }
    else if (_XftDrawCorePrepare (draw, color))
    {
	/* note: not XftRectCore() */
	XSetForeground (draw->dpy, draw->core.gc, color->pixel);
	XFillRectangle (draw->dpy, draw->drawable, draw->core.gc,
			x, y, width, height);
    }
}

_X_EXPORT Bool
XftDrawSetClip (XftDraw	*draw,
		Region	r)
{
    Region			n = NULL;

    /*
     * Check for quick exits
     */
    if (!r && draw->clip_type == XftClipTypeNone)
	return True;

    if (r &&
	draw->clip_type == XftClipTypeRegion &&
	XEqualRegion (r, draw->clip.region))
    {
	return True;
    }

    /*
     * Duplicate the region so future changes can be short circuited
     */
    if (r)
    {
	n = XCreateRegion ();
	if (n)
	{
	    if (!XUnionRegion (n, r, n))
	    {
		XDestroyRegion (n);
		return False;
	    }
	}
    }

    /*
     * Destroy existing clip
     */
    switch (draw->clip_type) {
    case XftClipTypeRegion:
	XDestroyRegion (draw->clip.region);
	break;
    case XftClipTypeRectangles:
	free (draw->clip.rect);
	break;
    case XftClipTypeNone:
	break;
    }

    /*
     * Set the clip
     */
    if (n)
    {
	draw->clip_type = XftClipTypeRegion;
	draw->clip.region = n;
    }
    else
    {
	draw->clip_type = XftClipTypeNone;
    }
    /*
     * Apply new clip to existing objects
     */
    if (draw->render.pict)
    {
	if (n)
	    XRenderSetPictureClipRegion (draw->dpy, draw->render.pict, n);
	else
	{
	    XRenderPictureAttributes	pa;
	    pa.clip_mask = None;
	    XRenderChangePicture (draw->dpy, draw->render.pict,
				  CPClipMask, &pa);
	}
    }
    if (draw->core.gc)
    {
	if (n)
	    XSetRegion (draw->dpy, draw->core.gc, draw->clip.region);
	else
	    XSetClipMask (draw->dpy, draw->core.gc, None);
    }
    return True;
}

_X_EXPORT Bool
XftDrawSetClipRectangles (XftDraw		*draw,
			  int			xOrigin,
			  int			yOrigin,
			  _Xconst XRectangle	*rects,
			  int			n)
{
    XftClipRect	*new = NULL;

    /*
     * Check for quick exit
     */
    if (draw->clip_type == XftClipTypeRectangles &&
	draw->clip.rect->n == n &&
	(n == 0 || (draw->clip.rect->xOrigin == xOrigin &&
		    draw->clip.rect->yOrigin == yOrigin)) &&
	!memcmp (XftClipRects (draw->clip.rect), rects, n * sizeof (XRectangle)))
    {
	return True;
    }

    /*
     * Duplicate the region so future changes can be short circuited
     */
    new = malloc (sizeof (XftClipRect) + n * sizeof (XRectangle));
    if (!new)
	return False;

    new->n = n;
    new->xOrigin = xOrigin;
    new->yOrigin = yOrigin;
    memcpy (XftClipRects (new), rects, n * sizeof (XRectangle));

    /*
     * Destroy existing clip
     */
    switch (draw->clip_type) {
    case XftClipTypeRegion:
	XDestroyRegion (draw->clip.region);
	break;
    case XftClipTypeRectangles:
	free (draw->clip.rect);
	break;
    case XftClipTypeNone:
	break;
    }

    /*
     * Set the clip
     */
    draw->clip_type = XftClipTypeRectangles;
    draw->clip.rect = new;
    /*
     * Apply new clip to existing objects
     */
    if (draw->render.pict)
    {
	XRenderSetPictureClipRectangles (draw->dpy, draw->render.pict,
					 new->xOrigin,
					 new->yOrigin,
					 XftClipRects(new),
					 new->n);
    }
    if (draw->core.gc)
    {
	XSetClipRectangles (draw->dpy, draw->core.gc,
			    new->xOrigin,
			    new->yOrigin,
			    XftClipRects (new),
			    new->n,
			    Unsorted);
    }
    return True;
}

_X_EXPORT void
XftDrawSetSubwindowMode (XftDraw *draw, int mode)
{
    if (mode == draw->subwindow_mode)
	return;
    draw->subwindow_mode = mode;
    if (draw->render.pict)
    {
	XRenderPictureAttributes    pa;

	pa.subwindow_mode = mode;
	XRenderChangePicture (draw->dpy, draw->render.pict,
			      CPSubwindowMode, &pa);
    }
    if (draw->core.gc)
	XSetSubwindowMode (draw->dpy, draw->core.gc, mode);
}