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