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