* net/tramp.el (tramp-file-name-handler)
[emacs.git] / src / font.c
blobb49664b5f31f6b16c68d7ca3336f42c8252cabc4
1 /* font.c -- "Font" primitives.
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
8 This file is part of GNU Emacs.
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.
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.
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/>. */
23 #include <config.h>
24 #include <float.h>
25 #include <stdio.h>
27 #include <c-ctype.h>
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"
40 #ifdef HAVE_WINDOW_SYSTEM
41 #include TERM_HEADER
42 #endif /* HAVE_WINDOW_SYSTEM */
44 Lisp_Object Qopentype;
46 /* Important character set strings. */
47 Lisp_Object Qascii_0, Qiso8859_1, Qiso10646_1, Qunicode_bmp, Qunicode_sip;
49 #define DEFAULT_ENCODING Qiso8859_1
51 /* Unicode category `Cf'. */
52 static Lisp_Object QCf;
54 /* Vector of Vfont_weight_table, Vfont_slant_table, and Vfont_width_table. */
55 static Lisp_Object font_style_table;
57 /* Structure used for tables mapping weight, slant, and width numeric
58 values and their names. */
60 struct table_entry
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];
68 /* Table of weight numeric values and their names. This table must be
69 sorted by numeric values in ascending order. */
71 static const struct table_entry weight_table[] =
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" }}
85 /* Table of slant numeric values and their names. This table must be
86 sorted by numeric values in ascending order. */
88 static const struct table_entry slant_table[] =
90 { 0, { "reverse-oblique", "ro" }},
91 { 10, { "reverse-italic", "ri" }},
92 { 100, { "normal", "r", "unspecified" }},
93 { 200, { "italic" ,"i", "ot" }},
94 { 210, { "oblique", "o" }}
97 /* Table of width numeric values and their names. This table must be
98 sorted by numeric values in ascending order. */
100 static const struct table_entry width_table[] =
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" }}
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;
126 static Lisp_Object QCuser_spec;
128 /* Alist of font registry symbols and the corresponding charset
129 information. The information is retrieved from
130 Vfont_encoding_alist on demand.
132 Eash element has the form:
133 (REGISTRY . (ENCODING-CHARSET-ID . REPERTORY-CHARSET-ID))
135 (REGISTRY . nil)
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.
142 The latter form means that the information for REGISTRY couldn't be
143 retrieved. */
144 static Lisp_Object font_charset_alist;
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;
151 #ifdef ENABLE_CHECKING
153 /* Used to catch bogus pointers in font objects. */
155 bool
156 valid_font_driver (struct font_driver *drv)
158 Lisp_Object tail, frame;
159 struct font_driver_list *fdl;
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;
171 #endif /* ENABLE_CHECKING */
173 /* Creators of font-related Lisp object. */
175 static Lisp_Object
176 font_make_spec (void)
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;
187 Lisp_Object
188 font_make_entity (void)
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;
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)
205 Lisp_Object font_object;
206 struct font *font
207 = (struct font *) allocate_pseudovector (size, FONT_OBJECT_MAX, PVEC_FONT);
208 int i;
210 XSETFONT (font_object, font);
212 if (! NILP (entity))
214 for (i = 1; i < FONT_SPEC_MAX; i++)
215 font->props[i] = AREF (entity, i);
216 if (! NILP (AREF (entity, FONT_EXTRA_INDEX)))
217 font->props[FONT_EXTRA_INDEX]
218 = Fcopy_alist (AREF (entity, FONT_EXTRA_INDEX));
220 if (size > 0)
221 font->props[FONT_SIZE_INDEX] = make_number (pixelsize);
222 return font_object;
227 static int font_pixel_size (struct frame *f, Lisp_Object);
228 static Lisp_Object font_open_entity (struct frame *, Lisp_Object, int);
229 static Lisp_Object font_matching_entity (struct frame *, Lisp_Object *,
230 Lisp_Object);
231 static unsigned font_encode_char (Lisp_Object, int);
233 /* Number of registered font drivers. */
234 static int num_font_drivers;
237 /* Return a Lispy value of a font property value at STR and LEN bytes.
238 If STR is "*", return nil. If FORCE_SYMBOL, or if STR does not
239 consist entirely of one or more digits, return a symbol interned
240 from STR. Otherwise, return an integer. */
242 Lisp_Object
243 font_intern_prop (const char *str, ptrdiff_t len, bool force_symbol)
245 ptrdiff_t i;
246 Lisp_Object tem;
247 Lisp_Object obarray;
248 ptrdiff_t nbytes, nchars;
250 if (len == 1 && *str == '*')
251 return Qnil;
252 if (!force_symbol && 0 < len && '0' <= *str && *str <= '9')
254 for (i = 1; i < len; i++)
255 if (! ('0' <= str[i] && str[i] <= '9'))
256 break;
257 if (i == len)
259 EMACS_INT n;
261 i = 0;
262 for (n = 0; (n += str[i++] - '0') <= MOST_POSITIVE_FIXNUM; n *= 10)
264 if (i == len)
265 return make_number (n);
266 if (MOST_POSITIVE_FIXNUM / 10 < n)
267 break;
270 xsignal1 (Qoverflow_error, make_string (str, len));
274 /* This code is similar to intern function from lread.c. */
275 obarray = check_obarray (Vobarray);
276 parse_str_as_multibyte ((unsigned char *) str, len, &nchars, &nbytes);
277 tem = oblookup (obarray, str,
278 (len == nchars || len != nbytes) ? len : nchars, len);
280 if (SYMBOLP (tem))
281 return tem;
282 if (len == nchars || len != nbytes)
283 tem = make_unibyte_string (str, len);
284 else
285 tem = make_multibyte_string (str, nchars, len);
286 return Fintern (tem, obarray);
289 /* Return a pixel size of font-spec SPEC on frame F. */
291 static int
292 font_pixel_size (struct frame *f, Lisp_Object spec)
294 #ifdef HAVE_WINDOW_SYSTEM
295 Lisp_Object size = AREF (spec, FONT_SIZE_INDEX);
296 double point_size;
297 int dpi, pixel_size;
298 Lisp_Object val;
300 if (INTEGERP (size))
301 return XINT (size);
302 if (NILP (size))
303 return 0;
304 eassert (FLOATP (size));
305 point_size = XFLOAT_DATA (size);
306 val = AREF (spec, FONT_DPI_INDEX);
307 if (INTEGERP (val))
308 dpi = XINT (val);
309 else
310 dpi = FRAME_RES_Y (f);
311 pixel_size = POINT_TO_PIXEL (point_size, dpi);
312 return pixel_size;
313 #else
314 return 1;
315 #endif
319 /* Return a value of PROP's VAL (symbol or integer) to be stored in a
320 font vector. If VAL is not valid (i.e. not registered in
321 font_style_table), return -1 if NOERROR is zero, and return a
322 proper index if NOERROR is nonzero. In that case, register VAL in
323 font_style_table if VAL is a symbol, and return the closest index if
324 VAL is an integer. */
327 font_style_to_value (enum font_property_index prop, Lisp_Object val,
328 bool noerror)
330 Lisp_Object table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
331 int len;
333 CHECK_VECTOR (table);
334 len = ASIZE (table);
336 if (SYMBOLP (val))
338 int i, j;
339 char *s;
340 Lisp_Object args[2], elt;
342 /* At first try exact match. */
343 for (i = 0; i < len; i++)
345 CHECK_VECTOR (AREF (table, i));
346 for (j = 1; j < ASIZE (AREF (table, i)); j++)
347 if (EQ (val, AREF (AREF (table, i), j)))
349 CHECK_NUMBER (AREF (AREF (table, i), 0));
350 return ((XINT (AREF (AREF (table, i), 0)) << 8)
351 | (i << 4) | (j - 1));
354 /* Try also with case-folding match. */
355 s = SSDATA (SYMBOL_NAME (val));
356 for (i = 0; i < len; i++)
357 for (j = 1; j < ASIZE (AREF (table, i)); j++)
359 elt = AREF (AREF (table, i), j);
360 if (xstrcasecmp (s, SSDATA (SYMBOL_NAME (elt))) == 0)
362 CHECK_NUMBER (AREF (AREF (table, i), 0));
363 return ((XINT (AREF (AREF (table, i), 0)) << 8)
364 | (i << 4) | (j - 1));
367 if (! noerror)
368 return -1;
369 eassert (len < 255);
370 elt = Fmake_vector (make_number (2), make_number (100));
371 ASET (elt, 1, val);
372 args[0] = table;
373 args[1] = Fmake_vector (make_number (1), elt);
374 ASET (font_style_table, prop - FONT_WEIGHT_INDEX, Fvconcat (2, args));
375 return (100 << 8) | (i << 4);
377 else
379 int i, last_n;
380 EMACS_INT numeric = XINT (val);
382 for (i = 0, last_n = -1; i < len; i++)
384 int n;
386 CHECK_VECTOR (AREF (table, i));
387 CHECK_NUMBER (AREF (AREF (table, i), 0));
388 n = XINT (AREF (AREF (table, i), 0));
389 if (numeric == n)
390 return (n << 8) | (i << 4);
391 if (numeric < n)
393 if (! noerror)
394 return -1;
395 return ((i == 0 || n - numeric < numeric - last_n)
396 ? (n << 8) | (i << 4): (last_n << 8 | ((i - 1) << 4)));
398 last_n = n;
400 if (! noerror)
401 return -1;
402 return ((last_n << 8) | ((i - 1) << 4));
406 Lisp_Object
407 font_style_symbolic (Lisp_Object font, enum font_property_index prop,
408 bool for_face)
410 Lisp_Object val = AREF (font, prop);
411 Lisp_Object table, elt;
412 int i;
414 if (NILP (val))
415 return Qnil;
416 table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
417 CHECK_VECTOR (table);
418 i = XINT (val) & 0xFF;
419 eassert (((i >> 4) & 0xF) < ASIZE (table));
420 elt = AREF (table, ((i >> 4) & 0xF));
421 CHECK_VECTOR (elt);
422 eassert ((i & 0xF) + 1 < ASIZE (elt));
423 elt = (for_face ? AREF (elt, 1) : AREF (elt, (i & 0xF) + 1));
424 CHECK_SYMBOL (elt);
425 return elt;
428 /* Return ENCODING or a cons of ENCODING and REPERTORY of the font
429 FONTNAME. ENCODING is a charset symbol that specifies the encoding
430 of the font. REPERTORY is a charset symbol or nil. */
432 Lisp_Object
433 find_font_encoding (Lisp_Object fontname)
435 Lisp_Object tail, elt;
437 for (tail = Vfont_encoding_alist; CONSP (tail); tail = XCDR (tail))
439 elt = XCAR (tail);
440 if (CONSP (elt)
441 && STRINGP (XCAR (elt))
442 && fast_string_match_ignore_case (XCAR (elt), fontname) >= 0
443 && (SYMBOLP (XCDR (elt))
444 ? CHARSETP (XCDR (elt))
445 : CONSP (XCDR (elt)) && CHARSETP (XCAR (XCDR (elt)))))
446 return (XCDR (elt));
448 return Qnil;
451 /* Return encoding charset and repertory charset for REGISTRY in
452 ENCODING and REPERTORY correspondingly. If correct information for
453 REGISTRY is available, return 0. Otherwise return -1. */
456 font_registry_charsets (Lisp_Object registry, struct charset **encoding, struct charset **repertory)
458 Lisp_Object val;
459 int encoding_id, repertory_id;
461 val = Fassoc_string (registry, font_charset_alist, Qt);
462 if (! NILP (val))
464 val = XCDR (val);
465 if (NILP (val))
466 return -1;
467 encoding_id = XINT (XCAR (val));
468 repertory_id = XINT (XCDR (val));
470 else
472 val = find_font_encoding (SYMBOL_NAME (registry));
473 if (SYMBOLP (val) && CHARSETP (val))
475 encoding_id = repertory_id = XINT (CHARSET_SYMBOL_ID (val));
477 else if (CONSP (val))
479 if (! CHARSETP (XCAR (val)))
480 goto invalid_entry;
481 encoding_id = XINT (CHARSET_SYMBOL_ID (XCAR (val)));
482 if (NILP (XCDR (val)))
483 repertory_id = -1;
484 else
486 if (! CHARSETP (XCDR (val)))
487 goto invalid_entry;
488 repertory_id = XINT (CHARSET_SYMBOL_ID (XCDR (val)));
491 else
492 goto invalid_entry;
493 val = Fcons (make_number (encoding_id), make_number (repertory_id));
494 font_charset_alist
495 = nconc2 (font_charset_alist, list1 (Fcons (registry, val)));
498 if (encoding)
499 *encoding = CHARSET_FROM_ID (encoding_id);
500 if (repertory)
501 *repertory = repertory_id >= 0 ? CHARSET_FROM_ID (repertory_id) : NULL;
502 return 0;
504 invalid_entry:
505 font_charset_alist
506 = nconc2 (font_charset_alist, list1 (Fcons (registry, Qnil)));
507 return -1;
511 /* Font property value validators. See the comment of
512 font_property_table for the meaning of the arguments. */
514 static Lisp_Object font_prop_validate (int, Lisp_Object, Lisp_Object);
515 static Lisp_Object font_prop_validate_symbol (Lisp_Object, Lisp_Object);
516 static Lisp_Object font_prop_validate_style (Lisp_Object, Lisp_Object);
517 static Lisp_Object font_prop_validate_non_neg (Lisp_Object, Lisp_Object);
518 static Lisp_Object font_prop_validate_spacing (Lisp_Object, Lisp_Object);
519 static int get_font_prop_index (Lisp_Object);
521 static Lisp_Object
522 font_prop_validate_symbol (Lisp_Object prop, Lisp_Object val)
524 if (STRINGP (val))
525 val = Fintern (val, Qnil);
526 if (! SYMBOLP (val))
527 val = Qerror;
528 else if (EQ (prop, QCregistry))
529 val = Fintern (Fdowncase (SYMBOL_NAME (val)), Qnil);
530 return val;
534 static Lisp_Object
535 font_prop_validate_style (Lisp_Object style, Lisp_Object val)
537 enum font_property_index prop = (EQ (style, QCweight) ? FONT_WEIGHT_INDEX
538 : EQ (style, QCslant) ? FONT_SLANT_INDEX
539 : FONT_WIDTH_INDEX);
540 if (INTEGERP (val))
542 EMACS_INT n = XINT (val);
543 CHECK_VECTOR (AREF (font_style_table, prop - FONT_WEIGHT_INDEX));
544 if (((n >> 4) & 0xF)
545 >= ASIZE (AREF (font_style_table, prop - FONT_WEIGHT_INDEX)))
546 val = Qerror;
547 else
549 Lisp_Object elt = AREF (AREF (font_style_table, prop - FONT_WEIGHT_INDEX), (n >> 4) & 0xF);
551 CHECK_VECTOR (elt);
552 if ((n & 0xF) + 1 >= ASIZE (elt))
553 val = Qerror;
554 else
556 CHECK_NUMBER (AREF (elt, 0));
557 if (XINT (AREF (elt, 0)) != (n >> 8))
558 val = Qerror;
562 else if (SYMBOLP (val))
564 int n = font_style_to_value (prop, val, 0);
566 val = n >= 0 ? make_number (n) : Qerror;
568 else
569 val = Qerror;
570 return val;
573 static Lisp_Object
574 font_prop_validate_non_neg (Lisp_Object prop, Lisp_Object val)
576 return (NATNUMP (val) || (FLOATP (val) && XFLOAT_DATA (val) >= 0)
577 ? val : Qerror);
580 static Lisp_Object
581 font_prop_validate_spacing (Lisp_Object prop, Lisp_Object val)
583 if (NILP (val) || (NATNUMP (val) && XINT (val) <= FONT_SPACING_CHARCELL))
584 return val;
585 if (SYMBOLP (val) && SBYTES (SYMBOL_NAME (val)) == 1)
587 char spacing = SDATA (SYMBOL_NAME (val))[0];
589 if (spacing == 'c' || spacing == 'C')
590 return make_number (FONT_SPACING_CHARCELL);
591 if (spacing == 'm' || spacing == 'M')
592 return make_number (FONT_SPACING_MONO);
593 if (spacing == 'p' || spacing == 'P')
594 return make_number (FONT_SPACING_PROPORTIONAL);
595 if (spacing == 'd' || spacing == 'D')
596 return make_number (FONT_SPACING_DUAL);
598 return Qerror;
601 static Lisp_Object
602 font_prop_validate_otf (Lisp_Object prop, Lisp_Object val)
604 Lisp_Object tail, tmp;
605 int i;
607 /* VAL = (SCRIPT [ LANGSYS [ GSUB-FEATURES [ GPOS-FEATURES ]]])
608 GSUB-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil
609 GPOS-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil */
610 if (! CONSP (val))
611 return Qerror;
612 if (! SYMBOLP (XCAR (val)))
613 return Qerror;
614 tail = XCDR (val);
615 if (NILP (tail))
616 return val;
617 if (! CONSP (tail) || ! SYMBOLP (XCAR (val)))
618 return Qerror;
619 for (i = 0; i < 2; i++)
621 tail = XCDR (tail);
622 if (NILP (tail))
623 return val;
624 if (! CONSP (tail))
625 return Qerror;
626 for (tmp = XCAR (tail); CONSP (tmp); tmp = XCDR (tmp))
627 if (! SYMBOLP (XCAR (tmp)))
628 return Qerror;
629 if (! NILP (tmp))
630 return Qerror;
632 return val;
635 /* Structure of known font property keys and validator of the
636 values. */
637 static const struct
639 /* Pointer to the key symbol. */
640 Lisp_Object *key;
641 /* Function to validate PROP's value VAL, or NULL if any value is
642 ok. The value is VAL or its regularized value if VAL is valid,
643 and Qerror if not. */
644 Lisp_Object (*validator) (Lisp_Object prop, Lisp_Object val);
645 } font_property_table[] =
646 { { &QCtype, font_prop_validate_symbol },
647 { &QCfoundry, font_prop_validate_symbol },
648 { &QCfamily, font_prop_validate_symbol },
649 { &QCadstyle, font_prop_validate_symbol },
650 { &QCregistry, font_prop_validate_symbol },
651 { &QCweight, font_prop_validate_style },
652 { &QCslant, font_prop_validate_style },
653 { &QCwidth, font_prop_validate_style },
654 { &QCsize, font_prop_validate_non_neg },
655 { &QCdpi, font_prop_validate_non_neg },
656 { &QCspacing, font_prop_validate_spacing },
657 { &QCavgwidth, font_prop_validate_non_neg },
658 /* The order of the above entries must match with enum
659 font_property_index. */
660 { &QClang, font_prop_validate_symbol },
661 { &QCscript, font_prop_validate_symbol },
662 { &QCotf, font_prop_validate_otf }
665 /* Size (number of elements) of the above table. */
666 #define FONT_PROPERTY_TABLE_SIZE \
667 ((sizeof font_property_table) / (sizeof *font_property_table))
669 /* Return an index number of font property KEY or -1 if KEY is not an
670 already known property. */
672 static int
673 get_font_prop_index (Lisp_Object key)
675 int i;
677 for (i = 0; i < FONT_PROPERTY_TABLE_SIZE; i++)
678 if (EQ (key, *font_property_table[i].key))
679 return i;
680 return -1;
683 /* Validate the font property. The property key is specified by the
684 symbol PROP, or the index IDX (if PROP is nil). If VAL is invalid,
685 signal an error. The value is VAL or the regularized one. */
687 static Lisp_Object
688 font_prop_validate (int idx, Lisp_Object prop, Lisp_Object val)
690 Lisp_Object validated;
692 if (NILP (val))
693 return val;
694 if (NILP (prop))
695 prop = *font_property_table[idx].key;
696 else
698 idx = get_font_prop_index (prop);
699 if (idx < 0)
700 return val;
702 validated = (font_property_table[idx].validator) (prop, val);
703 if (EQ (validated, Qerror))
704 signal_error ("invalid font property", Fcons (prop, val));
705 return validated;
709 /* Store VAL as a value of extra font property PROP in FONT while
710 keeping the sorting order. Don't check the validity of VAL. */
712 Lisp_Object
713 font_put_extra (Lisp_Object font, Lisp_Object prop, Lisp_Object val)
715 Lisp_Object extra = AREF (font, FONT_EXTRA_INDEX);
716 Lisp_Object slot = (NILP (extra) ? Qnil : assq_no_quit (prop, extra));
718 if (NILP (slot))
720 Lisp_Object prev = Qnil;
722 while (CONSP (extra)
723 && NILP (Fstring_lessp (prop, XCAR (XCAR (extra)))))
724 prev = extra, extra = XCDR (extra);
726 if (NILP (prev))
727 ASET (font, FONT_EXTRA_INDEX, Fcons (Fcons (prop, val), extra));
728 else
729 XSETCDR (prev, Fcons (Fcons (prop, val), extra));
731 return val;
733 XSETCDR (slot, val);
734 if (NILP (val))
735 ASET (font, FONT_EXTRA_INDEX, Fdelq (slot, extra));
736 return val;
740 /* Font name parser and unparser. */
742 static int parse_matrix (const char *);
743 static int font_expand_wildcards (Lisp_Object *, int);
744 static int font_parse_name (char *, ptrdiff_t, Lisp_Object);
746 /* An enumerator for each field of an XLFD font name. */
747 enum xlfd_field_index
749 XLFD_FOUNDRY_INDEX,
750 XLFD_FAMILY_INDEX,
751 XLFD_WEIGHT_INDEX,
752 XLFD_SLANT_INDEX,
753 XLFD_SWIDTH_INDEX,
754 XLFD_ADSTYLE_INDEX,
755 XLFD_PIXEL_INDEX,
756 XLFD_POINT_INDEX,
757 XLFD_RESX_INDEX,
758 XLFD_RESY_INDEX,
759 XLFD_SPACING_INDEX,
760 XLFD_AVGWIDTH_INDEX,
761 XLFD_REGISTRY_INDEX,
762 XLFD_ENCODING_INDEX,
763 XLFD_LAST_INDEX
766 /* An enumerator for mask bit corresponding to each XLFD field. */
767 enum xlfd_field_mask
769 XLFD_FOUNDRY_MASK = 0x0001,
770 XLFD_FAMILY_MASK = 0x0002,
771 XLFD_WEIGHT_MASK = 0x0004,
772 XLFD_SLANT_MASK = 0x0008,
773 XLFD_SWIDTH_MASK = 0x0010,
774 XLFD_ADSTYLE_MASK = 0x0020,
775 XLFD_PIXEL_MASK = 0x0040,
776 XLFD_POINT_MASK = 0x0080,
777 XLFD_RESX_MASK = 0x0100,
778 XLFD_RESY_MASK = 0x0200,
779 XLFD_SPACING_MASK = 0x0400,
780 XLFD_AVGWIDTH_MASK = 0x0800,
781 XLFD_REGISTRY_MASK = 0x1000,
782 XLFD_ENCODING_MASK = 0x2000
786 /* Parse P pointing to the pixel/point size field of the form
787 `[A B C D]' which specifies a transformation matrix:
789 A B 0
790 C D 0
791 0 0 1
793 by which all glyphs of the font are transformed. The spec says
794 that scalar value N for the pixel/point size is equivalent to:
795 A = N * resx/resy, B = C = 0, D = N.
797 Return the scalar value N if the form is valid. Otherwise return
798 -1. */
800 static int
801 parse_matrix (const char *p)
803 double matrix[4];
804 char *end;
805 int i;
807 for (i = 0, p++; i < 4 && *p && *p != ']'; i++)
809 if (*p == '~')
810 matrix[i] = - strtod (p + 1, &end);
811 else
812 matrix[i] = strtod (p, &end);
813 p = end;
815 return (i == 4 ? (int) matrix[3] : -1);
818 /* Expand a wildcard field in FIELD (the first N fields are filled) to
819 multiple fields to fill in all 14 XLFD fields while restricting a
820 field position by its contents. */
822 static int
823 font_expand_wildcards (Lisp_Object *field, int n)
825 /* Copy of FIELD. */
826 Lisp_Object tmp[XLFD_LAST_INDEX];
827 /* Array of information about where this element can go. Nth
828 element is for Nth element of FIELD. */
829 struct {
830 /* Minimum possible field. */
831 int from;
832 /* Maximum possible field. */
833 int to;
834 /* Bit mask of possible field. Nth bit corresponds to Nth field. */
835 int mask;
836 } range[XLFD_LAST_INDEX];
837 int i, j;
838 int range_from, range_to;
839 unsigned range_mask;
841 #define XLFD_SYMBOL_MASK (XLFD_FOUNDRY_MASK | XLFD_FAMILY_MASK \
842 | XLFD_ADSTYLE_MASK | XLFD_REGISTRY_MASK)
843 #define XLFD_NULL_MASK (XLFD_FOUNDRY_MASK | XLFD_ADSTYLE_MASK)
844 #define XLFD_LARGENUM_MASK (XLFD_POINT_MASK | XLFD_RESX_MASK | XLFD_RESY_MASK \
845 | XLFD_AVGWIDTH_MASK)
846 #define XLFD_REGENC_MASK (XLFD_REGISTRY_MASK | XLFD_ENCODING_MASK)
848 /* Initialize RANGE_MASK for FIELD[0] which can be 0th to (14 - N)th
849 field. The value is shifted to left one bit by one in the
850 following loop. */
851 for (i = 0, range_mask = 0; i <= 14 - n; i++)
852 range_mask = (range_mask << 1) | 1;
854 /* The triplet RANGE_FROM, RANGE_TO, and RANGE_MASK is a
855 position-based restriction for FIELD[I]. */
856 for (i = 0, range_from = 0, range_to = 14 - n; i < n;
857 i++, range_from++, range_to++, range_mask <<= 1)
859 Lisp_Object val = field[i];
861 tmp[i] = val;
862 if (NILP (val))
864 /* Wildcard. */
865 range[i].from = range_from;
866 range[i].to = range_to;
867 range[i].mask = range_mask;
869 else
871 /* The triplet FROM, TO, and MASK is a value-based
872 restriction for FIELD[I]. */
873 int from, to;
874 unsigned mask;
876 if (INTEGERP (val))
878 EMACS_INT numeric = XINT (val);
880 if (i + 1 == n)
881 from = to = XLFD_ENCODING_INDEX,
882 mask = XLFD_ENCODING_MASK;
883 else if (numeric == 0)
884 from = XLFD_PIXEL_INDEX, to = XLFD_AVGWIDTH_INDEX,
885 mask = XLFD_PIXEL_MASK | XLFD_LARGENUM_MASK;
886 else if (numeric <= 48)
887 from = to = XLFD_PIXEL_INDEX,
888 mask = XLFD_PIXEL_MASK;
889 else
890 from = XLFD_POINT_INDEX, to = XLFD_AVGWIDTH_INDEX,
891 mask = XLFD_LARGENUM_MASK;
893 else if (SBYTES (SYMBOL_NAME (val)) == 0)
894 from = XLFD_FOUNDRY_INDEX, to = XLFD_ADSTYLE_INDEX,
895 mask = XLFD_NULL_MASK;
896 else if (i == 0)
897 from = to = XLFD_FOUNDRY_INDEX, mask = XLFD_FOUNDRY_MASK;
898 else if (i + 1 == n)
900 Lisp_Object name = SYMBOL_NAME (val);
902 if (SDATA (name)[SBYTES (name) - 1] == '*')
903 from = XLFD_REGISTRY_INDEX, to = XLFD_ENCODING_INDEX,
904 mask = XLFD_REGENC_MASK;
905 else
906 from = to = XLFD_ENCODING_INDEX,
907 mask = XLFD_ENCODING_MASK;
909 else if (range_from <= XLFD_WEIGHT_INDEX
910 && range_to >= XLFD_WEIGHT_INDEX
911 && FONT_WEIGHT_NAME_NUMERIC (val) >= 0)
912 from = to = XLFD_WEIGHT_INDEX, mask = XLFD_WEIGHT_MASK;
913 else if (range_from <= XLFD_SLANT_INDEX
914 && range_to >= XLFD_SLANT_INDEX
915 && FONT_SLANT_NAME_NUMERIC (val) >= 0)
916 from = to = XLFD_SLANT_INDEX, mask = XLFD_SLANT_MASK;
917 else if (range_from <= XLFD_SWIDTH_INDEX
918 && range_to >= XLFD_SWIDTH_INDEX
919 && FONT_WIDTH_NAME_NUMERIC (val) >= 0)
920 from = to = XLFD_SWIDTH_INDEX, mask = XLFD_SWIDTH_MASK;
921 else
923 if (EQ (val, Qc) || EQ (val, Qm) || EQ (val, Qp) || EQ (val, Qd))
924 from = to = XLFD_SPACING_INDEX, mask = XLFD_SPACING_MASK;
925 else
926 from = XLFD_FOUNDRY_INDEX, to = XLFD_ENCODING_INDEX,
927 mask = XLFD_SYMBOL_MASK;
930 /* Merge position-based and value-based restrictions. */
931 mask &= range_mask;
932 while (from < range_from)
933 mask &= ~(1 << from++);
934 while (from < 14 && ! (mask & (1 << from)))
935 from++;
936 while (to > range_to)
937 mask &= ~(1 << to--);
938 while (to >= 0 && ! (mask & (1 << to)))
939 to--;
940 if (from > to)
941 return -1;
942 range[i].from = from;
943 range[i].to = to;
944 range[i].mask = mask;
946 if (from > range_from || to < range_to)
948 /* The range is narrowed by value-based restrictions.
949 Reflect it to the other fields. */
951 /* Following fields should be after FROM. */
952 range_from = from;
953 /* Preceding fields should be before TO. */
954 for (j = i - 1, from--, to--; j >= 0; j--, from--, to--)
956 /* Check FROM for non-wildcard field. */
957 if (! NILP (tmp[j]) && range[j].from < from)
959 while (range[j].from < from)
960 range[j].mask &= ~(1 << range[j].from++);
961 while (from < 14 && ! (range[j].mask & (1 << from)))
962 from++;
963 range[j].from = from;
965 else
966 from = range[j].from;
967 if (range[j].to > to)
969 while (range[j].to > to)
970 range[j].mask &= ~(1 << range[j].to--);
971 while (to >= 0 && ! (range[j].mask & (1 << to)))
972 to--;
973 range[j].to = to;
975 else
976 to = range[j].to;
977 if (from > to)
978 return -1;
984 /* Decide all fields from restrictions in RANGE. */
985 for (i = j = 0; i < n ; i++)
987 if (j < range[i].from)
989 if (i == 0 || ! NILP (tmp[i - 1]))
990 /* None of TMP[X] corresponds to Jth field. */
991 return -1;
992 for (; j < range[i].from; j++)
993 field[j] = Qnil;
995 field[j++] = tmp[i];
997 if (! NILP (tmp[n - 1]) && j < XLFD_REGISTRY_INDEX)
998 return -1;
999 for (; j < XLFD_LAST_INDEX; j++)
1000 field[j] = Qnil;
1001 if (INTEGERP (field[XLFD_ENCODING_INDEX]))
1002 field[XLFD_ENCODING_INDEX]
1003 = Fintern (Fnumber_to_string (field[XLFD_ENCODING_INDEX]), Qnil);
1004 return 0;
1008 /* Parse NAME (null terminated) as XLFD and store information in FONT
1009 (font-spec or font-entity). Size property of FONT is set as
1010 follows:
1011 specified XLFD fields FONT property
1012 --------------------- -------------
1013 PIXEL_SIZE PIXEL_SIZE (Lisp integer)
1014 POINT_SIZE and RESY calculated pixel size (Lisp integer)
1015 POINT_SIZE POINT_SIZE/10 (Lisp float)
1017 If NAME is successfully parsed, return 0. Otherwise return -1.
1019 FONT is usually a font-spec, but when this function is called from
1020 X font backend driver, it is a font-entity. In that case, NAME is
1021 a fully specified XLFD. */
1024 font_parse_xlfd (char *name, ptrdiff_t len, Lisp_Object font)
1026 int i, j, n;
1027 char *f[XLFD_LAST_INDEX + 1];
1028 Lisp_Object val;
1029 char *p;
1031 if (len > 255 || !len)
1032 /* Maximum XLFD name length is 255. */
1033 return -1;
1034 /* Accept "*-.." as a fully specified XLFD. */
1035 if (name[0] == '*' && (len == 1 || name[1] == '-'))
1036 i = 1, f[XLFD_FOUNDRY_INDEX] = name;
1037 else
1038 i = 0;
1039 for (p = name + i; *p; p++)
1040 if (*p == '-')
1042 f[i++] = p + 1;
1043 if (i == XLFD_LAST_INDEX)
1044 break;
1046 f[i] = name + len;
1048 #define INTERN_FIELD(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 0)
1049 #define INTERN_FIELD_SYM(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 1)
1051 if (i == XLFD_LAST_INDEX)
1053 /* Fully specified XLFD. */
1054 int pixel_size;
1056 ASET (font, FONT_FOUNDRY_INDEX, INTERN_FIELD_SYM (XLFD_FOUNDRY_INDEX));
1057 ASET (font, FONT_FAMILY_INDEX, INTERN_FIELD_SYM (XLFD_FAMILY_INDEX));
1058 for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
1059 i <= XLFD_SWIDTH_INDEX; i++, j++)
1061 val = INTERN_FIELD_SYM (i);
1062 if (! NILP (val))
1064 if ((n = font_style_to_value (j, INTERN_FIELD_SYM (i), 0)) < 0)
1065 return -1;
1066 ASET (font, j, make_number (n));
1069 ASET (font, FONT_ADSTYLE_INDEX, INTERN_FIELD_SYM (XLFD_ADSTYLE_INDEX));
1070 if (strcmp (f[XLFD_REGISTRY_INDEX], "*-*") == 0)
1071 ASET (font, FONT_REGISTRY_INDEX, Qnil);
1072 else
1073 ASET (font, FONT_REGISTRY_INDEX,
1074 font_intern_prop (f[XLFD_REGISTRY_INDEX],
1075 f[XLFD_LAST_INDEX] - f[XLFD_REGISTRY_INDEX],
1076 1));
1077 p = f[XLFD_PIXEL_INDEX];
1078 if (*p == '[' && (pixel_size = parse_matrix (p)) >= 0)
1079 ASET (font, FONT_SIZE_INDEX, make_number (pixel_size));
1080 else
1082 val = INTERN_FIELD (XLFD_PIXEL_INDEX);
1083 if (INTEGERP (val))
1084 ASET (font, FONT_SIZE_INDEX, val);
1085 else if (FONT_ENTITY_P (font))
1086 return -1;
1087 else
1089 double point_size = -1;
1091 eassert (FONT_SPEC_P (font));
1092 p = f[XLFD_POINT_INDEX];
1093 if (*p == '[')
1094 point_size = parse_matrix (p);
1095 else if (c_isdigit (*p))
1096 point_size = atoi (p), point_size /= 10;
1097 if (point_size >= 0)
1098 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1102 val = INTERN_FIELD (XLFD_RESY_INDEX);
1103 if (! NILP (val) && ! INTEGERP (val))
1104 return -1;
1105 ASET (font, FONT_DPI_INDEX, val);
1106 val = INTERN_FIELD (XLFD_SPACING_INDEX);
1107 if (! NILP (val))
1109 val = font_prop_validate_spacing (QCspacing, val);
1110 if (! INTEGERP (val))
1111 return -1;
1112 ASET (font, FONT_SPACING_INDEX, val);
1114 p = f[XLFD_AVGWIDTH_INDEX];
1115 if (*p == '~')
1116 p++;
1117 val = font_intern_prop (p, f[XLFD_REGISTRY_INDEX] - 1 - p, 0);
1118 if (! NILP (val) && ! INTEGERP (val))
1119 return -1;
1120 ASET (font, FONT_AVGWIDTH_INDEX, val);
1122 else
1124 bool wild_card_found = 0;
1125 Lisp_Object prop[XLFD_LAST_INDEX];
1127 if (FONT_ENTITY_P (font))
1128 return -1;
1129 for (j = 0; j < i; j++)
1131 if (*f[j] == '*')
1133 if (f[j][1] && f[j][1] != '-')
1134 return -1;
1135 prop[j] = Qnil;
1136 wild_card_found = 1;
1138 else if (j + 1 < i)
1139 prop[j] = INTERN_FIELD (j);
1140 else
1141 prop[j] = font_intern_prop (f[j], f[i] - f[j], 0);
1143 if (! wild_card_found)
1144 return -1;
1145 if (font_expand_wildcards (prop, i) < 0)
1146 return -1;
1148 ASET (font, FONT_FOUNDRY_INDEX, prop[XLFD_FOUNDRY_INDEX]);
1149 ASET (font, FONT_FAMILY_INDEX, prop[XLFD_FAMILY_INDEX]);
1150 for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
1151 i <= XLFD_SWIDTH_INDEX; i++, j++)
1152 if (! NILP (prop[i]))
1154 if ((n = font_style_to_value (j, prop[i], 1)) < 0)
1155 return -1;
1156 ASET (font, j, make_number (n));
1158 ASET (font, FONT_ADSTYLE_INDEX, prop[XLFD_ADSTYLE_INDEX]);
1159 val = prop[XLFD_REGISTRY_INDEX];
1160 if (NILP (val))
1162 val = prop[XLFD_ENCODING_INDEX];
1163 if (! NILP (val))
1164 val = concat2 (build_string ("*-"), SYMBOL_NAME (val));
1166 else if (NILP (prop[XLFD_ENCODING_INDEX]))
1167 val = concat2 (SYMBOL_NAME (val), build_string ("-*"));
1168 else
1169 val = concat3 (SYMBOL_NAME (val), build_string ("-"),
1170 SYMBOL_NAME (prop[XLFD_ENCODING_INDEX]));
1171 if (! NILP (val))
1172 ASET (font, FONT_REGISTRY_INDEX, Fintern (val, Qnil));
1174 if (INTEGERP (prop[XLFD_PIXEL_INDEX]))
1175 ASET (font, FONT_SIZE_INDEX, prop[XLFD_PIXEL_INDEX]);
1176 else if (INTEGERP (prop[XLFD_POINT_INDEX]))
1178 double point_size = XINT (prop[XLFD_POINT_INDEX]);
1180 ASET (font, FONT_SIZE_INDEX, make_float (point_size / 10));
1183 if (INTEGERP (prop[XLFD_RESX_INDEX]))
1184 ASET (font, FONT_DPI_INDEX, prop[XLFD_RESY_INDEX]);
1185 if (! NILP (prop[XLFD_SPACING_INDEX]))
1187 val = font_prop_validate_spacing (QCspacing,
1188 prop[XLFD_SPACING_INDEX]);
1189 if (! INTEGERP (val))
1190 return -1;
1191 ASET (font, FONT_SPACING_INDEX, val);
1193 if (INTEGERP (prop[XLFD_AVGWIDTH_INDEX]))
1194 ASET (font, FONT_AVGWIDTH_INDEX, prop[XLFD_AVGWIDTH_INDEX]);
1197 return 0;
1200 /* Store XLFD name of FONT (font-spec or font-entity) in NAME (NBYTES
1201 length), and return the name length. If FONT_SIZE_INDEX of FONT is
1202 0, use PIXEL_SIZE instead. */
1204 ptrdiff_t
1205 font_unparse_xlfd (Lisp_Object font, int pixel_size, char *name, int nbytes)
1207 char *p;
1208 const char *f[XLFD_REGISTRY_INDEX + 1];
1209 Lisp_Object val;
1210 int i, j, len;
1212 eassert (FONTP (font));
1214 for (i = FONT_FOUNDRY_INDEX, j = XLFD_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX;
1215 i++, j++)
1217 if (i == FONT_ADSTYLE_INDEX)
1218 j = XLFD_ADSTYLE_INDEX;
1219 else if (i == FONT_REGISTRY_INDEX)
1220 j = XLFD_REGISTRY_INDEX;
1221 val = AREF (font, i);
1222 if (NILP (val))
1224 if (j == XLFD_REGISTRY_INDEX)
1225 f[j] = "*-*";
1226 else
1227 f[j] = "*";
1229 else
1231 if (SYMBOLP (val))
1232 val = SYMBOL_NAME (val);
1233 if (j == XLFD_REGISTRY_INDEX
1234 && ! strchr (SSDATA (val), '-'))
1236 /* Change "jisx0208*" and "jisx0208" to "jisx0208*-*". */
1237 ptrdiff_t alloc = SBYTES (val) + 4;
1238 if (nbytes <= alloc)
1239 return -1;
1240 f[j] = p = alloca (alloc);
1241 sprintf (p, "%s%s-*", SDATA (val),
1242 &"*"[SDATA (val)[SBYTES (val) - 1] == '*']);
1244 else
1245 f[j] = SSDATA (val);
1249 for (i = FONT_WEIGHT_INDEX, j = XLFD_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX;
1250 i++, j++)
1252 val = font_style_symbolic (font, i, 0);
1253 if (NILP (val))
1254 f[j] = "*";
1255 else
1257 int c, k, l;
1258 ptrdiff_t alloc;
1260 val = SYMBOL_NAME (val);
1261 alloc = SBYTES (val) + 1;
1262 if (nbytes <= alloc)
1263 return -1;
1264 f[j] = p = alloca (alloc);
1265 /* Copy the name while excluding '-', '?', ',', and '"'. */
1266 for (k = l = 0; k < alloc; k++)
1268 c = SREF (val, k);
1269 if (c != '-' && c != '?' && c != ',' && c != '"')
1270 p[l++] = c;
1275 val = AREF (font, FONT_SIZE_INDEX);
1276 eassert (NUMBERP (val) || NILP (val));
1277 if (INTEGERP (val))
1279 EMACS_INT v = XINT (val);
1280 if (v <= 0)
1281 v = pixel_size;
1282 if (v > 0)
1284 f[XLFD_PIXEL_INDEX] = p =
1285 alloca (sizeof "-*" + INT_STRLEN_BOUND (EMACS_INT));
1286 sprintf (p, "%"pI"d-*", v);
1288 else
1289 f[XLFD_PIXEL_INDEX] = "*-*";
1291 else if (FLOATP (val))
1293 double v = XFLOAT_DATA (val) * 10;
1294 f[XLFD_PIXEL_INDEX] = p = alloca (sizeof "*-" + 1 + DBL_MAX_10_EXP + 1);
1295 sprintf (p, "*-%.0f", v);
1297 else
1298 f[XLFD_PIXEL_INDEX] = "*-*";
1300 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1302 EMACS_INT v = XINT (AREF (font, FONT_DPI_INDEX));
1303 f[XLFD_RESX_INDEX] = p =
1304 alloca (sizeof "-" + 2 * INT_STRLEN_BOUND (EMACS_INT));
1305 sprintf (p, "%"pI"d-%"pI"d", v, v);
1307 else
1308 f[XLFD_RESX_INDEX] = "*-*";
1309 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1311 EMACS_INT spacing = XINT (AREF (font, FONT_SPACING_INDEX));
1313 f[XLFD_SPACING_INDEX] = (spacing <= FONT_SPACING_PROPORTIONAL ? "p"
1314 : spacing <= FONT_SPACING_DUAL ? "d"
1315 : spacing <= FONT_SPACING_MONO ? "m"
1316 : "c");
1318 else
1319 f[XLFD_SPACING_INDEX] = "*";
1320 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1322 f[XLFD_AVGWIDTH_INDEX] = p = alloca (INT_BUFSIZE_BOUND (EMACS_INT));
1323 sprintf (p, "%"pI"d", XINT (AREF (font, FONT_AVGWIDTH_INDEX)));
1325 else
1326 f[XLFD_AVGWIDTH_INDEX] = "*";
1327 len = snprintf (name, nbytes, "-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s",
1328 f[XLFD_FOUNDRY_INDEX], f[XLFD_FAMILY_INDEX],
1329 f[XLFD_WEIGHT_INDEX], f[XLFD_SLANT_INDEX],
1330 f[XLFD_SWIDTH_INDEX], f[XLFD_ADSTYLE_INDEX],
1331 f[XLFD_PIXEL_INDEX], f[XLFD_RESX_INDEX],
1332 f[XLFD_SPACING_INDEX], f[XLFD_AVGWIDTH_INDEX],
1333 f[XLFD_REGISTRY_INDEX]);
1334 return len < nbytes ? len : -1;
1337 /* Parse NAME (null terminated) and store information in FONT
1338 (font-spec or font-entity). NAME is supplied in either the
1339 Fontconfig or GTK font name format. If NAME is successfully
1340 parsed, return 0. Otherwise return -1.
1342 The fontconfig format is
1344 FAMILY[-SIZE][:PROP1[=VAL1][:PROP2[=VAL2]...]]
1346 The GTK format is
1348 FAMILY [PROPS...] [SIZE]
1350 This function tries to guess which format it is. */
1352 static int
1353 font_parse_fcname (char *name, ptrdiff_t len, Lisp_Object font)
1355 char *p, *q;
1356 char *size_beg = NULL, *size_end = NULL;
1357 char *props_beg = NULL, *family_end = NULL;
1359 if (len == 0)
1360 return -1;
1362 for (p = name; *p; p++)
1364 if (*p == '\\' && p[1])
1365 p++;
1366 else if (*p == ':')
1368 props_beg = family_end = p;
1369 break;
1371 else if (*p == '-')
1373 bool decimal = 0, size_found = 1;
1374 for (q = p + 1; *q && *q != ':'; q++)
1375 if (! c_isdigit (*q))
1377 if (*q != '.' || decimal)
1379 size_found = 0;
1380 break;
1382 decimal = 1;
1384 if (size_found)
1386 family_end = p;
1387 size_beg = p + 1;
1388 size_end = q;
1389 break;
1394 if (family_end)
1396 Lisp_Object extra_props = Qnil;
1398 /* A fontconfig name with size and/or property data. */
1399 if (family_end > name)
1401 Lisp_Object family;
1402 family = font_intern_prop (name, family_end - name, 1);
1403 ASET (font, FONT_FAMILY_INDEX, family);
1405 if (size_beg)
1407 double point_size = strtod (size_beg, &size_end);
1408 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1409 if (*size_end == ':' && size_end[1])
1410 props_beg = size_end;
1412 if (props_beg)
1414 /* Now parse ":KEY=VAL" patterns. */
1415 Lisp_Object val;
1417 for (p = props_beg; *p; p = q)
1419 for (q = p + 1; *q && *q != '=' && *q != ':'; q++);
1420 if (*q != '=')
1422 /* Must be an enumerated value. */
1423 ptrdiff_t word_len;
1424 p = p + 1;
1425 word_len = q - p;
1426 val = font_intern_prop (p, q - p, 1);
1428 #define PROP_MATCH(STR) (word_len == strlen (STR) \
1429 && memcmp (p, STR, strlen (STR)) == 0)
1431 if (PROP_MATCH ("light")
1432 || PROP_MATCH ("medium")
1433 || PROP_MATCH ("demibold")
1434 || PROP_MATCH ("bold")
1435 || PROP_MATCH ("black"))
1436 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, val);
1437 else if (PROP_MATCH ("roman")
1438 || PROP_MATCH ("italic")
1439 || PROP_MATCH ("oblique"))
1440 FONT_SET_STYLE (font, FONT_SLANT_INDEX, val);
1441 else if (PROP_MATCH ("charcell"))
1442 ASET (font, FONT_SPACING_INDEX,
1443 make_number (FONT_SPACING_CHARCELL));
1444 else if (PROP_MATCH ("mono"))
1445 ASET (font, FONT_SPACING_INDEX,
1446 make_number (FONT_SPACING_MONO));
1447 else if (PROP_MATCH ("proportional"))
1448 ASET (font, FONT_SPACING_INDEX,
1449 make_number (FONT_SPACING_PROPORTIONAL));
1450 #undef PROP_MATCH
1452 else
1454 /* KEY=VAL pairs */
1455 Lisp_Object key;
1456 int prop;
1458 if (q - p == 10 && memcmp (p + 1, "pixelsize", 9) == 0)
1459 prop = FONT_SIZE_INDEX;
1460 else
1462 key = font_intern_prop (p, q - p, 1);
1463 prop = get_font_prop_index (key);
1466 p = q + 1;
1467 for (q = p; *q && *q != ':'; q++);
1468 val = font_intern_prop (p, q - p, 0);
1470 if (prop >= FONT_FOUNDRY_INDEX
1471 && prop < FONT_EXTRA_INDEX)
1472 ASET (font, prop, font_prop_validate (prop, Qnil, val));
1473 else
1475 extra_props = nconc2 (extra_props,
1476 list1 (Fcons (key, val)));
1479 p = q;
1483 if (! NILP (extra_props))
1485 struct font_driver_list *driver_list = font_driver_list;
1486 for ( ; driver_list; driver_list = driver_list->next)
1487 if (driver_list->driver->filter_properties)
1488 (*driver_list->driver->filter_properties) (font, extra_props);
1492 else
1494 /* Either a fontconfig-style name with no size and property
1495 data, or a GTK-style name. */
1496 Lisp_Object weight = Qnil, slant = Qnil;
1497 Lisp_Object width = Qnil, size = Qnil;
1498 char *word_start;
1499 ptrdiff_t word_len;
1501 /* Scan backwards from the end, looking for a size. */
1502 for (p = name + len - 1; p >= name; p--)
1503 if (!c_isdigit (*p))
1504 break;
1506 if ((p < name + len - 1) && ((p + 1 == name) || *p == ' '))
1507 /* Found a font size. */
1508 size = make_float (strtod (p + 1, NULL));
1509 else
1510 p = name + len;
1512 /* Now P points to the termination of the string, sans size.
1513 Scan backwards, looking for font properties. */
1514 for (; p > name; p = q)
1516 for (q = p - 1; q >= name; q--)
1518 if (q > name && *(q-1) == '\\')
1519 --q; /* Skip quoting backslashes. */
1520 else if (*q == ' ')
1521 break;
1524 word_start = q + 1;
1525 word_len = p - word_start;
1527 #define PROP_MATCH(STR) \
1528 (word_len == strlen (STR) \
1529 && memcmp (word_start, STR, strlen (STR)) == 0)
1530 #define PROP_SAVE(VAR, STR) \
1531 (VAR = NILP (VAR) ? font_intern_prop (STR, strlen (STR), 1) : VAR)
1533 if (PROP_MATCH ("Ultra-Light"))
1534 PROP_SAVE (weight, "ultra-light");
1535 else if (PROP_MATCH ("Light"))
1536 PROP_SAVE (weight, "light");
1537 else if (PROP_MATCH ("Book"))
1538 PROP_SAVE (weight, "book");
1539 else if (PROP_MATCH ("Medium"))
1540 PROP_SAVE (weight, "medium");
1541 else if (PROP_MATCH ("Semi-Bold"))
1542 PROP_SAVE (weight, "semi-bold");
1543 else if (PROP_MATCH ("Bold"))
1544 PROP_SAVE (weight, "bold");
1545 else if (PROP_MATCH ("Italic"))
1546 PROP_SAVE (slant, "italic");
1547 else if (PROP_MATCH ("Oblique"))
1548 PROP_SAVE (slant, "oblique");
1549 else if (PROP_MATCH ("Semi-Condensed"))
1550 PROP_SAVE (width, "semi-condensed");
1551 else if (PROP_MATCH ("Condensed"))
1552 PROP_SAVE (width, "condensed");
1553 /* An unknown word must be part of the font name. */
1554 else
1556 family_end = p;
1557 break;
1560 #undef PROP_MATCH
1561 #undef PROP_SAVE
1563 if (family_end)
1564 ASET (font, FONT_FAMILY_INDEX,
1565 font_intern_prop (name, family_end - name, 1));
1566 if (!NILP (size))
1567 ASET (font, FONT_SIZE_INDEX, size);
1568 if (!NILP (weight))
1569 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, weight);
1570 if (!NILP (slant))
1571 FONT_SET_STYLE (font, FONT_SLANT_INDEX, slant);
1572 if (!NILP (width))
1573 FONT_SET_STYLE (font, FONT_WIDTH_INDEX, width);
1576 return 0;
1579 /* Store fontconfig's font name of FONT (font-spec or font-entity) in
1580 NAME (NBYTES length), and return the name length. If
1581 FONT_SIZE_INDEX of FONT is 0, use PIXEL_SIZE instead. */
1584 font_unparse_fcname (Lisp_Object font, int pixel_size, char *name, int nbytes)
1586 Lisp_Object family, foundry;
1587 Lisp_Object val;
1588 int point_size;
1589 int i;
1590 char *p;
1591 char *lim;
1592 Lisp_Object styles[3];
1593 const char *style_names[3] = { "weight", "slant", "width" };
1595 family = AREF (font, FONT_FAMILY_INDEX);
1596 if (! NILP (family))
1598 if (SYMBOLP (family))
1599 family = SYMBOL_NAME (family);
1600 else
1601 family = Qnil;
1604 val = AREF (font, FONT_SIZE_INDEX);
1605 if (INTEGERP (val))
1607 if (XINT (val) != 0)
1608 pixel_size = XINT (val);
1609 point_size = -1;
1611 else
1613 eassert (FLOATP (val));
1614 pixel_size = -1;
1615 point_size = (int) XFLOAT_DATA (val);
1618 foundry = AREF (font, FONT_FOUNDRY_INDEX);
1619 if (! NILP (foundry))
1621 if (SYMBOLP (foundry))
1622 foundry = SYMBOL_NAME (foundry);
1623 else
1624 foundry = Qnil;
1627 for (i = 0; i < 3; i++)
1628 styles[i] = font_style_symbolic (font, FONT_WEIGHT_INDEX + i, 0);
1630 p = name;
1631 lim = name + nbytes;
1632 if (! NILP (family))
1634 int len = snprintf (p, lim - p, "%s", SSDATA (family));
1635 if (! (0 <= len && len < lim - p))
1636 return -1;
1637 p += len;
1639 if (point_size > 0)
1641 int len = snprintf (p, lim - p, &"-%d"[p == name], point_size);
1642 if (! (0 <= len && len < lim - p))
1643 return -1;
1644 p += len;
1646 else if (pixel_size > 0)
1648 int len = snprintf (p, lim - p, ":pixelsize=%d", pixel_size);
1649 if (! (0 <= len && len < lim - p))
1650 return -1;
1651 p += len;
1653 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
1655 int len = snprintf (p, lim - p, ":foundry=%s",
1656 SSDATA (SYMBOL_NAME (AREF (font,
1657 FONT_FOUNDRY_INDEX))));
1658 if (! (0 <= len && len < lim - p))
1659 return -1;
1660 p += len;
1662 for (i = 0; i < 3; i++)
1663 if (! NILP (styles[i]))
1665 int len = snprintf (p, lim - p, ":%s=%s", style_names[i],
1666 SSDATA (SYMBOL_NAME (styles[i])));
1667 if (! (0 <= len && len < lim - p))
1668 return -1;
1669 p += len;
1672 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1674 int len = snprintf (p, lim - p, ":dpi=%"pI"d",
1675 XINT (AREF (font, FONT_DPI_INDEX)));
1676 if (! (0 <= len && len < lim - p))
1677 return -1;
1678 p += len;
1681 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1683 int len = snprintf (p, lim - p, ":spacing=%"pI"d",
1684 XINT (AREF (font, FONT_SPACING_INDEX)));
1685 if (! (0 <= len && len < lim - p))
1686 return -1;
1687 p += len;
1690 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1692 int len = snprintf (p, lim - p,
1693 (XINT (AREF (font, FONT_AVGWIDTH_INDEX)) == 0
1694 ? ":scalable=true"
1695 : ":scalable=false"));
1696 if (! (0 <= len && len < lim - p))
1697 return -1;
1698 p += len;
1701 return (p - name);
1704 /* Parse NAME (null terminated) and store information in FONT
1705 (font-spec or font-entity). If NAME is successfully parsed, return
1706 0. Otherwise return -1. */
1708 static int
1709 font_parse_name (char *name, ptrdiff_t namelen, Lisp_Object font)
1711 if (name[0] == '-' || strchr (name, '*') || strchr (name, '?'))
1712 return font_parse_xlfd (name, namelen, font);
1713 return font_parse_fcname (name, namelen, font);
1717 /* Merge FAMILY and REGISTRY into FONT_SPEC. FAMILY may have the form
1718 "FAMILY-FOUNDRY". REGISTRY may not contain charset-encoding
1719 part. */
1721 void
1722 font_parse_family_registry (Lisp_Object family, Lisp_Object registry, Lisp_Object font_spec)
1724 int len;
1725 char *p0, *p1;
1727 if (! NILP (family)
1728 && NILP (AREF (font_spec, FONT_FAMILY_INDEX)))
1730 CHECK_STRING (family);
1731 len = SBYTES (family);
1732 p0 = SSDATA (family);
1733 p1 = strchr (p0, '-');
1734 if (p1)
1736 if ((*p0 != '*' && p1 - p0 > 0)
1737 && NILP (AREF (font_spec, FONT_FOUNDRY_INDEX)))
1738 Ffont_put (font_spec, QCfoundry, font_intern_prop (p0, p1 - p0, 1));
1739 p1++;
1740 len -= p1 - p0;
1741 Ffont_put (font_spec, QCfamily, font_intern_prop (p1, len, 1));
1743 else
1744 ASET (font_spec, FONT_FAMILY_INDEX, Fintern (family, Qnil));
1746 if (! NILP (registry))
1748 /* Convert "XXX" and "XXX*" to "XXX*-*". */
1749 CHECK_STRING (registry);
1750 len = SBYTES (registry);
1751 p0 = SSDATA (registry);
1752 p1 = strchr (p0, '-');
1753 if (! p1)
1755 if (SDATA (registry)[len - 1] == '*')
1756 registry = concat2 (registry, build_string ("-*"));
1757 else
1758 registry = concat2 (registry, build_string ("*-*"));
1760 registry = Fdowncase (registry);
1761 ASET (font_spec, FONT_REGISTRY_INDEX, Fintern (registry, Qnil));
1766 /* This part (through the next ^L) is still experimental and not
1767 tested much. We may drastically change codes. */
1769 /* OTF handler. */
1771 #if 0
1773 #define LGSTRING_HEADER_SIZE 6
1774 #define LGSTRING_GLYPH_SIZE 8
1776 static int
1777 check_gstring (Lisp_Object gstring)
1779 Lisp_Object val;
1780 ptrdiff_t i;
1781 int j;
1783 CHECK_VECTOR (gstring);
1784 val = AREF (gstring, 0);
1785 CHECK_VECTOR (val);
1786 if (ASIZE (val) < LGSTRING_HEADER_SIZE)
1787 goto err;
1788 CHECK_FONT_OBJECT (LGSTRING_FONT (gstring));
1789 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_LBEARING)))
1790 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_LBEARING));
1791 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_RBEARING)))
1792 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_RBEARING));
1793 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_WIDTH)))
1794 CHECK_NATNUM (LGSTRING_SLOT (gstring, LGSTRING_IX_WIDTH));
1795 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT)))
1796 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT));
1797 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT)))
1798 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT));
1800 for (i = 0; i < LGSTRING_GLYPH_LEN (gstring); i++)
1802 val = LGSTRING_GLYPH (gstring, i);
1803 CHECK_VECTOR (val);
1804 if (ASIZE (val) < LGSTRING_GLYPH_SIZE)
1805 goto err;
1806 if (NILP (AREF (val, LGLYPH_IX_CHAR)))
1807 break;
1808 CHECK_NATNUM (AREF (val, LGLYPH_IX_FROM));
1809 CHECK_NATNUM (AREF (val, LGLYPH_IX_TO));
1810 CHECK_CHARACTER (AREF (val, LGLYPH_IX_CHAR));
1811 if (!NILP (AREF (val, LGLYPH_IX_CODE)))
1812 CHECK_NATNUM (AREF (val, LGLYPH_IX_CODE));
1813 if (!NILP (AREF (val, LGLYPH_IX_WIDTH)))
1814 CHECK_NATNUM (AREF (val, LGLYPH_IX_WIDTH));
1815 if (!NILP (AREF (val, LGLYPH_IX_ADJUSTMENT)))
1817 val = AREF (val, LGLYPH_IX_ADJUSTMENT);
1818 CHECK_VECTOR (val);
1819 if (ASIZE (val) < 3)
1820 goto err;
1821 for (j = 0; j < 3; j++)
1822 CHECK_NUMBER (AREF (val, j));
1825 return i;
1826 err:
1827 error ("Invalid glyph-string format");
1828 return -1;
1831 static void
1832 check_otf_features (Lisp_Object otf_features)
1834 Lisp_Object val;
1836 CHECK_CONS (otf_features);
1837 CHECK_SYMBOL (XCAR (otf_features));
1838 otf_features = XCDR (otf_features);
1839 CHECK_CONS (otf_features);
1840 CHECK_SYMBOL (XCAR (otf_features));
1841 otf_features = XCDR (otf_features);
1842 for (val = Fcar (otf_features); CONSP (val); val = XCDR (val))
1844 CHECK_SYMBOL (XCAR (val));
1845 if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
1846 error ("Invalid OTF GSUB feature: %s",
1847 SDATA (SYMBOL_NAME (XCAR (val))));
1849 otf_features = XCDR (otf_features);
1850 for (val = Fcar (otf_features); CONSP (val); val = XCDR (val))
1852 CHECK_SYMBOL (XCAR (val));
1853 if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
1854 error ("Invalid OTF GPOS feature: %s",
1855 SDATA (SYMBOL_NAME (XCAR (val))));
1859 #ifdef HAVE_LIBOTF
1860 #include <otf.h>
1862 Lisp_Object otf_list;
1864 static Lisp_Object
1865 otf_tag_symbol (OTF_Tag tag)
1867 char name[5];
1869 OTF_tag_name (tag, name);
1870 return Fintern (make_unibyte_string (name, 4), Qnil);
1873 static OTF *
1874 otf_open (Lisp_Object file)
1876 Lisp_Object val = Fassoc (file, otf_list);
1877 OTF *otf;
1879 if (! NILP (val))
1880 otf = XSAVE_POINTER (XCDR (val), 0);
1881 else
1883 otf = STRINGP (file) ? OTF_open (SSDATA (file)) : NULL;
1884 val = make_save_ptr (otf);
1885 otf_list = Fcons (Fcons (file, val), otf_list);
1887 return otf;
1891 /* Return a list describing which scripts/languages FONT supports by
1892 which GSUB/GPOS features of OpenType tables. See the comment of
1893 (struct font_driver).otf_capability. */
1895 Lisp_Object
1896 font_otf_capability (struct font *font)
1898 OTF *otf;
1899 Lisp_Object capability = Fcons (Qnil, Qnil);
1900 int i;
1902 otf = otf_open (font->props[FONT_FILE_INDEX]);
1903 if (! otf)
1904 return Qnil;
1905 for (i = 0; i < 2; i++)
1907 OTF_GSUB_GPOS *gsub_gpos;
1908 Lisp_Object script_list = Qnil;
1909 int j;
1911 if (OTF_get_features (otf, i == 0) < 0)
1912 continue;
1913 gsub_gpos = i == 0 ? otf->gsub : otf->gpos;
1914 for (j = gsub_gpos->ScriptList.ScriptCount - 1; j >= 0; j--)
1916 OTF_Script *script = gsub_gpos->ScriptList.Script + j;
1917 Lisp_Object langsys_list = Qnil;
1918 Lisp_Object script_tag = otf_tag_symbol (script->ScriptTag);
1919 int k;
1921 for (k = script->LangSysCount; k >= 0; k--)
1923 OTF_LangSys *langsys;
1924 Lisp_Object feature_list = Qnil;
1925 Lisp_Object langsys_tag;
1926 int l;
1928 if (k == script->LangSysCount)
1930 langsys = &script->DefaultLangSys;
1931 langsys_tag = Qnil;
1933 else
1935 langsys = script->LangSys + k;
1936 langsys_tag
1937 = otf_tag_symbol (script->LangSysRecord[k].LangSysTag);
1939 for (l = langsys->FeatureCount - 1; l >= 0; l--)
1941 OTF_Feature *feature
1942 = gsub_gpos->FeatureList.Feature + langsys->FeatureIndex[l];
1943 Lisp_Object feature_tag
1944 = otf_tag_symbol (feature->FeatureTag);
1946 feature_list = Fcons (feature_tag, feature_list);
1948 langsys_list = Fcons (Fcons (langsys_tag, feature_list),
1949 langsys_list);
1951 script_list = Fcons (Fcons (script_tag, langsys_list),
1952 script_list);
1955 if (i == 0)
1956 XSETCAR (capability, script_list);
1957 else
1958 XSETCDR (capability, script_list);
1961 return capability;
1964 /* Parse OTF features in SPEC and write a proper features spec string
1965 in FEATURES for the call of OTF_drive_gsub/gpos (of libotf). It is
1966 assured that the sufficient memory has already allocated for
1967 FEATURES. */
1969 static void
1970 generate_otf_features (Lisp_Object spec, char *features)
1972 Lisp_Object val;
1973 char *p;
1974 bool asterisk;
1976 p = features;
1977 *p = '\0';
1978 for (asterisk = 0; CONSP (spec); spec = XCDR (spec))
1980 val = XCAR (spec);
1981 CHECK_SYMBOL (val);
1982 if (p > features)
1983 *p++ = ',';
1984 if (SREF (SYMBOL_NAME (val), 0) == '*')
1986 asterisk = 1;
1987 *p++ = '*';
1989 else if (! asterisk)
1991 val = SYMBOL_NAME (val);
1992 p += esprintf (p, "%s", SDATA (val));
1994 else
1996 val = SYMBOL_NAME (val);
1997 p += esprintf (p, "~%s", SDATA (val));
2000 if (CONSP (spec))
2001 error ("OTF spec too long");
2004 Lisp_Object
2005 font_otf_DeviceTable (OTF_DeviceTable *device_table)
2007 int len = device_table->StartSize - device_table->EndSize + 1;
2009 return Fcons (make_number (len),
2010 make_unibyte_string (device_table->DeltaValue, len));
2013 Lisp_Object
2014 font_otf_ValueRecord (int value_format, OTF_ValueRecord *value_record)
2016 Lisp_Object val = Fmake_vector (make_number (8), Qnil);
2018 if (value_format & OTF_XPlacement)
2019 ASET (val, 0, make_number (value_record->XPlacement));
2020 if (value_format & OTF_YPlacement)
2021 ASET (val, 1, make_number (value_record->YPlacement));
2022 if (value_format & OTF_XAdvance)
2023 ASET (val, 2, make_number (value_record->XAdvance));
2024 if (value_format & OTF_YAdvance)
2025 ASET (val, 3, make_number (value_record->YAdvance));
2026 if (value_format & OTF_XPlaDevice)
2027 ASET (val, 4, font_otf_DeviceTable (&value_record->XPlaDevice));
2028 if (value_format & OTF_YPlaDevice)
2029 ASET (val, 4, font_otf_DeviceTable (&value_record->YPlaDevice));
2030 if (value_format & OTF_XAdvDevice)
2031 ASET (val, 4, font_otf_DeviceTable (&value_record->XAdvDevice));
2032 if (value_format & OTF_YAdvDevice)
2033 ASET (val, 4, font_otf_DeviceTable (&value_record->YAdvDevice));
2034 return val;
2037 Lisp_Object
2038 font_otf_Anchor (OTF_Anchor *anchor)
2040 Lisp_Object val;
2042 val = Fmake_vector (make_number (anchor->AnchorFormat + 1), Qnil);
2043 ASET (val, 0, make_number (anchor->XCoordinate));
2044 ASET (val, 1, make_number (anchor->YCoordinate));
2045 if (anchor->AnchorFormat == 2)
2046 ASET (val, 2, make_number (anchor->f.f1.AnchorPoint));
2047 else
2049 ASET (val, 3, font_otf_DeviceTable (&anchor->f.f2.XDeviceTable));
2050 ASET (val, 4, font_otf_DeviceTable (&anchor->f.f2.YDeviceTable));
2052 return val;
2054 #endif /* HAVE_LIBOTF */
2055 #endif /* 0 */
2058 /* Font sorting. */
2060 static double
2061 font_rescale_ratio (Lisp_Object font_entity)
2063 Lisp_Object tail, elt;
2064 Lisp_Object name = Qnil;
2066 for (tail = Vface_font_rescale_alist; CONSP (tail); tail = XCDR (tail))
2068 elt = XCAR (tail);
2069 if (FLOATP (XCDR (elt)))
2071 if (STRINGP (XCAR (elt)))
2073 if (NILP (name))
2074 name = Ffont_xlfd_name (font_entity, Qnil);
2075 if (fast_string_match_ignore_case (XCAR (elt), name) >= 0)
2076 return XFLOAT_DATA (XCDR (elt));
2078 else if (FONT_SPEC_P (XCAR (elt)))
2080 if (font_match_p (XCAR (elt), font_entity))
2081 return XFLOAT_DATA (XCDR (elt));
2085 return 1.0;
2088 /* We sort fonts by scoring each of them against a specified
2089 font-spec. The score value is 32 bit (`unsigned'), and the smaller
2090 the value is, the closer the font is to the font-spec.
2092 The lowest 2 bits of the score are used for driver type. The font
2093 available by the most preferred font driver is 0.
2095 The 4 7-bit fields in the higher 28 bits are used for numeric properties
2096 WEIGHT, SLANT, WIDTH, and SIZE. */
2098 /* How many bits to shift to store the difference value of each font
2099 property in a score. Note that floats for FONT_TYPE_INDEX and
2100 FONT_REGISTRY_INDEX are not used. */
2101 static int sort_shift_bits[FONT_SIZE_INDEX + 1];
2103 /* Score font-entity ENTITY against properties of font-spec SPEC_PROP.
2104 The return value indicates how different ENTITY is compared with
2105 SPEC_PROP. */
2107 static unsigned
2108 font_score (Lisp_Object entity, Lisp_Object *spec_prop)
2110 unsigned score = 0;
2111 int i;
2113 /* Score three style numeric fields. Maximum difference is 127. */
2114 for (i = FONT_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX; i++)
2115 if (! NILP (spec_prop[i]) && ! EQ (AREF (entity, i), spec_prop[i]))
2117 EMACS_INT diff = ((XINT (AREF (entity, i)) >> 8)
2118 - (XINT (spec_prop[i]) >> 8));
2119 score |= min (eabs (diff), 127) << sort_shift_bits[i];
2122 /* Score the size. Maximum difference is 127. */
2123 i = FONT_SIZE_INDEX;
2124 if (! NILP (spec_prop[FONT_SIZE_INDEX])
2125 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
2127 /* We use the higher 6-bit for the actual size difference. The
2128 lowest bit is set if the DPI is different. */
2129 EMACS_INT diff;
2130 EMACS_INT pixel_size = XINT (spec_prop[FONT_SIZE_INDEX]);
2132 if (CONSP (Vface_font_rescale_alist))
2133 pixel_size *= font_rescale_ratio (entity);
2134 diff = eabs (pixel_size - XINT (AREF (entity, FONT_SIZE_INDEX))) << 1;
2135 if (! NILP (spec_prop[FONT_DPI_INDEX])
2136 && ! EQ (spec_prop[FONT_DPI_INDEX], AREF (entity, FONT_DPI_INDEX)))
2137 diff |= 1;
2138 if (! NILP (spec_prop[FONT_AVGWIDTH_INDEX])
2139 && ! EQ (spec_prop[FONT_AVGWIDTH_INDEX], AREF (entity, FONT_AVGWIDTH_INDEX)))
2140 diff |= 1;
2141 score |= min (diff, 127) << sort_shift_bits[FONT_SIZE_INDEX];
2144 return score;
2148 /* Concatenate all elements of LIST into one vector. LIST is a list
2149 of font-entity vectors. */
2151 static Lisp_Object
2152 font_vconcat_entity_vectors (Lisp_Object list)
2154 int nargs = XINT (Flength (list));
2155 Lisp_Object *args = alloca (word_size * nargs);
2156 int i;
2158 for (i = 0; i < nargs; i++, list = XCDR (list))
2159 args[i] = XCAR (list);
2160 return Fvconcat (nargs, args);
2164 /* The structure for elements being sorted by qsort. */
2165 struct font_sort_data
2167 unsigned score;
2168 int font_driver_preference;
2169 Lisp_Object entity;
2173 /* The comparison function for qsort. */
2175 static int
2176 font_compare (const void *d1, const void *d2)
2178 const struct font_sort_data *data1 = d1;
2179 const struct font_sort_data *data2 = d2;
2181 if (data1->score < data2->score)
2182 return -1;
2183 else if (data1->score > data2->score)
2184 return 1;
2185 return (data1->font_driver_preference - data2->font_driver_preference);
2189 /* Sort each font-entity vector in LIST by closeness to font-spec PREFER.
2190 If PREFER specifies a point-size, calculate the corresponding
2191 pixel-size from QCdpi property of PREFER or from the Y-resolution
2192 of FRAME before sorting.
2194 If BEST-ONLY is nonzero, return the best matching entity (that
2195 supports the character BEST-ONLY if BEST-ONLY is positive, or any
2196 if BEST-ONLY is negative). Otherwise, return the sorted result as
2197 a single vector of font-entities.
2199 This function does no optimization for the case that the total
2200 number of elements is 1. The caller should avoid calling this in
2201 such a case. */
2203 static Lisp_Object
2204 font_sort_entities (Lisp_Object list, Lisp_Object prefer,
2205 struct frame *f, int best_only)
2207 Lisp_Object prefer_prop[FONT_SPEC_MAX];
2208 int len, maxlen, i;
2209 struct font_sort_data *data;
2210 unsigned best_score;
2211 Lisp_Object best_entity;
2212 Lisp_Object tail, vec IF_LINT (= Qnil);
2213 USE_SAFE_ALLOCA;
2215 for (i = FONT_WEIGHT_INDEX; i <= FONT_AVGWIDTH_INDEX; i++)
2216 prefer_prop[i] = AREF (prefer, i);
2217 if (FLOATP (prefer_prop[FONT_SIZE_INDEX]))
2218 prefer_prop[FONT_SIZE_INDEX]
2219 = make_number (font_pixel_size (f, prefer));
2221 if (NILP (XCDR (list)))
2223 /* What we have to take care of is this single vector. */
2224 vec = XCAR (list);
2225 maxlen = ASIZE (vec);
2227 else if (best_only)
2229 /* We don't have to perform sort, so there's no need of creating
2230 a single vector. But, we must find the length of the longest
2231 vector. */
2232 maxlen = 0;
2233 for (tail = list; CONSP (tail); tail = XCDR (tail))
2234 if (maxlen < ASIZE (XCAR (tail)))
2235 maxlen = ASIZE (XCAR (tail));
2237 else
2239 /* We have to create a single vector to sort it. */
2240 vec = font_vconcat_entity_vectors (list);
2241 maxlen = ASIZE (vec);
2244 data = SAFE_ALLOCA (maxlen * sizeof *data);
2245 best_score = 0xFFFFFFFF;
2246 best_entity = Qnil;
2248 for (tail = list; CONSP (tail); tail = XCDR (tail))
2250 int font_driver_preference = 0;
2251 Lisp_Object current_font_driver;
2253 if (best_only)
2254 vec = XCAR (tail);
2255 len = ASIZE (vec);
2257 /* We are sure that the length of VEC > 0. */
2258 current_font_driver = AREF (AREF (vec, 0), FONT_TYPE_INDEX);
2259 /* Score the elements. */
2260 for (i = 0; i < len; i++)
2262 data[i].entity = AREF (vec, i);
2263 data[i].score
2264 = ((best_only <= 0 || font_has_char (f, data[i].entity, best_only)
2265 > 0)
2266 ? font_score (data[i].entity, prefer_prop)
2267 : 0xFFFFFFFF);
2268 if (best_only && best_score > data[i].score)
2270 best_score = data[i].score;
2271 best_entity = data[i].entity;
2272 if (best_score == 0)
2273 break;
2275 if (! EQ (current_font_driver, AREF (AREF (vec, i), FONT_TYPE_INDEX)))
2277 current_font_driver = AREF (AREF (vec, i), FONT_TYPE_INDEX);
2278 font_driver_preference++;
2280 data[i].font_driver_preference = font_driver_preference;
2283 /* Sort if necessary. */
2284 if (! best_only)
2286 qsort (data, len, sizeof *data, font_compare);
2287 for (i = 0; i < len; i++)
2288 ASET (vec, i, data[i].entity);
2289 break;
2291 else
2292 vec = best_entity;
2295 SAFE_FREE ();
2297 FONT_ADD_LOG ("sort-by", prefer, vec);
2298 return vec;
2302 /* API of Font Service Layer. */
2304 /* Reflect ORDER (see the variable font_sort_order in xfaces.c) to
2305 sort_shift_bits. Finternal_set_font_selection_order calls this
2306 function with font_sort_order after setting up it. */
2308 void
2309 font_update_sort_order (int *order)
2311 int i, shift_bits;
2313 for (i = 0, shift_bits = 23; i < 4; i++, shift_bits -= 7)
2315 int xlfd_idx = order[i];
2317 if (xlfd_idx == XLFD_WEIGHT_INDEX)
2318 sort_shift_bits[FONT_WEIGHT_INDEX] = shift_bits;
2319 else if (xlfd_idx == XLFD_SLANT_INDEX)
2320 sort_shift_bits[FONT_SLANT_INDEX] = shift_bits;
2321 else if (xlfd_idx == XLFD_SWIDTH_INDEX)
2322 sort_shift_bits[FONT_WIDTH_INDEX] = shift_bits;
2323 else
2324 sort_shift_bits[FONT_SIZE_INDEX] = shift_bits;
2328 static bool
2329 font_check_otf_features (Lisp_Object script, Lisp_Object langsys,
2330 Lisp_Object features, Lisp_Object table)
2332 Lisp_Object val;
2333 bool negative;
2335 table = assq_no_quit (script, table);
2336 if (NILP (table))
2337 return 0;
2338 table = XCDR (table);
2339 if (! NILP (langsys))
2341 table = assq_no_quit (langsys, table);
2342 if (NILP (table))
2343 return 0;
2345 else
2347 val = assq_no_quit (Qnil, table);
2348 if (NILP (val))
2349 table = XCAR (table);
2350 else
2351 table = val;
2353 table = XCDR (table);
2354 for (negative = 0; CONSP (features); features = XCDR (features))
2356 if (NILP (XCAR (features)))
2358 negative = 1;
2359 continue;
2361 if (NILP (Fmemq (XCAR (features), table)) != negative)
2362 return 0;
2364 return 1;
2367 /* Check if OTF_CAPABILITY satisfies SPEC (otf-spec). */
2369 static bool
2370 font_check_otf (Lisp_Object spec, Lisp_Object otf_capability)
2372 Lisp_Object script, langsys = Qnil, gsub = Qnil, gpos = Qnil;
2374 script = XCAR (spec);
2375 spec = XCDR (spec);
2376 if (! NILP (spec))
2378 langsys = XCAR (spec);
2379 spec = XCDR (spec);
2380 if (! NILP (spec))
2382 gsub = XCAR (spec);
2383 spec = XCDR (spec);
2384 if (! NILP (spec))
2385 gpos = XCAR (spec);
2389 if (! NILP (gsub) && ! font_check_otf_features (script, langsys, gsub,
2390 XCAR (otf_capability)))
2391 return 0;
2392 if (! NILP (gpos) && ! font_check_otf_features (script, langsys, gpos,
2393 XCDR (otf_capability)))
2394 return 0;
2395 return 1;
2400 /* Check if FONT (font-entity or font-object) matches with the font
2401 specification SPEC. */
2403 bool
2404 font_match_p (Lisp_Object spec, Lisp_Object font)
2406 Lisp_Object prop[FONT_SPEC_MAX], *props;
2407 Lisp_Object extra, font_extra;
2408 int i;
2410 for (i = FONT_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX; i++)
2411 if (! NILP (AREF (spec, i))
2412 && ! NILP (AREF (font, i))
2413 && ! EQ (AREF (spec, i), AREF (font, i)))
2414 return 0;
2415 props = XFONT_SPEC (spec)->props;
2416 if (FLOATP (props[FONT_SIZE_INDEX]))
2418 for (i = FONT_FOUNDRY_INDEX; i < FONT_SIZE_INDEX; i++)
2419 prop[i] = AREF (spec, i);
2420 prop[FONT_SIZE_INDEX]
2421 = make_number (font_pixel_size (XFRAME (selected_frame), spec));
2422 props = prop;
2425 if (font_score (font, props) > 0)
2426 return 0;
2427 extra = AREF (spec, FONT_EXTRA_INDEX);
2428 font_extra = AREF (font, FONT_EXTRA_INDEX);
2429 for (; CONSP (extra); extra = XCDR (extra))
2431 Lisp_Object key = XCAR (XCAR (extra));
2432 Lisp_Object val = XCDR (XCAR (extra)), val2;
2434 if (EQ (key, QClang))
2436 val2 = assq_no_quit (key, font_extra);
2437 if (NILP (val2))
2438 return 0;
2439 val2 = XCDR (val2);
2440 if (CONSP (val))
2442 if (! CONSP (val2))
2443 return 0;
2444 while (CONSP (val))
2445 if (NILP (Fmemq (val, val2)))
2446 return 0;
2448 else
2449 if (CONSP (val2)
2450 ? NILP (Fmemq (val, XCDR (val2)))
2451 : ! EQ (val, val2))
2452 return 0;
2454 else if (EQ (key, QCscript))
2456 val2 = assq_no_quit (val, Vscript_representative_chars);
2457 if (CONSP (val2))
2459 val2 = XCDR (val2);
2460 if (CONSP (val2))
2462 /* All characters in the list must be supported. */
2463 for (; CONSP (val2); val2 = XCDR (val2))
2465 if (! CHARACTERP (XCAR (val2)))
2466 continue;
2467 if (font_encode_char (font, XFASTINT (XCAR (val2)))
2468 == FONT_INVALID_CODE)
2469 return 0;
2472 else if (VECTORP (val2))
2474 /* At most one character in the vector must be supported. */
2475 for (i = 0; i < ASIZE (val2); i++)
2477 if (! CHARACTERP (AREF (val2, i)))
2478 continue;
2479 if (font_encode_char (font, XFASTINT (AREF (val2, i)))
2480 != FONT_INVALID_CODE)
2481 break;
2483 if (i == ASIZE (val2))
2484 return 0;
2488 else if (EQ (key, QCotf))
2490 struct font *fontp;
2492 if (! FONT_OBJECT_P (font))
2493 return 0;
2494 fontp = XFONT_OBJECT (font);
2495 if (! fontp->driver->otf_capability)
2496 return 0;
2497 val2 = fontp->driver->otf_capability (fontp);
2498 if (NILP (val2) || ! font_check_otf (val, val2))
2499 return 0;
2503 return 1;
2507 /* Font cache
2509 Each font backend has the callback function get_cache, and it
2510 returns a cons cell of which cdr part can be freely used for
2511 caching fonts. The cons cell may be shared by multiple frames
2512 and/or multiple font drivers. So, we arrange the cdr part as this:
2514 ((DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) ...)
2516 where DRIVER-TYPE is a symbol such as `x', `xft', etc., NUM-FRAMES
2517 is a number frames sharing this cache, and FONT-CACHE-DATA is a
2518 cons (FONT-SPEC . [FONT-ENTITY ...]). */
2520 static void font_prepare_cache (struct frame *, struct font_driver *);
2521 static void font_finish_cache (struct frame *, struct font_driver *);
2522 static Lisp_Object font_get_cache (struct frame *, struct font_driver *);
2523 static void font_clear_cache (struct frame *, Lisp_Object,
2524 struct font_driver *);
2526 static void
2527 font_prepare_cache (struct frame *f, struct font_driver *driver)
2529 Lisp_Object cache, val;
2531 cache = driver->get_cache (f);
2532 val = XCDR (cache);
2533 while (CONSP (val) && ! EQ (XCAR (XCAR (val)), driver->type))
2534 val = XCDR (val);
2535 if (NILP (val))
2537 val = list2 (driver->type, make_number (1));
2538 XSETCDR (cache, Fcons (val, XCDR (cache)));
2540 else
2542 val = XCDR (XCAR (val));
2543 XSETCAR (val, make_number (XINT (XCAR (val)) + 1));
2548 static void
2549 font_finish_cache (struct frame *f, struct font_driver *driver)
2551 Lisp_Object cache, val, tmp;
2554 cache = driver->get_cache (f);
2555 val = XCDR (cache);
2556 while (CONSP (val) && ! EQ (XCAR (XCAR (val)), driver->type))
2557 cache = val, val = XCDR (val);
2558 eassert (! NILP (val));
2559 tmp = XCDR (XCAR (val));
2560 XSETCAR (tmp, make_number (XINT (XCAR (tmp)) - 1));
2561 if (XINT (XCAR (tmp)) == 0)
2563 font_clear_cache (f, XCAR (val), driver);
2564 XSETCDR (cache, XCDR (val));
2569 static Lisp_Object
2570 font_get_cache (struct frame *f, struct font_driver *driver)
2572 Lisp_Object val = driver->get_cache (f);
2573 Lisp_Object type = driver->type;
2575 eassert (CONSP (val));
2576 for (val = XCDR (val); ! EQ (XCAR (XCAR (val)), type); val = XCDR (val));
2577 eassert (CONSP (val));
2578 /* VAL = ((DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) ...) */
2579 val = XCDR (XCAR (val));
2580 return val;
2584 static void
2585 font_clear_cache (struct frame *f, Lisp_Object cache, struct font_driver *driver)
2587 Lisp_Object tail, elt;
2588 Lisp_Object entity;
2589 ptrdiff_t i;
2591 /* CACHE = (DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) */
2592 for (tail = XCDR (XCDR (cache)); CONSP (tail); tail = XCDR (tail))
2594 elt = XCAR (tail);
2595 /* elt should have the form (FONT-SPEC . [FONT-ENTITY ...]) */
2596 if (CONSP (elt) && FONT_SPEC_P (XCAR (elt)))
2598 elt = XCDR (elt);
2599 eassert (VECTORP (elt));
2600 for (i = 0; i < ASIZE (elt); i++)
2602 entity = AREF (elt, i);
2604 if (FONT_ENTITY_P (entity)
2605 && EQ (driver->type, AREF (entity, FONT_TYPE_INDEX)))
2607 Lisp_Object objlist = AREF (entity, FONT_OBJLIST_INDEX);
2609 for (; CONSP (objlist); objlist = XCDR (objlist))
2611 Lisp_Object val = XCAR (objlist);
2612 struct font *font = XFONT_OBJECT (val);
2614 if (! NILP (AREF (val, FONT_TYPE_INDEX)))
2616 eassert (font && driver == font->driver);
2617 driver->close (font);
2620 if (driver->free_entity)
2621 driver->free_entity (entity);
2626 XSETCDR (cache, Qnil);
2630 static Lisp_Object scratch_font_spec, scratch_font_prefer;
2632 /* Check each font-entity in VEC, and return a list of font-entities
2633 that satisfy these conditions:
2634 (1) matches with SPEC and SIZE if SPEC is not nil, and
2635 (2) doesn't match with any regexps in Vface_ignored_fonts (if non-nil).
2638 static Lisp_Object
2639 font_delete_unmatched (Lisp_Object vec, Lisp_Object spec, int size)
2641 Lisp_Object entity, val;
2642 enum font_property_index prop;
2643 int i;
2645 for (val = Qnil, i = ASIZE (vec) - 1; i >= 0; i--)
2647 entity = AREF (vec, i);
2648 if (! NILP (Vface_ignored_fonts))
2650 char name[256];
2651 ptrdiff_t namelen;
2652 Lisp_Object tail, regexp;
2654 namelen = font_unparse_xlfd (entity, 0, name, 256);
2655 if (namelen >= 0)
2657 for (tail = Vface_ignored_fonts; CONSP (tail); tail = XCDR (tail))
2659 regexp = XCAR (tail);
2660 if (STRINGP (regexp)
2661 && fast_c_string_match_ignore_case (regexp, name,
2662 namelen) >= 0)
2663 break;
2665 if (CONSP (tail))
2666 continue;
2669 if (NILP (spec))
2671 val = Fcons (entity, val);
2672 continue;
2674 for (prop = FONT_WEIGHT_INDEX; prop < FONT_SIZE_INDEX; prop++)
2675 if (INTEGERP (AREF (spec, prop))
2676 && ((XINT (AREF (spec, prop)) >> 8)
2677 != (XINT (AREF (entity, prop)) >> 8)))
2678 prop = FONT_SPEC_MAX;
2679 if (prop < FONT_SPEC_MAX
2680 && size
2681 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
2683 int diff = XINT (AREF (entity, FONT_SIZE_INDEX)) - size;
2685 if (eabs (diff) > FONT_PIXEL_SIZE_QUANTUM)
2686 prop = FONT_SPEC_MAX;
2688 if (prop < FONT_SPEC_MAX
2689 && INTEGERP (AREF (spec, FONT_DPI_INDEX))
2690 && INTEGERP (AREF (entity, FONT_DPI_INDEX))
2691 && XINT (AREF (entity, FONT_DPI_INDEX)) != 0
2692 && ! EQ (AREF (spec, FONT_DPI_INDEX), AREF (entity, FONT_DPI_INDEX)))
2693 prop = FONT_SPEC_MAX;
2694 if (prop < FONT_SPEC_MAX
2695 && INTEGERP (AREF (spec, FONT_AVGWIDTH_INDEX))
2696 && INTEGERP (AREF (entity, FONT_AVGWIDTH_INDEX))
2697 && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) != 0
2698 && ! EQ (AREF (spec, FONT_AVGWIDTH_INDEX),
2699 AREF (entity, FONT_AVGWIDTH_INDEX)))
2700 prop = FONT_SPEC_MAX;
2701 if (prop < FONT_SPEC_MAX)
2702 val = Fcons (entity, val);
2704 return (Fvconcat (1, &val));
2708 /* Return a list of vectors of font-entities matching with SPEC on
2709 FRAME. Each elements in the list is a vector of entities from the
2710 same font-driver. */
2712 Lisp_Object
2713 font_list_entities (struct frame *f, Lisp_Object spec)
2715 struct font_driver_list *driver_list = f->font_driver_list;
2716 Lisp_Object ftype, val;
2717 Lisp_Object list = Qnil;
2718 int size;
2719 bool need_filtering = 0;
2720 int i;
2722 eassert (FONT_SPEC_P (spec));
2724 if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
2725 size = XINT (AREF (spec, FONT_SIZE_INDEX));
2726 else if (FLOATP (AREF (spec, FONT_SIZE_INDEX)))
2727 size = font_pixel_size (f, spec);
2728 else
2729 size = 0;
2731 ftype = AREF (spec, FONT_TYPE_INDEX);
2732 for (i = FONT_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX; i++)
2733 ASET (scratch_font_spec, i, AREF (spec, i));
2734 for (i = FONT_WEIGHT_INDEX; i < FONT_EXTRA_INDEX; i++)
2735 if (i != FONT_SPACING_INDEX)
2737 ASET (scratch_font_spec, i, Qnil);
2738 if (! NILP (AREF (spec, i)))
2739 need_filtering = 1;
2741 ASET (scratch_font_spec, FONT_SPACING_INDEX, AREF (spec, FONT_SPACING_INDEX));
2742 ASET (scratch_font_spec, FONT_EXTRA_INDEX, AREF (spec, FONT_EXTRA_INDEX));
2744 for (; driver_list; driver_list = driver_list->next)
2745 if (driver_list->on
2746 && (NILP (ftype) || EQ (driver_list->driver->type, ftype)))
2748 Lisp_Object cache = font_get_cache (f, driver_list->driver);
2750 ASET (scratch_font_spec, FONT_TYPE_INDEX, driver_list->driver->type);
2751 val = assoc_no_quit (scratch_font_spec, XCDR (cache));
2752 if (CONSP (val))
2753 val = XCDR (val);
2754 else
2756 Lisp_Object copy;
2758 val = driver_list->driver->list (f, scratch_font_spec);
2759 if (NILP (val))
2760 val = zero_vector;
2761 else
2762 val = Fvconcat (1, &val);
2763 copy = copy_font_spec (scratch_font_spec);
2764 ASET (copy, FONT_TYPE_INDEX, driver_list->driver->type);
2765 XSETCDR (cache, Fcons (Fcons (copy, val), XCDR (cache)));
2767 if (ASIZE (val) > 0
2768 && (need_filtering
2769 || ! NILP (Vface_ignored_fonts)))
2770 val = font_delete_unmatched (val, need_filtering ? spec : Qnil, size);
2771 if (ASIZE (val) > 0)
2772 list = Fcons (val, list);
2775 list = Fnreverse (list);
2776 FONT_ADD_LOG ("list", spec, list);
2777 return list;
2781 /* Return a font entity matching with SPEC on FRAME. ATTRS, if non
2782 nil, is an array of face's attributes, which specifies preferred
2783 font-related attributes. */
2785 static Lisp_Object
2786 font_matching_entity (struct frame *f, Lisp_Object *attrs, Lisp_Object spec)
2788 struct font_driver_list *driver_list = f->font_driver_list;
2789 Lisp_Object ftype, size, entity;
2790 Lisp_Object work = copy_font_spec (spec);
2792 ftype = AREF (spec, FONT_TYPE_INDEX);
2793 size = AREF (spec, FONT_SIZE_INDEX);
2795 if (FLOATP (size))
2796 ASET (work, FONT_SIZE_INDEX, make_number (font_pixel_size (f, spec)));
2797 FONT_SET_STYLE (work, FONT_WEIGHT_INDEX, attrs[LFACE_WEIGHT_INDEX]);
2798 FONT_SET_STYLE (work, FONT_SLANT_INDEX, attrs[LFACE_SLANT_INDEX]);
2799 FONT_SET_STYLE (work, FONT_WIDTH_INDEX, attrs[LFACE_SWIDTH_INDEX]);
2801 entity = Qnil;
2802 for (; driver_list; driver_list = driver_list->next)
2803 if (driver_list->on
2804 && (NILP (ftype) || EQ (driver_list->driver->type, ftype)))
2806 Lisp_Object cache = font_get_cache (f, driver_list->driver);
2807 Lisp_Object copy;
2809 ASET (work, FONT_TYPE_INDEX, driver_list->driver->type);
2810 entity = assoc_no_quit (work, XCDR (cache));
2811 if (CONSP (entity))
2812 entity = XCDR (entity);
2813 else
2815 entity = driver_list->driver->match (f, work);
2816 copy = copy_font_spec (work);
2817 ASET (copy, FONT_TYPE_INDEX, driver_list->driver->type);
2818 XSETCDR (cache, Fcons (Fcons (copy, entity), XCDR (cache)));
2820 if (! NILP (entity))
2821 break;
2823 FONT_ADD_LOG ("match", work, entity);
2824 return entity;
2828 /* Open a font of ENTITY and PIXEL_SIZE on frame F, and return the
2829 opened font object. */
2831 static Lisp_Object
2832 font_open_entity (struct frame *f, Lisp_Object entity, int pixel_size)
2834 struct font_driver_list *driver_list;
2835 Lisp_Object objlist, size, val, font_object;
2836 struct font *font;
2837 int min_width, height, psize;
2839 eassert (FONT_ENTITY_P (entity));
2840 size = AREF (entity, FONT_SIZE_INDEX);
2841 if (XINT (size) != 0)
2842 pixel_size = XINT (size);
2844 val = AREF (entity, FONT_TYPE_INDEX);
2845 for (driver_list = f->font_driver_list;
2846 driver_list && ! EQ (driver_list->driver->type, val);
2847 driver_list = driver_list->next);
2848 if (! driver_list)
2849 return Qnil;
2851 for (objlist = AREF (entity, FONT_OBJLIST_INDEX); CONSP (objlist);
2852 objlist = XCDR (objlist))
2854 Lisp_Object fn = XCAR (objlist);
2855 if (! NILP (AREF (fn, FONT_TYPE_INDEX))
2856 && XFONT_OBJECT (fn)->pixel_size == pixel_size)
2858 if (driver_list->driver->cached_font_ok == NULL
2859 || driver_list->driver->cached_font_ok (f, fn, entity))
2860 return fn;
2864 /* We always open a font of manageable size; i.e non-zero average
2865 width and height. */
2866 for (psize = pixel_size; ; psize++)
2868 font_object = driver_list->driver->open (f, entity, psize);
2869 if (NILP (font_object))
2870 return Qnil;
2871 font = XFONT_OBJECT (font_object);
2872 if (font->average_width > 0 && font->height > 0)
2873 break;
2875 ASET (font_object, FONT_SIZE_INDEX, make_number (pixel_size));
2876 FONT_ADD_LOG ("open", entity, font_object);
2877 ASET (entity, FONT_OBJLIST_INDEX,
2878 Fcons (font_object, AREF (entity, FONT_OBJLIST_INDEX)));
2880 font = XFONT_OBJECT (font_object);
2881 min_width = (font->min_width ? font->min_width
2882 : font->average_width ? font->average_width
2883 : font->space_width ? font->space_width
2884 : 1);
2885 height = (font->height ? font->height : 1);
2886 #ifdef HAVE_WINDOW_SYSTEM
2887 FRAME_DISPLAY_INFO (f)->n_fonts++;
2888 if (FRAME_DISPLAY_INFO (f)->n_fonts == 1)
2890 FRAME_SMALLEST_CHAR_WIDTH (f) = min_width;
2891 FRAME_SMALLEST_FONT_HEIGHT (f) = height;
2892 f->fonts_changed = 1;
2894 else
2896 if (FRAME_SMALLEST_CHAR_WIDTH (f) > min_width)
2897 FRAME_SMALLEST_CHAR_WIDTH (f) = min_width, f->fonts_changed = 1;
2898 if (FRAME_SMALLEST_FONT_HEIGHT (f) > height)
2899 FRAME_SMALLEST_FONT_HEIGHT (f) = height, f->fonts_changed = 1;
2901 #endif
2903 return font_object;
2907 /* Close FONT_OBJECT that is opened on frame F. */
2909 static void
2910 font_close_object (struct frame *f, Lisp_Object font_object)
2912 struct font *font = XFONT_OBJECT (font_object);
2914 if (NILP (AREF (font_object, FONT_TYPE_INDEX)))
2915 /* Already closed. */
2916 return;
2917 FONT_ADD_LOG ("close", font_object, Qnil);
2918 font->driver->close (font);
2919 #ifdef HAVE_WINDOW_SYSTEM
2920 eassert (FRAME_DISPLAY_INFO (f)->n_fonts);
2921 FRAME_DISPLAY_INFO (f)->n_fonts--;
2922 #endif
2926 /* Return 1 if FONT on F has a glyph for character C, 0 if not, -1 if
2927 FONT is a font-entity and it must be opened to check. */
2930 font_has_char (struct frame *f, Lisp_Object font, int c)
2932 struct font *fontp;
2934 if (FONT_ENTITY_P (font))
2936 Lisp_Object type = AREF (font, FONT_TYPE_INDEX);
2937 struct font_driver_list *driver_list;
2939 for (driver_list = f->font_driver_list;
2940 driver_list && ! EQ (driver_list->driver->type, type);
2941 driver_list = driver_list->next);
2942 if (! driver_list)
2943 return 0;
2944 if (! driver_list->driver->has_char)
2945 return -1;
2946 return driver_list->driver->has_char (font, c);
2949 eassert (FONT_OBJECT_P (font));
2950 fontp = XFONT_OBJECT (font);
2951 if (fontp->driver->has_char)
2953 int result = fontp->driver->has_char (font, c);
2955 if (result >= 0)
2956 return result;
2958 return (fontp->driver->encode_char (fontp, c) != FONT_INVALID_CODE);
2962 /* Return the glyph ID of FONT_OBJECT for character C. */
2964 static unsigned
2965 font_encode_char (Lisp_Object font_object, int c)
2967 struct font *font;
2969 eassert (FONT_OBJECT_P (font_object));
2970 font = XFONT_OBJECT (font_object);
2971 return font->driver->encode_char (font, c);
2975 /* Return the name of FONT_OBJECT. */
2977 Lisp_Object
2978 font_get_name (Lisp_Object font_object)
2980 eassert (FONT_OBJECT_P (font_object));
2981 return AREF (font_object, FONT_NAME_INDEX);
2985 /* Create a new font spec from FONT_NAME, and return it. If FONT_NAME
2986 could not be parsed by font_parse_name, return Qnil. */
2988 Lisp_Object
2989 font_spec_from_name (Lisp_Object font_name)
2991 Lisp_Object spec = Ffont_spec (0, NULL);
2993 CHECK_STRING (font_name);
2994 if (font_parse_name (SSDATA (font_name), SBYTES (font_name), spec) == -1)
2995 return Qnil;
2996 font_put_extra (spec, QCname, font_name);
2997 font_put_extra (spec, QCuser_spec, font_name);
2998 return spec;
3002 void
3003 font_clear_prop (Lisp_Object *attrs, enum font_property_index prop)
3005 Lisp_Object font = attrs[LFACE_FONT_INDEX];
3007 if (! FONTP (font))
3008 return;
3010 if (! NILP (Ffont_get (font, QCname)))
3012 font = copy_font_spec (font);
3013 font_put_extra (font, QCname, Qnil);
3016 if (NILP (AREF (font, prop))
3017 && prop != FONT_FAMILY_INDEX
3018 && prop != FONT_FOUNDRY_INDEX
3019 && prop != FONT_WIDTH_INDEX
3020 && prop != FONT_SIZE_INDEX)
3021 return;
3022 if (EQ (font, attrs[LFACE_FONT_INDEX]))
3023 font = copy_font_spec (font);
3024 ASET (font, prop, Qnil);
3025 if (prop == FONT_FAMILY_INDEX || prop == FONT_FOUNDRY_INDEX)
3027 if (prop == FONT_FAMILY_INDEX)
3029 ASET (font, FONT_FOUNDRY_INDEX, Qnil);
3030 /* If we are setting the font family, we must also clear
3031 FONT_WIDTH_INDEX to avoid rejecting families that lack
3032 support for some widths. */
3033 ASET (font, FONT_WIDTH_INDEX, Qnil);
3035 ASET (font, FONT_ADSTYLE_INDEX, Qnil);
3036 ASET (font, FONT_REGISTRY_INDEX, Qnil);
3037 ASET (font, FONT_SIZE_INDEX, Qnil);
3038 ASET (font, FONT_DPI_INDEX, Qnil);
3039 ASET (font, FONT_SPACING_INDEX, Qnil);
3040 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3042 else if (prop == FONT_SIZE_INDEX)
3044 ASET (font, FONT_DPI_INDEX, Qnil);
3045 ASET (font, FONT_SPACING_INDEX, Qnil);
3046 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3048 else if (prop == FONT_WIDTH_INDEX)
3049 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3050 attrs[LFACE_FONT_INDEX] = font;
3053 /* Select a font from ENTITIES (list of font-entity vectors) that
3054 supports C and is the best match for ATTRS and PIXEL_SIZE. */
3056 static Lisp_Object
3057 font_select_entity (struct frame *f, Lisp_Object entities,
3058 Lisp_Object *attrs, int pixel_size, int c)
3060 Lisp_Object font_entity;
3061 Lisp_Object prefer;
3062 int i;
3064 if (NILP (XCDR (entities))
3065 && ASIZE (XCAR (entities)) == 1)
3067 font_entity = AREF (XCAR (entities), 0);
3068 if (c < 0 || font_has_char (f, font_entity, c) > 0)
3069 return font_entity;
3070 return Qnil;
3073 /* Sort fonts by properties specified in ATTRS. */
3074 prefer = scratch_font_prefer;
3076 for (i = FONT_WEIGHT_INDEX; i <= FONT_SIZE_INDEX; i++)
3077 ASET (prefer, i, Qnil);
3078 if (FONTP (attrs[LFACE_FONT_INDEX]))
3080 Lisp_Object face_font = attrs[LFACE_FONT_INDEX];
3082 for (i = FONT_WEIGHT_INDEX; i <= FONT_SIZE_INDEX; i++)
3083 ASET (prefer, i, AREF (face_font, i));
3085 if (NILP (AREF (prefer, FONT_WEIGHT_INDEX)))
3086 FONT_SET_STYLE (prefer, FONT_WEIGHT_INDEX, attrs[LFACE_WEIGHT_INDEX]);
3087 if (NILP (AREF (prefer, FONT_SLANT_INDEX)))
3088 FONT_SET_STYLE (prefer, FONT_SLANT_INDEX, attrs[LFACE_SLANT_INDEX]);
3089 if (NILP (AREF (prefer, FONT_WIDTH_INDEX)))
3090 FONT_SET_STYLE (prefer, FONT_WIDTH_INDEX, attrs[LFACE_SWIDTH_INDEX]);
3091 ASET (prefer, FONT_SIZE_INDEX, make_number (pixel_size));
3093 return font_sort_entities (entities, prefer, f, c);
3096 /* Return a font-entity that satisfies SPEC and is the best match for
3097 face's font related attributes in ATTRS. C, if not negative, is a
3098 character that the entity must support. */
3100 Lisp_Object
3101 font_find_for_lface (struct frame *f, Lisp_Object *attrs, Lisp_Object spec, int c)
3103 Lisp_Object work;
3104 Lisp_Object entities, val;
3105 Lisp_Object foundry[3], *family, registry[3], adstyle[3];
3106 int pixel_size;
3107 int i, j, k, l;
3108 USE_SAFE_ALLOCA;
3110 registry[0] = AREF (spec, FONT_REGISTRY_INDEX);
3111 if (NILP (registry[0]))
3113 registry[0] = DEFAULT_ENCODING;
3114 registry[1] = Qascii_0;
3115 registry[2] = zero_vector;
3117 else
3118 registry[1] = zero_vector;
3120 if (c >= 0 && ! NILP (AREF (spec, FONT_REGISTRY_INDEX)))
3122 struct charset *encoding, *repertory;
3124 if (font_registry_charsets (AREF (spec, FONT_REGISTRY_INDEX),
3125 &encoding, &repertory) < 0)
3126 return Qnil;
3127 if (repertory
3128 && ENCODE_CHAR (repertory, c) == CHARSET_INVALID_CODE (repertory))
3129 return Qnil;
3130 else if (c > encoding->max_char)
3131 return Qnil;
3134 work = copy_font_spec (spec);
3135 ASET (work, FONT_TYPE_INDEX, AREF (spec, FONT_TYPE_INDEX));
3136 pixel_size = font_pixel_size (f, spec);
3137 if (pixel_size == 0 && INTEGERP (attrs[LFACE_HEIGHT_INDEX]))
3139 double pt = XINT (attrs[LFACE_HEIGHT_INDEX]);
3141 pixel_size = POINT_TO_PIXEL (pt / 10, FRAME_RES_Y (f));
3142 if (pixel_size < 1)
3143 pixel_size = 1;
3145 ASET (work, FONT_SIZE_INDEX, Qnil);
3146 foundry[0] = AREF (work, FONT_FOUNDRY_INDEX);
3147 if (! NILP (foundry[0]))
3148 foundry[1] = zero_vector;
3149 else if (STRINGP (attrs[LFACE_FOUNDRY_INDEX]))
3151 val = attrs[LFACE_FOUNDRY_INDEX];
3152 foundry[0] = font_intern_prop (SSDATA (val), SBYTES (val), 1);
3153 foundry[1] = Qnil;
3154 foundry[2] = zero_vector;
3156 else
3157 foundry[0] = Qnil, foundry[1] = zero_vector;
3159 adstyle[0] = AREF (work, FONT_ADSTYLE_INDEX);
3160 if (! NILP (adstyle[0]))
3161 adstyle[1] = zero_vector;
3162 else if (FONTP (attrs[LFACE_FONT_INDEX]))
3164 Lisp_Object face_font = attrs[LFACE_FONT_INDEX];
3166 if (! NILP (AREF (face_font, FONT_ADSTYLE_INDEX)))
3168 adstyle[0] = AREF (face_font, FONT_ADSTYLE_INDEX);
3169 adstyle[1] = Qnil;
3170 adstyle[2] = zero_vector;
3172 else
3173 adstyle[0] = Qnil, adstyle[1] = zero_vector;
3175 else
3176 adstyle[0] = Qnil, adstyle[1] = zero_vector;
3179 val = AREF (work, FONT_FAMILY_INDEX);
3180 if (NILP (val) && STRINGP (attrs[LFACE_FAMILY_INDEX]))
3182 val = attrs[LFACE_FAMILY_INDEX];
3183 val = font_intern_prop (SSDATA (val), SBYTES (val), 1);
3185 if (NILP (val))
3187 family = alloca ((sizeof family[0]) * 2);
3188 family[0] = Qnil;
3189 family[1] = zero_vector; /* terminator. */
3191 else
3193 Lisp_Object alters
3194 = Fassoc_string (val, Vface_alternative_font_family_alist, Qt);
3196 if (! NILP (alters))
3198 EMACS_INT alterslen = XFASTINT (Flength (alters));
3199 SAFE_ALLOCA_LISP (family, alterslen + 2);
3200 for (i = 0; CONSP (alters); i++, alters = XCDR (alters))
3201 family[i] = XCAR (alters);
3202 if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
3203 family[i++] = Qnil;
3204 family[i] = zero_vector;
3206 else
3208 family = alloca ((sizeof family[0]) * 3);
3209 i = 0;
3210 family[i++] = val;
3211 if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
3212 family[i++] = Qnil;
3213 family[i] = zero_vector;
3217 for (i = 0; SYMBOLP (family[i]); i++)
3219 ASET (work, FONT_FAMILY_INDEX, family[i]);
3220 for (j = 0; SYMBOLP (foundry[j]); j++)
3222 ASET (work, FONT_FOUNDRY_INDEX, foundry[j]);
3223 for (k = 0; SYMBOLP (registry[k]); k++)
3225 ASET (work, FONT_REGISTRY_INDEX, registry[k]);
3226 for (l = 0; SYMBOLP (adstyle[l]); l++)
3228 ASET (work, FONT_ADSTYLE_INDEX, adstyle[l]);
3229 entities = font_list_entities (f, work);
3230 if (! NILP (entities))
3232 val = font_select_entity (f, entities,
3233 attrs, pixel_size, c);
3234 if (! NILP (val))
3236 SAFE_FREE ();
3237 return val;
3245 SAFE_FREE ();
3246 return Qnil;
3250 Lisp_Object
3251 font_open_for_lface (struct frame *f, Lisp_Object entity, Lisp_Object *attrs, Lisp_Object spec)
3253 int size;
3255 if (INTEGERP (AREF (entity, FONT_SIZE_INDEX))
3256 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
3257 size = XINT (AREF (entity, FONT_SIZE_INDEX));
3258 else
3260 if (FONT_SPEC_P (spec) && ! NILP (AREF (spec, FONT_SIZE_INDEX)))
3261 size = font_pixel_size (f, spec);
3262 else
3264 double pt;
3265 if (INTEGERP (attrs[LFACE_HEIGHT_INDEX]))
3266 pt = XINT (attrs[LFACE_HEIGHT_INDEX]);
3267 else
3269 struct face *def = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3270 Lisp_Object height = def->lface[LFACE_HEIGHT_INDEX];
3271 eassert (INTEGERP (height));
3272 pt = XINT (height);
3275 pt /= 10;
3276 size = POINT_TO_PIXEL (pt, FRAME_RES_Y (f));
3277 #ifdef HAVE_NS
3278 if (size == 0)
3280 Lisp_Object ffsize = get_frame_param (f, Qfontsize);
3281 size = (NUMBERP (ffsize)
3282 ? POINT_TO_PIXEL (XINT (ffsize), FRAME_RES_Y (f)) : 0);
3284 #endif
3286 size *= font_rescale_ratio (entity);
3289 return font_open_entity (f, entity, size);
3293 /* Find a font that satisfies SPEC and is the best match for
3294 face's attributes in ATTRS on FRAME, and return the opened
3295 font-object. */
3297 Lisp_Object
3298 font_load_for_lface (struct frame *f, Lisp_Object *attrs, Lisp_Object spec)
3300 Lisp_Object entity, name;
3302 entity = font_find_for_lface (f, attrs, spec, -1);
3303 if (NILP (entity))
3305 /* No font is listed for SPEC, but each font-backend may have
3306 different criteria about "font matching". So, try it. */
3307 entity = font_matching_entity (f, attrs, spec);
3308 if (NILP (entity))
3309 return Qnil;
3311 /* Don't lose the original name that was put in initially. We need
3312 it to re-apply the font when font parameters (like hinting or dpi) have
3313 changed. */
3314 entity = font_open_for_lface (f, entity, attrs, spec);
3315 if (!NILP (entity))
3317 name = Ffont_get (spec, QCuser_spec);
3318 if (STRINGP (name)) font_put_extra (entity, QCuser_spec, name);
3320 return entity;
3324 /* Make FACE on frame F ready to use the font opened for FACE. */
3326 void
3327 font_prepare_for_face (struct frame *f, struct face *face)
3329 if (face->font->driver->prepare_face)
3330 face->font->driver->prepare_face (f, face);
3334 /* Make FACE on frame F stop using the font opened for FACE. */
3336 void
3337 font_done_for_face (struct frame *f, struct face *face)
3339 if (face->font->driver->done_face)
3340 face->font->driver->done_face (f, face);
3341 face->extra = NULL;
3345 /* Open a font that is a match for font-spec SPEC on frame F. If no proper
3346 font is found, return Qnil. */
3348 Lisp_Object
3349 font_open_by_spec (struct frame *f, Lisp_Object spec)
3351 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3353 /* We set up the default font-related attributes of a face to prefer
3354 a moderate font. */
3355 attrs[LFACE_FAMILY_INDEX] = attrs[LFACE_FOUNDRY_INDEX] = Qnil;
3356 attrs[LFACE_SWIDTH_INDEX] = attrs[LFACE_WEIGHT_INDEX]
3357 = attrs[LFACE_SLANT_INDEX] = Qnormal;
3358 #ifndef HAVE_NS
3359 attrs[LFACE_HEIGHT_INDEX] = make_number (120);
3360 #else
3361 attrs[LFACE_HEIGHT_INDEX] = make_number (0);
3362 #endif
3363 attrs[LFACE_FONT_INDEX] = Qnil;
3365 return font_load_for_lface (f, attrs, spec);
3369 /* Open a font that matches NAME on frame F. If no proper font is
3370 found, return Qnil. */
3372 Lisp_Object
3373 font_open_by_name (struct frame *f, Lisp_Object name)
3375 Lisp_Object args[2];
3376 Lisp_Object spec, ret;
3378 args[0] = QCname;
3379 args[1] = name;
3380 spec = Ffont_spec (2, args);
3381 ret = font_open_by_spec (f, spec);
3382 /* Do not lose name originally put in. */
3383 if (!NILP (ret))
3384 font_put_extra (ret, QCuser_spec, args[1]);
3386 return ret;
3390 /* Register font-driver DRIVER. This function is used in two ways.
3392 The first is with frame F non-NULL. In this case, make DRIVER
3393 available (but not yet activated) on F. All frame creators
3394 (e.g. Fx_create_frame) must call this function at least once with
3395 an available font-driver.
3397 The second is with frame F NULL. In this case, DRIVER is globally
3398 registered in the variable `font_driver_list'. All font-driver
3399 implementations must call this function in its syms_of_XXXX
3400 (e.g. syms_of_xfont). */
3402 void
3403 register_font_driver (struct font_driver *driver, struct frame *f)
3405 struct font_driver_list *root = f ? f->font_driver_list : font_driver_list;
3406 struct font_driver_list *prev, *list;
3408 #ifdef HAVE_WINDOW_SYSTEM
3409 if (f && ! driver->draw)
3410 error ("Unusable font driver for a frame: %s",
3411 SDATA (SYMBOL_NAME (driver->type)));
3412 #endif /* HAVE_WINDOW_SYSTEM */
3414 for (prev = NULL, list = root; list; prev = list, list = list->next)
3415 if (EQ (list->driver->type, driver->type))
3416 error ("Duplicated font driver: %s", SDATA (SYMBOL_NAME (driver->type)));
3418 list = xmalloc (sizeof *list);
3419 list->on = 0;
3420 list->driver = driver;
3421 list->next = NULL;
3422 if (prev)
3423 prev->next = list;
3424 else if (f)
3425 f->font_driver_list = list;
3426 else
3427 font_driver_list = list;
3428 if (! f)
3429 num_font_drivers++;
3432 void
3433 free_font_driver_list (struct frame *f)
3435 struct font_driver_list *list, *next;
3437 for (list = f->font_driver_list; list; list = next)
3439 next = list->next;
3440 xfree (list);
3442 f->font_driver_list = NULL;
3446 /* Make the frame F use font backends listed in NEW_DRIVERS (list of
3447 symbols, e.g. xft, x). If NEW_DRIVERS is t, make F use all
3448 available font drivers. If NEW_DRIVERS is nil, finalize all drivers.
3450 A caller must free all realized faces if any in advance. The
3451 return value is a list of font backends actually made used on
3452 F. */
3454 Lisp_Object
3455 font_update_drivers (struct frame *f, Lisp_Object new_drivers)
3457 Lisp_Object active_drivers = Qnil;
3458 struct font_driver_list *list;
3460 /* At first, turn off non-requested drivers, and turn on requested
3461 drivers. */
3462 for (list = f->font_driver_list; list; list = list->next)
3464 struct font_driver *driver = list->driver;
3465 if ((EQ (new_drivers, Qt) || ! NILP (Fmemq (driver->type, new_drivers)))
3466 != list->on)
3468 if (list->on)
3470 if (driver->end_for_frame)
3471 driver->end_for_frame (f);
3472 font_finish_cache (f, driver);
3473 list->on = 0;
3475 else
3477 if (! driver->start_for_frame
3478 || driver->start_for_frame (f) == 0)
3480 font_prepare_cache (f, driver);
3481 list->on = 1;
3487 if (NILP (new_drivers))
3488 return Qnil;
3490 if (! EQ (new_drivers, Qt))
3492 /* Re-order the driver list according to new_drivers. */
3493 struct font_driver_list **list_table, **next;
3494 Lisp_Object tail;
3495 int i;
3497 list_table = alloca (sizeof list_table[0] * (num_font_drivers + 1));
3498 for (i = 0, tail = new_drivers; ! NILP (tail); tail = XCDR (tail))
3500 for (list = f->font_driver_list; list; list = list->next)
3501 if (list->on && EQ (list->driver->type, XCAR (tail)))
3502 break;
3503 if (list)
3504 list_table[i++] = list;
3506 for (list = f->font_driver_list; list; list = list->next)
3507 if (! list->on)
3508 list_table[i++] = list;
3509 list_table[i] = NULL;
3511 next = &f->font_driver_list;
3512 for (i = 0; list_table[i]; i++)
3514 *next = list_table[i];
3515 next = &(*next)->next;
3517 *next = NULL;
3519 if (! f->font_driver_list->on)
3520 { /* None of the drivers is enabled: enable them all.
3521 Happens if you set the list of drivers to (xft x) in your .emacs
3522 and then use it under w32 or ns. */
3523 for (list = f->font_driver_list; list; list = list->next)
3525 struct font_driver *driver = list->driver;
3526 eassert (! list->on);
3527 if (! driver->start_for_frame
3528 || driver->start_for_frame (f) == 0)
3530 font_prepare_cache (f, driver);
3531 list->on = 1;
3537 for (list = f->font_driver_list; list; list = list->next)
3538 if (list->on)
3539 active_drivers = nconc2 (active_drivers, list1 (list->driver->type));
3540 return active_drivers;
3544 font_put_frame_data (struct frame *f, struct font_driver *driver, void *data)
3546 struct font_data_list *list, *prev;
3548 for (prev = NULL, list = f->font_data_list; list;
3549 prev = list, list = list->next)
3550 if (list->driver == driver)
3551 break;
3552 if (! data)
3554 if (list)
3556 if (prev)
3557 prev->next = list->next;
3558 else
3559 f->font_data_list = list->next;
3560 xfree (list);
3562 return 0;
3565 if (! list)
3567 list = xmalloc (sizeof *list);
3568 list->driver = driver;
3569 list->next = f->font_data_list;
3570 f->font_data_list = list;
3572 list->data = data;
3573 return 0;
3577 void *
3578 font_get_frame_data (struct frame *f, struct font_driver *driver)
3580 struct font_data_list *list;
3582 for (list = f->font_data_list; list; list = list->next)
3583 if (list->driver == driver)
3584 break;
3585 if (! list)
3586 return NULL;
3587 return list->data;
3591 /* Sets attributes on a font. Any properties that appear in ALIST and
3592 BOOLEAN_PROPERTIES or NON_BOOLEAN_PROPERTIES are set on the font.
3593 BOOLEAN_PROPERTIES and NON_BOOLEAN_PROPERTIES are NULL-terminated
3594 arrays of strings. This function is intended for use by the font
3595 drivers to implement their specific font_filter_properties. */
3596 void
3597 font_filter_properties (Lisp_Object font,
3598 Lisp_Object alist,
3599 const char *const boolean_properties[],
3600 const char *const non_boolean_properties[])
3602 Lisp_Object it;
3603 int i;
3605 /* Set boolean values to Qt or Qnil. */
3606 for (i = 0; boolean_properties[i] != NULL; ++i)
3607 for (it = alist; ! NILP (it); it = XCDR (it))
3609 Lisp_Object key = XCAR (XCAR (it));
3610 Lisp_Object val = XCDR (XCAR (it));
3611 char *keystr = SSDATA (SYMBOL_NAME (key));
3613 if (strcmp (boolean_properties[i], keystr) == 0)
3615 const char *str = INTEGERP (val) ? (XINT (val) ? "true" : "false")
3616 : SYMBOLP (val) ? SSDATA (SYMBOL_NAME (val))
3617 : "true";
3619 if (strcmp ("false", str) == 0 || strcmp ("False", str) == 0
3620 || strcmp ("FALSE", str) == 0 || strcmp ("FcFalse", str) == 0
3621 || strcmp ("off", str) == 0 || strcmp ("OFF", str) == 0
3622 || strcmp ("Off", str) == 0)
3623 val = Qnil;
3624 else
3625 val = Qt;
3627 Ffont_put (font, key, val);
3631 for (i = 0; non_boolean_properties[i] != NULL; ++i)
3632 for (it = alist; ! NILP (it); it = XCDR (it))
3634 Lisp_Object key = XCAR (XCAR (it));
3635 Lisp_Object val = XCDR (XCAR (it));
3636 char *keystr = SSDATA (SYMBOL_NAME (key));
3637 if (strcmp (non_boolean_properties[i], keystr) == 0)
3638 Ffont_put (font, key, val);
3643 /* Return the font used to draw character C by FACE at buffer position
3644 POS in window W. If STRING is non-nil, it is a string containing C
3645 at index POS. If C is negative, get C from the current buffer or
3646 STRING. */
3648 static Lisp_Object
3649 font_at (int c, ptrdiff_t pos, struct face *face, struct window *w,
3650 Lisp_Object string)
3652 struct frame *f;
3653 bool multibyte;
3654 Lisp_Object font_object;
3656 multibyte = (NILP (string)
3657 ? ! NILP (BVAR (current_buffer, enable_multibyte_characters))
3658 : STRING_MULTIBYTE (string));
3659 if (c < 0)
3661 if (NILP (string))
3663 if (multibyte)
3665 ptrdiff_t pos_byte = CHAR_TO_BYTE (pos);
3667 c = FETCH_CHAR (pos_byte);
3669 else
3670 c = FETCH_BYTE (pos);
3672 else
3674 unsigned char *str;
3676 multibyte = STRING_MULTIBYTE (string);
3677 if (multibyte)
3679 ptrdiff_t pos_byte = string_char_to_byte (string, pos);
3681 str = SDATA (string) + pos_byte;
3682 c = STRING_CHAR (str);
3684 else
3685 c = SDATA (string)[pos];
3689 f = XFRAME (w->frame);
3690 if (! FRAME_WINDOW_P (f))
3691 return Qnil;
3692 if (! face)
3694 int face_id;
3695 ptrdiff_t endptr;
3697 if (STRINGP (string))
3698 face_id = face_at_string_position (w, string, pos, 0, &endptr,
3699 DEFAULT_FACE_ID, 0);
3700 else
3701 face_id = face_at_buffer_position (w, pos, &endptr,
3702 pos + 100, 0, -1);
3703 face = FACE_FROM_ID (f, face_id);
3705 if (multibyte)
3707 int face_id = FACE_FOR_CHAR (f, face, c, pos, string);
3708 face = FACE_FROM_ID (f, face_id);
3710 if (! face->font)
3711 return Qnil;
3713 XSETFONT (font_object, face->font);
3714 return font_object;
3718 #ifdef HAVE_WINDOW_SYSTEM
3720 /* Check how many characters after character/byte position POS/POS_BYTE
3721 (at most to *LIMIT) can be displayed by the same font in the window W.
3722 FACE, if non-NULL, is the face selected for the character at POS.
3723 If STRING is not nil, it is the string to check instead of the current
3724 buffer. In that case, FACE must be not NULL.
3726 The return value is the font-object for the character at POS.
3727 *LIMIT is set to the position where that font can't be used.
3729 It is assured that the current buffer (or STRING) is multibyte. */
3731 Lisp_Object
3732 font_range (ptrdiff_t pos, ptrdiff_t pos_byte, ptrdiff_t *limit,
3733 struct window *w, struct face *face, Lisp_Object string)
3735 ptrdiff_t ignore;
3736 int c;
3737 Lisp_Object font_object = Qnil;
3739 if (NILP (string))
3741 if (! face)
3743 int face_id;
3745 face_id = face_at_buffer_position (w, pos, &ignore,
3746 *limit, 0, -1);
3747 face = FACE_FROM_ID (XFRAME (w->frame), face_id);
3750 else
3751 eassert (face);
3753 while (pos < *limit)
3755 Lisp_Object category;
3757 if (NILP (string))
3758 FETCH_CHAR_ADVANCE_NO_CHECK (c, pos, pos_byte);
3759 else
3760 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, string, pos, pos_byte);
3761 category = CHAR_TABLE_REF (Vunicode_category_table, c);
3762 if (INTEGERP (category)
3763 && (XINT (category) == UNICODE_CATEGORY_Cf
3764 || CHAR_VARIATION_SELECTOR_P (c)))
3765 continue;
3766 if (NILP (font_object))
3768 font_object = font_for_char (face, c, pos - 1, string);
3769 if (NILP (font_object))
3770 return Qnil;
3771 continue;
3773 if (font_encode_char (font_object, c) == FONT_INVALID_CODE)
3774 *limit = pos - 1;
3776 return font_object;
3778 #endif
3781 /* Lisp API. */
3783 DEFUN ("fontp", Ffontp, Sfontp, 1, 2, 0,
3784 doc: /* Return t if OBJECT is a font-spec, font-entity, or font-object.
3785 Return nil otherwise.
3786 Optional 2nd argument EXTRA-TYPE, if non-nil, specifies to check
3787 which kind of font it is. It must be one of `font-spec', `font-entity',
3788 `font-object'. */)
3789 (Lisp_Object object, Lisp_Object extra_type)
3791 if (NILP (extra_type))
3792 return (FONTP (object) ? Qt : Qnil);
3793 if (EQ (extra_type, Qfont_spec))
3794 return (FONT_SPEC_P (object) ? Qt : Qnil);
3795 if (EQ (extra_type, Qfont_entity))
3796 return (FONT_ENTITY_P (object) ? Qt : Qnil);
3797 if (EQ (extra_type, Qfont_object))
3798 return (FONT_OBJECT_P (object) ? Qt : Qnil);
3799 wrong_type_argument (intern ("font-extra-type"), extra_type);
3802 DEFUN ("font-spec", Ffont_spec, Sfont_spec, 0, MANY, 0,
3803 doc: /* Return a newly created font-spec with arguments as properties.
3805 ARGS must come in pairs KEY VALUE of font properties. KEY must be a
3806 valid font property name listed below:
3808 `:family', `:weight', `:slant', `:width'
3810 They are the same as face attributes of the same name. See
3811 `set-face-attribute'.
3813 `:foundry'
3815 VALUE must be a string or a symbol specifying the font foundry, e.g. ``misc''.
3817 `:adstyle'
3819 VALUE must be a string or a symbol specifying the additional
3820 typographic style information of a font, e.g. ``sans''.
3822 `:registry'
3824 VALUE must be a string or a symbol specifying the charset registry and
3825 encoding of a font, e.g. ``iso8859-1''.
3827 `:size'
3829 VALUE must be a non-negative integer or a floating point number
3830 specifying the font size. It specifies the font size in pixels (if
3831 VALUE is an integer), or in points (if VALUE is a float).
3833 `:name'
3835 VALUE must be a string of XLFD-style or fontconfig-style font name.
3837 `:script'
3839 VALUE must be a symbol representing a script that the font must
3840 support. It may be a symbol representing a subgroup of a script
3841 listed in the variable `script-representative-chars'.
3843 `:lang'
3845 VALUE must be a symbol of two-letter ISO-639 language names,
3846 e.g. `ja'.
3848 `:otf'
3850 VALUE must be a list (SCRIPT-TAG LANGSYS-TAG GSUB [ GPOS ]) to specify
3851 required OpenType features.
3853 SCRIPT-TAG: OpenType script tag symbol (e.g. `deva').
3854 LANGSYS-TAG: OpenType language system tag symbol,
3855 or nil for the default language system.
3856 GSUB: List of OpenType GSUB feature tag symbols, or nil if none required.
3857 GPOS: List of OpenType GPOS feature tag symbols, or nil if none required.
3859 GSUB and GPOS may contain `nil' element. In such a case, the font
3860 must not have any of the remaining elements.
3862 For instance, if the VALUE is `(thai nil nil (mark))', the font must
3863 be an OpenType font whose GPOS table of `thai' script's default
3864 language system must contain `mark' feature.
3866 usage: (font-spec ARGS...) */)
3867 (ptrdiff_t nargs, Lisp_Object *args)
3869 Lisp_Object spec = font_make_spec ();
3870 ptrdiff_t i;
3872 for (i = 0; i < nargs; i += 2)
3874 Lisp_Object key = args[i], val;
3876 CHECK_SYMBOL (key);
3877 if (i + 1 >= nargs)
3878 error ("No value for key `%s'", SDATA (SYMBOL_NAME (key)));
3879 val = args[i + 1];
3881 if (EQ (key, QCname))
3883 CHECK_STRING (val);
3884 if (font_parse_name (SSDATA (val), SBYTES (val), spec) < 0)
3885 error ("Invalid font name: %s", SSDATA (val));
3886 font_put_extra (spec, key, val);
3888 else
3890 int idx = get_font_prop_index (key);
3892 if (idx >= 0)
3894 val = font_prop_validate (idx, Qnil, val);
3895 if (idx < FONT_EXTRA_INDEX)
3896 ASET (spec, idx, val);
3897 else
3898 font_put_extra (spec, key, val);
3900 else
3901 font_put_extra (spec, key, font_prop_validate (0, key, val));
3904 return spec;
3907 /* Return a copy of FONT as a font-spec. */
3908 Lisp_Object
3909 copy_font_spec (Lisp_Object font)
3911 Lisp_Object new_spec, tail, prev, extra;
3912 int i;
3914 CHECK_FONT (font);
3915 new_spec = font_make_spec ();
3916 for (i = 1; i < FONT_EXTRA_INDEX; i++)
3917 ASET (new_spec, i, AREF (font, i));
3918 extra = Fcopy_alist (AREF (font, FONT_EXTRA_INDEX));
3919 /* We must remove :font-entity property. */
3920 for (prev = Qnil, tail = extra; CONSP (tail); prev = tail, tail = XCDR (tail))
3921 if (EQ (XCAR (XCAR (tail)), QCfont_entity))
3923 if (NILP (prev))
3924 extra = XCDR (extra);
3925 else
3926 XSETCDR (prev, XCDR (tail));
3927 break;
3929 ASET (new_spec, FONT_EXTRA_INDEX, extra);
3930 return new_spec;
3933 /* Merge font-specs FROM and TO, and return a new font-spec.
3934 Every specified property in FROM overrides the corresponding
3935 property in TO. */
3936 Lisp_Object
3937 merge_font_spec (Lisp_Object from, Lisp_Object to)
3939 Lisp_Object extra, tail;
3940 int i;
3942 CHECK_FONT (from);
3943 CHECK_FONT (to);
3944 to = copy_font_spec (to);
3945 for (i = 0; i < FONT_EXTRA_INDEX; i++)
3946 ASET (to, i, AREF (from, i));
3947 extra = AREF (to, FONT_EXTRA_INDEX);
3948 for (tail = AREF (from, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail))
3949 if (! EQ (XCAR (XCAR (tail)), Qfont_entity))
3951 Lisp_Object slot = assq_no_quit (XCAR (XCAR (tail)), extra);
3953 if (! NILP (slot))
3954 XSETCDR (slot, XCDR (XCAR (tail)));
3955 else
3956 extra = Fcons (Fcons (XCAR (XCAR (tail)), XCDR (XCAR (tail))), extra);
3958 ASET (to, FONT_EXTRA_INDEX, extra);
3959 return to;
3962 DEFUN ("font-get", Ffont_get, Sfont_get, 2, 2, 0,
3963 doc: /* Return the value of FONT's property KEY.
3964 FONT is a font-spec, a font-entity, or a font-object.
3965 KEY is any symbol, but these are reserved for specific meanings:
3966 :family, :weight, :slant, :width, :foundry, :adstyle, :registry,
3967 :size, :name, :script, :otf
3968 See the documentation of `font-spec' for their meanings.
3969 In addition, if FONT is a font-entity or a font-object, values of
3970 :script and :otf are different from those of a font-spec as below:
3972 The value of :script may be a list of scripts that are supported by the font.
3974 The value of :otf is a cons (GSUB . GPOS) where GSUB and GPOS are lists
3975 representing the OpenType features supported by the font by this form:
3976 ((SCRIPT (LANGSYS FEATURE ...) ...) ...)
3977 SCRIPT, LANGSYS, and FEATURE are all symbols representing OpenType
3978 Layout tags. */)
3979 (Lisp_Object font, Lisp_Object key)
3981 int idx;
3982 Lisp_Object val;
3984 CHECK_FONT (font);
3985 CHECK_SYMBOL (key);
3987 idx = get_font_prop_index (key);
3988 if (idx >= FONT_WEIGHT_INDEX && idx <= FONT_WIDTH_INDEX)
3989 return font_style_symbolic (font, idx, 0);
3990 if (idx >= 0 && idx < FONT_EXTRA_INDEX)
3991 return AREF (font, idx);
3992 val = Fassq (key, AREF (font, FONT_EXTRA_INDEX));
3993 if (NILP (val) && EQ (key, QCotf) && FONT_OBJECT_P (font))
3995 struct font *fontp = XFONT_OBJECT (font);
3997 if (fontp->driver->otf_capability)
3998 val = fontp->driver->otf_capability (fontp);
3999 else
4000 val = Fcons (Qnil, Qnil);
4002 else
4003 val = Fcdr (val);
4004 return val;
4007 #ifdef HAVE_WINDOW_SYSTEM
4009 DEFUN ("font-face-attributes", Ffont_face_attributes, Sfont_face_attributes, 1, 2, 0,
4010 doc: /* Return a plist of face attributes generated by FONT.
4011 FONT is a font name, a font-spec, a font-entity, or a font-object.
4012 The return value is a list of the form
4014 \(:family FAMILY :height HEIGHT :weight WEIGHT :slant SLANT :width WIDTH)
4016 where FAMILY, HEIGHT, WEIGHT, SLANT, and WIDTH are face attribute values
4017 compatible with `set-face-attribute'. Some of these key-attribute pairs
4018 may be omitted from the list if they are not specified by FONT.
4020 The optional argument FRAME specifies the frame that the face attributes
4021 are to be displayed on. If omitted, the selected frame is used. */)
4022 (Lisp_Object font, Lisp_Object frame)
4024 struct frame *f = decode_live_frame (frame);
4025 Lisp_Object plist[10];
4026 Lisp_Object val;
4027 int n = 0;
4029 if (STRINGP (font))
4031 int fontset = fs_query_fontset (font, 0);
4032 Lisp_Object name = font;
4033 if (fontset >= 0)
4034 font = fontset_ascii (fontset);
4035 font = font_spec_from_name (name);
4036 if (! FONTP (font))
4037 signal_error ("Invalid font name", name);
4039 else if (! FONTP (font))
4040 signal_error ("Invalid font object", font);
4042 val = AREF (font, FONT_FAMILY_INDEX);
4043 if (! NILP (val))
4045 plist[n++] = QCfamily;
4046 plist[n++] = SYMBOL_NAME (val);
4049 val = AREF (font, FONT_SIZE_INDEX);
4050 if (INTEGERP (val))
4052 Lisp_Object font_dpi = AREF (font, FONT_DPI_INDEX);
4053 int dpi = INTEGERP (font_dpi) ? XINT (font_dpi) : FRAME_RES_Y (f);
4054 plist[n++] = QCheight;
4055 plist[n++] = make_number (PIXEL_TO_POINT (XINT (val) * 10, dpi));
4057 else if (FLOATP (val))
4059 plist[n++] = QCheight;
4060 plist[n++] = make_number (10 * (int) XFLOAT_DATA (val));
4063 val = FONT_WEIGHT_FOR_FACE (font);
4064 if (! NILP (val))
4066 plist[n++] = QCweight;
4067 plist[n++] = val;
4070 val = FONT_SLANT_FOR_FACE (font);
4071 if (! NILP (val))
4073 plist[n++] = QCslant;
4074 plist[n++] = val;
4077 val = FONT_WIDTH_FOR_FACE (font);
4078 if (! NILP (val))
4080 plist[n++] = QCwidth;
4081 plist[n++] = val;
4084 return Flist (n, plist);
4087 #endif
4089 DEFUN ("font-put", Ffont_put, Sfont_put, 3, 3, 0,
4090 doc: /* Set one property of FONT: give property KEY value VAL.
4091 FONT is a font-spec, a font-entity, or a font-object.
4093 If FONT is a font-spec, KEY can be any symbol. But if KEY is the one
4094 accepted by the function `font-spec' (which see), VAL must be what
4095 allowed in `font-spec'.
4097 If FONT is a font-entity or a font-object, KEY must not be the one
4098 accepted by `font-spec'. */)
4099 (Lisp_Object font, Lisp_Object prop, Lisp_Object val)
4101 int idx;
4103 idx = get_font_prop_index (prop);
4104 if (idx >= 0 && idx < FONT_EXTRA_INDEX)
4106 CHECK_FONT_SPEC (font);
4107 ASET (font, idx, font_prop_validate (idx, Qnil, val));
4109 else
4111 if (EQ (prop, QCname)
4112 || EQ (prop, QCscript)
4113 || EQ (prop, QClang)
4114 || EQ (prop, QCotf))
4115 CHECK_FONT_SPEC (font);
4116 else
4117 CHECK_FONT (font);
4118 font_put_extra (font, prop, font_prop_validate (0, prop, val));
4120 return val;
4123 DEFUN ("list-fonts", Flist_fonts, Slist_fonts, 1, 4, 0,
4124 doc: /* List available fonts matching FONT-SPEC on the current frame.
4125 Optional 2nd argument FRAME specifies the target frame.
4126 Optional 3rd argument NUM, if non-nil, limits the number of returned fonts.
4127 Optional 4th argument PREFER, if non-nil, is a font-spec to
4128 control the order of the returned list. Fonts are sorted by
4129 how close they are to PREFER. */)
4130 (Lisp_Object font_spec, Lisp_Object frame, Lisp_Object num, Lisp_Object prefer)
4132 struct frame *f = decode_live_frame (frame);
4133 Lisp_Object vec, list;
4134 EMACS_INT n = 0;
4136 CHECK_FONT_SPEC (font_spec);
4137 if (! NILP (num))
4139 CHECK_NUMBER (num);
4140 n = XINT (num);
4141 if (n <= 0)
4142 return Qnil;
4144 if (! NILP (prefer))
4145 CHECK_FONT_SPEC (prefer);
4147 list = font_list_entities (f, font_spec);
4148 if (NILP (list))
4149 return Qnil;
4150 if (NILP (XCDR (list))
4151 && ASIZE (XCAR (list)) == 1)
4152 return list1 (AREF (XCAR (list), 0));
4154 if (! NILP (prefer))
4155 vec = font_sort_entities (list, prefer, f, 0);
4156 else
4157 vec = font_vconcat_entity_vectors (list);
4158 if (n == 0 || n >= ASIZE (vec))
4160 Lisp_Object args[2];
4162 args[0] = vec;
4163 args[1] = Qnil;
4164 list = Fappend (2, args);
4166 else
4168 for (list = Qnil, n--; n >= 0; n--)
4169 list = Fcons (AREF (vec, n), list);
4171 return list;
4174 DEFUN ("font-family-list", Ffont_family_list, Sfont_family_list, 0, 1, 0,
4175 doc: /* List available font families on the current frame.
4176 If FRAME is omitted or nil, the selected frame is used. */)
4177 (Lisp_Object frame)
4179 struct frame *f = decode_live_frame (frame);
4180 struct font_driver_list *driver_list;
4181 Lisp_Object list = Qnil;
4183 for (driver_list = f->font_driver_list; driver_list;
4184 driver_list = driver_list->next)
4185 if (driver_list->driver->list_family)
4187 Lisp_Object val = driver_list->driver->list_family (f);
4188 Lisp_Object tail = list;
4190 for (; CONSP (val); val = XCDR (val))
4191 if (NILP (Fmemq (XCAR (val), tail))
4192 && SYMBOLP (XCAR (val)))
4193 list = Fcons (SYMBOL_NAME (XCAR (val)), list);
4195 return list;
4198 DEFUN ("find-font", Ffind_font, Sfind_font, 1, 2, 0,
4199 doc: /* Return a font-entity matching with FONT-SPEC on the current frame.
4200 Optional 2nd argument FRAME, if non-nil, specifies the target frame. */)
4201 (Lisp_Object font_spec, Lisp_Object frame)
4203 Lisp_Object val = Flist_fonts (font_spec, frame, make_number (1), Qnil);
4205 if (CONSP (val))
4206 val = XCAR (val);
4207 return val;
4210 DEFUN ("font-xlfd-name", Ffont_xlfd_name, Sfont_xlfd_name, 1, 2, 0,
4211 doc: /* Return XLFD name of FONT.
4212 FONT is a font-spec, font-entity, or font-object.
4213 If the name is too long for XLFD (maximum 255 chars), return nil.
4214 If the 2nd optional arg FOLD-WILDCARDS is non-nil,
4215 the consecutive wildcards are folded into one. */)
4216 (Lisp_Object font, Lisp_Object fold_wildcards)
4218 char name[256];
4219 int namelen, pixel_size = 0;
4221 CHECK_FONT (font);
4223 if (FONT_OBJECT_P (font))
4225 Lisp_Object font_name = AREF (font, FONT_NAME_INDEX);
4227 if (STRINGP (font_name)
4228 && SDATA (font_name)[0] == '-')
4230 if (NILP (fold_wildcards))
4231 return font_name;
4232 strcpy (name, SSDATA (font_name));
4233 namelen = SBYTES (font_name);
4234 goto done;
4236 pixel_size = XFONT_OBJECT (font)->pixel_size;
4238 namelen = font_unparse_xlfd (font, pixel_size, name, 256);
4239 if (namelen < 0)
4240 return Qnil;
4241 done:
4242 if (! NILP (fold_wildcards))
4244 char *p0 = name, *p1;
4246 while ((p1 = strstr (p0, "-*-*")))
4248 strcpy (p1, p1 + 2);
4249 namelen -= 2;
4250 p0 = p1;
4254 return make_string (name, namelen);
4257 void
4258 clear_font_cache (struct frame *f)
4260 struct font_driver_list *driver_list = f->font_driver_list;
4262 for (; driver_list; driver_list = driver_list->next)
4263 if (driver_list->on)
4265 Lisp_Object val, tmp, cache = driver_list->driver->get_cache (f);
4267 val = XCDR (cache);
4268 while (! NILP (val)
4269 && ! EQ (XCAR (XCAR (val)), driver_list->driver->type))
4270 val = XCDR (val);
4271 eassert (! NILP (val));
4272 tmp = XCDR (XCAR (val));
4273 if (XINT (XCAR (tmp)) == 0)
4275 font_clear_cache (f, XCAR (val), driver_list->driver);
4276 XSETCDR (cache, XCDR (val));
4281 DEFUN ("clear-font-cache", Fclear_font_cache, Sclear_font_cache, 0, 0, 0,
4282 doc: /* Clear font cache of each frame. */)
4283 (void)
4285 Lisp_Object list, frame;
4287 FOR_EACH_FRAME (list, frame)
4288 clear_font_cache (XFRAME (frame));
4290 return Qnil;
4294 void
4295 font_fill_lglyph_metrics (Lisp_Object glyph, Lisp_Object font_object)
4297 struct font *font = XFONT_OBJECT (font_object);
4298 unsigned code = font->driver->encode_char (font, LGLYPH_CHAR (glyph));
4299 struct font_metrics metrics;
4301 LGLYPH_SET_CODE (glyph, code);
4302 font->driver->text_extents (font, &code, 1, &metrics);
4303 LGLYPH_SET_LBEARING (glyph, metrics.lbearing);
4304 LGLYPH_SET_RBEARING (glyph, metrics.rbearing);
4305 LGLYPH_SET_WIDTH (glyph, metrics.width);
4306 LGLYPH_SET_ASCENT (glyph, metrics.ascent);
4307 LGLYPH_SET_DESCENT (glyph, metrics.descent);
4311 DEFUN ("font-shape-gstring", Ffont_shape_gstring, Sfont_shape_gstring, 1, 1, 0,
4312 doc: /* Shape the glyph-string GSTRING.
4313 Shaping means substituting glyphs and/or adjusting positions of glyphs
4314 to get the correct visual image of character sequences set in the
4315 header of the glyph-string.
4317 If the shaping was successful, the value is GSTRING itself or a newly
4318 created glyph-string. Otherwise, the value is nil.
4320 See the documentation of `composition-get-gstring' for the format of
4321 GSTRING. */)
4322 (Lisp_Object gstring)
4324 struct font *font;
4325 Lisp_Object font_object, n, glyph;
4326 ptrdiff_t i, from, to;
4328 if (! composition_gstring_p (gstring))
4329 signal_error ("Invalid glyph-string: ", gstring);
4330 if (! NILP (LGSTRING_ID (gstring)))
4331 return gstring;
4332 font_object = LGSTRING_FONT (gstring);
4333 CHECK_FONT_OBJECT (font_object);
4334 font = XFONT_OBJECT (font_object);
4335 if (! font->driver->shape)
4336 return Qnil;
4338 /* Try at most three times with larger gstring each time. */
4339 for (i = 0; i < 3; i++)
4341 n = font->driver->shape (gstring);
4342 if (INTEGERP (n))
4343 break;
4344 gstring = larger_vector (gstring,
4345 LGSTRING_GLYPH_LEN (gstring), -1);
4347 if (i == 3 || XINT (n) == 0)
4348 return Qnil;
4349 if (XINT (n) < LGSTRING_GLYPH_LEN (gstring))
4350 LGSTRING_SET_GLYPH (gstring, XINT (n), Qnil);
4352 /* Check FROM_IDX and TO_IDX of each GLYPH in GSTRING to assure that
4353 GLYPHS covers all characters (except for the last few ones) in
4354 GSTRING. More formally, provided that NCHARS is the number of
4355 characters in GSTRING and GLYPHS[i] is the ith glyph, FROM_IDX
4356 and TO_IDX of each glyph must satisfy these conditions:
4358 GLYPHS[0].FROM_IDX == 0
4359 GLYPHS[i].FROM_IDX <= GLYPHS[i].TO_IDX
4360 if (GLYPHS[i].FROM_IDX == GLYPHS[i-1].FROM_IDX)
4361 ;; GLYPHS[i] and GLYPHS[i-1] belongs to the same grapheme cluster
4362 GLYPHS[i].TO_IDX == GLYPHS[i-1].TO_IDX
4363 else
4364 ;; Be sure to cover all characters.
4365 GLYPHS[i].FROM_IDX == GLYPHS[i-1].TO_IDX + 1 */
4366 glyph = LGSTRING_GLYPH (gstring, 0);
4367 from = LGLYPH_FROM (glyph);
4368 to = LGLYPH_TO (glyph);
4369 if (from != 0 || to < from)
4370 goto shaper_error;
4371 for (i = 1; i < LGSTRING_GLYPH_LEN (gstring); i++)
4373 glyph = LGSTRING_GLYPH (gstring, i);
4374 if (NILP (glyph))
4375 break;
4376 if (! (LGLYPH_FROM (glyph) <= LGLYPH_TO (glyph)
4377 && (LGLYPH_FROM (glyph) == from
4378 ? LGLYPH_TO (glyph) == to
4379 : LGLYPH_FROM (glyph) == to + 1)))
4380 goto shaper_error;
4381 from = LGLYPH_FROM (glyph);
4382 to = LGLYPH_TO (glyph);
4384 return composition_gstring_put_cache (gstring, XINT (n));
4386 shaper_error:
4387 return Qnil;
4390 DEFUN ("font-variation-glyphs", Ffont_variation_glyphs, Sfont_variation_glyphs,
4391 2, 2, 0,
4392 doc: /* Return a list of variation glyphs for CHAR in FONT-OBJECT.
4393 Each element of the value is a cons (VARIATION-SELECTOR . GLYPH-ID),
4394 where
4395 VARIATION-SELECTOR is a character code of variation selection
4396 (#xFE00..#xFE0F or #xE0100..#xE01EF)
4397 GLYPH-ID is a glyph code of the corresponding variation glyph. */)
4398 (Lisp_Object font_object, Lisp_Object character)
4400 unsigned variations[256];
4401 struct font *font;
4402 int i, n;
4403 Lisp_Object val;
4405 CHECK_FONT_OBJECT (font_object);
4406 CHECK_CHARACTER (character);
4407 font = XFONT_OBJECT (font_object);
4408 if (! font->driver->get_variation_glyphs)
4409 return Qnil;
4410 n = font->driver->get_variation_glyphs (font, XINT (character), variations);
4411 if (! n)
4412 return Qnil;
4413 val = Qnil;
4414 for (i = 0; i < 255; i++)
4415 if (variations[i])
4417 int vs = (i < 16 ? 0xFE00 + i : 0xE0100 + (i - 16));
4418 Lisp_Object code = INTEGER_TO_CONS (variations[i]);
4419 val = Fcons (Fcons (make_number (vs), code), val);
4421 return val;
4424 #if 0
4426 DEFUN ("font-drive-otf", Ffont_drive_otf, Sfont_drive_otf, 6, 6, 0,
4427 doc: /* Apply OpenType features on glyph-string GSTRING-IN.
4428 OTF-FEATURES specifies which features to apply in this format:
4429 (SCRIPT LANGSYS GSUB GPOS)
4430 where
4431 SCRIPT is a symbol specifying a script tag of OpenType,
4432 LANGSYS is a symbol specifying a langsys tag of OpenType,
4433 GSUB and GPOS, if non-nil, are lists of symbols specifying feature tags.
4435 If LANGSYS is nil, the default langsys is selected.
4437 The features are applied in the order they appear in the list. The
4438 symbol `*' means to apply all available features not present in this
4439 list, and the remaining features are ignored. For instance, (vatu
4440 pstf * haln) is to apply vatu and pstf in this order, then to apply
4441 all available features other than vatu, pstf, and haln.
4443 The features are applied to the glyphs in the range FROM and TO of
4444 the glyph-string GSTRING-IN.
4446 If some feature is actually applicable, the resulting glyphs are
4447 produced in the glyph-string GSTRING-OUT from the index INDEX. In
4448 this case, the value is the number of produced glyphs.
4450 If no feature is applicable, no glyph is produced in GSTRING-OUT, and
4451 the value is 0.
4453 If GSTRING-OUT is too short to hold produced glyphs, no glyphs are
4454 produced in GSTRING-OUT, and the value is nil.
4456 See the documentation of `composition-get-gstring' for the format of
4457 glyph-string. */)
4458 (Lisp_Object otf_features, Lisp_Object gstring_in, Lisp_Object from, Lisp_Object to, Lisp_Object gstring_out, Lisp_Object index)
4460 Lisp_Object font_object = LGSTRING_FONT (gstring_in);
4461 Lisp_Object val;
4462 struct font *font;
4463 int len, num;
4465 check_otf_features (otf_features);
4466 CHECK_FONT_OBJECT (font_object);
4467 font = XFONT_OBJECT (font_object);
4468 if (! font->driver->otf_drive)
4469 error ("Font backend %s can't drive OpenType GSUB table",
4470 SDATA (SYMBOL_NAME (font->driver->type)));
4471 CHECK_CONS (otf_features);
4472 CHECK_SYMBOL (XCAR (otf_features));
4473 val = XCDR (otf_features);
4474 CHECK_SYMBOL (XCAR (val));
4475 val = XCDR (otf_features);
4476 if (! NILP (val))
4477 CHECK_CONS (val);
4478 len = check_gstring (gstring_in);
4479 CHECK_VECTOR (gstring_out);
4480 CHECK_NATNUM (from);
4481 CHECK_NATNUM (to);
4482 CHECK_NATNUM (index);
4484 if (XINT (from) >= XINT (to) || XINT (to) > len)
4485 args_out_of_range_3 (from, to, make_number (len));
4486 if (XINT (index) >= ASIZE (gstring_out))
4487 args_out_of_range (index, make_number (ASIZE (gstring_out)));
4488 num = font->driver->otf_drive (font, otf_features,
4489 gstring_in, XINT (from), XINT (to),
4490 gstring_out, XINT (index), 0);
4491 if (num < 0)
4492 return Qnil;
4493 return make_number (num);
4496 DEFUN ("font-otf-alternates", Ffont_otf_alternates, Sfont_otf_alternates,
4497 3, 3, 0,
4498 doc: /* Return a list of alternate glyphs of CHARACTER in FONT-OBJECT.
4499 OTF-FEATURES specifies which features of the font FONT-OBJECT to apply
4500 in this format:
4501 (SCRIPT LANGSYS FEATURE ...)
4502 See the documentation of `font-drive-otf' for more detail.
4504 The value is a list of cons cells of the format (GLYPH-ID . CHARACTER),
4505 where GLYPH-ID is a glyph index of the font, and CHARACTER is a
4506 character code corresponding to the glyph or nil if there's no
4507 corresponding character. */)
4508 (Lisp_Object font_object, Lisp_Object character, Lisp_Object otf_features)
4510 struct font *font;
4511 Lisp_Object gstring_in, gstring_out, g;
4512 Lisp_Object alternates;
4513 int i, num;
4515 CHECK_FONT_GET_OBJECT (font_object, font);
4516 if (! font->driver->otf_drive)
4517 error ("Font backend %s can't drive OpenType GSUB table",
4518 SDATA (SYMBOL_NAME (font->driver->type)));
4519 CHECK_CHARACTER (character);
4520 CHECK_CONS (otf_features);
4522 gstring_in = Ffont_make_gstring (font_object, make_number (1));
4523 g = LGSTRING_GLYPH (gstring_in, 0);
4524 LGLYPH_SET_CHAR (g, XINT (character));
4525 gstring_out = Ffont_make_gstring (font_object, make_number (10));
4526 while ((num = font->driver->otf_drive (font, otf_features, gstring_in, 0, 1,
4527 gstring_out, 0, 1)) < 0)
4528 gstring_out = Ffont_make_gstring (font_object,
4529 make_number (ASIZE (gstring_out) * 2));
4530 alternates = Qnil;
4531 for (i = 0; i < num; i++)
4533 Lisp_Object g = LGSTRING_GLYPH (gstring_out, i);
4534 int c = LGLYPH_CHAR (g);
4535 unsigned code = LGLYPH_CODE (g);
4537 alternates = Fcons (Fcons (make_number (code),
4538 c > 0 ? make_number (c) : Qnil),
4539 alternates);
4541 return Fnreverse (alternates);
4543 #endif /* 0 */
4545 #ifdef FONT_DEBUG
4547 DEFUN ("open-font", Fopen_font, Sopen_font, 1, 3, 0,
4548 doc: /* Open FONT-ENTITY. */)
4549 (Lisp_Object font_entity, Lisp_Object size, Lisp_Object frame)
4551 EMACS_INT isize;
4552 struct frame *f = decode_live_frame (frame);
4554 CHECK_FONT_ENTITY (font_entity);
4556 if (NILP (size))
4557 isize = XINT (AREF (font_entity, FONT_SIZE_INDEX));
4558 else
4560 CHECK_NUMBER_OR_FLOAT (size);
4561 if (FLOATP (size))
4562 isize = POINT_TO_PIXEL (XFLOAT_DATA (size), FRAME_RES_Y (f));
4563 else
4564 isize = XINT (size);
4565 if (! (INT_MIN <= isize && isize <= INT_MAX))
4566 args_out_of_range (font_entity, size);
4567 if (isize == 0)
4568 isize = 120;
4570 return font_open_entity (f, font_entity, isize);
4573 DEFUN ("close-font", Fclose_font, Sclose_font, 1, 2, 0,
4574 doc: /* Close FONT-OBJECT. */)
4575 (Lisp_Object font_object, Lisp_Object frame)
4577 CHECK_FONT_OBJECT (font_object);
4578 font_close_object (decode_live_frame (frame), font_object);
4579 return Qnil;
4582 DEFUN ("query-font", Fquery_font, Squery_font, 1, 1, 0,
4583 doc: /* Return information about FONT-OBJECT.
4584 The value is a vector:
4585 [ NAME FILENAME PIXEL-SIZE SIZE ASCENT DESCENT SPACE-WIDTH AVERAGE-WIDTH
4586 CAPABILITY ]
4588 NAME is the font name, a string (or nil if the font backend doesn't
4589 provide a name).
4591 FILENAME is the font file name, a string (or nil if the font backend
4592 doesn't provide a file name).
4594 PIXEL-SIZE is a pixel size by which the font is opened.
4596 SIZE is a maximum advance width of the font in pixels.
4598 ASCENT, DESCENT, SPACE-WIDTH, AVERAGE-WIDTH are metrics of the font in
4599 pixels.
4601 CAPABILITY is a list whose first element is a symbol representing the
4602 font format \(x, opentype, truetype, type1, pcf, or bdf) and the
4603 remaining elements describe the details of the font capability.
4605 If the font is OpenType font, the form of the list is
4606 \(opentype GSUB GPOS)
4607 where GSUB shows which "GSUB" features the font supports, and GPOS
4608 shows which "GPOS" features the font supports. Both GSUB and GPOS are
4609 lists of the format:
4610 \((SCRIPT (LANGSYS FEATURE ...) ...) ...)
4612 If the font is not OpenType font, currently the length of the form is
4613 one.
4615 SCRIPT is a symbol representing OpenType script tag.
4617 LANGSYS is a symbol representing OpenType langsys tag, or nil
4618 representing the default langsys.
4620 FEATURE is a symbol representing OpenType feature tag.
4622 If the font is not OpenType font, CAPABILITY is nil. */)
4623 (Lisp_Object font_object)
4625 struct font *font;
4626 Lisp_Object val;
4628 CHECK_FONT_GET_OBJECT (font_object, font);
4630 val = make_uninit_vector (9);
4631 ASET (val, 0, AREF (font_object, FONT_NAME_INDEX));
4632 ASET (val, 1, AREF (font_object, FONT_FILE_INDEX));
4633 ASET (val, 2, make_number (font->pixel_size));
4634 ASET (val, 3, make_number (font->max_width));
4635 ASET (val, 4, make_number (font->ascent));
4636 ASET (val, 5, make_number (font->descent));
4637 ASET (val, 6, make_number (font->space_width));
4638 ASET (val, 7, make_number (font->average_width));
4639 if (font->driver->otf_capability)
4640 ASET (val, 8, Fcons (Qopentype, font->driver->otf_capability (font)));
4641 else
4642 ASET (val, 8, Qnil);
4643 return val;
4646 DEFUN ("font-get-glyphs", Ffont_get_glyphs, Sfont_get_glyphs, 3, 4, 0,
4647 doc:
4648 /* Return a vector of FONT-OBJECT's glyphs for the specified characters.
4649 FROM and TO are positions (integers or markers) specifying a region
4650 of the current buffer.
4651 If the optional fourth arg OBJECT is not nil, it is a string or a
4652 vector containing the target characters.
4654 Each element is a vector containing information of a glyph in this format:
4655 [FROM-IDX TO-IDX C CODE WIDTH LBEARING RBEARING ASCENT DESCENT ADJUSTMENT]
4656 where
4657 FROM is an index numbers of a character the glyph corresponds to.
4658 TO is the same as FROM.
4659 C is the character of the glyph.
4660 CODE is the glyph-code of C in FONT-OBJECT.
4661 WIDTH thru DESCENT are the metrics (in pixels) of the glyph.
4662 ADJUSTMENT is always nil.
4663 If FONT-OBJECT doesn't have a glyph for a character,
4664 the corresponding element is nil. */)
4665 (Lisp_Object font_object, Lisp_Object from, Lisp_Object to,
4666 Lisp_Object object)
4668 struct font *font;
4669 ptrdiff_t i, len;
4670 Lisp_Object *chars, vec;
4671 USE_SAFE_ALLOCA;
4673 CHECK_FONT_GET_OBJECT (font_object, font);
4674 if (NILP (object))
4676 ptrdiff_t charpos, bytepos;
4678 validate_region (&from, &to);
4679 if (EQ (from, to))
4680 return Qnil;
4681 len = XFASTINT (to) - XFASTINT (from);
4682 SAFE_ALLOCA_LISP (chars, len);
4683 charpos = XFASTINT (from);
4684 bytepos = CHAR_TO_BYTE (charpos);
4685 for (i = 0; charpos < XFASTINT (to); i++)
4687 int c;
4688 FETCH_CHAR_ADVANCE (c, charpos, bytepos);
4689 chars[i] = make_number (c);
4692 else if (STRINGP (object))
4694 const unsigned char *p;
4696 CHECK_NUMBER (from);
4697 CHECK_NUMBER (to);
4698 if (XINT (from) < 0 || XINT (from) > XINT (to)
4699 || XINT (to) > SCHARS (object))
4700 args_out_of_range_3 (object, from, to);
4701 if (EQ (from, to))
4702 return Qnil;
4703 len = XFASTINT (to) - XFASTINT (from);
4704 SAFE_ALLOCA_LISP (chars, len);
4705 p = SDATA (object);
4706 if (STRING_MULTIBYTE (object))
4707 for (i = 0; i < len; i++)
4709 int c = STRING_CHAR_ADVANCE (p);
4710 chars[i] = make_number (c);
4712 else
4713 for (i = 0; i < len; i++)
4714 chars[i] = make_number (p[i]);
4716 else
4718 CHECK_VECTOR (object);
4719 CHECK_NUMBER (from);
4720 CHECK_NUMBER (to);
4721 if (XINT (from) < 0 || XINT (from) > XINT (to)
4722 || XINT (to) > ASIZE (object))
4723 args_out_of_range_3 (object, from, to);
4724 if (EQ (from, to))
4725 return Qnil;
4726 len = XFASTINT (to) - XFASTINT (from);
4727 for (i = 0; i < len; i++)
4729 Lisp_Object elt = AREF (object, XFASTINT (from) + i);
4730 CHECK_CHARACTER (elt);
4732 chars = aref_addr (object, XFASTINT (from));
4735 vec = make_uninit_vector (len);
4736 for (i = 0; i < len; i++)
4738 Lisp_Object g;
4739 int c = XFASTINT (chars[i]);
4740 unsigned code;
4741 struct font_metrics metrics;
4743 code = font->driver->encode_char (font, c);
4744 if (code == FONT_INVALID_CODE)
4746 ASET (vec, i, Qnil);
4747 continue;
4749 g = LGLYPH_NEW ();
4750 LGLYPH_SET_FROM (g, i);
4751 LGLYPH_SET_TO (g, i);
4752 LGLYPH_SET_CHAR (g, c);
4753 LGLYPH_SET_CODE (g, code);
4754 font->driver->text_extents (font, &code, 1, &metrics);
4755 LGLYPH_SET_WIDTH (g, metrics.width);
4756 LGLYPH_SET_LBEARING (g, metrics.lbearing);
4757 LGLYPH_SET_RBEARING (g, metrics.rbearing);
4758 LGLYPH_SET_ASCENT (g, metrics.ascent);
4759 LGLYPH_SET_DESCENT (g, metrics.descent);
4760 ASET (vec, i, g);
4762 if (! VECTORP (object))
4763 SAFE_FREE ();
4764 return vec;
4767 DEFUN ("font-match-p", Ffont_match_p, Sfont_match_p, 2, 2, 0,
4768 doc: /* Return t if and only if font-spec SPEC matches with FONT.
4769 FONT is a font-spec, font-entity, or font-object. */)
4770 (Lisp_Object spec, Lisp_Object font)
4772 CHECK_FONT_SPEC (spec);
4773 CHECK_FONT (font);
4775 return (font_match_p (spec, font) ? Qt : Qnil);
4778 DEFUN ("font-at", Ffont_at, Sfont_at, 1, 3, 0,
4779 doc: /* Return a font-object for displaying a character at POSITION.
4780 Optional second arg WINDOW, if non-nil, is a window displaying
4781 the current buffer. It defaults to the currently selected window.
4782 Optional third arg STRING, if non-nil, is a string containing the target
4783 character at index specified by POSITION. */)
4784 (Lisp_Object position, Lisp_Object window, Lisp_Object string)
4786 struct window *w = decode_live_window (window);
4788 if (NILP (string))
4790 if (XBUFFER (w->contents) != current_buffer)
4791 error ("Specified window is not displaying the current buffer");
4792 CHECK_NUMBER_COERCE_MARKER (position);
4793 if (! (BEGV <= XINT (position) && XINT (position) < ZV))
4794 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
4796 else
4798 CHECK_NUMBER (position);
4799 CHECK_STRING (string);
4800 if (! (0 <= XINT (position) && XINT (position) < SCHARS (string)))
4801 args_out_of_range (string, position);
4804 return font_at (-1, XINT (position), NULL, w, string);
4807 #if 0
4808 DEFUN ("draw-string", Fdraw_string, Sdraw_string, 2, 2, 0,
4809 doc: /* Draw STRING by FONT-OBJECT on the top left corner of the current frame.
4810 The value is a number of glyphs drawn.
4811 Type C-l to recover what previously shown. */)
4812 (Lisp_Object font_object, Lisp_Object string)
4814 Lisp_Object frame = selected_frame;
4815 struct frame *f = XFRAME (frame);
4816 struct font *font;
4817 struct face *face;
4818 int i, len, width;
4819 unsigned *code;
4821 CHECK_FONT_GET_OBJECT (font_object, font);
4822 CHECK_STRING (string);
4823 len = SCHARS (string);
4824 code = alloca (sizeof (unsigned) * len);
4825 for (i = 0; i < len; i++)
4827 Lisp_Object ch = Faref (string, make_number (i));
4828 Lisp_Object val;
4829 int c = XINT (ch);
4831 code[i] = font->driver->encode_char (font, c);
4832 if (code[i] == FONT_INVALID_CODE)
4833 break;
4835 face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4836 face->fontp = font;
4837 if (font->driver->prepare_face)
4838 font->driver->prepare_face (f, face);
4839 width = font->driver->text_extents (font, code, i, NULL);
4840 len = font->driver->draw_text (f, face, 0, font->ascent, code, i, width);
4841 if (font->driver->done_face)
4842 font->driver->done_face (f, face);
4843 face->fontp = NULL;
4844 return make_number (len);
4846 #endif
4848 DEFUN ("frame-font-cache", Fframe_font_cache, Sframe_font_cache, 0, 1, 0,
4849 doc: /* Return FRAME's font cache. Mainly used for debugging.
4850 If FRAME is omitted or nil, use the selected frame. */)
4851 (Lisp_Object frame)
4853 #ifdef HAVE_WINDOW_SYSTEM
4854 struct frame *f = decode_live_frame (frame);
4856 if (FRAME_WINDOW_P (f))
4857 return FRAME_DISPLAY_INFO (f)->name_list_element;
4858 else
4859 #endif
4860 return Qnil;
4863 #endif /* FONT_DEBUG */
4865 #ifdef HAVE_WINDOW_SYSTEM
4867 DEFUN ("font-info", Ffont_info, Sfont_info, 1, 2, 0,
4868 doc: /* Return information about a font named NAME on frame FRAME.
4869 If FRAME is omitted or nil, use the selected frame.
4870 The returned value is a vector of OPENED-NAME, FULL-NAME, SIZE,
4871 HEIGHT, BASELINE-OFFSET, RELATIVE-COMPOSE, and DEFAULT-ASCENT,
4872 where
4873 OPENED-NAME is the name used for opening the font,
4874 FULL-NAME is the full name of the font,
4875 SIZE is the pixelsize of the font,
4876 HEIGHT is the pixel-height of the font (i.e., ascent + descent),
4877 BASELINE-OFFSET is the upward offset pixels from ASCII baseline,
4878 RELATIVE-COMPOSE and DEFAULT-ASCENT are the numbers controlling
4879 how to compose characters.
4880 If the named font is not yet loaded, return nil. */)
4881 (Lisp_Object name, Lisp_Object frame)
4883 struct frame *f;
4884 struct font *font;
4885 Lisp_Object info;
4886 Lisp_Object font_object;
4888 if (! FONTP (name))
4889 CHECK_STRING (name);
4890 f = decode_window_system_frame (frame);
4892 if (STRINGP (name))
4894 int fontset = fs_query_fontset (name, 0);
4896 if (fontset >= 0)
4897 name = fontset_ascii (fontset);
4898 font_object = font_open_by_name (f, name);
4900 else if (FONT_OBJECT_P (name))
4901 font_object = name;
4902 else if (FONT_ENTITY_P (name))
4903 font_object = font_open_entity (f, name, 0);
4904 else
4906 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4907 Lisp_Object entity = font_matching_entity (f, face->lface, name);
4909 font_object = ! NILP (entity) ? font_open_entity (f, entity, 0) : Qnil;
4911 if (NILP (font_object))
4912 return Qnil;
4913 font = XFONT_OBJECT (font_object);
4915 info = make_uninit_vector (7);
4916 ASET (info, 0, AREF (font_object, FONT_NAME_INDEX));
4917 ASET (info, 1, AREF (font_object, FONT_FULLNAME_INDEX));
4918 ASET (info, 2, make_number (font->pixel_size));
4919 ASET (info, 3, make_number (font->height));
4920 ASET (info, 4, make_number (font->baseline_offset));
4921 ASET (info, 5, make_number (font->relative_compose));
4922 ASET (info, 6, make_number (font->default_ascent));
4924 #if 0
4925 /* As font_object is still in FONT_OBJLIST of the entity, we can't
4926 close it now. Perhaps, we should manage font-objects
4927 by `reference-count'. */
4928 font_close_object (f, font_object);
4929 #endif
4930 return info;
4932 #endif
4935 #define BUILD_STYLE_TABLE(TBL) \
4936 build_style_table ((TBL), sizeof TBL / sizeof (struct table_entry))
4938 static Lisp_Object
4939 build_style_table (const struct table_entry *entry, int nelement)
4941 int i, j;
4942 Lisp_Object table, elt;
4944 table = make_uninit_vector (nelement);
4945 for (i = 0; i < nelement; i++)
4947 for (j = 0; entry[i].names[j]; j++);
4948 elt = Fmake_vector (make_number (j + 1), Qnil);
4949 ASET (elt, 0, make_number (entry[i].numeric));
4950 for (j = 0; entry[i].names[j]; j++)
4951 ASET (elt, j + 1, intern_c_string (entry[i].names[j]));
4952 ASET (table, i, elt);
4954 return table;
4957 /* The deferred font-log data of the form [ACTION ARG RESULT].
4958 If ACTION is not nil, that is added to the log when font_add_log is
4959 called next time. At that time, ACTION is set back to nil. */
4960 static Lisp_Object Vfont_log_deferred;
4962 /* Prepend the font-related logging data in Vfont_log if it is not
4963 `t'. ACTION describes a kind of font-related action (e.g. listing,
4964 opening), ARG is the argument for the action, and RESULT is the
4965 result of the action. */
4966 void
4967 font_add_log (const char *action, Lisp_Object arg, Lisp_Object result)
4969 Lisp_Object val;
4970 int i;
4972 if (EQ (Vfont_log, Qt))
4973 return;
4974 if (STRINGP (AREF (Vfont_log_deferred, 0)))
4976 char *str = SSDATA (AREF (Vfont_log_deferred, 0));
4978 ASET (Vfont_log_deferred, 0, Qnil);
4979 font_add_log (str, AREF (Vfont_log_deferred, 1),
4980 AREF (Vfont_log_deferred, 2));
4983 if (FONTP (arg))
4985 Lisp_Object tail, elt;
4986 Lisp_Object equalstr = build_string ("=");
4988 val = Ffont_xlfd_name (arg, Qt);
4989 for (tail = AREF (arg, FONT_EXTRA_INDEX); CONSP (tail);
4990 tail = XCDR (tail))
4992 elt = XCAR (tail);
4993 if (EQ (XCAR (elt), QCscript)
4994 && SYMBOLP (XCDR (elt)))
4995 val = concat3 (val, SYMBOL_NAME (QCscript),
4996 concat2 (equalstr, SYMBOL_NAME (XCDR (elt))));
4997 else if (EQ (XCAR (elt), QClang)
4998 && SYMBOLP (XCDR (elt)))
4999 val = concat3 (val, SYMBOL_NAME (QClang),
5000 concat2 (equalstr, SYMBOL_NAME (XCDR (elt))));
5001 else if (EQ (XCAR (elt), QCotf)
5002 && CONSP (XCDR (elt)) && SYMBOLP (XCAR (XCDR (elt))))
5003 val = concat3 (val, SYMBOL_NAME (QCotf),
5004 concat2 (equalstr,
5005 SYMBOL_NAME (XCAR (XCDR (elt)))));
5007 arg = val;
5010 if (CONSP (result)
5011 && VECTORP (XCAR (result))
5012 && ASIZE (XCAR (result)) > 0
5013 && FONTP (AREF (XCAR (result), 0)))
5014 result = font_vconcat_entity_vectors (result);
5015 if (FONTP (result))
5017 val = Ffont_xlfd_name (result, Qt);
5018 if (! FONT_SPEC_P (result))
5019 val = concat3 (SYMBOL_NAME (AREF (result, FONT_TYPE_INDEX)),
5020 build_string (":"), val);
5021 result = val;
5023 else if (CONSP (result))
5025 Lisp_Object tail;
5026 result = Fcopy_sequence (result);
5027 for (tail = result; CONSP (tail); tail = XCDR (tail))
5029 val = XCAR (tail);
5030 if (FONTP (val))
5031 val = Ffont_xlfd_name (val, Qt);
5032 XSETCAR (tail, val);
5035 else if (VECTORP (result))
5037 result = Fcopy_sequence (result);
5038 for (i = 0; i < ASIZE (result); i++)
5040 val = AREF (result, i);
5041 if (FONTP (val))
5042 val = Ffont_xlfd_name (val, Qt);
5043 ASET (result, i, val);
5046 Vfont_log = Fcons (list3 (intern (action), arg, result), Vfont_log);
5049 /* Record a font-related logging data to be added to Vfont_log when
5050 font_add_log is called next time. ACTION, ARG, RESULT are the same
5051 as font_add_log. */
5053 void
5054 font_deferred_log (const char *action, Lisp_Object arg, Lisp_Object result)
5056 if (EQ (Vfont_log, Qt))
5057 return;
5058 ASET (Vfont_log_deferred, 0, build_string (action));
5059 ASET (Vfont_log_deferred, 1, arg);
5060 ASET (Vfont_log_deferred, 2, result);
5063 void
5064 syms_of_font (void)
5066 sort_shift_bits[FONT_TYPE_INDEX] = 0;
5067 sort_shift_bits[FONT_SLANT_INDEX] = 2;
5068 sort_shift_bits[FONT_WEIGHT_INDEX] = 9;
5069 sort_shift_bits[FONT_SIZE_INDEX] = 16;
5070 sort_shift_bits[FONT_WIDTH_INDEX] = 23;
5071 /* Note that the other elements in sort_shift_bits are not used. */
5073 staticpro (&font_charset_alist);
5074 font_charset_alist = Qnil;
5076 DEFSYM (Qopentype, "opentype");
5078 DEFSYM (Qascii_0, "ascii-0");
5079 DEFSYM (Qiso8859_1, "iso8859-1");
5080 DEFSYM (Qiso10646_1, "iso10646-1");
5081 DEFSYM (Qunicode_bmp, "unicode-bmp");
5082 DEFSYM (Qunicode_sip, "unicode-sip");
5084 DEFSYM (QCf, "Cf");
5086 DEFSYM (QCotf, ":otf");
5087 DEFSYM (QClang, ":lang");
5088 DEFSYM (QCscript, ":script");
5089 DEFSYM (QCantialias, ":antialias");
5091 DEFSYM (QCfoundry, ":foundry");
5092 DEFSYM (QCadstyle, ":adstyle");
5093 DEFSYM (QCregistry, ":registry");
5094 DEFSYM (QCspacing, ":spacing");
5095 DEFSYM (QCdpi, ":dpi");
5096 DEFSYM (QCscalable, ":scalable");
5097 DEFSYM (QCavgwidth, ":avgwidth");
5098 DEFSYM (QCfont_entity, ":font-entity");
5099 DEFSYM (QCfc_unknown_spec, ":fc-unknown-spec");
5101 DEFSYM (Qc, "c");
5102 DEFSYM (Qm, "m");
5103 DEFSYM (Qp, "p");
5104 DEFSYM (Qd, "d");
5106 DEFSYM (Qja, "ja");
5107 DEFSYM (Qko, "ko");
5109 DEFSYM (QCuser_spec, "user-spec");
5111 staticpro (&scratch_font_spec);
5112 scratch_font_spec = Ffont_spec (0, NULL);
5113 staticpro (&scratch_font_prefer);
5114 scratch_font_prefer = Ffont_spec (0, NULL);
5116 staticpro (&Vfont_log_deferred);
5117 Vfont_log_deferred = Fmake_vector (make_number (3), Qnil);
5119 #if 0
5120 #ifdef HAVE_LIBOTF
5121 staticpro (&otf_list);
5122 otf_list = Qnil;
5123 #endif /* HAVE_LIBOTF */
5124 #endif /* 0 */
5126 defsubr (&Sfontp);
5127 defsubr (&Sfont_spec);
5128 defsubr (&Sfont_get);
5129 #ifdef HAVE_WINDOW_SYSTEM
5130 defsubr (&Sfont_face_attributes);
5131 #endif
5132 defsubr (&Sfont_put);
5133 defsubr (&Slist_fonts);
5134 defsubr (&Sfont_family_list);
5135 defsubr (&Sfind_font);
5136 defsubr (&Sfont_xlfd_name);
5137 defsubr (&Sclear_font_cache);
5138 defsubr (&Sfont_shape_gstring);
5139 defsubr (&Sfont_variation_glyphs);
5140 #if 0
5141 defsubr (&Sfont_drive_otf);
5142 defsubr (&Sfont_otf_alternates);
5143 #endif /* 0 */
5145 #ifdef FONT_DEBUG
5146 defsubr (&Sopen_font);
5147 defsubr (&Sclose_font);
5148 defsubr (&Squery_font);
5149 defsubr (&Sfont_get_glyphs);
5150 defsubr (&Sfont_match_p);
5151 defsubr (&Sfont_at);
5152 #if 0
5153 defsubr (&Sdraw_string);
5154 #endif
5155 defsubr (&Sframe_font_cache);
5156 #endif /* FONT_DEBUG */
5157 #ifdef HAVE_WINDOW_SYSTEM
5158 defsubr (&Sfont_info);
5159 #endif
5161 DEFVAR_LISP ("font-encoding-alist", Vfont_encoding_alist,
5162 doc: /*
5163 Alist of fontname patterns vs the corresponding encoding and repertory info.
5164 Each element looks like (REGEXP . (ENCODING . REPERTORY)),
5165 where ENCODING is a charset or a char-table,
5166 and REPERTORY is a charset, a char-table, or nil.
5168 If ENCODING and REPERTORY are the same, the element can have the form
5169 \(REGEXP . ENCODING).
5171 ENCODING is for converting a character to a glyph code of the font.
5172 If ENCODING is a charset, encoding a character by the charset gives
5173 the corresponding glyph code. If ENCODING is a char-table, looking up
5174 the table by a character gives the corresponding glyph code.
5176 REPERTORY specifies a repertory of characters supported by the font.
5177 If REPERTORY is a charset, all characters belonging to the charset are
5178 supported. If REPERTORY is a char-table, all characters who have a
5179 non-nil value in the table are supported. If REPERTORY is nil, Emacs
5180 gets the repertory information by an opened font and ENCODING. */);
5181 Vfont_encoding_alist = Qnil;
5183 /* FIXME: These 3 vars are not quite what they appear: setq on them
5184 won't have any effect other than disconnect them from the style
5185 table used by the font display code. So we make them read-only,
5186 to avoid this confusing situation. */
5188 DEFVAR_LISP_NOPRO ("font-weight-table", Vfont_weight_table,
5189 doc: /* Vector of valid font weight values.
5190 Each element has the form:
5191 [NUMERIC-VALUE SYMBOLIC-NAME ALIAS-NAME ...]
5192 NUMERIC-VALUE is an integer, and SYMBOLIC-NAME and ALIAS-NAME are symbols. */);
5193 Vfont_weight_table = BUILD_STYLE_TABLE (weight_table);
5194 XSYMBOL (intern_c_string ("font-weight-table"))->constant = 1;
5196 DEFVAR_LISP_NOPRO ("font-slant-table", Vfont_slant_table,
5197 doc: /* Vector of font slant symbols vs the corresponding numeric values.
5198 See `font-weight-table' for the format of the vector. */);
5199 Vfont_slant_table = BUILD_STYLE_TABLE (slant_table);
5200 XSYMBOL (intern_c_string ("font-slant-table"))->constant = 1;
5202 DEFVAR_LISP_NOPRO ("font-width-table", Vfont_width_table,
5203 doc: /* Alist of font width symbols vs the corresponding numeric values.
5204 See `font-weight-table' for the format of the vector. */);
5205 Vfont_width_table = BUILD_STYLE_TABLE (width_table);
5206 XSYMBOL (intern_c_string ("font-width-table"))->constant = 1;
5208 staticpro (&font_style_table);
5209 font_style_table = make_uninit_vector (3);
5210 ASET (font_style_table, 0, Vfont_weight_table);
5211 ASET (font_style_table, 1, Vfont_slant_table);
5212 ASET (font_style_table, 2, Vfont_width_table);
5214 DEFVAR_LISP ("font-log", Vfont_log, doc: /*
5215 *Logging list of font related actions and results.
5216 The value t means to suppress the logging.
5217 The initial value is set to nil if the environment variable
5218 EMACS_FONT_LOG is set. Otherwise, it is set to t. */);
5219 Vfont_log = Qnil;
5221 #ifdef HAVE_WINDOW_SYSTEM
5222 #ifdef HAVE_FREETYPE
5223 syms_of_ftfont ();
5224 #ifdef HAVE_X_WINDOWS
5225 syms_of_xfont ();
5226 syms_of_ftxfont ();
5227 #ifdef HAVE_XFT
5228 syms_of_xftfont ();
5229 #endif /* HAVE_XFT */
5230 #endif /* HAVE_X_WINDOWS */
5231 #else /* not HAVE_FREETYPE */
5232 #ifdef HAVE_X_WINDOWS
5233 syms_of_xfont ();
5234 #endif /* HAVE_X_WINDOWS */
5235 #endif /* not HAVE_FREETYPE */
5236 #ifdef HAVE_BDFFONT
5237 syms_of_bdffont ();
5238 #endif /* HAVE_BDFFONT */
5239 #ifdef HAVE_NTGUI
5240 syms_of_w32font ();
5241 #endif /* HAVE_NTGUI */
5242 #endif /* HAVE_WINDOW_SYSTEM */
5245 void
5246 init_font (void)
5248 Vfont_log = egetenv ("EMACS_FONT_LOG") ? Qnil : Qt;