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