xftfont.c 18.6 KB
Newer Older
Kenichi Handa's avatar
Kenichi Handa committed
1
/* xftfont.c -- XFT font driver.
Glenn Morris's avatar
Glenn Morris committed
2 3
   Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
   Copyright (C) 2006, 2007, 2008, 2009
Kenichi Handa's avatar
Kenichi Handa committed
4 5 6 7 8
     National Institute of Advanced Industrial Science and Technology (AIST)
     Registration Number H13PRO009

This file is part of GNU Emacs.

9
GNU Emacs is free software: you can redistribute it and/or modify
Kenichi Handa's avatar
Kenichi Handa committed
10
it under the terms of the GNU General Public License as published by
11 12
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Kenichi Handa's avatar
Kenichi Handa committed
13 14 15 16 17 18 19

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
20
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Kenichi Handa's avatar
Kenichi Handa committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

#include <config.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>

#include "lisp.h"
#include "dispextern.h"
#include "xterm.h"
#include "frame.h"
#include "blockinput.h"
#include "character.h"
#include "charset.h"
#include "fontset.h"
#include "font.h"
Kenichi Handa's avatar
Kenichi Handa committed
36
#include "ftfont.h"
Kenichi Handa's avatar
Kenichi Handa committed
37 38 39 40

/* Xft font driver.  */

static Lisp_Object Qxft;
41
static Lisp_Object QChinting , QCautohint, QChintstyle, QCrgba, QCembolden;
Kenichi Handa's avatar
Kenichi Handa committed
42 43 44 45 46 47 48

/* The actual structure for Xft font that can be casted to struct
   font.  */

struct xftfont_info
{
  struct font font;
49
  /* The following four members must be here in this order to be
50
     compatible with struct ftfont_info (in ftfont.c).  */
Kenichi Handa's avatar
Kenichi Handa committed
51 52 53
#ifdef HAVE_LIBOTF
  int maybe_otf;	  /* Flag to tell if this may be OTF or not.  */
  OTF *otf;
Kenichi Handa's avatar
Kenichi Handa committed
54
#endif	/* HAVE_LIBOTF */
55
  FT_Size ft_size;
56
  int index;
57 58 59
  Display *display;
  int screen;
  XftFont *xftfont;
Kenichi Handa's avatar
Kenichi Handa committed
60 61 62
};

/* Structure pointed by (struct face *)->extra  */
Kenichi Handa's avatar
Kenichi Handa committed
63

Kenichi Handa's avatar
Kenichi Handa committed
64 65
struct xftface_info
{
Kenichi Handa's avatar
Kenichi Handa committed
66 67
  XftColor xft_fg;		/* color for face->foreground */
  XftColor xft_bg;		/* color for face->background */
Kenichi Handa's avatar
Kenichi Handa committed
68 69 70 71 72 73 74
};

static void xftfont_get_colors P_ ((FRAME_PTR, struct face *, GC gc,
				    struct xftface_info *,
				    XftColor *fg, XftColor *bg));


Kenichi Handa's avatar
Kenichi Handa committed
75 76 77 78
/* Setup foreground and background colors of GC into FG and BG.  If
   XFTFACE_INFO is not NULL, reuse the colors in it if possible.  BG
   may be NULL.  */

Kenichi Handa's avatar
Kenichi Handa committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
static void
xftfont_get_colors (f, face, gc, xftface_info, fg, bg)
     FRAME_PTR f;
     struct face *face;
     GC gc;
     struct xftface_info *xftface_info;
     XftColor *fg, *bg;
{
  if (xftface_info && face->gc == gc)
    {
      *fg = xftface_info->xft_fg;
      if (bg)
	*bg = xftface_info->xft_bg;
    }
  else
    {
      XGCValues xgcv;
      int fg_done = 0, bg_done = 0;

      BLOCK_INPUT;
      XGetGCValues (FRAME_X_DISPLAY (f), gc,
		    GCForeground | GCBackground, &xgcv);
      if (xftface_info)
	{
	  if (xgcv.foreground == face->foreground)
	    *fg = xftface_info->xft_fg, fg_done = 1;
	  else if (xgcv.foreground == face->background)
	    *fg = xftface_info->xft_bg, fg_done = 1;
	  if (! bg)
	    bg_done = 1;
	  else if (xgcv.background == face->background)
	    *bg = xftface_info->xft_bg, bg_done = 1;
	  else if (xgcv.background == face->foreground)
	    *bg = xftface_info->xft_fg, bg_done = 1;
	}

      if (fg_done + bg_done < 2)
	{
	  XColor colors[2];

	  colors[0].pixel = fg->pixel = xgcv.foreground;
	  if (bg)
	    colors[1].pixel = bg->pixel = xgcv.background;
	  XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors,
			bg ? 2 : 1);
	  fg->color.alpha = 0xFFFF;
	  fg->color.red = colors[0].red;
	  fg->color.green = colors[0].green;
	  fg->color.blue = colors[0].blue;
	  if (bg)
	    {
	      bg->color.alpha = 0xFFFF;
	      bg->color.red = colors[1].red;
	      bg->color.green = colors[1].green;
	      bg->color.blue = colors[1].blue;
	    }
	}
      UNBLOCK_INPUT;
    }
}


static Lisp_Object xftfont_list P_ ((Lisp_Object, Lisp_Object));
Kenichi Handa's avatar
Kenichi Handa committed
142
static Lisp_Object xftfont_match P_ ((Lisp_Object, Lisp_Object));
143
static Lisp_Object xftfont_open P_ ((FRAME_PTR, Lisp_Object, int));
Kenichi Handa's avatar
Kenichi Handa committed
144 145 146
static void xftfont_close P_ ((FRAME_PTR, struct font *));
static int xftfont_prepare_face P_ ((FRAME_PTR, struct face *));
static void xftfont_done_face P_ ((FRAME_PTR, struct face *));
147
static int xftfont_has_char P_ ((Lisp_Object, int));
Kenichi Handa's avatar
Kenichi Handa committed
148 149 150 151
static unsigned xftfont_encode_char P_ ((struct font *, int));
static int xftfont_text_extents P_ ((struct font *, unsigned *, int,
				     struct font_metrics *));
static int xftfont_draw P_ ((struct glyph_string *, int, int, int, int, int));
152
static int xftfont_end_for_frame P_ ((FRAME_PTR f));
Kenichi Handa's avatar
Kenichi Handa committed
153 154 155 156 157 158 159 160

struct font_driver xftfont_driver;

static Lisp_Object
xftfont_list (frame, spec)
     Lisp_Object frame;
     Lisp_Object spec;
{
161
  Lisp_Object list = ftfont_driver.list (frame, spec), tail;
162

163 164 165
  for (tail = list; CONSP (tail); tail = XCDR (tail))
    ASET (XCAR (tail), FONT_TYPE_INDEX, Qxft);
  return list;
Kenichi Handa's avatar
Kenichi Handa committed
166 167
}

Kenichi Handa's avatar
Kenichi Handa committed
168 169 170 171 172 173 174
static Lisp_Object
xftfont_match (frame, spec)
     Lisp_Object frame;
     Lisp_Object spec;
{
  Lisp_Object entity = ftfont_driver.match (frame, spec);

175
  if (! NILP (entity))
Kenichi Handa's avatar
Kenichi Handa committed
176 177 178 179
    ASET (entity, FONT_TYPE_INDEX, Qxft);
  return entity;
}

180
extern Lisp_Object ftfont_font_format P_ ((FcPattern *, Lisp_Object));
181
extern FcCharSet *ftfont_get_fc_charset P_ ((Lisp_Object));
182
extern Lisp_Object QCantialias;
183

Kenichi Handa's avatar
Kenichi Handa committed
184 185
static FcChar8 ascii_printable[95];

186
static Lisp_Object
Kenichi Handa's avatar
Kenichi Handa committed
187 188 189 190 191
xftfont_open (f, entity, pixel_size)
     FRAME_PTR f;
     Lisp_Object entity;
     int pixel_size;
{
192
  FcResult result;
Kenichi Handa's avatar
Kenichi Handa committed
193
  Display *display = FRAME_X_DISPLAY (f);
194
  Lisp_Object val, filename, index, tail, font_object;
195
  FcPattern *pat = NULL, *match;
196
  struct xftfont_info *xftfont_info = NULL;
Kenichi Handa's avatar
Kenichi Handa committed
197 198
  struct font *font;
  double size = 0;
199
  XftFont *xftfont = NULL;
Kenichi Handa's avatar
Kenichi Handa committed
200
  int spacing;
201 202
  char name[256];
  int len, i;
203
  XGlyphInfo extents;
204
  FT_Face ft_face;
Kenichi Handa's avatar
Kenichi Handa committed
205

206
  val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
207
  if (! CONSP (val))
208
    return Qnil;
209 210
  val = XCDR (val);
  filename = XCAR (val);
211
  index = XCDR (val);
Kenichi Handa's avatar
Kenichi Handa committed
212 213 214 215
  size = XINT (AREF (entity, FONT_SIZE_INDEX));
  if (size == 0)
    size = pixel_size;
  pat = FcPatternCreate ();
216 217 218 219 220
  FcPatternAddInteger (pat, FC_WEIGHT, FONT_WEIGHT_NUMERIC (entity));
  i = FONT_SLANT_NUMERIC (entity) - 100;
  if (i < 0) i = 0;
  FcPatternAddInteger (pat, FC_SLANT, i);
  FcPatternAddInteger (pat, FC_WIDTH, FONT_WIDTH_NUMERIC (entity));
Kenichi Handa's avatar
Kenichi Handa committed
221
  FcPatternAddDouble (pat, FC_PIXEL_SIZE, pixel_size);
222 223 224
  val = AREF (entity, FONT_FAMILY_INDEX);
  if (! NILP (val))
    FcPatternAddString (pat, FC_FAMILY, (FcChar8 *) SDATA (SYMBOL_NAME (val)));
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  val = AREF (entity, FONT_FOUNDRY_INDEX);
  if (! NILP (val))
    FcPatternAddString (pat, FC_FOUNDRY, (FcChar8 *) SDATA (SYMBOL_NAME (val)));
  val = AREF (entity, FONT_SPACING_INDEX);
  if (! NILP (val))
    FcPatternAddInteger (pat, FC_SPACING, XINT (val));
  val = AREF (entity, FONT_DPI_INDEX);
  if (! NILP (val))
    {
      double dbl = XINT (val);

      FcPatternAddDouble (pat, FC_DPI, dbl);
    }
  val = AREF (entity, FONT_AVGWIDTH_INDEX);
  if (INTEGERP (val) && XINT (val) == 0)
    FcPatternAddBool (pat, FC_SCALABLE, FcTrue);
241 242 243
  /* This is necessary to identify the exact font (e.g. 10x20.pcf.gz
     over 10x20-ISO8859-1.pcf.gz).  */
  FcPatternAddCharSet (pat, FC_CHARSET, ftfont_get_fc_charset (entity));
244

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
  for (tail = AREF (entity, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail))
    {
      Lisp_Object key, val;

      key = XCAR (XCAR (tail)), val = XCDR (XCAR (tail));
      if (EQ (key, QCantialias))
	FcPatternAddBool (pat, FC_ANTIALIAS, NILP (val) ? FcFalse : FcTrue);
      else if (EQ (key, QChinting))
	FcPatternAddBool (pat, FC_HINTING, NILP (val) ? FcFalse : FcTrue);
      else if (EQ (key, QCautohint))
	FcPatternAddBool (pat, FC_AUTOHINT, NILP (val) ? FcFalse : FcTrue);
      else if (EQ (key, QChintstyle))
	{
	  if (INTEGERP (val))
	    FcPatternAddInteger (pat, FC_RGBA, XINT (val));
	}
      else if (EQ (key, QCrgba))
	{
	  if (INTEGERP (val))
	    FcPatternAddInteger (pat, FC_RGBA, XINT (val));
	}
266
#ifdef FC_EMBOLDEN
267 268
      else if (EQ (key, QCembolden))
	FcPatternAddBool (pat, FC_EMBOLDEN, NILP (val) ? FcFalse : FcTrue);
269
#endif
270
    }
271

272 273
  FcPatternAddString (pat, FC_FILE, (FcChar8 *) SDATA (filename));
  FcPatternAddInteger (pat, FC_INDEX, XINT (index));
274

275

276
  BLOCK_INPUT;
277 278 279 280 281 282 283 284 285 286
  /* Make sure that the Xrender extension is added before the Xft one.
     Otherwise, the close-display hook set by Xft is called after the
     one for Xrender, and the former tries to re-add the latter.  This
     results in inconsistency of internal states and leads to X
     protocol error when one reconnects to the same X server.
     (Bug#1696)  */
  {
    int event_base, error_base;
    XRenderQueryExtension (display, &event_base, &error_base);
  }
287 288 289
  match = XftFontMatch (display, FRAME_X_SCREEN_NUMBER (f), pat, &result);
  FcPatternDestroy (pat);
  xftfont = XftFontOpenPattern (display, match);
290
  if (!xftfont)
291
    {
292
      UNBLOCK_INPUT;
293 294 295
      XftPatternDestroy (match);
      return Qnil;
    }
296 297 298
  ft_face = XftLockFace (xftfont);
  UNBLOCK_INPUT;

Kenichi Handa's avatar
Kenichi Handa committed
299 300
  /* We should not destroy PAT here because it is kept in XFTFONT and
     destroyed automatically when XFTFONT is closed.  */
301
  font_object = font_make_object (VECSIZE (struct xftfont_info), entity, size);
302 303 304
  ASET (font_object, FONT_TYPE_INDEX, Qxft);
  len = font_unparse_xlfd (entity, size, name, 256);
  if (len > 0)
305
    ASET (font_object, FONT_NAME_INDEX, make_string (name, len));
306 307
  len = font_unparse_fcname (entity, size, name, 256);
  if (len > 0)
308
    ASET (font_object, FONT_FULLNAME_INDEX, make_string (name, len));
309 310 311
  else
    ASET (font_object, FONT_FULLNAME_INDEX,
	  AREF (font_object, FONT_NAME_INDEX));
312 313
  ASET (font_object, FONT_FILE_INDEX, filename);
  ASET (font_object, FONT_FORMAT_INDEX,
314
	ftfont_font_format (xftfont->pattern, filename));
315 316 317 318 319 320
  font = XFONT_OBJECT (font_object);
  font->pixel_size = pixel_size;
  font->driver = &xftfont_driver;
  font->encoding_charset = font->repertory_charset = -1;

  xftfont_info = (struct xftfont_info *) font;
Kenichi Handa's avatar
Kenichi Handa committed
321 322 323 324 325
  xftfont_info->display = display;
  xftfont_info->screen = FRAME_X_SCREEN_NUMBER (f);
  xftfont_info->xftfont = xftfont;
  font->pixel_size = size;
  font->driver = &xftfont_driver;
326 327 328
  if (INTEGERP (AREF (entity, FONT_SPACING_INDEX)))
    spacing = XINT (AREF (entity, FONT_SPACING_INDEX));
  else
Kenichi Handa's avatar
Kenichi Handa committed
329
    spacing = FC_PROPORTIONAL;
330 331 332 333 334 335
  if (! ascii_printable[0])
    {
      int i;
      for (i = 0; i < 95; i++)
	ascii_printable[i] = ' ' + i;
    }
336
  BLOCK_INPUT;
Kenichi Handa's avatar
Kenichi Handa committed
337
  if (spacing != FC_PROPORTIONAL)
338
    {
339
      font->min_width = font->average_width = font->space_width
340 341 342
	= xftfont->max_advance_width;
      XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents);
    }
Kenichi Handa's avatar
Kenichi Handa committed
343 344 345
  else
    {
      XftTextExtents8 (display, xftfont, ascii_printable, 1, &extents);
346 347
      font->space_width = extents.xOff;
      if (font->space_width <= 0)
Kenichi Handa's avatar
Kenichi Handa committed
348
	/* dirty workaround */
349
	font->space_width = pixel_size;
Kenichi Handa's avatar
Kenichi Handa committed
350
      XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents);
351
      font->average_width = (font->space_width + extents.xOff) / 95;
Kenichi Handa's avatar
Kenichi Handa committed
352
    }
353
  UNBLOCK_INPUT;
Kenichi Handa's avatar
Kenichi Handa committed
354

355 356
  font->ascent = xftfont->ascent;
  font->descent = xftfont->descent;
357 358 359 360 361 362 363 364 365 366
  if (pixel_size >= 5)
    {
      /* The above condition is a dirty workaround because
	 XftTextExtents8 behaves strangely for some fonts
	 (e.g. "Dejavu Sans Mono") when pixel_size is less than 5. */
      if (font->ascent < extents.y)
	font->ascent = extents.y;
      if (font->descent < extents.height - extents.y)
	font->descent = extents.height - extents.y;
    }
367
  font->height = font->ascent + font->descent;
368

369
  if (XINT (AREF (entity, FONT_SIZE_INDEX)) == 0)
Kenichi Handa's avatar
Kenichi Handa committed
370
    {
371 372 373
      int upEM = ft_face->units_per_EM;

      font->underline_position = -ft_face->underline_position * size / upEM;
374
      font->underline_thickness = ft_face->underline_thickness * size / upEM;
375 376
      if (font->underline_thickness > 2)
	font->underline_position -= font->underline_thickness / 2;
Kenichi Handa's avatar
Kenichi Handa committed
377 378 379
    }
  else
    {
380 381
      font->underline_position = -1;
      font->underline_thickness = 0;
Kenichi Handa's avatar
Kenichi Handa committed
382
    }
383 384 385 386
#ifdef HAVE_LIBOTF
  xftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT;
  xftfont_info->otf = NULL;
#endif	/* HAVE_LIBOTF */
387
  xftfont_info->ft_size = ft_face->size;
Kenichi Handa's avatar
Kenichi Handa committed
388

389 390 391
  /* Unfortunately Xft doesn't provide a way to get minimum char
     width.  So, we use space_width instead.  */
  font->min_width = font->space_width;
392

393 394 395 396
  font->baseline_offset = 0;
  font->relative_compose = 0;
  font->default_ascent = 0;
  font->vertical_centering = 0;
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
#ifdef FT_BDF_H
  if (! (ft_face->face_flags & FT_FACE_FLAG_SFNT))
    {
      BDF_PropertyRec rec;

      if (FT_Get_BDF_Property (ft_face, "_MULE_BASELINE_OFFSET", &rec) == 0
	  && rec.type == BDF_PROPERTY_TYPE_INTEGER)
	font->baseline_offset = rec.u.integer;
      if (FT_Get_BDF_Property (ft_face, "_MULE_RELATIVE_COMPOSE", &rec) == 0
	  && rec.type == BDF_PROPERTY_TYPE_INTEGER)
	font->relative_compose = rec.u.integer;
      if (FT_Get_BDF_Property (ft_face, "_MULE_DEFAULT_ASCENT", &rec) == 0
	  && rec.type == BDF_PROPERTY_TYPE_INTEGER)
	font->default_ascent = rec.u.integer;
    }
#endif
413 414

  return font_object;
Kenichi Handa's avatar
Kenichi Handa committed
415 416 417 418 419 420 421 422 423
}

static void
xftfont_close (f, font)
     FRAME_PTR f;
     struct font *font;
{
  struct xftfont_info *xftfont_info = (struct xftfont_info *) font;

Kenichi Handa's avatar
Kenichi Handa committed
424 425 426 427
#ifdef HAVE_LIBOTF
  if (xftfont_info->otf)
    OTF_close (xftfont_info->otf);
#endif
428
  BLOCK_INPUT;
429
  XftUnlockFace (xftfont_info->xftfont);
Kenichi Handa's avatar
Kenichi Handa committed
430
  XftFontClose (xftfont_info->display, xftfont_info->xftfont);
431
  UNBLOCK_INPUT;
Kenichi Handa's avatar
Kenichi Handa committed
432 433 434 435 436 437 438
}

static int
xftfont_prepare_face (f, face)
     FRAME_PTR f;
     struct face *face;
{
439
  struct xftface_info *xftface_info;
Kenichi Handa's avatar
Kenichi Handa committed
440

441 442
#if 0
  /* This doesn't work if face->ascii_face doesn't use an Xft font. */
443 444 445 446 447
  if (face != face->ascii_face)
    {
      face->extra = face->ascii_face->extra;
      return 0;
    }
448
#endif
449 450

  xftface_info = malloc (sizeof (struct xftface_info));
Kenichi Handa's avatar
Kenichi Handa committed
451 452 453 454 455 456 457 458 459 460 461 462 463
  if (! xftface_info)
    return -1;
  xftfont_get_colors (f, face, face->gc, NULL,
		      &xftface_info->xft_fg, &xftface_info->xft_bg);
  face->extra = xftface_info;
  return 0;
}

static void
xftfont_done_face (f, face)
     FRAME_PTR f;
     struct face *face;
{
464
  struct xftface_info *xftface_info;
465

466 467
#if 0
  /* This doesn't work if face->ascii_face doesn't use an Xft font. */
468 469 470
  if (face != face->ascii_face
      || ! face->extra)
    return;
471
#endif
Kenichi Handa's avatar
Kenichi Handa committed
472

473
  xftface_info = (struct xftface_info *) face->extra;
474 475 476
  if (xftface_info)
    {
      free (xftface_info);
477
      face->extra = NULL;
478
    }
Kenichi Handa's avatar
Kenichi Handa committed
479 480
}

481 482
extern Lisp_Object Qja, Qko;

483 484 485 486 487 488
static int
xftfont_has_char (font, c)
     Lisp_Object font;
     int c;
{
  struct xftfont_info *xftfont_info;
489
  struct charset *cs = NULL;
490 491 492 493

  if (FONT_ENTITY_P (font))
    return ftfont_driver.has_char (font, c);

494 495 496 497 498 499 500 501 502
  if (EQ (AREF (font, FONT_ADSTYLE_INDEX), Qja)
      && charset_jisx0208 >= 0)
    cs = CHARSET_FROM_ID (charset_jisx0208);
  else if (EQ (AREF (font, FONT_ADSTYLE_INDEX), Qko)
      && charset_ksc5601 >= 0)
    cs = CHARSET_FROM_ID (charset_ksc5601);
  if (cs)
    return (ENCODE_CHAR (cs, c) != CHARSET_INVALID_CODE (cs));

503 504 505 506 507
  xftfont_info = (struct xftfont_info *) XFONT_OBJECT (font);
  return (XftCharExists (xftfont_info->display, xftfont_info->xftfont,
			 (FcChar32) c) == FcTrue);
}

Kenichi Handa's avatar
Kenichi Handa committed
508 509 510 511 512 513 514 515
static unsigned
xftfont_encode_char (font, c)
     struct font *font;
     int c;
{
  struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
  unsigned code = XftCharIndex (xftfont_info->display, xftfont_info->xftfont,
				(FcChar32) c);
516

517
  return (code ? code : FONT_INVALID_CODE);
Kenichi Handa's avatar
Kenichi Handa committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
}

static int
xftfont_text_extents (font, code, nglyphs, metrics)
     struct font *font;
     unsigned *code;
     int nglyphs;
     struct font_metrics *metrics;
{
  struct xftfont_info *xftfont_info = (struct xftfont_info *) font;
  XGlyphInfo extents;

  BLOCK_INPUT;
  XftGlyphExtents (xftfont_info->display, xftfont_info->xftfont, code, nglyphs,
		   &extents);
  UNBLOCK_INPUT;
  if (metrics)
    {
      metrics->lbearing = - extents.x;
      metrics->rbearing = - extents.x + extents.width;
      metrics->width = extents.xOff;
      metrics->ascent = extents.y;
540
      metrics->descent = extents.height - extents.y;
Kenichi Handa's avatar
Kenichi Handa committed
541 542 543 544
    }
  return extents.xOff;
}

545 546 547 548
static XftDraw *
xftfont_get_xft_draw (f)
     FRAME_PTR f;
{
549
  XftDraw *xft_draw = font_get_frame_data (f, &xftfont_driver);
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565

  if (! xft_draw)
    {
      BLOCK_INPUT;
      xft_draw= XftDrawCreate (FRAME_X_DISPLAY (f),
			       FRAME_X_WINDOW (f),
			       FRAME_X_VISUAL (f),
			       FRAME_X_COLORMAP (f));
      UNBLOCK_INPUT;
      if (! xft_draw)
	abort ();
      font_put_frame_data (f, &xftfont_driver, xft_draw);
    }
  return xft_draw;
}

Kenichi Handa's avatar
Kenichi Handa committed
566 567 568 569 570 571 572
static int
xftfont_draw (s, from, to, x, y, with_background)
     struct glyph_string *s;
     int from, to, x, y, with_background;
{
  FRAME_PTR f = s->f;
  struct face *face = s->face;
573
  struct xftfont_info *xftfont_info = (struct xftfont_info *) s->font;
574
  struct xftface_info *xftface_info = NULL;
575
  XftDraw *xft_draw = xftfont_get_xft_draw (f);
Kenichi Handa's avatar
Kenichi Handa committed
576 577 578 579 580
  FT_UInt *code;
  XftColor fg, bg;
  int len = to - from;
  int i;

581
  if (s->font == face->font)
582
    xftface_info = (struct xftface_info *) face->extra;
Kenichi Handa's avatar
Kenichi Handa committed
583
  xftfont_get_colors (f, face, s->gc, xftface_info,
584
		      &fg, with_background ? &bg : NULL);
Kenichi Handa's avatar
Kenichi Handa committed
585
  BLOCK_INPUT;
586 587 588 589 590
  if (s->num_clips > 0)
    XftDrawSetClipRectangles (xft_draw, 0, 0, s->clip, s->num_clips);
  else
    XftDrawSetClip (xft_draw, NULL);

Kenichi Handa's avatar
Kenichi Handa committed
591
  if (with_background)
592 593
    XftDrawRect (xft_draw, &bg,
		 x, y - face->font->ascent, s->width, face->font->height);
Kenichi Handa's avatar
Kenichi Handa committed
594 595 596 597 598
  code = alloca (sizeof (FT_UInt) * len);
  for (i = 0; i < len; i++)
    code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
	       | XCHAR2B_BYTE2 (s->char2b + from + i));

599 600 601 602 603 604 605
  if (s->padding_p)
    for (i = 0; i < len; i++)
      XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont,
		     x + i, y, code + i, 1);
  else
    XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont,
		   x, y, code, len);
Kenichi Handa's avatar
Kenichi Handa committed
606 607 608 609 610
  UNBLOCK_INPUT;

  return len;
}

611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
static int
xftfont_end_for_frame (f)
     FRAME_PTR f;
{
  XftDraw *xft_draw = font_get_frame_data (f, &xftfont_driver);

  if (xft_draw)
    {
      BLOCK_INPUT;
      XftDrawDestroy (xft_draw);
      UNBLOCK_INPUT;
      font_put_frame_data (f, &xftfont_driver, NULL);
    }
  return 0;
}
Kenichi Handa's avatar
Kenichi Handa committed
626 627 628 629 630

void
syms_of_xftfont ()
{
  DEFSYM (Qxft, "xft");
631
  DEFSYM (QChinting, ":hinting");
Chong Yidong's avatar
Chong Yidong committed
632
  DEFSYM (QCautohint, ":autohint");
633 634 635
  DEFSYM (QChintstyle, ":hintstyle");
  DEFSYM (QCrgba, ":rgba");
  DEFSYM (QCembolden, ":embolden");
Kenichi Handa's avatar
Kenichi Handa committed
636 637 638 639 640

  xftfont_driver = ftfont_driver;
  xftfont_driver.type = Qxft;
  xftfont_driver.get_cache = xfont_driver.get_cache;
  xftfont_driver.list = xftfont_list;
Kenichi Handa's avatar
Kenichi Handa committed
641
  xftfont_driver.match = xftfont_match;
Kenichi Handa's avatar
Kenichi Handa committed
642 643 644 645
  xftfont_driver.open = xftfont_open;
  xftfont_driver.close = xftfont_close;
  xftfont_driver.prepare_face = xftfont_prepare_face;
  xftfont_driver.done_face = xftfont_done_face;
646
  xftfont_driver.has_char = xftfont_has_char;
Kenichi Handa's avatar
Kenichi Handa committed
647 648 649
  xftfont_driver.encode_char = xftfont_encode_char;
  xftfont_driver.text_extents = xftfont_text_extents;
  xftfont_driver.draw = xftfont_draw;
650
  xftfont_driver.end_for_frame = xftfont_end_for_frame;
Kenichi Handa's avatar
Kenichi Handa committed
651 652 653

  register_font_driver (&xftfont_driver, NULL);
}
Miles Bader's avatar
Miles Bader committed
654 655 656

/* arch-tag: 64ec61bf-7c8e-4fe6-b953-c6a85d5e1605
   (do not change this comment) */