Merge from emacs--rel--22
[bpt/emacs.git] / src / xftfont.c
1 /* xftfont.c -- XFT font driver.
2 Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
3 Copyright (C) 2006, 2007, 2008
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 <X11/Xlib.h>
25 #include <X11/Xft/Xft.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 #include "ftfont.h"
37
38 /* Xft font driver. */
39
40 static Lisp_Object Qxft;
41
42 /* The actual structure for Xft font that can be casted to struct
43 font. */
44
45 struct xftfont_info
46 {
47 struct font font;
48 Display *display;
49 int screen;
50 XftFont *xftfont;
51 #ifdef HAVE_LIBOTF
52 int maybe_otf; /* Flag to tell if this may be OTF or not. */
53 OTF *otf;
54 #endif /* HAVE_LIBOTF */
55 };
56
57 /* Structure pointed by (struct face *)->extra */
58
59 struct xftface_info
60 {
61 XftColor xft_fg; /* color for face->foreground */
62 XftColor xft_bg; /* color for face->background */
63 };
64
65 static void xftfont_get_colors P_ ((FRAME_PTR, struct face *, GC gc,
66 struct xftface_info *,
67 XftColor *fg, XftColor *bg));
68
69
70 /* Setup foreground and background colors of GC into FG and BG. If
71 XFTFACE_INFO is not NULL, reuse the colors in it if possible. BG
72 may be NULL. */
73
74 static void
75 xftfont_get_colors (f, face, gc, xftface_info, fg, bg)
76 FRAME_PTR f;
77 struct face *face;
78 GC gc;
79 struct xftface_info *xftface_info;
80 XftColor *fg, *bg;
81 {
82 if (xftface_info && face->gc == gc)
83 {
84 *fg = xftface_info->xft_fg;
85 if (bg)
86 *bg = xftface_info->xft_bg;
87 }
88 else
89 {
90 XGCValues xgcv;
91 int fg_done = 0, bg_done = 0;
92
93 BLOCK_INPUT;
94 XGetGCValues (FRAME_X_DISPLAY (f), gc,
95 GCForeground | GCBackground, &xgcv);
96 if (xftface_info)
97 {
98 if (xgcv.foreground == face->foreground)
99 *fg = xftface_info->xft_fg, fg_done = 1;
100 else if (xgcv.foreground == face->background)
101 *fg = xftface_info->xft_bg, fg_done = 1;
102 if (! bg)
103 bg_done = 1;
104 else if (xgcv.background == face->background)
105 *bg = xftface_info->xft_bg, bg_done = 1;
106 else if (xgcv.background == face->foreground)
107 *bg = xftface_info->xft_fg, bg_done = 1;
108 }
109
110 if (fg_done + bg_done < 2)
111 {
112 XColor colors[2];
113
114 colors[0].pixel = fg->pixel = xgcv.foreground;
115 if (bg)
116 colors[1].pixel = bg->pixel = xgcv.background;
117 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors,
118 bg ? 2 : 1);
119 fg->color.alpha = 0xFFFF;
120 fg->color.red = colors[0].red;
121 fg->color.green = colors[0].green;
122 fg->color.blue = colors[0].blue;
123 if (bg)
124 {
125 bg->color.alpha = 0xFFFF;
126 bg->color.red = colors[1].red;
127 bg->color.green = colors[1].green;
128 bg->color.blue = colors[1].blue;
129 }
130 }
131 UNBLOCK_INPUT;
132 }
133 }
134
135
136 static Lisp_Object xftfont_list P_ ((Lisp_Object, Lisp_Object));
137 static Lisp_Object xftfont_match P_ ((Lisp_Object, Lisp_Object));
138 static Lisp_Object xftfont_open P_ ((FRAME_PTR, Lisp_Object, int));
139 static void xftfont_close P_ ((FRAME_PTR, struct font *));
140 static int xftfont_prepare_face P_ ((FRAME_PTR, struct face *));
141 static void xftfont_done_face P_ ((FRAME_PTR, struct face *));
142 static unsigned xftfont_encode_char P_ ((struct font *, int));
143 static int xftfont_text_extents P_ ((struct font *, unsigned *, int,
144 struct font_metrics *));
145 static int xftfont_draw P_ ((struct glyph_string *, int, int, int, int, int));
146
147 static int xftfont_anchor_point P_ ((struct font *, unsigned, int,
148 int *, int *));
149 static int xftfont_end_for_frame P_ ((FRAME_PTR f));
150
151 struct font_driver xftfont_driver;
152
153 static Lisp_Object
154 xftfont_list (frame, spec)
155 Lisp_Object frame;
156 Lisp_Object spec;
157 {
158 Lisp_Object list = ftfont_driver.list (frame, spec), tail;
159
160 for (tail = list; CONSP (tail); tail = XCDR (tail))
161 ASET (XCAR (tail), FONT_TYPE_INDEX, Qxft);
162 return list;
163 }
164
165 static Lisp_Object
166 xftfont_match (frame, spec)
167 Lisp_Object frame;
168 Lisp_Object spec;
169 {
170 Lisp_Object entity = ftfont_driver.match (frame, spec);
171
172 if (! NILP (entity))
173 ASET (entity, FONT_TYPE_INDEX, Qxft);
174 return entity;
175 }
176
177 extern Lisp_Object ftfont_font_format P_ ((FcPattern *));
178
179 static FcChar8 ascii_printable[95];
180
181 static Lisp_Object
182 xftfont_open (f, entity, pixel_size)
183 FRAME_PTR f;
184 Lisp_Object entity;
185 int pixel_size;
186 {
187 Display *display = FRAME_X_DISPLAY (f);
188 Lisp_Object val, filename, font_object;
189 FcPattern *pat = NULL;
190 struct xftfont_info *xftfont_info = NULL;
191 struct font *font;
192 double size = 0;
193 XftFont *xftfont = NULL;
194 int spacing;
195 char name[256];
196 int len, i;
197 XGlyphInfo extents;
198 FT_Face ft_face;
199
200 val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
201 if (! CONSP (val))
202 return Qnil;
203 val = XCDR (val);
204 filename = XCAR (val);
205 size = XINT (AREF (entity, FONT_SIZE_INDEX));
206 if (size == 0)
207 size = pixel_size;
208 pat = FcPatternCreate ();
209 FcPatternAddString (pat, FC_FILE, (FcChar8 *) SDATA (filename));
210 FcPatternAddDouble (pat, FC_PIXEL_SIZE, pixel_size);
211 /*FcPatternAddBool (pat, FC_ANTIALIAS, FcTrue);*/
212 val = AREF (entity, FONT_FAMILY_INDEX);
213 if (! NILP (val))
214 FcPatternAddString (pat, FC_FAMILY, (FcChar8 *) SDATA (SYMBOL_NAME (val)));
215 FcConfigSubstitute (NULL, pat, FcMatchPattern);
216
217 BLOCK_INPUT;
218 XftDefaultSubstitute (display, FRAME_X_SCREEN_NUMBER (f), pat);
219 xftfont = XftFontOpenPattern (display, pat);
220 UNBLOCK_INPUT;
221 if (! xftfont)
222 return Qnil;
223 /* We should not destroy PAT here because it is kept in XFTFONT and
224 destroyed automatically when XFTFONT is closed. */
225 font_object = font_make_object (VECSIZE (struct xftfont_info));
226 ASET (font_object, FONT_TYPE_INDEX, Qxft);
227 for (i = 1; i < FONT_ENTITY_MAX; i++)
228 ASET (font_object, i, AREF (entity, i));
229 ASET (font_object, FONT_SIZE_INDEX, make_number (size));
230 len = font_unparse_xlfd (entity, size, name, 256);
231 if (len > 0)
232 ASET (font_object, FONT_NAME_INDEX, make_unibyte_string (name, len));
233 len = font_unparse_fcname (entity, size, name, 256);
234 if (len > 0)
235 ASET (font_object, FONT_FULLNAME_INDEX, make_unibyte_string (name, len));
236 else
237 ASET (font_object, FONT_FULLNAME_INDEX,
238 AREF (font_object, FONT_NAME_INDEX));
239 ASET (font_object, FONT_FILE_INDEX, filename);
240 ASET (font_object, FONT_FORMAT_INDEX,
241 ftfont_font_format (xftfont->pattern));
242 font = XFONT_OBJECT (font_object);
243 font->pixel_size = pixel_size;
244 font->driver = &xftfont_driver;
245 font->encoding_charset = font->repertory_charset = -1;
246
247 xftfont_info = (struct xftfont_info *) font;
248 xftfont_info->display = display;
249 xftfont_info->screen = FRAME_X_SCREEN_NUMBER (f);
250 xftfont_info->xftfont = xftfont;
251 font->pixel_size = size;
252 font->driver = &xftfont_driver;
253 if (INTEGERP (AREF (entity, FONT_SPACING_INDEX)))
254 spacing = XINT (AREF (entity, FONT_SPACING_INDEX));
255 else
256 spacing = FC_PROPORTIONAL;
257 if (! ascii_printable[0])
258 {
259 int i;
260 for (i = 0; i < 95; i++)
261 ascii_printable[i] = ' ' + i;
262 }
263 BLOCK_INPUT;
264 if (spacing != FC_PROPORTIONAL)
265 {
266 font->min_width = font->average_width = font->space_width
267 = xftfont->max_advance_width;
268 XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents);
269 }
270 else
271 {
272 XftTextExtents8 (display, xftfont, ascii_printable, 1, &extents);
273 font->space_width = extents.xOff;
274 if (font->space_width <= 0)
275 /* dirty workaround */
276 font->space_width = pixel_size;
277 XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents);
278 font->average_width = (font->space_width + extents.xOff) / 95;
279 }
280 UNBLOCK_INPUT;
281
282 font->ascent = xftfont->ascent;
283 if (font->ascent < extents.y)
284 font->ascent = extents.y;
285 font->descent = xftfont->descent;
286 if (font->descent < extents.height - extents.y)
287 font->descent = extents.height - extents.y;
288 font->height = font->ascent + font->descent;
289
290 ft_face = XftLockFace (xftfont);
291 if (XINT (AREF (entity, FONT_SIZE_INDEX)) == 0)
292 {
293 int upEM = ft_face->units_per_EM;
294
295 font->underline_position = -ft_face->underline_position * size / upEM;
296 font->underline_thickness = -ft_face->underline_thickness * size / upEM;
297 if (font->underline_thickness > 2)
298 font->underline_position -= font->underline_thickness / 2;
299 }
300 else
301 {
302 font->underline_position = -1;
303 font->underline_thickness = 0;
304 }
305 #ifdef HAVE_LIBOTF
306 xftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT;
307 xftfont_info->otf = NULL;
308 #endif /* HAVE_LIBOTF */
309 XftUnlockFace (xftfont);
310
311 /* Unfortunately Xft doesn't provide a way to get minimum char
312 width. So, we use space_width instead. */
313 font->min_width = font->space_width;
314
315 font->baseline_offset = 0;
316 font->relative_compose = 0;
317 font->default_ascent = 0;
318 font->vertical_centering = 0;
319
320 return font_object;
321 }
322
323 static void
324 xftfont_close (f, font)
325 FRAME_PTR f;
326 struct font *font;
327 {
328 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
329
330 #ifdef HAVE_LIBOTF
331 if (xftfont_info->otf)
332 OTF_close (xftfont_info->otf);
333 #endif
334 BLOCK_INPUT;
335 XftFontClose (xftfont_info->display, xftfont_info->xftfont);
336 UNBLOCK_INPUT;
337 }
338
339 static int
340 xftfont_prepare_face (f, face)
341 FRAME_PTR f;
342 struct face *face;
343 {
344 struct xftface_info *xftface_info;
345
346 #if 0
347 /* This doesn't work if face->ascii_face doesn't use an Xft font. */
348 if (face != face->ascii_face)
349 {
350 face->extra = face->ascii_face->extra;
351 return 0;
352 }
353 #endif
354
355 xftface_info = malloc (sizeof (struct xftface_info));
356 if (! xftface_info)
357 return -1;
358 xftfont_get_colors (f, face, face->gc, NULL,
359 &xftface_info->xft_fg, &xftface_info->xft_bg);
360 face->extra = xftface_info;
361 return 0;
362 }
363
364 static void
365 xftfont_done_face (f, face)
366 FRAME_PTR f;
367 struct face *face;
368 {
369 struct xftface_info *xftface_info;
370
371 #if 0
372 /* This doesn't work if face->ascii_face doesn't use an Xft font. */
373 if (face != face->ascii_face
374 || ! face->extra)
375 return;
376 #endif
377
378 xftface_info = (struct xftface_info *) face->extra;
379 if (xftface_info)
380 {
381 free (xftface_info);
382 face->extra = NULL;
383 }
384 }
385
386 static unsigned
387 xftfont_encode_char (font, c)
388 struct font *font;
389 int c;
390 {
391 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
392 unsigned code = XftCharIndex (xftfont_info->display, xftfont_info->xftfont,
393 (FcChar32) c);
394
395 return (code ? code : FONT_INVALID_CODE);
396 }
397
398 static int
399 xftfont_text_extents (font, code, nglyphs, metrics)
400 struct font *font;
401 unsigned *code;
402 int nglyphs;
403 struct font_metrics *metrics;
404 {
405 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
406 XGlyphInfo extents;
407
408 BLOCK_INPUT;
409 XftGlyphExtents (xftfont_info->display, xftfont_info->xftfont, code, nglyphs,
410 &extents);
411 UNBLOCK_INPUT;
412 if (metrics)
413 {
414 metrics->lbearing = - extents.x;
415 metrics->rbearing = - extents.x + extents.width;
416 metrics->width = extents.xOff;
417 metrics->ascent = extents.y;
418 metrics->descent = extents.height - extents.y;
419 }
420 return extents.xOff;
421 }
422
423 static XftDraw *
424 xftfont_get_xft_draw (f)
425 FRAME_PTR f;
426 {
427 XftDraw *xft_draw = font_get_frame_data (f, &xftfont_driver);;
428
429 if (! xft_draw)
430 {
431 BLOCK_INPUT;
432 xft_draw= XftDrawCreate (FRAME_X_DISPLAY (f),
433 FRAME_X_WINDOW (f),
434 FRAME_X_VISUAL (f),
435 FRAME_X_COLORMAP (f));
436 UNBLOCK_INPUT;
437 if (! xft_draw)
438 abort ();
439 font_put_frame_data (f, &xftfont_driver, xft_draw);
440 }
441 return xft_draw;
442 }
443
444 static int
445 xftfont_draw (s, from, to, x, y, with_background)
446 struct glyph_string *s;
447 int from, to, x, y, with_background;
448 {
449 FRAME_PTR f = s->f;
450 struct face *face = s->face;
451 struct xftfont_info *xftfont_info = (struct xftfont_info *) s->font;
452 struct xftface_info *xftface_info = NULL;
453 XftDraw *xft_draw = xftfont_get_xft_draw (f);
454 FT_UInt *code;
455 XftColor fg, bg;
456 int len = to - from;
457 int i;
458
459 if (s->font == face->font)
460 xftface_info = (struct xftface_info *) face->extra;
461 xftfont_get_colors (f, face, s->gc, xftface_info,
462 &fg, with_background ? &bg : NULL);
463 BLOCK_INPUT;
464 if (s->num_clips > 0)
465 XftDrawSetClipRectangles (xft_draw, 0, 0, s->clip, s->num_clips);
466 else
467 XftDrawSetClip (xft_draw, NULL);
468
469 if (with_background)
470 XftDrawRect (xft_draw, &bg,
471 x, y - face->font->ascent, s->width, face->font->height);
472 code = alloca (sizeof (FT_UInt) * len);
473 for (i = 0; i < len; i++)
474 code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
475 | XCHAR2B_BYTE2 (s->char2b + from + i));
476
477 if (s->padding_p)
478 for (i = 0; i < len; i++)
479 XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont,
480 x + i, y, code + i, 1);
481 else
482 XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont,
483 x, y, code, len);
484 UNBLOCK_INPUT;
485
486 return len;
487 }
488
489 static int
490 xftfont_anchor_point (font, code, index, x, y)
491 struct font *font;
492 unsigned code;
493 int index;
494 int *x, *y;
495 {
496 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
497 FT_Face ft_face = XftLockFace (xftfont_info->xftfont);
498 int result;
499
500 if (FT_Load_Glyph (ft_face, code, FT_LOAD_DEFAULT) != 0)
501 result = -1;
502 else if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
503 result = -1;
504 else if (index >= ft_face->glyph->outline.n_points)
505 result = -1;
506 else
507 {
508 *x = ft_face->glyph->outline.points[index].x;
509 *y = ft_face->glyph->outline.points[index].y;
510 }
511 XftUnlockFace (xftfont_info->xftfont);
512 return result;
513 }
514
515 static int
516 xftfont_end_for_frame (f)
517 FRAME_PTR f;
518 {
519 XftDraw *xft_draw = font_get_frame_data (f, &xftfont_driver);
520
521 if (xft_draw)
522 {
523 BLOCK_INPUT;
524 XftDrawDestroy (xft_draw);
525 UNBLOCK_INPUT;
526 font_put_frame_data (f, &xftfont_driver, NULL);
527 }
528 return 0;
529 }
530
531 #ifdef HAVE_LIBOTF
532 #ifdef HAVE_M17N_FLT
533 static Lisp_Object
534 xftfont_shape (lgstring)
535 Lisp_Object lgstring;
536 {
537 struct font *font;
538 struct xftfont_info *xftfont_info;
539 Lisp_Object result;
540 FT_Face ft_face;
541
542 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring), font);
543 xftfont_info = (struct xftfont_info *) font;
544 if (! xftfont_info->maybe_otf)
545 return Qnil;
546 ft_face = XftLockFace (xftfont_info->xftfont);
547 if (! xftfont_info->otf)
548 {
549 OTF *otf = OTF_open_ft_face (ft_face);
550
551 if (! otf || OTF_get_table (otf, "head") < 0)
552 {
553 if (otf)
554 OTF_close (otf);
555 xftfont_info->maybe_otf = 0;
556 XftUnlockFace (xftfont_info->xftfont);
557 return Qnil;
558 }
559 xftfont_info->otf = otf;
560 }
561
562 result = ftfont_shape_by_flt (lgstring, font, ft_face, xftfont_info->otf);
563 XftUnlockFace (xftfont_info->xftfont);
564 return result;
565 }
566 #endif /* HAVE_M17N_FLT */
567 #endif /* HAVE_LIBOTF */
568
569 void
570 syms_of_xftfont ()
571 {
572 DEFSYM (Qxft, "xft");
573
574 xftfont_driver = ftfont_driver;
575 xftfont_driver.type = Qxft;
576 xftfont_driver.get_cache = xfont_driver.get_cache;
577 xftfont_driver.list = xftfont_list;
578 xftfont_driver.match = xftfont_match;
579 xftfont_driver.open = xftfont_open;
580 xftfont_driver.close = xftfont_close;
581 xftfont_driver.prepare_face = xftfont_prepare_face;
582 xftfont_driver.done_face = xftfont_done_face;
583 xftfont_driver.encode_char = xftfont_encode_char;
584 xftfont_driver.text_extents = xftfont_text_extents;
585 xftfont_driver.draw = xftfont_draw;
586 xftfont_driver.anchor_point = xftfont_anchor_point;
587 xftfont_driver.end_for_frame = xftfont_end_for_frame;
588 #ifdef HAVE_LIBOTF
589 #ifdef HAVE_M17N_FLT
590 xftfont_driver.shape = xftfont_shape;
591 #endif /* HAVE_M17N_FLT */
592 #endif /* HAVE_LIBOTF */
593
594 register_font_driver (&xftfont_driver, NULL);
595 }
596
597 /* arch-tag: 64ec61bf-7c8e-4fe6-b953-c6a85d5e1605
598 (do not change this comment) */