* dbusbind.c (QCdbus_type_unix_fd): New Lisp object.
[bpt/emacs.git] / src / font.c
CommitLineData
c2f5bfd6 1/* font.c -- "Font" primitives.
537b04b9
GM
2
3Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4Copyright (C) 2006, 2007, 2008, 2009, 2010
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H13PRO009
c2f5bfd6
KH
7
8This file is part of GNU Emacs.
9
9ec0b715 10GNU Emacs is free software: you can redistribute it and/or modify
c2f5bfd6 11it under the terms of the GNU General Public License as published by
9ec0b715
GM
12the Free Software Foundation, either version 3 of the License, or
13(at your option) any later version.
c2f5bfd6
KH
14
15GNU Emacs is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
9ec0b715 21along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
c2f5bfd6
KH
22
23#include <config.h>
24#include <stdio.h>
c2f5bfd6 25#include <ctype.h>
d7306fe6 26#include <setjmp.h>
c2f5bfd6
KH
27
28#include "lisp.h"
29#include "buffer.h"
30#include "frame.h"
10d16101 31#include "window.h"
c2f5bfd6
KH
32#include "dispextern.h"
33#include "charset.h"
34#include "character.h"
35#include "composite.h"
36#include "fontset.h"
37#include "font.h"
38
43c0454d
KH
39#ifdef HAVE_X_WINDOWS
40#include "xterm.h"
41#endif /* HAVE_X_WINDOWS */
42
43#ifdef HAVE_NTGUI
44#include "w32term.h"
45#endif /* HAVE_NTGUI */
46
edfda783
AR
47#ifdef HAVE_NS
48#include "nsterm.h"
49#endif /* HAVE_NS */
50
e0708580
KH
51Lisp_Object Qopentype;
52
35027d0c 53/* Important character set strings. */
9e1bb909 54Lisp_Object Qascii_0, Qiso8859_1, Qiso10646_1, Qunicode_bmp, Qunicode_sip;
1bb1d99b 55
edfda783 56#define DEFAULT_ENCODING Qiso8859_1
edfda783 57
071132a9
KH
58/* Unicode category `Cf'. */
59static Lisp_Object QCf;
60
c2f5bfd6
KH
61/* Special vector of zero length. This is repeatedly used by (struct
62 font_driver *)->list when a specified font is not found. */
35027d0c 63static Lisp_Object null_vector;
c2f5bfd6 64
d0ab1ebe
KH
65static Lisp_Object Vfont_weight_table, Vfont_slant_table, Vfont_width_table;
66
67/* Vector of Vfont_weight_table, Vfont_slant_table, and Vfont_width_table. */
c2f5bfd6
KH
68static Lisp_Object font_style_table;
69
d0ab1ebe
KH
70/* Structure used for tables mapping weight, slant, and width numeric
71 values and their names. */
72
73struct table_entry
74{
75 int numeric;
76 /* The first one is a valid name as a face attribute.
77 The second one (if any) is a typical name in XLFD field. */
5e2327cf 78 const char *names[5];
d0ab1ebe
KH
79};
80
81/* Table of weight numeric values and their names. This table must be
82 sorted by numeric values in ascending order. */
83
5e2327cf 84static const struct table_entry weight_table[] =
d0ab1ebe
KH
85{
86 { 0, { "thin" }},
87 { 20, { "ultra-light", "ultralight" }},
88 { 40, { "extra-light", "extralight" }},
89 { 50, { "light" }},
90 { 75, { "semi-light", "semilight", "demilight", "book" }},
c2694a06 91 { 100, { "normal", "medium", "regular", "unspecified" }},
d0ab1ebe
KH
92 { 180, { "semi-bold", "semibold", "demibold", "demi" }},
93 { 200, { "bold" }},
94 { 205, { "extra-bold", "extrabold" }},
95 { 210, { "ultra-bold", "ultrabold", "black" }}
96};
97
98/* Table of slant numeric values and their names. This table must be
99 sorted by numeric values in ascending order. */
100
5e2327cf 101static const struct table_entry slant_table[] =
d0ab1ebe
KH
102{
103 { 0, { "reverse-oblique", "ro" }},
104 { 10, { "reverse-italic", "ri" }},
c2694a06 105 { 100, { "normal", "r", "unspecified" }},
d0ab1ebe
KH
106 { 200, { "italic" ,"i", "ot" }},
107 { 210, { "oblique", "o" }}
108};
109
110/* Table of width numeric values and their names. This table must be
111 sorted by numeric values in ascending order. */
112
5e2327cf 113static const struct table_entry width_table[] =
d0ab1ebe
KH
114{
115 { 50, { "ultra-condensed", "ultracondensed" }},
116 { 63, { "extra-condensed", "extracondensed" }},
117 { 75, { "condensed", "compressed", "narrow" }},
118 { 87, { "semi-condensed", "semicondensed", "demicondensed" }},
c2694a06 119 { 100, { "normal", "medium", "regular", "unspecified" }},
d0ab1ebe
KH
120 { 113, { "semi-expanded", "semiexpanded", "demiexpanded" }},
121 { 125, { "expanded" }},
122 { 150, { "extra-expanded", "extraexpanded" }},
123 { 200, { "ultra-expanded", "ultraexpanded", "wide" }}
124};
125
35027d0c 126Lisp_Object QCfoundry, QCadstyle, QCregistry;
c2f5bfd6 127/* Symbols representing keys of font extra info. */
35027d0c
KH
128Lisp_Object QCspacing, QCdpi, QCscalable, QCotf, QClang, QCscript, QCavgwidth;
129Lisp_Object QCantialias, QCfont_entity, QCfc_unknown_spec;
ec6fe57c
KH
130/* Symbols representing values of font spacing property. */
131Lisp_Object Qc, Qm, Qp, Qd;
cf702558
CY
132/* Special ADSTYLE properties to avoid fonts used for Latin
133 characters; used in xfont.c and ftfont.c. */
134Lisp_Object Qja, Qko;
c2f5bfd6 135
42707278
JD
136Lisp_Object QCuser_spec;
137
819e81df
KH
138Lisp_Object Vfont_encoding_alist;
139
1701724c
KH
140/* Alist of font registry symbol and the corresponding charsets
141 information. The information is retrieved from
142 Vfont_encoding_alist on demand.
143
144 Eash element has the form:
145 (REGISTRY . (ENCODING-CHARSET-ID . REPERTORY-CHARSET-ID))
146 or
147 (REGISTRY . nil)
148
149 In the former form, ENCODING-CHARSET-ID is an ID of a charset that
150 encodes a character code to a glyph code of a font, and
151 REPERTORY-CHARSET-ID is an ID of a charset that tells if a
152 character is supported by a font.
153
154 The latter form means that the information for REGISTRY couldn't be
155 retrieved. */
156static Lisp_Object font_charset_alist;
157
f697fff0
KH
158/* List of all font drivers. Each font-backend (XXXfont.c) calls
159 register_font_driver in syms_of_XXXfont to register its font-driver
c2f5bfd6
KH
160 here. */
161static struct font_driver_list *font_driver_list;
162
35027d0c
KH
163\f
164
165/* Creaters of font-related Lisp object. */
166
167Lisp_Object
971de7fb 168font_make_spec (void)
35027d0c
KH
169{
170 Lisp_Object font_spec;
171 struct font_spec *spec
172 = ((struct font_spec *)
173 allocate_pseudovector (VECSIZE (struct font_spec),
174 FONT_SPEC_MAX, PVEC_FONT));
175 XSETFONT (font_spec, spec);
176 return font_spec;
177}
178
179Lisp_Object
971de7fb 180font_make_entity (void)
35027d0c
KH
181{
182 Lisp_Object font_entity;
183 struct font_entity *entity
184 = ((struct font_entity *)
185 allocate_pseudovector (VECSIZE (struct font_entity),
186 FONT_ENTITY_MAX, PVEC_FONT));
187 XSETFONT (font_entity, entity);
188 return font_entity;
189}
190
51c13510
KH
191/* Create a font-object whose structure size is SIZE. If ENTITY is
192 not nil, copy properties from ENTITY to the font-object. If
193 PIXELSIZE is positive, set the `size' property to PIXELSIZE. */
35027d0c 194Lisp_Object
971de7fb 195font_make_object (int size, Lisp_Object entity, int pixelsize)
35027d0c
KH
196{
197 Lisp_Object font_object;
198 struct font *font
199 = (struct font *) allocate_pseudovector (size, FONT_OBJECT_MAX, PVEC_FONT);
51c13510
KH
200 int i;
201
35027d0c
KH
202 XSETFONT (font_object, font);
203
51c13510
KH
204 if (! NILP (entity))
205 {
206 for (i = 1; i < FONT_SPEC_MAX; i++)
207 font->props[i] = AREF (entity, i);
208 if (! NILP (AREF (entity, FONT_EXTRA_INDEX)))
209 font->props[FONT_EXTRA_INDEX]
581e51e8 210 = Fcopy_alist (AREF (entity, FONT_EXTRA_INDEX));
51c13510
KH
211 }
212 if (size > 0)
213 font->props[FONT_SIZE_INDEX] = make_number (pixelsize);
35027d0c
KH
214 return font_object;
215}
216
217\f
218
f57e2426
J
219static int font_pixel_size (FRAME_PTR f, Lisp_Object);
220static Lisp_Object font_open_entity (FRAME_PTR, Lisp_Object, int);
221static Lisp_Object font_matching_entity (FRAME_PTR, Lisp_Object *,
222 Lisp_Object);
c2f5bfd6
KH
223
224/* Number of registered font drivers. */
225static int num_font_drivers;
226
35027d0c
KH
227
228/* Return a Lispy value of a font property value at STR and LEN bytes.
229 If STR is "*", it returns nil.
2f286d4f
KH
230 If FORCE_SYMBOL is zero and all characters in STR are digits, it
231 returns an integer. Otherwise, it returns a symbol interned from
232 STR. */
35027d0c
KH
233
234Lisp_Object
675e2c69 235font_intern_prop (const char *str, int len, int force_symbol)
35027d0c
KH
236{
237 int i;
d0ab1ebe 238 Lisp_Object tem;
35027d0c 239 Lisp_Object obarray;
14162469 240 EMACS_INT nbytes, nchars;
35027d0c
KH
241
242 if (len == 1 && *str == '*')
243 return Qnil;
2f286d4f 244 if (!force_symbol && len >=1 && isdigit (*str))
35027d0c
KH
245 {
246 for (i = 1; i < len; i++)
247 if (! isdigit (str[i]))
248 break;
249 if (i == len)
250 return make_number (atoi (str));
251 }
252
545312c2
KH
253 /* The following code is copied from the function intern (in
254 lread.c), and modified to suite our purpose. */
35027d0c
KH
255 obarray = Vobarray;
256 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
257 obarray = check_obarray (obarray);
72d36834 258 parse_str_as_multibyte ((unsigned char *) str, len, &nchars, &nbytes);
545312c2
KH
259 if (len == nchars || len != nbytes)
260 /* CONTENTS contains no multibyte sequences or contains an invalid
261 multibyte sequence. We'll make a unibyte string. */
262 tem = oblookup (obarray, str, len, len);
263 else
264 tem = oblookup (obarray, str, nchars, len);
35027d0c
KH
265 if (SYMBOLP (tem))
266 return tem;
545312c2
KH
267 if (len == nchars || len != nbytes)
268 tem = make_unibyte_string (str, len);
269 else
270 tem = make_multibyte_string (str, nchars, len);
271 return Fintern (tem, obarray);
35027d0c
KH
272}
273
9331887d 274/* Return a pixel size of font-spec SPEC on frame F. */
ec6fe57c 275
9331887d 276static int
971de7fb 277font_pixel_size (FRAME_PTR f, Lisp_Object spec)
9331887d 278{
819e81df 279#ifdef HAVE_WINDOW_SYSTEM
9331887d
KH
280 Lisp_Object size = AREF (spec, FONT_SIZE_INDEX);
281 double point_size;
35027d0c 282 int dpi, pixel_size;
d0ab1ebe 283 Lisp_Object val;
51c01100 284
9331887d
KH
285 if (INTEGERP (size))
286 return XINT (size);
287 if (NILP (size))
819e81df 288 return 0;
d0ab1ebe 289 font_assert (FLOATP (size));
9331887d 290 point_size = XFLOAT_DATA (size);
35027d0c
KH
291 val = AREF (spec, FONT_DPI_INDEX);
292 if (INTEGERP (val))
17e28f6d 293 dpi = XINT (val);
9331887d
KH
294 else
295 dpi = f->resy;
296 pixel_size = POINT_TO_PIXEL (point_size, dpi);
297 return pixel_size;
819e81df
KH
298#else
299 return 1;
300#endif
9331887d
KH
301}
302
c2f5bfd6 303
35027d0c
KH
304/* Return a value of PROP's VAL (symbol or integer) to be stored in a
305 font vector. If VAL is not valid (i.e. not registered in
306 font_style_table), return -1 if NOERROR is zero, and return a
307 proper index if NOERROR is nonzero. In that case, register VAL in
308 font_style_table if VAL is a symbol, and return a closest index if
309 VAL is an integer. */
c2f5bfd6 310
35027d0c 311int
971de7fb 312font_style_to_value (enum font_property_index prop, Lisp_Object val, int noerror)
c2f5bfd6 313{
35027d0c
KH
314 Lisp_Object table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
315 int len = ASIZE (table);
d0ab1ebe 316 int i, j;
c2f5bfd6 317
35027d0c 318 if (SYMBOLP (val))
c2f5bfd6 319 {
72606e45 320 unsigned char *s;
35027d0c
KH
321 Lisp_Object args[2], elt;
322
323 /* At first try exact match. */
324 for (i = 0; i < len; i++)
d0ab1ebe
KH
325 for (j = 1; j < ASIZE (AREF (table, i)); j++)
326 if (EQ (val, AREF (AREF (table, i), j)))
327 return ((XINT (AREF (AREF (table, i), 0)) << 8)
328 | (i << 4) | (j - 1));
35027d0c 329 /* Try also with case-folding match. */
72606e45 330 s = SDATA (SYMBOL_NAME (val));
35027d0c 331 for (i = 0; i < len; i++)
d0ab1ebe
KH
332 for (j = 1; j < ASIZE (AREF (table, i)); j++)
333 {
334 elt = AREF (AREF (table, i), j);
72606e45 335 if (xstrcasecmp (s, SDATA (SYMBOL_NAME (elt))) == 0)
d0ab1ebe
KH
336 return ((XINT (AREF (AREF (table, i), 0)) << 8)
337 | (i << 4) | (j - 1));
338 }
35027d0c
KH
339 if (! noerror)
340 return -1;
341 if (len == 255)
342 abort ();
c2694a06 343 elt = Fmake_vector (make_number (2), make_number (100));
d0ab1ebe 344 ASET (elt, 1, val);
35027d0c 345 args[0] = table;
d0ab1ebe 346 args[1] = Fmake_vector (make_number (1), elt);
35027d0c 347 ASET (font_style_table, prop - FONT_WEIGHT_INDEX, Fvconcat (2, args));
c2694a06 348 return (100 << 8) | (i << 4);
c2f5bfd6 349 }
35027d0c
KH
350 else
351 {
d0ab1ebe 352 int i, last_n;
35027d0c 353 int numeric = XINT (val);
c2f5bfd6 354
d0ab1ebe 355 for (i = 0, last_n = -1; i < len; i++)
35027d0c 356 {
d0ab1ebe 357 int n = XINT (AREF (AREF (table, i), 0));
c2f5bfd6 358
35027d0c 359 if (numeric == n)
d0ab1ebe 360 return (n << 8) | (i << 4);
35027d0c
KH
361 if (numeric < n)
362 {
363 if (! noerror)
364 return -1;
d0ab1ebe
KH
365 return ((i == 0 || n - numeric < numeric - last_n)
366 ? (n << 8) | (i << 4): (last_n << 8 | ((i - 1) << 4)));
35027d0c 367 }
35027d0c 368 last_n = n;
35027d0c
KH
369 }
370 if (! noerror)
371 return -1;
d0ab1ebe 372 return ((last_n << 8) | ((i - 1) << 4));
35027d0c
KH
373 }
374}
c2f5bfd6
KH
375
376Lisp_Object
971de7fb 377font_style_symbolic (Lisp_Object font, enum font_property_index prop, int for_face)
c2f5bfd6 378{
3ef8c1b4 379 Lisp_Object val = AREF (font, prop);
d0ab1ebe
KH
380 Lisp_Object table, elt;
381 int i;
c2f5bfd6 382
35027d0c
KH
383 if (NILP (val))
384 return Qnil;
385 table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
d0ab1ebe
KH
386 i = XINT (val) & 0xFF;
387 font_assert (((i >> 4) & 0xF) < ASIZE (table));
388 elt = AREF (table, ((i >> 4) & 0xF));
389 font_assert ((i & 0xF) + 1 < ASIZE (elt));
ba3de0e8 390 return (for_face ? AREF (elt, 1) : AREF (elt, (i & 0xF) + 1));
6fe9826d
JR
391}
392
819e81df
KH
393/* Return ENCODING or a cons of ENCODING and REPERTORY of the font
394 FONTNAME. ENCODING is a charset symbol that specifies the encoding
395 of the font. REPERTORY is a charset symbol or nil. */
396
397Lisp_Object
971de7fb 398find_font_encoding (Lisp_Object fontname)
819e81df
KH
399{
400 Lisp_Object tail, elt;
401
402 for (tail = Vfont_encoding_alist; CONSP (tail); tail = XCDR (tail))
403 {
404 elt = XCAR (tail);
405 if (CONSP (elt)
406 && STRINGP (XCAR (elt))
407 && fast_string_match_ignore_case (XCAR (elt), fontname) >= 0
408 && (SYMBOLP (XCDR (elt))
409 ? CHARSETP (XCDR (elt))
410 : CONSP (XCDR (elt)) && CHARSETP (XCAR (XCDR (elt)))))
411 return (XCDR (elt));
412 }
182ba28c 413 return Qnil;
819e81df
KH
414}
415
1701724c
KH
416/* Return encoding charset and repertory charset for REGISTRY in
417 ENCODING and REPERTORY correspondingly. If correct information for
418 REGISTRY is available, return 0. Otherwise return -1. */
419
420int
971de7fb 421font_registry_charsets (Lisp_Object registry, struct charset **encoding, struct charset **repertory)
1701724c
KH
422{
423 Lisp_Object val;
424 int encoding_id, repertory_id;
425
35027d0c 426 val = Fassoc_string (registry, font_charset_alist, Qt);
1701724c
KH
427 if (! NILP (val))
428 {
429 val = XCDR (val);
430 if (NILP (val))
431 return -1;
432 encoding_id = XINT (XCAR (val));
433 repertory_id = XINT (XCDR (val));
434 }
435 else
436 {
437 val = find_font_encoding (SYMBOL_NAME (registry));
438 if (SYMBOLP (val) && CHARSETP (val))
439 {
440 encoding_id = repertory_id = XINT (CHARSET_SYMBOL_ID (val));
441 }
442 else if (CONSP (val))
443 {
444 if (! CHARSETP (XCAR (val)))
445 goto invalid_entry;
446 encoding_id = XINT (CHARSET_SYMBOL_ID (XCAR (val)));
447 if (NILP (XCDR (val)))
448 repertory_id = -1;
449 else
450 {
451 if (! CHARSETP (XCDR (val)))
452 goto invalid_entry;
453 repertory_id = XINT (CHARSET_SYMBOL_ID (XCDR (val)));
454 }
51c01100 455 }
1701724c
KH
456 else
457 goto invalid_entry;
458 val = Fcons (make_number (encoding_id), make_number (repertory_id));
459 font_charset_alist
460 = nconc2 (font_charset_alist, Fcons (Fcons (registry, val), Qnil));
461 }
462
463 if (encoding)
464 *encoding = CHARSET_FROM_ID (encoding_id);
465 if (repertory)
466 *repertory = repertory_id >= 0 ? CHARSET_FROM_ID (repertory_id) : NULL;
467 return 0;
468
469 invalid_entry:
470 font_charset_alist
471 = nconc2 (font_charset_alist, Fcons (Fcons (registry, Qnil), Qnil));
472 return -1;
473}
474
c2f5bfd6 475\f
45eb10fb
KH
476/* Font property value validaters. See the comment of
477 font_property_table for the meaning of the arguments. */
478
f57e2426
J
479static Lisp_Object font_prop_validate (int, Lisp_Object, Lisp_Object);
480static Lisp_Object font_prop_validate_symbol (Lisp_Object, Lisp_Object);
481static Lisp_Object font_prop_validate_style (Lisp_Object, Lisp_Object);
482static Lisp_Object font_prop_validate_non_neg (Lisp_Object, Lisp_Object);
483static Lisp_Object font_prop_validate_spacing (Lisp_Object, Lisp_Object);
484static int get_font_prop_index (Lisp_Object);
c2f5bfd6
KH
485
486static Lisp_Object
971de7fb 487font_prop_validate_symbol (Lisp_Object prop, Lisp_Object val)
c2f5bfd6
KH
488{
489 if (STRINGP (val))
35027d0c
KH
490 val = Fintern (val, Qnil);
491 if (! SYMBOLP (val))
c2f5bfd6 492 val = Qerror;
35027d0c
KH
493 else if (EQ (prop, QCregistry))
494 val = Fintern (Fdowncase (SYMBOL_NAME (val)), Qnil);
c2f5bfd6
KH
495 return val;
496}
497
35027d0c 498
c2f5bfd6 499static Lisp_Object
971de7fb 500font_prop_validate_style (Lisp_Object style, Lisp_Object val)
c2f5bfd6 501{
35027d0c
KH
502 enum font_property_index prop = (EQ (style, QCweight) ? FONT_WEIGHT_INDEX
503 : EQ (style, QCslant) ? FONT_SLANT_INDEX
504 : FONT_WIDTH_INDEX);
505 int n;
506 if (INTEGERP (val))
c2f5bfd6 507 {
35027d0c 508 n = XINT (val);
64b900e3 509 if (((n >> 4) & 0xF)
35027d0c 510 >= ASIZE (AREF (font_style_table, prop - FONT_WEIGHT_INDEX)))
c2f5bfd6
KH
511 val = Qerror;
512 else
513 {
64b900e3
KH
514 Lisp_Object elt = AREF (AREF (font_style_table, prop - FONT_WEIGHT_INDEX), (n >> 4) & 0xF);
515
516 if ((n & 0xF) + 1 >= ASIZE (elt))
517 val = Qerror;
518 else if (XINT (AREF (elt, 0)) != (n >> 8))
c2f5bfd6
KH
519 val = Qerror;
520 }
521 }
35027d0c
KH
522 else if (SYMBOLP (val))
523 {
524 int n = font_style_to_value (prop, val, 0);
525
526 val = n >= 0 ? make_number (n) : Qerror;
527 }
528 else
529 val = Qerror;
c2f5bfd6
KH
530 return val;
531}
532
533static Lisp_Object
971de7fb 534font_prop_validate_non_neg (Lisp_Object prop, Lisp_Object val)
c2f5bfd6
KH
535{
536 return (NATNUMP (val) || (FLOATP (val) && XFLOAT_DATA (val) >= 0)
537 ? val : Qerror);
538}
539
540static Lisp_Object
971de7fb 541font_prop_validate_spacing (Lisp_Object prop, Lisp_Object val)
ec6fe57c
KH
542{
543 if (NILP (val) || (NATNUMP (val) && XINT (val) <= FONT_SPACING_CHARCELL))
544 return val;
3692570f
KH
545 if (SYMBOLP (val) && SBYTES (SYMBOL_NAME (val)) == 1)
546 {
547 char spacing = SDATA (SYMBOL_NAME (val))[0];
548
549 if (spacing == 'c' || spacing == 'C')
550 return make_number (FONT_SPACING_CHARCELL);
551 if (spacing == 'm' || spacing == 'M')
552 return make_number (FONT_SPACING_MONO);
0615d903 553 if (spacing == 'p' || spacing == 'P')
3692570f
KH
554 return make_number (FONT_SPACING_PROPORTIONAL);
555 if (spacing == 'd' || spacing == 'D')
556 return make_number (FONT_SPACING_DUAL);
557 }
ec6fe57c
KH
558 return Qerror;
559}
560
1701724c 561static Lisp_Object
971de7fb 562font_prop_validate_otf (Lisp_Object prop, Lisp_Object val)
1701724c
KH
563{
564 Lisp_Object tail, tmp;
565 int i;
566
567 /* VAL = (SCRIPT [ LANGSYS [ GSUB-FEATURES [ GPOS-FEATURES ]]])
568 GSUB-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil
569 GPOS-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil */
570 if (! CONSP (val))
571 return Qerror;
572 if (! SYMBOLP (XCAR (val)))
573 return Qerror;
574 tail = XCDR (val);
575 if (NILP (tail))
576 return val;
577 if (! CONSP (tail) || ! SYMBOLP (XCAR (val)))
578 return Qerror;
579 for (i = 0; i < 2; i++)
580 {
581 tail = XCDR (tail);
582 if (NILP (tail))
583 return val;
584 if (! CONSP (tail))
585 return Qerror;
586 for (tmp = XCAR (tail); CONSP (tmp); tmp = XCDR (tmp))
587 if (! SYMBOLP (XCAR (tmp)))
588 return Qerror;
589 if (! NILP (tmp))
590 return Qerror;
591 }
592 return val;
593}
594
ec6fe57c
KH
595/* Structure of known font property keys and validater of the
596 values. */
04bab72c 597static const struct
c2f5bfd6 598{
ec6fe57c 599 /* Pointer to the key symbol. */
c2f5bfd6 600 Lisp_Object *key;
45eb10fb
KH
601 /* Function to validate PROP's value VAL, or NULL if any value is
602 ok. The value is VAL or its regularized value if VAL is valid,
603 and Qerror if not. */
f57e2426 604 Lisp_Object (*validater) (Lisp_Object prop, Lisp_Object val);
ec6fe57c
KH
605} font_property_table[] =
606 { { &QCtype, font_prop_validate_symbol },
c2f5bfd6
KH
607 { &QCfoundry, font_prop_validate_symbol },
608 { &QCfamily, font_prop_validate_symbol },
609 { &QCadstyle, font_prop_validate_symbol },
610 { &QCregistry, font_prop_validate_symbol },
611 { &QCweight, font_prop_validate_style },
612 { &QCslant, font_prop_validate_style },
613 { &QCwidth, font_prop_validate_style },
ec6fe57c 614 { &QCsize, font_prop_validate_non_neg },
ec6fe57c
KH
615 { &QCdpi, font_prop_validate_non_neg },
616 { &QCspacing, font_prop_validate_spacing },
35027d0c
KH
617 { &QCavgwidth, font_prop_validate_non_neg },
618 /* The order of the above entries must match with enum
619 font_property_index. */
620 { &QClang, font_prop_validate_symbol },
621 { &QCscript, font_prop_validate_symbol },
622 { &QCotf, font_prop_validate_otf }
c2f5bfd6
KH
623 };
624
45eb10fb 625/* Size (number of elements) of the above table. */
ec6fe57c
KH
626#define FONT_PROPERTY_TABLE_SIZE \
627 ((sizeof font_property_table) / (sizeof *font_property_table))
628
45eb10fb 629/* Return an index number of font property KEY or -1 if KEY is not an
35027d0c 630 already known property. */
45eb10fb 631
ec6fe57c 632static int
971de7fb 633get_font_prop_index (Lisp_Object key)
c2f5bfd6 634{
35027d0c
KH
635 int i;
636
637 for (i = 0; i < FONT_PROPERTY_TABLE_SIZE; i++)
638 if (EQ (key, *font_property_table[i].key))
639 return i;
ec6fe57c 640 return -1;
c2f5bfd6
KH
641}
642
35027d0c
KH
643/* Validate the font property. The property key is specified by the
644 symbol PROP, or the index IDX (if PROP is nil). If VAL is invalid,
645 signal an error. The value is VAL or the regularized one. */
45eb10fb 646
c2f5bfd6 647static Lisp_Object
971de7fb 648font_prop_validate (int idx, Lisp_Object prop, Lisp_Object val)
c2f5bfd6 649{
35027d0c 650 Lisp_Object validated;
c2f5bfd6 651
1f09f444
KH
652 if (NILP (val))
653 return val;
35027d0c
KH
654 if (NILP (prop))
655 prop = *font_property_table[idx].key;
656 else
ec6fe57c 657 {
35027d0c
KH
658 idx = get_font_prop_index (prop);
659 if (idx < 0)
660 return val;
ec6fe57c 661 }
35027d0c
KH
662 validated = (font_property_table[idx].validater) (prop, val);
663 if (EQ (validated, Qerror))
664 signal_error ("invalid font property", Fcons (prop, val));
665 return validated;
c2f5bfd6 666}
51c01100 667
35027d0c
KH
668
669/* Store VAL as a value of extra font property PROP in FONT while
670 keeping the sorting order. Don't check the validity of VAL. */
45eb10fb 671
01dbeb0b 672Lisp_Object
971de7fb 673font_put_extra (Lisp_Object font, Lisp_Object prop, Lisp_Object val)
9331887d
KH
674{
675 Lisp_Object extra = AREF (font, FONT_EXTRA_INDEX);
ec6fe57c 676 Lisp_Object slot = (NILP (extra) ? Qnil : assq_no_quit (prop, extra));
9331887d
KH
677
678 if (NILP (slot))
679 {
35027d0c
KH
680 Lisp_Object prev = Qnil;
681
682 while (CONSP (extra)
683 && NILP (Fstring_lessp (prop, XCAR (XCAR (extra)))))
684 prev = extra, extra = XCDR (extra);
581e51e8 685
ce75f06e 686 if (NILP (prev))
581e51e8 687 ASET (font, FONT_EXTRA_INDEX, Fcons (Fcons (prop, val), extra));
ce75f06e 688 else
581e51e8 689 XSETCDR (prev, Fcons (Fcons (prop, val), extra));
ce75f06e 690
ec6fe57c 691 return val;
9331887d 692 }
9331887d 693 XSETCDR (slot, val);
483670b5
KH
694 if (NILP (val))
695 ASET (font, FONT_EXTRA_INDEX, Fdelq (slot, extra));
ec6fe57c 696 return val;
9331887d
KH
697}
698
c2f5bfd6
KH
699\f
700/* Font name parser and unparser */
701
8ea90aa3 702static int parse_matrix (const char *);
f57e2426 703static int font_expand_wildcards (Lisp_Object *, int);
09d93395 704static int font_parse_name (char *, Lisp_Object);
c2f5bfd6 705
ec6fe57c 706/* An enumerator for each field of an XLFD font name. */
c2f5bfd6
KH
707enum xlfd_field_index
708{
709 XLFD_FOUNDRY_INDEX,
710 XLFD_FAMILY_INDEX,
711 XLFD_WEIGHT_INDEX,
712 XLFD_SLANT_INDEX,
713 XLFD_SWIDTH_INDEX,
714 XLFD_ADSTYLE_INDEX,
4485a28e
KH
715 XLFD_PIXEL_INDEX,
716 XLFD_POINT_INDEX,
c2f5bfd6
KH
717 XLFD_RESX_INDEX,
718 XLFD_RESY_INDEX,
719 XLFD_SPACING_INDEX,
720 XLFD_AVGWIDTH_INDEX,
721 XLFD_REGISTRY_INDEX,
722 XLFD_ENCODING_INDEX,
723 XLFD_LAST_INDEX
724};
725
ec6fe57c 726/* An enumerator for mask bit corresponding to each XLFD field. */
4485a28e
KH
727enum xlfd_field_mask
728{
729 XLFD_FOUNDRY_MASK = 0x0001,
730 XLFD_FAMILY_MASK = 0x0002,
731 XLFD_WEIGHT_MASK = 0x0004,
732 XLFD_SLANT_MASK = 0x0008,
733 XLFD_SWIDTH_MASK = 0x0010,
734 XLFD_ADSTYLE_MASK = 0x0020,
735 XLFD_PIXEL_MASK = 0x0040,
736 XLFD_POINT_MASK = 0x0080,
737 XLFD_RESX_MASK = 0x0100,
738 XLFD_RESY_MASK = 0x0200,
739 XLFD_SPACING_MASK = 0x0400,
740 XLFD_AVGWIDTH_MASK = 0x0800,
741 XLFD_REGISTRY_MASK = 0x1000,
742 XLFD_ENCODING_MASK = 0x2000
743};
744
745
c2f5bfd6
KH
746/* Parse P pointing the pixel/point size field of the form
747 `[A B C D]' which specifies a transformation matrix:
748
749 A B 0
750 C D 0
751 0 0 1
752
753 by which all glyphs of the font are transformed. The spec says
754 that scalar value N for the pixel/point size is equivalent to:
755 A = N * resx/resy, B = C = 0, D = N.
756
757 Return the scalar value N if the form is valid. Otherwise return
758 -1. */
759
760static int
8ea90aa3 761parse_matrix (const char *p)
c2f5bfd6
KH
762{
763 double matrix[4];
764 char *end;
765 int i;
766
767 for (i = 0, p++; i < 4 && *p && *p != ']'; i++)
768 {
769 if (*p == '~')
770 matrix[i] = - strtod (p + 1, &end);
771 else
772 matrix[i] = strtod (p, &end);
773 p = end;
774 }
775 return (i == 4 ? (int) matrix[3] : -1);
776}
777
4485a28e
KH
778/* Expand a wildcard field in FIELD (the first N fields are filled) to
779 multiple fields to fill in all 14 XLFD fields while restring a
780 field position by its contents. */
781
ec6fe57c 782static int
971de7fb 783font_expand_wildcards (Lisp_Object *field, int n)
4485a28e
KH
784{
785 /* Copy of FIELD. */
786 Lisp_Object tmp[XLFD_LAST_INDEX];
787 /* Array of information about where this element can go. Nth
788 element is for Nth element of FIELD. */
789 struct {
790 /* Minimum possible field. */
791 int from;
792 /* Maxinum possible field. */
793 int to;
794 /* Bit mask of possible field. Nth bit corresponds to Nth field. */
795 int mask;
796 } range[XLFD_LAST_INDEX];
797 int i, j;
ec6fe57c 798 int range_from, range_to;
4485a28e
KH
799 unsigned range_mask;
800
801#define XLFD_SYMBOL_MASK (XLFD_FOUNDRY_MASK | XLFD_FAMILY_MASK \
802 | XLFD_ADSTYLE_MASK | XLFD_REGISTRY_MASK)
803#define XLFD_NULL_MASK (XLFD_FOUNDRY_MASK | XLFD_ADSTYLE_MASK)
4485a28e 804#define XLFD_LARGENUM_MASK (XLFD_POINT_MASK | XLFD_RESX_MASK | XLFD_RESY_MASK \
ef18374f 805 | XLFD_AVGWIDTH_MASK)
4485a28e
KH
806#define XLFD_REGENC_MASK (XLFD_REGISTRY_MASK | XLFD_ENCODING_MASK)
807
808 /* Initialize RANGE_MASK for FIELD[0] which can be 0th to (14 - N)th
809 field. The value is shifted to left one bit by one in the
810 following loop. */
811 for (i = 0, range_mask = 0; i <= 14 - n; i++)
812 range_mask = (range_mask << 1) | 1;
813
ec6fe57c
KH
814 /* The triplet RANGE_FROM, RANGE_TO, and RANGE_MASK is a
815 position-based retriction for FIELD[I]. */
816 for (i = 0, range_from = 0, range_to = 14 - n; i < n;
817 i++, range_from++, range_to++, range_mask <<= 1)
4485a28e 818 {
4485a28e
KH
819 Lisp_Object val = field[i];
820
821 tmp[i] = val;
822 if (NILP (val))
823 {
824 /* Wildcard. */
825 range[i].from = range_from;
826 range[i].to = range_to;
827 range[i].mask = range_mask;
828 }
829 else
830 {
831 /* The triplet FROM, TO, and MASK is a value-based
832 retriction for FIELD[I]. */
833 int from, to;
834 unsigned mask;
835
836 if (INTEGERP (val))
837 {
838 int numeric = XINT (val);
839
ef18374f
KH
840 if (i + 1 == n)
841 from = to = XLFD_ENCODING_INDEX,
842 mask = XLFD_ENCODING_MASK;
ec6fe57c
KH
843 else if (numeric == 0)
844 from = XLFD_PIXEL_INDEX, to = XLFD_AVGWIDTH_INDEX,
845 mask = XLFD_PIXEL_MASK | XLFD_LARGENUM_MASK;
ef18374f
KH
846 else if (numeric <= 48)
847 from = to = XLFD_PIXEL_INDEX,
848 mask = XLFD_PIXEL_MASK;
51c01100 849 else
ec6fe57c 850 from = XLFD_POINT_INDEX, to = XLFD_AVGWIDTH_INDEX,
4485a28e
KH
851 mask = XLFD_LARGENUM_MASK;
852 }
35027d0c 853 else if (SBYTES (SYMBOL_NAME (val)) == 0)
4485a28e
KH
854 from = XLFD_FOUNDRY_INDEX, to = XLFD_ADSTYLE_INDEX,
855 mask = XLFD_NULL_MASK;
856 else if (i == 0)
857 from = to = XLFD_FOUNDRY_INDEX, mask = XLFD_FOUNDRY_MASK;
858 else if (i + 1 == n)
859 {
860 Lisp_Object name = SYMBOL_NAME (val);
861
862 if (SDATA (name)[SBYTES (name) - 1] == '*')
863 from = XLFD_REGISTRY_INDEX, to = XLFD_ENCODING_INDEX,
864 mask = XLFD_REGENC_MASK;
865 else
866 from = to = XLFD_ENCODING_INDEX,
867 mask = XLFD_ENCODING_MASK;
868 }
ef18374f
KH
869 else if (range_from <= XLFD_WEIGHT_INDEX
870 && range_to >= XLFD_WEIGHT_INDEX
35027d0c 871 && FONT_WEIGHT_NAME_NUMERIC (val) >= 0)
4485a28e 872 from = to = XLFD_WEIGHT_INDEX, mask = XLFD_WEIGHT_MASK;
ef18374f
KH
873 else if (range_from <= XLFD_SLANT_INDEX
874 && range_to >= XLFD_SLANT_INDEX
35027d0c 875 && FONT_SLANT_NAME_NUMERIC (val) >= 0)
4485a28e 876 from = to = XLFD_SLANT_INDEX, mask = XLFD_SLANT_MASK;
ef18374f
KH
877 else if (range_from <= XLFD_SWIDTH_INDEX
878 && range_to >= XLFD_SWIDTH_INDEX
35027d0c 879 && FONT_WIDTH_NAME_NUMERIC (val) >= 0)
4485a28e
KH
880 from = to = XLFD_SWIDTH_INDEX, mask = XLFD_SWIDTH_MASK;
881 else
882 {
ec6fe57c 883 if (EQ (val, Qc) || EQ (val, Qm) || EQ (val, Qp) || EQ (val, Qd))
4485a28e
KH
884 from = to = XLFD_SPACING_INDEX, mask = XLFD_SPACING_MASK;
885 else
886 from = XLFD_FOUNDRY_INDEX, to = XLFD_ENCODING_INDEX,
887 mask = XLFD_SYMBOL_MASK;
888 }
889
890 /* Merge position-based and value-based restrictions. */
891 mask &= range_mask;
892 while (from < range_from)
893 mask &= ~(1 << from++);
894 while (from < 14 && ! (mask & (1 << from)))
895 from++;
896 while (to > range_to)
897 mask &= ~(1 << to--);
898 while (to >= 0 && ! (mask & (1 << to)))
899 to--;
900 if (from > to)
901 return -1;
902 range[i].from = from;
903 range[i].to = to;
904 range[i].mask = mask;
905
906 if (from > range_from || to < range_to)
ec6fe57c
KH
907 {
908 /* The range is narrowed by value-based restrictions.
909 Reflect it to the other fields. */
910
911 /* Following fields should be after FROM. */
912 range_from = from;
913 /* Preceding fields should be before TO. */
914 for (j = i - 1, from--, to--; j >= 0; j--, from--, to--)
915 {
916 /* Check FROM for non-wildcard field. */
917 if (! NILP (tmp[j]) && range[j].from < from)
918 {
919 while (range[j].from < from)
920 range[j].mask &= ~(1 << range[j].from++);
921 while (from < 14 && ! (range[j].mask & (1 << from)))
922 from++;
923 range[j].from = from;
924 }
925 else
926 from = range[j].from;
927 if (range[j].to > to)
928 {
929 while (range[j].to > to)
930 range[j].mask &= ~(1 << range[j].to--);
931 while (to >= 0 && ! (range[j].mask & (1 << to)))
932 to--;
933 range[j].to = to;
934 }
935 else
936 to = range[j].to;
937 if (from > to)
938 return -1;
939 }
940 }
4485a28e
KH
941 }
942 }
943
944 /* Decide all fileds from restrictions in RANGE. */
945 for (i = j = 0; i < n ; i++)
946 {
947 if (j < range[i].from)
948 {
949 if (i == 0 || ! NILP (tmp[i - 1]))
950 /* None of TMP[X] corresponds to Jth field. */
951 return -1;
952 for (; j < range[i].from; j++)
953 field[j] = Qnil;
954 }
955 field[j++] = tmp[i];
956 }
957 if (! NILP (tmp[n - 1]) && j < XLFD_REGISTRY_INDEX)
958 return -1;
959 for (; j < XLFD_LAST_INDEX; j++)
960 field[j] = Qnil;
961 if (INTEGERP (field[XLFD_ENCODING_INDEX]))
962 field[XLFD_ENCODING_INDEX]
963 = Fintern (Fnumber_to_string (field[XLFD_ENCODING_INDEX]), Qnil);
964 return 0;
965}
966
43a1d19b 967
ef18374f 968/* Parse NAME (null terminated) as XLFD and store information in FONT
9331887d
KH
969 (font-spec or font-entity). Size property of FONT is set as
970 follows:
971 specified XLFD fields FONT property
972 --------------------- -------------
973 PIXEL_SIZE PIXEL_SIZE (Lisp integer)
974 POINT_SIZE and RESY calculated pixel size (Lisp integer)
975 POINT_SIZE POINT_SIZE/10 (Lisp float)
976
ec6fe57c 977 If NAME is successfully parsed, return 0. Otherwise return -1.
9331887d 978
ec6fe57c
KH
979 FONT is usually a font-spec, but when this function is called from
980 X font backend driver, it is a font-entity. In that case, NAME is
35027d0c 981 a fully specified XLFD. */
c2f5bfd6
KH
982
983int
09d93395 984font_parse_xlfd (char *name, Lisp_Object font)
c2f5bfd6
KH
985{
986 int len = strlen (name);
35027d0c 987 int i, j, n;
cf23b845 988 char *f[XLFD_LAST_INDEX + 1];
c2f5bfd6 989 Lisp_Object val;
4485a28e 990 char *p;
c2f5bfd6 991
e3928081 992 if (len > 255 || !len)
c2f5bfd6
KH
993 /* Maximum XLFD name length is 255. */
994 return -1;
ec6fe57c 995 /* Accept "*-.." as a fully specified XLFD. */
e3928081 996 if (name[0] == '*' && (len == 1 || name[1] == '-'))
ec6fe57c
KH
997 i = 1, f[XLFD_FOUNDRY_INDEX] = name;
998 else
999 i = 0;
1000 for (p = name + i; *p; p++)
35027d0c
KH
1001 if (*p == '-')
1002 {
1003 f[i++] = p + 1;
1004 if (i == XLFD_LAST_INDEX)
1005 break;
1006 }
1007 f[i] = name + len;
c2f5bfd6 1008
2f286d4f
KH
1009#define INTERN_FIELD(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 0)
1010#define INTERN_FIELD_SYM(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 1)
4485a28e 1011
ec6fe57c 1012 if (i == XLFD_LAST_INDEX)
4485a28e 1013 {
35027d0c 1014 /* Fully specified XLFD. */
ec6fe57c
KH
1015 int pixel_size;
1016
2f286d4f
KH
1017 ASET (font, FONT_FOUNDRY_INDEX, INTERN_FIELD_SYM (XLFD_FOUNDRY_INDEX));
1018 ASET (font, FONT_FAMILY_INDEX, INTERN_FIELD_SYM (XLFD_FAMILY_INDEX));
35027d0c
KH
1019 for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
1020 i <= XLFD_SWIDTH_INDEX; i++, j++)
4485a28e 1021 {
2f286d4f 1022 val = INTERN_FIELD_SYM (i);
ec6fe57c 1023 if (! NILP (val))
4485a28e 1024 {
2f286d4f 1025 if ((n = font_style_to_value (j, INTERN_FIELD_SYM (i), 0)) < 0)
35027d0c
KH
1026 return -1;
1027 ASET (font, j, make_number (n));
ec6fe57c
KH
1028 }
1029 }
2f286d4f 1030 ASET (font, FONT_ADSTYLE_INDEX, INTERN_FIELD_SYM (XLFD_ADSTYLE_INDEX));
35027d0c
KH
1031 if (strcmp (f[XLFD_REGISTRY_INDEX], "*-*") == 0)
1032 ASET (font, FONT_REGISTRY_INDEX, Qnil);
1033 else
1034 ASET (font, FONT_REGISTRY_INDEX,
1035 font_intern_prop (f[XLFD_REGISTRY_INDEX],
2f286d4f
KH
1036 f[XLFD_LAST_INDEX] - f[XLFD_REGISTRY_INDEX],
1037 1));
ec6fe57c
KH
1038 p = f[XLFD_PIXEL_INDEX];
1039 if (*p == '[' && (pixel_size = parse_matrix (p)) >= 0)
51c01100 1040 ASET (font, FONT_SIZE_INDEX, make_number (pixel_size));
ec6fe57c
KH
1041 else
1042 {
35027d0c
KH
1043 val = INTERN_FIELD (XLFD_PIXEL_INDEX);
1044 if (INTEGERP (val))
ec6fe57c 1045 ASET (font, FONT_SIZE_INDEX, val);
b57d9029
KH
1046 else if (FONT_ENTITY_P (font))
1047 return -1;
ec6fe57c
KH
1048 else
1049 {
1050 double point_size = -1;
1051
d0ab1ebe 1052 font_assert (FONT_SPEC_P (font));
ec6fe57c
KH
1053 p = f[XLFD_POINT_INDEX];
1054 if (*p == '[')
1055 point_size = parse_matrix (p);
1056 else if (isdigit (*p))
1057 point_size = atoi (p), point_size /= 10;
1058 if (point_size >= 0)
1059 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
4485a28e
KH
1060 }
1061 }
ec6fe57c 1062
087048cd
KH
1063 val = INTERN_FIELD (XLFD_RESY_INDEX);
1064 if (! NILP (val) && ! INTEGERP (val))
1065 return -1;
1066 ASET (font, FONT_DPI_INDEX, val);
35027d0c
KH
1067 val = INTERN_FIELD (XLFD_SPACING_INDEX);
1068 if (! NILP (val))
ec6fe57c 1069 {
35027d0c
KH
1070 val = font_prop_validate_spacing (QCspacing, val);
1071 if (! INTEGERP (val))
1072 return -1;
1073 ASET (font, FONT_SPACING_INDEX, val);
ec6fe57c 1074 }
ec6fe57c
KH
1075 p = f[XLFD_AVGWIDTH_INDEX];
1076 if (*p == '~')
1077 p++;
087048cd
KH
1078 val = font_intern_prop (p, f[XLFD_REGISTRY_INDEX] - 1 - p, 0);
1079 if (! NILP (val) && ! INTEGERP (val))
1080 return -1;
1081 ASET (font, FONT_AVGWIDTH_INDEX, val);
4485a28e
KH
1082 }
1083 else
c2f5bfd6 1084 {
4485a28e 1085 int wild_card_found = 0;
ec6fe57c 1086 Lisp_Object prop[XLFD_LAST_INDEX];
4485a28e 1087
35027d0c
KH
1088 if (FONT_ENTITY_P (font))
1089 return -1;
ec6fe57c 1090 for (j = 0; j < i; j++)
4485a28e 1091 {
ec6fe57c 1092 if (*f[j] == '*')
4485a28e 1093 {
ec6fe57c
KH
1094 if (f[j][1] && f[j][1] != '-')
1095 return -1;
1096 prop[j] = Qnil;
1097 wild_card_found = 1;
1098 }
ec6fe57c 1099 else if (j + 1 < i)
35027d0c 1100 prop[j] = INTERN_FIELD (j);
ec6fe57c 1101 else
2f286d4f 1102 prop[j] = font_intern_prop (f[j], f[i] - f[j], 0);
4485a28e
KH
1103 }
1104 if (! wild_card_found)
c2f5bfd6 1105 return -1;
ec6fe57c 1106 if (font_expand_wildcards (prop, i) < 0)
4485a28e 1107 return -1;
ec6fe57c 1108
35027d0c
KH
1109 ASET (font, FONT_FOUNDRY_INDEX, prop[XLFD_FOUNDRY_INDEX]);
1110 ASET (font, FONT_FAMILY_INDEX, prop[XLFD_FAMILY_INDEX]);
1111 for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
1112 i <= XLFD_SWIDTH_INDEX; i++, j++)
ec6fe57c 1113 if (! NILP (prop[i]))
35027d0c
KH
1114 {
1115 if ((n = font_style_to_value (j, prop[i], 1)) < 0)
1116 return -1;
1117 ASET (font, j, make_number (n));
1118 }
1119 ASET (font, FONT_ADSTYLE_INDEX, prop[XLFD_ADSTYLE_INDEX]);
ec6fe57c
KH
1120 val = prop[XLFD_REGISTRY_INDEX];
1121 if (NILP (val))
c2f5bfd6 1122 {
ec6fe57c
KH
1123 val = prop[XLFD_ENCODING_INDEX];
1124 if (! NILP (val))
35027d0c 1125 val = concat2 (build_string ("*-"), SYMBOL_NAME (val));
4485a28e 1126 }
ec6fe57c 1127 else if (NILP (prop[XLFD_ENCODING_INDEX]))
35027d0c 1128 val = concat2 (SYMBOL_NAME (val), build_string ("-*"));
4485a28e 1129 else
35027d0c
KH
1130 val = concat3 (SYMBOL_NAME (val), build_string ("-"),
1131 SYMBOL_NAME (prop[XLFD_ENCODING_INDEX]));
ec6fe57c 1132 if (! NILP (val))
35027d0c 1133 ASET (font, FONT_REGISTRY_INDEX, Fintern (val, Qnil));
ec6fe57c
KH
1134
1135 if (INTEGERP (prop[XLFD_PIXEL_INDEX]))
1136 ASET (font, FONT_SIZE_INDEX, prop[XLFD_PIXEL_INDEX]);
1137 else if (INTEGERP (prop[XLFD_POINT_INDEX]))
4485a28e 1138 {
ec6fe57c 1139 double point_size = XINT (prop[XLFD_POINT_INDEX]);
c2f5bfd6 1140
ec6fe57c
KH
1141 ASET (font, FONT_SIZE_INDEX, make_float (point_size / 10));
1142 }
c2f5bfd6 1143
35027d0c
KH
1144 if (INTEGERP (prop[XLFD_RESX_INDEX]))
1145 ASET (font, FONT_DPI_INDEX, prop[XLFD_RESY_INDEX]);
1146 if (! NILP (prop[XLFD_SPACING_INDEX]))
1147 {
1148 val = font_prop_validate_spacing (QCspacing,
1149 prop[XLFD_SPACING_INDEX]);
1150 if (! INTEGERP (val))
1151 return -1;
1152 ASET (font, FONT_SPACING_INDEX, val);
1153 }
ec6fe57c 1154 if (INTEGERP (prop[XLFD_AVGWIDTH_INDEX]))
35027d0c 1155 ASET (font, FONT_AVGWIDTH_INDEX, prop[XLFD_AVGWIDTH_INDEX]);
c2f5bfd6
KH
1156 }
1157
ec6fe57c 1158 return 0;
c2f5bfd6
KH
1159}
1160
1161/* Store XLFD name of FONT (font-spec or font-entity) in NAME (NBYTES
1162 length), and return the name length. If FONT_SIZE_INDEX of FONT is
1163 0, use PIXEL_SIZE instead. */
1164
1165int
971de7fb 1166font_unparse_xlfd (Lisp_Object font, int pixel_size, char *name, int nbytes)
c2f5bfd6 1167{
ec6fe57c 1168 char *f[XLFD_REGISTRY_INDEX + 1];
c2f5bfd6
KH
1169 Lisp_Object val;
1170 int i, j, len = 0;
1171
d0ab1ebe 1172 font_assert (FONTP (font));
c2f5bfd6
KH
1173
1174 for (i = FONT_FOUNDRY_INDEX, j = XLFD_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX;
1175 i++, j++)
1176 {
1177 if (i == FONT_ADSTYLE_INDEX)
1178 j = XLFD_ADSTYLE_INDEX;
1179 else if (i == FONT_REGISTRY_INDEX)
1180 j = XLFD_REGISTRY_INDEX;
1181 val = AREF (font, i);
1182 if (NILP (val))
1bb1d99b
KH
1183 {
1184 if (j == XLFD_REGISTRY_INDEX)
1185 f[j] = "*-*", len += 4;
1186 else
1187 f[j] = "*", len += 2;
1188 }
c2f5bfd6
KH
1189 else
1190 {
1191 if (SYMBOLP (val))
1192 val = SYMBOL_NAME (val);
1bb1d99b
KH
1193 if (j == XLFD_REGISTRY_INDEX
1194 && ! strchr ((char *) SDATA (val), '-'))
1195 {
1196 /* Change "jisx0208*" and "jisx0208" to "jisx0208*-*". */
1197 if (SDATA (val)[SBYTES (val) - 1] == '*')
1198 {
1199 f[j] = alloca (SBYTES (val) + 3);
1200 sprintf (f[j], "%s-*", SDATA (val));
1201 len += SBYTES (val) + 3;
1202 }
1203 else
1204 {
1205 f[j] = alloca (SBYTES (val) + 4);
1206 sprintf (f[j], "%s*-*", SDATA (val));
1207 len += SBYTES (val) + 4;
1208 }
1209 }
1210 else
1211 f[j] = (char *) SDATA (val), len += SBYTES (val) + 1;
c2f5bfd6
KH
1212 }
1213 }
1214
1215 for (i = FONT_WEIGHT_INDEX, j = XLFD_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX;
1216 i++, j++)
1217 {
35027d0c 1218 val = font_style_symbolic (font, i, 0);
c2f5bfd6
KH
1219 if (NILP (val))
1220 f[j] = "*", len += 2;
1221 else
1222 {
35027d0c 1223 val = SYMBOL_NAME (val);
c2f5bfd6
KH
1224 f[j] = (char *) SDATA (val), len += SBYTES (val) + 1;
1225 }
1226 }
1227
1228 val = AREF (font, FONT_SIZE_INDEX);
d0ab1ebe 1229 font_assert (NUMBERP (val) || NILP (val));
c2f5bfd6
KH
1230 if (INTEGERP (val))
1231 {
35027d0c 1232 i = XINT (val);
81aefea4
SM
1233 if (i <= 0)
1234 i = pixel_size;
c2f5bfd6 1235 if (i > 0)
81aefea4
SM
1236 {
1237 f[XLFD_PIXEL_INDEX] = alloca (22);
1238 len += sprintf (f[XLFD_PIXEL_INDEX], "%d-*", i) + 1;
1239 }
1240 else
1241 f[XLFD_PIXEL_INDEX] = "*-*", len += 4;
c2f5bfd6
KH
1242 }
1243 else if (FLOATP (val))
1244 {
35027d0c 1245 i = XFLOAT_DATA (val) * 10;
ec6fe57c 1246 f[XLFD_PIXEL_INDEX] = alloca (12);
ec6fe57c 1247 len += sprintf (f[XLFD_PIXEL_INDEX], "*-%d", i) + 1;
c2f5bfd6
KH
1248 }
1249 else
ec6fe57c
KH
1250 f[XLFD_PIXEL_INDEX] = "*-*", len += 4;
1251
35027d0c 1252 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
c2f5bfd6 1253 {
35027d0c
KH
1254 i = XINT (AREF (font, FONT_DPI_INDEX));
1255 f[XLFD_RESX_INDEX] = alloca (22);
1256 len += sprintf (f[XLFD_RESX_INDEX],
1257 "%d-%d", i, i) + 1;
c2f5bfd6
KH
1258 }
1259 else
35027d0c
KH
1260 f[XLFD_RESX_INDEX] = "*-*", len += 4;
1261 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
ec6fe57c 1262 {
35027d0c 1263 int spacing = XINT (AREF (font, FONT_SPACING_INDEX));
ec6fe57c 1264
35027d0c
KH
1265 f[XLFD_SPACING_INDEX] = (spacing <= FONT_SPACING_PROPORTIONAL ? "p"
1266 : spacing <= FONT_SPACING_DUAL ? "d"
1267 : spacing <= FONT_SPACING_MONO ? "m"
1268 : "c");
1269 len += 2;
ec6fe57c 1270 }
35027d0c
KH
1271 else
1272 f[XLFD_SPACING_INDEX] = "*", len += 2;
1273 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1274 {
1275 f[XLFD_AVGWIDTH_INDEX] = alloca (11);
c56c14a1
AR
1276 len += sprintf (f[XLFD_AVGWIDTH_INDEX], "%ld",
1277 (long) XINT (AREF (font, FONT_AVGWIDTH_INDEX))) + 1;
35027d0c
KH
1278 }
1279 else
1280 f[XLFD_AVGWIDTH_INDEX] = "*", len += 2;
ec6fe57c 1281 len++; /* for terminating '\0'. */
c2f5bfd6
KH
1282 if (len >= nbytes)
1283 return -1;
35027d0c 1284 return sprintf (name, "-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s",
c2f5bfd6
KH
1285 f[XLFD_FOUNDRY_INDEX], f[XLFD_FAMILY_INDEX],
1286 f[XLFD_WEIGHT_INDEX], f[XLFD_SLANT_INDEX],
35027d0c
KH
1287 f[XLFD_SWIDTH_INDEX], f[XLFD_ADSTYLE_INDEX],
1288 f[XLFD_PIXEL_INDEX], f[XLFD_RESX_INDEX],
1289 f[XLFD_SPACING_INDEX], f[XLFD_AVGWIDTH_INDEX],
1290 f[XLFD_REGISTRY_INDEX]);
c2f5bfd6
KH
1291}
1292
8c102d5b
CY
1293/* Parse NAME (null terminated) and store information in FONT
1294 (font-spec or font-entity). NAME is supplied in either the
1295 Fontconfig or GTK font name format. If NAME is successfully
1296 parsed, return 0. Otherwise return -1.
1297
1298 The fontconfig format is
1299
1300 FAMILY[-SIZE][:PROP1[=VAL1][:PROP2[=VAL2]...]]
1301
1302 The GTK format is
1303
1304 FAMILY [PROPS...] [SIZE]
1305
1306 This function tries to guess which format it is. */
ef18374f
KH
1307
1308int
09d93395 1309font_parse_fcname (char *name, Lisp_Object font)
ef18374f 1310{
8c102d5b
CY
1311 char *p, *q;
1312 char *size_beg = NULL, *size_end = NULL;
1313 char *props_beg = NULL, *family_end = NULL;
9331887d 1314 int len = strlen (name);
ef18374f 1315
ec6fe57c
KH
1316 if (len == 0)
1317 return -1;
8c102d5b
CY
1318
1319 for (p = name; *p; p++)
ef18374f 1320 {
8c102d5b
CY
1321 if (*p == '\\' && p[1])
1322 p++;
1323 else if (*p == ':')
ef18374f 1324 {
59facb78 1325 props_beg = family_end = p;
8c102d5b
CY
1326 break;
1327 }
1328 else if (*p == '-')
1329 {
b1868a1a 1330 int decimal = 0, size_found = 1;
8c102d5b 1331 for (q = p + 1; *q && *q != ':'; q++)
b1868a1a 1332 if (! isdigit(*q))
8c102d5b 1333 {
b1868a1a
CY
1334 if (*q != '.' || decimal)
1335 {
1336 size_found = 0;
1337 break;
1338 }
1339 decimal = 1;
8c102d5b
CY
1340 }
1341 if (size_found)
1342 {
1343 family_end = p;
1344 size_beg = p + 1;
1345 size_end = q;
1346 break;
1347 }
ef18374f 1348 }
ef18374f 1349 }
9331887d 1350
8c102d5b
CY
1351 if (family_end)
1352 {
637fa988
JD
1353 Lisp_Object extra_props = Qnil;
1354
8c102d5b
CY
1355 /* A fontconfig name with size and/or property data. */
1356 if (family_end > name)
1357 {
1358 Lisp_Object family;
1359 family = font_intern_prop (name, family_end - name, 1);
1360 ASET (font, FONT_FAMILY_INDEX, family);
1361 }
1362 if (size_beg)
1363 {
1364 double point_size = strtod (size_beg, &size_end);
1365 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1366 if (*size_end == ':' && size_end[1])
59facb78 1367 props_beg = size_end;
8c102d5b
CY
1368 }
1369 if (props_beg)
1370 {
d26424c5 1371 /* Now parse ":KEY=VAL" patterns. */
59facb78 1372 Lisp_Object val;
8c102d5b 1373
59facb78 1374 for (p = props_beg; *p; p = q)
8c102d5b 1375 {
8c102d5b 1376 for (q = p + 1; *q && *q != '=' && *q != ':'; q++);
8c102d5b
CY
1377 if (*q != '=')
1378 {
1379 /* Must be an enumerated value. */
59facb78
CY
1380 int word_len;
1381 p = p + 1;
1382 word_len = q - p;
8c102d5b 1383 val = font_intern_prop (p, q - p, 1);
9277a69d
CY
1384
1385#define PROP_MATCH(STR,N) ((word_len == N) && memcmp (p, STR, N) == 0)
1386
8c102d5b
CY
1387 if (PROP_MATCH ("light", 5)
1388 || PROP_MATCH ("medium", 6)
1389 || PROP_MATCH ("demibold", 8)
1390 || PROP_MATCH ("bold", 4)
1391 || PROP_MATCH ("black", 5))
1392 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, val);
1393 else if (PROP_MATCH ("roman", 5)
1394 || PROP_MATCH ("italic", 6)
1395 || PROP_MATCH ("oblique", 7))
1396 FONT_SET_STYLE (font, FONT_SLANT_INDEX, val);
1397 else if (PROP_MATCH ("charcell", 8))
1398 ASET (font, FONT_SPACING_INDEX,
1399 make_number (FONT_SPACING_CHARCELL));
1400 else if (PROP_MATCH ("mono", 4))
1401 ASET (font, FONT_SPACING_INDEX,
1402 make_number (FONT_SPACING_MONO));
1403 else if (PROP_MATCH ("proportional", 12))
1404 ASET (font, FONT_SPACING_INDEX,
1405 make_number (FONT_SPACING_PROPORTIONAL));
9277a69d 1406#undef PROP_MATCH
8c102d5b 1407 }
59facb78 1408 else
8c102d5b 1409 {
59facb78 1410 /* KEY=VAL pairs */
8c102d5b 1411 Lisp_Object key;
59facb78 1412 int prop;
51c01100 1413
59facb78 1414 if (q - p == 10 && memcmp (p + 1, "pixelsize", 9) == 0)
8c102d5b
CY
1415 prop = FONT_SIZE_INDEX;
1416 else
1417 {
1418 key = font_intern_prop (p, q - p, 1);
1419 prop = get_font_prop_index (key);
1420 }
77989187 1421
8c102d5b
CY
1422 p = q + 1;
1423 for (q = p; *q && *q != ':'; q++);
90c00b01 1424 val = font_intern_prop (p, q - p, 0);
77989187 1425
d26424c5
KH
1426 if (prop >= FONT_FOUNDRY_INDEX
1427 && prop < FONT_EXTRA_INDEX)
637fa988 1428 ASET (font, prop, font_prop_validate (prop, Qnil, val));
ce75f06e 1429 else
637fa988
JD
1430 {
1431 extra_props = nconc2 (extra_props,
1432 Fcons (Fcons (key, val), Qnil));
1433 }
8c102d5b 1434 }
d26424c5 1435 p = q;
8c102d5b 1436 }
8c102d5b 1437 }
637fa988
JD
1438
1439 if (! NILP (extra_props))
1440 {
1441 struct font_driver_list *driver_list = font_driver_list;
1442 for ( ; driver_list; driver_list = driver_list->next)
1443 if (driver_list->driver->filter_properties)
1444 (*driver_list->driver->filter_properties) (font, extra_props);
1445 }
ce75f06e 1446
8c102d5b
CY
1447 }
1448 else
ef18374f 1449 {
8c102d5b
CY
1450 /* Either a fontconfig-style name with no size and property
1451 data, or a GTK-style name. */
1452 Lisp_Object prop;
1453 int word_len, prop_found = 0;
ef18374f 1454
8c102d5b 1455 for (p = name; *p; p = *q ? q + 1 : q)
ef18374f 1456 {
8c102d5b 1457 if (isdigit (*p))
9331887d 1458 {
8c102d5b 1459 int size_found = 1;
7181ea6a 1460
8c102d5b 1461 for (q = p + 1; *q && *q != ' '; q++)
32a679fd 1462 if (! isdigit (*q) && *q != '.')
8c102d5b
CY
1463 {
1464 size_found = 0;
1465 break;
1466 }
1467 if (size_found)
1468 {
1469 double point_size = strtod (p, &q);
1470 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1471 continue;
1472 }
027a33c0 1473 }
8c102d5b
CY
1474
1475 for (q = p + 1; *q && *q != ' '; q++)
1476 if (*q == '\\' && q[1])
1477 q++;
1478 word_len = q - p;
1479
1480#define PROP_MATCH(STR,N) ((word_len == N) && memcmp (p, STR, N) == 0)
1481
1482 if (PROP_MATCH ("Ultra-Light", 11))
9331887d 1483 {
8c102d5b
CY
1484 prop_found = 1;
1485 prop = font_intern_prop ("ultra-light", 11, 1);
1486 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
9331887d 1487 }
8c102d5b 1488 else if (PROP_MATCH ("Light", 5))
9331887d 1489 {
8c102d5b
CY
1490 prop_found = 1;
1491 prop = font_intern_prop ("light", 5, 1);
1492 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
9331887d 1493 }
fe69a722
CY
1494 else if (PROP_MATCH ("Book", 4))
1495 {
1496 prop_found = 1;
1497 prop = font_intern_prop ("book", 4, 1);
1498 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
1499 }
4ed28cf4
CY
1500 else if (PROP_MATCH ("Medium", 6))
1501 {
1502 prop_found = 1;
1503 prop = font_intern_prop ("medium", 6, 1);
1504 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
1505 }
8c102d5b 1506 else if (PROP_MATCH ("Semi-Bold", 9))
9331887d 1507 {
8c102d5b
CY
1508 prop_found = 1;
1509 prop = font_intern_prop ("semi-bold", 9, 1);
1510 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
1511 }
1512 else if (PROP_MATCH ("Bold", 4))
1513 {
1514 prop_found = 1;
1515 prop = font_intern_prop ("bold", 4, 1);
1516 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
ef18374f 1517 }
8c102d5b
CY
1518 else if (PROP_MATCH ("Italic", 6))
1519 {
1520 prop_found = 1;
1521 prop = font_intern_prop ("italic", 4, 1);
1522 FONT_SET_STYLE (font, FONT_SLANT_INDEX, prop);
1523 }
1524 else if (PROP_MATCH ("Oblique", 7))
1525 {
1526 prop_found = 1;
1527 prop = font_intern_prop ("oblique", 7, 1);
1528 FONT_SET_STYLE (font, FONT_SLANT_INDEX, prop);
1529 }
fe69a722
CY
1530 else if (PROP_MATCH ("Semi-Condensed", 14))
1531 {
1532 prop_found = 1;
1533 prop = font_intern_prop ("semi-condensed", 14, 1);
1534 FONT_SET_STYLE (font, FONT_WIDTH_INDEX, prop);
1535 }
1536 else if (PROP_MATCH ("Condensed", 9))
1537 {
1538 prop_found = 1;
1539 prop = font_intern_prop ("condensed", 9, 1);
1540 FONT_SET_STYLE (font, FONT_WIDTH_INDEX, prop);
1541 }
8c102d5b
CY
1542 else {
1543 if (prop_found)
1544 return -1; /* Unknown property in GTK-style font name. */
1545 family_end = q;
1546 }
ef18374f 1547 }
8c102d5b 1548#undef PROP_MATCH
9ba6fd41 1549
8c102d5b
CY
1550 if (family_end)
1551 {
1552 Lisp_Object family;
1553 family = font_intern_prop (name, family_end - name, 1);
1554 ASET (font, FONT_FAMILY_INDEX, family);
1555 }
1556 }
17ab8f5d 1557
9331887d 1558 return 0;
ef18374f
KH
1559}
1560
1561/* Store fontconfig's font name of FONT (font-spec or font-entity) in
1562 NAME (NBYTES length), and return the name length. If
1563 FONT_SIZE_INDEX of FONT is 0, use PIXEL_SIZE instead. */
1564
1565int
09d93395 1566font_unparse_fcname (Lisp_Object font, int pixel_size, char *name, int nbytes)
ef18374f 1567{
b1868a1a 1568 Lisp_Object family, foundry;
35027d0c 1569 Lisp_Object tail, val;
ec6fe57c 1570 int point_size;
ec6fe57c 1571 int i, len = 1;
ef18374f 1572 char *p;
a9262bb8 1573 Lisp_Object styles[3];
675e2c69 1574 const char *style_names[3] = { "weight", "slant", "width" };
35027d0c 1575 char work[256];
ef18374f 1576
b1868a1a
CY
1577 family = AREF (font, FONT_FAMILY_INDEX);
1578 if (! NILP (family))
1579 {
1580 if (SYMBOLP (family))
1581 {
1582 family = SYMBOL_NAME (family);
1583 len += SBYTES (family);
1584 }
1585 else
1586 family = Qnil;
1587 }
ec6fe57c
KH
1588
1589 val = AREF (font, FONT_SIZE_INDEX);
1590 if (INTEGERP (val))
ef18374f 1591 {
ec6fe57c
KH
1592 if (XINT (val) != 0)
1593 pixel_size = XINT (val);
1594 point_size = -1;
1595 len += 21; /* for ":pixelsize=NUM" */
ef18374f 1596 }
ec6fe57c 1597 else if (FLOATP (val))
ef18374f 1598 {
ec6fe57c
KH
1599 pixel_size = -1;
1600 point_size = (int) XFLOAT_DATA (val);
1601 len += 11; /* for "-NUM" */
ef18374f 1602 }
ec6fe57c 1603
b1868a1a
CY
1604 foundry = AREF (font, FONT_FOUNDRY_INDEX);
1605 if (! NILP (foundry))
1606 {
1607 if (SYMBOLP (foundry))
1608 {
1609 foundry = SYMBOL_NAME (foundry);
1610 len += 9 + SBYTES (foundry); /* ":foundry=NAME" */
1611 }
1612 else
1613 foundry = Qnil;
1614 }
ec6fe57c 1615
35027d0c 1616 for (i = 0; i < 3; i++)
a9262bb8 1617 {
35027d0c
KH
1618 styles[i] = font_style_symbolic (font, FONT_WEIGHT_INDEX + i, 0);
1619 if (! NILP (styles[i]))
1620 len += sprintf (work, ":%s=%s", style_names[i],
1621 SDATA (SYMBOL_NAME (styles[i])));
ec6fe57c 1622 }
35027d0c
KH
1623
1624 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
c56c14a1 1625 len += sprintf (work, ":dpi=%ld", (long)XINT (AREF (font, FONT_DPI_INDEX)));
35027d0c
KH
1626 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1627 len += strlen (":spacing=100");
1628 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1629 len += strlen (":scalable=false"); /* or ":scalable=true" */
1630 for (tail = AREF (font, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail))
ec6fe57c 1631 {
35027d0c
KH
1632 Lisp_Object key = XCAR (XCAR (tail)), val = XCDR (XCAR (tail));
1633
1634 len += SBYTES (SYMBOL_NAME (key)) + 1; /* for :KEY= */
1635 if (STRINGP (val))
1636 len += SBYTES (val);
1637 else if (INTEGERP (val))
c56c14a1 1638 len += sprintf (work, "%ld", (long) XINT (val));
35027d0c
KH
1639 else if (SYMBOLP (val))
1640 len += (NILP (val) ? 5 : 4); /* for "false" or "true" */
ec6fe57c
KH
1641 }
1642
ef18374f
KH
1643 if (len > nbytes)
1644 return -1;
1645 p = name;
b1868a1a
CY
1646 if (! NILP (family))
1647 p += sprintf (p, "%s", SDATA (family));
ec6fe57c
KH
1648 if (point_size > 0)
1649 {
1650 if (p == name)
1651 p += sprintf (p, "%d", point_size);
1652 else
1653 p += sprintf (p, "-%d", point_size);
1654 }
ef18374f
KH
1655 else if (pixel_size > 0)
1656 p += sprintf (p, ":pixelsize=%d", pixel_size);
35027d0c 1657 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
a9262bb8
KH
1658 p += sprintf (p, ":foundry=%s",
1659 SDATA (SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX))));
1660 for (i = 0; i < 3; i++)
35027d0c 1661 if (! NILP (styles[i]))
a9262bb8 1662 p += sprintf (p, ":%s=%s", style_names[i],
35027d0c
KH
1663 SDATA (SYMBOL_NAME (styles[i])));
1664 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
c56c14a1 1665 p += sprintf (p, ":dpi=%ld", (long) XINT (AREF (font, FONT_DPI_INDEX)));
35027d0c 1666 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
c56c14a1
AR
1667 p += sprintf (p, ":spacing=%ld",
1668 (long) XINT (AREF (font, FONT_SPACING_INDEX)));
35027d0c
KH
1669 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1670 {
1671 if (XINT (AREF (font, FONT_AVGWIDTH_INDEX)) == 0)
1672 p += sprintf (p, ":scalable=true");
1673 else
1674 p += sprintf (p, ":scalable=false");
1675 }
ef18374f
KH
1676 return (p - name);
1677}
1678
b1868a1a
CY
1679/* Store GTK-style font name of FONT (font-spec or font-entity) in
1680 NAME (NBYTES length), and return the name length. F is the frame
1681 on which the font is displayed; it is used to calculate the point
1682 size. */
1683
1684int
971de7fb 1685font_unparse_gtkname (Lisp_Object font, struct frame *f, char *name, int nbytes)
b1868a1a
CY
1686{
1687 char *p;
1688 int len = 1;
1689 Lisp_Object family, weight, slant, size;
1690 int point_size = -1;
1691
1692 family = AREF (font, FONT_FAMILY_INDEX);
1693 if (! NILP (family))
1694 {
1695 if (! SYMBOLP (family))
1696 return -1;
1697 family = SYMBOL_NAME (family);
1698 len += SBYTES (family);
1699 }
1700
1701 weight = font_style_symbolic (font, FONT_WEIGHT_INDEX, 0);
93eed26b 1702 if (EQ (weight, Qnormal))
b1868a1a
CY
1703 weight = Qnil;
1704 else if (! NILP (weight))
1705 {
1706 weight = SYMBOL_NAME (weight);
1707 len += SBYTES (weight);
1708 }
1709
1710 slant = font_style_symbolic (font, FONT_SLANT_INDEX, 0);
93eed26b 1711 if (EQ (slant, Qnormal))
b1868a1a
CY
1712 slant = Qnil;
1713 else if (! NILP (slant))
1714 {
1715 slant = SYMBOL_NAME (slant);
1716 len += SBYTES (slant);
1717 }
1718
1719 size = AREF (font, FONT_SIZE_INDEX);
1720 /* Convert pixel size to point size. */
1721 if (INTEGERP (size))
1722 {
1723 Lisp_Object font_dpi = AREF (font, FONT_DPI_INDEX);
1724 int dpi = 75;
1725 if (INTEGERP (font_dpi))
1726 dpi = XINT (font_dpi);
1727 else if (f)
1728 dpi = f->resy;
1729 point_size = PIXEL_TO_POINT (XINT (size), dpi);
1730 len += 11;
1731 }
1732 else if (FLOATP (size))
1733 {
1734 point_size = (int) XFLOAT_DATA (size);
1735 len += 11;
1736 }
1737
1738 if (len > nbytes)
1739 return -1;
1740
1741 p = name + sprintf (name, "%s", SDATA (family));
1742
1743 if (! NILP (weight))
1744 {
1745 char *q = p;
1746 p += sprintf (p, " %s", SDATA (weight));
1747 q[1] = toupper (q[1]);
1748 }
1749
1750 if (! NILP (slant))
1751 {
1752 char *q = p;
1753 p += sprintf (p, " %s", SDATA (slant));
1754 q[1] = toupper (q[1]);
1755 }
1756
1757 if (point_size > 0)
1758 p += sprintf (p, " %d", point_size);
1759
1760 return (p - name);
1761}
1762
ef18374f
KH
1763/* Parse NAME (null terminated) and store information in FONT
1764 (font-spec or font-entity). If NAME is successfully parsed, return
35027d0c 1765 0. Otherwise return -1. */
ef18374f
KH
1766
1767static int
09d93395 1768font_parse_name (char *name, Lisp_Object font)
ef18374f 1769{
8966b757 1770 if (name[0] == '-' || strchr (name, '*') || strchr (name, '?'))
e950d6f1 1771 return font_parse_xlfd (name, font);
ec6fe57c 1772 return font_parse_fcname (name, font);
ef18374f
KH
1773}
1774
35027d0c
KH
1775
1776/* Merge FAMILY and REGISTRY into FONT_SPEC. FAMILY may have the form
1777 "FAMILY-FOUNDRY". REGISTRY may not contain charset-encoding
1778 part. */
45eb10fb 1779
c2f5bfd6 1780void
971de7fb 1781font_parse_family_registry (Lisp_Object family, Lisp_Object registry, Lisp_Object font_spec)
c2f5bfd6 1782{
35027d0c
KH
1783 int len;
1784 char *p0, *p1;
1785
d0ab1ebe
KH
1786 if (! NILP (family)
1787 && NILP (AREF (font_spec, FONT_FAMILY_INDEX)))
c2f5bfd6 1788 {
35027d0c
KH
1789 CHECK_STRING (family);
1790 len = SBYTES (family);
1791 p0 = (char *) SDATA (family);
8966b757 1792 p1 = strchr (p0, '-');
35027d0c 1793 if (p1)
c2f5bfd6 1794 {
9903d1e6 1795 if ((*p0 != '*' && p1 - p0 > 0)
d0ab1ebe 1796 && NILP (AREF (font_spec, FONT_FOUNDRY_INDEX)))
2f286d4f 1797 Ffont_put (font_spec, QCfoundry, font_intern_prop (p0, p1 - p0, 1));
35027d0c
KH
1798 p1++;
1799 len -= p1 - p0;
2f286d4f 1800 Ffont_put (font_spec, QCfamily, font_intern_prop (p1, len, 1));
c2f5bfd6 1801 }
35027d0c
KH
1802 else
1803 ASET (font_spec, FONT_FAMILY_INDEX, Fintern (family, Qnil));
c2f5bfd6 1804 }
35027d0c 1805 if (! NILP (registry))
c2f5bfd6 1806 {
35027d0c
KH
1807 /* Convert "XXX" and "XXX*" to "XXX*-*". */
1808 CHECK_STRING (registry);
1809 len = SBYTES (registry);
1810 p0 = (char *) SDATA (registry);
8966b757 1811 p1 = strchr (p0, '-');
35027d0c 1812 if (! p1)
c2f5bfd6 1813 {
35027d0c
KH
1814 if (SDATA (registry)[len - 1] == '*')
1815 registry = concat2 (registry, build_string ("-*"));
1816 else
1817 registry = concat2 (registry, build_string ("*-*"));
c2f5bfd6 1818 }
35027d0c
KH
1819 registry = Fdowncase (registry);
1820 ASET (font_spec, FONT_REGISTRY_INDEX, Fintern (registry, Qnil));
c2f5bfd6
KH
1821 }
1822}
1823
45eb10fb 1824\f
35027d0c
KH
1825/* This part (through the next ^L) is still experimental and not
1826 tested much. We may drastically change codes. */
10d16101 1827
45eb10fb 1828/* OTF handler */
10d16101 1829
6a3dadd2
KH
1830#if 0
1831
e950d6f1
KH
1832#define LGSTRING_HEADER_SIZE 6
1833#define LGSTRING_GLYPH_SIZE 8
1834
1835static int
1836check_gstring (gstring)
1837 Lisp_Object gstring;
1838{
1839 Lisp_Object val;
1840 int i, j;
1841
1842 CHECK_VECTOR (gstring);
1843 val = AREF (gstring, 0);
1844 CHECK_VECTOR (val);
1845 if (ASIZE (val) < LGSTRING_HEADER_SIZE)
1846 goto err;
1847 CHECK_FONT_OBJECT (LGSTRING_FONT (gstring));
f9ffa1ea
SM
1848 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_LBEARING)))
1849 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_LBEARING));
1850 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_RBEARING)))
1851 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_RBEARING));
1852 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_WIDTH)))
1853 CHECK_NATNUM (LGSTRING_SLOT (gstring, LGSTRING_IX_WIDTH));
1854 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT)))
1855 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT));
1856 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT)))
1857 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT));
e950d6f1 1858
071132a9 1859 for (i = 0; i < LGSTRING_GLYPH_LEN (gstring); i++)
e950d6f1
KH
1860 {
1861 val = LGSTRING_GLYPH (gstring, i);
1862 CHECK_VECTOR (val);
1863 if (ASIZE (val) < LGSTRING_GLYPH_SIZE)
1864 goto err;
f9ffa1ea 1865 if (NILP (AREF (val, LGLYPH_IX_CHAR)))
e950d6f1 1866 break;
f9ffa1ea
SM
1867 CHECK_NATNUM (AREF (val, LGLYPH_IX_FROM));
1868 CHECK_NATNUM (AREF (val, LGLYPH_IX_TO));
1869 CHECK_CHARACTER (AREF (val, LGLYPH_IX_CHAR));
1870 if (!NILP (AREF (val, LGLYPH_IX_CODE)))
1871 CHECK_NATNUM (AREF (val, LGLYPH_IX_CODE));
1872 if (!NILP (AREF (val, LGLYPH_IX_WIDTH)))
1873 CHECK_NATNUM (AREF (val, LGLYPH_IX_WIDTH));
1874 if (!NILP (AREF (val, LGLYPH_IX_ADJUSTMENT)))
e950d6f1 1875 {
f9ffa1ea 1876 val = AREF (val, LGLYPH_IX_ADJUSTMENT);
e950d6f1
KH
1877 CHECK_VECTOR (val);
1878 if (ASIZE (val) < 3)
1879 goto err;
1880 for (j = 0; j < 3; j++)
1881 CHECK_NUMBER (AREF (val, j));
1882 }
1883 }
1884 return i;
1885 err:
1886 error ("Invalid glyph-string format");
1887 return -1;
1888}
1889
cf385d93
KH
1890static void
1891check_otf_features (otf_features)
1892 Lisp_Object otf_features;
1893{
35027d0c 1894 Lisp_Object val;
cf385d93
KH
1895
1896 CHECK_CONS (otf_features);
1897 CHECK_SYMBOL (XCAR (otf_features));
1898 otf_features = XCDR (otf_features);
1899 CHECK_CONS (otf_features);
1900 CHECK_SYMBOL (XCAR (otf_features));
1901 otf_features = XCDR (otf_features);
1902 for (val = Fcar (otf_features); ! NILP (val); val = Fcdr (val))
1903 {
1904 CHECK_SYMBOL (Fcar (val));
1905 if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
1906 error ("Invalid OTF GSUB feature: %s", SYMBOL_NAME (XCAR (val)));
1907 }
1908 otf_features = XCDR (otf_features);
1909 for (val = Fcar (otf_features); ! NILP (val); val = Fcdr (val))
1910 {
1911 CHECK_SYMBOL (Fcar (val));
1912 if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
1913 error ("Invalid OTF GPOS feature: %s", SYMBOL_NAME (XCAR (val)));
1914 }
1915}
1916
c2f5bfd6
KH
1917#ifdef HAVE_LIBOTF
1918#include <otf.h>
1919
733fd013 1920Lisp_Object otf_list;
c2f5bfd6
KH
1921
1922static Lisp_Object
1923otf_tag_symbol (tag)
1924 OTF_Tag tag;
1925{
1926 char name[5];
1927
1928 OTF_tag_name (tag, name);
1929 return Fintern (make_unibyte_string (name, 4), Qnil);
1930}
1931
1932static OTF *
35027d0c
KH
1933otf_open (file)
1934 Lisp_Object file;
c2f5bfd6 1935{
35027d0c 1936 Lisp_Object val = Fassoc (file, otf_list);
733fd013
KH
1937 OTF *otf;
1938
1939 if (! NILP (val))
1940 otf = XSAVE_VALUE (XCDR (val))->pointer;
1941 else
c2f5bfd6 1942 {
35027d0c 1943 otf = STRINGP (file) ? OTF_open ((char *) SDATA (file)) : NULL;
733fd013 1944 val = make_save_value (otf, 0);
35027d0c 1945 otf_list = Fcons (Fcons (file, val), otf_list);
c2f5bfd6 1946 }
733fd013 1947 return otf;
c2f5bfd6
KH
1948}
1949
1950
1951/* Return a list describing which scripts/languages FONT supports by
1952 which GSUB/GPOS features of OpenType tables. See the comment of
51c01100 1953 (struct font_driver).otf_capability. */
c2f5bfd6
KH
1954
1955Lisp_Object
1956font_otf_capability (font)
1957 struct font *font;
1958{
1959 OTF *otf;
1960 Lisp_Object capability = Fcons (Qnil, Qnil);
1961 int i;
1962
35027d0c 1963 otf = otf_open (font->props[FONT_FILE_INDEX]);
c2f5bfd6
KH
1964 if (! otf)
1965 return Qnil;
1966 for (i = 0; i < 2; i++)
1967 {
1968 OTF_GSUB_GPOS *gsub_gpos;
1969 Lisp_Object script_list = Qnil;
1970 int j;
1971
1972 if (OTF_get_features (otf, i == 0) < 0)
1973 continue;
1974 gsub_gpos = i == 0 ? otf->gsub : otf->gpos;
1975 for (j = gsub_gpos->ScriptList.ScriptCount - 1; j >= 0; j--)
1976 {
1977 OTF_Script *script = gsub_gpos->ScriptList.Script + j;
1978 Lisp_Object langsys_list = Qnil;
1979 Lisp_Object script_tag = otf_tag_symbol (script->ScriptTag);
1980 int k;
1981
1982 for (k = script->LangSysCount; k >= 0; k--)
1983 {
1984 OTF_LangSys *langsys;
1985 Lisp_Object feature_list = Qnil;
1986 Lisp_Object langsys_tag;
1987 int l;
1988
e80e09b4 1989 if (k == script->LangSysCount)
c2f5bfd6
KH
1990 {
1991 langsys = &script->DefaultLangSys;
1992 langsys_tag = Qnil;
1993 }
1994 else
1995 {
1996 langsys = script->LangSys + k;
1997 langsys_tag
1998 = otf_tag_symbol (script->LangSysRecord[k].LangSysTag);
1999 }
e80e09b4 2000 for (l = langsys->FeatureCount - 1; l >= 0; l--)
c2f5bfd6
KH
2001 {
2002 OTF_Feature *feature
2003 = gsub_gpos->FeatureList.Feature + langsys->FeatureIndex[l];
2004 Lisp_Object feature_tag
2005 = otf_tag_symbol (feature->FeatureTag);
2006
2007 feature_list = Fcons (feature_tag, feature_list);
2008 }
2009 langsys_list = Fcons (Fcons (langsys_tag, feature_list),
2010 langsys_list);
2011 }
2012 script_list = Fcons (Fcons (script_tag, langsys_list),
2013 script_list);
2014 }
2015
2016 if (i == 0)
2017 XSETCAR (capability, script_list);
2018 else
2019 XSETCDR (capability, script_list);
2020 }
2021
2022 return capability;
2023}
2024
733fd013
KH
2025/* Parse OTF features in SPEC and write a proper features spec string
2026 in FEATURES for the call of OTF_drive_gsub/gpos (of libotf). It is
2027 assured that the sufficient memory has already allocated for
2028 FEATURES. */
2029
e80e09b4 2030static void
733fd013 2031generate_otf_features (spec, features)
c2f5bfd6 2032 Lisp_Object spec;
733fd013 2033 char *features;
c2f5bfd6
KH
2034{
2035 Lisp_Object val;
35027d0c 2036 char *p;
c2f5bfd6
KH
2037 int asterisk;
2038
733fd013 2039 p = features;
e80e09b4 2040 *p = '\0';
c2f5bfd6
KH
2041 for (asterisk = 0; CONSP (spec); spec = XCDR (spec))
2042 {
2043 val = XCAR (spec);
e80e09b4
KH
2044 CHECK_SYMBOL (val);
2045 if (p > features)
733fd013 2046 *p++ = ',';
c2f5bfd6
KH
2047 if (SREF (SYMBOL_NAME (val), 0) == '*')
2048 {
2049 asterisk = 1;
e80e09b4 2050 *p++ = '*';
c2f5bfd6
KH
2051 }
2052 else if (! asterisk)
e80e09b4
KH
2053 {
2054 val = SYMBOL_NAME (val);
e80e09b4
KH
2055 p += sprintf (p, "%s", SDATA (val));
2056 }
c2f5bfd6 2057 else
e80e09b4
KH
2058 {
2059 val = SYMBOL_NAME (val);
e80e09b4
KH
2060 p += sprintf (p, "~%s", SDATA (val));
2061 }
c2f5bfd6 2062 }
e80e09b4
KH
2063 if (CONSP (spec))
2064 error ("OTF spec too long");
2065}
2066
733fd013
KH
2067Lisp_Object
2068font_otf_DeviceTable (device_table)
2069 OTF_DeviceTable *device_table;
2070{
2071 int len = device_table->StartSize - device_table->EndSize + 1;
2072
2073 return Fcons (make_number (len),
2074 make_unibyte_string (device_table->DeltaValue, len));
2075}
2076
2077Lisp_Object
2078font_otf_ValueRecord (value_format, value_record)
2079 int value_format;
2080 OTF_ValueRecord *value_record;
2081{
2082 Lisp_Object val = Fmake_vector (make_number (8), Qnil);
2083
2084 if (value_format & OTF_XPlacement)
43c0454d 2085 ASET (val, 0, make_number (value_record->XPlacement));
733fd013 2086 if (value_format & OTF_YPlacement)
43c0454d 2087 ASET (val, 1, make_number (value_record->YPlacement));
733fd013 2088 if (value_format & OTF_XAdvance)
43c0454d 2089 ASET (val, 2, make_number (value_record->XAdvance));
733fd013 2090 if (value_format & OTF_YAdvance)
43c0454d 2091 ASET (val, 3, make_number (value_record->YAdvance));
733fd013
KH
2092 if (value_format & OTF_XPlaDevice)
2093 ASET (val, 4, font_otf_DeviceTable (&value_record->XPlaDevice));
2094 if (value_format & OTF_YPlaDevice)
2095 ASET (val, 4, font_otf_DeviceTable (&value_record->YPlaDevice));
2096 if (value_format & OTF_XAdvDevice)
2097 ASET (val, 4, font_otf_DeviceTable (&value_record->XAdvDevice));
2098 if (value_format & OTF_YAdvDevice)
2099 ASET (val, 4, font_otf_DeviceTable (&value_record->YAdvDevice));
2100 return val;
2101}
2102
2103Lisp_Object
2104font_otf_Anchor (anchor)
2105 OTF_Anchor *anchor;
2106{
2107 Lisp_Object val;
2108
2109 val = Fmake_vector (make_number (anchor->AnchorFormat + 1), Qnil);
2110 ASET (val, 0, make_number (anchor->XCoordinate));
2111 ASET (val, 1, make_number (anchor->YCoordinate));
2112 if (anchor->AnchorFormat == 2)
2113 ASET (val, 2, make_number (anchor->f.f1.AnchorPoint));
2114 else
2115 {
2116 ASET (val, 3, font_otf_DeviceTable (&anchor->f.f2.XDeviceTable));
2117 ASET (val, 4, font_otf_DeviceTable (&anchor->f.f2.YDeviceTable));
2118 }
2119 return val;
2120}
c2f5bfd6 2121#endif /* HAVE_LIBOTF */
6a3dadd2 2122#endif /* 0 */
c2f5bfd6 2123
c2f5bfd6
KH
2124\f
2125/* Font sorting */
2126
f57e2426
J
2127static unsigned font_score (Lisp_Object, Lisp_Object *);
2128static int font_compare (const void *, const void *);
2129static Lisp_Object font_sort_entities (Lisp_Object, Lisp_Object,
2130 Lisp_Object, int);
c2f5bfd6 2131
55e41770 2132static double
971de7fb 2133font_rescale_ratio (Lisp_Object font_entity)
55e41770
KH
2134{
2135 Lisp_Object tail, elt;
2136 Lisp_Object name = Qnil;
2137
2138 for (tail = Vface_font_rescale_alist; CONSP (tail); tail = XCDR (tail))
2139 {
2140 elt = XCAR (tail);
2141 if (FLOATP (XCDR (elt)))
2142 {
2143 if (STRINGP (XCAR (elt)))
2144 {
2145 if (NILP (name))
2146 name = Ffont_xlfd_name (font_entity, Qnil);
2147 if (fast_string_match_ignore_case (XCAR (elt), name) >= 0)
2148 return XFLOAT_DATA (XCDR (elt));
2149 }
2150 else if (FONT_SPEC_P (XCAR (elt)))
2151 {
2152 if (font_match_p (XCAR (elt), font_entity))
2153 return XFLOAT_DATA (XCDR (elt));
2154 }
2155 }
2156 }
2157 return 1.0;
2158}
2159
c2f5bfd6
KH
2160/* We sort fonts by scoring each of them against a specified
2161 font-spec. The score value is 32 bit (`unsigned'), and the smaller
2162 the value is, the closer the font is to the font-spec.
2163
4007dd1c
KH
2164 The lowest 2 bits of the score is used for driver type. The font
2165 available by the most preferred font driver is 0.
35027d0c 2166
4007dd1c 2167 Each 7-bit in the higher 28 bits are used for numeric properties
c2f5bfd6
KH
2168 WEIGHT, SLANT, WIDTH, and SIZE. */
2169
2170/* How many bits to shift to store the difference value of each font
35027d0c
KH
2171 property in a score. Note that flots for FONT_TYPE_INDEX and
2172 FONT_REGISTRY_INDEX are not used. */
c2f5bfd6
KH
2173static int sort_shift_bits[FONT_SIZE_INDEX + 1];
2174
9331887d
KH
2175/* Score font-entity ENTITY against properties of font-spec SPEC_PROP.
2176 The return value indicates how different ENTITY is compared with
51c13510 2177 SPEC_PROP. */
c2f5bfd6
KH
2178
2179static unsigned
971de7fb 2180font_score (Lisp_Object entity, Lisp_Object *spec_prop)
c2f5bfd6
KH
2181{
2182 unsigned score = 0;
2183 int i;
c2f5bfd6 2184
35027d0c
KH
2185 /* Score three style numeric fields. Maximum difference is 127. */
2186 for (i = FONT_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX; i++)
2187 if (! NILP (spec_prop[i]) && ! EQ (AREF (entity, i), spec_prop[i]))
2188 {
2189 int diff = (XINT (AREF (entity, i)) >> 8) - (XINT (spec_prop[i]) >> 8);
2190
2191 if (diff < 0)
2192 diff = - diff;
f4cc0153
KH
2193 if (diff > 0)
2194 score |= min (diff, 127) << sort_shift_bits[i];
35027d0c
KH
2195 }
2196
2197 /* Score the size. Maximum difference is 127. */
2198 i = FONT_SIZE_INDEX;
55e41770
KH
2199 if (! NILP (spec_prop[FONT_SIZE_INDEX])
2200 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
35027d0c
KH
2201 {
2202 /* We use the higher 6-bit for the actual size difference. The
2203 lowest bit is set if the DPI is different. */
55e41770
KH
2204 int diff;
2205 int pixel_size = XINT (spec_prop[FONT_SIZE_INDEX]);
35027d0c 2206
55e41770
KH
2207 if (CONSP (Vface_font_rescale_alist))
2208 pixel_size *= font_rescale_ratio (entity);
2209 diff = pixel_size - XINT (AREF (entity, FONT_SIZE_INDEX));
35027d0c
KH
2210 if (diff < 0)
2211 diff = - diff;
d0ab1ebe 2212 diff <<= 1;
35027d0c
KH
2213 if (! NILP (spec_prop[FONT_DPI_INDEX])
2214 && ! EQ (spec_prop[FONT_DPI_INDEX], AREF (entity, FONT_DPI_INDEX)))
2215 diff |= 1;
c0a6070d
KH
2216 if (! NILP (spec_prop[FONT_AVGWIDTH_INDEX])
2217 && ! EQ (spec_prop[FONT_AVGWIDTH_INDEX], AREF (entity, FONT_AVGWIDTH_INDEX)))
2218 diff |= 1;
35027d0c 2219 score |= min (diff, 127) << sort_shift_bits[FONT_SIZE_INDEX];
c2f5bfd6
KH
2220 }
2221
2222 return score;
2223}
2224
2225
72d36834
KH
2226/* Concatenate all elements of LIST into one vector. LIST is a list
2227 of font-entity vectors. */
c2f5bfd6 2228
72d36834
KH
2229static Lisp_Object
2230font_vconcat_entity_vectors (Lisp_Object list)
c2f5bfd6 2231{
72d36834
KH
2232 int nargs = XINT (Flength (list));
2233 Lisp_Object *args = alloca (sizeof (Lisp_Object) * nargs);
2234 int i;
2235
2236 for (i = 0; i < nargs; i++, list = XCDR (list))
2237 args[i] = XCAR (list);
2238 return Fvconcat (nargs, args);
c2f5bfd6
KH
2239}
2240
2241
2242/* The structure for elements being sorted by qsort. */
2243struct font_sort_data
2244{
2245 unsigned score;
72d36834 2246 int font_driver_preference;
c2f5bfd6
KH
2247 Lisp_Object entity;
2248};
2249
2250
72d36834
KH
2251/* The comparison function for qsort. */
2252
2253static int
971de7fb 2254font_compare (const void *d1, const void *d2)
72d36834
KH
2255{
2256 const struct font_sort_data *data1 = d1;
2257 const struct font_sort_data *data2 = d2;
2258
2259 if (data1->score < data2->score)
2260 return -1;
2261 else if (data1->score > data2->score)
2262 return 1;
2263 return (data1->font_driver_preference - data2->font_driver_preference);
2264}
2265
2266
2267/* Sort each font-entity vector in LIST by closeness to font-spec PREFER.
c2f5bfd6 2268 If PREFER specifies a point-size, calculate the corresponding
9331887d 2269 pixel-size from QCdpi property of PREFER or from the Y-resolution
7181ea6a 2270 of FRAME before sorting.
35027d0c 2271
c9477f01
KH
2272 If BEST-ONLY is nonzero, return the best matching entity (that
2273 supports the character BEST-ONLY if BEST-ONLY is positive, or any
72d36834
KH
2274 if BEST-ONLY is negative). Otherwise, return the sorted result as
2275 a single vector of font-entities.
c9477f01 2276
72d36834
KH
2277 This function does no optimization for the case that the total
2278 number of elements is 1. The caller should avoid calling this in
2279 such a case. */
c2f5bfd6
KH
2280
2281static Lisp_Object
971de7fb 2282font_sort_entities (Lisp_Object list, Lisp_Object prefer, Lisp_Object frame, int best_only)
c2f5bfd6 2283{
9331887d 2284 Lisp_Object prefer_prop[FONT_SPEC_MAX];
72d36834 2285 int len, maxlen, i;
c2f5bfd6 2286 struct font_sort_data *data;
35027d0c 2287 unsigned best_score;
72d36834 2288 Lisp_Object best_entity;
4007dd1c 2289 struct frame *f = XFRAME (frame);
72d36834 2290 Lisp_Object tail, vec;
c2f5bfd6
KH
2291 USE_SAFE_ALLOCA;
2292
9903d1e6 2293 for (i = FONT_WEIGHT_INDEX; i <= FONT_AVGWIDTH_INDEX; i++)
9331887d 2294 prefer_prop[i] = AREF (prefer, i);
9331887d
KH
2295 if (FLOATP (prefer_prop[FONT_SIZE_INDEX]))
2296 prefer_prop[FONT_SIZE_INDEX]
2297 = make_number (font_pixel_size (XFRAME (frame), prefer));
2298
72d36834
KH
2299 if (NILP (XCDR (list)))
2300 {
2301 /* What we have to take care of is this single vector. */
2302 vec = XCAR (list);
2303 maxlen = ASIZE (vec);
2304 }
2305 else if (best_only)
2306 {
2307 /* We don't have to perform sort, so there's no need of creating
2308 a single vector. But, we must find the length of the longest
2309 vector. */
2310 maxlen = 0;
2311 for (tail = list; CONSP (tail); tail = XCDR (tail))
2312 if (maxlen < ASIZE (XCAR (tail)))
2313 maxlen = ASIZE (XCAR (tail));
2314 }
2315 else
2316 {
2317 /* We have to create a single vector to sort it. */
2318 vec = font_vconcat_entity_vectors (list);
2319 maxlen = ASIZE (vec);
2320 }
2321
2322 SAFE_ALLOCA (data, struct font_sort_data *, (sizeof *data) * maxlen);
c9477f01
KH
2323 best_score = 0xFFFFFFFF;
2324 best_entity = Qnil;
72d36834
KH
2325
2326 for (tail = list; CONSP (tail); tail = XCDR (tail))
c2f5bfd6 2327 {
72d36834
KH
2328 int font_driver_preference = 0;
2329 Lisp_Object current_font_driver;
ce75f06e 2330
72d36834
KH
2331 if (best_only)
2332 vec = XCAR (tail);
2333 len = ASIZE (vec);
2334
2335 /* We are sure that the length of VEC > 0. */
2336 current_font_driver = AREF (AREF (vec, 0), FONT_TYPE_INDEX);
2337 /* Score the elements. */
2338 for (i = 0; i < len; i++)
35027d0c 2339 {
72d36834
KH
2340 data[i].entity = AREF (vec, i);
2341 data[i].score
2342 = ((best_only <= 0 || font_has_char (f, data[i].entity, best_only)
2343 > 0)
2344 ? font_score (data[i].entity, prefer_prop)
2345 : 0xFFFFFFFF);
2346 if (best_only && best_score > data[i].score)
2347 {
2348 best_score = data[i].score;
2349 best_entity = data[i].entity;
2350 if (best_score == 0)
2351 break;
2352 }
2353 if (! EQ (current_font_driver, AREF (AREF (vec, i), FONT_TYPE_INDEX)))
2354 {
2355 current_font_driver = AREF (AREF (vec, i), FONT_TYPE_INDEX);
2356 font_driver_preference++;
2357 }
2358 data[i].font_driver_preference = font_driver_preference;
35027d0c 2359 }
72d36834
KH
2360
2361 /* Sort if necessary. */
2362 if (! best_only)
2363 {
2364 qsort (data, len, sizeof *data, font_compare);
2365 for (i = 0; i < len; i++)
2366 ASET (vec, i, data[i].entity);
2367 break;
2368 }
2369 else
2370 vec = best_entity;
c2f5bfd6 2371 }
72d36834 2372
c2f5bfd6
KH
2373 SAFE_FREE ();
2374
652b9560 2375 FONT_ADD_LOG ("sort-by", prefer, vec);
c2f5bfd6
KH
2376 return vec;
2377}
2378
2379\f
2380/* API of Font Service Layer. */
2381
45eb10fb
KH
2382/* Reflect ORDER (see the variable font_sort_order in xfaces.c) to
2383 sort_shift_bits. Finternal_set_font_selection_order calls this
2384 function with font_sort_order after setting up it. */
2385
c2f5bfd6 2386void
971de7fb 2387font_update_sort_order (int *order)
c2f5bfd6 2388{
35027d0c 2389 int i, shift_bits;
c2f5bfd6 2390
943b7eea 2391 for (i = 0, shift_bits = 23; i < 4; i++, shift_bits -= 7)
c2f5bfd6
KH
2392 {
2393 int xlfd_idx = order[i];
2394
2395 if (xlfd_idx == XLFD_WEIGHT_INDEX)
2396 sort_shift_bits[FONT_WEIGHT_INDEX] = shift_bits;
2397 else if (xlfd_idx == XLFD_SLANT_INDEX)
2398 sort_shift_bits[FONT_SLANT_INDEX] = shift_bits;
2399 else if (xlfd_idx == XLFD_SWIDTH_INDEX)
2400 sort_shift_bits[FONT_WIDTH_INDEX] = shift_bits;
2401 else
2402 sort_shift_bits[FONT_SIZE_INDEX] = shift_bits;
2403 }
2404}
2405
51c13510 2406static int
971de7fb 2407font_check_otf_features (Lisp_Object script, Lisp_Object langsys, Lisp_Object features, Lisp_Object table)
51c13510
KH
2408{
2409 Lisp_Object val;
2410 int negative;
2411
2412 table = assq_no_quit (script, table);
2413 if (NILP (table))
2414 return 0;
2415 table = XCDR (table);
2416 if (! NILP (langsys))
2417 {
2418 table = assq_no_quit (langsys, table);
2419 if (NILP (table))
2420 return 0;
2421 }
2422 else
2423 {
2424 val = assq_no_quit (Qnil, table);
2425 if (NILP (val))
2426 table = XCAR (table);
2427 else
2428 table = val;
2429 }
2430 table = XCDR (table);
2431 for (negative = 0; CONSP (features); features = XCDR (features))
2432 {
2433 if (NILP (XCAR (features)))
c423ecca
KH
2434 {
2435 negative = 1;
2436 continue;
2437 }
51c13510
KH
2438 if (NILP (Fmemq (XCAR (features), table)) != negative)
2439 return 0;
2440 }
2441 return 1;
2442}
2443
2444/* Check if OTF_CAPABILITY satisfies SPEC (otf-spec). */
2445
2446static int
3cba9369 2447font_check_otf (Lisp_Object spec, Lisp_Object otf_capability)
51c13510
KH
2448{
2449 Lisp_Object script, langsys = Qnil, gsub = Qnil, gpos = Qnil;
2450
2451 script = XCAR (spec);
2452 spec = XCDR (spec);
2453 if (! NILP (spec))
2454 {
2455 langsys = XCAR (spec);
2456 spec = XCDR (spec);
2457 if (! NILP (spec))
2458 {
2459 gsub = XCAR (spec);
2460 spec = XCDR (spec);
2461 if (! NILP (spec))
2462 gpos = XCAR (spec);
2463 }
2464 }
2465
2466 if (! NILP (gsub) && ! font_check_otf_features (script, langsys, gsub,
2467 XCAR (otf_capability)))
2468 return 0;
2469 if (! NILP (gpos) && ! font_check_otf_features (script, langsys, gpos,
2470 XCDR (otf_capability)))
2471 return 0;
2472 return 1;
2473}
2474
45eb10fb 2475
51c13510
KH
2476
2477/* Check if FONT (font-entity or font-object) matches with the font
2478 specification SPEC. */
45eb10fb 2479
ef18374f 2480int
971de7fb 2481font_match_p (Lisp_Object spec, Lisp_Object font)
ef18374f 2482{
51c13510
KH
2483 Lisp_Object prop[FONT_SPEC_MAX], *props;
2484 Lisp_Object extra, font_extra;
ef18374f
KH
2485 int i;
2486
51c13510
KH
2487 for (i = FONT_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX; i++)
2488 if (! NILP (AREF (spec, i))
2489 && ! NILP (AREF (font, i))
2490 && ! EQ (AREF (spec, i), AREF (font, i)))
2491 return 0;
2492 props = XFONT_SPEC (spec)->props;
2493 if (FLOATP (props[FONT_SIZE_INDEX]))
2494 {
2495 for (i = FONT_FOUNDRY_INDEX; i < FONT_SIZE_INDEX; i++)
2496 prop[i] = AREF (spec, i);
2497 prop[FONT_SIZE_INDEX]
2498 = make_number (font_pixel_size (XFRAME (selected_frame), spec));
2499 props = prop;
2500 }
2501
2502 if (font_score (font, props) > 0)
2503 return 0;
2504 extra = AREF (spec, FONT_EXTRA_INDEX);
2505 font_extra = AREF (font, FONT_EXTRA_INDEX);
2506 for (; CONSP (extra); extra = XCDR (extra))
35027d0c 2507 {
51c13510
KH
2508 Lisp_Object key = XCAR (XCAR (extra));
2509 Lisp_Object val = XCDR (XCAR (extra)), val2;
2510
2511 if (EQ (key, QClang))
2512 {
2513 val2 = assq_no_quit (key, font_extra);
2514 if (NILP (val2))
2515 return 0;
2516 val2 = XCDR (val2);
2517 if (CONSP (val))
2518 {
2519 if (! CONSP (val2))
2520 return 0;
2521 while (CONSP (val))
2522 if (NILP (Fmemq (val, val2)))
2523 return 0;
2524 }
2525 else
2526 if (CONSP (val2)
2527 ? NILP (Fmemq (val, XCDR (val2)))
2528 : ! EQ (val, val2))
2529 return 0;
2530 }
2531 else if (EQ (key, QCscript))
2532 {
2533 val2 = assq_no_quit (val, Vscript_representative_chars);
5bdd4dd2
KH
2534 if (CONSP (val2))
2535 {
2536 val2 = XCDR (val2);
2537 if (CONSP (val2))
2538 {
2539 /* All characters in the list must be supported. */
2540 for (; CONSP (val2); val2 = XCDR (val2))
2541 {
2542 if (! NATNUMP (XCAR (val2)))
2543 continue;
2544 if (font_encode_char (font, XFASTINT (XCAR (val2)))
2545 == FONT_INVALID_CODE)
2546 return 0;
2547 }
2548 }
2549 else if (VECTORP (val2))
2550 {
2551 /* At most one character in the vector must be supported. */
2552 for (i = 0; i < ASIZE (val2); i++)
2553 {
2554 if (! NATNUMP (AREF (val2, i)))
2555 continue;
2556 if (font_encode_char (font, XFASTINT (AREF (val2, i)))
2557 != FONT_INVALID_CODE)
f5485732 2558 break;
5bdd4dd2
KH
2559 }
2560 if (i == ASIZE (val2))
2561 return 0;
2562 }
2563 }
51c13510
KH
2564 }
2565 else if (EQ (key, QCotf))
2566 {
2567 struct font *fontp;
2568
2569 if (! FONT_OBJECT_P (font))
2570 return 0;
2571 fontp = XFONT_OBJECT (font);
2572 if (! fontp->driver->otf_capability)
2573 return 0;
2574 val2 = fontp->driver->otf_capability (fontp);
2575 if (NILP (val2) || ! font_check_otf (val, val2))
2576 return 0;
2577 }
35027d0c
KH
2578 }
2579
51c13510 2580 return 1;
ef18374f 2581}
ca4da08a 2582\f
819e81df 2583
ca4da08a
KH
2584/* Font cache
2585
2586 Each font backend has the callback function get_cache, and it
2587 returns a cons cell of which cdr part can be freely used for
2588 caching fonts. The cons cell may be shared by multiple frames
2589 and/or multiple font drivers. So, we arrange the cdr part as this:
2590
2591 ((DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) ...)
2592
2593 where DRIVER-TYPE is a symbol such as `x', `xft', etc., NUM-FRAMES
2594 is a number frames sharing this cache, and FONT-CACHE-DATA is a
2595 cons (FONT-SPEC FONT-ENTITY ...). */
2596
f57e2426
J
2597static void font_prepare_cache (FRAME_PTR, struct font_driver *);
2598static void font_finish_cache (FRAME_PTR, struct font_driver *);
2599static Lisp_Object font_get_cache (FRAME_PTR, struct font_driver *);
2600static void font_clear_cache (FRAME_PTR, Lisp_Object,
2601 struct font_driver *);
ca4da08a
KH
2602
2603static void
971de7fb 2604font_prepare_cache (FRAME_PTR f, struct font_driver *driver)
ca4da08a
KH
2605{
2606 Lisp_Object cache, val;
2607
2608 cache = driver->get_cache (f);
2609 val = XCDR (cache);
2610 while (CONSP (val) && ! EQ (XCAR (XCAR (val)), driver->type))
2611 val = XCDR (val);
2612 if (NILP (val))
2613 {
2614 val = Fcons (driver->type, Fcons (make_number (1), Qnil));
2615 XSETCDR (cache, Fcons (val, XCDR (cache)));
2616 }
2617 else
2618 {
2619 val = XCDR (XCAR (val));
2620 XSETCAR (val, make_number (XINT (XCAR (val)) + 1));
2621 }
2622}
2623
43c0454d 2624
ca4da08a 2625static void
971de7fb 2626font_finish_cache (FRAME_PTR f, struct font_driver *driver)
ca4da08a
KH
2627{
2628 Lisp_Object cache, val, tmp;
2629
2630
2631 cache = driver->get_cache (f);
2632 val = XCDR (cache);
2633 while (CONSP (val) && ! EQ (XCAR (XCAR (val)), driver->type))
2634 cache = val, val = XCDR (val);
d0ab1ebe 2635 font_assert (! NILP (val));
ca4da08a 2636 tmp = XCDR (XCAR (val));
43c0454d 2637 XSETCAR (tmp, make_number (XINT (XCAR (tmp)) - 1));
ca4da08a
KH
2638 if (XINT (XCAR (tmp)) == 0)
2639 {
2640 font_clear_cache (f, XCAR (val), driver);
2641 XSETCDR (cache, XCDR (val));
2642 }
ca4da08a
KH
2643}
2644
43c0454d 2645
ca4da08a 2646static Lisp_Object
971de7fb 2647font_get_cache (FRAME_PTR f, struct font_driver *driver)
ca4da08a
KH
2648{
2649 Lisp_Object val = driver->get_cache (f);
2650 Lisp_Object type = driver->type;
2651
d0ab1ebe 2652 font_assert (CONSP (val));
ca4da08a 2653 for (val = XCDR (val); ! EQ (XCAR (XCAR (val)), type); val = XCDR (val));
d0ab1ebe 2654 font_assert (CONSP (val));
ca4da08a
KH
2655 /* VAL = ((DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) ...) */
2656 val = XCDR (XCAR (val));
2657 return val;
2658}
2659
43c0454d
KH
2660static int num_fonts;
2661
ca4da08a 2662static void
971de7fb 2663font_clear_cache (FRAME_PTR f, Lisp_Object cache, struct font_driver *driver)
ca4da08a
KH
2664{
2665 Lisp_Object tail, elt;
6136b72f 2666 Lisp_Object tail2, entity;
51c01100 2667
ca4da08a
KH
2668 /* CACHE = (DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) */
2669 for (tail = XCDR (XCDR (cache)); CONSP (tail); tail = XCDR (tail))
2670 {
2671 elt = XCAR (tail);
6136b72f
CY
2672 /* elt should have the form (FONT-SPEC FONT-ENTITY ...) */
2673 if (CONSP (elt) && FONT_SPEC_P (XCAR (elt)))
ca4da08a 2674 {
6136b72f 2675 for (tail2 = XCDR (elt); CONSP (tail2); tail2 = XCDR (tail2))
ca4da08a 2676 {
6136b72f 2677 entity = XCAR (tail2);
ca4da08a 2678
6136b72f
CY
2679 if (FONT_ENTITY_P (entity)
2680 && EQ (driver->type, AREF (entity, FONT_TYPE_INDEX)))
ca4da08a
KH
2681 {
2682 Lisp_Object objlist = AREF (entity, FONT_OBJLIST_INDEX);
2683
2684 for (; CONSP (objlist); objlist = XCDR (objlist))
2685 {
2686 Lisp_Object val = XCAR (objlist);
35027d0c 2687 struct font *font = XFONT_OBJECT (val);
ca4da08a 2688
5e634ec9
KH
2689 if (! NILP (AREF (val, FONT_TYPE_INDEX)))
2690 {
2691 font_assert (font && driver == font->driver);
2692 driver->close (f, font);
2693 num_fonts--;
2694 }
ca4da08a
KH
2695 }
2696 if (driver->free_entity)
2697 driver->free_entity (entity);
2698 }
2699 }
2700 }
2701 }
2702 XSETCDR (cache, Qnil);
2703}
2704\f
2705
c2f5bfd6
KH
2706static Lisp_Object scratch_font_spec, scratch_font_prefer;
2707
7d56b2dd
KH
2708/* Check each font-entity in VEC, and return a list of font-entities
2709 that satisfy this condition:
2710 (1) matches with SPEC and SIZE if SPEC is not nil, and
2711 (2) doesn't match with any regexps in Vface_ignored_fonts (if non-nil).
2712*/
2713
35027d0c 2714Lisp_Object
971de7fb 2715font_delete_unmatched (Lisp_Object vec, Lisp_Object spec, int size)
35027d0c 2716{
d0ab1ebe 2717 Lisp_Object entity, val;
35027d0c 2718 enum font_property_index prop;
72d36834 2719 int i;
45eb10fb 2720
72d36834 2721 for (val = Qnil, i = ASIZE (vec) - 1; i >= 0; i--)
35027d0c 2722 {
72d36834 2723 entity = AREF (vec, i);
7d56b2dd
KH
2724 if (! NILP (Vface_ignored_fonts))
2725 {
2726 char name[256];
2727 Lisp_Object tail, regexp;
2728
2729 if (font_unparse_xlfd (entity, 0, name, 256) >= 0)
2730 {
2731 for (tail = Vface_ignored_fonts; CONSP (tail); tail = XCDR (tail))
2732 {
2733 regexp = XCAR (tail);
2734 if (STRINGP (regexp)
2735 && fast_c_string_match_ignore_case (regexp, name) >= 0)
2736 break;
2737 }
2738 if (CONSP (tail))
2739 continue;
2740 }
2741 }
2742 if (NILP (spec))
2743 {
2744 val = Fcons (entity, val);
2745 continue;
2746 }
35027d0c
KH
2747 for (prop = FONT_WEIGHT_INDEX; prop < FONT_SIZE_INDEX; prop++)
2748 if (INTEGERP (AREF (spec, prop))
2749 && ((XINT (AREF (spec, prop)) >> 8)
2750 != (XINT (AREF (entity, prop)) >> 8)))
2751 prop = FONT_SPEC_MAX;
7181ea6a 2752 if (prop < FONT_SPEC_MAX
35027d0c
KH
2753 && size
2754 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
2755 {
2756 int diff = XINT (AREF (entity, FONT_SIZE_INDEX)) - size;
c2f5bfd6 2757
35027d0c
KH
2758 if (diff != 0
2759 && (diff < 0 ? -diff > FONT_PIXEL_SIZE_QUANTUM
2760 : diff > FONT_PIXEL_SIZE_QUANTUM))
2761 prop = FONT_SPEC_MAX;
2762 }
7181ea6a
KH
2763 if (prop < FONT_SPEC_MAX
2764 && INTEGERP (AREF (spec, FONT_DPI_INDEX))
2765 && INTEGERP (AREF (entity, FONT_DPI_INDEX))
c576d6dc 2766 && XINT (AREF (entity, FONT_DPI_INDEX)) != 0
7181ea6a
KH
2767 && ! EQ (AREF (spec, FONT_DPI_INDEX), AREF (entity, FONT_DPI_INDEX)))
2768 prop = FONT_SPEC_MAX;
2769 if (prop < FONT_SPEC_MAX
2770 && INTEGERP (AREF (spec, FONT_AVGWIDTH_INDEX))
2771 && INTEGERP (AREF (entity, FONT_AVGWIDTH_INDEX))
c576d6dc 2772 && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) != 0
7181ea6a
KH
2773 && ! EQ (AREF (spec, FONT_AVGWIDTH_INDEX),
2774 AREF (entity, FONT_AVGWIDTH_INDEX)))
2775 prop = FONT_SPEC_MAX;
35027d0c 2776 if (prop < FONT_SPEC_MAX)
d0ab1ebe 2777 val = Fcons (entity, val);
35027d0c 2778 }
72d36834 2779 return (Fvconcat (1, &val));
35027d0c
KH
2780}
2781
2782
72d36834 2783/* Return a list of vectors of font-entities matching with SPEC on
ce75f06e
CY
2784 FRAME. Each elements in the list is a vector of entities from the
2785 same font-driver. */
35027d0c
KH
2786
2787Lisp_Object
971de7fb 2788font_list_entities (Lisp_Object frame, Lisp_Object spec)
c2f5bfd6
KH
2789{
2790 FRAME_PTR f = XFRAME (frame);
2791 struct font_driver_list *driver_list = f->font_driver_list;
4007dd1c 2792 Lisp_Object ftype, val;
72d36834 2793 Lisp_Object list = Qnil;
35027d0c
KH
2794 int size;
2795 int need_filtering = 0;
c2f5bfd6
KH
2796 int i;
2797
d0ab1ebe 2798 font_assert (FONT_SPEC_P (spec));
c2f5bfd6 2799
35027d0c
KH
2800 if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
2801 size = XINT (AREF (spec, FONT_SIZE_INDEX));
2802 else if (FLOATP (AREF (spec, FONT_SIZE_INDEX)))
2803 size = font_pixel_size (f, spec);
2804 else
2805 size = 0;
2806
c2f5bfd6 2807 ftype = AREF (spec, FONT_TYPE_INDEX);
4007dd1c 2808 for (i = FONT_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX; i++)
35027d0c 2809 ASET (scratch_font_spec, i, AREF (spec, i));
4007dd1c 2810 for (i = FONT_WEIGHT_INDEX; i < FONT_EXTRA_INDEX; i++)
35027d0c
KH
2811 {
2812 ASET (scratch_font_spec, i, Qnil);
2813 if (! NILP (AREF (spec, i)))
2814 need_filtering = 1;
4007dd1c
KH
2815 if (i == FONT_DPI_INDEX)
2816 /* Skip FONT_SPACING_INDEX */
2817 i++;
35027d0c 2818 }
aa50ca2f 2819 ASET (scratch_font_spec, FONT_SPACING_INDEX, AREF (spec, FONT_SPACING_INDEX));
35027d0c
KH
2820 ASET (scratch_font_spec, FONT_EXTRA_INDEX, AREF (spec, FONT_EXTRA_INDEX));
2821
c2f5bfd6 2822 for (i = 0; driver_list; driver_list = driver_list->next)
417a1b10
KH
2823 if (driver_list->on
2824 && (NILP (ftype) || EQ (driver_list->driver->type, ftype)))
c2f5bfd6 2825 {
ca4da08a 2826 Lisp_Object cache = font_get_cache (f, driver_list->driver);
c2f5bfd6 2827
e4c93315 2828 ASET (scratch_font_spec, FONT_TYPE_INDEX, driver_list->driver->type);
4007dd1c
KH
2829 val = assoc_no_quit (scratch_font_spec, XCDR (cache));
2830 if (CONSP (val))
2831 val = XCDR (val);
2832 else
c2f5bfd6 2833 {
4007dd1c 2834 Lisp_Object copy;
35027d0c 2835
4007dd1c 2836 val = driver_list->driver->list (frame, scratch_font_spec);
72d36834
KH
2837 if (NILP (val))
2838 val = null_vector;
2839 else
2840 val = Fvconcat (1, &val);
4007dd1c
KH
2841 copy = Fcopy_font_spec (scratch_font_spec);
2842 ASET (copy, FONT_TYPE_INDEX, driver_list->driver->type);
2843 XSETCDR (cache, Fcons (Fcons (copy, val), XCDR (cache)));
c2f5bfd6 2844 }
7d56b2dd
KH
2845 if (ASIZE (val) > 0
2846 && (need_filtering
2847 || ! NILP (Vface_ignored_fonts)))
2848 val = font_delete_unmatched (val, need_filtering ? spec : Qnil, size);
72d36834
KH
2849 if (ASIZE (val) > 0)
2850 list = Fcons (val, list);
c2f5bfd6 2851 }
35027d0c 2852
72d36834
KH
2853 list = Fnreverse (list);
2854 FONT_ADD_LOG ("list", spec, list);
2855 return list;
c2f5bfd6
KH
2856}
2857
45eb10fb 2858
35027d0c
KH
2859/* Return a font entity matching with SPEC on FRAME. ATTRS, if non
2860 nil, is an array of face's attributes, which specifies preferred
2861 font-related attributes. */
45eb10fb 2862
e950d6f1 2863static Lisp_Object
971de7fb 2864font_matching_entity (FRAME_PTR f, Lisp_Object *attrs, Lisp_Object spec)
e950d6f1 2865{
e950d6f1
KH
2866 struct font_driver_list *driver_list = f->font_driver_list;
2867 Lisp_Object ftype, size, entity;
35027d0c 2868 Lisp_Object frame;
819ab95f 2869 Lisp_Object work = Fcopy_font_spec (spec);
e950d6f1 2870
35027d0c 2871 XSETFRAME (frame, f);
e950d6f1
KH
2872 ftype = AREF (spec, FONT_TYPE_INDEX);
2873 size = AREF (spec, FONT_SIZE_INDEX);
819ab95f 2874
8d0e382e
AR
2875 if (FLOATP (size))
2876 ASET (work, FONT_SIZE_INDEX, make_number (font_pixel_size (f, spec)));
819ab95f
KH
2877 FONT_SET_STYLE (work, FONT_WEIGHT_INDEX, attrs[LFACE_WEIGHT_INDEX]);
2878 FONT_SET_STYLE (work, FONT_SLANT_INDEX, attrs[LFACE_SLANT_INDEX]);
2879 FONT_SET_STYLE (work, FONT_WIDTH_INDEX, attrs[LFACE_SWIDTH_INDEX]);
2880
e950d6f1
KH
2881 entity = Qnil;
2882 for (; driver_list; driver_list = driver_list->next)
2883 if (driver_list->on
2884 && (NILP (ftype) || EQ (driver_list->driver->type, ftype)))
2885 {
ca4da08a 2886 Lisp_Object cache = font_get_cache (f, driver_list->driver);
7cee5d63 2887 Lisp_Object copy;
e950d6f1 2888
819ab95f
KH
2889 ASET (work, FONT_TYPE_INDEX, driver_list->driver->type);
2890 entity = assoc_no_quit (work, XCDR (cache));
7cee5d63 2891 if (CONSP (entity))
e950d6f1
KH
2892 entity = XCDR (entity);
2893 else
2894 {
819ab95f
KH
2895 entity = driver_list->driver->match (frame, work);
2896 copy = Fcopy_font_spec (work);
7cee5d63
KH
2897 ASET (copy, FONT_TYPE_INDEX, driver_list->driver->type);
2898 XSETCDR (cache, Fcons (Fcons (copy, entity), XCDR (cache)));
e950d6f1
KH
2899 }
2900 if (! NILP (entity))
2901 break;
2902 }
652b9560 2903 FONT_ADD_LOG ("match", work, entity);
e950d6f1
KH
2904 return entity;
2905}
2906
45eb10fb
KH
2907
2908/* Open a font of ENTITY and PIXEL_SIZE on frame F, and return the
2909 opened font object. */
2910
c2f5bfd6 2911static Lisp_Object
971de7fb 2912font_open_entity (FRAME_PTR f, Lisp_Object entity, int pixel_size)
c2f5bfd6
KH
2913{
2914 struct font_driver_list *driver_list;
43c0454d 2915 Lisp_Object objlist, size, val, font_object;
c2f5bfd6 2916 struct font *font;
d26424c5 2917 int min_width, height;
a35dd56b 2918 int scaled_pixel_size;
c2f5bfd6 2919
d0ab1ebe 2920 font_assert (FONT_ENTITY_P (entity));
c2f5bfd6 2921 size = AREF (entity, FONT_SIZE_INDEX);
c2f5bfd6 2922 if (XINT (size) != 0)
a35dd56b 2923 scaled_pixel_size = pixel_size = XINT (size);
55e41770 2924 else if (CONSP (Vface_font_rescale_alist))
a35dd56b 2925 scaled_pixel_size = pixel_size * font_rescale_ratio (entity);
c2f5bfd6 2926
35027d0c
KH
2927 val = AREF (entity, FONT_TYPE_INDEX);
2928 for (driver_list = f->font_driver_list;
2929 driver_list && ! EQ (driver_list->driver->type, val);
2930 driver_list = driver_list->next);
2931 if (! driver_list)
2932 return Qnil;
c2f5bfd6 2933
d0cf45b7
JD
2934 for (objlist = AREF (entity, FONT_OBJLIST_INDEX); CONSP (objlist);
2935 objlist = XCDR (objlist))
2936 {
2937 Lisp_Object fn = XCAR (objlist);
2938 if (! NILP (AREF (fn, FONT_TYPE_INDEX))
2939 && XFONT_OBJECT (fn)->pixel_size == pixel_size)
2940 {
2941 if (driver_list->driver->cached_font_ok == NULL
2942 || driver_list->driver->cached_font_ok (f, fn, entity))
2943 return fn;
2944 }
2945 }
2946
a35dd56b 2947 font_object = driver_list->driver->open (f, entity, scaled_pixel_size);
66f5ced0
YM
2948 if (!NILP (font_object))
2949 ASET (font_object, FONT_SIZE_INDEX, make_number (pixel_size));
652b9560 2950 FONT_ADD_LOG ("open", entity, font_object);
43c0454d 2951 if (NILP (font_object))
35027d0c
KH
2952 return Qnil;
2953 ASET (entity, FONT_OBJLIST_INDEX,
2954 Fcons (font_object, AREF (entity, FONT_OBJLIST_INDEX)));
35027d0c
KH
2955 num_fonts++;
2956
2957 font = XFONT_OBJECT (font_object);
2958 min_width = (font->min_width ? font->min_width
2959 : font->average_width ? font->average_width
2960 : font->space_width ? font->space_width
2961 : 1);
d26424c5 2962 height = (font->height ? font->height : 1);
819e81df 2963#ifdef HAVE_WINDOW_SYSTEM
35027d0c
KH
2964 FRAME_X_DISPLAY_INFO (f)->n_fonts++;
2965 if (FRAME_X_DISPLAY_INFO (f)->n_fonts == 1)
43c0454d 2966 {
35027d0c 2967 FRAME_SMALLEST_CHAR_WIDTH (f) = min_width;
d26424c5 2968 FRAME_SMALLEST_FONT_HEIGHT (f) = height;
35027d0c
KH
2969 fonts_changed_p = 1;
2970 }
2971 else
2972 {
2973 if (FRAME_SMALLEST_CHAR_WIDTH (f) > min_width)
2974 FRAME_SMALLEST_CHAR_WIDTH (f) = min_width, fonts_changed_p = 1;
d26424c5
KH
2975 if (FRAME_SMALLEST_FONT_HEIGHT (f) > height)
2976 FRAME_SMALLEST_FONT_HEIGHT (f) = height, fonts_changed_p = 1;
43c0454d 2977 }
819e81df 2978#endif
43c0454d
KH
2979
2980 return font_object;
c2f5bfd6
KH
2981}
2982
45eb10fb
KH
2983
2984/* Close FONT_OBJECT that is opened on frame F. */
2985
c2f5bfd6 2986void
971de7fb 2987font_close_object (FRAME_PTR f, Lisp_Object font_object)
c2f5bfd6 2988{
35027d0c 2989 struct font *font = XFONT_OBJECT (font_object);
c2f5bfd6 2990
5e634ec9
KH
2991 if (NILP (AREF (font_object, FONT_TYPE_INDEX)))
2992 /* Already closed. */
2993 return;
652b9560 2994 FONT_ADD_LOG ("close", font_object, Qnil);
5e634ec9 2995 font->driver->close (f, font);
819e81df 2996#ifdef HAVE_WINDOW_SYSTEM
5e634ec9
KH
2997 font_assert (FRAME_X_DISPLAY_INFO (f)->n_fonts);
2998 FRAME_X_DISPLAY_INFO (f)->n_fonts--;
819e81df 2999#endif
5e634ec9 3000 num_fonts--;
c2f5bfd6
KH
3001}
3002
45eb10fb 3003
1701724c
KH
3004/* Return 1 if FONT on F has a glyph for character C, 0 if not, -1 if
3005 FONT is a font-entity and it must be opened to check. */
45eb10fb 3006
c2f5bfd6 3007int
971de7fb 3008font_has_char (FRAME_PTR f, Lisp_Object font, int c)
c2f5bfd6 3009{
1b834a8d 3010 struct font *fontp;
c2f5bfd6 3011
1b834a8d
KH
3012 if (FONT_ENTITY_P (font))
3013 {
3014 Lisp_Object type = AREF (font, FONT_TYPE_INDEX);
3015 struct font_driver_list *driver_list;
3016
3017 for (driver_list = f->font_driver_list;
3018 driver_list && ! EQ (driver_list->driver->type, type);
3019 driver_list = driver_list->next);
3020 if (! driver_list)
3021 return 0;
3022 if (! driver_list->driver->has_char)
3023 return -1;
3024 return driver_list->driver->has_char (font, c);
3025 }
3026
d0ab1ebe 3027 font_assert (FONT_OBJECT_P (font));
35027d0c 3028 fontp = XFONT_OBJECT (font);
1b834a8d
KH
3029 if (fontp->driver->has_char)
3030 {
35027d0c 3031 int result = fontp->driver->has_char (font, c);
1b834a8d
KH
3032
3033 if (result >= 0)
3034 return result;
3035 }
3036 return (fontp->driver->encode_char (fontp, c) != FONT_INVALID_CODE);
c2f5bfd6
KH
3037}
3038
45eb10fb
KH
3039
3040/* Return the glyph ID of FONT_OBJECT for character C. */
3041
c2f5bfd6 3042unsigned
971de7fb 3043font_encode_char (Lisp_Object font_object, int c)
c2f5bfd6 3044{
35027d0c 3045 struct font *font;
c2f5bfd6 3046
d0ab1ebe 3047 font_assert (FONT_OBJECT_P (font_object));
35027d0c 3048 font = XFONT_OBJECT (font_object);
c2f5bfd6
KH
3049 return font->driver->encode_char (font, c);
3050}
3051
45eb10fb
KH
3052
3053/* Return the name of FONT_OBJECT. */
3054
ef18374f 3055Lisp_Object
971de7fb 3056font_get_name (Lisp_Object font_object)
c2f5bfd6 3057{
d0ab1ebe 3058 font_assert (FONT_OBJECT_P (font_object));
35027d0c 3059 return AREF (font_object, FONT_NAME_INDEX);
ef18374f
KH
3060}
3061
45eb10fb
KH
3062
3063/* Return the specification of FONT_OBJECT. */
3064
ef18374f 3065Lisp_Object
971de7fb 3066font_get_spec (Lisp_Object font_object)
ef18374f 3067{
35027d0c 3068 Lisp_Object spec = font_make_spec ();
ef18374f
KH
3069 int i;
3070
3071 for (i = 0; i < FONT_SIZE_INDEX; i++)
35027d0c
KH
3072 ASET (spec, i, AREF (font_object, i));
3073 ASET (spec, FONT_SIZE_INDEX,
3074 make_number (XFONT_OBJECT (font_object)->pixel_size));
ef18374f 3075 return spec;
c2f5bfd6
KH
3076}
3077
05802645
CY
3078
3079/* Create a new font spec from FONT_NAME, and return it. If FONT_NAME
3080 could not be parsed by font_parse_name, return Qnil. */
3081
35027d0c 3082Lisp_Object
971de7fb 3083font_spec_from_name (Lisp_Object font_name)
35027d0c 3084{
05802645 3085 Lisp_Object spec = Ffont_spec (0, NULL);
35027d0c 3086
05802645
CY
3087 CHECK_STRING (font_name);
3088 if (font_parse_name ((char *) SDATA (font_name), spec) == -1)
3089 return Qnil;
3090 font_put_extra (spec, QCname, font_name);
42707278 3091 font_put_extra (spec, QCuser_spec, font_name);
05802645 3092 return spec;
35027d0c
KH
3093}
3094
45eb10fb 3095
35027d0c 3096void
971de7fb 3097font_clear_prop (Lisp_Object *attrs, enum font_property_index prop)
35027d0c
KH
3098{
3099 Lisp_Object font = attrs[LFACE_FONT_INDEX];
45eb10fb 3100
35027d0c
KH
3101 if (! FONTP (font))
3102 return;
42707278 3103
483670b5
KH
3104 if (! NILP (Ffont_get (font, QCname)))
3105 {
3106 font = Fcopy_font_spec (font);
3107 font_put_extra (font, QCname, Qnil);
3108 }
3109
35027d0c 3110 if (NILP (AREF (font, prop))
e234927a
CY
3111 && prop != FONT_FAMILY_INDEX
3112 && prop != FONT_FOUNDRY_INDEX
3113 && prop != FONT_WIDTH_INDEX
4007dd1c 3114 && prop != FONT_SIZE_INDEX)
35027d0c 3115 return;
483670b5
KH
3116 if (EQ (font, attrs[LFACE_FONT_INDEX]))
3117 font = Fcopy_font_spec (font);
35027d0c 3118 ASET (font, prop, Qnil);
4007dd1c 3119 if (prop == FONT_FAMILY_INDEX || prop == FONT_FOUNDRY_INDEX)
35027d0c 3120 {
4007dd1c 3121 if (prop == FONT_FAMILY_INDEX)
962e8aa9
CY
3122 {
3123 ASET (font, FONT_FOUNDRY_INDEX, Qnil);
3124 /* If we are setting the font family, we must also clear
3125 FONT_WIDTH_INDEX to avoid rejecting families that lack
3126 support for some widths. */
3127 ASET (font, FONT_WIDTH_INDEX, Qnil);
3128 }
35027d0c 3129 ASET (font, FONT_ADSTYLE_INDEX, Qnil);
4007dd1c 3130 ASET (font, FONT_REGISTRY_INDEX, Qnil);
35027d0c
KH
3131 ASET (font, FONT_SIZE_INDEX, Qnil);
3132 ASET (font, FONT_DPI_INDEX, Qnil);
3133 ASET (font, FONT_SPACING_INDEX, Qnil);
3134 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3135 }
3136 else if (prop == FONT_SIZE_INDEX)
3137 {
3138 ASET (font, FONT_DPI_INDEX, Qnil);
3139 ASET (font, FONT_SPACING_INDEX, Qnil);
3140 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3141 }
e234927a
CY
3142 else if (prop == FONT_WIDTH_INDEX)
3143 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
35027d0c
KH
3144 attrs[LFACE_FONT_INDEX] = font;
3145}
3146
3147void
971de7fb 3148font_update_lface (FRAME_PTR f, Lisp_Object *attrs)
c2f5bfd6 3149{
d0ab1ebe 3150 Lisp_Object spec;
35027d0c
KH
3151
3152 spec = attrs[LFACE_FONT_INDEX];
3153 if (! FONT_SPEC_P (spec))
3154 return;
3155
4007dd1c
KH
3156 if (! NILP (AREF (spec, FONT_FOUNDRY_INDEX)))
3157 attrs[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (spec, FONT_FOUNDRY_INDEX));
3158 if (! NILP (AREF (spec, FONT_FAMILY_INDEX)))
3159 attrs[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (spec, FONT_FAMILY_INDEX));
35027d0c
KH
3160 if (! NILP (AREF (spec, FONT_WEIGHT_INDEX)))
3161 attrs[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (spec);
3162 if (! NILP (AREF (spec, FONT_SLANT_INDEX)))
8510724d 3163 attrs[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (spec);
35027d0c
KH
3164 if (! NILP (AREF (spec, FONT_WIDTH_INDEX)))
3165 attrs[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (spec);
3166 if (! NILP (AREF (spec, FONT_SIZE_INDEX)))
3167 {
3168 int point;
3169
3170 if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
3171 {
3172 Lisp_Object val;
3173 int dpi = f->resy;
3174
3175 val = Ffont_get (spec, QCdpi);
3176 if (! NILP (val))
3177 dpi = XINT (val);
3178 point = PIXEL_TO_POINT (XINT (AREF (spec, FONT_SIZE_INDEX)) * 10,
3179 dpi);
2cf4d521 3180 attrs[LFACE_HEIGHT_INDEX] = make_number (point);
35027d0c
KH
3181 }
3182 else if (FLOATP (AREF (spec, FONT_SIZE_INDEX)))
2cf4d521
CY
3183 {
3184 point = XFLOAT_DATA (AREF (spec, FONT_SIZE_INDEX)) * 10;
3185 attrs[LFACE_HEIGHT_INDEX] = make_number (point);
3186 }
35027d0c 3187 }
c2f5bfd6
KH
3188}
3189
45eb10fb 3190
72d36834
KH
3191/* Selecte a font from ENTITIES (list of font-entity vectors) that
3192 supports C and matches best with ATTRS and PIXEL_SIZE. */
988a7ddb
KH
3193
3194static Lisp_Object
971de7fb 3195font_select_entity (Lisp_Object frame, Lisp_Object entities, Lisp_Object *attrs, int pixel_size, int c)
988a7ddb
KH
3196{
3197 Lisp_Object font_entity;
3198 Lisp_Object prefer;
988a7ddb
KH
3199 int result, i;
3200 FRAME_PTR f = XFRAME (frame);
3201
72d36834
KH
3202 if (NILP (XCDR (entities))
3203 && ASIZE (XCAR (entities)) == 1)
988a7ddb 3204 {
72d36834 3205 font_entity = AREF (XCAR (entities), 0);
988a7ddb
KH
3206 if (c < 0
3207 || (result = font_has_char (f, font_entity, c)) > 0)
3208 return font_entity;
3209 return Qnil;
3210 }
3211
3212 /* Sort fonts by properties specified in ATTRS. */
3213 prefer = scratch_font_prefer;
3214
3215 for (i = FONT_WEIGHT_INDEX; i <= FONT_SIZE_INDEX; i++)
3216 ASET (prefer, i, Qnil);
3217 if (FONTP (attrs[LFACE_FONT_INDEX]))
3218 {
3219 Lisp_Object face_font = attrs[LFACE_FONT_INDEX];
3220
3221 for (i = FONT_WEIGHT_INDEX; i <= FONT_SIZE_INDEX; i++)
3222 ASET (prefer, i, AREF (face_font, i));
3223 }
3224 if (NILP (AREF (prefer, FONT_WEIGHT_INDEX)))
3225 FONT_SET_STYLE (prefer, FONT_WEIGHT_INDEX, attrs[LFACE_WEIGHT_INDEX]);
3226 if (NILP (AREF (prefer, FONT_SLANT_INDEX)))
3227 FONT_SET_STYLE (prefer, FONT_SLANT_INDEX, attrs[LFACE_SLANT_INDEX]);
3228 if (NILP (AREF (prefer, FONT_WIDTH_INDEX)))
3229 FONT_SET_STYLE (prefer, FONT_WIDTH_INDEX, attrs[LFACE_SWIDTH_INDEX]);
3230 ASET (prefer, FONT_SIZE_INDEX, make_number (pixel_size));
988a7ddb 3231
2645d15b 3232 return font_sort_entities (entities, prefer, frame, c);
988a7ddb
KH
3233}
3234
35027d0c
KH
3235/* Return a font-entity satisfying SPEC and best matching with face's
3236 font related attributes in ATTRS. C, if not negative, is a
1701724c 3237 character that the entity must support. */
c2f5bfd6
KH
3238
3239Lisp_Object
971de7fb 3240font_find_for_lface (FRAME_PTR f, Lisp_Object *attrs, Lisp_Object spec, int c)
c2f5bfd6 3241{
4007dd1c 3242 Lisp_Object work;
72d36834 3243 Lisp_Object frame, entities, val;
22459668 3244 Lisp_Object size, foundry[3], *family, registry[3], adstyle[3];
1d1e1245 3245 int pixel_size;
72d36834 3246 int i, j, k, l;
d0a47776
KH
3247
3248 registry[0] = AREF (spec, FONT_REGISTRY_INDEX);
3249 if (NILP (registry[0]))
3250 {
edfda783 3251 registry[0] = DEFAULT_ENCODING;
d0a47776
KH
3252 registry[1] = Qascii_0;
3253 registry[2] = null_vector;
3254 }
3255 else
3256 registry[1] = null_vector;
c2f5bfd6 3257
4007dd1c 3258 if (c >= 0 && ! NILP (AREF (spec, FONT_REGISTRY_INDEX)))
fe5ddfbc 3259 {
35027d0c 3260 struct charset *encoding, *repertory;
1701724c 3261
4007dd1c
KH
3262 if (font_registry_charsets (AREF (spec, FONT_REGISTRY_INDEX),
3263 &encoding, &repertory) < 0)
35027d0c 3264 return Qnil;
72d36834
KH
3265 if (repertory
3266 && ENCODE_CHAR (repertory, c) == CHARSET_INVALID_CODE (repertory))
3267 return Qnil;
35027d0c
KH
3268 else if (c > encoding->max_char)
3269 return Qnil;
c2f5bfd6
KH
3270 }
3271
4007dd1c 3272 work = Fcopy_font_spec (spec);
227f12fa 3273 ASET (work, FONT_TYPE_INDEX, AREF (spec, FONT_TYPE_INDEX));
35027d0c
KH
3274 XSETFRAME (frame, f);
3275 size = AREF (spec, FONT_SIZE_INDEX);
1d1e1245
KH
3276 pixel_size = font_pixel_size (f, spec);
3277 if (pixel_size == 0)
3278 {
3279 double pt = XINT (attrs[LFACE_HEIGHT_INDEX]);
3280
3281 pixel_size = POINT_TO_PIXEL (pt / 10, f->resy);
3282 }
4007dd1c
KH
3283 ASET (work, FONT_SIZE_INDEX, Qnil);
3284 foundry[0] = AREF (work, FONT_FOUNDRY_INDEX);
3285 if (! NILP (foundry[0]))
3286 foundry[1] = null_vector;
3287 else if (STRINGP (attrs[LFACE_FOUNDRY_INDEX]))
3288 {
071132a9
KH
3289 val = attrs[LFACE_FOUNDRY_INDEX];
3290 foundry[0] = font_intern_prop ((char *) SDATA (val), SBYTES (val), 1);
4007dd1c
KH
3291 foundry[1] = Qnil;
3292 foundry[2] = null_vector;
3293 }
3294 else
3295 foundry[0] = Qnil, foundry[1] = null_vector;
3296
22459668
KH
3297 adstyle[0] = AREF (work, FONT_ADSTYLE_INDEX);
3298 if (! NILP (adstyle[0]))
3299 adstyle[1] = null_vector;
3300 else if (FONTP (attrs[LFACE_FONT_INDEX]))
3301 {
3302 Lisp_Object face_font = attrs[LFACE_FONT_INDEX];
3303
3304 if (! NILP (AREF (face_font, FONT_ADSTYLE_INDEX)))
3305 {
3306 adstyle[0] = AREF (face_font, FONT_ADSTYLE_INDEX);
3307 adstyle[1] = Qnil;
3308 adstyle[2] = null_vector;
3309 }
3310 else
3311 adstyle[0] = Qnil, adstyle[1] = null_vector;
3312 }
3313 else
3314 adstyle[0] = Qnil, adstyle[1] = null_vector;
3315
3316
4007dd1c
KH
3317 val = AREF (work, FONT_FAMILY_INDEX);
3318 if (NILP (val) && STRINGP (attrs[LFACE_FAMILY_INDEX]))
071132a9
KH
3319 {
3320 val = attrs[LFACE_FAMILY_INDEX];
3321 val = font_intern_prop ((char *) SDATA (val), SBYTES (val), 1);
3322 }
4007dd1c
KH
3323 if (NILP (val))
3324 {
3325 family = alloca ((sizeof family[0]) * 2);
3326 family[0] = Qnil;
3327 family[1] = null_vector; /* terminator. */
3328 }
3329 else
3330 {
3331 Lisp_Object alters
819ab95f 3332 = Fassoc_string (val, Vface_alternative_font_family_alist,
7107c29e 3333 /* Font family names are case-sensitive under NS. */
819ab95f
KH
3334#ifndef HAVE_NS
3335 Qt
3336#else
3337 Qnil
3338#endif
3339 );
4007dd1c
KH
3340
3341 if (! NILP (alters))
3342 {
3343 family = alloca ((sizeof family[0]) * (XINT (Flength (alters)) + 2));
3344 for (i = 0; CONSP (alters); i++, alters = XCDR (alters))
3345 family[i] = XCAR (alters);
3346 if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
3347 family[i++] = Qnil;
3348 family[i] = null_vector;
3349 }
3350 else
3351 {
3352 family = alloca ((sizeof family[0]) * 3);
3353 i = 0;
3354 family[i++] = val;
3355 if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
3356 family[i++] = Qnil;
3357 family[i] = null_vector;
3358 }
3359 }
3360
d0a47776 3361 for (i = 0; SYMBOLP (family[i]); i++)
4007dd1c 3362 {
d0a47776
KH
3363 ASET (work, FONT_FAMILY_INDEX, family[i]);
3364 for (j = 0; SYMBOLP (foundry[j]); j++)
4007dd1c 3365 {
d0a47776
KH
3366 ASET (work, FONT_FOUNDRY_INDEX, foundry[j]);
3367 for (k = 0; SYMBOLP (registry[k]); k++)
3368 {
904a2e0e 3369 ASET (work, FONT_REGISTRY_INDEX, registry[k]);
22459668
KH
3370 for (l = 0; SYMBOLP (adstyle[l]); l++)
3371 {
3372 ASET (work, FONT_ADSTYLE_INDEX, adstyle[l]);
3373 entities = font_list_entities (frame, work);
72d36834 3374 if (! NILP (entities))
988a7ddb
KH
3375 {
3376 val = font_select_entity (frame, entities,
3377 attrs, pixel_size, c);
3378 if (! NILP (val))
637fa988 3379 return val;
988a7ddb 3380 }
22459668 3381 }
d0a47776 3382 }
4007dd1c 3383 }
4007dd1c 3384 }
d0a47776 3385 return Qnil;
1701724c 3386}
45eb10fb
KH
3387
3388
c2f5bfd6 3389Lisp_Object
971de7fb 3390font_open_for_lface (FRAME_PTR f, Lisp_Object entity, Lisp_Object *attrs, Lisp_Object spec)
c2f5bfd6 3391{
9331887d 3392 int size;
c2f5bfd6 3393
4007dd1c
KH
3394 if (INTEGERP (AREF (entity, FONT_SIZE_INDEX))
3395 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
3396 size = XINT (AREF (entity, FONT_SIZE_INDEX));
3397 else if (FONT_SPEC_P (spec) && ! NILP (AREF (spec, FONT_SIZE_INDEX)))
1d1e1245 3398 size = font_pixel_size (f, spec);
733fd013
KH
3399 else
3400 {
50b0cd29
CY
3401 double pt;
3402 if (INTEGERP (attrs[LFACE_HEIGHT_INDEX]))
3403 pt = XINT (attrs[LFACE_HEIGHT_INDEX]);
3404 else
3405 {
3406 struct face *def = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3407 Lisp_Object height = def->lface[LFACE_HEIGHT_INDEX];
3408 if (INTEGERP (height))
3409 pt = XINT (height);
3410 else
3411 abort(); /* We should never end up here. */
3412 }
733fd013
KH
3413
3414 pt /= 10;
3415 size = POINT_TO_PIXEL (pt, f->resy);
ed96cde8
AR
3416#ifdef HAVE_NS
3417 if (size == 0)
3418 {
3419 Lisp_Object ffsize = get_frame_param(f, Qfontsize);
3420 size = NUMBERP (ffsize) ? POINT_TO_PIXEL (XINT (ffsize), f->resy) : 0;
3421 }
3422#endif
733fd013 3423 }
c2f5bfd6
KH
3424 return font_open_entity (f, entity, size);
3425}
3426
45eb10fb 3427
35027d0c
KH
3428/* Find a font satisfying SPEC and best matching with face's
3429 attributes in ATTRS on FRAME, and return the opened
3430 font-object. */
45eb10fb 3431
35027d0c 3432Lisp_Object
971de7fb 3433font_load_for_lface (FRAME_PTR f, Lisp_Object *attrs, Lisp_Object spec)
c2f5bfd6 3434{
637fa988 3435 Lisp_Object entity, name;
ef18374f 3436
908567ef 3437 entity = font_find_for_lface (f, attrs, spec, -1);
35027d0c 3438 if (NILP (entity))
ef18374f 3439 {
35027d0c
KH
3440 /* No font is listed for SPEC, but each font-backend may have
3441 the different criteria about "font matching". So, try
3442 it. */
3443 entity = font_matching_entity (f, attrs, spec);
3444 if (NILP (entity))
3445 return Qnil;
c2f5bfd6 3446 }
537b04b9 3447 /* Don't lose the original name that was put in initially. We need
637fa988
JD
3448 it to re-apply the font when font parameters (like hinting or dpi) have
3449 changed. */
3450 entity = font_open_for_lface (f, entity, attrs, spec);
8096a0ff
YM
3451 if (!NILP (entity))
3452 {
42707278
JD
3453 name = Ffont_get (spec, QCuser_spec);
3454 if (STRINGP (name)) font_put_extra (entity, QCuser_spec, name);
8096a0ff 3455 }
637fa988 3456 return entity;
c2f5bfd6
KH
3457}
3458
45eb10fb
KH
3459
3460/* Make FACE on frame F ready to use the font opened for FACE. */
3461
c2f5bfd6 3462void
971de7fb 3463font_prepare_for_face (FRAME_PTR f, struct face *face)
c2f5bfd6 3464{
35027d0c
KH
3465 if (face->font->driver->prepare_face)
3466 face->font->driver->prepare_face (f, face);
c2f5bfd6
KH
3467}
3468
45eb10fb
KH
3469
3470/* Make FACE on frame F stop using the font opened for FACE. */
3471
c2f5bfd6 3472void
971de7fb 3473font_done_for_face (FRAME_PTR f, struct face *face)
c2f5bfd6 3474{
35027d0c
KH
3475 if (face->font->driver->done_face)
3476 face->font->driver->done_face (f, face);
c2f5bfd6
KH
3477 face->extra = NULL;
3478}
3479
45eb10fb 3480
c50b7e98
KH
3481/* Open a font matching with font-spec SPEC on frame F. If no proper
3482 font is found, return Qnil. */
45eb10fb 3483
c2f5bfd6 3484Lisp_Object
971de7fb 3485font_open_by_spec (FRAME_PTR f, Lisp_Object spec)
c2f5bfd6 3486{
c50b7e98 3487 Lisp_Object attrs[LFACE_VECTOR_SIZE];
a9262bb8 3488
4007dd1c
KH
3489 /* We set up the default font-related attributes of a face to prefer
3490 a moderate font. */
3491 attrs[LFACE_FAMILY_INDEX] = attrs[LFACE_FOUNDRY_INDEX] = Qnil;
3492 attrs[LFACE_SWIDTH_INDEX] = attrs[LFACE_WEIGHT_INDEX]
3493 = attrs[LFACE_SLANT_INDEX] = Qnormal;
ed96cde8 3494#ifndef HAVE_NS
4007dd1c 3495 attrs[LFACE_HEIGHT_INDEX] = make_number (120);
ed96cde8
AR
3496#else
3497 attrs[LFACE_HEIGHT_INDEX] = make_number (0);
3498#endif
4007dd1c
KH
3499 attrs[LFACE_FONT_INDEX] = Qnil;
3500
3501 return font_load_for_lface (f, attrs, spec);
c2f5bfd6
KH
3502}
3503
3504
c50b7e98
KH
3505/* Open a font matching with NAME on frame F. If no proper font is
3506 found, return Qnil. */
3507
3508Lisp_Object
42ca4633 3509font_open_by_name (FRAME_PTR f, const char *name)
c50b7e98
KH
3510{
3511 Lisp_Object args[2];
581e51e8 3512 Lisp_Object spec, ret;
c50b7e98
KH
3513
3514 args[0] = QCname;
3515 args[1] = make_unibyte_string (name, strlen (name));
3516 spec = Ffont_spec (2, args);
581e51e8 3517 ret = font_open_by_spec (f, spec);
537b04b9 3518 /* Do not lose name originally put in. */
8096a0ff 3519 if (!NILP (ret))
42707278 3520 font_put_extra (ret, QCuser_spec, args[1]);
581e51e8
JD
3521
3522 return ret;
c50b7e98
KH
3523}
3524
3525
c2f5bfd6
KH
3526/* Register font-driver DRIVER. This function is used in two ways.
3527
417a1b10
KH
3528 The first is with frame F non-NULL. In this case, make DRIVER
3529 available (but not yet activated) on F. All frame creaters
3530 (e.g. Fx_create_frame) must call this function at least once with
3531 an available font-driver.
c2f5bfd6
KH
3532
3533 The second is with frame F NULL. In this case, DRIVER is globally
3534 registered in the variable `font_driver_list'. All font-driver
3535 implementations must call this function in its syms_of_XXXX
3536 (e.g. syms_of_xfont). */
3537
3538void
971de7fb 3539register_font_driver (struct font_driver *driver, FRAME_PTR f)
c2f5bfd6
KH
3540{
3541 struct font_driver_list *root = f ? f->font_driver_list : font_driver_list;
3542 struct font_driver_list *prev, *list;
3543
3544 if (f && ! driver->draw)
43a1d19b 3545 error ("Unusable font driver for a frame: %s",
c2f5bfd6
KH
3546 SDATA (SYMBOL_NAME (driver->type)));
3547
3548 for (prev = NULL, list = root; list; prev = list, list = list->next)
cf23b845 3549 if (EQ (list->driver->type, driver->type))
c2f5bfd6
KH
3550 error ("Duplicated font driver: %s", SDATA (SYMBOL_NAME (driver->type)));
3551
c115043b 3552 list = xmalloc (sizeof (struct font_driver_list));
417a1b10 3553 list->on = 0;
c2f5bfd6
KH
3554 list->driver = driver;
3555 list->next = NULL;
3556 if (prev)
3557 prev->next = list;
3558 else if (f)
3559 f->font_driver_list = list;
3560 else
3561 font_driver_list = list;
72606e45
KH
3562 if (! f)
3563 num_font_drivers++;
c2f5bfd6
KH
3564}
3565
2ed98482 3566void
971de7fb 3567free_font_driver_list (FRAME_PTR f)
2ed98482
CY
3568{
3569 struct font_driver_list *list, *next;
3570
3571 for (list = f->font_driver_list; list; list = next)
3572 {
3573 next = list->next;
3574 xfree (list);
3575 }
3576 f->font_driver_list = NULL;
3577}
3578
45eb10fb 3579
f697fff0 3580/* Make the frame F use font backends listed in NEW_DRIVERS (list of
ca4da08a
KH
3581 symbols, e.g. xft, x). If NEW_DRIVERS is t, make F use all
3582 available font drivers. If NEW_DRIVERS is nil, finalize all drivers.
417a1b10 3583
ca4da08a
KH
3584 A caller must free all realized faces if any in advance. The
3585 return value is a list of font backends actually made used on
3586 F. */
e950d6f1
KH
3587
3588Lisp_Object
971de7fb 3589font_update_drivers (FRAME_PTR f, Lisp_Object new_drivers)
417a1b10
KH
3590{
3591 Lisp_Object active_drivers = Qnil;
4007dd1c 3592 struct font_driver *driver;
417a1b10
KH
3593 struct font_driver_list *list;
3594
4007dd1c
KH
3595 /* At first, turn off non-requested drivers, and turn on requested
3596 drivers. */
f697fff0 3597 for (list = f->font_driver_list; list; list = list->next)
4007dd1c
KH
3598 {
3599 driver = list->driver;
3600 if ((EQ (new_drivers, Qt) || ! NILP (Fmemq (driver->type, new_drivers)))
3601 != list->on)
3602 {
3603 if (list->on)
3604 {
3605 if (driver->end_for_frame)
3606 driver->end_for_frame (f);
3607 font_finish_cache (f, driver);
3608 list->on = 0;
3609 }
3610 else
3611 {
3612 if (! driver->start_for_frame
3613 || driver->start_for_frame (f) == 0)
3614 {
3615 font_prepare_cache (f, driver);
3616 list->on = 1;
3617 }
3618 }
3619 }
3620 }
3621
3622 if (NILP (new_drivers))
3623 return Qnil;
3624
3625 if (! EQ (new_drivers, Qt))
3626 {
3627 /* Re-order the driver list according to new_drivers. */
3306c6dc 3628 struct font_driver_list **list_table, **next;
4007dd1c
KH
3629 Lisp_Object tail;
3630 int i;
3631
3632 list_table = alloca (sizeof list_table[0] * (num_font_drivers + 1));
3633 for (i = 0, tail = new_drivers; ! NILP (tail); tail = XCDR (tail))
3634 {
3635 for (list = f->font_driver_list; list; list = list->next)
3636 if (list->on && EQ (list->driver->type, XCAR (tail)))
3637 break;
3638 if (list)
3639 list_table[i++] = list;
3640 }
3641 for (list = f->font_driver_list; list; list = list->next)
3642 if (! list->on)
6136b72f 3643 list_table[i++] = list;
4007dd1c
KH
3644 list_table[i] = NULL;
3645
3306c6dc 3646 next = &f->font_driver_list;
4007dd1c
KH
3647 for (i = 0; list_table[i]; i++)
3648 {
3306c6dc
AS
3649 *next = list_table[i];
3650 next = &(*next)->next;
4007dd1c 3651 }
3306c6dc 3652 *next = NULL;
ba98e3a0
SM
3653
3654 if (! f->font_driver_list->on)
3655 { /* None of the drivers is enabled: enable them all.
3656 Happens if you set the list of drivers to (xft x) in your .emacs
3657 and then use it under w32 or ns. */
3658 for (list = f->font_driver_list; list; list = list->next)
3659 {
3660 struct font_driver *driver = list->driver;
3661 eassert (! list->on);
3662 if (! driver->start_for_frame
3663 || driver->start_for_frame (f) == 0)
3664 {
3665 font_prepare_cache (f, driver);
3666 list->on = 1;
3667 }
3668 }
3669 }
4007dd1c 3670 }
417a1b10 3671
4007dd1c
KH
3672 for (list = f->font_driver_list; list; list = list->next)
3673 if (list->on)
3674 active_drivers = nconc2 (active_drivers,
3675 Fcons (list->driver->type, Qnil));
e950d6f1 3676 return active_drivers;
417a1b10
KH
3677}
3678
f697fff0 3679int
971de7fb 3680font_put_frame_data (FRAME_PTR f, struct font_driver *driver, void *data)
f697fff0
KH
3681{
3682 struct font_data_list *list, *prev;
3683
3684 for (prev = NULL, list = f->font_data_list; list;
3685 prev = list, list = list->next)
3686 if (list->driver == driver)
3687 break;
3688 if (! data)
3689 {
3690 if (list)
3691 {
3692 if (prev)
3693 prev->next = list->next;
3694 else
3695 f->font_data_list = list->next;
973e7849 3696 xfree (list);
f697fff0
KH
3697 }
3698 return 0;
3699 }
3700
3701 if (! list)
3702 {
c115043b 3703 list = xmalloc (sizeof (struct font_data_list));
f697fff0
KH
3704 list->driver = driver;
3705 list->next = f->font_data_list;
3706 f->font_data_list = list;
3707 }
3708 list->data = data;
3709 return 0;
3710}
3711
3712
3713void *
971de7fb 3714font_get_frame_data (FRAME_PTR f, struct font_driver *driver)
f697fff0
KH
3715{
3716 struct font_data_list *list;
3717
3718 for (list = f->font_data_list; list; list = list->next)
3719 if (list->driver == driver)
3720 break;
3721 if (! list)
3722 return NULL;
3723 return list->data;
3724}
3725
417a1b10 3726
9fa82824
DP
3727/* Sets attributes on a font. Any properties that appear in ALIST and
3728 BOOLEAN_PROPERTIES or NON_BOOLEAN_PROPERTIES are set on the font.
3729 BOOLEAN_PROPERTIES and NON_BOOLEAN_PROPERTIES are NULL-terminated
3730 arrays of strings. This function is intended for use by the font
3731 drivers to implement their specific font_filter_properties. */
3732void
220d91b8
JB
3733font_filter_properties (Lisp_Object font,
3734 Lisp_Object alist,
3735 const char *boolean_properties[],
3736 const char *non_boolean_properties[])
9fa82824
DP
3737{
3738 Lisp_Object it;
3739 int i;
3740
3741 /* Set boolean values to Qt or Qnil */
3742 for (i = 0; boolean_properties[i] != NULL; ++i)
3743 for (it = alist; ! NILP (it); it = XCDR (it))
3744 {
3745 Lisp_Object key = XCAR (XCAR (it));
3746 Lisp_Object val = XCDR (XCAR (it));
3747 char *keystr = SDATA (SYMBOL_NAME (key));
3748
3749 if (strcmp (boolean_properties[i], keystr) == 0)
3750 {
3751 const char *str = INTEGERP (val) ? (XINT (val) ? "true" : "false")
3752 : SYMBOLP (val) ? (const char *) SDATA (SYMBOL_NAME (val))
3753 : "true";
3754
3755 if (strcmp ("false", str) == 0 || strcmp ("False", str) == 0
3756 || strcmp ("FALSE", str) == 0 || strcmp ("FcFalse", str) == 0
3757 || strcmp ("off", str) == 0 || strcmp ("OFF", str) == 0
3758 || strcmp ("Off", str) == 0)
3759 val = Qnil;
3760 else
3761 val = Qt;
3762
3763 Ffont_put (font, key, val);
3764 }
3765 }
3766
3767 for (i = 0; non_boolean_properties[i] != NULL; ++i)
3768 for (it = alist; ! NILP (it); it = XCDR (it))
3769 {
3770 Lisp_Object key = XCAR (XCAR (it));
3771 Lisp_Object val = XCDR (XCAR (it));
3772 char *keystr = SDATA (SYMBOL_NAME (key));
3773 if (strcmp (non_boolean_properties[i], keystr) == 0)
3774 Ffont_put (font, key, val);
3775 }
3776}
3777
3778
45eb10fb 3779/* Return the font used to draw character C by FACE at buffer position
e3ee0340
KH
3780 POS in window W. If STRING is non-nil, it is a string containing C
3781 at index POS. If C is negative, get C from the current buffer or
3782 STRING. */
45eb10fb 3783
10d16101 3784Lisp_Object
971de7fb 3785font_at (int c, EMACS_INT pos, struct face *face, struct window *w, Lisp_Object string)
10d16101
KH
3786{
3787 FRAME_PTR f;
e3ee0340 3788 int multibyte;
35027d0c 3789 Lisp_Object font_object;
e3ee0340 3790
e500c47d
KH
3791 multibyte = (NILP (string)
3792 ? ! NILP (current_buffer->enable_multibyte_characters)
3793 : STRING_MULTIBYTE (string));
e3ee0340
KH
3794 if (c < 0)
3795 {
3796 if (NILP (string))
3797 {
e3ee0340
KH
3798 if (multibyte)
3799 {
3800 EMACS_INT pos_byte = CHAR_TO_BYTE (pos);
3801
3802 c = FETCH_CHAR (pos_byte);
3803 }
3804 else
3805 c = FETCH_BYTE (pos);
3806 }
3807 else
3808 {
3809 unsigned char *str;
3810
3811 multibyte = STRING_MULTIBYTE (string);
3812 if (multibyte)
3813 {
3814 EMACS_INT pos_byte = string_char_to_byte (string, pos);
3815
3816 str = SDATA (string) + pos_byte;
62a6e103 3817 c = STRING_CHAR (str);
e3ee0340
KH
3818 }
3819 else
3820 c = SDATA (string)[pos];
3821 }
3822 }
10d16101
KH
3823
3824 f = XFRAME (w->frame);
1385a806
KH
3825 if (! FRAME_WINDOW_P (f))
3826 return Qnil;
10d16101
KH
3827 if (! face)
3828 {
e3ee0340 3829 int face_id;
0f8b27ea 3830 EMACS_INT endptr;
e3ee0340
KH
3831
3832 if (STRINGP (string))
3833 face_id = face_at_string_position (w, string, pos, 0, -1, -1, &endptr,
10d16101
KH
3834 DEFAULT_FACE_ID, 0);
3835 else
e3ee0340 3836 face_id = face_at_buffer_position (w, pos, -1, -1, &endptr,
6970f632 3837 pos + 100, 0, -1);
10d16101
KH
3838 face = FACE_FROM_ID (f, face_id);
3839 }
e3ee0340
KH
3840 if (multibyte)
3841 {
3842 int face_id = FACE_FOR_CHAR (f, face, c, pos, string);
3843 face = FACE_FROM_ID (f, face_id);
3844 }
35027d0c 3845 if (! face->font)
10d16101 3846 return Qnil;
35027d0c 3847
35027d0c
KH
3848 XSETFONT (font_object, face->font);
3849 return font_object;
3850}
3851
3852
071132a9
KH
3853#ifdef HAVE_WINDOW_SYSTEM
3854
3855/* Check how many characters after POS (at most to *LIMIT) can be
3856 displayed by the same font on the window W. FACE, if non-NULL, is
3857 the face selected for the character at POS. If STRING is not nil,
3858 it is the string to check instead of the current buffer. In that
3859 case, FACE must be not NULL.
027a33c0 3860
071132a9
KH
3861 The return value is the font-object for the character at POS.
3862 *LIMIT is set to the position where that font can't be used.
35027d0c 3863
071132a9
KH
3864 It is assured that the current buffer (or STRING) is multibyte. */
3865
3866Lisp_Object
971de7fb 3867font_range (EMACS_INT pos, EMACS_INT *limit, struct window *w, struct face *face, Lisp_Object string)
35027d0c 3868{
0e5d7800 3869 EMACS_INT pos_byte, ignore;
35027d0c 3870 int c;
071132a9 3871 Lisp_Object font_object = Qnil;
35027d0c
KH
3872
3873 if (NILP (string))
3874 {
35027d0c 3875 pos_byte = CHAR_TO_BYTE (pos);
071132a9
KH
3876 if (! face)
3877 {
3878 int face_id;
3879
6970f632
CY
3880 face_id = face_at_buffer_position (w, pos, 0, 0, &ignore,
3881 *limit, 0, -1);
071132a9
KH
3882 face = FACE_FROM_ID (XFRAME (w->frame), face_id);
3883 }
35027d0c
KH
3884 }
3885 else
3886 {
071132a9 3887 font_assert (face);
35027d0c
KH
3888 pos_byte = string_char_to_byte (string, pos);
3889 }
3890
071132a9 3891 while (pos < *limit)
35027d0c 3892 {
071132a9 3893 Lisp_Object category;
35027d0c
KH
3894
3895 if (NILP (string))
3896 FETCH_CHAR_ADVANCE_NO_CHECK (c, pos, pos_byte);
3897 else
3898 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, string, pos, pos_byte);
0e5d7800
KH
3899 category = CHAR_TABLE_REF (Vunicode_category_table, c);
3900 if (EQ (category, QCf)
3901 || CHAR_VARIATION_SELECTOR_P (c))
3902 continue;
071132a9 3903 if (NILP (font_object))
35027d0c 3904 {
071132a9
KH
3905 font_object = font_for_char (face, c, pos - 1, string);
3906 if (NILP (font_object))
3907 return Qnil;
35027d0c
KH
3908 continue;
3909 }
0e5d7800
KH
3910 if (font_encode_char (font_object, c) == FONT_INVALID_CODE)
3911 *limit = pos - 1;
35027d0c 3912 }
071132a9 3913 return font_object;
10d16101 3914}
071132a9 3915#endif
10d16101 3916
c2f5bfd6
KH
3917\f
3918/* Lisp API */
3919
35027d0c 3920DEFUN ("fontp", Ffontp, Sfontp, 1, 2, 0,
6c8ec042 3921 doc: /* Return t if OBJECT is a font-spec, font-entity, or font-object.
35027d0c
KH
3922Return nil otherwise.
3923Optional 2nd argument EXTRA-TYPE, if non-nil, specifies to check
027a33c0 3924which kind of font it is. It must be one of `font-spec', `font-entity',
35027d0c 3925`font-object'. */)
5842a27b 3926 (Lisp_Object object, Lisp_Object extra_type)
c2f5bfd6 3927{
35027d0c
KH
3928 if (NILP (extra_type))
3929 return (FONTP (object) ? Qt : Qnil);
3930 if (EQ (extra_type, Qfont_spec))
3931 return (FONT_SPEC_P (object) ? Qt : Qnil);
3932 if (EQ (extra_type, Qfont_entity))
3933 return (FONT_ENTITY_P (object) ? Qt : Qnil);
3934 if (EQ (extra_type, Qfont_object))
3935 return (FONT_OBJECT_P (object) ? Qt : Qnil);
3936 wrong_type_argument (intern ("font-extra-type"), extra_type);
c2f5bfd6
KH
3937}
3938
3939DEFUN ("font-spec", Ffont_spec, Sfont_spec, 0, MANY, 0,
45eb10fb
KH
3940 doc: /* Return a newly created font-spec with arguments as properties.
3941
3942ARGS must come in pairs KEY VALUE of font properties. KEY must be a
3943valid font property name listed below:
3944
3945`:family', `:weight', `:slant', `:width'
3946
3947They are the same as face attributes of the same name. See
51c01100 3948`set-face-attribute'.
45eb10fb
KH
3949
3950`:foundry'
3951
3952VALUE must be a string or a symbol specifying the font foundry, e.g. ``misc''.
3953
3954`:adstyle'
3955
3956VALUE must be a string or a symbol specifying the additional
35027d0c 3957typographic style information of a font, e.g. ``sans''.
45eb10fb
KH
3958
3959`:registry'
3960
3961VALUE must be a string or a symbol specifying the charset registry and
3962encoding of a font, e.g. ``iso8859-1''.
3963
3964`:size'
3965
3966VALUE must be a non-negative integer or a floating point number
c423ecca
KH
3967specifying the font size. It specifies the font size in pixels (if
3968VALUE is an integer), or in points (if VALUE is a float).
2babb359
KH
3969
3970`:name'
3971
7a18a178 3972VALUE must be a string of XLFD-style or fontconfig-style font name.
5bdd4dd2
KH
3973
3974`:script'
3975
3976VALUE must be a symbol representing a script that the font must
209f39ac
KH
3977support. It may be a symbol representing a subgroup of a script
3978listed in the variable `script-representative-chars'.
c423ecca
KH
3979
3980`:lang'
3981
3982VALUE must be a symbol of two-letter ISO-639 language names,
3983e.g. `ja'.
3984
3985`:otf'
3986
3987VALUE must be a list (SCRIPT-TAG LANGSYS-TAG GSUB [ GPOS ]) to specify
3988required OpenType features.
3989
3990 SCRIPT-TAG: OpenType script tag symbol (e.g. `deva').
3991 LANGSYS-TAG: OpenType language system tag symbol,
3992 or nil for the default language system.
3993 GSUB: List of OpenType GSUB feature tag symbols, or nil if none required.
3994 GPOS: List of OpenType GPOS feature tag symbols, or nil if none required.
3995
3996GSUB and GPOS may contain `nil' element. In such a case, the font
3997must not have any of the remaining elements.
3998
3999For instance, if the VALUE is `(thai nil nil (mark))', the font must
22f79966 4000be an OpenType font, and whose GPOS table of `thai' script's default
c423ecca
KH
4001language system must contain `mark' feature.
4002
ba3de0e8 4003usage: (font-spec ARGS...) */)
5842a27b 4004 (int nargs, Lisp_Object *args)
c2f5bfd6 4005{
35027d0c 4006 Lisp_Object spec = font_make_spec ();
c2f5bfd6
KH
4007 int i;
4008
4009 for (i = 0; i < nargs; i += 2)
4010 {
cccd42d5
KH
4011 Lisp_Object key = args[i], val;
4012
4013 CHECK_SYMBOL (key);
4014 if (i + 1 >= nargs)
4015 error ("No value for key `%s'", SDATA (SYMBOL_NAME (key)));
4016 val = args[i + 1];
c2f5bfd6 4017
35027d0c
KH
4018 if (EQ (key, QCname))
4019 {
4020 CHECK_STRING (val);
4021 font_parse_name ((char *) SDATA (val), spec);
4022 font_put_extra (spec, key, val);
4023 }
c2f5bfd6 4024 else
4485a28e 4025 {
35027d0c
KH
4026 int idx = get_font_prop_index (key);
4027
4028 if (idx >= 0)
ec6fe57c 4029 {
35027d0c
KH
4030 val = font_prop_validate (idx, Qnil, val);
4031 if (idx < FONT_EXTRA_INDEX)
4032 ASET (spec, idx, val);
4033 else
4034 font_put_extra (spec, key, val);
ec6fe57c 4035 }
35027d0c
KH
4036 else
4037 font_put_extra (spec, key, font_prop_validate (0, key, val));
4485a28e 4038 }
e950d6f1 4039 }
c2f5bfd6
KH
4040 return spec;
4041}
4042
35027d0c
KH
4043DEFUN ("copy-font-spec", Fcopy_font_spec, Scopy_font_spec, 1, 1, 0,
4044 doc: /* Return a copy of FONT as a font-spec. */)
5842a27b 4045 (Lisp_Object font)
35027d0c 4046{
d26424c5 4047 Lisp_Object new_spec, tail, prev, extra;
35027d0c
KH
4048 int i;
4049
4050 CHECK_FONT (font);
4051 new_spec = font_make_spec ();
4052 for (i = 1; i < FONT_EXTRA_INDEX; i++)
4053 ASET (new_spec, i, AREF (font, i));
581e51e8 4054 extra = Fcopy_alist (AREF (font, FONT_EXTRA_INDEX));
d26424c5
KH
4055 /* We must remove :font-entity property. */
4056 for (prev = Qnil, tail = extra; CONSP (tail); prev = tail, tail = XCDR (tail))
4057 if (EQ (XCAR (XCAR (tail)), QCfont_entity))
4058 {
4059 if (NILP (prev))
4060 extra = XCDR (extra);
4061 else
4062 XSETCDR (prev, XCDR (tail));
4063 break;
4064 }
35027d0c
KH
4065 ASET (new_spec, FONT_EXTRA_INDEX, extra);
4066 return new_spec;
4067}
4068
4069DEFUN ("merge-font-spec", Fmerge_font_spec, Smerge_font_spec, 2, 2, 0,
4070 doc: /* Merge font-specs FROM and TO, and return a new font-spec.
4071Every specified properties in FROM override the corresponding
4072properties in TO. */)
5842a27b 4073 (Lisp_Object from, Lisp_Object to)
35027d0c
KH
4074{
4075 Lisp_Object extra, tail;
4076 int i;
4077
4078 CHECK_FONT (from);
4079 CHECK_FONT (to);
4080 to = Fcopy_font_spec (to);
4081 for (i = 0; i < FONT_EXTRA_INDEX; i++)
4082 ASET (to, i, AREF (from, i));
4083 extra = AREF (to, FONT_EXTRA_INDEX);
4084 for (tail = AREF (from, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail))
4085 if (! EQ (XCAR (XCAR (tail)), Qfont_entity))
4086 {
4087 Lisp_Object slot = assq_no_quit (XCAR (XCAR (tail)), extra);
4088
4089 if (! NILP (slot))
4090 XSETCDR (slot, XCDR (XCAR (tail)));
4091 else
4092 extra = Fcons (Fcons (XCAR (XCAR (tail)), XCDR (XCAR (tail))), extra);
4093 }
4094 ASET (to, FONT_EXTRA_INDEX, extra);
4095 return to;
4096}
c2f5bfd6
KH
4097
4098DEFUN ("font-get", Ffont_get, Sfont_get, 2, 2, 0,
45eb10fb 4099 doc: /* Return the value of FONT's property KEY.
5bdd4dd2 4100FONT is a font-spec, a font-entity, or a font-object.
a7840ffb 4101KEY is any symbol, but these are reserved for specific meanings:
5bdd4dd2 4102 :family, :weight, :slant, :width, :foundry, :adstyle, :registry,
a7840ffb 4103 :size, :name, :script, :otf
5bdd4dd2 4104See the documentation of `font-spec' for their meanings.
a7840ffb
KH
4105In addition, if FONT is a font-entity or a font-object, values of
4106:script and :otf are different from those of a font-spec as below:
4107
4108The value of :script may be a list of scripts that are supported by the font.
4109
4110The value of :otf is a cons (GSUB . GPOS) where GSUB and GPOS are lists
4111representing the OpenType features supported by the font by this form:
4112 ((SCRIPT (LANGSYS FEATURE ...) ...) ...)
4113SCRIPT, LANGSYS, and FEATURE are all symbols representing OpenType
4114Layout tags. */)
5842a27b 4115 (Lisp_Object font, Lisp_Object key)
c2f5bfd6 4116{
35027d0c 4117 int idx;
a7840ffb 4118 Lisp_Object val;
c2f5bfd6 4119
35027d0c
KH
4120 CHECK_FONT (font);
4121 CHECK_SYMBOL (key);
e80e09b4 4122
35027d0c 4123 idx = get_font_prop_index (key);
2babb359
KH
4124 if (idx >= FONT_WEIGHT_INDEX && idx <= FONT_WIDTH_INDEX)
4125 return font_style_symbolic (font, idx, 0);
35027d0c 4126 if (idx >= 0 && idx < FONT_EXTRA_INDEX)
c2f5bfd6 4127 return AREF (font, idx);
a7840ffb
KH
4128 val = Fassq (key, AREF (font, FONT_EXTRA_INDEX));
4129 if (NILP (val) && EQ (key, QCotf) && FONT_OBJECT_P (font))
4130 {
4131 struct font *fontp = XFONT_OBJECT (font);
a7840ffb 4132
f6c1c771
KH
4133 if (fontp->driver->otf_capability)
4134 val = fontp->driver->otf_capability (fontp);
a7840ffb 4135 else
f6c1c771
KH
4136 val = Fcons (Qnil, Qnil);
4137 font_put_extra (font, QCotf, val);
a7840ffb
KH
4138 }
4139 else
4140 val = Fcdr (val);
4141 return val;
c2f5bfd6
KH
4142}
4143
51cf11be
AS
4144#ifdef HAVE_WINDOW_SYSTEM
4145
b1868a1a
CY
4146DEFUN ("font-face-attributes", Ffont_face_attributes, Sfont_face_attributes, 1, 2, 0,
4147 doc: /* Return a plist of face attributes generated by FONT.
4148FONT is a font name, a font-spec, a font-entity, or a font-object.
4149The return value is a list of the form
4150
6f568955 4151\(:family FAMILY :height HEIGHT :weight WEIGHT :slant SLANT :width WIDTH)
b1868a1a 4152
48105a6a 4153where FAMILY, HEIGHT, WEIGHT, SLANT, and WIDTH are face attribute values
5989ba2f
CY
4154compatible with `set-face-attribute'. Some of these key-attribute pairs
4155may be omitted from the list if they are not specified by FONT.
b1868a1a 4156
48105a6a
JB
4157The optional argument FRAME specifies the frame that the face attributes
4158are to be displayed on. If omitted, the selected frame is used. */)
5842a27b 4159 (Lisp_Object font, Lisp_Object frame)
b1868a1a
CY
4160{
4161 struct frame *f;
4162 Lisp_Object plist[10];
4163 Lisp_Object val;
5989ba2f 4164 int n = 0;
b1868a1a
CY
4165
4166 if (NILP (frame))
4167 frame = selected_frame;
4168 CHECK_LIVE_FRAME (frame);
4169 f = XFRAME (frame);
4170
4171 if (STRINGP (font))
4172 {
4173 int fontset = fs_query_fontset (font, 0);
4174 Lisp_Object name = font;
4175 if (fontset >= 0)
4176 font = fontset_ascii (fontset);
4177 font = font_spec_from_name (name);
4178 if (! FONTP (font))
4179 signal_error ("Invalid font name", name);
4180 }
4181 else if (! FONTP (font))
4182 signal_error ("Invalid font object", font);
4183
b1868a1a 4184 val = AREF (font, FONT_FAMILY_INDEX);
5989ba2f
CY
4185 if (! NILP (val))
4186 {
4187 plist[n++] = QCfamily;
4188 plist[n++] = SYMBOL_NAME (val);
4189 }
b1868a1a 4190
b1868a1a
CY
4191 val = AREF (font, FONT_SIZE_INDEX);
4192 if (INTEGERP (val))
4193 {
4194 Lisp_Object font_dpi = AREF (font, FONT_DPI_INDEX);
4195 int dpi = INTEGERP (font_dpi) ? XINT (font_dpi) : f->resy;
5989ba2f 4196 plist[n++] = QCheight;
c3bb5465 4197 plist[n++] = make_number (PIXEL_TO_POINT (XINT (val) * 10, dpi));
b1868a1a
CY
4198 }
4199 else if (FLOATP (val))
5989ba2f
CY
4200 {
4201 plist[n++] = QCheight;
4202 plist[n++] = make_number (10 * (int) XFLOAT_DATA (val));
4203 }
b1868a1a 4204
b1868a1a 4205 val = FONT_WEIGHT_FOR_FACE (font);
5989ba2f
CY
4206 if (! NILP (val))
4207 {
4208 plist[n++] = QCweight;
4209 plist[n++] = val;
4210 }
b1868a1a 4211
b1868a1a 4212 val = FONT_SLANT_FOR_FACE (font);
5989ba2f
CY
4213 if (! NILP (val))
4214 {
4215 plist[n++] = QCslant;
4216 plist[n++] = val;
4217 }
b1868a1a 4218
b1868a1a 4219 val = FONT_WIDTH_FOR_FACE (font);
5989ba2f
CY
4220 if (! NILP (val))
4221 {
4222 plist[n++] = QCwidth;
4223 plist[n++] = val;
4224 }
b1868a1a 4225
5989ba2f 4226 return Flist (n, plist);
b1868a1a 4227}
c2f5bfd6 4228
51cf11be
AS
4229#endif
4230
c2f5bfd6 4231DEFUN ("font-put", Ffont_put, Sfont_put, 3, 3, 0,
a7840ffb
KH
4232 doc: /* Set one property of FONT: give property KEY value VAL.
4233FONT is a font-spec, a font-entity, or a font-object.
4234
4235If FONT is a font-spec, KEY can be any symbol. But if KEY is the one
4236accepted by the function `font-spec' (which see), VAL must be what
4237allowed in `font-spec'.
4238
4239If FONT is a font-entity or a font-object, KEY must not be the one
4240accepted by `font-spec'. */)
e1ffae3b 4241 (Lisp_Object font, Lisp_Object prop, Lisp_Object val)
c2f5bfd6 4242{
35027d0c 4243 int idx;
c2f5bfd6 4244
35027d0c
KH
4245 idx = get_font_prop_index (prop);
4246 if (idx >= 0 && idx < FONT_EXTRA_INDEX)
a7840ffb
KH
4247 {
4248 CHECK_FONT_SPEC (font);
4249 ASET (font, idx, font_prop_validate (idx, Qnil, val));
4250 }
c2f5bfd6 4251 else
a7840ffb
KH
4252 {
4253 if (EQ (prop, QCname)
4254 || EQ (prop, QCscript)
4255 || EQ (prop, QClang)
4256 || EQ (prop, QCotf))
4257 CHECK_FONT_SPEC (font);
4258 else
4259 CHECK_FONT (font);
4260 font_put_extra (font, prop, font_prop_validate (0, prop, val));
4261 }
c2f5bfd6
KH
4262 return val;
4263}
4264
4265DEFUN ("list-fonts", Flist_fonts, Slist_fonts, 1, 4, 0,
4266 doc: /* List available fonts matching FONT-SPEC on the current frame.
4267Optional 2nd argument FRAME specifies the target frame.
4268Optional 3rd argument NUM, if non-nil, limits the number of returned fonts.
45eb10fb
KH
4269Optional 4th argument PREFER, if non-nil, is a font-spec to
4270control the order of the returned list. Fonts are sorted by
027a33c0 4271how close they are to PREFER. */)
5842a27b 4272 (Lisp_Object font_spec, Lisp_Object frame, Lisp_Object num, Lisp_Object prefer)
c2f5bfd6 4273{
72d36834
KH
4274 Lisp_Object vec, list;
4275 int n = 0;
c2f5bfd6
KH
4276
4277 if (NILP (frame))
4278 frame = selected_frame;
4279 CHECK_LIVE_FRAME (frame);
35027d0c 4280 CHECK_FONT_SPEC (font_spec);
c2f5bfd6
KH
4281 if (! NILP (num))
4282 {
4283 CHECK_NUMBER (num);
4284 n = XINT (num);
4285 if (n <= 0)
4286 return Qnil;
4287 }
4288 if (! NILP (prefer))
35027d0c 4289 CHECK_FONT_SPEC (prefer);
c2f5bfd6 4290
72d36834
KH
4291 list = font_list_entities (frame, font_spec);
4292 if (NILP (list))
c2f5bfd6 4293 return Qnil;
72d36834
KH
4294 if (NILP (XCDR (list))
4295 && ASIZE (XCAR (list)) == 1)
4296 return Fcons (AREF (XCAR (list), 0), Qnil);
c2f5bfd6
KH
4297
4298 if (! NILP (prefer))
72d36834
KH
4299 vec = font_sort_entities (list, prefer, frame, 0);
4300 else
4301 vec = font_vconcat_entity_vectors (list);
4302 if (n == 0 || n >= ASIZE (vec))
c2f5bfd6 4303 {
72d36834 4304 Lisp_Object args[2];
c2f5bfd6 4305
72d36834
KH
4306 args[0] = vec;
4307 args[1] = Qnil;
4308 list = Fappend (2, args);
4309 }
4310 else
4311 {
4312 for (list = Qnil, n--; n >= 0; n--)
4313 list = Fcons (AREF (vec, n), list);
c2f5bfd6
KH
4314 }
4315 return list;
4316}
4317
35027d0c 4318DEFUN ("font-family-list", Ffont_family_list, Sfont_family_list, 0, 1, 0,
c2f5bfd6 4319 doc: /* List available font families on the current frame.
027a33c0 4320Optional argument FRAME, if non-nil, specifies the target frame. */)
5842a27b 4321 (Lisp_Object frame)
c2f5bfd6
KH
4322{
4323 FRAME_PTR f;
4324 struct font_driver_list *driver_list;
4325 Lisp_Object list;
4326
4327 if (NILP (frame))
4328 frame = selected_frame;
4329 CHECK_LIVE_FRAME (frame);
4330 f = XFRAME (frame);
4331 list = Qnil;
4332 for (driver_list = f->font_driver_list; driver_list;
4333 driver_list = driver_list->next)
4334 if (driver_list->driver->list_family)
4335 {
4336 Lisp_Object val = driver_list->driver->list_family (frame);
13bf758b 4337 Lisp_Object tail = list;
c2f5bfd6 4338
13bf758b
CY
4339 for (; CONSP (val); val = XCDR (val))
4340 if (NILP (Fmemq (XCAR (val), tail))
4341 && SYMBOLP (XCAR (val)))
4342 list = Fcons (SYMBOL_NAME (XCAR (val)), list);
c2f5bfd6
KH
4343 }
4344 return list;
4345}
4346
4347DEFUN ("find-font", Ffind_font, Sfind_font, 1, 2, 0,
4348 doc: /* Return a font-entity matching with FONT-SPEC on the current frame.
4349Optional 2nd argument FRAME, if non-nil, specifies the target frame. */)
5842a27b 4350 (Lisp_Object font_spec, Lisp_Object frame)
c2f5bfd6
KH
4351{
4352 Lisp_Object val = Flist_fonts (font_spec, frame, make_number (1), Qnil);
4353
4354 if (CONSP (val))
4355 val = XCAR (val);
4356 return val;
4357}
4358
d0ab1ebe 4359DEFUN ("font-xlfd-name", Ffont_xlfd_name, Sfont_xlfd_name, 1, 2, 0,
c2f5bfd6
KH
4360 doc: /* Return XLFD name of FONT.
4361FONT is a font-spec, font-entity, or font-object.
d0ab1ebe
KH
4362If the name is too long for XLFD (maximum 255 chars), return nil.
4363If the 2nd optional arg FOLD-WILDCARDS is non-nil,
4364the consecutive wildcards are folded to one. */)
5842a27b 4365 (Lisp_Object font, Lisp_Object fold_wildcards)
c2f5bfd6
KH
4366{
4367 char name[256];
4368 int pixel_size = 0;
4369
35027d0c
KH
4370 CHECK_FONT (font);
4371
4372 if (FONT_OBJECT_P (font))
c2f5bfd6 4373 {
35027d0c 4374 Lisp_Object font_name = AREF (font, FONT_NAME_INDEX);
c2f5bfd6 4375
35027d0c
KH
4376 if (STRINGP (font_name)
4377 && SDATA (font_name)[0] == '-')
d0ab1ebe
KH
4378 {
4379 if (NILP (fold_wildcards))
4380 return font_name;
4381 strcpy (name, (char *) SDATA (font_name));
4382 goto done;
4383 }
35027d0c 4384 pixel_size = XFONT_OBJECT (font)->pixel_size;
c2f5bfd6 4385 }
c2f5bfd6
KH
4386 if (font_unparse_xlfd (font, pixel_size, name, 256) < 0)
4387 return Qnil;
d0ab1ebe
KH
4388 done:
4389 if (! NILP (fold_wildcards))
4390 {
4391 char *p0 = name, *p1;
4392
4393 while ((p1 = strstr (p0, "-*-*")))
4394 {
4395 strcpy (p1, p1 + 2);
4396 p0 = p1;
4397 }
4398 }
4399
c2f5bfd6
KH
4400 return build_string (name);
4401}
4402
4403DEFUN ("clear-font-cache", Fclear_font_cache, Sclear_font_cache, 0, 0, 0,
4404 doc: /* Clear font cache. */)
5842a27b 4405 (void)
c2f5bfd6
KH
4406{
4407 Lisp_Object list, frame;
4408
4409 FOR_EACH_FRAME (list, frame)
4410 {
4411 FRAME_PTR f = XFRAME (frame);
4412 struct font_driver_list *driver_list = f->font_driver_list;
4413
4414 for (; driver_list; driver_list = driver_list->next)
417a1b10
KH
4415 if (driver_list->on)
4416 {
ca4da08a 4417 Lisp_Object cache = driver_list->driver->get_cache (f);
7a6f7fea 4418 Lisp_Object val, tmp;
51c01100 4419
ca4da08a 4420 val = XCDR (cache);
43c0454d
KH
4421 while (! NILP (val)
4422 && ! EQ (XCAR (XCAR (val)), driver_list->driver->type))
ca4da08a 4423 val = XCDR (val);
d0ab1ebe 4424 font_assert (! NILP (val));
7a6f7fea
AS
4425 tmp = XCDR (XCAR (val));
4426 if (XINT (XCAR (tmp)) == 0)
417a1b10 4427 {
ca4da08a
KH
4428 font_clear_cache (f, XCAR (val), driver_list->driver);
4429 XSETCDR (cache, XCDR (val));
417a1b10 4430 }
417a1b10 4431 }
c2f5bfd6
KH
4432 }
4433
4434 return Qnil;
4435}
4436
071132a9
KH
4437\f
4438void
971de7fb 4439font_fill_lglyph_metrics (Lisp_Object glyph, Lisp_Object font_object)
c2f5bfd6 4440{
071132a9 4441 struct font *font = XFONT_OBJECT (font_object);
f62ab7c5
EZ
4442 unsigned code;
4443 /* ecode used in LGLYPH_SET_CODE to avoid compiler warnings. */
4444 EMACS_INT ecode = font->driver->encode_char (font, LGLYPH_CHAR (glyph));
071132a9 4445 struct font_metrics metrics;
c2f5bfd6 4446
f62ab7c5
EZ
4447 LGLYPH_SET_CODE (glyph, ecode);
4448 code = ecode;
071132a9
KH
4449 font->driver->text_extents (font, &code, 1, &metrics);
4450 LGLYPH_SET_LBEARING (glyph, metrics.lbearing);
4451 LGLYPH_SET_RBEARING (glyph, metrics.rbearing);
4452 LGLYPH_SET_WIDTH (glyph, metrics.width);
4453 LGLYPH_SET_ASCENT (glyph, metrics.ascent);
4454 LGLYPH_SET_DESCENT (glyph, metrics.descent);
c2f5bfd6
KH
4455}
4456
c2f5bfd6 4457
071132a9
KH
4458DEFUN ("font-shape-gstring", Ffont_shape_gstring, Sfont_shape_gstring, 1, 1, 0,
4459 doc: /* Shape the glyph-string GSTRING.
4460Shaping means substituting glyphs and/or adjusting positions of glyphs
4461to get the correct visual image of character sequences set in the
4462header of the glyph-string.
1701724c 4463
071132a9
KH
4464If the shaping was successful, the value is GSTRING itself or a newly
4465created glyph-string. Otherwise, the value is nil. */)
5842a27b 4466 (Lisp_Object gstring)
1701724c
KH
4467{
4468 struct font *font;
071132a9 4469 Lisp_Object font_object, n, glyph;
fc3a285e 4470 int i, j, from, to;
ba3de0e8 4471
071132a9
KH
4472 if (! composition_gstring_p (gstring))
4473 signal_error ("Invalid glyph-string: ", gstring);
4474 if (! NILP (LGSTRING_ID (gstring)))
4475 return gstring;
4476 font_object = LGSTRING_FONT (gstring);
4477 CHECK_FONT_OBJECT (font_object);
35027d0c 4478 font = XFONT_OBJECT (font_object);
b876ea91
KH
4479 if (! font->driver->shape)
4480 return Qnil;
4481
40fb53d6
KH
4482 /* Try at most three times with larger gstring each time. */
4483 for (i = 0; i < 3; i++)
4484 {
40fb53d6
KH
4485 n = font->driver->shape (gstring);
4486 if (INTEGERP (n))
4487 break;
071132a9
KH
4488 gstring = larger_vector (gstring,
4489 ASIZE (gstring) + LGSTRING_GLYPH_LEN (gstring),
4490 Qnil);
40fb53d6 4491 }
071132a9 4492 if (i == 3 || XINT (n) == 0)
1701724c 4493 return Qnil;
dfe3c90f
KH
4494 if (XINT (n) < LGSTRING_GLYPH_LEN (gstring))
4495 LGSTRING_SET_GLYPH (gstring, XINT (n), Qnil);
ba3de0e8 4496
071132a9 4497 glyph = LGSTRING_GLYPH (gstring, 0);
fc3a285e
KH
4498 from = LGLYPH_FROM (glyph);
4499 to = LGLYPH_TO (glyph);
4500 for (i = 1, j = 0; i < LGSTRING_GLYPH_LEN (gstring); i++)
071132a9
KH
4501 {
4502 Lisp_Object this = LGSTRING_GLYPH (gstring, i);
10d16101 4503
071132a9
KH
4504 if (NILP (this))
4505 break;
4506 if (NILP (LGLYPH_ADJUSTMENT (this)))
fc3a285e
KH
4507 {
4508 if (j < i - 1)
4509 for (; j < i; j++)
4510 {
4511 glyph = LGSTRING_GLYPH (gstring, j);
4512 LGLYPH_SET_FROM (glyph, from);
4513 LGLYPH_SET_TO (glyph, to);
4514 }
4515 from = LGLYPH_FROM (this);
4516 to = LGLYPH_TO (this);
4517 j = i;
4518 }
071132a9 4519 else
b2937119 4520 {
fc3a285e
KH
4521 if (from > LGLYPH_FROM (this))
4522 from = LGLYPH_FROM (this);
4523 if (to < LGLYPH_TO (this))
4524 to = LGLYPH_TO (this);
b2937119 4525 }
10d16101 4526 }
fc3a285e
KH
4527 if (j < i - 1)
4528 for (; j < i; j++)
4529 {
4530 glyph = LGSTRING_GLYPH (gstring, j);
4531 LGLYPH_SET_FROM (glyph, from);
4532 LGLYPH_SET_TO (glyph, to);
4533 }
071132a9 4534 return composition_gstring_put_cache (gstring, XINT (n));
c2f5bfd6
KH
4535}
4536
78a2f9cd
KH
4537DEFUN ("font-variation-glyphs", Ffont_variation_glyphs, Sfont_variation_glyphs,
4538 2, 2, 0,
4539 doc: /* Return a list of variation glyphs for CHAR in FONT-OBJECT.
4540Each element of the value is a cons (VARIATION-SELECTOR . GLYPH-ID),
4541where
c0943d3d 4542 VARIATION-SELECTOR is a character code of variation selection
78a2f9cd
KH
4543 (#xFE00..#xFE0F or #xE0100..#xE01EF)
4544 GLYPH-ID is a glyph code of the corresponding variation glyph. */)
5842a27b 4545 (Lisp_Object font_object, Lisp_Object character)
78a2f9cd
KH
4546{
4547 unsigned variations[256];
4548 struct font *font;
4549 int i, n;
4550 Lisp_Object val;
4551
4552 CHECK_FONT_OBJECT (font_object);
4553 CHECK_CHARACTER (character);
4554 font = XFONT_OBJECT (font_object);
4555 if (! font->driver->get_variation_glyphs)
4556 return Qnil;
4557 n = font->driver->get_variation_glyphs (font, XINT (character), variations);
4558 if (! n)
4559 return Qnil;
4560 val = Qnil;
4561 for (i = 0; i < 255; i++)
4562 if (variations[i])
4563 {
4564 Lisp_Object code;
4565 int vs = (i < 16 ? 0xFE00 + i : 0xE0100 + (i - 16));
b60861e6
GM
4566 /* Stops GCC whining about limited range of data type. */
4567 EMACS_INT var = variations[i];
78a2f9cd 4568
b60861e6 4569 if (var > MOST_POSITIVE_FIXNUM)
78a2f9cd
KH
4570 code = Fcons (make_number ((variations[i]) >> 16),
4571 make_number ((variations[i]) & 0xFFFF));
4572 else
4573 code = make_number (variations[i]);
4574 val = Fcons (Fcons (make_number (vs), code), val);
4575 }
4576 return val;
4577}
4578
6a3dadd2
KH
4579#if 0
4580
733fd013
KH
4581DEFUN ("font-drive-otf", Ffont_drive_otf, Sfont_drive_otf, 6, 6, 0,
4582 doc: /* Apply OpenType features on glyph-string GSTRING-IN.
51c01100 4583OTF-FEATURES specifies which features to apply in this format:
733fd013 4584 (SCRIPT LANGSYS GSUB GPOS)
e80e09b4
KH
4585where
4586 SCRIPT is a symbol specifying a script tag of OpenType,
4587 LANGSYS is a symbol specifying a langsys tag of OpenType,
733fd013 4588 GSUB and GPOS, if non-nil, are lists of symbols specifying feature tags.
e80e09b4
KH
4589
4590If LANGYS is nil, the default langsys is selected.
4591
51c01100
JB
4592The features are applied in the order they appear in the list. The
4593symbol `*' means to apply all available features not present in this
733fd013
KH
4594list, and the remaining features are ignored. For instance, (vatu
4595pstf * haln) is to apply vatu and pstf in this order, then to apply
4596all available features other than vatu, pstf, and haln.
e80e09b4
KH
4597
4598The features are applied to the glyphs in the range FROM and TO of
733fd013 4599the glyph-string GSTRING-IN.
e80e09b4 4600
51c01100 4601If some feature is actually applicable, the resulting glyphs are
e80e09b4
KH
4602produced in the glyph-string GSTRING-OUT from the index INDEX. In
4603this case, the value is the number of produced glyphs.
4604
4605If no feature is applicable, no glyph is produced in GSTRING-OUT, and
4606the value is 0.
4607
51c01100 4608If GSTRING-OUT is too short to hold produced glyphs, no glyphs are
e80e09b4
KH
4609produced in GSTRING-OUT, and the value is nil.
4610
4611See the documentation of `font-make-gstring' for the format of
4612glyph-string. */)
5842a27b 4613 (Lisp_Object otf_features, Lisp_Object gstring_in, Lisp_Object from, Lisp_Object to, Lisp_Object gstring_out, Lisp_Object index)
e80e09b4
KH
4614{
4615 Lisp_Object font_object = LGSTRING_FONT (gstring_in);
733fd013
KH
4616 Lisp_Object val;
4617 struct font *font;
e80e09b4
KH
4618 int len, num;
4619
733fd013 4620 check_otf_features (otf_features);
35027d0c
KH
4621 CHECK_FONT_OBJECT (font_object);
4622 font = XFONT_OBJECT (font_object);
733fd013 4623 if (! font->driver->otf_drive)
e80e09b4
KH
4624 error ("Font backend %s can't drive OpenType GSUB table",
4625 SDATA (SYMBOL_NAME (font->driver->type)));
733fd013
KH
4626 CHECK_CONS (otf_features);
4627 CHECK_SYMBOL (XCAR (otf_features));
4628 val = XCDR (otf_features);
4629 CHECK_SYMBOL (XCAR (val));
4630 val = XCDR (otf_features);
4631 if (! NILP (val))
4632 CHECK_CONS (val);
e80e09b4
KH
4633 len = check_gstring (gstring_in);
4634 CHECK_VECTOR (gstring_out);
4635 CHECK_NATNUM (from);
4636 CHECK_NATNUM (to);
4637 CHECK_NATNUM (index);
4638
4639 if (XINT (from) >= XINT (to) || XINT (to) > len)
4640 args_out_of_range_3 (from, to, make_number (len));
4641 if (XINT (index) >= ASIZE (gstring_out))
4642 args_out_of_range (index, make_number (ASIZE (gstring_out)));
733fd013
KH
4643 num = font->driver->otf_drive (font, otf_features,
4644 gstring_in, XINT (from), XINT (to),
4645 gstring_out, XINT (index), 0);
e80e09b4
KH
4646 if (num < 0)
4647 return Qnil;
4648 return make_number (num);
4649}
4650
e80e09b4
KH
4651DEFUN ("font-otf-alternates", Ffont_otf_alternates, Sfont_otf_alternates,
4652 3, 3, 0,
4653 doc: /* Return a list of alternate glyphs of CHARACTER in FONT-OBJECT.
51c01100 4654OTF-FEATURES specifies which features of the font FONT-OBJECT to apply
e80e09b4
KH
4655in this format:
4656 (SCRIPT LANGSYS FEATURE ...)
027a33c0 4657See the documentation of `font-drive-otf' for more detail.
e80e09b4
KH
4658
4659The value is a list of cons cells of the format (GLYPH-ID . CHARACTER),
4660where GLYPH-ID is a glyph index of the font, and CHARACTER is a
4661character code corresponding to the glyph or nil if there's no
4662corresponding character. */)
5842a27b 4663 (Lisp_Object font_object, Lisp_Object character, Lisp_Object otf_features)
e80e09b4
KH
4664{
4665 struct font *font;
4666 Lisp_Object gstring_in, gstring_out, g;
4667 Lisp_Object alternates;
4668 int i, num;
4669
4670 CHECK_FONT_GET_OBJECT (font_object, font);
733fd013 4671 if (! font->driver->otf_drive)
e950d6f1
KH
4672 error ("Font backend %s can't drive OpenType GSUB table",
4673 SDATA (SYMBOL_NAME (font->driver->type)));
e80e09b4 4674 CHECK_CHARACTER (character);
733fd013 4675 CHECK_CONS (otf_features);
e80e09b4
KH
4676
4677 gstring_in = Ffont_make_gstring (font_object, make_number (1));
4678 g = LGSTRING_GLYPH (gstring_in, 0);
f9ffa1ea 4679 LGLYPH_SET_CHAR (g, XINT (character));
e80e09b4 4680 gstring_out = Ffont_make_gstring (font_object, make_number (10));
733fd013
KH
4681 while ((num = font->driver->otf_drive (font, otf_features, gstring_in, 0, 1,
4682 gstring_out, 0, 1)) < 0)
e80e09b4
KH
4683 gstring_out = Ffont_make_gstring (font_object,
4684 make_number (ASIZE (gstring_out) * 2));
4685 alternates = Qnil;
4686 for (i = 0; i < num; i++)
4687 {
4688 Lisp_Object g = LGSTRING_GLYPH (gstring_out, i);
f9ffa1ea
SM
4689 int c = LGLYPH_CHAR (g);
4690 unsigned code = LGLYPH_CODE (g);
e80e09b4
KH
4691
4692 alternates = Fcons (Fcons (make_number (code),
4693 c > 0 ? make_number (c) : Qnil),
4694 alternates);
4695 }
4696 return Fnreverse (alternates);
4697}
6a3dadd2 4698#endif /* 0 */
c2f5bfd6
KH
4699
4700#ifdef FONT_DEBUG
4701
4702DEFUN ("open-font", Fopen_font, Sopen_font, 1, 3, 0,
4703 doc: /* Open FONT-ENTITY. */)
5842a27b 4704 (Lisp_Object font_entity, Lisp_Object size, Lisp_Object frame)
c2f5bfd6
KH
4705{
4706 int isize;
4707
4708 CHECK_FONT_ENTITY (font_entity);
c2f5bfd6
KH
4709 if (NILP (frame))
4710 frame = selected_frame;
4711 CHECK_LIVE_FRAME (frame);
51c01100 4712
35027d0c
KH
4713 if (NILP (size))
4714 isize = XINT (AREF (font_entity, FONT_SIZE_INDEX));
4715 else
4716 {
4717 CHECK_NUMBER_OR_FLOAT (size);
4718 if (FLOATP (size))
d03b5cdb 4719 isize = POINT_TO_PIXEL (XFLOAT_DATA (size), XFRAME (frame)->resy);
35027d0c
KH
4720 else
4721 isize = XINT (size);
4722 if (isize == 0)
4723 isize = 120;
4724 }
c2f5bfd6
KH
4725 return font_open_entity (XFRAME (frame), font_entity, isize);
4726}
4727
4728DEFUN ("close-font", Fclose_font, Sclose_font, 1, 2, 0,
4729 doc: /* Close FONT-OBJECT. */)
5842a27b 4730 (Lisp_Object font_object, Lisp_Object frame)
c2f5bfd6
KH
4731{
4732 CHECK_FONT_OBJECT (font_object);
4733 if (NILP (frame))
4734 frame = selected_frame;
4735 CHECK_LIVE_FRAME (frame);
4736 font_close_object (XFRAME (frame), font_object);
4737 return Qnil;
4738}
4739
4740DEFUN ("query-font", Fquery_font, Squery_font, 1, 1, 0,
e80e09b4
KH
4741 doc: /* Return information about FONT-OBJECT.
4742The value is a vector:
4743 [ NAME FILENAME PIXEL-SIZE SIZE ASCENT DESCENT SPACE-WIDTH AVERAGE-WIDTH
e0708580 4744 CAPABILITY ]
e80e09b4
KH
4745
4746NAME is a string of the font name (or nil if the font backend doesn't
4747provide a name).
4748
4749FILENAME is a string of the font file (or nil if the font backend
4750doesn't provide a file name).
4751
4752PIXEL-SIZE is a pixel size by which the font is opened.
4753
027a33c0 4754SIZE is a maximum advance width of the font in pixels.
e80e09b4
KH
4755
4756ASCENT, DESCENT, SPACE-WIDTH, AVERAGE-WIDTH are metrics of the font in
027a33c0 4757pixels.
e80e09b4 4758
e0708580
KH
4759CAPABILITY is a list whose first element is a symbol representing the
4760font format \(x, opentype, truetype, type1, pcf, or bdf) and the
027a33c0 4761remaining elements describe the details of the font capability.
e0708580
KH
4762
4763If the font is OpenType font, the form of the list is
4764 \(opentype GSUB GPOS)
4765where GSUB shows which "GSUB" features the font supports, and GPOS
4766shows which "GPOS" features the font supports. Both GSUB and GPOS are
4767lists of the format:
4768 \((SCRIPT (LANGSYS FEATURE ...) ...) ...)
4769
4770If the font is not OpenType font, currently the length of the form is
4771one.
e80e09b4
KH
4772
4773SCRIPT is a symbol representing OpenType script tag.
4774
4775LANGSYS is a symbol representing OpenType langsys tag, or nil
4776representing the default langsys.
4777
51c01100 4778FEATURE is a symbol representing OpenType feature tag.
e80e09b4 4779
51c01100 4780If the font is not OpenType font, CAPABILITY is nil. */)
5842a27b 4781 (Lisp_Object font_object)
c2f5bfd6
KH
4782{
4783 struct font *font;
4784 Lisp_Object val;
4785
4786 CHECK_FONT_GET_OBJECT (font_object, font);
4787
4788 val = Fmake_vector (make_number (9), Qnil);
35027d0c
KH
4789 ASET (val, 0, AREF (font_object, FONT_NAME_INDEX));
4790 ASET (val, 1, AREF (font_object, FONT_FILE_INDEX));
c2f5bfd6 4791 ASET (val, 2, make_number (font->pixel_size));
35027d0c 4792 ASET (val, 3, make_number (font->max_width));
c2f5bfd6
KH
4793 ASET (val, 4, make_number (font->ascent));
4794 ASET (val, 5, make_number (font->descent));
35027d0c
KH
4795 ASET (val, 6, make_number (font->space_width));
4796 ASET (val, 7, make_number (font->average_width));
c2f5bfd6 4797 if (font->driver->otf_capability)
e0708580 4798 ASET (val, 8, Fcons (Qopentype, font->driver->otf_capability (font)));
c2f5bfd6
KH
4799 return val;
4800}
4801
a7840ffb
KH
4802DEFUN ("font-get-glyphs", Ffont_get_glyphs, Sfont_get_glyphs, 3, 4, 0,
4803 doc:
4804 /* Return a vector of FONT-OBJECT's glyphs for the specified characters.
4805FROM and TO are positions (integers or markers) specifying a region
4806of the current buffer.
4807If the optional fourth arg OBJECT is not nil, it is a string or a
4808vector containing the target characters.
4809
4810Each element is a vector containing information of a glyph in this format:
4811 [FROM-IDX TO-IDX C CODE WIDTH LBEARING RBEARING ASCENT DESCENT ADJUSTMENT]
4812where
4813 FROM is an index numbers of a character the glyph corresponds to.
4814 TO is the same as FROM.
4815 C is the character of the glyph.
4816 CODE is the glyph-code of C in FONT-OBJECT.
4817 WIDTH thru DESCENT are the metrics (in pixels) of the glyph.
4818 ADJUSTMENT is always nil.
4819If FONT-OBJECT doesn't have a glyph for a character,
4820the corresponding element is nil. */)
e1ffae3b
KH
4821 (Lisp_Object font_object, Lisp_Object from, Lisp_Object to,
4822 Lisp_Object object)
c2f5bfd6
KH
4823{
4824 struct font *font;
a7840ffb
KH
4825 int i, len, c;
4826 Lisp_Object *chars, vec;
4827 USE_SAFE_ALLOCA;
c2f5bfd6
KH
4828
4829 CHECK_FONT_GET_OBJECT (font_object, font);
a7840ffb
KH
4830 if (NILP (object))
4831 {
4832 EMACS_INT charpos, bytepos;
4833
4834 validate_region (&from, &to);
4835 if (EQ (from, to))
4836 return Qnil;
4837 len = XFASTINT (to) - XFASTINT (from);
4838 SAFE_ALLOCA_LISP (chars, len);
4839 charpos = XFASTINT (from);
4840 bytepos = CHAR_TO_BYTE (charpos);
4841 for (i = 0; charpos < XFASTINT (to); i++)
4842 {
4843 FETCH_CHAR_ADVANCE (c, charpos, bytepos);
4844 chars[i] = make_number (c);
4845 }
4846 }
4847 else if (STRINGP (object))
4848 {
4849 const unsigned char *p;
4850
4851 CHECK_NUMBER (from);
4852 CHECK_NUMBER (to);
4853 if (XINT (from) < 0 || XINT (from) > XINT (to)
4854 || XINT (to) > SCHARS (object))
4855 args_out_of_range_3 (object, from, to);
4856 if (EQ (from, to))
4857 return Qnil;
4858 len = XFASTINT (to) - XFASTINT (from);
4859 SAFE_ALLOCA_LISP (chars, len);
4860 p = SDATA (object);
4861 if (STRING_MULTIBYTE (object))
4862 for (i = 0; i < len; i++)
4863 {
4864 c = STRING_CHAR_ADVANCE (p);
4865 chars[i] = make_number (c);
4866 }
4867 else
4868 for (i = 0; i < len; i++)
4869 chars[i] = make_number (p[i]);
4870 }
4871 else
4872 {
4873 CHECK_VECTOR (object);
4874 CHECK_NUMBER (from);
4875 CHECK_NUMBER (to);
4876 if (XINT (from) < 0 || XINT (from) > XINT (to)
4877 || XINT (to) > ASIZE (object))
4878 args_out_of_range_3 (object, from, to);
4879 if (EQ (from, to))
4880 return Qnil;
4881 len = XFASTINT (to) - XFASTINT (from);
4882 for (i = 0; i < len; i++)
4883 {
4884 Lisp_Object elt = AREF (object, XFASTINT (from) + i);
4885 CHECK_CHARACTER (elt);
4886 }
4887 chars = &(AREF (object, XFASTINT (from)));
4888 }
4889
c2f5bfd6
KH
4890 vec = Fmake_vector (make_number (len), Qnil);
4891 for (i = 0; i < len; i++)
4892 {
a7840ffb
KH
4893 Lisp_Object g;
4894 int c = XFASTINT (chars[i]);
c2f5bfd6 4895 unsigned code;
2930d117 4896 EMACS_INT cod;
c2f5bfd6
KH
4897 struct font_metrics metrics;
4898
2930d117 4899 cod = code = font->driver->encode_char (font, c);
c2f5bfd6
KH
4900 if (code == FONT_INVALID_CODE)
4901 continue;
a7840ffb
KH
4902 g = Fmake_vector (make_number (LGLYPH_SIZE), Qnil);
4903 LGLYPH_SET_FROM (g, i);
4904 LGLYPH_SET_TO (g, i);
4905 LGLYPH_SET_CHAR (g, c);
4906 LGLYPH_SET_CODE (g, code);
51c01100 4907 font->driver->text_extents (font, &code, 1, &metrics);
a7840ffb
KH
4908 LGLYPH_SET_WIDTH (g, metrics.width);
4909 LGLYPH_SET_LBEARING (g, metrics.lbearing);
4910 LGLYPH_SET_RBEARING (g, metrics.rbearing);
4911 LGLYPH_SET_ASCENT (g, metrics.ascent);
4912 LGLYPH_SET_DESCENT (g, metrics.descent);
4913 ASET (vec, i, g);
4914 }
4915 if (! VECTORP (object))
4916 SAFE_FREE ();
c2f5bfd6
KH
4917 return vec;
4918}
4919
ec6fe57c 4920DEFUN ("font-match-p", Ffont_match_p, Sfont_match_p, 2, 2, 0,
67b5d7de 4921 doc: /* Return t if and only if font-spec SPEC matches with FONT.
ec6fe57c 4922FONT is a font-spec, font-entity, or font-object. */)
5842a27b 4923 (Lisp_Object spec, Lisp_Object font)
ec6fe57c
KH
4924{
4925 CHECK_FONT_SPEC (spec);
35027d0c 4926 CHECK_FONT (font);
ec6fe57c
KH
4927
4928 return (font_match_p (spec, font) ? Qt : Qnil);
4929}
4930
1701724c 4931DEFUN ("font-at", Ffont_at, Sfont_at, 1, 3, 0,
51c01100 4932 doc: /* Return a font-object for displaying a character at POSITION.
10d16101
KH
4933Optional second arg WINDOW, if non-nil, is a window displaying
4934the current buffer. It defaults to the currently selected window. */)
5842a27b 4935 (Lisp_Object position, Lisp_Object window, Lisp_Object string)
10d16101
KH
4936{
4937 struct window *w;
e3ee0340 4938 EMACS_INT pos;
10d16101 4939
1701724c
KH
4940 if (NILP (string))
4941 {
4942 CHECK_NUMBER_COERCE_MARKER (position);
4943 pos = XINT (position);
4944 if (pos < BEGV || pos >= ZV)
4945 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
1701724c
KH
4946 }
4947 else
4948 {
1701724c
KH
4949 CHECK_NUMBER (position);
4950 CHECK_STRING (string);
4951 pos = XINT (position);
4952 if (pos < 0 || pos >= SCHARS (string))
4953 args_out_of_range (string, position);
1701724c 4954 }
10d16101
KH
4955 if (NILP (window))
4956 window = selected_window;
4957 CHECK_LIVE_WINDOW (window);
40fb53d6 4958 w = XWINDOW (window);
10d16101 4959
40fb53d6 4960 return font_at (-1, pos, NULL, w, string);
10d16101
KH
4961}
4962
c2f5bfd6
KH
4963#if 0
4964DEFUN ("draw-string", Fdraw_string, Sdraw_string, 2, 2, 0,
4965 doc: /* Draw STRING by FONT-OBJECT on the top left corner of the current frame.
4966The value is a number of glyphs drawn.
4967Type C-l to recover what previously shown. */)
5842a27b 4968 (Lisp_Object font_object, Lisp_Object string)
c2f5bfd6
KH
4969{
4970 Lisp_Object frame = selected_frame;
4971 FRAME_PTR f = XFRAME (frame);
4972 struct font *font;
4973 struct face *face;
4974 int i, len, width;
4975 unsigned *code;
4976
4977 CHECK_FONT_GET_OBJECT (font_object, font);
4978 CHECK_STRING (string);
4979 len = SCHARS (string);
4980 code = alloca (sizeof (unsigned) * len);
4981 for (i = 0; i < len; i++)
4982 {
4983 Lisp_Object ch = Faref (string, make_number (i));
4984 Lisp_Object val;
4985 int c = XINT (ch);
4986
4987 code[i] = font->driver->encode_char (font, c);
4988 if (code[i] == FONT_INVALID_CODE)
4989 break;
4990 }
4991 face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4992 face->fontp = font;
4993 if (font->driver->prepare_face)
4994 font->driver->prepare_face (f, face);
4995 width = font->driver->text_extents (font, code, i, NULL);
4996 len = font->driver->draw_text (f, face, 0, font->ascent, code, i, width);
4997 if (font->driver->done_face)
4998 font->driver->done_face (f, face);
4999 face->fontp = NULL;
5000 return make_number (len);
5001}
5002#endif
5003
5004#endif /* FONT_DEBUG */
5005
a266686a
KH
5006#ifdef HAVE_WINDOW_SYSTEM
5007
72606e45
KH
5008DEFUN ("font-info", Ffont_info, Sfont_info, 1, 2, 0,
5009 doc: /* Return information about a font named NAME on frame FRAME.
5010If FRAME is omitted or nil, use the selected frame.
06f19b91 5011The returned value is a vector of OPENED-NAME, FULL-NAME, SIZE,
72606e45
KH
5012 HEIGHT, BASELINE-OFFSET, RELATIVE-COMPOSE, and DEFAULT-ASCENT,
5013where
5014 OPENED-NAME is the name used for opening the font,
5015 FULL-NAME is the full name of the font,
06f19b91
KH
5016 SIZE is the pixelsize of the font,
5017 HEIGHT is the pixel-height of the font (i.e ascent + descent),
72606e45
KH
5018 BASELINE-OFFSET is the upward offset pixels from ASCII baseline,
5019 RELATIVE-COMPOSE and DEFAULT-ASCENT are the numbers controlling
5020 how to compose characters.
5021If the named font is not yet loaded, return nil. */)
5842a27b 5022 (Lisp_Object name, Lisp_Object frame)
72606e45
KH
5023{
5024 FRAME_PTR f;
5025 struct font *font;
5026 Lisp_Object info;
5027 Lisp_Object font_object;
5028
5029 (*check_window_system_func) ();
5030
5031 if (! FONTP (name))
5032 CHECK_STRING (name);
5033 if (NILP (frame))
5034 frame = selected_frame;
5035 CHECK_LIVE_FRAME (frame);
5036 f = XFRAME (frame);
5037
5038 if (STRINGP (name))
5039 {
5040 int fontset = fs_query_fontset (name, 0);
5041
5042 if (fontset >= 0)
5043 name = fontset_ascii (fontset);
5044 font_object = font_open_by_name (f, (char *) SDATA (name));
5045 }
5046 else if (FONT_OBJECT_P (name))
5047 font_object = name;
5048 else if (FONT_ENTITY_P (name))
5049 font_object = font_open_entity (f, name, 0);
5050 else
5051 {
5052 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5053 Lisp_Object entity = font_matching_entity (f, face->lface, name);
5054
5055 font_object = ! NILP (entity) ? font_open_entity (f, entity, 0) : Qnil;
5056 }
5057 if (NILP (font_object))
5058 return Qnil;
5059 font = XFONT_OBJECT (font_object);
5060
5061 info = Fmake_vector (make_number (7), Qnil);
5062 XVECTOR (info)->contents[0] = AREF (font_object, FONT_NAME_INDEX);
06f19b91 5063 XVECTOR (info)->contents[1] = AREF (font_object, FONT_FULLNAME_INDEX);
72606e45
KH
5064 XVECTOR (info)->contents[2] = make_number (font->pixel_size);
5065 XVECTOR (info)->contents[3] = make_number (font->height);
5066 XVECTOR (info)->contents[4] = make_number (font->baseline_offset);
5067 XVECTOR (info)->contents[5] = make_number (font->relative_compose);
5068 XVECTOR (info)->contents[6] = make_number (font->default_ascent);
5069
5070#if 0
5071 /* As font_object is still in FONT_OBJLIST of the entity, we can't
5072 close it now. Perhaps, we should manage font-objects
5073 by `reference-count'. */
5074 font_close_object (f, font_object);
5075#endif
5076 return info;
5077}
a266686a 5078#endif
72606e45 5079
c2f5bfd6 5080\f
d0ab1ebe
KH
5081#define BUILD_STYLE_TABLE(TBL) \
5082 build_style_table ((TBL), sizeof TBL / sizeof (struct table_entry))
5083
5084static Lisp_Object
971de7fb 5085build_style_table (const struct table_entry *entry, int nelement)
d0ab1ebe
KH
5086{
5087 int i, j;
5088 Lisp_Object table, elt;
17ab8f5d 5089
d0ab1ebe
KH
5090 table = Fmake_vector (make_number (nelement), Qnil);
5091 for (i = 0; i < nelement; i++)
5092 {
5093 for (j = 0; entry[i].names[j]; j++);
5094 elt = Fmake_vector (make_number (j + 1), Qnil);
5095 ASET (elt, 0, make_number (entry[i].numeric));
5096 for (j = 0; entry[i].names[j]; j++)
d67b4f80 5097 ASET (elt, j + 1, intern_c_string (entry[i].names[j]));
d0ab1ebe
KH
5098 ASET (table, i, elt);
5099 }
5100 return table;
5101}
5102
652b9560 5103Lisp_Object Vfont_log;
d0ab1ebe 5104
d0818984
KH
5105/* The deferred font-log data of the form [ACTION ARG RESULT].
5106 If ACTION is not nil, that is added to the log when font_add_log is
5107 called next time. At that time, ACTION is set back to nil. */
5108static Lisp_Object Vfont_log_deferred;
5109
5110/* Prepend the font-related logging data in Vfont_log if it is not
5111 `t'. ACTION describes a kind of font-related action (e.g. listing,
5112 opening), ARG is the argument for the action, and RESULT is the
5113 result of the action. */
d0ab1ebe 5114void
675e2c69 5115font_add_log (const char *action, Lisp_Object arg, Lisp_Object result)
d0ab1ebe
KH
5116{
5117 Lisp_Object tail, val;
5118 int i;
5119
d0ab1ebe
KH
5120 if (EQ (Vfont_log, Qt))
5121 return;
d0818984
KH
5122 if (STRINGP (AREF (Vfont_log_deferred, 0)))
5123 {
071132a9 5124 char *str = (char *) SDATA (AREF (Vfont_log_deferred, 0));
d0818984
KH
5125
5126 ASET (Vfont_log_deferred, 0, Qnil);
5127 font_add_log (str, AREF (Vfont_log_deferred, 1),
5128 AREF (Vfont_log_deferred, 2));
5129 }
5130
d0ab1ebe 5131 if (FONTP (arg))
db716644
KH
5132 {
5133 Lisp_Object tail, elt;
5134 Lisp_Object equalstr = build_string ("=");
5135
5136 val = Ffont_xlfd_name (arg, Qt);
5137 for (tail = AREF (arg, FONT_EXTRA_INDEX); CONSP (tail);
5138 tail = XCDR (tail))
5139 {
5140 elt = XCAR (tail);
49f9c344
KH
5141 if (EQ (XCAR (elt), QCscript)
5142 && SYMBOLP (XCDR (elt)))
db716644
KH
5143 val = concat3 (val, SYMBOL_NAME (QCscript),
5144 concat2 (equalstr, SYMBOL_NAME (XCDR (elt))));
49f9c344
KH
5145 else if (EQ (XCAR (elt), QClang)
5146 && SYMBOLP (XCDR (elt)))
db716644
KH
5147 val = concat3 (val, SYMBOL_NAME (QClang),
5148 concat2 (equalstr, SYMBOL_NAME (XCDR (elt))));
49f9c344
KH
5149 else if (EQ (XCAR (elt), QCotf)
5150 && CONSP (XCDR (elt)) && SYMBOLP (XCAR (XCDR (elt))))
db716644
KH
5151 val = concat3 (val, SYMBOL_NAME (QCotf),
5152 concat2 (equalstr,
5153 SYMBOL_NAME (XCAR (XCDR (elt)))));
5154 }
5155 arg = val;
5156 }
72d36834
KH
5157
5158 if (CONSP (result)
5159 && VECTORP (XCAR (result))
5160 && ASIZE (XCAR (result)) > 0
5161 && FONTP (AREF (XCAR (result), 0)))
5162 result = font_vconcat_entity_vectors (result);
d0ab1ebe 5163 if (FONTP (result))
d26424c5
KH
5164 {
5165 val = Ffont_xlfd_name (result, Qt);
5166 if (! FONT_SPEC_P (result))
5167 val = concat3 (SYMBOL_NAME (AREF (result, FONT_TYPE_INDEX)),
5168 build_string (":"), val);
5169 result = val;
5170 }
d0ab1ebe
KH
5171 else if (CONSP (result))
5172 {
5173 result = Fcopy_sequence (result);
5174 for (tail = result; CONSP (tail); tail = XCDR (tail))
5175 {
5176 val = XCAR (tail);
5177 if (FONTP (val))
5178 val = Ffont_xlfd_name (val, Qt);
5179 XSETCAR (tail, val);
5180 }
5181 }
5182 else if (VECTORP (result))
5183 {
5184 result = Fcopy_sequence (result);
5185 for (i = 0; i < ASIZE (result); i++)
5186 {
5187 val = AREF (result, i);
5188 if (FONTP (val))
5189 val = Ffont_xlfd_name (val, Qt);
5190 ASET (result, i, val);
5191 }
5192 }
5193 Vfont_log = Fcons (list3 (intern (action), arg, result), Vfont_log);
5194}
5195
d0818984
KH
5196/* Record a font-related logging data to be added to Vfont_log when
5197 font_add_log is called next time. ACTION, ARG, RESULT are the same
5198 as font_add_log. */
5199
5200void
675e2c69 5201font_deferred_log (const char *action, Lisp_Object arg, Lisp_Object result)
d0818984 5202{
652b9560
KH
5203 if (EQ (Vfont_log, Qt))
5204 return;
d0818984
KH
5205 ASET (Vfont_log_deferred, 0, build_string (action));
5206 ASET (Vfont_log_deferred, 1, arg);
5207 ASET (Vfont_log_deferred, 2, result);
ba3de0e8 5208}
d0818984 5209
c2f5bfd6 5210void
971de7fb 5211syms_of_font (void)
c2f5bfd6 5212{
4007dd1c
KH
5213 sort_shift_bits[FONT_TYPE_INDEX] = 0;
5214 sort_shift_bits[FONT_SLANT_INDEX] = 2;
5215 sort_shift_bits[FONT_WEIGHT_INDEX] = 9;
5216 sort_shift_bits[FONT_SIZE_INDEX] = 16;
5217 sort_shift_bits[FONT_WIDTH_INDEX] = 23;
5218 /* Note that the other elements in sort_shift_bits are not used. */
c2f5bfd6 5219
1701724c
KH
5220 staticpro (&font_charset_alist);
5221 font_charset_alist = Qnil;
5222
e0708580 5223 DEFSYM (Qopentype, "opentype");
c2f5bfd6 5224
9e1bb909 5225 DEFSYM (Qascii_0, "ascii-0");
1bb1d99b
KH
5226 DEFSYM (Qiso8859_1, "iso8859-1");
5227 DEFSYM (Qiso10646_1, "iso10646-1");
5228 DEFSYM (Qunicode_bmp, "unicode-bmp");
cf96c5c2 5229 DEFSYM (Qunicode_sip, "unicode-sip");
1bb1d99b 5230
071132a9
KH
5231 DEFSYM (QCf, "Cf");
5232
c2f5bfd6 5233 DEFSYM (QCotf, ":otf");
35027d0c 5234 DEFSYM (QClang, ":lang");
c2f5bfd6 5235 DEFSYM (QCscript, ":script");
4c496d0d 5236 DEFSYM (QCantialias, ":antialias");
c2f5bfd6
KH
5237
5238 DEFSYM (QCfoundry, ":foundry");
5239 DEFSYM (QCadstyle, ":adstyle");
5240 DEFSYM (QCregistry, ":registry");
9331887d
KH
5241 DEFSYM (QCspacing, ":spacing");
5242 DEFSYM (QCdpi, ":dpi");
ec6fe57c 5243 DEFSYM (QCscalable, ":scalable");
35027d0c
KH
5244 DEFSYM (QCavgwidth, ":avgwidth");
5245 DEFSYM (QCfont_entity, ":font-entity");
5246 DEFSYM (QCfc_unknown_spec, ":fc-unknown-spec");
c2f5bfd6 5247
ec6fe57c
KH
5248 DEFSYM (Qc, "c");
5249 DEFSYM (Qm, "m");
5250 DEFSYM (Qp, "p");
5251 DEFSYM (Qd, "d");
5252
cf702558
CY
5253 DEFSYM (Qja, "ja");
5254 DEFSYM (Qko, "ko");
5255
42707278
JD
5256 DEFSYM (QCuser_spec, "user-spec");
5257
c2f5bfd6
KH
5258 staticpro (&null_vector);
5259 null_vector = Fmake_vector (make_number (0), Qnil);
5260
5261 staticpro (&scratch_font_spec);
5262 scratch_font_spec = Ffont_spec (0, NULL);
5263 staticpro (&scratch_font_prefer);
5264 scratch_font_prefer = Ffont_spec (0, NULL);
5265
d0818984
KH
5266 staticpro (&Vfont_log_deferred);
5267 Vfont_log_deferred = Fmake_vector (make_number (3), Qnil);
5268
6a3dadd2 5269#if 0
733fd013
KH
5270#ifdef HAVE_LIBOTF
5271 staticpro (&otf_list);
5272 otf_list = Qnil;
6a3dadd2
KH
5273#endif /* HAVE_LIBOTF */
5274#endif /* 0 */
733fd013 5275
c2f5bfd6
KH
5276 defsubr (&Sfontp);
5277 defsubr (&Sfont_spec);
5278 defsubr (&Sfont_get);
51cf11be 5279#ifdef HAVE_WINDOW_SYSTEM
b1868a1a 5280 defsubr (&Sfont_face_attributes);
51cf11be 5281#endif
c2f5bfd6
KH
5282 defsubr (&Sfont_put);
5283 defsubr (&Slist_fonts);
35027d0c 5284 defsubr (&Sfont_family_list);
c2f5bfd6
KH
5285 defsubr (&Sfind_font);
5286 defsubr (&Sfont_xlfd_name);
5287 defsubr (&Sclear_font_cache);
071132a9 5288 defsubr (&Sfont_shape_gstring);
78a2f9cd 5289 defsubr (&Sfont_variation_glyphs);
6a3dadd2 5290#if 0
733fd013 5291 defsubr (&Sfont_drive_otf);
e80e09b4 5292 defsubr (&Sfont_otf_alternates);
6a3dadd2 5293#endif /* 0 */
c2f5bfd6
KH
5294
5295#ifdef FONT_DEBUG
5296 defsubr (&Sopen_font);
5297 defsubr (&Sclose_font);
5298 defsubr (&Squery_font);
a7840ffb 5299 defsubr (&Sfont_get_glyphs);
ec6fe57c 5300 defsubr (&Sfont_match_p);
10d16101 5301 defsubr (&Sfont_at);
c2f5bfd6
KH
5302#if 0
5303 defsubr (&Sdraw_string);
5304#endif
5305#endif /* FONT_DEBUG */
a266686a 5306#ifdef HAVE_WINDOW_SYSTEM
72606e45 5307 defsubr (&Sfont_info);
a266686a 5308#endif
c2f5bfd6 5309
819e81df
KH
5310 DEFVAR_LISP ("font-encoding-alist", &Vfont_encoding_alist,
5311 doc: /*
5312Alist of fontname patterns vs the corresponding encoding and repertory info.
5313Each element looks like (REGEXP . (ENCODING . REPERTORY)),
5314where ENCODING is a charset or a char-table,
5315and REPERTORY is a charset, a char-table, or nil.
5316
027a33c0 5317If ENCODING and REPERTORY are the same, the element can have the form
819e81df
KH
5318\(REGEXP . ENCODING).
5319
5320ENCODING is for converting a character to a glyph code of the font.
5321If ENCODING is a charset, encoding a character by the charset gives
5322the corresponding glyph code. If ENCODING is a char-table, looking up
5323the table by a character gives the corresponding glyph code.
5324
5325REPERTORY specifies a repertory of characters supported by the font.
5326If REPERTORY is a charset, all characters beloging to the charset are
5327supported. If REPERTORY is a char-table, all characters who have a
027a33c0 5328non-nil value in the table are supported. If REPERTORY is nil, Emacs
819e81df
KH
5329gets the repertory information by an opened font and ENCODING. */);
5330 Vfont_encoding_alist = Qnil;
5331
933ac235
SM
5332 /* FIXME: These 3 vars are not quite what they appear: setq on them
5333 won't have any effect other than disconnect them from the style
5334 table used by the font display code. So we make them read-only,
5335 to avoid this confusing situation. */
5336
d0ab1ebe
KH
5337 DEFVAR_LISP_NOPRO ("font-weight-table", &Vfont_weight_table,
5338 doc: /* Vector of valid font weight values.
5339Each element has the form:
5340 [NUMERIC-VALUE SYMBOLIC-NAME ALIAS-NAME ...]
17ab8f5d 5341NUMERIC-VALUE is an integer, and SYMBOLIC-NAME and ALIAS-NAME are symbols. */);
d0ab1ebe 5342 Vfont_weight_table = BUILD_STYLE_TABLE (weight_table);
933ac235 5343 XSYMBOL (intern_c_string ("font-weight-table"))->constant = 1;
d0ab1ebe
KH
5344
5345 DEFVAR_LISP_NOPRO ("font-slant-table", &Vfont_slant_table,
5346 doc: /* Vector of font slant symbols vs the corresponding numeric values.
17ab8f5d 5347See `font-weight-table' for the format of the vector. */);
d0ab1ebe 5348 Vfont_slant_table = BUILD_STYLE_TABLE (slant_table);
933ac235 5349 XSYMBOL (intern_c_string ("font-slant-table"))->constant = 1;
d0ab1ebe
KH
5350
5351 DEFVAR_LISP_NOPRO ("font-width-table", &Vfont_width_table,
5352 doc: /* Alist of font width symbols vs the corresponding numeric values.
17ab8f5d 5353See `font-weight-table' for the format of the vector. */);
d0ab1ebe 5354 Vfont_width_table = BUILD_STYLE_TABLE (width_table);
933ac235 5355 XSYMBOL (intern_c_string ("font-width-table"))->constant = 1;
d0ab1ebe
KH
5356
5357 staticpro (&font_style_table);
5358 font_style_table = Fmake_vector (make_number (3), Qnil);
5359 ASET (font_style_table, 0, Vfont_weight_table);
5360 ASET (font_style_table, 1, Vfont_slant_table);
5361 ASET (font_style_table, 2, Vfont_width_table);
5362
5363 DEFVAR_LISP ("font-log", &Vfont_log, doc: /*
5364*Logging list of font related actions and results.
5365The value t means to suppress the logging.
5366The initial value is set to nil if the environment variable
5367EMACS_FONT_LOG is set. Otherwise, it is set to t. */);
5368 Vfont_log = Qnil;
5369
819e81df 5370#ifdef HAVE_WINDOW_SYSTEM
c2f5bfd6 5371#ifdef HAVE_FREETYPE
35027d0c 5372 syms_of_ftfont ();
c2f5bfd6 5373#ifdef HAVE_X_WINDOWS
35027d0c
KH
5374 syms_of_xfont ();
5375 syms_of_ftxfont ();
c2f5bfd6 5376#ifdef HAVE_XFT
35027d0c 5377 syms_of_xftfont ();
c2f5bfd6
KH
5378#endif /* HAVE_XFT */
5379#endif /* HAVE_X_WINDOWS */
5380#else /* not HAVE_FREETYPE */
5381#ifdef HAVE_X_WINDOWS
35027d0c 5382 syms_of_xfont ();
c2f5bfd6
KH
5383#endif /* HAVE_X_WINDOWS */
5384#endif /* not HAVE_FREETYPE */
5385#ifdef HAVE_BDFFONT
35027d0c 5386 syms_of_bdffont ();
c2f5bfd6
KH
5387#endif /* HAVE_BDFFONT */
5388#ifdef WINDOWSNT
35027d0c 5389 syms_of_w32font ();
c2f5bfd6 5390#endif /* WINDOWSNT */
edfda783
AR
5391#ifdef HAVE_NS
5392 syms_of_nsfont ();
5393#endif /* HAVE_NS */
819e81df 5394#endif /* HAVE_WINDOW_SYSTEM */
c2f5bfd6 5395}
885b7d09 5396
652b9560 5397void
971de7fb 5398init_font (void)
652b9560
KH
5399{
5400 Vfont_log = egetenv ("EMACS_FONT_LOG") ? Qnil : Qt;
5401}
5402