*** empty log message ***
[bpt/emacs.git] / src / ftxfont.c
CommitLineData
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
7This file is part of GNU Emacs.
8
9GNU Emacs is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2, or (at your option)
12any later version.
13
14GNU Emacs is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with GNU Emacs; see the file COPYING. If not, write to
21the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22Boston, 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
40static Lisp_Object Qftx;
41
42/* Prototypes for helper function. */
00c37e18
KH
43static GC *ftxfont_get_gcs P_ ((FRAME_PTR, unsigned long, unsigned long));
44static int ftxfont_draw_bitmap P_ ((FRAME_PTR, GC, GC *, struct font *,
45 unsigned, int, int, XPoint *, int, int *,
46 int));
c2f5bfd6
KH
47static void ftxfont_draw_backgrond P_ ((FRAME_PTR, struct font *, GC,
48 int, int, int));
056360d0
KH
49static Font ftxfont_default_fid P_ ((FRAME_PTR));
50
00c37e18
KH
51struct ftxfont_frame_data
52{
53 /* Background and foreground colors. */
54 XColor colors[2];
55 /* GCs interporationg the above colors. gcs[0] is for a color
56 closest to BACKGROUND, and gcs[5] is for a color closest to
57 FOREGROUND. */
58 GC gcs[6];
59 struct ftxfont_frame_data *next;
60};
056360d0 61
00c37e18
KH
62
63/* Return an array of 6 GCs for antialiasing. */
64
65static GC *
66ftxfont_get_gcs (f, foreground, background)
056360d0 67 FRAME_PTR f;
056360d0
KH
68 unsigned long foreground, background;
69{
00c37e18 70 XColor color;
056360d0
KH
71 XGCValues xgcv;
72 int i;
00c37e18
KH
73 struct ftxfont_frame_data *data = font_get_frame_data (f, &ftxfont_driver);
74 struct ftxfont_frame_data *prev = NULL, *this = NULL, *new;
056360d0 75
00c37e18
KH
76 if (data)
77 {
78 for (this = data; this; prev = this, this = this->next)
79 {
80 if (this->colors[0].pixel < background)
81 continue;
82 if (this->colors[0].pixel > background)
83 break;
84 if (this->colors[1].pixel < foreground)
85 continue;
86 if (this->colors[1].pixel > foreground)
87 break;
88 return this->gcs;
89 }
90 }
91
92 new = malloc (sizeof (struct ftxfont_frame_data));
93 if (! new)
94 return NULL;
95 new->next = this;
96 if (prev)
97 {
98 prev->next = new;
99 }
100 else if (font_put_frame_data (f, &ftxfont_driver, new) < 0)
101 {
102 free (new);
103 return NULL;
104 }
105
106 new->colors[0].pixel = background;
107 new->colors[1].pixel = foreground;
056360d0
KH
108
109 BLOCK_INPUT;
00c37e18 110 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), new->colors, 2);
056360d0
KH
111 for (i = 1; i < 7; i++)
112 {
00c37e18
KH
113 /* Interpolate colors linearly. Any better algorithm? */
114 color.red
115 = (new->colors[1].red * i + new->colors[0].red * (8 - i)) / 8;
116 color.green
117 = (new->colors[1].green * i + new->colors[0].green * (8 - i)) / 8;
118 color.blue
119 = (new->colors[1].blue * i + new->colors[0].blue * (8 - i)) / 8;
120 if (! x_alloc_nearest_color (f, FRAME_X_COLORMAP (f), &color))
056360d0 121 break;
00c37e18
KH
122 xgcv.foreground = color.pixel;
123 new->gcs[i - 1] = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
124 GCForeground, &xgcv);
056360d0
KH
125 }
126 UNBLOCK_INPUT;
127
128 if (i < 7)
129 {
130 BLOCK_INPUT;
131 for (i--; i >= 0; i--)
00c37e18 132 XFreeGC (FRAME_X_DISPLAY (f), new->gcs[i]);
056360d0 133 UNBLOCK_INPUT;
00c37e18
KH
134 if (prev)
135 prev->next = new->next;
136 else if (data)
137 font_put_frame_data (f, &ftxfont_driver, new->next);
138 free (new);
139 return NULL;
056360d0 140 }
00c37e18 141 return new->gcs;
056360d0 142}
c2f5bfd6
KH
143
144static int
00c37e18 145ftxfont_draw_bitmap (f, gc_fore, gcs, font, code, x, y, p, size, n, flush)
c2f5bfd6 146 FRAME_PTR f;
00c37e18 147 GC gc_fore, *gcs;
c2f5bfd6
KH
148 struct font *font;
149 unsigned code;
150 int x, y;
151 XPoint *p;
152 int size, *n;
00c37e18 153 int flush;
c2f5bfd6
KH
154{
155 struct font_bitmap bitmap;
156 unsigned char *b;
157 int i, j;
158
056360d0 159 if (ftfont_driver.get_bitmap (font, code, &bitmap, size > 0x100 ? 1 : 8) < 0)
c2f5bfd6 160 return 0;
00c37e18 161 if (size > 0x100)
c2f5bfd6 162 {
00c37e18
KH
163 for (i = 0, b = bitmap.buffer; i < bitmap.rows;
164 i++, b += bitmap.pitch)
c2f5bfd6
KH
165 {
166 for (j = 0; j < bitmap.width; j++)
167 if (b[j / 8] & (1 << (7 - (j % 8))))
168 {
169 p[n[0]].x = x + bitmap.left + j;
170 p[n[0]].y = y - bitmap.top + i;
00c37e18 171 if (++n[0] == size)
c2f5bfd6
KH
172 {
173 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
00c37e18 174 gc_fore, p, size, CoordModeOrigin);
c2f5bfd6
KH
175 n[0] = 0;
176 }
177 }
178 }
00c37e18
KH
179 if (flush && n[0] > 0)
180 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
181 gc_fore, p, n[0], CoordModeOrigin);
182 }
183 else
184 {
185 for (i = 0, b = bitmap.buffer; i < bitmap.rows;
186 i++, b += bitmap.pitch)
c2f5bfd6
KH
187 {
188 for (j = 0; j < bitmap.width; j++)
189 {
00c37e18
KH
190 int idx = (bitmap.bits_per_pixel == 1
191 ? ((b[j / 8] & (1 << (7 - (j % 8)))) ? 6 : -1)
192 : (b[j] >> 5) - 1);
c2f5bfd6
KH
193
194 if (idx >= 0)
195 {
196 XPoint *pp = p + size * idx;
197
198 pp[n[idx]].x = x + bitmap.left + j;
199 pp[n[idx]].y = y - bitmap.top + i;
00c37e18 200 if (++(n[idx]) == size)
c2f5bfd6
KH
201 {
202 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
00c37e18
KH
203 idx == 6 ? gc_fore : gcs[idx], pp, size,
204 CoordModeOrigin);
c2f5bfd6
KH
205 n[idx] = 0;
206 }
207 }
208 }
209 }
00c37e18
KH
210 if (flush)
211 {
212 for (i = 0; i < 6; i++)
213 if (n[i] > 0)
214 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
215 gcs[i], p + 0x100 * i, n[i], CoordModeOrigin);
216 if (n[6] > 0)
217 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
218 gc_fore, p + 0x600, n[6], CoordModeOrigin);
219 }
c2f5bfd6
KH
220 }
221
222 if (ftfont_driver.free_bitmap)
223 ftfont_driver.free_bitmap (font, &bitmap);
224
225 return bitmap.advance;
226}
227
228static void
229ftxfont_draw_backgrond (f, font, gc, x, y, width)
230 FRAME_PTR f;
231 struct font *font;
232 GC gc;
233 int x, y, width;
234{
235 XGCValues xgcv;
236
237 XGetGCValues (FRAME_X_DISPLAY (f), gc,
238 GCForeground | GCBackground, &xgcv);
239 XSetForeground (FRAME_X_DISPLAY (f), gc, xgcv.background);
240 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
056360d0 241 x, y - font->ascent, width, y + font->descent);
c2f5bfd6
KH
242 XSetForeground (FRAME_X_DISPLAY (f), gc, xgcv.foreground);
243}
244
056360d0
KH
245/* Return the default Font ID on frame F. */
246
247static Font
248ftxfont_default_fid (f)
249 FRAME_PTR f;
250{
251 static int fid_known;
252 static Font fid;
253
254 if (! fid_known)
255 {
256 fid = XLoadFont (FRAME_X_DISPLAY (f), "fixed");
257 if (! fid)
258 {
259 fid = XLoadFont (FRAME_X_DISPLAY (f), "*");
260 if (! fid)
261 abort ();
262 }
263 fid_known = 1;
264 }
265 return fid;
266}
267
c2f5bfd6
KH
268/* Prototypes for font-driver methods. */
269static Lisp_Object ftxfont_list P_ ((Lisp_Object, Lisp_Object));
3a91626c 270static Lisp_Object ftxfont_match P_ ((Lisp_Object, Lisp_Object));
c2f5bfd6
KH
271static struct font *ftxfont_open P_ ((FRAME_PTR, Lisp_Object, int));
272static void ftxfont_close P_ ((FRAME_PTR, struct font *));
c2f5bfd6
KH
273static int ftxfont_draw P_ ((struct glyph_string *, int, int, int, int, int));
274
275struct font_driver ftxfont_driver;
276
277static Lisp_Object
278ftxfont_list (frame, spec)
279 Lisp_Object frame;
280 Lisp_Object spec;
281{
282 Lisp_Object val = ftfont_driver.list (frame, spec);
283
284 if (! NILP (val))
285 {
286 int i;
287
288 for (i = 0; i < ASIZE (val); i++)
289 ASET (AREF (val, i), FONT_TYPE_INDEX, Qftx);
290 }
291 return val;
292}
293
3a91626c
KH
294static Lisp_Object
295ftxfont_match (frame, spec)
296 Lisp_Object frame;
297 Lisp_Object spec;
298{
299 Lisp_Object entity = ftfont_driver.match (frame, spec);
300
301 if (VECTORP (entity))
302 ASET (entity, FONT_TYPE_INDEX, Qftx);
303 return entity;
304}
305
c2f5bfd6
KH
306static struct font *
307ftxfont_open (f, entity, pixel_size)
308 FRAME_PTR f;
309 Lisp_Object entity;
310 int pixel_size;
311{
312 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
313 struct font *font;
314 XFontStruct *xfont = malloc (sizeof (XFontStruct));
315
316 if (! xfont)
317 return NULL;
318 font = ftfont_driver.open (f, entity, pixel_size);
319 if (! font)
320 {
321 free (xfont);
322 return NULL;
323 }
324
056360d0 325 xfont->fid = ftxfont_default_fid (f);
c2f5bfd6
KH
326 xfont->ascent = font->ascent;
327 xfont->descent = font->descent;
328 xfont->max_bounds.width = font->font.size;
329 xfont->min_bounds.width = font->min_width;
330 font->font.font = xfont;
331 font->driver = &ftxfont_driver;
332
333 dpyinfo->n_fonts++;
334
335 /* Set global flag fonts_changed_p to non-zero if the font loaded
336 has a character with a smaller width than any other character
337 before, or if the font loaded has a smaller height than any other
338 font loaded before. If this happens, it will make a glyph matrix
339 reallocation necessary. */
340 if (dpyinfo->n_fonts == 1)
341 {
342 dpyinfo->smallest_font_height = font->font.height;
343 dpyinfo->smallest_char_width = font->min_width;
344 fonts_changed_p = 1;
345 }
346 else
347 {
348 if (dpyinfo->smallest_font_height > font->font.height)
349 dpyinfo->smallest_font_height = font->font.height, fonts_changed_p |= 1;
350 if (dpyinfo->smallest_char_width > font->min_width)
351 dpyinfo->smallest_char_width = font->min_width, fonts_changed_p |= 1;
352 }
353
354 return font;
355}
356
357static void
358ftxfont_close (f, font)
359 FRAME_PTR f;
360 struct font *font;
361{
362 ftfont_driver.close (f, font);
363 FRAME_X_DISPLAY_INFO (f)->n_fonts--;
364}
365
c2f5bfd6
KH
366static int
367ftxfont_draw (s, from, to, x, y, with_background)
368 struct glyph_string *s;
369 int from, to, x, y, with_background;
370{
371 FRAME_PTR f = s->f;
372 struct face *face = s->face;
056360d0 373 struct font *font = (struct font *) face->font_info;
c2f5bfd6
KH
374 XPoint p[0x700];
375 int n[7];
376 unsigned *code;
377 int len = to - from;
378 int i;
056360d0 379 GC *gcs;
c2f5bfd6
KH
380
381 n[0] = n[1] = n[2] = n[3] = n[4] = n[5] = n[6] = 0;
382
383 BLOCK_INPUT;
c2f5bfd6
KH
384 if (with_background)
385 ftxfont_draw_backgrond (f, font, s->gc, x, y, s->width);
386 code = alloca (sizeof (unsigned) * len);
387 for (i = 0; i < len; i++)
388 code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
389 | XCHAR2B_BYTE2 (s->char2b + from + i));
390
00c37e18
KH
391 if (face->gc == s->gc)
392 {
393 gcs = ftxfont_get_gcs (f, face->foreground, face->background);
394 }
395 else
c2f5bfd6 396 {
056360d0
KH
397 XGCValues xgcv;
398 unsigned long mask = GCForeground | GCBackground;
399
056360d0 400 XGetGCValues (FRAME_X_DISPLAY (f), s->gc, mask, &xgcv);
00c37e18 401 gcs = ftxfont_get_gcs (f, xgcv.foreground, xgcv.background);
056360d0
KH
402 }
403
00c37e18 404 if (gcs)
056360d0 405 {
00c37e18
KH
406 if (s->num_clips)
407 for (i = 0; i < 6; i++)
408 XSetClipRectangles (FRAME_X_DISPLAY (f), gcs[i], 0, 0,
409 s->clip, s->num_clips, Unsorted);
410
c2f5bfd6 411 for (i = 0; i < len; i++)
00c37e18
KH
412 x += ftxfont_draw_bitmap (f, s->gc, gcs, font, code[i], x, y,
413 p, 0x100, n, i + 1 == len);
414 if (s->num_clips)
415 for (i = 0; i < 6; i++)
416 XSetClipMask (FRAME_X_DISPLAY (f), gcs[i], None);
c2f5bfd6
KH
417 }
418 else
419 {
00c37e18
KH
420 /* We can't draw with antialiasing.
421 s->gc should already have a proper clipping setting. */
c2f5bfd6 422 for (i = 0; i < len; i++)
00c37e18
KH
423 x += ftxfont_draw_bitmap (f, s->gc, NULL, font, code[i], x, y,
424 p, 0x700, n, i + 1 == len);
c2f5bfd6
KH
425 }
426
427 UNBLOCK_INPUT;
428
429 return len;
430}
431
00c37e18
KH
432static int
433ftxfont_end_for_frame (f)
434 FRAME_PTR f;
435{
436 struct ftxfont_frame_data *data = font_get_frame_data (f, &ftxfont_driver);
437
438 BLOCK_INPUT;
439 while (data)
440 {
441 struct ftxfont_frame_data *next = data->next;
442 int i;
443
444 for (i = 0; i < 7; i++)
445 XFreeGC (FRAME_X_DISPLAY (f), data->gcs[i]);
446 free (data);
447 data = next;
448 }
449 UNBLOCK_INPUT;
450 return 0;
451}
452
c2f5bfd6
KH
453\f
454
455void
456syms_of_ftxfont ()
457{
458 DEFSYM (Qftx, "ftx");
459
460 ftxfont_driver = ftfont_driver;
461 ftxfont_driver.type = Qftx;
462 ftxfont_driver.list = ftxfont_list;
3a91626c 463 ftxfont_driver.match = ftxfont_match;
c2f5bfd6
KH
464 ftxfont_driver.open = ftxfont_open;
465 ftxfont_driver.close = ftxfont_close;
c2f5bfd6 466 ftxfont_driver.draw = ftxfont_draw;
00c37e18 467 ftxfont_driver.end_for_frame = ftxfont_end_for_frame;
c2f5bfd6
KH
468 register_font_driver (&ftxfont_driver, NULL);
469}
885b7d09
MB
470
471/* arch-tag: 59bd3469-5330-413f-b29d-1aa36492abe8
472 (do not change this comment) */