608
|
1 /* Routines shared between window-system backends for glyph objects.
|
563
|
2 Copyright (C) 1993, 1994 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995 Board of Trustees, University of Illinois.
|
|
4 Copyright (C) 1995 Tinker Systems
|
771
|
5 Copyright (C) 1995, 1996, 2001 Ben Wing
|
563
|
6 Copyright (C) 1995 Sun Microsystems
|
|
7 Copyright (C) 1998, 1999, 2000 Andy Piper.
|
|
8
|
|
9 This file is part of XEmacs.
|
|
10
|
|
11 XEmacs is free software; you can redistribute it and/or modify it
|
|
12 under the terms of the GNU General Public License as published by the
|
|
13 Free Software Foundation; either version 2, or (at your option) any
|
|
14 later version.
|
|
15
|
|
16 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
19 for more details.
|
|
20
|
|
21 You should have received a copy of the GNU General Public License
|
|
22 along with XEmacs; see the file COPYING. If not, write to
|
|
23 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
24 Boston, MA 02111-1307, USA. */
|
|
25
|
|
26 /* Synched up with: Not in FSF. */
|
|
27
|
|
28 /* taken from glyphs-*.c
|
|
29 HINT HINT HINT Bill Perry: Please put code here and avoid massive
|
|
30 duplication in *-gtk.c!!! */
|
|
31
|
|
32 #include <config.h>
|
|
33 #include "lisp.h"
|
|
34 #include "lstream.h"
|
|
35
|
|
36 #include "window.h"
|
|
37 #include "elhash.h"
|
|
38 #include "buffer.h"
|
|
39 #include "frame.h"
|
|
40 #include "insdel.h"
|
|
41 #include "opaque.h"
|
|
42 #include "sysdep.h"
|
|
43 #include "sysfile.h"
|
|
44 #include "faces.h"
|
|
45 #include "imgproc.h"
|
|
46
|
|
47 Lisp_Object Q_resource_type, Q_resource_id;
|
|
48
|
|
49 void
|
|
50 shared_resource_validate (Lisp_Object instantiator)
|
|
51 {
|
|
52 if ((NILP (find_keyword_in_vector (instantiator, Q_file))
|
|
53 &&
|
|
54 NILP (find_keyword_in_vector (instantiator, Q_resource_id)))
|
|
55 ||
|
|
56 NILP (find_keyword_in_vector (instantiator, Q_resource_type)))
|
|
57 sferror ("Must supply :file, :resource-id and :resource-type",
|
|
58 instantiator);
|
|
59 }
|
|
60
|
|
61
|
|
62 Lisp_Object
|
|
63 shared_resource_normalize (Lisp_Object inst, Lisp_Object console_type,
|
|
64 Lisp_Object dest_mask, Lisp_Object tag)
|
|
65 {
|
|
66 /* This function can call lisp */
|
|
67 Lisp_Object file = Qnil;
|
|
68 struct gcpro gcpro1, gcpro2;
|
|
69 Lisp_Object alist = Qnil;
|
|
70
|
|
71 GCPRO2 (file, alist);
|
|
72
|
|
73 file = potential_pixmap_file_instantiator (inst, Q_file, Q_data,
|
|
74 console_type);
|
|
75
|
|
76 if (CONSP (file)) /* failure locating filename */
|
|
77 signal_double_image_error ("Opening pixmap file",
|
|
78 "no such file or directory",
|
|
79 Fcar (file));
|
|
80
|
|
81 if (NILP (file)) /* no conversion necessary */
|
|
82 RETURN_UNGCPRO (inst);
|
|
83
|
|
84 alist = tagged_vector_to_alist (inst);
|
|
85
|
|
86 {
|
|
87 alist = remassq_no_quit (Q_file, alist);
|
|
88 alist = Fcons (Fcons (Q_file, file), alist);
|
|
89 }
|
|
90
|
|
91 {
|
|
92 Lisp_Object result = alist_to_tagged_vector (tag, alist);
|
|
93 free_alist (alist);
|
|
94 RETURN_UNGCPRO (result);
|
|
95 }
|
|
96 }
|
|
97
|
608
|
98 /* Originally from xmu.c, but is now shared across X11, GTK, and MSW. */
|
|
99 /*
|
|
100 * Based on an optimized version provided by Jim Becker, August 5, 1988.
|
|
101 */
|
|
102
|
|
103
|
|
104 #ifndef BitmapSuccess
|
|
105 #define BitmapSuccess 0
|
|
106 #define BitmapOpenFailed 1
|
|
107 #define BitmapFileInvalid 2
|
|
108 #define BitmapNoMemory 3
|
|
109 #endif
|
|
110
|
|
111 #define MAX_SIZE 255
|
|
112
|
|
113 /* shared data for the image read/parse logic */
|
|
114 static short hexTable[256]; /* conversion value */
|
|
115 static int hex_initialized; /* easier to fill in at run time */
|
|
116
|
|
117
|
|
118 /*
|
|
119 * Table index for the hex values. Initialized once, first time.
|
|
120 * Used for translation value or delimiter significance lookup.
|
|
121 */
|
|
122 static void initHexTable (void)
|
|
123 {
|
|
124 /*
|
|
125 * We build the table at run time for several reasons:
|
|
126 *
|
|
127 * 1. portable to non-ASCII machines.
|
|
128 * 2. still reentrant since we set the init flag after setting table.
|
|
129 * 3. easier to extend.
|
|
130 * 4. less prone to bugs.
|
|
131 */
|
|
132 hexTable['0'] = 0; hexTable['1'] = 1;
|
|
133 hexTable['2'] = 2; hexTable['3'] = 3;
|
|
134 hexTable['4'] = 4; hexTable['5'] = 5;
|
|
135 hexTable['6'] = 6; hexTable['7'] = 7;
|
|
136 hexTable['8'] = 8; hexTable['9'] = 9;
|
|
137 hexTable['A'] = 10; hexTable['B'] = 11;
|
|
138 hexTable['C'] = 12; hexTable['D'] = 13;
|
|
139 hexTable['E'] = 14; hexTable['F'] = 15;
|
|
140 hexTable['a'] = 10; hexTable['b'] = 11;
|
|
141 hexTable['c'] = 12; hexTable['d'] = 13;
|
|
142 hexTable['e'] = 14; hexTable['f'] = 15;
|
|
143
|
|
144 /* delimiters of significance are flagged w/ negative value */
|
|
145 hexTable[' '] = -1; hexTable[','] = -1;
|
|
146 hexTable['}'] = -1; hexTable['\n'] = -1;
|
|
147 hexTable['\t'] = -1;
|
|
148
|
|
149 hex_initialized = 1;
|
|
150 }
|
|
151
|
|
152 /*
|
|
153 * read next hex value in the input stream, return -1 if EOF
|
|
154 */
|
|
155 static int NextInt (FILE *fstream)
|
|
156 {
|
|
157 int ch;
|
|
158 int value = 0;
|
|
159 int gotone = 0;
|
|
160 int done = 0;
|
|
161
|
|
162 /* loop, accumulate hex value until find delimiter */
|
|
163 /* skip any initial delimiters found in read stream */
|
|
164
|
|
165 while (!done) {
|
|
166 ch = getc(fstream);
|
|
167 if (ch == EOF) {
|
|
168 value = -1;
|
|
169 done++;
|
|
170 } else {
|
|
171 /* trim high bits, check type and accumulate */
|
|
172 ch &= 0xff;
|
|
173 if (isascii(ch) && isxdigit(ch)) {
|
|
174 value = (value << 4) + hexTable[ch];
|
|
175 gotone++;
|
|
176 } else if ((hexTable[ch]) < 0 && gotone)
|
|
177 done++;
|
|
178 }
|
|
179 }
|
|
180 return value;
|
|
181 }
|
|
182
|
|
183
|
|
184 /*
|
|
185 * The data returned by the following routine is always in left-most byte
|
|
186 * first and left-most bit first. If it doesn't return BitmapSuccess then
|
|
187 * its arguments won't have been touched. This routine should look as much
|
|
188 * like the Xlib routine XReadBitmapfile as possible.
|
|
189 */
|
611
|
190 static int
|
647
|
191 read_bitmap_data (FILE *fstream, int *width, int *height, UChar_Binary **datap,
|
611
|
192 int *x_hot, int *y_hot)
|
608
|
193 {
|
|
194 UChar_Binary *data = NULL; /* working variable */
|
647
|
195 Char_ASCII line[MAX_SIZE]; /* input line from file */
|
608
|
196 int size; /* number of bytes of data */
|
647
|
197 Char_ASCII name_and_type[MAX_SIZE]; /* an input line */
|
|
198 Char_ASCII *type; /* for parsing */
|
608
|
199 int value; /* from an input line */
|
|
200 int version10p; /* boolean, old format */
|
|
201 int padding; /* to handle alignment */
|
|
202 int bytes_per_line; /* per scanline of data */
|
647
|
203 int ww = 0; /* width */
|
|
204 int hh = 0; /* height */
|
608
|
205 int hx = -1; /* x hotspot */
|
|
206 int hy = -1; /* y hotspot */
|
|
207
|
|
208 #ifndef Xmalloc
|
|
209 #define Xmalloc(size) malloc(size)
|
|
210 #endif
|
|
211
|
|
212 /* first time initialization */
|
|
213 if (!hex_initialized) initHexTable();
|
|
214
|
|
215 /* error cleanup and return macro */
|
|
216 #define RETURN(code) { if (data) free (data); return code; }
|
|
217
|
|
218 while (fgets(line, MAX_SIZE, fstream)) {
|
|
219 if (strlen(line) == MAX_SIZE-1) {
|
|
220 RETURN (BitmapFileInvalid);
|
|
221 }
|
|
222 if (sscanf(line,"#define %s %d",name_and_type,&value) == 2) {
|
|
223 if (!(type = strrchr(name_and_type, '_')))
|
|
224 type = name_and_type;
|
|
225 else
|
|
226 type++;
|
|
227
|
|
228 if (!strcmp("width", type))
|
647
|
229 ww = value;
|
608
|
230 if (!strcmp("height", type))
|
647
|
231 hh = value;
|
608
|
232 if (!strcmp("hot", type)) {
|
|
233 if (type-- == name_and_type || type-- == name_and_type)
|
|
234 continue;
|
|
235 if (!strcmp("x_hot", type))
|
|
236 hx = value;
|
|
237 if (!strcmp("y_hot", type))
|
|
238 hy = value;
|
|
239 }
|
|
240 continue;
|
|
241 }
|
|
242
|
|
243 if (sscanf(line, "static short %s = {", name_and_type) == 1)
|
|
244 version10p = 1;
|
|
245 else if (sscanf(line,"static unsigned char %s = {",name_and_type) == 1)
|
|
246 version10p = 0;
|
|
247 else if (sscanf(line, "static char %s = {", name_and_type) == 1)
|
|
248 version10p = 0;
|
|
249 else
|
|
250 continue;
|
|
251
|
|
252 if (!(type = strrchr(name_and_type, '_')))
|
|
253 type = name_and_type;
|
|
254 else
|
|
255 type++;
|
|
256
|
|
257 if (strcmp("bits[]", type))
|
|
258 continue;
|
|
259
|
|
260 if (!ww || !hh)
|
|
261 RETURN (BitmapFileInvalid);
|
|
262
|
|
263 if ((ww % 16) && ((ww % 16) < 9) && version10p)
|
|
264 padding = 1;
|
|
265 else
|
|
266 padding = 0;
|
|
267
|
|
268 bytes_per_line = (ww+7)/8 + padding;
|
|
269
|
|
270 size = bytes_per_line * hh;
|
|
271 data = (UChar_Binary *) Xmalloc ((unsigned int) size);
|
|
272 if (!data)
|
|
273 RETURN (BitmapNoMemory);
|
|
274
|
|
275 if (version10p) {
|
|
276 UChar_Binary *ptr;
|
|
277 int bytes;
|
|
278
|
|
279 for (bytes=0, ptr=data; bytes<size; (bytes += 2)) {
|
|
280 if ((value = NextInt(fstream)) < 0)
|
|
281 RETURN (BitmapFileInvalid);
|
|
282 *(ptr++) = value;
|
|
283 if (!padding || ((bytes+2) % bytes_per_line))
|
|
284 *(ptr++) = value >> 8;
|
|
285 }
|
|
286 } else {
|
|
287 UChar_Binary *ptr;
|
|
288 int bytes;
|
|
289
|
|
290 for (bytes=0, ptr=data; bytes<size; bytes++, ptr++) {
|
|
291 if ((value = NextInt(fstream)) < 0)
|
|
292 RETURN (BitmapFileInvalid);
|
|
293 *ptr=value;
|
|
294 }
|
|
295 }
|
|
296 break;
|
|
297 } /* end while */
|
|
298
|
|
299 if (data == NULL) {
|
|
300 RETURN (BitmapFileInvalid);
|
|
301 }
|
|
302
|
|
303 *datap = data;
|
|
304 data = NULL;
|
|
305 *width = ww;
|
|
306 *height = hh;
|
|
307 if (x_hot) *x_hot = hx;
|
|
308 if (y_hot) *y_hot = hy;
|
|
309
|
|
310 RETURN (BitmapSuccess);
|
|
311 }
|
|
312
|
|
313
|
611
|
314 int
|
771
|
315 read_bitmap_data_from_file (Lisp_Object filename,
|
611
|
316 /* Remaining args are RETURNED */
|
647
|
317 int *width,
|
|
318 int *height,
|
611
|
319 UChar_Binary **datap,
|
|
320 int *x_hot, int *y_hot)
|
608
|
321 {
|
771
|
322 FILE *fstream;
|
|
323 int status;
|
|
324 Extbyte *fileext;
|
608
|
325
|
771
|
326 LISP_STRING_TO_EXTERNAL (filename, fileext, Qnative);
|
|
327 if ((fstream = fopen (fileext, "r")) == NULL)
|
|
328 return BitmapOpenFailed;
|
|
329 status = read_bitmap_data (fstream, width, height, datap, x_hot, y_hot);
|
|
330 retry_fclose (fstream);
|
|
331 return status;
|
608
|
332 }
|
|
333
|
563
|
334 void
|
|
335 syms_of_glyphs_shared (void)
|
|
336 {
|
|
337 DEFKEYWORD (Q_resource_id);
|
|
338 DEFKEYWORD (Q_resource_type);
|
|
339 }
|