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