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