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