Commit | Line | Data |
---|---|---|
c2f5bfd6 | 1 | /* xftfont.c -- XFT font driver. |
76b6f707 GM |
2 | Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
3 | Copyright (C) 2006, 2007, 2008, 2009 | |
c2f5bfd6 KH |
4 | National Institute of Advanced Industrial Science and Technology (AIST) |
5 | Registration Number H13PRO009 | |
6 | ||
7 | This file is part of GNU Emacs. | |
8 | ||
9ec0b715 | 9 | GNU Emacs is free software: you can redistribute it and/or modify |
c2f5bfd6 | 10 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
11 | the Free Software Foundation, either version 3 of the License, or |
12 | (at your option) any later version. | |
c2f5bfd6 KH |
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 | |
9ec0b715 | 20 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
c2f5bfd6 KH |
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" | |
4613015e | 36 | #include "ftfont.h" |
c2f5bfd6 KH |
37 | |
38 | /* Xft font driver. */ | |
39 | ||
40 | static Lisp_Object Qxft; | |
81094fab | 41 | static Lisp_Object QChinting , QCautohint, QChintstyle, QCrgba, QCembolden; |
c2f5bfd6 KH |
42 | |
43 | /* The actual structure for Xft font that can be casted to struct | |
44 | font. */ | |
45 | ||
46 | struct xftfont_info | |
47 | { | |
48 | struct font font; | |
b0c25608 | 49 | /* The following four members must be here in this order to be |
0fce2b40 | 50 | compatible with struct ftfont_info (in ftfont.c). */ |
4613015e KH |
51 | #ifdef HAVE_LIBOTF |
52 | int maybe_otf; /* Flag to tell if this may be OTF or not. */ | |
53 | OTF *otf; | |
794eba0f | 54 | #endif /* HAVE_LIBOTF */ |
0fce2b40 | 55 | FT_Size ft_size; |
b0c25608 | 56 | int index; |
0fce2b40 KH |
57 | Display *display; |
58 | int screen; | |
59 | XftFont *xftfont; | |
c2f5bfd6 KH |
60 | }; |
61 | ||
62 | /* Structure pointed by (struct face *)->extra */ | |
10aca0f7 | 63 | |
c2f5bfd6 KH |
64 | struct xftface_info |
65 | { | |
10aca0f7 KH |
66 | XftColor xft_fg; /* color for face->foreground */ |
67 | XftColor xft_bg; /* color for face->background */ | |
c2f5bfd6 KH |
68 | }; |
69 | ||
70 | static void xftfont_get_colors P_ ((FRAME_PTR, struct face *, GC gc, | |
71 | struct xftface_info *, | |
72 | XftColor *fg, XftColor *bg)); | |
c2f5bfd6 KH |
73 | |
74 | ||
10aca0f7 KH |
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. */ | |
78 | ||
c2f5bfd6 KH |
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; | |
86 | { | |
87 | if (xftface_info && face->gc == gc) | |
88 | { | |
89 | *fg = xftface_info->xft_fg; | |
90 | if (bg) | |
91 | *bg = xftface_info->xft_bg; | |
92 | } | |
93 | else | |
94 | { | |
95 | XGCValues xgcv; | |
96 | int fg_done = 0, bg_done = 0; | |
97 | ||
98 | BLOCK_INPUT; | |
99 | XGetGCValues (FRAME_X_DISPLAY (f), gc, | |
100 | GCForeground | GCBackground, &xgcv); | |
101 | if (xftface_info) | |
102 | { | |
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; | |
113 | } | |
114 | ||
115 | if (fg_done + bg_done < 2) | |
116 | { | |
117 | XColor colors[2]; | |
118 | ||
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) | |
129 | { | |
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; | |
134 | } | |
135 | } | |
136 | UNBLOCK_INPUT; | |
137 | } | |
138 | } | |
139 | ||
c2f5bfd6 KH |
140 | |
141 | static Lisp_Object xftfont_list P_ ((Lisp_Object, Lisp_Object)); | |
10aca0f7 | 142 | static Lisp_Object xftfont_match P_ ((Lisp_Object, Lisp_Object)); |
3528e709 | 143 | static Lisp_Object xftfont_open P_ ((FRAME_PTR, Lisp_Object, int)); |
c2f5bfd6 KH |
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 *)); | |
6570a1c4 | 147 | static int xftfont_has_char P_ ((Lisp_Object, int)); |
c2f5bfd6 KH |
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)); | |
a92ee6bf | 152 | static int xftfont_end_for_frame P_ ((FRAME_PTR f)); |
c2f5bfd6 KH |
153 | |
154 | struct font_driver xftfont_driver; | |
155 | ||
156 | static Lisp_Object | |
157 | xftfont_list (frame, spec) | |
158 | Lisp_Object frame; | |
159 | Lisp_Object spec; | |
160 | { | |
3528e709 | 161 | Lisp_Object list = ftfont_driver.list (frame, spec), tail; |
c2f5bfd6 | 162 | |
3528e709 KH |
163 | for (tail = list; CONSP (tail); tail = XCDR (tail)) |
164 | ASET (XCAR (tail), FONT_TYPE_INDEX, Qxft); | |
165 | return list; | |
c2f5bfd6 KH |
166 | } |
167 | ||
10aca0f7 KH |
168 | static Lisp_Object |
169 | xftfont_match (frame, spec) | |
170 | Lisp_Object frame; | |
171 | Lisp_Object spec; | |
172 | { | |
173 | Lisp_Object entity = ftfont_driver.match (frame, spec); | |
174 | ||
3528e709 | 175 | if (! NILP (entity)) |
10aca0f7 KH |
176 | ASET (entity, FONT_TYPE_INDEX, Qxft); |
177 | return entity; | |
178 | } | |
179 | ||
1cf6763f | 180 | extern Lisp_Object ftfont_font_format P_ ((FcPattern *, Lisp_Object)); |
0fce2b40 | 181 | extern FcCharSet *ftfont_get_fc_charset P_ ((Lisp_Object)); |
81094fab | 182 | extern Lisp_Object QCantialias; |
98d12656 | 183 | |
c2f5bfd6 KH |
184 | static FcChar8 ascii_printable[95]; |
185 | ||
3528e709 | 186 | static Lisp_Object |
c2f5bfd6 KH |
187 | xftfont_open (f, entity, pixel_size) |
188 | FRAME_PTR f; | |
189 | Lisp_Object entity; | |
190 | int pixel_size; | |
191 | { | |
81094fab | 192 | FcResult result; |
c2f5bfd6 | 193 | Display *display = FRAME_X_DISPLAY (f); |
0fce2b40 | 194 | Lisp_Object val, filename, index, tail, font_object; |
81094fab | 195 | FcPattern *pat = NULL, *match; |
2d93c6bd | 196 | struct xftfont_info *xftfont_info = NULL; |
c2f5bfd6 KH |
197 | struct font *font; |
198 | double size = 0; | |
2d93c6bd | 199 | XftFont *xftfont = NULL; |
c2f5bfd6 | 200 | int spacing; |
3528e709 KH |
201 | char name[256]; |
202 | int len, i; | |
91c5bb27 | 203 | XGlyphInfo extents; |
365131ac | 204 | FT_Face ft_face; |
c2f5bfd6 | 205 | |
3528e709 | 206 | val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX)); |
686ee703 | 207 | if (! CONSP (val)) |
3528e709 | 208 | return Qnil; |
686ee703 KH |
209 | val = XCDR (val); |
210 | filename = XCAR (val); | |
0fce2b40 | 211 | index = XCDR (val); |
c2f5bfd6 KH |
212 | size = XINT (AREF (entity, FONT_SIZE_INDEX)); |
213 | if (size == 0) | |
214 | size = pixel_size; | |
215 | pat = FcPatternCreate (); | |
81094fab KH |
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)); | |
c2f5bfd6 | 221 | FcPatternAddDouble (pat, FC_PIXEL_SIZE, pixel_size); |
3cc2aca0 KH |
222 | val = AREF (entity, FONT_FAMILY_INDEX); |
223 | if (! NILP (val)) | |
224 | FcPatternAddString (pat, FC_FAMILY, (FcChar8 *) SDATA (SYMBOL_NAME (val))); | |
dc2226d0 KH |
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)) | |
233 | { | |
234 | double dbl = XINT (val); | |
235 | ||
236 | FcPatternAddDouble (pat, FC_DPI, dbl); | |
237 | } | |
238 | val = AREF (entity, FONT_AVGWIDTH_INDEX); | |
239 | if (INTEGERP (val) && XINT (val) == 0) | |
240 | FcPatternAddBool (pat, FC_SCALABLE, FcTrue); | |
0fce2b40 KH |
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)); | |
dc2226d0 | 244 | |
81094fab KH |
245 | for (tail = AREF (entity, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail)) |
246 | { | |
247 | Lisp_Object key, val; | |
248 | ||
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)) | |
257 | { | |
258 | if (INTEGERP (val)) | |
259 | FcPatternAddInteger (pat, FC_RGBA, XINT (val)); | |
260 | } | |
261 | else if (EQ (key, QCrgba)) | |
262 | { | |
263 | if (INTEGERP (val)) | |
264 | FcPatternAddInteger (pat, FC_RGBA, XINT (val)); | |
265 | } | |
808dd567 | 266 | #ifdef FC_EMBOLDEN |
81094fab KH |
267 | else if (EQ (key, QCembolden)) |
268 | FcPatternAddBool (pat, FC_EMBOLDEN, NILP (val) ? FcFalse : FcTrue); | |
808dd567 | 269 | #endif |
81094fab | 270 | } |
dcce3c58 | 271 | |
0fce2b40 KH |
272 | FcPatternAddString (pat, FC_FILE, (FcChar8 *) SDATA (filename)); |
273 | FcPatternAddInteger (pat, FC_INDEX, XINT (index)); | |
274 | ||
275 | ||
dcce3c58 | 276 | BLOCK_INPUT; |
81094fab KH |
277 | match = XftFontMatch (display, FRAME_X_SCREEN_NUMBER (f), pat, &result); |
278 | FcPatternDestroy (pat); | |
279 | xftfont = XftFontOpenPattern (display, match); | |
0fce2b40 | 280 | ft_face = XftLockFace (xftfont); |
3528e709 | 281 | UNBLOCK_INPUT; |
81094fab | 282 | |
3528e709 | 283 | if (! xftfont) |
5c629bf6 CY |
284 | { |
285 | XftPatternDestroy (match); | |
286 | return Qnil; | |
287 | } | |
c2f5bfd6 KH |
288 | /* We should not destroy PAT here because it is kept in XFTFONT and |
289 | destroyed automatically when XFTFONT is closed. */ | |
0fce2b40 | 290 | font_object = font_make_object (VECSIZE (struct xftfont_info), entity, size); |
3528e709 | 291 | ASET (font_object, FONT_TYPE_INDEX, Qxft); |
3528e709 KH |
292 | len = font_unparse_xlfd (entity, size, name, 256); |
293 | if (len > 0) | |
2781b4e0 | 294 | ASET (font_object, FONT_NAME_INDEX, make_string (name, len)); |
3528e709 KH |
295 | len = font_unparse_fcname (entity, size, name, 256); |
296 | if (len > 0) | |
2781b4e0 | 297 | ASET (font_object, FONT_FULLNAME_INDEX, make_string (name, len)); |
3528e709 KH |
298 | else |
299 | ASET (font_object, FONT_FULLNAME_INDEX, | |
300 | AREF (font_object, FONT_NAME_INDEX)); | |
686ee703 KH |
301 | ASET (font_object, FONT_FILE_INDEX, filename); |
302 | ASET (font_object, FONT_FORMAT_INDEX, | |
1cf6763f | 303 | ftfont_font_format (xftfont->pattern, filename)); |
3528e709 KH |
304 | font = XFONT_OBJECT (font_object); |
305 | font->pixel_size = pixel_size; | |
306 | font->driver = &xftfont_driver; | |
307 | font->encoding_charset = font->repertory_charset = -1; | |
308 | ||
309 | xftfont_info = (struct xftfont_info *) font; | |
c2f5bfd6 KH |
310 | xftfont_info->display = display; |
311 | xftfont_info->screen = FRAME_X_SCREEN_NUMBER (f); | |
312 | xftfont_info->xftfont = xftfont; | |
c2f5bfd6 KH |
313 | font->pixel_size = size; |
314 | font->driver = &xftfont_driver; | |
686ee703 KH |
315 | if (INTEGERP (AREF (entity, FONT_SPACING_INDEX))) |
316 | spacing = XINT (AREF (entity, FONT_SPACING_INDEX)); | |
317 | else | |
c2f5bfd6 | 318 | spacing = FC_PROPORTIONAL; |
91c5bb27 KH |
319 | if (! ascii_printable[0]) |
320 | { | |
321 | int i; | |
322 | for (i = 0; i < 95; i++) | |
323 | ascii_printable[i] = ' ' + i; | |
324 | } | |
3528e709 | 325 | BLOCK_INPUT; |
c2f5bfd6 | 326 | if (spacing != FC_PROPORTIONAL) |
91c5bb27 | 327 | { |
3528e709 | 328 | font->min_width = font->average_width = font->space_width |
91c5bb27 KH |
329 | = xftfont->max_advance_width; |
330 | XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents); | |
331 | } | |
c2f5bfd6 KH |
332 | else |
333 | { | |
c2f5bfd6 | 334 | XftTextExtents8 (display, xftfont, ascii_printable, 1, &extents); |
3528e709 KH |
335 | font->space_width = extents.xOff; |
336 | if (font->space_width <= 0) | |
c2f5bfd6 | 337 | /* dirty workaround */ |
3528e709 | 338 | font->space_width = pixel_size; |
c2f5bfd6 | 339 | XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents); |
3528e709 | 340 | font->average_width = (font->space_width + extents.xOff) / 95; |
c2f5bfd6 | 341 | } |
dcce3c58 | 342 | UNBLOCK_INPUT; |
c2f5bfd6 | 343 | |
91c5bb27 | 344 | font->ascent = xftfont->ascent; |
91c5bb27 | 345 | font->descent = xftfont->descent; |
81094fab KH |
346 | if (pixel_size >= 5) |
347 | { | |
348 | /* The above condition is a dirty workaround because | |
349 | XftTextExtents8 behaves strangely for some fonts | |
350 | (e.g. "Dejavu Sans Mono") when pixel_size is less than 5. */ | |
351 | if (font->ascent < extents.y) | |
352 | font->ascent = extents.y; | |
353 | if (font->descent < extents.height - extents.y) | |
354 | font->descent = extents.height - extents.y; | |
355 | } | |
3528e709 | 356 | font->height = font->ascent + font->descent; |
91c5bb27 | 357 | |
3528e709 | 358 | if (XINT (AREF (entity, FONT_SIZE_INDEX)) == 0) |
c2f5bfd6 | 359 | { |
3528e709 KH |
360 | int upEM = ft_face->units_per_EM; |
361 | ||
362 | font->underline_position = -ft_face->underline_position * size / upEM; | |
363 | font->underline_thickness = -ft_face->underline_thickness * size / upEM; | |
951b8112 KH |
364 | if (font->underline_thickness > 2) |
365 | font->underline_position -= font->underline_thickness / 2; | |
c2f5bfd6 KH |
366 | } |
367 | else | |
368 | { | |
3528e709 KH |
369 | font->underline_position = -1; |
370 | font->underline_thickness = 0; | |
c2f5bfd6 | 371 | } |
3528e709 KH |
372 | #ifdef HAVE_LIBOTF |
373 | xftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT; | |
374 | xftfont_info->otf = NULL; | |
375 | #endif /* HAVE_LIBOTF */ | |
0fce2b40 | 376 | xftfont_info->ft_size = ft_face->size; |
c2f5bfd6 | 377 | |
3528e709 KH |
378 | /* Unfortunately Xft doesn't provide a way to get minimum char |
379 | width. So, we use space_width instead. */ | |
380 | font->min_width = font->space_width; | |
2d93c6bd | 381 | |
3528e709 KH |
382 | font->baseline_offset = 0; |
383 | font->relative_compose = 0; | |
384 | font->default_ascent = 0; | |
385 | font->vertical_centering = 0; | |
0fce2b40 KH |
386 | #ifdef FT_BDF_H |
387 | if (! (ft_face->face_flags & FT_FACE_FLAG_SFNT)) | |
388 | { | |
389 | BDF_PropertyRec rec; | |
390 | ||
391 | if (FT_Get_BDF_Property (ft_face, "_MULE_BASELINE_OFFSET", &rec) == 0 | |
392 | && rec.type == BDF_PROPERTY_TYPE_INTEGER) | |
393 | font->baseline_offset = rec.u.integer; | |
394 | if (FT_Get_BDF_Property (ft_face, "_MULE_RELATIVE_COMPOSE", &rec) == 0 | |
395 | && rec.type == BDF_PROPERTY_TYPE_INTEGER) | |
396 | font->relative_compose = rec.u.integer; | |
397 | if (FT_Get_BDF_Property (ft_face, "_MULE_DEFAULT_ASCENT", &rec) == 0 | |
398 | && rec.type == BDF_PROPERTY_TYPE_INTEGER) | |
399 | font->default_ascent = rec.u.integer; | |
400 | } | |
401 | #endif | |
3528e709 KH |
402 | |
403 | return font_object; | |
c2f5bfd6 KH |
404 | } |
405 | ||
406 | static void | |
407 | xftfont_close (f, font) | |
408 | FRAME_PTR f; | |
409 | struct font *font; | |
410 | { | |
411 | struct xftfont_info *xftfont_info = (struct xftfont_info *) font; | |
412 | ||
4613015e KH |
413 | #ifdef HAVE_LIBOTF |
414 | if (xftfont_info->otf) | |
415 | OTF_close (xftfont_info->otf); | |
416 | #endif | |
3528e709 | 417 | BLOCK_INPUT; |
0fce2b40 | 418 | XftUnlockFace (xftfont_info->xftfont); |
c2f5bfd6 | 419 | XftFontClose (xftfont_info->display, xftfont_info->xftfont); |
3528e709 | 420 | UNBLOCK_INPUT; |
c2f5bfd6 KH |
421 | } |
422 | ||
c2f5bfd6 KH |
423 | static int |
424 | xftfont_prepare_face (f, face) | |
425 | FRAME_PTR f; | |
426 | struct face *face; | |
427 | { | |
e2d0c925 | 428 | struct xftface_info *xftface_info; |
c2f5bfd6 | 429 | |
a703d27d KH |
430 | #if 0 |
431 | /* This doesn't work if face->ascii_face doesn't use an Xft font. */ | |
e2d0c925 KH |
432 | if (face != face->ascii_face) |
433 | { | |
434 | face->extra = face->ascii_face->extra; | |
435 | return 0; | |
436 | } | |
a703d27d | 437 | #endif |
e2d0c925 KH |
438 | |
439 | xftface_info = malloc (sizeof (struct xftface_info)); | |
c2f5bfd6 KH |
440 | if (! xftface_info) |
441 | return -1; | |
c2f5bfd6 KH |
442 | xftfont_get_colors (f, face, face->gc, NULL, |
443 | &xftface_info->xft_fg, &xftface_info->xft_bg); | |
c2f5bfd6 KH |
444 | face->extra = xftface_info; |
445 | return 0; | |
446 | } | |
447 | ||
448 | static void | |
449 | xftfont_done_face (f, face) | |
450 | FRAME_PTR f; | |
451 | struct face *face; | |
452 | { | |
e2d0c925 KH |
453 | struct xftface_info *xftface_info; |
454 | ||
a703d27d KH |
455 | #if 0 |
456 | /* This doesn't work if face->ascii_face doesn't use an Xft font. */ | |
e2d0c925 KH |
457 | if (face != face->ascii_face |
458 | || ! face->extra) | |
459 | return; | |
a703d27d | 460 | #endif |
c2f5bfd6 | 461 | |
e2d0c925 | 462 | xftface_info = (struct xftface_info *) face->extra; |
773039e8 JD |
463 | if (xftface_info) |
464 | { | |
773039e8 | 465 | free (xftface_info); |
a92ee6bf | 466 | face->extra = NULL; |
773039e8 | 467 | } |
c2f5bfd6 KH |
468 | } |
469 | ||
6570a1c4 KH |
470 | static int |
471 | xftfont_has_char (font, c) | |
472 | Lisp_Object font; | |
473 | int c; | |
474 | { | |
475 | struct xftfont_info *xftfont_info; | |
476 | ||
477 | if (FONT_ENTITY_P (font)) | |
478 | return ftfont_driver.has_char (font, c); | |
479 | ||
480 | xftfont_info = (struct xftfont_info *) XFONT_OBJECT (font); | |
481 | return (XftCharExists (xftfont_info->display, xftfont_info->xftfont, | |
482 | (FcChar32) c) == FcTrue); | |
483 | } | |
484 | ||
c2f5bfd6 KH |
485 | static unsigned |
486 | xftfont_encode_char (font, c) | |
487 | struct font *font; | |
488 | int c; | |
489 | { | |
490 | struct xftfont_info *xftfont_info = (struct xftfont_info *) font; | |
491 | unsigned code = XftCharIndex (xftfont_info->display, xftfont_info->xftfont, | |
492 | (FcChar32) c); | |
493 | ||
b5b2545a | 494 | return (code ? code : FONT_INVALID_CODE); |
c2f5bfd6 KH |
495 | } |
496 | ||
497 | static int | |
498 | xftfont_text_extents (font, code, nglyphs, metrics) | |
499 | struct font *font; | |
500 | unsigned *code; | |
501 | int nglyphs; | |
502 | struct font_metrics *metrics; | |
503 | { | |
504 | struct xftfont_info *xftfont_info = (struct xftfont_info *) font; | |
505 | XGlyphInfo extents; | |
506 | ||
507 | BLOCK_INPUT; | |
508 | XftGlyphExtents (xftfont_info->display, xftfont_info->xftfont, code, nglyphs, | |
509 | &extents); | |
510 | UNBLOCK_INPUT; | |
511 | if (metrics) | |
512 | { | |
513 | metrics->lbearing = - extents.x; | |
514 | metrics->rbearing = - extents.x + extents.width; | |
515 | metrics->width = extents.xOff; | |
516 | metrics->ascent = extents.y; | |
4b848612 | 517 | metrics->descent = extents.height - extents.y; |
c2f5bfd6 KH |
518 | } |
519 | return extents.xOff; | |
520 | } | |
521 | ||
a92ee6bf KH |
522 | static XftDraw * |
523 | xftfont_get_xft_draw (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 | xft_draw= XftDrawCreate (FRAME_X_DISPLAY (f), | |
532 | FRAME_X_WINDOW (f), | |
533 | FRAME_X_VISUAL (f), | |
534 | FRAME_X_COLORMAP (f)); | |
535 | UNBLOCK_INPUT; | |
536 | if (! xft_draw) | |
537 | abort (); | |
538 | font_put_frame_data (f, &xftfont_driver, xft_draw); | |
539 | } | |
540 | return xft_draw; | |
541 | } | |
542 | ||
c2f5bfd6 KH |
543 | static int |
544 | xftfont_draw (s, from, to, x, y, with_background) | |
545 | struct glyph_string *s; | |
546 | int from, to, x, y, with_background; | |
547 | { | |
548 | FRAME_PTR f = s->f; | |
549 | struct face *face = s->face; | |
3528e709 | 550 | struct xftfont_info *xftfont_info = (struct xftfont_info *) s->font; |
c78c1659 | 551 | struct xftface_info *xftface_info = NULL; |
a92ee6bf | 552 | XftDraw *xft_draw = xftfont_get_xft_draw (f); |
c2f5bfd6 KH |
553 | FT_UInt *code; |
554 | XftColor fg, bg; | |
c2f5bfd6 KH |
555 | int len = to - from; |
556 | int i; | |
557 | ||
3528e709 | 558 | if (s->font == face->font) |
a92ee6bf | 559 | xftface_info = (struct xftface_info *) face->extra; |
c2f5bfd6 | 560 | xftfont_get_colors (f, face, s->gc, xftface_info, |
322f8671 | 561 | &fg, with_background ? &bg : NULL); |
c2f5bfd6 | 562 | BLOCK_INPUT; |
03d198e8 KH |
563 | if (s->num_clips > 0) |
564 | XftDrawSetClipRectangles (xft_draw, 0, 0, s->clip, s->num_clips); | |
565 | else | |
566 | XftDrawSetClip (xft_draw, NULL); | |
567 | ||
c2f5bfd6 | 568 | if (with_background) |
3528e709 KH |
569 | XftDrawRect (xft_draw, &bg, |
570 | x, y - face->font->ascent, s->width, face->font->height); | |
c2f5bfd6 KH |
571 | code = alloca (sizeof (FT_UInt) * len); |
572 | for (i = 0; i < len; i++) | |
573 | code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8) | |
574 | | XCHAR2B_BYTE2 (s->char2b + from + i)); | |
575 | ||
785543da KH |
576 | if (s->padding_p) |
577 | for (i = 0; i < len; i++) | |
578 | XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont, | |
579 | x + i, y, code + i, 1); | |
580 | else | |
581 | XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont, | |
582 | x, y, code, len); | |
c2f5bfd6 KH |
583 | UNBLOCK_INPUT; |
584 | ||
585 | return len; | |
586 | } | |
587 | ||
a92ee6bf KH |
588 | static int |
589 | xftfont_end_for_frame (f) | |
590 | FRAME_PTR f; | |
591 | { | |
592 | XftDraw *xft_draw = font_get_frame_data (f, &xftfont_driver); | |
593 | ||
594 | if (xft_draw) | |
595 | { | |
596 | BLOCK_INPUT; | |
597 | XftDrawDestroy (xft_draw); | |
598 | UNBLOCK_INPUT; | |
599 | font_put_frame_data (f, &xftfont_driver, NULL); | |
600 | } | |
601 | return 0; | |
602 | } | |
c2f5bfd6 KH |
603 | |
604 | void | |
605 | syms_of_xftfont () | |
606 | { | |
607 | DEFSYM (Qxft, "xft"); | |
81094fab | 608 | DEFSYM (QChinting, ":hinting"); |
9743ac48 | 609 | DEFSYM (QCautohint, ":autohint"); |
81094fab KH |
610 | DEFSYM (QChintstyle, ":hintstyle"); |
611 | DEFSYM (QCrgba, ":rgba"); | |
612 | DEFSYM (QCembolden, ":embolden"); | |
c2f5bfd6 KH |
613 | |
614 | xftfont_driver = ftfont_driver; | |
615 | xftfont_driver.type = Qxft; | |
616 | xftfont_driver.get_cache = xfont_driver.get_cache; | |
617 | xftfont_driver.list = xftfont_list; | |
10aca0f7 | 618 | xftfont_driver.match = xftfont_match; |
c2f5bfd6 KH |
619 | xftfont_driver.open = xftfont_open; |
620 | xftfont_driver.close = xftfont_close; | |
621 | xftfont_driver.prepare_face = xftfont_prepare_face; | |
622 | xftfont_driver.done_face = xftfont_done_face; | |
6570a1c4 | 623 | xftfont_driver.has_char = xftfont_has_char; |
c2f5bfd6 KH |
624 | xftfont_driver.encode_char = xftfont_encode_char; |
625 | xftfont_driver.text_extents = xftfont_text_extents; | |
626 | xftfont_driver.draw = xftfont_draw; | |
a92ee6bf | 627 | xftfont_driver.end_for_frame = xftfont_end_for_frame; |
c2f5bfd6 KH |
628 | |
629 | register_font_driver (&xftfont_driver, NULL); | |
630 | } | |
885b7d09 MB |
631 | |
632 | /* arch-tag: 64ec61bf-7c8e-4fe6-b953-c6a85d5e1605 | |
633 | (do not change this comment) */ |