font.c 104 KB
Newer Older
Kenichi Handa's avatar
Kenichi Handa committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/* font.c -- "Font" primitives.
   Copyright (C) 2006 Free Software Foundation, Inc.
   Copyright (C) 2006
     National Institute of Advanced Industrial Science and Technology (AIST)
     Registration Number H13PRO009

This file is part of GNU Emacs.

GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

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

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

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

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

40
#ifndef FONT_DEBUG
Kenichi Handa's avatar
Kenichi Handa committed
41
#define FONT_DEBUG
42
#endif
Kenichi Handa's avatar
Kenichi Handa committed
43 44 45 46 47 48 49 50 51 52 53 54

#ifdef FONT_DEBUG
#undef xassert
#define xassert(X)	do {if (!(X)) abort ();} while (0)
#else
#define xassert(X)	(void) 0
#endif

int enable_font_backend;

Lisp_Object Qfontp;

Kenichi Handa's avatar
Kenichi Handa committed
55 56
Lisp_Object Qopentype;

57
/* Important character set symbols.  */
Kenichi Handa's avatar
Kenichi Handa committed
58
Lisp_Object Qiso8859_1, Qiso10646_1, Qunicode_bmp, Qunicode_sip;
59

Kenichi Handa's avatar
Kenichi Handa committed
60 61 62 63 64 65 66 67 68 69 70 71
/* Like CHECK_FONT_SPEC but also validate properties of the font-spec,
   and set X to the validated result.  */

#define CHECK_VALIDATE_FONT_SPEC(x)				\
  do {								\
    if (! FONT_SPEC_P (x)) x = wrong_type_argument (Qfont, x);	\
    x = font_prop_validate (x);					\
  } while (0)

/* Number of pt per inch (from the TeXbook).  */
#define PT_PER_INCH 72.27

72
/* Return a pixel size (integer) corresponding to POINT size (double)
73 74
   on resolution DPI.  */
#define POINT_TO_PIXEL(POINT, DPI) ((POINT) * (DPI) / PT_PER_INCH + 0.5)
Kenichi Handa's avatar
Kenichi Handa committed
75

76
/* Return a point size (double) corresponding to POINT size (integer)
77 78
   on resolution DPI.  */
#define PIXEL_TO_POINT(PIXEL, DPI) ((PIXEL) * PT_PER_INCH * 10 / (DPI) + 0.5)
Kenichi Handa's avatar
Kenichi Handa committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

/* Special string of zero length.  It is used to specify a NULL name
   in a font properties (e.g. adstyle).  We don't use the symbol of
   NULL name because it's confusing (Lisp printer prints nothing for
   it). */
Lisp_Object null_string;

/* Special vector of zero length.  This is repeatedly used by (struct
   font_driver *)->list when a specified font is not found. */
Lisp_Object null_vector;

/* Vector of 3 elements.  Each element is an alist for one of font
   style properties (weight, slant, width).  The alist contains a
   mapping between symbolic property values (e.g. `medium' for weight)
   and numeric property values (e.g. 100).  So, it looks like this:
	[((thin . 0) ... (heavy . 210))
	 ((ro . 0) ... (ot . 210))
	 ((ultracondensed . 50) ... (wide . 200))]  */
static Lisp_Object font_style_table;

/* Alist of font family vs the corresponding aliases.
   Each element has this form:
	(FAMILY ALIAS1 ALIAS2 ...)   */

static Lisp_Object font_family_alist;

/* Symbols representing keys of normal font properties.  */
extern Lisp_Object QCtype, QCfamily, QCweight, QCslant, QCwidth, QCsize, QCname;
Lisp_Object QCfoundry, QCadstyle, QCregistry, QCextra;
/* Symbols representing keys of font extra info.  */
109 110 111
Lisp_Object QCspacing, QCdpi, QCscalable, QCotf, QClanguage, QCscript;
/* Symbols representing values of font spacing property.  */
Lisp_Object Qc, Qm, Qp, Qd;
Kenichi Handa's avatar
Kenichi Handa committed
112 113 114 115 116 117

/* List of all font drivers.  All font-backends (XXXfont.c) call
   add_font_driver in syms_of_XXXfont to register the font-driver
   here.  */
static struct font_driver_list *font_driver_list;

118
static int font_pixel_size P_ ((FRAME_PTR f, Lisp_Object));
Kenichi Handa's avatar
Kenichi Handa committed
119 120 121 122
static Lisp_Object prop_name_to_numeric P_ ((enum font_property_index,
					     Lisp_Object));
static Lisp_Object prop_numeric_to_name P_ ((enum font_property_index, int));
static Lisp_Object font_open_entity P_ ((FRAME_PTR, Lisp_Object, int));
123
static void build_font_family_alist P_ ((void));
Kenichi Handa's avatar
Kenichi Handa committed
124 125 126 127

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

128
/* Return a pixel size of font-spec SPEC on frame F.  */
129

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
static int
font_pixel_size (f, spec)
     FRAME_PTR f;
     Lisp_Object spec;
{
  Lisp_Object size = AREF (spec, FONT_SIZE_INDEX);
  double point_size;
  int pixel_size, dpi;
  Lisp_Object extra, val;
      
  if (INTEGERP (size))
    return XINT (size);
  if (NILP (size))
    return 0;
  point_size = XFLOAT_DATA (size);
  extra = AREF (spec, FONT_EXTRA_INDEX);
  val = assq_no_quit (extra, QCdpi);
147 148 149 150 151 152 153
  if (CONSP (val))
    {
      if (INTEGERP (XCDR (val)))
	dpi = XINT (XCDR (val));
      else
	dpi = XFLOAT_DATA (XCDR (val)) + 0.5;
    }
154 155 156 157 158 159
  else
    dpi = f->resy;
  pixel_size = POINT_TO_PIXEL (point_size, dpi);
  return pixel_size;
}

Kenichi Handa's avatar
Kenichi Handa committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
/* Return a numeric value corresponding to PROP's NAME (symbol).  If
   NAME is not registered in font_style_table, return Qnil.  PROP must
   be one of FONT_{WEIGHT|SLANT|SWIDTH}_INDEX.  */

static Lisp_Object
prop_name_to_numeric (prop, name)
     enum font_property_index prop;
     Lisp_Object name;
{
  int table_index = prop - FONT_WEIGHT_INDEX;
  Lisp_Object val;

  val = assq_no_quit (name, AREF (font_style_table, table_index));
  return (NILP (val) ? Qnil : XCDR (val));
}


/* Return a name (symbol) corresponding to PROP's NUMERIC value.  If
   no name is registered for NUMERIC in font_style_table, return a
   symbol of integer name (e.g. `123').  PROP must be one of
   FONT_{WEIGHT|SLANT|SWIDTH}_INDEX.  */

static Lisp_Object
prop_numeric_to_name (prop, numeric)
     enum font_property_index prop;
     int numeric;
{
  int table_index = prop - FONT_WEIGHT_INDEX;
  Lisp_Object table = AREF (font_style_table, table_index);
  char buf[10];

  while (! NILP (table))
    {
      if (XINT (XCDR (XCAR (table))) >= numeric)
	{
	  if (XINT (XCDR (XCAR (table))) == numeric)
	    return XCAR (XCAR (table));
	  else
	    break;
	}
      table = XCDR (table);
    }
  sprintf (buf, "%d", numeric);
  return intern (buf);
}


/* Return a symbol whose name is STR (length LEN).  If STR contains
   uppercase letters, downcase them in advance.  */

Lisp_Object
intern_downcase (str, len)
     char *str;
     int len;
{
  char *buf;
  int i;

  for (i = 0; i < len; i++)
    if (isupper (str[i]))
      break;
  if (i == len)
    return Fintern (make_unibyte_string (str, len), Qnil);
  buf = alloca (len);
  if (! buf)
    return Fintern (null_string, Qnil);
  bcopy (str, buf, len);
  for (; i < len; i++)
    if (isascii (buf[i]))
      buf[i] = tolower (buf[i]);
  return Fintern (make_unibyte_string (buf, len), Qnil);
}

extern Lisp_Object Vface_alternative_font_family_alist;

static void
build_font_family_alist ()
{
  Lisp_Object alist = Vface_alternative_font_family_alist;

  for (; CONSP (alist); alist = XCDR (alist))
    {
      Lisp_Object tail, elt;

      for (tail = XCAR (alist), elt = Qnil ; CONSP (tail); tail = XCDR (tail))
	elt = nconc2 (elt, Fcons (Fintern (XCAR (tail), Qnil), Qnil));
      font_family_alist = Fcons (elt, font_family_alist);
    }
}


/* Font property validater.  */

253 254 255 256 257 258 259 260 261 262
static Lisp_Object font_prop_validate_symbol P_ ((enum font_property_index,
						  Lisp_Object, Lisp_Object));
static Lisp_Object font_prop_validate_style P_ ((enum font_property_index,
						 Lisp_Object, Lisp_Object));
static Lisp_Object font_prop_validate_non_neg P_ ((enum font_property_index,
						   Lisp_Object, Lisp_Object));
static Lisp_Object font_prop_validate_spacing P_ ((enum font_property_index,
						   Lisp_Object, Lisp_Object));
static int get_font_prop_index P_ ((Lisp_Object, int));
static Lisp_Object font_prop_validate P_ ((Lisp_Object));
Kenichi Handa's avatar
Kenichi Handa committed
263 264

static Lisp_Object
265 266 267
font_prop_validate_symbol (prop_index, prop, val)
     enum font_property_index prop_index;
     Lisp_Object prop, val;
Kenichi Handa's avatar
Kenichi Handa committed
268
{
269 270
  if (EQ (prop, QCotf))
    return (SYMBOLP (val) ? val : Qerror);
Kenichi Handa's avatar
Kenichi Handa committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284
  if (STRINGP (val))
    val = (SCHARS (val) == 0 ? null_string
	   : intern_downcase ((char *) SDATA (val), SBYTES (val)));
  else if (SYMBOLP (val))
    {
      if (SCHARS (SYMBOL_NAME (val)) == 0)
	val = null_string;
    }
  else
    val = Qerror;
  return val;
}

static Lisp_Object
285 286 287
font_prop_validate_style (prop_index, prop, val)
     enum font_property_index prop_index;
     Lisp_Object prop, val;
Kenichi Handa's avatar
Kenichi Handa committed
288 289 290 291 292 293 294 295 296
{
  if (! INTEGERP (val))
    {
      if (STRINGP (val))
	val = intern_downcase ((char *) SDATA (val), SBYTES (val));
      if (! SYMBOLP (val))
	val = Qerror;
      else
	{
297
	  val = prop_name_to_numeric (prop_index, val);
Kenichi Handa's avatar
Kenichi Handa committed
298 299 300 301 302 303 304 305
	  if (NILP (val))
	    val = Qerror;
	}
    }
  return val;
}

static Lisp_Object
306 307 308
font_prop_validate_non_neg (prop_index, prop, val)
     enum font_property_index prop_index;
     Lisp_Object prop, val;
Kenichi Handa's avatar
Kenichi Handa committed
309 310 311 312 313 314
{
  return (NATNUMP (val) || (FLOATP (val) && XFLOAT_DATA (val) >= 0)
	  ? val : Qerror);
}

static Lisp_Object
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
font_prop_validate_spacing (prop_index, prop, val)
     enum font_property_index prop_index;
     Lisp_Object prop, val;
{
  if (NILP (val) || (NATNUMP (val) && XINT (val) <= FONT_SPACING_CHARCELL))
    return val;
  if (EQ (val, Qc))
    return make_number (FONT_SPACING_CHARCELL);
  if (EQ (val, Qm))
    return make_number (FONT_SPACING_MONO);
  if (EQ (val, Qp))
    return make_number (FONT_SPACING_PROPORTIONAL);
  return Qerror;
}

/* Structure of known font property keys and validater of the
   values.  */
Kenichi Handa's avatar
Kenichi Handa committed
332 333
struct
{
334
  /* Pointer to the key symbol.  */
Kenichi Handa's avatar
Kenichi Handa committed
335
  Lisp_Object *key;
336 337 338 339 340
  /* Function to validate the value VAL, or NULL if any value is ok.  */
  Lisp_Object (*validater) P_ ((enum font_property_index prop_index,
				Lisp_Object prop, Lisp_Object val));
} font_property_table[] =
  { { &QCtype, font_prop_validate_symbol },
Kenichi Handa's avatar
Kenichi Handa committed
341 342 343 344 345 346 347
    { &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 },
348 349 350 351 352 353 354
    { &QCsize, font_prop_validate_non_neg },
    { &QClanguage, font_prop_validate_symbol },
    { &QCscript, font_prop_validate_symbol },
    { &QCdpi, font_prop_validate_non_neg },
    { &QCspacing, font_prop_validate_spacing },
    { &QCscalable, NULL },
    { &QCotf, font_prop_validate_symbol }
Kenichi Handa's avatar
Kenichi Handa committed
355 356
  };

357 358 359 360 361
#define FONT_PROPERTY_TABLE_SIZE \
  ((sizeof font_property_table) / (sizeof *font_property_table))

static int
get_font_prop_index (key, from)
Kenichi Handa's avatar
Kenichi Handa committed
362
     Lisp_Object key;
363
     int from;
Kenichi Handa's avatar
Kenichi Handa committed
364
{
365 366 367 368
  for (; from < FONT_PROPERTY_TABLE_SIZE; from++)
    if (EQ (key, *font_property_table[from].key))
      return from;
  return -1;
Kenichi Handa's avatar
Kenichi Handa committed
369 370 371 372 373 374
}

static Lisp_Object
font_prop_validate (spec)
     Lisp_Object spec;
{
375 376
  int i;
  Lisp_Object prop, val, extra;
Kenichi Handa's avatar
Kenichi Handa committed
377

378
  for (i = FONT_TYPE_INDEX; i < FONT_EXTRA_INDEX; i++)
Kenichi Handa's avatar
Kenichi Handa committed
379 380 381
    {
      if (! NILP (AREF (spec, i)))
	{
382 383
	  prop = *font_property_table[i].key;
	  val = (font_property_table[i].validater) (i, prop, AREF (spec, i));
Kenichi Handa's avatar
Kenichi Handa committed
384
	  if (EQ (val, Qerror))
385 386
	    Fsignal (Qfont, list2 (build_string ("invalid font property"),
				   Fcons (prop, AREF (spec, i))));
Kenichi Handa's avatar
Kenichi Handa committed
387 388 389
	  ASET (spec, i, val);
	}
    }
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
  for (extra = AREF (spec, FONT_EXTRA_INDEX);
       CONSP (extra); extra = XCDR (extra))
    {
      Lisp_Object elt = XCAR (extra);

      prop = XCAR (elt);
      i = get_font_prop_index (prop, FONT_EXTRA_INDEX);
      if (i >= 0
	  && font_property_table[i].validater)
	{
	  val = (font_property_table[i].validater) (i, prop, XCDR (elt));
	  if (EQ (val, Qerror))
	    Fsignal (Qfont, list2 (build_string ("invalid font property"),
				   elt));
	  XSETCDR (elt, val);
	}
    }
Kenichi Handa's avatar
Kenichi Handa committed
407 408 409
  return spec;
}
      
410
Lisp_Object
411
font_put_extra (font, prop, val)
412 413 414
     Lisp_Object font, prop, val;
{
  Lisp_Object extra = AREF (font, FONT_EXTRA_INDEX);
415
  Lisp_Object slot = (NILP (extra) ? Qnil : assq_no_quit (prop, extra));
416 417 418 419 420

  if (NILP (slot))
    {
      extra = Fcons (Fcons (prop, val), extra);
      ASET (font, FONT_EXTRA_INDEX, extra);
421
      return val;
422 423
    }
  XSETCDR (slot, val);
424
  return val;
425 426
}

Kenichi Handa's avatar
Kenichi Handa committed
427 428 429

/* Font name parser and unparser */

430 431 432 433
static Lisp_Object intern_font_field P_ ((char *, int));
static int parse_matrix P_ ((char *));
static int font_expand_wildcards P_ ((Lisp_Object *, int));
static int font_parse_name P_ ((char *, Lisp_Object));
Kenichi Handa's avatar
Kenichi Handa committed
434

435
/* An enumerator for each field of an XLFD font name.  */
Kenichi Handa's avatar
Kenichi Handa committed
436 437 438 439 440 441 442 443
enum xlfd_field_index
{
  XLFD_FOUNDRY_INDEX,
  XLFD_FAMILY_INDEX,
  XLFD_WEIGHT_INDEX,
  XLFD_SLANT_INDEX,
  XLFD_SWIDTH_INDEX,
  XLFD_ADSTYLE_INDEX,
444 445
  XLFD_PIXEL_INDEX,
  XLFD_POINT_INDEX,
Kenichi Handa's avatar
Kenichi Handa committed
446 447 448 449 450 451 452 453 454
  XLFD_RESX_INDEX,
  XLFD_RESY_INDEX,
  XLFD_SPACING_INDEX,
  XLFD_AVGWIDTH_INDEX,
  XLFD_REGISTRY_INDEX,
  XLFD_ENCODING_INDEX,
  XLFD_LAST_INDEX
};

455
/* An enumerator for mask bit corresponding to each XLFD field.  */
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
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
};


475 476 477 478 479
/* Return a Lispy value of a XLFD font field at STR and LEN bytes.
   If LEN is zero, it returns `null_string'.
   If STR is "*", it returns nil.
   If all characters in STR are digits, it returns an integer.
   Otherwise, it returns a symbol interned from downcased STR.  */
Kenichi Handa's avatar
Kenichi Handa committed
480 481

static Lisp_Object
482 483 484
intern_font_field (str, len)
     char *str;
     int len;
Kenichi Handa's avatar
Kenichi Handa committed
485
{
486
  int i;
Kenichi Handa's avatar
Kenichi Handa committed
487 488 489 490 491

  if (len == 0)
    return null_string;
  if (*str == '*' && len == 1)
    return Qnil;
492 493 494 495 496 497 498 499
  if (isdigit (*str))
    {
      for (i = 1; i < len; i++)
	if (! isdigit (str[i]))
	  break;
      if (i == len)
	return make_number (atoi (str));
    }
Kenichi Handa's avatar
Kenichi Handa committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
  return intern_downcase (str, len);
}

/* Parse P pointing the pixel/point size field of the form
   `[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
parse_matrix (p)
     char *p;
{
  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);
}

536 537 538 539
/* Expand a wildcard field in FIELD (the first N fields are filled) to
   multiple fields to fill in all 14 XLFD fields while restring a
   field position by its contents.  */

540
static int
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
font_expand_wildcards (field, n)
     Lisp_Object field[XLFD_LAST_INDEX];
     int n;
{
  /* 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;
558
  int range_from, range_to;
559 560 561 562 563 564
  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 \
565
			    | XLFD_AVGWIDTH_MASK)
566 567 568 569 570 571 572 573
#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;

574 575 576 577
  /* 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)
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
    {
      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);

600 601 602
	      if (i + 1 == n)
		from = to = XLFD_ENCODING_INDEX,
		  mask = XLFD_ENCODING_MASK;
603 604 605
	      else if (numeric == 0)
		from = XLFD_PIXEL_INDEX, to = XLFD_AVGWIDTH_INDEX,
		  mask = XLFD_PIXEL_MASK | XLFD_LARGENUM_MASK;
606 607 608
	      else if (numeric <= 48)
		from = to = XLFD_PIXEL_INDEX,
		  mask = XLFD_PIXEL_MASK;
609 610
	      else 
		from = XLFD_POINT_INDEX, to = XLFD_AVGWIDTH_INDEX,
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
		  mask = XLFD_LARGENUM_MASK;
	    }
	  else if (EQ (val, null_string))
	    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;
	    }
629 630 631
	  else if (range_from <= XLFD_WEIGHT_INDEX
		   && range_to >= XLFD_WEIGHT_INDEX
		   && !NILP (prop_name_to_numeric (FONT_WEIGHT_INDEX, val)))
632
	    from = to = XLFD_WEIGHT_INDEX, mask = XLFD_WEIGHT_MASK;
633 634 635
	  else if (range_from <= XLFD_SLANT_INDEX
		   && range_to >= XLFD_SLANT_INDEX
		   && !NILP (prop_name_to_numeric (FONT_SLANT_INDEX, val)))
636
	    from = to = XLFD_SLANT_INDEX, mask = XLFD_SLANT_MASK;
637 638 639
	  else if (range_from <= XLFD_SWIDTH_INDEX
		   && range_to >= XLFD_SWIDTH_INDEX
		   && !NILP (prop_name_to_numeric (FONT_WIDTH_INDEX, val)))
640 641 642
	    from = to = XLFD_SWIDTH_INDEX, mask = XLFD_SWIDTH_MASK;
	  else
	    {
643
	      if (EQ (val, Qc) || EQ (val, Qm) || EQ (val, Qp) || EQ (val, Qd))
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
		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)
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
	    {
	      /* 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;
		}
	    }
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
	}
    }

  /* 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;
}

727
/* Parse NAME (null terminated) as XLFD and store information in FONT
728 729 730 731 732 733 734 735
   (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)

736
   If NAME is successfully parsed, return 0.  Otherwise return -1.
737

738 739 740 741 742
   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
   a fully specified XLFD, and we set FONT_EXTRA_INDEX of FONT to a
   symbol RESX-RESY-SPACING-AVGWIDTH.
*/
Kenichi Handa's avatar
Kenichi Handa committed
743 744

int
745
font_parse_xlfd (name, font)
Kenichi Handa's avatar
Kenichi Handa committed
746 747 748 749 750
     char *name;
     Lisp_Object font;
{
  int len = strlen (name);
  int i, j;
751 752
  Lisp_Object dpi, spacing;
  int avgwidth;
753
  char *f[XLFD_LAST_INDEX + 1];
Kenichi Handa's avatar
Kenichi Handa committed
754
  Lisp_Object val;
755
  char *p;
Kenichi Handa's avatar
Kenichi Handa committed
756 757 758 759

  if (len > 255)
    /* Maximum XLFD name length is 255. */
    return -1;
760 761 762 763 764 765 766 767 768
  /* Accept "*-.." as a fully specified XLFD. */
  if (name[0] == '*' && name[1] == '-')
    i = 1, f[XLFD_FOUNDRY_INDEX] = name;
  else
    i = 0;
  for (p = name + i; *p; p++)
    if (*p == '-' && i < XLFD_LAST_INDEX)
      f[i++] = p + 1;
  f[i] = p;
Kenichi Handa's avatar
Kenichi Handa committed
769

770 771
  dpi = spacing = Qnil;
  avgwidth = -1;
772

773
  if (i == XLFD_LAST_INDEX)
774
    {
775 776
      int pixel_size;

777
      /* Fully specified XLFD.  */
778 779 780 781 782 783 784
      for (i = 0, j = FONT_FOUNDRY_INDEX; i < XLFD_WEIGHT_INDEX; i++, j++)
	{
	  val = intern_font_field (f[i], f[i + 1] - 1 - f[i]);
	  if (! NILP (val))
	    ASET (font, j, val);
	}
      for (j = FONT_WEIGHT_INDEX; i < XLFD_ADSTYLE_INDEX; i++, j++)
785
	{
786 787
	  val = intern_font_field (f[i], f[i + 1] - 1 - f[i]);
	  if (! NILP (val))
788
	    {
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
	      Lisp_Object numeric = prop_name_to_numeric (j, val);

	      if (INTEGERP (numeric))
		val = numeric;
	      ASET (font, j, val);
	    }
	}
      val = intern_font_field (f[i], f[i + 1] - 1 - f[i]);
      if (! NILP (val))
	ASET (font, FONT_ADSTYLE_INDEX, val);
      i = XLFD_REGISTRY_INDEX;
      val = intern_font_field (f[i], f[i + 2] - f[i]);
      if (! NILP (val))
	ASET (font, FONT_REGISTRY_INDEX, val);

      p = f[XLFD_PIXEL_INDEX];
      if (*p == '[' && (pixel_size = parse_matrix (p)) >= 0)
	ASET (font, FONT_SIZE_INDEX, make_number (pixel_size));	      
      else
	{
	  i = XLFD_PIXEL_INDEX;
	  val = intern_font_field (f[i], f[i + 1] - 1 - f[i]);
	  if (! NILP (val))
	    ASET (font, FONT_SIZE_INDEX, val);
	  else
	    {
	      double point_size = -1;

	      xassert (FONT_SPEC_P (font));
	      p = f[XLFD_POINT_INDEX];
	      if (*p == '[')
		point_size = parse_matrix (p);
	      else if (isdigit (*p))
		point_size = atoi (p), point_size /= 10;
	      if (point_size >= 0)
		ASET (font, FONT_SIZE_INDEX, make_float (point_size));
825 826
	      else
		{
827 828 829 830
		  i = XLFD_PIXEL_INDEX;
		  val = intern_font_field (f[i], f[i + 1] - 1 - f[i]);
		  if (! NILP (val))
		    ASET (font, FONT_SIZE_INDEX, val);
831 832 833
		}
	    }
	}
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854

      /* Parse RESX, RESY, SPACING, and AVGWIDTH.  */
      if (FONT_ENTITY_P (font))
	{
	  i = XLFD_RESX_INDEX;
	  ASET (font, FONT_EXTRA_INDEX,
		intern_font_field (f[i], f[XLFD_REGISTRY_INDEX] - 1 - f[i]));
	  return 0;
	}

      /* Here we just setup DPI, SPACING, and AVGWIDTH.  They are set
	 in FONT_EXTRA_INDEX later.  */
      i = XLFD_RESX_INDEX;
      dpi = intern_font_field (f[i], f[i + 1] - 1 - f[i]);
      i = XLFD_SPACING_INDEX;
      spacing = intern_font_field (f[i], f[i + 1] - 1 - f[i]);
      p = f[XLFD_AVGWIDTH_INDEX];
      if (*p == '~')
	p++;
      if (isdigit (*p))
	avgwidth = atoi (p);
855 856
    }
  else
Kenichi Handa's avatar
Kenichi Handa committed
857
    {
858
      int wild_card_found = 0;
859
      Lisp_Object prop[XLFD_LAST_INDEX];
860

861
      for (j = 0; j < i; j++)
862
	{
863
	  if (*f[j] == '*')
864
	    {
865 866 867 868 869 870 871 872 873 874
	      if (f[j][1] && f[j][1] != '-')
		return -1;
	      prop[j] = Qnil;
	      wild_card_found = 1;
	    }
	  else if (isdigit (*f[j]))
	    {
	      for (p = f[j] + 1; isdigit (*p); p++);
	      if (*p && *p != '-')
		prop[j] = intern_downcase (f[j], p - f[j]);
875
	      else
876
		prop[j] = make_number (atoi (f[j]));
877
	    }
878 879 880 881
	  else if (j + 1 < i)
	    prop[j] = intern_font_field (f[j], f[j + 1] - 1 - f[j]);
	  else
	    prop[j] = intern_font_field (f[j], f[i] - f[j]);
882 883
	}
      if (! wild_card_found)
Kenichi Handa's avatar
Kenichi Handa committed
884
	return -1;
885
      if (font_expand_wildcards (prop, i) < 0)
886
	return -1;
887 888 889 890 891 892 893 894 895 896 897

      for (i = 0, j = FONT_FOUNDRY_INDEX; i < XLFD_WEIGHT_INDEX; i++, j++)
	if (! NILP (prop[i]))
	  ASET (font, j, prop[i]);
      for (j = FONT_WEIGHT_INDEX; i < XLFD_ADSTYLE_INDEX; i++, j++)
	if (! NILP (prop[i]))
	  ASET (font, j, prop[i]);
      if (! NILP (prop[XLFD_ADSTYLE_INDEX]))
	ASET (font, FONT_ADSTYLE_INDEX, prop[XLFD_ADSTYLE_INDEX]);
      val = prop[XLFD_REGISTRY_INDEX];
      if (NILP (val))
Kenichi Handa's avatar
Kenichi Handa committed
898
	{
899 900 901 902
	  val = prop[XLFD_ENCODING_INDEX];
	  if (! NILP (val))
	    val = Fintern (concat2 (build_string ("*-"), SYMBOL_NAME (val)),
			   Qnil);
903
	}
904 905 906
      else if (NILP (prop[XLFD_ENCODING_INDEX]))
	val = Fintern (concat2 (SYMBOL_NAME (val), build_string ("-*")),
		       Qnil);
907
      else
908 909 910 911 912 913 914 915 916
	val = Fintern (concat3 (SYMBOL_NAME (val), build_string ("-"),
				SYMBOL_NAME (prop[XLFD_ENCODING_INDEX])),
		       Qnil);
      if (! NILP (val))
	ASET (font, FONT_REGISTRY_INDEX, val);

      if (INTEGERP (prop[XLFD_PIXEL_INDEX]))
	ASET (font, FONT_SIZE_INDEX, prop[XLFD_PIXEL_INDEX]);
      else if (INTEGERP (prop[XLFD_POINT_INDEX]))
917
	{
918
	  double point_size = XINT (prop[XLFD_POINT_INDEX]);
Kenichi Handa's avatar
Kenichi Handa committed
919

920 921
	  ASET (font, FONT_SIZE_INDEX, make_float (point_size / 10));
	}
Kenichi Handa's avatar
Kenichi Handa committed
922

923 924 925 926
      dpi = prop[XLFD_RESX_INDEX];
      spacing = prop[XLFD_SPACING_INDEX];
      if (INTEGERP (prop[XLFD_AVGWIDTH_INDEX]))
	avgwidth = XINT (prop[XLFD_AVGWIDTH_INDEX]);
Kenichi Handa's avatar
Kenichi Handa committed
927 928
    }

929 930 931 932 933 934
  if (! NILP (dpi))
    font_put_extra (font, QCdpi, dpi);
  if (! NILP (spacing))
    font_put_extra (font, QCspacing, spacing);
  if (avgwidth >= 0)
    font_put_extra (font, QCscalable, avgwidth == 0 ? Qt : Qnil);
Kenichi Handa's avatar
Kenichi Handa committed
935

936
  return 0;
Kenichi Handa's avatar
Kenichi Handa committed
937 938 939 940 941 942 943 944 945
}

/* Store XLFD name of FONT (font-spec or font-entity) in NAME (NBYTES
   length), and return the name length.  If FONT_SIZE_INDEX of FONT is
   0, use PIXEL_SIZE instead.  */

int
font_unparse_xlfd (font, pixel_size, name, nbytes)
     Lisp_Object font;
946
     int pixel_size;
Kenichi Handa's avatar
Kenichi Handa committed
947 948 949
     char *name;
     int nbytes;
{
950
  char *f[XLFD_REGISTRY_INDEX + 1];
Kenichi Handa's avatar
Kenichi Handa committed
951 952 953 954 955 956 957 958 959 960 961 962 963 964
  Lisp_Object val;
  int i, j, len = 0;

  xassert (FONTP (font));

  for (i = FONT_FOUNDRY_INDEX, j = XLFD_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX;
       i++, j++)
    {
      if (i == FONT_ADSTYLE_INDEX)
	j = XLFD_ADSTYLE_INDEX;
      else if (i == FONT_REGISTRY_INDEX)
	j = XLFD_REGISTRY_INDEX;
      val = AREF (font, i);
      if (NILP (val))
965 966 967 968 969 970
	{
	  if (j == XLFD_REGISTRY_INDEX)
	    f[j] = "*-*", len += 4;
	  else
	    f[j] = "*", len += 2;
	}
Kenichi Handa's avatar
Kenichi Handa committed
971 972 973 974
      else
	{
	  if (SYMBOLP (val))
	    val = SYMBOL_NAME (val);
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
	  if (j == XLFD_REGISTRY_INDEX
	      && ! strchr ((char *) SDATA (val), '-'))
	    {
	      /* Change "jisx0208*" and "jisx0208" to "jisx0208*-*".  */
	      if (SDATA (val)[SBYTES (val) - 1] == '*')
		{
		  f[j] = alloca (SBYTES (val) + 3);
		  sprintf (f[j], "%s-*", SDATA (val));
		  len += SBYTES (val) + 3;
		}
	      else
		{
		  f[j] = alloca (SBYTES (val) + 4);
		  sprintf (f[j], "%s*-*", SDATA (val));
		  len += SBYTES (val) + 4;
		}
	    }
	  else
	    f[j] = (char *) SDATA (val), len += SBYTES (val) + 1;
Kenichi Handa's avatar
Kenichi Handa committed
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
	}
    }

  for (i = FONT_WEIGHT_INDEX, j = XLFD_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX;
       i++, j++)
    {
      val = AREF (font, i);
      if (NILP (val))
	f[j] = "*", len += 2;
      else
	{
	  if (INTEGERP (val))
	    val = prop_numeric_to_name (i, XINT (val));
	  if (SYMBOLP (val))
	    val = SYMBOL_NAME (val);
	  xassert (STRINGP (val));
	  f[j] = (char *) SDATA (val), len += SBYTES (val) + 1;
	}
    }

  val = AREF (font, FONT_SIZE_INDEX);
  xassert (NUMBERP (val) || NILP (val));
  if (INTEGERP (val))
    {
1018
      f[XLFD_PIXEL_INDEX] = alloca (22);
Kenichi Handa's avatar
Kenichi Handa committed
1019 1020
      i = XINT (val);
      if (i > 0)
1021
	len += sprintf (f[XLFD_PIXEL_INDEX], "%d-*", i) + 1;
Kenichi Handa's avatar
Kenichi Handa committed
1022
      else 			/* i == 0 */
1023
	len += sprintf (f[XLFD_PIXEL_INDEX], "%d-*", pixel_size) + 1;
Kenichi Handa's avatar
Kenichi Handa committed
1024 1025 1026
    }
  else if (FLOATP (val))
    {
1027
      f[XLFD_PIXEL_INDEX] = alloca (12);
Kenichi Handa's avatar
Kenichi Handa committed
1028
      i = XFLOAT_DATA (val) * 10;
1029
      len += sprintf (f[XLFD_PIXEL_INDEX], "*-%d", i) + 1;
Kenichi Handa's avatar
Kenichi Handa committed
1030 1031
    }
  else
1032 1033 1034
    f[XLFD_PIXEL_INDEX] = "*-*", len += 4;

  val = AREF (font, FONT_EXTRA_INDEX);
Kenichi Handa's avatar
Kenichi Handa committed
1035 1036 1037 1038

  if (FONT_ENTITY_P (font)
      && EQ (AREF (font, FONT_TYPE_INDEX), Qx))
    {
1039
      /* Setup names for RESX-RESY-SPACING-AVWIDTH.  */
Kenichi Handa's avatar
Kenichi Handa committed
1040 1041 1042
      if (SYMBOLP (val) && ! NILP (val))
	{
	  val = SYMBOL_NAME (val);
1043
	  f[XLFD_RESX_INDEX] = (char *) SDATA (val), len += SBYTES (val) + 1;
Kenichi Handa's avatar
Kenichi Handa committed
1044 1045
	}
      else
1046
	f[XLFD_RESX_INDEX] = "*-*-*-*", len += 6;
Kenichi Handa's avatar
Kenichi Handa committed
1047 1048
    }
  else
1049 1050 1051 1052 1053 1054 1055 1056 1057
    {
      Lisp_Object dpi = assq_no_quit (QCdpi, val);
      Lisp_Object spacing = assq_no_quit (QCspacing, val);
      Lisp_Object scalable = assq_no_quit (QCscalable, val);

      if (CONSP (dpi) || CONSP (spacing) || CONSP (scalable))
	{
	  char *str = alloca (24);
	  int this_len;
Kenichi Handa's avatar
Kenichi Handa committed
1058

1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
	  if (CONSP (dpi) && INTEGERP (XCDR (dpi)))
	    this_len = sprintf (str, "%d-%d",
				XINT (XCDR (dpi)), XINT (XCDR (dpi)));
	  else
	    this_len = sprintf (str, "*-*");
	  if (CONSP (spacing) && ! NILP (XCDR (spacing)))
	    {
	      val = XCDR (spacing);
	      if (INTEGERP (val))
		{
		  if (XINT (val) < FONT_SPACING_MONO)
		    val = Qp;
		  else if (XINT (val) < FONT_SPACING_CHARCELL)
		    val = Qm;
		  else
		    val = Qc;
		}
	      xassert (SYMBOLP (val));
	      this_len += sprintf (str + this_len, "-%c",
				   SDATA (SYMBOL_NAME (val))[0]);
	    }
	  else
	    this_len += sprintf (str + this_len, "-*");
	  if (CONSP (scalable) && ! NILP (XCDR (spacing)))
	    this_len += sprintf (str + this_len, "-0");
	  else
	    this_len += sprintf (str + this_len, "-*");
	  f[XLFD_RESX_INDEX] = str;
	  len += this_len;
	}
      else
	f[XLFD_RESX_INDEX] = "*-*-*-*", len += 8;
    }

  len++;	/* for terminating '\0'.  */
Kenichi Handa's avatar
Kenichi Handa committed
1094 1095
  if (len >= nbytes)
    return -1;
1096
  return sprintf (name, "-%s-%s-%s-%s-%s-%s-%s-%s-%s",
Kenichi Handa's avatar
Kenichi Handa committed
1097 1098 1099
		  f[XLFD_FOUNDRY_INDEX], f[XLFD_FAMILY_INDEX],
		  f[XLFD_WEIGHT_INDEX], f[XLFD_SLANT_INDEX],
		  f[XLFD_SWIDTH_INDEX],
1100 1101
		  f[XLFD_ADSTYLE_INDEX], f[XLFD_PIXEL_INDEX],
		  f[XLFD_RESX_INDEX], f[XLFD_REGISTRY_INDEX]);
Kenichi Handa's avatar
Kenichi Handa committed
1102 1103
}

1104
/* Parse NAME (null terminated) as Fonconfig's name format and store
1105 1106
   information in FONT (font-spec or font-entity).  If NAME is
   successfully parsed, return 0.  Otherwise return -1.  */
1107 1108

int
1109
font_parse_fcname (name, font)
1110 1111 1112 1113
     char *name;
     Lisp_Object font;
{
  char *p0, *p1;
1114 1115
  int len = strlen (name);
  char *copy;
1116

1117 1118
  if (len == 0)
    return -1;
1119 1120 1121 1122 1123
  /* It is assured that (name[0] && name[0] != '-').  */
  if (name[0] == ':')
    p0 = name;
  else
    {
1124 1125 1126 1127 1128 1129 1130 1131
      Lisp_Object family;
      double point_size;

      for (p0 = name + 1; *p0 && (*p0 != '-' && *p0 != ':'); p0++)
	if (*p0 == '\\' && p0[1])
	  p0++;
      family = intern_font_field (name, p0 - name);
      if (*p0 == '-')
1132
	{
1133 1134 1135 1136 1137 1138 1139
	  if (! isdigit (p0[1]))
	    return -1;
	  point_size = strtod (p0 + 1, &p1);
	  if (*p1 && *p1 != ':')
	    return -1;
	  ASET (font, FONT_SIZE_INDEX, make_float (point_size));
	  p0 = p1;
1140
	}
1141
      ASET (font, FONT_FAMILY_INDEX, family);
1142
    }
1143 1144 1145 1146 1147 1148 1149 1150 1151

  len -= p0 - name;
  copy = alloca (len + 1);
  if (! copy)
    return -1;
  name = copy;
  
  /* Now parse ":KEY=VAL" patterns.  Store known keys and values in
     extra, copy unknown ones to COPY.  */
1152 1153 1154
  while (*p0)
    {
      Lisp_Object key, val;
1155
      int prop;
1156

1157
      for (p1 = p0 + 1; *p1 && *p1 != '=' && *p1 != ':'; p1++);
1158
      if (*p1 != '=')
1159
	{
1160 1161 1162 1163 1164 1165 1166 1167
	  /* Must be an enumerated value.  */
	  val = intern_font_field (p0 + 1, p1 - p0 - 1);
	  if (memcmp (p0 + 1, "light", 5) == 0
	      || memcmp (p0 + 1, "medium", 6) == 0
	      || memcmp (p0 + 1, "demibold", 8) == 0
	      || memcmp (p0 + 1, "bold", 4) == 0
	      || memcmp (p0 + 1, "black", 5) == 0)
	    {
1168
	      ASET (font, FONT_WEIGHT_INDEX, val);
1169 1170 1171 1172 1173
	    }
	  else if (memcmp (p0 + 1, "roman", 5) == 0
		   || memcmp (p0 + 1, "italic", 6) == 0
		   || memcmp (p0 + 1, "oblique", 7) == 0)
	    {
1174
	      ASET (font, FONT_SLANT_INDEX, val);
1175 1176 1177 1178 1179 1180
	    }
	  else if (memcmp (p0 + 1, "charcell", 8) == 0
		   || memcmp (p0 + 1, "mono", 4) == 0
		   || memcmp (p0 + 1, "proportional", 12) == 0)
	    {
	      font_put_extra (font, QCspacing,
1181
			      (p0[1] == 'c' ? Qc : p0[1] == 'm' ? Qm : Qp));
1182 1183 1184 1185 1186 1187 1188
	    }
	  else
	    {
	      /* unknown key */
	      bcopy (p0, copy, p1 - p0);
	      copy += p1 - p0;
	    }
1189 1190 1191
	}
      else
	{
1192 1193 1194 1195 1196
	  if (memcmp (p0 + 1, "pixelsize=", 10) == 0)
	    prop = FONT_SIZE_INDEX;
	  else
	    {
	      key = intern_font_field (p0, p1 - p0);
1197
	      prop = get_font_prop_index (key, 0);
1198 1199 1200
	    }
	  p0 = p1 + 1;
	  for (p1 = p0; *p1 && *p1 != ':'; p1++);
1201 1202
	  val = intern_font_field (p0, p1 - p0);
	  if (! NILP (val))
1203
	    {
1204
	      if (prop >= 0 && prop < FONT_EXTRA_INDEX)
1205
		{
1206 1207 1208
		  ASET (font, prop, val);
		}
	      else
1209
		font_put_extra (font, key, val);
1210 1211 1212 1213
	    }
	}
      p0 = p1;
    }
1214 1215

  return 0;
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
}

/* Store fontconfig's font name of FONT (font-spec or font-entity) in
   NAME (NBYTES length), and return the name length.  If
   FONT_SIZE_INDEX of FONT is 0, use PIXEL_SIZE instead.  */

int
font_unparse_fcname (font, pixel_size, name, nbytes)
     Lisp_Object font;
     int pixel_size;
     char *name;
     int nbytes;
{
1229 1230 1231 1232
  Lisp_Object val;
  int point_size;
  int dpi, spacing, scalable;
  int i, len = 1;
1233
  char *p;
1234
  Lisp_Object styles[3];
1235
  char *style_names[3] = { "weight", "slant", "width" };
1236

1237 1238 1239 1240 1241 1242
  val = AREF (font, FONT_FAMILY_INDEX);
  if (SYMBOLP (val) && ! NILP (val))
    len += SBYTES (SYMBOL_NAME (val));

  val = AREF (font, FONT_SIZE_INDEX);
  if (INTEGERP (val))
1243
    {
1244 1245 1246 1247
      if (XINT (val) != 0)
	pixel_size = XINT (val);
      point_size = -1;
      len += 21;		/* for ":pixelsize=NUM" */
1248
    }
1249
  else if (FLOATP (val))
1250
    {
1251 1252 1253
      pixel_size = -1;
      point_size = (int) XFLOAT_DATA (val);
      len += 11;		/* for "-NUM" */
1254
    }
1255 1256

  val = AREF (font, FONT_FOUNDRY_INDEX);
1257
  if (SYMBOLP (val) && ! NILP (val))
1258
    /* ":foundry=NAME" */
1259 1260
    len += 9 + SBYTES (SYMBOL_NAME (val));

1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
  for (i = FONT_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX; i++)
    {
      val = AREF (font, i);
      if (INTEGERP (val))
	{
	  val = prop_numeric_to_name (i, XINT (val));
	  len += (strlen (style_names[i - FONT_WEIGHT_INDEX])
		  + 2 + SBYTES (SYMBOL_NAME (val))); /* :xxx=NAME */
	}
      styles[i - FONT_WEIGHT_INDEX] = val;
    }
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307

  val = AREF (font, FONT_EXTRA_INDEX);
  if (FONT_ENTITY_P (font)
      && EQ (AREF (font, FONT_TYPE_INDEX), Qx))
    {
      char *p;

      /* VAL is a symbol of name `RESX-RESY-SPACING-AVWIDTH'.  */
      p = (char *) SDATA (SYMBOL_NAME (val));
      dpi = atoi (p);
      for (p++; *p != '-'; p++);	/* skip RESX */
      for (p++; *p != '-'; p++);	/* skip RESY */
      spacing = (*p == 'c' ? FONT_SPACING_CHARCELL
		 : *p == 'm' ? FONT_SPACING_MONO
		 : FONT_SPACING_PROPORTIONAL);
      for (p++; *p != '-'; p++);	/* skip SPACING */
      scalable = (atoi (p) == 0);
      /* The longest pattern is ":dpi=NUM:scalable=False:spacing=100" */
      len += 42;
    }
  else
    {
      Lisp_Object elt;

      dpi = spacing = scalable = -1;
      elt = assq_no_quit (QCdpi, val);
      if (CONSP (elt))
	dpi = XINT (XCDR (elt)), len += 15; /* for ":dpi=NUM" */
      elt = assq_no_quit (QCspacing, val);
      if (CONSP (elt))
	spacing = XINT (XCDR (elt)), len += 12; /* for ":spacing=100" */
      elt = assq_no_quit (QCscalable, val);
      if (CONSP (elt))
	scalable = ! NILP (XCDR (elt)), len += 15; /* for ":scalable=False" */
    }

1308 1309 1310 1311
  if (len > nbytes)
    return -1;
  p = name;
  if (! NILP (AREF (font, FONT_FAMILY_INDEX)))
1312 1313
    p += sprintf(p, "%s",
		 SDATA (SYMBOL_NAME (AREF (font, FONT_FAMILY_INDEX))));
1314 1315 1316 1317 1318 1319 1320
  if (point_size > 0)
    {
      if (p == name)
	p += sprintf (p, "%d", point_size);
      else
	p += sprintf (p, "-%d", point_size);
    }
1321 1322
  else if (pixel_size > 0)
    p += sprintf (p, ":pixelsize=%d", pixel_size);
1323 1324 1325 1326 1327
  if (SYMBOLP (AREF (font, FONT_FOUNDRY_INDEX))
      && ! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
    p += sprintf (p, ":foundry=%s",
		  SDATA (SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX))));
  for (i = 0; i < 3; i++)
1328
    if (SYMBOLP (styles[i]) && ! NILP (styles [i]))
1329 1330
      p += sprintf (p, ":%s=%s", style_names[i],
		    SDATA (SYMBOL_NAME (styles [i])));
1331 1332 1333 1334 1335 1336 1337 1338
  if (dpi >= 0)
    p += sprintf (p, ":dpi=%d", dpi);
  if (spacing >= 0)
    p += sprintf (p, ":spacing=%d", spacing);
  if (scalable > 0)
    p += sprintf (p, ":scalable=True");
  else if (scalable == 0)
    p += sprintf (p, ":scalable=False");
1339 1340 1341 1342 1343
  return (p - name);
}

/* Parse NAME (null terminated) and store information in FONT
   (font-spec or font-entity).  If NAME is successfully parsed, return
1344
   0.  Otherwise return -1.
1345 1346

   If NAME is XLFD and FONT is a font-entity, store
1347 1348
   RESX-RESY-SPACING-AVWIDTH information as a symbol in
   FONT_EXTRA_INDEX.  */
1349 1350

static int
1351
font_parse_name (name, font)
1352 1353 1354 1355
     char *name;
     Lisp_Object font;
{
  if (name[0] == '-' || index (name, '*'))
1356
    return font_parse_xlfd (name, font);
1357
  return font_parse_fcname (name, font);
1358 1359
}

Kenichi Handa's avatar
Kenichi Handa committed
1360 1361 1362 1363 1364 1365
void
font_merge_old_spec (name, family, registry, spec)
     Lisp_Object name, family, registry, spec;
{
  if (STRINGP (name))
    {
1366
      if (font_parse_xlfd ((char *) SDATA (name), spec) < 0)
Kenichi Handa's avatar
Kenichi Handa committed
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
	{
	  Lisp_Object extra = Fcons (Fcons (QCname, name), Qnil);

	  ASET (spec, FONT_EXTRA_INDEX, extra);
	}
    }
  else
    {
      if (! NILP (family))
	{
	  int len;
	  char *p0, *p1;

	  xassert (STRINGP (family));
	  len = SBYTES (family);
	  p0 = (char *) SDATA (family);
	  p1 = index (p0, '-');
	  if (p1)
	    {
1386 1387
	      if ((*p0 != '*' || p1 - p0 > 1)
		  && NILP (AREF (spec, FONT_FOUNDRY_INDEX)))
Kenichi Handa's avatar
Kenichi Handa committed
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
		ASET (spec, FONT_FOUNDRY_INDEX,
		      intern_downcase (p0, p1 - p0));
	      if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
		ASET (spec, FONT_FAMILY_INDEX,
		      intern_downcase (p1 + 1, len - (p1 + 1 - p0)));
	    }
	  else if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
	    ASET (spec, FONT_FAMILY_INDEX, intern_downcase (p0, len));
	}
      if (! NILP (registry)
	  && NILP (AREF (spec, FONT_REGISTRY_INDEX)))
	ASET (spec, FONT_REGISTRY_INDEX,
	      intern_downcase ((char *) SDATA (registry), SBYTES (registry)));
    }
}

Kenichi Handa's avatar
Kenichi Handa committed
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
static Lisp_Object
font_lispy_object (font)
     struct font *font;
{
  Lisp_Object objlist = AREF (font->entity, FONT_OBJLIST_INDEX);

  for (; ! NILP (objlist); objlist = XCDR (objlist))
    {
      struct Lisp_Save_Value *p = XSAVE_VALUE (XCAR (objlist));

      if (font == (struct font *) p->pointer)
	break;
    }
  xassert (! NILP (objlist));
  return XCAR (objlist);
}

1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
#define LGSTRING_HEADER_SIZE 6
#define LGSTRING_GLYPH_SIZE 8

static int
check_gstring (gstring)
     Lisp_Object gstring;
{
  Lisp_Object val;
  int i, j;

  CHECK_VECTOR (gstring);
  val = AREF (gstring, 0);
  CHECK_VECTOR (val);
  if (ASIZE (val) < LGSTRING_HEADER_SIZE)
    goto err;
  CHECK_FONT_OBJECT (LGSTRING_FONT (gstring));
  if (! NILP (LGSTRING_LBEARING (gstring)))
    CHECK_NUMBER (LGSTRING_LBEARING (gstring));
  if (! NILP (LGSTRING_RBEARING (gstring)))
    CHECK_NUMBER (LGSTRING_RBEARING (gstring));
  if (! NILP (LGSTRING_WIDTH (gstring)))
    CHECK_NATNUM (LGSTRING_WIDTH (gstring));
  if (! NILP (LGSTRING_ASCENT (gstring)))
    CHECK_NUMBER (LGSTRING_ASCENT (gstring));
  if (! NILP (LGSTRING_DESCENT (gstring)))
    CHECK_NUMBER (LGSTRING_DESCENT(gstring));

  for (i = 0; i < LGSTRING_LENGTH (gstring); i++)
    {
      val = LGSTRING_GLYPH (gstring, i);
      CHECK_VECTOR (val);
      if (ASIZE (val) < LGSTRING_GLYPH_SIZE)
	goto err;
      if (NILP (LGLYPH_CHAR (val)))
	break;
      CHECK_NATNUM (LGLYPH_FROM (val));
      CHECK_NATNUM (LGLYPH_TO (val));
      CHECK_CHARACTER (LGLYPH_CHAR (val));
      if (! NILP (LGLYPH_CODE (val)))
	CHECK_NATNUM (LGLYPH_CODE (val));
      if (! NILP (LGLYPH_WIDTH (val)))
	CHECK_NATNUM (LGLYPH_WIDTH (val));
      if (! NILP (LGLYPH_ADJUSTMENT (val)))
	{
	  val = LGLYPH_ADJUSTMENT (val);
	  CHECK_VECTOR (val);
	  if (ASIZE (val) < 3)
	    goto err;
	  for (j = 0; j < 3; j++)
	    CHECK_NUMBER (AREF (val, j));
	}
    }
  return i;
 err:
  error ("Invalid glyph-string format");
  return -1;
}

Kenichi Handa's avatar
Kenichi Handa committed
1479 1480 1481

/* OTF handler */

1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
static void
check_otf_features (otf_features)
     Lisp_Object otf_features;
{
  Lisp_Object val, elt;

  CHECK_CONS (otf_features);
  CHECK_SYMBOL (XCAR (otf_features));
  otf_features = XCDR (otf_features);
  CHECK_CONS (otf_features);
  CHECK_SYMBOL (XCAR (otf_features));
  otf_features = XCDR (otf_features);
  for (val = Fcar (otf_features); ! NILP (val);  val = Fcdr (val))
    {
      CHECK_SYMBOL (Fcar (val));
      if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
	error ("Invalid OTF GSUB feature: %s", SYMBOL_NAME (XCAR (val)));
    }
  otf_features = XCDR (otf_features);
  for (val = Fcar (otf_features); ! NILP (val);  val = Fcdr (val))
    {
      CHECK_SYMBOL (Fcar (val));
      if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
	error ("Invalid OTF GPOS feature: %s", SYMBOL_NAME (XCAR (val)));
    }