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