(xfont_encode_char): Fix calculation of char2b.
[bpt/emacs.git] / src / xftfont.c
CommitLineData
c2f5bfd6
KH
1/* xftfont.c -- XFT font driver.
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#include <X11/Xft/Xft.h>
28
29#include "lisp.h"
30#include "dispextern.h"
31#include "xterm.h"
32#include "frame.h"
33#include "blockinput.h"
34#include "character.h"
35#include "charset.h"
36#include "fontset.h"
37#include "font.h"
38
39/* Xft font driver. */
40
41static Lisp_Object Qxft;
42
43/* The actual structure for Xft font that can be casted to struct
44 font. */
45
46struct xftfont_info
47{
48 struct font font;
49 Display *display;
50 int screen;
51 XftFont *xftfont;
52 FT_Face ft_face;
53};
54
55/* Structure pointed by (struct face *)->extra */
56struct xftface_info
57{
58 XftColor xft_fg;
59 XftColor xft_bg;
60 XftDraw *xft_draw;
61};
62
63static void xftfont_get_colors P_ ((FRAME_PTR, struct face *, GC gc,
64 struct xftface_info *,
65 XftColor *fg, XftColor *bg));
66static Font xftfont_default_fid P_ ((FRAME_PTR));
67
68
69/* Setup colors pointed by FG and BG for GC. If XFTFACE_INFO is not
70 NULL, reuse the colors in it if possible. BG may be NULL. */
71static void
72xftfont_get_colors (f, face, gc, xftface_info, fg, bg)
73 FRAME_PTR f;
74 struct face *face;
75 GC gc;
76 struct xftface_info *xftface_info;
77 XftColor *fg, *bg;
78{
79 if (xftface_info && face->gc == gc)
80 {
81 *fg = xftface_info->xft_fg;
82 if (bg)
83 *bg = xftface_info->xft_bg;
84 }
85 else
86 {
87 XGCValues xgcv;
88 int fg_done = 0, bg_done = 0;
89
90 BLOCK_INPUT;
91 XGetGCValues (FRAME_X_DISPLAY (f), gc,
92 GCForeground | GCBackground, &xgcv);
93 if (xftface_info)
94 {
95 if (xgcv.foreground == face->foreground)
96 *fg = xftface_info->xft_fg, fg_done = 1;
97 else if (xgcv.foreground == face->background)
98 *fg = xftface_info->xft_bg, fg_done = 1;
99 if (! bg)
100 bg_done = 1;
101 else if (xgcv.background == face->background)
102 *bg = xftface_info->xft_bg, bg_done = 1;
103 else if (xgcv.background == face->foreground)
104 *bg = xftface_info->xft_fg, bg_done = 1;
105 }
106
107 if (fg_done + bg_done < 2)
108 {
109 XColor colors[2];
110
111 colors[0].pixel = fg->pixel = xgcv.foreground;
112 if (bg)
113 colors[1].pixel = bg->pixel = xgcv.background;
114 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors,
115 bg ? 2 : 1);
116 fg->color.alpha = 0xFFFF;
117 fg->color.red = colors[0].red;
118 fg->color.green = colors[0].green;
119 fg->color.blue = colors[0].blue;
120 if (bg)
121 {
122 bg->color.alpha = 0xFFFF;
123 bg->color.red = colors[1].red;
124 bg->color.green = colors[1].green;
125 bg->color.blue = colors[1].blue;
126 }
127 }
128 UNBLOCK_INPUT;
129 }
130}
131
132/* Return the default Font ID on frame F. */
133
134static Font
135xftfont_default_fid (f)
136 FRAME_PTR f;
137{
138 static int fid_known;
139 static Font fid;
140
141 if (! fid_known)
142 {
143 fid = XLoadFont (FRAME_X_DISPLAY (f), "fixed");
144 if (! fid)
145 {
146 fid = XLoadFont (FRAME_X_DISPLAY (f), "*");
147 if (! fid)
148 abort ();
149 }
150 }
151 return fid;
152}
153
154
155static Lisp_Object xftfont_list P_ ((Lisp_Object, Lisp_Object));
156static struct font *xftfont_open P_ ((FRAME_PTR, Lisp_Object, int));
157static void xftfont_close P_ ((FRAME_PTR, struct font *));
158static int xftfont_prepare_face P_ ((FRAME_PTR, struct face *));
159static void xftfont_done_face P_ ((FRAME_PTR, struct face *));
160static unsigned xftfont_encode_char P_ ((struct font *, int));
161static int xftfont_text_extents P_ ((struct font *, unsigned *, int,
162 struct font_metrics *));
163static int xftfont_draw P_ ((struct glyph_string *, int, int, int, int, int));
164
165static int xftfont_anchor_point P_ ((struct font *, unsigned, int,
166 int *, int *));
167
168struct font_driver xftfont_driver;
169
170static Lisp_Object
171xftfont_list (frame, spec)
172 Lisp_Object frame;
173 Lisp_Object spec;
174{
175 Lisp_Object val = ftfont_driver.list (frame, spec);
176
177 if (! NILP (val))
178 {
179 int i;
180
181 for (i = 0; i < ASIZE (val); i++)
182 ASET (AREF (val, i), FONT_TYPE_INDEX, Qxft);
183 }
184 return val;
185}
186
187static FcChar8 ascii_printable[95];
188
189static struct font *
190xftfont_open (f, entity, pixel_size)
191 FRAME_PTR f;
192 Lisp_Object entity;
193 int pixel_size;
194{
195 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
196 Display *display = FRAME_X_DISPLAY (f);
197 Lisp_Object val;
198 FcPattern *pattern, *pat;
199 FcChar8 *file;
200 XFontStruct *xfont;
201 struct xftfont_info *xftfont_info;
202 struct font *font;
203 double size = 0;
204 XftFont *xftfont;
205 int spacing;
206
207 val = AREF (entity, FONT_EXTRA_INDEX);
208 if (XTYPE (val) != Lisp_Misc
209 || XMISCTYPE (val) != Lisp_Misc_Save_Value)
210 return NULL;
211 pattern = XSAVE_VALUE (val)->pointer;
212 if (FcPatternGetString (pattern, FC_FILE, 0, &file) != FcResultMatch)
213 return NULL;
214
215 size = XINT (AREF (entity, FONT_SIZE_INDEX));
216 if (size == 0)
217 size = pixel_size;
218 pat = FcPatternCreate ();
219 FcPatternAddString (pat, FC_FILE, file);
220 FcPatternAddDouble (pat, FC_PIXEL_SIZE, pixel_size);
221 FcPatternAddBool (pat, FC_ANTIALIAS, FcTrue);
222 xftfont = XftFontOpenPattern (display, pat);
223 /* We should not destroy PAT here because it is kept in XFTFONT and
224 destroyed automatically when XFTFONT is closed. */
225 if (! xftfont)
226 return NULL;
227
228 xftfont_info = malloc (sizeof (struct xftfont_info));
229 if (! xftfont_info)
230 {
231 XftFontClose (display, xftfont);
232 return NULL;
233 }
234 xfont = malloc (sizeof (XFontStruct));
235 if (! xftfont_info)
236 {
237 XftFontClose (display, xftfont);
238 free (xftfont_info);
239 return NULL;
240 }
241 xftfont_info->display = display;
242 xftfont_info->screen = FRAME_X_SCREEN_NUMBER (f);
243 xftfont_info->xftfont = xftfont;
244 xftfont_info->ft_face = XftLockFace (xftfont);
245
246 font = (struct font *) xftfont_info;
247 font->entity = entity;
248 font->pixel_size = size;
249 font->driver = &xftfont_driver;
250 font->font.name = font->font.full_name = NULL;
251 font->file_name = (char *) file;
252 font->font.size = xftfont->max_advance_width;
253 font->ascent = xftfont->ascent;
254 font->descent = xftfont->descent;
255 font->font.height = xftfont->ascent + xftfont->descent;
256
257 if (FcPatternGetInteger (xftfont->pattern, FC_SPACING, 0, &spacing)
258 != FcResultMatch)
259 spacing = FC_PROPORTIONAL;
260 if (spacing != FC_PROPORTIONAL)
261 font->font.average_width = font->font.space_width
262 = xftfont->max_advance_width;
263 else
264 {
265 XGlyphInfo extents;
266
267 if (! ascii_printable[0])
268 {
269 int i;
270 for (i = 0; i < 95; i++)
271 ascii_printable[i] = ' ' + i;
272 }
273 XftTextExtents8 (display, xftfont, ascii_printable, 1, &extents);
274 font->font.space_width = extents.xOff;
275 if (font->font.space_width <= 0)
276 /* dirty workaround */
277 font->font.space_width = pixel_size;
278 XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents);
279 font->font.average_width = (font->font.space_width + extents.xOff) / 95;
280 }
281
282 /* Unfortunately Xft doesn't provide a way to get minimum char
283 width. So, we use space_width instead. */
284 font->min_width = font->font.space_width;
285
286 font->font.baseline_offset = 0;
287 font->font.relative_compose = 0;
288 font->font.default_ascent = 0;
289 font->font.vertical_centering = 0;
290
291 /* Setup pseudo XFontStruct */
292 xfont->fid = xftfont_default_fid (f);
293 xfont->ascent = xftfont->ascent;
294 xfont->descent = xftfont->descent;
295 xfont->max_bounds.descent = xftfont->descent;
296 xfont->max_bounds.width = xftfont->max_advance_width;
297 xfont->min_bounds.width = font->font.space_width;
298 font->font.font = xfont;
299
300 dpyinfo->n_fonts++;
301
302 /* Set global flag fonts_changed_p to non-zero if the font loaded
303 has a character with a smaller width than any other character
304 before, or if the font loaded has a smaller height than any other
305 font loaded before. If this happens, it will make a glyph matrix
306 reallocation necessary. */
307 if (dpyinfo->n_fonts == 1)
308 {
309 dpyinfo->smallest_font_height = font->font.height;
310 dpyinfo->smallest_char_width = font->min_width;
311 fonts_changed_p = 1;
312 }
313 else
314 {
315 if (dpyinfo->smallest_font_height > font->font.height)
316 dpyinfo->smallest_font_height = font->font.height,
317 fonts_changed_p |= 1;
318 if (dpyinfo->smallest_char_width > font->min_width)
319 dpyinfo->smallest_char_width = font->min_width,
320 fonts_changed_p |= 1;
321 }
322
323 return font;
324}
325
326static void
327xftfont_close (f, font)
328 FRAME_PTR f;
329 struct font *font;
330{
331 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
332
333 XftUnlockFace (xftfont_info->xftfont);
334 XftFontClose (xftfont_info->display, xftfont_info->xftfont);
335 free (font);
336 FRAME_X_DISPLAY_INFO (f)->n_fonts--;
337}
338
339struct xftdraw_list
340{
341 XftDraw *xftdraw;
342 struct xftdraw_list *next;
343};
344
345static struct xftdraw_list *xftdraw_list;
346
347static void
348register_xftdraw (xftdraw)
349 XftDraw *xftdraw;
350{
351 struct xftdraw_list *list = malloc (sizeof (struct xftdraw_list));
352
353 list->xftdraw = xftdraw;
354 list->next = xftdraw_list;
355 xftdraw_list = list;
356}
357
358static void
359check_xftdraw (xftdraw)
360 XftDraw *xftdraw;
361{
362 struct xftdraw_list *list, *prev;
363
364 for (list = xftdraw_list, prev = NULL; list; prev = list, list = list->next)
365 {
366 if (list->xftdraw == xftdraw)
367 {
368 if (! prev)
369 {
370 list = xftdraw_list->next;
371 free (xftdraw_list);
372 xftdraw_list = list;
373 }
374 else
375 {
376 prev->next = list->next;
377 free (list);
378 list = prev;
379 }
380 return;
381 }
382 }
383 abort ();
384}
385
386static int
387xftfont_prepare_face (f, face)
388 FRAME_PTR f;
389 struct face *face;
390{
391 struct xftface_info *xftface_info = malloc (sizeof (struct xftface_info));
392
393 if (! xftface_info)
394 return -1;
395
396 BLOCK_INPUT;
397 xftface_info->xft_draw = XftDrawCreate (FRAME_X_DISPLAY (f),
398 FRAME_X_WINDOW (f),
399 FRAME_X_VISUAL (f),
400 FRAME_X_COLORMAP (f));
401 register_xftdraw (xftface_info->xft_draw);
402
403 xftfont_get_colors (f, face, face->gc, NULL,
404 &xftface_info->xft_fg, &xftface_info->xft_bg);
405 UNBLOCK_INPUT;
406
407 face->extra = xftface_info;
408 return 0;
409}
410
411static void
412xftfont_done_face (f, face)
413 FRAME_PTR f;
414 struct face *face;
415{
416 struct xftface_info *xftface_info = (struct xftface_info *) face->extra;
417
418 if (xftface_info)
419 {
420 BLOCK_INPUT;
421 check_xftdraw (xftface_info->xft_draw);
422 XftDrawDestroy (xftface_info->xft_draw);
423 UNBLOCK_INPUT;
424 free (xftface_info);
425 face->extra = NULL;
426 }
427}
428
429static unsigned
430xftfont_encode_char (font, c)
431 struct font *font;
432 int c;
433{
434 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
435 unsigned code = XftCharIndex (xftfont_info->display, xftfont_info->xftfont,
436 (FcChar32) c);
437
438 return (code ? code : 0xFFFFFFFF);
439}
440
441static int
442xftfont_text_extents (font, code, nglyphs, metrics)
443 struct font *font;
444 unsigned *code;
445 int nglyphs;
446 struct font_metrics *metrics;
447{
448 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
449 XGlyphInfo extents;
450
451 BLOCK_INPUT;
452 XftGlyphExtents (xftfont_info->display, xftfont_info->xftfont, code, nglyphs,
453 &extents);
454 UNBLOCK_INPUT;
455 if (metrics)
456 {
457 metrics->lbearing = - extents.x;
458 metrics->rbearing = - extents.x + extents.width;
459 metrics->width = extents.xOff;
460 metrics->ascent = extents.y;
461 metrics->descent = extents.y - extents.height;
462 }
463 return extents.xOff;
464}
465
466static int
467xftfont_draw (s, from, to, x, y, with_background)
468 struct glyph_string *s;
469 int from, to, x, y, with_background;
470{
471 FRAME_PTR f = s->f;
472 struct face *face = s->face;
473 struct xftfont_info *xftfont_info = (struct xftfont_info *) face->font_info;
474 struct xftface_info *xftface_info = (struct xftface_info *) face->extra;
475 FT_UInt *code;
476 XftColor fg, bg;
477 XRectangle r;
478 int len = to - from;
479 int i;
480
481 xftfont_get_colors (f, face, s->gc, xftface_info,
482 &fg, s->width ? &bg : NULL);
483 BLOCK_INPUT;
484 if (s->clip_width)
485 {
486 r.x = s->clip_x, r.width = s->clip_width;
487 r.y = s->clip_y, r.height = s->clip_height;
488 XftDrawSetClipRectangles (xftface_info->xft_draw, 0, 0, &r, 1);
489 }
490 if (with_background)
491 {
492 struct font *font = (struct font *) face->font_info;
493
494 XftDrawRect (xftface_info->xft_draw, &bg,
495 x, y - face->font->ascent, s->width, font->font.height);
496 }
497 code = alloca (sizeof (FT_UInt) * len);
498 for (i = 0; i < len; i++)
499 code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
500 | XCHAR2B_BYTE2 (s->char2b + from + i));
501
502 XftDrawGlyphs (xftface_info->xft_draw, &fg, xftfont_info->xftfont,
503 x, y, code, len);
504 if (s->clip_width)
505 XftDrawSetClip (xftface_info->xft_draw, NULL);
506 UNBLOCK_INPUT;
507
508 return len;
509}
510
511static int
512xftfont_anchor_point (font, code, index, x, y)
513 struct font *font;
514 unsigned code;
515 int index;
516 int *x, *y;
517{
518 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
519 FT_Face ft_face = xftfont_info->ft_face;
520
521 if (FT_Load_Glyph (ft_face, code, FT_LOAD_DEFAULT) != 0)
522 return -1;
523 if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
524 return -1;
525 if (index >= ft_face->glyph->outline.n_points)
526 return -1;
527 *x = ft_face->glyph->outline.points[index].x;
528 *y = ft_face->glyph->outline.points[index].y;
529 return 0;
530}
531
532
533void
534syms_of_xftfont ()
535{
536 DEFSYM (Qxft, "xft");
537
538 xftfont_driver = ftfont_driver;
539 xftfont_driver.type = Qxft;
540 xftfont_driver.get_cache = xfont_driver.get_cache;
541 xftfont_driver.list = xftfont_list;
542 xftfont_driver.open = xftfont_open;
543 xftfont_driver.close = xftfont_close;
544 xftfont_driver.prepare_face = xftfont_prepare_face;
545 xftfont_driver.done_face = xftfont_done_face;
546 xftfont_driver.encode_char = xftfont_encode_char;
547 xftfont_driver.text_extents = xftfont_text_extents;
548 xftfont_driver.draw = xftfont_draw;
549 xftfont_driver.anchor_point = xftfont_anchor_point;
550
551 register_font_driver (&xftfont_driver, NULL);
552}