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