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 int i;
160
161 for (tail = list; CONSP (tail); tail = XCDR (tail))
162 ASET (XCAR (tail), FONT_TYPE_INDEX, Qxft);
163 return list;
164 }
165
166 static Lisp_Object
167 xftfont_match (frame, spec)
168 Lisp_Object frame;
169 Lisp_Object spec;
170 {
171 Lisp_Object entity = ftfont_driver.match (frame, spec);
172
173 if (! NILP (entity))
174 ASET (entity, FONT_TYPE_INDEX, Qxft);
175 return entity;
176 }
177
178 extern Lisp_Object ftfont_font_format P_ ((FcPattern *));
179
180 static FcChar8 ascii_printable[95];
181
182 static Lisp_Object
183 xftfont_open (f, entity, pixel_size)
184 FRAME_PTR f;
185 Lisp_Object entity;
186 int pixel_size;
187 {
188 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
189 Display *display = FRAME_X_DISPLAY (f);
190 Lisp_Object val, font_object;
191 FcPattern *pattern, *pat = NULL;
192 FcChar8 *file;
193 struct xftfont_info *xftfont_info = NULL;
194 XFontStruct *xfont = NULL;
195 struct font *font;
196 double size = 0;
197 XftFont *xftfont = NULL;
198 int spacing;
199 char name[256];
200 int len, i;
201 XGlyphInfo extents;
202 FT_Face ft_face;
203
204 val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
205 if (! CONSP (val)
206 || XTYPE (XCDR (val)) != Lisp_Misc
207 || XMISCTYPE (XCDR (val)) != Lisp_Misc_Save_Value)
208 return Qnil;
209 pattern = XSAVE_VALUE (XCDR (val))->pointer;
210 if (FcPatternGetString (pattern, FC_FILE, 0, &file) != FcResultMatch)
211 return Qnil;
212
213 size = XINT (AREF (entity, FONT_SIZE_INDEX));
214 if (size == 0)
215 size = pixel_size;
216
217 pat = FcPatternCreate ();
218 FcPatternAddString (pat, FC_FILE, file);
219 FcPatternAddDouble (pat, FC_PIXEL_SIZE, pixel_size);
220 /*FcPatternAddBool (pat, FC_ANTIALIAS, FcTrue);*/
221 val = AREF (entity, FONT_FAMILY_INDEX);
222 if (! NILP (val))
223 FcPatternAddString (pat, FC_FAMILY, (FcChar8 *) SDATA (SYMBOL_NAME (val)));
224 FcConfigSubstitute (NULL, pat, FcMatchPattern);
225
226 BLOCK_INPUT;
227 XftDefaultSubstitute (display, FRAME_X_SCREEN_NUMBER (f), pat);
228 xftfont = XftFontOpenPattern (display, pat);
229 UNBLOCK_INPUT;
230 if (! xftfont)
231 return Qnil;
232 /* We should not destroy PAT here because it is kept in XFTFONT and
233 destroyed automatically when XFTFONT is closed. */
234 font_object = font_make_object (VECSIZE (struct xftfont_info));
235 ASET (font_object, FONT_TYPE_INDEX, Qxft);
236 for (i = 1; i < FONT_ENTITY_MAX; i++)
237 ASET (font_object, i, AREF (entity, i));
238 ASET (font_object, FONT_SIZE_INDEX, make_number (size));
239 len = font_unparse_xlfd (entity, size, name, 256);
240 if (len > 0)
241 ASET (font_object, FONT_NAME_INDEX, make_unibyte_string (name, len));
242 len = font_unparse_fcname (entity, size, name, 256);
243 if (len > 0)
244 ASET (font_object, FONT_FULLNAME_INDEX, make_unibyte_string (name, len));
245 else
246 ASET (font_object, FONT_FULLNAME_INDEX,
247 AREF (font_object, FONT_NAME_INDEX));
248 ASET (font_object, FONT_FILE_INDEX,
249 make_unibyte_string ((char *) file, strlen ((char *) file)));
250 ASET (font_object, FONT_FORMAT_INDEX, ftfont_font_format (pattern));
251 font = XFONT_OBJECT (font_object);
252 font->pixel_size = pixel_size;
253 font->driver = &xftfont_driver;
254 font->encoding_charset = font->repertory_charset = -1;
255
256 xftfont_info = (struct xftfont_info *) font;
257 xftfont_info->display = display;
258 xftfont_info->screen = FRAME_X_SCREEN_NUMBER (f);
259 xftfont_info->xftfont = xftfont;
260 font->pixel_size = size;
261 font->driver = &xftfont_driver;
262 if (FcPatternGetInteger (xftfont->pattern, FC_SPACING, 0, &spacing)
263 != FcResultMatch)
264 spacing = FC_PROPORTIONAL;
265 if (! ascii_printable[0])
266 {
267 int i;
268 for (i = 0; i < 95; i++)
269 ascii_printable[i] = ' ' + i;
270 }
271 BLOCK_INPUT;
272 if (spacing != FC_PROPORTIONAL)
273 {
274 font->min_width = font->average_width = font->space_width
275 = xftfont->max_advance_width;
276 XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents);
277 }
278 else
279 {
280 XftTextExtents8 (display, xftfont, ascii_printable, 1, &extents);
281 font->space_width = extents.xOff;
282 if (font->space_width <= 0)
283 /* dirty workaround */
284 font->space_width = pixel_size;
285 XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents);
286 font->average_width = (font->space_width + extents.xOff) / 95;
287 }
288 UNBLOCK_INPUT;
289
290 font->ascent = xftfont->ascent;
291 if (font->ascent < extents.y)
292 font->ascent = extents.y;
293 font->descent = xftfont->descent;
294 if (font->descent < extents.height - extents.y)
295 font->descent = extents.height - extents.y;
296 font->height = font->ascent + font->descent;
297
298 ft_face = XftLockFace (xftfont);
299 if (XINT (AREF (entity, FONT_SIZE_INDEX)) == 0)
300 {
301 int upEM = ft_face->units_per_EM;
302
303 font->underline_position = -ft_face->underline_position * size / upEM;
304 font->underline_thickness = -ft_face->underline_thickness * size / upEM;
305 }
306 else
307 {
308 font->underline_position = -1;
309 font->underline_thickness = 0;
310 }
311 #ifdef HAVE_LIBOTF
312 xftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT;
313 xftfont_info->otf = NULL;
314 #endif /* HAVE_LIBOTF */
315 XftUnlockFace (xftfont);
316
317 /* Unfortunately Xft doesn't provide a way to get minimum char
318 width. So, we use space_width instead. */
319 font->min_width = font->space_width;
320
321 font->baseline_offset = 0;
322 font->relative_compose = 0;
323 font->default_ascent = 0;
324 font->vertical_centering = 0;
325
326 return font_object;
327 }
328
329 static void
330 xftfont_close (f, font)
331 FRAME_PTR f;
332 struct font *font;
333 {
334 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
335
336 #ifdef HAVE_LIBOTF
337 if (xftfont_info->otf)
338 OTF_close (xftfont_info->otf);
339 #endif
340 BLOCK_INPUT;
341 XftFontClose (xftfont_info->display, xftfont_info->xftfont);
342 UNBLOCK_INPUT;
343 }
344
345 static int
346 xftfont_prepare_face (f, face)
347 FRAME_PTR f;
348 struct face *face;
349 {
350 struct xftface_info *xftface_info;
351
352 #if 0
353 /* This doesn't work if face->ascii_face doesn't use an Xft font. */
354 if (face != face->ascii_face)
355 {
356 face->extra = face->ascii_face->extra;
357 return 0;
358 }
359 #endif
360
361 xftface_info = malloc (sizeof (struct xftface_info));
362 if (! xftface_info)
363 return -1;
364 xftfont_get_colors (f, face, face->gc, NULL,
365 &xftface_info->xft_fg, &xftface_info->xft_bg);
366 face->extra = xftface_info;
367 return 0;
368 }
369
370 static void
371 xftfont_done_face (f, face)
372 FRAME_PTR f;
373 struct face *face;
374 {
375 struct xftface_info *xftface_info;
376
377 #if 0
378 /* This doesn't work if face->ascii_face doesn't use an Xft font. */
379 if (face != face->ascii_face
380 || ! face->extra)
381 return;
382 #endif
383
384 xftface_info = (struct xftface_info *) face->extra;
385 if (xftface_info)
386 {
387 free (xftface_info);
388 face->extra = NULL;
389 }
390 }
391
392 static unsigned
393 xftfont_encode_char (font, c)
394 struct font *font;
395 int c;
396 {
397 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
398 unsigned code = XftCharIndex (xftfont_info->display, xftfont_info->xftfont,
399 (FcChar32) c);
400
401 return (code ? code : FONT_INVALID_CODE);
402 }
403
404 static int
405 xftfont_text_extents (font, code, nglyphs, metrics)
406 struct font *font;
407 unsigned *code;
408 int nglyphs;
409 struct font_metrics *metrics;
410 {
411 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
412 XGlyphInfo extents;
413
414 BLOCK_INPUT;
415 XftGlyphExtents (xftfont_info->display, xftfont_info->xftfont, code, nglyphs,
416 &extents);
417 UNBLOCK_INPUT;
418 if (metrics)
419 {
420 metrics->lbearing = - extents.x;
421 metrics->rbearing = - extents.x + extents.width;
422 metrics->width = extents.xOff;
423 metrics->ascent = extents.y;
424 metrics->descent = extents.height - extents.y;
425 }
426 return extents.xOff;
427 }
428
429 static XftDraw *
430 xftfont_get_xft_draw (f)
431 FRAME_PTR f;
432 {
433 XftDraw *xft_draw = font_get_frame_data (f, &xftfont_driver);;
434
435 if (! xft_draw)
436 {
437 BLOCK_INPUT;
438 xft_draw= XftDrawCreate (FRAME_X_DISPLAY (f),
439 FRAME_X_WINDOW (f),
440 FRAME_X_VISUAL (f),
441 FRAME_X_COLORMAP (f));
442 UNBLOCK_INPUT;
443 if (! xft_draw)
444 abort ();
445 font_put_frame_data (f, &xftfont_driver, xft_draw);
446 }
447 return xft_draw;
448 }
449
450 static int
451 xftfont_draw (s, from, to, x, y, with_background)
452 struct glyph_string *s;
453 int from, to, x, y, with_background;
454 {
455 FRAME_PTR f = s->f;
456 struct face *face = s->face;
457 struct xftfont_info *xftfont_info = (struct xftfont_info *) s->font;
458 struct xftface_info *xftface_info = NULL;
459 XftDraw *xft_draw = xftfont_get_xft_draw (f);
460 FT_UInt *code;
461 XftColor fg, bg;
462 XRectangle r;
463 int len = to - from;
464 int i;
465
466 if (s->font == face->font)
467 xftface_info = (struct xftface_info *) face->extra;
468 xftfont_get_colors (f, face, s->gc, xftface_info,
469 &fg, with_background ? &bg : NULL);
470 BLOCK_INPUT;
471 if (s->num_clips > 0)
472 XftDrawSetClipRectangles (xft_draw, 0, 0, s->clip, s->num_clips);
473 else
474 XftDrawSetClip (xft_draw, NULL);
475
476 if (with_background)
477 XftDrawRect (xft_draw, &bg,
478 x, y - face->font->ascent, s->width, face->font->height);
479 code = alloca (sizeof (FT_UInt) * len);
480 for (i = 0; i < len; i++)
481 code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
482 | XCHAR2B_BYTE2 (s->char2b + from + i));
483
484 if (s->padding_p)
485 for (i = 0; i < len; i++)
486 XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont,
487 x + i, y, code + i, 1);
488 else
489 XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont,
490 x, y, code, len);
491 UNBLOCK_INPUT;
492
493 return len;
494 }
495
496 static int
497 xftfont_anchor_point (font, code, index, x, y)
498 struct font *font;
499 unsigned code;
500 int index;
501 int *x, *y;
502 {
503 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
504 FT_Face ft_face = XftLockFace (xftfont_info->xftfont);
505 int result;
506
507 if (FT_Load_Glyph (ft_face, code, FT_LOAD_DEFAULT) != 0)
508 result = -1;
509 else if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
510 result = -1;
511 else if (index >= ft_face->glyph->outline.n_points)
512 result = -1;
513 else
514 {
515 *x = ft_face->glyph->outline.points[index].x;
516 *y = ft_face->glyph->outline.points[index].y;
517 }
518 XftUnlockFace (xftfont_info->xftfont);
519 return result;
520 }
521
522 static int
523 xftfont_end_for_frame (f)
524 FRAME_PTR f;
525 {
526 XftDraw *xft_draw = font_get_frame_data (f, &xftfont_driver);
527
528 if (xft_draw)
529 {
530 BLOCK_INPUT;
531 XftDrawDestroy (xft_draw);
532 UNBLOCK_INPUT;
533 font_put_frame_data (f, &xftfont_driver, NULL);
534 }
535 return 0;
536 }
537
538 #ifdef HAVE_LIBOTF
539 #ifdef HAVE_M17N_FLT
540 static Lisp_Object
541 xftfont_shape (lgstring)
542 Lisp_Object lgstring;
543 {
544 struct font *font;
545 struct xftfont_info *xftfont_info;
546 Lisp_Object result;
547 FT_Face ft_face;
548
549 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring), font);
550 xftfont_info = (struct xftfont_info *) font;
551 if (! xftfont_info->maybe_otf)
552 return Qnil;
553 ft_face = XftLockFace (xftfont_info->xftfont);
554 if (! xftfont_info->otf)
555 {
556 OTF *otf = OTF_open_ft_face (ft_face);
557
558 if (! otf || OTF_get_table (otf, "head") < 0)
559 {
560 if (otf)
561 OTF_close (otf);
562 xftfont_info->maybe_otf = 0;
563 XftUnlockFace (xftfont_info->xftfont);
564 return Qnil;
565 }
566 xftfont_info->otf = otf;
567 }
568
569 result = ftfont_shape_by_flt (lgstring, font, ft_face, xftfont_info->otf);
570 XftUnlockFace (xftfont_info->xftfont);
571 return result;
572 }
573 #endif /* HAVE_M17N_FLT */
574 #endif /* HAVE_LIBOTF */
575
576 void
577 syms_of_xftfont ()
578 {
579 DEFSYM (Qxft, "xft");
580
581 xftfont_driver = ftfont_driver;
582 xftfont_driver.type = Qxft;
583 xftfont_driver.get_cache = xfont_driver.get_cache;
584 xftfont_driver.list = xftfont_list;
585 xftfont_driver.match = xftfont_match;
586 xftfont_driver.open = xftfont_open;
587 xftfont_driver.close = xftfont_close;
588 xftfont_driver.prepare_face = xftfont_prepare_face;
589 xftfont_driver.done_face = xftfont_done_face;
590 xftfont_driver.encode_char = xftfont_encode_char;
591 xftfont_driver.text_extents = xftfont_text_extents;
592 xftfont_driver.draw = xftfont_draw;
593 xftfont_driver.anchor_point = xftfont_anchor_point;
594 xftfont_driver.end_for_frame = xftfont_end_for_frame;
595 #ifdef HAVE_LIBOTF
596 #ifdef HAVE_M17N_FLT
597 xftfont_driver.shape = xftfont_shape;
598 #endif /* HAVE_M17N_FLT */
599 #endif /* HAVE_LIBOTF */
600
601 register_font_driver (&xftfont_driver, NULL);
602 }
603
604 /* arch-tag: 64ec61bf-7c8e-4fe6-b953-c6a85d5e1605
605 (do not change this comment) */