ftfont.c 84.3 KB
Newer Older
Kenichi Handa's avatar
Kenichi Handa committed
1
/* ftfont.c -- FreeType font driver.
Paul Eggert's avatar
Paul Eggert committed
2
   Copyright (C) 2006-2020 Free Software Foundation, Inc.
3
   Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
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 <https://www.gnu.org/licenses/>.  */
Kenichi Handa's avatar
Kenichi Handa committed
21 22 23 24 25

#include <config.h>
#include <fontconfig/fontconfig.h>
#include <fontconfig/fcfreetype.h>

26 27 28 29 30 31 32 33 34 35 36
/* These two blocks are here because this file is built when using XFT
   and when using Cairo, so struct font_info in ftfont.h needs access
   to the appropriate types.  */
#ifdef HAVE_XFT
# include <X11/Xlib.h>
# include <X11/Xft/Xft.h>
#endif
#ifdef USE_CAIRO
# include <cairo-ft.h>
#endif

37 38
#include <c-strcase.h>

Kenichi Handa's avatar
Kenichi Handa committed
39 40 41 42
#include "lisp.h"
#include "dispextern.h"
#include "character.h"
#include "charset.h"
43
#include "category.h"
Kenichi Handa's avatar
Kenichi Handa committed
44
#include "composite.h"
Kenichi Handa's avatar
Kenichi Handa committed
45
#include "font.h"
46
#include "ftfont.h"
Daniel Colascione's avatar
Daniel Colascione committed
47
#include "pdumper.h"
Kenichi Handa's avatar
Kenichi Handa committed
48

49
static struct font_driver const ftfont_driver;
50 51 52
#ifdef HAVE_HARFBUZZ
static struct font_driver fthbfont_driver;
#endif	/* HAVE_HARFBUZZ */
53

Juanma Barranquero's avatar
Juanma Barranquero committed
54
/* Flag to tell if FcInit is already called or not.  */
55
static bool fc_initialized;
56 57

/* Handle to a FreeType library instance.  */
Kenichi Handa's avatar
Kenichi Handa committed
58 59
static FT_Library ft_library;

60
/* Cache for FreeType fonts.  */
Kenichi Handa's avatar
Kenichi Handa committed
61 62
static Lisp_Object freetype_font_cache;

63
/* Cache for FT_Face and FcCharSet. */
Kenichi Handa's avatar
Kenichi Handa committed
64
static Lisp_Object ft_face_cache;
Kenichi Handa's avatar
Kenichi Handa committed
65

66 67 68 69 70 71 72
enum ftfont_cache_for
  {
    FTFONT_CACHE_FOR_FACE,
    FTFONT_CACHE_FOR_CHARSET,
    FTFONT_CACHE_FOR_ENTITY
  };

73 74
static Lisp_Object ftfont_lookup_cache (Lisp_Object,
                                        enum ftfont_cache_for);
75

76 77
#define SYMBOL_FcChar8(SYM) (FcChar8 *) SDATA (SYMBOL_NAME (SYM))

Kenichi Handa's avatar
Kenichi Handa committed
78
static struct
Kenichi Handa's avatar
Kenichi Handa committed
79
{
80
  /* registry name */
81
  const char *name;
Kenichi Handa's avatar
Kenichi Handa committed
82 83
  /* characters to distinguish the charset from the others */
  int uniquifier[6];
84
  /* additional constraint by language */
85
  const char *lang;
Kenichi Handa's avatar
Kenichi Handa committed
86 87 88
  /* set on demand */
  FcCharSet *fc_charset;
} fc_charset_table[] =
89
  { { "iso8859-1", { 0x00A0, 0x00A1, 0x00B4, 0x00BC, 0x00D0 } },
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    { "iso8859-2", { 0x00A0, 0x010E }},
    { "iso8859-3", { 0x00A0, 0x0108 }},
    { "iso8859-4", { 0x00A0, 0x00AF, 0x0128, 0x0156, 0x02C7 }},
    { "iso8859-5", { 0x00A0, 0x0401 }},
    { "iso8859-6", { 0x00A0, 0x060C }},
    { "iso8859-7", { 0x00A0, 0x0384 }},
    { "iso8859-8", { 0x00A0, 0x05D0 }},
    { "iso8859-9", { 0x00A0, 0x00A1, 0x00BC, 0x011E }},
    { "iso8859-10", { 0x00A0, 0x00D0, 0x0128, 0x2015 }},
    { "iso8859-11", { 0x00A0, 0x0E01 }},
    { "iso8859-13", { 0x00A0, 0x201C }},
    { "iso8859-14", { 0x00A0, 0x0174 }},
    { "iso8859-15", { 0x00A0, 0x00A1, 0x00D0, 0x0152 }},
    { "iso8859-16", { 0x00A0, 0x0218}},
    { "gb2312.1980-0", { 0x4E13 }, "zh-cn"},
105
    { "big5-0", { 0x9C21 }, "zh-tw" },
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    { "jisx0208.1983-0", { 0x4E55 }, "ja"},
    { "ksc5601.1985-0", { 0xAC00 }, "ko"},
    { "cns11643.1992-1", { 0xFE32 }, "zh-tw"},
    { "cns11643.1992-2", { 0x4E33, 0x7934 }},
    { "cns11643.1992-3", { 0x201A9 }},
    { "cns11643.1992-4", { 0x20057 }},
    { "cns11643.1992-5", { 0x20000 }},
    { "cns11643.1992-6", { 0x20003 }},
    { "cns11643.1992-7", { 0x20055 }},
    { "gbk-0", { 0x4E06 }, "zh-cn"},
    { "jisx0212.1990-0", { 0x4E44 }},
    { "jisx0213.2000-1", { 0xFA10 }, "ja"},
    { "jisx0213.2000-2", { 0xFA49 }},
    { "jisx0213.2004-1", { 0x20B9F }},
    { "viscii1.1-1", { 0x1EA0, 0x1EAE, 0x1ED2 }, "vi"},
    { "tis620.2529-1", { 0x0E01 }, "th"},
122
    { "microsoft-cp1251", { 0x0401, 0x0490 }, "ru"},
123
    { "koi8-r", { 0x0401, 0x2219 }, "ru"},
124
    { "mulelao-1", { 0x0E81 }, "lo"},
125
    { "unicode-sip", { 0x20000 }},
Kenichi Handa's avatar
Kenichi Handa committed
126 127
    { NULL }
  };
Kenichi Handa's avatar
Kenichi Handa committed
128

129 130 131 132 133 134
static bool
matching_prefix (char const *str, ptrdiff_t len, char const *pat)
{
  return len == strlen (pat) && c_strncasecmp (str, pat, len) == 0;
}

Kenichi Handa's avatar
Kenichi Handa committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/* Dirty hack for handing ADSTYLE property.

   Fontconfig (actually the underlying FreeType) gives such ADSTYLE
   font property of PCF/BDF fonts in FC_STYLE.  And, "Bold",
   "Oblique", "Italic", or any non-normal SWIDTH property names
   (e.g. SemiCondensed) are appended.  In addition, if there's no
   ADSTYLE property nor non-normal WEIGHT/SLANT/SWIDTH properties,
   "Regular" is used for FC_STYLE (see the function
   pcf_interpret_style in src/pcf/pcfread.c of FreeType).

   Unfortunately this behavior is not documented, so the following
   code may fail if FreeType changes the behavior in the future.  */

static Lisp_Object
get_adstyle_property (FcPattern *p)
{
151 152
  FcChar8 *fcstr;
  char *str, *end;
Kenichi Handa's avatar
Kenichi Handa committed
153 154
  Lisp_Object adstyle;

155 156
#ifdef FC_FONTFORMAT
  if ((FcPatternGetString (p, FC_FONTFORMAT, 0, &fcstr) == FcResultMatch)
157 158
      && xstrcasecmp ((char *) fcstr, "bdf") != 0
      && xstrcasecmp ((char *) fcstr, "pcf") != 0)
159 160 161
    /* Not a BDF nor PCF font.  */
    return Qnil;
#endif
162
  if (FcPatternGetString (p, FC_STYLE, 0, &fcstr) != FcResultMatch)
Kenichi Handa's avatar
Kenichi Handa committed
163
    return Qnil;
164
  str = (char *) fcstr;
Kenichi Handa's avatar
Kenichi Handa committed
165
  for (end = str; *end && *end != ' '; end++);
166 167 168 169
  if (matching_prefix (str, end - str, "Regular")
      || matching_prefix (str, end - str, "Bold")
      || matching_prefix (str, end - str, "Oblique")
      || matching_prefix (str, end - str, "Italic"))
Kenichi Handa's avatar
Kenichi Handa committed
170
    return Qnil;
171
  adstyle = font_intern_prop (str, end - str, 1);
Kenichi Handa's avatar
Kenichi Handa committed
172 173 174 175 176
  if (font_style_to_value (FONT_WIDTH_INDEX, adstyle, 0) >= 0)
    return Qnil;
  return adstyle;
}

177
static Lisp_Object
178
ftfont_pattern_entity (FcPattern *p, Lisp_Object extra)
179
{
180
  Lisp_Object key, cache, entity;
181 182
  FcChar8 *str;
  char *file;
183
  int idx;
184 185
  int numeric;
  double dbl;
186
  FcBool b;
187

188
  if (FcPatternGetString (p, FC_FILE, 0, &str) != FcResultMatch)
189
    return Qnil;
190
  if (FcPatternGetInteger (p, FC_INDEX, 0, &idx) != FcResultMatch)
191
    return Qnil;
192

193
  file = (char *) str;
194
  key = Fcons (build_unibyte_string (file), make_fixnum (idx));
195 196 197
  cache = ftfont_lookup_cache (key, FTFONT_CACHE_FOR_ENTITY);
  entity = XCAR (cache);
  if (! NILP (entity))
198 199 200 201 202 203
    {
      Lisp_Object val = font_make_entity ();
      int i;

      for (i = 0; i < FONT_OBJLIST_INDEX; i++)
	ASET (val, i, AREF (entity, i));
204 205 206 207

      ASET (val, FONT_EXTRA_INDEX, Fcopy_sequence (extra));
      font_put_extra (val, QCfont_entity, key);

208 209
      return val;
    }
210
  entity = font_make_entity ();
211
  XSETCAR (cache, entity);
212 213

  ASET (entity, FONT_TYPE_INDEX, Qfreetype);
214
  ASET (entity, FONT_REGISTRY_INDEX, Qiso10646_1);
215

216 217 218 219 220 221 222 223 224 225
  if (FcPatternGetString (p, FC_FOUNDRY, 0, &str) == FcResultMatch)
    {
      char *s = (char *) str;
      ASET (entity, FONT_FOUNDRY_INDEX, font_intern_prop (s, strlen (s), 1));
    }
  if (FcPatternGetString (p, FC_FAMILY, 0, &str) == FcResultMatch)
    {
      char *s = (char *) str;
      ASET (entity, FONT_FAMILY_INDEX, font_intern_prop (s, strlen (s), 1));
    }
226
  if (FcPatternGetInteger (p, FC_WEIGHT, 0, &numeric) == FcResultMatch)
227
    {
228 229
      if (numeric >= FC_WEIGHT_REGULAR && numeric < FC_WEIGHT_MEDIUM)
	numeric = FC_WEIGHT_MEDIUM;
230
      FONT_SET_STYLE (entity, FONT_WEIGHT_INDEX, make_fixnum (numeric));
231
    }
232
  if (FcPatternGetInteger (p, FC_SLANT, 0, &numeric) == FcResultMatch)
233 234
    {
      numeric += 100;
235
      FONT_SET_STYLE (entity, FONT_SLANT_INDEX, make_fixnum (numeric));
236
    }
237
  if (FcPatternGetInteger (p, FC_WIDTH, 0, &numeric) == FcResultMatch)
238
    {
239
      FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_fixnum (numeric));
240
    }
241
  if (FcPatternGetDouble (p, FC_PIXEL_SIZE, 0, &dbl) == FcResultMatch)
Kenichi Handa's avatar
Kenichi Handa committed
242
    {
243
      ASET (entity, FONT_SIZE_INDEX, make_fixnum (dbl));
Kenichi Handa's avatar
Kenichi Handa committed
244
    }
245
  else
246
    ASET (entity, FONT_SIZE_INDEX, make_fixnum (0));
247
  if (FcPatternGetInteger (p, FC_SPACING, 0, &numeric) == FcResultMatch)
248
    ASET (entity, FONT_SPACING_INDEX, make_fixnum (numeric));
249 250 251
  if (FcPatternGetDouble (p, FC_DPI, 0, &dbl) == FcResultMatch)
    {
      int dpi = dbl;
252
      ASET (entity, FONT_DPI_INDEX, make_fixnum (dpi));
253 254 255
    }
  if (FcPatternGetBool (p, FC_SCALABLE, 0, &b) == FcResultMatch
      && b == FcTrue)
256
    {
257 258
      ASET (entity, FONT_SIZE_INDEX, make_fixnum (0));
      ASET (entity, FONT_AVGWIDTH_INDEX, make_fixnum (0));
259 260 261
    }
  else
    {
Paul Eggert's avatar
Paul Eggert committed
262
      /* As this font is not scalable, perhaps this is a BDF or PCF
263
	 font. */
264 265 266 267
      FT_Face ft_face;

      ASET (entity, FONT_ADSTYLE_INDEX, get_adstyle_property (p));
      if ((ft_library || FT_Init_FreeType (&ft_library) == 0)
268
	  && FT_New_Face (ft_library, file, idx, &ft_face) == 0)
269 270 271 272 273
	{
	  BDF_PropertyRec rec;

	  if (FT_Get_BDF_Property (ft_face, "AVERAGE_WIDTH", &rec) == 0
	      && rec.type == BDF_PROPERTY_TYPE_INTEGER)
274
	    ASET (entity, FONT_AVGWIDTH_INDEX, make_fixnum (rec.u.integer));
275 276 277
	  FT_Done_Face (ft_face);
	}
    }
278

279
  ASET (entity, FONT_EXTRA_INDEX, Fcopy_sequence (extra));
280
  font_put_extra (entity, QCfont_entity, key);
281 282 283
  return entity;
}

284

285 286 287
static Lisp_Object ftfont_generic_family_list;

static Lisp_Object
288
ftfont_resolve_generic_family (Lisp_Object family, FcPattern *pattern)
289
{
290
  Lisp_Object slot;
291
  FcPattern *match;
292
  FcResult result;
Kenichi Handa's avatar
Kenichi Handa committed
293
  FcLangSet *langset;
294

295
  family = Fintern (Fdowncase (SYMBOL_NAME (family)), Qnil);
296 297 298 299 300 301
  if (EQ (family, Qmono))
    family = Qmonospace;
  else if (EQ (family, Qsans) || EQ (family, Qsans__serif))
    family = Qsans_serif;
  slot = assq_no_quit (family, ftfont_generic_family_list);
  if (! CONSP (slot))
302
    return Qnil;
303 304
  if (! EQ (XCDR (slot), Qt))
    return XCDR (slot);
305
  pattern = FcPatternDuplicate (pattern);
306 307
  if (! pattern)
    goto err;
308
  FcPatternDel (pattern, FC_FOUNDRY);
309 310
  FcPatternDel (pattern, FC_FAMILY);
  FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (family));
Kenichi Handa's avatar
Kenichi Handa committed
311 312 313
  if (FcPatternGetLangSet (pattern, FC_LANG, 0, &langset) != FcResultMatch)
    {
      /* This is to avoid the effect of locale.  */
314
      static const FcChar8 lang[] = "en";
Kenichi Handa's avatar
Kenichi Handa committed
315
      langset = FcLangSetCreate ();
316
      FcLangSetAdd (langset, lang);
Kenichi Handa's avatar
Kenichi Handa committed
317 318 319
      FcPatternAddLangSet (pattern, FC_LANG, langset);
      FcLangSetDestroy (langset);
    }
320
  FcConfigSubstitute (NULL, pattern, FcMatchPattern);
321 322 323
  FcDefaultSubstitute (pattern);
  match = FcFontMatch (NULL, pattern, &result);
  if (match)
324
    {
325 326 327 328
      FcChar8 *fam;

      if (FcPatternGetString (match, FC_FAMILY, 0, &fam) == FcResultMatch)
	family = intern ((char *) fam);
329
    }
330 331 332 333
  else
    family = Qnil;
  XSETCDR (slot, family);
  if (match) FcPatternDestroy (match);
334
 err:
335
  if (pattern) FcPatternDestroy (pattern);
336
  return family;
337 338
}

339 340 341 342
struct ftfont_cache_data
{
  FT_Face ft_face;
  FcCharSet *fc_charset;
343
  intptr_t face_refcount;
344 345 346
};

static Lisp_Object
347
ftfont_lookup_cache (Lisp_Object key, enum ftfont_cache_for cache_for)
Kenichi Handa's avatar
Kenichi Handa committed
348
{
349
  Lisp_Object cache, val;
350
  struct ftfont_cache_data *cache_data;
Kenichi Handa's avatar
Kenichi Handa committed
351

Kenichi Handa's avatar
Kenichi Handa committed
352 353
  if (FONT_ENTITY_P (key))
    {
354
      val = assq_no_quit (QCfont_entity, AREF (key, FONT_EXTRA_INDEX));
355
      eassert (CONSP (val));
Kenichi Handa's avatar
Kenichi Handa committed
356 357 358
      key = XCDR (val);
    }

359 360 361 362
  if (NILP (ft_face_cache))
    cache = Qnil;
  else
    cache = Fgethash (key, ft_face_cache, Qnil);
Kenichi Handa's avatar
Kenichi Handa committed
363 364
  if (NILP (cache))
    {
365
      if (NILP (ft_face_cache))
366
	ft_face_cache = CALLN (Fmake_hash_table, QCtest, Qequal);
367 368
      cache_data = xzalloc (sizeof *cache_data);
      val = make_mint_ptr (cache_data);
369 370
      cache = Fcons (Qnil, val);
      Fputhash (key, cache, ft_face_cache);
Kenichi Handa's avatar
Kenichi Handa committed
371 372 373
    }
  else
    {
374
      val = XCDR (cache);
375
      cache_data = xmint_pointer (val);
376
    }
377 378 379 380 381 382

  if (cache_for == FTFONT_CACHE_FOR_ENTITY)
    return cache;

  if (cache_for == FTFONT_CACHE_FOR_FACE
      ? ! cache_data->ft_face : ! cache_data->fc_charset)
383
    {
384
      char *filename = SSDATA (XCAR (key));
Tom Tromey's avatar
Tom Tromey committed
385
      int idx = XFIXNUM (XCDR (key));
Kenichi Handa's avatar
Kenichi Handa committed
386

387
      if (cache_for == FTFONT_CACHE_FOR_FACE)
388 389 390 391
	{
	  if (! ft_library
	      && FT_Init_FreeType (&ft_library) != 0)
	    return Qnil;
392
	  if (FT_New_Face (ft_library, filename, idx, &cache_data->ft_face)
393 394 395 396 397
	      != 0)
	    return Qnil;
	}
      else
	{
Kenichi Handa's avatar
Kenichi Handa committed
398 399 400 401
	  FcPattern *pat = NULL;
	  FcFontSet *fontset = NULL;
	  FcObjectSet *objset = NULL;
	  FcCharSet *charset = NULL;
402 403

	  pat = FcPatternBuild (0, FC_FILE, FcTypeString, (FcChar8 *) filename,
404
				FC_INDEX, FcTypeInteger, idx, NULL);
Kenichi Handa's avatar
Kenichi Handa committed
405 406 407 408 409
	  if (! pat)
	    goto finish;
	  objset = FcObjectSetBuild (FC_CHARSET, FC_STYLE, NULL);
	  if (! objset)
	    goto finish;
410
	  fontset = FcFontList (NULL, pat, objset);
Kenichi Handa's avatar
Kenichi Handa committed
411 412
	  if (! fontset)
	    goto finish;
413 414
	  if (fontset && fontset->nfont > 0
	      && (FcPatternGetCharSet (fontset->fonts[0], FC_CHARSET, 0,
415
				       &charset)
416 417
		  == FcResultMatch))
	    cache_data->fc_charset = FcCharSetCopy (charset);
418 419
	  else
	    cache_data->fc_charset = FcCharSetCreate ();
Kenichi Handa's avatar
Kenichi Handa committed
420 421 422 423 424 425 426 427

	finish:
	  if (fontset)
	    FcFontSetDestroy (fontset);
	  if (objset)
	    FcObjectSetDestroy (objset);
	  if (pat)
	    FcPatternDestroy (pat);
428
	}
Kenichi Handa's avatar
Kenichi Handa committed
429 430 431 432
    }
  return cache;
}

Paul Eggert's avatar
Paul Eggert committed
433
static FcCharSet *
434
ftfont_get_fc_charset (Lisp_Object entity)
435 436 437 438
{
  Lisp_Object val, cache;
  struct ftfont_cache_data *cache_data;

439
  cache = ftfont_lookup_cache (entity, FTFONT_CACHE_FOR_CHARSET);
440
  val = XCDR (cache);
441
  cache_data = xmint_pointer (val);
442 443 444 445 446
  return cache_data->fc_charset;
}

#ifdef HAVE_LIBOTF
static OTF *
447
ftfont_get_otf (struct font_info *ftfont_info)
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
{
  OTF *otf;

  if (ftfont_info->otf)
    return ftfont_info->otf;
  if (! ftfont_info->maybe_otf)
    return NULL;
  otf = OTF_open_ft_face (ftfont_info->ft_size->face);
  if (! otf || OTF_get_table (otf, "head") < 0)
    {
      if (otf)
	OTF_close (otf);
      ftfont_info->maybe_otf = 0;
      return NULL;
    }
  ftfont_info->otf = otf;
  return otf;
}
466
#endif	/* HAVE_LIBOTF */
467

468
Lisp_Object
Dmitry Antipov's avatar
Dmitry Antipov committed
469
ftfont_get_cache (struct frame *f)
Kenichi Handa's avatar
Kenichi Handa committed
470 471 472 473
{
  return freetype_font_cache;
}

Kenichi Handa's avatar
Kenichi Handa committed
474
static int
475
ftfont_get_charset (Lisp_Object registry)
Kenichi Handa's avatar
Kenichi Handa committed
476
{
477
  char *str = SSDATA (SYMBOL_NAME (registry));
478 479
  USE_SAFE_ALLOCA;
  char *re = SAFE_ALLOCA (SBYTES (SYMBOL_NAME (registry)) * 2 + 1);
Kenichi Handa's avatar
Kenichi Handa committed
480 481
  int i, j;

482 483 484 485 486 487 488 489 490 491 492
  for (i = j = 0; i < SBYTES (SYMBOL_NAME (registry)); i++, j++)
    {
      if (str[i] == '.')
	re[j++] = '\\';
      else if (str[i] == '*')
	re[j++] = '.';
      re[j] = str[i];
      if (re[j] == '?')
	re[j] = '.';
    }
  re[j] = '\0';
Paul Eggert's avatar
Paul Eggert committed
493
  AUTO_STRING_WITH_LEN (regexp, re, j);
Kenichi Handa's avatar
Kenichi Handa committed
494
  for (i = 0; fc_charset_table[i].name; i++)
495 496 497
    if (fast_c_string_match_ignore_case
	(regexp, fc_charset_table[i].name,
	 strlen (fc_charset_table[i].name)) >= 0)
Kenichi Handa's avatar
Kenichi Handa committed
498
      break;
Paul Eggert's avatar
Paul Eggert committed
499
  SAFE_FREE ();
Kenichi Handa's avatar
Kenichi Handa committed
500 501 502 503
  if (! fc_charset_table[i].name)
    return -1;
  if (! fc_charset_table[i].fc_charset)
    {
504 505
      FcCharSet *charset = FcCharSetCreate ();
      int *uniquifier = fc_charset_table[i].uniquifier;
Kenichi Handa's avatar
Kenichi Handa committed
506

507 508 509 510 511 512
      if (! charset)
	return -1;
      for (j = 0; uniquifier[j]; j++)
	if (! FcCharSetAddChar (charset, uniquifier[j]))
	  {
	    FcCharSetDestroy (charset);
Kenichi Handa's avatar
Kenichi Handa committed
513
	    return -1;
514 515
	  }
      fc_charset_table[i].fc_charset = charset;
Kenichi Handa's avatar
Kenichi Handa committed
516 517 518 519
    }
  return i;
}

520 521
struct OpenTypeSpec
{
522 523
  Lisp_Object script;
  unsigned int script_tag, langsys_tag;
524 525 526 527
  int nfeatures[2];
  unsigned int *features[2];
};

528
#define OTF_SYM_TAG(SYM, TAG)					\
529
  do {								\
530 531
    unsigned char *p = SDATA (SYMBOL_NAME (SYM));		\
    TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];	\
532 533
  } while (0)

534
#define OTF_TAG_STR(TAG, P)			\
535
  do {						\
536 537 538 539
    (P)[0] = (char) (TAG >> 24);		\
    (P)[1] = (char) ((TAG >> 16) & 0xFF);	\
    (P)[2] = (char) ((TAG >> 8) & 0xFF);	\
    (P)[3] = (char) (TAG & 0xFF);		\
540
    (P)[4] = '\0';				\
541 542
  } while (0)

543
#ifdef HAVE_LIBOTF
544 545 546 547 548 549 550
#define OTF_TAG_SYM(SYM, TAG)			\
  do {						\
    char str[5];				\
    						\
    OTF_TAG_STR (TAG, str);			\
    (SYM) = font_intern_prop (str, 4, 1);	\
  } while (0)
551 552 553 554 555 556 557 558 559 560 561 562 563
#elif defined HAVE_HARFBUZZ
/* Libotf emulations on HarfBuzz for the functions called from
   ftfont_list.  They are a bit slower than the original ones, so used
   as fallbacks when libotf is not available.  */
typedef hb_face_t OTF;
typedef unsigned int OTF_tag;
static OTF *hbotf_open (const char *);
static int hbotf_check_features (OTF *, int, OTF_tag, OTF_tag,
				 const OTF_tag *, int);
#define OTF_open hbotf_open
#define OTF_close hb_face_destroy
#define OTF_check_features hbotf_check_features
#endif	/* !HAVE_LIBOTF && HAVE_HARFBUZZ */
564 565


566 567 568
static struct OpenTypeSpec *
ftfont_get_open_type_spec (Lisp_Object otf_spec)
{
569
  struct OpenTypeSpec *spec = malloc (sizeof *spec);
570
  Lisp_Object val;
571 572
  int i, j;
  bool negative;
573 574 575

  if (! spec)
    return NULL;
576
  spec->script = XCAR (otf_spec);
577
  if (! NILP (spec->script))
578 579 580 581 582 583 584 585
    {
      OTF_SYM_TAG (spec->script, spec->script_tag);
      val = assq_no_quit (spec->script, Votf_script_alist);
      if (CONSP (val) && SYMBOLP (XCDR (val)))
	spec->script = XCDR (val);
      else
	spec->script = Qnil;
    }
586
  else
587
    spec->script_tag = 0x44464C54; 	/* "DFLT" */
588
  otf_spec = XCDR (otf_spec);
Kenichi Handa's avatar
Kenichi Handa committed
589 590 591 592 593 594 595 596
  spec->langsys_tag = 0;
  if (! NILP (otf_spec))
    {
      val = XCAR (otf_spec);
      if (! NILP (val))
	OTF_SYM_TAG (val, spec->langsys_tag);
      otf_spec = XCDR (otf_spec);
    }
597
  spec->nfeatures[0] = spec->nfeatures[1] = 0;
Kenichi Handa's avatar
Kenichi Handa committed
598
  for (i = 0; i < 2 && ! NILP (otf_spec); i++, otf_spec = XCDR (otf_spec))
599 600 601 602
    {
      val = XCAR (otf_spec);
      if (NILP (val))
	continue;
Paul Eggert's avatar
Paul Eggert committed
603
      ptrdiff_t len = list_length (val);
604
      spec->features[i] =
Paul Eggert's avatar
Paul Eggert committed
605
	(min (PTRDIFF_MAX, SIZE_MAX) / sizeof (int) < len
606
	 ? 0
Paul Eggert's avatar
Paul Eggert committed
607
	 : malloc (len * sizeof *spec->features[i]));
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
      if (! spec->features[i])
	{
	  if (i > 0 && spec->features[0])
	    free (spec->features[0]);
	  free (spec);
	  return NULL;
	}
      for (j = 0, negative = 0; CONSP (val); val = XCDR (val))
	{
	  if (NILP (XCAR (val)))
	    negative = 1;
	  else
	    {
	      unsigned int tag;

	      OTF_SYM_TAG (XCAR (val), tag);
624
	      spec->features[i][j++] = negative ? tag | 0x80000000 : tag;
625 626 627 628 629 630 631
	    }
	}
      spec->nfeatures[i] = j;
    }
  return spec;
}

632
static FcPattern *
633
ftfont_spec_pattern (Lisp_Object spec, char *otlayout, struct OpenTypeSpec **otspec, const char **langname)
Kenichi Handa's avatar
Kenichi Handa committed
634
{
Kenichi Handa's avatar
Kenichi Handa committed
635
  Lisp_Object tmp, extra;
Kenichi Handa's avatar
Kenichi Handa committed
636 637 638
  FcPattern *pattern = NULL;
  FcCharSet *charset = NULL;
  FcLangSet *langset = NULL;
639 640
  int n;
  int dpi = -1;
641
  int scalable = -1;
642
  Lisp_Object script = Qnil;
Kenichi Handa's avatar
Kenichi Handa committed
643
  Lisp_Object registry;
644
  int fc_charset_idx;
Kenichi Handa's avatar
Kenichi Handa committed
645

646 647
  if ((n = FONT_SLANT_NUMERIC (spec)) >= 0
      && n < 100)
Paul Eggert's avatar
Paul Eggert committed
648
    /* Fontconfig doesn't support reverse-italic/oblique.  */
649 650
    return NULL;

651
  if (FIXNUMP (AREF (spec, FONT_DPI_INDEX)))
Tom Tromey's avatar
Tom Tromey committed
652
    dpi = XFIXNUM (AREF (spec, FONT_DPI_INDEX));
653
  if (FIXNUMP (AREF (spec, FONT_AVGWIDTH_INDEX))
Tom Tromey's avatar
Tom Tromey committed
654
      && XFIXNUM (AREF (spec, FONT_AVGWIDTH_INDEX)) == 0)
655
    scalable = 1;
656

Kenichi Handa's avatar
Kenichi Handa committed
657 658
  registry = AREF (spec, FONT_REGISTRY_INDEX);
  if (NILP (registry)
659
      || EQ (registry, Qascii_0)
Kenichi Handa's avatar
Kenichi Handa committed
660
      || EQ (registry, Qiso10646_1)
661
      || EQ (registry, Qunicode_bmp))
662
    fc_charset_idx = -1;
Kenichi Handa's avatar
Kenichi Handa committed
663
  else
Kenichi Handa's avatar
Kenichi Handa committed
664
    {
665 666
      FcChar8 *lang;

667 668
      fc_charset_idx = ftfont_get_charset (registry);
      if (fc_charset_idx < 0)
669
	return NULL;
670
      charset = fc_charset_table[fc_charset_idx].fc_charset;
671 672
      *langname = fc_charset_table[fc_charset_idx].lang;
      lang = (FcChar8 *) *langname;
673 674 675 676 677 678 679
      if (lang)
	{
	  langset = FcLangSetCreate ();
	  if (! langset)
	    goto err;
	  FcLangSetAdd (langset, lang);
	}
Kenichi Handa's avatar
Kenichi Handa committed
680 681
    }

682
  otlayout[0] = '\0';
683 684
  for (extra = AREF (spec, FONT_EXTRA_INDEX);
       CONSP (extra); extra = XCDR (extra))
Kenichi Handa's avatar
Kenichi Handa committed
685
    {
686 687
      Lisp_Object key, val;

688 689
      key = XCAR (XCAR (extra)), val = XCDR (XCAR (extra));
      if (EQ (key, QCdpi))
690
	{
691
	  if (FIXNUMP (val))
Tom Tromey's avatar
Tom Tromey committed
692
	    dpi = XFIXNUM (val);
693
	}
694
      else if (EQ (key, QClang))
Kenichi Handa's avatar
Kenichi Handa committed
695
	{
696 697
	  if (! langset)
	    langset = FcLangSetCreate ();
Kenichi Handa's avatar
Kenichi Handa committed
698 699
	  if (! langset)
	    goto err;
700
	  if (SYMBOLP (val))
Kenichi Handa's avatar
Kenichi Handa committed
701
	    {
702
	      if (! FcLangSetAdd (langset, SYMBOL_FcChar8 (val)))
Kenichi Handa's avatar
Kenichi Handa committed
703 704 705
		goto err;
	    }
	  else
706 707 708 709
	    for (; CONSP (val); val = XCDR (val))
	      if (SYMBOLP (XCAR (val))
		  && ! FcLangSetAdd (langset, SYMBOL_FcChar8 (XCAR (val))))
		goto err;
Kenichi Handa's avatar
Kenichi Handa committed
710
	}
711 712
      else if (EQ (key, QCotf))
	{
713 714 715 716 717
	  if (CONSP (val))
	    {
	      *otspec = ftfont_get_open_type_spec (val);
	      if (! *otspec)
		return NULL;
718
	      strcpy (otlayout, "otlayout:");
719 720 721
	      OTF_TAG_STR ((*otspec)->script_tag, otlayout + 9);
	      script = (*otspec)->script;
	    }
722
	}
723 724 725 726 727
      else if (EQ (key, QCscript))
	script = val;
      else if (EQ (key, QCscalable))
	scalable = ! NILP (val);
    }
Kenichi Handa's avatar
Kenichi Handa committed
728

729 730 731 732
  if (! NILP (script) && ! charset)
    {
      Lisp_Object chars = assq_no_quit (script, Vscript_representative_chars);

733
      if (CONSP (chars) && CONSP (CDR (chars)))
734 735 736 737 738 739
	{
	  charset = FcCharSetCreate ();
	  if (! charset)
	    goto err;
	  for (chars = XCDR (chars); CONSP (chars); chars = XCDR (chars))
	    if (CHARACTERP (XCAR (chars))
Tom Tromey's avatar
Tom Tromey committed
740
		&& ! FcCharSetAddChar (charset, XFIXNAT (XCAR (chars))))
741
	      goto err;
Kenichi Handa's avatar
Kenichi Handa committed
742 743 744
	}
    }

745
  pattern = FcPatternCreate ();
746 747 748
  if (! pattern)
    goto err;
  tmp = AREF (spec, FONT_FOUNDRY_INDEX);
749
  if (! NILP (tmp)
750 751
      && ! FcPatternAddString (pattern, FC_FOUNDRY, SYMBOL_FcChar8 (tmp)))
    goto err;
752 753 754 755
  tmp = AREF (spec, FONT_FAMILY_INDEX);
  if (! NILP (tmp)
      && ! FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (tmp)))
    goto err;
Kenichi Handa's avatar
Kenichi Handa committed
756 757 758 759 760 761
  if (charset
      && ! FcPatternAddCharSet (pattern, FC_CHARSET, charset))
    goto err;
  if (langset
      && ! FcPatternAddLangSet (pattern, FC_LANG, langset))
    goto err;
762 763 764 765
  if (dpi >= 0
      && ! FcPatternAddDouble (pattern, FC_DPI, dpi))
    goto err;
  if (scalable >= 0
Kenichi Handa's avatar
Kenichi Handa committed
766
      && ! FcPatternAddBool (pattern, FC_SCALABLE, scalable ? FcTrue : FcFalse))
767
    goto err;
768
#if defined HAVE_XFT && defined FC_COLOR
769 770 771
  /* We really don't like color fonts, they cause Xft crashes.  See
     Bug#30874.  */
  if (Vxft_ignore_color_fonts
772
      && ! FcPatternAddBool (pattern, FC_COLOR, FcFalse))
773 774
    goto err;
#endif
775

776
  goto finish;
Kenichi Handa's avatar
Kenichi Handa committed
777

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
 err:
  /* We come here because of unexpected error in fontconfig API call
     (usually insufficient memory).  */
  if (pattern)
    {
      FcPatternDestroy (pattern);
      pattern = NULL;
    }
  if (*otspec)
    {
      if ((*otspec)->nfeatures[0] > 0)
	free ((*otspec)->features[0]);
      if ((*otspec)->nfeatures[1] > 0)
	free ((*otspec)->features[1]);
      free (*otspec);
      *otspec = NULL;
    }

 finish:
  if (langset) FcLangSetDestroy (langset);
798
  if (charset && fc_charset_idx < 0) FcCharSetDestroy (charset);
799 800 801
  return pattern;
}

802
static Lisp_Object
803
ftfont_list (struct frame *f, Lisp_Object spec)
804
{
Kenichi Handa's avatar
Kenichi Handa committed
805
  Lisp_Object val = Qnil, family, adstyle;
806 807 808 809
  int i;
  FcPattern *pattern;
  FcFontSet *fontset = NULL;
  FcObjectSet *objset = NULL;
810 811
  FcCharSet *charset;
  Lisp_Object chars = Qnil;
812 813
  char otlayout[15];		/* For "otlayout:XXXX" */
  struct OpenTypeSpec *otspec = NULL;
814
  int spacing = -1;
815
  const char *langname = NULL;
Juanma Barranquero's avatar
Juanma Barranquero committed
816

817 818 819 820 821 822
  if (! fc_initialized)
    {
      FcInit ();
      fc_initialized = 1;
    }

823
  pattern = ftfont_spec_pattern (spec, otlayout, &otspec, &langname);
824 825
  if (! pattern)
    return Qnil;
826 827 828 829 830 831 832 833 834 835 836
  if (FcPatternGetCharSet (pattern, FC_CHARSET, 0, &charset) != FcResultMatch)
    {
      val = assq_no_quit (QCscript, AREF (spec, FONT_EXTRA_INDEX));
      if (! NILP (val))
	{
	  val = assq_no_quit (XCDR (val), Vscript_representative_chars);
	  if (CONSP (val) && VECTORP (XCDR (val)))
	    chars = XCDR (val);
	}
      val = Qnil;
    }
837
  if (FIXNUMP (AREF (spec, FONT_SPACING_INDEX)))
Tom Tromey's avatar
Tom Tromey committed
838
    spacing = XFIXNUM (AREF (spec, FONT_SPACING_INDEX));
839 840 841 842 843
  family = AREF (spec, FONT_FAMILY_INDEX);
  if (! NILP (family))
    {
      Lisp_Object resolved;

844
      resolved = ftfont_resolve_generic_family (family, pattern);
845 846 847 848 849 850 851 852
      if (! NILP (resolved))
	{
	  FcPatternDel (pattern, FC_FAMILY);
	  if (! FcPatternAddString (pattern, FC_FAMILY,
				    SYMBOL_FcChar8 (resolved)))
	    goto err;
	}
    }
Kenichi Handa's avatar
Kenichi Handa committed
853 854 855
  adstyle = AREF (spec, FONT_ADSTYLE_INDEX);
  if (! NILP (adstyle) && SBYTES (SYMBOL_NAME (adstyle)) == 0)
    adstyle = Qnil;
856
  objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT,
857
			     FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING, FC_SCALABLE,
Kenichi Handa's avatar
Kenichi Handa committed
858
			     FC_STYLE, FC_FILE, FC_INDEX,
859 860 861
#ifdef FC_CAPABILITY
			     FC_CAPABILITY,
#endif	/* FC_CAPABILITY */
862 863
#ifdef FC_FONTFORMAT
			     FC_FONTFORMAT,
864 865 866
#endif
#if defined HAVE_XFT && defined FC_COLOR
                             FC_COLOR,
867
#endif
868
			     NULL);
869 870
  if (! objset)
    goto err;
871 872
  if (! NILP (chars))
    FcObjectSetAdd (objset, FC_CHARSET);
873

874
  fontset = FcFontList (NULL, pattern, objset);
875 876
  if (! fontset || fontset->nfont == 0)
    goto finish;
877 878 879 880
#if 0
  /* Need fix because this finds any fonts.  */
  if (fontset->nfont == 0 && ! NILP (family))
    {
Paul Eggert's avatar
Paul Eggert committed
881
      /* Try matching with configuration.  For instance, the
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
	 configuration may specify "Nimbus Mono L" as an alias of
	 "Courier".  */
      FcPattern *pat = FcPatternBuild (0, FC_FAMILY, FcTypeString,
				       SYMBOL_FcChar8 (family), NULL);
      FcChar8 *fam;

      if (FcConfigSubstitute (NULL, pat, FcMatchPattern) == FcTrue)
	{
	  for (i = 0;
	       FcPatternGetString (pat, FC_FAMILY, i, &fam) == FcResultMatch;
	       i++)
	    {
	      FcPatternDel (pattern, FC_FAMILY);
	      FcPatternAddString (pattern, FC_FAMILY, fam);
	      FcFontSetDestroy (fontset);
	      fontset = FcFontList (NULL, pattern, objset);
898
	      if (fontset && fontset->nfont > 0)
899 900 901 902 903
		break;
	    }
	}
    }
#endif
904
  for (i = 0; i < fontset->nfont; i++)
905
    {
906
      Lisp_Object entity;
907 908 909 910 911 912 913 914 915 916 917 918 919
#if defined HAVE_XFT && defined FC_COLOR
      {
        /* Some fonts, notably NotoColorEmoji, have an FC_COLOR value
           that's neither FcTrue nor FcFalse, which means FcFontList
           returns them even when it shouldn't really do so, so we
           need to manually skip them here (Bug#37786).  */
        FcBool b;
        if (Vxft_ignore_color_fonts
            && FcPatternGetBool (fontset->fonts[i], FC_COLOR, 0, &b)
            == FcResultMatch && b != FcFalse)
            continue;
      }
#endif
920 921 922 923 924 925 926 927 928 929
      if (spacing >= 0)
	{
	  int this;

	  if ((FcPatternGetInteger (fontset->fonts[i], FC_SPACING, 0, &this)
	       == FcResultMatch)
	      && spacing != this)
	    continue;
	}

930
#ifdef FC_CAPABILITY
931 932 933
      if (otlayout[0])
	{
	  FcChar8 *this;
934

935 936
	  if (FcPatternGetString (fontset->fonts[i], FC_CAPABILITY, 0, &this)
	      != FcResultMatch
937 938 939
	      || ! strstr ((char *) this, otlayout))
	    continue;
	}
940
#endif	/* FC_CAPABILITY */
941
#if defined HAVE_LIBOTF || defined HAVE_HARFBUZZ
942 943 944
      if (otspec)
	{
	  FcChar8 *file;
945
	  bool passed;
946 947 948 949 950 951 952 953
	  OTF *otf;

	  if (FcPatternGetString (fontset->fonts[i], FC_FILE, 0, &file)
	      != FcResultMatch)
	    continue;
	  otf = OTF_open ((char *) file);
	  if (! otf)
	    continue;
954 955 956 957 958 959 960 961 962 963
	  passed = (OTF_check_features (otf, 1, otspec->script_tag,
					otspec->langsys_tag,
					otspec->features[0],
					otspec->nfeatures[0]) == 1
		    && OTF_check_features (otf, 0, otspec->script_tag,
					   otspec->langsys_tag,
					   otspec->features[1],
					   otspec->nfeatures[1]) == 1);
	  OTF_close (otf);
	  if (!passed)
964
	    continue;
Kenichi Handa's avatar
Kenichi Handa committed
965
	}
966
#endif	/* HAVE_LIBOTF || HAVE_HARFBUZZ */
967 968
      if (VECTORP (chars))
	{
969
	  ptrdiff_t j;
970 971 972 973 974

	  if (FcPatternGetCharSet (fontset->fonts[i], FC_CHARSET, 0, &charset)
	      != FcResultMatch)
	    continue;
	  for (j = 0; j < ASIZE (chars); j++)
975
	    if (TYPE_RANGED_FIXNUMP (FcChar32, AREF (chars, j))
Tom Tromey's avatar
Tom Tromey committed
976
		&& FcCharSetHasChar (charset, XFIXNAT (AREF (chars, j))))
977 978 979 980
	      break;
	  if (j == ASIZE (chars))
	    continue;
	}
981
      if (! NILP (adstyle) || langname)
Kenichi Handa's avatar
Kenichi Handa committed
982 983 984
	{
	  Lisp_Object this_adstyle = get_adstyle_property (fontset->fonts[i]);

985 986
	  if (! NILP (adstyle)
	      && (NILP (this_adstyle)
987 988
		  || xstrcasecmp (SSDATA (SYMBOL_NAME (adstyle)),
				  SSDATA (SYMBOL_NAME (this_adstyle))) != 0))
989 990 991
	    continue;
	  if (langname
	      && ! NILP (this_adstyle)
992
	      && xstrcasecmp (langname, SSDATA (SYMBOL_NAME (this_adstyle))))
Kenichi Handa's avatar
Kenichi Handa committed
993 994
	    continue;
	}
995 996
      entity = ftfont_pattern_entity (fontset->fonts[i],
				      AREF (spec, FONT_EXTRA_INDEX));
997 998
      if (! NILP (entity))
	val = Fcons (entity, val);
Kenichi Handa's avatar
Kenichi Handa committed
999
    }
1000
  val = Fnreverse (val);
Kenichi Handa's avatar
Kenichi Handa committed
1001 1002 1003 1004
  goto finish;

 err:
  /* We come here because of unexpected error in fontconfig API call
1005
     (usually insufficient memory).  */
Kenichi Handa's avatar
Kenichi Handa committed
1006 1007 1008
  val = Qnil;

 finish:
1009
  FONT_ADD_LOG ("ftfont-list", spec, val);
Kenichi Handa's avatar
Kenichi Handa committed
1010 1011 1012 1013 1014 1015
  if (objset) FcObjectSetDestroy (objset);
  if (fontset) FcFontSetDestroy (fontset);
  if (pattern) FcPatternDestroy (pattern);
  return val;
}

1016
Lisp_Object
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
ftfont_list2 (struct frame *f, Lisp_Object spec, Lisp_Object type)
{
  Lisp_Object list = ftfont_list (f, spec);

  for (Lisp_Object tail = list; CONSP (tail); tail = XCDR (tail))
    ASET (XCAR (tail), FONT_TYPE_INDEX, type);
  return list;
}

static Lisp_Object
1027
ftfont_match (struct frame *f, Lisp_Object spec)
1028
{
1029
  Lisp_Object entity = Qnil;
1030
  FcPattern *pattern, *match = NULL;
1031
  FcResult result;
1032 1033
  char otlayout[15];		/* For "otlayout:XXXX" */
  struct OpenTypeSpec *otspec = NULL;
1034
  const char *langname = NULL;
1035 1036 1037 1038 1039 1040 1041

  if (! fc_initialized)
    {
      FcInit ();
      fc_initialized = 1;
    }

1042
  pattern = ftfont_spec_pattern (spec, otlayout, &otspec, &langname);
1043
  if (! pattern)
1044 1045
    return Qnil;

1046
  if (FIXNUMP (AREF (spec, FONT_SIZE_INDEX)))
1047
    {
1048
      FcValue value;
1049

1050
      value.type = FcTypeDouble;
Tom Tromey's avatar
Tom Tromey committed
1051
      value.u.d = XFIXNUM (AREF (spec, FONT_SIZE_INDEX));
1052 1053 1054 1055 1056 1057 1058
      FcPatternAdd (pattern, FC_PIXEL_SIZE, value, FcFalse);
    }
  if (FcConfigSubstitute (NULL, pattern, FcMatchPattern) == FcTrue)
    {
      FcDefaultSubstitute (pattern);
      match = FcFontMatch (NULL, pattern, &result);
      if (match)
1059
	{
1060
	  entity = ftfont_pattern_entity (match, AREF (spec, FONT_EXTRA_INDEX));
1061 1062 1063 1064 1065 1066 1067
	  FcPatternDestroy (match);
	  if (! NILP (AREF (spec, FONT_FAMILY_INDEX))
	      && NILP (assq_no_quit (AREF (spec, FONT_FAMILY_INDEX),
				     ftfont_generic_family_list))
	      && NILP (Fstring_equal (AREF (spec, FONT_FAMILY_INDEX),