Initialize Tizen 2.3
[framework/uifw/xorg/lib/libx11.git] / src / Text16.c
1 /*
2
3 Copyright 1986, 1998  The Open Group
4
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in 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
32 int
33 XDrawString16(
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 #if defined(MUSTCOPY) || defined(MUSTCOPY2B)
86             {
87                 register int i;
88                 register unsigned char *cp;
89                 for (i = 0, cp = ((unsigned char *)elt) + 2; i < 254; i++) {
90                     *cp++ = CharacterOffset[i].byte1;
91                     *cp++ = CharacterOffset[i].byte2;
92                 }
93             }
94 #else
95             memcpy (((char *) elt) + 2, (char *)CharacterOffset, 254 * 2);
96 #endif
97             PartialNChars = PartialNChars - 254;
98             CharacterOffset += 254;
99         }
100
101         if (PartialNChars)
102         {
103             nbytes = PartialNChars * 2  + SIZEOF(xTextElt);
104             BufAlloc (xTextElt *, elt, nbytes);
105             elt->delta = 0;
106             elt->len = PartialNChars;
107 #if defined(MUSTCOPY) || defined(MUSTCOPY2B)
108             {
109                 register int i;
110                 register unsigned char *cp;
111                 for (i = 0, cp = ((unsigned char *)elt) + 2; i < PartialNChars;
112                      i++) {
113                     *cp++ = CharacterOffset[i].byte1;
114                     *cp++ = CharacterOffset[i].byte2;
115                 }
116             }
117 #else
118             memcpy(((char *)elt) + 2, (char *)CharacterOffset, PartialNChars * 2);
119 #endif
120          }
121     }
122
123     /* Pad request out to a 32-bit boundary */
124
125     if (Datalength &= 3) {
126         char *pad;
127         /*
128          * BufAlloc is a macro that uses its last argument more than
129          * once, otherwise I'd write "BufAlloc (char *, pad, 4-length)"
130          */
131         length = 4 - Datalength;
132         BufAlloc (char *, pad, length);
133         /*
134          * if there are 3 bytes of padding, the first byte MUST be 0
135          * so the pad bytes aren't mistaken for a final xTextElt
136          */
137         *pad = 0;
138         }
139
140     /*
141      * If the buffer pointer is not now pointing to a 32-bit boundary,
142      * we must flush the buffer so that it does point to a 32-bit boundary
143      * at the end of this routine.
144      */
145
146     if ((dpy->bufptr - dpy->buffer) & 3)
147        _XFlush (dpy);
148     UnlockDisplay(dpy);
149     SyncHandle();
150     return 0;
151 }
152