ftfont.c 72.2 KB
Newer Older
Kenichi Handa's avatar
Kenichi Handa committed
1
/* ftfont.c -- FreeType font driver.
2
   Copyright (C) 2006-2011 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 <http://www.gnu.org/licenses/>.  */
Kenichi Handa's avatar
Kenichi Handa committed
21 22 23

#include <config.h>
#include <stdio.h>
24
#include <setjmp.h>
Kenichi Handa's avatar
Kenichi Handa committed
25 26 27 28 29 30 31 32 33 34 35

#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
36
#include "composite.h"
Kenichi Handa's avatar
Kenichi Handa committed
37 38
#include "fontset.h"
#include "font.h"
39
#include "ftfont.h"
Kenichi Handa's avatar
Kenichi Handa committed
40

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

44 45 46
/* 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
47
/* Flag to tell if FcInit is already called or not.  */
Kenichi Handa's avatar
Kenichi Handa committed
48
static int fc_initialized;
49 50

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

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

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

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

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

76 77 78 79 80 81 82
enum ftfont_cache_for
  {
    FTFONT_CACHE_FOR_FACE,
    FTFONT_CACHE_FOR_CHARSET,
    FTFONT_CACHE_FOR_ENTITY
  };

83
static Lisp_Object ftfont_pattern_entity (FcPattern *, Lisp_Object);
Kenichi Handa's avatar
Kenichi Handa committed
84

85 86 87 88
static Lisp_Object ftfont_resolve_generic_family (Lisp_Object,
                                                  FcPattern *);
static Lisp_Object ftfont_lookup_cache (Lisp_Object,
                                        enum ftfont_cache_for);
89

90
static void ftfont_filter_properties (Lisp_Object font, Lisp_Object alist);
91

92
Lisp_Object ftfont_font_format (FcPattern *, Lisp_Object);
93 94 95

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

Kenichi Handa's avatar
Kenichi Handa committed
96
static struct
Kenichi Handa's avatar
Kenichi Handa committed
97
{
98
  /* registry name */
99
  const char *name;
Kenichi Handa's avatar
Kenichi Handa committed
100 101
  /* characters to distinguish the charset from the others */
  int uniquifier[6];
102
  /* additional constraint by language */
103
  const char *lang;
Kenichi Handa's avatar
Kenichi Handa committed
104 105 106
  /* set on demand */
  FcCharSet *fc_charset;
} fc_charset_table[] =
107
  { { "iso8859-1", { 0x00A0, 0x00A1, 0x00B4, 0x00BC, 0x00D0 } },
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
    { "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"},
140 141
    { "windows-1251", { 0x0401, 0x0490 }, "ru"},
    { "koi8-r", { 0x0401, 0x2219 }, "ru"},
142
    { "mulelao-1", { 0x0E81 }, "lo"},
143
    { "unicode-sip", { 0x20000 }},
Kenichi Handa's avatar
Kenichi Handa committed
144 145
    { NULL }
  };
Kenichi Handa's avatar
Kenichi Handa committed
146

Kenichi Handa's avatar
Kenichi Handa committed
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
/* 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)
{
  char *str, *end;
  Lisp_Object adstyle;

  if (FcPatternGetString (p, FC_STYLE, 0, (FcChar8 **) &str) != FcResultMatch)
    return Qnil;
  for (end = str; *end && *end != ' '; end++);
  if (*end)
    {
      char *p = alloca (end - str + 1);
      memcpy (p, str, end - str);
      p[end - str] = '\0';
174
      end = p + (end - str);
Kenichi Handa's avatar
Kenichi Handa committed
175 176 177 178 179 180 181
      str = p;
    }
  if (xstrcasecmp (str, "Regular") == 0
      || xstrcasecmp (str, "Bold") == 0
      || xstrcasecmp (str, "Oblique") == 0
      || xstrcasecmp (str, "Italic") == 0)
    return Qnil;
182
  adstyle = font_intern_prop (str, end - str, 1);
Kenichi Handa's avatar
Kenichi Handa committed
183 184 185 186 187
  if (font_style_to_value (FONT_WIDTH_INDEX, adstyle, 0) >= 0)
    return Qnil;
  return adstyle;
}

188
static Lisp_Object
189
ftfont_pattern_entity (FcPattern *p, Lisp_Object extra)
190
{
191
  Lisp_Object key, cache, entity;
Kenichi Handa's avatar
Kenichi Handa committed
192
  char *file, *str;
193
  int index;
194 195
  int numeric;
  double dbl;
196
  FcBool b;
197

Kenichi Handa's avatar
Kenichi Handa committed
198
  if (FcPatternGetString (p, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
199
    return Qnil;
200 201
  if (FcPatternGetInteger (p, FC_INDEX, 0, &index) != FcResultMatch)
    return Qnil;
202

203 204 205 206 207
  key = Fcons (make_unibyte_string ((char *) file, strlen ((char *) file)),
	       make_number (index));
  cache = ftfont_lookup_cache (key, FTFONT_CACHE_FOR_ENTITY);
  entity = XCAR (cache);
  if (! NILP (entity))
208 209 210 211 212 213 214 215
    {
      Lisp_Object val = font_make_entity ();
      int i;

      for (i = 0; i < FONT_OBJLIST_INDEX; i++)
	ASET (val, i, AREF (entity, i));
      return val;
    }
216
  entity = font_make_entity ();
217
  XSETCAR (cache, entity);
218 219

  ASET (entity, FONT_TYPE_INDEX, Qfreetype);
220
  ASET (entity, FONT_REGISTRY_INDEX, Qiso10646_1);
221

Kenichi Handa's avatar
Kenichi Handa committed
222
  if (FcPatternGetString (p, FC_FOUNDRY, 0, (FcChar8 **) &str) == FcResultMatch)
223
    ASET (entity, FONT_FOUNDRY_INDEX, font_intern_prop (str, strlen (str), 1));
Kenichi Handa's avatar
Kenichi Handa committed
224
  if (FcPatternGetString (p, FC_FAMILY, 0, (FcChar8 **) &str) == FcResultMatch)
225
    ASET (entity, FONT_FAMILY_INDEX, font_intern_prop (str, strlen (str), 1));
226
  if (FcPatternGetInteger (p, FC_WEIGHT, 0, &numeric) == FcResultMatch)
227
    {
228 229 230
      if (numeric >= FC_WEIGHT_REGULAR && numeric < FC_WEIGHT_MEDIUM)
	numeric = FC_WEIGHT_MEDIUM;
      FONT_SET_STYLE (entity, FONT_WEIGHT_INDEX, make_number (numeric));
231
    }
232
  if (FcPatternGetInteger (p, FC_SLANT, 0, &numeric) == FcResultMatch)
233 234 235 236
    {
      numeric += 100;
      FONT_SET_STYLE (entity, FONT_SLANT_INDEX, make_number (numeric));
    }
237
  if (FcPatternGetInteger (p, FC_WIDTH, 0, &numeric) == FcResultMatch)
238 239 240
    {
      FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_number (numeric));
    }
241
  if (FcPatternGetDouble (p, FC_PIXEL_SIZE, 0, &dbl) == FcResultMatch)
Kenichi Handa's avatar
Kenichi Handa committed
242 243 244
    {
      ASET (entity, FONT_SIZE_INDEX, make_number (dbl));
    }
245 246
  else
    ASET (entity, FONT_SIZE_INDEX, make_number (0));
247 248 249 250 251 252 253 254 255
  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)
256 257 258 259 260 261 262
    {
      ASET (entity, FONT_SIZE_INDEX, make_number (0));
      ASET (entity, FONT_AVGWIDTH_INDEX, make_number (0));
    }
  else
    {
      /* As this font is not scalable, parhaps this is a BDF or PCF
263
	 font. */
264 265 266 267 268 269 270 271 272 273 274 275 276 277
      FT_Face ft_face;

      ASET (entity, FONT_ADSTYLE_INDEX, get_adstyle_property (p));
      if ((ft_library || FT_Init_FreeType (&ft_library) == 0)
	  && FT_New_Face (ft_library, file, index, &ft_face) == 0)
	{
	  BDF_PropertyRec rec;

	  if (FT_Get_BDF_Property (ft_face, "AVERAGE_WIDTH", &rec) == 0
	      && rec.type == BDF_PROPERTY_TYPE_INTEGER)
	    ASET (entity, FONT_AVGWIDTH_INDEX, make_number (rec.u.integer));
	  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 314 315 316 317 318
  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);
    }
319
  FcConfigSubstitute (NULL, pattern, FcMatchPattern);
320 321 322
  FcDefaultSubstitute (pattern);
  match = FcFontMatch (NULL, pattern, &result);
  if (match)
323
    {
324 325 326 327
      FcChar8 *fam;

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

338 339 340 341 342 343 344
struct ftfont_cache_data
{
  FT_Face ft_face;
  FcCharSet *fc_charset;
};

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

Kenichi Handa's avatar
Kenichi Handa committed
350 351 352 353 354 355 356 357 358 359
  if (FONT_ENTITY_P (key))
    {
      entity = key;
      val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
      xassert (CONSP (val));
      key = XCDR (val);
    }
  else
    entity = Qnil;

360 361 362 363
  if (NILP (ft_face_cache))
    cache = Qnil;
  else
    cache = Fgethash (key, ft_face_cache, Qnil);
Kenichi Handa's avatar
Kenichi Handa committed
364 365
  if (NILP (cache))
    {
366 367 368 369 370 371 372 373
      if (NILP (ft_face_cache))
	{
	  Lisp_Object args[2];

	  args[0] = QCtest;
	  args[1] = Qequal;
	  ft_face_cache = Fmake_hash_table (2, args);
	}
374 375 376
      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
377
      val = make_save_value (NULL, 0);
378 379
      XSAVE_VALUE (val)->integer = 0;
      XSAVE_VALUE (val)->pointer = cache_data;
380 381
      cache = Fcons (Qnil, val);
      Fputhash (key, cache, ft_face_cache);
Kenichi Handa's avatar
Kenichi Handa committed
382 383 384
    }
  else
    {
385 386 387
      val = XCDR (cache);
      cache_data = XSAVE_VALUE (val)->pointer;
    }
388 389 390 391 392 393

  if (cache_for == FTFONT_CACHE_FOR_ENTITY)
    return cache;

  if (cache_for == FTFONT_CACHE_FOR_FACE
      ? ! cache_data->ft_face : ! cache_data->fc_charset)
394
    {
395
      char *filename = SSDATA (XCAR (key));
396
      int index = XINT (XCDR (key));
Kenichi Handa's avatar
Kenichi Handa committed
397

398
      if (cache_for == FTFONT_CACHE_FOR_FACE)
399 400 401 402 403 404 405 406 407 408
	{
	  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
	{
Kenichi Handa's avatar
Kenichi Handa committed
409 410 411 412
	  FcPattern *pat = NULL;
	  FcFontSet *fontset = NULL;
	  FcObjectSet *objset = NULL;
	  FcCharSet *charset = NULL;
413 414 415

	  pat = FcPatternBuild (0, FC_FILE, FcTypeString, (FcChar8 *) filename,
				FC_INDEX, FcTypeInteger, index, NULL);
Kenichi Handa's avatar
Kenichi Handa committed
416 417 418 419 420
	  if (! pat)
	    goto finish;
	  objset = FcObjectSetBuild (FC_CHARSET, FC_STYLE, NULL);
	  if (! objset)
	    goto finish;
421
	  fontset = FcFontList (NULL, pat, objset);
Kenichi Handa's avatar
Kenichi Handa committed
422 423
	  if (! fontset)
	    goto finish;
424 425
	  if (fontset && fontset->nfont > 0
	      && (FcPatternGetCharSet (fontset->fonts[0], FC_CHARSET, 0,
426
				       &charset)
427 428
		  == FcResultMatch))
	    cache_data->fc_charset = FcCharSetCopy (charset);
429 430
	  else
	    cache_data->fc_charset = FcCharSetCreate ();
Kenichi Handa's avatar
Kenichi Handa committed
431 432 433 434 435 436 437 438

	finish:
	  if (fontset)
	    FcFontSetDestroy (fontset);
	  if (objset)
	    FcObjectSetDestroy (objset);
	  if (pat)
	    FcPatternDestroy (pat);
439
	}
Kenichi Handa's avatar
Kenichi Handa committed
440 441 442 443
    }
  return cache;
}

444
FcCharSet *
445
ftfont_get_fc_charset (Lisp_Object entity)
446 447 448 449
{
  Lisp_Object val, cache;
  struct ftfont_cache_data *cache_data;

450
  cache = ftfont_lookup_cache (entity, FTFONT_CACHE_FOR_CHARSET);
451 452 453 454 455 456 457
  val = XCDR (cache);
  cache_data = XSAVE_VALUE (val)->pointer;
  return cache_data->fc_charset;
}

#ifdef HAVE_LIBOTF
static OTF *
Andreas Schwab's avatar
Andreas Schwab committed
458
ftfont_get_otf (struct ftfont_info *ftfont_info)
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
{
  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 */

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
static Lisp_Object ftfont_get_cache (FRAME_PTR);
static Lisp_Object ftfont_list (Lisp_Object, Lisp_Object);
static Lisp_Object ftfont_match (Lisp_Object, Lisp_Object);
static Lisp_Object ftfont_list_family (Lisp_Object);
static Lisp_Object ftfont_open (FRAME_PTR, Lisp_Object, int);
static void ftfont_close (FRAME_PTR, struct font *);
static int ftfont_has_char (Lisp_Object, int);
static unsigned ftfont_encode_char (struct font *, int);
static int ftfont_text_extents (struct font *, unsigned *, int,
                                struct font_metrics *);
static int ftfont_get_bitmap (struct font *, unsigned,
                              struct font_bitmap *, int);
static int ftfont_anchor_point (struct font *, unsigned, int,
                                int *, int *);
static Lisp_Object ftfont_otf_capability (struct font *);
static Lisp_Object ftfont_shape (Lisp_Object);
495 496

#ifdef HAVE_OTF_GET_VARIATION_GLYPHS
497 498
static int ftfont_variation_glyphs (struct font *, int c,
                                    unsigned variations[256]);
499
#endif /* HAVE_OTF_GET_VARIATION_GLYPHS */
Kenichi Handa's avatar
Kenichi Handa committed
500 501 502

struct font_driver ftfont_driver =
  {
503
    0,				/* Qfreetype */
504
    0,				/* case insensitive */
Kenichi Handa's avatar
Kenichi Handa committed
505 506
    ftfont_get_cache,
    ftfont_list,
507
    ftfont_match,
Kenichi Handa's avatar
Kenichi Handa committed
508
    ftfont_list_family,
509
    NULL,			/* free_entity */
Kenichi Handa's avatar
Kenichi Handa committed
510 511 512
    ftfont_open,
    ftfont_close,
    /* We can't draw a text without device dependent functions.  */
513 514
    NULL,			/* prepare_face */
    NULL,			/* done_face */
Kenichi Handa's avatar
Kenichi Handa committed
515 516 517 518
    ftfont_has_char,
    ftfont_encode_char,
    ftfont_text_extents,
    /* We can't draw a text without device dependent functions.  */
519
    NULL,			/* draw */
Kenichi Handa's avatar
Kenichi Handa committed
520
    ftfont_get_bitmap,
521 522 523
    NULL,			/* get_bitmap */
    NULL,			/* free_bitmap */
    NULL,			/* get_outline */
Kenichi Handa's avatar
Kenichi Handa committed
524
    ftfont_anchor_point,
525 526 527
#ifdef HAVE_LIBOTF
    ftfont_otf_capability,
#else  /* not HAVE_LIBOTF */
Kenichi Handa's avatar
Kenichi Handa committed
528
    NULL,
529
#endif	/* not HAVE_LIBOTF */
530 531 532
    NULL,			/* otf_drive */
    NULL,			/* start_for_frame */
    NULL,			/* end_for_frame */
533
#if defined (HAVE_M17N_FLT) && defined (HAVE_LIBOTF)
534
    ftfont_shape,
535
#else  /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
536
    NULL,
537
#endif	/* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
538
    NULL,			/* check */
539

540
#ifdef HAVE_OTF_GET_VARIATION_GLYPHS
541
    ftfont_variation_glyphs,
542
#else
543
    NULL,
544
#endif
545 546

    ftfont_filter_properties, /* filter_properties */
Kenichi Handa's avatar
Kenichi Handa committed
547 548 549
  };

static Lisp_Object
550
ftfont_get_cache (FRAME_PTR f)
Kenichi Handa's avatar
Kenichi Handa committed
551 552 553 554
{
  return freetype_font_cache;
}

Kenichi Handa's avatar
Kenichi Handa committed
555
static int
556
ftfont_get_charset (Lisp_Object registry)
Kenichi Handa's avatar
Kenichi Handa committed
557
{
558
  char *str = SSDATA (SYMBOL_NAME (registry));
559 560
  char *re = alloca (SBYTES (SYMBOL_NAME (registry)) * 2 + 1);
  Lisp_Object regexp;
Kenichi Handa's avatar
Kenichi Handa committed
561 562
  int i, j;

563 564 565 566 567 568 569 570 571 572 573 574
  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
575
  for (i = 0; fc_charset_table[i].name; i++)
576
    if (fast_c_string_match_ignore_case (regexp, fc_charset_table[i].name) >= 0)
Kenichi Handa's avatar
Kenichi Handa committed
577 578 579 580 581
      break;
  if (! fc_charset_table[i].name)
    return -1;
  if (! fc_charset_table[i].fc_charset)
    {
582 583
      FcCharSet *charset = FcCharSetCreate ();
      int *uniquifier = fc_charset_table[i].uniquifier;
Kenichi Handa's avatar
Kenichi Handa committed
584

585 586 587 588 589 590
      if (! charset)
	return -1;
      for (j = 0; uniquifier[j]; j++)
	if (! FcCharSetAddChar (charset, uniquifier[j]))
	  {
	    FcCharSetDestroy (charset);
Kenichi Handa's avatar
Kenichi Handa committed
591
	    return -1;
592 593
	  }
      fc_charset_table[i].fc_charset = charset;
Kenichi Handa's avatar
Kenichi Handa committed
594 595 596 597
    }
  return i;
}

598 599
struct OpenTypeSpec
{
600 601
  Lisp_Object script;
  unsigned int script_tag, langsys_tag;
602 603 604 605
  int nfeatures[2];
  unsigned int *features[2];
};

606
#define OTF_SYM_TAG(SYM, TAG)					\
607
  do {								\
608 609
    unsigned char *p = SDATA (SYMBOL_NAME (SYM));		\
    TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];	\
610 611
  } while (0)

612
#define OTF_TAG_STR(TAG, P)			\
613
  do {						\
614 615 616 617
    (P)[0] = (char) (TAG >> 24);		\
    (P)[1] = (char) ((TAG >> 16) & 0xFF);	\
    (P)[2] = (char) ((TAG >> 8) & 0xFF);	\
    (P)[3] = (char) (TAG & 0xFF);		\
618
    (P)[4] = '\0';				\
619 620
  } while (0)

621 622 623 624 625 626 627 628 629
#define OTF_TAG_SYM(SYM, TAG)			\
  do {						\
    char str[5];				\
    						\
    OTF_TAG_STR (TAG, str);			\
    (SYM) = font_intern_prop (str, 4, 1);	\
  } while (0)


630 631 632 633 634 635 636 637 638
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;
639
  spec->script = XCAR (otf_spec);
640
  if (! NILP (spec->script))
641 642 643 644 645 646 647 648
    {
      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;
    }
649
  else
650
    spec->script_tag = 0x44464C54; 	/* "DFLT" */
651
  otf_spec = XCDR (otf_spec);
Kenichi Handa's avatar
Kenichi Handa committed
652 653 654 655 656 657 658 659
  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);
    }
660
  spec->nfeatures[0] = spec->nfeatures[1] = 0;
Kenichi Handa's avatar
Kenichi Handa committed
661
  for (i = 0; i < 2 && ! NILP (otf_spec); i++, otf_spec = XCDR (otf_spec))
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
    {
      Lisp_Object len;

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

694
static FcPattern *
695
ftfont_spec_pattern (Lisp_Object spec, char *otlayout, struct OpenTypeSpec **otspec, const char **langname)
Kenichi Handa's avatar
Kenichi Handa committed
696
{
Kenichi Handa's avatar
Kenichi Handa committed
697
  Lisp_Object tmp, extra;
Kenichi Handa's avatar
Kenichi Handa committed
698 699 700
  FcPattern *pattern = NULL;
  FcCharSet *charset = NULL;
  FcLangSet *langset = NULL;
701 702
  int n;
  int dpi = -1;
703
  int scalable = -1;
704
  Lisp_Object script = Qnil;
Kenichi Handa's avatar
Kenichi Handa committed
705
  Lisp_Object registry;
706
  int fc_charset_idx;
Kenichi Handa's avatar
Kenichi Handa committed
707

708 709
  if ((n = FONT_SLANT_NUMERIC (spec)) >= 0
      && n < 100)
710
    /* Fontconfig doesn't support reverse-italic/obligue.  */
711 712 713 714 715 716 717
    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;
718

Kenichi Handa's avatar
Kenichi Handa committed
719 720
  registry = AREF (spec, FONT_REGISTRY_INDEX);
  if (NILP (registry)
721
      || EQ (registry, Qascii_0)
Kenichi Handa's avatar
Kenichi Handa committed
722
      || EQ (registry, Qiso10646_1)
723
      || EQ (registry, Qunicode_bmp))
724
    fc_charset_idx = -1;
Kenichi Handa's avatar
Kenichi Handa committed
725
  else
Kenichi Handa's avatar
Kenichi Handa committed
726
    {
727 728
      FcChar8 *lang;

729 730
      fc_charset_idx = ftfont_get_charset (registry);
      if (fc_charset_idx < 0)
731
	return NULL;
732
      charset = fc_charset_table[fc_charset_idx].fc_charset;
733 734
      *langname = fc_charset_table[fc_charset_idx].lang;
      lang = (FcChar8 *) *langname;
735 736 737 738 739 740 741
      if (lang)
	{
	  langset = FcLangSetCreate ();
	  if (! langset)
	    goto err;
	  FcLangSetAdd (langset, lang);
	}
Kenichi Handa's avatar
Kenichi Handa committed
742 743
    }

744
  otlayout[0] = '\0';
745 746
  for (extra = AREF (spec, FONT_EXTRA_INDEX);
       CONSP (extra); extra = XCDR (extra))
Kenichi Handa's avatar
Kenichi Handa committed
747
    {
748 749
      Lisp_Object key, val;

750 751
      key = XCAR (XCAR (extra)), val = XCDR (XCAR (extra));
      if (EQ (key, QCdpi))
752 753 754 755
	{
	  if (INTEGERP (val))
	    dpi = XINT (val);
	}
756
      else if (EQ (key, QClang))
Kenichi Handa's avatar
Kenichi Handa committed
757
	{
758 759
	  if (! langset)
	    langset = FcLangSetCreate ();
Kenichi Handa's avatar
Kenichi Handa committed
760 761
	  if (! langset)
	    goto err;
762
	  if (SYMBOLP (val))
Kenichi Handa's avatar
Kenichi Handa committed
763
	    {
764
	      if (! FcLangSetAdd (langset, SYMBOL_FcChar8 (val)))
Kenichi Handa's avatar
Kenichi Handa committed
765 766 767
		goto err;
	    }
	  else
768 769 770 771
	    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
772
	}
773 774
      else if (EQ (key, QCotf))
	{
775 776 777 778 779 780 781 782 783
	  if (CONSP (val))
	    {
	      *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;
	    }
784
	}
785 786 787 788 789
      else if (EQ (key, QCscript))
	script = val;
      else if (EQ (key, QCscalable))
	scalable = ! NILP (val);
    }
Kenichi Handa's avatar
Kenichi Handa committed
790

791 792 793 794
  if (! NILP (script) && ! charset)
    {
      Lisp_Object chars = assq_no_quit (script, Vscript_representative_chars);

795
      if (CONSP (chars) && CONSP (CDR (chars)))
796 797 798 799 800 801 802 803
	{
	  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
804 805 806
	}
    }

807
  pattern = FcPatternCreate ();
808 809 810
  if (! pattern)
    goto err;
  tmp = AREF (spec, FONT_FOUNDRY_INDEX);
811
  if (! NILP (tmp)
812 813
      && ! FcPatternAddString (pattern, FC_FOUNDRY, SYMBOL_FcChar8 (tmp)))
    goto err;
814 815 816 817
  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
818 819 820 821 822 823
  if (charset
      && ! FcPatternAddCharSet (pattern, FC_CHARSET, charset))
    goto err;
  if (langset
      && ! FcPatternAddLangSet (pattern, FC_LANG, langset))
    goto err;
824 825 826 827
  if (dpi >= 0
      && ! FcPatternAddDouble (pattern, FC_DPI, dpi))
    goto err;
  if (scalable >= 0
Kenichi Handa's avatar
Kenichi Handa committed
828
      && ! FcPatternAddBool (pattern, FC_SCALABLE, scalable ? FcTrue : FcFalse))
829
    goto err;
830

831
  goto finish;
Kenichi Handa's avatar
Kenichi Handa committed
832

833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
 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);
853
  if (charset && fc_charset_idx < 0) FcCharSetDestroy (charset);
854 855 856 857
  return pattern;
}

static Lisp_Object
858
ftfont_list (Lisp_Object frame, Lisp_Object spec)
859
{
Kenichi Handa's avatar
Kenichi Handa committed
860
  Lisp_Object val = Qnil, family, adstyle;
861 862 863 864
  int i;
  FcPattern *pattern;
  FcFontSet *fontset = NULL;
  FcObjectSet *objset = NULL;
865 866 867
  FcCharSet *charset;
  Lisp_Object chars = Qnil;
  FcResult result;
868 869
  char otlayout[15];		/* For "otlayout:XXXX" */
  struct OpenTypeSpec *otspec = NULL;
870
  int spacing = -1;
871
  const char *langname = NULL;
Juanma Barranquero's avatar
Juanma Barranquero committed
872

873 874 875 876 877 878
  if (! fc_initialized)
    {
      FcInit ();
      fc_initialized = 1;
    }

879
  pattern = ftfont_spec_pattern (spec, otlayout, &otspec, &langname);
880 881
  if (! pattern)
    return Qnil;
882 883 884 885 886 887 888 889 890 891 892
  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;
    }
893 894
  if (INTEGERP (AREF (spec, FONT_SPACING_INDEX)))
    spacing = XINT (AREF (spec, FONT_SPACING_INDEX));
895 896 897 898 899
  family = AREF (spec, FONT_FAMILY_INDEX);
  if (! NILP (family))
    {
      Lisp_Object resolved;

900
      resolved = ftfont_resolve_generic_family (family, pattern);
901 902 903 904 905 906 907 908
      if (! NILP (resolved))
	{
	  FcPatternDel (pattern, FC_FAMILY);
	  if (! FcPatternAddString (pattern, FC_FAMILY,
				    SYMBOL_FcChar8 (resolved)))
	    goto err;
	}
    }
Kenichi Handa's avatar
Kenichi Handa committed
909 910 911
  adstyle = AREF (spec, FONT_ADSTYLE_INDEX);
  if (! NILP (adstyle) && SBYTES (SYMBOL_NAME (adstyle)) == 0)
    adstyle = Qnil;
912
  objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT,
913
			     FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING, FC_SCALABLE,
Kenichi Handa's avatar
Kenichi Handa committed
914
			     FC_STYLE, FC_FILE, FC_INDEX,
915 916 917
#ifdef FC_CAPABILITY
			     FC_CAPABILITY,
#endif	/* FC_CAPABILITY */
918 919 920
#ifdef FC_FONTFORMAT
			     FC_FONTFORMAT,
#endif
921
			     NULL);
922 923
  if (! objset)
    goto err;
924 925
  if (! NILP (chars))
    FcObjectSetAdd (objset, FC_CHARSET);
926

927
  fontset = FcFontList (NULL, pattern, objset);
928 929
  if (! fontset || fontset->nfont == 0)
    goto finish;
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
#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);
951
	      if (fontset && fontset->nfont > 0)
952 953 954 955 956
		break;
	    }
	}
    }
#endif
957
  for (i = 0; i < fontset->nfont; i++)
958
    {
959
      Lisp_Object entity;
960

961 962 963 964 965 966 967 968 969 970
      if (spacing >= 0)
	{
	  int this;

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

971
#ifdef FC_CAPABILITY