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