Fix insertion of " inside @smallexample.
[emacs.git] / src / ftxfont.c
blob5effe6e91044325d7e1148df9db1da484020796b
1 /* ftxfont.c -- FreeType font driver on X (without using XFT).
2 Copyright (C) 2006-2012 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 "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"
36 /* FTX font driver. */
38 static Lisp_Object Qftx;
40 #if defined HAVE_XFT || !defined HAVE_FREETYPE
41 static
42 #endif
43 struct font_driver ftxfont_driver;
45 struct ftxfont_frame_data
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;
57 /* Return an array of 6 GCs for antialiasing. */
59 static GC *
60 ftxfont_get_gcs (FRAME_PTR f, long unsigned int foreground, long unsigned int background)
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;
68 if (data)
70 for (this = data; this; prev = this, this = this->next)
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;
84 new = malloc (sizeof *new);
85 if (! new)
86 return NULL;
87 new->next = this;
88 if (prev)
90 prev->next = new;
92 else if (font_put_frame_data (f, &ftxfont_driver, new) < 0)
94 free (new);
95 return NULL;
98 new->colors[0].pixel = background;
99 new->colors[1].pixel = foreground;
101 block_input ();
102 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), new->colors, 2);
103 for (i = 1; i < 7; i++)
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);
118 unblock_input ();
120 if (i < 7)
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;
133 return new->gcs;
136 static int
137 ftxfont_draw_bitmap (FRAME_PTR 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)
141 struct font_bitmap bitmap;
142 unsigned char *b;
143 int i, j;
145 if (ftfont_driver.get_bitmap (font, code, &bitmap, size > 0x100 ? 1 : 8) < 0)
146 return 0;
147 if (size > 0x100)
149 for (i = 0, b = bitmap.buffer; i < bitmap.rows;
150 i++, b += bitmap.pitch)
152 for (j = 0; j < bitmap.width; j++)
153 if (b[j / 8] & (1 << (7 - (j % 8))))
155 p[n[0]].x = x + bitmap.left + j;
156 p[n[0]].y = y - bitmap.top + i;
157 if (++n[0] == size)
159 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
160 gc_fore, p, size, CoordModeOrigin);
161 n[0] = 0;
165 if (flush && n[0] > 0)
166 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
167 gc_fore, p, n[0], CoordModeOrigin);
169 else
171 for (i = 0, b = bitmap.buffer; i < bitmap.rows;
172 i++, b += bitmap.pitch)
174 for (j = 0; j < bitmap.width; j++)
176 int idx = (bitmap.bits_per_pixel == 1
177 ? ((b[j / 8] & (1 << (7 - (j % 8)))) ? 6 : -1)
178 : (b[j] >> 5) - 1);
180 if (idx >= 0)
182 XPoint *pp = p + size * idx;
184 pp[n[idx]].x = x + bitmap.left + j;
185 pp[n[idx]].y = y - bitmap.top + i;
186 if (++(n[idx]) == size)
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;
196 if (flush)
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);
208 if (ftfont_driver.free_bitmap)
209 ftfont_driver.free_bitmap (font, &bitmap);
211 return bitmap.advance;
214 static void
215 ftxfont_draw_background (FRAME_PTR f, struct font *font, GC gc, int x, int y,
216 int width)
218 XGCValues xgcv;
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);
228 static Lisp_Object
229 ftxfont_list (Lisp_Object frame, Lisp_Object spec)
231 Lisp_Object list = ftfont_driver.list (frame, spec), tail;
233 for (tail = list; CONSP (tail); tail = XCDR (tail))
234 ASET (XCAR (tail), FONT_TYPE_INDEX, Qftx);
235 return list;
238 static Lisp_Object
239 ftxfont_match (Lisp_Object frame, Lisp_Object spec)
241 Lisp_Object entity = ftfont_driver.match (frame, spec);
243 if (VECTORP (entity))
244 ASET (entity, FONT_TYPE_INDEX, Qftx);
245 return entity;
248 static Lisp_Object
249 ftxfont_open (FRAME_PTR f, Lisp_Object entity, int pixel_size)
251 Lisp_Object font_object;
252 struct font *font;
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 font->driver = &ftxfont_driver;
259 return font_object;
262 static void
263 ftxfont_close (FRAME_PTR f, struct font *font)
265 ftfont_driver.close (f, font);
268 static int
269 ftxfont_draw (struct glyph_string *s, int from, int to, int x, int y,
270 bool with_background)
272 FRAME_PTR f = s->f;
273 struct face *face = s->face;
274 struct font *font = s->font;
275 XPoint p[0x700];
276 int n[7];
277 unsigned *code;
278 int len = to - from;
279 int i;
280 GC *gcs;
281 int xadvance;
283 n[0] = n[1] = n[2] = n[3] = n[4] = n[5] = n[6] = 0;
285 block_input ();
286 if (with_background)
287 ftxfont_draw_background (f, font, s->gc, x, y, s->width);
288 code = alloca (sizeof (unsigned) * len);
289 for (i = 0; i < len; i++)
290 code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
291 | XCHAR2B_BYTE2 (s->char2b + from + i));
293 if (face->gc == s->gc)
295 gcs = ftxfont_get_gcs (f, face->foreground, face->background);
297 else
299 XGCValues xgcv;
300 unsigned long mask = GCForeground | GCBackground;
302 XGetGCValues (FRAME_X_DISPLAY (f), s->gc, mask, &xgcv);
303 gcs = ftxfont_get_gcs (f, xgcv.foreground, xgcv.background);
306 if (gcs)
308 if (s->num_clips)
309 for (i = 0; i < 6; i++)
310 XSetClipRectangles (FRAME_X_DISPLAY (f), gcs[i], 0, 0,
311 s->clip, s->num_clips, Unsorted);
313 for (i = 0; i < len; i++)
315 xadvance = ftxfont_draw_bitmap (f, s->gc, gcs, font, code[i], x, y,
316 p, 0x100, n, i + 1 == len);
317 x += (s->padding_p ? 1 : xadvance);
319 if (s->num_clips)
320 for (i = 0; i < 6; i++)
321 XSetClipMask (FRAME_X_DISPLAY (f), gcs[i], None);
323 else
325 /* We can't draw with antialiasing.
326 s->gc should already have a proper clipping setting. */
327 for (i = 0; i < len; i++)
329 xadvance = ftxfont_draw_bitmap (f, s->gc, NULL, font, code[i], x, y,
330 p, 0x700, n, i + 1 == len);
331 x += (s->padding_p ? 1 : xadvance);
335 unblock_input ();
337 return len;
340 static int
341 ftxfont_end_for_frame (FRAME_PTR f)
343 struct ftxfont_frame_data *data = font_get_frame_data (f, &ftxfont_driver);
345 block_input ();
346 while (data)
348 struct ftxfont_frame_data *next = data->next;
349 int i;
351 for (i = 0; i < 6; i++)
352 XFreeGC (FRAME_X_DISPLAY (f), data->gcs[i]);
353 free (data);
354 data = next;
356 unblock_input ();
357 font_put_frame_data (f, &ftxfont_driver, NULL);
358 return 0;
363 void
364 syms_of_ftxfont (void)
366 DEFSYM (Qftx, "ftx");
368 ftxfont_driver = ftfont_driver;
369 ftxfont_driver.type = Qftx;
370 ftxfont_driver.list = ftxfont_list;
371 ftxfont_driver.match = ftxfont_match;
372 ftxfont_driver.open = ftxfont_open;
373 ftxfont_driver.close = ftxfont_close;
374 ftxfont_driver.draw = ftxfont_draw;
375 ftxfont_driver.end_for_frame = ftxfont_end_for_frame;
376 register_font_driver (&ftxfont_driver, NULL);