ftfont.c 55.7 KB
Newer Older
Kenichi Handa's avatar
Kenichi Handa committed
1
/* ftfont.c -- FreeType 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

#include <config.h>
#include <stdio.h>

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

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

40
/* Symbolic type of this font-driver.  */
Kenichi Handa's avatar
Kenichi Handa committed
41 42
Lisp_Object Qfreetype;

43 44 45
/* Fontconfig's generic families and their aliases.  */
static Lisp_Object Qmonospace, Qsans_serif, Qserif, Qmono, Qsans, Qsans__serif;

Juanma Barranquero's avatar
Juanma Barranquero committed
46
/* Flag to tell if FcInit is already called or not.  */
Kenichi Handa's avatar
Kenichi Handa committed
47
static int fc_initialized;
48 49

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

52
/* Cache for FreeType fonts.  */
Kenichi Handa's avatar
Kenichi Handa committed
53 54
static Lisp_Object freetype_font_cache;

55
/* Cache for FT_Face and FcCharSet. */
Kenichi Handa's avatar
Kenichi Handa committed
56
static Lisp_Object ft_face_cache;
Kenichi Handa's avatar
Kenichi Handa committed
57 58 59 60 61 62 63

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

struct ftfont_info
{
  struct font font;
64
#ifdef HAVE_LIBOTF
65
  /* The following four members must be here in this order to be
66
     compatible with struct xftfont_info (in xftfont.c).  */
67 68 69
  int maybe_otf;	/* Flag to tell if this may be OTF or not.  */
  OTF *otf;
#endif	/* HAVE_LIBOTF */
70 71
  FT_Size ft_size;
  int index;
Kenichi Handa's avatar
Kenichi Handa committed
72 73
};

74
static Lisp_Object ftfont_pattern_entity P_ ((FcPattern *, Lisp_Object));
Kenichi Handa's avatar
Kenichi Handa committed
75

76 77
static Lisp_Object ftfont_resolve_generic_family P_ ((Lisp_Object,
						      FcPattern *));
78 79
static Lisp_Object ftfont_lookup_cache P_ ((Lisp_Object, int));

80
Lisp_Object ftfont_font_format P_ ((FcPattern *, Lisp_Object));
81 82 83

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

Kenichi Handa's avatar
Kenichi Handa committed
84
static struct
Kenichi Handa's avatar
Kenichi Handa committed
85
{
86
  /* registry name */
Kenichi Handa's avatar
Kenichi Handa committed
87 88 89
  char *name;
  /* characters to distinguish the charset from the others */
  int uniquifier[6];
90 91
  /* additional constraint by language */
  char *lang;
Kenichi Handa's avatar
Kenichi Handa committed
92 93 94
  /* set on demand */
  FcCharSet *fc_charset;
} fc_charset_table[] =
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
  { { "iso8859-1", { 0x00A0, 0x00A1, 0x00B4, 0x00BC, 0x00D0 } },
    { "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"},
    { "big5-0", { 0xF6B1 }, "zh-tw" },
    { "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"},
128 129
    { "windows-1251", { 0x0401, 0x0490 }, "ru"},
    { "koi8-r", { 0x0401, 0x2219 }, "ru"},
130
    { "mulelao-1", { 0x0E81 }, "lo"},
131
    { "unicode-sip", { 0x20000 }},
Kenichi Handa's avatar
Kenichi Handa committed
132 133
    { NULL }
  };
Kenichi Handa's avatar
Kenichi Handa committed
134

135 136
extern Lisp_Object Qc, Qm, Qp, Qd;

137
static Lisp_Object
138
ftfont_pattern_entity (p, extra)
139
     FcPattern *p;
140
     Lisp_Object extra;
141 142
{
  Lisp_Object entity;
Kenichi Handa's avatar
Kenichi Handa committed
143
  char *file, *str;
144
  int index;
145 146
  int numeric;
  double dbl;
147
  FcBool b;
148

Kenichi Handa's avatar
Kenichi Handa committed
149
  if (FcPatternGetString (p, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
150
    return Qnil;
151 152
  if (FcPatternGetInteger (p, FC_INDEX, 0, &index) != FcResultMatch)
    return Qnil;
153

154
  entity = font_make_entity ();
155 156

  ASET (entity, FONT_TYPE_INDEX, Qfreetype);
157
  ASET (entity, FONT_REGISTRY_INDEX, Qiso10646_1);
158

Kenichi Handa's avatar
Kenichi Handa committed
159
  if (FcPatternGetString (p, FC_FOUNDRY, 0, (FcChar8 **) &str) == FcResultMatch)
160
    ASET (entity, FONT_FOUNDRY_INDEX, font_intern_prop (str, strlen (str), 1));
Kenichi Handa's avatar
Kenichi Handa committed
161
  if (FcPatternGetString (p, FC_FAMILY, 0, (FcChar8 **) &str) == FcResultMatch)
162
    ASET (entity, FONT_FAMILY_INDEX, font_intern_prop (str, strlen (str), 1));
163
  if (FcPatternGetInteger (p, FC_WEIGHT, 0, &numeric) == FcResultMatch)
164
    {
165 166 167
      if (numeric >= FC_WEIGHT_REGULAR && numeric < FC_WEIGHT_MEDIUM)
	numeric = FC_WEIGHT_MEDIUM;
      FONT_SET_STYLE (entity, FONT_WEIGHT_INDEX, make_number (numeric));
168
    }
169
  if (FcPatternGetInteger (p, FC_SLANT, 0, &numeric) == FcResultMatch)
170 171 172 173
    {
      numeric += 100;
      FONT_SET_STYLE (entity, FONT_SLANT_INDEX, make_number (numeric));
    }
174
  if (FcPatternGetInteger (p, FC_WIDTH, 0, &numeric) == FcResultMatch)
175 176 177
    {
      FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_number (numeric));
    }
178 179 180 181
  if (FcPatternGetDouble (p, FC_PIXEL_SIZE, 0, &dbl) == FcResultMatch)
    ASET (entity, FONT_SIZE_INDEX, make_number (dbl));
  else
    ASET (entity, FONT_SIZE_INDEX, make_number (0));
182 183 184 185 186 187 188 189 190 191
  if (FcPatternGetInteger (p, FC_SPACING, 0, &numeric) == FcResultMatch)
    ASET (entity, FONT_SPACING_INDEX, make_number (numeric));
  if (FcPatternGetDouble (p, FC_DPI, 0, &dbl) == FcResultMatch)
    {
      int dpi = dbl;
      ASET (entity, FONT_DPI_INDEX, make_number (dpi));
    }
  if (FcPatternGetBool (p, FC_SCALABLE, 0, &b) == FcResultMatch
      && b == FcTrue)
    ASET (entity, FONT_AVGWIDTH_INDEX, make_number (0));
192

193
  ASET (entity, FONT_EXTRA_INDEX, Fcopy_sequence (extra));
Kenichi Handa's avatar
Kenichi Handa committed
194 195 196
  font_put_extra (entity, QCfont_entity,
		  Fcons (make_unibyte_string ((char *) file,
					      strlen ((char *) file)),
197
			 make_number (index)));
198 199 200
  return entity;
}

201

202 203 204
static Lisp_Object ftfont_generic_family_list;

static Lisp_Object
205
ftfont_resolve_generic_family (family, pattern)
206
     Lisp_Object family;
207
     FcPattern *pattern;
208
{
209
  Lisp_Object slot;
210
  FcPattern *match;
211
  FcResult result;
Kenichi Handa's avatar
Kenichi Handa committed
212
  FcLangSet *langset;
213

214
  family = Fintern (Fdowncase (SYMBOL_NAME (family)), Qnil);
215 216 217 218 219 220
  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))
221
    return Qnil;
222 223
  if (! EQ (XCDR (slot), Qt))
    return XCDR (slot);
224
  pattern = FcPatternDuplicate (pattern);
225 226
  if (! pattern)
    goto err;
227
  FcPatternDel (pattern, FC_FOUNDRY);
228 229
  FcPatternDel (pattern, FC_FAMILY);
  FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (family));
Kenichi Handa's avatar
Kenichi Handa committed
230 231 232 233 234 235 236 237
  if (FcPatternGetLangSet (pattern, FC_LANG, 0, &langset) != FcResultMatch)
    {
      /* This is to avoid the effect of locale.  */
      langset = FcLangSetCreate ();
      FcLangSetAdd (langset, "en");
      FcPatternAddLangSet (pattern, FC_LANG, langset);
      FcLangSetDestroy (langset);
    }
238
  FcConfigSubstitute (NULL, pattern, FcMatchPattern);
239 240 241
  FcDefaultSubstitute (pattern);
  match = FcFontMatch (NULL, pattern, &result);
  if (match)
242
    {
243 244 245 246
      FcChar8 *fam;

      if (FcPatternGetString (match, FC_FAMILY, 0, &fam) == FcResultMatch)
	family = intern ((char *) fam);
247
    }
248 249 250 251
  else
    family = Qnil;
  XSETCDR (slot, family);
  if (match) FcPatternDestroy (match);
252
 err:
253
  if (pattern) FcPatternDestroy (pattern);
254
  return family;
255 256
}

257 258 259 260 261 262 263 264 265 266
struct ftfont_cache_data
{
  FT_Face ft_face;
  FcCharSet *fc_charset;
};

static Lisp_Object
ftfont_lookup_cache (key, for_face)
     Lisp_Object key;
     int for_face;
Kenichi Handa's avatar
Kenichi Handa committed
267 268
{
  Lisp_Object cache, val;
269
  struct ftfont_cache_data *cache_data;
Kenichi Handa's avatar
Kenichi Handa committed
270

271
  cache = assoc_no_quit (key, ft_face_cache);
Kenichi Handa's avatar
Kenichi Handa committed
272 273
  if (NILP (cache))
    {
274 275 276
      cache_data = xmalloc (sizeof (struct ftfont_cache_data));
      cache_data->ft_face = NULL;
      cache_data->fc_charset = NULL;
Kenichi Handa's avatar
Kenichi Handa committed
277
      val = make_save_value (NULL, 0);
278 279 280
      XSAVE_VALUE (val)->integer = 0;
      XSAVE_VALUE (val)->pointer = cache_data;
      cache = Fcons (key, val);
Kenichi Handa's avatar
Kenichi Handa committed
281 282 283 284
      ft_face_cache = Fcons (cache, ft_face_cache);
    }
  else
    {
285 286 287 288 289 290 291
      val = XCDR (cache);
      cache_data = XSAVE_VALUE (val)->pointer;
    }
  if (for_face ? ! cache_data->ft_face : ! cache_data->fc_charset)
    {
      char *filename = (char *) SDATA (XCAR (key));
      int index = XINT (XCDR (key));
Kenichi Handa's avatar
Kenichi Handa committed
292

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
      if (for_face)
	{
	  if (! ft_library
	      && FT_Init_FreeType (&ft_library) != 0)
	    return Qnil;
	  if (FT_New_Face (ft_library, filename, index, &cache_data->ft_face)
	      != 0)
	    return Qnil;
	}
      else
	{
	  FcPattern *pat;
	  FcFontSet *fontset;
	  FcObjectSet *objset;
	  FcCharSet *charset;

	  pat = FcPatternBuild (0, FC_FILE, FcTypeString, (FcChar8 *) filename,
				FC_INDEX, FcTypeInteger, index, NULL);
	  objset = FcObjectSetBuild (FC_CHARSET, NULL);
	  fontset = FcFontList (NULL, pat, objset);
313 314 315 316
	  if (fontset && fontset->nfont > 0
	      && (FcPatternGetCharSet (fontset->fonts[0], FC_CHARSET, 0,
				       &charset)
		  == FcResultMatch))
317 318 319 320 321 322 323
	    cache_data->fc_charset = FcCharSetCopy (charset);
	  else
	    cache_data->fc_charset = FcCharSetCreate ();
	  FcFontSetDestroy (fontset);
	  FcObjectSetDestroy (objset);
	  FcPatternDestroy (pat);
	}
Kenichi Handa's avatar
Kenichi Handa committed
324 325 326 327
    }
  return cache;
}

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
FcCharSet *
ftfont_get_fc_charset (entity)
     Lisp_Object entity;
{
  Lisp_Object val, cache;
  struct ftfont_cache_data *cache_data;

  val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
  xassert (CONSP (val));
  val = XCDR (val);
  cache = ftfont_lookup_cache (val, 0);
  val = XCDR (cache);
  cache_data = XSAVE_VALUE (val)->pointer;
  return cache_data->fc_charset;
}

#ifdef HAVE_LIBOTF
static OTF *
ftfont_get_otf (ftfont_info)
     struct ftfont_info *ftfont_info;
{
  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;
}
#endif	/* HAVE_LIBOTF */

368
static Lisp_Object ftfont_get_cache P_ ((FRAME_PTR));
Kenichi Handa's avatar
Kenichi Handa committed
369
static Lisp_Object ftfont_list P_ ((Lisp_Object, Lisp_Object));
370
static Lisp_Object ftfont_match P_ ((Lisp_Object, Lisp_Object));
Kenichi Handa's avatar
Kenichi Handa committed
371
static Lisp_Object ftfont_list_family P_ ((Lisp_Object));
372
static Lisp_Object ftfont_open P_ ((FRAME_PTR, Lisp_Object, int));
Kenichi Handa's avatar
Kenichi Handa committed
373 374 375 376 377 378 379 380 381
static void ftfont_close P_ ((FRAME_PTR, struct font *));
static int ftfont_has_char P_ ((Lisp_Object, int));
static unsigned ftfont_encode_char P_ ((struct font *, int));
static int ftfont_text_extents P_ ((struct font *, unsigned *, int,
				    struct font_metrics *));
static int ftfont_get_bitmap P_ ((struct font *, unsigned,
				  struct font_bitmap *, int));
static int ftfont_anchor_point P_ ((struct font *, unsigned, int,
				    int *, int *));
382
static Lisp_Object ftfont_otf_capability P_ ((struct font *));
383
static Lisp_Object ftfont_shape P_ ((Lisp_Object));
384 385
static int ftfont_variation_glyphs P_ ((struct font *, int c,
					unsigned variations[256]));
Kenichi Handa's avatar
Kenichi Handa committed
386 387 388

struct font_driver ftfont_driver =
  {
389
    0,				/* Qfreetype */
390
    0,				/* case insensitive */
Kenichi Handa's avatar
Kenichi Handa committed
391 392
    ftfont_get_cache,
    ftfont_list,
393
    ftfont_match,
Kenichi Handa's avatar
Kenichi Handa committed
394
    ftfont_list_family,
395
    NULL,			/* free_entity */
Kenichi Handa's avatar
Kenichi Handa committed
396 397 398
    ftfont_open,
    ftfont_close,
    /* We can't draw a text without device dependent functions.  */
399 400
    NULL,			/* prepare_face */
    NULL,			/* done_face */
Kenichi Handa's avatar
Kenichi Handa committed
401 402 403 404
    ftfont_has_char,
    ftfont_encode_char,
    ftfont_text_extents,
    /* We can't draw a text without device dependent functions.  */
405
    NULL,			/* draw */
Kenichi Handa's avatar
Kenichi Handa committed
406
    ftfont_get_bitmap,
407 408 409
    NULL,			/* get_bitmap */
    NULL,			/* free_bitmap */
    NULL,			/* get_outline */
Kenichi Handa's avatar
Kenichi Handa committed
410
    ftfont_anchor_point,
411 412 413
#ifdef HAVE_LIBOTF
    ftfont_otf_capability,
#else  /* not HAVE_LIBOTF */
Kenichi Handa's avatar
Kenichi Handa committed
414
    NULL,
415
#endif	/* not HAVE_LIBOTF */
416 417 418
    NULL,			/* otf_drive */
    NULL,			/* start_for_frame */
    NULL,			/* end_for_frame */
419
#if defined (HAVE_M17N_FLT) && defined (HAVE_LIBOTF)
420
    ftfont_shape,
421
#else  /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
422
    NULL,
423
#endif	/* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
424 425 426 427 428 429
    NULL,			/* check */
#ifdef HAVE_OTF_GET_VARIATION_GLYPHS
    ftfont_variation_glyphs
#else
    NULL
#endif
Kenichi Handa's avatar
Kenichi Handa committed
430 431 432 433 434
  };

extern Lisp_Object QCname;

static Lisp_Object
435 436
ftfont_get_cache (f)
     FRAME_PTR f;
Kenichi Handa's avatar
Kenichi Handa committed
437 438 439 440
{
  return freetype_font_cache;
}

Kenichi Handa's avatar
Kenichi Handa committed
441 442 443 444
static int
ftfont_get_charset (registry)
     Lisp_Object registry;
{
445 446 447
  char *str = (char *) SDATA (SYMBOL_NAME (registry));
  char *re = alloca (SBYTES (SYMBOL_NAME (registry)) * 2 + 1);
  Lisp_Object regexp;
Kenichi Handa's avatar
Kenichi Handa committed
448 449
  int i, j;

450 451 452 453 454 455 456 457 458 459 460 461
  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';
  regexp = make_unibyte_string (re, j);
Kenichi Handa's avatar
Kenichi Handa committed
462
  for (i = 0; fc_charset_table[i].name; i++)
463
    if (fast_c_string_match_ignore_case (regexp, fc_charset_table[i].name) >= 0)
Kenichi Handa's avatar
Kenichi Handa committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
      break;
  if (! fc_charset_table[i].name)
    return -1;
  if (! fc_charset_table[i].fc_charset)
    {
      FcCharSet *charset = FcCharSetCreate ();
      int *uniquifier = fc_charset_table[i].uniquifier;

      if (! charset)
	return -1;
      for (j = 0; uniquifier[j]; j++)
	if (! FcCharSetAddChar (charset, uniquifier[j]))
	  {
	    FcCharSetDestroy (charset);
	    return -1;
	  }
Juanma Barranquero's avatar
Juanma Barranquero committed
480
      fc_charset_table[i].fc_charset = charset;
Kenichi Handa's avatar
Kenichi Handa committed
481 482 483 484
    }
  return i;
}

485 486
struct OpenTypeSpec
{
487 488
  Lisp_Object script;
  unsigned int script_tag, langsys_tag;
489 490 491 492
  int nfeatures[2];
  unsigned int *features[2];
};

493
#define OTF_SYM_TAG(SYM, TAG)					\
494
  do {								\
495 496
    unsigned char *p = SDATA (SYMBOL_NAME (SYM));		\
    TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];	\
497 498
  } while (0)

499
#define OTF_TAG_STR(TAG, P)			\
500
  do {						\
501 502 503 504
    (P)[0] = (char) (TAG >> 24);		\
    (P)[1] = (char) ((TAG >> 16) & 0xFF);	\
    (P)[2] = (char) ((TAG >> 8) & 0xFF);	\
    (P)[3] = (char) (TAG & 0xFF);		\
505
    (P)[4] = '\0';				\
506 507
  } while (0)

508 509 510 511 512 513 514 515 516
#define OTF_TAG_SYM(SYM, TAG)			\
  do {						\
    char str[5];				\
    						\
    OTF_TAG_STR (TAG, str);			\
    (SYM) = font_intern_prop (str, 4, 1);	\
  } while (0)


517 518 519 520 521 522 523 524 525
static struct OpenTypeSpec *
ftfont_get_open_type_spec (Lisp_Object otf_spec)
{
  struct OpenTypeSpec *spec = malloc (sizeof (struct OpenTypeSpec));
  Lisp_Object val;
  int i, j, negative;

  if (! spec)
    return NULL;
526
  spec->script = XCAR (otf_spec);
527
  if (! NILP (spec->script))
528 529 530 531 532 533 534 535
    {
      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;
    }
536
  else
537
    spec->script_tag = 0x44464C54; 	/* "DFLT" */
538 539 540
  otf_spec = XCDR (otf_spec);
  val = XCAR (otf_spec);
  if (! NILP (val))
541
    OTF_SYM_TAG (val, spec->langsys_tag);
542
  else
543
    spec->langsys_tag = 0;
544 545 546 547 548
  spec->nfeatures[0] = spec->nfeatures[1] = 0;
  for (i = 0; i < 2; i++)
    {
      Lisp_Object len;

Juanma Barranquero's avatar
Juanma Barranquero committed
549
      otf_spec = XCDR (otf_spec);
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
      if (NILP (otf_spec))
	break;
      val = XCAR (otf_spec);
      if (NILP (val))
	continue;
      len = Flength (val);
      spec->features[i] = malloc (sizeof (int) * XINT (len));
      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);
	      spec->features[i][j++] = negative ? tag & 0x80000000 : tag;
	    }
	}
      spec->nfeatures[i] = j;
    }
  return spec;
}

581
static FcPattern *ftfont_spec_pattern P_ ((Lisp_Object, char *,
Kenichi Handa's avatar
Kenichi Handa committed
582 583
					   struct OpenTypeSpec **));

584
static FcPattern *
585
ftfont_spec_pattern (spec, otlayout, otspec)
586 587 588
     Lisp_Object spec;
     char *otlayout;
     struct OpenTypeSpec **otspec;
Kenichi Handa's avatar
Kenichi Handa committed
589
{
Kenichi Handa's avatar
Kenichi Handa committed
590
  Lisp_Object tmp, extra;
Kenichi Handa's avatar
Kenichi Handa committed
591 592 593
  FcPattern *pattern = NULL;
  FcCharSet *charset = NULL;
  FcLangSet *langset = NULL;
594 595
  int n;
  int dpi = -1;
596
  int scalable = -1;
597
  Lisp_Object script = Qnil;
Kenichi Handa's avatar
Kenichi Handa committed
598
  Lisp_Object registry;
599
  int fc_charset_idx;
Kenichi Handa's avatar
Kenichi Handa committed
600

601
  if (! NILP (AREF (spec, FONT_ADSTYLE_INDEX))
602 603 604 605 606
      && SBYTES (SYMBOL_NAME (AREF (spec, FONT_ADSTYLE_INDEX))) > 0)
    /* Fontconfig doesn't support adstyle property.  */
    return NULL;
  if ((n = FONT_SLANT_NUMERIC (spec)) >= 0
      && n < 100)
607
    /* Fontconfig doesn't support reverse-italic/obligue.  */
608 609 610 611 612 613 614
    return NULL;

  if (INTEGERP (AREF (spec, FONT_DPI_INDEX)))
    dpi = XINT (AREF (spec, FONT_DPI_INDEX));
  if (INTEGERP (AREF (spec, FONT_AVGWIDTH_INDEX))
      && XINT (AREF (spec, FONT_AVGWIDTH_INDEX)) == 0)
    scalable = 1;
615

Kenichi Handa's avatar
Kenichi Handa committed
616 617
  registry = AREF (spec, FONT_REGISTRY_INDEX);
  if (NILP (registry)
618
      || EQ (registry, Qascii_0)
Kenichi Handa's avatar
Kenichi Handa committed
619
      || EQ (registry, Qiso10646_1)
620
      || EQ (registry, Qunicode_bmp))
621
    fc_charset_idx = -1;
Kenichi Handa's avatar
Kenichi Handa committed
622
  else
Kenichi Handa's avatar
Kenichi Handa committed
623
    {
624 625
      FcChar8 *lang;

626 627
      fc_charset_idx = ftfont_get_charset (registry);
      if (fc_charset_idx < 0)
628
	return NULL;
629 630
      charset = fc_charset_table[fc_charset_idx].fc_charset;
      lang = (FcChar8 *) fc_charset_table[fc_charset_idx].lang;
631 632 633 634 635 636 637
      if (lang)
	{
	  langset = FcLangSetCreate ();
	  if (! langset)
	    goto err;
	  FcLangSetAdd (langset, lang);
	}
Kenichi Handa's avatar
Kenichi Handa committed
638 639
    }

640
  otlayout[0] = '\0';
641 642
  for (extra = AREF (spec, FONT_EXTRA_INDEX);
       CONSP (extra); extra = XCDR (extra))
Kenichi Handa's avatar
Kenichi Handa committed
643
    {
644 645
      Lisp_Object key, val;

646 647 648 649
      key = XCAR (XCAR (extra)), val = XCDR (XCAR (extra));
      if (EQ (key, QCdpi))
	dpi = XINT (val);
      else if (EQ (key, QClang))
Kenichi Handa's avatar
Kenichi Handa committed
650
	{
651 652
	  if (! langset)
	    langset = FcLangSetCreate ();
Kenichi Handa's avatar
Kenichi Handa committed
653 654
	  if (! langset)
	    goto err;
655
	  if (SYMBOLP (val))
Kenichi Handa's avatar
Kenichi Handa committed
656
	    {
657
	      if (! FcLangSetAdd (langset, SYMBOL_FcChar8 (val)))
Kenichi Handa's avatar
Kenichi Handa committed
658 659 660
		goto err;
	    }
	  else
661 662 663 664
	    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
665
	}
666 667 668 669 670 671 672 673 674
      else if (EQ (key, QCotf))
	{
	  *otspec = ftfont_get_open_type_spec (val);
	  if (! *otspec)
	    return NULL;
	  strcat (otlayout, "otlayout:");
	  OTF_TAG_STR ((*otspec)->script_tag, otlayout + 9);
	  script = (*otspec)->script;
	}
675 676 677 678 679
      else if (EQ (key, QCscript))
	script = val;
      else if (EQ (key, QCscalable))
	scalable = ! NILP (val);
    }
Kenichi Handa's avatar
Kenichi Handa committed
680

681 682 683 684
  if (! NILP (script) && ! charset)
    {
      Lisp_Object chars = assq_no_quit (script, Vscript_representative_chars);

685
      if (CONSP (chars) && CONSP (CDR (chars)))
686 687 688 689 690 691 692 693
	{
	  charset = FcCharSetCreate ();
	  if (! charset)
	    goto err;
	  for (chars = XCDR (chars); CONSP (chars); chars = XCDR (chars))
	    if (CHARACTERP (XCAR (chars))
		&& ! FcCharSetAddChar (charset, XUINT (XCAR (chars))))
	      goto err;
Kenichi Handa's avatar
Kenichi Handa committed
694 695 696
	}
    }

697
  pattern = FcPatternCreate ();
698 699 700
  if (! pattern)
    goto err;
  tmp = AREF (spec, FONT_FOUNDRY_INDEX);
701
  if (! NILP (tmp)
702 703
      && ! FcPatternAddString (pattern, FC_FOUNDRY, SYMBOL_FcChar8 (tmp)))
    goto err;
704 705 706 707
  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
708 709 710 711 712 713
  if (charset
      && ! FcPatternAddCharSet (pattern, FC_CHARSET, charset))
    goto err;
  if (langset
      && ! FcPatternAddLangSet (pattern, FC_LANG, langset))
    goto err;
714 715 716 717
  if (dpi >= 0
      && ! FcPatternAddDouble (pattern, FC_DPI, dpi))
    goto err;
  if (scalable >= 0
Kenichi Handa's avatar
Kenichi Handa committed
718
      && ! FcPatternAddBool (pattern, FC_SCALABLE, scalable ? FcTrue : FcFalse))
719
    goto err;
720

721
  goto finish;
Kenichi Handa's avatar
Kenichi Handa committed
722

723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
 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);
743
  if (charset && fc_charset_idx < 0) FcCharSetDestroy (charset);
744 745 746 747 748 749 750
  return pattern;
}

static Lisp_Object
ftfont_list (frame, spec)
     Lisp_Object frame, spec;
{
751
  Lisp_Object val = Qnil, family;
752 753 754 755
  int i;
  FcPattern *pattern;
  FcFontSet *fontset = NULL;
  FcObjectSet *objset = NULL;
756 757 758
  FcCharSet *charset;
  Lisp_Object chars = Qnil;
  FcResult result;
759 760
  char otlayout[15];		/* For "otlayout:XXXX" */
  struct OpenTypeSpec *otspec = NULL;
761
  int spacing = -1;
Juanma Barranquero's avatar
Juanma Barranquero committed
762

763 764 765 766 767 768
  if (! fc_initialized)
    {
      FcInit ();
      fc_initialized = 1;
    }

769
  pattern = ftfont_spec_pattern (spec, otlayout, &otspec);
770 771
  if (! pattern)
    return Qnil;
772 773 774 775 776 777 778 779 780 781 782
  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;
    }
783 784
  if (INTEGERP (AREF (spec, FONT_SPACING_INDEX)))
    spacing = XINT (AREF (spec, FONT_SPACING_INDEX));
785 786 787 788 789
  family = AREF (spec, FONT_FAMILY_INDEX);
  if (! NILP (family))
    {
      Lisp_Object resolved;

790
      resolved = ftfont_resolve_generic_family (family, pattern);
791 792 793 794 795 796 797 798 799
      if (! NILP (resolved))
	{
	  FcPatternDel (pattern, FC_FAMILY);
	  if (! FcPatternAddString (pattern, FC_FAMILY,
				    SYMBOL_FcChar8 (resolved)))
	    goto err;
	}
    }

800
  objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT,
801
			     FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING, FC_SCALABLE,
802
			     FC_FILE, FC_INDEX,
803 804 805
#ifdef FC_CAPABILITY
			     FC_CAPABILITY,
#endif	/* FC_CAPABILITY */
806 807 808
#ifdef FC_FONTFORMAT
			     FC_FONTFORMAT,
#endif
809
			     NULL);
810 811
  if (! objset)
    goto err;
812 813
  if (! NILP (chars))
    FcObjectSetAdd (objset, FC_CHARSET);
814

815
  fontset = FcFontList (NULL, pattern, objset);
816 817
  if (! fontset || fontset->nfont == 0)
    goto finish;
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
#if 0
  /* Need fix because this finds any fonts.  */
  if (fontset->nfont == 0 && ! NILP (family))
    {
      /* Try maching with configuration.  For instance, the
	 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);
839
	      if (fontset && fontset->nfont > 0)
840 841 842 843 844
		break;
	    }
	}
    }
#endif
845
  for (i = 0; i < fontset->nfont; i++)
846
    {
847
      Lisp_Object entity;
848

849 850 851 852 853 854 855 856 857 858
      if (spacing >= 0)
	{
	  int this;

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

859
#ifdef FC_CAPABILITY
860 861 862
      if (otlayout[0])
	{
	  FcChar8 *this;
863

864 865
	  if (FcPatternGetString (fontset->fonts[i], FC_CAPABILITY, 0, &this)
	      != FcResultMatch
866 867 868
	      || ! strstr ((char *) this, otlayout))
	    continue;
	}
869
#endif	/* FC_CAPABILITY */
870
#ifdef HAVE_LIBOTF
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
      if (otspec)
	{
	  FcChar8 *file;
	  OTF *otf;

	  if (FcPatternGetString (fontset->fonts[i], FC_FILE, 0, &file)
	      != FcResultMatch)
	    continue;
	  otf = OTF_open ((char *) file);
	  if (! otf)
	    continue;
	  if (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)
	    continue;
Kenichi Handa's avatar
Kenichi Handa committed
891
	}
892
#endif	/* HAVE_LIBOTF */
893 894 895 896 897 898 899 900 901 902 903 904 905 906
      if (VECTORP (chars))
	{
	  int j;

	  if (FcPatternGetCharSet (fontset->fonts[i], FC_CHARSET, 0, &charset)
	      != FcResultMatch)
	    continue;
	  for (j = 0; j < ASIZE (chars); j++)
	    if (NATNUMP (AREF (chars, j))
		&& FcCharSetHasChar (charset, XFASTINT (AREF (chars, j))))
	      break;
	  if (j == ASIZE (chars))
	    continue;
	}
907 908
      entity = ftfont_pattern_entity (fontset->fonts[i],
				      AREF (spec, FONT_EXTRA_INDEX));
909 910
      if (! NILP (entity))
	val = Fcons (entity, val);
Kenichi Handa's avatar
Kenichi Handa committed
911
    }
912
  val = Fnreverse (val);
Kenichi Handa's avatar
Kenichi Handa committed
913 914 915 916
  goto finish;

 err:
  /* We come here because of unexpected error in fontconfig API call
917
     (usually insufficient memory).  */
Kenichi Handa's avatar
Kenichi Handa committed
918 919 920
  val = Qnil;

 finish:
921
  font_add_log ("ftfont-list", spec, val);
Kenichi Handa's avatar
Kenichi Handa committed
922 923 924 925 926 927
  if (objset) FcObjectSetDestroy (objset);
  if (fontset) FcFontSetDestroy (fontset);
  if (pattern) FcPatternDestroy (pattern);
  return val;
}

928 929 930 931
static Lisp_Object
ftfont_match (frame, spec)
     Lisp_Object frame, spec;
{
932
  Lisp_Object entity = Qnil;
933
  FcPattern *pattern, *match = NULL;
934
  FcResult result;
935 936
  char otlayout[15];		/* For "otlayout:XXXX" */
  struct OpenTypeSpec *otspec = NULL;
937 938 939 940 941 942 943

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

944
  pattern = ftfont_spec_pattern (spec, otlayout, &otspec);
945
  if (! pattern)
946 947
    return Qnil;

948
  if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
949
    {
950
      FcValue value;
951

952 953 954 955 956 957 958 959 960
      value.type = FcTypeDouble;
      value.u.d = XINT (AREF (spec, FONT_SIZE_INDEX));
      FcPatternAdd (pattern, FC_PIXEL_SIZE, value, FcFalse);
    }
  if (FcConfigSubstitute (NULL, pattern, FcMatchPattern) == FcTrue)
    {
      FcDefaultSubstitute (pattern);
      match = FcFontMatch (NULL, pattern, &result);
      if (match)
961
	{
962
	  entity = ftfont_pattern_entity (match, AREF (spec, FONT_EXTRA_INDEX));
963 964 965 966 967 968 969
	  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),
				      AREF (entity, FONT_FAMILY_INDEX))))
	    entity = Qnil;
970 971
	}
    }
972
  FcPatternDestroy (pattern);
973

974
  font_add_log ("ftfont-match", spec, entity);
975 976 977
  return entity;
}

Kenichi Handa's avatar
Kenichi Handa committed
978 979 980 981
static Lisp_Object
ftfont_list_family (frame)
     Lisp_Object frame;
{
982
  Lisp_Object list = Qnil;
Kenichi Handa's avatar
Kenichi Handa committed
983 984 985 986 987 988 989 990 991 992 993 994 995 996
  FcPattern *pattern = NULL;
  FcFontSet *fontset = NULL;
  FcObjectSet *objset = NULL;
  int i;

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

  pattern = FcPatternCreate ();
  if (! pattern)
    goto finish;
997
  objset = FcObjectSetBuild (FC_FAMILY, NULL);
Kenichi Handa's avatar
Kenichi Handa committed
998 999 1000 1001 1002 1003 1004 1005 1006