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