Merge changes made in Gnus trunk.
[emacs.git] / src / ftxfont.c
blob0a9137617b890bff185ae070f069ac7755c57d9f
1 /* ftxfont.c -- FreeType font driver on X (without using XFT).
2 Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
3 Copyright (C) 2006, 2007, 2008, 2009, 2010
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H13PRO009
7 This file is part of GNU Emacs.
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.
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.
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/>. */
22 #include <config.h>
23 #include <stdio.h>
24 #include <setjmp.h>
25 #include <X11/Xlib.h>
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"
37 /* FTX font driver. */
39 static Lisp_Object Qftx;
41 /* Prototypes for helper function. */
42 static GC *ftxfont_get_gcs (FRAME_PTR, unsigned long, unsigned long);
43 static int ftxfont_draw_bitmap (FRAME_PTR, GC, GC *, struct font *,
44 unsigned, int, int, XPoint *, int, int *,
45 int);
46 static void ftxfont_draw_backgrond (FRAME_PTR, struct font *, GC,
47 int, int, int);
49 struct ftxfont_frame_data
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;
61 /* Return an array of 6 GCs for antialiasing. */
63 static GC *
64 ftxfont_get_gcs (FRAME_PTR f, long unsigned int foreground, long unsigned int background)
66 XColor color;
67 XGCValues xgcv;
68 int i;
69 struct ftxfont_frame_data *data = font_get_frame_data (f, &ftxfont_driver);
70 struct ftxfont_frame_data *prev = NULL, *this = NULL, *new;
72 if (data)
74 for (this = data; this; prev = this, this = this->next)
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;
88 new = malloc (sizeof (struct ftxfont_frame_data));
89 if (! new)
90 return NULL;
91 new->next = this;
92 if (prev)
94 prev->next = new;
96 else if (font_put_frame_data (f, &ftxfont_driver, new) < 0)
98 free (new);
99 return NULL;
102 new->colors[0].pixel = background;
103 new->colors[1].pixel = foreground;
105 BLOCK_INPUT;
106 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), new->colors, 2);
107 for (i = 1; i < 7; i++)
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))
117 break;
118 xgcv.foreground = color.pixel;
119 new->gcs[i - 1] = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
120 GCForeground, &xgcv);
122 UNBLOCK_INPUT;
124 if (i < 7)
126 BLOCK_INPUT;
127 for (i--; i >= 0; i--)
128 XFreeGC (FRAME_X_DISPLAY (f), new->gcs[i]);
129 UNBLOCK_INPUT;
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;
137 return new->gcs;
140 static int
141 ftxfont_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)
143 struct font_bitmap bitmap;
144 unsigned char *b;
145 int i, j;
147 if (ftfont_driver.get_bitmap (font, code, &bitmap, size > 0x100 ? 1 : 8) < 0)
148 return 0;
149 if (size > 0x100)
151 for (i = 0, b = bitmap.buffer; i < bitmap.rows;
152 i++, b += bitmap.pitch)
154 for (j = 0; j < bitmap.width; j++)
155 if (b[j / 8] & (1 << (7 - (j % 8))))
157 p[n[0]].x = x + bitmap.left + j;
158 p[n[0]].y = y - bitmap.top + i;
159 if (++n[0] == size)
161 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
162 gc_fore, p, size, CoordModeOrigin);
163 n[0] = 0;
167 if (flush && n[0] > 0)
168 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
169 gc_fore, p, n[0], CoordModeOrigin);
171 else
173 for (i = 0, b = bitmap.buffer; i < bitmap.rows;
174 i++, b += bitmap.pitch)
176 for (j = 0; j < bitmap.width; j++)
178 int idx = (bitmap.bits_per_pixel == 1
179 ? ((b[j / 8] & (1 << (7 - (j % 8)))) ? 6 : -1)
180 : (b[j] >> 5) - 1);
182 if (idx >= 0)
184 XPoint *pp = p + size * idx;
186 pp[n[idx]].x = x + bitmap.left + j;
187 pp[n[idx]].y = y - bitmap.top + i;
188 if (++(n[idx]) == size)
190 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
191 idx == 6 ? gc_fore : gcs[idx], pp, size,
192 CoordModeOrigin);
193 n[idx] = 0;
198 if (flush)
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);
210 if (ftfont_driver.free_bitmap)
211 ftfont_driver.free_bitmap (font, &bitmap);
213 return bitmap.advance;
216 static void
217 ftxfont_draw_backgrond (FRAME_PTR f, struct font *font, GC gc, int x, int y, int width)
219 XGCValues xgcv;
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);
229 /* Prototypes for font-driver methods. */
230 static Lisp_Object ftxfont_list (Lisp_Object, Lisp_Object);
231 static Lisp_Object ftxfont_match (Lisp_Object, Lisp_Object);
232 static Lisp_Object ftxfont_open (FRAME_PTR, Lisp_Object, int);
233 static void ftxfont_close (FRAME_PTR, struct font *);
234 static int ftxfont_draw (struct glyph_string *, int, int, int, int, int);
236 struct font_driver ftxfont_driver;
238 static Lisp_Object
239 ftxfont_list (Lisp_Object frame, Lisp_Object spec)
241 Lisp_Object list = ftfont_driver.list (frame, spec), tail;
243 for (tail = list; CONSP (tail); tail = XCDR (tail))
244 ASET (XCAR (tail), FONT_TYPE_INDEX, Qftx);
245 return list;
248 static Lisp_Object
249 ftxfont_match (Lisp_Object frame, Lisp_Object spec)
251 Lisp_Object entity = ftfont_driver.match (frame, spec);
253 if (VECTORP (entity))
254 ASET (entity, FONT_TYPE_INDEX, Qftx);
255 return entity;
258 static Lisp_Object
259 ftxfont_open (FRAME_PTR f, Lisp_Object entity, int pixel_size)
261 Lisp_Object font_object;
262 struct font *font;
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;
272 static void
273 ftxfont_close (FRAME_PTR f, struct font *font)
275 ftfont_driver.close (f, font);
278 static int
279 ftxfont_draw (struct glyph_string *s, int from, int to, int x, int y, int with_background)
281 FRAME_PTR f = s->f;
282 struct face *face = s->face;
283 struct font *font = s->font;
284 XPoint p[0x700];
285 int n[7];
286 unsigned *code;
287 int len = to - from;
288 int i;
289 GC *gcs;
290 int xadvance;
292 n[0] = n[1] = n[2] = n[3] = n[4] = n[5] = n[6] = 0;
294 BLOCK_INPUT;
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));
302 if (face->gc == s->gc)
304 gcs = ftxfont_get_gcs (f, face->foreground, face->background);
306 else
308 XGCValues xgcv;
309 unsigned long mask = GCForeground | GCBackground;
311 XGetGCValues (FRAME_X_DISPLAY (f), s->gc, mask, &xgcv);
312 gcs = ftxfont_get_gcs (f, xgcv.foreground, xgcv.background);
315 if (gcs)
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);
322 for (i = 0; i < len; i++)
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);
328 if (s->num_clips)
329 for (i = 0; i < 6; i++)
330 XSetClipMask (FRAME_X_DISPLAY (f), gcs[i], None);
332 else
334 /* We can't draw with antialiasing.
335 s->gc should already have a proper clipping setting. */
336 for (i = 0; i < len; i++)
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);
344 UNBLOCK_INPUT;
346 return len;
349 static int
350 ftxfont_end_for_frame (FRAME_PTR f)
352 struct ftxfont_frame_data *data = font_get_frame_data (f, &ftxfont_driver);
354 BLOCK_INPUT;
355 while (data)
357 struct ftxfont_frame_data *next = data->next;
358 int i;
360 for (i = 0; i < 6; i++)
361 XFreeGC (FRAME_X_DISPLAY (f), data->gcs[i]);
362 free (data);
363 data = next;
365 UNBLOCK_INPUT;
366 font_put_frame_data (f, &ftxfont_driver, NULL);
367 return 0;
372 void
373 syms_of_ftxfont (void)
375 DEFSYM (Qftx, "ftx");
377 ftxfont_driver = ftfont_driver;
378 ftxfont_driver.type = Qftx;
379 ftxfont_driver.list = ftxfont_list;
380 ftxfont_driver.match = ftxfont_match;
381 ftxfont_driver.open = ftxfont_open;
382 ftxfont_driver.close = ftxfont_close;
383 ftxfont_driver.draw = ftxfont_draw;
384 ftxfont_driver.end_for_frame = ftxfont_end_for_frame;
385 register_font_driver (&ftxfont_driver, NULL);
388 /* arch-tag: 59bd3469-5330-413f-b29d-1aa36492abe8
389 (do not change this comment) */