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