Remove floating-point check.
[emacs.git] / src / xftfont.c
blob55d0b943c13af084425e9668fa7a357b7e6cc1c3
1 /* xftfont.c -- XFT font driver.
2 Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
3 Copyright (C) 2006, 2007, 2008
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H13PRO009
7 This file is part of GNU Emacs.
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 3, or (at your option)
12 any later version.
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.
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. */
24 #include <config.h>
25 #include <stdio.h>
26 #include <X11/Xlib.h>
27 #include <X11/Xft/Xft.h>
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 #include "ftfont.h"
40 /* Xft font driver. */
42 static Lisp_Object Qxft;
44 /* The actual structure for Xft font that can be casted to struct
45 font. */
47 struct xftfont_info
49 struct font font;
50 Display *display;
51 int screen;
52 XftFont *xftfont;
53 #ifdef HAVE_LIBOTF
54 int maybe_otf; /* Flag to tell if this may be OTF or not. */
55 OTF *otf;
56 #endif /* HAVE_LIBOTF */
59 /* Structure pointed by (struct face *)->extra */
61 struct xftface_info
63 XftColor xft_fg; /* color for face->foreground */
64 XftColor xft_bg; /* color for face->background */
67 static void xftfont_get_colors P_ ((FRAME_PTR, struct face *, GC gc,
68 struct xftface_info *,
69 XftColor *fg, XftColor *bg));
72 /* Setup foreground and background colors of GC into FG and BG. If
73 XFTFACE_INFO is not NULL, reuse the colors in it if possible. BG
74 may be NULL. */
76 static void
77 xftfont_get_colors (f, face, gc, xftface_info, fg, bg)
78 FRAME_PTR f;
79 struct face *face;
80 GC gc;
81 struct xftface_info *xftface_info;
82 XftColor *fg, *bg;
84 if (xftface_info && face->gc == gc)
86 *fg = xftface_info->xft_fg;
87 if (bg)
88 *bg = xftface_info->xft_bg;
90 else
92 XGCValues xgcv;
93 int fg_done = 0, bg_done = 0;
95 BLOCK_INPUT;
96 XGetGCValues (FRAME_X_DISPLAY (f), gc,
97 GCForeground | GCBackground, &xgcv);
98 if (xftface_info)
100 if (xgcv.foreground == face->foreground)
101 *fg = xftface_info->xft_fg, fg_done = 1;
102 else if (xgcv.foreground == face->background)
103 *fg = xftface_info->xft_bg, fg_done = 1;
104 if (! bg)
105 bg_done = 1;
106 else if (xgcv.background == face->background)
107 *bg = xftface_info->xft_bg, bg_done = 1;
108 else if (xgcv.background == face->foreground)
109 *bg = xftface_info->xft_fg, bg_done = 1;
112 if (fg_done + bg_done < 2)
114 XColor colors[2];
116 colors[0].pixel = fg->pixel = xgcv.foreground;
117 if (bg)
118 colors[1].pixel = bg->pixel = xgcv.background;
119 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors,
120 bg ? 2 : 1);
121 fg->color.alpha = 0xFFFF;
122 fg->color.red = colors[0].red;
123 fg->color.green = colors[0].green;
124 fg->color.blue = colors[0].blue;
125 if (bg)
127 bg->color.alpha = 0xFFFF;
128 bg->color.red = colors[1].red;
129 bg->color.green = colors[1].green;
130 bg->color.blue = colors[1].blue;
133 UNBLOCK_INPUT;
138 static Lisp_Object xftfont_list P_ ((Lisp_Object, Lisp_Object));
139 static Lisp_Object xftfont_match P_ ((Lisp_Object, Lisp_Object));
140 static struct font *xftfont_open P_ ((FRAME_PTR, Lisp_Object, int));
141 static void xftfont_close P_ ((FRAME_PTR, struct font *));
142 static int xftfont_prepare_face P_ ((FRAME_PTR, struct face *));
143 static void xftfont_done_face P_ ((FRAME_PTR, struct face *));
144 static unsigned xftfont_encode_char P_ ((struct font *, int));
145 static int xftfont_text_extents P_ ((struct font *, unsigned *, int,
146 struct font_metrics *));
147 static int xftfont_draw P_ ((struct glyph_string *, int, int, int, int, int));
149 static int xftfont_anchor_point P_ ((struct font *, unsigned, int,
150 int *, int *));
151 static int xftfont_end_for_frame P_ ((FRAME_PTR f));
153 struct font_driver xftfont_driver;
155 static Lisp_Object
156 xftfont_list (frame, spec)
157 Lisp_Object frame;
158 Lisp_Object spec;
160 Lisp_Object val = ftfont_driver.list (frame, spec);
161 int i;
163 if (! NILP (val))
164 for (i = 0; i < ASIZE (val); i++)
165 ASET (AREF (val, i), FONT_TYPE_INDEX, Qxft);
166 return val;
169 static Lisp_Object
170 xftfont_match (frame, spec)
171 Lisp_Object frame;
172 Lisp_Object spec;
174 Lisp_Object entity = ftfont_driver.match (frame, spec);
176 if (VECTORP (entity))
177 ASET (entity, FONT_TYPE_INDEX, Qxft);
178 return entity;
181 extern Lisp_Object ftfont_font_format P_ ((FcPattern *));
183 static FcChar8 ascii_printable[95];
185 static struct font *
186 xftfont_open (f, entity, pixel_size)
187 FRAME_PTR f;
188 Lisp_Object entity;
189 int pixel_size;
191 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
192 Display *display = FRAME_X_DISPLAY (f);
193 Lisp_Object val;
194 FcPattern *pattern, *pat = NULL;
195 FcChar8 *file;
196 struct xftfont_info *xftfont_info = NULL;
197 XFontStruct *xfont = NULL;
198 struct font *font;
199 double size = 0;
200 XftFont *xftfont = NULL;
201 int spacing;
202 char *name;
203 int len;
204 XGlyphInfo extents;
205 FT_Face ft_face;
207 val = AREF (entity, FONT_EXTRA_INDEX);
208 if (XTYPE (val) != Lisp_Misc
209 || XMISCTYPE (val) != Lisp_Misc_Save_Value)
210 return NULL;
211 pattern = XSAVE_VALUE (val)->pointer;
212 if (FcPatternGetString (pattern, FC_FILE, 0, &file) != FcResultMatch)
213 return NULL;
215 size = XINT (AREF (entity, FONT_SIZE_INDEX));
216 if (size == 0)
217 size = pixel_size;
219 pat = FcPatternCreate ();
220 FcPatternAddString (pat, FC_FILE, file);
221 FcPatternAddDouble (pat, FC_PIXEL_SIZE, pixel_size);
222 /*FcPatternAddBool (pat, FC_ANTIALIAS, FcTrue);*/
223 val = AREF (entity, FONT_FAMILY_INDEX);
224 if (! NILP (val))
225 FcPatternAddString (pat, FC_FAMILY, (FcChar8 *) SDATA (SYMBOL_NAME (val)));
226 FcConfigSubstitute (NULL, pat, FcMatchPattern);
228 BLOCK_INPUT;
229 XftDefaultSubstitute (display, FRAME_X_SCREEN_NUMBER (f), pat);
230 xftfont = XftFontOpenPattern (display, pat);
231 /* We should not destroy PAT here because it is kept in XFTFONT and
232 destroyed automatically when XFTFONT is closed. */
233 if (! xftfont)
234 goto err;
236 xftfont_info = malloc (sizeof (struct xftfont_info));
237 if (! xftfont_info)
238 goto err;
239 xfont = malloc (sizeof (XFontStruct));
240 if (! xfont)
241 goto err;
242 xftfont_info->display = display;
243 xftfont_info->screen = FRAME_X_SCREEN_NUMBER (f);
244 xftfont_info->xftfont = xftfont;
245 #ifdef HAVE_LIBOTF
246 ft_face = XftLockFace (xftfont);
247 xftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT;
248 XftUnlockFace (xftfont);
249 xftfont_info->otf = NULL;
250 #endif /* HAVE_LIBOTF */
252 font = (struct font *) xftfont_info;
253 font->format = ftfont_font_format (xftfont->pattern);
254 font->entity = entity;
255 font->pixel_size = size;
256 font->driver = &xftfont_driver;
257 len = 96;
258 name = malloc (len);
259 while (name && font_unparse_fcname (entity, pixel_size, name, len) < 0)
261 char *new = realloc (name, len += 32);
263 if (! new)
264 free (name);
265 name = new;
267 if (! name)
268 goto err;
269 font->font.full_name = font->font.name = name;
270 font->file_name = (char *) file;
271 font->font.size = xftfont->max_advance_width;
272 font->font.charset = font->encoding_charset = font->repertory_charset = -1;
274 if (FcPatternGetInteger (xftfont->pattern, FC_SPACING, 0, &spacing)
275 != FcResultMatch)
276 spacing = FC_PROPORTIONAL;
277 if (! ascii_printable[0])
279 int i;
280 for (i = 0; i < 95; i++)
281 ascii_printable[i] = ' ' + i;
283 if (spacing != FC_PROPORTIONAL)
285 font->font.average_width = font->font.space_width
286 = xftfont->max_advance_width;
287 XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents);
289 else
291 XftTextExtents8 (display, xftfont, ascii_printable, 1, &extents);
292 font->font.space_width = extents.xOff;
293 if (font->font.space_width <= 0)
294 /* dirty workaround */
295 font->font.space_width = pixel_size;
296 XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents);
297 font->font.average_width = (font->font.space_width + extents.xOff) / 95;
299 UNBLOCK_INPUT;
301 font->ascent = xftfont->ascent;
302 if (font->ascent < extents.y)
303 font->ascent = extents.y;
304 font->descent = xftfont->descent;
305 if (font->descent < extents.height - extents.y)
306 font->descent = extents.height - extents.y;
307 font->font.height = font->ascent + font->descent;
309 /* Unfortunately Xft doesn't provide a way to get minimum char
310 width. So, we use space_width instead. */
311 font->min_width = font->font.space_width;
313 font->font.baseline_offset = 0;
314 font->font.relative_compose = 0;
315 font->font.default_ascent = 0;
316 font->font.vertical_centering = 0;
318 /* Setup pseudo XFontStruct */
319 xfont->fid = 0;
320 xfont->ascent = font->ascent;
321 xfont->descent = font->descent;
322 xfont->max_bounds.descent = font->descent;
323 xfont->max_bounds.width = xftfont->max_advance_width;
324 xfont->min_bounds.width = font->font.space_width;
325 font->font.font = xfont;
327 dpyinfo->n_fonts++;
329 /* Set global flag fonts_changed_p to non-zero if the font loaded
330 has a character with a smaller width than any other character
331 before, or if the font loaded has a smaller height than any other
332 font loaded before. If this happens, it will make a glyph matrix
333 reallocation necessary. */
334 if (dpyinfo->n_fonts == 1)
336 dpyinfo->smallest_font_height = font->font.height;
337 dpyinfo->smallest_char_width = font->min_width;
338 fonts_changed_p = 1;
340 else
342 if (dpyinfo->smallest_font_height > font->font.height)
343 dpyinfo->smallest_font_height = font->font.height,
344 fonts_changed_p |= 1;
345 if (dpyinfo->smallest_char_width > font->min_width)
346 dpyinfo->smallest_char_width = font->min_width,
347 fonts_changed_p |= 1;
350 return font;
352 err:
353 if (xftfont) XftFontClose (display, xftfont);
354 UNBLOCK_INPUT;
355 if (xftfont_info) free (xftfont_info);
356 if (xfont) free (xfont);
357 return NULL;
360 static void
361 xftfont_close (f, font)
362 FRAME_PTR f;
363 struct font *font;
365 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
367 #ifdef HAVE_LIBOTF
368 if (xftfont_info->otf)
369 OTF_close (xftfont_info->otf);
370 #endif
371 XftFontClose (xftfont_info->display, xftfont_info->xftfont);
372 if (font->font.name)
373 free (font->font.name);
374 free (font);
375 FRAME_X_DISPLAY_INFO (f)->n_fonts--;
378 static int
379 xftfont_prepare_face (f, face)
380 FRAME_PTR f;
381 struct face *face;
383 struct xftface_info *xftface_info;
385 #if 0
386 /* This doesn't work if face->ascii_face doesn't use an Xft font. */
387 if (face != face->ascii_face)
389 face->extra = face->ascii_face->extra;
390 return 0;
392 #endif
394 xftface_info = malloc (sizeof (struct xftface_info));
395 if (! xftface_info)
396 return -1;
398 BLOCK_INPUT;
399 xftfont_get_colors (f, face, face->gc, NULL,
400 &xftface_info->xft_fg, &xftface_info->xft_bg);
401 UNBLOCK_INPUT;
403 face->extra = xftface_info;
404 return 0;
407 static void
408 xftfont_done_face (f, face)
409 FRAME_PTR f;
410 struct face *face;
412 struct xftface_info *xftface_info;
414 #if 0
415 /* This doesn't work if face->ascii_face doesn't use an Xft font. */
416 if (face != face->ascii_face
417 || ! face->extra)
418 return;
419 #endif
421 xftface_info = (struct xftface_info *) face->extra;
422 if (xftface_info)
424 free (xftface_info);
425 face->extra = NULL;
429 static unsigned
430 xftfont_encode_char (font, c)
431 struct font *font;
432 int c;
434 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
435 unsigned code = XftCharIndex (xftfont_info->display, xftfont_info->xftfont,
436 (FcChar32) c);
438 return (code ? code : FONT_INVALID_CODE);
441 static int
442 xftfont_text_extents (font, code, nglyphs, metrics)
443 struct font *font;
444 unsigned *code;
445 int nglyphs;
446 struct font_metrics *metrics;
448 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
449 XGlyphInfo extents;
451 BLOCK_INPUT;
452 XftGlyphExtents (xftfont_info->display, xftfont_info->xftfont, code, nglyphs,
453 &extents);
454 UNBLOCK_INPUT;
455 if (metrics)
457 metrics->lbearing = - extents.x;
458 metrics->rbearing = - extents.x + extents.width;
459 metrics->width = extents.xOff;
460 metrics->ascent = extents.y;
461 metrics->descent = extents.height - extents.y;
463 return extents.xOff;
466 static XftDraw *
467 xftfont_get_xft_draw (f)
468 FRAME_PTR f;
470 XftDraw *xft_draw = font_get_frame_data (f, &xftfont_driver);;
472 if (! xft_draw)
474 BLOCK_INPUT;
475 xft_draw= XftDrawCreate (FRAME_X_DISPLAY (f),
476 FRAME_X_WINDOW (f),
477 FRAME_X_VISUAL (f),
478 FRAME_X_COLORMAP (f));
479 UNBLOCK_INPUT;
480 if (! xft_draw)
481 abort ();
482 font_put_frame_data (f, &xftfont_driver, xft_draw);
484 return xft_draw;
487 static int
488 xftfont_draw (s, from, to, x, y, with_background)
489 struct glyph_string *s;
490 int from, to, x, y, with_background;
492 FRAME_PTR f = s->f;
493 struct face *face = s->face;
494 struct xftfont_info *xftfont_info = (struct xftfont_info *) s->font_info;
495 struct xftface_info *xftface_info = NULL;
496 XftDraw *xft_draw = xftfont_get_xft_draw (f);
497 FT_UInt *code;
498 XftColor fg, bg;
499 XRectangle r;
500 int len = to - from;
501 int i;
503 if (s->font_info == face->font_info)
504 xftface_info = (struct xftface_info *) face->extra;
505 xftfont_get_colors (f, face, s->gc, xftface_info,
506 &fg, with_background ? &bg : NULL);
507 BLOCK_INPUT;
508 if (s->num_clips > 0)
509 XftDrawSetClipRectangles (xft_draw, 0, 0, s->clip, s->num_clips);
510 else
511 XftDrawSetClip (xft_draw, NULL);
513 if (with_background)
515 struct font *font = (struct font *) face->font_info;
517 XftDrawRect (xft_draw, &bg,
518 x, y - face->font->ascent, s->width, font->font.height);
520 code = alloca (sizeof (FT_UInt) * len);
521 for (i = 0; i < len; i++)
522 code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
523 | XCHAR2B_BYTE2 (s->char2b + from + i));
525 if (s->padding_p)
526 for (i = 0; i < len; i++)
527 XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont,
528 x + i, y, code + i, 1);
529 else
530 XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont,
531 x, y, code, len);
532 UNBLOCK_INPUT;
534 return len;
537 static int
538 xftfont_anchor_point (font, code, index, x, y)
539 struct font *font;
540 unsigned code;
541 int index;
542 int *x, *y;
544 struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
545 FT_Face ft_face = XftLockFace (xftfont_info->xftfont);
546 int result;
548 if (FT_Load_Glyph (ft_face, code, FT_LOAD_DEFAULT) != 0)
549 result = -1;
550 else if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
551 result = -1;
552 else if (index >= ft_face->glyph->outline.n_points)
553 result = -1;
554 else
556 *x = ft_face->glyph->outline.points[index].x;
557 *y = ft_face->glyph->outline.points[index].y;
559 XftUnlockFace (xftfont_info->xftfont);
560 return result;
563 static int
564 xftfont_end_for_frame (f)
565 FRAME_PTR f;
567 XftDraw *xft_draw = font_get_frame_data (f, &xftfont_driver);
569 if (xft_draw)
571 BLOCK_INPUT;
572 XftDrawDestroy (xft_draw);
573 UNBLOCK_INPUT;
574 font_put_frame_data (f, &xftfont_driver, NULL);
576 return 0;
579 #ifdef HAVE_LIBOTF
580 #ifdef HAVE_M17N_FLT
581 static Lisp_Object
582 xftfont_shape (lgstring)
583 Lisp_Object lgstring;
585 struct font *font;
586 struct xftfont_info *xftfont_info;
587 int result;
588 FT_Face ft_face;
590 CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring), font);
591 xftfont_info = (struct xftfont_info *) font;
592 if (! xftfont_info->maybe_otf)
593 return Qnil;
594 ft_face = XftLockFace (xftfont_info->xftfont);
595 if (! xftfont_info->otf)
597 OTF *otf = OTF_open_ft_face (ft_face);
599 if (! otf || OTF_get_table (otf, "head") < 0)
601 if (otf)
602 OTF_close (otf);
603 xftfont_info->maybe_otf = 0;
604 XftUnlockFace (xftfont_info->xftfont);
605 return 0;
607 xftfont_info->otf = otf;
610 result = ftfont_shape_by_flt (lgstring, font, ft_face, xftfont_info->otf);
611 XftUnlockFace (xftfont_info->xftfont);
612 return result;
614 #endif /* HAVE_M17N_FLT */
615 #endif /* HAVE_LIBOTF */
617 void
618 syms_of_xftfont ()
620 DEFSYM (Qxft, "xft");
622 xftfont_driver = ftfont_driver;
623 xftfont_driver.type = Qxft;
624 xftfont_driver.get_cache = xfont_driver.get_cache;
625 xftfont_driver.list = xftfont_list;
626 xftfont_driver.match = xftfont_match;
627 xftfont_driver.open = xftfont_open;
628 xftfont_driver.close = xftfont_close;
629 xftfont_driver.prepare_face = xftfont_prepare_face;
630 xftfont_driver.done_face = xftfont_done_face;
631 xftfont_driver.encode_char = xftfont_encode_char;
632 xftfont_driver.text_extents = xftfont_text_extents;
633 xftfont_driver.draw = xftfont_draw;
634 xftfont_driver.anchor_point = xftfont_anchor_point;
635 xftfont_driver.end_for_frame = xftfont_end_for_frame;
636 #ifdef HAVE_LIBOTF
637 #ifdef HAVE_M17N_FLT
638 xftfont_driver.shape = xftfont_shape;
639 #endif /* HAVE_M17N_FLT */
640 #endif /* HAVE_LIBOTF */
642 register_font_driver (&xftfont_driver, NULL);
645 /* arch-tag: 64ec61bf-7c8e-4fe6-b953-c6a85d5e1605
646 (do not change this comment) */