font.c 145 KB
Newer Older
Kenichi Handa's avatar
Kenichi Handa committed
1
/* font.c -- "Font" primitives.
Glenn Morris's avatar
Glenn Morris committed
2

3
Copyright (C) 2006-2014 Free Software Foundation, Inc.
4 5 6
Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
  National Institute of Advanced Industrial Science and Technology (AIST)
  Registration Number H13PRO009
Kenichi Handa's avatar
Kenichi Handa committed
7 8 9

This file is part of GNU Emacs.

10
GNU Emacs is free software: you can redistribute it and/or modify
Kenichi Handa's avatar
Kenichi Handa committed
11
it under the terms of the GNU General Public License as published by
12 13
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Kenichi Handa's avatar
Kenichi Handa committed
14 15 16 17 18 19 20

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
21
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Kenichi Handa's avatar
Kenichi Handa committed
22 23

#include <config.h>
24
#include <float.h>
Kenichi Handa's avatar
Kenichi Handa committed
25 26
#include <stdio.h>

27 28
#include <c-ctype.h>

Kenichi Handa's avatar
Kenichi Handa committed
29
#include "lisp.h"
30
#include "character.h"
Kenichi Handa's avatar
Kenichi Handa committed
31 32
#include "buffer.h"
#include "frame.h"
Kenichi Handa's avatar
Kenichi Handa committed
33
#include "window.h"
Kenichi Handa's avatar
Kenichi Handa committed
34 35 36 37 38 39
#include "dispextern.h"
#include "charset.h"
#include "composite.h"
#include "fontset.h"
#include "font.h"

40 41 42
#ifdef HAVE_WINDOW_SYSTEM
#include TERM_HEADER
#endif /* HAVE_WINDOW_SYSTEM */
43

44
Lisp_Object Qopentype;
Kenichi Handa's avatar
Kenichi Handa committed
45

Kenichi Handa's avatar
Kenichi Handa committed
46
/* Important character set strings.  */
Kenichi Handa's avatar
Kenichi Handa committed
47
Lisp_Object Qascii_0, Qiso8859_1, Qiso10646_1, Qunicode_bmp, Qunicode_sip;
48

49 50
#define DEFAULT_ENCODING Qiso8859_1

Kenichi Handa's avatar
Kenichi Handa committed
51 52 53
/* Unicode category `Cf'.  */
static Lisp_Object QCf;

54
/* Vector of Vfont_weight_table, Vfont_slant_table, and Vfont_width_table. */
Kenichi Handa's avatar
Kenichi Handa committed
55 56
static Lisp_Object font_style_table;

57 58 59 60 61 62 63 64
/* Structure used for tables mapping weight, slant, and width numeric
   values and their names.  */

struct table_entry
{
  int numeric;
  /* The first one is a valid name as a face attribute.
     The second one (if any) is a typical name in XLFD field.  */
65
  const char *names[5];
66 67 68 69 70
};

/* Table of weight numeric values and their names.  This table must be
   sorted by numeric values in ascending order.  */

71
static const struct table_entry weight_table[] =
72 73 74 75 76 77
{
  { 0, { "thin" }},
  { 20, { "ultra-light", "ultralight" }},
  { 40, { "extra-light", "extralight" }},
  { 50, { "light" }},
  { 75, { "semi-light", "semilight", "demilight", "book" }},
78
  { 100, { "normal", "medium", "regular", "unspecified" }},
79 80 81 82 83 84 85 86 87
  { 180, { "semi-bold", "semibold", "demibold", "demi" }},
  { 200, { "bold" }},
  { 205, { "extra-bold", "extrabold" }},
  { 210, { "ultra-bold", "ultrabold", "black" }}
};

/* Table of slant numeric values and their names.  This table must be
   sorted by numeric values in ascending order.  */

88
static const struct table_entry slant_table[] =
89 90 91
{
  { 0, { "reverse-oblique", "ro" }},
  { 10, { "reverse-italic", "ri" }},
92
  { 100, { "normal", "r", "unspecified" }},
93 94 95 96 97 98 99
  { 200, { "italic" ,"i", "ot" }},
  { 210, { "oblique", "o" }}
};

/* Table of width numeric values and their names.  This table must be
   sorted by numeric values in ascending order.  */

100
static const struct table_entry width_table[] =
101 102 103 104 105
{
  { 50, { "ultra-condensed", "ultracondensed" }},
  { 63, { "extra-condensed", "extracondensed" }},
  { 75, { "condensed", "compressed", "narrow" }},
  { 87, { "semi-condensed", "semicondensed", "demicondensed" }},
106
  { 100, { "normal", "medium", "regular", "unspecified" }},
107 108 109 110 111 112
  { 113, { "semi-expanded", "semiexpanded", "demiexpanded" }},
  { 125, { "expanded" }},
  { 150, { "extra-expanded", "extraexpanded" }},
  { 200, { "ultra-expanded", "ultraexpanded", "wide" }}
};

Andreas Schwab's avatar
Andreas Schwab committed
113 114
Lisp_Object QCfoundry;
static Lisp_Object QCadstyle, QCregistry;
Kenichi Handa's avatar
Kenichi Handa committed
115
/* Symbols representing keys of font extra info.  */
Kenichi Handa's avatar
Kenichi Handa committed
116
Lisp_Object QCspacing, QCdpi, QCscalable, QCotf, QClang, QCscript, QCavgwidth;
117 118
Lisp_Object QCantialias, QCfont_entity;
static Lisp_Object QCfc_unknown_spec;
119
/* Symbols representing values of font spacing property.  */
120 121
static Lisp_Object Qc, Qm, Qd;
Lisp_Object Qp;
122 123 124
/* Special ADSTYLE properties to avoid fonts used for Latin
   characters; used in xfont.c and ftfont.c.  */
Lisp_Object Qja, Qko;
Kenichi Handa's avatar
Kenichi Handa committed
125

126
static Lisp_Object QCuser_spec;
127

128
/* Alist of font registry symbols and the corresponding charset
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
   information.  The information is retrieved from
   Vfont_encoding_alist on demand.

   Eash element has the form:
	(REGISTRY . (ENCODING-CHARSET-ID . REPERTORY-CHARSET-ID))
   or
	(REGISTRY . nil)

   In the former form, ENCODING-CHARSET-ID is an ID of a charset that
   encodes a character code to a glyph code of a font, and
   REPERTORY-CHARSET-ID is an ID of a charset that tells if a
   character is supported by a font.

   The latter form means that the information for REGISTRY couldn't be
   retrieved.  */
static Lisp_Object font_charset_alist;

146 147
/* List of all font drivers.  Each font-backend (XXXfont.c) calls
   register_font_driver in syms_of_XXXfont to register its font-driver
Kenichi Handa's avatar
Kenichi Handa committed
148 149 150
   here.  */
static struct font_driver_list *font_driver_list;

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
#ifdef ENABLE_CHECKING

/* Used to catch bogus pointers in font objects.  */

bool
valid_font_driver (struct font_driver *drv)
{
  Lisp_Object tail, frame;
  struct font_driver_list *fdl;

  for (fdl = font_driver_list; fdl; fdl = fdl->next)
    if (fdl->driver == drv)
      return true;
  FOR_EACH_FRAME (tail, frame)
    for (fdl = XFRAME (frame)->font_driver_list; fdl; fdl = fdl->next)
      if (fdl->driver == drv)
	return true;
  return false;
}

#endif /* ENABLE_CHECKING */
Kenichi Handa's avatar
Kenichi Handa committed
172

Paul Eggert's avatar
Paul Eggert committed
173
/* Creators of font-related Lisp object.  */
Kenichi Handa's avatar
Kenichi Handa committed
174

Andreas Schwab's avatar
Andreas Schwab committed
175
static Lisp_Object
176
font_make_spec (void)
Kenichi Handa's avatar
Kenichi Handa committed
177 178 179 180 181 182 183 184 185 186 187
{
  Lisp_Object font_spec;
  struct font_spec *spec
    = ((struct font_spec *)
       allocate_pseudovector (VECSIZE (struct font_spec),
			      FONT_SPEC_MAX, PVEC_FONT));
  XSETFONT (font_spec, spec);
  return font_spec;
}

Lisp_Object
188
font_make_entity (void)
Kenichi Handa's avatar
Kenichi Handa committed
189 190 191 192 193 194 195 196 197 198
{
  Lisp_Object font_entity;
  struct font_entity *entity
    = ((struct font_entity *)
       allocate_pseudovector (VECSIZE (struct font_entity),
			      FONT_ENTITY_MAX, PVEC_FONT));
  XSETFONT (font_entity, entity);
  return font_entity;
}

199 200 201
/* Create a font-object whose structure size is SIZE.  If ENTITY is
   not nil, copy properties from ENTITY to the font-object.  If
   PIXELSIZE is positive, set the `size' property to PIXELSIZE.  */
Kenichi Handa's avatar
Kenichi Handa committed
202
Lisp_Object
203
font_make_object (int size, Lisp_Object entity, int pixelsize)
Kenichi Handa's avatar
Kenichi Handa committed
204 205 206 207
{
  Lisp_Object font_object;
  struct font *font
    = (struct font *) allocate_pseudovector (size, FONT_OBJECT_MAX, PVEC_FONT);
208 209
  int i;

210 211 212
  /* GC can happen before the driver is set up,
     so avoid dangling pointer here (Bug#17771).  */
  font->driver = NULL;
Kenichi Handa's avatar
Kenichi Handa committed
213 214
  XSETFONT (font_object, font);

215 216 217 218 219 220
  if (! NILP (entity))
    {
      for (i = 1; i < FONT_SPEC_MAX; i++)
	font->props[i] = AREF (entity, i);
      if (! NILP (AREF (entity, FONT_EXTRA_INDEX)))
	font->props[FONT_EXTRA_INDEX]
221
	  = Fcopy_alist (AREF (entity, FONT_EXTRA_INDEX));
222 223 224
    }
  if (size > 0)
    font->props[FONT_SIZE_INDEX] = make_number (pixelsize);
Kenichi Handa's avatar
Kenichi Handa committed
225 226 227
  return font_object;
}

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
#if defined (HAVE_XFT) || defined (HAVE_FREETYPE) || defined (HAVE_NS)

/* Like above, but also set `type', `name' and `fullname' properties
   of font-object.  */

Lisp_Object
font_build_object (int vectorsize, Lisp_Object type,
		   Lisp_Object entity, double pixelsize)
{
  int len;
  char name[256];
  Lisp_Object font_object = font_make_object (vectorsize, entity, pixelsize);

  ASET (font_object, FONT_TYPE_INDEX, type);
  len = font_unparse_xlfd (entity, pixelsize, name, sizeof name);
  if (len > 0)
    ASET (font_object, FONT_NAME_INDEX, make_string (name, len));
  len = font_unparse_fcname (entity, pixelsize, name, sizeof name);
  if (len > 0)
    ASET (font_object, FONT_FULLNAME_INDEX, make_string (name, len));
  else
    ASET (font_object, FONT_FULLNAME_INDEX,
	  AREF (font_object, FONT_NAME_INDEX));
  return font_object;
}

#endif /* HAVE_XFT || HAVE_FREETYPE || HAVE_NS */
Kenichi Handa's avatar
Kenichi Handa committed
255

Dmitry Antipov's avatar
Dmitry Antipov committed
256 257 258
static int font_pixel_size (struct frame *f, Lisp_Object);
static Lisp_Object font_open_entity (struct frame *, Lisp_Object, int);
static Lisp_Object font_matching_entity (struct frame *, Lisp_Object *,
259
                                         Lisp_Object);
Andreas Schwab's avatar
Andreas Schwab committed
260
static unsigned font_encode_char (Lisp_Object, int);
Kenichi Handa's avatar
Kenichi Handa committed
261 262 263 264

/* Number of registered font drivers.  */
static int num_font_drivers;

Kenichi Handa's avatar
Kenichi Handa committed
265 266

/* Return a Lispy value of a font property value at STR and LEN bytes.
267 268 269
   If STR is "*", return nil.  If FORCE_SYMBOL, or if STR does not
   consist entirely of one or more digits, return a symbol interned
   from STR.  Otherwise, return an integer.  */
Kenichi Handa's avatar
Kenichi Handa committed
270 271

Lisp_Object
272
font_intern_prop (const char *str, ptrdiff_t len, bool force_symbol)
Kenichi Handa's avatar
Kenichi Handa committed
273
{
274
  ptrdiff_t i;
275
  Lisp_Object tem;
Kenichi Handa's avatar
Kenichi Handa committed
276
  Lisp_Object obarray;
277
  ptrdiff_t nbytes, nchars;
Kenichi Handa's avatar
Kenichi Handa committed
278 279 280

  if (len == 1 && *str == '*')
    return Qnil;
281
  if (!force_symbol && 0 < len && '0' <= *str && *str <= '9')
Kenichi Handa's avatar
Kenichi Handa committed
282 283
    {
      for (i = 1; i < len; i++)
284
	if (! ('0' <= str[i] && str[i] <= '9'))
Kenichi Handa's avatar
Kenichi Handa committed
285 286
	  break;
      if (i == len)
287
	{
288 289 290 291 292 293 294
	  EMACS_INT n;

	  i = 0;
	  for (n = 0; (n += str[i++] - '0') <= MOST_POSITIVE_FIXNUM; n *= 10)
	    {
	      if (i == len)
		return make_number (n);
295
	      if (MOST_POSITIVE_FIXNUM / 10 < n)
296 297 298 299
		break;
	    }

	  xsignal1 (Qoverflow_error, make_string (str, len));
300
	}
Kenichi Handa's avatar
Kenichi Handa committed
301 302
    }

303 304
  /* This code is similar to intern function from lread.c.  */
  obarray = check_obarray (Vobarray);
305
  parse_str_as_multibyte ((unsigned char *) str, len, &nchars, &nbytes);
306 307 308
  tem = oblookup (obarray, str,
		  (len == nchars || len != nbytes) ? len : nchars, len);

Kenichi Handa's avatar
Kenichi Handa committed
309 310
  if (SYMBOLP (tem))
    return tem;
311 312
  tem = make_specified_string (str, nchars, len,
			       len != nchars && len == nbytes);
313
  return Fintern (tem, obarray);
Kenichi Handa's avatar
Kenichi Handa committed
314 315
}

316
/* Return a pixel size of font-spec SPEC on frame F.  */
317

318
static int
Dmitry Antipov's avatar
Dmitry Antipov committed
319
font_pixel_size (struct frame *f, Lisp_Object spec)
320
{
321
#ifdef HAVE_WINDOW_SYSTEM
322 323
  Lisp_Object size = AREF (spec, FONT_SIZE_INDEX);
  double point_size;
Kenichi Handa's avatar
Kenichi Handa committed
324
  int dpi, pixel_size;
325
  Lisp_Object val;
326

327 328 329
  if (INTEGERP (size))
    return XINT (size);
  if (NILP (size))
330
    return 0;
331
  eassert (FLOATP (size));
332
  point_size = XFLOAT_DATA (size);
Kenichi Handa's avatar
Kenichi Handa committed
333 334
  val = AREF (spec, FONT_DPI_INDEX);
  if (INTEGERP (val))
335
    dpi = XINT (val);
336
  else
337
    dpi = FRAME_RES_Y (f);
338 339
  pixel_size = POINT_TO_PIXEL (point_size, dpi);
  return pixel_size;
340 341 342
#else
  return 1;
#endif
343 344
}

Kenichi Handa's avatar
Kenichi Handa committed
345

Kenichi Handa's avatar
Kenichi Handa committed
346 347 348 349
/* Return a value of PROP's VAL (symbol or integer) to be stored in a
   font vector.  If VAL is not valid (i.e. not registered in
   font_style_table), return -1 if NOERROR is zero, and return a
   proper index if NOERROR is nonzero.  In that case, register VAL in
350
   font_style_table if VAL is a symbol, and return the closest index if
Kenichi Handa's avatar
Kenichi Handa committed
351
   VAL is an integer.  */
Kenichi Handa's avatar
Kenichi Handa committed
352

Kenichi Handa's avatar
Kenichi Handa committed
353
int
354 355
font_style_to_value (enum font_property_index prop, Lisp_Object val,
                     bool noerror)
Kenichi Handa's avatar
Kenichi Handa committed
356
{
Kenichi Handa's avatar
Kenichi Handa committed
357
  Lisp_Object table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
358 359 360 361
  int len;

  CHECK_VECTOR (table);
  len = ASIZE (table);
Kenichi Handa's avatar
Kenichi Handa committed
362

Kenichi Handa's avatar
Kenichi Handa committed
363
  if (SYMBOLP (val))
Kenichi Handa's avatar
Kenichi Handa committed
364
    {
365
      int i, j;
366
      char *s;
Kenichi Handa's avatar
Kenichi Handa committed
367 368 369 370
      Lisp_Object args[2], elt;

      /* At first try exact match.  */
      for (i = 0; i < len; i++)
371 372 373 374 375 376 377 378 379 380
	{
	  CHECK_VECTOR (AREF (table, i));
	  for (j = 1; j < ASIZE (AREF (table, i)); j++)
	    if (EQ (val, AREF (AREF (table, i), j)))
	      {
		CHECK_NUMBER (AREF (AREF (table, i), 0));
		return ((XINT (AREF (AREF (table, i), 0)) << 8)
			| (i << 4) | (j - 1));
	      }
	}
Kenichi Handa's avatar
Kenichi Handa committed
381
      /* Try also with case-folding match.  */
382
      s = SSDATA (SYMBOL_NAME (val));
Kenichi Handa's avatar
Kenichi Handa committed
383
      for (i = 0; i < len; i++)
384 385 386
	for (j = 1; j < ASIZE (AREF (table, i)); j++)
	  {
	    elt = AREF (AREF (table, i), j);
387
	    if (xstrcasecmp (s, SSDATA (SYMBOL_NAME (elt))) == 0)
388 389 390 391 392
	      {
		CHECK_NUMBER (AREF (AREF (table, i), 0));
		return ((XINT (AREF (AREF (table, i), 0)) << 8)
			| (i << 4) | (j - 1));
	      }
393
	  }
Kenichi Handa's avatar
Kenichi Handa committed
394 395
      if (! noerror)
	return -1;
396
      eassert (len < 255);
397
      elt = Fmake_vector (make_number (2), make_number (100));
398
      ASET (elt, 1, val);
Kenichi Handa's avatar
Kenichi Handa committed
399
      args[0] = table;
400
      args[1] = Fmake_vector (make_number (1), elt);
Kenichi Handa's avatar
Kenichi Handa committed
401
      ASET (font_style_table, prop - FONT_WEIGHT_INDEX, Fvconcat (2, args));
402
      return (100 << 8) | (i << 4);
Kenichi Handa's avatar
Kenichi Handa committed
403
    }
Kenichi Handa's avatar
Kenichi Handa committed
404 405
  else
    {
406
      int i, last_n;
407
      EMACS_INT numeric = XINT (val);
Kenichi Handa's avatar
Kenichi Handa committed
408

409
      for (i = 0, last_n = -1; i < len; i++)
Kenichi Handa's avatar
Kenichi Handa committed
410
	{
411
	  int n;
Kenichi Handa's avatar
Kenichi Handa committed
412

413 414 415
	  CHECK_VECTOR (AREF (table, i));
	  CHECK_NUMBER (AREF (AREF (table, i), 0));
	  n = XINT (AREF (AREF (table, i), 0));
Kenichi Handa's avatar
Kenichi Handa committed
416
	  if (numeric == n)
417
	    return (n << 8) | (i << 4);
Kenichi Handa's avatar
Kenichi Handa committed
418 419 420 421
	  if (numeric < n)
	    {
	      if (! noerror)
		return -1;
422 423
	      return ((i == 0 || n - numeric < numeric - last_n)
		      ? (n << 8) | (i << 4): (last_n << 8 | ((i - 1) << 4)));
Kenichi Handa's avatar
Kenichi Handa committed
424 425 426 427 428
	    }
	  last_n = n;
	}
      if (! noerror)
	return -1;
429
      return ((last_n << 8) | ((i - 1) << 4));
Kenichi Handa's avatar
Kenichi Handa committed
430 431
    }
}
Kenichi Handa's avatar
Kenichi Handa committed
432 433

Lisp_Object
434 435
font_style_symbolic (Lisp_Object font, enum font_property_index prop,
                     bool for_face)
Kenichi Handa's avatar
Kenichi Handa committed
436
{
437
  Lisp_Object val = AREF (font, prop);
438 439
  Lisp_Object table, elt;
  int i;
Kenichi Handa's avatar
Kenichi Handa committed
440

Kenichi Handa's avatar
Kenichi Handa committed
441 442 443
  if (NILP (val))
    return Qnil;
  table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
444
  CHECK_VECTOR (table);
445
  i = XINT (val) & 0xFF;
446
  eassert (((i >> 4) & 0xF) < ASIZE (table));
447
  elt = AREF (table, ((i >> 4) & 0xF));
448
  CHECK_VECTOR (elt);
449
  eassert ((i & 0xF) + 1 < ASIZE (elt));
450 451 452
  elt = (for_face ? AREF (elt, 1) : AREF (elt, (i & 0xF) + 1));
  CHECK_SYMBOL (elt);
  return elt;
453 454
}

455 456 457 458 459
/* Return ENCODING or a cons of ENCODING and REPERTORY of the font
   FONTNAME.  ENCODING is a charset symbol that specifies the encoding
   of the font.  REPERTORY is a charset symbol or nil.  */

Lisp_Object
460
find_font_encoding (Lisp_Object fontname)
461 462 463 464 465 466 467 468 469 470 471 472 473 474
{
  Lisp_Object tail, elt;

  for (tail = Vfont_encoding_alist; CONSP (tail); tail = XCDR (tail))
    {
      elt = XCAR (tail);
      if (CONSP (elt)
	  && STRINGP (XCAR (elt))
	  && fast_string_match_ignore_case (XCAR (elt), fontname) >= 0
	  && (SYMBOLP (XCDR (elt))
	      ? CHARSETP (XCDR (elt))
	      : CONSP (XCDR (elt)) && CHARSETP (XCAR (XCDR (elt)))))
	return (XCDR (elt));
    }
475
  return Qnil;
476 477
}

478 479 480 481 482
/* Return encoding charset and repertory charset for REGISTRY in
   ENCODING and REPERTORY correspondingly.  If correct information for
   REGISTRY is available, return 0.  Otherwise return -1.  */

int
483
font_registry_charsets (Lisp_Object registry, struct charset **encoding, struct charset **repertory)
484 485 486 487
{
  Lisp_Object val;
  int encoding_id, repertory_id;

Kenichi Handa's avatar
Kenichi Handa committed
488
  val = Fassoc_string (registry, font_charset_alist, Qt);
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
  if (! NILP (val))
    {
      val = XCDR (val);
      if (NILP (val))
	return -1;
      encoding_id = XINT (XCAR (val));
      repertory_id = XINT (XCDR (val));
    }
  else
    {
      val = find_font_encoding (SYMBOL_NAME (registry));
      if (SYMBOLP (val) && CHARSETP (val))
	{
	  encoding_id = repertory_id = XINT (CHARSET_SYMBOL_ID (val));
	}
      else if (CONSP (val))
	{
	  if (! CHARSETP (XCAR (val)))
	    goto invalid_entry;
	  encoding_id = XINT (CHARSET_SYMBOL_ID (XCAR (val)));
	  if (NILP (XCDR (val)))
	    repertory_id = -1;
	  else
	    {
	      if (! CHARSETP (XCDR (val)))
		goto invalid_entry;
	      repertory_id = XINT (CHARSET_SYMBOL_ID (XCDR (val)));
	    }
517
	}
518 519 520 521
      else
	goto invalid_entry;
      val = Fcons (make_number (encoding_id), make_number (repertory_id));
      font_charset_alist
522
	= nconc2 (font_charset_alist, list1 (Fcons (registry, val)));
523 524 525 526 527 528 529 530 531 532
    }

  if (encoding)
    *encoding = CHARSET_FROM_ID (encoding_id);
  if (repertory)
    *repertory = repertory_id >= 0 ? CHARSET_FROM_ID (repertory_id) : NULL;
  return 0;

 invalid_entry:
  font_charset_alist
533
    = nconc2 (font_charset_alist, list1 (Fcons (registry, Qnil)));
534 535 536
  return -1;
}

Kenichi Handa's avatar
Kenichi Handa committed
537

538
/* Font property value validators.  See the comment of
539 540
   font_property_table for the meaning of the arguments.  */

541 542 543 544 545 546
static Lisp_Object font_prop_validate (int, Lisp_Object, Lisp_Object);
static Lisp_Object font_prop_validate_symbol (Lisp_Object, Lisp_Object);
static Lisp_Object font_prop_validate_style (Lisp_Object, Lisp_Object);
static Lisp_Object font_prop_validate_non_neg (Lisp_Object, Lisp_Object);
static Lisp_Object font_prop_validate_spacing (Lisp_Object, Lisp_Object);
static int get_font_prop_index (Lisp_Object);
Kenichi Handa's avatar
Kenichi Handa committed
547 548

static Lisp_Object
549
font_prop_validate_symbol (Lisp_Object prop, Lisp_Object val)
Kenichi Handa's avatar
Kenichi Handa committed
550 551
{
  if (STRINGP (val))
Kenichi Handa's avatar
Kenichi Handa committed
552 553
    val = Fintern (val, Qnil);
  if (! SYMBOLP (val))
Kenichi Handa's avatar
Kenichi Handa committed
554
    val = Qerror;
Kenichi Handa's avatar
Kenichi Handa committed
555 556
  else if (EQ (prop, QCregistry))
    val = Fintern (Fdowncase (SYMBOL_NAME (val)), Qnil);
Kenichi Handa's avatar
Kenichi Handa committed
557 558 559
  return val;
}

Kenichi Handa's avatar
Kenichi Handa committed
560

Kenichi Handa's avatar
Kenichi Handa committed
561
static Lisp_Object
562
font_prop_validate_style (Lisp_Object style, Lisp_Object val)
Kenichi Handa's avatar
Kenichi Handa committed
563
{
Kenichi Handa's avatar
Kenichi Handa committed
564 565 566 567
  enum font_property_index prop = (EQ (style, QCweight) ? FONT_WEIGHT_INDEX
				   : EQ (style, QCslant) ? FONT_SLANT_INDEX
				   : FONT_WIDTH_INDEX);
  if (INTEGERP (val))
Kenichi Handa's avatar
Kenichi Handa committed
568
    {
569
      EMACS_INT n = XINT (val);
570
      CHECK_VECTOR (AREF (font_style_table, prop - FONT_WEIGHT_INDEX));
571
      if (((n >> 4) & 0xF)
Kenichi Handa's avatar
Kenichi Handa committed
572
	  >= ASIZE (AREF (font_style_table, prop - FONT_WEIGHT_INDEX)))
Kenichi Handa's avatar
Kenichi Handa committed
573 574 575
	val = Qerror;
      else
	{
576 577
	  Lisp_Object elt = AREF (AREF (font_style_table, prop - FONT_WEIGHT_INDEX), (n >> 4) & 0xF);

578
	  CHECK_VECTOR (elt);
579 580
	  if ((n & 0xF) + 1 >= ASIZE (elt))
	    val = Qerror;
581 582 583 584 585 586
	  else
	    {
	      CHECK_NUMBER (AREF (elt, 0));
	      if (XINT (AREF (elt, 0)) != (n >> 8))
		val = Qerror;
	    }
Kenichi Handa's avatar
Kenichi Handa committed
587 588
	}
    }
Kenichi Handa's avatar
Kenichi Handa committed
589 590 591 592 593 594 595 596
  else if (SYMBOLP (val))
    {
      int n = font_style_to_value (prop, val, 0);

      val = n >= 0 ? make_number (n) : Qerror;
    }
  else
    val = Qerror;
Kenichi Handa's avatar
Kenichi Handa committed
597 598 599 600
  return val;
}

static Lisp_Object
601
font_prop_validate_non_neg (Lisp_Object prop, Lisp_Object val)
Kenichi Handa's avatar
Kenichi Handa committed
602 603 604 605 606 607
{
  return (NATNUMP (val) || (FLOATP (val) && XFLOAT_DATA (val) >= 0)
	  ? val : Qerror);
}

static Lisp_Object
608
font_prop_validate_spacing (Lisp_Object prop, Lisp_Object val)
609 610 611
{
  if (NILP (val) || (NATNUMP (val) && XINT (val) <= FONT_SPACING_CHARCELL))
    return val;
612 613 614 615 616 617 618 619
  if (SYMBOLP (val) && SBYTES (SYMBOL_NAME (val)) == 1)
    {
      char spacing = SDATA (SYMBOL_NAME (val))[0];

      if (spacing == 'c' || spacing == 'C')
	return make_number (FONT_SPACING_CHARCELL);
      if (spacing == 'm' || spacing == 'M')
	return make_number (FONT_SPACING_MONO);
620
      if (spacing == 'p' || spacing == 'P')
621 622 623 624
	return make_number (FONT_SPACING_PROPORTIONAL);
      if (spacing == 'd' || spacing == 'D')
	return make_number (FONT_SPACING_DUAL);
    }
625 626 627
  return Qerror;
}

628
static Lisp_Object
629
font_prop_validate_otf (Lisp_Object prop, Lisp_Object val)
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
{
  Lisp_Object tail, tmp;
  int i;

  /* VAL = (SCRIPT [ LANGSYS [ GSUB-FEATURES [ GPOS-FEATURES ]]])
     GSUB-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil
     GPOS-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil  */
  if (! CONSP (val))
    return Qerror;
  if (! SYMBOLP (XCAR (val)))
    return Qerror;
  tail = XCDR (val);
  if (NILP (tail))
    return val;
  if (! CONSP (tail) || ! SYMBOLP (XCAR (val)))
    return Qerror;
  for (i = 0; i < 2; i++)
    {
      tail = XCDR (tail);
      if (NILP (tail))
	return val;
      if (! CONSP (tail))
	return Qerror;
      for (tmp = XCAR (tail); CONSP (tmp); tmp = XCDR (tmp))
	if (! SYMBOLP (XCAR (tmp)))
	  return Qerror;
      if (! NILP (tmp))
	return Qerror;
    }
  return val;
}

662
/* Structure of known font property keys and validator of the
663
   values.  */
664
static const struct
Kenichi Handa's avatar
Kenichi Handa committed
665
{
666
  /* Pointer to the key symbol.  */
Kenichi Handa's avatar
Kenichi Handa committed
667
  Lisp_Object *key;
668 669 670
  /* Function to validate PROP's value VAL, or NULL if any value is
     ok.  The value is VAL or its regularized value if VAL is valid,
     and Qerror if not.  */
Paul Eggert's avatar
Paul Eggert committed
671
  Lisp_Object (*validator) (Lisp_Object prop, Lisp_Object val);
672 673
} font_property_table[] =
  { { &QCtype, font_prop_validate_symbol },
Kenichi Handa's avatar
Kenichi Handa committed
674 675 676 677 678 679 680
    { &QCfoundry, font_prop_validate_symbol },
    { &QCfamily, font_prop_validate_symbol },
    { &QCadstyle, font_prop_validate_symbol },
    { &QCregistry, font_prop_validate_symbol },
    { &QCweight, font_prop_validate_style },
    { &QCslant, font_prop_validate_style },
    { &QCwidth, font_prop_validate_style },
681 682 683
    { &QCsize, font_prop_validate_non_neg },
    { &QCdpi, font_prop_validate_non_neg },
    { &QCspacing, font_prop_validate_spacing },
Kenichi Handa's avatar
Kenichi Handa committed
684 685 686 687 688 689
    { &QCavgwidth, font_prop_validate_non_neg },
    /* The order of the above entries must match with enum
       font_property_index.  */
    { &QClang, font_prop_validate_symbol },
    { &QCscript, font_prop_validate_symbol },
    { &QCotf, font_prop_validate_otf }
Kenichi Handa's avatar
Kenichi Handa committed
690 691
  };

692
/* Return an index number of font property KEY or -1 if KEY is not an
Kenichi Handa's avatar
Kenichi Handa committed
693
   already known property.  */
694

695
static int
696
get_font_prop_index (Lisp_Object key)
Kenichi Handa's avatar
Kenichi Handa committed
697
{
Kenichi Handa's avatar
Kenichi Handa committed
698 699
  int i;

700
  for (i = 0; i < ARRAYELTS (font_property_table); i++)
Kenichi Handa's avatar
Kenichi Handa committed
701 702
    if (EQ (key, *font_property_table[i].key))
      return i;
703
  return -1;
Kenichi Handa's avatar
Kenichi Handa committed
704 705
}

Kenichi Handa's avatar
Kenichi Handa committed
706 707 708
/* Validate the font property.  The property key is specified by the
   symbol PROP, or the index IDX (if PROP is nil).  If VAL is invalid,
   signal an error.  The value is VAL or the regularized one.  */
709

Kenichi Handa's avatar
Kenichi Handa committed
710
static Lisp_Object
711
font_prop_validate (int idx, Lisp_Object prop, Lisp_Object val)
Kenichi Handa's avatar
Kenichi Handa committed
712
{
Kenichi Handa's avatar
Kenichi Handa committed
713
  Lisp_Object validated;
Kenichi Handa's avatar
Kenichi Handa committed
714

715 716
  if (NILP (val))
    return val;
Kenichi Handa's avatar
Kenichi Handa committed
717 718 719
  if (NILP (prop))
    prop = *font_property_table[idx].key;
  else
720
    {
Kenichi Handa's avatar
Kenichi Handa committed
721 722 723
      idx = get_font_prop_index (prop);
      if (idx < 0)
	return val;
724
    }
Paul Eggert's avatar
Paul Eggert committed
725
  validated = (font_property_table[idx].validator) (prop, val);
Kenichi Handa's avatar
Kenichi Handa committed
726 727 728
  if (EQ (validated, Qerror))
    signal_error ("invalid font property", Fcons (prop, val));
  return validated;
Kenichi Handa's avatar
Kenichi Handa committed
729
}
730

Kenichi Handa's avatar
Kenichi Handa committed
731 732 733

/* Store VAL as a value of extra font property PROP in FONT while
   keeping the sorting order.  Don't check the validity of VAL.  */
734

735
Lisp_Object
736
font_put_extra (Lisp_Object font, Lisp_Object prop, Lisp_Object val)
737 738
{
  Lisp_Object extra = AREF (font, FONT_EXTRA_INDEX);
739
  Lisp_Object slot = (NILP (extra) ? Qnil : assq_no_quit (prop, extra));
740 741 742

  if (NILP (slot))
    {
Kenichi Handa's avatar
Kenichi Handa committed
743 744 745 746 747
      Lisp_Object prev = Qnil;

      while (CONSP (extra)
	     && NILP (Fstring_lessp (prop, XCAR (XCAR (extra)))))
	prev = extra, extra = XCDR (extra);
748

749
      if (NILP (prev))
750
        ASET (font, FONT_EXTRA_INDEX, Fcons (Fcons (prop, val), extra));
751
      else
752
        XSETCDR (prev, Fcons (Fcons (prop, val), extra));
753

754
      return val;
755 756
    }
  XSETCDR (slot, val);
757 758
  if (NILP (val))
    ASET (font, FONT_EXTRA_INDEX, Fdelq (slot, extra));
759
  return val;
760 761
}

Kenichi Handa's avatar
Kenichi Handa committed
762

763
/* Font name parser and unparser.  */
Kenichi Handa's avatar
Kenichi Handa committed
764

765
static int parse_matrix (const char *);
766
static int font_expand_wildcards (Lisp_Object *, int);
767
static int font_parse_name (char *, ptrdiff_t, Lisp_Object);
Kenichi Handa's avatar
Kenichi Handa committed
768

769
/* An enumerator for each field of an XLFD font name.  */
Kenichi Handa's avatar
Kenichi Handa committed
770 771 772 773 774 775 776 777
enum xlfd_field_index
{
  XLFD_FOUNDRY_INDEX,
  XLFD_FAMILY_INDEX,
  XLFD_WEIGHT_INDEX,
  XLFD_SLANT_INDEX,
  XLFD_SWIDTH_INDEX,
  XLFD_ADSTYLE_INDEX,
778 779
  XLFD_PIXEL_INDEX,
  XLFD_POINT_INDEX,
Kenichi Handa's avatar
Kenichi Handa committed
780 781 782 783 784 785 786 787 788
  XLFD_RESX_INDEX,
  XLFD_RESY_INDEX,
  XLFD_SPACING_INDEX,
  XLFD_AVGWIDTH_INDEX,
  XLFD_REGISTRY_INDEX,
  XLFD_ENCODING_INDEX,
  XLFD_LAST_INDEX
};

789
/* An enumerator for mask bit corresponding to each XLFD field.  */
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
enum xlfd_field_mask
{
  XLFD_FOUNDRY_MASK = 0x0001,
  XLFD_FAMILY_MASK = 0x0002,
  XLFD_WEIGHT_MASK = 0x0004,
  XLFD_SLANT_MASK = 0x0008,
  XLFD_SWIDTH_MASK = 0x0010,
  XLFD_ADSTYLE_MASK = 0x0020,
  XLFD_PIXEL_MASK = 0x0040,
  XLFD_POINT_MASK = 0x0080,
  XLFD_RESX_MASK = 0x0100,
  XLFD_RESY_MASK = 0x0200,
  XLFD_SPACING_MASK = 0x0400,
  XLFD_AVGWIDTH_MASK = 0x0800,
  XLFD_REGISTRY_MASK = 0x1000,
  XLFD_ENCODING_MASK = 0x2000
};


809
/* Parse P pointing to the pixel/point size field of the form
Kenichi Handa's avatar
Kenichi Handa committed
810 811 812 813 814 815 816 817 818 819 820 821 822 823
   `[A B C D]' which specifies a transformation matrix:

	A  B  0
	C  D  0
	0  0  1

   by which all glyphs of the font are transformed.  The spec says
   that scalar value N for the pixel/point size is equivalent to:
   A = N * resx/resy, B = C = 0, D = N.

   Return the scalar value N if the form is valid.  Otherwise return
   -1.  */

static int
824
parse_matrix (const char *p)
Kenichi Handa's avatar
Kenichi Handa committed
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
{
  double matrix[4];
  char *end;
  int i;

  for (i = 0, p++; i < 4 && *p && *p != ']'; i++)
    {
      if (*p == '~')
	matrix[i] = - strtod (p + 1, &end);
      else
	matrix[i] = strtod (p, &end);
      p = end;
    }
  return (i == 4 ? (int) matrix[3] : -1);
}

841
/* Expand a wildcard field in FIELD (the first N fields are filled) to
842
   multiple fields to fill in all 14 XLFD fields while restricting a
843 844
   field position by its contents.  */

845
static int
846
font_expand_wildcards (Lisp_Object *field, int n)
847 848 849 850 851 852 853 854
{
  /* Copy of FIELD.  */
  Lisp_Object tmp[XLFD_LAST_INDEX];
  /* Array of information about where this element can go.  Nth
     element is for Nth element of FIELD. */
  struct {
    /* Minimum possible field.  */
    int from;
Paul Eggert's avatar
Paul Eggert committed
855
    /* Maximum possible field.  */
856 857 858 859 860
    int to;
    /* Bit mask of possible field.  Nth bit corresponds to Nth field.  */
    int mask;
  } range[XLFD_LAST_INDEX];
  int i, j;
861
  int range_from, range_to;
862 863 864 865 866 867
  unsigned range_mask;

#define XLFD_SYMBOL_MASK (XLFD_FOUNDRY_MASK | XLFD_FAMILY_MASK \
			  | XLFD_ADSTYLE_MASK  | XLFD_REGISTRY_MASK)
#define XLFD_NULL_MASK (XLFD_FOUNDRY_MASK | XLFD_ADSTYLE_MASK)
#define XLFD_LARGENUM_MASK (XLFD_POINT_MASK | XLFD_RESX_MASK | XLFD_RESY_MASK \
868
			    | XLFD_AVGWIDTH_MASK)
869 870 871 872 873 874 875 876
#define XLFD_REGENC_MASK (XLFD_REGISTRY_MASK | XLFD_ENCODING_MASK)

  /* Initialize RANGE_MASK for FIELD[0] which can be 0th to (14 - N)th
     field.  The value is shifted to left one bit by one in the
     following loop.  */
  for (i = 0, range_mask = 0; i <= 14 - n; i++)
    range_mask = (range_mask << 1) | 1;

877
  /* The triplet RANGE_FROM, RANGE_TO, and RANGE_MASK is a
Paul Eggert's avatar
Paul Eggert committed
878
     position-based restriction for FIELD[I].  */
879 880
  for (i = 0, range_from = 0, range_to = 14 - n; i < n;
       i++, range_from++, range_to++, range_mask <<= 1)
881 882 883 884 885 886 887 888 889 890 891 892 893 894
    {
      Lisp_Object val = field[i];

      tmp[i] = val;
      if (NILP (val))
	{
	  /* Wildcard.  */
	  range[i].from = range_from;
	  range[i].to = range_to;
	  range[i].mask = range_mask;
	}
      else
	{
	  /* The triplet FROM, TO, and MASK is a value-based
Paul Eggert's avatar
Paul Eggert committed
895
	     restriction for FIELD[I].  */
896 897 898 899 900
	  int from, to;
	  unsigned mask;

	  if (INTEGERP (val))
	    {
901
	      EMACS_INT numeric = XINT (val);
902

903 904 905
	      if (i + 1 == n)
		from = to = XLFD_ENCODING_INDEX,
		  mask = XLFD_ENCODING_MASK;
906 907 908
	      else if (numeric == 0)
		from = XLFD_PIXEL_INDEX, to = XLFD_AVGWIDTH_INDEX,
		  mask = XLFD_PIXEL_MASK | XLFD_LARGENUM_MASK;
909 910 911
	      else if (numeric <= 48)
		from = to = XLFD_PIXEL_INDEX,
		  mask = XLFD_PIXEL_MASK;
912
	      else
913
		from = XLFD_POINT_INDEX, to = XLFD_AVGWIDTH_INDEX,
914 915
		  mask = XLFD_LARGENUM_MASK;
	    }
Kenichi Handa's avatar
Kenichi Handa committed
916
	  else if (SBYTES (SYMBOL_NAME (val)) == 0)
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
	    from = XLFD_FOUNDRY_INDEX, to = XLFD_ADSTYLE_INDEX,
	      mask = XLFD_NULL_MASK;
	  else if (i == 0)
	    from = to = XLFD_FOUNDRY_INDEX, mask = XLFD_FOUNDRY_MASK;
	  else if (i + 1 == n)
	    {
	      Lisp_Object name = SYMBOL_NAME (val);

	      if (SDATA (name)[SBYTES (name) - 1] == '*')
		from = XLFD_REGISTRY_INDEX, to = XLFD_ENCODING_INDEX,
		  mask = XLFD_REGENC_MASK;
	      else
		from = to = XLFD_ENCODING_INDEX,
		  mask = XLFD_ENCODING_MASK;
	    }
932 933
	  else if (range_from <= XLFD_WEIGHT_INDEX
		   && range_to >= XLFD_WEIGHT_INDEX
Kenichi Handa's avatar
Kenichi Handa committed
934
		   && FONT_WEIGHT_NAME_NUMERIC (val) >= 0)
935
	    from = to = XLFD_WEIGHT_INDEX, mask = XLFD_WEIGHT_MASK;
936 937
	  else if (range_from <= XLFD_SLANT_INDEX
		   && range_to >= XLFD_SLANT_INDEX
Kenichi Handa's avatar
Kenichi Handa committed
938
		   && FONT_SLANT_NAME_NUMERIC (val) >= 0)
939
	    from = to = XLFD_SLANT_INDEX, mask = XLFD_SLANT_MASK;
940 941
	  else if (range_from <= XLFD_SWIDTH_INDEX
		   && range_to >= XLFD_SWIDTH_INDEX
Kenichi Handa's avatar
Kenichi Handa committed
942
		   && FONT_WIDTH_NAME_NUMERIC (val) >= 0)
943 944 945
	    from = to = XLFD_SWIDTH_INDEX, mask = XLFD_SWIDTH_MASK;
	  else
	    {
946
	      if (EQ (val, Qc) || EQ (val, Qm) || EQ (val, Qp) || EQ (val, Qd))
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
		from = to = XLFD_SPACING_INDEX, mask = XLFD_SPACING_MASK;
	      else
		from = XLFD_FOUNDRY_INDEX, to = XLFD_ENCODING_INDEX,
		  mask = XLFD_SYMBOL_MASK;
	    }

	  /* Merge position-based and value-based restrictions.  */
	  mask &= range_mask;
	  while (from < range_from)
	    mask &= ~(1 << from++);
	  while (from < 14 && ! (mask & (1 << from)))
	    from++;
	  while (to > range_to)
	    mask &= ~(1 << to--);
	  while (to