(ftfont_get_bitmap): Set bitmap->bits_per_pixel.
[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. */
056360d0
KH
43static int ftxfont_create_gcs P_ ((FRAME_PTR, GC *,
44 unsigned long, unsigned long));
c2f5bfd6
KH
45static int ftxfont_draw_bitmap P_ ((FRAME_PTR, GC *, struct font *, unsigned,
46 int, int, XPoint *, int, int *n));
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
51/* Create 6 GCs for antialiasing by interpolating colors FOREGROUND
52 and BACKGROUND. GCS[0] is closest to BACKGROUND, and GCS[5] is
53 closest to FOREGROUND. */
54
55static int
56ftxfont_create_gcs (f, gcs, foreground, background)
57 FRAME_PTR f;
58 GC *gcs;
59 unsigned long foreground, background;
60{
61 XColor colors[3];
62 XGCValues xgcv;
63 int i;
64
65 colors[0].pixel = foreground;
66 colors[1].pixel = background;
67
68 BLOCK_INPUT;
69 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, 2);
70 for (i = 1; i < 7; i++)
71 {
72 colors[2].red = (colors[0].red * i + colors[1].red * (8 - i)) / 8;
73 colors[2].green = (colors[0].green * i + colors[1].green * (8 - i)) / 8;
74 colors[2].blue = (colors[0].blue * i + colors[1].blue * (8 - i)) / 8;
75 if (! x_alloc_nearest_color (f, FRAME_X_COLORMAP (f), &colors[2]))
76 break;
77 xgcv.foreground = colors[2].pixel;
78 gcs[i - 1] = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
79 GCForeground, &xgcv);
80 }
81 UNBLOCK_INPUT;
82
83 if (i < 7)
84 {
85 BLOCK_INPUT;
86 for (i--; i >= 0; i--)
87 XFreeGC (FRAME_X_DISPLAY (f), gcs[i]);
88 UNBLOCK_INPUT;
89 return -1;
90 }
91 return 0;
92}
c2f5bfd6
KH
93
94static int
95ftxfont_draw_bitmap (f, gc, font, code, x, y, p, size, n)
96 FRAME_PTR f;
97 GC *gc;
98 struct font *font;
99 unsigned code;
100 int x, y;
101 XPoint *p;
102 int size, *n;
103{
104 struct font_bitmap bitmap;
105 unsigned char *b;
106 int i, j;
107
056360d0 108 if (ftfont_driver.get_bitmap (font, code, &bitmap, size > 0x100 ? 1 : 8) < 0)
c2f5bfd6
KH
109 return 0;
110 for (i = 0, b = bitmap.buffer; i < bitmap.rows;
111 i++, b += bitmap.pitch)
112 {
113 if (size > 0x100)
114 {
115 for (j = 0; j < bitmap.width; j++)
116 if (b[j / 8] & (1 << (7 - (j % 8))))
117 {
118 p[n[0]].x = x + bitmap.left + j;
119 p[n[0]].y = y - bitmap.top + i;
120 if (++n[0] == 0x400)
121 {
122 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
123 gc[0], p, size, CoordModeOrigin);
124 n[0] = 0;
125 }
126 }
127 }
128 else
129 {
130 for (j = 0; j < bitmap.width; j++)
131 {
132 int idx = (b[j] >> 5) - 1;
133
134 if (idx >= 0)
135 {
136 XPoint *pp = p + size * idx;
137
138 pp[n[idx]].x = x + bitmap.left + j;
139 pp[n[idx]].y = y - bitmap.top + i;
140 if (++(n[idx]) == 0x100)
141 {
142 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
143 gc[idx], pp, size, CoordModeOrigin);
144 n[idx] = 0;
145 }
146 }
147 }
148 }
149 }
150
151 if (ftfont_driver.free_bitmap)
152 ftfont_driver.free_bitmap (font, &bitmap);
153
154 return bitmap.advance;
155}
156
157static void
158ftxfont_draw_backgrond (f, font, gc, x, y, width)
159 FRAME_PTR f;
160 struct font *font;
161 GC gc;
162 int x, y, width;
163{
164 XGCValues xgcv;
165
166 XGetGCValues (FRAME_X_DISPLAY (f), gc,
167 GCForeground | GCBackground, &xgcv);
168 XSetForeground (FRAME_X_DISPLAY (f), gc, xgcv.background);
169 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
056360d0 170 x, y - font->ascent, width, y + font->descent);
c2f5bfd6
KH
171 XSetForeground (FRAME_X_DISPLAY (f), gc, xgcv.foreground);
172}
173
056360d0
KH
174/* Return the default Font ID on frame F. */
175
176static Font
177ftxfont_default_fid (f)
178 FRAME_PTR f;
179{
180 static int fid_known;
181 static Font fid;
182
183 if (! fid_known)
184 {
185 fid = XLoadFont (FRAME_X_DISPLAY (f), "fixed");
186 if (! fid)
187 {
188 fid = XLoadFont (FRAME_X_DISPLAY (f), "*");
189 if (! fid)
190 abort ();
191 }
192 fid_known = 1;
193 }
194 return fid;
195}
196
c2f5bfd6
KH
197/* Prototypes for font-driver methods. */
198static Lisp_Object ftxfont_list P_ ((Lisp_Object, Lisp_Object));
3a91626c 199static Lisp_Object ftxfont_match P_ ((Lisp_Object, Lisp_Object));
c2f5bfd6
KH
200static struct font *ftxfont_open P_ ((FRAME_PTR, Lisp_Object, int));
201static void ftxfont_close P_ ((FRAME_PTR, struct font *));
202static int ftxfont_prepare_face (FRAME_PTR, struct face *);
203static void ftxfont_done_face (FRAME_PTR, struct face *);
204
205static int ftxfont_draw P_ ((struct glyph_string *, int, int, int, int, int));
206
207struct font_driver ftxfont_driver;
208
209static Lisp_Object
210ftxfont_list (frame, spec)
211 Lisp_Object frame;
212 Lisp_Object spec;
213{
214 Lisp_Object val = ftfont_driver.list (frame, spec);
215
216 if (! NILP (val))
217 {
218 int i;
219
220 for (i = 0; i < ASIZE (val); i++)
221 ASET (AREF (val, i), FONT_TYPE_INDEX, Qftx);
222 }
223 return val;
224}
225
3a91626c
KH
226static Lisp_Object
227ftxfont_match (frame, spec)
228 Lisp_Object frame;
229 Lisp_Object spec;
230{
231 Lisp_Object entity = ftfont_driver.match (frame, spec);
232
233 if (VECTORP (entity))
234 ASET (entity, FONT_TYPE_INDEX, Qftx);
235 return entity;
236}
237
c2f5bfd6
KH
238static struct font *
239ftxfont_open (f, entity, pixel_size)
240 FRAME_PTR f;
241 Lisp_Object entity;
242 int pixel_size;
243{
244 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
245 struct font *font;
246 XFontStruct *xfont = malloc (sizeof (XFontStruct));
247
248 if (! xfont)
249 return NULL;
250 font = ftfont_driver.open (f, entity, pixel_size);
251 if (! font)
252 {
253 free (xfont);
254 return NULL;
255 }
256
056360d0 257 xfont->fid = ftxfont_default_fid (f);
c2f5bfd6
KH
258 xfont->ascent = font->ascent;
259 xfont->descent = font->descent;
260 xfont->max_bounds.width = font->font.size;
261 xfont->min_bounds.width = font->min_width;
262 font->font.font = xfont;
263 font->driver = &ftxfont_driver;
264
265 dpyinfo->n_fonts++;
266
267 /* Set global flag fonts_changed_p to non-zero if the font loaded
268 has a character with a smaller width than any other character
269 before, or if the font loaded has a smaller height than any other
270 font loaded before. If this happens, it will make a glyph matrix
271 reallocation necessary. */
272 if (dpyinfo->n_fonts == 1)
273 {
274 dpyinfo->smallest_font_height = font->font.height;
275 dpyinfo->smallest_char_width = font->min_width;
276 fonts_changed_p = 1;
277 }
278 else
279 {
280 if (dpyinfo->smallest_font_height > font->font.height)
281 dpyinfo->smallest_font_height = font->font.height, fonts_changed_p |= 1;
282 if (dpyinfo->smallest_char_width > font->min_width)
283 dpyinfo->smallest_char_width = font->min_width, fonts_changed_p |= 1;
284 }
285
286 return font;
287}
288
289static void
290ftxfont_close (f, font)
291 FRAME_PTR f;
292 struct font *font;
293{
294 ftfont_driver.close (f, font);
295 FRAME_X_DISPLAY_INFO (f)->n_fonts--;
296}
297
298static int
299ftxfont_prepare_face (f, face)
300 FRAME_PTR f;
301 struct face *face;
302{
056360d0
KH
303 struct font *font = (struct font *) face->font_info;
304 GC gcs[6];
c2f5bfd6
KH
305 int i;
306
307 face->extra = NULL;
308
056360d0
KH
309 if (! font->scalable)
310 return 0;
311
312 if (ftxfont_create_gcs (f, gcs, face->foreground, face->background) < 0)
313 /* Give up antialiasing. */
314 return 0;
c2f5bfd6 315
c2f5bfd6
KH
316 face->extra = malloc (sizeof (GC) * 7);
317 if (! face->extra)
318 return -1;
319 for (i = 0; i < 6; i++)
056360d0 320 ((GC *) face->extra)[i] = gcs[i];
c2f5bfd6
KH
321 ((GC *) face->extra)[i] = face->gc;
322 return 0;
323}
324
325static void
326ftxfont_done_face (f, face)
327 FRAME_PTR f;
328 struct face *face;
329{
330 if (face->extra)
331 {
332 int i;
333
334 BLOCK_INPUT;
056360d0 335 for (i = 0; i < 6; i++)
c2f5bfd6
KH
336 XFreeGC (FRAME_X_DISPLAY (f), ((GC *) face->extra)[i]);
337 UNBLOCK_INPUT;
338 free (face->extra);
339 face->extra = NULL;
340 }
341}
342
343static int
344ftxfont_draw (s, from, to, x, y, with_background)
345 struct glyph_string *s;
346 int from, to, x, y, with_background;
347{
348 FRAME_PTR f = s->f;
349 struct face *face = s->face;
056360d0 350 struct font *font = (struct font *) face->font_info;
c2f5bfd6
KH
351 XPoint p[0x700];
352 int n[7];
353 unsigned *code;
354 int len = to - from;
355 int i;
056360d0 356 GC *gcs;
c2f5bfd6
KH
357
358 n[0] = n[1] = n[2] = n[3] = n[4] = n[5] = n[6] = 0;
359
360 BLOCK_INPUT;
361
362 if (with_background)
363 ftxfont_draw_backgrond (f, font, s->gc, x, y, s->width);
364 code = alloca (sizeof (unsigned) * len);
365 for (i = 0; i < len; i++)
366 code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
367 | XCHAR2B_BYTE2 (s->char2b + from + i));
368
056360d0
KH
369 gcs = face->extra;
370 if (gcs && face->gc != s->gc)
c2f5bfd6 371 {
056360d0
KH
372 /* We are drawing for cursor or for mouse highlighting, and
373 can't use the prepared GCs. */
374 XGCValues xgcv;
375 unsigned long mask = GCForeground | GCBackground;
376
377 gcs = alloca (sizeof (GC) * 7);
378 XGetGCValues (FRAME_X_DISPLAY (f), s->gc, mask, &xgcv);
379 if (ftxfont_create_gcs (f, gcs, xgcv.foreground, xgcv.background) < 0)
380 gcs = NULL;
381 gcs[6] = s->gc;
382 }
383
384 if (! gcs)
385 {
386 /* We are drawing with a bitmap font which doesn't use
387 antialiasing. */
c2f5bfd6 388 for (i = 0; i < len; i++)
056360d0 389 x += ftxfont_draw_bitmap (f, &s->gc, font, code[i], x, y,
c2f5bfd6
KH
390 p, 0x700, n);
391 if (n[0] > 0)
392 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
056360d0 393 s->gc, p, n[0], CoordModeOrigin);
c2f5bfd6
KH
394 }
395 else
396 {
056360d0
KH
397 /* We are drawing with a scalable font which use
398 antialiasing. */
c2f5bfd6 399 for (i = 0; i < len; i++)
056360d0 400 x += ftxfont_draw_bitmap (f, gcs, font, code[i], x, y,
c2f5bfd6
KH
401 p, 0x100, n);
402 for (i = 0; i < 7; i++)
403 if (n[i] > 0)
404 XDrawPoints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
056360d0
KH
405 gcs[i], p + 0x100 * i, n[i], CoordModeOrigin);
406 if (face->gc != s->gc)
407 for (i = 0; i < 6; i++)
408 XFreeGC (FRAME_X_DISPLAY (f), gcs[i]);
c2f5bfd6
KH
409 }
410
411 UNBLOCK_INPUT;
412
413 return len;
414}
415
416\f
417
418void
419syms_of_ftxfont ()
420{
421 DEFSYM (Qftx, "ftx");
422
423 ftxfont_driver = ftfont_driver;
424 ftxfont_driver.type = Qftx;
425 ftxfont_driver.list = ftxfont_list;
3a91626c 426 ftxfont_driver.match = ftxfont_match;
c2f5bfd6
KH
427 ftxfont_driver.open = ftxfont_open;
428 ftxfont_driver.close = ftxfont_close;
429 ftxfont_driver.prepare_face = ftxfont_prepare_face;
430 ftxfont_driver.done_face = ftxfont_done_face;
431 ftxfont_driver.draw = ftxfont_draw;
432
433 register_font_driver (&ftxfont_driver, NULL);
434}
885b7d09
MB
435
436/* arch-tag: 59bd3469-5330-413f-b29d-1aa36492abe8
437 (do not change this comment) */