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
33XDrawString16(
34    register Display *dpy,
35    Drawable d,
36    GC gc,
37    int x,
38    int y,
39    _Xconst XChar2b *string,
40    int length)
41{
42    int Datalength = 0;
43    register xPolyText16Req *req;
44
45    if (length <= 0)
46       return 0;
47
48    LockDisplay(dpy);
49    FlushGC(dpy, gc);
50    GetReq (PolyText16, 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 << 1);
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 	XChar2b *CharacterOffset = (XChar2b *)string;
78
79	while(PartialNChars > 254)
80        {
81 	    nbytes = 254 * 2 + SIZEOF(xTextElt);
82	    BufAlloc (xTextElt *, elt, nbytes);
83	    elt->delta = 0;
84	    elt->len = 254;
85            memcpy (((char *) elt) + 2, (char *)CharacterOffset, 254 * 2);
86	    PartialNChars = PartialNChars - 254;
87	    CharacterOffset += 254;
88	}
89
90        if (PartialNChars)
91        {
92	    nbytes = PartialNChars * 2  + SIZEOF(xTextElt);
93	    BufAlloc (xTextElt *, elt, nbytes);
94	    elt->delta = 0;
95	    elt->len = PartialNChars;
96            memcpy(((char *)elt) + 2, (char *)CharacterOffset, PartialNChars * 2);
97	 }
98    }
99
100    /* Pad request out to a 32-bit boundary */
101
102    if (Datalength &= 3) {
103	char *pad;
104	/*
105	 * BufAlloc is a macro that uses its last argument more than
106	 * once, otherwise I'd write "BufAlloc (char *, pad, 4-length)"
107	 */
108	length = 4 - Datalength;
109	BufAlloc (char *, pad, length);
110	/*
111	 * if there are 3 bytes of padding, the first byte MUST be 0
112	 * so the pad bytes aren't mistaken for a final xTextElt
113	 */
114	*pad = 0;
115        }
116
117    /*
118     * If the buffer pointer is not now pointing to a 32-bit boundary,
119     * we must flush the buffer so that it does point to a 32-bit boundary
120     * at the end of this routine.
121     */
122
123    if ((dpy->bufptr - dpy->buffer) & 3)
124       _XFlush (dpy);
125    UnlockDisplay(dpy);
126    SyncHandle();
127    return 0;
128}
129
130