0
|
1 /* X-specific Lisp objects.
|
|
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.
|
|
5 Copyright (C) 1995, 1996 Ben Wing.
|
|
6 Copyright (C) 1995 Sun Microsystems, Inc.
|
|
7
|
|
8 This file is part of XEmacs.
|
|
9
|
|
10 XEmacs is free software; you can redistribute it and/or modify it
|
|
11 under the terms of the GNU General Public License as published by the
|
|
12 Free Software Foundation; either version 2, or (at your option) any
|
|
13 later version.
|
|
14
|
|
15 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
18 for more details.
|
|
19
|
|
20 You should have received a copy of the GNU General Public License
|
|
21 along with XEmacs; see the file COPYING. If not, write to
|
|
22 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
23 Boston, MA 02111-1307, USA. */
|
|
24
|
|
25 /* Synched up with: Not in FSF. */
|
|
26
|
|
27 /* Authors: Jamie Zawinski, Chuck Thompson, Ben Wing */
|
|
28
|
|
29 #include <config.h>
|
|
30 #include "lisp.h"
|
|
31
|
|
32 #include "console-x.h"
|
|
33 #include "objects-x.h"
|
|
34
|
|
35 #include "buffer.h"
|
|
36 #include "device.h"
|
|
37 #include "insdel.h"
|
|
38
|
|
39 int handle_nonfull_spec_fonts;
|
|
40
|
|
41
|
|
42 /************************************************************************/
|
|
43 /* color instances */
|
|
44 /************************************************************************/
|
|
45
|
|
46 /* Replacement for XAllocColor() that tries to return the nearest
|
219
|
47 available color if the colormap is full. Original was from FSFmacs,
|
|
48 but rewritten by Jareth Hein <jareth@camelot-soft.com> 97/11/25 */
|
0
|
49
|
219
|
50 /* Return value is 1 for normal success, 2 for nearest color success,
|
|
51 3 for Non-deallocable sucess, and 0 for absolute failure (shouldn't
|
|
52 happen?) */
|
0
|
53 int
|
219
|
54 allocate_nearest_color (Display *display, Colormap colormap, Visual *visual,
|
0
|
55 XColor *color_def)
|
|
56 {
|
|
57 int status;
|
|
58
|
219
|
59 if (visual->class == DirectColor || visual->class == TrueColor)
|
0
|
60 {
|
219
|
61 if (XAllocColor (display, colormap, color_def) != 0)
|
|
62 {
|
|
63 status = 1;
|
|
64 }
|
|
65 else
|
|
66 {
|
|
67 /* We're dealing with a TrueColor/DirectColor visual, so play games
|
|
68 with the RGB values in the XColor struct. */
|
|
69 /* ### JH: I'm not sure how a call to XAllocColor can fail in a
|
|
70 TrueColor or DirectColor visual, so I will just reformat the
|
|
71 request to match the requirements of the visual, and re-issue
|
|
72 the request. If this fails for anybody, I wanna know about it
|
|
73 so I can come up with a better plan */
|
0
|
74
|
219
|
75 unsigned long rshift,gshift,bshift,rbits,gbits,bbits,junk;
|
|
76 junk = visual->red_mask;
|
|
77 rshift = 0;
|
|
78 while ((junk & 0x1) == 0) {
|
|
79 junk = junk >> 1;
|
|
80 rshift ++;
|
|
81 }
|
|
82 rbits = 0;
|
|
83 while (junk != 0) {
|
|
84 junk = junk >> 1;
|
|
85 rbits++;
|
|
86 }
|
|
87 junk = visual->green_mask;
|
|
88 gshift = 0;
|
|
89 while ((junk & 0x1) == 0) {
|
|
90 junk = junk >> 1;
|
|
91 gshift ++;
|
|
92 }
|
|
93 gbits = 0;
|
|
94 while (junk != 0) {
|
|
95 junk = junk >> 1;
|
|
96 gbits++;
|
|
97 }
|
|
98 junk = visual->blue_mask;
|
|
99 bshift = 0;
|
|
100 while ((junk & 0x1) == 0) {
|
|
101 junk = junk >> 1;
|
|
102 bshift ++;
|
|
103 }
|
|
104 bbits = 0;
|
|
105 while (junk != 0) {
|
|
106 junk = junk >> 1;
|
|
107 bbits++;
|
|
108 }
|
|
109
|
|
110 color_def->red = color_def->red >> (16 - rbits);
|
|
111 color_def->green = color_def->green >> (16 - gbits);
|
|
112 color_def->blue = color_def->blue >> (16 - bbits);
|
|
113 if (XAllocColor (display, colormap, color_def) != 0)
|
|
114 status = 1;
|
|
115 else
|
|
116 {
|
|
117 /* ### JH: I'm punting here, knowing that doing this will at
|
|
118 least draw the color correctly. However, unless we convert
|
|
119 all of the functions that allocate colors (graphics
|
|
120 libraries, etc) to use this function doing this is very
|
|
121 likely to cause problems later... */
|
|
122 color_def->pixel = (color_def->red << rshift) | (color_def->green << gshift) |
|
|
123 (color_def->blue << bshift);
|
|
124 status = 3;
|
0
|
125 }
|
|
126 }
|
|
127 }
|
219
|
128 else
|
|
129 {
|
|
130 if (XAllocColor (display, colormap, color_def) != 0)
|
|
131 status = 1;
|
|
132 else
|
|
133 {
|
|
134 /* If we got to this point, the colormap is full, so we're
|
|
135 going to try and get the next closest color. The algorithm used
|
|
136 is a least-squares matching, which is what X uses for closest
|
|
137 color matching with StaticColor visuals. */
|
|
138 XColor *cells;
|
|
139 /* JH: I can't believe there's no way to go backwards from a
|
|
140 colormap ID and get its visual and number of entries, but X
|
|
141 apparently isn't built that way... */
|
|
142 int no_cells = visual->map_entries;
|
|
143 int nearest;
|
|
144 long nearest_delta, trial_delta;
|
|
145 int x;
|
0
|
146
|
219
|
147 cells = alloca_array (XColor, no_cells);
|
|
148
|
|
149 for (x = 0; x < no_cells; x++)
|
|
150 cells[x].pixel = x;
|
|
151
|
|
152 /* read the current colormap */
|
|
153 XQueryColors (display, colormap, cells, no_cells);
|
|
154 nearest = 0;
|
|
155 /* I'm assuming CSE so I'm not going to condense this. */
|
|
156 nearest_delta = ((((color_def->red >> 8) - (cells[0].red >> 8))
|
|
157 * ((color_def->red >> 8) - (cells[0].red >> 8)))
|
|
158 +
|
|
159 (((color_def->green >> 8) - (cells[0].green >> 8))
|
|
160 * ((color_def->green >> 8) - (cells[0].green >> 8)))
|
|
161 +
|
|
162 (((color_def->blue >> 8) - (cells[0].blue >> 8))
|
|
163 * ((color_def->blue >> 8) - (cells[0].blue >> 8))));
|
|
164 for (x = 1; x < no_cells; x++)
|
|
165 {
|
|
166 trial_delta = ((((color_def->red >> 8) - (cells[x].red >> 8))
|
|
167 * ((color_def->red >> 8) - (cells[x].red >> 8)))
|
|
168 +
|
|
169 (((color_def->green >> 8) - (cells[x].green >> 8))
|
|
170 * ((color_def->green >> 8) - (cells[x].green >> 8)))
|
|
171 +
|
|
172 (((color_def->blue >> 8) - (cells[x].blue >> 8))
|
|
173 * ((color_def->blue >> 8) - (cells[x].blue >> 8))));
|
|
174 if (trial_delta < nearest_delta)
|
|
175 {
|
|
176 nearest = x;
|
|
177 nearest_delta = trial_delta;
|
|
178 }
|
|
179 }
|
|
180 color_def->red = cells[nearest].red;
|
|
181 color_def->green = cells[nearest].green;
|
|
182 color_def->blue = cells[nearest].blue;
|
|
183 if (XAllocColor (display, colormap, color_def) != 0)
|
|
184 status = 2;
|
|
185 else
|
|
186 status = 0; /* JH: how does this happen??? DOES this happen??? */
|
|
187 }
|
|
188 }
|
0
|
189 return status;
|
|
190 }
|
|
191
|
|
192 int
|
|
193 x_parse_nearest_color (struct device *d, XColor *color, Bufbyte *name,
|
|
194 Bytecount len, Error_behavior errb)
|
|
195 {
|
|
196 Display *dpy;
|
|
197 Screen *xs;
|
|
198 Colormap cmap;
|
219
|
199 Visual *visual;
|
0
|
200 int result;
|
185
|
201
|
0
|
202 dpy = DEVICE_X_DISPLAY (d);
|
|
203 xs = DefaultScreenOfDisplay (dpy);
|
219
|
204 cmap = DEVICE_X_COLORMAP(d);
|
|
205 visual = DEVICE_X_VISUAL (d);
|
185
|
206
|
0
|
207 memset (color, 0, sizeof (*color));
|
|
208 {
|
|
209 CONST Extbyte *extname;
|
|
210 Extcount extnamelen;
|
|
211
|
|
212 GET_CHARPTR_EXT_BINARY_DATA_ALLOCA (name, len, extname, extnamelen);
|
|
213 result = XParseColor (dpy, cmap, (char *) extname, color);
|
|
214 }
|
|
215 if (!result)
|
|
216 {
|
|
217 maybe_signal_simple_error ("unrecognized color", make_string (name, len),
|
|
218 Qcolor, errb);
|
|
219 return 0;
|
|
220 }
|
219
|
221 result = allocate_nearest_color (dpy, cmap, visual, color);
|
0
|
222 if (!result)
|
|
223 {
|
|
224 maybe_signal_simple_error ("couldn't allocate color",
|
|
225 make_string (name, len), Qcolor, errb);
|
|
226 return 0;
|
|
227 }
|
|
228
|
219
|
229 return result;
|
0
|
230 }
|
|
231
|
|
232 static int
|
|
233 x_initialize_color_instance (struct Lisp_Color_Instance *c, Lisp_Object name,
|
|
234 Lisp_Object device, Error_behavior errb)
|
|
235 {
|
|
236 XColor color;
|
|
237 int result;
|
|
238
|
|
239 result = x_parse_nearest_color (XDEVICE (device), &color,
|
14
|
240 XSTRING_DATA (name),
|
|
241 XSTRING_LENGTH (name),
|
0
|
242 errb);
|
|
243
|
|
244 if (!result)
|
|
245 return 0;
|
|
246
|
|
247 /* Don't allocate the data until we're sure that we will succeed,
|
|
248 or the finalize method may get fucked. */
|
185
|
249 c->data = xnew (struct x_color_instance_data);
|
219
|
250 if (result == 3)
|
|
251 COLOR_INSTANCE_X_DEALLOC (c) = 0;
|
|
252 else
|
|
253 COLOR_INSTANCE_X_DEALLOC (c) = 1;
|
0
|
254 COLOR_INSTANCE_X_COLOR (c) = color;
|
|
255 return 1;
|
|
256 }
|
|
257
|
|
258 static void
|
|
259 x_print_color_instance (struct Lisp_Color_Instance *c,
|
|
260 Lisp_Object printcharfun,
|
|
261 int escapeflag)
|
|
262 {
|
|
263 char buf[100];
|
|
264 XColor color = COLOR_INSTANCE_X_COLOR (c);
|
|
265 sprintf (buf, " %ld=(%X,%X,%X)",
|
|
266 color.pixel, color.red, color.green, color.blue);
|
|
267 write_c_string (buf, printcharfun);
|
|
268 }
|
|
269
|
|
270 static void
|
|
271 x_finalize_color_instance (struct Lisp_Color_Instance *c)
|
|
272 {
|
|
273 if (c->data)
|
|
274 {
|
|
275 if (DEVICE_LIVE_P (XDEVICE (c->device)))
|
|
276 {
|
219
|
277 if (COLOR_INSTANCE_X_DEALLOC (c))
|
|
278 {
|
|
279 XFreeColors (DEVICE_X_DISPLAY (XDEVICE (c->device)), DEVICE_X_COLORMAP (XDEVICE (c->device)),
|
|
280 &COLOR_INSTANCE_X_COLOR (c).pixel, 1, 0);
|
|
281 }
|
0
|
282 }
|
|
283 xfree (c->data);
|
|
284 c->data = 0;
|
|
285 }
|
|
286 }
|
|
287
|
|
288 /* Color instances are equal if they resolve to the same color on the
|
|
289 screen (have the same RGB values). I imagine that
|
219
|
290 "same RGB values" == "same cell in the colormap." Arguably we should
|
|
291 be comparing their names or pixel values instead. */
|
0
|
292
|
|
293 static int
|
|
294 x_color_instance_equal (struct Lisp_Color_Instance *c1,
|
|
295 struct Lisp_Color_Instance *c2,
|
|
296 int depth)
|
|
297 {
|
|
298 XColor color1 = COLOR_INSTANCE_X_COLOR (c1);
|
|
299 XColor color2 = COLOR_INSTANCE_X_COLOR (c2);
|
|
300 return ((color1.red == color2.red) &&
|
|
301 (color1.green == color2.green) &&
|
|
302 (color1.blue == color2.blue));
|
|
303 }
|
|
304
|
|
305 static unsigned long
|
|
306 x_color_instance_hash (struct Lisp_Color_Instance *c, int depth)
|
|
307 {
|
|
308 XColor color = COLOR_INSTANCE_X_COLOR (c);
|
|
309 return HASH3 (color.red, color.green, color.blue);
|
|
310 }
|
|
311
|
|
312 static Lisp_Object
|
|
313 x_color_instance_rgb_components (struct Lisp_Color_Instance *c)
|
|
314 {
|
|
315 XColor color = COLOR_INSTANCE_X_COLOR (c);
|
185
|
316 return (list3 (make_int (color.red),
|
0
|
317 make_int (color.green),
|
|
318 make_int (color.blue)));
|
|
319 }
|
|
320
|
|
321 static int
|
|
322 x_valid_color_name_p (struct device *d, Lisp_Object color)
|
|
323 {
|
|
324 XColor c;
|
|
325 Display *dpy = DEVICE_X_DISPLAY (d);
|
219
|
326 Colormap cmap = DEVICE_X_COLORMAP (d);
|
|
327
|
0
|
328 CONST char *extname;
|
185
|
329
|
0
|
330 GET_C_STRING_CTEXT_DATA_ALLOCA (color, extname);
|
|
331
|
219
|
332 return XParseColor (dpy, cmap,
|
0
|
333 extname, &c);
|
|
334 }
|
|
335
|
|
336
|
|
337 /************************************************************************/
|
|
338 /* font instances */
|
|
339 /************************************************************************/
|
|
340
|
|
341 static int
|
|
342 x_initialize_font_instance (struct Lisp_Font_Instance *f, Lisp_Object name,
|
|
343 Lisp_Object device, Error_behavior errb)
|
|
344 {
|
|
345 Display *dpy;
|
|
346 XFontStruct *xf;
|
|
347 CONST char *extname;
|
185
|
348
|
0
|
349 dpy = DEVICE_X_DISPLAY (XDEVICE (device));
|
|
350 GET_C_STRING_CTEXT_DATA_ALLOCA (f->name, extname);
|
|
351 xf = XLoadQueryFont (dpy, extname);
|
185
|
352
|
0
|
353 if (!xf)
|
|
354 {
|
|
355 maybe_signal_simple_error ("couldn't load font", f->name,
|
|
356 Qfont, errb);
|
|
357 return 0;
|
|
358 }
|
185
|
359
|
0
|
360 if (!xf->max_bounds.width)
|
|
361 {
|
|
362 /* yes, this has been known to happen. */
|
|
363 XFreeFont (dpy, xf);
|
|
364 maybe_signal_simple_error ("X font is too small", f->name,
|
|
365 Qfont, errb);
|
|
366 return 0;
|
|
367 }
|
185
|
368
|
0
|
369 /* Don't allocate the data until we're sure that we will succeed,
|
|
370 or the finalize method may get fucked. */
|
185
|
371 f->data = xnew (struct x_font_instance_data);
|
0
|
372 FONT_INSTANCE_X_TRUENAME (f) = Qnil;
|
|
373 FONT_INSTANCE_X_FONT (f) = xf;
|
|
374 f->ascent = xf->ascent;
|
|
375 f->descent = xf->descent;
|
|
376 f->height = xf->ascent + xf->descent;
|
|
377 {
|
80
|
378 /* following change suggested by Ted Phelps <phelps@dstc.edu.au> */
|
|
379 unsigned int def_char = 'n'; /*xf->default_char;*/
|
0
|
380 int byte1, byte2;
|
|
381
|
|
382 once_more:
|
|
383 byte1 = def_char >> 8;
|
|
384 byte2 = def_char & 0xFF;
|
|
385
|
|
386 if (xf->per_char)
|
|
387 {
|
|
388 /* Old versions of the R5 font server have garbage (>63k) as
|
|
389 def_char. 'n' might not be a valid character. */
|
|
390 if (byte1 < xf->min_byte1 || byte1 > xf->max_byte1 ||
|
|
391 byte2 < xf->min_char_or_byte2 || byte2 > xf->max_char_or_byte2)
|
|
392 f->width = 0;
|
|
393 else
|
|
394 f->width = xf->per_char[(byte1 - xf->min_byte1) *
|
|
395 (xf->max_char_or_byte2 -
|
|
396 xf->min_char_or_byte2 + 1) +
|
|
397 (byte2 - xf->min_char_or_byte2)].width;
|
|
398 }
|
|
399 else
|
|
400 f->width = xf->max_bounds.width;
|
185
|
401
|
0
|
402 /* Some fonts have a default char whose width is 0. This is no good.
|
|
403 If that's the case, first try 'n' as the default char, and if n has
|
|
404 0 width too (unlikely) then just use the max width. */
|
|
405 if (f->width == 0)
|
|
406 {
|
80
|
407 if (def_char == xf->default_char)
|
0
|
408 f->width = xf->max_bounds.width;
|
|
409 else
|
|
410 {
|
80
|
411 def_char = xf->default_char;
|
0
|
412 goto once_more;
|
|
413 }
|
|
414 }
|
|
415 }
|
|
416 /* If all characters don't exist then there could potentially be
|
|
417 0-width characters lurking out there. Not setting this flag
|
|
418 trips an optimization that would make them appear to have width
|
|
419 to redisplay. This is bad. So we set it if not all characters
|
|
420 have the same width or if not all characters are defined.
|
|
421 */
|
|
422 /* #### This sucks. There is a measurable performance increase
|
|
423 when using proportional width fonts if this flag is not set.
|
|
424 Unfortunately so many of the fucking X fonts are not fully
|
|
425 defined that we could almost just get rid of this damn flag and
|
|
426 make it an assertion. */
|
|
427 f->proportional_p = (xf->min_bounds.width != xf->max_bounds.width ||
|
|
428 (handle_nonfull_spec_fonts &&
|
|
429 !xf->all_chars_exist));
|
|
430
|
|
431 return 1;
|
|
432 }
|
|
433
|
|
434 static void
|
|
435 x_mark_font_instance (struct Lisp_Font_Instance *f,
|
|
436 void (*markobj) (Lisp_Object))
|
|
437 {
|
|
438 ((markobj) (FONT_INSTANCE_X_TRUENAME (f)));
|
|
439 }
|
|
440
|
|
441 static void
|
|
442 x_print_font_instance (struct Lisp_Font_Instance *f,
|
|
443 Lisp_Object printcharfun,
|
|
444 int escapeflag)
|
|
445 {
|
|
446 char buf[200];
|
|
447 sprintf (buf, " 0x%lx", (unsigned long) FONT_INSTANCE_X_FONT (f)->fid);
|
|
448 write_c_string (buf, printcharfun);
|
|
449 }
|
|
450
|
|
451 static void
|
|
452 x_finalize_font_instance (struct Lisp_Font_Instance *f)
|
|
453 {
|
185
|
454
|
0
|
455 if (f->data)
|
|
456 {
|
|
457 if (DEVICE_LIVE_P (XDEVICE (f->device)))
|
|
458 {
|
|
459 Display *dpy = DEVICE_X_DISPLAY (XDEVICE (f->device));
|
|
460
|
|
461 XFreeFont (dpy, FONT_INSTANCE_X_FONT (f));
|
|
462 }
|
|
463 xfree (f->data);
|
|
464 f->data = 0;
|
|
465 }
|
|
466 }
|
|
467
|
|
468 /* Determining the truename of a font is hard. (Big surprise.)
|
|
469
|
|
470 By "truename" we mean an XLFD-form name which contains no wildcards, yet
|
|
471 which resolves to *exactly* the same font as the one which we already have
|
|
472 the (probably wildcarded) name and `XFontStruct' of.
|
|
473
|
|
474 One might think that the first font returned by XListFonts would be the one
|
|
475 that XOpenFont would pick. Apparently this is the case on some servers,
|
|
476 but not on others. It would seem not to be specified.
|
|
477
|
|
478 The MIT R5 server sometimes appears to be picking the lexicographically
|
|
479 smallest font which matches the name (thus picking "adobe" fonts before
|
|
480 "bitstream" fonts even if the bitstream fonts are earlier in the path, and
|
|
481 also picking 100dpi adobe fonts over 75dpi adobe fonts even though the
|
|
482 75dpi are in the path earlier) but sometimes appears to be doing something
|
|
483 else entirely (for example, removing the bitsream fonts from the path will
|
|
484 cause the 75dpi adobe fonts to be used instead of the100dpi, even though
|
|
485 their relative positions in the path (and their names!) have not changed).
|
|
486
|
|
487 The documentation for XSetFontPath() seems to indicate that the order of
|
|
488 entries in the font path means something, but it's pretty noncommital about
|
|
489 it, and the spirit of the law is apparently not being obeyed...
|
|
490
|
|
491 All the fonts I've seen have a property named `FONT' which contains the
|
|
492 truename of the font. However, there are two problems with using this: the
|
|
493 first is that the X Protocol Document is quite explicit that all properties
|
|
494 are optional, so we can't depend on it being there. The second is that
|
|
495 it's concievable that this alleged truename isn't actually accessible as a
|
|
496 font, due to some difference of opinion between the font designers and
|
|
497 whoever installed the font on the system.
|
|
498
|
|
499 So, our first attempt is to look for a FONT property, and then verify that
|
|
500 the name there is a valid name by running XListFonts on it. There's still
|
|
501 the potential that this could be true but we could still be being lied to,
|
|
502 but that seems pretty remote.
|
|
503
|
|
504 Late breaking news: I've gotten reports that SunOS 4.1.3U1
|
|
505 with OpenWound 3.0 has a font whose truename is really
|
|
506 "-Adobe-Courier-Medium-R-Normal--12-120-75-75-M-70-ISO8859-1"
|
|
507 but whose FONT property contains "Courier".
|
|
508
|
|
509 So we disbelieve the FONT property unless it begins with a dash and
|
|
510 is more than 30 characters long. X Windows: The defacto substandard.
|
|
511 X Windows: Complex nonsolutions to simple nonproblems. X Windows:
|
|
512 Live the nightmare.
|
|
513
|
|
514 If the FONT property doesn't exist, then we try and construct an XLFD name
|
|
515 out of the other font properties (FOUNDRY, FAMILY_NAME, WEIGHT_NAME, etc).
|
|
516 This is necessary at least for some versions of OpenWound. But who knows
|
|
517 what the future will bring.
|
|
518
|
|
519 If that doesn't work, then we use XListFonts and either take the first font
|
|
520 (which I think is the most sensible thing) or we find the lexicographically
|
|
521 least, depending on whether the preprocessor constant `XOPENFONT_SORTS' is
|
|
522 defined. This sucks because the two behaviors are a property of the server
|
|
523 being used, not the architecture on which emacs has been compiled. Also,
|
|
524 as I described above, sorting isn't ALWAYS what the server does. Really it
|
|
525 does something seemingly random. There is no reliable way to win if the
|
|
526 FONT property isn't present.
|
|
527
|
|
528 Another possibility which I haven't bothered to implement would be to map
|
|
529 over all of the matching fonts and find the first one that has the same
|
|
530 character metrics as the font we already have loaded. Even if this didn't
|
|
531 return exactly the same font, it would at least return one whose characters
|
|
532 were the same sizes, which would probably be good enough.
|
|
533
|
|
534 More late-breaking news: on RS/6000 AIX 3.2.4, the expression
|
|
535 XLoadQueryFont (dpy, "-*-Fixed-Medium-R-*-*-*-130-75-75-*-*-ISO8859-1")
|
|
536 actually returns the font
|
|
537 -Misc-Fixed-Medium-R-Normal--13-120-75-75-C-80-ISO8859-1
|
|
538 which is crazy, because that font doesn't even match that pattern! It is
|
|
539 also not included in the output produced by `xlsfonts' with that pattern.
|
|
540
|
|
541 So this is yet another example of XListFonts() and XOpenFont() using
|
|
542 completely different algorithms. This, however, is a goofier example of
|
185
|
543 this bug, because in this case, it's not just the search order that is
|
0
|
544 different -- the sets don't even intersect.
|
|
545
|
|
546 If anyone has any better ideas how to do this, or any insights on what it is
|
|
547 that the various servers are actually doing, please let me know! -- jwz. */
|
|
548
|
|
549 static int
|
|
550 valid_x_font_name_p (Display *dpy, char *name)
|
|
551 {
|
|
552 /* Maybe this should be implemented by callign XLoadFont and trapping
|
|
553 the error. That would be a lot of work, and wasteful as hell, but
|
|
554 might be more correct.
|
|
555 */
|
|
556 int nnames = 0;
|
|
557 char **names = 0;
|
|
558 if (! name)
|
|
559 return 0;
|
|
560 names = XListFonts (dpy, name, 1, &nnames);
|
|
561 if (names)
|
|
562 XFreeFontNames (names);
|
|
563 return (nnames != 0);
|
|
564 }
|
|
565
|
|
566 static char *
|
|
567 truename_via_FONT_prop (Display *dpy, XFontStruct *font)
|
|
568 {
|
|
569 unsigned long value = 0;
|
|
570 char *result = 0;
|
|
571 if (XGetFontProperty (font, XA_FONT, &value))
|
|
572 result = XGetAtomName (dpy, value);
|
|
573 /* result is now 0, or the string value of the FONT property. */
|
|
574 if (result)
|
|
575 {
|
|
576 /* Verify that result is an XLFD name (roughly...) */
|
|
577 if (result [0] != '-' || strlen (result) < (unsigned int) 30)
|
|
578 {
|
|
579 XFree (result);
|
|
580 result = 0;
|
|
581 }
|
|
582 }
|
|
583 return result; /* this must be freed by caller if non-0 */
|
|
584 }
|
|
585
|
|
586 static char *
|
|
587 truename_via_random_props (Display *dpy, XFontStruct *font)
|
|
588 {
|
|
589 struct device *d = get_device_from_display (dpy);
|
|
590 unsigned long value = 0;
|
|
591 char *foundry, *family, *weight, *slant, *setwidth, *add_style;
|
|
592 unsigned long pixel, point, res_x, res_y;
|
|
593 char *spacing;
|
|
594 unsigned long avg_width;
|
|
595 char *registry, *encoding;
|
|
596 char composed_name [2048];
|
|
597 int ok = 0;
|
|
598 char *result;
|
|
599
|
|
600 #define get_string(atom,var) \
|
|
601 if (XGetFontProperty (font, (atom), &value)) \
|
|
602 var = XGetAtomName (dpy, value); \
|
|
603 else { \
|
|
604 var = 0; \
|
|
605 goto FAIL; }
|
|
606 #define get_number(atom,var) \
|
|
607 if (!XGetFontProperty (font, (atom), &var) || \
|
|
608 var > 999) \
|
|
609 goto FAIL;
|
|
610
|
|
611 foundry = family = weight = slant = setwidth = 0;
|
|
612 add_style = spacing = registry = encoding = 0;
|
|
613
|
|
614 get_string (DEVICE_XATOM_FOUNDRY (d), foundry);
|
|
615 get_string (DEVICE_XATOM_FAMILY_NAME (d), family);
|
|
616 get_string (DEVICE_XATOM_WEIGHT_NAME (d), weight);
|
|
617 get_string (DEVICE_XATOM_SLANT (d), slant);
|
|
618 get_string (DEVICE_XATOM_SETWIDTH_NAME (d), setwidth);
|
|
619 get_string (DEVICE_XATOM_ADD_STYLE_NAME (d), add_style);
|
|
620 get_number (DEVICE_XATOM_PIXEL_SIZE (d), pixel);
|
|
621 get_number (DEVICE_XATOM_POINT_SIZE (d), point);
|
|
622 get_number (DEVICE_XATOM_RESOLUTION_X (d), res_x);
|
|
623 get_number (DEVICE_XATOM_RESOLUTION_Y (d), res_y);
|
|
624 get_string (DEVICE_XATOM_SPACING (d), spacing);
|
|
625 get_number (DEVICE_XATOM_AVERAGE_WIDTH (d), avg_width);
|
|
626 get_string (DEVICE_XATOM_CHARSET_REGISTRY (d), registry);
|
|
627 get_string (DEVICE_XATOM_CHARSET_ENCODING (d), encoding);
|
|
628 #undef get_number
|
|
629 #undef get_string
|
|
630
|
|
631 sprintf (composed_name,
|
|
632 "-%s-%s-%s-%s-%s-%s-%ld-%ld-%ld-%ld-%s-%ld-%s-%s",
|
|
633 foundry, family, weight, slant, setwidth, add_style, pixel,
|
|
634 point, res_x, res_y, spacing, avg_width, registry, encoding);
|
|
635 ok = 1;
|
|
636
|
|
637 FAIL:
|
|
638 if (ok)
|
|
639 {
|
|
640 int L = strlen (composed_name) + 1;
|
185
|
641 result = (char *) xmalloc (L);
|
0
|
642 strncpy (result, composed_name, L);
|
|
643 }
|
|
644 else
|
|
645 result = 0;
|
|
646
|
|
647 if (foundry) XFree (foundry);
|
|
648 if (family) XFree (family);
|
|
649 if (weight) XFree (weight);
|
|
650 if (slant) XFree (slant);
|
|
651 if (setwidth) XFree (setwidth);
|
|
652 if (add_style) XFree (add_style);
|
|
653 if (spacing) XFree (spacing);
|
|
654 if (registry) XFree (registry);
|
|
655 if (encoding) XFree (encoding);
|
|
656
|
|
657 return result;
|
|
658 }
|
|
659
|
|
660 /* Unbounded, for sufficiently small values of infinity... */
|
|
661 #define MAX_FONT_COUNT 5000
|
|
662
|
|
663 static char *
|
|
664 truename_via_XListFonts (Display *dpy, char *font_name)
|
|
665 {
|
|
666 char *result = 0;
|
|
667 char **names;
|
|
668 int count = 0;
|
|
669
|
|
670 #ifndef XOPENFONT_SORTS
|
|
671 /* In a sensible world, the first font returned by XListFonts()
|
|
672 would be the font that XOpenFont() would use. */
|
|
673 names = XListFonts (dpy, font_name, 1, &count);
|
|
674 if (count) result = names [0];
|
|
675 #else
|
|
676 /* But the world I live in is much more perverse. */
|
|
677 names = XListFonts (dpy, font_name, MAX_FONT_COUNT, &count);
|
|
678 while (count--)
|
|
679 /* If names[count] is lexicographically less than result, use it.
|
|
680 (#### Should we be comparing case-insensitively?) */
|
|
681 if (result == 0 || (strcmp (result, names [count]) < 0))
|
|
682 result = names [count];
|
|
683 #endif
|
|
684
|
|
685 if (result)
|
|
686 result = xstrdup (result);
|
|
687 if (names)
|
|
688 XFreeFontNames (names);
|
|
689
|
|
690 return result; /* this must be freed by caller if non-0 */
|
|
691 }
|
|
692
|
|
693 static Lisp_Object
|
|
694 x_font_truename (Display *dpy, char *name, XFontStruct *font)
|
|
695 {
|
|
696 char *truename_FONT = 0;
|
|
697 char *truename_random = 0;
|
|
698 char *truename = 0;
|
185
|
699
|
0
|
700 /* The search order is:
|
|
701 - if FONT property exists, and is a valid name, return it.
|
|
702 - if the other props exist, and add up to a valid name, return it.
|
|
703 - if we find a matching name with XListFonts, return it.
|
|
704 - if FONT property exists, return it regardless.
|
|
705 - if other props exist, return the resultant name regardless.
|
|
706 - else return 0.
|
|
707 */
|
|
708
|
|
709 truename = truename_FONT = truename_via_FONT_prop (dpy, font);
|
|
710 if (truename && !valid_x_font_name_p (dpy, truename))
|
|
711 truename = 0;
|
|
712 if (!truename)
|
|
713 truename = truename_random = truename_via_random_props (dpy, font);
|
|
714 if (truename && !valid_x_font_name_p (dpy, truename))
|
|
715 truename = 0;
|
|
716 if (!truename && name)
|
|
717 truename = truename_via_XListFonts (dpy, name);
|
|
718
|
|
719 if (!truename)
|
|
720 {
|
|
721 /* Gag - we weren't able to find a seemingly-valid truename.
|
|
722 Well, maybe we're on one of those braindead systems where
|
|
723 XListFonts() and XLoadFont() are in violent disagreement.
|
|
724 If we were able to compute a truename, try using that even
|
|
725 if evidence suggests that it's not a valid name - because
|
|
726 maybe it is, really, and that's better than nothing.
|
|
727 X Windows: You'll envy the dead.
|
|
728 */
|
|
729 if (truename_FONT)
|
|
730 truename = truename_FONT;
|
|
731 else if (truename_random)
|
|
732 truename = truename_random;
|
|
733 }
|
|
734
|
|
735 /* One or both of these are not being used - free them. */
|
|
736 if (truename_FONT && truename_FONT != truename)
|
|
737 XFree (truename_FONT);
|
|
738 if (truename_random && truename_random != truename)
|
|
739 XFree (truename_random);
|
|
740
|
|
741 if (truename)
|
|
742 {
|
|
743 Lisp_Object result = build_string (truename);
|
100
|
744 XFree (truename);
|
0
|
745 return result;
|
|
746 }
|
|
747 else
|
|
748 return Qnil;
|
|
749 }
|
|
750
|
|
751 static Lisp_Object
|
|
752 x_font_instance_truename (struct Lisp_Font_Instance *f, Error_behavior errb)
|
|
753 {
|
|
754 struct device *d = XDEVICE (f->device);
|
|
755
|
|
756 if (NILP (FONT_INSTANCE_X_TRUENAME (f)))
|
|
757 {
|
|
758 Display *dpy = DEVICE_X_DISPLAY (d);
|
14
|
759 char *name = (char *) XSTRING_DATA (f->name);
|
0
|
760 {
|
|
761 FONT_INSTANCE_X_TRUENAME (f) =
|
|
762 x_font_truename (dpy, name, FONT_INSTANCE_X_FONT (f));
|
|
763 }
|
|
764 if (NILP (FONT_INSTANCE_X_TRUENAME (f)))
|
|
765 {
|
|
766 Lisp_Object font_instance = Qnil;
|
|
767 XSETFONT_INSTANCE (font_instance, f);
|
|
768
|
|
769 maybe_signal_simple_error ("couldn't determine font truename",
|
|
770 font_instance, Qfont, errb);
|
|
771 /* Ok, just this once, return the font name as the truename.
|
|
772 (This is only used by Fequal() right now.) */
|
|
773 return f->name;
|
|
774 }
|
|
775 }
|
|
776 return (FONT_INSTANCE_X_TRUENAME (f));
|
|
777 }
|
|
778
|
|
779 static Lisp_Object
|
|
780 x_font_instance_properties (struct Lisp_Font_Instance *f)
|
|
781 {
|
|
782 struct device *d = XDEVICE (f->device);
|
|
783 int i;
|
|
784 Lisp_Object result = Qnil;
|
|
785 XFontProp *props;
|
|
786 Display *dpy;
|
185
|
787
|
0
|
788 dpy = DEVICE_X_DISPLAY (d);
|
|
789 props = FONT_INSTANCE_X_FONT (f)->properties;
|
|
790 for (i = FONT_INSTANCE_X_FONT (f)->n_properties - 1; i >= 0; i--)
|
|
791 {
|
|
792 char *name_str = 0;
|
|
793 char *val_str = 0;
|
|
794 Lisp_Object name, value;
|
|
795 Atom atom = props [i].name;
|
|
796 name_str = XGetAtomName (dpy, atom);
|
|
797 name = (name_str ? intern (name_str) : Qnil);
|
|
798 if (name_str &&
|
|
799 (atom == XA_FONT ||
|
|
800 atom == DEVICE_XATOM_FOUNDRY (d) ||
|
|
801 atom == DEVICE_XATOM_FAMILY_NAME (d) ||
|
|
802 atom == DEVICE_XATOM_WEIGHT_NAME (d) ||
|
|
803 atom == DEVICE_XATOM_SLANT (d) ||
|
|
804 atom == DEVICE_XATOM_SETWIDTH_NAME (d) ||
|
|
805 atom == DEVICE_XATOM_ADD_STYLE_NAME (d) ||
|
|
806 atom == DEVICE_XATOM_SPACING (d) ||
|
|
807 atom == DEVICE_XATOM_CHARSET_REGISTRY (d) ||
|
|
808 atom == DEVICE_XATOM_CHARSET_ENCODING (d) ||
|
|
809 !strcmp (name_str, "CHARSET_COLLECTIONS") ||
|
|
810 !strcmp (name_str, "FONTNAME_REGISTRY") ||
|
|
811 !strcmp (name_str, "CLASSIFICATION") ||
|
|
812 !strcmp (name_str, "COPYRIGHT") ||
|
|
813 !strcmp (name_str, "DEVICE_FONT_NAME") ||
|
|
814 !strcmp (name_str, "FULL_NAME") ||
|
|
815 !strcmp (name_str, "MONOSPACED") ||
|
|
816 !strcmp (name_str, "QUALITY") ||
|
|
817 !strcmp (name_str, "RELATIVE_SET") ||
|
|
818 !strcmp (name_str, "RELATIVE_WEIGHT") ||
|
|
819 !strcmp (name_str, "STYLE")))
|
|
820 {
|
|
821 val_str = XGetAtomName (dpy, props [i].card32);
|
|
822 value = (val_str ? build_string (val_str) : Qnil);
|
|
823 }
|
|
824 else
|
|
825 value = make_int (props [i].card32);
|
|
826 if (name_str) XFree (name_str);
|
|
827 result = Fcons (Fcons (name, value), result);
|
|
828 }
|
|
829 return result;
|
|
830 }
|
|
831
|
|
832 static Lisp_Object
|
|
833 x_list_fonts (Lisp_Object pattern, Lisp_Object device)
|
|
834 {
|
|
835 char **names;
|
|
836 int count = 0;
|
|
837 Lisp_Object result = Qnil;
|
|
838 CONST char *patternext;
|
|
839
|
|
840 GET_C_STRING_BINARY_DATA_ALLOCA (pattern, patternext);
|
185
|
841
|
0
|
842 names = XListFonts (DEVICE_X_DISPLAY (XDEVICE (device)),
|
|
843 patternext, MAX_FONT_COUNT, &count);
|
|
844 while (count--)
|
|
845 result = Fcons (build_ext_string (names [count], FORMAT_BINARY), result);
|
|
846 if (names)
|
|
847 XFreeFontNames (names);
|
|
848 return result;
|
|
849 }
|
|
850
|
70
|
851 #ifdef MULE
|
|
852
|
|
853 static int
|
|
854 x_font_spec_matches_charset (struct device *d, Lisp_Object charset,
|
|
855 CONST Bufbyte *nonreloc, Lisp_Object reloc,
|
|
856 Bytecount offset, Bytecount length)
|
|
857 {
|
|
858 if (UNBOUNDP (charset))
|
|
859 return 1;
|
|
860 /* Hack! Short font names don't have the registry in them,
|
|
861 so we just assume the user knows what they're doing in the
|
|
862 case of ASCII. For other charsets, you gotta give the
|
|
863 long form; sorry buster.
|
|
864 */
|
|
865 if (EQ (charset, Vcharset_ascii))
|
|
866 {
|
|
867 CONST Bufbyte *the_nonreloc = nonreloc;
|
|
868 int i;
|
|
869 Bytecount the_length = length;
|
185
|
870
|
70
|
871 if (!the_nonreloc)
|
|
872 the_nonreloc = XSTRING_DATA (reloc);
|
|
873 fixup_internal_substring (nonreloc, reloc, offset, &the_length);
|
|
874 the_nonreloc += offset;
|
|
875 if (!memchr (the_nonreloc, '*', the_length))
|
|
876 {
|
|
877 for (i = 0;; i++)
|
|
878 {
|
185
|
879 CONST Bufbyte *new_nonreloc = (CONST Bufbyte *)
|
70
|
880 memchr (the_nonreloc, '-', the_length);
|
|
881 if (!new_nonreloc)
|
|
882 break;
|
|
883 new_nonreloc++;
|
|
884 the_length -= new_nonreloc - the_nonreloc;
|
|
885 the_nonreloc = new_nonreloc;
|
|
886 }
|
185
|
887
|
70
|
888 /* If it has less than 5 dashes, it's a short font.
|
|
889 Of course, long fonts always have 14 dashes or so, but short
|
|
890 fonts never have more than 1 or 2 dashes, so this is some
|
|
891 sort of reasonable heuristic. */
|
|
892 if (i < 5)
|
|
893 return 1;
|
|
894 }
|
|
895 }
|
185
|
896
|
70
|
897 return (fast_string_match (XCHARSET_REGISTRY (charset),
|
|
898 nonreloc, reloc, offset, length, 1,
|
|
899 ERROR_ME, 0) >= 0);
|
|
900 }
|
|
901
|
|
902 /* find a font spec that matches font spec FONT and also matches
|
|
903 (the registry of) CHARSET. */
|
|
904 static Lisp_Object
|
|
905 x_find_charset_font (Lisp_Object device, Lisp_Object font,
|
|
906 Lisp_Object charset)
|
|
907 {
|
|
908 char **names;
|
|
909 int count = 0;
|
|
910 Lisp_Object result = Qnil;
|
|
911 CONST char *patternext;
|
|
912 int i;
|
|
913
|
|
914 GET_C_STRING_BINARY_DATA_ALLOCA (font, patternext);
|
185
|
915
|
70
|
916 names = XListFonts (DEVICE_X_DISPLAY (XDEVICE (device)),
|
|
917 patternext, MAX_FONT_COUNT, &count);
|
|
918 for (i = 0; i < count; i ++)
|
|
919 {
|
|
920 CONST char *intname;
|
|
921
|
|
922 GET_C_CHARPTR_INT_BINARY_DATA_ALLOCA (names[i], intname);
|
|
923 if (x_font_spec_matches_charset (XDEVICE (device), charset,
|
|
924 (unsigned char *) intname,
|
|
925 Qnil, 0, -1))
|
|
926 {
|
|
927 result = build_string (intname);
|
|
928 break;
|
|
929 }
|
|
930 }
|
|
931
|
|
932 if (names)
|
|
933 XFreeFontNames (names);
|
|
934
|
|
935 /* Check for a short font name. */
|
|
936 if (NILP (result)
|
|
937 && x_font_spec_matches_charset (XDEVICE (device), charset, 0,
|
|
938 font, 0, -1))
|
|
939 return font;
|
|
940
|
|
941 return result;
|
|
942 }
|
|
943
|
|
944 #endif /* MULE */
|
|
945
|
0
|
946
|
|
947 /************************************************************************/
|
|
948 /* initialization */
|
|
949 /************************************************************************/
|
|
950
|
|
951 void
|
|
952 syms_of_objects_x (void)
|
|
953 {
|
|
954 }
|
|
955
|
|
956 void
|
|
957 console_type_create_objects_x (void)
|
|
958 {
|
|
959 /* object methods */
|
|
960
|
|
961 CONSOLE_HAS_METHOD (x, initialize_color_instance);
|
|
962 CONSOLE_HAS_METHOD (x, print_color_instance);
|
|
963 CONSOLE_HAS_METHOD (x, finalize_color_instance);
|
|
964 CONSOLE_HAS_METHOD (x, color_instance_equal);
|
|
965 CONSOLE_HAS_METHOD (x, color_instance_hash);
|
|
966 CONSOLE_HAS_METHOD (x, color_instance_rgb_components);
|
|
967 CONSOLE_HAS_METHOD (x, valid_color_name_p);
|
|
968
|
|
969 CONSOLE_HAS_METHOD (x, initialize_font_instance);
|
|
970 CONSOLE_HAS_METHOD (x, mark_font_instance);
|
|
971 CONSOLE_HAS_METHOD (x, print_font_instance);
|
|
972 CONSOLE_HAS_METHOD (x, finalize_font_instance);
|
|
973 CONSOLE_HAS_METHOD (x, font_instance_truename);
|
|
974 CONSOLE_HAS_METHOD (x, font_instance_properties);
|
|
975 CONSOLE_HAS_METHOD (x, list_fonts);
|
70
|
976 #ifdef MULE
|
|
977 CONSOLE_HAS_METHOD (x, find_charset_font);
|
|
978 CONSOLE_HAS_METHOD (x, font_spec_matches_charset);
|
|
979 #endif
|
0
|
980 }
|
|
981
|
|
982 void
|
|
983 vars_of_objects_x (void)
|
|
984 {
|
|
985 DEFVAR_BOOL ("x-handle-non-fully-specified-fonts",&handle_nonfull_spec_fonts /*
|
|
986 If this is true then fonts which do not have all characters specified
|
|
987 will be considered to be proportional width even if they are actually
|
|
988 fixed-width. If this is not done then characters which are supposed to
|
|
989 have 0 width may appear to actually have some width.
|
|
990
|
|
991 Note: While setting this to t guarantees correct output in all
|
|
992 circumstances, it also causes a noticeable performance hit when using
|
|
993 fixed-width fonts. Since most people don't use characters which could
|
|
994 cause problems this is set to nil by default.
|
|
995 */ );
|
|
996 handle_nonfull_spec_fonts = 0;
|
|
997 }
|
|
998
|
|
999 void
|
|
1000 Xatoms_of_objects_x (struct device *d)
|
|
1001 {
|
149
|
1002 Display *D = DEVICE_X_DISPLAY (d);
|
185
|
1003
|
149
|
1004 DEVICE_XATOM_FOUNDRY (d) = XInternAtom (D, "FOUNDRY", False);
|
|
1005 DEVICE_XATOM_FAMILY_NAME (d) = XInternAtom (D, "FAMILY_NAME", False);
|
|
1006 DEVICE_XATOM_WEIGHT_NAME (d) = XInternAtom (D, "WEIGHT_NAME", False);
|
|
1007 DEVICE_XATOM_SLANT (d) = XInternAtom (D, "SLANT", False);
|
|
1008 DEVICE_XATOM_SETWIDTH_NAME (d) = XInternAtom (D, "SETWIDTH_NAME", False);
|
|
1009 DEVICE_XATOM_ADD_STYLE_NAME (d) = XInternAtom (D, "ADD_STYLE_NAME", False);
|
|
1010 DEVICE_XATOM_PIXEL_SIZE (d) = XInternAtom (D, "PIXEL_SIZE", False);
|
|
1011 DEVICE_XATOM_POINT_SIZE (d) = XInternAtom (D, "POINT_SIZE", False);
|
|
1012 DEVICE_XATOM_RESOLUTION_X (d) = XInternAtom (D, "RESOLUTION_X", False);
|
|
1013 DEVICE_XATOM_RESOLUTION_Y (d) = XInternAtom (D, "RESOLUTION_Y", False);
|
|
1014 DEVICE_XATOM_SPACING (d) = XInternAtom (D, "SPACING", False);
|
|
1015 DEVICE_XATOM_AVERAGE_WIDTH (d) = XInternAtom (D, "AVERAGE_WIDTH", False);
|
|
1016 DEVICE_XATOM_CHARSET_REGISTRY(d) = XInternAtom (D, "CHARSET_REGISTRY",False);
|
|
1017 DEVICE_XATOM_CHARSET_ENCODING(d) = XInternAtom (D, "CHARSET_ENCODING",False);
|
0
|
1018 }
|