summaryrefslogtreecommitdiff
path: root/src/xftfont.c
blob: d8ad403548172e6f484ae32eea2f8ea678d8c821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/* xftfont.c -- XFT font driver.
   Copyright (C) 2006-2021 Free Software Foundation, Inc.
   Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
     National Institute of Advanced Industrial Science and Technology (AIST)
     Registration Number H13PRO009

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.

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
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */

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

#include "lisp.h"
#include "xterm.h"
#include "frame.h"
#include "blockinput.h"
#include "charset.h"
#include "composite.h"
#include "font.h"
#include "ftfont.h"
#include "pdumper.h"

#ifndef FC_LCD_FILTER
/* Older fontconfig versions don't have FC_LCD_FILTER.  */
# define FC_LCD_FILTER "lcdfilter"
#endif


/* Xft font driver.  */

/* Structure pointed by (struct face *)->extra  */

struct xftface_info
{
  XftColor xft_fg;		/* color for face->foreground */
  XftColor xft_bg;		/* color for face->background */
};

/* 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.  */

static void
xftfont_get_colors (struct frame *f, struct face *face, GC gc,
		    struct xftface_info *xftface_info,
		    XftColor *fg, XftColor *bg)
{
  if (xftface_info && face->gc == gc)
    {
      *fg = xftface_info->xft_fg;
      if (bg)
	*bg = xftface_info->xft_bg;
    }
  else
    {
      XGCValues xgcv;
      bool fg_done = false, bg_done = false;

      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 = true;
	  else if (xgcv.foreground == face->background)
	    *fg = xftface_info->xft_bg, fg_done = true;
	  if (! bg)
	    bg_done = true;
	  else if (xgcv.background == face->background)
	    *bg = xftface_info->xft_bg, bg_done = true;
	  else if (xgcv.background == face->foreground)
	    *bg = xftface_info->xft_fg, bg_done = true;
	}

      if (! (fg_done & bg_done))
	{
	  XColor colors[2];

	  colors[0].pixel = fg->pixel = xgcv.foreground;
	  if (bg)
	    colors[1].pixel = bg->pixel = xgcv.background;
	  x_query_colors (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 (struct frame *f, Lisp_Object spec)
{
  return ftfont_list2 (f, spec, Qxft);
}

static Lisp_Object
xftfont_match (struct frame *f, Lisp_Object spec)
{
  return ftfont_match2 (f, spec, Qxft);
}

static FcChar8 ascii_printable[95];

static Lisp_Object
xftfont_open (struct frame *f, Lisp_Object entity, int pixel_size)
{
  FcResult result;
  Display *display = FRAME_X_DISPLAY (f);
  Lisp_Object val, filename, font_object;
  FcPattern *pat = NULL, *match;
  struct font_info *xftfont_info = NULL;
  struct font *font;
  double size = 0;
  XftFont *xftfont = NULL;
  int spacing;
  XGlyphInfo extents;
  FT_Face ft_face;
  FcMatrix *matrix;

  val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
  if (! CONSP (val))
    return Qnil;
  val = XCDR (val);
  filename = XCAR (val);
  size = XFIXNUM (AREF (entity, FONT_SIZE_INDEX));
  if (size == 0)
    size = pixel_size;

  block_input ();

  pat = ftfont_entity_pattern (entity, pixel_size);
  /* Substitute in values from X resources and XftDefaultSet.  */
  XftDefaultSubstitute (display, FRAME_X_SCREEN_NUMBER (f), pat);
  match = XftFontMatch (display, FRAME_X_SCREEN_NUMBER (f), pat, &result);
  ftfont_fix_match (pat, match);

  FcPatternDestroy (pat);
  xftfont = XftFontOpenPattern (display, match);
  if (!xftfont)
    {
      unblock_input ();
      XftPatternDestroy (match);
      return Qnil;
    }
  ft_face = XftLockFace (xftfont);
  unblock_input ();

  /* We should not destroy PAT here because it is kept in XFTFONT and
     destroyed automatically when XFTFONT is closed.  */
  font_object = font_build_object (VECSIZE (struct font_info),
				   AREF (entity, FONT_TYPE_INDEX),
				   entity, size);
  ASET (font_object, FONT_FILE_INDEX, filename);
  font = XFONT_OBJECT (font_object);
  font->pixel_size = size;
#ifdef HAVE_HARFBUZZ
  if (EQ (AREF (font_object, FONT_TYPE_INDEX), Qxfthb))
    font->driver = &xfthbfont_driver;
  else
#endif	/* HAVE_HARFBUZZ */
  font->driver = &xftfont_driver;
  font->encoding_charset = font->repertory_charset = -1;

  xftfont_info = (struct font_info *) font;
  xftfont_info->display = display;
  xftfont_info->xftfont = xftfont;
  xftfont_info->x_display_id = FRAME_DISPLAY_INFO (f)->x_id;
  /* This means that there's no need of transformation.  */
  xftfont_info->matrix.xx = 0;
  if (FcPatternGetMatrix (xftfont->pattern, FC_MATRIX, 0, &matrix)
      == FcResultMatch)
    {
      xftfont_info->matrix.xx = 0x10000L * matrix->xx;
      xftfont_info->matrix.yy = 0x10000L * matrix->yy;
      xftfont_info->matrix.xy = 0x10000L * matrix->xy;
      xftfont_info->matrix.yx = 0x10000L * matrix->yx;
    }
  if (FIXNUMP (AREF (entity, FONT_SPACING_INDEX)))
    spacing = XFIXNUM (AREF (entity, FONT_SPACING_INDEX));
  else
    spacing = FC_PROPORTIONAL;
  if (! ascii_printable[0])
    {
      int ch;
      for (ch = 0; ch < 95; ch++)
	ascii_printable[ch] = ' ' + ch;
    }
  block_input ();

  /* Unfortunately Xft doesn't provide a way to get minimum char
     width.  So, we set min_width to space_width.  */

  if (spacing != FC_PROPORTIONAL
#ifdef FC_DUAL
      && spacing != FC_DUAL
#endif	/* FC_DUAL */
#ifdef FC_CHARCELL
      && spacing != FC_CHARCELL
#endif	/* FC_CHARCELL */
      )
    {
      font->min_width = font->max_width = font->average_width
	= font->space_width = xftfont->max_advance_width;
      XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents);
    }
  else
    {
      XftTextExtents8 (display, xftfont, ascii_printable, 1, &extents);
      font->min_width = font->max_width = font->space_width
	= extents.xOff;
      if (font->space_width <= 0)
	/* dirty workaround */
	font->space_width = pixel_size;
      XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents);
      font->average_width = (font->space_width + extents.xOff) / 95;
    }
  unblock_input ();

  font->ascent = xftfont->ascent;
  font->descent = xftfont->descent;
  /* The following workaround is unnecessary on most systems, and
     causes annoying differences in glyph height between regular and
     bold fonts (see bug#22383).  However, with some fonts, such as
     monaco, removing the workaround results in overlapping vertical
     space of a line, see bug#23360.  As long as the way to reconcile
     these opposites is not known, we provide a user option to work
     around the problem.  */
  if (pixel_size >= 5
      && xft_font_ascent_descent_override)
    {
      /* 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;
    }
  font->height = font->ascent + font->descent;

  if (XFIXNUM (AREF (entity, FONT_SIZE_INDEX)) == 0)
    {
      int upEM = ft_face->units_per_EM;

      font->underline_position = -ft_face->underline_position * size / upEM;
      font->underline_thickness = ft_face->underline_thickness * size / upEM;
      if (font->underline_thickness > 2)
	font->underline_position -= font->underline_thickness / 2;
    }
  else
    {
      font->underline_position = -1;
      font->underline_thickness = 0;
    }
#ifdef HAVE_LIBOTF
  xftfont_info->maybe_otf = (ft_face->face_flags & FT_FACE_FLAG_SFNT) != 0;
  xftfont_info->otf = NULL;
#endif	/* HAVE_LIBOTF */
#ifdef HAVE_HARFBUZZ
  xftfont_info->hb_font = NULL;
#endif	/* HAVE_HARFBUZZ */
  xftfont_info->ft_size = ft_face->size;

  font->baseline_offset = 0;
  font->relative_compose = 0;
  font->default_ascent = 0;
  font->vertical_centering = false;
#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

  return font_object;
}

static void
xftfont_close (struct font *font)
{
  struct x_display_info *xdi;
  struct font_info *xftfont_info = (struct font_info *) font;

#ifdef HAVE_LIBOTF
  if (xftfont_info->otf)
    {
      OTF_close (xftfont_info->otf);
      xftfont_info->otf = NULL;
    }
#endif
#ifdef HAVE_HARFBUZZ
  if (xftfont_info->hb_font)
    {
      hb_font_destroy (xftfont_info->hb_font);
      xftfont_info->hb_font = NULL;
    }
#endif

  /* See comment in xfont_close.  */
  if (xftfont_info->xftfont
      && ((xdi = x_display_info_for_display (xftfont_info->display))
	  && xftfont_info->x_display_id == xdi->x_id))
    {
      block_input ();
      XftUnlockFace (xftfont_info->xftfont);
      XftFontClose (xftfont_info->display, xftfont_info->xftfont);
      unblock_input ();
      xftfont_info->xftfont = NULL;
    }
}

static void
xftfont_prepare_face (struct frame *f, struct face *face)
{
  struct xftface_info *xftface_info;

#if false
  /* This doesn't work if face->ascii_face doesn't use an Xft font. */
  if (face != face->ascii_face)
    {
      face->extra = face->ascii_face->extra;
      return;
    }
#endif

  xftface_info = xmalloc (sizeof *xftface_info);
  xftfont_get_colors (f, face, face->gc, NULL,
		      &xftface_info->xft_fg, &xftface_info->xft_bg);
  face->extra = xftface_info;
}

static void
xftfont_done_face (struct frame *f, struct face *face)
{
  struct xftface_info *xftface_info;

#if false
  /* This doesn't work if face->ascii_face doesn't use an Xft font. */
  if (face != face->ascii_face
      || ! face->extra)
    return;
#endif

  xftface_info = (struct xftface_info *) face->extra;
  if (xftface_info)
    {
      xfree (xftface_info);
      face->extra = NULL;
    }
}

static int
xftfont_has_char (Lisp_Object font, int c)
{
  struct font_info *xftfont_info;
  struct charset *cs = NULL;

  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));

  if (FONT_ENTITY_P (font))
    return ftfont_has_char (font, c);
  xftfont_info = (struct font_info *) XFONT_OBJECT (font);
  return (XftCharExists (xftfont_info->display, xftfont_info->xftfont,
			 (FcChar32) c) == FcTrue);
}

static unsigned
xftfont_encode_char (struct font *font, int c)
{
  struct font_info *xftfont_info = (struct font_info *) font;
  unsigned code = XftCharIndex (xftfont_info->display, xftfont_info->xftfont,
				(FcChar32) c);

  return (code ? code : FONT_INVALID_CODE);
}

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

  block_input ();
  XftGlyphExtents (xftfont_info->display, xftfont_info->xftfont, code, nglyphs,
		   &extents);
  unblock_input ();

  metrics->lbearing = - extents.x;
  metrics->rbearing = - extents.x + extents.width;
  metrics->width = extents.xOff;
  metrics->ascent = extents.y;
  metrics->descent = extents.height - extents.y;
}

static XftDraw *
xftfont_get_xft_draw (struct frame *f)
{
  XftDraw *xft_draw = font_get_frame_data (f, Qxft);

  if (! xft_draw)
    {
      block_input ();
      xft_draw= XftDrawCreate (FRAME_X_DISPLAY (f),
                               FRAME_X_DRAWABLE (f),
			       FRAME_X_VISUAL (f),
			       FRAME_X_COLORMAP (f));
      unblock_input ();
      eassert (xft_draw != NULL);
      font_put_frame_data (f, Qxft, xft_draw);
    }
  return xft_draw;
}

static int
xftfont_draw (struct glyph_string *s, int from, int to, int x, int y,
              bool with_background)
{
  block_input ();

  struct frame *f = s->f;
  struct face *face = s->face;
  struct font_info *xftfont_info = (struct font_info *) s->font;
  struct xftface_info *xftface_info = NULL;
  XftDraw *xft_draw = xftfont_get_xft_draw (f);
  FT_UInt *code;
  XftColor fg, bg;
  int len = to - from;
  int i;

  if (s->font == face->font)
    xftface_info = (struct xftface_info *) face->extra;
  xftfont_get_colors (f, face, s->gc, xftface_info,
		      &fg, with_background ? &bg : NULL);
  if (s->num_clips > 0)
    XftDrawSetClipRectangles (xft_draw, 0, 0, s->clip, s->num_clips);
  else
    XftDrawSetClip (xft_draw, NULL);

  if (with_background)
    {
      int height = FONT_HEIGHT (s->font), ascent = FONT_BASE (s->font);

      /* Font's global height and ascent values might be
	 preposterously large for some fonts.  We fix here the case
	 when those fonts are used for display of glyphless
	 characters, because drawing background with font dimensions
	 in those cases makes the display illegible.  There's only one
	 more call to the draw method with with_background set to
	 true, and that's in x_draw_glyph_string_foreground, when
	 drawing the cursor, where we have no such heuristics
	 available.  FIXME.  */
      if (s->first_glyph->type == GLYPHLESS_GLYPH
	  && (s->first_glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE
	      || s->first_glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM))
	height = ascent =
	  s->first_glyph->slice.glyphless.lower_yoff
	  - s->first_glyph->slice.glyphless.upper_yoff;
      XftDrawRect (xft_draw, &bg, x, y - ascent, s->width, height);
    }
  code = alloca (sizeof (FT_UInt) * len);
  for (i = 0; i < len; i++)
    code[i] = s->char2b[from + i];

  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);
  /* Need to explicitly mark the frame dirty because we didn't call
     FRAME_X_DRAWABLE in order to draw: we cached the drawable in the
     XftDraw structure.  */
  x_mark_frame_dirty (f);
  unblock_input ();
  return len;
}

#if defined HAVE_M17N_FLT && defined HAVE_LIBOTF
static Lisp_Object
xftfont_shape (Lisp_Object lgstring, Lisp_Object direction)
{
  struct font *font = CHECK_FONT_GET_OBJECT (LGSTRING_FONT (lgstring));
  struct font_info *xftfont_info = (struct font_info *) font;
  FT_Face ft_face = XftLockFace (xftfont_info->xftfont);
  xftfont_info->ft_size = ft_face->size;
  Lisp_Object val = ftfont_shape (lgstring, direction);
  XftUnlockFace (xftfont_info->xftfont);
  return val;
}
#endif

static int
xftfont_end_for_frame (struct frame *f)
{
  block_input ();
  XftDraw *xft_draw;

  xft_draw = font_get_frame_data (f, Qxft);
  if (xft_draw)
    {
      block_input ();
      XftDrawDestroy (xft_draw);
      unblock_input ();
      font_put_frame_data (f, Qxft, NULL);
    }
  unblock_input ();
  return 0;
}

/* When using X double buffering, the XftDraw structure we build
   seems to be useless once a frame is resized, so recreate it on
   ConfigureNotify and in some other cases.  */

static void
xftfont_drop_xrender_surfaces (struct frame *f)
{
  block_input ();
  if (FRAME_X_DOUBLE_BUFFERED_P (f))
    xftfont_end_for_frame (f);
  unblock_input ();
}

static bool
xftfont_cached_font_ok (struct frame *f, Lisp_Object font_object,
                        Lisp_Object entity)
{
  struct font_info *info = (struct font_info *) XFONT_OBJECT (font_object);
  FcPattern *oldpat = info->xftfont->pattern;
  Display *display = FRAME_X_DISPLAY (f);
  FcPattern *pat = FcPatternCreate ();
  FcBool b1, b2;
  bool ok = false;
  int i1, i2, r1, r2;

  ftfont_add_rendering_parameters (pat, entity);
  XftDefaultSubstitute (display, FRAME_X_SCREEN_NUMBER (f), pat);

  r1 = FcPatternGetBool (pat, FC_ANTIALIAS, 0, &b1);
  r2 = FcPatternGetBool (oldpat, FC_ANTIALIAS, 0, &b2);
  if (r1 != r2 || b1 != b2) goto out;
  r1 = FcPatternGetBool (pat, FC_HINTING, 0, &b1);
  r2 = FcPatternGetBool (oldpat, FC_HINTING, 0, &b2);
  if (r1 != r2 || b1 != b2) goto out;
  r1 = FcPatternGetBool (pat, FC_AUTOHINT, 0, &b1);
  r2 = FcPatternGetBool (oldpat, FC_AUTOHINT, 0, &b2);
  if (r1 != r2 || b1 != b2) goto out;
#ifdef FC_EMBOLDEN
  r1 = FcPatternGetBool (pat, FC_EMBOLDEN, 0, &b1);
  r2 = FcPatternGetBool (oldpat, FC_EMBOLDEN, 0, &b2);
  if (r1 != r2 || b1 != b2) goto out;
#endif
  r1 = FcPatternGetInteger (pat, FC_HINT_STYLE, 0, &i1);
  r2 = FcPatternGetInteger (oldpat, FC_HINT_STYLE, 0, &i2);
  if (r1 != r2 || i1 != i2) goto out;
  r1 = FcPatternGetInteger (pat, FC_LCD_FILTER, 0, &i1);
  r2 = FcPatternGetInteger (oldpat, FC_LCD_FILTER, 0, &i2);
  if (r1 != r2 || i1 != i2) goto out;
  r1 = FcPatternGetInteger (pat, FC_RGBA, 0, &i1);
  r2 = FcPatternGetInteger (oldpat, FC_RGBA, 0, &i2);
  if (r1 != r2 || i1 != i2) goto out;

  ok = true;
 out:
  FcPatternDestroy (pat);
  return ok;
}

#ifdef HAVE_HARFBUZZ

static Lisp_Object
xfthbfont_list (struct frame *f, Lisp_Object spec)
{
  return ftfont_list2 (f, spec, Qxfthb);
}

static Lisp_Object
xfthbfont_match (struct frame *f, Lisp_Object spec)
{
  return ftfont_match2 (f, spec, Qxfthb);
}

static hb_font_t *
xfthbfont_begin_hb_font (struct font *font, double *position_unit)
{
  struct font_info *xftfont_info = (struct font_info *) font;
  FT_Face ft_face = XftLockFace (xftfont_info->xftfont);

  xftfont_info->ft_size = ft_face->size;

  return fthbfont_begin_hb_font (font, position_unit);
}

static void
xfthbfont_end_hb_font (struct font *font, hb_font_t *hb_font)
{
  struct font_info *xftfont_info = (struct font_info *) font;

  XftUnlockFace (xftfont_info->xftfont);
}

#endif	/* HAVE_HARFBUZZ */

static void syms_of_xftfont_for_pdumper (void);

struct font_driver const xftfont_driver =
  {
    /* We can't draw a text without device dependent functions.  */
  .type = LISPSYM_INITIALLY (Qxft),
  .get_cache = xfont_get_cache,
  .list = xftfont_list,
  .match = xftfont_match,
  .list_family = ftfont_list_family,
  .open_font = xftfont_open,
  .close_font = xftfont_close,
  .prepare_face = xftfont_prepare_face,
  .done_face = xftfont_done_face,
  .has_char = xftfont_has_char,
  .encode_char = xftfont_encode_char,
  .text_extents = xftfont_text_extents,
  .draw = xftfont_draw,
  .get_bitmap = ftfont_get_bitmap,
  .anchor_point = ftfont_anchor_point,
#ifdef HAVE_LIBOTF
  .otf_capability = ftfont_otf_capability,
#endif
  .end_for_frame = xftfont_end_for_frame,
#if defined HAVE_M17N_FLT && defined HAVE_LIBOTF
  .shape = xftfont_shape,
#endif
#if defined HAVE_OTF_GET_VARIATION_GLYPHS || defined HAVE_FT_FACE_GETCHARVARIANTINDEX
  .get_variation_glyphs = ftfont_variation_glyphs,
#endif
  .filter_properties = ftfont_filter_properties,
  .cached_font_ok = xftfont_cached_font_ok,
  .combining_capability = ftfont_combining_capability,
  .drop_xrender_surfaces = xftfont_drop_xrender_surfaces,
  };
#ifdef HAVE_HARFBUZZ
struct font_driver xfthbfont_driver;
#endif	/* HAVE_HARFBUZZ */

void
syms_of_xftfont (void)
{
  DEFSYM (Qxft, "xft");
#ifdef HAVE_HARFBUZZ
  DEFSYM (Qxfthb, "xfthb");
  Fput (Qxft, Qfont_driver_superseded_by, Qxfthb);
#endif	/* HAVE_HARFBUZZ */

  DEFVAR_BOOL ("xft-font-ascent-descent-override",
	       xft_font_ascent_descent_override,
	       doc:  /* Non-nil means override the ascent and descent values for Xft font driver.
This is needed with some fonts to correct vertical overlap of glyphs.  */);
  xft_font_ascent_descent_override = 0;

  pdumper_do_now_and_after_load (syms_of_xftfont_for_pdumper);
}

static void
syms_of_xftfont_for_pdumper (void)
{
  register_font_driver (&xftfont_driver, NULL);
#ifdef HAVE_HARFBUZZ
  xfthbfont_driver = xftfont_driver;
  xfthbfont_driver.type = Qxfthb;
  xfthbfont_driver.list = xfthbfont_list;
  xfthbfont_driver.match = xfthbfont_match;
  xfthbfont_driver.otf_capability = hbfont_otf_capability;
  xfthbfont_driver.shape = hbfont_shape;
  xfthbfont_driver.combining_capability = hbfont_combining_capability;
  xfthbfont_driver.begin_hb_font = xfthbfont_begin_hb_font;
  xfthbfont_driver.end_hb_font = xfthbfont_end_hb_font;
  register_font_driver (&xfthbfont_driver, NULL);
#endif	/* HAVE_HARFBUZZ */
}