Debian Codegraph @

nx-X11/lib/src/FontInfo.c

raw
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 #include "reallocarray.h"
32 #include <limits.h>
33
34 #if defined(XF86BIGFONT)
35 #define USE_XF86BIGFONT
36 #endif
37 #ifdef USE_XF86BIGFONT
38 extern void _XF86BigfontFreeFontMetrics(
39 XFontStruct* /* fs */
40 );
41 #endif
42
43 char **XListFontsWithInfo(
44 register Display *dpy,
45 _Xconst char *pattern, /* null-terminated */
46 int maxNames,
47 int *actualCount, /* RETURN */
48 XFontStruct **info) /* RETURN */
49 {
50 unsigned long nbytes;
51 unsigned long reply_left; /* unused data left in reply buffer */
52 register int i;
53 register XFontStruct *fs;
54 unsigned int size = 0;
55 XFontStruct *finfo = NULL;
56 char **flist = NULL;
57 xListFontsWithInfoReply reply;
58 register xListFontsReq *req;
59 int j;
60
61 if (pattern != NULL && strlen(pattern) >= USHRT_MAX)
62 return NULL;
63
64 LockDisplay(dpy);
65 GetReq(ListFontsWithInfo, req);
66 req->maxNames = maxNames;
67 nbytes = req->nbytes = pattern ? (CARD16) strlen (pattern) : 0;
68 req->length += (nbytes + 3) >> 2;
69 _XSend (dpy, pattern, nbytes);
70 /* use _XSend instead of Data, since subsequent _XReply will flush buffer */
71
72 for (i = 0; ; i++) {
73 if (!_XReply (dpy, (xReply *) &reply,
74 ((SIZEOF(xListFontsWithInfoReply) -
75 SIZEOF(xGenericReply)) >> 2), xFalse)) {
76 reply.nameLength = 0; /* avoid trying to read more replies */
77 reply_left = 0;
78 goto badmem;
79 }
80 reply_left = reply.length -
81 ((SIZEOF(xListFontsWithInfoReply) - SIZEOF(xGenericReply)) >> 2);
82 if (reply.nameLength == 0) {
83 _XEatDataWords(dpy, reply_left);
84 break;
85 }
86 if (reply.nReplies >= (INT_MAX - i)) /* avoid overflowing size */
87 goto badmem;
88 if ((i + reply.nReplies) >= size) {
89 size = i + reply.nReplies + 1;
90
91 if (size >= (INT_MAX / sizeof(XFontStruct)))
92 goto badmem;
93
94 if (finfo) {
95 XFontStruct * tmp_finfo;
96 char ** tmp_flist;
97
98 tmp_finfo = Xreallocarray (finfo, size, sizeof(XFontStruct));
99 if (tmp_finfo)
100 finfo = tmp_finfo;
101 else
102 goto badmem;
103
104 tmp_flist = Xreallocarray (flist, size + 1, sizeof(char *));
105 if (tmp_flist)
106 flist = tmp_flist;
107 else
108 goto badmem;
109 }
110 else {
111 if (! (finfo = Xmallocarray(size, sizeof(XFontStruct))))
112 goto clearwire;
113 if (! (flist = Xmallocarray(size + 1, sizeof(char *)))) {
114 Xfree(finfo);
115 goto clearwire;
116 }
117 }
118 }
119 fs = &finfo[i];
120
121 fs->ext_data = NULL;
122 fs->per_char = NULL;
123 fs->fid = None;
124 fs->direction = reply.drawDirection;
125 fs->min_char_or_byte2 = reply.minCharOrByte2;
126 fs->max_char_or_byte2 = reply.maxCharOrByte2;
127 fs->min_byte1 = reply.minByte1;
128 fs->max_byte1 = reply.maxByte1;
129 fs->default_char = reply.defaultChar;
130 fs->all_chars_exist = reply.allCharsExist;
131 fs->ascent = cvtINT16toInt (reply.fontAscent);
132 fs->descent = cvtINT16toInt (reply.fontDescent);
133
134 /* XXX the next two statements won't work if short isn't 16 bits */
135 fs->min_bounds = * (XCharStruct *) &reply.minBounds;
136 fs->max_bounds = * (XCharStruct *) &reply.maxBounds;
137
138 fs->n_properties = reply.nFontProps;
139 fs->properties = NULL;
140 if (fs->n_properties > 0) {
141 /* nFontProps is a CARD16 */
142 nbytes = reply.nFontProps * SIZEOF(xFontProp);
143 if ((nbytes >> 2) <= reply_left) {
144 fs->properties = Xmallocarray (reply.nFontProps,
145 sizeof(XFontProp));
146 }
147 if (! fs->properties)
148 goto badmem;
149 _XRead32 (dpy, (long *)fs->properties, nbytes);
150 reply_left -= (nbytes >> 2);
151 }
152
153 /* nameLength is a CARD8 */
154 nbytes = reply.nameLength + 1;
155 if (!i)
156 nbytes++; /* make first string 1 byte longer, to match XListFonts */
157 flist[i] = Xmalloc (nbytes);
158 if (! flist[i]) {
159 if (finfo[i].properties) Xfree(finfo[i].properties);
160 goto badmem;
161 }
162 if (!i) {
163 *flist[0] = 0; /* zero to distinguish from XListFonts */
164 flist[0]++;
165 }
166 flist[i][reply.nameLength] = '\0';
167 _XReadPad (dpy, flist[i], (long) reply.nameLength);
168 }
169 *info = finfo;
170 *actualCount = i;
171 if (flist)
172 flist[i] = NULL; /* required in case XFreeFontNames is called */
173 UnlockDisplay(dpy);
174 SyncHandle();
175 return (flist);
176
177
178 badmem:
179 /* Free all memory allocated by this function. */
180 for (j=(i-1); (j >= 0); j--) {
181 if (j == 0)
182 flist[j]--; /* was incremented above */
183 Xfree(flist[j]);
184 if (finfo[j].properties) Xfree(finfo[j].properties);
185 }
186 Xfree(flist);
187 Xfree(finfo);
188
189 clearwire:
190 /* Clear the wire. */
191 _XEatDataWords(dpy, reply_left);
192 while ((reply.nameLength != 0) &&
193 _XReply(dpy, (xReply *) &reply,
194 ((SIZEOF(xListFontsWithInfoReply) - SIZEOF(xGenericReply))
195 >> 2), xTrue));
196 UnlockDisplay(dpy);
197 SyncHandle();
198 *info = NULL;
199 *actualCount = 0;
200 return (char **) NULL;
201 }
202
203 int
204 XFreeFontInfo (
205 char **names,
206 XFontStruct *info,
207 int actualCount)
208 {
209 register int i;
210 if (names) {
211 Xfree (names[0]-1);
212 for (i = 1; i < actualCount; i++) {
213 Xfree (names[i]);
214 }
215 Xfree(names);
216 }
217 if (info) {
218 for (i = 0; i < actualCount; i++) {
219 if (info[i].per_char)
220 #ifdef USE_XF86BIGFONT
221 _XF86BigfontFreeFontMetrics(&info[i]);
222 #else
223 Xfree (info[i].per_char);
224 #endif
225 if (info[i].properties)
226 Xfree (info[i].properties);
227 }
228 Xfree(info);
229 }
230 return 1;
231 }