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, or (at your option)
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; see the file COPYING. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
27 #include <X11/Xft/Xft.h>
30 #include "dispextern.h"
33 #include "blockinput.h"
34 #include "character.h"
40 /* Xft font driver. */
42 static Lisp_Object Qxft
;
44 /* The actual structure for Xft font that can be casted to struct
54 int maybe_otf
; /* Flag to tell if this may be OTF or not. */
56 #endif /* HAVE_LIBOTF */
59 /* Structure pointed by (struct face *)->extra */
63 XftColor xft_fg
; /* color for face->foreground */
64 XftColor xft_bg
; /* color for face->background */
67 static void xftfont_get_colors
P_ ((FRAME_PTR
, struct face
*, GC gc
,
68 struct xftface_info
*,
69 XftColor
*fg
, XftColor
*bg
));
72 /* Setup foreground and background colors of GC into FG and BG. If
73 XFTFACE_INFO is not NULL, reuse the colors in it if possible. BG
77 xftfont_get_colors (f
, face
, gc
, xftface_info
, fg
, bg
)
81 struct xftface_info
*xftface_info
;
84 if (xftface_info
&& face
->gc
== gc
)
86 *fg
= xftface_info
->xft_fg
;
88 *bg
= xftface_info
->xft_bg
;
93 int fg_done
= 0, bg_done
= 0;
96 XGetGCValues (FRAME_X_DISPLAY (f
), gc
,
97 GCForeground
| GCBackground
, &xgcv
);
100 if (xgcv
.foreground
== face
->foreground
)
101 *fg
= xftface_info
->xft_fg
, fg_done
= 1;
102 else if (xgcv
.foreground
== face
->background
)
103 *fg
= xftface_info
->xft_bg
, fg_done
= 1;
106 else if (xgcv
.background
== face
->background
)
107 *bg
= xftface_info
->xft_bg
, bg_done
= 1;
108 else if (xgcv
.background
== face
->foreground
)
109 *bg
= xftface_info
->xft_fg
, bg_done
= 1;
112 if (fg_done
+ bg_done
< 2)
116 colors
[0].pixel
= fg
->pixel
= xgcv
.foreground
;
118 colors
[1].pixel
= bg
->pixel
= xgcv
.background
;
119 XQueryColors (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
), colors
,
121 fg
->color
.alpha
= 0xFFFF;
122 fg
->color
.red
= colors
[0].red
;
123 fg
->color
.green
= colors
[0].green
;
124 fg
->color
.blue
= colors
[0].blue
;
127 bg
->color
.alpha
= 0xFFFF;
128 bg
->color
.red
= colors
[1].red
;
129 bg
->color
.green
= colors
[1].green
;
130 bg
->color
.blue
= colors
[1].blue
;
138 static Lisp_Object xftfont_list
P_ ((Lisp_Object
, Lisp_Object
));
139 static Lisp_Object xftfont_match
P_ ((Lisp_Object
, Lisp_Object
));
140 static Lisp_Object xftfont_open
P_ ((FRAME_PTR
, Lisp_Object
, int));
141 static void xftfont_close
P_ ((FRAME_PTR
, struct font
*));
142 static int xftfont_prepare_face
P_ ((FRAME_PTR
, struct face
*));
143 static void xftfont_done_face
P_ ((FRAME_PTR
, struct face
*));
144 static unsigned xftfont_encode_char
P_ ((struct font
*, int));
145 static int xftfont_text_extents
P_ ((struct font
*, unsigned *, int,
146 struct font_metrics
*));
147 static int xftfont_draw
P_ ((struct glyph_string
*, int, int, int, int, int));
149 static int xftfont_anchor_point
P_ ((struct font
*, unsigned, int,
151 static int xftfont_end_for_frame
P_ ((FRAME_PTR f
));
153 struct font_driver xftfont_driver
;
156 xftfont_list (frame
, spec
)
160 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
);
169 xftfont_match (frame
, spec
)
173 Lisp_Object entity
= ftfont_driver
.match (frame
, spec
);
176 ASET (entity
, FONT_TYPE_INDEX
, Qxft
);
180 extern Lisp_Object ftfont_font_format
P_ ((FcPattern
*));
182 static FcChar8 ascii_printable
[95];
185 xftfont_open (f
, entity
, pixel_size
)
190 Display_Info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
191 Display
*display
= FRAME_X_DISPLAY (f
);
192 Lisp_Object val
, font_object
;
193 FcPattern
*pattern
, *pat
= NULL
;
195 struct xftfont_info
*xftfont_info
= NULL
;
196 XFontStruct
*xfont
= NULL
;
199 XftFont
*xftfont
= NULL
;
206 val
= assq_no_quit (QCfont_entity
, AREF (entity
, FONT_EXTRA_INDEX
));
208 || XTYPE (XCDR (val
)) != Lisp_Misc
209 || XMISCTYPE (XCDR (val
)) != Lisp_Misc_Save_Value
)
211 pattern
= XSAVE_VALUE (XCDR (val
))->pointer
;
212 if (FcPatternGetString (pattern
, FC_FILE
, 0, &file
) != FcResultMatch
)
215 size
= XINT (AREF (entity
, FONT_SIZE_INDEX
));
219 pat
= FcPatternCreate ();
220 FcPatternAddString (pat
, FC_FILE
, file
);
221 FcPatternAddDouble (pat
, FC_PIXEL_SIZE
, pixel_size
);
222 /*FcPatternAddBool (pat, FC_ANTIALIAS, FcTrue);*/
223 val
= AREF (entity
, FONT_FAMILY_INDEX
);
225 FcPatternAddString (pat
, FC_FAMILY
, (FcChar8
*) SDATA (SYMBOL_NAME (val
)));
226 FcConfigSubstitute (NULL
, pat
, FcMatchPattern
);
229 XftDefaultSubstitute (display
, FRAME_X_SCREEN_NUMBER (f
), pat
);
230 xftfont
= XftFontOpenPattern (display
, pat
);
234 /* We should not destroy PAT here because it is kept in XFTFONT and
235 destroyed automatically when XFTFONT is closed. */
236 font_object
= font_make_object (VECSIZE (struct xftfont_info
));
237 ASET (font_object
, FONT_TYPE_INDEX
, Qxft
);
238 for (i
= 1; i
< FONT_ENTITY_MAX
; i
++)
239 ASET (font_object
, i
, AREF (entity
, i
));
240 ASET (font_object
, FONT_SIZE_INDEX
, make_number (size
));
241 len
= font_unparse_xlfd (entity
, size
, name
, 256);
243 ASET (font_object
, FONT_NAME_INDEX
, make_unibyte_string (name
, len
));
244 len
= font_unparse_fcname (entity
, size
, name
, 256);
246 ASET (font_object
, FONT_FULLNAME_INDEX
, make_unibyte_string (name
, len
));
248 ASET (font_object
, FONT_FULLNAME_INDEX
,
249 AREF (font_object
, FONT_NAME_INDEX
));
250 ASET (font_object
, FONT_FILE_INDEX
,
251 make_unibyte_string ((char *) file
, strlen ((char *) file
)));
252 ASET (font_object
, FONT_FORMAT_INDEX
, ftfont_font_format (pattern
));
253 font
= XFONT_OBJECT (font_object
);
254 font
->pixel_size
= pixel_size
;
255 font
->driver
= &xftfont_driver
;
256 font
->encoding_charset
= font
->repertory_charset
= -1;
258 xftfont_info
= (struct xftfont_info
*) font
;
259 xftfont_info
->display
= display
;
260 xftfont_info
->screen
= FRAME_X_SCREEN_NUMBER (f
);
261 xftfont_info
->xftfont
= xftfont
;
262 font
->pixel_size
= size
;
263 font
->driver
= &xftfont_driver
;
264 if (FcPatternGetInteger (xftfont
->pattern
, FC_SPACING
, 0, &spacing
)
266 spacing
= FC_PROPORTIONAL
;
267 if (! ascii_printable
[0])
270 for (i
= 0; i
< 95; i
++)
271 ascii_printable
[i
] = ' ' + i
;
274 if (spacing
!= FC_PROPORTIONAL
)
276 font
->min_width
= font
->average_width
= font
->space_width
277 = xftfont
->max_advance_width
;
278 XftTextExtents8 (display
, xftfont
, ascii_printable
+ 1, 94, &extents
);
282 XftTextExtents8 (display
, xftfont
, ascii_printable
, 1, &extents
);
283 font
->space_width
= extents
.xOff
;
284 if (font
->space_width
<= 0)
285 /* dirty workaround */
286 font
->space_width
= pixel_size
;
287 XftTextExtents8 (display
, xftfont
, ascii_printable
+ 1, 94, &extents
);
288 font
->average_width
= (font
->space_width
+ extents
.xOff
) / 95;
292 font
->ascent
= xftfont
->ascent
;
293 if (font
->ascent
< extents
.y
)
294 font
->ascent
= extents
.y
;
295 font
->descent
= xftfont
->descent
;
296 if (font
->descent
< extents
.height
- extents
.y
)
297 font
->descent
= extents
.height
- extents
.y
;
298 font
->height
= font
->ascent
+ font
->descent
;
300 ft_face
= XftLockFace (xftfont
);
301 if (XINT (AREF (entity
, FONT_SIZE_INDEX
)) == 0)
303 int upEM
= ft_face
->units_per_EM
;
305 font
->underline_position
= -ft_face
->underline_position
* size
/ upEM
;
306 font
->underline_thickness
= -ft_face
->underline_thickness
* size
/ upEM
;
310 font
->underline_position
= -1;
311 font
->underline_thickness
= 0;
314 xftfont_info
->maybe_otf
= ft_face
->face_flags
& FT_FACE_FLAG_SFNT
;
315 xftfont_info
->otf
= NULL
;
316 #endif /* HAVE_LIBOTF */
317 XftUnlockFace (xftfont
);
319 /* Unfortunately Xft doesn't provide a way to get minimum char
320 width. So, we use space_width instead. */
321 font
->min_width
= font
->space_width
;
323 font
->baseline_offset
= 0;
324 font
->relative_compose
= 0;
325 font
->default_ascent
= 0;
326 font
->vertical_centering
= 0;
332 xftfont_close (f
, font
)
336 struct xftfont_info
*xftfont_info
= (struct xftfont_info
*) font
;
339 if (xftfont_info
->otf
)
340 OTF_close (xftfont_info
->otf
);
343 XftFontClose (xftfont_info
->display
, xftfont_info
->xftfont
);
348 xftfont_prepare_face (f
, face
)
352 struct xftface_info
*xftface_info
;
355 /* This doesn't work if face->ascii_face doesn't use an Xft font. */
356 if (face
!= face
->ascii_face
)
358 face
->extra
= face
->ascii_face
->extra
;
363 xftface_info
= malloc (sizeof (struct xftface_info
));
366 xftfont_get_colors (f
, face
, face
->gc
, NULL
,
367 &xftface_info
->xft_fg
, &xftface_info
->xft_bg
);
368 face
->extra
= xftface_info
;
373 xftfont_done_face (f
, face
)
377 struct xftface_info
*xftface_info
;
380 /* This doesn't work if face->ascii_face doesn't use an Xft font. */
381 if (face
!= face
->ascii_face
386 xftface_info
= (struct xftface_info
*) face
->extra
;
395 xftfont_encode_char (font
, c
)
399 struct xftfont_info
*xftfont_info
= (struct xftfont_info
*) font
;
400 unsigned code
= XftCharIndex (xftfont_info
->display
, xftfont_info
->xftfont
,
403 return (code
? code
: FONT_INVALID_CODE
);
407 xftfont_text_extents (font
, code
, nglyphs
, metrics
)
411 struct font_metrics
*metrics
;
413 struct xftfont_info
*xftfont_info
= (struct xftfont_info
*) font
;
417 XftGlyphExtents (xftfont_info
->display
, xftfont_info
->xftfont
, code
, nglyphs
,
422 metrics
->lbearing
= - extents
.x
;
423 metrics
->rbearing
= - extents
.x
+ extents
.width
;
424 metrics
->width
= extents
.xOff
;
425 metrics
->ascent
= extents
.y
;
426 metrics
->descent
= extents
.height
- extents
.y
;
432 xftfont_get_xft_draw (f
)
435 XftDraw
*xft_draw
= font_get_frame_data (f
, &xftfont_driver
);;
440 xft_draw
= XftDrawCreate (FRAME_X_DISPLAY (f
),
443 FRAME_X_COLORMAP (f
));
447 font_put_frame_data (f
, &xftfont_driver
, xft_draw
);
453 xftfont_draw (s
, from
, to
, x
, y
, with_background
)
454 struct glyph_string
*s
;
455 int from
, to
, x
, y
, with_background
;
458 struct face
*face
= s
->face
;
459 struct xftfont_info
*xftfont_info
= (struct xftfont_info
*) s
->font
;
460 struct xftface_info
*xftface_info
= NULL
;
461 XftDraw
*xft_draw
= xftfont_get_xft_draw (f
);
468 if (s
->font
== face
->font
)
469 xftface_info
= (struct xftface_info
*) face
->extra
;
470 xftfont_get_colors (f
, face
, s
->gc
, xftface_info
,
471 &fg
, with_background
? &bg
: NULL
);
473 if (s
->num_clips
> 0)
474 XftDrawSetClipRectangles (xft_draw
, 0, 0, s
->clip
, s
->num_clips
);
476 XftDrawSetClip (xft_draw
, NULL
);
479 XftDrawRect (xft_draw
, &bg
,
480 x
, y
- face
->font
->ascent
, s
->width
, face
->font
->height
);
481 code
= alloca (sizeof (FT_UInt
) * len
);
482 for (i
= 0; i
< len
; i
++)
483 code
[i
] = ((XCHAR2B_BYTE1 (s
->char2b
+ from
+ i
) << 8)
484 | XCHAR2B_BYTE2 (s
->char2b
+ from
+ i
));
487 for (i
= 0; i
< len
; i
++)
488 XftDrawGlyphs (xft_draw
, &fg
, xftfont_info
->xftfont
,
489 x
+ i
, y
, code
+ i
, 1);
491 XftDrawGlyphs (xft_draw
, &fg
, xftfont_info
->xftfont
,
499 xftfont_anchor_point (font
, code
, index
, x
, y
)
505 struct xftfont_info
*xftfont_info
= (struct xftfont_info
*) font
;
506 FT_Face ft_face
= XftLockFace (xftfont_info
->xftfont
);
509 if (FT_Load_Glyph (ft_face
, code
, FT_LOAD_DEFAULT
) != 0)
511 else if (ft_face
->glyph
->format
!= FT_GLYPH_FORMAT_OUTLINE
)
513 else if (index
>= ft_face
->glyph
->outline
.n_points
)
517 *x
= ft_face
->glyph
->outline
.points
[index
].x
;
518 *y
= ft_face
->glyph
->outline
.points
[index
].y
;
520 XftUnlockFace (xftfont_info
->xftfont
);
525 xftfont_end_for_frame (f
)
528 XftDraw
*xft_draw
= font_get_frame_data (f
, &xftfont_driver
);
533 XftDrawDestroy (xft_draw
);
535 font_put_frame_data (f
, &xftfont_driver
, NULL
);
543 xftfont_shape (lgstring
)
544 Lisp_Object lgstring
;
547 struct xftfont_info
*xftfont_info
;
551 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring
), font
);
552 xftfont_info
= (struct xftfont_info
*) font
;
553 if (! xftfont_info
->maybe_otf
)
555 ft_face
= XftLockFace (xftfont_info
->xftfont
);
556 if (! xftfont_info
->otf
)
558 OTF
*otf
= OTF_open_ft_face (ft_face
);
560 if (! otf
|| OTF_get_table (otf
, "head") < 0)
564 xftfont_info
->maybe_otf
= 0;
565 XftUnlockFace (xftfont_info
->xftfont
);
568 xftfont_info
->otf
= otf
;
571 result
= ftfont_shape_by_flt (lgstring
, font
, ft_face
, xftfont_info
->otf
);
572 XftUnlockFace (xftfont_info
->xftfont
);
575 #endif /* HAVE_M17N_FLT */
576 #endif /* HAVE_LIBOTF */
581 DEFSYM (Qxft
, "xft");
583 xftfont_driver
= ftfont_driver
;
584 xftfont_driver
.type
= Qxft
;
585 xftfont_driver
.get_cache
= xfont_driver
.get_cache
;
586 xftfont_driver
.list
= xftfont_list
;
587 xftfont_driver
.match
= xftfont_match
;
588 xftfont_driver
.open
= xftfont_open
;
589 xftfont_driver
.close
= xftfont_close
;
590 xftfont_driver
.prepare_face
= xftfont_prepare_face
;
591 xftfont_driver
.done_face
= xftfont_done_face
;
592 xftfont_driver
.encode_char
= xftfont_encode_char
;
593 xftfont_driver
.text_extents
= xftfont_text_extents
;
594 xftfont_driver
.draw
= xftfont_draw
;
595 xftfont_driver
.anchor_point
= xftfont_anchor_point
;
596 xftfont_driver
.end_for_frame
= xftfont_end_for_frame
;
599 xftfont_driver
.shape
= xftfont_shape
;
600 #endif /* HAVE_M17N_FLT */
601 #endif /* HAVE_LIBOTF */
603 register_font_driver (&xftfont_driver
, NULL
);
606 /* arch-tag: 64ec61bf-7c8e-4fe6-b953-c6a85d5e1605
607 (do not change this comment) */