* lisp/autorevert.el (auto-revert-handler): Use vc-refresh-state.
[emacs.git] / src / ftxfont.c
blob99b06f2b49ce5becf6d81156636eaf83485ce53f
1 /* ftxfont.c -- FreeType font driver on X (without using XFT).
2 Copyright (C) 2006-2015 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
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 <X11/Xlib.h>
26 #include "lisp.h"
27 #include "xterm.h"
28 #include "frame.h"
29 #include "blockinput.h"
30 #include "font.h"
32 /* FTX font driver. */
34 struct font_driver ftxfont_driver;
36 struct ftxfont_frame_data
38 /* Background and foreground colors. */
39 XColor colors[2];
40 /* GCs interpolating the above colors. gcs[0] is for a color
41 closest to BACKGROUND, and gcs[5] is for a color closest to
42 FOREGROUND. */
43 GC gcs[6];
44 struct ftxfont_frame_data *next;
48 /* Return an array of 6 GCs for antialiasing. */
50 static GC *
51 ftxfont_get_gcs (struct frame *f, unsigned long foreground, unsigned long background)
53 XColor color;
54 XGCValues xgcv;
55 int i;
56 struct ftxfont_frame_data *data = font_get_frame_data (f, Qftx);
57 struct ftxfont_frame_data *prev = NULL, *this = NULL, *new;
59 if (data)
61 for (this = data; this; prev = this, this = this->next)
63 if (this->colors[0].pixel < background)
64 continue;
65 if (this->colors[0].pixel > background)
66 break;
67 if (this->colors[1].pixel < foreground)
68 continue;
69 if (this->colors[1].pixel > foreground)
70 break;
71 return this->gcs;
75 new = xmalloc (sizeof *new);
76 new->next = this;
77 if (prev)
78 prev->next = new;
79 font_put_frame_data (f, Qftx, new);
81 new->colors[0].pixel = background;
82 new->colors[1].pixel = foreground;
84 block_input ();
85 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), new->colors, 2);
86 for (i = 1; i < 7; i++)
88 /* Interpolate colors linearly. Any better algorithm? */
89 color.red
90 = (new->colors[1].red * i + new->colors[0].red * (8 - i)) / 8;
91 color.green
92 = (new->colors[1].green * i + new->colors[0].green * (8 - i)) / 8;
93 color.blue
94 = (new->colors[1].blue * i + new->colors[0].blue * (8 - i)) / 8;
95 if (! x_alloc_nearest_color (f, FRAME_X_COLORMAP (f), &color))
96 break;
97 xgcv.foreground = color.pixel;
98 new->gcs[i - 1] = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
99 GCForeground, &xgcv);
101 unblock_input ();
103 if (i < 7)
105 block_input ();
106 for (i--; i >= 0; i--)
107 XFreeGC (FRAME_X_DISPLAY (f), new->gcs[i]);
108 unblock_input ();
109 if (prev)
110 prev->next = new->next;
111 else if (data)
112 font_put_frame_data (f, Qftx, new->next);
113 xfree (new);
114 return NULL;
116 return new->gcs;
119 static int
120 ftxfont_draw_bitmap (struct frame *f, GC gc_fore, GC *gcs, struct font *font,
121 unsigned int code, int x, int y, XPoint *p, int size,
122 int *n, bool flush)
124 struct font_bitmap bitmap;
125 unsigned char *b;
126 int i, j;
128 if (ftfont_driver.get_bitmap (font, code, &bitmap, size > 0x100 ? 1 : 8) < 0)
129 return 0;
130 if (size > 0x100)
132 for (i = 0, b = bitmap.buffer; i < bitmap.rows;
133 i++, b += bitmap.pitch)
135 for (j = 0; j < bitmap.width; j++)
136 if (b[j / 8] & (1 << (7 - (j % 8))))
138 p[n[0]].x = x + bitmap.left + j;
139 p[n[0]].y = y - bitmap.top + i;
140 if (++n[0] == size)
142 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
143 gc_fore, p, size, CoordModeOrigin);
144 n[0] = 0;
148 if (flush && n[0] > 0)
149 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
150 gc_fore, p, n[0], CoordModeOrigin);
152 else
154 for (i = 0, b = bitmap.buffer; i < bitmap.rows;
155 i++, b += bitmap.pitch)
157 for (j = 0; j < bitmap.width; j++)
159 int idx = (bitmap.bits_per_pixel == 1
160 ? ((b[j / 8] & (1 << (7 - (j % 8)))) ? 6 : -1)
161 : (b[j] >> 5) - 1);
163 if (idx >= 0)
165 XPoint *pp = p + size * idx;
167 pp[n[idx]].x = x + bitmap.left + j;
168 pp[n[idx]].y = y - bitmap.top + i;
169 if (++(n[idx]) == size)
171 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
172 idx == 6 ? gc_fore : gcs[idx], pp, size,
173 CoordModeOrigin);
174 n[idx] = 0;
179 if (flush)
181 for (i = 0; i < 6; i++)
182 if (n[i] > 0)
183 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
184 gcs[i], p + 0x100 * i, n[i], CoordModeOrigin);
185 if (n[6] > 0)
186 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
187 gc_fore, p + 0x600, n[6], CoordModeOrigin);
191 if (ftfont_driver.free_bitmap)
192 ftfont_driver.free_bitmap (font, &bitmap);
194 return bitmap.advance;
197 static void
198 ftxfont_draw_background (struct frame *f, struct font *font, GC gc, int x, int y,
199 int width)
201 XGCValues xgcv;
203 XGetGCValues (FRAME_X_DISPLAY (f), gc,
204 GCForeground | GCBackground, &xgcv);
205 XSetForeground (FRAME_X_DISPLAY (f), gc, xgcv.background);
206 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
207 x, y - FONT_BASE (font), width, FONT_HEIGHT (font));
208 XSetForeground (FRAME_X_DISPLAY (f), gc, xgcv.foreground);
211 static Lisp_Object
212 ftxfont_list (struct frame *f, Lisp_Object spec)
214 Lisp_Object list = ftfont_driver.list (f, spec), tail;
216 for (tail = list; CONSP (tail); tail = XCDR (tail))
217 ASET (XCAR (tail), FONT_TYPE_INDEX, Qftx);
218 return list;
221 static Lisp_Object
222 ftxfont_match (struct frame *f, Lisp_Object spec)
224 Lisp_Object entity = ftfont_driver.match (f, spec);
226 if (VECTORP (entity))
227 ASET (entity, FONT_TYPE_INDEX, Qftx);
228 return entity;
231 static Lisp_Object
232 ftxfont_open (struct frame *f, Lisp_Object entity, int pixel_size)
234 Lisp_Object font_object;
235 struct font *font;
237 font_object = ftfont_driver.open (f, entity, pixel_size);
238 if (NILP (font_object))
239 return Qnil;
240 font = XFONT_OBJECT (font_object);
241 font->driver = &ftxfont_driver;
242 return font_object;
245 static void
246 ftxfont_close (struct font *font)
248 ftfont_driver.close (font);
251 static int
252 ftxfont_draw (struct glyph_string *s, int from, int to, int x, int y,
253 bool with_background)
255 struct frame *f = s->f;
256 struct face *face = s->face;
257 struct font *font = s->font;
258 XPoint p[0x700];
259 int n[7];
260 unsigned *code;
261 int len = to - from;
262 int i;
263 GC *gcs;
264 int xadvance;
266 n[0] = n[1] = n[2] = n[3] = n[4] = n[5] = n[6] = 0;
268 USE_SAFE_ALLOCA;
269 SAFE_NALLOCA (code, 1, len);
270 block_input ();
271 if (with_background)
272 ftxfont_draw_background (f, font, s->gc, x, y, s->width);
273 for (i = 0; i < len; i++)
274 code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
275 | XCHAR2B_BYTE2 (s->char2b + from + i));
277 if (face->gc == s->gc)
279 gcs = ftxfont_get_gcs (f, face->foreground, face->background);
281 else
283 XGCValues xgcv;
284 unsigned long mask = GCForeground | GCBackground;
286 XGetGCValues (FRAME_X_DISPLAY (f), s->gc, mask, &xgcv);
287 gcs = ftxfont_get_gcs (f, xgcv.foreground, xgcv.background);
290 if (gcs)
292 if (s->num_clips)
293 for (i = 0; i < 6; i++)
294 XSetClipRectangles (FRAME_X_DISPLAY (f), gcs[i], 0, 0,
295 s->clip, s->num_clips, Unsorted);
297 for (i = 0; i < len; i++)
299 xadvance = ftxfont_draw_bitmap (f, s->gc, gcs, font, code[i], x, y,
300 p, 0x100, n, i + 1 == len);
301 x += (s->padding_p ? 1 : xadvance);
303 if (s->num_clips)
304 for (i = 0; i < 6; i++)
305 XSetClipMask (FRAME_X_DISPLAY (f), gcs[i], None);
307 else
309 /* We can't draw with antialiasing.
310 s->gc should already have a proper clipping setting. */
311 for (i = 0; i < len; i++)
313 xadvance = ftxfont_draw_bitmap (f, s->gc, NULL, font, code[i], x, y,
314 p, 0x700, n, i + 1 == len);
315 x += (s->padding_p ? 1 : xadvance);
319 unblock_input ();
320 SAFE_FREE ();
322 return len;
325 static int
326 ftxfont_end_for_frame (struct frame *f)
328 struct ftxfont_frame_data *data = font_get_frame_data (f, Qftx);
330 block_input ();
331 while (data)
333 struct ftxfont_frame_data *next = data->next;
334 int i;
336 for (i = 0; i < 6; i++)
337 XFreeGC (FRAME_X_DISPLAY (f), data->gcs[i]);
338 xfree (data);
339 data = next;
341 unblock_input ();
342 font_put_frame_data (f, Qftx, NULL);
343 return 0;
348 void
349 syms_of_ftxfont (void)
351 DEFSYM (Qftx, "ftx");
353 ftxfont_driver = ftfont_driver;
354 ftxfont_driver.type = Qftx;
355 ftxfont_driver.list = ftxfont_list;
356 ftxfont_driver.match = ftxfont_match;
357 ftxfont_driver.open = ftxfont_open;
358 ftxfont_driver.close = ftxfont_close;
359 ftxfont_driver.draw = ftxfont_draw;
360 ftxfont_driver.end_for_frame = ftxfont_end_for_frame;
361 register_font_driver (&ftxfont_driver, NULL);