comparison src/xmu.c @ 608:4d7fdf497470

[xemacs-hg @ 2001-06-04 16:59:51 by wmperry] 2001-06-04 William M. Perry <wmperry@gnu.org> * gpmevent.c (KG_CTRL): Just define these unconditionally. The linux headers are so lame that they do not expose these to userland programs and you cannot gracefully include the kernel headers. 2001-06-03 William M. Perry <wmperry@gnu.org> * scrollbar-gtk.c (gtk_create_scrollbar_instance): Make calling of gtk_size_request unconditional. 2001-06-02 William M. Perry <wmperry@gnu.org> * emacs-marshals.c: Regenerated. 2001-06-01 William M. Perry <wmperry@gnu.org> * glyphs-shared.c (read_bitmap_data): Common definition of read_bitmap_data_from_file added. This does not attempt to use the Xmu based code at all - lets us be consistent across platforms. * glyphs-gtk.c: Removed definition of read_bitmap_data_from_file - this is now in glyphs-shared.c * glyphs-msw.c: Ditto. * glyphs-x.c: Ditto. 2001-06-03 William M. Perry <wmperry@gnu.org> * dialog-gtk.el (popup-builtin-open-dialog): Yikes - don't forget to return the filename! * font.el (font-window-system-mappings): Add gtk entry - just an alias to the X code) 2001-06-02 William M. Perry <wmperry@gnu.org> * gtk-marshal.el: Fix for removing of the string_hash utility functions in hash.c
author wmperry
date Mon, 04 Jun 2001 17:00:02 +0000
parents abe6d1db359e
children b39c14581166
comparison
equal deleted inserted replaced
607:9979b8030c99 608:4d7fdf497470
154 } 154 }
155 155
156 return -1; 156 return -1;
157 } 157 }
158 158
159 159 /* Definition of XmuReadBitmapDataFromFile replaced with the code in
160 /* 160 * glyphs-shared.c
161 * Based on an optimized version provided by Jim Becker, August 5, 1988. 161 *
162 */ 162 * wmperry Jun 1, 2001
163 163 */
164
165 #define MAX_SIZE 255
166
167 /* shared data for the image read/parse logic */
168 static short hexTable[256]; /* conversion value */
169 static int hex_initialized; /* easier to fill in at run time */
170
171
172 /*
173 * Table index for the hex values. Initialized once, first time.
174 * Used for translation value or delimiter significance lookup.
175 */
176 static void initHexTable (void)
177 {
178 /*
179 * We build the table at run time for several reasons:
180 *
181 * 1. portable to non-ASCII machines.
182 * 2. still reentrant since we set the init flag after setting table.
183 * 3. easier to extend.
184 * 4. less prone to bugs.
185 */
186 hexTable['0'] = 0; hexTable['1'] = 1;
187 hexTable['2'] = 2; hexTable['3'] = 3;
188 hexTable['4'] = 4; hexTable['5'] = 5;
189 hexTable['6'] = 6; hexTable['7'] = 7;
190 hexTable['8'] = 8; hexTable['9'] = 9;
191 hexTable['A'] = 10; hexTable['B'] = 11;
192 hexTable['C'] = 12; hexTable['D'] = 13;
193 hexTable['E'] = 14; hexTable['F'] = 15;
194 hexTable['a'] = 10; hexTable['b'] = 11;
195 hexTable['c'] = 12; hexTable['d'] = 13;
196 hexTable['e'] = 14; hexTable['f'] = 15;
197
198 /* delimiters of significance are flagged w/ negative value */
199 hexTable[' '] = -1; hexTable[','] = -1;
200 hexTable['}'] = -1; hexTable['\n'] = -1;
201 hexTable['\t'] = -1;
202
203 hex_initialized = 1;
204 }
205
206 /*
207 * read next hex value in the input stream, return -1 if EOF
208 */
209 static int NextInt (FILE *fstream)
210 {
211 int ch;
212 int value = 0;
213 int gotone = 0;
214 int done = 0;
215
216 /* loop, accumulate hex value until find delimiter */
217 /* skip any initial delimiters found in read stream */
218
219 while (!done) {
220 ch = getc(fstream);
221 if (ch == EOF) {
222 value = -1;
223 done++;
224 } else {
225 /* trim high bits, check type and accumulate */
226 ch &= 0xff;
227 if (isascii(ch) && isxdigit(ch)) {
228 value = (value << 4) + hexTable[ch];
229 gotone++;
230 } else if ((hexTable[ch]) < 0 && gotone)
231 done++;
232 }
233 }
234 return value;
235 }
236
237
238 /*
239 * The data returned by the following routine is always in left-most byte
240 * first and left-most bit first. If it doesn't return BitmapSuccess then
241 * its arguments won't have been touched. This routine should look as much
242 * like the Xlib routine XReadBitmapfile as possible.
243 */
244 int XmuReadBitmapData (
245 FILE *fstream, /* handle on file */
246 unsigned int *width, /* RETURNED */
247 unsigned int *height, /* RETURNED */
248 unsigned char **datap, /* RETURNED */
249 int *x_hot, int *y_hot) /* RETURNED */
250 {
251 unsigned char *data = NULL; /* working variable */
252 char line[MAX_SIZE]; /* input line from file */
253 int size; /* number of bytes of data */
254 char name_and_type[MAX_SIZE]; /* an input line */
255 char *type; /* for parsing */
256 int value; /* from an input line */
257 int version10p; /* boolean, old format */
258 int padding; /* to handle alignment */
259 int bytes_per_line; /* per scanline of data */
260 unsigned int ww = 0; /* width */
261 unsigned int hh = 0; /* height */
262 int hx = -1; /* x hotspot */
263 int hy = -1; /* y hotspot */
264
265 #ifndef Xmalloc
266 #define Xmalloc(size) malloc(size)
267 #endif
268
269 /* first time initialization */
270 if (!hex_initialized) initHexTable();
271
272 /* error cleanup and return macro */
273 #define RETURN(code) { if (data) free (data); return code; }
274
275 while (fgets(line, MAX_SIZE, fstream)) {
276 if (strlen(line) == MAX_SIZE-1) {
277 RETURN (BitmapFileInvalid);
278 }
279 if (sscanf(line,"#define %s %d",name_and_type,&value) == 2) {
280 if (!(type = strrchr(name_and_type, '_')))
281 type = name_and_type;
282 else
283 type++;
284
285 if (!strcmp("width", type))
286 ww = (unsigned int) value;
287 if (!strcmp("height", type))
288 hh = (unsigned int) value;
289 if (!strcmp("hot", type)) {
290 if (type-- == name_and_type || type-- == name_and_type)
291 continue;
292 if (!strcmp("x_hot", type))
293 hx = value;
294 if (!strcmp("y_hot", type))
295 hy = value;
296 }
297 continue;
298 }
299
300 if (sscanf(line, "static short %s = {", name_and_type) == 1)
301 version10p = 1;
302 else if (sscanf(line,"static unsigned char %s = {",name_and_type) == 1)
303 version10p = 0;
304 else if (sscanf(line, "static char %s = {", name_and_type) == 1)
305 version10p = 0;
306 else
307 continue;
308
309 if (!(type = strrchr(name_and_type, '_')))
310 type = name_and_type;
311 else
312 type++;
313
314 if (strcmp("bits[]", type))
315 continue;
316
317 if (!ww || !hh)
318 RETURN (BitmapFileInvalid);
319
320 if ((ww % 16) && ((ww % 16) < 9) && version10p)
321 padding = 1;
322 else
323 padding = 0;
324
325 bytes_per_line = (ww+7)/8 + padding;
326
327 size = bytes_per_line * hh;
328 data = (unsigned char *) Xmalloc ((unsigned int) size);
329 if (!data)
330 RETURN (BitmapNoMemory);
331
332 if (version10p) {
333 unsigned char *ptr;
334 int bytes;
335
336 for (bytes=0, ptr=data; bytes<size; (bytes += 2)) {
337 if ((value = NextInt(fstream)) < 0)
338 RETURN (BitmapFileInvalid);
339 *(ptr++) = value;
340 if (!padding || ((bytes+2) % bytes_per_line))
341 *(ptr++) = value >> 8;
342 }
343 } else {
344 unsigned char *ptr;
345 int bytes;
346
347 for (bytes=0, ptr=data; bytes<size; bytes++, ptr++) {
348 if ((value = NextInt(fstream)) < 0)
349 RETURN (BitmapFileInvalid);
350 *ptr=value;
351 }
352 }
353 break;
354 } /* end while */
355
356 if (data == NULL) {
357 RETURN (BitmapFileInvalid);
358 }
359
360 *datap = data;
361 data = NULL;
362 *width = ww;
363 *height = hh;
364 if (x_hot) *x_hot = hx;
365 if (y_hot) *y_hot = hy;
366
367 RETURN (BitmapSuccess);
368 }
369
370
371 int XmuReadBitmapDataFromFile (const char *filename,
372 /* Remaining args are RETURNED */
373 unsigned int *width,
374 unsigned int *height,
375 unsigned char **datap,
376 int *x_hot, int *y_hot)
377 {
378 FILE *fstream;
379 int status;
380
381 if ((fstream = fopen (filename, "r")) == NULL) {
382 return BitmapOpenFailed;
383 }
384 status = XmuReadBitmapData (fstream, width, height, datap, x_hot, y_hot);
385 fclose (fstream);
386 return status;
387 }
388 164
389 /* 165 /*
390 * XmuPrintDefaultErrorMessage - print a nice error that looks like the usual 166 * XmuPrintDefaultErrorMessage - print a nice error that looks like the usual
391 * message. Return 1 if the caller should consider exiting, else 0. 167 * message. Return 1 if the caller should consider exiting, else 0.
392 */ 168 */