11ab64890Smrg/*
21ab64890Smrg
31ab64890SmrgCopyright 1986, 1998  The Open Group
41ab64890Smrg
51ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
61ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
71ab64890Smrgthe above copyright notice appear in all copies and that both that
81ab64890Smrgcopyright notice and this permission notice appear in supporting
91ab64890Smrgdocumentation.
101ab64890Smrg
111ab64890SmrgThe above copyright notice and this permission notice shall be included in
121ab64890Smrgall copies or substantial portions of the Software.
131ab64890Smrg
141ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
151ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
161ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
171ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
181ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
191ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
201ab64890Smrg
211ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be
221ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings
231ab64890Smrgin this Software without prior written authorization from The Open Group.
241ab64890Smrg
251ab64890Smrg*/
261ab64890Smrg
271ab64890Smrg#ifdef HAVE_CONFIG_H
281ab64890Smrg#include <config.h>
291ab64890Smrg#endif
301ab64890Smrg#include "Xlibint.h"
311ab64890Smrg
321ab64890Smrgint
331ab64890SmrgXDrawText(
341ab64890Smrg    register Display *dpy,
351ab64890Smrg    Drawable d,
361ab64890Smrg    GC gc,
371ab64890Smrg    int x,
381ab64890Smrg    int y,
391ab64890Smrg    XTextItem *items,
401ab64890Smrg    int nitems)
4161b2299dSmrg{
421ab64890Smrg    register int i;
431ab64890Smrg    register XTextItem *item;
441ab64890Smrg    int length = 0;
451ab64890Smrg    register xPolyText8Req *req;
461ab64890Smrg
471ab64890Smrg    LockDisplay(dpy);
481ab64890Smrg    FlushGC(dpy, gc);
491ab64890Smrg    GetReq (PolyText8, req);
501ab64890Smrg    req->drawable = d;
511ab64890Smrg    req->gc = gc->gid;
521ab64890Smrg    req->x = x;
531ab64890Smrg    req->y = y;
541ab64890Smrg
551ab64890Smrg    item = items;
561ab64890Smrg    for (i=0; i < nitems; i++) {
571ab64890Smrg	if (item->font)
581ab64890Smrg	    length += 5;  /* a 255 byte, plus size of Font id */
591ab64890Smrg        if (item->delta)
601ab64890Smrg        {
611ab64890Smrg	    if (item->delta > 0)
621ab64890Smrg	    {
631ab64890Smrg	      length += SIZEOF(xTextElt) * ((item->delta + 126) / 127);
641ab64890Smrg	    }
651ab64890Smrg            else
661ab64890Smrg            {
671ab64890Smrg   	      length += SIZEOF(xTextElt) * ((-item->delta + 127) / 128);
681ab64890Smrg 	    }
691ab64890Smrg        }
701ab64890Smrg	if (item->nchars > 0)
711ab64890Smrg	{
721ab64890Smrg	    length += SIZEOF(xTextElt) * ((item->nchars + 253) / 254 - 1);
731ab64890Smrg	    if (!item->delta) length += SIZEOF(xTextElt);
741ab64890Smrg	    length += item->nchars;
751ab64890Smrg     	}
761ab64890Smrg	item++;
771ab64890Smrg    }
781ab64890Smrg
791ab64890Smrg    req->length += (length + 3)>>2;  /* convert to number of 32-bit words */
801ab64890Smrg
811ab64890Smrg
8261b2299dSmrg    /*
831ab64890Smrg     * If the entire request does not fit into the remaining space in the
841ab64890Smrg     * buffer, flush the buffer first.   If the request does fit into the
851ab64890Smrg     * empty buffer, then we won't have to flush it at the end to keep
8661b2299dSmrg     * the buffer 32-bit aligned.
871ab64890Smrg     */
881ab64890Smrg
891ab64890Smrg    if (dpy->bufptr + length > dpy->bufmax)
901ab64890Smrg    	_XFlush (dpy);
911ab64890Smrg
921ab64890Smrg    item = items;
931ab64890Smrg    for (i=0; i< nitems; i++) {
941ab64890Smrg
951ab64890Smrg	if (item->font) {
961ab64890Smrg            /* to mark a font shift, write a 255 byte followed by
971ab64890Smrg	       the 4 bytes of font ID, big-end first */
981ab64890Smrg	    register unsigned char *f;
991ab64890Smrg	    BufAlloc (unsigned char *, f, 5);
1001ab64890Smrg
1011ab64890Smrg	    f[0] = 255;
1021ab64890Smrg	    f[1] = (item->font & 0xff000000) >> 24;
1031ab64890Smrg	    f[2] = (item->font & 0x00ff0000) >> 16;
1041ab64890Smrg	    f[3] = (item->font & 0x0000ff00) >> 8;
1051ab64890Smrg	    f[4] =  item->font & 0x000000ff;
1061ab64890Smrg
1071ab64890Smrg            /* update GC shadow */
1081ab64890Smrg	    gc->values.font = item->font;
1091ab64890Smrg	    }
1101ab64890Smrg
1111ab64890Smrg	{
1121ab64890Smrg	    int nbytes = SIZEOF(xTextElt);
1131ab64890Smrg	    int PartialNChars = item->nchars;
1141ab64890Smrg	    int PartialDelta = item->delta;
1151ab64890Smrg            /* register xTextElt *elt; */
1161ab64890Smrg	    int FirstTimeThrough = True;
1171ab64890Smrg 	    char *CharacterOffset = item->chars;
1181ab64890Smrg            char *tbuf = NULL;
1191ab64890Smrg
1201ab64890Smrg	    while((PartialDelta < -128) || (PartialDelta > 127))
1211ab64890Smrg            {
1221ab64890Smrg	    	int nb = SIZEOF(xTextElt);
1231ab64890Smrg
12461b2299dSmrg	    	BufAlloc (char *, tbuf, nb);
1251ab64890Smrg	    	*tbuf = 0;    /*   elt->len  */
12661b2299dSmrg	    	if (PartialDelta > 0 )
1271ab64890Smrg		{
1281ab64890Smrg		    *(tbuf+1) = 127;  /* elt->delta  */
1291ab64890Smrg		    PartialDelta = PartialDelta - 127;
1301ab64890Smrg		}
1311ab64890Smrg		else
1321ab64890Smrg		{
1331ab64890Smrg		    /* -128 = 0x8, need to be careful of signed chars... */
1341ab64890Smrg		    *((unsigned char *)(tbuf+1)) = 0x80;  /* elt->delta */
1351ab64890Smrg		    PartialDelta = PartialDelta + 128;
1361ab64890Smrg		}
1371ab64890Smrg	    }
1381ab64890Smrg	    if (PartialDelta)
1391ab64890Smrg            {
14061b2299dSmrg                BufAlloc (char *, tbuf , nbytes);
1411ab64890Smrg	        *tbuf = 0;      /* elt->len */
1421ab64890Smrg		*(tbuf+1) = PartialDelta;    /* elt->delta  */
1431ab64890Smrg	    }
1441ab64890Smrg	    while(PartialNChars > 254)
1451ab64890Smrg            {
1461ab64890Smrg		nbytes = 254;
1471ab64890Smrg	    	if (FirstTimeThrough)
1481ab64890Smrg		{
1491ab64890Smrg		    FirstTimeThrough = False;
1501ab64890Smrg		    if (!item->delta)
15161b2299dSmrg 		    {
1521ab64890Smrg			nbytes += SIZEOF(xTextElt);
15361b2299dSmrg	   		BufAlloc (char *, tbuf, nbytes);
1541ab64890Smrg		        *(tbuf+1) = 0;     /* elt->delta */
1551ab64890Smrg		    }
1561ab64890Smrg		    else
1571ab64890Smrg		    {
1581ab64890Smrg			char *DummyChar;
1591ab64890Smrg		        BufAlloc(char *, DummyChar, nbytes);
1601ab64890Smrg		    }
1611ab64890Smrg		}
1621ab64890Smrg		else
1631ab64890Smrg		{
1641ab64890Smrg 		    nbytes += SIZEOF(xTextElt);
1651ab64890Smrg	   	    BufAlloc (char *, tbuf, nbytes);
1661ab64890Smrg		    *(tbuf+1) = 0;   /* elt->delta */
1671ab64890Smrg		}
1681ab64890Smrg		/* watch out for signs on chars */
1691ab64890Smrg		*(unsigned char *)tbuf = 254;  /* elt->len */
1701ab64890Smrg                memcpy (tbuf+2 , CharacterOffset, 254);
1711ab64890Smrg		PartialNChars = PartialNChars - 254;
1721ab64890Smrg		CharacterOffset += 254;
1731ab64890Smrg
1741ab64890Smrg	    }
1751ab64890Smrg	    if (PartialNChars)
1761ab64890Smrg            {
1771ab64890Smrg		nbytes = PartialNChars;
1781ab64890Smrg	    	if (FirstTimeThrough)
1791ab64890Smrg		{
1801ab64890Smrg		    FirstTimeThrough = False;
1811ab64890Smrg		    if (!item->delta)
18261b2299dSmrg 		    {
1831ab64890Smrg			nbytes += SIZEOF(xTextElt);
18461b2299dSmrg	   		BufAlloc (char *, tbuf, nbytes);
1851ab64890Smrg			*(tbuf+1) = 0;   /*  elt->delta  */
1861ab64890Smrg		    }
1871ab64890Smrg		    else
1881ab64890Smrg		    {
1891ab64890Smrg			char *DummyChar;
1901ab64890Smrg		        BufAlloc(char *, DummyChar, nbytes);
1911ab64890Smrg		    }
1921ab64890Smrg		}
1931ab64890Smrg		else
1941ab64890Smrg		{
1951ab64890Smrg 		    nbytes += SIZEOF(xTextElt);
19661b2299dSmrg	   	    BufAlloc (char *, tbuf, nbytes);
1971ab64890Smrg		    *(tbuf+1) = 0;   /* elt->delta  */
1981ab64890Smrg		}
1991ab64890Smrg	    	*tbuf = PartialNChars;   /*  elt->len  */
2009c019ec5Smaya                memcpy (tbuf+2 , CharacterOffset, (size_t) PartialNChars);
2011ab64890Smrg	    }
2021ab64890Smrg	}
2031ab64890Smrg    item++;
2041ab64890Smrg    }
2051ab64890Smrg
2061ab64890Smrg    /* Pad request out to a 32-bit boundary */
2071ab64890Smrg
2081ab64890Smrg    if (length &= 3) {
2091ab64890Smrg	char *pad;
21061b2299dSmrg	/*
2111ab64890Smrg	 * BufAlloc is a macro that uses its last argument more than
21261b2299dSmrg	 * once, otherwise I'd write "BufAlloc (char *, pad, 4-length)"
2131ab64890Smrg	 */
2141ab64890Smrg	length = 4 - length;
2151ab64890Smrg	BufAlloc (char *, pad, length);
21661b2299dSmrg	/*
2171ab64890Smrg	 * if there are 3 bytes of padding, the first byte MUST be 0
21861b2299dSmrg	 * so the pad bytes aren't mistaken for a final xTextElt
2191ab64890Smrg	 */
2201ab64890Smrg	*pad = 0;
2211ab64890Smrg        }
2221ab64890Smrg
22361b2299dSmrg    /*
2241ab64890Smrg     * If the buffer pointer is not now pointing to a 32-bit boundary,
2251ab64890Smrg     * we must flush the buffer so that it does point to a 32-bit boundary
22661b2299dSmrg     * at the end of this routine.
2271ab64890Smrg     */
2281ab64890Smrg
2291ab64890Smrg    if ((dpy->bufptr - dpy->buffer) & 3)
2301ab64890Smrg       _XFlush (dpy);
2311ab64890Smrg    UnlockDisplay(dpy);
2321ab64890Smrg    SyncHandle();
2331ab64890Smrg    return 0;
2341ab64890Smrg    }
235