Text.c revision 61b2299d
1/* $Xorg: Text.c,v 1.4 2001/02/09 02:03:37 xorgcvs Exp $ */
2/*
3
4Copyright 1986, 1998  The Open Group
5
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation.
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall not be
23used in advertising or otherwise to promote the sale, use or other dealings
24in this Software without prior written authorization from The Open Group.
25
26*/
27/* $XFree86: xc/lib/X11/Text.c,v 1.4 2001/12/14 19:54:07 dawes Exp $ */
28
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
32#include "Xlibint.h"
33
34int
35XDrawString(
36    register Display *dpy,
37    Drawable d,
38    GC gc,
39    int x,
40    int y,
41    _Xconst char *string,
42    int length)
43{
44    int Datalength = 0;
45    register xPolyText8Req *req;
46
47    if (length <= 0)
48       return 0;
49
50    LockDisplay(dpy);
51    FlushGC(dpy, gc);
52    GetReq (PolyText8, req);
53    req->drawable = d;
54    req->gc = gc->gid;
55    req->x = x;
56    req->y = y;
57
58
59    Datalength += SIZEOF(xTextElt) * ((length + 253) / 254) + length;
60
61
62    req->length += (Datalength + 3)>>2;  /* convert to number of 32-bit words */
63
64
65    /*
66     * If the entire request does not fit into the remaining space in the
67     * buffer, flush the buffer first.   If the request does fit into the
68     * empty buffer, then we won't have to flush it at the end to keep
69     * the buffer 32-bit aligned.
70     */
71
72    if (dpy->bufptr + Datalength > dpy->bufmax)
73    	_XFlush (dpy);
74
75    {
76	int nbytes;
77	int PartialNChars = length;
78        /* register xTextElt *elt; */
79 	char *CharacterOffset = (char *)string;
80        unsigned char *tbuf;
81
82	while(PartialNChars > 254)
83        {
84 	    nbytes = 254 + SIZEOF(xTextElt);
85	    BufAlloc (unsigned char *, tbuf, nbytes);
86/*    elt->delta = 0;
87 *    elt->len = 254;
88 */
89            *(unsigned char *)tbuf = 254;
90            *(tbuf+1) = 0;
91/*       memcpy ((char *) (elt + 1), CharacterOffset, 254);
92 */
93            memcpy ((char *)tbuf+2, CharacterOffset, 254);
94	    PartialNChars = PartialNChars - 254;
95	    CharacterOffset += 254;
96	}
97
98        if (PartialNChars)
99        {
100	    nbytes = PartialNChars + SIZEOF(xTextElt);
101	    BufAlloc (unsigned char *, tbuf, nbytes);
102/*    elt->delta = 0;
103 *    elt->len = PartialNChars;
104 */
105            *(unsigned char *)tbuf =  PartialNChars;
106            *(tbuf+1) = 0;
107/*     memcpy ((char *) (elt + 1), CharacterOffset, PartialNChars);
108 */
109         memcpy ((char *)tbuf+2, CharacterOffset, PartialNChars);
110	 }
111    }
112
113    /* Pad request out to a 32-bit boundary */
114
115    if (Datalength &= 3) {
116	char *pad;
117	/*
118	 * BufAlloc is a macro that uses its last argument more than
119	 * once, otherwise I'd write "BufAlloc (char *, pad, 4-length)"
120	 */
121	length = 4 - Datalength;
122	BufAlloc (char *, pad, length);
123	/*
124	 * if there are 3 bytes of padding, the first byte MUST be 0
125	 * so the pad bytes aren't mistaken for a final xTextElt
126	 */
127	*pad = 0;
128        }
129
130    /*
131     * If the buffer pointer is not now pointing to a 32-bit boundary,
132     * we must flush the buffer so that it does point to a 32-bit boundary
133     * at the end of this routine.
134     */
135
136    if ((dpy->bufptr - dpy->buffer) & 3)
137       _XFlush (dpy);
138    UnlockDisplay(dpy);
139    SyncHandle();
140    return 0;
141}
142