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> | |
d7306fe6 | 24 | #include <setjmp.h> |
c2f5bfd6 KH |
25 | #include <X11/Xlib.h> |
26 | #include <X11/Xft/Xft.h> | |
27 | ||
28 | #include "lisp.h" | |
29 | #include "dispextern.h" | |
30 | #include "xterm.h" | |
31 | #include "frame.h" | |
32 | #include "blockinput.h" | |
33 | #include "character.h" | |
34 | #include "charset.h" | |
35 | #include "fontset.h" | |
36 | #include "font.h" | |
4613015e | 37 | #include "ftfont.h" |
c2f5bfd6 KH |
38 | |
39 | /* Xft font driver. */ | |
40 | ||
41 | static Lisp_Object Qxft; | |
81094fab | 42 | static Lisp_Object QChinting , QCautohint, QChintstyle, QCrgba, QCembolden; |
c2f5bfd6 KH |
43 | |
44 | /* The actual structure for Xft font that can be casted to struct | |
45 | font. */ | |
46 | ||
47 | struct xftfont_info | |
48 | { | |
49 | struct font font; | |
b0c25608 | 50 | /* The following four members must be here in this order to be |
0fce2b40 | 51 | compatible with struct ftfont_info (in ftfont.c). */ |
4613015e KH |
52 | #ifdef HAVE_LIBOTF |
53 | int maybe_otf; /* Flag to tell if this may be OTF or not. */ | |
54 | OTF *otf; | |
794eba0f | 55 | #endif /* HAVE_LIBOTF */ |
0fce2b40 | 56 | FT_Size ft_size; |
b0c25608 | 57 | int index; |
0fce2b40 KH |
58 | Display *display; |
59 | int screen; | |
60 | XftFont *xftfont; | |
c2f5bfd6 KH |
61 | }; |
62 | ||
63 | /* Structure pointed by (struct face *)->extra */ | |
10aca0f7 | 64 | |
c2f5bfd6 KH |
65 | struct xftface_info |
66 | { | |
10aca0f7 KH |
67 | XftColor xft_fg; /* color for face->foreground */ |
68 | XftColor xft_bg; /* color for face->background */ | |
c2f5bfd6 KH |
69 | }; |
70 | ||
71 | static void xftfont_get_colors P_ ((FRAME_PTR, struct face *, GC gc, | |
72 | struct xftface_info *, | |
73 | XftColor *fg, XftColor *bg)); | |
c2f5bfd6 KH |
74 | |
75 | ||
10aca0f7 KH |
76 | /* Setup foreground and background colors of GC into FG and BG. If |
77 | XFTFACE_INFO is not NULL, reuse the colors in it if possible. BG | |
78 | may be NULL. */ | |
79 | ||
c2f5bfd6 KH |
80 | static void |
81 | xftfont_get_colors (f, face, gc, xftface_info, fg, bg) | |
82 | FRAME_PTR f; | |
83 | struct face *face; | |
84 | GC gc; | |
85 | struct xftface_info *xftface_info; | |
86 | XftColor *fg, *bg; | |
87 | { | |
88 | if (xftface_info && face->gc == gc) | |
89 | { | |
90 | *fg = xftface_info->xft_fg; | |
91 | if (bg) | |
92 | *bg = xftface_info->xft_bg; | |
93 | } | |
94 | else | |
95 | { | |
96 | XGCValues xgcv; | |
97 | int fg_done = 0, bg_done = 0; | |
98 | ||
99 | BLOCK_INPUT; | |
100 | XGetGCValues (FRAME_X_DISPLAY (f), gc, | |
101 | GCForeground | GCBackground, &xgcv); | |
102 | if (xftface_info) | |
103 | { | |
104 | if (xgcv.foreground == face->foreground) | |
105 | *fg = xftface_info->xft_fg, fg_done = 1; | |
106 | else if (xgcv.foreground == face->background) | |
107 | *fg = xftface_info->xft_bg, fg_done = 1; | |
108 | if (! bg) | |
109 | bg_done = 1; | |
110 | else if (xgcv.background == face->background) | |
111 | *bg = xftface_info->xft_bg, bg_done = 1; | |
112 | else if (xgcv.background == face->foreground) | |
113 | *bg = xftface_info->xft_fg, bg_done = 1; | |
114 | } | |
115 | ||
116 | if (fg_done + bg_done < 2) | |
117 | { | |
118 | XColor colors[2]; | |
119 | ||
120 | colors[0].pixel = fg->pixel = xgcv.foreground; | |
121 | if (bg) | |
122 | colors[1].pixel = bg->pixel = xgcv.background; | |
123 | XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, | |
124 | bg ? 2 : 1); | |
125 | fg->color.alpha = 0xFFFF; | |
126 | fg->color.red = colors[0].red; | |
127 | fg->color.green = colors[0].green; | |
128 | fg->color.blue = colors[0].blue; | |
129 | if (bg) | |
130 | { | |
131 | bg->color.alpha = 0xFFFF; | |
132 | bg->color.red = colors[1].red; | |
133 | bg->color.green = colors[1].green; | |
134 | bg->color.blue = colors[1].blue; | |
135 | } | |
136 | } | |
137 | UNBLOCK_INPUT; | |
138 | } | |
139 | } | |
140 | ||
c2f5bfd6 KH |
141 | |
142 | static Lisp_Object xftfont_list P_ ((Lisp_Object, Lisp_Object)); | |
10aca0f7 | 143 | static Lisp_Object xftfont_match P_ ((Lisp_Object, Lisp_Object)); |
3528e709 | 144 | static Lisp_Object xftfont_open P_ ((FRAME_PTR, Lisp_Object, int)); |
c2f5bfd6 KH |
145 | static void xftfont_close P_ ((FRAME_PTR, struct font *)); |
146 | static int xftfont_prepare_face P_ ((FRAME_PTR, struct face *)); | |
147 | static void xftfont_done_face P_ ((FRAME_PTR, struct face *)); | |
6570a1c4 | 148 | static int xftfont_has_char P_ ((Lisp_Object, int)); |
c2f5bfd6 KH |
149 | static unsigned xftfont_encode_char P_ ((struct font *, int)); |
150 | static int xftfont_text_extents P_ ((struct font *, unsigned *, int, | |
151 | struct font_metrics *)); | |
152 | static int xftfont_draw P_ ((struct glyph_string *, int, int, int, int, int)); | |
a92ee6bf | 153 | static int xftfont_end_for_frame P_ ((FRAME_PTR f)); |
c2f5bfd6 KH |
154 | |
155 | struct font_driver xftfont_driver; | |
156 | ||
157 | static Lisp_Object | |
158 | xftfont_list (frame, spec) | |
159 | Lisp_Object frame; | |
160 | Lisp_Object spec; | |
161 | { | |
3528e709 | 162 | Lisp_Object list = ftfont_driver.list (frame, spec), tail; |
8510724d | 163 | |
3528e709 KH |
164 | for (tail = list; CONSP (tail); tail = XCDR (tail)) |
165 | ASET (XCAR (tail), FONT_TYPE_INDEX, Qxft); | |
166 | return list; | |
c2f5bfd6 KH |
167 | } |
168 | ||
10aca0f7 KH |
169 | static Lisp_Object |
170 | xftfont_match (frame, spec) | |
171 | Lisp_Object frame; | |
172 | Lisp_Object spec; | |
173 | { | |
174 | Lisp_Object entity = ftfont_driver.match (frame, spec); | |
175 | ||
3528e709 | 176 | if (! NILP (entity)) |
10aca0f7 KH |
177 | ASET (entity, FONT_TYPE_INDEX, Qxft); |
178 | return entity; | |
179 | } | |
180 | ||
1cf6763f | 181 | extern Lisp_Object ftfont_font_format P_ ((FcPattern *, Lisp_Object)); |
0fce2b40 | 182 | extern FcCharSet *ftfont_get_fc_charset P_ ((Lisp_Object)); |
81094fab | 183 | extern Lisp_Object QCantialias; |
98d12656 | 184 | |
c2f5bfd6 KH |
185 | static FcChar8 ascii_printable[95]; |
186 | ||
3528e709 | 187 | static Lisp_Object |
c2f5bfd6 KH |
188 | xftfont_open (f, entity, pixel_size) |
189 | FRAME_PTR f; | |
190 | Lisp_Object entity; | |
191 | int pixel_size; | |
192 | { | |
81094fab | 193 | FcResult result; |
c2f5bfd6 | 194 | Display *display = FRAME_X_DISPLAY (f); |
0fce2b40 | 195 | Lisp_Object val, filename, index, tail, font_object; |
81094fab | 196 | FcPattern *pat = NULL, *match; |
2d93c6bd | 197 | struct xftfont_info *xftfont_info = NULL; |
c2f5bfd6 KH |
198 | struct font *font; |
199 | double size = 0; | |
2d93c6bd | 200 | XftFont *xftfont = NULL; |
c2f5bfd6 | 201 | int spacing; |
3528e709 KH |
202 | char name[256]; |
203 | int len, i; | |
91c5bb27 | 204 | XGlyphInfo extents; |
365131ac | 205 | FT_Face ft_face; |
c2f5bfd6 | 206 | |
3528e709 | 207 | val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX)); |
686ee703 | 208 | if (! CONSP (val)) |
3528e709 | 209 | return Qnil; |
686ee703 KH |
210 | val = XCDR (val); |
211 | filename = XCAR (val); | |
0fce2b40 | 212 | index = XCDR (val); |
c2f5bfd6 KH |
213 | size = XINT (AREF (entity, FONT_SIZE_INDEX)); |
214 | if (size == 0) | |
215 | size = pixel_size; | |
216 | pat = FcPatternCreate (); | |
81094fab KH |
217 | FcPatternAddInteger (pat, FC_WEIGHT, FONT_WEIGHT_NUMERIC (entity)); |
218 | i = FONT_SLANT_NUMERIC (entity) - 100; | |
219 | if (i < 0) i = 0; | |
220 | FcPatternAddInteger (pat, FC_SLANT, i); | |
221 | FcPatternAddInteger (pat, FC_WIDTH, FONT_WIDTH_NUMERIC (entity)); | |
c2f5bfd6 | 222 | FcPatternAddDouble (pat, FC_PIXEL_SIZE, pixel_size); |
3cc2aca0 KH |
223 | val = AREF (entity, FONT_FAMILY_INDEX); |
224 | if (! NILP (val)) | |
225 | FcPatternAddString (pat, FC_FAMILY, (FcChar8 *) SDATA (SYMBOL_NAME (val))); | |
dc2226d0 KH |
226 | val = AREF (entity, FONT_FOUNDRY_INDEX); |
227 | if (! NILP (val)) | |
228 | FcPatternAddString (pat, FC_FOUNDRY, (FcChar8 *) SDATA (SYMBOL_NAME (val))); | |
229 | val = AREF (entity, FONT_SPACING_INDEX); | |
230 | if (! NILP (val)) | |
231 | FcPatternAddInteger (pat, FC_SPACING, XINT (val)); | |
232 | val = AREF (entity, FONT_DPI_INDEX); | |
233 | if (! NILP (val)) | |
234 | { | |
235 | double dbl = XINT (val); | |
236 | ||
237 | FcPatternAddDouble (pat, FC_DPI, dbl); | |
238 | } | |
239 | val = AREF (entity, FONT_AVGWIDTH_INDEX); | |
240 | if (INTEGERP (val) && XINT (val) == 0) | |
241 | FcPatternAddBool (pat, FC_SCALABLE, FcTrue); | |
0fce2b40 KH |
242 | /* This is necessary to identify the exact font (e.g. 10x20.pcf.gz |
243 | over 10x20-ISO8859-1.pcf.gz). */ | |
244 | FcPatternAddCharSet (pat, FC_CHARSET, ftfont_get_fc_charset (entity)); | |
dc2226d0 | 245 | |
81094fab KH |
246 | for (tail = AREF (entity, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail)) |
247 | { | |
248 | Lisp_Object key, val; | |
249 | ||
250 | key = XCAR (XCAR (tail)), val = XCDR (XCAR (tail)); | |
251 | if (EQ (key, QCantialias)) | |
252 | FcPatternAddBool (pat, FC_ANTIALIAS, NILP (val) ? FcFalse : FcTrue); | |
253 | else if (EQ (key, QChinting)) | |
254 | FcPatternAddBool (pat, FC_HINTING, NILP (val) ? FcFalse : FcTrue); | |
255 | else if (EQ (key, QCautohint)) | |
256 | FcPatternAddBool (pat, FC_AUTOHINT, NILP (val) ? FcFalse : FcTrue); | |
257 | else if (EQ (key, QChintstyle)) | |
258 | { | |
259 | if (INTEGERP (val)) | |
ea12eb81 | 260 | FcPatternAddInteger (pat, FC_HINT_STYLE, XINT (val)); |
81094fab KH |
261 | } |
262 | else if (EQ (key, QCrgba)) | |
263 | { | |
264 | if (INTEGERP (val)) | |
265 | FcPatternAddInteger (pat, FC_RGBA, XINT (val)); | |
266 | } | |
808dd567 | 267 | #ifdef FC_EMBOLDEN |
81094fab KH |
268 | else if (EQ (key, QCembolden)) |
269 | FcPatternAddBool (pat, FC_EMBOLDEN, NILP (val) ? FcFalse : FcTrue); | |
808dd567 | 270 | #endif |
81094fab | 271 | } |
dcce3c58 | 272 | |
0fce2b40 KH |
273 | FcPatternAddString (pat, FC_FILE, (FcChar8 *) SDATA (filename)); |
274 | FcPatternAddInteger (pat, FC_INDEX, XINT (index)); | |
8510724d | 275 | |
0fce2b40 | 276 | |
dcce3c58 | 277 | BLOCK_INPUT; |
9cb363db YM |
278 | /* Make sure that the Xrender extension is added before the Xft one. |
279 | Otherwise, the close-display hook set by Xft is called after the | |
280 | one for Xrender, and the former tries to re-add the latter. This | |
281 | results in inconsistency of internal states and leads to X | |
282 | protocol error when one reconnects to the same X server. | |
283 | (Bug#1696) */ | |
284 | { | |
285 | int event_base, error_base; | |
286 | XRenderQueryExtension (display, &event_base, &error_base); | |
287 | } | |
81094fab KH |
288 | match = XftFontMatch (display, FRAME_X_SCREEN_NUMBER (f), pat, &result); |
289 | FcPatternDestroy (pat); | |
290 | xftfont = XftFontOpenPattern (display, match); | |
99061dfc | 291 | if (!xftfont) |
5c629bf6 | 292 | { |
99061dfc | 293 | UNBLOCK_INPUT; |
5c629bf6 CY |
294 | XftPatternDestroy (match); |
295 | return Qnil; | |
296 | } | |
99061dfc CY |
297 | ft_face = XftLockFace (xftfont); |
298 | UNBLOCK_INPUT; | |
299 | ||
c2f5bfd6 KH |
300 | /* We should not destroy PAT here because it is kept in XFTFONT and |
301 | destroyed automatically when XFTFONT is closed. */ | |
0fce2b40 | 302 | font_object = font_make_object (VECSIZE (struct xftfont_info), entity, size); |
3528e709 | 303 | ASET (font_object, FONT_TYPE_INDEX, Qxft); |
3528e709 KH |
304 | len = font_unparse_xlfd (entity, size, name, 256); |
305 | if (len > 0) | |
2781b4e0 | 306 | ASET (font_object, FONT_NAME_INDEX, make_string (name, len)); |
3528e709 KH |
307 | len = font_unparse_fcname (entity, size, name, 256); |
308 | if (len > 0) | |
2781b4e0 | 309 | ASET (font_object, FONT_FULLNAME_INDEX, make_string (name, len)); |
3528e709 KH |
310 | else |
311 | ASET (font_object, FONT_FULLNAME_INDEX, | |
312 | AREF (font_object, FONT_NAME_INDEX)); | |
686ee703 KH |
313 | ASET (font_object, FONT_FILE_INDEX, filename); |
314 | ASET (font_object, FONT_FORMAT_INDEX, | |
1cf6763f | 315 | ftfont_font_format (xftfont->pattern, filename)); |
3528e709 KH |
316 | font = XFONT_OBJECT (font_object); |
317 | font->pixel_size = pixel_size; | |
318 | font->driver = &xftfont_driver; | |
319 | font->encoding_charset = font->repertory_charset = -1; | |
320 | ||
321 | xftfont_info = (struct xftfont_info *) font; | |
c2f5bfd6 KH |
322 | xftfont_info->display = display; |
323 | xftfont_info->screen = FRAME_X_SCREEN_NUMBER (f); | |
324 | xftfont_info->xftfont = xftfont; | |
c2f5bfd6 KH |
325 | font->pixel_size = size; |
326 | font->driver = &xftfont_driver; | |
686ee703 KH |
327 | if (INTEGERP (AREF (entity, FONT_SPACING_INDEX))) |
328 | spacing = XINT (AREF (entity, FONT_SPACING_INDEX)); | |
329 | else | |
c2f5bfd6 | 330 | spacing = FC_PROPORTIONAL; |
91c5bb27 KH |
331 | if (! ascii_printable[0]) |
332 | { | |
333 | int i; | |
334 | for (i = 0; i < 95; i++) | |
335 | ascii_printable[i] = ' ' + i; | |
336 | } | |
3528e709 | 337 | BLOCK_INPUT; |
c2f5bfd6 | 338 | if (spacing != FC_PROPORTIONAL) |
91c5bb27 | 339 | { |
3528e709 | 340 | font->min_width = font->average_width = font->space_width |
91c5bb27 KH |
341 | = xftfont->max_advance_width; |
342 | XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents); | |
343 | } | |
c2f5bfd6 KH |
344 | else |
345 | { | |
c2f5bfd6 | 346 | XftTextExtents8 (display, xftfont, ascii_printable, 1, &extents); |
3528e709 KH |
347 | font->space_width = extents.xOff; |
348 | if (font->space_width <= 0) | |
c2f5bfd6 | 349 | /* dirty workaround */ |
8510724d | 350 | font->space_width = pixel_size; |
c2f5bfd6 | 351 | XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents); |
3528e709 | 352 | font->average_width = (font->space_width + extents.xOff) / 95; |
c2f5bfd6 | 353 | } |
dcce3c58 | 354 | UNBLOCK_INPUT; |
c2f5bfd6 | 355 | |
91c5bb27 | 356 | font->ascent = xftfont->ascent; |
91c5bb27 | 357 | font->descent = xftfont->descent; |
81094fab KH |
358 | if (pixel_size >= 5) |
359 | { | |
360 | /* The above condition is a dirty workaround because | |
361 | XftTextExtents8 behaves strangely for some fonts | |
362 | (e.g. "Dejavu Sans Mono") when pixel_size is less than 5. */ | |
363 | if (font->ascent < extents.y) | |
364 | font->ascent = extents.y; | |
365 | if (font->descent < extents.height - extents.y) | |
366 | font->descent = extents.height - extents.y; | |
367 | } | |
3528e709 | 368 | font->height = font->ascent + font->descent; |
91c5bb27 | 369 | |
3528e709 | 370 | if (XINT (AREF (entity, FONT_SIZE_INDEX)) == 0) |
c2f5bfd6 | 371 | { |
3528e709 KH |
372 | int upEM = ft_face->units_per_EM; |
373 | ||
374 | font->underline_position = -ft_face->underline_position * size / upEM; | |
bc981e4e | 375 | font->underline_thickness = ft_face->underline_thickness * size / upEM; |
951b8112 KH |
376 | if (font->underline_thickness > 2) |
377 | font->underline_position -= font->underline_thickness / 2; | |
c2f5bfd6 KH |
378 | } |
379 | else | |
380 | { | |
3528e709 KH |
381 | font->underline_position = -1; |
382 | font->underline_thickness = 0; | |
c2f5bfd6 | 383 | } |
3528e709 KH |
384 | #ifdef HAVE_LIBOTF |
385 | xftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT; | |
386 | xftfont_info->otf = NULL; | |
387 | #endif /* HAVE_LIBOTF */ | |
0fce2b40 | 388 | xftfont_info->ft_size = ft_face->size; |
c2f5bfd6 | 389 | |
3528e709 KH |
390 | /* Unfortunately Xft doesn't provide a way to get minimum char |
391 | width. So, we use space_width instead. */ | |
392 | font->min_width = font->space_width; | |
2d93c6bd | 393 | |
3528e709 KH |
394 | font->baseline_offset = 0; |
395 | font->relative_compose = 0; | |
396 | font->default_ascent = 0; | |
397 | font->vertical_centering = 0; | |
0fce2b40 KH |
398 | #ifdef FT_BDF_H |
399 | if (! (ft_face->face_flags & FT_FACE_FLAG_SFNT)) | |
400 | { | |
401 | BDF_PropertyRec rec; | |
402 | ||
403 | if (FT_Get_BDF_Property (ft_face, "_MULE_BASELINE_OFFSET", &rec) == 0 | |
404 | && rec.type == BDF_PROPERTY_TYPE_INTEGER) | |
405 | font->baseline_offset = rec.u.integer; | |
406 | if (FT_Get_BDF_Property (ft_face, "_MULE_RELATIVE_COMPOSE", &rec) == 0 | |
407 | && rec.type == BDF_PROPERTY_TYPE_INTEGER) | |
408 | font->relative_compose = rec.u.integer; | |
409 | if (FT_Get_BDF_Property (ft_face, "_MULE_DEFAULT_ASCENT", &rec) == 0 | |
410 | && rec.type == BDF_PROPERTY_TYPE_INTEGER) | |
411 | font->default_ascent = rec.u.integer; | |
412 | } | |
413 | #endif | |
3528e709 KH |
414 | |
415 | return font_object; | |
c2f5bfd6 KH |
416 | } |
417 | ||
418 | static void | |
419 | xftfont_close (f, font) | |
420 | FRAME_PTR f; | |
421 | struct font *font; | |
422 | { | |
423 | struct xftfont_info *xftfont_info = (struct xftfont_info *) font; | |
424 | ||
4613015e KH |
425 | #ifdef HAVE_LIBOTF |
426 | if (xftfont_info->otf) | |
427 | OTF_close (xftfont_info->otf); | |
428 | #endif | |
3528e709 | 429 | BLOCK_INPUT; |
0fce2b40 | 430 | XftUnlockFace (xftfont_info->xftfont); |
c2f5bfd6 | 431 | XftFontClose (xftfont_info->display, xftfont_info->xftfont); |
3528e709 | 432 | UNBLOCK_INPUT; |
c2f5bfd6 KH |
433 | } |
434 | ||
c2f5bfd6 KH |
435 | static int |
436 | xftfont_prepare_face (f, face) | |
437 | FRAME_PTR f; | |
438 | struct face *face; | |
439 | { | |
e2d0c925 | 440 | struct xftface_info *xftface_info; |
c2f5bfd6 | 441 | |
a703d27d KH |
442 | #if 0 |
443 | /* This doesn't work if face->ascii_face doesn't use an Xft font. */ | |
e2d0c925 KH |
444 | if (face != face->ascii_face) |
445 | { | |
446 | face->extra = face->ascii_face->extra; | |
447 | return 0; | |
448 | } | |
a703d27d | 449 | #endif |
e2d0c925 KH |
450 | |
451 | xftface_info = malloc (sizeof (struct xftface_info)); | |
c2f5bfd6 KH |
452 | if (! xftface_info) |
453 | return -1; | |
c2f5bfd6 KH |
454 | xftfont_get_colors (f, face, face->gc, NULL, |
455 | &xftface_info->xft_fg, &xftface_info->xft_bg); | |
c2f5bfd6 KH |
456 | face->extra = xftface_info; |
457 | return 0; | |
458 | } | |
459 | ||
460 | static void | |
461 | xftfont_done_face (f, face) | |
462 | FRAME_PTR f; | |
463 | struct face *face; | |
464 | { | |
e2d0c925 | 465 | struct xftface_info *xftface_info; |
8510724d | 466 | |
a703d27d KH |
467 | #if 0 |
468 | /* This doesn't work if face->ascii_face doesn't use an Xft font. */ | |
e2d0c925 KH |
469 | if (face != face->ascii_face |
470 | || ! face->extra) | |
471 | return; | |
a703d27d | 472 | #endif |
c2f5bfd6 | 473 | |
e2d0c925 | 474 | xftface_info = (struct xftface_info *) face->extra; |
773039e8 JD |
475 | if (xftface_info) |
476 | { | |
773039e8 | 477 | free (xftface_info); |
a92ee6bf | 478 | face->extra = NULL; |
773039e8 | 479 | } |
c2f5bfd6 KH |
480 | } |
481 | ||
b840b299 KH |
482 | extern Lisp_Object Qja, Qko; |
483 | ||
6570a1c4 KH |
484 | static int |
485 | xftfont_has_char (font, c) | |
486 | Lisp_Object font; | |
487 | int c; | |
488 | { | |
489 | struct xftfont_info *xftfont_info; | |
b840b299 | 490 | struct charset *cs = NULL; |
6570a1c4 | 491 | |
b840b299 KH |
492 | if (EQ (AREF (font, FONT_ADSTYLE_INDEX), Qja) |
493 | && charset_jisx0208 >= 0) | |
494 | cs = CHARSET_FROM_ID (charset_jisx0208); | |
495 | else if (EQ (AREF (font, FONT_ADSTYLE_INDEX), Qko) | |
496 | && charset_ksc5601 >= 0) | |
497 | cs = CHARSET_FROM_ID (charset_ksc5601); | |
498 | if (cs) | |
499 | return (ENCODE_CHAR (cs, c) != CHARSET_INVALID_CODE (cs)); | |
500 | ||
ea12eb81 KH |
501 | if (FONT_ENTITY_P (font)) |
502 | return ftfont_driver.has_char (font, c); | |
6570a1c4 KH |
503 | xftfont_info = (struct xftfont_info *) XFONT_OBJECT (font); |
504 | return (XftCharExists (xftfont_info->display, xftfont_info->xftfont, | |
505 | (FcChar32) c) == FcTrue); | |
506 | } | |
507 | ||
c2f5bfd6 KH |
508 | static unsigned |
509 | xftfont_encode_char (font, c) | |
510 | struct font *font; | |
511 | int c; | |
512 | { | |
513 | struct xftfont_info *xftfont_info = (struct xftfont_info *) font; | |
514 | unsigned code = XftCharIndex (xftfont_info->display, xftfont_info->xftfont, | |
515 | (FcChar32) c); | |
8510724d | 516 | |
b5b2545a | 517 | return (code ? code : FONT_INVALID_CODE); |
c2f5bfd6 KH |
518 | } |
519 | ||
520 | static int | |
521 | xftfont_text_extents (font, code, nglyphs, metrics) | |
522 | struct font *font; | |
523 | unsigned *code; | |
524 | int nglyphs; | |
525 | struct font_metrics *metrics; | |
526 | { | |
527 | struct xftfont_info *xftfont_info = (struct xftfont_info *) font; | |
528 | XGlyphInfo extents; | |
529 | ||
530 | BLOCK_INPUT; | |
531 | XftGlyphExtents (xftfont_info->display, xftfont_info->xftfont, code, nglyphs, | |
532 | &extents); | |
533 | UNBLOCK_INPUT; | |
534 | if (metrics) | |
535 | { | |
536 | metrics->lbearing = - extents.x; | |
537 | metrics->rbearing = - extents.x + extents.width; | |
538 | metrics->width = extents.xOff; | |
539 | metrics->ascent = extents.y; | |
4b848612 | 540 | metrics->descent = extents.height - extents.y; |
c2f5bfd6 KH |
541 | } |
542 | return extents.xOff; | |
543 | } | |
544 | ||
a92ee6bf KH |
545 | static XftDraw * |
546 | xftfont_get_xft_draw (f) | |
547 | FRAME_PTR f; | |
548 | { | |
8510724d | 549 | XftDraw *xft_draw = font_get_frame_data (f, &xftfont_driver); |
a92ee6bf KH |
550 | |
551 | if (! xft_draw) | |
552 | { | |
553 | BLOCK_INPUT; | |
554 | xft_draw= XftDrawCreate (FRAME_X_DISPLAY (f), | |
555 | FRAME_X_WINDOW (f), | |
556 | FRAME_X_VISUAL (f), | |
557 | FRAME_X_COLORMAP (f)); | |
558 | UNBLOCK_INPUT; | |
559 | if (! xft_draw) | |
560 | abort (); | |
561 | font_put_frame_data (f, &xftfont_driver, xft_draw); | |
562 | } | |
563 | return xft_draw; | |
564 | } | |
565 | ||
c2f5bfd6 KH |
566 | static int |
567 | xftfont_draw (s, from, to, x, y, with_background) | |
568 | struct glyph_string *s; | |
569 | int from, to, x, y, with_background; | |
570 | { | |
571 | FRAME_PTR f = s->f; | |
572 | struct face *face = s->face; | |
3528e709 | 573 | struct xftfont_info *xftfont_info = (struct xftfont_info *) s->font; |
c78c1659 | 574 | struct xftface_info *xftface_info = NULL; |
a92ee6bf | 575 | XftDraw *xft_draw = xftfont_get_xft_draw (f); |
c2f5bfd6 KH |
576 | FT_UInt *code; |
577 | XftColor fg, bg; | |
c2f5bfd6 KH |
578 | int len = to - from; |
579 | int i; | |
580 | ||
3528e709 | 581 | if (s->font == face->font) |
a92ee6bf | 582 | xftface_info = (struct xftface_info *) face->extra; |
c2f5bfd6 | 583 | xftfont_get_colors (f, face, s->gc, xftface_info, |
322f8671 | 584 | &fg, with_background ? &bg : NULL); |
c2f5bfd6 | 585 | BLOCK_INPUT; |
03d198e8 KH |
586 | if (s->num_clips > 0) |
587 | XftDrawSetClipRectangles (xft_draw, 0, 0, s->clip, s->num_clips); | |
588 | else | |
589 | XftDrawSetClip (xft_draw, NULL); | |
590 | ||
c2f5bfd6 | 591 | if (with_background) |
3528e709 KH |
592 | XftDrawRect (xft_draw, &bg, |
593 | x, y - face->font->ascent, s->width, face->font->height); | |
c2f5bfd6 KH |
594 | code = alloca (sizeof (FT_UInt) * len); |
595 | for (i = 0; i < len; i++) | |
596 | code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8) | |
597 | | XCHAR2B_BYTE2 (s->char2b + from + i)); | |
598 | ||
785543da KH |
599 | if (s->padding_p) |
600 | for (i = 0; i < len; i++) | |
601 | XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont, | |
602 | x + i, y, code + i, 1); | |
603 | else | |
604 | XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont, | |
605 | x, y, code, len); | |
c2f5bfd6 KH |
606 | UNBLOCK_INPUT; |
607 | ||
608 | return len; | |
609 | } | |
610 | ||
a92ee6bf KH |
611 | static int |
612 | xftfont_end_for_frame (f) | |
613 | FRAME_PTR f; | |
614 | { | |
615 | XftDraw *xft_draw = font_get_frame_data (f, &xftfont_driver); | |
616 | ||
617 | if (xft_draw) | |
618 | { | |
619 | BLOCK_INPUT; | |
620 | XftDrawDestroy (xft_draw); | |
621 | UNBLOCK_INPUT; | |
622 | font_put_frame_data (f, &xftfont_driver, NULL); | |
623 | } | |
624 | return 0; | |
625 | } | |
c2f5bfd6 KH |
626 | |
627 | void | |
628 | syms_of_xftfont () | |
629 | { | |
630 | DEFSYM (Qxft, "xft"); | |
81094fab | 631 | DEFSYM (QChinting, ":hinting"); |
9743ac48 | 632 | DEFSYM (QCautohint, ":autohint"); |
81094fab KH |
633 | DEFSYM (QChintstyle, ":hintstyle"); |
634 | DEFSYM (QCrgba, ":rgba"); | |
635 | DEFSYM (QCembolden, ":embolden"); | |
c2f5bfd6 KH |
636 | |
637 | xftfont_driver = ftfont_driver; | |
638 | xftfont_driver.type = Qxft; | |
639 | xftfont_driver.get_cache = xfont_driver.get_cache; | |
640 | xftfont_driver.list = xftfont_list; | |
10aca0f7 | 641 | xftfont_driver.match = xftfont_match; |
c2f5bfd6 KH |
642 | xftfont_driver.open = xftfont_open; |
643 | xftfont_driver.close = xftfont_close; | |
644 | xftfont_driver.prepare_face = xftfont_prepare_face; | |
645 | xftfont_driver.done_face = xftfont_done_face; | |
6570a1c4 | 646 | xftfont_driver.has_char = xftfont_has_char; |
c2f5bfd6 KH |
647 | xftfont_driver.encode_char = xftfont_encode_char; |
648 | xftfont_driver.text_extents = xftfont_text_extents; | |
649 | xftfont_driver.draw = xftfont_draw; | |
a92ee6bf | 650 | xftfont_driver.end_for_frame = xftfont_end_for_frame; |
c2f5bfd6 KH |
651 | |
652 | register_font_driver (&xftfont_driver, NULL); | |
653 | } | |
885b7d09 MB |
654 | |
655 | /* arch-tag: 64ec61bf-7c8e-4fe6-b953-c6a85d5e1605 | |
656 | (do not change this comment) */ |