Commit | Line | Data |
---|---|---|
c2f5bfd6 KH |
1 | /* ftxfont.c -- FreeType font driver on X (without using XFT). |
2 | Copyright (C) 2006 Free Software Foundation, Inc. | |
3 | Copyright (C) 2006 | |
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 2, or (at your option) | |
12 | 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; see the file COPYING. If not, write to | |
21 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
22 | Boston, MA 02110-1301, USA. */ | |
23 | ||
24 | #include <config.h> | |
25 | #include <stdio.h> | |
26 | #include <X11/Xlib.h> | |
27 | ||
28 | #include "lisp.h" | |
29 | #include "dispextern.h" | |
30 | #include "xterm.h" | |
31 | #include "frame.h" | |
32 | #include "blockinput.h" | |
33 | #include "character.h" | |
34 | #include "charset.h" | |
35 | #include "fontset.h" | |
36 | #include "font.h" | |
37 | ||
38 | /* FTX font driver. */ | |
39 | ||
40 | static Lisp_Object Qftx; | |
41 | ||
42 | /* Prototypes for helper function. */ | |
43 | static int ftxfont_draw_bitmap P_ ((FRAME_PTR, GC *, struct font *, unsigned, | |
44 | int, int, XPoint *, int, int *n)); | |
45 | static void ftxfont_draw_backgrond P_ ((FRAME_PTR, struct font *, GC, | |
46 | int, int, int)); | |
47 | ||
48 | static int | |
49 | ftxfont_draw_bitmap (f, gc, font, code, x, y, p, size, n) | |
50 | FRAME_PTR f; | |
51 | GC *gc; | |
52 | struct font *font; | |
53 | unsigned code; | |
54 | int x, y; | |
55 | XPoint *p; | |
56 | int size, *n; | |
57 | { | |
58 | struct font_bitmap bitmap; | |
59 | unsigned char *b; | |
60 | int i, j; | |
61 | ||
62 | if (ftfont_driver.get_bitmap (font, code, &bitmap, 1) < 0) | |
63 | return 0; | |
64 | for (i = 0, b = bitmap.buffer; i < bitmap.rows; | |
65 | i++, b += bitmap.pitch) | |
66 | { | |
67 | if (size > 0x100) | |
68 | { | |
69 | for (j = 0; j < bitmap.width; j++) | |
70 | if (b[j / 8] & (1 << (7 - (j % 8)))) | |
71 | { | |
72 | p[n[0]].x = x + bitmap.left + j; | |
73 | p[n[0]].y = y - bitmap.top + i; | |
74 | if (++n[0] == 0x400) | |
75 | { | |
76 | XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
77 | gc[0], p, size, CoordModeOrigin); | |
78 | n[0] = 0; | |
79 | } | |
80 | } | |
81 | } | |
82 | else | |
83 | { | |
84 | for (j = 0; j < bitmap.width; j++) | |
85 | { | |
86 | int idx = (b[j] >> 5) - 1; | |
87 | ||
88 | if (idx >= 0) | |
89 | { | |
90 | XPoint *pp = p + size * idx; | |
91 | ||
92 | pp[n[idx]].x = x + bitmap.left + j; | |
93 | pp[n[idx]].y = y - bitmap.top + i; | |
94 | if (++(n[idx]) == 0x100) | |
95 | { | |
96 | XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
97 | gc[idx], pp, size, CoordModeOrigin); | |
98 | n[idx] = 0; | |
99 | } | |
100 | } | |
101 | } | |
102 | } | |
103 | } | |
104 | ||
105 | if (ftfont_driver.free_bitmap) | |
106 | ftfont_driver.free_bitmap (font, &bitmap); | |
107 | ||
108 | return bitmap.advance; | |
109 | } | |
110 | ||
111 | static void | |
112 | ftxfont_draw_backgrond (f, font, gc, x, y, width) | |
113 | FRAME_PTR f; | |
114 | struct font *font; | |
115 | GC gc; | |
116 | int x, y, width; | |
117 | { | |
118 | XGCValues xgcv; | |
119 | ||
120 | XGetGCValues (FRAME_X_DISPLAY (f), gc, | |
121 | GCForeground | GCBackground, &xgcv); | |
122 | XSetForeground (FRAME_X_DISPLAY (f), gc, xgcv.background); | |
123 | XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc, | |
124 | x, y - font->ascent, width, font->font.height); | |
125 | XSetForeground (FRAME_X_DISPLAY (f), gc, xgcv.foreground); | |
126 | } | |
127 | ||
128 | /* Prototypes for font-driver methods. */ | |
129 | static Lisp_Object ftxfont_list P_ ((Lisp_Object, Lisp_Object)); | |
130 | static struct font *ftxfont_open P_ ((FRAME_PTR, Lisp_Object, int)); | |
131 | static void ftxfont_close P_ ((FRAME_PTR, struct font *)); | |
132 | static int ftxfont_prepare_face (FRAME_PTR, struct face *); | |
133 | static void ftxfont_done_face (FRAME_PTR, struct face *); | |
134 | ||
135 | static int ftxfont_draw P_ ((struct glyph_string *, int, int, int, int, int)); | |
136 | ||
137 | struct font_driver ftxfont_driver; | |
138 | ||
139 | static Lisp_Object | |
140 | ftxfont_list (frame, spec) | |
141 | Lisp_Object frame; | |
142 | Lisp_Object spec; | |
143 | { | |
144 | Lisp_Object val = ftfont_driver.list (frame, spec); | |
145 | ||
146 | if (! NILP (val)) | |
147 | { | |
148 | int i; | |
149 | ||
150 | for (i = 0; i < ASIZE (val); i++) | |
151 | ASET (AREF (val, i), FONT_TYPE_INDEX, Qftx); | |
152 | } | |
153 | return val; | |
154 | } | |
155 | ||
156 | static struct font * | |
157 | ftxfont_open (f, entity, pixel_size) | |
158 | FRAME_PTR f; | |
159 | Lisp_Object entity; | |
160 | int pixel_size; | |
161 | { | |
162 | Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
163 | struct font *font; | |
164 | XFontStruct *xfont = malloc (sizeof (XFontStruct)); | |
165 | ||
166 | if (! xfont) | |
167 | return NULL; | |
168 | font = ftfont_driver.open (f, entity, pixel_size); | |
169 | if (! font) | |
170 | { | |
171 | free (xfont); | |
172 | return NULL; | |
173 | } | |
174 | ||
175 | xfont->fid = FRAME_FONT (f)->fid; | |
176 | xfont->ascent = font->ascent; | |
177 | xfont->descent = font->descent; | |
178 | xfont->max_bounds.width = font->font.size; | |
179 | xfont->min_bounds.width = font->min_width; | |
180 | font->font.font = xfont; | |
181 | font->driver = &ftxfont_driver; | |
182 | ||
183 | dpyinfo->n_fonts++; | |
184 | ||
185 | /* Set global flag fonts_changed_p to non-zero if the font loaded | |
186 | has a character with a smaller width than any other character | |
187 | before, or if the font loaded has a smaller height than any other | |
188 | font loaded before. If this happens, it will make a glyph matrix | |
189 | reallocation necessary. */ | |
190 | if (dpyinfo->n_fonts == 1) | |
191 | { | |
192 | dpyinfo->smallest_font_height = font->font.height; | |
193 | dpyinfo->smallest_char_width = font->min_width; | |
194 | fonts_changed_p = 1; | |
195 | } | |
196 | else | |
197 | { | |
198 | if (dpyinfo->smallest_font_height > font->font.height) | |
199 | dpyinfo->smallest_font_height = font->font.height, fonts_changed_p |= 1; | |
200 | if (dpyinfo->smallest_char_width > font->min_width) | |
201 | dpyinfo->smallest_char_width = font->min_width, fonts_changed_p |= 1; | |
202 | } | |
203 | ||
204 | return font; | |
205 | } | |
206 | ||
207 | static void | |
208 | ftxfont_close (f, font) | |
209 | FRAME_PTR f; | |
210 | struct font *font; | |
211 | { | |
212 | ftfont_driver.close (f, font); | |
213 | FRAME_X_DISPLAY_INFO (f)->n_fonts--; | |
214 | } | |
215 | ||
216 | static int | |
217 | ftxfont_prepare_face (f, face) | |
218 | FRAME_PTR f; | |
219 | struct face *face; | |
220 | { | |
221 | GC gc[6]; | |
222 | XColor colors[3]; | |
223 | XGCValues xgcv; | |
224 | unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures; | |
225 | int i; | |
226 | ||
227 | face->extra = NULL; | |
228 | ||
229 | /* Here, we create 6 more GCs to simulate anti-aliasing. */ | |
230 | BLOCK_INPUT; | |
231 | XGetGCValues (FRAME_X_DISPLAY (f), face->gc, mask, &xgcv); | |
232 | colors[0].pixel = face->foreground; | |
233 | colors[1].pixel = face->background; | |
234 | XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, 2); | |
235 | for (i = 1; i < 7; i++) | |
236 | { | |
237 | colors[2].red = (colors[0].red * i + colors[1].red * (7 - i)) / 7; | |
238 | colors[2].green = (colors[0].green * i + colors[1].green * (7 - i)) / 7; | |
239 | colors[2].blue = (colors[0].blue * i + colors[1].blue * (7 - i)) / 7; | |
240 | if (! x_alloc_nearest_color (f, FRAME_X_COLORMAP (f), &colors[2])) | |
241 | break; | |
242 | xgcv.foreground = colors[2].pixel; | |
243 | gc[i - 1] = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
244 | mask, &xgcv); | |
245 | } | |
246 | UNBLOCK_INPUT; | |
247 | ||
248 | if (i < 7) | |
249 | return -1; | |
250 | face->extra = malloc (sizeof (GC) * 7); | |
251 | if (! face->extra) | |
252 | return -1; | |
253 | for (i = 0; i < 6; i++) | |
254 | ((GC *) face->extra)[i] = gc[i]; | |
255 | ((GC *) face->extra)[i] = face->gc; | |
256 | return 0; | |
257 | } | |
258 | ||
259 | static void | |
260 | ftxfont_done_face (f, face) | |
261 | FRAME_PTR f; | |
262 | struct face *face; | |
263 | { | |
264 | if (face->extra) | |
265 | { | |
266 | int i; | |
267 | ||
268 | BLOCK_INPUT; | |
269 | for (i = 0; i < 7; i++) | |
270 | XFreeGC (FRAME_X_DISPLAY (f), ((GC *) face->extra)[i]); | |
271 | UNBLOCK_INPUT; | |
272 | free (face->extra); | |
273 | face->extra = NULL; | |
274 | } | |
275 | } | |
276 | ||
277 | static int | |
278 | ftxfont_draw (s, from, to, x, y, with_background) | |
279 | struct glyph_string *s; | |
280 | int from, to, x, y, with_background; | |
281 | { | |
282 | FRAME_PTR f = s->f; | |
283 | struct face *face = s->face; | |
284 | struct font *font = (struct font *) face->font; | |
285 | XPoint p[0x700]; | |
286 | int n[7]; | |
287 | unsigned *code; | |
288 | int len = to - from; | |
289 | int i; | |
290 | ||
291 | n[0] = n[1] = n[2] = n[3] = n[4] = n[5] = n[6] = 0; | |
292 | ||
293 | BLOCK_INPUT; | |
294 | ||
295 | if (with_background) | |
296 | ftxfont_draw_backgrond (f, font, s->gc, x, y, s->width); | |
297 | code = alloca (sizeof (unsigned) * len); | |
298 | for (i = 0; i < len; i++) | |
299 | code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8) | |
300 | | XCHAR2B_BYTE2 (s->char2b + from + i)); | |
301 | ||
302 | if (! face->extra) | |
303 | { | |
304 | for (i = 0; i < len; i++) | |
305 | x += ftxfont_draw_bitmap (f, &face->gc, font, code[i], x, y, | |
306 | p, 0x700, n); | |
307 | if (n[0] > 0) | |
308 | XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
309 | face->gc, p, n[0], CoordModeOrigin); | |
310 | } | |
311 | else | |
312 | { | |
313 | GC *gc = face->extra; | |
314 | ||
315 | for (i = 0; i < len; i++) | |
316 | x += ftxfont_draw_bitmap (f, &face->gc, font, code[i], x, y, | |
317 | p, 0x100, n); | |
318 | for (i = 0; i < 7; i++) | |
319 | if (n[i] > 0) | |
320 | XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), | |
321 | gc[i], p + 0x100 * i, n[i], CoordModeOrigin); | |
322 | } | |
323 | ||
324 | UNBLOCK_INPUT; | |
325 | ||
326 | return len; | |
327 | } | |
328 | ||
329 | \f | |
330 | ||
331 | void | |
332 | syms_of_ftxfont () | |
333 | { | |
334 | DEFSYM (Qftx, "ftx"); | |
335 | ||
336 | ftxfont_driver = ftfont_driver; | |
337 | ftxfont_driver.type = Qftx; | |
338 | ftxfont_driver.list = ftxfont_list; | |
339 | ftxfont_driver.open = ftxfont_open; | |
340 | ftxfont_driver.close = ftxfont_close; | |
341 | ftxfont_driver.prepare_face = ftxfont_prepare_face; | |
342 | ftxfont_driver.done_face = ftxfont_done_face; | |
343 | ftxfont_driver.draw = ftxfont_draw; | |
344 | ||
345 | register_font_driver (&ftxfont_driver, NULL); | |
346 | } |