]> code.delx.au - gnu-emacs/blob - src/ftxfont.c
Don't install keyboard hook when debugged on MS-Windows
[gnu-emacs] / src / ftxfont.c
1 /* ftxfont.c -- FreeType font driver on X (without using XFT).
2 Copyright (C) 2006-2016 Free Software Foundation, Inc.
3 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H13PRO009
6
7 This file is part of GNU Emacs.
8
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or (at
12 your option) any later version.
13
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <X11/Xlib.h>
25
26 #include "lisp.h"
27 #include "xterm.h"
28 #include "frame.h"
29 #include "blockinput.h"
30 #include "font.h"
31
32 /* FTX font driver. */
33
34 struct font_driver ftxfont_driver;
35
36 struct ftxfont_frame_data
37 {
38 /* Background and foreground colors. */
39 XColor colors[2];
40 /* GCs interpolating the above colors. gcs[0] is for a color
41 closest to BACKGROUND, and gcs[5] is for a color closest to
42 FOREGROUND. */
43 GC gcs[6];
44 struct ftxfont_frame_data *next;
45 };
46
47
48 /* Return an array of 6 GCs for antialiasing. */
49
50 static GC *
51 ftxfont_get_gcs (struct frame *f, unsigned long foreground, unsigned long background)
52 {
53 XColor color;
54 XGCValues xgcv;
55 int i;
56 struct ftxfont_frame_data *data = font_get_frame_data (f, Qftx);
57 struct ftxfont_frame_data *prev = NULL, *this = NULL, *new;
58
59 if (data)
60 {
61 for (this = data; this; prev = this, this = this->next)
62 {
63 if (this->colors[0].pixel < background)
64 continue;
65 if (this->colors[0].pixel > background)
66 break;
67 if (this->colors[1].pixel < foreground)
68 continue;
69 if (this->colors[1].pixel > foreground)
70 break;
71 return this->gcs;
72 }
73 }
74
75 new = xmalloc (sizeof *new);
76 new->next = this;
77 if (prev)
78 prev->next = new;
79 font_put_frame_data (f, Qftx, new);
80
81 new->colors[0].pixel = background;
82 new->colors[1].pixel = foreground;
83
84 block_input ();
85 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), new->colors, 2);
86 for (i = 1; i < 7; i++)
87 {
88 /* Interpolate colors linearly. Any better algorithm? */
89 color.red
90 = (new->colors[1].red * i + new->colors[0].red * (8 - i)) / 8;
91 color.green
92 = (new->colors[1].green * i + new->colors[0].green * (8 - i)) / 8;
93 color.blue
94 = (new->colors[1].blue * i + new->colors[0].blue * (8 - i)) / 8;
95 if (! x_alloc_nearest_color (f, FRAME_X_COLORMAP (f), &color))
96 break;
97 xgcv.foreground = color.pixel;
98 new->gcs[i - 1] = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
99 GCForeground, &xgcv);
100 }
101 unblock_input ();
102
103 if (i < 7)
104 {
105 block_input ();
106 for (i--; i >= 0; i--)
107 XFreeGC (FRAME_X_DISPLAY (f), new->gcs[i]);
108 unblock_input ();
109 if (prev)
110 prev->next = new->next;
111 else if (data)
112 font_put_frame_data (f, Qftx, new->next);
113 xfree (new);
114 return NULL;
115 }
116 return new->gcs;
117 }
118
119 static int
120 ftxfont_draw_bitmap (struct frame *f, GC gc_fore, GC *gcs, struct font *font,
121 unsigned int code, int x, int y, XPoint *p, int size,
122 int *n, bool flush)
123 {
124 struct font_bitmap bitmap;
125 unsigned char *b;
126 int i, j;
127
128 if (ftfont_driver.get_bitmap (font, code, &bitmap, size > 0x100 ? 1 : 8) < 0)
129 return 0;
130 if (size > 0x100)
131 {
132 for (i = 0, b = bitmap.buffer; i < bitmap.rows;
133 i++, b += bitmap.pitch)
134 {
135 for (j = 0; j < bitmap.width; j++)
136 if (b[j / 8] & (1 << (7 - (j % 8))))
137 {
138 p[n[0]].x = x + bitmap.left + j;
139 p[n[0]].y = y - bitmap.top + i;
140 if (++n[0] == size)
141 {
142 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
143 gc_fore, p, size, CoordModeOrigin);
144 n[0] = 0;
145 }
146 }
147 }
148 if (flush && n[0] > 0)
149 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
150 gc_fore, p, n[0], CoordModeOrigin);
151 }
152 else
153 {
154 for (i = 0, b = bitmap.buffer; i < bitmap.rows;
155 i++, b += bitmap.pitch)
156 {
157 for (j = 0; j < bitmap.width; j++)
158 {
159 int idx = (bitmap.bits_per_pixel == 1
160 ? ((b[j / 8] & (1 << (7 - (j % 8)))) ? 6 : -1)
161 : (b[j] >> 5) - 1);
162
163 if (idx >= 0)
164 {
165 XPoint *pp = p + size * idx;
166
167 pp[n[idx]].x = x + bitmap.left + j;
168 pp[n[idx]].y = y - bitmap.top + i;
169 if (++(n[idx]) == size)
170 {
171 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
172 idx == 6 ? gc_fore : gcs[idx], pp, size,
173 CoordModeOrigin);
174 n[idx] = 0;
175 }
176 }
177 }
178 }
179 if (flush)
180 {
181 for (i = 0; i < 6; i++)
182 if (n[i] > 0)
183 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
184 gcs[i], p + 0x100 * i, n[i], CoordModeOrigin);
185 if (n[6] > 0)
186 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
187 gc_fore, p + 0x600, n[6], CoordModeOrigin);
188 }
189 }
190
191 if (ftfont_driver.free_bitmap)
192 ftfont_driver.free_bitmap (font, &bitmap);
193
194 return bitmap.advance;
195 }
196
197 static void
198 ftxfont_draw_background (struct frame *f, struct font *font, GC gc, int x, int y,
199 int width)
200 {
201 XGCValues xgcv;
202
203 XGetGCValues (FRAME_X_DISPLAY (f), gc,
204 GCForeground | GCBackground, &xgcv);
205 XSetForeground (FRAME_X_DISPLAY (f), gc, xgcv.background);
206 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
207 x, y - FONT_BASE (font), width, FONT_HEIGHT (font));
208 XSetForeground (FRAME_X_DISPLAY (f), gc, xgcv.foreground);
209 }
210
211 static Lisp_Object
212 ftxfont_list (struct frame *f, Lisp_Object spec)
213 {
214 Lisp_Object list = ftfont_driver.list (f, spec), tail;
215
216 for (tail = list; CONSP (tail); tail = XCDR (tail))
217 ASET (XCAR (tail), FONT_TYPE_INDEX, Qftx);
218 return list;
219 }
220
221 static Lisp_Object
222 ftxfont_match (struct frame *f, Lisp_Object spec)
223 {
224 Lisp_Object entity = ftfont_driver.match (f, spec);
225
226 if (VECTORP (entity))
227 ASET (entity, FONT_TYPE_INDEX, Qftx);
228 return entity;
229 }
230
231 static Lisp_Object
232 ftxfont_open (struct frame *f, Lisp_Object entity, int pixel_size)
233 {
234 Lisp_Object font_object;
235 struct font *font;
236
237 font_object = ftfont_driver.open (f, entity, pixel_size);
238 if (NILP (font_object))
239 return Qnil;
240 font = XFONT_OBJECT (font_object);
241 font->driver = &ftxfont_driver;
242 return font_object;
243 }
244
245 static void
246 ftxfont_close (struct font *font)
247 {
248 ftfont_driver.close (font);
249 }
250
251 static int
252 ftxfont_draw (struct glyph_string *s, int from, int to, int x, int y,
253 bool with_background)
254 {
255 struct frame *f = s->f;
256 struct face *face = s->face;
257 struct font *font = s->font;
258 XPoint p[0x700];
259 int n[7];
260 unsigned *code;
261 int len = to - from;
262 int i;
263 GC *gcs;
264 int xadvance;
265
266 n[0] = n[1] = n[2] = n[3] = n[4] = n[5] = n[6] = 0;
267
268 USE_SAFE_ALLOCA;
269 SAFE_NALLOCA (code, 1, len);
270 block_input ();
271 if (with_background)
272 ftxfont_draw_background (f, font, s->gc, x, y, s->width);
273 for (i = 0; i < len; i++)
274 code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
275 | XCHAR2B_BYTE2 (s->char2b + from + i));
276
277 if (face->gc == s->gc)
278 {
279 gcs = ftxfont_get_gcs (f, face->foreground, face->background);
280 }
281 else
282 {
283 XGCValues xgcv;
284 unsigned long mask = GCForeground | GCBackground;
285
286 XGetGCValues (FRAME_X_DISPLAY (f), s->gc, mask, &xgcv);
287 gcs = ftxfont_get_gcs (f, xgcv.foreground, xgcv.background);
288 }
289
290 if (gcs)
291 {
292 if (s->num_clips)
293 for (i = 0; i < 6; i++)
294 XSetClipRectangles (FRAME_X_DISPLAY (f), gcs[i], 0, 0,
295 s->clip, s->num_clips, Unsorted);
296
297 for (i = 0; i < len; i++)
298 {
299 xadvance = ftxfont_draw_bitmap (f, s->gc, gcs, font, code[i], x, y,
300 p, 0x100, n, i + 1 == len);
301 x += (s->padding_p ? 1 : xadvance);
302 }
303 if (s->num_clips)
304 for (i = 0; i < 6; i++)
305 XSetClipMask (FRAME_X_DISPLAY (f), gcs[i], None);
306 }
307 else
308 {
309 /* We can't draw with antialiasing.
310 s->gc should already have a proper clipping setting. */
311 for (i = 0; i < len; i++)
312 {
313 xadvance = ftxfont_draw_bitmap (f, s->gc, NULL, font, code[i], x, y,
314 p, 0x700, n, i + 1 == len);
315 x += (s->padding_p ? 1 : xadvance);
316 }
317 }
318
319 unblock_input ();
320 SAFE_FREE ();
321
322 return len;
323 }
324
325 static int
326 ftxfont_end_for_frame (struct frame *f)
327 {
328 struct ftxfont_frame_data *data = font_get_frame_data (f, Qftx);
329
330 block_input ();
331 while (data)
332 {
333 struct ftxfont_frame_data *next = data->next;
334 int i;
335
336 for (i = 0; i < 6; i++)
337 XFreeGC (FRAME_X_DISPLAY (f), data->gcs[i]);
338 xfree (data);
339 data = next;
340 }
341 unblock_input ();
342 font_put_frame_data (f, Qftx, NULL);
343 return 0;
344 }
345
346 \f
347
348 void
349 syms_of_ftxfont (void)
350 {
351 DEFSYM (Qftx, "ftx");
352
353 ftxfont_driver = ftfont_driver;
354 ftxfont_driver.type = Qftx;
355 ftxfont_driver.list = ftxfont_list;
356 ftxfont_driver.match = ftxfont_match;
357 ftxfont_driver.open = ftxfont_open;
358 ftxfont_driver.close = ftxfont_close;
359 ftxfont_driver.draw = ftxfont_draw;
360 ftxfont_driver.end_for_frame = ftxfont_end_for_frame;
361 register_font_driver (&ftxfont_driver, NULL);
362 }