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