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