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

3 4 5 6
Copyright (C) 2006-2011  Free Software Foundation, Inc.
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 24 25

#include <config.h>
#include <stdio.h>
#include <ctype.h>
26
#include <setjmp.h>
Kenichi Handa's avatar
Kenichi Handa committed
27 28 29 30

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

Kenichi Handa's avatar
Kenichi Handa committed
39 40 41 42 43 44 45 46
#ifdef HAVE_X_WINDOWS
#include "xterm.h"
#endif /* HAVE_X_WINDOWS */

#ifdef HAVE_NTGUI
#include "w32term.h"
#endif /* HAVE_NTGUI */

47 48 49 50
#ifdef HAVE_NS
#include "nsterm.h"
#endif /* HAVE_NS */

51
Lisp_Object Qopentype;
Kenichi Handa's avatar
Kenichi Handa committed
52

Kenichi Handa's avatar
Kenichi Handa committed
53
/* Important character set strings.  */
Kenichi Handa's avatar
Kenichi Handa committed
54
Lisp_Object Qascii_0, Qiso8859_1, Qiso10646_1, Qunicode_bmp, Qunicode_sip;
55

56 57
#define DEFAULT_ENCODING Qiso8859_1

Kenichi Handa's avatar
Kenichi Handa committed
58 59 60
/* Unicode category `Cf'.  */
static Lisp_Object QCf;

Kenichi Handa's avatar
Kenichi Handa committed
61 62
/* Special vector of zero length.  This is repeatedly used by (struct
   font_driver *)->list when a specified font is not found. */
Kenichi Handa's avatar
Kenichi Handa committed
63
static Lisp_Object null_vector;
Kenichi Handa's avatar
Kenichi Handa committed
64

65
/* Vector of Vfont_weight_table, Vfont_slant_table, and Vfont_width_table. */
Kenichi Handa's avatar
Kenichi Handa committed
66 67
static Lisp_Object font_style_table;

68 69 70 71 72 73 74 75
/* 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.  */
76
  const char *names[5];
77 78 79 80 81
};

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

82
static const struct table_entry weight_table[] =
83 84 85 86 87 88
{
  { 0, { "thin" }},
  { 20, { "ultra-light", "ultralight" }},
  { 40, { "extra-light", "extralight" }},
  { 50, { "light" }},
  { 75, { "semi-light", "semilight", "demilight", "book" }},
89
  { 100, { "normal", "medium", "regular", "unspecified" }},
90 91 92 93 94 95 96 97 98
  { 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.  */

99
static const struct table_entry slant_table[] =
100 101 102
{
  { 0, { "reverse-oblique", "ro" }},
  { 10, { "reverse-italic", "ri" }},
103
  { 100, { "normal", "r", "unspecified" }},
104 105 106 107 108 109 110
  { 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.  */

111
static const struct table_entry width_table[] =
112 113 114 115 116
{
  { 50, { "ultra-condensed", "ultracondensed" }},
  { 63, { "extra-condensed", "extracondensed" }},
  { 75, { "condensed", "compressed", "narrow" }},
  { 87, { "semi-condensed", "semicondensed", "demicondensed" }},
117
  { 100, { "normal", "medium", "regular", "unspecified" }},
118 119 120 121 122 123
  { 113, { "semi-expanded", "semiexpanded", "demiexpanded" }},
  { 125, { "expanded" }},
  { 150, { "extra-expanded", "extraexpanded" }},
  { 200, { "ultra-expanded", "ultraexpanded", "wide" }}
};

Andreas Schwab's avatar
Andreas Schwab committed
124 125
Lisp_Object QCfoundry;
static Lisp_Object QCadstyle, QCregistry;
Kenichi Handa's avatar
Kenichi Handa committed
126
/* Symbols representing keys of font extra info.  */
Kenichi Handa's avatar
Kenichi Handa committed
127
Lisp_Object QCspacing, QCdpi, QCscalable, QCotf, QClang, QCscript, QCavgwidth;
128 129
Lisp_Object QCantialias, QCfont_entity;
static Lisp_Object QCfc_unknown_spec;
130
/* Symbols representing values of font spacing property.  */
131 132
static Lisp_Object Qc, Qm, Qd;
Lisp_Object Qp;
133 134 135
/* 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
136

137
static Lisp_Object QCuser_spec;
138

139
/* Alist of font registry symbols and the corresponding charset
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
   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;

157 158
/* 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
159 160 161
   here.  */
static struct font_driver_list *font_driver_list;

Kenichi Handa's avatar
Kenichi Handa committed
162 163 164 165


/* Creaters of font-related Lisp object.  */

Andreas Schwab's avatar
Andreas Schwab committed
166
static Lisp_Object
167
font_make_spec (void)
Kenichi Handa's avatar
Kenichi Handa committed
168 169 170 171 172 173 174 175 176 177 178
{
  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
179
font_make_entity (void)
Kenichi Handa's avatar
Kenichi Handa committed
180 181 182 183 184 185 186 187 188 189
{
  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;
}

190 191 192
/* 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
193
Lisp_Object
194
font_make_object (int size, Lisp_Object entity, int pixelsize)
Kenichi Handa's avatar
Kenichi Handa committed
195 196 197 198
{
  Lisp_Object font_object;
  struct font *font
    = (struct font *) allocate_pseudovector (size, FONT_OBJECT_MAX, PVEC_FONT);
199 200
  int i;

Kenichi Handa's avatar
Kenichi Handa committed
201 202
  XSETFONT (font_object, font);

203 204 205 206 207 208
  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]
209
	  = Fcopy_alist (AREF (entity, FONT_EXTRA_INDEX));
210 211 212
    }
  if (size > 0)
    font->props[FONT_SIZE_INDEX] = make_number (pixelsize);
Kenichi Handa's avatar
Kenichi Handa committed
213 214 215 216 217
  return font_object;
}



218 219 220 221
static int font_pixel_size (FRAME_PTR f, Lisp_Object);
static Lisp_Object font_open_entity (FRAME_PTR, Lisp_Object, int);
static Lisp_Object font_matching_entity (FRAME_PTR, Lisp_Object *,
                                         Lisp_Object);
Andreas Schwab's avatar
Andreas Schwab committed
222
static unsigned font_encode_char (Lisp_Object, int);
Kenichi Handa's avatar
Kenichi Handa committed
223 224 225 226

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

Kenichi Handa's avatar
Kenichi Handa committed
227 228

/* Return a Lispy value of a font property value at STR and LEN bytes.
229 230 231
   If STR is "*", return nil.
   If FORCE_SYMBOL is zero and all characters in STR are digits,
   return an integer.  Otherwise, return a symbol interned from
232
   STR.  */
Kenichi Handa's avatar
Kenichi Handa committed
233 234

Lisp_Object
235
font_intern_prop (const char *str, ptrdiff_t len, int force_symbol)
Kenichi Handa's avatar
Kenichi Handa committed
236
{
237
  ptrdiff_t i;
238
  Lisp_Object tem;
Kenichi Handa's avatar
Kenichi Handa committed
239
  Lisp_Object obarray;
240
  EMACS_INT nbytes, nchars;
Kenichi Handa's avatar
Kenichi Handa committed
241 242 243

  if (len == 1 && *str == '*')
    return Qnil;
244
  if (!force_symbol && len >=1 && isdigit (*str))
Kenichi Handa's avatar
Kenichi Handa committed
245 246 247 248 249
    {
      for (i = 1; i < len; i++)
	if (! isdigit (str[i]))
	  break;
      if (i == len)
250 251 252 253 254 255
	{
	  Lisp_Object num = string_to_number (str, 10, 0);
	  if (! INTEGERP (num))
	    xsignal1 (Qoverflow_error, num);
	  return num;
	}
Kenichi Handa's avatar
Kenichi Handa committed
256 257
    }

258
  /* The following code is copied from the function intern (in
259
     lread.c), and modified to suit our purpose.  */
Kenichi Handa's avatar
Kenichi Handa committed
260
  obarray = Vobarray;
261
  if (!VECTORP (obarray) || ASIZE (obarray) == 0)
Kenichi Handa's avatar
Kenichi Handa committed
262
    obarray = check_obarray (obarray);
263
  parse_str_as_multibyte ((unsigned char *) str, len, &nchars, &nbytes);
264 265 266 267 268 269
  if (len == nchars || len != nbytes)
    /* CONTENTS contains no multibyte sequences or contains an invalid
       multibyte sequence.  We'll make a unibyte string.  */
    tem = oblookup (obarray, str, len, len);
  else
    tem = oblookup (obarray, str, nchars, len);
Kenichi Handa's avatar
Kenichi Handa committed
270 271
  if (SYMBOLP (tem))
    return tem;
272 273 274 275 276
  if (len == nchars || len != nbytes)
    tem = make_unibyte_string (str, len);
  else
    tem = make_multibyte_string (str, nchars, len);
  return Fintern (tem, obarray);
Kenichi Handa's avatar
Kenichi Handa committed
277 278
}

279
/* Return a pixel size of font-spec SPEC on frame F.  */
280

281
static int
282
font_pixel_size (FRAME_PTR f, Lisp_Object spec)
283
{
284
#ifdef HAVE_WINDOW_SYSTEM
285 286
  Lisp_Object size = AREF (spec, FONT_SIZE_INDEX);
  double point_size;
Kenichi Handa's avatar
Kenichi Handa committed
287
  int dpi, pixel_size;
288
  Lisp_Object val;
289

290 291 292
  if (INTEGERP (size))
    return XINT (size);
  if (NILP (size))
293
    return 0;
294
  font_assert (FLOATP (size));
295
  point_size = XFLOAT_DATA (size);
Kenichi Handa's avatar
Kenichi Handa committed
296 297
  val = AREF (spec, FONT_DPI_INDEX);
  if (INTEGERP (val))
298
    dpi = XINT (val);
299 300 301 302
  else
    dpi = f->resy;
  pixel_size = POINT_TO_PIXEL (point_size, dpi);
  return pixel_size;
303 304 305
#else
  return 1;
#endif
306 307
}

Kenichi Handa's avatar
Kenichi Handa committed
308

Kenichi Handa's avatar
Kenichi Handa committed
309 310 311 312
/* 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
313
   font_style_table if VAL is a symbol, and return the closest index if
Kenichi Handa's avatar
Kenichi Handa committed
314
   VAL is an integer.  */
Kenichi Handa's avatar
Kenichi Handa committed
315

Kenichi Handa's avatar
Kenichi Handa committed
316
int
317
font_style_to_value (enum font_property_index prop, Lisp_Object val, int noerror)
Kenichi Handa's avatar
Kenichi Handa committed
318
{
Kenichi Handa's avatar
Kenichi Handa committed
319 320
  Lisp_Object table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
  int len = ASIZE (table);
Kenichi Handa's avatar
Kenichi Handa committed
321

Kenichi Handa's avatar
Kenichi Handa committed
322
  if (SYMBOLP (val))
Kenichi Handa's avatar
Kenichi Handa committed
323
    {
324
      int i, j;
325
      char *s;
Kenichi Handa's avatar
Kenichi Handa committed
326 327 328 329
      Lisp_Object args[2], elt;

      /* At first try exact match.  */
      for (i = 0; i < len; i++)
330 331 332 333
	for (j = 1; j < ASIZE (AREF (table, i)); j++)
	  if (EQ (val, AREF (AREF (table, i), j)))
	    return ((XINT (AREF (AREF (table, i), 0)) << 8)
		    | (i << 4) | (j - 1));
Kenichi Handa's avatar
Kenichi Handa committed
334
      /* Try also with case-folding match.  */
335
      s = SSDATA (SYMBOL_NAME (val));
Kenichi Handa's avatar
Kenichi Handa committed
336
      for (i = 0; i < len; i++)
337 338 339
	for (j = 1; j < ASIZE (AREF (table, i)); j++)
	  {
	    elt = AREF (AREF (table, i), j);
340
	    if (xstrcasecmp (s, SSDATA (SYMBOL_NAME (elt))) == 0)
341 342 343
	      return ((XINT (AREF (AREF (table, i), 0)) << 8)
		      | (i << 4) | (j - 1));
	  }
Kenichi Handa's avatar
Kenichi Handa committed
344 345 346 347
      if (! noerror)
	return -1;
      if (len == 255)
	abort ();
348
      elt = Fmake_vector (make_number (2), make_number (100));
349
      ASET (elt, 1, val);
Kenichi Handa's avatar
Kenichi Handa committed
350
      args[0] = table;
351
      args[1] = Fmake_vector (make_number (1), elt);
Kenichi Handa's avatar
Kenichi Handa committed
352
      ASET (font_style_table, prop - FONT_WEIGHT_INDEX, Fvconcat (2, args));
353
      return (100 << 8) | (i << 4);
Kenichi Handa's avatar
Kenichi Handa committed
354
    }
Kenichi Handa's avatar
Kenichi Handa committed
355 356
  else
    {
357
      int i, last_n;
Kenichi Handa's avatar
Kenichi Handa committed
358
      int numeric = XINT (val);
Kenichi Handa's avatar
Kenichi Handa committed
359

360
      for (i = 0, last_n = -1; i < len; i++)
Kenichi Handa's avatar
Kenichi Handa committed
361
	{
362
	  int n = XINT (AREF (AREF (table, i), 0));
Kenichi Handa's avatar
Kenichi Handa committed
363

Kenichi Handa's avatar
Kenichi Handa committed
364
	  if (numeric == n)
365
	    return (n << 8) | (i << 4);
Kenichi Handa's avatar
Kenichi Handa committed
366 367 368 369
	  if (numeric < n)
	    {
	      if (! noerror)
		return -1;
370 371
	      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
372 373 374 375 376
	    }
	  last_n = n;
	}
      if (! noerror)
	return -1;
377
      return ((last_n << 8) | ((i - 1) << 4));
Kenichi Handa's avatar
Kenichi Handa committed
378 379
    }
}
Kenichi Handa's avatar
Kenichi Handa committed
380 381

Lisp_Object
382
font_style_symbolic (Lisp_Object font, enum font_property_index prop, int for_face)
Kenichi Handa's avatar
Kenichi Handa committed
383
{
384
  Lisp_Object val = AREF (font, prop);
385 386
  Lisp_Object table, elt;
  int i;
Kenichi Handa's avatar
Kenichi Handa committed
387

Kenichi Handa's avatar
Kenichi Handa committed
388 389 390
  if (NILP (val))
    return Qnil;
  table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
391 392 393 394
  i = XINT (val) & 0xFF;
  font_assert (((i >> 4) & 0xF) < ASIZE (table));
  elt = AREF (table, ((i >> 4) & 0xF));
  font_assert ((i & 0xF) + 1 < ASIZE (elt));
395
  return (for_face ? AREF (elt, 1) : AREF (elt, (i & 0xF) + 1));
396 397
}

398 399 400 401 402
/* 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
403
find_font_encoding (Lisp_Object fontname)
404 405 406 407 408 409 410 411 412 413 414 415 416 417
{
  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));
    }
418
  return Qnil;
419 420
}

421 422 423 424 425
/* 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
426
font_registry_charsets (Lisp_Object registry, struct charset **encoding, struct charset **repertory)
427 428 429 430
{
  Lisp_Object val;
  int encoding_id, repertory_id;

Kenichi Handa's avatar
Kenichi Handa committed
431
  val = Fassoc_string (registry, font_charset_alist, Qt);
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
  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)));
	    }
460
	}
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
      else
	goto invalid_entry;
      val = Fcons (make_number (encoding_id), make_number (repertory_id));
      font_charset_alist
	= nconc2 (font_charset_alist, Fcons (Fcons (registry, val), Qnil));
    }

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

Kenichi Handa's avatar
Kenichi Handa committed
480

481
/* Font property value validators.  See the comment of
482 483
   font_property_table for the meaning of the arguments.  */

484 485 486 487 488 489
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
490 491

static Lisp_Object
492
font_prop_validate_symbol (Lisp_Object prop, Lisp_Object val)
Kenichi Handa's avatar
Kenichi Handa committed
493 494
{
  if (STRINGP (val))
Kenichi Handa's avatar
Kenichi Handa committed
495 496
    val = Fintern (val, Qnil);
  if (! SYMBOLP (val))
Kenichi Handa's avatar
Kenichi Handa committed
497
    val = Qerror;
Kenichi Handa's avatar
Kenichi Handa committed
498 499
  else if (EQ (prop, QCregistry))
    val = Fintern (Fdowncase (SYMBOL_NAME (val)), Qnil);
Kenichi Handa's avatar
Kenichi Handa committed
500 501 502
  return val;
}

Kenichi Handa's avatar
Kenichi Handa committed
503

Kenichi Handa's avatar
Kenichi Handa committed
504
static Lisp_Object
505
font_prop_validate_style (Lisp_Object style, Lisp_Object val)
Kenichi Handa's avatar
Kenichi Handa committed
506
{
Kenichi Handa's avatar
Kenichi Handa committed
507 508 509 510
  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
511
    {
512
      int n = XINT (val);
513
      if (((n >> 4) & 0xF)
Kenichi Handa's avatar
Kenichi Handa committed
514
	  >= ASIZE (AREF (font_style_table, prop - FONT_WEIGHT_INDEX)))
Kenichi Handa's avatar
Kenichi Handa committed
515 516 517
	val = Qerror;
      else
	{
518 519 520 521 522
	  Lisp_Object elt = AREF (AREF (font_style_table, prop - FONT_WEIGHT_INDEX), (n >> 4) & 0xF);

	  if ((n & 0xF) + 1 >= ASIZE (elt))
	    val = Qerror;
	  else if (XINT (AREF (elt, 0)) != (n >> 8))
Kenichi Handa's avatar
Kenichi Handa committed
523 524 525
	    val = Qerror;
	}
    }
Kenichi Handa's avatar
Kenichi Handa committed
526 527 528 529 530 531 532 533
  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
534 535 536 537
  return val;
}

static Lisp_Object
538
font_prop_validate_non_neg (Lisp_Object prop, Lisp_Object val)
Kenichi Handa's avatar
Kenichi Handa committed
539 540 541 542 543 544
{
  return (NATNUMP (val) || (FLOATP (val) && XFLOAT_DATA (val) >= 0)
	  ? val : Qerror);
}

static Lisp_Object
545
font_prop_validate_spacing (Lisp_Object prop, Lisp_Object val)
546 547 548
{
  if (NILP (val) || (NATNUMP (val) && XINT (val) <= FONT_SPACING_CHARCELL))
    return val;
549 550 551 552 553 554 555 556
  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);
557
      if (spacing == 'p' || spacing == 'P')
558 559 560 561
	return make_number (FONT_SPACING_PROPORTIONAL);
      if (spacing == 'd' || spacing == 'D')
	return make_number (FONT_SPACING_DUAL);
    }
562 563 564
  return Qerror;
}

565
static Lisp_Object
566
font_prop_validate_otf (Lisp_Object prop, Lisp_Object val)
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
{
  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;
}

599
/* Structure of known font property keys and validator of the
600
   values.  */
601
static const struct
Kenichi Handa's avatar
Kenichi Handa committed
602
{
603
  /* Pointer to the key symbol.  */
Kenichi Handa's avatar
Kenichi Handa committed
604
  Lisp_Object *key;
605 606 607
  /* 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.  */
608
  Lisp_Object (*validater) (Lisp_Object prop, Lisp_Object val);
609 610
} font_property_table[] =
  { { &QCtype, font_prop_validate_symbol },
Kenichi Handa's avatar
Kenichi Handa committed
611 612 613 614 615 616 617
    { &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 },
618 619 620
    { &QCsize, font_prop_validate_non_neg },
    { &QCdpi, font_prop_validate_non_neg },
    { &QCspacing, font_prop_validate_spacing },
Kenichi Handa's avatar
Kenichi Handa committed
621 622 623 624 625 626
    { &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
627 628
  };

629
/* Size (number of elements) of the above table.  */
630 631 632
#define FONT_PROPERTY_TABLE_SIZE \
  ((sizeof font_property_table) / (sizeof *font_property_table))

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

636
static int
637
get_font_prop_index (Lisp_Object key)
Kenichi Handa's avatar
Kenichi Handa committed
638
{
Kenichi Handa's avatar
Kenichi Handa committed
639 640 641 642 643
  int i;

  for (i = 0; i < FONT_PROPERTY_TABLE_SIZE; i++)
    if (EQ (key, *font_property_table[i].key))
      return i;
644
  return -1;
Kenichi Handa's avatar
Kenichi Handa committed
645 646
}

Kenichi Handa's avatar
Kenichi Handa committed
647 648 649
/* 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.  */
650

Kenichi Handa's avatar
Kenichi Handa committed
651
static Lisp_Object
652
font_prop_validate (int idx, Lisp_Object prop, Lisp_Object val)
Kenichi Handa's avatar
Kenichi Handa committed
653
{
Kenichi Handa's avatar
Kenichi Handa committed
654
  Lisp_Object validated;
Kenichi Handa's avatar
Kenichi Handa committed
655

656 657
  if (NILP (val))
    return val;
Kenichi Handa's avatar
Kenichi Handa committed
658 659 660
  if (NILP (prop))
    prop = *font_property_table[idx].key;
  else
661
    {
Kenichi Handa's avatar
Kenichi Handa committed
662 663 664
      idx = get_font_prop_index (prop);
      if (idx < 0)
	return val;
665
    }
Kenichi Handa's avatar
Kenichi Handa committed
666 667 668 669
  validated = (font_property_table[idx].validater) (prop, val);
  if (EQ (validated, Qerror))
    signal_error ("invalid font property", Fcons (prop, val));
  return validated;
Kenichi Handa's avatar
Kenichi Handa committed
670
}
671

Kenichi Handa's avatar
Kenichi Handa committed
672 673 674

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

676
Lisp_Object
677
font_put_extra (Lisp_Object font, Lisp_Object prop, Lisp_Object val)
678 679
{
  Lisp_Object extra = AREF (font, FONT_EXTRA_INDEX);
680
  Lisp_Object slot = (NILP (extra) ? Qnil : assq_no_quit (prop, extra));
681 682 683

  if (NILP (slot))
    {
Kenichi Handa's avatar
Kenichi Handa committed
684 685 686 687 688
      Lisp_Object prev = Qnil;

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

690
      if (NILP (prev))
691
        ASET (font, FONT_EXTRA_INDEX, Fcons (Fcons (prop, val), extra));
692
      else
693
        XSETCDR (prev, Fcons (Fcons (prop, val), extra));
694

695
      return val;
696 697
    }
  XSETCDR (slot, val);
698 699
  if (NILP (val))
    ASET (font, FONT_EXTRA_INDEX, Fdelq (slot, extra));
700
  return val;
701 702
}

Kenichi Handa's avatar
Kenichi Handa committed
703 704 705

/* Font name parser and unparser */

706
static int parse_matrix (const char *);
707
static int font_expand_wildcards (Lisp_Object *, int);
Dan Nicolaescu's avatar
Dan Nicolaescu committed
708
static int font_parse_name (char *, Lisp_Object);
Kenichi Handa's avatar
Kenichi Handa committed
709

710
/* An enumerator for each field of an XLFD font name.  */
Kenichi Handa's avatar
Kenichi Handa committed
711 712 713 714 715 716 717 718
enum xlfd_field_index
{
  XLFD_FOUNDRY_INDEX,
  XLFD_FAMILY_INDEX,
  XLFD_WEIGHT_INDEX,
  XLFD_SLANT_INDEX,
  XLFD_SWIDTH_INDEX,
  XLFD_ADSTYLE_INDEX,
719 720
  XLFD_PIXEL_INDEX,
  XLFD_POINT_INDEX,
Kenichi Handa's avatar
Kenichi Handa committed
721 722 723 724 725 726 727 728 729
  XLFD_RESX_INDEX,
  XLFD_RESY_INDEX,
  XLFD_SPACING_INDEX,
  XLFD_AVGWIDTH_INDEX,
  XLFD_REGISTRY_INDEX,
  XLFD_ENCODING_INDEX,
  XLFD_LAST_INDEX
};

730
/* An enumerator for mask bit corresponding to each XLFD field.  */
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
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
};


750
/* Parse P pointing to the pixel/point size field of the form
Kenichi Handa's avatar
Kenichi Handa committed
751 752 753 754 755 756 757 758 759 760 761 762 763 764
   `[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
765
parse_matrix (const char *p)
Kenichi Handa's avatar
Kenichi Handa committed
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
{
  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);
}

782
/* Expand a wildcard field in FIELD (the first N fields are filled) to
783
   multiple fields to fill in all 14 XLFD fields while restricting a
784 785
   field position by its contents.  */

786
static int
787
font_expand_wildcards (Lisp_Object *field, int n)
788 789 790 791 792 793 794 795 796 797 798 799 800 801
{
  /* 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;
    /* Maxinum possible field.  */
    int to;
    /* Bit mask of possible field.  Nth bit corresponds to Nth field.  */
    int mask;
  } range[XLFD_LAST_INDEX];
  int i, j;
802
  int range_from, range_to;
803 804 805 806 807 808
  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 \
809
			    | XLFD_AVGWIDTH_MASK)
810 811 812 813 814 815 816 817
#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;

818 819 820 821
  /* The triplet RANGE_FROM, RANGE_TO, and RANGE_MASK is a
     position-based retriction for FIELD[I].  */
  for (i = 0, range_from = 0, range_to = 14 - n; i < n;
       i++, range_from++, range_to++, range_mask <<= 1)
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
    {
      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
	     retriction for FIELD[I].  */
	  int from, to;
	  unsigned mask;

	  if (INTEGERP (val))
	    {
	      int numeric = XINT (val);

844 845 846
	      if (i + 1 == n)
		from = to = XLFD_ENCODING_INDEX,
		  mask = XLFD_ENCODING_MASK;
847 848 849
	      else if (numeric == 0)
		from = XLFD_PIXEL_INDEX, to = XLFD_AVGWIDTH_INDEX,
		  mask = XLFD_PIXEL_MASK | XLFD_LARGENUM_MASK;
850 851 852
	      else if (numeric <= 48)
		from = to = XLFD_PIXEL_INDEX,
		  mask = XLFD_PIXEL_MASK;
853
	      else
854
		from = XLFD_POINT_INDEX, to = XLFD_AVGWIDTH_INDEX,
855 856
		  mask = XLFD_LARGENUM_MASK;
	    }
Kenichi Handa's avatar
Kenichi Handa committed
857
	  else if (SBYTES (SYMBOL_NAME (val)) == 0)
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
	    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;
	    }
873 874
	  else if (range_from <= XLFD_WEIGHT_INDEX
		   && range_to >= XLFD_WEIGHT_INDEX
Kenichi Handa's avatar
Kenichi Handa committed
875
		   && FONT_WEIGHT_NAME_NUMERIC (val) >= 0)
876
	    from = to = XLFD_WEIGHT_INDEX, mask = XLFD_WEIGHT_MASK;
877 878
	  else if (range_from <= XLFD_SLANT_INDEX
		   && range_to >= XLFD_SLANT_INDEX
Kenichi Handa's avatar
Kenichi Handa committed
879
		   && FONT_SLANT_NAME_NUMERIC (val) >= 0)
880
	    from = to = XLFD_SLANT_INDEX, mask = XLFD_SLANT_MASK;
881 882
	  else if (range_from <= XLFD_SWIDTH_INDEX
		   && range_to >= XLFD_SWIDTH_INDEX
Kenichi Handa's avatar
Kenichi Handa committed
883
		   && FONT_WIDTH_NAME_NUMERIC (val) >= 0)
884 885 886
	    from = to = XLFD_SWIDTH_INDEX, mask = XLFD_SWIDTH_MASK;
	  else
	    {
887
	      if (EQ (val, Qc) || EQ (val, Qm) || EQ (val, Qp) || EQ (val, Qd))
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
		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)
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
	    {
	      /* 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;
		}
	    }
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
	}
    }

  /* Decide all fileds from restrictions in RANGE.  */
  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;
	  for (; j < range[i].from; j++)
	    field[j] = Qnil;
	}
      field[j++] = tmp[i];
    }
  if (! NILP (tmp[n - 1]) && j < XLFD_REGISTRY_INDEX)
    return -1;
  for (; j < XLFD_LAST_INDEX; j++)
    field[j] = Qnil;
  if (INTEGERP (field[XLFD_ENCODING_INDEX]))
    field[XLFD_ENCODING_INDEX]
      = Fintern (Fnumber_to_string (field[XLFD_ENCODING_INDEX]), Qnil);
  return 0;
}

971

972
/* Parse NAME (null terminated) as XLFD and store information in FONT
973 974 975 976 977 978 979 980
   (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)

981
   If NAME is successfully parsed, return 0.  Otherwise return -1.
982

983 984
   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
985
   a fully specified XLFD.  */
Kenichi Handa's avatar
Kenichi Handa committed
986 987

int
Dan Nicolaescu's avatar
Dan Nicolaescu committed
988
font_parse_xlfd (char *name, Lisp_Object font)
Kenichi Handa's avatar
Kenichi Handa committed
989
{
990
  ptrdiff_t len = strlen (name);
Kenichi Handa's avatar
Kenichi Handa committed
991
  int i, j, n;
992
  char *f[XLFD_LAST_INDEX + 1];
Kenichi Handa's avatar
Kenichi Handa committed
993
  Lisp_Object val;
994
  char *p;
Kenichi Handa's avatar
Kenichi Handa committed
995

996
  if (len > 255 || !len)
Kenichi Handa's avatar
Kenichi Handa committed
997 998
    /* Maximum XLFD name length is 255. */
    return -1;
999
  /* Accept "*-.." as a fully specified XLFD. */
1000
  if (name[0] == '*' && (len == 1 || name[1] == '-'))
1001 1002 1003 1004
    i = 1, f[XLFD_FOUNDRY_INDEX] = name;
  else
    i = 0;
  for (p = name + i; *p; p++)
Kenichi Handa's avatar
Kenichi Handa committed
1005 1006 1007 1008 1009 1010 1011
    if (*p == '-')
      {
	f[i++] = p + 1;
	if (i == XLFD_LAST_INDEX)
	  break;
      }
  f[i] = name + len;
Kenichi Handa's avatar
Kenichi Handa committed
1012

1013 1014
#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)
Kenichi Handa's avatar