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

Paul Eggert's avatar
Paul Eggert committed
3
Copyright (C) 2006-2020 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 <https://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
#include <stdio.h>
Paul Eggert's avatar
Paul Eggert committed
26
#include <stdlib.h>
Kenichi Handa's avatar
Kenichi Handa committed
27

28 29
#include <c-ctype.h>

Kenichi Handa's avatar
Kenichi Handa committed
30
#include "lisp.h"
31
#include "character.h"
Kenichi Handa's avatar
Kenichi Handa committed
32 33
#include "buffer.h"
#include "frame.h"
Kenichi Handa's avatar
Kenichi Handa committed
34
#include "window.h"
Kenichi Handa's avatar
Kenichi Handa committed
35 36 37 38 39
#include "dispextern.h"
#include "charset.h"
#include "composite.h"
#include "fontset.h"
#include "font.h"
40
#include "termhooks.h"
Daniel Colascione's avatar
Daniel Colascione committed
41
#include "pdumper.h"
Kenichi Handa's avatar
Kenichi Handa committed
42

43 44 45
#ifdef HAVE_WINDOW_SYSTEM
#include TERM_HEADER
#endif /* HAVE_WINDOW_SYSTEM */
46 47 48

#define DEFAULT_ENCODING Qiso8859_1

49
/* Vector of Vfont_weight_table, Vfont_slant_table, and Vfont_width_table. */
Kenichi Handa's avatar
Kenichi Handa committed
50 51
static Lisp_Object font_style_table;

52 53 54 55 56 57 58 59
/* 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.  */
60
  const char *names[5];
61 62 63 64 65
};

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

66
static const struct table_entry weight_table[] =
67 68 69 70 71 72
{
  { 0, { "thin" }},
  { 20, { "ultra-light", "ultralight" }},
  { 40, { "extra-light", "extralight" }},
  { 50, { "light" }},
  { 75, { "semi-light", "semilight", "demilight", "book" }},
73
  { 100, { "normal", "medium", "regular", "unspecified" }},
74 75 76 77 78 79 80 81 82
  { 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.  */

83
static const struct table_entry slant_table[] =
84 85 86
{
  { 0, { "reverse-oblique", "ro" }},
  { 10, { "reverse-italic", "ri" }},
87
  { 100, { "normal", "r", "unspecified" }},
88 89 90 91 92 93 94
  { 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.  */

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

108
/* Alist of font registry symbols and the corresponding charset
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
   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;

126 127
/* 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
128 129 130
   here.  */
static struct font_driver_list *font_driver_list;

131 132 133 134 135
#ifdef ENABLE_CHECKING

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

bool
136
valid_font_driver (struct font_driver const *drv)
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
{
  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
152

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

Andreas Schwab's avatar
Andreas Schwab committed
155
static Lisp_Object
156
font_make_spec (void)
Kenichi Handa's avatar
Kenichi Handa committed
157 158 159 160 161
{
  Lisp_Object font_spec;
  struct font_spec *spec
    = ((struct font_spec *)
       allocate_pseudovector (VECSIZE (struct font_spec),
162
			      FONT_SPEC_MAX, FONT_SPEC_MAX, PVEC_FONT));
Kenichi Handa's avatar
Kenichi Handa committed
163 164 165 166 167
  XSETFONT (font_spec, spec);
  return font_spec;
}

Lisp_Object
168
font_make_entity (void)
Kenichi Handa's avatar
Kenichi Handa committed
169 170 171 172 173
{
  Lisp_Object font_entity;
  struct font_entity *entity
    = ((struct font_entity *)
       allocate_pseudovector (VECSIZE (struct font_entity),
174
			      FONT_ENTITY_MAX, FONT_ENTITY_MAX, PVEC_FONT));
Kenichi Handa's avatar
Kenichi Handa committed
175 176 177 178
  XSETFONT (font_entity, entity);
  return font_entity;
}

179 180 181
/* 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
182
Lisp_Object
183
font_make_object (int size, Lisp_Object entity, int pixelsize)
Kenichi Handa's avatar
Kenichi Handa committed
184 185 186
{
  Lisp_Object font_object;
  struct font *font
187 188
    = (struct font *) allocate_pseudovector (size, FONT_OBJECT_MAX,
					     FONT_OBJECT_MAX, PVEC_FONT);
189 190
  int i;

191 192 193
  /* 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
194 195
  XSETFONT (font_object, font);

196 197 198 199 200 201
  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]
202
	  = Fcopy_alist (AREF (entity, FONT_EXTRA_INDEX));
203 204
    }
  if (size > 0)
205
    font->props[FONT_SIZE_INDEX] = make_fixnum (pixelsize);
Kenichi Handa's avatar
Kenichi Handa committed
206 207 208
  return font_object;
}

209 210
#if defined (HAVE_XFT) || defined (HAVE_FREETYPE) || defined (HAVE_NS)

211 212
static int font_unparse_fcname (Lisp_Object, int, char *, int);

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
/* 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
238

Dmitry Antipov's avatar
Dmitry Antipov committed
239 240 241
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 *,
242
                                         Lisp_Object);
Andreas Schwab's avatar
Andreas Schwab committed
243
static unsigned font_encode_char (Lisp_Object, int);
Kenichi Handa's avatar
Kenichi Handa committed
244 245 246 247

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

Kenichi Handa's avatar
Kenichi Handa committed
248 249

/* Return a Lispy value of a font property value at STR and LEN bytes.
250 251 252
   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
253 254

Lisp_Object
255
font_intern_prop (const char *str, ptrdiff_t len, bool force_symbol)
Kenichi Handa's avatar
Kenichi Handa committed
256
{
257 258
  ptrdiff_t i, nbytes, nchars;
  Lisp_Object tem, name, obarray;
Kenichi Handa's avatar
Kenichi Handa committed
259 260 261

  if (len == 1 && *str == '*')
    return Qnil;
Paul Eggert's avatar
Paul Eggert committed
262
  if (!force_symbol && 0 < len && '0' <= *str && *str <= '9')
Kenichi Handa's avatar
Kenichi Handa committed
263 264
    {
      for (i = 1; i < len; i++)
265
	if (! ('0' <= str[i] && str[i] <= '9'))
Kenichi Handa's avatar
Kenichi Handa committed
266 267
	  break;
      if (i == len)
268
	{
269
	  i = 0;
270 271
	  for (EMACS_INT n = 0;
	       (n += str[i++] - '0') <= MOST_POSITIVE_FIXNUM; )
272 273
	    {
	      if (i == len)
274
		return make_fixnum (n);
275
	      if (INT_MULTIPLY_WRAPV (n, 10, &n))
276 277 278 279
		break;
	    }

	  xsignal1 (Qoverflow_error, make_string (str, len));
280
	}
Kenichi Handa's avatar
Kenichi Handa committed
281 282
    }

283 284
  /* This code is similar to intern function from lread.c.  */
  obarray = check_obarray (Vobarray);
285
  parse_str_as_multibyte ((unsigned char *) str, len, &nchars, &nbytes);
286 287
  tem = oblookup (obarray, str,
		  (len == nchars || len != nbytes) ? len : nchars, len);
Kenichi Handa's avatar
Kenichi Handa committed
288 289
  if (SYMBOLP (tem))
    return tem;
290 291
  name = make_specified_string (str, nchars, len,
				len != nchars && len == nbytes);
292
  return intern_driver (name, obarray, tem);
Kenichi Handa's avatar
Kenichi Handa committed
293 294
}

295
/* Return a pixel size of font-spec SPEC on frame F.  */
296

297
static int
Dmitry Antipov's avatar
Dmitry Antipov committed
298
font_pixel_size (struct frame *f, Lisp_Object spec)
299
{
300
#ifdef HAVE_WINDOW_SYSTEM
301 302
  Lisp_Object size = AREF (spec, FONT_SIZE_INDEX);
  double point_size;
Kenichi Handa's avatar
Kenichi Handa committed
303
  int dpi, pixel_size;
304
  Lisp_Object val;
305

306
  if (FIXNUMP (size))
Tom Tromey's avatar
Tom Tromey committed
307
    return XFIXNUM (size);
308
  if (NILP (size))
309
    return 0;
310 311 312 313 314
  if (FRAME_WINDOW_P (f))
    {
      eassert (FLOATP (size));
      point_size = XFLOAT_DATA (size);
      val = AREF (spec, FONT_DPI_INDEX);
315
      if (FIXNUMP (val))
Tom Tromey's avatar
Tom Tromey committed
316
	dpi = XFIXNUM (val);
317 318 319 320 321
      else
	dpi = FRAME_RES_Y (f);
      pixel_size = POINT_TO_PIXEL (point_size, dpi);
      return pixel_size;
    }
322
#endif
323
  return 1;
324 325
}

Kenichi Handa's avatar
Kenichi Handa committed
326

Kenichi Handa's avatar
Kenichi Handa committed
327 328 329 330
/* 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
331
   font_style_table if VAL is a symbol, and return the closest index if
Kenichi Handa's avatar
Kenichi Handa committed
332
   VAL is an integer.  */
Kenichi Handa's avatar
Kenichi Handa committed
333

Kenichi Handa's avatar
Kenichi Handa committed
334
int
335 336
font_style_to_value (enum font_property_index prop, Lisp_Object val,
                     bool noerror)
Kenichi Handa's avatar
Kenichi Handa committed
337
{
Kenichi Handa's avatar
Kenichi Handa committed
338
  Lisp_Object table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
339 340 341 342
  int len;

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

Kenichi Handa's avatar
Kenichi Handa committed
344
  if (SYMBOLP (val))
Kenichi Handa's avatar
Kenichi Handa committed
345
    {
346
      int i, j;
347
      char *s;
348
      Lisp_Object elt;
Kenichi Handa's avatar
Kenichi Handa committed
349 350 351

      /* At first try exact match.  */
      for (i = 0; i < len; i++)
352 353 354 355 356
	{
	  CHECK_VECTOR (AREF (table, i));
	  for (j = 1; j < ASIZE (AREF (table, i)); j++)
	    if (EQ (val, AREF (AREF (table, i), j)))
	      {
357
		CHECK_FIXNUM (AREF (AREF (table, i), 0));
Tom Tromey's avatar
Tom Tromey committed
358
		return ((XFIXNUM (AREF (AREF (table, i), 0)) << 8)
359 360 361
			| (i << 4) | (j - 1));
	      }
	}
Kenichi Handa's avatar
Kenichi Handa committed
362
      /* Try also with case-folding match.  */
363
      s = SSDATA (SYMBOL_NAME (val));
Kenichi Handa's avatar
Kenichi Handa committed
364
      for (i = 0; i < len; i++)
365 366 367
	for (j = 1; j < ASIZE (AREF (table, i)); j++)
	  {
	    elt = AREF (AREF (table, i), j);
368
	    if (xstrcasecmp (s, SSDATA (SYMBOL_NAME (elt))) == 0)
369
	      {
370
		CHECK_FIXNUM (AREF (AREF (table, i), 0));
Tom Tromey's avatar
Tom Tromey committed
371
		return ((XFIXNUM (AREF (AREF (table, i), 0)) << 8)
372 373
			| (i << 4) | (j - 1));
	      }
374
	  }
Kenichi Handa's avatar
Kenichi Handa committed
375 376
      if (! noerror)
	return -1;
377
      eassert (len < 255);
378
      elt = make_vector (2, make_fixnum (100));
379
      ASET (elt, 1, val);
380
      ASET (font_style_table, prop - FONT_WEIGHT_INDEX,
381
	    CALLN (Fvconcat, table, make_vector (1, elt)));
382
      return (100 << 8) | (i << 4);
Kenichi Handa's avatar
Kenichi Handa committed
383
    }
Kenichi Handa's avatar
Kenichi Handa committed
384 385
  else
    {
386
      int i, last_n;
Tom Tromey's avatar
Tom Tromey committed
387
      EMACS_INT numeric = XFIXNUM (val);
Kenichi Handa's avatar
Kenichi Handa committed
388

389
      for (i = 0, last_n = -1; i < len; i++)
Kenichi Handa's avatar
Kenichi Handa committed
390
	{
391
	  int n;
Kenichi Handa's avatar
Kenichi Handa committed
392

393
	  CHECK_VECTOR (AREF (table, i));
394
	  CHECK_FIXNUM (AREF (AREF (table, i), 0));
Tom Tromey's avatar
Tom Tromey committed
395
	  n = XFIXNUM (AREF (AREF (table, i), 0));
Kenichi Handa's avatar
Kenichi Handa committed
396
	  if (numeric == n)
397
	    return (n << 8) | (i << 4);
Kenichi Handa's avatar
Kenichi Handa committed
398 399 400 401
	  if (numeric < n)
	    {
	      if (! noerror)
		return -1;
402 403
	      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
404 405 406 407 408
	    }
	  last_n = n;
	}
      if (! noerror)
	return -1;
409
      return ((last_n << 8) | ((i - 1) << 4));
Kenichi Handa's avatar
Kenichi Handa committed
410 411
    }
}
Kenichi Handa's avatar
Kenichi Handa committed
412 413

Lisp_Object
414 415
font_style_symbolic (Lisp_Object font, enum font_property_index prop,
                     bool for_face)
Kenichi Handa's avatar
Kenichi Handa committed
416
{
417
  Lisp_Object val = AREF (font, prop);
418 419
  Lisp_Object table, elt;
  int i;
Kenichi Handa's avatar
Kenichi Handa committed
420

Kenichi Handa's avatar
Kenichi Handa committed
421 422 423
  if (NILP (val))
    return Qnil;
  table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
424
  CHECK_VECTOR (table);
Tom Tromey's avatar
Tom Tromey committed
425
  i = XFIXNUM (val) & 0xFF;
Paul Eggert's avatar
Paul Eggert committed
426
  eassert (((i >> 4) & 0xF) < ASIZE (table));
427
  elt = AREF (table, ((i >> 4) & 0xF));
428
  CHECK_VECTOR (elt);
Paul Eggert's avatar
Paul Eggert committed
429
  eassert ((i & 0xF) + 1 < ASIZE (elt));
430 431 432
  elt = (for_face ? AREF (elt, 1) : AREF (elt, (i & 0xF) + 1));
  CHECK_SYMBOL (elt);
  return elt;
433 434
}

435 436 437 438 439
/* 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
440
find_font_encoding (Lisp_Object fontname)
441 442 443 444 445 446 447 448 449 450 451 452 453 454
{
  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));
    }
455
  return Qnil;
456 457
}

458 459 460 461 462
/* 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
463
font_registry_charsets (Lisp_Object registry, struct charset **encoding, struct charset **repertory)
464 465 466 467
{
  Lisp_Object val;
  int encoding_id, repertory_id;

Kenichi Handa's avatar
Kenichi Handa committed
468
  val = Fassoc_string (registry, font_charset_alist, Qt);
469 470 471 472 473
  if (! NILP (val))
    {
      val = XCDR (val);
      if (NILP (val))
	return -1;
Tom Tromey's avatar
Tom Tromey committed
474 475
      encoding_id = XFIXNUM (XCAR (val));
      repertory_id = XFIXNUM (XCDR (val));
476 477 478 479 480 481
    }
  else
    {
      val = find_font_encoding (SYMBOL_NAME (registry));
      if (SYMBOLP (val) && CHARSETP (val))
	{
Tom Tromey's avatar
Tom Tromey committed
482
	  encoding_id = repertory_id = XFIXNUM (CHARSET_SYMBOL_ID (val));
483 484 485 486 487
	}
      else if (CONSP (val))
	{
	  if (! CHARSETP (XCAR (val)))
	    goto invalid_entry;
Tom Tromey's avatar
Tom Tromey committed
488
	  encoding_id = XFIXNUM (CHARSET_SYMBOL_ID (XCAR (val)));
489 490 491 492 493 494
	  if (NILP (XCDR (val)))
	    repertory_id = -1;
	  else
	    {
	      if (! CHARSETP (XCDR (val)))
		goto invalid_entry;
Tom Tromey's avatar
Tom Tromey committed
495
	      repertory_id = XFIXNUM (CHARSET_SYMBOL_ID (XCDR (val)));
496
	    }
497
	}
498 499
      else
	goto invalid_entry;
500
      val = Fcons (make_fixnum (encoding_id), make_fixnum (repertory_id));
501
      font_charset_alist
502
	= nconc2 (font_charset_alist, list1 (Fcons (registry, val)));
503 504 505 506 507 508 509 510 511 512
    }

  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
513
    = nconc2 (font_charset_alist, list1 (Fcons (registry, Qnil)));
514 515 516
  return -1;
}

Kenichi Handa's avatar
Kenichi Handa committed
517

518
/* Font property value validators.  See the comment of
519 520
   font_property_table for the meaning of the arguments.  */

521 522 523 524 525 526
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
527 528

static Lisp_Object
529
font_prop_validate_symbol (Lisp_Object prop, Lisp_Object val)
Kenichi Handa's avatar
Kenichi Handa committed
530 531
{
  if (STRINGP (val))
Kenichi Handa's avatar
Kenichi Handa committed
532 533
    val = Fintern (val, Qnil);
  if (! SYMBOLP (val))
Kenichi Handa's avatar
Kenichi Handa committed
534
    val = Qerror;
Kenichi Handa's avatar
Kenichi Handa committed
535 536
  else if (EQ (prop, QCregistry))
    val = Fintern (Fdowncase (SYMBOL_NAME (val)), Qnil);
Kenichi Handa's avatar
Kenichi Handa committed
537 538 539
  return val;
}

Kenichi Handa's avatar
Kenichi Handa committed
540

Kenichi Handa's avatar
Kenichi Handa committed
541
static Lisp_Object
542
font_prop_validate_style (Lisp_Object style, Lisp_Object val)
Kenichi Handa's avatar
Kenichi Handa committed
543
{
Kenichi Handa's avatar
Kenichi Handa committed
544 545 546
  enum font_property_index prop = (EQ (style, QCweight) ? FONT_WEIGHT_INDEX
				   : EQ (style, QCslant) ? FONT_SLANT_INDEX
				   : FONT_WIDTH_INDEX);
547
  if (FIXNUMP (val))
Kenichi Handa's avatar
Kenichi Handa committed
548
    {
Tom Tromey's avatar
Tom Tromey committed
549
      EMACS_INT n = XFIXNUM (val);
550
      CHECK_VECTOR (AREF (font_style_table, prop - FONT_WEIGHT_INDEX));
551
      if (((n >> 4) & 0xF)
Kenichi Handa's avatar
Kenichi Handa committed
552
	  >= ASIZE (AREF (font_style_table, prop - FONT_WEIGHT_INDEX)))
Kenichi Handa's avatar
Kenichi Handa committed
553 554 555
	val = Qerror;
      else
	{
556 557
	  Lisp_Object elt = AREF (AREF (font_style_table, prop - FONT_WEIGHT_INDEX), (n >> 4) & 0xF);

558
	  CHECK_VECTOR (elt);
559 560
	  if ((n & 0xF) + 1 >= ASIZE (elt))
	    val = Qerror;
561 562
	  else
	    {
563
	      CHECK_FIXNUM (AREF (elt, 0));
Tom Tromey's avatar
Tom Tromey committed
564
	      if (XFIXNUM (AREF (elt, 0)) != (n >> 8))
565 566
		val = Qerror;
	    }
Kenichi Handa's avatar
Kenichi Handa committed
567 568
	}
    }
Kenichi Handa's avatar
Kenichi Handa committed
569 570 571 572
  else if (SYMBOLP (val))
    {
      int n = font_style_to_value (prop, val, 0);

573
      val = n >= 0 ? make_fixnum (n) : Qerror;
Kenichi Handa's avatar
Kenichi Handa committed
574 575 576
    }
  else
    val = Qerror;
Kenichi Handa's avatar
Kenichi Handa committed
577 578 579 580
  return val;
}

static Lisp_Object
581
font_prop_validate_non_neg (Lisp_Object prop, Lisp_Object val)
Kenichi Handa's avatar
Kenichi Handa committed
582
{
583
  return (FIXNATP (val) || (FLOATP (val) && XFLOAT_DATA (val) >= 0)
Kenichi Handa's avatar
Kenichi Handa committed
584 585 586 587
	  ? val : Qerror);
}

static Lisp_Object
588
font_prop_validate_spacing (Lisp_Object prop, Lisp_Object val)
589
{
Tom Tromey's avatar
Tom Tromey committed
590
  if (NILP (val) || (FIXNATP (val) && XFIXNUM (val) <= FONT_SPACING_CHARCELL))
591
    return val;
592 593 594 595 596
  if (SYMBOLP (val) && SBYTES (SYMBOL_NAME (val)) == 1)
    {
      char spacing = SDATA (SYMBOL_NAME (val))[0];

      if (spacing == 'c' || spacing == 'C')
597
	return make_fixnum (FONT_SPACING_CHARCELL);
598
      if (spacing == 'm' || spacing == 'M')
599
	return make_fixnum (FONT_SPACING_MONO);
600
      if (spacing == 'p' || spacing == 'P')
601
	return make_fixnum (FONT_SPACING_PROPORTIONAL);
602
      if (spacing == 'd' || spacing == 'D')
603
	return make_fixnum (FONT_SPACING_DUAL);
604
    }
605 606 607
  return Qerror;
}

608
static Lisp_Object
609
font_prop_validate_otf (Lisp_Object prop, Lisp_Object val)
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
{
  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;
}

642
/* Structure of known font property keys and validator of the
643
   values.  */
644
static const struct
Kenichi Handa's avatar
Kenichi Handa committed
645
{
Paul Eggert's avatar
Paul Eggert committed
646 647
  /* Index of the key symbol.  */
  int key;
648 649 650
  /* 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
651
  Lisp_Object (*validator) (Lisp_Object prop, Lisp_Object val);
652
} font_property_table[] =
Paul Eggert's avatar
Paul Eggert committed
653 654 655 656 657 658 659 660 661 662 663 664
  { { SYMBOL_INDEX (QCtype), font_prop_validate_symbol },
    { SYMBOL_INDEX (QCfoundry), font_prop_validate_symbol },
    { SYMBOL_INDEX (QCfamily), font_prop_validate_symbol },
    { SYMBOL_INDEX (QCadstyle), font_prop_validate_symbol },
    { SYMBOL_INDEX (QCregistry), font_prop_validate_symbol },
    { SYMBOL_INDEX (QCweight), font_prop_validate_style },
    { SYMBOL_INDEX (QCslant), font_prop_validate_style },
    { SYMBOL_INDEX (QCwidth), font_prop_validate_style },
    { SYMBOL_INDEX (QCsize), font_prop_validate_non_neg },
    { SYMBOL_INDEX (QCdpi), font_prop_validate_non_neg },
    { SYMBOL_INDEX (QCspacing), font_prop_validate_spacing },
    { SYMBOL_INDEX (QCavgwidth), font_prop_validate_non_neg },
Kenichi Handa's avatar
Kenichi Handa committed
665 666
    /* The order of the above entries must match with enum
       font_property_index.  */
Paul Eggert's avatar
Paul Eggert committed
667 668 669
    { SYMBOL_INDEX (QClang), font_prop_validate_symbol },
    { SYMBOL_INDEX (QCscript), font_prop_validate_symbol },
    { SYMBOL_INDEX (QCotf), font_prop_validate_otf }
Kenichi Handa's avatar
Kenichi Handa committed
670 671
  };

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

675
static int
676
get_font_prop_index (Lisp_Object key)
Kenichi Handa's avatar
Kenichi Handa committed
677
{
Kenichi Handa's avatar
Kenichi Handa committed
678 679
  int i;

680
  for (i = 0; i < ARRAYELTS (font_property_table); i++)
Paul Eggert's avatar
Paul Eggert committed
681
    if (EQ (key, builtin_lisp_symbol (font_property_table[i].key)))
Kenichi Handa's avatar
Kenichi Handa committed
682
      return i;
683
  return -1;
Kenichi Handa's avatar
Kenichi Handa committed
684 685
}

Kenichi Handa's avatar
Kenichi Handa committed
686 687 688
/* 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.  */
689

Kenichi Handa's avatar
Kenichi Handa committed
690
static Lisp_Object
691
font_prop_validate (int idx, Lisp_Object prop, Lisp_Object val)
Kenichi Handa's avatar
Kenichi Handa committed
692
{
Kenichi Handa's avatar
Kenichi Handa committed
693
  Lisp_Object validated;
Kenichi Handa's avatar
Kenichi Handa committed
694

695 696
  if (NILP (val))
    return val;
Kenichi Handa's avatar
Kenichi Handa committed
697
  if (NILP (prop))
Paul Eggert's avatar
Paul Eggert committed
698
    prop = builtin_lisp_symbol (font_property_table[idx].key);
Kenichi Handa's avatar
Kenichi Handa committed
699
  else
700
    {
Kenichi Handa's avatar
Kenichi Handa committed
701 702 703
      idx = get_font_prop_index (prop);
      if (idx < 0)
	return val;
704
    }
Paul Eggert's avatar
Paul Eggert committed
705
  validated = (font_property_table[idx].validator) (prop, val);
Kenichi Handa's avatar
Kenichi Handa committed
706 707 708
  if (EQ (validated, Qerror))
    signal_error ("invalid font property", Fcons (prop, val));
  return validated;
Kenichi Handa's avatar
Kenichi Handa committed
709
}
710

Kenichi Handa's avatar
Kenichi Handa committed
711 712

/* Store VAL as a value of extra font property PROP in FONT while
713 714 715
   keeping the sorting order.  Don't check the validity of VAL.  If
   VAL is Qunbound, delete the slot for PROP from the list of extra
   properties.  */
716

717
Lisp_Object
718
font_put_extra (Lisp_Object font, Lisp_Object prop, Lisp_Object val)
719 720
{
  Lisp_Object extra = AREF (font, FONT_EXTRA_INDEX);
721
  Lisp_Object slot = (NILP (extra) ? Qnil : assq_no_quit (prop, extra));
722 723 724

  if (NILP (slot))
    {
Kenichi Handa's avatar
Kenichi Handa committed
725 726
      Lisp_Object prev = Qnil;

727 728
      if (EQ (val, Qunbound))
	return val;
Kenichi Handa's avatar
Kenichi Handa committed
729 730 731
      while (CONSP (extra)
	     && NILP (Fstring_lessp (prop, XCAR (XCAR (extra)))))
	prev = extra, extra = XCDR (extra);
732

733
      if (NILP (prev))
734
        ASET (font, FONT_EXTRA_INDEX, Fcons (Fcons (prop, val), extra));
735
      else
736
        XSETCDR (prev, Fcons (Fcons (prop, val), extra));
737

738
      return val;
739 740
    }
  XSETCDR (slot, val);
741
  if (EQ (val, Qunbound))
742
    ASET (font, FONT_EXTRA_INDEX, Fdelq (slot, extra));
743
  return val;
744 745
}

Kenichi Handa's avatar
Kenichi Handa committed
746

747
/* Font name parser and unparser.  */
Kenichi Handa's avatar
Kenichi Handa committed
748

749
static int parse_matrix (const char *);
750
static int font_expand_wildcards (Lisp_Object *, int);
751
static int font_parse_name (char *, ptrdiff_t, Lisp_Object);
Kenichi Handa's avatar
Kenichi Handa committed
752

753
/* An enumerator for each field of an XLFD font name.  */
Kenichi Handa's avatar
Kenichi Handa committed
754 755 756 757 758 759 760 761
enum xlfd_field_index
{
  XLFD_FOUNDRY_INDEX,
  XLFD_FAMILY_INDEX,
  XLFD_WEIGHT_INDEX,
  XLFD_SLANT_INDEX,
  XLFD_SWIDTH_INDEX,
  XLFD_ADSTYLE_INDEX,
762 763
  XLFD_PIXEL_INDEX,
  XLFD_POINT_INDEX,
Kenichi Handa's avatar
Kenichi Handa committed
764 765 766 767 768 769 770 771 772
  XLFD_RESX_INDEX,
  XLFD_RESY_INDEX,
  XLFD_SPACING_INDEX,
  XLFD_AVGWIDTH_INDEX,
  XLFD_REGISTRY_INDEX,
  XLFD_ENCODING_INDEX,
  XLFD_LAST_INDEX
};

773
/* An enumerator for mask bit corresponding to each XLFD field.  */
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
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
};


793
/* Parse P pointing to the pixel/point size field of the form
Kenichi Handa's avatar
Kenichi Handa committed
794 795 796 797 798 799 800 801 802 803 804 805 806 807
   `[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
808
parse_matrix (const char *p)
Kenichi Handa's avatar
Kenichi Handa committed
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
{
  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);
}

825
/* Expand a wildcard field in FIELD (the first N fields are filled) to
826
   multiple fields to fill in all 14 XLFD fields while restricting a
827 828
   field position by its contents.  */

829
static int
830
font_expand_wildcards (Lisp_Object *field, int n)
831 832 833 834 835 836 837 838
{
  /* 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
839
    /* Maximum possible field.  */
840 841 842 843 844
    int to;
    /* Bit mask of possible field.  Nth bit corresponds to Nth field.  */
    int mask;
  } range[XLFD_LAST_INDEX];
  int i, j;
845
  int range_from, range_to;
846 847 848 849 850 851
  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 \
852
			    | XLFD_AVGWIDTH_MASK)
853 854 855 856 857 858 859 860
#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;

861
  /* The triplet RANGE_FROM, RANGE_TO, and RANGE_MASK is a
Paul Eggert's avatar
Paul Eggert committed
862
     position-based restriction for FIELD[I].  */
863 864
  for (i = 0, range_from = 0, range_to = 14 - n; i < n;
       i++, range_from++, range_to++, range_mask <<= 1)
865 866 867 868 869 870 871 872 873 874 875 876 877 878
    {
      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
879
	     restriction for FIELD[I].  */
880 881 882
	  int from, to;
	  unsigned mask;

883
	  if (FIXNUMP (val))
884
	    {
Tom Tromey's avatar
Tom Tromey committed
885
	      EMACS_INT numeric = XFIXNUM (val);
886

887 888 889
	      if (i + 1 == n)
		from = to = XLFD_ENCODING_INDEX,
		  mask = XLFD_ENCODING_MASK;
890 891 892
	      else if (numeric == 0)
		from = XLFD_PIXEL_INDEX, to = XLFD_AVGWIDTH_INDEX,
		  mask = XLFD_PIXEL_MASK | XLFD_LARGENUM_MASK;
893 894 895
	      else if (numeric <= 48)
		from = to = XLFD_PIXEL_INDEX,
		  mask = XLFD_PIXEL_MASK;
896
	      else
897
		from = XLFD_POINT_INDEX, to = XLFD_AVGWIDTH_INDEX,
898 899
		  mask = XLFD_LARGENUM_MASK;
	    }
Kenichi Handa's avatar
Kenichi Handa committed
900
	  else if (SBYTES (SYMBOL_NAME (val)) == 0)
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
	    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;
	    }
916 917
	  else if (range_from <= XLFD_WEIGHT_INDEX
		   && range_to >= XLFD_WEIGHT_INDEX
Kenichi Handa's avatar
Kenichi Handa committed
918
		   && FONT_WEIGHT_NAME_NUMERIC (val) >= 0)
919
	    from = to = XLFD_WEIGHT_INDEX, mask = XLFD_WEIGHT_MASK;
920 921
	  else if (range_from <= XLFD_SLANT_INDEX
		   && range_to >= XLFD_SLANT_INDEX
Kenichi Handa's avatar
Kenichi Handa committed
922
		   && FONT_SLANT_NAME_NUMERIC (val) >= 0)
923
	    from = to = XLFD_SLANT_INDEX, mask = XLFD_SLANT_MASK;
924 925
	  else if (range_from <= XLFD_SWIDTH_INDEX
		   && range_to >= XLFD_SWIDTH_INDEX
Kenichi Handa's avatar
Kenichi Handa committed
926
		   && FONT_WIDTH_NAME_NUMERIC (val) >= 0)
927 928 929
	    from = to = XLFD_SWIDTH_INDEX, mask = XLFD_SWIDTH_MASK;
	  else
	    {
930
	      if (EQ (val, Qc) || EQ (val, Qm) || EQ (val, Qp) || EQ (val, Qd))
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
		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 >= 0 && ! (mask & (1 << to)))
	    to--;
	  if (from > to)
	    return -1;
	  range[i].from = from;
	  range[i].to = to;
	  range[i].mask = mask;

	  if (from > range_from || to < range_to)
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
	    {
	      /* The range is narrowed by value-based restrictions.
		 Reflect it to the other fields.  */

	      /* Following fields should be after FROM.  */
	      range_from = from;
	      /* Preceding fields should be before TO.  */
	      for (j = i - 1, from--, to--; j >= 0; j--, from--, to--)
		{
		  /* Check FROM for non-wildcard field.  */
		  if (! NILP (tmp[j]) && range[j].from < from)
		    {
		      while (range[j].from < from)
			range[j].mask &= ~(1 << range[j].from++);
		      while (from < 14 && ! (range[j].mask & (1 << from)))
			from++;
		      range[j].from = from;
		    }
		  else
		    from = range[j].from;
		  if (range[j].to > to)
		    {
		      while (range[j].to > to)
			range[j].mask &= ~(1 << range[j].to--);
		      while (to >= 0 && ! (range[j].mask & (1 << to)))
			to--;
		      range[j].to = to;
		    }
		  else
		    to = range[j].to;
		  if (from > to)
		    return -1;
		}
	    }
988 989 990
	}
    }

Paul Eggert's avatar
Paul Eggert committed
991
  /* Decide all fields from restrictions in RANGE.  */
992 993 994 995 996 997 998
  for (i = j = 0; i < n ; i++)
    {
      if (j < range[i].from)
	{
	  if (i == 0 || ! NILP (tmp[i - 1]))
	    /* None of TMP[X] corresponds to Jth field.  */
	    return -1;
999
	  memclear (field + j, (range[i].from - j) * word_size);
1000
	  j = range[i].from;
1001 1002 1003 1004 1005
	}
      field[j++] = tmp[i];
    }
  if (! NILP (tmp[n - 1]) && j < XLFD_REGISTRY_INDEX)
    return -1;
1006
  memclear (field + j, (XLFD_LAST_INDEX - j) * word_size);
1007
  if (FIXNUMP (field[XLFD_ENCODING_INDEX]))
1008 1009 1010 1011 1012
    field[XLFD_ENCODING_INDEX]
      = Fintern (Fnumber_to_string (field[XLFD_ENCODING_INDEX]), Qnil);
  return 0;
}

1013

1014
/* Parse NAME (NUL terminated) as XLFD and store information in FONT
1015 1016 1017 1018 1019 1020 1021 1022
   (font-spec or font-entity).  Size property of FONT is set as
   follows:
	specified XLFD fields		FONT property
	---------------------		-------------
	PIXEL_SIZE			PIXEL_SIZE (Lisp integer)
	POINT_SIZE and RESY		calculated pixel size (Lisp integer)
	POINT_SIZE			POINT_SIZE/10 (Lisp float)

1023
   If NAME is successfully parsed, return 0.  Otherwise return -1.
1024

1025 1026
   FONT is usually a font-spec, but when this function is called from
   X font backend driver, it is a font-entity.  In that case, NAME is
Kenichi Handa's avatar
Kenichi Handa committed
1027
   a fully specified XLFD.  */
Kenichi Handa's avatar
Kenichi Handa committed
1028 1029

int
1030
font_parse_xlfd (char *name, ptrdiff_t len, Lisp_Object font)
Kenichi Handa's avatar
Kenichi Handa committed
1031
{
Kenichi Handa's avatar
Kenichi Handa committed
1032
  int i, j, n;
1033
  char *f[XLFD_LAST_INDEX + 1];
Kenichi Handa's avatar
Kenichi Handa committed
1034
  Lisp_Object val;
1035
  char *p;
Kenichi Handa's avatar
Kenichi Handa committed
1036

1037
  if (len > 255 || !len)
Kenichi Handa's avatar
Kenichi Handa committed
1038 1039
    /* Maximum XLFD name length is 255. */
    return -1;
1040
  /* Accept "*-.." as a fully specified XLFD. */
1041
  if (name[0] == '*' && (len == 1 || name[1] == '-'))
1042 1043 1044 1045
    i = 1, f[XLFD_FOUNDRY_INDEX] = name;
  else
    i = 0;
  for (p = name + i; *p; p++)
Kenichi Handa's avatar
Kenichi Handa committed
1046 1047 1048 1049 1050 1051 1052
    if (*p == '-')
      {
	f[i++] = p + 1;
	if (i == XLFD_LAST_INDEX)
	  break;
      }
  f[i] = name + len;
Kenichi Handa's avatar
Kenichi Handa committed
1053

1054 1055
#define INTERN_FIELD(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 0)
#define INTERN_FIELD_SYM(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 1)
1056

1057
  if (i == XLFD_LAST_INDEX)
1058
    {
Kenichi Handa's avatar
Kenichi Handa committed
1059
      /* Fully specified XLFD.  */
1060 1061
      int pixel_size;

1062 1063
      ASET (font, FONT_FOUNDRY_INDEX, INTERN_FIELD_SYM (XLFD_FOUNDRY_INDEX));
      ASET (font, FONT_FAMILY_INDEX, INTERN_FIELD_SYM (XLFD_FAMILY_INDEX));
Kenichi Handa's avatar
Kenichi Handa committed
1064 1065
      for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
	   i <= XLFD_SWIDTH_INDEX; i++, j++)
1066
	{
1067
	  val = INTERN_FIELD_SYM (i);
1068
	  if (! NILP (val))
1069
	    {
1070
	      if ((n = font_style_to_value (j, INTERN_FIELD_SYM (i), 0)) < 0)
Kenichi Handa's avatar
Kenichi Handa committed
1071
		return -1;
1072
	      ASET (font, j, make_fixnum (n));
1073 1074
	    }
	}
1075
      ASET (font, FONT_ADSTYLE_INDEX, INTERN_FIELD_SYM (XLFD_ADSTYLE_INDEX));
Kenichi Handa's avatar
Kenichi Handa committed
1076 1077 1078 1079 1080
      if (strcmp (f[XLFD_REGISTRY_INDEX], "*-*") == 0)
	ASET (font, FONT_REGISTRY_INDEX, Qnil);
      else
	ASET (font, FONT_REGISTRY_INDEX,
	      font_intern_prop (f[XLFD_REGISTRY_INDEX],
1081 1082
				f[XLFD_LAST_INDEX] - f[XLFD_REGISTRY_INDEX],
				1));
1083 1084
      p = f[XLFD_PIXEL_INDEX];
      if (*p == '[' && (pixel_size = parse_matrix (p)) >= 0)
1085
	ASET (font, FONT_SIZE_INDEX, make_fixnum (pixel_size));
1086 1087
      else
	{
Kenichi Handa's avatar
Kenichi Handa committed
1088
	  val = INTERN_FIELD (XLFD_PIXEL_INDEX);
1089
	  if (FIXNUMP (val))
1090
	    ASET (font, FONT_SIZE_INDEX, val);
1091 1092
	  else if (FONT_ENTITY_P (font))
	    return -1;
1093 1094 1095 1096
	  else
	    {
	      double point_size