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