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