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