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

This file is part of GNU Emacs.

GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 3, or (at your option)
Kenichi Handa's avatar
Kenichi Handa committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
any later version.

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

You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */

#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"
#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;

47
/* Flag to tell if FcInit is areadly 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 57
/* Fontconfig's charset used for finding fonts of registry
   "iso8859-1".  */
Kenichi Handa's avatar
Kenichi Handa committed
58 59 60 61 62 63 64 65 66
static FcCharSet *cs_iso8859_1;

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

struct ftfont_info
{
  struct font font;
  FT_Size ft_size;
67 68 69 70
#ifdef HAVE_LIBOTF
  int maybe_otf;	/* Flag to tell if this may be OTF or not.  */
  OTF *otf;
#endif	/* HAVE_LIBOTF */
Kenichi Handa's avatar
Kenichi Handa committed
71 72
};

73
static int ftfont_build_basic_charsets P_ ((void));
74 75
static Lisp_Object ftfont_pattern_entity P_ ((FcPattern *, Lisp_Object));
static Lisp_Object ftfont_list_generic_family P_ ((Lisp_Object));
76
Lisp_Object ftfont_font_format P_ ((FcPattern *));
77 78 79

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

Kenichi Handa's avatar
Kenichi Handa committed
80 81 82 83 84 85 86 87 88 89 90
static int
ftfont_build_basic_charsets ()
{
  FcChar32 c;

  cs_iso8859_1 = FcCharSetCreate ();
  if (! cs_iso8859_1)
    return -1;
  for (c = ' '; c < 127; c++)
    if (! FcCharSetAddChar (cs_iso8859_1, c))
      return -1;
Kenichi Handa's avatar
Kenichi Handa committed
91 92
#if 0
  /* This part is currently disabled.  Should be fixed later.  */
Kenichi Handa's avatar
Kenichi Handa committed
93 94 95
  for (c = 192; c < 256; c++)
    if (! FcCharSetAddChar (cs_iso8859_1, c))
      return -1;
Kenichi Handa's avatar
Kenichi Handa committed
96
#endif
Kenichi Handa's avatar
Kenichi Handa committed
97 98 99
  return 0;
}

100 101
extern Lisp_Object Qc, Qm, Qp, Qd;

102
static Lisp_Object
103
ftfont_pattern_entity (p, registry)
104
     FcPattern *p;
105
     Lisp_Object registry;
106 107
{
  Lisp_Object entity;
108
  FcChar8 *file, *fontformat;
109
  FcCharSet *charset;
110
  FcChar8 *str;
111 112
  int numeric;
  double dbl;
113
  FcBool b;
114 115 116

  if (FcPatternGetString (p, FC_FILE, 0, &file) != FcResultMatch)
    return Qnil;
117
  if (FcPatternGetCharSet (p, FC_CHARSET, 0, &charset) != FcResultMatch)
118
    charset = NULL;
119
#ifdef FC_FONTFORMAT
120
  if (FcPatternGetString (p, FC_FONTFORMAT, 0, &fontformat) != FcResultMatch)
121
#endif	/* FC_FONTFORMAT */
122
    fontformat = NULL;
123

124
  entity = font_make_entity ();
125 126 127 128

  ASET (entity, FONT_TYPE_INDEX, Qfreetype);
  ASET (entity, FONT_REGISTRY_INDEX, registry);

129
  if (FcPatternGetString (p, FC_FOUNDRY, 0, &str) == FcResultMatch)
130
    ASET (entity, FONT_FOUNDRY_INDEX, font_intern_prop (str, strlen (str)));
131
  if (FcPatternGetString (p, FC_FAMILY, 0, &str) == FcResultMatch)
132
    ASET (entity, FONT_FAMILY_INDEX, font_intern_prop (str, strlen (str)));
133
  if (FcPatternGetInteger (p, FC_WEIGHT, 0, &numeric) == FcResultMatch)
134
    {
135 136 137
      if (numeric >= FC_WEIGHT_REGULAR && numeric < FC_WEIGHT_MEDIUM)
	numeric = FC_WEIGHT_MEDIUM;
      FONT_SET_STYLE (entity, FONT_WEIGHT_INDEX, make_number (numeric));
138
    }
139
  if (FcPatternGetInteger (p, FC_SLANT, 0, &numeric) == FcResultMatch)
140 141 142 143
    {
      numeric += 100;
      FONT_SET_STYLE (entity, FONT_SLANT_INDEX, make_number (numeric));
    }
144
  if (FcPatternGetInteger (p, FC_WIDTH, 0, &numeric) == FcResultMatch)
145 146 147
    {
      FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_number (numeric));
    }
148 149 150 151
  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));
152 153 154 155 156 157 158 159 160 161
  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));
162 163 164 165 166 167 168 169 170

  file = FcStrCopy (file);
  if (! file)
    return Qnil;
  p = FcPatternCreate ();
  if (! p)
    return Qnil;

  if (FcPatternAddString (p, FC_FILE, file) == FcFalse
171 172
      || (charset
	  && FcPatternAddCharSet (p, FC_CHARSET, charset) == FcFalse)
173
#ifdef FC_FONTFORMAT
174 175
      || (fontformat
	  && FcPatternAddString (p, FC_FONTFORMAT, fontformat) == FcFalse)
176
#endif	/* FC_FONTFORMAT */
177
      )
178 179 180 181
    {
      FcPatternDestroy (p);
      return Qnil;
    }
182
  font_put_extra (entity, QCfont_entity, make_save_value (p, 0));
183 184 185
  return entity;
}

186

187 188 189
static Lisp_Object ftfont_generic_family_list;

static Lisp_Object
190 191
ftfont_list_generic_family (family)
     Lisp_Object family;
192 193
{
  Lisp_Object slot, list, val;
194 195 196 197 198
  FcObjectSet *objset = NULL;
  FcPattern *pattern = NULL, *pat = NULL;
  FcFontSet *fontset = NULL;
  FcChar8 *fam;
  int i, j;
199 200 201 202 203 204 205

  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))
206
    return Qnil;
207
  list = XCDR (slot);
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  if (! EQ (list, Qt))
    return list;

  objset = FcObjectSetBuild (FC_FAMILY, NULL);
  if (! objset)
    goto err;
  pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString,
			    SYMBOL_FcChar8 (family), (char *) 0);
  if (! pattern)
    goto err;
  pat = FcPatternCreate ();
  if (! pat)
    goto err;
  FcConfigSubstitute (NULL, pattern, FcMatchPattern);
  for (i = 0, list = Qnil;
       FcPatternGetString (pattern, FC_FAMILY, i, &fam) == FcResultMatch;
       i++)
225
    {
226 227 228
      if (strcmp ((char *) fam, (char *) SYMBOL_FcChar8 (family)) == 0)
	continue;
      if (! FcPatternAddString (pat, FC_FAMILY, fam))
229
	goto err;
230 231
      fontset = FcFontList (NULL, pat, objset);
      if (! fontset)
232
	goto err;
233 234 235 236 237
      if (fontset->nfont > 0)
	list = Fcons (intern ((char *) fam), list);
      FcFontSetDestroy (fontset);
      fontset = NULL;
      FcPatternDel (pat, FC_FAMILY);
238
    }
239 240 241 242 243 244 245
  XSETCDR (slot, list);
 err:
  if (pat) FcPatternDestroy (pat);
  if (pattern) FcPatternDestroy (pattern);
  if (fontset) FcFontSetDestroy (fontset);
  if (objset) FcObjectSetDestroy (objset);
  return list;
246 247
}

248
static Lisp_Object ftfont_get_cache P_ ((FRAME_PTR));
Kenichi Handa's avatar
Kenichi Handa committed
249
static Lisp_Object ftfont_list P_ ((Lisp_Object, Lisp_Object));
250
static Lisp_Object ftfont_match P_ ((Lisp_Object, Lisp_Object));
Kenichi Handa's avatar
Kenichi Handa committed
251
static Lisp_Object ftfont_list_family P_ ((Lisp_Object));
252
static Lisp_Object ftfont_open P_ ((FRAME_PTR, Lisp_Object, int));
Kenichi Handa's avatar
Kenichi Handa committed
253 254 255 256 257 258 259 260 261
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 *));
262
static Lisp_Object ftfont_shape P_ ((Lisp_Object));
Kenichi Handa's avatar
Kenichi Handa committed
263 264 265

struct font_driver ftfont_driver =
  {
266
    0,				/* Qfreetype */
267
    0,				/* case insensitive */
Kenichi Handa's avatar
Kenichi Handa committed
268 269
    ftfont_get_cache,
    ftfont_list,
270
    ftfont_match,
Kenichi Handa's avatar
Kenichi Handa committed
271
    ftfont_list_family,
272
    NULL,
Kenichi Handa's avatar
Kenichi Handa committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    ftfont_open,
    ftfont_close,
    /* We can't draw a text without device dependent functions.  */
    NULL,
    NULL,
    ftfont_has_char,
    ftfont_encode_char,
    ftfont_text_extents,
    /* We can't draw a text without device dependent functions.  */
    NULL,
    ftfont_get_bitmap,
    NULL,
    NULL,
    NULL,
    ftfont_anchor_point,
    NULL,
    NULL,
290 291
    NULL,
    NULL,
292
#if defined (HAVE_M17N_FLT) && defined (HAVE_LIBOTF)
293
    ftfont_shape
294
#else  /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
Kenichi Handa's avatar
Kenichi Handa committed
295
    NULL
296
#endif	/* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
Kenichi Handa's avatar
Kenichi Handa committed
297 298 299 300 301
  };

extern Lisp_Object QCname;

static Lisp_Object
302 303
ftfont_get_cache (f)
     FRAME_PTR f;
Kenichi Handa's avatar
Kenichi Handa committed
304 305 306 307
{
  return freetype_font_cache;
}

308 309
struct OpenTypeSpec
{
310 311
  Lisp_Object script;
  unsigned int script_tag, langsys_tag;
312 313 314 315
  int nfeatures[2];
  unsigned int *features[2];
};

316
#define OTF_SYM_TAG(SYM, TAG)					\
317
  do {								\
318 319
    unsigned char *p = SDATA (SYMBOL_NAME (SYM));		\
    TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];	\
320 321
  } while (0)

322
#define OTF_TAG_STR(TAG, P)			\
323
  do {						\
324 325 326 327
    (P)[0] = (char) (TAG >> 24);		\
    (P)[1] = (char) ((TAG >> 16) & 0xFF);	\
    (P)[2] = (char) ((TAG >> 8) & 0xFF);	\
    (P)[3] = (char) (TAG & 0xFF);		\
328
    (P)[4] = '\0';				\
329 330 331 332 333 334 335 336 337 338 339
  } while (0)

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;
340
  spec->script = XCAR (otf_spec);
341
  if (! NILP (spec->script))
342 343 344 345 346 347 348 349
    {
      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;
    }
350
  else
351
    spec->script_tag = 0x44464C54; 	/* "DFLT" */
352 353 354
  otf_spec = XCDR (otf_spec);
  val = XCAR (otf_spec);
  if (! NILP (val))
355
    OTF_SYM_TAG (val, spec->langsys_tag);
356
  else
357
    spec->langsys_tag = 0;
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
  spec->nfeatures[0] = spec->nfeatures[1] = 0;
  for (i = 0; i < 2; i++)
    {
      Lisp_Object len;

      otf_spec = XCDR (otf_spec);    
      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;
}

395 396 397 398 399
static FcPattern *
ftfont_spec_pattern (spec, otlayout, otspec)
     Lisp_Object spec;
     char *otlayout;
     struct OpenTypeSpec **otspec;
Kenichi Handa's avatar
Kenichi Handa committed
400
{
401
  Lisp_Object val, tmp, extra;
Kenichi Handa's avatar
Kenichi Handa committed
402 403 404 405
  int i;
  FcPattern *pattern = NULL;
  FcCharSet *charset = NULL;
  FcLangSet *langset = NULL;
406 407
  int n;
  int dpi = -1;
408 409
  int spacing = -1;
  int scalable = -1;
410 411 412
  Lisp_Object name = Qnil;
  Lisp_Object script = Qnil;
  Lisp_Object registry = Qunicode_bmp;
Kenichi Handa's avatar
Kenichi Handa committed
413

414
  if (! NILP (AREF (spec, FONT_ADSTYLE_INDEX))
415 416 417 418 419
      && 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)
420
    /* Fontconfig doesn't support reverse-italic/obligue.  */
421 422 423 424 425 426 427 428 429
    return NULL;

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

Kenichi Handa's avatar
Kenichi Handa committed
431 432 433 434 435 436 437
  if (! NILP (AREF (spec, FONT_REGISTRY_INDEX)))
    {
      registry = AREF (spec, FONT_REGISTRY_INDEX);
      if (EQ (registry, Qiso8859_1))
	{
	  if (! cs_iso8859_1
	      && ftfont_build_basic_charsets () < 0)
438
	    return NULL;
Kenichi Handa's avatar
Kenichi Handa committed
439 440
	  charset = cs_iso8859_1;
	}
441 442 443
      else if (! EQ (registry, Qiso10646_1)
	       && ! EQ (registry, Qunicode_bmp)
	       && ! EQ (registry, Qunicode_sip))
444
	return NULL;
Kenichi Handa's avatar
Kenichi Handa committed
445 446
    }

447
  otlayout[0] = '\0';
448 449
  for (extra = AREF (spec, FONT_EXTRA_INDEX);
       CONSP (extra); extra = XCDR (extra))
Kenichi Handa's avatar
Kenichi Handa committed
450
    {
451 452
      Lisp_Object key, val;

453 454 455 456 457 458
      key = XCAR (XCAR (extra)), val = XCDR (XCAR (extra));
      if (EQ (key, QCdpi))
	dpi = XINT (val);
      else if (EQ (key, QCfc_unknown_spec))
	name = val;
      else if (EQ (key, QClang))
Kenichi Handa's avatar
Kenichi Handa committed
459 460 461 462
	{
	  langset = FcLangSetCreate ();
	  if (! langset)
	    goto err;
463
	  if (SYMBOLP (val))
Kenichi Handa's avatar
Kenichi Handa committed
464
	    {
465
	      if (! FcLangSetAdd (langset, SYMBOL_FcChar8 (val)))
Kenichi Handa's avatar
Kenichi Handa committed
466 467 468
		goto err;
	    }
	  else
469 470 471 472
	    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
473
	}
474 475 476 477 478 479 480 481 482 483 484
      else if (EQ (key, QCname))
	name = val;
      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;
	}
485 486 487 488 489
      else if (EQ (key, QCscript))
	script = val;
      else if (EQ (key, QCscalable))
	scalable = ! NILP (val);
    }
Kenichi Handa's avatar
Kenichi Handa committed
490

491 492 493 494 495 496 497 498 499 500 501 502 503
  if (! NILP (script) && ! charset)
    {
      Lisp_Object chars = assq_no_quit (script, Vscript_representative_chars);

      if (CONSP (chars))
	{
	  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
504 505 506
	}
    }

507
  pattern = NILP (name) ? FcPatternCreate () : FcNameParse (SDATA (name));
508 509
  if (! pattern)
    goto err;
510 511
  FcPatternDel (pattern, FC_SIZE);
  FcPatternDel (pattern, FC_PIXEL_SIZE);
512
  tmp = AREF (spec, FONT_FOUNDRY_INDEX);
513
  if (! NILP (tmp)
514 515
      && ! FcPatternAddString (pattern, FC_FOUNDRY, SYMBOL_FcChar8 (tmp)))
    goto err;
Kenichi Handa's avatar
Kenichi Handa committed
516 517 518 519 520 521
  if (charset
      && ! FcPatternAddCharSet (pattern, FC_CHARSET, charset))
    goto err;
  if (langset
      && ! FcPatternAddLangSet (pattern, FC_LANG, langset))
    goto err;
522 523 524 525 526 527 528
  if (dpi >= 0
      && ! FcPatternAddDouble (pattern, FC_DPI, dpi))
    goto err;
  if (spacing >= 0
      && ! FcPatternAddInteger (pattern, FC_SPACING, spacing))
    goto err;
  if (scalable >= 0
Kenichi Handa's avatar
Kenichi Handa committed
529
      && ! FcPatternAddBool (pattern, FC_SCALABLE, scalable ? FcTrue : FcFalse))
530
    goto err;
531
  goto finish;
Kenichi Handa's avatar
Kenichi Handa committed
532

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
 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 (charset && charset != cs_iso8859_1) FcCharSetDestroy (charset);
  if (langset) FcLangSetDestroy (langset);
  return pattern;
}

static Lisp_Object
ftfont_list (frame, spec)
     Lisp_Object frame, spec;
{
  Lisp_Object val, tmp, registry, family, family_list;
  int i;
  FcPattern *pattern;
  FcFontSet *fontset = NULL;
  FcObjectSet *objset = NULL;
  double pixel_size = 0;
  int weight = -1, slant = -1, width = -1;
  double dpi = -1;
  int spacing = -1;
  int scalable = -1;
  char otlayout[15];		/* For "otlayout:XXXX" */
  struct OpenTypeSpec *otspec = NULL;
  
  if (! fc_initialized)
    {
      FcInit ();
      fc_initialized = 1;
    }

  pattern = ftfont_spec_pattern (spec, otlayout, &otspec);
  if (! pattern)
    return Qnil;
583
  objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT,
584
			     FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING, FC_SCALABLE,
585
			     FC_CHARSET, FC_FILE,
586 587 588
#ifdef FC_CAPABILITY
			     FC_CAPABILITY,
#endif	/* FC_CAPABILITY */
589 590 591 592
#ifdef FC_FONTFORMAT
			     FC_FONTFORMAT,
#endif	/* FC_FONTFORMAT */
			     NULL);
593 594
  if (! objset)
    goto err;
595 596 597 598 599 600

  registry = AREF (spec, FONT_REGISTRY_INDEX);
  family = AREF (spec, FONT_FAMILY_INDEX);
  if (NILP (family))
    family_list = Fcons (Qnil, Qnil);
  else
601
    {
602 603 604
      family_list = ftfont_list_generic_family (family);
      if (NILP (family_list))
	family_list = Fcons (family, Qnil);
605
    }
606

607
  for (val = Qnil; CONSP (family_list); family_list = XCDR (family_list))
608
    {
609 610 611 612 613 614 615 616 617 618 619
      family = XCAR (family_list);
      if (! NILP (family))
	{
	  FcPatternDel (pattern, FC_FAMILY);
	  if (! FcPatternAddString (pattern, FC_FAMILY, SYMBOL_FcChar8 (family)))
	    goto err;
	}
      fontset = FcFontList (NULL, pattern, objset);
      if (! fontset)
	goto err;
      for (i = 0; i < fontset->nfont; i++)
Kenichi Handa's avatar
Kenichi Handa committed
620
	{
621
	  Lisp_Object entity;
622 623
	  int n;
	  double dbl;
624

625
#ifdef FC_CAPABILITY
626
	  if (otlayout[0])
627 628 629 630 631
	    {
	      FcChar8 *this;

	      if (FcPatternGetString (fontset->fonts[i], FC_CAPABILITY, 0,
				      &this) != FcResultMatch
632
		  || ! strstr ((char *) this, otlayout))
633 634
		continue;
	    }
635
#endif	/* FC_CAPABILITY */
636 637 638 639 640 641 642 643 644 645 646 647
#ifdef HAVE_LIBOTF
	  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;
648 649
	      if (OTF_check_features (otf, 1,
				      otspec->script_tag, otspec->langsys_tag,
650 651
				      otspec->features[0],
				      otspec->nfeatures[0]) != 1
652 653
		  || OTF_check_features (otf, 0,
					 otspec->script_tag, otspec->langsys_tag,
654 655 656 657 658
					 otspec->features[1],
					 otspec->nfeatures[1]) != 1)
		continue;
	    }
#endif	/* HAVE_LIBOTF */
659
	  entity = ftfont_pattern_entity (fontset->fonts[i], registry);
660 661
	  if (! NILP (entity))
	    val = Fcons (entity, val);
Kenichi Handa's avatar
Kenichi Handa committed
662
	}
663 664
      FcFontSetDestroy (fontset);
      fontset = NULL;
Kenichi Handa's avatar
Kenichi Handa committed
665 666 667 668 669
    }
  goto finish;

 err:
  /* We come here because of unexpected error in fontconfig API call
670
     (usually insufficient memory).  */
Kenichi Handa's avatar
Kenichi Handa committed
671 672 673 674 675 676 677 678 679
  val = Qnil;

 finish:
  if (objset) FcObjectSetDestroy (objset);
  if (fontset) FcFontSetDestroy (fontset);
  if (pattern) FcPatternDestroy (pattern);
  return val;
}

680 681 682 683 684
static Lisp_Object
ftfont_match (frame, spec)
     Lisp_Object frame, spec;
{
  Lisp_Object extra, val, entity;
685
  FcPattern *pattern, *match = NULL;
686
  FcResult result;
687 688
  char otlayout[15];		/* For "otlayout:XXXX" */
  struct OpenTypeSpec *otspec = NULL;
689 690 691 692 693 694 695

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

696 697
  pattern = ftfont_spec_pattern (spec, otlayout, &otspec);
  if (! pattern)
698 699
    return Qnil;

700
  if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
701
    {
702
      FcValue value;
703

704 705 706 707 708 709 710 711 712
      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)
713
	{
714 715 716 717 718 719 720 721
	  entity = ftfont_pattern_entity (match, Qunicode_bmp);
	  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;
722 723
	}
    }
724
  FcPatternDestroy (pattern);
725 726 727 728

  return entity;
}

Kenichi Handa's avatar
Kenichi Handa committed
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
static Lisp_Object
ftfont_list_family (frame)
     Lisp_Object frame;
{
  Lisp_Object list;
  FcPattern *pattern = NULL;
  FcFontSet *fontset = NULL;
  FcObjectSet *objset = NULL;
  int i;

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

  pattern = FcPatternCreate ();
  if (! pattern)
    goto finish;
748
  objset = FcObjectSetBuild (FC_FAMILY, NULL);
Kenichi Handa's avatar
Kenichi Handa committed
749 750 751 752 753 754 755 756 757 758 759 760 761
  if (! objset)
    goto finish;
  fontset = FcFontList (NULL, pattern, objset);
  if (! fontset)
    goto finish;

  list = Qnil;
  for (i = 0; i < fontset->nfont; i++)
    {
      FcPattern *pat = fontset->fonts[i];
      FcChar8 *str;

      if (FcPatternGetString (pat, FC_FAMILY, 0, &str) == FcResultMatch)
762
	list = Fcons (intern ((char *) str), list);
Kenichi Handa's avatar
Kenichi Handa committed
763 764 765 766 767 768 769 770 771 772 773
    }

 finish:
  if (objset) FcObjectSetDestroy (objset);
  if (fontset) FcFontSetDestroy (fontset);
  if (pattern) FcPatternDestroy (pattern);

  return list;
}


774
static Lisp_Object
Kenichi Handa's avatar
Kenichi Handa committed
775 776 777 778 779 780 781 782 783 784
ftfont_open (f, entity, pixel_size)
     FRAME_PTR f;
     Lisp_Object entity;
     int pixel_size;
{
  struct ftfont_info *ftfont_info;
  struct font *font;
  FT_Face ft_face;
  FT_Size ft_size;
  FT_UInt size;
785
  Lisp_Object val, font_object;
Kenichi Handa's avatar
Kenichi Handa committed
786
  FcPattern *pattern;
787 788
  FcChar8 *file = NULL, *fmt = NULL;
  FcBool scalable;
Kenichi Handa's avatar
Kenichi Handa committed
789
  int spacing;
790 791 792
  char name[256];
  int i, len;
  int upEM;
Kenichi Handa's avatar
Kenichi Handa committed
793

794 795 796 797
  val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
  if (! CONSP (val))
    return Qnil;
  val = XCDR (val);
Kenichi Handa's avatar
Kenichi Handa committed
798 799 800 801 802 803
  pattern = XSAVE_VALUE (val)->pointer;
  if (XSAVE_VALUE (val)->integer == 0)
    {
      /* We have not yet created FT_Face for this font.  */
      if (! ft_library
	  && FT_Init_FreeType (&ft_library) != 0)
804
	return Qnil;
Kenichi Handa's avatar
Kenichi Handa committed
805
      if (FcPatternGetString (pattern, FC_FILE, 0, &file) != FcResultMatch)
806
	return Qnil;
Kenichi Handa's avatar
Kenichi Handa committed
807
      if (FT_New_Face (ft_library, (char *) file, 0, &ft_face) != 0)
808
	return Qnil;
Kenichi Handa's avatar
Kenichi Handa committed
809 810
      FcPatternAddFTFace (pattern, FC_FT_FACE, ft_face);
      ft_size = ft_face->size;
811
      XSAVE_VALUE (val)->integer++;
Kenichi Handa's avatar
Kenichi Handa committed
812 813 814 815 816
    }
  else
    {
      if (FcPatternGetFTFace (pattern, FC_FT_FACE, 0, &ft_face)
	  != FcResultMatch)
817
	return Qnil;
Kenichi Handa's avatar
Kenichi Handa committed
818
      if (FT_New_Size (ft_face, &ft_size) != 0)
819
	return Qnil;
Kenichi Handa's avatar
Kenichi Handa committed
820 821 822
      if (FT_Activate_Size (ft_size) != 0)
	{
	  FT_Done_Size (ft_size);
823
	  return Qnil;
Kenichi Handa's avatar
Kenichi Handa committed
824 825 826 827 828 829 830 831 832 833
	}
    } 

  size = XINT (AREF (entity, FONT_SIZE_INDEX));
  if (size == 0)
    size = pixel_size;
  if (FT_Set_Pixel_Sizes (ft_face, size, size) != 0)
    {
      if (XSAVE_VALUE (val)->integer == 0)
	FT_Done_Face (ft_face);
834
      return Qnil;
Kenichi Handa's avatar
Kenichi Handa committed
835 836
    }

837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
  font_object = font_make_object (VECSIZE (struct ftfont_info));
  ASET (font_object, FONT_TYPE_INDEX, Qfreetype);
  for (i = 1;i < FONT_ENTITY_MAX; i++)
    ASET (font_object, i, AREF (entity, i));
  ASET (font_object, FONT_SIZE_INDEX, make_number (size));
  len = font_unparse_xlfd (entity, size, name, 256);
  if (len > 0)
    ASET (font_object, FONT_NAME_INDEX, make_unibyte_string (name, len));
  len = font_unparse_fcname (entity, size, name, 256);
  if (len > 0)
    ASET (font_object, FONT_FULLNAME_INDEX, make_unibyte_string (name, len));
  else
    ASET (font_object, FONT_FULLNAME_INDEX,
	  AREF (font_object, FONT_NAME_INDEX));
  if (! file
      && ! FcPatternGetString (pattern, FC_FILE, 0, &file) != FcResultMatch)
    return Qnil;
  ASET (font_object, FONT_FILE_INDEX,
	make_unibyte_string ((char *) file, strlen ((char *) file)));
  ASET (font_object, FONT_FORMAT_INDEX, ftfont_font_format (pattern));
  font = XFONT_OBJECT (font_object);
  ftfont_info = (struct ftfont_info *) font;
Kenichi Handa's avatar
Kenichi Handa committed
859
  ftfont_info->ft_size = ft_size;
860
#ifdef HAVE_LIBOTF
861 862
  ftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT;
  ftfont_info->otf = NULL;
863
#endif	/* HAVE_LIBOTF */
Kenichi Handa's avatar
Kenichi Handa committed
864 865
  font->pixel_size = size;
  font->driver = &ftfont_driver;
866
  font->encoding_charset = font->repertory_charset = -1;
867

868 869 870 871 872 873 874 875 876 877 878 879 880 881
  upEM = ft_face->units_per_EM;
  if (! FcPatternGetBool (pattern, FC_SCALABLE, 0, &scalable) == FcResultMatch)
    scalable = FcFalse;
  if (scalable)
    {
      font->ascent = ft_face->ascender * size / upEM;
      font->descent = - ft_face->descender * size / upEM;
      font->height = ft_face->height * size / upEM;
    }
  else
    {
      font->ascent = ft_face->size->metrics.ascender >> 6;
      font->descent = - ft_face->size->metrics.descender >> 6;
      font->height = ft_face->size->metrics.height >> 6;
882 883 884 885
    }
  if (FcPatternGetInteger (pattern, FC_SPACING, 0, &spacing) != FcResultMatch)
    spacing = FC_PROPORTIONAL;
  if (spacing != FC_PROPORTIONAL)
886 887 888
    font->min_width = font->average_width = font->space_width
      = (scalable ? ft_face->max_advance_width * size / upEM
	 : ft_face->size->metrics.max_advance >> 6);
Kenichi Handa's avatar
Kenichi Handa committed
889 890
  else
    {
891
      int n;
Kenichi Handa's avatar
Kenichi Handa committed
892

893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
      font->min_width = font->average_width = font->space_width = 0;
      for (i = 32, n = 0; i < 127; i++)
	if (FT_Load_Char (ft_face, i, FT_LOAD_DEFAULT) != 0)
	  {
	    int this_width = ft_face->glyph->metrics.horiAdvance >> 6;

	    if (this_width > 0
		&& (! font->min_width || font->min_width > this_width))
	      font->min_width = this_width;
	    if (i == 32)
	      font->space_width = this_width;
	    font->average_width += this_width;
	    n++;
	  }
      if (n > 0)
	font->average_width /= n;
Kenichi Handa's avatar
Kenichi Handa committed
909 910
    }

911 912 913 914 915 916 917 918 919 920 921 922 923 924
  font->baseline_offset = 0;
  font->relative_compose = 0;
  font->default_ascent = 0;
  font->vertical_centering = 0;
  if (scalable)
    {
      font->underline_position = -ft_face->underline_position * size / upEM;
      font->underline_thickness = -ft_face->underline_thickness * size / upEM;
    }
  else
    {
      font->underline_position = -1;
      font->underline_thickness = 0;
    }
Kenichi Handa's avatar
Kenichi Handa committed
925

926
  return font_object;
Kenichi Handa's avatar
Kenichi Handa committed
927 928 929 930 931 932 933 934
}

static void
ftfont_close (f, font)
     FRAME_PTR f;
     struct font *font;
{
  struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
935
  Lisp_Object val;
Kenichi Handa's avatar
Kenichi Handa committed
936

937 938
  val = assq_no_quit (QCfont_entity, font->props[FONT_EXTRA_INDEX]);
  val = XCDR (val);
Kenichi Handa's avatar
Kenichi Handa committed
939 940
  (XSAVE_VALUE (val)->integer)--;
  if (XSAVE_VALUE (val)->integer == 0)
941 942 943 944 945 946 947
    {
      FT_Done_Face (ftfont_info->ft_size->face);
#ifdef HAVE_LIBOTF
      if (ftfont_info->otf)
	OTF_close (ftfont_info->otf);
#endif
    }
Kenichi Handa's avatar
Kenichi Handa committed
948 949 950 951 952 953 954 955 956 957 958 959 960
  else
    FT_Done_Size (ftfont_info->ft_size);
}

static int 
ftfont_has_char (entity, c)
     Lisp_Object entity;
     int c;
{
  Lisp_Object val;
  FcPattern *pattern;
  FcCharSet *charset;

961 962
  val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
  val = XCDR (val);
Kenichi Handa's avatar
Kenichi Handa committed
963
  pattern = XSAVE_VALUE (val)->pointer;
964 965
  if (FcPatternGetCharSet (pattern, FC_CHARSET, 0, &charset) != FcResultMatch)
    return -1;
Kenichi Handa's avatar
Kenichi Handa committed
966 967 968 969 970 971 972 973 974 975 976 977 978
  return (FcCharSetHasChar (charset, (FcChar32) c) == FcTrue);
}

static unsigned
ftfont_encode_char (font, c)
     struct font *font;
     int c;
{
  struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
  FT_Face ft_face = ftfont_info->ft_size->face;
  FT_ULong charcode = c;
  FT_UInt code = FT_Get_Char_Index (ft_face, charcode);

979
  return (code > 0 ? code : FONT_INVALID_CODE);
Kenichi Handa's avatar
Kenichi Handa committed
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
}

static int
ftfont_text_extents (font, code, nglyphs, metrics)
     struct font *font;
     unsigned *code;
     int nglyphs;
     struct font_metrics *metrics;
{
  struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
  FT_Face ft_face = ftfont_info->ft_size->face;
  int width = 0;
  int i;

  if (ftfont_info->ft_size != ft_face->size)
    FT_Activate_Size (ftfont_info->ft_size);
  if (metrics)
    bzero (metrics, sizeof (struct font_metrics));
  for (i = 0; i < nglyphs; i++)
    {
      if (FT_Load_Glyph (ft_face, code[i], FT_LOAD_DEFAULT) == 0)
	{
	  FT_Glyph_Metrics *m = &ft_face->glyph->metrics;

	  if (metrics)
	    {
	      if (metrics->lbearing > width + (m->horiBearingX >> 6))
		metrics->lbearing = width + (m->horiBearingX >> 6);
	      if (metrics->rbearing
		  < width + ((m->horiBearingX + m->width) >> 6))
		metrics->rbearing
		  = width + ((m->horiBearingX + m->width) >> 6);
	      if (metrics->ascent < (m->horiBearingY >> 6))
		metrics->ascent = m->horiBearingY >> 6;
	      if (metrics->descent > ((m->horiBearingY + m->height) >> 6))
		metrics->descent = (m->horiBearingY + m->height) >> 6;
	    }
	  width += m->horiAdvance >> 6;
	}
      else
	{
1021
	  width += font->space_width;
Kenichi Handa's avatar
Kenichi Handa committed
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
	}
    }
  if (metrics)
    metrics->width = width;

  return width;
}

static int
ftfont_get_bitmap (font, code, bitmap, bits_per_pixel)
     struct font *font;
     unsigned code;
     struct font_bitmap *bitmap;
     int bits_per_pixel;
{
  struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
  FT_Face ft_face = ftfont_info->ft_size->face;
  FT_Int32 load_flags = FT_LOAD_RENDER;

  if (ftfont_info->ft_size != ft_face->size)
    FT_Activate_Size (ftfont_info->ft_size);
  if (bits_per_pixel == 1)
    {
#ifdef FT_LOAD_TARGET_MONO
      load_flags |= FT_LOAD_TARGET_MONO;
#else
      load_flags |= FT_LOAD_MONOCHROME;
#endif
    }
  else if (bits_per_pixel != 8)
    /* We don't support such a rendering.  */
    return -1;

  if (FT_Load_Glyph (ft_face, code, load_flags) != 0)
    return -1;
1057 1058 1059 1060 1061 1062 1063 1064 1065
  bitmap->bits_per_pixel
    = (ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO ? 1
       : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY ? 8
       : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD ? 8
       : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD_V ? 8
       : -1);
  if (bitmap->bits_per_pixel < 0)
    /* We don't suport that kind of pixel mode.  */
    return -1;
Kenichi Handa's avatar
Kenichi Handa committed
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
  bitmap->rows = ft_face->glyph->bitmap.rows;
  bitmap->width = ft_face->glyph->bitmap.width;
  bitmap->pitch = ft_face->glyph->bitmap.pitch;
  bitmap->buffer = ft_face->glyph->bitmap.buffer;
  bitmap->left = ft_face->glyph->bitmap_left;
  bitmap->top = ft_face->glyph->bitmap_top;
  bitmap->advance = ft_face->glyph->metrics.horiAdvance >> 6;
  bitmap->extra = NULL;

  return 0;
}

static int
ftfont_anchor_point (font, code, index, x, y)
     struct font *font;
     unsigned code;
     int index;
     int *x, *y;
{
  struct ftfont_info *ftfont_info = (struct ftfont_info *) font;
  FT_Face ft_face = ftfont_info->ft_size->face;

  if (ftfont_info->ft_size != ft_face->size)
    FT_Activate_Size (ftfont_info->ft_size);
  if (FT_Load_Glyph (ft_face, code, FT_LOAD_DEFAULT) != 0)
    return -1;
  if (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
    return -1;
  if (index >= ft_face->glyph->outline.n_points)
    return -1;
  *x = ft_face->glyph->outline.points[index].x;
  *y = ft_face->glyph->outline.points[index].y;
  return 0;
}

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145