Note that comment-style now defaults to indent.
[emacs.git] / src / xftfont.c
blob4a1b488bcf9d7efc42968aacf00ead5afae67c03
1 /* xftfont.c -- XFT font driver.
2 Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
3 Copyright (C) 2006, 2007, 2008, 2009
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H13PRO009
7 This file is part of GNU Emacs.
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.
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.
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/>. */
22 #include <config.h>
23 #include <stdio.h>
24 #include <X11/Xlib.h>
25 #include <X11/Xft/Xft.h>
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"
38 /* Xft font driver. */
40 static Lisp_Object Qxft;
41 static Lisp_Object QChinting , QCautohint, QChintstyle, QCrgba, QCembolden;
43 /* The actual structure for Xft font that can be casted to struct
44 font. */
46 struct xftfont_info
48 struct font font;
49 /* The following four members must be here in this order to be
50 compatible with struct ftfont_info (in ftfont.c). */
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 FT_Size ft_size;
56 int index;
57 Display *display;
58 int screen;
59 XftFont *xftfont;
62 /* Structure pointed by (struct face *)->extra */
64 struct xftface_info
66 XftColor xft_fg; /* color for face->foreground */
67 XftColor xft_bg; /* color for face->background */
70 static void xftfont_get_colors P_ ((FRAME_PTR, struct face *, GC gc,
71 struct xftface_info *,
72 XftColor *fg, XftColor *bg));
75 /* Setup foreground and background colors of GC into FG and BG. If
76 XFTFACE_INFO is not NULL, reuse the colors in it if possible. BG
77 may be NULL. */
79 static void
80 xftfont_get_colors (f, face, gc, xftface_info, fg, bg)
81 FRAME_PTR f;
82 struct face *face;
83 GC gc;
84 struct xftface_info *xftface_info;
85 XftColor *fg, *bg;
87 if (xftface_info && face->gc == gc)
89 *fg = xftface_info->xft_fg;
90 if (bg)
91 *bg = xftface_info->xft_bg;
93 else
95 XGCValues xgcv;
96 int fg_done = 0, bg_done = 0;
98 BLOCK_INPUT;
99 XGetGCValues (FRAME_X_DISPLAY (f), gc,
100 GCForeground | GCBackground, &xgcv);
101 if (xftface_info)
103 if (xgcv.foreground == face->foreground)
104 *fg = xftface_info->xft_fg, fg_done = 1;
105 else if (xgcv.foreground == face->background)
106 *fg = xftface_info->xft_bg, fg_done = 1;
107 if (! bg)
108 bg_done = 1;
109 else if (xgcv.background == face->background)
110 *bg = xftface_info->xft_bg, bg_done = 1;
111 else if (xgcv.background == face->foreground)
112 *bg = xftface_info->xft_fg, bg_done = 1;
115 if (fg_done + bg_done < 2)
117 XColor colors[2];
119 colors[0].pixel = fg->pixel = xgcv.foreground;
120 if (bg)
121 colors[1].pixel = bg->pixel = xgcv.background;
122 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors,
123 bg ? 2 : 1);
124 fg->color.alpha = 0xFFFF;
125 fg->color.red = colors[0].red;
126 fg->color.green = colors[0].green;
127 fg->color.blue = colors[0].blue;
128 if (bg)
130 bg->color.alpha = 0xFFFF;
131 bg->color.red = colors[1].red;
132 bg->color.green = colors[1].green;
133 bg->color.blue = colors[1].blue;
136 UNBLOCK_INPUT;
141 static Lisp_Object xftfont_list P_ ((Lisp_Object, Lisp_Object));
142 static Lisp_Object xftfont_match P_ ((Lisp_Object, Lisp_Object));
143 static Lisp_Object xftfont_open P_ ((FRAME_PTR, Lisp_Object, int));
144 static void xftfont_close P_ ((FRAME_PTR, struct font *));
145 static int xftfont_prepare_face P_ ((FRAME_PTR, struct face *));
146 static void xftfont_done_face P_ ((FRAME_PTR, struct face *));
147 static int xftfont_has_char P_ ((Lisp_Object, int));
148 static unsigned xftfont_encode_char P_ ((struct font *, int));
149 static int xftfont_text_extents P_ ((struct font *, unsigned *, int,
150 struct font_metrics *));
151 static int xftfont_draw P_ ((struct glyph_string *, int, int, int, int, int));
152 static int xftfont_end_for_frame P_ ((FRAME_PTR f));
154 struct font_driver xftfont_driver;
156 static Lisp_Object
157 xftfont_list (frame, spec)
158 Lisp_Object frame;
159 Lisp_Object spec;
161 Lisp_Object list = ftfont_driver.list (frame, spec), tail;
163 for (tail = list; CONSP (tail); tail = XCDR (tail))
164 ASET (XCAR (tail), FONT_TYPE_INDEX, Qxft);
165 return list;
168 static Lisp_Object
169 xftfont_match (frame, spec)
170 Lisp_Object frame;
171 Lisp_Object spec;
173 Lisp_Object entity = ftfont_driver.match (frame, spec);
175 if (! NILP (entity))
176 ASET (entity, FONT_TYPE_INDEX, Qxft);
177 return entity;
180 extern Lisp_Object ftfont_font_format P_ ((FcPattern *, Lisp_Object));
181 extern FcCharSet *ftfont_get_fc_charset P_ ((Lisp_Object));
182 extern Lisp_Object QCantialias;
184 static FcChar8 ascii_printable[95];
186 static Lisp_Object
187 xftfont_open (f, entity, pixel_size)
188 FRAME_PTR f;
189 Lisp_Object entity;
190 int pixel_size;
192 FcResult result;
193 Display *display = FRAME_X_DISPLAY (f);
194 Lisp_Object val, filename, index, tail, font_object;
195 FcPattern *pat = NULL, *match;
196 struct xftfont_info *xftfont_info = NULL;
197 struct font *font;
198 double size = 0;
199 XftFont *xftfont = NULL;
200 int spacing;
201 char name[256];
202 int len, i;
203 XGlyphInfo extents;
204 FT_Face ft_face;
206 val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
207 if (! CONSP (val))
208 return Qnil;
209 val = XCDR (val);
210 filename = XCAR (val);
211 index = XCDR (val);
212 size = XINT (AREF (entity, FONT_SIZE_INDEX));
213 if (size == 0)
214 size = pixel_size;
215 pat = FcPatternCreate ();
216 FcPatternAddInteger (pat, FC_WEIGHT, FONT_WEIGHT_NUMERIC (entity));
217 i = FONT_SLANT_NUMERIC (entity) - 100;
218 if (i < 0) i = 0;
219 FcPatternAddInteger (pat, FC_SLANT, i);
220 FcPatternAddInteger (pat, FC_WIDTH, FONT_WIDTH_NUMERIC (entity));
221 FcPatternAddDouble (pat, FC_PIXEL_SIZE, pixel_size);
222 val = AREF (entity, FONT_FAMILY_INDEX);
223 if (! NILP (val))
224 FcPatternAddString (pat, FC_FAMILY, (FcChar8 *) SDATA (SYMBOL_NAME (val)));
225 val = AREF (entity, FONT_FOUNDRY_INDEX);
226 if (! NILP (val))
227 FcPatternAddString (pat, FC_FOUNDRY, (FcChar8 *) SDATA (SYMBOL_NAME (val)));
228 val = AREF (entity, FONT_SPACING_INDEX);
229 if (! NILP (val))
230 FcPatternAddInteger (pat, FC_SPACING, XINT (val));
231 val = AREF (entity, FONT_DPI_INDEX);
232 if (! NILP (val))
234 double dbl = XINT (val);
236 FcPatternAddDouble (pat, FC_DPI, dbl);
238 val = AREF (entity, FONT_AVGWIDTH_INDEX);
239 if (INTEGERP (val) && XINT (val) == 0)
240 FcPatternAddBool (pat, FC_SCALABLE, FcTrue);
241 /* This is necessary to identify the exact font (e.g. 10x20.pcf.gz
242 over 10x20-ISO8859-1.pcf.gz). */
243 FcPatternAddCharSet (pat, FC_CHARSET, ftfont_get_fc_charset (entity));
245 for (tail = AREF (entity, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail))
247 Lisp_Object key, val;
249 key = XCAR (XCAR (tail)), val = XCDR (XCAR (tail));
250 if (EQ (key, QCantialias))
251 FcPatternAddBool (pat, FC_ANTIALIAS, NILP (val) ? FcFalse : FcTrue);
252 else if (EQ (key, QChinting))
253 FcPatternAddBool (pat, FC_HINTING, NILP (val) ? FcFalse : FcTrue);
254 else if (EQ (key, QCautohint))
255 FcPatternAddBool (pat, FC_AUTOHINT, NILP (val) ? FcFalse : FcTrue);
256 else if (EQ (key, QChintstyle))
258 if (INTEGERP (val))
259 FcPatternAddInteger (pat, FC_RGBA, XINT (val));
261 else if (EQ (key, QCrgba))
263 if (INTEGERP (val))
264 FcPatternAddInteger (pat, FC_RGBA, XINT (val));
266 #ifdef FC_EMBOLDEN
267 else if (EQ (key, QCembolden))
268 FcPatternAddBool (pat, FC_EMBOLDEN, NILP (val) ? FcFalse : FcTrue);
269 #endif
272 FcPatternAddString (pat, FC_FILE, (FcChar8 *) SDATA (filename));
273 FcPatternAddInteger (pat, FC_INDEX, XINT (index));
276 BLOCK_INPUT;
277 /* Make sure that the Xrender extension is added before the Xft one.
278 Otherwise, the close-display hook set by Xft is called after the
279 one for Xrender, and the former tries to re-add the latter. This
280 results in inconsistency of internal states and leads to X
281 protocol error when one reconnects to the same X server.
282 (Bug#1696) */
284 int event_base, error_base;
285 XRenderQueryExtension (display, &event_base, &error_base);
287 match = XftFontMatch (display, FRAME_X_SCREEN_NUMBER (f), pat, &result);
288 FcPatternDestroy (pat);
289 xftfont = XftFontOpenPattern (display, match);
290 ft_face = XftLockFace (xftfont);
291 UNBLOCK_INPUT;
293 if (! xftfont)
295 XftPatternDestroy (match);
296 return Qnil;
298 /* We should not destroy PAT here because it is kept in XFTFONT and
299 destroyed automatically when XFTFONT is closed. */
300 font_object = font_make_object (VECSIZE (struct xftfont_info), entity, size);
301 ASET (font_object, FONT_TYPE_INDEX, Qxft);
302 len = font_unparse_xlfd (entity, size, name, 256);
303 if (len > 0)
304 ASET (font_object, FONT_NAME_INDEX, make_string (name, len));
305 len = font_unparse_fcname (entity, size, name, 256);
306 if (len > 0)
307 ASET (font_object, FONT_FULLNAME_INDEX, make_string (name, len));
308 else
309 ASET (font_object, FONT_FULLNAME_INDEX,
310 AREF (font_object, FONT_NAME_INDEX));
311 ASET (font_object, FONT_FILE_INDEX, filename);
312 ASET (font_object, FONT_FORMAT_INDEX,
313 ftfont_font_format (xftfont->pattern, filename));
314 font = XFONT_OBJECT (font_object);
315 font->pixel_size = pixel_size;
316 font->driver = &xftfont_driver;
317 font->encoding_charset = font->repertory_charset = -1;
319 xftfont_info = (struct xftfont_info *) font;
320 xftfont_info->display = display;
321 xftfont_info->screen = FRAME_X_SCREEN_NUMBER (f);
322 xftfont_info->xftfont = xftfont;
323 font->pixel_size = size;
324 font->driver = &xftfont_driver;
325 if (INTEGERP (AREF (entity, FONT_SPACING_INDEX)))
326 spacing = XINT (AREF (entity, FONT_SPACING_INDEX));
327 else
328 spacing = FC_PROPORTIONAL;
329 if (! ascii_printable[0])
331 int i;
332 for (i = 0; i < 95; i++)
333 ascii_printable[i] = ' ' + i;
335 BLOCK_INPUT;
336 if (spacing != FC_PROPORTIONAL)
338 font->min_width = font->average_width = font->space_width
339 = xftfont->max_advance_width;
340 XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents);
342 else
344 XftTextExtents8 (display, xftfont, ascii_printable, 1, &extents);
345 font->space_width = extents.xOff;
346 if (font->space_width <= 0)
347 /* dirty workaround */
348 font->space_width = pixel_size;
349 XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents);
350 font->average_width = (font->space_width + extents.xOff) / 95;
352 UNBLOCK_INPUT;
354 font->ascent = xftfont->ascent;
355 font->descent = xftfont->descent;
356 if (pixel_size >= 5)
358 /* The above condition is a dirty workaround because
359 XftTextExtents8 behaves strangely for some fonts
360 (e.g. "Dejavu Sans Mono") when pixel_size is less than 5. */
361 if (font->ascent < extents.y)
362 font->ascent = extents.y;
363 if (font->descent < extents.height - extents.y)
364 font->descent = extents.height - extents.y;
366 font->height = font->ascent + font->descent;
368 if (XINT (AREF (entity, FONT_SIZE_INDEX)) == 0)
370 int upEM = ft_face->units_per_EM;
372 font->underline_position = -ft_face->underline_position * size / upEM;
373 font->underline_thickness = ft_face->underline_thickness * size / upEM;
374 if (font->underline_thickness > 2)
375 font->underline_position -= font->underline_thickness / 2;
377 else
379 font->underline_position = -1;
380 font->underline_thickness = 0;
382 #ifdef HAVE_LIBOTF
383 xftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT;
384 xftfont_info->otf = NULL;
385 #endif /* HAVE_LIBOTF */
386 xftfont_info->ft_size = ft_face->size;
388 /* Unfortunately Xft doesn't provide a way to get minimum char
389 width. So, we use space_width instead. */
390 font->min_width = font->space_width;
392 font->baseline_offset = 0;
393 font->relative_compose = 0;
394 font->default_ascent = 0;
395 font->vertical_centering = 0;
396 #ifdef FT_BDF_H
397 if (! (ft_face->face_flags & FT_FACE_FLAG_SFNT))
399 BDF_PropertyRec rec;
401 if (FT_Get_BDF_Property (ft_face, "_MULE_BASELINE_OFFSET", &rec) == 0
402 && rec.type == BDF_PROPERTY_TYPE_INTEGER)
403 font->baseline_offset = rec.u.integer;
404 if (FT_Get_BDF_Property (ft_face, "_MULE_RELATIVE_COMPOSE", &rec) == 0
405 && rec.type == BDF_PROPERTY_TYPE_INTEGER)
406 font->relative_compose = rec.u.integer;
407 if (FT_Get_BDF_Property (ft_face, "_MULE_DEFAULT_ASCENT", &rec) == 0
408 && rec.type == BDF_PROPERTY_TYPE_INTEGER)
409 font->default_ascent = rec.u.integer;
411 #endif
413 return font_object;
416 static void
417 xftfont_close (f, font)
418 FRAME_PTR f;
419 struct font *font;
421 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
423 #ifdef HAVE_LIBOTF
424 if (xftfont_info->otf)
425 OTF_close (xftfont_info->otf);
426 #endif
427 BLOCK_INPUT;
428 XftUnlockFace (xftfont_info->xftfont);
429 XftFontClose (xftfont_info->display, xftfont_info->xftfont);
430 UNBLOCK_INPUT;
433 static int
434 xftfont_prepare_face (f, face)
435 FRAME_PTR f;
436 struct face *face;
438 struct xftface_info *xftface_info;
440 #if 0
441 /* This doesn't work if face->ascii_face doesn't use an Xft font. */
442 if (face != face->ascii_face)
444 face->extra = face->ascii_face->extra;
445 return 0;
447 #endif
449 xftface_info = malloc (sizeof (struct xftface_info));
450 if (! xftface_info)
451 return -1;
452 xftfont_get_colors (f, face, face->gc, NULL,
453 &xftface_info->xft_fg, &xftface_info->xft_bg);
454 face->extra = xftface_info;
455 return 0;
458 static void
459 xftfont_done_face (f, face)
460 FRAME_PTR f;
461 struct face *face;
463 struct xftface_info *xftface_info;
465 #if 0
466 /* This doesn't work if face->ascii_face doesn't use an Xft font. */
467 if (face != face->ascii_face
468 || ! face->extra)
469 return;
470 #endif
472 xftface_info = (struct xftface_info *) face->extra;
473 if (xftface_info)
475 free (xftface_info);
476 face->extra = NULL;
480 extern Lisp_Object Qja, Qko;
482 static int
483 xftfont_has_char (font, c)
484 Lisp_Object font;
485 int c;
487 struct xftfont_info *xftfont_info;
488 struct charset *cs = NULL;
490 if (FONT_ENTITY_P (font))
491 return ftfont_driver.has_char (font, c);
493 if (EQ (AREF (font, FONT_ADSTYLE_INDEX), Qja)
494 && charset_jisx0208 >= 0)
495 cs = CHARSET_FROM_ID (charset_jisx0208);
496 else if (EQ (AREF (font, FONT_ADSTYLE_INDEX), Qko)
497 && charset_ksc5601 >= 0)
498 cs = CHARSET_FROM_ID (charset_ksc5601);
499 if (cs)
500 return (ENCODE_CHAR (cs, c) != CHARSET_INVALID_CODE (cs));
502 xftfont_info = (struct xftfont_info *) XFONT_OBJECT (font);
503 return (XftCharExists (xftfont_info->display, xftfont_info->xftfont,
504 (FcChar32) c) == FcTrue);
507 static unsigned
508 xftfont_encode_char (font, c)
509 struct font *font;
510 int c;
512 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
513 unsigned code = XftCharIndex (xftfont_info->display, xftfont_info->xftfont,
514 (FcChar32) c);
516 return (code ? code : FONT_INVALID_CODE);
519 static int
520 xftfont_text_extents (font, code, nglyphs, metrics)
521 struct font *font;
522 unsigned *code;
523 int nglyphs;
524 struct font_metrics *metrics;
526 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
527 XGlyphInfo extents;
529 BLOCK_INPUT;
530 XftGlyphExtents (xftfont_info->display, xftfont_info->xftfont, code, nglyphs,
531 &extents);
532 UNBLOCK_INPUT;
533 if (metrics)
535 metrics->lbearing = - extents.x;
536 metrics->rbearing = - extents.x + extents.width;
537 metrics->width = extents.xOff;
538 metrics->ascent = extents.y;
539 metrics->descent = extents.height - extents.y;
541 return extents.xOff;
544 static XftDraw *
545 xftfont_get_xft_draw (f)
546 FRAME_PTR f;
548 XftDraw *xft_draw = font_get_frame_data (f, &xftfont_driver);
550 if (! xft_draw)
552 BLOCK_INPUT;
553 xft_draw= XftDrawCreate (FRAME_X_DISPLAY (f),
554 FRAME_X_WINDOW (f),
555 FRAME_X_VISUAL (f),
556 FRAME_X_COLORMAP (f));
557 UNBLOCK_INPUT;
558 if (! xft_draw)
559 abort ();
560 font_put_frame_data (f, &xftfont_driver, xft_draw);
562 return xft_draw;
565 static int
566 xftfont_draw (s, from, to, x, y, with_background)
567 struct glyph_string *s;
568 int from, to, x, y, with_background;
570 FRAME_PTR f = s->f;
571 struct face *face = s->face;
572 struct xftfont_info *xftfont_info = (struct xftfont_info *) s->font;
573 struct xftface_info *xftface_info = NULL;
574 XftDraw *xft_draw = xftfont_get_xft_draw (f);
575 FT_UInt *code;
576 XftColor fg, bg;
577 int len = to - from;
578 int i;
580 if (s->font == face->font)
581 xftface_info = (struct xftface_info *) face->extra;
582 xftfont_get_colors (f, face, s->gc, xftface_info,
583 &fg, with_background ? &bg : NULL);
584 BLOCK_INPUT;
585 if (s->num_clips > 0)
586 XftDrawSetClipRectangles (xft_draw, 0, 0, s->clip, s->num_clips);
587 else
588 XftDrawSetClip (xft_draw, NULL);
590 if (with_background)
591 XftDrawRect (xft_draw, &bg,
592 x, y - face->font->ascent, s->width, face->font->height);
593 code = alloca (sizeof (FT_UInt) * len);
594 for (i = 0; i < len; i++)
595 code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
596 | XCHAR2B_BYTE2 (s->char2b + from + i));
598 if (s->padding_p)
599 for (i = 0; i < len; i++)
600 XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont,
601 x + i, y, code + i, 1);
602 else
603 XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont,
604 x, y, code, len);
605 UNBLOCK_INPUT;
607 return len;
610 static int
611 xftfont_end_for_frame (f)
612 FRAME_PTR f;
614 XftDraw *xft_draw = font_get_frame_data (f, &xftfont_driver);
616 if (xft_draw)
618 BLOCK_INPUT;
619 XftDrawDestroy (xft_draw);
620 UNBLOCK_INPUT;
621 font_put_frame_data (f, &xftfont_driver, NULL);
623 return 0;
626 void
627 syms_of_xftfont ()
629 DEFSYM (Qxft, "xft");
630 DEFSYM (QChinting, ":hinting");
631 DEFSYM (QCautohint, ":autohint");
632 DEFSYM (QChintstyle, ":hintstyle");
633 DEFSYM (QCrgba, ":rgba");
634 DEFSYM (QCembolden, ":embolden");
636 xftfont_driver = ftfont_driver;
637 xftfont_driver.type = Qxft;
638 xftfont_driver.get_cache = xfont_driver.get_cache;
639 xftfont_driver.list = xftfont_list;
640 xftfont_driver.match = xftfont_match;
641 xftfont_driver.open = xftfont_open;
642 xftfont_driver.close = xftfont_close;
643 xftfont_driver.prepare_face = xftfont_prepare_face;
644 xftfont_driver.done_face = xftfont_done_face;
645 xftfont_driver.has_char = xftfont_has_char;
646 xftfont_driver.encode_char = xftfont_encode_char;
647 xftfont_driver.text_extents = xftfont_text_extents;
648 xftfont_driver.draw = xftfont_draw;
649 xftfont_driver.end_for_frame = xftfont_end_for_frame;
651 register_font_driver (&xftfont_driver, NULL);
654 /* arch-tag: 64ec61bf-7c8e-4fe6-b953-c6a85d5e1605
655 (do not change this comment) */