ftfont.c 46.9 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
     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 35 36

#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"
37
#include "ftfont.h"
Kenichi Handa's avatar
Kenichi Handa committed
38

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

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

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

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

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

Kenichi Handa's avatar
Kenichi Handa committed
54 55
/* Cache for FT_Face */
static Lisp_Object ft_face_cache;
Kenichi Handa's avatar
Kenichi Handa committed
56 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;
  FT_Size ft_size;
Kenichi Handa's avatar
Kenichi Handa committed
64
  int fc_charset_idx;
65 66 67 68
#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
69 70
};

Kenichi Handa's avatar
Kenichi Handa committed
71 72
static Lisp_Object ftfont_pattern_entity P_ ((FcPattern *, Lisp_Object, int));

73
static Lisp_Object ftfont_resolve_generic_family P_ ((Lisp_Object));
74
Lisp_Object ftfont_font_format P_ ((FcPattern *));
75 76 77

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

Kenichi Handa's avatar
Kenichi Handa committed
78
static struct
Kenichi Handa's avatar
Kenichi Handa committed
79
{
Kenichi Handa's avatar
Kenichi Handa committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  /* charset name */
  char *name;
  /* characters to distinguish the charset from the others */
  int uniquifier[6];
  /* set in syms_of_ftfont */
  int charset_id;
  /* set on demand */
  FcCharSet *fc_charset;
} fc_charset_table[] =
  { { "iso-8859-1", { 0x00A0, 0x00A1, 0x00B4, 0x00BC, 0x00D0 }, -1 },
    { "iso-8859-2", { 0x00A0, 0x010E }},
    { "iso-8859-3", { 0x00A0, 0x0108 }},
    { "iso-8859-4", { 0x00A0, 0x00AF, 0x0128, 0x0156, 0x02C7 }},
    { "iso-8859-5", { 0x00A0, 0x0401 }},
    { "iso-8859-6", { 0x00A0, 0x060C }},
    { "iso-8859-7", { 0x00A0, 0x0384 }},
    { "iso-8859-8", { 0x00A0, 0x05D0 }},
    { "iso-8859-9", { 0x00A0, 0x00A1, 0x00BC, 0x011E }},
    { "iso-8859-10", { 0x00A0, 0x00D0, 0x0128, 0x2015 }},
    { "iso-8859-11", { 0x00A0, 0x0E01 }},
    { "iso-8859-13", { 0x00A0, 0x201C }},
    { "iso-8859-14", { 0x00A0, 0x0174 }},
    { "iso-8859-15", { 0x00A0, 0x00A1, 0x00D0, 0x0152 }},
    { "iso-8859-16", { 0x00A0, 0x0218}},
    { "chinese-gb2312", { 0x4E13 }},
    { "big5", { 0xF6B1 }},
    { "japanese-jisx0208", { 0x4E55 }},
    { "korean-ksc5601", { 0xAC00 }},
    { "chinese-cns11643-1", { 0xFE32 }},
    { "chinese-cns11643-2", { 0x4E33, 0x7934 }},
    { "chinese-cns11643-3", { 0x201A9 }},
    { "chinese-cns11643-4", { 0x20057 }},
    { "chinese-cns11643-5", { 0x20000 }},
    { "chinese-cns11643-6", { 0x20003 }},
    { "chinese-cns11643-7", { 0x20055 }},
    { "chinese-gbk", { 0x4E06 }},
    { "japanese-jisx0212", { 0x4E44 }},
    { "japanese-jisx0213-1", { 0xFA10 }},
    { "japanese-jisx0213-2", { 0xFA49 }},
    { "japanese-jisx0213.2004-1", { 0x20B9F }},
    { "viscii", { 0x1EA0, 0x1EAE, 0x1ED2 }},
    { "tis620", { 0x0E01 }},
    { "windows-1251", { 0x0401, 0x0490 }},
    { "koi8-r", { 0x0401, 0x2219 }},
    { "mule-lao", { 0x0E81 }},
    { NULL }
  };
Kenichi Handa's avatar
Kenichi Handa committed
127

128 129
extern Lisp_Object Qc, Qm, Qp, Qd;

130
static Lisp_Object
Kenichi Handa's avatar
Kenichi Handa committed
131
ftfont_pattern_entity (p, registry, fc_charset_idx)
132
     FcPattern *p;
133
     Lisp_Object registry;
Kenichi Handa's avatar
Kenichi Handa committed
134
     int fc_charset_idx;
135 136
{
  Lisp_Object entity;
Kenichi Handa's avatar
Kenichi Handa committed
137
  char *file, *str;
138 139
  int numeric;
  double dbl;
140
  FcBool b;
141

Kenichi Handa's avatar
Kenichi Handa committed
142
  if (FcPatternGetString (p, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
143 144
    return Qnil;

145
  entity = font_make_entity ();
146 147 148 149

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

Kenichi Handa's avatar
Kenichi Handa committed
150
  if (FcPatternGetString (p, FC_FOUNDRY, 0, (FcChar8 **) &str) == FcResultMatch)
151
    ASET (entity, FONT_FOUNDRY_INDEX, font_intern_prop (str, strlen (str)));
Kenichi Handa's avatar
Kenichi Handa committed
152
  if (FcPatternGetString (p, FC_FAMILY, 0, (FcChar8 **) &str) == FcResultMatch)
153
    ASET (entity, FONT_FAMILY_INDEX, font_intern_prop (str, strlen (str)));
154
  if (FcPatternGetInteger (p, FC_WEIGHT, 0, &numeric) == FcResultMatch)
155
    {
156 157 158
      if (numeric >= FC_WEIGHT_REGULAR && numeric < FC_WEIGHT_MEDIUM)
	numeric = FC_WEIGHT_MEDIUM;
      FONT_SET_STYLE (entity, FONT_WEIGHT_INDEX, make_number (numeric));
159
    }
160
  if (FcPatternGetInteger (p, FC_SLANT, 0, &numeric) == FcResultMatch)
161 162 163 164
    {
      numeric += 100;
      FONT_SET_STYLE (entity, FONT_SLANT_INDEX, make_number (numeric));
    }
165
  if (FcPatternGetInteger (p, FC_WIDTH, 0, &numeric) == FcResultMatch)
166 167 168
    {
      FONT_SET_STYLE (entity, FONT_WIDTH_INDEX, make_number (numeric));
    }
169 170 171 172
  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));
173 174 175 176 177 178 179 180 181 182
  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));
183

Kenichi Handa's avatar
Kenichi Handa committed
184 185 186 187
  font_put_extra (entity, QCfont_entity,
		  Fcons (make_unibyte_string ((char *) file,
					      strlen ((char *) file)),
			 make_number (fc_charset_idx)));
188 189 190
  return entity;
}

191

192 193 194
static Lisp_Object ftfont_generic_family_list;

static Lisp_Object
195
ftfont_resolve_generic_family (family)
196
     Lisp_Object family;
197
{
198 199 200
  Lisp_Object slot;
  FcPattern *pattern = NULL, *match;
  FcResult result;
201

202
  family = Fintern (Fdowncase (SYMBOL_NAME (family)), Qnil);
203 204 205 206 207 208
  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))
209
    return Qnil;
210 211
  if (! EQ (XCDR (slot), Qt))
    return XCDR (slot);
212 213 214 215 216
  pattern = FcPatternBuild (NULL, FC_FAMILY, FcTypeString,
			    SYMBOL_FcChar8 (family), (char *) 0);
  if (! pattern)
    goto err;
  FcConfigSubstitute (NULL, pattern, FcMatchPattern);
217 218 219
  FcDefaultSubstitute (pattern);
  match = FcFontMatch (NULL, pattern, &result);
  if (match)
220
    {
221 222 223 224
      FcChar8 *fam;

      if (FcPatternGetString (match, FC_FAMILY, 0, &fam) == FcResultMatch)
	family = intern ((char *) fam);
225
    }
226 227 228
  else
    family = Qnil;
  XSETCDR (slot, family);
229
 err:
230
  if (match) FcPatternDestroy (match);
231
  if (pattern) FcPatternDestroy (pattern);
232
  return family;
233 234
}

Kenichi Handa's avatar
Kenichi Handa committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
Lisp_Object
ftfont_lookup_cache (filename)
     Lisp_Object filename;
{
  Lisp_Object cache, val;

  cache = assoc_no_quit (filename, ft_face_cache);
  if (NILP (cache))
    {
      val = make_save_value (NULL, 0);
      cache = Fcons (filename, val);
      ft_face_cache = Fcons (cache, ft_face_cache);
    }
  else
    val = XCDR (cache);
  if (! XSAVE_VALUE (val)->pointer)
    {
      FT_Face ft_face;

      if (! ft_library
	  && FT_Init_FreeType (&ft_library) != 0)
	return Qnil;
      if (FT_New_Face (ft_library, (char *) SDATA (filename), 0, &ft_face) != 0)
	return Qnil;
      XSAVE_VALUE (val)->pointer = ft_face;
    }
  return cache;
}

264
static Lisp_Object ftfont_get_cache P_ ((FRAME_PTR));
Kenichi Handa's avatar
Kenichi Handa committed
265
static Lisp_Object ftfont_list P_ ((Lisp_Object, Lisp_Object));
266
static Lisp_Object ftfont_match P_ ((Lisp_Object, Lisp_Object));
Kenichi Handa's avatar
Kenichi Handa committed
267
static Lisp_Object ftfont_list_family P_ ((Lisp_Object));
268
static Lisp_Object ftfont_open P_ ((FRAME_PTR, Lisp_Object, int));
Kenichi Handa's avatar
Kenichi Handa committed
269 270 271 272 273 274 275 276 277
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 *));
278
static Lisp_Object ftfont_shape P_ ((Lisp_Object));
Kenichi Handa's avatar
Kenichi Handa committed
279 280 281

struct font_driver ftfont_driver =
  {
282
    0,				/* Qfreetype */
283
    0,				/* case insensitive */
Kenichi Handa's avatar
Kenichi Handa committed
284 285
    ftfont_get_cache,
    ftfont_list,
286
    ftfont_match,
Kenichi Handa's avatar
Kenichi Handa committed
287
    ftfont_list_family,
288
    NULL,
Kenichi Handa's avatar
Kenichi Handa committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    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,
306 307
    NULL,
    NULL,
308
#if defined (HAVE_M17N_FLT) && defined (HAVE_LIBOTF)
309
    ftfont_shape
310
#else  /* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
Kenichi Handa's avatar
Kenichi Handa committed
311
    NULL
312
#endif	/* not (HAVE_M17N_FLT && HAVE_LIBOTF) */
Kenichi Handa's avatar
Kenichi Handa committed
313 314 315 316 317
  };

extern Lisp_Object QCname;

static Lisp_Object
318 319
ftfont_get_cache (f)
     FRAME_PTR f;
Kenichi Handa's avatar
Kenichi Handa committed
320 321 322 323
{
  return freetype_font_cache;
}

Kenichi Handa's avatar
Kenichi Handa committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
static int
ftfont_get_charset (registry)
     Lisp_Object registry;
{
  struct charset *encoding;
  int i, j;

  if (font_registry_charsets (registry, &encoding, NULL) < 0)
    return -1;
  if (fc_charset_table[0].charset_id < 0)
    /* Setup charset_id field of all elements.  */
    for (i = 0; fc_charset_table[i].name; i++)
      {
	Lisp_Object sym = intern (fc_charset_table[i].name);

	if (CHARSETP (sym))
	  fc_charset_table[i].charset_id = XINT (CHARSET_SYMBOL_ID (sym));
	else
	  fc_charset_table[i].charset_id = -1;
      }

  for (i = 0; fc_charset_table[i].name; i++)
    if (encoding->id == fc_charset_table[i].charset_id)
      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;
	  }
      fc_charset_table[i].fc_charset = charset;      
    }
  return i;
}

368 369
struct OpenTypeSpec
{
370 371
  Lisp_Object script;
  unsigned int script_tag, langsys_tag;
372 373 374 375
  int nfeatures[2];
  unsigned int *features[2];
};

376
#define OTF_SYM_TAG(SYM, TAG)					\
377
  do {								\
378 379
    unsigned char *p = SDATA (SYMBOL_NAME (SYM));		\
    TAG = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];	\
380 381
  } while (0)

382
#define OTF_TAG_STR(TAG, P)			\
383
  do {						\
384 385 386 387
    (P)[0] = (char) (TAG >> 24);		\
    (P)[1] = (char) ((TAG >> 16) & 0xFF);	\
    (P)[2] = (char) ((TAG >> 8) & 0xFF);	\
    (P)[3] = (char) (TAG & 0xFF);		\
388
    (P)[4] = '\0';				\
389 390 391 392 393 394 395 396 397 398 399
  } 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;
400
  spec->script = XCAR (otf_spec);
401
  if (! NILP (spec->script))
402 403 404 405 406 407 408 409
    {
      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;
    }
410
  else
411
    spec->script_tag = 0x44464C54; 	/* "DFLT" */
412 413 414
  otf_spec = XCDR (otf_spec);
  val = XCAR (otf_spec);
  if (! NILP (val))
415
    OTF_SYM_TAG (val, spec->langsys_tag);
416
  else
417
    spec->langsys_tag = 0;
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
  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;
}

Kenichi Handa's avatar
Kenichi Handa committed
455 456 457
static FcPattern *ftfont_spec_pattern P_ ((Lisp_Object, int *, char *,
					   struct OpenTypeSpec **));

458
static FcPattern *
Kenichi Handa's avatar
Kenichi Handa committed
459
ftfont_spec_pattern (spec, fc_charset_idx, otlayout, otspec)
460
     Lisp_Object spec;
Kenichi Handa's avatar
Kenichi Handa committed
461
     int *fc_charset_idx;
462 463
     char *otlayout;
     struct OpenTypeSpec **otspec;
Kenichi Handa's avatar
Kenichi Handa committed
464
{
Kenichi Handa's avatar
Kenichi Handa committed
465
  Lisp_Object tmp, extra;
Kenichi Handa's avatar
Kenichi Handa committed
466 467 468
  FcPattern *pattern = NULL;
  FcCharSet *charset = NULL;
  FcLangSet *langset = NULL;
469 470
  int n;
  int dpi = -1;
471
  int scalable = -1;
472 473
  Lisp_Object name = Qnil;
  Lisp_Object script = Qnil;
Kenichi Handa's avatar
Kenichi Handa committed
474
  Lisp_Object registry;
Kenichi Handa's avatar
Kenichi Handa committed
475

476
  if (! NILP (AREF (spec, FONT_ADSTYLE_INDEX))
477 478 479 480 481
      && 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)
482
    /* Fontconfig doesn't support reverse-italic/obligue.  */
483 484 485 486 487 488 489
    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;
490

Kenichi Handa's avatar
Kenichi Handa committed
491 492 493 494 495 496 497
  registry = AREF (spec, FONT_REGISTRY_INDEX);
  if (NILP (registry)
      || EQ (registry, Qiso10646_1)
      || EQ (registry, Qunicode_bmp)
      || EQ (registry, Qunicode_sip))
    *fc_charset_idx = -1;
  else
Kenichi Handa's avatar
Kenichi Handa committed
498
    {
Kenichi Handa's avatar
Kenichi Handa committed
499 500
      *fc_charset_idx = ftfont_get_charset (registry);
      if (*fc_charset_idx < 0)
501
	return NULL;
Kenichi Handa's avatar
Kenichi Handa committed
502
      charset = fc_charset_table[*fc_charset_idx].fc_charset;
Kenichi Handa's avatar
Kenichi Handa committed
503 504
    }

505
  otlayout[0] = '\0';
506 507
  for (extra = AREF (spec, FONT_EXTRA_INDEX);
       CONSP (extra); extra = XCDR (extra))
Kenichi Handa's avatar
Kenichi Handa committed
508
    {
509 510
      Lisp_Object key, val;

511 512 513 514 515 516
      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
517 518 519 520
	{
	  langset = FcLangSetCreate ();
	  if (! langset)
	    goto err;
521
	  if (SYMBOLP (val))
Kenichi Handa's avatar
Kenichi Handa committed
522
	    {
523
	      if (! FcLangSetAdd (langset, SYMBOL_FcChar8 (val)))
Kenichi Handa's avatar
Kenichi Handa committed
524 525 526
		goto err;
	    }
	  else
527 528 529 530
	    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
531
	}
532 533 534 535 536 537 538 539 540 541 542
      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;
	}
543 544 545 546 547
      else if (EQ (key, QCscript))
	script = val;
      else if (EQ (key, QCscalable))
	scalable = ! NILP (val);
    }
Kenichi Handa's avatar
Kenichi Handa committed
548

549 550 551 552 553 554 555 556 557 558 559 560 561
  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
562 563 564
	}
    }

565
  pattern = NILP (name) ? FcPatternCreate () : FcNameParse (SDATA (name));
566 567
  if (! pattern)
    goto err;
568 569
  FcPatternDel (pattern, FC_SIZE);
  FcPatternDel (pattern, FC_PIXEL_SIZE);
570 571
  FcPatternDel (pattern, FC_FOUNDRY);
  FcPatternDel (pattern, FC_FAMILY);
572
  tmp = AREF (spec, FONT_FOUNDRY_INDEX);
573
  if (! NILP (tmp)
574 575
      && ! FcPatternAddString (pattern, FC_FOUNDRY, SYMBOL_FcChar8 (tmp)))
    goto err;
576 577 578 579
  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
580 581 582 583 584 585
  if (charset
      && ! FcPatternAddCharSet (pattern, FC_CHARSET, charset))
    goto err;
  if (langset
      && ! FcPatternAddLangSet (pattern, FC_LANG, langset))
    goto err;
586 587 588 589
  if (dpi >= 0
      && ! FcPatternAddDouble (pattern, FC_DPI, dpi))
    goto err;
  if (scalable >= 0
Kenichi Handa's avatar
Kenichi Handa committed
590
      && ! FcPatternAddBool (pattern, FC_SCALABLE, scalable ? FcTrue : FcFalse))
591
    goto err;
592
  goto finish;
Kenichi Handa's avatar
Kenichi Handa committed
593

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
 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);
614
  if (charset && *fc_charset_idx < 0) FcCharSetDestroy (charset);
615 616 617 618 619 620 621
  return pattern;
}

static Lisp_Object
ftfont_list (frame, spec)
     Lisp_Object frame, spec;
{
622
  Lisp_Object val = Qnil, registry, family;
623 624 625 626
  int i;
  FcPattern *pattern;
  FcFontSet *fontset = NULL;
  FcObjectSet *objset = NULL;
Kenichi Handa's avatar
Kenichi Handa committed
627
  int fc_charset_idx;
628 629
  char otlayout[15];		/* For "otlayout:XXXX" */
  struct OpenTypeSpec *otspec = NULL;
630
  int spacing = -1;
631 632 633 634 635 636 637
  
  if (! fc_initialized)
    {
      FcInit ();
      fc_initialized = 1;
    }

Kenichi Handa's avatar
Kenichi Handa committed
638
  pattern = ftfont_spec_pattern (spec, &fc_charset_idx, otlayout, &otspec);
639 640
  if (! pattern)
    return Qnil;
641 642
  if (INTEGERP (AREF (spec, FONT_SPACING_INDEX)))
    spacing = XINT (AREF (spec, FONT_SPACING_INDEX));
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
  registry = AREF (spec, FONT_REGISTRY_INDEX);
  family = AREF (spec, FONT_FAMILY_INDEX);
  if (! NILP (family))
    {
      Lisp_Object resolved;

      resolved = ftfont_resolve_generic_family (family);
      if (! NILP (resolved))
	{
	  FcPatternDel (pattern, FC_FAMILY);
	  if (! FcPatternAddString (pattern, FC_FAMILY,
				    SYMBOL_FcChar8 (resolved)))
	    goto err;
	}
    }

659
  objset = FcObjectSetBuild (FC_FOUNDRY, FC_FAMILY, FC_WEIGHT, FC_SLANT,
660
			     FC_WIDTH, FC_PIXEL_SIZE, FC_SPACING, FC_SCALABLE,
661
			     FC_FILE,
662 663 664
#ifdef FC_CAPABILITY
			     FC_CAPABILITY,
#endif	/* FC_CAPABILITY */
665
			     NULL);
666 667
  if (! objset)
    goto err;
668

669 670 671 672
  fontset = FcFontList (NULL, pattern, objset);
  if (! fontset)
    goto err;
  for (i = 0; i < fontset->nfont; i++)
673
    {
674
      Lisp_Object entity;
675

676 677 678 679 680 681 682 683 684 685
      if (spacing >= 0)
	{
	  int this;

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

686
#ifdef FC_CAPABILITY
687 688 689
      if (otlayout[0])
	{
	  FcChar8 *this;
690

691 692 693 694 695
	  if (FcPatternGetString (fontset->fonts[i], FC_CAPABILITY, 0,
				  &this) != FcResultMatch
	      || ! strstr ((char *) this, otlayout))
	    continue;
	}
696
#endif	/* FC_CAPABILITY */
697
#ifdef HAVE_LIBOTF
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
      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
718
	}
719 720 721 722 723
#endif	/* HAVE_LIBOTF */
      entity = ftfont_pattern_entity (fontset->fonts[i], registry,
				      fc_charset_idx);
      if (! NILP (entity))
	val = Fcons (entity, val);
Kenichi Handa's avatar
Kenichi Handa committed
724
    }
725
  font_add_log ("ftfont-list", spec, val);
Kenichi Handa's avatar
Kenichi Handa committed
726 727 728 729
  goto finish;

 err:
  /* We come here because of unexpected error in fontconfig API call
730
     (usually insufficient memory).  */
Kenichi Handa's avatar
Kenichi Handa committed
731 732 733 734 735 736 737 738 739
  val = Qnil;

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

740 741 742 743
static Lisp_Object
ftfont_match (frame, spec)
     Lisp_Object frame, spec;
{
Kenichi Handa's avatar
Kenichi Handa committed
744
  Lisp_Object entity;
745
  FcPattern *pattern, *match = NULL;
746
  FcResult result;
747 748
  char otlayout[15];		/* For "otlayout:XXXX" */
  struct OpenTypeSpec *otspec = NULL;
Kenichi Handa's avatar
Kenichi Handa committed
749
  int fc_charset_idx;
750 751 752 753 754 755 756

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

Kenichi Handa's avatar
Kenichi Handa committed
757
  pattern = ftfont_spec_pattern (spec, &fc_charset_idx, otlayout, &otspec);
758
  if (! pattern)
759 760
    return Qnil;

761
  if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
762
    {
763
      FcValue value;
764

765 766 767 768 769 770 771 772 773
      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)
774
	{
Kenichi Handa's avatar
Kenichi Handa committed
775
	  entity = ftfont_pattern_entity (match, Qunicode_bmp, fc_charset_idx);
776 777 778 779 780 781 782
	  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;
783 784
	}
    }
785
  FcPatternDestroy (pattern);
786

787
  font_add_log ("ftfont-match", spec, entity);
788 789 790
  return entity;
}

Kenichi Handa's avatar
Kenichi Handa committed
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
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;
810
  objset = FcObjectSetBuild (FC_FAMILY, NULL);
Kenichi Handa's avatar
Kenichi Handa committed
811 812 813 814 815 816 817 818 819 820 821 822 823
  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)
824
	list = Fcons (intern ((char *) str), list);
Kenichi Handa's avatar
Kenichi Handa committed
825 826 827 828 829 830 831 832 833 834 835
    }

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

  return list;
}


836
static Lisp_Object
Kenichi Handa's avatar
Kenichi Handa committed
837 838 839 840 841 842 843 844 845 846
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;
Kenichi Handa's avatar
Kenichi Handa committed
847 848
  Lisp_Object val, filename, cache, font_object;
  int fc_charset_idx;
Kenichi Handa's avatar
Kenichi Handa committed
849
  FcPattern *pattern;
Kenichi Handa's avatar
Kenichi Handa committed
850
  int scalable;
Kenichi Handa's avatar
Kenichi Handa committed
851
  int spacing;
852 853 854
  char name[256];
  int i, len;
  int upEM;
Kenichi Handa's avatar
Kenichi Handa committed
855

856 857 858 859
  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
860 861 862 863 864 865 866 867 868
  filename = XCAR (val);
  fc_charset_idx = XINT (XCDR (val));
  cache = ftfont_lookup_cache (filename);
  if (NILP (cache))
    return Qnil;
  filename = XCAR (cache);
  val = XCDR (cache);
  ft_face = XSAVE_VALUE (val)->pointer;
  if (XSAVE_VALUE (val)->integer > 0)
Kenichi Handa's avatar
Kenichi Handa committed
869
    {
Kenichi Handa's avatar
Kenichi Handa committed
870
      /* FT_Face in this cache is already used by the different size.  */
Kenichi Handa's avatar
Kenichi Handa committed
871
      if (FT_New_Size (ft_face, &ft_size) != 0)
872
	return Qnil;
Kenichi Handa's avatar
Kenichi Handa committed
873 874 875
      if (FT_Activate_Size (ft_size) != 0)
	{
	  FT_Done_Size (ft_size);
876
	  return Qnil;
Kenichi Handa's avatar
Kenichi Handa committed
877
	}
Kenichi Handa's avatar
Kenichi Handa committed
878 879
    }
  XSAVE_VALUE (val)->integer++;
Kenichi Handa's avatar
Kenichi Handa committed
880 881 882 883 884 885 886
  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);
887
      return Qnil;
Kenichi Handa's avatar
Kenichi Handa committed
888 889
    }

890 891 892 893 894 895 896 897 898 899 900 901 902 903
  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));
Kenichi Handa's avatar
Kenichi Handa committed
904
  ASET (font_object, FONT_FILE_INDEX, filename);
905 906 907
  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
908 909
  ftfont_info->ft_size = ft_face->size;
  ftfont_info->fc_charset_idx = fc_charset_idx;
910
#ifdef HAVE_LIBOTF
911 912
  ftfont_info->maybe_otf = ft_face->face_flags & FT_FACE_FLAG_SFNT;
  ftfont_info->otf = NULL;
913
#endif	/* HAVE_LIBOTF */
Kenichi Handa's avatar
Kenichi Handa committed
914 915
  font->pixel_size = size;
  font->driver = &ftfont_driver;
916
  font->encoding_charset = font->repertory_charset = -1;
917

918
  upEM = ft_face->units_per_EM;
Kenichi Handa's avatar
Kenichi Handa committed
919 920
  scalable = (INTEGERP (AREF (entity, FONT_AVGWIDTH_INDEX))
	      && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) == 0);
921 922 923 924 925 926 927 928 929 930 931
  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;
932
    }
Kenichi Handa's avatar
Kenichi Handa committed
933 934 935
  if (INTEGERP (AREF (entity, FONT_SPACING_INDEX)))
    spacing = XINT (AREF (entity, FONT_SPACING_INDEX));
  else
936 937
    spacing = FC_PROPORTIONAL;
  if (spacing != FC_PROPORTIONAL)
938 939 940
    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
941 942
  else
    {
943
      int n;
Kenichi Handa's avatar
Kenichi Handa committed
944

945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
      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
961 962
    }

963 964 965 966 967 968 969 970 971 972 973 974 975 976
  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
977

978
  return font_object;
Kenichi Handa's avatar
Kenichi Handa committed
979 980 981 982 983 984 985 986
}

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

Kenichi Handa's avatar
Kenichi Handa committed
989 990
  cache = ftfont_lookup_cache (font->props[FONT_FILE_INDEX]);
  val = XCDR (cache);
Kenichi Handa's avatar
Kenichi Handa committed
991 992
  (XSAVE_VALUE (val)->integer)--;
  if (XSAVE_VALUE (val)->integer == 0)
993
    {
Kenichi Handa's avatar
Kenichi Handa committed
994 995 996
      FT_Face ft_face = XSAVE_VALUE (val)->pointer;

      FT_Done_Face (ft_face);
997 998 999 1000
#ifdef HAVE_LIBOTF
      if (ftfont_info->otf)
	OTF_close (ftfont_info->otf);
#endif
Kenichi Handa's avatar
Kenichi Handa committed
1001
      XSAVE_VALUE (val)->pointer = NULL;
1002
    }
Kenichi Handa's avatar
Kenichi Handa committed
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
  else
    FT_Done_Size (ftfont_info->ft_size);
}

static int 
ftfont_has_char (entity, c)
     Lisp_Object entity;
     int c;
{
  Lisp_Object val;
Kenichi Handa's avatar
Kenichi Handa committed
1013 1014
  int fc_charset_idx;
  struct charset *charset;
Kenichi Handa's avatar
Kenichi Handa committed
1015

1016
  val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
Kenichi Handa's avatar
Kenichi Handa committed
1017 1018
  fc_charset_idx = XINT (XCDR (XCDR (val)));
  if (fc_charset_idx < 0)
1019
    return -1;
Kenichi Handa's avatar
Kenichi Handa committed
1020 1021
  charset = CHARSET_FROM_ID (fc_charset_table[fc_charset_idx].charset_id);
  return (ENCODE_CHAR (charset, c) != CHARSET_INVALID_CODE (charset));
Kenichi Handa's avatar
Kenichi Handa committed
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
}

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

1034
  return (code > 0 ? code : FONT_INVALID_CODE);
Kenichi Handa's avatar
Kenichi Handa committed
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
}

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
	{
1076
	  width += font->space_width;