Commit | Line | Data |
---|---|---|
c2f5bfd6 KH |
1 | /* xftfont.c -- XFT font driver. |
2 | Copyright (C) 2006 Free Software Foundation, Inc. | |
3 | Copyright (C) 2006 | |
4 | National Institute of Advanced Industrial Science and Technology (AIST) | |
5 | Registration Number H13PRO009 | |
6 | ||
7 | This file is part of GNU Emacs. | |
8 | ||
9 | GNU Emacs is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2, or (at your option) | |
12 | any later version. | |
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 | |
20 | along with GNU Emacs; see the file COPYING. If not, write to | |
21 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
22 | Boston, MA 02110-1301, USA. */ | |
23 | ||
24 | #include <config.h> | |
25 | #include <stdio.h> | |
26 | #include <X11/Xlib.h> | |
27 | #include <X11/Xft/Xft.h> | |
28 | ||
29 | #include "lisp.h" | |
30 | #include "dispextern.h" | |
31 | #include "xterm.h" | |
32 | #include "frame.h" | |
33 | #include "blockinput.h" | |
34 | #include "character.h" | |
35 | #include "charset.h" | |
36 | #include "fontset.h" | |
37 | #include "font.h" | |
38 | ||
39 | /* Xft font driver. */ | |
40 | ||
41 | static Lisp_Object Qxft; | |
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; | |
52 | FT_Face ft_face; | |
53 | }; | |
54 | ||
55 | /* Structure pointed by (struct face *)->extra */ | |
56 | struct xftface_info | |
57 | { | |
58 | XftColor xft_fg; | |
59 | XftColor xft_bg; | |
60 | XftDraw *xft_draw; | |
61 | }; | |
62 | ||
63 | static void xftfont_get_colors P_ ((FRAME_PTR, struct face *, GC gc, | |
64 | struct xftface_info *, | |
65 | XftColor *fg, XftColor *bg)); | |
66 | static Font xftfont_default_fid P_ ((FRAME_PTR)); | |
67 | ||
68 | ||
69 | /* Setup colors pointed by FG and BG for GC. If XFTFACE_INFO is not | |
70 | NULL, reuse the colors in it if possible. BG may be NULL. */ | |
71 | static void | |
72 | xftfont_get_colors (f, face, gc, xftface_info, fg, bg) | |
73 | FRAME_PTR f; | |
74 | struct face *face; | |
75 | GC gc; | |
76 | struct xftface_info *xftface_info; | |
77 | XftColor *fg, *bg; | |
78 | { | |
79 | if (xftface_info && face->gc == gc) | |
80 | { | |
81 | *fg = xftface_info->xft_fg; | |
82 | if (bg) | |
83 | *bg = xftface_info->xft_bg; | |
84 | } | |
85 | else | |
86 | { | |
87 | XGCValues xgcv; | |
88 | int fg_done = 0, bg_done = 0; | |
89 | ||
90 | BLOCK_INPUT; | |
91 | XGetGCValues (FRAME_X_DISPLAY (f), gc, | |
92 | GCForeground | GCBackground, &xgcv); | |
93 | if (xftface_info) | |
94 | { | |
95 | if (xgcv.foreground == face->foreground) | |
96 | *fg = xftface_info->xft_fg, fg_done = 1; | |
97 | else if (xgcv.foreground == face->background) | |
98 | *fg = xftface_info->xft_bg, fg_done = 1; | |
99 | if (! bg) | |
100 | bg_done = 1; | |
101 | else if (xgcv.background == face->background) | |
102 | *bg = xftface_info->xft_bg, bg_done = 1; | |
103 | else if (xgcv.background == face->foreground) | |
104 | *bg = xftface_info->xft_fg, bg_done = 1; | |
105 | } | |
106 | ||
107 | if (fg_done + bg_done < 2) | |
108 | { | |
109 | XColor colors[2]; | |
110 | ||
111 | colors[0].pixel = fg->pixel = xgcv.foreground; | |
112 | if (bg) | |
113 | colors[1].pixel = bg->pixel = xgcv.background; | |
114 | XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, | |
115 | bg ? 2 : 1); | |
116 | fg->color.alpha = 0xFFFF; | |
117 | fg->color.red = colors[0].red; | |
118 | fg->color.green = colors[0].green; | |
119 | fg->color.blue = colors[0].blue; | |
120 | if (bg) | |
121 | { | |
122 | bg->color.alpha = 0xFFFF; | |
123 | bg->color.red = colors[1].red; | |
124 | bg->color.green = colors[1].green; | |
125 | bg->color.blue = colors[1].blue; | |
126 | } | |
127 | } | |
128 | UNBLOCK_INPUT; | |
129 | } | |
130 | } | |
131 | ||
132 | /* Return the default Font ID on frame F. */ | |
133 | ||
134 | static Font | |
135 | xftfont_default_fid (f) | |
136 | FRAME_PTR f; | |
137 | { | |
138 | static int fid_known; | |
139 | static Font fid; | |
140 | ||
141 | if (! fid_known) | |
142 | { | |
143 | fid = XLoadFont (FRAME_X_DISPLAY (f), "fixed"); | |
144 | if (! fid) | |
145 | { | |
146 | fid = XLoadFont (FRAME_X_DISPLAY (f), "*"); | |
147 | if (! fid) | |
148 | abort (); | |
149 | } | |
322f8671 | 150 | fid_known = 1; |
c2f5bfd6 KH |
151 | } |
152 | return fid; | |
153 | } | |
154 | ||
155 | ||
156 | static Lisp_Object xftfont_list P_ ((Lisp_Object, Lisp_Object)); | |
157 | static struct font *xftfont_open P_ ((FRAME_PTR, Lisp_Object, int)); | |
158 | static void xftfont_close P_ ((FRAME_PTR, struct font *)); | |
159 | static int xftfont_prepare_face P_ ((FRAME_PTR, struct face *)); | |
160 | static void xftfont_done_face P_ ((FRAME_PTR, struct face *)); | |
161 | static unsigned xftfont_encode_char P_ ((struct font *, int)); | |
162 | static int xftfont_text_extents P_ ((struct font *, unsigned *, int, | |
163 | struct font_metrics *)); | |
164 | static int xftfont_draw P_ ((struct glyph_string *, int, int, int, int, int)); | |
165 | ||
166 | static int xftfont_anchor_point P_ ((struct font *, unsigned, int, | |
167 | int *, int *)); | |
168 | ||
169 | struct font_driver xftfont_driver; | |
170 | ||
171 | static Lisp_Object | |
172 | xftfont_list (frame, spec) | |
173 | Lisp_Object frame; | |
174 | Lisp_Object spec; | |
175 | { | |
176 | Lisp_Object val = ftfont_driver.list (frame, spec); | |
177 | ||
178 | if (! NILP (val)) | |
179 | { | |
180 | int i; | |
181 | ||
182 | for (i = 0; i < ASIZE (val); i++) | |
183 | ASET (AREF (val, i), FONT_TYPE_INDEX, Qxft); | |
184 | } | |
185 | return val; | |
186 | } | |
187 | ||
188 | static FcChar8 ascii_printable[95]; | |
189 | ||
190 | static struct font * | |
191 | xftfont_open (f, entity, pixel_size) | |
192 | FRAME_PTR f; | |
193 | Lisp_Object entity; | |
194 | int pixel_size; | |
195 | { | |
196 | Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
197 | Display *display = FRAME_X_DISPLAY (f); | |
198 | Lisp_Object val; | |
199 | FcPattern *pattern, *pat; | |
dcce3c58 | 200 | FcChar8 *file, *name; |
c2f5bfd6 KH |
201 | XFontStruct *xfont; |
202 | struct xftfont_info *xftfont_info; | |
203 | struct font *font; | |
204 | double size = 0; | |
205 | XftFont *xftfont; | |
206 | int spacing; | |
dcce3c58 | 207 | int len; |
c2f5bfd6 KH |
208 | |
209 | val = AREF (entity, FONT_EXTRA_INDEX); | |
210 | if (XTYPE (val) != Lisp_Misc | |
211 | || XMISCTYPE (val) != Lisp_Misc_Save_Value) | |
212 | return NULL; | |
213 | pattern = XSAVE_VALUE (val)->pointer; | |
214 | if (FcPatternGetString (pattern, FC_FILE, 0, &file) != FcResultMatch) | |
215 | return NULL; | |
216 | ||
217 | size = XINT (AREF (entity, FONT_SIZE_INDEX)); | |
218 | if (size == 0) | |
219 | size = pixel_size; | |
dcce3c58 KH |
220 | if (FcPatternGetString (pattern, FC_FILE, 1, &name) != FcResultMatch) |
221 | { | |
222 | int isize = size; | |
223 | ||
224 | name = malloc (strlen ((char *) file) + 30); | |
225 | if (! name) | |
226 | return NULL; | |
227 | sprintf (name, ":file=%s:pixelsize=%d", (char *) file, isize); | |
228 | } | |
229 | ||
c2f5bfd6 KH |
230 | pat = FcPatternCreate (); |
231 | FcPatternAddString (pat, FC_FILE, file); | |
232 | FcPatternAddDouble (pat, FC_PIXEL_SIZE, pixel_size); | |
233 | FcPatternAddBool (pat, FC_ANTIALIAS, FcTrue); | |
dcce3c58 KH |
234 | |
235 | BLOCK_INPUT; | |
c2f5bfd6 KH |
236 | xftfont = XftFontOpenPattern (display, pat); |
237 | /* We should not destroy PAT here because it is kept in XFTFONT and | |
238 | destroyed automatically when XFTFONT is closed. */ | |
239 | if (! xftfont) | |
dcce3c58 KH |
240 | { |
241 | UNBLOCK_INPUT; | |
242 | return NULL; | |
243 | } | |
c2f5bfd6 KH |
244 | |
245 | xftfont_info = malloc (sizeof (struct xftfont_info)); | |
246 | if (! xftfont_info) | |
247 | { | |
248 | XftFontClose (display, xftfont); | |
dcce3c58 | 249 | UNBLOCK_INPUT; |
c2f5bfd6 KH |
250 | return NULL; |
251 | } | |
252 | xfont = malloc (sizeof (XFontStruct)); | |
253 | if (! xftfont_info) | |
254 | { | |
255 | XftFontClose (display, xftfont); | |
256 | free (xftfont_info); | |
dcce3c58 | 257 | UNBLOCK_INPUT; |
c2f5bfd6 KH |
258 | return NULL; |
259 | } | |
260 | xftfont_info->display = display; | |
261 | xftfont_info->screen = FRAME_X_SCREEN_NUMBER (f); | |
262 | xftfont_info->xftfont = xftfont; | |
263 | xftfont_info->ft_face = XftLockFace (xftfont); | |
264 | ||
265 | font = (struct font *) xftfont_info; | |
266 | font->entity = entity; | |
267 | font->pixel_size = size; | |
268 | font->driver = &xftfont_driver; | |
dcce3c58 | 269 | font->font.full_name = font->font.name = (char *) name; |
c2f5bfd6 KH |
270 | font->file_name = (char *) file; |
271 | font->font.size = xftfont->max_advance_width; | |
272 | font->ascent = xftfont->ascent; | |
273 | font->descent = xftfont->descent; | |
274 | font->font.height = xftfont->ascent + xftfont->descent; | |
275 | ||
276 | if (FcPatternGetInteger (xftfont->pattern, FC_SPACING, 0, &spacing) | |
277 | != FcResultMatch) | |
278 | spacing = FC_PROPORTIONAL; | |
279 | if (spacing != FC_PROPORTIONAL) | |
280 | font->font.average_width = font->font.space_width | |
281 | = xftfont->max_advance_width; | |
282 | else | |
283 | { | |
284 | XGlyphInfo extents; | |
285 | ||
286 | if (! ascii_printable[0]) | |
287 | { | |
288 | int i; | |
289 | for (i = 0; i < 95; i++) | |
290 | ascii_printable[i] = ' ' + i; | |
291 | } | |
292 | XftTextExtents8 (display, xftfont, ascii_printable, 1, &extents); | |
293 | font->font.space_width = extents.xOff; | |
294 | if (font->font.space_width <= 0) | |
295 | /* dirty workaround */ | |
296 | font->font.space_width = pixel_size; | |
297 | XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents); | |
298 | font->font.average_width = (font->font.space_width + extents.xOff) / 95; | |
299 | } | |
dcce3c58 | 300 | UNBLOCK_INPUT; |
c2f5bfd6 KH |
301 | |
302 | /* Unfortunately Xft doesn't provide a way to get minimum char | |
303 | width. So, we use space_width instead. */ | |
304 | font->min_width = font->font.space_width; | |
305 | ||
306 | font->font.baseline_offset = 0; | |
307 | font->font.relative_compose = 0; | |
308 | font->font.default_ascent = 0; | |
309 | font->font.vertical_centering = 0; | |
310 | ||
311 | /* Setup pseudo XFontStruct */ | |
312 | xfont->fid = xftfont_default_fid (f); | |
313 | xfont->ascent = xftfont->ascent; | |
314 | xfont->descent = xftfont->descent; | |
315 | xfont->max_bounds.descent = xftfont->descent; | |
316 | xfont->max_bounds.width = xftfont->max_advance_width; | |
317 | xfont->min_bounds.width = font->font.space_width; | |
318 | font->font.font = xfont; | |
319 | ||
320 | dpyinfo->n_fonts++; | |
321 | ||
322 | /* Set global flag fonts_changed_p to non-zero if the font loaded | |
323 | has a character with a smaller width than any other character | |
324 | before, or if the font loaded has a smaller height than any other | |
325 | font loaded before. If this happens, it will make a glyph matrix | |
326 | reallocation necessary. */ | |
327 | if (dpyinfo->n_fonts == 1) | |
328 | { | |
329 | dpyinfo->smallest_font_height = font->font.height; | |
330 | dpyinfo->smallest_char_width = font->min_width; | |
331 | fonts_changed_p = 1; | |
332 | } | |
333 | else | |
334 | { | |
335 | if (dpyinfo->smallest_font_height > font->font.height) | |
336 | dpyinfo->smallest_font_height = font->font.height, | |
337 | fonts_changed_p |= 1; | |
338 | if (dpyinfo->smallest_char_width > font->min_width) | |
339 | dpyinfo->smallest_char_width = font->min_width, | |
340 | fonts_changed_p |= 1; | |
341 | } | |
342 | ||
343 | return font; | |
344 | } | |
345 | ||
346 | static void | |
347 | xftfont_close (f, font) | |
348 | FRAME_PTR f; | |
349 | struct font *font; | |
350 | { | |
351 | struct xftfont_info *xftfont_info = (struct xftfont_info *) font; | |
352 | ||
353 | XftUnlockFace (xftfont_info->xftfont); | |
354 | XftFontClose (xftfont_info->display, xftfont_info->xftfont); | |
dcce3c58 KH |
355 | if (font->font.name) |
356 | free (font->font.name); | |
c2f5bfd6 KH |
357 | free (font); |
358 | FRAME_X_DISPLAY_INFO (f)->n_fonts--; | |
359 | } | |
360 | ||
c2f5bfd6 KH |
361 | static int |
362 | xftfont_prepare_face (f, face) | |
363 | FRAME_PTR f; | |
364 | struct face *face; | |
365 | { | |
366 | struct xftface_info *xftface_info = malloc (sizeof (struct xftface_info)); | |
367 | ||
368 | if (! xftface_info) | |
369 | return -1; | |
370 | ||
371 | BLOCK_INPUT; | |
372 | xftface_info->xft_draw = XftDrawCreate (FRAME_X_DISPLAY (f), | |
373 | FRAME_X_WINDOW (f), | |
374 | FRAME_X_VISUAL (f), | |
375 | FRAME_X_COLORMAP (f)); | |
c2f5bfd6 KH |
376 | xftfont_get_colors (f, face, face->gc, NULL, |
377 | &xftface_info->xft_fg, &xftface_info->xft_bg); | |
378 | UNBLOCK_INPUT; | |
379 | ||
380 | face->extra = xftface_info; | |
381 | return 0; | |
382 | } | |
383 | ||
384 | static void | |
385 | xftfont_done_face (f, face) | |
386 | FRAME_PTR f; | |
387 | struct face *face; | |
388 | { | |
389 | struct xftface_info *xftface_info = (struct xftface_info *) face->extra; | |
390 | ||
391 | if (xftface_info) | |
392 | { | |
393 | BLOCK_INPUT; | |
c2f5bfd6 KH |
394 | XftDrawDestroy (xftface_info->xft_draw); |
395 | UNBLOCK_INPUT; | |
396 | free (xftface_info); | |
397 | face->extra = NULL; | |
398 | } | |
399 | } | |
400 | ||
401 | static unsigned | |
402 | xftfont_encode_char (font, c) | |
403 | struct font *font; | |
404 | int c; | |
405 | { | |
406 | struct xftfont_info *xftfont_info = (struct xftfont_info *) font; | |
407 | unsigned code = XftCharIndex (xftfont_info->display, xftfont_info->xftfont, | |
408 | (FcChar32) c); | |
409 | ||
410 | return (code ? code : 0xFFFFFFFF); | |
411 | } | |
412 | ||
413 | static int | |
414 | xftfont_text_extents (font, code, nglyphs, metrics) | |
415 | struct font *font; | |
416 | unsigned *code; | |
417 | int nglyphs; | |
418 | struct font_metrics *metrics; | |
419 | { | |
420 | struct xftfont_info *xftfont_info = (struct xftfont_info *) font; | |
421 | XGlyphInfo extents; | |
422 | ||
423 | BLOCK_INPUT; | |
424 | XftGlyphExtents (xftfont_info->display, xftfont_info->xftfont, code, nglyphs, | |
425 | &extents); | |
426 | UNBLOCK_INPUT; | |
427 | if (metrics) | |
428 | { | |
429 | metrics->lbearing = - extents.x; | |
430 | metrics->rbearing = - extents.x + extents.width; | |
431 | metrics->width = extents.xOff; | |
432 | metrics->ascent = extents.y; | |
433 | metrics->descent = extents.y - extents.height; | |
434 | } | |
435 | return extents.xOff; | |
436 | } | |
437 | ||
438 | static int | |
439 | xftfont_draw (s, from, to, x, y, with_background) | |
440 | struct glyph_string *s; | |
441 | int from, to, x, y, with_background; | |
442 | { | |
443 | FRAME_PTR f = s->f; | |
444 | struct face *face = s->face; | |
445 | struct xftfont_info *xftfont_info = (struct xftfont_info *) face->font_info; | |
446 | struct xftface_info *xftface_info = (struct xftface_info *) face->extra; | |
447 | FT_UInt *code; | |
448 | XftColor fg, bg; | |
449 | XRectangle r; | |
450 | int len = to - from; | |
451 | int i; | |
452 | ||
453 | xftfont_get_colors (f, face, s->gc, xftface_info, | |
322f8671 | 454 | &fg, with_background ? &bg : NULL); |
c2f5bfd6 KH |
455 | BLOCK_INPUT; |
456 | if (s->clip_width) | |
457 | { | |
458 | r.x = s->clip_x, r.width = s->clip_width; | |
459 | r.y = s->clip_y, r.height = s->clip_height; | |
460 | XftDrawSetClipRectangles (xftface_info->xft_draw, 0, 0, &r, 1); | |
461 | } | |
462 | if (with_background) | |
463 | { | |
464 | struct font *font = (struct font *) face->font_info; | |
465 | ||
466 | XftDrawRect (xftface_info->xft_draw, &bg, | |
467 | x, y - face->font->ascent, s->width, font->font.height); | |
468 | } | |
469 | code = alloca (sizeof (FT_UInt) * len); | |
470 | for (i = 0; i < len; i++) | |
471 | code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8) | |
472 | | XCHAR2B_BYTE2 (s->char2b + from + i)); | |
473 | ||
474 | XftDrawGlyphs (xftface_info->xft_draw, &fg, xftfont_info->xftfont, | |
475 | x, y, code, len); | |
476 | if (s->clip_width) | |
477 | XftDrawSetClip (xftface_info->xft_draw, NULL); | |
478 | UNBLOCK_INPUT; | |
479 | ||
480 | return len; | |
481 | } | |
482 | ||
483 | static int | |
484 | xftfont_anchor_point (font, code, index, x, y) | |
485 | struct font *font; | |
486 | unsigned code; | |
487 | int index; | |
488 | int *x, *y; | |
489 | { | |
490 | struct xftfont_info *xftfont_info = (struct xftfont_info *) font; | |
491 | FT_Face ft_face = xftfont_info->ft_face; | |
492 | ||
493 | if (FT_Load_Glyph (ft_face, code, FT_LOAD_DEFAULT) != 0) | |
494 | return -1; | |
495 | if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE) | |
496 | return -1; | |
497 | if (index >= ft_face->glyph->outline.n_points) | |
498 | return -1; | |
499 | *x = ft_face->glyph->outline.points[index].x; | |
500 | *y = ft_face->glyph->outline.points[index].y; | |
501 | return 0; | |
502 | } | |
503 | ||
504 | ||
505 | void | |
506 | syms_of_xftfont () | |
507 | { | |
508 | DEFSYM (Qxft, "xft"); | |
509 | ||
510 | xftfont_driver = ftfont_driver; | |
511 | xftfont_driver.type = Qxft; | |
512 | xftfont_driver.get_cache = xfont_driver.get_cache; | |
513 | xftfont_driver.list = xftfont_list; | |
514 | xftfont_driver.open = xftfont_open; | |
515 | xftfont_driver.close = xftfont_close; | |
516 | xftfont_driver.prepare_face = xftfont_prepare_face; | |
517 | xftfont_driver.done_face = xftfont_done_face; | |
518 | xftfont_driver.encode_char = xftfont_encode_char; | |
519 | xftfont_driver.text_extents = xftfont_text_extents; | |
520 | xftfont_driver.draw = xftfont_draw; | |
521 | xftfont_driver.anchor_point = xftfont_anchor_point; | |
522 | ||
523 | register_font_driver (&xftfont_driver, NULL); | |
524 | } | |
885b7d09 MB |
525 | |
526 | /* arch-tag: 64ec61bf-7c8e-4fe6-b953-c6a85d5e1605 | |
527 | (do not change this comment) */ |