(Old Revisions): Fix diff-switches description.
[emacs.git] / src / xftfont.c
blob5da5ac499dac22b9915379d354946361d5527230
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
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 three 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 Display *display;
57 int screen;
58 XftFont *xftfont;
61 /* Structure pointed by (struct face *)->extra */
63 struct xftface_info
65 XftColor xft_fg; /* color for face->foreground */
66 XftColor xft_bg; /* color for face->background */
69 static void xftfont_get_colors P_ ((FRAME_PTR, struct face *, GC gc,
70 struct xftface_info *,
71 XftColor *fg, XftColor *bg));
74 /* Setup foreground and background colors of GC into FG and BG. If
75 XFTFACE_INFO is not NULL, reuse the colors in it if possible. BG
76 may be NULL. */
78 static void
79 xftfont_get_colors (f, face, gc, xftface_info, fg, bg)
80 FRAME_PTR f;
81 struct face *face;
82 GC gc;
83 struct xftface_info *xftface_info;
84 XftColor *fg, *bg;
86 if (xftface_info && face->gc == gc)
88 *fg = xftface_info->xft_fg;
89 if (bg)
90 *bg = xftface_info->xft_bg;
92 else
94 XGCValues xgcv;
95 int fg_done = 0, bg_done = 0;
97 BLOCK_INPUT;
98 XGetGCValues (FRAME_X_DISPLAY (f), gc,
99 GCForeground | GCBackground, &xgcv);
100 if (xftface_info)
102 if (xgcv.foreground == face->foreground)
103 *fg = xftface_info->xft_fg, fg_done = 1;
104 else if (xgcv.foreground == face->background)
105 *fg = xftface_info->xft_bg, fg_done = 1;
106 if (! bg)
107 bg_done = 1;
108 else if (xgcv.background == face->background)
109 *bg = xftface_info->xft_bg, bg_done = 1;
110 else if (xgcv.background == face->foreground)
111 *bg = xftface_info->xft_fg, bg_done = 1;
114 if (fg_done + bg_done < 2)
116 XColor colors[2];
118 colors[0].pixel = fg->pixel = xgcv.foreground;
119 if (bg)
120 colors[1].pixel = bg->pixel = xgcv.background;
121 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors,
122 bg ? 2 : 1);
123 fg->color.alpha = 0xFFFF;
124 fg->color.red = colors[0].red;
125 fg->color.green = colors[0].green;
126 fg->color.blue = colors[0].blue;
127 if (bg)
129 bg->color.alpha = 0xFFFF;
130 bg->color.red = colors[1].red;
131 bg->color.green = colors[1].green;
132 bg->color.blue = colors[1].blue;
135 UNBLOCK_INPUT;
140 static Lisp_Object xftfont_list P_ ((Lisp_Object, Lisp_Object));
141 static Lisp_Object xftfont_match P_ ((Lisp_Object, Lisp_Object));
142 static Lisp_Object xftfont_open P_ ((FRAME_PTR, Lisp_Object, int));
143 static void xftfont_close P_ ((FRAME_PTR, struct font *));
144 static int xftfont_prepare_face P_ ((FRAME_PTR, struct face *));
145 static void xftfont_done_face P_ ((FRAME_PTR, struct face *));
146 static unsigned xftfont_encode_char P_ ((struct font *, int));
147 static int xftfont_text_extents P_ ((struct font *, unsigned *, int,
148 struct font_metrics *));
149 static int xftfont_draw P_ ((struct glyph_string *, int, int, int, int, int));
150 static int xftfont_end_for_frame P_ ((FRAME_PTR f));
152 struct font_driver xftfont_driver;
154 static Lisp_Object
155 xftfont_list (frame, spec)
156 Lisp_Object frame;
157 Lisp_Object spec;
159 Lisp_Object list = ftfont_driver.list (frame, spec), tail;
161 for (tail = list; CONSP (tail); tail = XCDR (tail))
162 ASET (XCAR (tail), FONT_TYPE_INDEX, Qxft);
163 return list;
166 static Lisp_Object
167 xftfont_match (frame, spec)
168 Lisp_Object frame;
169 Lisp_Object spec;
171 Lisp_Object entity = ftfont_driver.match (frame, spec);
173 if (! NILP (entity))
174 ASET (entity, FONT_TYPE_INDEX, Qxft);
175 return entity;
178 extern Lisp_Object ftfont_font_format P_ ((FcPattern *, Lisp_Object));
179 extern FcCharSet *ftfont_get_fc_charset P_ ((Lisp_Object));
180 extern Lisp_Object QCantialias;
182 static FcChar8 ascii_printable[95];
184 static Lisp_Object
185 xftfont_open (f, entity, pixel_size)
186 FRAME_PTR f;
187 Lisp_Object entity;
188 int pixel_size;
190 FcResult result;
191 Display *display = FRAME_X_DISPLAY (f);
192 Lisp_Object val, filename, index, tail, font_object;
193 FcPattern *pat = NULL, *match;
194 struct xftfont_info *xftfont_info = 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;
204 val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
205 if (! CONSP (val))
206 return Qnil;
207 val = XCDR (val);
208 filename = XCAR (val);
209 index = XCDR (val);
210 size = XINT (AREF (entity, FONT_SIZE_INDEX));
211 if (size == 0)
212 size = pixel_size;
213 pat = FcPatternCreate ();
214 FcPatternAddInteger (pat, FC_WEIGHT, FONT_WEIGHT_NUMERIC (entity));
215 i = FONT_SLANT_NUMERIC (entity) - 100;
216 if (i < 0) i = 0;
217 FcPatternAddInteger (pat, FC_SLANT, i);
218 FcPatternAddInteger (pat, FC_WIDTH, FONT_WIDTH_NUMERIC (entity));
219 FcPatternAddDouble (pat, FC_PIXEL_SIZE, pixel_size);
220 val = AREF (entity, FONT_FAMILY_INDEX);
221 if (! NILP (val))
222 FcPatternAddString (pat, FC_FAMILY, (FcChar8 *) SDATA (SYMBOL_NAME (val)));
223 val = AREF (entity, FONT_FOUNDRY_INDEX);
224 if (! NILP (val))
225 FcPatternAddString (pat, FC_FOUNDRY, (FcChar8 *) SDATA (SYMBOL_NAME (val)));
226 val = AREF (entity, FONT_SPACING_INDEX);
227 if (! NILP (val))
228 FcPatternAddInteger (pat, FC_SPACING, XINT (val));
229 val = AREF (entity, FONT_DPI_INDEX);
230 if (! NILP (val))
232 double dbl = XINT (val);
234 FcPatternAddDouble (pat, FC_DPI, dbl);
236 val = AREF (entity, FONT_AVGWIDTH_INDEX);
237 if (INTEGERP (val) && XINT (val) == 0)
238 FcPatternAddBool (pat, FC_SCALABLE, FcTrue);
239 /* This is necessary to identify the exact font (e.g. 10x20.pcf.gz
240 over 10x20-ISO8859-1.pcf.gz). */
241 FcPatternAddCharSet (pat, FC_CHARSET, ftfont_get_fc_charset (entity));
243 for (tail = AREF (entity, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail))
245 Lisp_Object key, val;
247 key = XCAR (XCAR (tail)), val = XCDR (XCAR (tail));
248 if (EQ (key, QCantialias))
249 FcPatternAddBool (pat, FC_ANTIALIAS, NILP (val) ? FcFalse : FcTrue);
250 else if (EQ (key, QChinting))
251 FcPatternAddBool (pat, FC_HINTING, NILP (val) ? FcFalse : FcTrue);
252 else if (EQ (key, QCautohint))
253 FcPatternAddBool (pat, FC_AUTOHINT, NILP (val) ? FcFalse : FcTrue);
254 else if (EQ (key, QChintstyle))
256 if (INTEGERP (val))
257 FcPatternAddInteger (pat, FC_RGBA, XINT (val));
259 else if (EQ (key, QCrgba))
261 if (INTEGERP (val))
262 FcPatternAddInteger (pat, FC_RGBA, XINT (val));
264 #ifdef FC_EMBOLDEN
265 else if (EQ (key, QCembolden))
266 FcPatternAddBool (pat, FC_EMBOLDEN, NILP (val) ? FcFalse : FcTrue);
267 #endif
270 FcPatternAddString (pat, FC_FILE, (FcChar8 *) SDATA (filename));
271 FcPatternAddInteger (pat, FC_INDEX, XINT (index));
274 BLOCK_INPUT;
275 match = XftFontMatch (display, FRAME_X_SCREEN_NUMBER (f), pat, &result);
276 FcPatternDestroy (pat);
277 xftfont = XftFontOpenPattern (display, match);
278 ft_face = XftLockFace (xftfont);
279 UNBLOCK_INPUT;
281 if (! xftfont)
282 return Qnil;
283 /* We should not destroy PAT here because it is kept in XFTFONT and
284 destroyed automatically when XFTFONT is closed. */
285 font_object = font_make_object (VECSIZE (struct xftfont_info), entity, size);
286 ASET (font_object, FONT_TYPE_INDEX, Qxft);
287 len = font_unparse_xlfd (entity, size, name, 256);
288 if (len > 0)
289 ASET (font_object, FONT_NAME_INDEX, make_unibyte_string (name, len));
290 len = font_unparse_fcname (entity, size, name, 256);
291 if (len > 0)
292 ASET (font_object, FONT_FULLNAME_INDEX, make_unibyte_string (name, len));
293 else
294 ASET (font_object, FONT_FULLNAME_INDEX,
295 AREF (font_object, FONT_NAME_INDEX));
296 ASET (font_object, FONT_FILE_INDEX, filename);
297 ASET (font_object, FONT_FORMAT_INDEX,
298 ftfont_font_format (xftfont->pattern, filename));
299 font = XFONT_OBJECT (font_object);
300 font->pixel_size = pixel_size;
301 font->driver = &xftfont_driver;
302 font->encoding_charset = font->repertory_charset = -1;
304 xftfont_info = (struct xftfont_info *) font;
305 xftfont_info->display = display;
306 xftfont_info->screen = FRAME_X_SCREEN_NUMBER (f);
307 xftfont_info->xftfont = xftfont;
308 font->pixel_size = size;
309 font->driver = &xftfont_driver;
310 if (INTEGERP (AREF (entity, FONT_SPACING_INDEX)))
311 spacing = XINT (AREF (entity, FONT_SPACING_INDEX));
312 else
313 spacing = FC_PROPORTIONAL;
314 if (! ascii_printable[0])
316 int i;
317 for (i = 0; i < 95; i++)
318 ascii_printable[i] = ' ' + i;
320 BLOCK_INPUT;
321 if (spacing != FC_PROPORTIONAL)
323 font->min_width = font->average_width = font->space_width
324 = xftfont->max_advance_width;
325 XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents);
327 else
329 XftTextExtents8 (display, xftfont, ascii_printable, 1, &extents);
330 font->space_width = extents.xOff;
331 if (font->space_width <= 0)
332 /* dirty workaround */
333 font->space_width = pixel_size;
334 XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents);
335 font->average_width = (font->space_width + extents.xOff) / 95;
337 UNBLOCK_INPUT;
339 font->ascent = xftfont->ascent;
340 font->descent = xftfont->descent;
341 if (pixel_size >= 5)
343 /* The above condition is a dirty workaround because
344 XftTextExtents8 behaves strangely for some fonts
345 (e.g. "Dejavu Sans Mono") when pixel_size is less than 5. */
346 if (font->ascent < extents.y)
347 font->ascent = extents.y;
348 if (font->descent < extents.height - extents.y)
349 font->descent = extents.height - extents.y;
351 font->height = font->ascent + font->descent;
353 if (XINT (AREF (entity, FONT_SIZE_INDEX)) == 0)
355 int upEM = ft_face->units_per_EM;
357 font->underline_position = -ft_face->underline_position * size / upEM;
358 font->underline_thickness = -ft_face->underline_thickness * size / upEM;
359 if (font->underline_thickness > 2)
360 font->underline_position -= font->underline_thickness / 2;
362 else
364 font->underline_position = -1;
365 font->underline_thickness = 0;
367 #ifdef HAVE_LIBOTF
368 xftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT;
369 xftfont_info->otf = NULL;
370 #endif /* HAVE_LIBOTF */
371 xftfont_info->ft_size = ft_face->size;
373 /* Unfortunately Xft doesn't provide a way to get minimum char
374 width. So, we use space_width instead. */
375 font->min_width = font->space_width;
377 font->baseline_offset = 0;
378 font->relative_compose = 0;
379 font->default_ascent = 0;
380 font->vertical_centering = 0;
381 #ifdef FT_BDF_H
382 if (! (ft_face->face_flags & FT_FACE_FLAG_SFNT))
384 BDF_PropertyRec rec;
386 if (FT_Get_BDF_Property (ft_face, "_MULE_BASELINE_OFFSET", &rec) == 0
387 && rec.type == BDF_PROPERTY_TYPE_INTEGER)
388 font->baseline_offset = rec.u.integer;
389 if (FT_Get_BDF_Property (ft_face, "_MULE_RELATIVE_COMPOSE", &rec) == 0
390 && rec.type == BDF_PROPERTY_TYPE_INTEGER)
391 font->relative_compose = rec.u.integer;
392 if (FT_Get_BDF_Property (ft_face, "_MULE_DEFAULT_ASCENT", &rec) == 0
393 && rec.type == BDF_PROPERTY_TYPE_INTEGER)
394 font->default_ascent = rec.u.integer;
396 #endif
398 return font_object;
401 static void
402 xftfont_close (f, font)
403 FRAME_PTR f;
404 struct font *font;
406 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
408 #ifdef HAVE_LIBOTF
409 if (xftfont_info->otf)
410 OTF_close (xftfont_info->otf);
411 #endif
412 BLOCK_INPUT;
413 XftUnlockFace (xftfont_info->xftfont);
414 XftFontClose (xftfont_info->display, xftfont_info->xftfont);
415 UNBLOCK_INPUT;
418 static int
419 xftfont_prepare_face (f, face)
420 FRAME_PTR f;
421 struct face *face;
423 struct xftface_info *xftface_info;
425 #if 0
426 /* This doesn't work if face->ascii_face doesn't use an Xft font. */
427 if (face != face->ascii_face)
429 face->extra = face->ascii_face->extra;
430 return 0;
432 #endif
434 xftface_info = malloc (sizeof (struct xftface_info));
435 if (! xftface_info)
436 return -1;
437 xftfont_get_colors (f, face, face->gc, NULL,
438 &xftface_info->xft_fg, &xftface_info->xft_bg);
439 face->extra = xftface_info;
440 return 0;
443 static void
444 xftfont_done_face (f, face)
445 FRAME_PTR f;
446 struct face *face;
448 struct xftface_info *xftface_info;
450 #if 0
451 /* This doesn't work if face->ascii_face doesn't use an Xft font. */
452 if (face != face->ascii_face
453 || ! face->extra)
454 return;
455 #endif
457 xftface_info = (struct xftface_info *) face->extra;
458 if (xftface_info)
460 free (xftface_info);
461 face->extra = NULL;
465 static unsigned
466 xftfont_encode_char (font, c)
467 struct font *font;
468 int c;
470 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
471 unsigned code = XftCharIndex (xftfont_info->display, xftfont_info->xftfont,
472 (FcChar32) c);
474 return (code ? code : FONT_INVALID_CODE);
477 static int
478 xftfont_text_extents (font, code, nglyphs, metrics)
479 struct font *font;
480 unsigned *code;
481 int nglyphs;
482 struct font_metrics *metrics;
484 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
485 XGlyphInfo extents;
487 BLOCK_INPUT;
488 XftGlyphExtents (xftfont_info->display, xftfont_info->xftfont, code, nglyphs,
489 &extents);
490 UNBLOCK_INPUT;
491 if (metrics)
493 metrics->lbearing = - extents.x;
494 metrics->rbearing = - extents.x + extents.width;
495 metrics->width = extents.xOff;
496 metrics->ascent = extents.y;
497 metrics->descent = extents.height - extents.y;
499 return extents.xOff;
502 static XftDraw *
503 xftfont_get_xft_draw (f)
504 FRAME_PTR f;
506 XftDraw *xft_draw = font_get_frame_data (f, &xftfont_driver);;
508 if (! xft_draw)
510 BLOCK_INPUT;
511 xft_draw= XftDrawCreate (FRAME_X_DISPLAY (f),
512 FRAME_X_WINDOW (f),
513 FRAME_X_VISUAL (f),
514 FRAME_X_COLORMAP (f));
515 UNBLOCK_INPUT;
516 if (! xft_draw)
517 abort ();
518 font_put_frame_data (f, &xftfont_driver, xft_draw);
520 return xft_draw;
523 static int
524 xftfont_draw (s, from, to, x, y, with_background)
525 struct glyph_string *s;
526 int from, to, x, y, with_background;
528 FRAME_PTR f = s->f;
529 struct face *face = s->face;
530 struct xftfont_info *xftfont_info = (struct xftfont_info *) s->font;
531 struct xftface_info *xftface_info = NULL;
532 XftDraw *xft_draw = xftfont_get_xft_draw (f);
533 FT_UInt *code;
534 XftColor fg, bg;
535 int len = to - from;
536 int i;
538 if (s->font == face->font)
539 xftface_info = (struct xftface_info *) face->extra;
540 xftfont_get_colors (f, face, s->gc, xftface_info,
541 &fg, with_background ? &bg : NULL);
542 BLOCK_INPUT;
543 if (s->num_clips > 0)
544 XftDrawSetClipRectangles (xft_draw, 0, 0, s->clip, s->num_clips);
545 else
546 XftDrawSetClip (xft_draw, NULL);
548 if (with_background)
549 XftDrawRect (xft_draw, &bg,
550 x, y - face->font->ascent, s->width, face->font->height);
551 code = alloca (sizeof (FT_UInt) * len);
552 for (i = 0; i < len; i++)
553 code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
554 | XCHAR2B_BYTE2 (s->char2b + from + i));
556 if (s->padding_p)
557 for (i = 0; i < len; i++)
558 XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont,
559 x + i, y, code + i, 1);
560 else
561 XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont,
562 x, y, code, len);
563 UNBLOCK_INPUT;
565 return len;
568 static int
569 xftfont_end_for_frame (f)
570 FRAME_PTR f;
572 XftDraw *xft_draw = font_get_frame_data (f, &xftfont_driver);
574 if (xft_draw)
576 BLOCK_INPUT;
577 XftDrawDestroy (xft_draw);
578 UNBLOCK_INPUT;
579 font_put_frame_data (f, &xftfont_driver, NULL);
581 return 0;
584 void
585 syms_of_xftfont ()
587 DEFSYM (Qxft, "xft");
588 DEFSYM (QChinting, ":hinting");
589 DEFSYM (QCautohint, ":autohing");
590 DEFSYM (QChintstyle, ":hintstyle");
591 DEFSYM (QCrgba, ":rgba");
592 DEFSYM (QCembolden, ":embolden");
594 xftfont_driver = ftfont_driver;
595 xftfont_driver.type = Qxft;
596 xftfont_driver.get_cache = xfont_driver.get_cache;
597 xftfont_driver.list = xftfont_list;
598 xftfont_driver.match = xftfont_match;
599 xftfont_driver.open = xftfont_open;
600 xftfont_driver.close = xftfont_close;
601 xftfont_driver.prepare_face = xftfont_prepare_face;
602 xftfont_driver.done_face = xftfont_done_face;
603 xftfont_driver.encode_char = xftfont_encode_char;
604 xftfont_driver.text_extents = xftfont_text_extents;
605 xftfont_driver.draw = xftfont_draw;
606 xftfont_driver.end_for_frame = xftfont_end_for_frame;
608 register_font_driver (&xftfont_driver, NULL);
611 /* arch-tag: 64ec61bf-7c8e-4fe6-b953-c6a85d5e1605
612 (do not change this comment) */