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