;Fix ChangeLog entry
[emacs.git] / src / font.c
blob039493bcbea8b762cb4bafc24d05bce1b7830a57
1 /* font.c -- "Font" primitives.
3 Copyright (C) 2006-2016 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"
39 #include "termhooks.h"
41 #ifdef HAVE_WINDOW_SYSTEM
42 #include TERM_HEADER
43 #endif /* HAVE_WINDOW_SYSTEM */
45 #define DEFAULT_ENCODING Qiso8859_1
47 /* Vector of Vfont_weight_table, Vfont_slant_table, and Vfont_width_table. */
48 static Lisp_Object font_style_table;
50 /* Structure used for tables mapping weight, slant, and width numeric
51 values and their names. */
53 struct table_entry
55 int numeric;
56 /* The first one is a valid name as a face attribute.
57 The second one (if any) is a typical name in XLFD field. */
58 const char *names[5];
61 /* Table of weight numeric values and their names. This table must be
62 sorted by numeric values in ascending order. */
64 static const struct table_entry weight_table[] =
66 { 0, { "thin" }},
67 { 20, { "ultra-light", "ultralight" }},
68 { 40, { "extra-light", "extralight" }},
69 { 50, { "light" }},
70 { 75, { "semi-light", "semilight", "demilight", "book" }},
71 { 100, { "normal", "medium", "regular", "unspecified" }},
72 { 180, { "semi-bold", "semibold", "demibold", "demi" }},
73 { 200, { "bold" }},
74 { 205, { "extra-bold", "extrabold" }},
75 { 210, { "ultra-bold", "ultrabold", "black" }}
78 /* Table of slant numeric values and their names. This table must be
79 sorted by numeric values in ascending order. */
81 static const struct table_entry slant_table[] =
83 { 0, { "reverse-oblique", "ro" }},
84 { 10, { "reverse-italic", "ri" }},
85 { 100, { "normal", "r", "unspecified" }},
86 { 200, { "italic" ,"i", "ot" }},
87 { 210, { "oblique", "o" }}
90 /* Table of width numeric values and their names. This table must be
91 sorted by numeric values in ascending order. */
93 static const struct table_entry width_table[] =
95 { 50, { "ultra-condensed", "ultracondensed" }},
96 { 63, { "extra-condensed", "extracondensed" }},
97 { 75, { "condensed", "compressed", "narrow" }},
98 { 87, { "semi-condensed", "semicondensed", "demicondensed" }},
99 { 100, { "normal", "medium", "regular", "unspecified" }},
100 { 113, { "semi-expanded", "semiexpanded", "demiexpanded" }},
101 { 125, { "expanded" }},
102 { 150, { "extra-expanded", "extraexpanded" }},
103 { 200, { "ultra-expanded", "ultraexpanded", "wide" }}
106 /* Alist of font registry symbols and the corresponding charset
107 information. The information is retrieved from
108 Vfont_encoding_alist on demand.
110 Eash element has the form:
111 (REGISTRY . (ENCODING-CHARSET-ID . REPERTORY-CHARSET-ID))
113 (REGISTRY . nil)
115 In the former form, ENCODING-CHARSET-ID is an ID of a charset that
116 encodes a character code to a glyph code of a font, and
117 REPERTORY-CHARSET-ID is an ID of a charset that tells if a
118 character is supported by a font.
120 The latter form means that the information for REGISTRY couldn't be
121 retrieved. */
122 static Lisp_Object font_charset_alist;
124 /* List of all font drivers. Each font-backend (XXXfont.c) calls
125 register_font_driver in syms_of_XXXfont to register its font-driver
126 here. */
127 static struct font_driver_list *font_driver_list;
129 #ifdef ENABLE_CHECKING
131 /* Used to catch bogus pointers in font objects. */
133 bool
134 valid_font_driver (struct font_driver *drv)
136 Lisp_Object tail, frame;
137 struct font_driver_list *fdl;
139 for (fdl = font_driver_list; fdl; fdl = fdl->next)
140 if (fdl->driver == drv)
141 return true;
142 FOR_EACH_FRAME (tail, frame)
143 for (fdl = XFRAME (frame)->font_driver_list; fdl; fdl = fdl->next)
144 if (fdl->driver == drv)
145 return true;
146 return false;
149 #endif /* ENABLE_CHECKING */
151 /* Creators of font-related Lisp object. */
153 static Lisp_Object
154 font_make_spec (void)
156 Lisp_Object font_spec;
157 struct font_spec *spec
158 = ((struct font_spec *)
159 allocate_pseudovector (VECSIZE (struct font_spec),
160 FONT_SPEC_MAX, FONT_SPEC_MAX, PVEC_FONT));
161 XSETFONT (font_spec, spec);
162 return font_spec;
165 Lisp_Object
166 font_make_entity (void)
168 Lisp_Object font_entity;
169 struct font_entity *entity
170 = ((struct font_entity *)
171 allocate_pseudovector (VECSIZE (struct font_entity),
172 FONT_ENTITY_MAX, FONT_ENTITY_MAX, PVEC_FONT));
173 XSETFONT (font_entity, entity);
174 return font_entity;
177 /* Create a font-object whose structure size is SIZE. If ENTITY is
178 not nil, copy properties from ENTITY to the font-object. If
179 PIXELSIZE is positive, set the `size' property to PIXELSIZE. */
180 Lisp_Object
181 font_make_object (int size, Lisp_Object entity, int pixelsize)
183 Lisp_Object font_object;
184 struct font *font
185 = (struct font *) allocate_pseudovector (size, FONT_OBJECT_MAX,
186 FONT_OBJECT_MAX, PVEC_FONT);
187 int i;
189 /* GC can happen before the driver is set up,
190 so avoid dangling pointer here (Bug#17771). */
191 font->driver = NULL;
192 XSETFONT (font_object, font);
194 if (! NILP (entity))
196 for (i = 1; i < FONT_SPEC_MAX; i++)
197 font->props[i] = AREF (entity, i);
198 if (! NILP (AREF (entity, FONT_EXTRA_INDEX)))
199 font->props[FONT_EXTRA_INDEX]
200 = Fcopy_alist (AREF (entity, FONT_EXTRA_INDEX));
202 if (size > 0)
203 font->props[FONT_SIZE_INDEX] = make_number (pixelsize);
204 return font_object;
207 #if defined (HAVE_XFT) || defined (HAVE_FREETYPE) || defined (HAVE_NS)
209 static int font_unparse_fcname (Lisp_Object, int, char *, int);
211 /* Like above, but also set `type', `name' and `fullname' properties
212 of font-object. */
214 Lisp_Object
215 font_build_object (int vectorsize, Lisp_Object type,
216 Lisp_Object entity, double pixelsize)
218 int len;
219 char name[256];
220 Lisp_Object font_object = font_make_object (vectorsize, entity, pixelsize);
222 ASET (font_object, FONT_TYPE_INDEX, type);
223 len = font_unparse_xlfd (entity, pixelsize, name, sizeof name);
224 if (len > 0)
225 ASET (font_object, FONT_NAME_INDEX, make_string (name, len));
226 len = font_unparse_fcname (entity, pixelsize, name, sizeof name);
227 if (len > 0)
228 ASET (font_object, FONT_FULLNAME_INDEX, make_string (name, len));
229 else
230 ASET (font_object, FONT_FULLNAME_INDEX,
231 AREF (font_object, FONT_NAME_INDEX));
232 return font_object;
235 #endif /* HAVE_XFT || HAVE_FREETYPE || HAVE_NS */
237 static int font_pixel_size (struct frame *f, Lisp_Object);
238 static Lisp_Object font_open_entity (struct frame *, Lisp_Object, int);
239 static Lisp_Object font_matching_entity (struct frame *, Lisp_Object *,
240 Lisp_Object);
241 static unsigned font_encode_char (Lisp_Object, int);
243 /* Number of registered font drivers. */
244 static int num_font_drivers;
247 /* Return a Lispy value of a font property value at STR and LEN bytes.
248 If STR is "*", return nil. If FORCE_SYMBOL, or if STR does not
249 consist entirely of one or more digits, return a symbol interned
250 from STR. Otherwise, return an integer. */
252 Lisp_Object
253 font_intern_prop (const char *str, ptrdiff_t len, bool force_symbol)
255 ptrdiff_t i, nbytes, nchars;
256 Lisp_Object tem, name, obarray;
258 if (len == 1 && *str == '*')
259 return Qnil;
260 if (!force_symbol && 0 < len && '0' <= *str && *str <= '9')
262 for (i = 1; i < len; i++)
263 if (! ('0' <= str[i] && str[i] <= '9'))
264 break;
265 if (i == len)
267 EMACS_INT n;
269 i = 0;
270 for (n = 0; (n += str[i++] - '0') <= MOST_POSITIVE_FIXNUM; n *= 10)
272 if (i == len)
273 return make_number (n);
274 if (MOST_POSITIVE_FIXNUM / 10 < n)
275 break;
278 xsignal1 (Qoverflow_error, make_string (str, len));
282 /* This code is similar to intern function from lread.c. */
283 obarray = check_obarray (Vobarray);
284 parse_str_as_multibyte ((unsigned char *) str, len, &nchars, &nbytes);
285 tem = oblookup (obarray, str,
286 (len == nchars || len != nbytes) ? len : nchars, len);
287 if (SYMBOLP (tem))
288 return tem;
289 name = make_specified_string (str, nchars, len,
290 len != nchars && len == nbytes);
291 return intern_driver (name, obarray, tem);
294 /* Return a pixel size of font-spec SPEC on frame F. */
296 static int
297 font_pixel_size (struct frame *f, Lisp_Object spec)
299 #ifdef HAVE_WINDOW_SYSTEM
300 Lisp_Object size = AREF (spec, FONT_SIZE_INDEX);
301 double point_size;
302 int dpi, pixel_size;
303 Lisp_Object val;
305 if (INTEGERP (size))
306 return XINT (size);
307 if (NILP (size))
308 return 0;
309 eassert (FLOATP (size));
310 point_size = XFLOAT_DATA (size);
311 val = AREF (spec, FONT_DPI_INDEX);
312 if (INTEGERP (val))
313 dpi = XINT (val);
314 else
315 dpi = FRAME_RES_Y (f);
316 pixel_size = POINT_TO_PIXEL (point_size, dpi);
317 return pixel_size;
318 #else
319 return 1;
320 #endif
324 /* Return a value of PROP's VAL (symbol or integer) to be stored in a
325 font vector. If VAL is not valid (i.e. not registered in
326 font_style_table), return -1 if NOERROR is zero, and return a
327 proper index if NOERROR is nonzero. In that case, register VAL in
328 font_style_table if VAL is a symbol, and return the closest index if
329 VAL is an integer. */
332 font_style_to_value (enum font_property_index prop, Lisp_Object val,
333 bool noerror)
335 Lisp_Object table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
336 int len;
338 CHECK_VECTOR (table);
339 len = ASIZE (table);
341 if (SYMBOLP (val))
343 int i, j;
344 char *s;
345 Lisp_Object elt;
347 /* At first try exact match. */
348 for (i = 0; i < len; i++)
350 CHECK_VECTOR (AREF (table, i));
351 for (j = 1; j < ASIZE (AREF (table, i)); j++)
352 if (EQ (val, AREF (AREF (table, i), j)))
354 CHECK_NUMBER (AREF (AREF (table, i), 0));
355 return ((XINT (AREF (AREF (table, i), 0)) << 8)
356 | (i << 4) | (j - 1));
359 /* Try also with case-folding match. */
360 s = SSDATA (SYMBOL_NAME (val));
361 for (i = 0; i < len; i++)
362 for (j = 1; j < ASIZE (AREF (table, i)); j++)
364 elt = AREF (AREF (table, i), j);
365 if (xstrcasecmp (s, SSDATA (SYMBOL_NAME (elt))) == 0)
367 CHECK_NUMBER (AREF (AREF (table, i), 0));
368 return ((XINT (AREF (AREF (table, i), 0)) << 8)
369 | (i << 4) | (j - 1));
372 if (! noerror)
373 return -1;
374 eassert (len < 255);
375 elt = Fmake_vector (make_number (2), make_number (100));
376 ASET (elt, 1, val);
377 ASET (font_style_table, prop - FONT_WEIGHT_INDEX,
378 CALLN (Fvconcat, table, Fmake_vector (make_number (1), elt)));
379 return (100 << 8) | (i << 4);
381 else
383 int i, last_n;
384 EMACS_INT numeric = XINT (val);
386 for (i = 0, last_n = -1; i < len; i++)
388 int n;
390 CHECK_VECTOR (AREF (table, i));
391 CHECK_NUMBER (AREF (AREF (table, i), 0));
392 n = XINT (AREF (AREF (table, i), 0));
393 if (numeric == n)
394 return (n << 8) | (i << 4);
395 if (numeric < n)
397 if (! noerror)
398 return -1;
399 return ((i == 0 || n - numeric < numeric - last_n)
400 ? (n << 8) | (i << 4): (last_n << 8 | ((i - 1) << 4)));
402 last_n = n;
404 if (! noerror)
405 return -1;
406 return ((last_n << 8) | ((i - 1) << 4));
410 Lisp_Object
411 font_style_symbolic (Lisp_Object font, enum font_property_index prop,
412 bool for_face)
414 Lisp_Object val = AREF (font, prop);
415 Lisp_Object table, elt;
416 int i;
418 if (NILP (val))
419 return Qnil;
420 table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
421 CHECK_VECTOR (table);
422 i = XINT (val) & 0xFF;
423 eassert (((i >> 4) & 0xF) < ASIZE (table));
424 elt = AREF (table, ((i >> 4) & 0xF));
425 CHECK_VECTOR (elt);
426 eassert ((i & 0xF) + 1 < ASIZE (elt));
427 elt = (for_face ? AREF (elt, 1) : AREF (elt, (i & 0xF) + 1));
428 CHECK_SYMBOL (elt);
429 return elt;
432 /* Return ENCODING or a cons of ENCODING and REPERTORY of the font
433 FONTNAME. ENCODING is a charset symbol that specifies the encoding
434 of the font. REPERTORY is a charset symbol or nil. */
436 Lisp_Object
437 find_font_encoding (Lisp_Object fontname)
439 Lisp_Object tail, elt;
441 for (tail = Vfont_encoding_alist; CONSP (tail); tail = XCDR (tail))
443 elt = XCAR (tail);
444 if (CONSP (elt)
445 && STRINGP (XCAR (elt))
446 && fast_string_match_ignore_case (XCAR (elt), fontname) >= 0
447 && (SYMBOLP (XCDR (elt))
448 ? CHARSETP (XCDR (elt))
449 : CONSP (XCDR (elt)) && CHARSETP (XCAR (XCDR (elt)))))
450 return (XCDR (elt));
452 return Qnil;
455 /* Return encoding charset and repertory charset for REGISTRY in
456 ENCODING and REPERTORY correspondingly. If correct information for
457 REGISTRY is available, return 0. Otherwise return -1. */
460 font_registry_charsets (Lisp_Object registry, struct charset **encoding, struct charset **repertory)
462 Lisp_Object val;
463 int encoding_id, repertory_id;
465 val = Fassoc_string (registry, font_charset_alist, Qt);
466 if (! NILP (val))
468 val = XCDR (val);
469 if (NILP (val))
470 return -1;
471 encoding_id = XINT (XCAR (val));
472 repertory_id = XINT (XCDR (val));
474 else
476 val = find_font_encoding (SYMBOL_NAME (registry));
477 if (SYMBOLP (val) && CHARSETP (val))
479 encoding_id = repertory_id = XINT (CHARSET_SYMBOL_ID (val));
481 else if (CONSP (val))
483 if (! CHARSETP (XCAR (val)))
484 goto invalid_entry;
485 encoding_id = XINT (CHARSET_SYMBOL_ID (XCAR (val)));
486 if (NILP (XCDR (val)))
487 repertory_id = -1;
488 else
490 if (! CHARSETP (XCDR (val)))
491 goto invalid_entry;
492 repertory_id = XINT (CHARSET_SYMBOL_ID (XCDR (val)));
495 else
496 goto invalid_entry;
497 val = Fcons (make_number (encoding_id), make_number (repertory_id));
498 font_charset_alist
499 = nconc2 (font_charset_alist, list1 (Fcons (registry, val)));
502 if (encoding)
503 *encoding = CHARSET_FROM_ID (encoding_id);
504 if (repertory)
505 *repertory = repertory_id >= 0 ? CHARSET_FROM_ID (repertory_id) : NULL;
506 return 0;
508 invalid_entry:
509 font_charset_alist
510 = nconc2 (font_charset_alist, list1 (Fcons (registry, Qnil)));
511 return -1;
515 /* Font property value validators. See the comment of
516 font_property_table for the meaning of the arguments. */
518 static Lisp_Object font_prop_validate (int, Lisp_Object, Lisp_Object);
519 static Lisp_Object font_prop_validate_symbol (Lisp_Object, Lisp_Object);
520 static Lisp_Object font_prop_validate_style (Lisp_Object, Lisp_Object);
521 static Lisp_Object font_prop_validate_non_neg (Lisp_Object, Lisp_Object);
522 static Lisp_Object font_prop_validate_spacing (Lisp_Object, Lisp_Object);
523 static int get_font_prop_index (Lisp_Object);
525 static Lisp_Object
526 font_prop_validate_symbol (Lisp_Object prop, Lisp_Object val)
528 if (STRINGP (val))
529 val = Fintern (val, Qnil);
530 if (! SYMBOLP (val))
531 val = Qerror;
532 else if (EQ (prop, QCregistry))
533 val = Fintern (Fdowncase (SYMBOL_NAME (val)), Qnil);
534 return val;
538 static Lisp_Object
539 font_prop_validate_style (Lisp_Object style, Lisp_Object val)
541 enum font_property_index prop = (EQ (style, QCweight) ? FONT_WEIGHT_INDEX
542 : EQ (style, QCslant) ? FONT_SLANT_INDEX
543 : FONT_WIDTH_INDEX);
544 if (INTEGERP (val))
546 EMACS_INT n = XINT (val);
547 CHECK_VECTOR (AREF (font_style_table, prop - FONT_WEIGHT_INDEX));
548 if (((n >> 4) & 0xF)
549 >= ASIZE (AREF (font_style_table, prop - FONT_WEIGHT_INDEX)))
550 val = Qerror;
551 else
553 Lisp_Object elt = AREF (AREF (font_style_table, prop - FONT_WEIGHT_INDEX), (n >> 4) & 0xF);
555 CHECK_VECTOR (elt);
556 if ((n & 0xF) + 1 >= ASIZE (elt))
557 val = Qerror;
558 else
560 CHECK_NUMBER (AREF (elt, 0));
561 if (XINT (AREF (elt, 0)) != (n >> 8))
562 val = Qerror;
566 else if (SYMBOLP (val))
568 int n = font_style_to_value (prop, val, 0);
570 val = n >= 0 ? make_number (n) : Qerror;
572 else
573 val = Qerror;
574 return val;
577 static Lisp_Object
578 font_prop_validate_non_neg (Lisp_Object prop, Lisp_Object val)
580 return (NATNUMP (val) || (FLOATP (val) && XFLOAT_DATA (val) >= 0)
581 ? val : Qerror);
584 static Lisp_Object
585 font_prop_validate_spacing (Lisp_Object prop, Lisp_Object val)
587 if (NILP (val) || (NATNUMP (val) && XINT (val) <= FONT_SPACING_CHARCELL))
588 return val;
589 if (SYMBOLP (val) && SBYTES (SYMBOL_NAME (val)) == 1)
591 char spacing = SDATA (SYMBOL_NAME (val))[0];
593 if (spacing == 'c' || spacing == 'C')
594 return make_number (FONT_SPACING_CHARCELL);
595 if (spacing == 'm' || spacing == 'M')
596 return make_number (FONT_SPACING_MONO);
597 if (spacing == 'p' || spacing == 'P')
598 return make_number (FONT_SPACING_PROPORTIONAL);
599 if (spacing == 'd' || spacing == 'D')
600 return make_number (FONT_SPACING_DUAL);
602 return Qerror;
605 static Lisp_Object
606 font_prop_validate_otf (Lisp_Object prop, Lisp_Object val)
608 Lisp_Object tail, tmp;
609 int i;
611 /* VAL = (SCRIPT [ LANGSYS [ GSUB-FEATURES [ GPOS-FEATURES ]]])
612 GSUB-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil
613 GPOS-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil */
614 if (! CONSP (val))
615 return Qerror;
616 if (! SYMBOLP (XCAR (val)))
617 return Qerror;
618 tail = XCDR (val);
619 if (NILP (tail))
620 return val;
621 if (! CONSP (tail) || ! SYMBOLP (XCAR (val)))
622 return Qerror;
623 for (i = 0; i < 2; i++)
625 tail = XCDR (tail);
626 if (NILP (tail))
627 return val;
628 if (! CONSP (tail))
629 return Qerror;
630 for (tmp = XCAR (tail); CONSP (tmp); tmp = XCDR (tmp))
631 if (! SYMBOLP (XCAR (tmp)))
632 return Qerror;
633 if (! NILP (tmp))
634 return Qerror;
636 return val;
639 /* Structure of known font property keys and validator of the
640 values. */
641 static const struct
643 /* Index of the key symbol. */
644 int key;
645 /* Function to validate PROP's value VAL, or NULL if any value is
646 ok. The value is VAL or its regularized value if VAL is valid,
647 and Qerror if not. */
648 Lisp_Object (*validator) (Lisp_Object prop, Lisp_Object val);
649 } font_property_table[] =
650 { { SYMBOL_INDEX (QCtype), font_prop_validate_symbol },
651 { SYMBOL_INDEX (QCfoundry), font_prop_validate_symbol },
652 { SYMBOL_INDEX (QCfamily), font_prop_validate_symbol },
653 { SYMBOL_INDEX (QCadstyle), font_prop_validate_symbol },
654 { SYMBOL_INDEX (QCregistry), font_prop_validate_symbol },
655 { SYMBOL_INDEX (QCweight), font_prop_validate_style },
656 { SYMBOL_INDEX (QCslant), font_prop_validate_style },
657 { SYMBOL_INDEX (QCwidth), font_prop_validate_style },
658 { SYMBOL_INDEX (QCsize), font_prop_validate_non_neg },
659 { SYMBOL_INDEX (QCdpi), font_prop_validate_non_neg },
660 { SYMBOL_INDEX (QCspacing), font_prop_validate_spacing },
661 { SYMBOL_INDEX (QCavgwidth), font_prop_validate_non_neg },
662 /* The order of the above entries must match with enum
663 font_property_index. */
664 { SYMBOL_INDEX (QClang), font_prop_validate_symbol },
665 { SYMBOL_INDEX (QCscript), font_prop_validate_symbol },
666 { SYMBOL_INDEX (QCotf), font_prop_validate_otf }
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 < ARRAYELTS (font_property_table); i++)
678 if (EQ (key, builtin_lisp_symbol (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 = builtin_lisp_symbol (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 memclear (field + j, (range[i].from - j) * word_size);
993 j = range[i].from;
995 field[j++] = tmp[i];
997 if (! NILP (tmp[n - 1]) && j < XLFD_REGISTRY_INDEX)
998 return -1;
999 memclear (field + j, (XLFD_LAST_INDEX - j) * word_size);
1000 if (INTEGERP (field[XLFD_ENCODING_INDEX]))
1001 field[XLFD_ENCODING_INDEX]
1002 = Fintern (Fnumber_to_string (field[XLFD_ENCODING_INDEX]), Qnil);
1003 return 0;
1007 /* Parse NAME (null terminated) as XLFD and store information in FONT
1008 (font-spec or font-entity). Size property of FONT is set as
1009 follows:
1010 specified XLFD fields FONT property
1011 --------------------- -------------
1012 PIXEL_SIZE PIXEL_SIZE (Lisp integer)
1013 POINT_SIZE and RESY calculated pixel size (Lisp integer)
1014 POINT_SIZE POINT_SIZE/10 (Lisp float)
1016 If NAME is successfully parsed, return 0. Otherwise return -1.
1018 FONT is usually a font-spec, but when this function is called from
1019 X font backend driver, it is a font-entity. In that case, NAME is
1020 a fully specified XLFD. */
1023 font_parse_xlfd (char *name, ptrdiff_t len, Lisp_Object font)
1025 int i, j, n;
1026 char *f[XLFD_LAST_INDEX + 1];
1027 Lisp_Object val;
1028 char *p;
1030 if (len > 255 || !len)
1031 /* Maximum XLFD name length is 255. */
1032 return -1;
1033 /* Accept "*-.." as a fully specified XLFD. */
1034 if (name[0] == '*' && (len == 1 || name[1] == '-'))
1035 i = 1, f[XLFD_FOUNDRY_INDEX] = name;
1036 else
1037 i = 0;
1038 for (p = name + i; *p; p++)
1039 if (*p == '-')
1041 f[i++] = p + 1;
1042 if (i == XLFD_LAST_INDEX)
1043 break;
1045 f[i] = name + len;
1047 #define INTERN_FIELD(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 0)
1048 #define INTERN_FIELD_SYM(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 1)
1050 if (i == XLFD_LAST_INDEX)
1052 /* Fully specified XLFD. */
1053 int pixel_size;
1055 ASET (font, FONT_FOUNDRY_INDEX, INTERN_FIELD_SYM (XLFD_FOUNDRY_INDEX));
1056 ASET (font, FONT_FAMILY_INDEX, INTERN_FIELD_SYM (XLFD_FAMILY_INDEX));
1057 for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
1058 i <= XLFD_SWIDTH_INDEX; i++, j++)
1060 val = INTERN_FIELD_SYM (i);
1061 if (! NILP (val))
1063 if ((n = font_style_to_value (j, INTERN_FIELD_SYM (i), 0)) < 0)
1064 return -1;
1065 ASET (font, j, make_number (n));
1068 ASET (font, FONT_ADSTYLE_INDEX, INTERN_FIELD_SYM (XLFD_ADSTYLE_INDEX));
1069 if (strcmp (f[XLFD_REGISTRY_INDEX], "*-*") == 0)
1070 ASET (font, FONT_REGISTRY_INDEX, Qnil);
1071 else
1072 ASET (font, FONT_REGISTRY_INDEX,
1073 font_intern_prop (f[XLFD_REGISTRY_INDEX],
1074 f[XLFD_LAST_INDEX] - f[XLFD_REGISTRY_INDEX],
1075 1));
1076 p = f[XLFD_PIXEL_INDEX];
1077 if (*p == '[' && (pixel_size = parse_matrix (p)) >= 0)
1078 ASET (font, FONT_SIZE_INDEX, make_number (pixel_size));
1079 else
1081 val = INTERN_FIELD (XLFD_PIXEL_INDEX);
1082 if (INTEGERP (val))
1083 ASET (font, FONT_SIZE_INDEX, val);
1084 else if (FONT_ENTITY_P (font))
1085 return -1;
1086 else
1088 double point_size = -1;
1090 eassert (FONT_SPEC_P (font));
1091 p = f[XLFD_POINT_INDEX];
1092 if (*p == '[')
1093 point_size = parse_matrix (p);
1094 else if (c_isdigit (*p))
1095 point_size = atoi (p), point_size /= 10;
1096 if (point_size >= 0)
1097 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1101 val = INTERN_FIELD (XLFD_RESY_INDEX);
1102 if (! NILP (val) && ! INTEGERP (val))
1103 return -1;
1104 ASET (font, FONT_DPI_INDEX, val);
1105 val = INTERN_FIELD (XLFD_SPACING_INDEX);
1106 if (! NILP (val))
1108 val = font_prop_validate_spacing (QCspacing, val);
1109 if (! INTEGERP (val))
1110 return -1;
1111 ASET (font, FONT_SPACING_INDEX, val);
1113 p = f[XLFD_AVGWIDTH_INDEX];
1114 if (*p == '~')
1115 p++;
1116 val = font_intern_prop (p, f[XLFD_REGISTRY_INDEX] - 1 - p, 0);
1117 if (! NILP (val) && ! INTEGERP (val))
1118 return -1;
1119 ASET (font, FONT_AVGWIDTH_INDEX, val);
1121 else
1123 bool wild_card_found = 0;
1124 Lisp_Object prop[XLFD_LAST_INDEX];
1126 if (FONT_ENTITY_P (font))
1127 return -1;
1128 for (j = 0; j < i; j++)
1130 if (*f[j] == '*')
1132 if (f[j][1] && f[j][1] != '-')
1133 return -1;
1134 prop[j] = Qnil;
1135 wild_card_found = 1;
1137 else if (j + 1 < i)
1138 prop[j] = INTERN_FIELD (j);
1139 else
1140 prop[j] = font_intern_prop (f[j], f[i] - f[j], 0);
1142 if (! wild_card_found)
1143 return -1;
1144 if (font_expand_wildcards (prop, i) < 0)
1145 return -1;
1147 ASET (font, FONT_FOUNDRY_INDEX, prop[XLFD_FOUNDRY_INDEX]);
1148 ASET (font, FONT_FAMILY_INDEX, prop[XLFD_FAMILY_INDEX]);
1149 for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
1150 i <= XLFD_SWIDTH_INDEX; i++, j++)
1151 if (! NILP (prop[i]))
1153 if ((n = font_style_to_value (j, prop[i], 1)) < 0)
1154 return -1;
1155 ASET (font, j, make_number (n));
1157 ASET (font, FONT_ADSTYLE_INDEX, prop[XLFD_ADSTYLE_INDEX]);
1158 val = prop[XLFD_REGISTRY_INDEX];
1159 if (NILP (val))
1161 val = prop[XLFD_ENCODING_INDEX];
1162 if (! NILP (val))
1164 AUTO_STRING (star_dash, "*-");
1165 val = concat2 (star_dash, SYMBOL_NAME (val));
1168 else if (NILP (prop[XLFD_ENCODING_INDEX]))
1170 AUTO_STRING (dash_star, "-*");
1171 val = concat2 (SYMBOL_NAME (val), dash_star);
1173 else
1175 AUTO_STRING (dash, "-");
1176 val = concat3 (SYMBOL_NAME (val), dash,
1177 SYMBOL_NAME (prop[XLFD_ENCODING_INDEX]));
1179 if (! NILP (val))
1180 ASET (font, FONT_REGISTRY_INDEX, Fintern (val, Qnil));
1182 if (INTEGERP (prop[XLFD_PIXEL_INDEX]))
1183 ASET (font, FONT_SIZE_INDEX, prop[XLFD_PIXEL_INDEX]);
1184 else if (INTEGERP (prop[XLFD_POINT_INDEX]))
1186 double point_size = XINT (prop[XLFD_POINT_INDEX]);
1188 ASET (font, FONT_SIZE_INDEX, make_float (point_size / 10));
1191 if (INTEGERP (prop[XLFD_RESX_INDEX]))
1192 ASET (font, FONT_DPI_INDEX, prop[XLFD_RESY_INDEX]);
1193 if (! NILP (prop[XLFD_SPACING_INDEX]))
1195 val = font_prop_validate_spacing (QCspacing,
1196 prop[XLFD_SPACING_INDEX]);
1197 if (! INTEGERP (val))
1198 return -1;
1199 ASET (font, FONT_SPACING_INDEX, val);
1201 if (INTEGERP (prop[XLFD_AVGWIDTH_INDEX]))
1202 ASET (font, FONT_AVGWIDTH_INDEX, prop[XLFD_AVGWIDTH_INDEX]);
1205 return 0;
1208 /* Store XLFD name of FONT (font-spec or font-entity) in NAME (NBYTES
1209 length), and return the name length. If FONT_SIZE_INDEX of FONT is
1210 0, use PIXEL_SIZE instead. */
1212 ptrdiff_t
1213 font_unparse_xlfd (Lisp_Object font, int pixel_size, char *name, int nbytes)
1215 char *p;
1216 const char *f[XLFD_REGISTRY_INDEX + 1];
1217 Lisp_Object val;
1218 int i, j, len;
1220 eassert (FONTP (font));
1222 for (i = FONT_FOUNDRY_INDEX, j = XLFD_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX;
1223 i++, j++)
1225 if (i == FONT_ADSTYLE_INDEX)
1226 j = XLFD_ADSTYLE_INDEX;
1227 else if (i == FONT_REGISTRY_INDEX)
1228 j = XLFD_REGISTRY_INDEX;
1229 val = AREF (font, i);
1230 if (NILP (val))
1232 if (j == XLFD_REGISTRY_INDEX)
1233 f[j] = "*-*";
1234 else
1235 f[j] = "*";
1237 else
1239 if (SYMBOLP (val))
1240 val = SYMBOL_NAME (val);
1241 if (j == XLFD_REGISTRY_INDEX
1242 && ! strchr (SSDATA (val), '-'))
1244 /* Change "jisx0208*" and "jisx0208" to "jisx0208*-*". */
1245 ptrdiff_t alloc = SBYTES (val) + 4;
1246 if (nbytes <= alloc)
1247 return -1;
1248 f[j] = p = alloca (alloc);
1249 sprintf (p, "%s%s-*", SDATA (val),
1250 &"*"[SDATA (val)[SBYTES (val) - 1] == '*']);
1252 else
1253 f[j] = SSDATA (val);
1257 for (i = FONT_WEIGHT_INDEX, j = XLFD_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX;
1258 i++, j++)
1260 val = font_style_symbolic (font, i, 0);
1261 if (NILP (val))
1262 f[j] = "*";
1263 else
1265 int c, k, l;
1266 ptrdiff_t alloc;
1268 val = SYMBOL_NAME (val);
1269 alloc = SBYTES (val) + 1;
1270 if (nbytes <= alloc)
1271 return -1;
1272 f[j] = p = alloca (alloc);
1273 /* Copy the name while excluding '-', '?', ',', and '"'. */
1274 for (k = l = 0; k < alloc; k++)
1276 c = SREF (val, k);
1277 if (c != '-' && c != '?' && c != ',' && c != '"')
1278 p[l++] = c;
1283 val = AREF (font, FONT_SIZE_INDEX);
1284 eassert (NUMBERP (val) || NILP (val));
1285 char font_size_index_buf[sizeof "-*"
1286 + max (INT_STRLEN_BOUND (EMACS_INT),
1287 1 + DBL_MAX_10_EXP + 1)];
1288 if (INTEGERP (val))
1290 EMACS_INT v = XINT (val);
1291 if (v <= 0)
1292 v = pixel_size;
1293 if (v > 0)
1295 f[XLFD_PIXEL_INDEX] = p = font_size_index_buf;
1296 sprintf (p, "%"pI"d-*", v);
1298 else
1299 f[XLFD_PIXEL_INDEX] = "*-*";
1301 else if (FLOATP (val))
1303 double v = XFLOAT_DATA (val) * 10;
1304 f[XLFD_PIXEL_INDEX] = p = font_size_index_buf;
1305 sprintf (p, "*-%.0f", v);
1307 else
1308 f[XLFD_PIXEL_INDEX] = "*-*";
1310 char dpi_index_buf[sizeof "-" + 2 * INT_STRLEN_BOUND (EMACS_INT)];
1311 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1313 EMACS_INT v = XINT (AREF (font, FONT_DPI_INDEX));
1314 f[XLFD_RESX_INDEX] = p = dpi_index_buf;
1315 sprintf (p, "%"pI"d-%"pI"d", v, v);
1317 else
1318 f[XLFD_RESX_INDEX] = "*-*";
1320 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1322 EMACS_INT spacing = XINT (AREF (font, FONT_SPACING_INDEX));
1324 f[XLFD_SPACING_INDEX] = (spacing <= FONT_SPACING_PROPORTIONAL ? "p"
1325 : spacing <= FONT_SPACING_DUAL ? "d"
1326 : spacing <= FONT_SPACING_MONO ? "m"
1327 : "c");
1329 else
1330 f[XLFD_SPACING_INDEX] = "*";
1332 char avgwidth_index_buf[INT_BUFSIZE_BOUND (EMACS_INT)];
1333 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1335 f[XLFD_AVGWIDTH_INDEX] = p = avgwidth_index_buf;
1336 sprintf (p, "%"pI"d", XINT (AREF (font, FONT_AVGWIDTH_INDEX)));
1338 else
1339 f[XLFD_AVGWIDTH_INDEX] = "*";
1341 len = snprintf (name, nbytes, "-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s",
1342 f[XLFD_FOUNDRY_INDEX], f[XLFD_FAMILY_INDEX],
1343 f[XLFD_WEIGHT_INDEX], f[XLFD_SLANT_INDEX],
1344 f[XLFD_SWIDTH_INDEX], f[XLFD_ADSTYLE_INDEX],
1345 f[XLFD_PIXEL_INDEX], f[XLFD_RESX_INDEX],
1346 f[XLFD_SPACING_INDEX], f[XLFD_AVGWIDTH_INDEX],
1347 f[XLFD_REGISTRY_INDEX]);
1348 return len < nbytes ? len : -1;
1351 /* Parse NAME (null terminated) and store information in FONT
1352 (font-spec or font-entity). NAME is supplied in either the
1353 Fontconfig or GTK font name format. If NAME is successfully
1354 parsed, return 0. Otherwise return -1.
1356 The fontconfig format is
1358 FAMILY[-SIZE][:PROP1[=VAL1][:PROP2[=VAL2]...]]
1360 The GTK format is
1362 FAMILY [PROPS...] [SIZE]
1364 This function tries to guess which format it is. */
1366 static int
1367 font_parse_fcname (char *name, ptrdiff_t len, Lisp_Object font)
1369 char *p, *q;
1370 char *size_beg = NULL, *size_end = NULL;
1371 char *props_beg = NULL, *family_end = NULL;
1373 if (len == 0)
1374 return -1;
1376 for (p = name; *p; p++)
1378 if (*p == '\\' && p[1])
1379 p++;
1380 else if (*p == ':')
1382 props_beg = family_end = p;
1383 break;
1385 else if (*p == '-')
1387 bool decimal = 0, size_found = 1;
1388 for (q = p + 1; *q && *q != ':'; q++)
1389 if (! c_isdigit (*q))
1391 if (*q != '.' || decimal)
1393 size_found = 0;
1394 break;
1396 decimal = 1;
1398 if (size_found)
1400 family_end = p;
1401 size_beg = p + 1;
1402 size_end = q;
1403 break;
1408 if (family_end)
1410 Lisp_Object extra_props = Qnil;
1412 /* A fontconfig name with size and/or property data. */
1413 if (family_end > name)
1415 Lisp_Object family;
1416 family = font_intern_prop (name, family_end - name, 1);
1417 ASET (font, FONT_FAMILY_INDEX, family);
1419 if (size_beg)
1421 double point_size = strtod (size_beg, &size_end);
1422 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1423 if (*size_end == ':' && size_end[1])
1424 props_beg = size_end;
1426 if (props_beg)
1428 /* Now parse ":KEY=VAL" patterns. */
1429 Lisp_Object val;
1431 for (p = props_beg; *p; p = q)
1433 for (q = p + 1; *q && *q != '=' && *q != ':'; q++);
1434 if (*q != '=')
1436 /* Must be an enumerated value. */
1437 ptrdiff_t word_len;
1438 p = p + 1;
1439 word_len = q - p;
1440 val = font_intern_prop (p, q - p, 1);
1442 #define PROP_MATCH(STR) (word_len == strlen (STR) \
1443 && memcmp (p, STR, strlen (STR)) == 0)
1445 if (PROP_MATCH ("light")
1446 || PROP_MATCH ("medium")
1447 || PROP_MATCH ("demibold")
1448 || PROP_MATCH ("bold")
1449 || PROP_MATCH ("black"))
1450 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, val);
1451 else if (PROP_MATCH ("roman")
1452 || PROP_MATCH ("italic")
1453 || PROP_MATCH ("oblique"))
1454 FONT_SET_STYLE (font, FONT_SLANT_INDEX, val);
1455 else if (PROP_MATCH ("charcell"))
1456 ASET (font, FONT_SPACING_INDEX,
1457 make_number (FONT_SPACING_CHARCELL));
1458 else if (PROP_MATCH ("mono"))
1459 ASET (font, FONT_SPACING_INDEX,
1460 make_number (FONT_SPACING_MONO));
1461 else if (PROP_MATCH ("proportional"))
1462 ASET (font, FONT_SPACING_INDEX,
1463 make_number (FONT_SPACING_PROPORTIONAL));
1464 #undef PROP_MATCH
1466 else
1468 /* KEY=VAL pairs */
1469 Lisp_Object key;
1470 int prop;
1472 if (q - p == 10 && memcmp (p + 1, "pixelsize", 9) == 0)
1473 prop = FONT_SIZE_INDEX;
1474 else
1476 key = font_intern_prop (p, q - p, 1);
1477 prop = get_font_prop_index (key);
1480 p = q + 1;
1481 for (q = p; *q && *q != ':'; q++);
1482 val = font_intern_prop (p, q - p, 0);
1484 if (prop >= FONT_FOUNDRY_INDEX
1485 && prop < FONT_EXTRA_INDEX)
1486 ASET (font, prop, font_prop_validate (prop, Qnil, val));
1487 else
1489 extra_props = nconc2 (extra_props,
1490 list1 (Fcons (key, val)));
1493 p = q;
1497 if (! NILP (extra_props))
1499 struct font_driver_list *driver_list = font_driver_list;
1500 for ( ; driver_list; driver_list = driver_list->next)
1501 if (driver_list->driver->filter_properties)
1502 (*driver_list->driver->filter_properties) (font, extra_props);
1506 else
1508 /* Either a fontconfig-style name with no size and property
1509 data, or a GTK-style name. */
1510 Lisp_Object weight = Qnil, slant = Qnil;
1511 Lisp_Object width = Qnil, size = Qnil;
1512 char *word_start;
1513 ptrdiff_t word_len;
1515 /* Scan backwards from the end, looking for a size. */
1516 for (p = name + len - 1; p >= name; p--)
1517 if (!c_isdigit (*p))
1518 break;
1520 if ((p < name + len - 1) && ((p + 1 == name) || *p == ' '))
1521 /* Found a font size. */
1522 size = make_float (strtod (p + 1, NULL));
1523 else
1524 p = name + len;
1526 /* Now P points to the termination of the string, sans size.
1527 Scan backwards, looking for font properties. */
1528 for (; p > name; p = q)
1530 for (q = p - 1; q >= name; q--)
1532 if (q > name && *(q-1) == '\\')
1533 --q; /* Skip quoting backslashes. */
1534 else if (*q == ' ')
1535 break;
1538 word_start = q + 1;
1539 word_len = p - word_start;
1541 #define PROP_MATCH(STR) \
1542 (word_len == strlen (STR) \
1543 && memcmp (word_start, STR, strlen (STR)) == 0)
1544 #define PROP_SAVE(VAR, STR) \
1545 (VAR = NILP (VAR) ? font_intern_prop (STR, strlen (STR), 1) : VAR)
1547 if (PROP_MATCH ("Ultra-Light"))
1548 PROP_SAVE (weight, "ultra-light");
1549 else if (PROP_MATCH ("Light"))
1550 PROP_SAVE (weight, "light");
1551 else if (PROP_MATCH ("Book"))
1552 PROP_SAVE (weight, "book");
1553 else if (PROP_MATCH ("Medium"))
1554 PROP_SAVE (weight, "medium");
1555 else if (PROP_MATCH ("Semi-Bold"))
1556 PROP_SAVE (weight, "semi-bold");
1557 else if (PROP_MATCH ("Bold"))
1558 PROP_SAVE (weight, "bold");
1559 else if (PROP_MATCH ("Italic"))
1560 PROP_SAVE (slant, "italic");
1561 else if (PROP_MATCH ("Oblique"))
1562 PROP_SAVE (slant, "oblique");
1563 else if (PROP_MATCH ("Semi-Condensed"))
1564 PROP_SAVE (width, "semi-condensed");
1565 else if (PROP_MATCH ("Condensed"))
1566 PROP_SAVE (width, "condensed");
1567 /* An unknown word must be part of the font name. */
1568 else
1570 family_end = p;
1571 break;
1574 #undef PROP_MATCH
1575 #undef PROP_SAVE
1577 if (family_end)
1578 ASET (font, FONT_FAMILY_INDEX,
1579 font_intern_prop (name, family_end - name, 1));
1580 if (!NILP (size))
1581 ASET (font, FONT_SIZE_INDEX, size);
1582 if (!NILP (weight))
1583 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, weight);
1584 if (!NILP (slant))
1585 FONT_SET_STYLE (font, FONT_SLANT_INDEX, slant);
1586 if (!NILP (width))
1587 FONT_SET_STYLE (font, FONT_WIDTH_INDEX, width);
1590 return 0;
1593 #if defined HAVE_XFT || defined HAVE_FREETYPE || defined HAVE_NS
1595 /* Store fontconfig's font name of FONT (font-spec or font-entity) in
1596 NAME (NBYTES length), and return the name length. If
1597 FONT_SIZE_INDEX of FONT is 0, use PIXEL_SIZE instead.
1598 Return a negative value on error. */
1600 static int
1601 font_unparse_fcname (Lisp_Object font, int pixel_size, char *name, int nbytes)
1603 Lisp_Object family, foundry;
1604 Lisp_Object val;
1605 int point_size;
1606 int i;
1607 char *p;
1608 char *lim;
1609 Lisp_Object styles[3];
1610 const char *style_names[3] = { "weight", "slant", "width" };
1612 family = AREF (font, FONT_FAMILY_INDEX);
1613 if (! NILP (family))
1615 if (SYMBOLP (family))
1616 family = SYMBOL_NAME (family);
1617 else
1618 family = Qnil;
1621 val = AREF (font, FONT_SIZE_INDEX);
1622 if (INTEGERP (val))
1624 if (XINT (val) != 0)
1625 pixel_size = XINT (val);
1626 point_size = -1;
1628 else
1630 eassert (FLOATP (val));
1631 pixel_size = -1;
1632 point_size = (int) XFLOAT_DATA (val);
1635 foundry = AREF (font, FONT_FOUNDRY_INDEX);
1636 if (! NILP (foundry))
1638 if (SYMBOLP (foundry))
1639 foundry = SYMBOL_NAME (foundry);
1640 else
1641 foundry = Qnil;
1644 for (i = 0; i < 3; i++)
1645 styles[i] = font_style_symbolic (font, FONT_WEIGHT_INDEX + i, 0);
1647 p = name;
1648 lim = name + nbytes;
1649 if (! NILP (family))
1651 int len = snprintf (p, lim - p, "%s", SSDATA (family));
1652 if (! (0 <= len && len < lim - p))
1653 return -1;
1654 p += len;
1656 if (point_size > 0)
1658 int len = snprintf (p, lim - p, &"-%d"[p == name], point_size);
1659 if (! (0 <= len && len < lim - p))
1660 return -1;
1661 p += len;
1663 else if (pixel_size > 0)
1665 int len = snprintf (p, lim - p, ":pixelsize=%d", pixel_size);
1666 if (! (0 <= len && len < lim - p))
1667 return -1;
1668 p += len;
1670 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
1672 int len = snprintf (p, lim - p, ":foundry=%s",
1673 SSDATA (SYMBOL_NAME (AREF (font,
1674 FONT_FOUNDRY_INDEX))));
1675 if (! (0 <= len && len < lim - p))
1676 return -1;
1677 p += len;
1679 for (i = 0; i < 3; i++)
1680 if (! NILP (styles[i]))
1682 int len = snprintf (p, lim - p, ":%s=%s", style_names[i],
1683 SSDATA (SYMBOL_NAME (styles[i])));
1684 if (! (0 <= len && len < lim - p))
1685 return -1;
1686 p += len;
1689 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1691 int len = snprintf (p, lim - p, ":dpi=%"pI"d",
1692 XINT (AREF (font, FONT_DPI_INDEX)));
1693 if (! (0 <= len && len < lim - p))
1694 return -1;
1695 p += len;
1698 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1700 int len = snprintf (p, lim - p, ":spacing=%"pI"d",
1701 XINT (AREF (font, FONT_SPACING_INDEX)));
1702 if (! (0 <= len && len < lim - p))
1703 return -1;
1704 p += len;
1707 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1709 int len = snprintf (p, lim - p,
1710 (XINT (AREF (font, FONT_AVGWIDTH_INDEX)) == 0
1711 ? ":scalable=true"
1712 : ":scalable=false"));
1713 if (! (0 <= len && len < lim - p))
1714 return -1;
1715 p += len;
1718 return (p - name);
1721 #endif
1723 /* Parse NAME (null terminated) and store information in FONT
1724 (font-spec or font-entity). If NAME is successfully parsed, return
1725 0. Otherwise return -1. */
1727 static int
1728 font_parse_name (char *name, ptrdiff_t namelen, Lisp_Object font)
1730 if (name[0] == '-' || strchr (name, '*') || strchr (name, '?'))
1731 return font_parse_xlfd (name, namelen, font);
1732 return font_parse_fcname (name, namelen, font);
1736 /* Merge FAMILY and REGISTRY into FONT_SPEC. FAMILY may have the form
1737 "FAMILY-FOUNDRY". REGISTRY may not contain charset-encoding
1738 part. */
1740 void
1741 font_parse_family_registry (Lisp_Object family, Lisp_Object registry, Lisp_Object font_spec)
1743 ptrdiff_t len;
1744 char *p0, *p1;
1746 if (! NILP (family)
1747 && NILP (AREF (font_spec, FONT_FAMILY_INDEX)))
1749 CHECK_STRING (family);
1750 len = SBYTES (family);
1751 p0 = SSDATA (family);
1752 p1 = strchr (p0, '-');
1753 if (p1)
1755 if ((*p0 != '*' && p1 - p0 > 0)
1756 && NILP (AREF (font_spec, FONT_FOUNDRY_INDEX)))
1757 Ffont_put (font_spec, QCfoundry, font_intern_prop (p0, p1 - p0, 1));
1758 p1++;
1759 len -= p1 - p0;
1760 Ffont_put (font_spec, QCfamily, font_intern_prop (p1, len, 1));
1762 else
1763 ASET (font_spec, FONT_FAMILY_INDEX, Fintern (family, Qnil));
1765 if (! NILP (registry))
1767 /* Convert "XXX" and "XXX*" to "XXX*-*". */
1768 CHECK_STRING (registry);
1769 len = SBYTES (registry);
1770 p0 = SSDATA (registry);
1771 p1 = strchr (p0, '-');
1772 if (! p1)
1774 AUTO_STRING (extra, (&"*-*"[len && p0[len - 1] == '*']));
1775 registry = concat2 (registry, extra);
1777 registry = Fdowncase (registry);
1778 ASET (font_spec, FONT_REGISTRY_INDEX, Fintern (registry, Qnil));
1783 /* This part (through the next ^L) is still experimental and not
1784 tested much. We may drastically change codes. */
1786 /* OTF handler. */
1788 #if 0
1790 #define LGSTRING_HEADER_SIZE 6
1791 #define LGSTRING_GLYPH_SIZE 8
1793 static int
1794 check_gstring (Lisp_Object gstring)
1796 Lisp_Object val;
1797 ptrdiff_t i;
1798 int j;
1800 CHECK_VECTOR (gstring);
1801 val = AREF (gstring, 0);
1802 CHECK_VECTOR (val);
1803 if (ASIZE (val) < LGSTRING_HEADER_SIZE)
1804 goto err;
1805 CHECK_FONT_OBJECT (LGSTRING_FONT (gstring));
1806 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_LBEARING)))
1807 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_LBEARING));
1808 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_RBEARING)))
1809 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_RBEARING));
1810 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_WIDTH)))
1811 CHECK_NATNUM (LGSTRING_SLOT (gstring, LGSTRING_IX_WIDTH));
1812 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT)))
1813 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT));
1814 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT)))
1815 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT));
1817 for (i = 0; i < LGSTRING_GLYPH_LEN (gstring); i++)
1819 val = LGSTRING_GLYPH (gstring, i);
1820 CHECK_VECTOR (val);
1821 if (ASIZE (val) < LGSTRING_GLYPH_SIZE)
1822 goto err;
1823 if (NILP (AREF (val, LGLYPH_IX_CHAR)))
1824 break;
1825 CHECK_NATNUM (AREF (val, LGLYPH_IX_FROM));
1826 CHECK_NATNUM (AREF (val, LGLYPH_IX_TO));
1827 CHECK_CHARACTER (AREF (val, LGLYPH_IX_CHAR));
1828 if (!NILP (AREF (val, LGLYPH_IX_CODE)))
1829 CHECK_NATNUM (AREF (val, LGLYPH_IX_CODE));
1830 if (!NILP (AREF (val, LGLYPH_IX_WIDTH)))
1831 CHECK_NATNUM (AREF (val, LGLYPH_IX_WIDTH));
1832 if (!NILP (AREF (val, LGLYPH_IX_ADJUSTMENT)))
1834 val = AREF (val, LGLYPH_IX_ADJUSTMENT);
1835 CHECK_VECTOR (val);
1836 if (ASIZE (val) < 3)
1837 goto err;
1838 for (j = 0; j < 3; j++)
1839 CHECK_NUMBER (AREF (val, j));
1842 return i;
1843 err:
1844 error ("Invalid glyph-string format");
1845 return -1;
1848 static void
1849 check_otf_features (Lisp_Object otf_features)
1851 Lisp_Object val;
1853 CHECK_CONS (otf_features);
1854 CHECK_SYMBOL (XCAR (otf_features));
1855 otf_features = XCDR (otf_features);
1856 CHECK_CONS (otf_features);
1857 CHECK_SYMBOL (XCAR (otf_features));
1858 otf_features = XCDR (otf_features);
1859 for (val = Fcar (otf_features); CONSP (val); val = XCDR (val))
1861 CHECK_SYMBOL (XCAR (val));
1862 if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
1863 error ("Invalid OTF GSUB feature: %s",
1864 SDATA (SYMBOL_NAME (XCAR (val))));
1866 otf_features = XCDR (otf_features);
1867 for (val = Fcar (otf_features); CONSP (val); val = XCDR (val))
1869 CHECK_SYMBOL (XCAR (val));
1870 if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
1871 error ("Invalid OTF GPOS feature: %s",
1872 SDATA (SYMBOL_NAME (XCAR (val))));
1876 #ifdef HAVE_LIBOTF
1877 #include <otf.h>
1879 Lisp_Object otf_list;
1881 static Lisp_Object
1882 otf_tag_symbol (OTF_Tag tag)
1884 char name[5];
1886 OTF_tag_name (tag, name);
1887 return Fintern (make_unibyte_string (name, 4), Qnil);
1890 static OTF *
1891 otf_open (Lisp_Object file)
1893 Lisp_Object val = Fassoc (file, otf_list);
1894 OTF *otf;
1896 if (! NILP (val))
1897 otf = XSAVE_POINTER (XCDR (val), 0);
1898 else
1900 otf = STRINGP (file) ? OTF_open (SSDATA (file)) : NULL;
1901 val = make_save_ptr (otf);
1902 otf_list = Fcons (Fcons (file, val), otf_list);
1904 return otf;
1908 /* Return a list describing which scripts/languages FONT supports by
1909 which GSUB/GPOS features of OpenType tables. See the comment of
1910 (struct font_driver).otf_capability. */
1912 Lisp_Object
1913 font_otf_capability (struct font *font)
1915 OTF *otf;
1916 Lisp_Object capability = Fcons (Qnil, Qnil);
1917 int i;
1919 otf = otf_open (font->props[FONT_FILE_INDEX]);
1920 if (! otf)
1921 return Qnil;
1922 for (i = 0; i < 2; i++)
1924 OTF_GSUB_GPOS *gsub_gpos;
1925 Lisp_Object script_list = Qnil;
1926 int j;
1928 if (OTF_get_features (otf, i == 0) < 0)
1929 continue;
1930 gsub_gpos = i == 0 ? otf->gsub : otf->gpos;
1931 for (j = gsub_gpos->ScriptList.ScriptCount - 1; j >= 0; j--)
1933 OTF_Script *script = gsub_gpos->ScriptList.Script + j;
1934 Lisp_Object langsys_list = Qnil;
1935 Lisp_Object script_tag = otf_tag_symbol (script->ScriptTag);
1936 int k;
1938 for (k = script->LangSysCount; k >= 0; k--)
1940 OTF_LangSys *langsys;
1941 Lisp_Object feature_list = Qnil;
1942 Lisp_Object langsys_tag;
1943 int l;
1945 if (k == script->LangSysCount)
1947 langsys = &script->DefaultLangSys;
1948 langsys_tag = Qnil;
1950 else
1952 langsys = script->LangSys + k;
1953 langsys_tag
1954 = otf_tag_symbol (script->LangSysRecord[k].LangSysTag);
1956 for (l = langsys->FeatureCount - 1; l >= 0; l--)
1958 OTF_Feature *feature
1959 = gsub_gpos->FeatureList.Feature + langsys->FeatureIndex[l];
1960 Lisp_Object feature_tag
1961 = otf_tag_symbol (feature->FeatureTag);
1963 feature_list = Fcons (feature_tag, feature_list);
1965 langsys_list = Fcons (Fcons (langsys_tag, feature_list),
1966 langsys_list);
1968 script_list = Fcons (Fcons (script_tag, langsys_list),
1969 script_list);
1972 if (i == 0)
1973 XSETCAR (capability, script_list);
1974 else
1975 XSETCDR (capability, script_list);
1978 return capability;
1981 /* Parse OTF features in SPEC and write a proper features spec string
1982 in FEATURES for the call of OTF_drive_gsub/gpos (of libotf). It is
1983 assured that the sufficient memory has already allocated for
1984 FEATURES. */
1986 static void
1987 generate_otf_features (Lisp_Object spec, char *features)
1989 Lisp_Object val;
1990 char *p;
1991 bool asterisk;
1993 p = features;
1994 *p = '\0';
1995 for (asterisk = 0; CONSP (spec); spec = XCDR (spec))
1997 val = XCAR (spec);
1998 CHECK_SYMBOL (val);
1999 if (p > features)
2000 *p++ = ',';
2001 if (SREF (SYMBOL_NAME (val), 0) == '*')
2003 asterisk = 1;
2004 *p++ = '*';
2006 else if (! asterisk)
2008 val = SYMBOL_NAME (val);
2009 p += esprintf (p, "%s", SDATA (val));
2011 else
2013 val = SYMBOL_NAME (val);
2014 p += esprintf (p, "~%s", SDATA (val));
2017 if (CONSP (spec))
2018 error ("OTF spec too long");
2021 Lisp_Object
2022 font_otf_DeviceTable (OTF_DeviceTable *device_table)
2024 int len = device_table->StartSize - device_table->EndSize + 1;
2026 return Fcons (make_number (len),
2027 make_unibyte_string (device_table->DeltaValue, len));
2030 Lisp_Object
2031 font_otf_ValueRecord (int value_format, OTF_ValueRecord *value_record)
2033 Lisp_Object val = Fmake_vector (make_number (8), Qnil);
2035 if (value_format & OTF_XPlacement)
2036 ASET (val, 0, make_number (value_record->XPlacement));
2037 if (value_format & OTF_YPlacement)
2038 ASET (val, 1, make_number (value_record->YPlacement));
2039 if (value_format & OTF_XAdvance)
2040 ASET (val, 2, make_number (value_record->XAdvance));
2041 if (value_format & OTF_YAdvance)
2042 ASET (val, 3, make_number (value_record->YAdvance));
2043 if (value_format & OTF_XPlaDevice)
2044 ASET (val, 4, font_otf_DeviceTable (&value_record->XPlaDevice));
2045 if (value_format & OTF_YPlaDevice)
2046 ASET (val, 4, font_otf_DeviceTable (&value_record->YPlaDevice));
2047 if (value_format & OTF_XAdvDevice)
2048 ASET (val, 4, font_otf_DeviceTable (&value_record->XAdvDevice));
2049 if (value_format & OTF_YAdvDevice)
2050 ASET (val, 4, font_otf_DeviceTable (&value_record->YAdvDevice));
2051 return val;
2054 Lisp_Object
2055 font_otf_Anchor (OTF_Anchor *anchor)
2057 Lisp_Object val;
2059 val = Fmake_vector (make_number (anchor->AnchorFormat + 1), Qnil);
2060 ASET (val, 0, make_number (anchor->XCoordinate));
2061 ASET (val, 1, make_number (anchor->YCoordinate));
2062 if (anchor->AnchorFormat == 2)
2063 ASET (val, 2, make_number (anchor->f.f1.AnchorPoint));
2064 else
2066 ASET (val, 3, font_otf_DeviceTable (&anchor->f.f2.XDeviceTable));
2067 ASET (val, 4, font_otf_DeviceTable (&anchor->f.f2.YDeviceTable));
2069 return val;
2071 #endif /* HAVE_LIBOTF */
2072 #endif /* 0 */
2075 /* Font sorting. */
2077 static double
2078 font_rescale_ratio (Lisp_Object font_entity)
2080 Lisp_Object tail, elt;
2081 Lisp_Object name = Qnil;
2083 for (tail = Vface_font_rescale_alist; CONSP (tail); tail = XCDR (tail))
2085 elt = XCAR (tail);
2086 if (FLOATP (XCDR (elt)))
2088 if (STRINGP (XCAR (elt)))
2090 if (NILP (name))
2091 name = Ffont_xlfd_name (font_entity, Qnil);
2092 if (fast_string_match_ignore_case (XCAR (elt), name) >= 0)
2093 return XFLOAT_DATA (XCDR (elt));
2095 else if (FONT_SPEC_P (XCAR (elt)))
2097 if (font_match_p (XCAR (elt), font_entity))
2098 return XFLOAT_DATA (XCDR (elt));
2102 return 1.0;
2105 /* We sort fonts by scoring each of them against a specified
2106 font-spec. The score value is 32 bit (`unsigned'), and the smaller
2107 the value is, the closer the font is to the font-spec.
2109 The lowest 2 bits of the score are used for driver type. The font
2110 available by the most preferred font driver is 0.
2112 The 4 7-bit fields in the higher 28 bits are used for numeric properties
2113 WEIGHT, SLANT, WIDTH, and SIZE. */
2115 /* How many bits to shift to store the difference value of each font
2116 property in a score. Note that floats for FONT_TYPE_INDEX and
2117 FONT_REGISTRY_INDEX are not used. */
2118 static int sort_shift_bits[FONT_SIZE_INDEX + 1];
2120 /* Score font-entity ENTITY against properties of font-spec SPEC_PROP.
2121 The return value indicates how different ENTITY is compared with
2122 SPEC_PROP. */
2124 static unsigned
2125 font_score (Lisp_Object entity, Lisp_Object *spec_prop)
2127 unsigned score = 0;
2128 int i;
2130 /* Score three style numeric fields. Maximum difference is 127. */
2131 for (i = FONT_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX; i++)
2132 if (! NILP (spec_prop[i]) && ! EQ (AREF (entity, i), spec_prop[i]))
2134 EMACS_INT diff = ((XINT (AREF (entity, i)) >> 8)
2135 - (XINT (spec_prop[i]) >> 8));
2136 score |= min (eabs (diff), 127) << sort_shift_bits[i];
2139 /* Score the size. Maximum difference is 127. */
2140 if (! NILP (spec_prop[FONT_SIZE_INDEX])
2141 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
2143 /* We use the higher 6-bit for the actual size difference. The
2144 lowest bit is set if the DPI is different. */
2145 EMACS_INT diff;
2146 EMACS_INT pixel_size = XINT (spec_prop[FONT_SIZE_INDEX]);
2147 EMACS_INT entity_size = XINT (AREF (entity, FONT_SIZE_INDEX));
2149 if (CONSP (Vface_font_rescale_alist))
2150 pixel_size *= font_rescale_ratio (entity);
2151 if (pixel_size * 2 < entity_size || entity_size * 2 < pixel_size)
2152 /* This size is wrong by more than a factor 2: reject it! */
2153 return 0xFFFFFFFF;
2154 diff = eabs (pixel_size - entity_size) << 1;
2155 if (! NILP (spec_prop[FONT_DPI_INDEX])
2156 && ! EQ (spec_prop[FONT_DPI_INDEX], AREF (entity, FONT_DPI_INDEX)))
2157 diff |= 1;
2158 if (! NILP (spec_prop[FONT_AVGWIDTH_INDEX])
2159 && ! EQ (spec_prop[FONT_AVGWIDTH_INDEX], AREF (entity, FONT_AVGWIDTH_INDEX)))
2160 diff |= 1;
2161 score |= min (diff, 127) << sort_shift_bits[FONT_SIZE_INDEX];
2164 return score;
2168 /* Concatenate all elements of LIST into one vector. LIST is a list
2169 of font-entity vectors. */
2171 static Lisp_Object
2172 font_vconcat_entity_vectors (Lisp_Object list)
2174 EMACS_INT nargs = XFASTINT (Flength (list));
2175 Lisp_Object *args;
2176 USE_SAFE_ALLOCA;
2177 SAFE_ALLOCA_LISP (args, nargs);
2178 ptrdiff_t i;
2180 for (i = 0; i < nargs; i++, list = XCDR (list))
2181 args[i] = XCAR (list);
2182 Lisp_Object result = Fvconcat (nargs, args);
2183 SAFE_FREE ();
2184 return result;
2188 /* The structure for elements being sorted by qsort. */
2189 struct font_sort_data
2191 unsigned score;
2192 int font_driver_preference;
2193 Lisp_Object entity;
2197 /* The comparison function for qsort. */
2199 static int
2200 font_compare (const void *d1, const void *d2)
2202 const struct font_sort_data *data1 = d1;
2203 const struct font_sort_data *data2 = d2;
2205 if (data1->score < data2->score)
2206 return -1;
2207 else if (data1->score > data2->score)
2208 return 1;
2209 return (data1->font_driver_preference - data2->font_driver_preference);
2213 /* Sort each font-entity vector in LIST by closeness to font-spec PREFER.
2214 If PREFER specifies a point-size, calculate the corresponding
2215 pixel-size from QCdpi property of PREFER or from the Y-resolution
2216 of FRAME before sorting.
2218 If BEST-ONLY is nonzero, return the best matching entity (that
2219 supports the character BEST-ONLY if BEST-ONLY is positive, or any
2220 if BEST-ONLY is negative). Otherwise, return the sorted result as
2221 a single vector of font-entities.
2223 This function does no optimization for the case that the total
2224 number of elements is 1. The caller should avoid calling this in
2225 such a case. */
2227 static Lisp_Object
2228 font_sort_entities (Lisp_Object list, Lisp_Object prefer,
2229 struct frame *f, int best_only)
2231 Lisp_Object prefer_prop[FONT_SPEC_MAX];
2232 int len, maxlen, i;
2233 struct font_sort_data *data;
2234 unsigned best_score;
2235 Lisp_Object best_entity;
2236 Lisp_Object tail, vec IF_LINT (= Qnil);
2237 USE_SAFE_ALLOCA;
2239 for (i = FONT_WEIGHT_INDEX; i <= FONT_AVGWIDTH_INDEX; i++)
2240 prefer_prop[i] = AREF (prefer, i);
2241 if (FLOATP (prefer_prop[FONT_SIZE_INDEX]))
2242 prefer_prop[FONT_SIZE_INDEX]
2243 = make_number (font_pixel_size (f, prefer));
2245 if (NILP (XCDR (list)))
2247 /* What we have to take care of is this single vector. */
2248 vec = XCAR (list);
2249 maxlen = ASIZE (vec);
2251 else if (best_only)
2253 /* We don't have to perform sort, so there's no need of creating
2254 a single vector. But, we must find the length of the longest
2255 vector. */
2256 maxlen = 0;
2257 for (tail = list; CONSP (tail); tail = XCDR (tail))
2258 if (maxlen < ASIZE (XCAR (tail)))
2259 maxlen = ASIZE (XCAR (tail));
2261 else
2263 /* We have to create a single vector to sort it. */
2264 vec = font_vconcat_entity_vectors (list);
2265 maxlen = ASIZE (vec);
2268 data = SAFE_ALLOCA (maxlen * sizeof *data);
2269 best_score = 0xFFFFFFFF;
2270 best_entity = Qnil;
2272 for (tail = list; CONSP (tail); tail = XCDR (tail))
2274 int font_driver_preference = 0;
2275 Lisp_Object current_font_driver;
2277 if (best_only)
2278 vec = XCAR (tail);
2279 len = ASIZE (vec);
2281 /* We are sure that the length of VEC > 0. */
2282 current_font_driver = AREF (AREF (vec, 0), FONT_TYPE_INDEX);
2283 /* Score the elements. */
2284 for (i = 0; i < len; i++)
2286 data[i].entity = AREF (vec, i);
2287 data[i].score
2288 = ((best_only <= 0 || font_has_char (f, data[i].entity, best_only)
2289 > 0)
2290 ? font_score (data[i].entity, prefer_prop)
2291 : 0xFFFFFFFF);
2292 if (best_only && best_score > data[i].score)
2294 best_score = data[i].score;
2295 best_entity = data[i].entity;
2296 if (best_score == 0)
2297 break;
2299 if (! EQ (current_font_driver, AREF (AREF (vec, i), FONT_TYPE_INDEX)))
2301 current_font_driver = AREF (AREF (vec, i), FONT_TYPE_INDEX);
2302 font_driver_preference++;
2304 data[i].font_driver_preference = font_driver_preference;
2307 /* Sort if necessary. */
2308 if (! best_only)
2310 qsort (data, len, sizeof *data, font_compare);
2311 for (i = 0; i < len; i++)
2312 ASET (vec, i, data[i].entity);
2313 break;
2315 else
2316 vec = best_entity;
2319 SAFE_FREE ();
2321 FONT_ADD_LOG ("sort-by", prefer, vec);
2322 return vec;
2326 /* API of Font Service Layer. */
2328 /* Reflect ORDER (see the variable font_sort_order in xfaces.c) to
2329 sort_shift_bits. Finternal_set_font_selection_order calls this
2330 function with font_sort_order after setting up it. */
2332 void
2333 font_update_sort_order (int *order)
2335 int i, shift_bits;
2337 for (i = 0, shift_bits = 23; i < 4; i++, shift_bits -= 7)
2339 int xlfd_idx = order[i];
2341 if (xlfd_idx == XLFD_WEIGHT_INDEX)
2342 sort_shift_bits[FONT_WEIGHT_INDEX] = shift_bits;
2343 else if (xlfd_idx == XLFD_SLANT_INDEX)
2344 sort_shift_bits[FONT_SLANT_INDEX] = shift_bits;
2345 else if (xlfd_idx == XLFD_SWIDTH_INDEX)
2346 sort_shift_bits[FONT_WIDTH_INDEX] = shift_bits;
2347 else
2348 sort_shift_bits[FONT_SIZE_INDEX] = shift_bits;
2352 static bool
2353 font_check_otf_features (Lisp_Object script, Lisp_Object langsys,
2354 Lisp_Object features, Lisp_Object table)
2356 Lisp_Object val;
2357 bool negative;
2359 table = assq_no_quit (script, table);
2360 if (NILP (table))
2361 return 0;
2362 table = XCDR (table);
2363 if (! NILP (langsys))
2365 table = assq_no_quit (langsys, table);
2366 if (NILP (table))
2367 return 0;
2369 else
2371 val = assq_no_quit (Qnil, table);
2372 if (NILP (val))
2373 table = XCAR (table);
2374 else
2375 table = val;
2377 table = XCDR (table);
2378 for (negative = 0; CONSP (features); features = XCDR (features))
2380 if (NILP (XCAR (features)))
2382 negative = 1;
2383 continue;
2385 if (NILP (Fmemq (XCAR (features), table)) != negative)
2386 return 0;
2388 return 1;
2391 /* Check if OTF_CAPABILITY satisfies SPEC (otf-spec). */
2393 static bool
2394 font_check_otf (Lisp_Object spec, Lisp_Object otf_capability)
2396 Lisp_Object script, langsys = Qnil, gsub = Qnil, gpos = Qnil;
2398 script = XCAR (spec);
2399 spec = XCDR (spec);
2400 if (! NILP (spec))
2402 langsys = XCAR (spec);
2403 spec = XCDR (spec);
2404 if (! NILP (spec))
2406 gsub = XCAR (spec);
2407 spec = XCDR (spec);
2408 if (! NILP (spec))
2409 gpos = XCAR (spec);
2413 if (! NILP (gsub) && ! font_check_otf_features (script, langsys, gsub,
2414 XCAR (otf_capability)))
2415 return 0;
2416 if (! NILP (gpos) && ! font_check_otf_features (script, langsys, gpos,
2417 XCDR (otf_capability)))
2418 return 0;
2419 return 1;
2424 /* Check if FONT (font-entity or font-object) matches with the font
2425 specification SPEC. */
2427 bool
2428 font_match_p (Lisp_Object spec, Lisp_Object font)
2430 Lisp_Object prop[FONT_SPEC_MAX], *props;
2431 Lisp_Object extra, font_extra;
2432 int i;
2434 for (i = FONT_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX; i++)
2435 if (! NILP (AREF (spec, i))
2436 && ! NILP (AREF (font, i))
2437 && ! EQ (AREF (spec, i), AREF (font, i)))
2438 return 0;
2439 props = XFONT_SPEC (spec)->props;
2440 if (FLOATP (props[FONT_SIZE_INDEX]))
2442 for (i = FONT_FOUNDRY_INDEX; i < FONT_SIZE_INDEX; i++)
2443 prop[i] = AREF (spec, i);
2444 prop[FONT_SIZE_INDEX]
2445 = make_number (font_pixel_size (XFRAME (selected_frame), spec));
2446 props = prop;
2449 if (font_score (font, props) > 0)
2450 return 0;
2451 extra = AREF (spec, FONT_EXTRA_INDEX);
2452 font_extra = AREF (font, FONT_EXTRA_INDEX);
2453 for (; CONSP (extra); extra = XCDR (extra))
2455 Lisp_Object key = XCAR (XCAR (extra));
2456 Lisp_Object val = XCDR (XCAR (extra)), val2;
2458 if (EQ (key, QClang))
2460 val2 = assq_no_quit (key, font_extra);
2461 if (NILP (val2))
2462 return 0;
2463 val2 = XCDR (val2);
2464 if (CONSP (val))
2466 if (! CONSP (val2))
2467 return 0;
2468 while (CONSP (val))
2469 if (NILP (Fmemq (val, val2)))
2470 return 0;
2472 else
2473 if (CONSP (val2)
2474 ? NILP (Fmemq (val, XCDR (val2)))
2475 : ! EQ (val, val2))
2476 return 0;
2478 else if (EQ (key, QCscript))
2480 val2 = assq_no_quit (val, Vscript_representative_chars);
2481 if (CONSP (val2))
2483 val2 = XCDR (val2);
2484 if (CONSP (val2))
2486 /* All characters in the list must be supported. */
2487 for (; CONSP (val2); val2 = XCDR (val2))
2489 if (! CHARACTERP (XCAR (val2)))
2490 continue;
2491 if (font_encode_char (font, XFASTINT (XCAR (val2)))
2492 == FONT_INVALID_CODE)
2493 return 0;
2496 else if (VECTORP (val2))
2498 /* At most one character in the vector must be supported. */
2499 for (i = 0; i < ASIZE (val2); i++)
2501 if (! CHARACTERP (AREF (val2, i)))
2502 continue;
2503 if (font_encode_char (font, XFASTINT (AREF (val2, i)))
2504 != FONT_INVALID_CODE)
2505 break;
2507 if (i == ASIZE (val2))
2508 return 0;
2512 else if (EQ (key, QCotf))
2514 struct font *fontp;
2516 if (! FONT_OBJECT_P (font))
2517 return 0;
2518 fontp = XFONT_OBJECT (font);
2519 if (! fontp->driver->otf_capability)
2520 return 0;
2521 val2 = fontp->driver->otf_capability (fontp);
2522 if (NILP (val2) || ! font_check_otf (val, val2))
2523 return 0;
2527 return 1;
2531 /* Font cache
2533 Each font backend has the callback function get_cache, and it
2534 returns a cons cell of which cdr part can be freely used for
2535 caching fonts. The cons cell may be shared by multiple frames
2536 and/or multiple font drivers. So, we arrange the cdr part as this:
2538 ((DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) ...)
2540 where DRIVER-TYPE is a symbol such as `x', `xft', etc., NUM-FRAMES
2541 is a number frames sharing this cache, and FONT-CACHE-DATA is a
2542 cons (FONT-SPEC . [FONT-ENTITY ...]). */
2544 static void font_prepare_cache (struct frame *, struct font_driver *);
2545 static void font_finish_cache (struct frame *, struct font_driver *);
2546 static Lisp_Object font_get_cache (struct frame *, struct font_driver *);
2547 static void font_clear_cache (struct frame *, Lisp_Object,
2548 struct font_driver *);
2550 static void
2551 font_prepare_cache (struct frame *f, struct font_driver *driver)
2553 Lisp_Object cache, val;
2555 cache = driver->get_cache (f);
2556 val = XCDR (cache);
2557 while (CONSP (val) && ! EQ (XCAR (XCAR (val)), driver->type))
2558 val = XCDR (val);
2559 if (NILP (val))
2561 val = list2 (driver->type, make_number (1));
2562 XSETCDR (cache, Fcons (val, XCDR (cache)));
2564 else
2566 val = XCDR (XCAR (val));
2567 XSETCAR (val, make_number (XINT (XCAR (val)) + 1));
2572 static void
2573 font_finish_cache (struct frame *f, struct font_driver *driver)
2575 Lisp_Object cache, val, tmp;
2578 cache = driver->get_cache (f);
2579 val = XCDR (cache);
2580 while (CONSP (val) && ! EQ (XCAR (XCAR (val)), driver->type))
2581 cache = val, val = XCDR (val);
2582 eassert (! NILP (val));
2583 tmp = XCDR (XCAR (val));
2584 XSETCAR (tmp, make_number (XINT (XCAR (tmp)) - 1));
2585 if (XINT (XCAR (tmp)) == 0)
2587 font_clear_cache (f, XCAR (val), driver);
2588 XSETCDR (cache, XCDR (val));
2593 static Lisp_Object
2594 font_get_cache (struct frame *f, struct font_driver *driver)
2596 Lisp_Object val = driver->get_cache (f);
2597 Lisp_Object type = driver->type;
2599 eassert (CONSP (val));
2600 for (val = XCDR (val); ! EQ (XCAR (XCAR (val)), type); val = XCDR (val));
2601 eassert (CONSP (val));
2602 /* VAL = ((DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) ...) */
2603 val = XCDR (XCAR (val));
2604 return val;
2608 static void
2609 font_clear_cache (struct frame *f, Lisp_Object cache, struct font_driver *driver)
2611 Lisp_Object tail, elt;
2612 Lisp_Object entity;
2613 ptrdiff_t i;
2615 /* CACHE = (DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) */
2616 for (tail = XCDR (XCDR (cache)); CONSP (tail); tail = XCDR (tail))
2618 elt = XCAR (tail);
2619 /* elt should have the form (FONT-SPEC . [FONT-ENTITY ...]) */
2620 if (CONSP (elt) && FONT_SPEC_P (XCAR (elt)))
2622 elt = XCDR (elt);
2623 eassert (VECTORP (elt));
2624 for (i = 0; i < ASIZE (elt); i++)
2626 entity = AREF (elt, i);
2628 if (FONT_ENTITY_P (entity)
2629 && EQ (driver->type, AREF (entity, FONT_TYPE_INDEX)))
2631 Lisp_Object objlist = AREF (entity, FONT_OBJLIST_INDEX);
2633 for (; CONSP (objlist); objlist = XCDR (objlist))
2635 Lisp_Object val = XCAR (objlist);
2636 struct font *font = XFONT_OBJECT (val);
2638 if (! NILP (AREF (val, FONT_TYPE_INDEX)))
2640 eassert (font && driver == font->driver);
2641 driver->close (font);
2644 if (driver->free_entity)
2645 driver->free_entity (entity);
2650 XSETCDR (cache, Qnil);
2654 static Lisp_Object scratch_font_spec, scratch_font_prefer;
2656 /* Check each font-entity in VEC, and return a list of font-entities
2657 that satisfy these conditions:
2658 (1) matches with SPEC and SIZE if SPEC is not nil, and
2659 (2) doesn't match with any regexps in Vface_ignored_fonts (if non-nil).
2662 static Lisp_Object
2663 font_delete_unmatched (Lisp_Object vec, Lisp_Object spec, int size)
2665 Lisp_Object entity, val;
2666 enum font_property_index prop;
2667 ptrdiff_t i;
2669 for (val = Qnil, i = ASIZE (vec) - 1; i >= 0; i--)
2671 entity = AREF (vec, i);
2672 if (! NILP (Vface_ignored_fonts))
2674 char name[256];
2675 ptrdiff_t namelen;
2676 Lisp_Object tail, regexp;
2678 namelen = font_unparse_xlfd (entity, 0, name, 256);
2679 if (namelen >= 0)
2681 for (tail = Vface_ignored_fonts; CONSP (tail); tail = XCDR (tail))
2683 regexp = XCAR (tail);
2684 if (STRINGP (regexp)
2685 && fast_c_string_match_ignore_case (regexp, name,
2686 namelen) >= 0)
2687 break;
2689 if (CONSP (tail))
2690 continue;
2693 if (NILP (spec))
2695 val = Fcons (entity, val);
2696 continue;
2698 for (prop = FONT_WEIGHT_INDEX; prop < FONT_SIZE_INDEX; prop++)
2699 if (INTEGERP (AREF (spec, prop))
2700 && ((XINT (AREF (spec, prop)) >> 8)
2701 != (XINT (AREF (entity, prop)) >> 8)))
2702 prop = FONT_SPEC_MAX;
2703 if (prop < FONT_SPEC_MAX
2704 && size
2705 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
2707 int diff = XINT (AREF (entity, FONT_SIZE_INDEX)) - size;
2709 if (eabs (diff) > FONT_PIXEL_SIZE_QUANTUM)
2710 prop = FONT_SPEC_MAX;
2712 if (prop < FONT_SPEC_MAX
2713 && INTEGERP (AREF (spec, FONT_DPI_INDEX))
2714 && INTEGERP (AREF (entity, FONT_DPI_INDEX))
2715 && XINT (AREF (entity, FONT_DPI_INDEX)) != 0
2716 && ! EQ (AREF (spec, FONT_DPI_INDEX), AREF (entity, FONT_DPI_INDEX)))
2717 prop = FONT_SPEC_MAX;
2718 if (prop < FONT_SPEC_MAX
2719 && INTEGERP (AREF (spec, FONT_AVGWIDTH_INDEX))
2720 && INTEGERP (AREF (entity, FONT_AVGWIDTH_INDEX))
2721 && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) != 0
2722 && ! EQ (AREF (spec, FONT_AVGWIDTH_INDEX),
2723 AREF (entity, FONT_AVGWIDTH_INDEX)))
2724 prop = FONT_SPEC_MAX;
2725 if (prop < FONT_SPEC_MAX)
2726 val = Fcons (entity, val);
2728 return (Fvconcat (1, &val));
2732 /* Return a list of vectors of font-entities matching with SPEC on
2733 FRAME. Each elements in the list is a vector of entities from the
2734 same font-driver. */
2736 Lisp_Object
2737 font_list_entities (struct frame *f, Lisp_Object spec)
2739 struct font_driver_list *driver_list = f->font_driver_list;
2740 Lisp_Object ftype, val;
2741 Lisp_Object list = Qnil;
2742 int size;
2743 bool need_filtering = 0;
2744 int i;
2746 eassert (FONT_SPEC_P (spec));
2748 if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
2749 size = XINT (AREF (spec, FONT_SIZE_INDEX));
2750 else if (FLOATP (AREF (spec, FONT_SIZE_INDEX)))
2751 size = font_pixel_size (f, spec);
2752 else
2753 size = 0;
2755 ftype = AREF (spec, FONT_TYPE_INDEX);
2756 for (i = FONT_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX; i++)
2757 ASET (scratch_font_spec, i, AREF (spec, i));
2758 for (i = FONT_WEIGHT_INDEX; i < FONT_EXTRA_INDEX; i++)
2759 if (i != FONT_SPACING_INDEX)
2761 ASET (scratch_font_spec, i, Qnil);
2762 if (! NILP (AREF (spec, i)))
2763 need_filtering = 1;
2765 ASET (scratch_font_spec, FONT_SPACING_INDEX, AREF (spec, FONT_SPACING_INDEX));
2766 ASET (scratch_font_spec, FONT_EXTRA_INDEX, AREF (spec, FONT_EXTRA_INDEX));
2768 for (; driver_list; driver_list = driver_list->next)
2769 if (driver_list->on
2770 && (NILP (ftype) || EQ (driver_list->driver->type, ftype)))
2772 Lisp_Object cache = font_get_cache (f, driver_list->driver);
2774 ASET (scratch_font_spec, FONT_TYPE_INDEX, driver_list->driver->type);
2775 val = assoc_no_quit (scratch_font_spec, XCDR (cache));
2776 if (CONSP (val))
2777 val = XCDR (val);
2778 else
2780 val = driver_list->driver->list (f, scratch_font_spec);
2781 if (!NILP (val))
2783 Lisp_Object copy = copy_font_spec (scratch_font_spec);
2785 val = Fvconcat (1, &val);
2786 ASET (copy, FONT_TYPE_INDEX, driver_list->driver->type);
2787 XSETCDR (cache, Fcons (Fcons (copy, val), XCDR (cache)));
2790 if (VECTORP (val) && ASIZE (val) > 0
2791 && (need_filtering
2792 || ! NILP (Vface_ignored_fonts)))
2793 val = font_delete_unmatched (val, need_filtering ? spec : Qnil, size);
2794 if (VECTORP (val) && ASIZE (val) > 0)
2795 list = Fcons (val, list);
2798 list = Fnreverse (list);
2799 FONT_ADD_LOG ("list", spec, list);
2800 return list;
2804 /* Return a font entity matching with SPEC on FRAME. ATTRS, if non
2805 nil, is an array of face's attributes, which specifies preferred
2806 font-related attributes. */
2808 static Lisp_Object
2809 font_matching_entity (struct frame *f, Lisp_Object *attrs, Lisp_Object spec)
2811 struct font_driver_list *driver_list = f->font_driver_list;
2812 Lisp_Object ftype, size, entity;
2813 Lisp_Object work = copy_font_spec (spec);
2815 ftype = AREF (spec, FONT_TYPE_INDEX);
2816 size = AREF (spec, FONT_SIZE_INDEX);
2818 if (FLOATP (size))
2819 ASET (work, FONT_SIZE_INDEX, make_number (font_pixel_size (f, spec)));
2820 FONT_SET_STYLE (work, FONT_WEIGHT_INDEX, attrs[LFACE_WEIGHT_INDEX]);
2821 FONT_SET_STYLE (work, FONT_SLANT_INDEX, attrs[LFACE_SLANT_INDEX]);
2822 FONT_SET_STYLE (work, FONT_WIDTH_INDEX, attrs[LFACE_SWIDTH_INDEX]);
2824 entity = Qnil;
2825 for (; driver_list; driver_list = driver_list->next)
2826 if (driver_list->on
2827 && (NILP (ftype) || EQ (driver_list->driver->type, ftype)))
2829 Lisp_Object cache = font_get_cache (f, driver_list->driver);
2831 ASET (work, FONT_TYPE_INDEX, driver_list->driver->type);
2832 entity = assoc_no_quit (work, XCDR (cache));
2833 if (CONSP (entity))
2834 entity = AREF (XCDR (entity), 0);
2835 else
2837 entity = driver_list->driver->match (f, work);
2838 if (!NILP (entity))
2840 Lisp_Object copy = copy_font_spec (work);
2841 Lisp_Object match = Fvector (1, &entity);
2843 ASET (copy, FONT_TYPE_INDEX, driver_list->driver->type);
2844 XSETCDR (cache, Fcons (Fcons (copy, match), XCDR (cache)));
2847 if (! NILP (entity))
2848 break;
2850 FONT_ADD_LOG ("match", work, entity);
2851 return entity;
2855 /* Open a font of ENTITY and PIXEL_SIZE on frame F, and return the
2856 opened font object. */
2858 static Lisp_Object
2859 font_open_entity (struct frame *f, Lisp_Object entity, int pixel_size)
2861 struct font_driver_list *driver_list;
2862 Lisp_Object objlist, size, val, font_object;
2863 struct font *font;
2864 int min_width, height, psize;
2866 eassert (FONT_ENTITY_P (entity));
2867 size = AREF (entity, FONT_SIZE_INDEX);
2868 if (XINT (size) != 0)
2869 pixel_size = XINT (size);
2871 val = AREF (entity, FONT_TYPE_INDEX);
2872 for (driver_list = f->font_driver_list;
2873 driver_list && ! EQ (driver_list->driver->type, val);
2874 driver_list = driver_list->next);
2875 if (! driver_list)
2876 return Qnil;
2878 for (objlist = AREF (entity, FONT_OBJLIST_INDEX); CONSP (objlist);
2879 objlist = XCDR (objlist))
2881 Lisp_Object fn = XCAR (objlist);
2882 if (! NILP (AREF (fn, FONT_TYPE_INDEX))
2883 && XFONT_OBJECT (fn)->pixel_size == pixel_size)
2885 if (driver_list->driver->cached_font_ok == NULL
2886 || driver_list->driver->cached_font_ok (f, fn, entity))
2887 return fn;
2891 /* We always open a font of manageable size; i.e non-zero average
2892 width and height. */
2893 for (psize = pixel_size; ; psize++)
2895 font_object = driver_list->driver->open (f, entity, psize);
2896 if (NILP (font_object))
2897 return Qnil;
2898 font = XFONT_OBJECT (font_object);
2899 if (font->average_width > 0 && font->height > 0)
2900 break;
2902 ASET (font_object, FONT_SIZE_INDEX, make_number (pixel_size));
2903 FONT_ADD_LOG ("open", entity, font_object);
2904 ASET (entity, FONT_OBJLIST_INDEX,
2905 Fcons (font_object, AREF (entity, FONT_OBJLIST_INDEX)));
2907 font = XFONT_OBJECT (font_object);
2908 min_width = (font->min_width ? font->min_width
2909 : font->average_width ? font->average_width
2910 : font->space_width ? font->space_width
2911 : 1);
2913 int font_ascent, font_descent;
2914 get_font_ascent_descent (font, &font_ascent, &font_descent);
2915 height = font_ascent + font_descent;
2916 if (height <= 0)
2917 height = 1;
2918 #ifdef HAVE_WINDOW_SYSTEM
2919 FRAME_DISPLAY_INFO (f)->n_fonts++;
2920 if (FRAME_DISPLAY_INFO (f)->n_fonts == 1)
2922 FRAME_SMALLEST_CHAR_WIDTH (f) = min_width;
2923 FRAME_SMALLEST_FONT_HEIGHT (f) = height;
2924 f->fonts_changed = 1;
2926 else
2928 if (FRAME_SMALLEST_CHAR_WIDTH (f) > min_width)
2929 FRAME_SMALLEST_CHAR_WIDTH (f) = min_width, f->fonts_changed = 1;
2930 if (FRAME_SMALLEST_FONT_HEIGHT (f) > height)
2931 FRAME_SMALLEST_FONT_HEIGHT (f) = height, f->fonts_changed = 1;
2933 #endif
2935 return font_object;
2939 /* Close FONT_OBJECT that is opened on frame F. */
2941 static void
2942 font_close_object (struct frame *f, Lisp_Object font_object)
2944 struct font *font = XFONT_OBJECT (font_object);
2946 if (NILP (AREF (font_object, FONT_TYPE_INDEX)))
2947 /* Already closed. */
2948 return;
2949 FONT_ADD_LOG ("close", font_object, Qnil);
2950 font->driver->close (font);
2951 #ifdef HAVE_WINDOW_SYSTEM
2952 eassert (FRAME_DISPLAY_INFO (f)->n_fonts);
2953 FRAME_DISPLAY_INFO (f)->n_fonts--;
2954 #endif
2958 /* Return 1 if FONT on F has a glyph for character C, 0 if not, -1 if
2959 FONT is a font-entity and it must be opened to check. */
2962 font_has_char (struct frame *f, Lisp_Object font, int c)
2964 struct font *fontp;
2966 if (FONT_ENTITY_P (font))
2968 Lisp_Object type = AREF (font, FONT_TYPE_INDEX);
2969 struct font_driver_list *driver_list;
2971 for (driver_list = f->font_driver_list;
2972 driver_list && ! EQ (driver_list->driver->type, type);
2973 driver_list = driver_list->next);
2974 if (! driver_list)
2975 return 0;
2976 if (! driver_list->driver->has_char)
2977 return -1;
2978 return driver_list->driver->has_char (font, c);
2981 eassert (FONT_OBJECT_P (font));
2982 fontp = XFONT_OBJECT (font);
2983 if (fontp->driver->has_char)
2985 int result = fontp->driver->has_char (font, c);
2987 if (result >= 0)
2988 return result;
2990 return (fontp->driver->encode_char (fontp, c) != FONT_INVALID_CODE);
2994 /* Return the glyph ID of FONT_OBJECT for character C. */
2996 static unsigned
2997 font_encode_char (Lisp_Object font_object, int c)
2999 struct font *font;
3001 eassert (FONT_OBJECT_P (font_object));
3002 font = XFONT_OBJECT (font_object);
3003 return font->driver->encode_char (font, c);
3007 /* Return the name of FONT_OBJECT. */
3009 Lisp_Object
3010 font_get_name (Lisp_Object font_object)
3012 eassert (FONT_OBJECT_P (font_object));
3013 return AREF (font_object, FONT_NAME_INDEX);
3017 /* Create a new font spec from FONT_NAME, and return it. If FONT_NAME
3018 could not be parsed by font_parse_name, return Qnil. */
3020 Lisp_Object
3021 font_spec_from_name (Lisp_Object font_name)
3023 Lisp_Object spec = Ffont_spec (0, NULL);
3025 CHECK_STRING (font_name);
3026 if (font_parse_name (SSDATA (font_name), SBYTES (font_name), spec) == -1)
3027 return Qnil;
3028 font_put_extra (spec, QCname, font_name);
3029 font_put_extra (spec, QCuser_spec, font_name);
3030 return spec;
3034 void
3035 font_clear_prop (Lisp_Object *attrs, enum font_property_index prop)
3037 Lisp_Object font = attrs[LFACE_FONT_INDEX];
3039 if (! FONTP (font))
3040 return;
3042 if (! NILP (Ffont_get (font, QCname)))
3044 font = copy_font_spec (font);
3045 font_put_extra (font, QCname, Qnil);
3048 if (NILP (AREF (font, prop))
3049 && prop != FONT_FAMILY_INDEX
3050 && prop != FONT_FOUNDRY_INDEX
3051 && prop != FONT_WIDTH_INDEX
3052 && prop != FONT_SIZE_INDEX)
3053 return;
3054 if (EQ (font, attrs[LFACE_FONT_INDEX]))
3055 font = copy_font_spec (font);
3056 ASET (font, prop, Qnil);
3057 if (prop == FONT_FAMILY_INDEX || prop == FONT_FOUNDRY_INDEX)
3059 if (prop == FONT_FAMILY_INDEX)
3061 ASET (font, FONT_FOUNDRY_INDEX, Qnil);
3062 /* If we are setting the font family, we must also clear
3063 FONT_WIDTH_INDEX to avoid rejecting families that lack
3064 support for some widths. */
3065 ASET (font, FONT_WIDTH_INDEX, Qnil);
3067 ASET (font, FONT_ADSTYLE_INDEX, Qnil);
3068 ASET (font, FONT_REGISTRY_INDEX, Qnil);
3069 ASET (font, FONT_SIZE_INDEX, Qnil);
3070 ASET (font, FONT_DPI_INDEX, Qnil);
3071 ASET (font, FONT_SPACING_INDEX, Qnil);
3072 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3074 else if (prop == FONT_SIZE_INDEX)
3076 ASET (font, FONT_DPI_INDEX, Qnil);
3077 ASET (font, FONT_SPACING_INDEX, Qnil);
3078 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3080 else if (prop == FONT_WIDTH_INDEX)
3081 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3082 attrs[LFACE_FONT_INDEX] = font;
3085 /* Select a font from ENTITIES (list of font-entity vectors) that
3086 supports C and is the best match for ATTRS and PIXEL_SIZE. */
3088 static Lisp_Object
3089 font_select_entity (struct frame *f, Lisp_Object entities,
3090 Lisp_Object *attrs, int pixel_size, int c)
3092 Lisp_Object font_entity;
3093 Lisp_Object prefer;
3094 int i;
3096 if (NILP (XCDR (entities))
3097 && ASIZE (XCAR (entities)) == 1)
3099 font_entity = AREF (XCAR (entities), 0);
3100 if (c < 0 || font_has_char (f, font_entity, c) > 0)
3101 return font_entity;
3102 return Qnil;
3105 /* Sort fonts by properties specified in ATTRS. */
3106 prefer = scratch_font_prefer;
3108 for (i = FONT_WEIGHT_INDEX; i <= FONT_SIZE_INDEX; i++)
3109 ASET (prefer, i, Qnil);
3110 if (FONTP (attrs[LFACE_FONT_INDEX]))
3112 Lisp_Object face_font = attrs[LFACE_FONT_INDEX];
3114 for (i = FONT_WEIGHT_INDEX; i <= FONT_SIZE_INDEX; i++)
3115 ASET (prefer, i, AREF (face_font, i));
3117 if (NILP (AREF (prefer, FONT_WEIGHT_INDEX)))
3118 FONT_SET_STYLE (prefer, FONT_WEIGHT_INDEX, attrs[LFACE_WEIGHT_INDEX]);
3119 if (NILP (AREF (prefer, FONT_SLANT_INDEX)))
3120 FONT_SET_STYLE (prefer, FONT_SLANT_INDEX, attrs[LFACE_SLANT_INDEX]);
3121 if (NILP (AREF (prefer, FONT_WIDTH_INDEX)))
3122 FONT_SET_STYLE (prefer, FONT_WIDTH_INDEX, attrs[LFACE_SWIDTH_INDEX]);
3123 ASET (prefer, FONT_SIZE_INDEX, make_number (pixel_size));
3125 return font_sort_entities (entities, prefer, f, c);
3128 /* Return a font-entity that satisfies SPEC and is the best match for
3129 face's font related attributes in ATTRS. C, if not negative, is a
3130 character that the entity must support. */
3132 Lisp_Object
3133 font_find_for_lface (struct frame *f, Lisp_Object *attrs, Lisp_Object spec, int c)
3135 Lisp_Object work;
3136 Lisp_Object entities, val;
3137 Lisp_Object foundry[3], *family, registry[3], adstyle[3];
3138 int pixel_size;
3139 int i, j, k, l;
3140 USE_SAFE_ALLOCA;
3142 registry[0] = AREF (spec, FONT_REGISTRY_INDEX);
3143 if (NILP (registry[0]))
3145 registry[0] = DEFAULT_ENCODING;
3146 registry[1] = Qascii_0;
3147 registry[2] = zero_vector;
3149 else
3150 registry[1] = zero_vector;
3152 if (c >= 0 && ! NILP (AREF (spec, FONT_REGISTRY_INDEX)))
3154 struct charset *encoding, *repertory;
3156 if (font_registry_charsets (AREF (spec, FONT_REGISTRY_INDEX),
3157 &encoding, &repertory) < 0)
3158 return Qnil;
3159 if (repertory
3160 && ENCODE_CHAR (repertory, c) == CHARSET_INVALID_CODE (repertory))
3161 return Qnil;
3162 else if (c > encoding->max_char)
3163 return Qnil;
3166 work = copy_font_spec (spec);
3167 ASET (work, FONT_TYPE_INDEX, AREF (spec, FONT_TYPE_INDEX));
3168 pixel_size = font_pixel_size (f, spec);
3169 if (pixel_size == 0 && INTEGERP (attrs[LFACE_HEIGHT_INDEX]))
3171 double pt = XINT (attrs[LFACE_HEIGHT_INDEX]);
3173 pixel_size = POINT_TO_PIXEL (pt / 10, FRAME_RES_Y (f));
3174 if (pixel_size < 1)
3175 pixel_size = 1;
3177 ASET (work, FONT_SIZE_INDEX, Qnil);
3178 foundry[0] = AREF (work, FONT_FOUNDRY_INDEX);
3179 if (! NILP (foundry[0]))
3180 foundry[1] = zero_vector;
3181 else if (STRINGP (attrs[LFACE_FOUNDRY_INDEX]))
3183 val = attrs[LFACE_FOUNDRY_INDEX];
3184 foundry[0] = font_intern_prop (SSDATA (val), SBYTES (val), 1);
3185 foundry[1] = Qnil;
3186 foundry[2] = zero_vector;
3188 else
3189 foundry[0] = Qnil, foundry[1] = zero_vector;
3191 adstyle[0] = AREF (work, FONT_ADSTYLE_INDEX);
3192 if (! NILP (adstyle[0]))
3193 adstyle[1] = zero_vector;
3194 else if (FONTP (attrs[LFACE_FONT_INDEX]))
3196 Lisp_Object face_font = attrs[LFACE_FONT_INDEX];
3198 if (! NILP (AREF (face_font, FONT_ADSTYLE_INDEX)))
3200 adstyle[0] = AREF (face_font, FONT_ADSTYLE_INDEX);
3201 adstyle[1] = Qnil;
3202 adstyle[2] = zero_vector;
3204 else
3205 adstyle[0] = Qnil, adstyle[1] = zero_vector;
3207 else
3208 adstyle[0] = Qnil, adstyle[1] = zero_vector;
3211 val = AREF (work, FONT_FAMILY_INDEX);
3212 if (NILP (val) && STRINGP (attrs[LFACE_FAMILY_INDEX]))
3214 val = attrs[LFACE_FAMILY_INDEX];
3215 val = font_intern_prop (SSDATA (val), SBYTES (val), 1);
3217 Lisp_Object familybuf[3];
3218 if (NILP (val))
3220 family = familybuf;
3221 family[0] = Qnil;
3222 family[1] = zero_vector; /* terminator. */
3224 else
3226 Lisp_Object alters
3227 = Fassoc_string (val, Vface_alternative_font_family_alist, Qt);
3229 if (! NILP (alters))
3231 EMACS_INT alterslen = XFASTINT (Flength (alters));
3232 SAFE_ALLOCA_LISP (family, alterslen + 2);
3233 for (i = 0; CONSP (alters); i++, alters = XCDR (alters))
3234 family[i] = XCAR (alters);
3235 if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
3236 family[i++] = Qnil;
3237 family[i] = zero_vector;
3239 else
3241 family = familybuf;
3242 i = 0;
3243 family[i++] = val;
3244 if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
3245 family[i++] = Qnil;
3246 family[i] = zero_vector;
3250 for (i = 0; SYMBOLP (family[i]); i++)
3252 ASET (work, FONT_FAMILY_INDEX, family[i]);
3253 for (j = 0; SYMBOLP (foundry[j]); j++)
3255 ASET (work, FONT_FOUNDRY_INDEX, foundry[j]);
3256 for (k = 0; SYMBOLP (registry[k]); k++)
3258 ASET (work, FONT_REGISTRY_INDEX, registry[k]);
3259 for (l = 0; SYMBOLP (adstyle[l]); l++)
3261 ASET (work, FONT_ADSTYLE_INDEX, adstyle[l]);
3262 entities = font_list_entities (f, work);
3263 if (! NILP (entities))
3265 val = font_select_entity (f, entities,
3266 attrs, pixel_size, c);
3267 if (! NILP (val))
3269 SAFE_FREE ();
3270 return val;
3278 SAFE_FREE ();
3279 return Qnil;
3283 Lisp_Object
3284 font_open_for_lface (struct frame *f, Lisp_Object entity, Lisp_Object *attrs, Lisp_Object spec)
3286 int size;
3288 if (INTEGERP (AREF (entity, FONT_SIZE_INDEX))
3289 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
3290 size = XINT (AREF (entity, FONT_SIZE_INDEX));
3291 else
3293 if (FONT_SPEC_P (spec) && ! NILP (AREF (spec, FONT_SIZE_INDEX)))
3294 size = font_pixel_size (f, spec);
3295 else
3297 double pt;
3298 if (INTEGERP (attrs[LFACE_HEIGHT_INDEX]))
3299 pt = XINT (attrs[LFACE_HEIGHT_INDEX]);
3300 else
3302 struct face *def = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3303 Lisp_Object height = def->lface[LFACE_HEIGHT_INDEX];
3304 eassert (INTEGERP (height));
3305 pt = XINT (height);
3308 pt /= 10;
3309 size = POINT_TO_PIXEL (pt, FRAME_RES_Y (f));
3310 #ifdef HAVE_NS
3311 if (size == 0)
3313 Lisp_Object ffsize = get_frame_param (f, Qfontsize);
3314 size = (NUMBERP (ffsize)
3315 ? POINT_TO_PIXEL (XINT (ffsize), FRAME_RES_Y (f)) : 0);
3317 #endif
3319 size *= font_rescale_ratio (entity);
3322 return font_open_entity (f, entity, size);
3326 /* Find a font that satisfies SPEC and is the best match for
3327 face's attributes in ATTRS on FRAME, and return the opened
3328 font-object. */
3330 Lisp_Object
3331 font_load_for_lface (struct frame *f, Lisp_Object *attrs, Lisp_Object spec)
3333 Lisp_Object entity, name;
3335 entity = font_find_for_lface (f, attrs, spec, -1);
3336 if (NILP (entity))
3338 /* No font is listed for SPEC, but each font-backend may have
3339 different criteria about "font matching". So, try it. */
3340 entity = font_matching_entity (f, attrs, spec);
3341 /* Perhaps the user asked for a font "Foobar-123", and we
3342 interpreted "-123" as the size, whereas it really is part of
3343 the name. So we reset the size to nil and the family name to
3344 the entire "Foobar-123" thing, and try again with that. */
3345 if (NILP (entity))
3347 name = Ffont_get (spec, QCuser_spec);
3348 if (STRINGP (name))
3350 char *p = SSDATA (name), *q = strrchr (p, '-');
3352 if (q != NULL && c_isdigit (q[1]))
3354 char *tail;
3355 double font_size = strtod (q + 1, &tail);
3357 if (font_size > 0 && tail != q + 1)
3359 Lisp_Object lsize = Ffont_get (spec, QCsize);
3361 if ((FLOATP (lsize) && XFLOAT_DATA (lsize) == font_size)
3362 || (INTEGERP (lsize) && XINT (lsize) == font_size))
3364 ASET (spec, FONT_FAMILY_INDEX,
3365 font_intern_prop (p, tail - p, 1));
3366 ASET (spec, FONT_SIZE_INDEX, Qnil);
3367 entity = font_matching_entity (f, attrs, spec);
3373 if (NILP (entity))
3374 return Qnil;
3376 /* Don't lose the original name that was put in initially. We need
3377 it to re-apply the font when font parameters (like hinting or dpi) have
3378 changed. */
3379 entity = font_open_for_lface (f, entity, attrs, spec);
3380 if (!NILP (entity))
3382 name = Ffont_get (spec, QCuser_spec);
3383 if (STRINGP (name)) font_put_extra (entity, QCuser_spec, name);
3385 return entity;
3389 /* Make FACE on frame F ready to use the font opened for FACE. */
3391 void
3392 font_prepare_for_face (struct frame *f, struct face *face)
3394 if (face->font->driver->prepare_face)
3395 face->font->driver->prepare_face (f, face);
3399 /* Make FACE on frame F stop using the font opened for FACE. */
3401 void
3402 font_done_for_face (struct frame *f, struct face *face)
3404 if (face->font->driver->done_face)
3405 face->font->driver->done_face (f, face);
3409 /* Open a font that is a match for font-spec SPEC on frame F. If no proper
3410 font is found, return Qnil. */
3412 Lisp_Object
3413 font_open_by_spec (struct frame *f, Lisp_Object spec)
3415 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3417 /* We set up the default font-related attributes of a face to prefer
3418 a moderate font. */
3419 attrs[LFACE_FAMILY_INDEX] = attrs[LFACE_FOUNDRY_INDEX] = Qnil;
3420 attrs[LFACE_SWIDTH_INDEX] = attrs[LFACE_WEIGHT_INDEX]
3421 = attrs[LFACE_SLANT_INDEX] = Qnormal;
3422 #ifndef HAVE_NS
3423 attrs[LFACE_HEIGHT_INDEX] = make_number (120);
3424 #else
3425 attrs[LFACE_HEIGHT_INDEX] = make_number (0);
3426 #endif
3427 attrs[LFACE_FONT_INDEX] = Qnil;
3429 return font_load_for_lface (f, attrs, spec);
3433 /* Open a font that matches NAME on frame F. If no proper font is
3434 found, return Qnil. */
3436 Lisp_Object
3437 font_open_by_name (struct frame *f, Lisp_Object name)
3439 Lisp_Object spec = CALLN (Ffont_spec, QCname, name);
3440 Lisp_Object ret = font_open_by_spec (f, spec);
3441 /* Do not lose name originally put in. */
3442 if (!NILP (ret))
3443 font_put_extra (ret, QCuser_spec, name);
3445 return ret;
3449 /* Register font-driver DRIVER. This function is used in two ways.
3451 The first is with frame F non-NULL. In this case, make DRIVER
3452 available (but not yet activated) on F. All frame creators
3453 (e.g. Fx_create_frame) must call this function at least once with
3454 an available font-driver.
3456 The second is with frame F NULL. In this case, DRIVER is globally
3457 registered in the variable `font_driver_list'. All font-driver
3458 implementations must call this function in its syms_of_XXXX
3459 (e.g. syms_of_xfont). */
3461 void
3462 register_font_driver (struct font_driver *driver, struct frame *f)
3464 struct font_driver_list *root = f ? f->font_driver_list : font_driver_list;
3465 struct font_driver_list *prev, *list;
3467 #ifdef HAVE_WINDOW_SYSTEM
3468 if (f && ! driver->draw)
3469 error ("Unusable font driver for a frame: %s",
3470 SDATA (SYMBOL_NAME (driver->type)));
3471 #endif /* HAVE_WINDOW_SYSTEM */
3473 for (prev = NULL, list = root; list; prev = list, list = list->next)
3474 if (EQ (list->driver->type, driver->type))
3475 error ("Duplicated font driver: %s", SDATA (SYMBOL_NAME (driver->type)));
3477 list = xmalloc (sizeof *list);
3478 list->on = 0;
3479 list->driver = driver;
3480 list->next = NULL;
3481 if (prev)
3482 prev->next = list;
3483 else if (f)
3484 f->font_driver_list = list;
3485 else
3486 font_driver_list = list;
3487 if (! f)
3488 num_font_drivers++;
3491 void
3492 free_font_driver_list (struct frame *f)
3494 struct font_driver_list *list, *next;
3496 for (list = f->font_driver_list; list; list = next)
3498 next = list->next;
3499 xfree (list);
3501 f->font_driver_list = NULL;
3505 /* Make the frame F use font backends listed in NEW_DRIVERS (list of
3506 symbols, e.g. xft, x). If NEW_DRIVERS is t, make F use all
3507 available font drivers. If NEW_DRIVERS is nil, finalize all drivers.
3509 A caller must free all realized faces if any in advance. The
3510 return value is a list of font backends actually made used on
3511 F. */
3513 Lisp_Object
3514 font_update_drivers (struct frame *f, Lisp_Object new_drivers)
3516 Lisp_Object active_drivers = Qnil;
3517 struct font_driver_list *list;
3519 /* At first, turn off non-requested drivers, and turn on requested
3520 drivers. */
3521 for (list = f->font_driver_list; list; list = list->next)
3523 struct font_driver *driver = list->driver;
3524 if ((EQ (new_drivers, Qt) || ! NILP (Fmemq (driver->type, new_drivers)))
3525 != list->on)
3527 if (list->on)
3529 if (driver->end_for_frame)
3530 driver->end_for_frame (f);
3531 font_finish_cache (f, driver);
3532 list->on = 0;
3534 else
3536 if (! driver->start_for_frame
3537 || driver->start_for_frame (f) == 0)
3539 font_prepare_cache (f, driver);
3540 list->on = 1;
3546 if (NILP (new_drivers))
3547 return Qnil;
3549 if (! EQ (new_drivers, Qt))
3551 /* Re-order the driver list according to new_drivers. */
3552 struct font_driver_list **list_table, **next;
3553 Lisp_Object tail;
3554 int i;
3555 USE_SAFE_ALLOCA;
3557 SAFE_NALLOCA (list_table, 1, num_font_drivers + 1);
3558 for (i = 0, tail = new_drivers; ! NILP (tail); tail = XCDR (tail))
3560 for (list = f->font_driver_list; list; list = list->next)
3561 if (list->on && EQ (list->driver->type, XCAR (tail)))
3562 break;
3563 if (list)
3564 list_table[i++] = list;
3566 for (list = f->font_driver_list; list; list = list->next)
3567 if (! list->on)
3568 list_table[i++] = list;
3569 list_table[i] = NULL;
3571 next = &f->font_driver_list;
3572 for (i = 0; list_table[i]; i++)
3574 *next = list_table[i];
3575 next = &(*next)->next;
3577 *next = NULL;
3578 SAFE_FREE ();
3580 if (! f->font_driver_list->on)
3581 { /* None of the drivers is enabled: enable them all.
3582 Happens if you set the list of drivers to (xft x) in your .emacs
3583 and then use it under w32 or ns. */
3584 for (list = f->font_driver_list; list; list = list->next)
3586 struct font_driver *driver = list->driver;
3587 eassert (! list->on);
3588 if (! driver->start_for_frame
3589 || driver->start_for_frame (f) == 0)
3591 font_prepare_cache (f, driver);
3592 list->on = 1;
3598 for (list = f->font_driver_list; list; list = list->next)
3599 if (list->on)
3600 active_drivers = nconc2 (active_drivers, list1 (list->driver->type));
3601 return active_drivers;
3604 #if defined (HAVE_XFT) || defined (HAVE_FREETYPE)
3606 static void
3607 fset_font_data (struct frame *f, Lisp_Object val)
3609 f->font_data = val;
3612 void
3613 font_put_frame_data (struct frame *f, Lisp_Object driver, void *data)
3615 Lisp_Object val = assq_no_quit (driver, f->font_data);
3617 if (!data)
3618 fset_font_data (f, Fdelq (val, f->font_data));
3619 else
3621 if (NILP (val))
3622 fset_font_data (f, Fcons (Fcons (driver, make_save_ptr (data)),
3623 f->font_data));
3624 else
3625 XSETCDR (val, make_save_ptr (data));
3629 void *
3630 font_get_frame_data (struct frame *f, Lisp_Object driver)
3632 Lisp_Object val = assq_no_quit (driver, f->font_data);
3634 return NILP (val) ? NULL : XSAVE_POINTER (XCDR (val), 0);
3637 #endif /* HAVE_XFT || HAVE_FREETYPE */
3639 /* Sets attributes on a font. Any properties that appear in ALIST and
3640 BOOLEAN_PROPERTIES or NON_BOOLEAN_PROPERTIES are set on the font.
3641 BOOLEAN_PROPERTIES and NON_BOOLEAN_PROPERTIES are NULL-terminated
3642 arrays of strings. This function is intended for use by the font
3643 drivers to implement their specific font_filter_properties. */
3644 void
3645 font_filter_properties (Lisp_Object font,
3646 Lisp_Object alist,
3647 const char *const boolean_properties[],
3648 const char *const non_boolean_properties[])
3650 Lisp_Object it;
3651 int i;
3653 /* Set boolean values to Qt or Qnil. */
3654 for (i = 0; boolean_properties[i] != NULL; ++i)
3655 for (it = alist; ! NILP (it); it = XCDR (it))
3657 Lisp_Object key = XCAR (XCAR (it));
3658 Lisp_Object val = XCDR (XCAR (it));
3659 char *keystr = SSDATA (SYMBOL_NAME (key));
3661 if (strcmp (boolean_properties[i], keystr) == 0)
3663 const char *str = INTEGERP (val) ? (XINT (val) ? "true" : "false")
3664 : SYMBOLP (val) ? SSDATA (SYMBOL_NAME (val))
3665 : "true";
3667 if (strcmp ("false", str) == 0 || strcmp ("False", str) == 0
3668 || strcmp ("FALSE", str) == 0 || strcmp ("FcFalse", str) == 0
3669 || strcmp ("off", str) == 0 || strcmp ("OFF", str) == 0
3670 || strcmp ("Off", str) == 0)
3671 val = Qnil;
3672 else
3673 val = Qt;
3675 Ffont_put (font, key, val);
3679 for (i = 0; non_boolean_properties[i] != NULL; ++i)
3680 for (it = alist; ! NILP (it); it = XCDR (it))
3682 Lisp_Object key = XCAR (XCAR (it));
3683 Lisp_Object val = XCDR (XCAR (it));
3684 char *keystr = SSDATA (SYMBOL_NAME (key));
3685 if (strcmp (non_boolean_properties[i], keystr) == 0)
3686 Ffont_put (font, key, val);
3691 /* Return the font used to draw character C by FACE at buffer position
3692 POS in window W. If STRING is non-nil, it is a string containing C
3693 at index POS. If C is negative, get C from the current buffer or
3694 STRING. */
3696 static Lisp_Object
3697 font_at (int c, ptrdiff_t pos, struct face *face, struct window *w,
3698 Lisp_Object string)
3700 struct frame *f;
3701 bool multibyte;
3702 Lisp_Object font_object;
3704 multibyte = (NILP (string)
3705 ? ! NILP (BVAR (current_buffer, enable_multibyte_characters))
3706 : STRING_MULTIBYTE (string));
3707 if (c < 0)
3709 if (NILP (string))
3711 if (multibyte)
3713 ptrdiff_t pos_byte = CHAR_TO_BYTE (pos);
3715 c = FETCH_CHAR (pos_byte);
3717 else
3718 c = FETCH_BYTE (pos);
3720 else
3722 unsigned char *str;
3724 multibyte = STRING_MULTIBYTE (string);
3725 if (multibyte)
3727 ptrdiff_t pos_byte = string_char_to_byte (string, pos);
3729 str = SDATA (string) + pos_byte;
3730 c = STRING_CHAR (str);
3732 else
3733 c = SDATA (string)[pos];
3737 f = XFRAME (w->frame);
3738 if (! FRAME_WINDOW_P (f))
3739 return Qnil;
3740 if (! face)
3742 int face_id;
3743 ptrdiff_t endptr;
3745 if (STRINGP (string))
3746 face_id = face_at_string_position (w, string, pos, 0, &endptr,
3747 DEFAULT_FACE_ID, false);
3748 else
3749 face_id = face_at_buffer_position (w, pos, &endptr,
3750 pos + 100, false, -1);
3751 face = FACE_FROM_ID (f, face_id);
3753 if (multibyte)
3755 int face_id = FACE_FOR_CHAR (f, face, c, pos, string);
3756 face = FACE_FROM_ID (f, face_id);
3758 if (! face->font)
3759 return Qnil;
3761 XSETFONT (font_object, face->font);
3762 return font_object;
3766 #ifdef HAVE_WINDOW_SYSTEM
3768 /* Check how many characters after character/byte position POS/POS_BYTE
3769 (at most to *LIMIT) can be displayed by the same font in the window W.
3770 FACE, if non-NULL, is the face selected for the character at POS.
3771 If STRING is not nil, it is the string to check instead of the current
3772 buffer. In that case, FACE must be not NULL.
3774 The return value is the font-object for the character at POS.
3775 *LIMIT is set to the position where that font can't be used.
3777 It is assured that the current buffer (or STRING) is multibyte. */
3779 Lisp_Object
3780 font_range (ptrdiff_t pos, ptrdiff_t pos_byte, ptrdiff_t *limit,
3781 struct window *w, struct face *face, Lisp_Object string)
3783 ptrdiff_t ignore;
3784 int c;
3785 Lisp_Object font_object = Qnil;
3787 if (NILP (string))
3789 if (! face)
3791 int face_id;
3793 face_id = face_at_buffer_position (w, pos, &ignore,
3794 *limit, false, -1);
3795 face = FACE_FROM_ID (XFRAME (w->frame), face_id);
3798 else
3799 eassert (face);
3801 while (pos < *limit)
3803 Lisp_Object category;
3805 if (NILP (string))
3806 FETCH_CHAR_ADVANCE_NO_CHECK (c, pos, pos_byte);
3807 else
3808 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, string, pos, pos_byte);
3809 category = CHAR_TABLE_REF (Vunicode_category_table, c);
3810 if (INTEGERP (category)
3811 && (XINT (category) == UNICODE_CATEGORY_Cf
3812 || CHAR_VARIATION_SELECTOR_P (c)))
3813 continue;
3814 if (NILP (font_object))
3816 font_object = font_for_char (face, c, pos - 1, string);
3817 if (NILP (font_object))
3818 return Qnil;
3819 continue;
3821 if (font_encode_char (font_object, c) == FONT_INVALID_CODE)
3822 *limit = pos - 1;
3824 return font_object;
3826 #endif
3829 /* Lisp API. */
3831 DEFUN ("fontp", Ffontp, Sfontp, 1, 2, 0,
3832 doc: /* Return t if OBJECT is a font-spec, font-entity, or font-object.
3833 Return nil otherwise.
3834 Optional 2nd argument EXTRA-TYPE, if non-nil, specifies to check
3835 which kind of font it is. It must be one of `font-spec', `font-entity',
3836 `font-object'. */)
3837 (Lisp_Object object, Lisp_Object extra_type)
3839 if (NILP (extra_type))
3840 return (FONTP (object) ? Qt : Qnil);
3841 if (EQ (extra_type, Qfont_spec))
3842 return (FONT_SPEC_P (object) ? Qt : Qnil);
3843 if (EQ (extra_type, Qfont_entity))
3844 return (FONT_ENTITY_P (object) ? Qt : Qnil);
3845 if (EQ (extra_type, Qfont_object))
3846 return (FONT_OBJECT_P (object) ? Qt : Qnil);
3847 wrong_type_argument (intern ("font-extra-type"), extra_type);
3850 DEFUN ("font-spec", Ffont_spec, Sfont_spec, 0, MANY, 0,
3851 doc: /* Return a newly created font-spec with arguments as properties.
3853 ARGS must come in pairs KEY VALUE of font properties. KEY must be a
3854 valid font property name listed below:
3856 `:family', `:weight', `:slant', `:width'
3858 They are the same as face attributes of the same name. See
3859 `set-face-attribute'.
3861 `:foundry'
3863 VALUE must be a string or a symbol specifying the font foundry, e.g. `misc'.
3865 `:adstyle'
3867 VALUE must be a string or a symbol specifying the additional
3868 typographic style information of a font, e.g. `sans'.
3870 `:registry'
3872 VALUE must be a string or a symbol specifying the charset registry and
3873 encoding of a font, e.g. `iso8859-1'.
3875 `:size'
3877 VALUE must be a non-negative integer or a floating point number
3878 specifying the font size. It specifies the font size in pixels (if
3879 VALUE is an integer), or in points (if VALUE is a float).
3881 `:name'
3883 VALUE must be a string of XLFD-style or fontconfig-style font name.
3885 `:script'
3887 VALUE must be a symbol representing a script that the font must
3888 support. It may be a symbol representing a subgroup of a script
3889 listed in the variable `script-representative-chars'.
3891 `:lang'
3893 VALUE must be a symbol whose name is a two-letter ISO-639 language
3894 name, e.g. `ja'. The value is matched against the "Additional Style"
3895 field of the XLFD spec of a font, if it's non-empty, on X, and
3896 against the codepages supported by the font on w32.
3898 `:otf'
3900 VALUE must be a list (SCRIPT-TAG LANGSYS-TAG GSUB [ GPOS ]) to specify
3901 required OpenType features.
3903 SCRIPT-TAG: OpenType script tag symbol (e.g. `deva').
3904 LANGSYS-TAG: OpenType language system tag symbol,
3905 or nil for the default language system.
3906 GSUB: List of OpenType GSUB feature tag symbols, or nil if none required.
3907 GPOS: List of OpenType GPOS feature tag symbols, or nil if none required.
3909 GSUB and GPOS may contain nil elements. In such a case, the font
3910 must not have any of the remaining elements.
3912 For instance, if the VALUE is `(thai nil nil (mark))', the font must
3913 be an OpenType font whose GPOS table of `thai' script's default
3914 language system must contain `mark' feature.
3916 usage: (font-spec ARGS...) */)
3917 (ptrdiff_t nargs, Lisp_Object *args)
3919 Lisp_Object spec = font_make_spec ();
3920 ptrdiff_t i;
3922 for (i = 0; i < nargs; i += 2)
3924 Lisp_Object key = args[i], val;
3926 CHECK_SYMBOL (key);
3927 if (i + 1 >= nargs)
3928 error ("No value for key `%s'", SDATA (SYMBOL_NAME (key)));
3929 val = args[i + 1];
3931 if (EQ (key, QCname))
3933 CHECK_STRING (val);
3934 if (font_parse_name (SSDATA (val), SBYTES (val), spec) < 0)
3935 error ("Invalid font name: %s", SSDATA (val));
3936 font_put_extra (spec, key, val);
3938 else
3940 int idx = get_font_prop_index (key);
3942 if (idx >= 0)
3944 val = font_prop_validate (idx, Qnil, val);
3945 if (idx < FONT_EXTRA_INDEX)
3946 ASET (spec, idx, val);
3947 else
3948 font_put_extra (spec, key, val);
3950 else
3951 font_put_extra (spec, key, font_prop_validate (0, key, val));
3954 return spec;
3957 /* Return a copy of FONT as a font-spec. For the sake of speed, this code
3958 relies on an internal stuff exposed from alloc.c and should be handled
3959 with care. */
3961 Lisp_Object
3962 copy_font_spec (Lisp_Object font)
3964 enum { font_spec_size = VECSIZE (struct font_spec) };
3965 Lisp_Object new_spec, tail, *pcdr;
3966 struct font_spec *spec;
3968 CHECK_FONT (font);
3970 /* Make an uninitialized font-spec object. */
3971 spec = (struct font_spec *) allocate_vector (font_spec_size);
3972 XSETPVECTYPESIZE (spec, PVEC_FONT, FONT_SPEC_MAX,
3973 font_spec_size - FONT_SPEC_MAX);
3975 spec->props[FONT_TYPE_INDEX] = spec->props[FONT_EXTRA_INDEX] = Qnil;
3977 /* Copy basic properties FONT_FOUNDRY_INDEX..FONT_AVGWIDTH_INDEX. */
3978 memcpy (spec->props + 1, XVECTOR (font)->contents + 1,
3979 (FONT_EXTRA_INDEX - 1) * word_size);
3981 /* Copy an alist of extra information but discard :font-entity property. */
3982 pcdr = spec->props + FONT_EXTRA_INDEX;
3983 for (tail = AREF (font, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail))
3984 if (!EQ (XCAR (XCAR (tail)), QCfont_entity))
3986 *pcdr = Fcons (Fcons (XCAR (XCAR (tail)), CDR (XCAR (tail))), Qnil);
3987 pcdr = xcdr_addr (*pcdr);
3990 XSETFONT (new_spec, spec);
3991 return new_spec;
3994 /* Merge font-specs FROM and TO, and return a new font-spec.
3995 Every specified property in FROM overrides the corresponding
3996 property in TO. */
3997 Lisp_Object
3998 merge_font_spec (Lisp_Object from, Lisp_Object to)
4000 Lisp_Object extra, tail;
4001 int i;
4003 CHECK_FONT (from);
4004 CHECK_FONT (to);
4005 to = copy_font_spec (to);
4006 for (i = 0; i < FONT_EXTRA_INDEX; i++)
4007 ASET (to, i, AREF (from, i));
4008 extra = AREF (to, FONT_EXTRA_INDEX);
4009 for (tail = AREF (from, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail))
4010 if (! EQ (XCAR (XCAR (tail)), Qfont_entity))
4012 Lisp_Object slot = assq_no_quit (XCAR (XCAR (tail)), extra);
4014 if (! NILP (slot))
4015 XSETCDR (slot, XCDR (XCAR (tail)));
4016 else
4017 extra = Fcons (Fcons (XCAR (XCAR (tail)), XCDR (XCAR (tail))), extra);
4019 ASET (to, FONT_EXTRA_INDEX, extra);
4020 return to;
4023 DEFUN ("font-get", Ffont_get, Sfont_get, 2, 2, 0,
4024 doc: /* Return the value of FONT's property KEY.
4025 FONT is a font-spec, a font-entity, or a font-object.
4026 KEY is any symbol, but these are reserved for specific meanings:
4027 :family, :weight, :slant, :width, :foundry, :adstyle, :registry,
4028 :size, :name, :script, :otf
4029 See the documentation of `font-spec' for their meanings.
4030 In addition, if FONT is a font-entity or a font-object, values of
4031 :script and :otf are different from those of a font-spec as below:
4033 The value of :script may be a list of scripts that are supported by the font.
4035 The value of :otf is a cons (GSUB . GPOS) where GSUB and GPOS are lists
4036 representing the OpenType features supported by the font by this form:
4037 ((SCRIPT (LANGSYS FEATURE ...) ...) ...)
4038 SCRIPT, LANGSYS, and FEATURE are all symbols representing OpenType
4039 Layout tags.
4041 In addition to the keys listed abobe, the following keys are reserved
4042 for the specific meanings as below:
4044 The value of :combining-capability is non-nil if the font-backend of
4045 FONT supports rendering of combining characters for non-OTF fonts. */)
4046 (Lisp_Object font, Lisp_Object key)
4048 int idx;
4049 Lisp_Object val;
4051 CHECK_FONT (font);
4052 CHECK_SYMBOL (key);
4054 idx = get_font_prop_index (key);
4055 if (idx >= FONT_WEIGHT_INDEX && idx <= FONT_WIDTH_INDEX)
4056 return font_style_symbolic (font, idx, 0);
4057 if (idx >= 0 && idx < FONT_EXTRA_INDEX)
4058 return AREF (font, idx);
4059 val = Fassq (key, AREF (font, FONT_EXTRA_INDEX));
4060 if (NILP (val) && FONT_OBJECT_P (font))
4062 struct font *fontp = XFONT_OBJECT (font);
4064 if (EQ (key, QCotf))
4066 if (fontp->driver->otf_capability)
4067 val = fontp->driver->otf_capability (fontp);
4068 else
4069 val = Fcons (Qnil, Qnil);
4071 else if (EQ (key, QCcombining_capability))
4073 if (fontp->driver->combining_capability)
4074 val = fontp->driver->combining_capability (fontp);
4077 else
4078 val = Fcdr (val);
4079 return val;
4082 #ifdef HAVE_WINDOW_SYSTEM
4084 DEFUN ("font-face-attributes", Ffont_face_attributes, Sfont_face_attributes, 1, 2, 0,
4085 doc: /* Return a plist of face attributes generated by FONT.
4086 FONT is a font name, a font-spec, a font-entity, or a font-object.
4087 The return value is a list of the form
4089 (:family FAMILY :height HEIGHT :weight WEIGHT :slant SLANT :width WIDTH)
4091 where FAMILY, HEIGHT, WEIGHT, SLANT, and WIDTH are face attribute values
4092 compatible with `set-face-attribute'. Some of these key-attribute pairs
4093 may be omitted from the list if they are not specified by FONT.
4095 The optional argument FRAME specifies the frame that the face attributes
4096 are to be displayed on. If omitted, the selected frame is used. */)
4097 (Lisp_Object font, Lisp_Object frame)
4099 struct frame *f = decode_live_frame (frame);
4100 Lisp_Object plist[10];
4101 Lisp_Object val;
4102 int n = 0;
4104 if (STRINGP (font))
4106 int fontset = fs_query_fontset (font, 0);
4107 Lisp_Object name = font;
4108 if (fontset >= 0)
4109 font = fontset_ascii (fontset);
4110 font = font_spec_from_name (name);
4111 if (! FONTP (font))
4112 signal_error ("Invalid font name", name);
4114 else if (! FONTP (font))
4115 signal_error ("Invalid font object", font);
4117 val = AREF (font, FONT_FAMILY_INDEX);
4118 if (! NILP (val))
4120 plist[n++] = QCfamily;
4121 plist[n++] = SYMBOL_NAME (val);
4124 val = AREF (font, FONT_SIZE_INDEX);
4125 if (INTEGERP (val))
4127 Lisp_Object font_dpi = AREF (font, FONT_DPI_INDEX);
4128 int dpi = INTEGERP (font_dpi) ? XINT (font_dpi) : FRAME_RES_Y (f);
4129 plist[n++] = QCheight;
4130 plist[n++] = make_number (PIXEL_TO_POINT (XINT (val) * 10, dpi));
4132 else if (FLOATP (val))
4134 plist[n++] = QCheight;
4135 plist[n++] = make_number (10 * (int) XFLOAT_DATA (val));
4138 val = FONT_WEIGHT_FOR_FACE (font);
4139 if (! NILP (val))
4141 plist[n++] = QCweight;
4142 plist[n++] = val;
4145 val = FONT_SLANT_FOR_FACE (font);
4146 if (! NILP (val))
4148 plist[n++] = QCslant;
4149 plist[n++] = val;
4152 val = FONT_WIDTH_FOR_FACE (font);
4153 if (! NILP (val))
4155 plist[n++] = QCwidth;
4156 plist[n++] = val;
4159 return Flist (n, plist);
4162 #endif
4164 DEFUN ("font-put", Ffont_put, Sfont_put, 3, 3, 0,
4165 doc: /* Set one property of FONT: give property KEY value VAL.
4166 FONT is a font-spec, a font-entity, or a font-object.
4168 If FONT is a font-spec, KEY can be any symbol. But if KEY is the one
4169 accepted by the function `font-spec' (which see), VAL must be what
4170 allowed in `font-spec'.
4172 If FONT is a font-entity or a font-object, KEY must not be the one
4173 accepted by `font-spec'. */)
4174 (Lisp_Object font, Lisp_Object prop, Lisp_Object val)
4176 int idx;
4178 idx = get_font_prop_index (prop);
4179 if (idx >= 0 && idx < FONT_EXTRA_INDEX)
4181 CHECK_FONT_SPEC (font);
4182 ASET (font, idx, font_prop_validate (idx, Qnil, val));
4184 else
4186 if (EQ (prop, QCname)
4187 || EQ (prop, QCscript)
4188 || EQ (prop, QClang)
4189 || EQ (prop, QCotf))
4190 CHECK_FONT_SPEC (font);
4191 else
4192 CHECK_FONT (font);
4193 font_put_extra (font, prop, font_prop_validate (0, prop, val));
4195 return val;
4198 DEFUN ("list-fonts", Flist_fonts, Slist_fonts, 1, 4, 0,
4199 doc: /* List available fonts matching FONT-SPEC on the current frame.
4200 Optional 2nd argument FRAME specifies the target frame.
4201 Optional 3rd argument NUM, if non-nil, limits the number of returned fonts.
4202 Optional 4th argument PREFER, if non-nil, is a font-spec to
4203 control the order of the returned list. Fonts are sorted by
4204 how close they are to PREFER. */)
4205 (Lisp_Object font_spec, Lisp_Object frame, Lisp_Object num, Lisp_Object prefer)
4207 struct frame *f = decode_live_frame (frame);
4208 Lisp_Object vec, list;
4209 EMACS_INT n = 0;
4211 CHECK_FONT_SPEC (font_spec);
4212 if (! NILP (num))
4214 CHECK_NUMBER (num);
4215 n = XINT (num);
4216 if (n <= 0)
4217 return Qnil;
4219 if (! NILP (prefer))
4220 CHECK_FONT_SPEC (prefer);
4222 list = font_list_entities (f, font_spec);
4223 if (NILP (list))
4224 return Qnil;
4225 if (NILP (XCDR (list))
4226 && ASIZE (XCAR (list)) == 1)
4227 return list1 (AREF (XCAR (list), 0));
4229 if (! NILP (prefer))
4230 vec = font_sort_entities (list, prefer, f, 0);
4231 else
4232 vec = font_vconcat_entity_vectors (list);
4233 if (n == 0 || n >= ASIZE (vec))
4234 list = CALLN (Fappend, vec, Qnil);
4235 else
4237 for (list = Qnil, n--; n >= 0; n--)
4238 list = Fcons (AREF (vec, n), list);
4240 return list;
4243 DEFUN ("font-family-list", Ffont_family_list, Sfont_family_list, 0, 1, 0,
4244 doc: /* List available font families on the current frame.
4245 If FRAME is omitted or nil, the selected frame is used. */)
4246 (Lisp_Object frame)
4248 struct frame *f = decode_live_frame (frame);
4249 struct font_driver_list *driver_list;
4250 Lisp_Object list = Qnil;
4252 for (driver_list = f->font_driver_list; driver_list;
4253 driver_list = driver_list->next)
4254 if (driver_list->driver->list_family)
4256 Lisp_Object val = driver_list->driver->list_family (f);
4257 Lisp_Object tail = list;
4259 for (; CONSP (val); val = XCDR (val))
4260 if (NILP (Fmemq (XCAR (val), tail))
4261 && SYMBOLP (XCAR (val)))
4262 list = Fcons (SYMBOL_NAME (XCAR (val)), list);
4264 return list;
4267 DEFUN ("find-font", Ffind_font, Sfind_font, 1, 2, 0,
4268 doc: /* Return a font-entity matching with FONT-SPEC on the current frame.
4269 Optional 2nd argument FRAME, if non-nil, specifies the target frame. */)
4270 (Lisp_Object font_spec, Lisp_Object frame)
4272 Lisp_Object val = Flist_fonts (font_spec, frame, make_number (1), Qnil);
4274 if (CONSP (val))
4275 val = XCAR (val);
4276 return val;
4279 DEFUN ("font-xlfd-name", Ffont_xlfd_name, Sfont_xlfd_name, 1, 2, 0,
4280 doc: /* Return XLFD name of FONT.
4281 FONT is a font-spec, font-entity, or font-object.
4282 If the name is too long for XLFD (maximum 255 chars), return nil.
4283 If the 2nd optional arg FOLD-WILDCARDS is non-nil,
4284 the consecutive wildcards are folded into one. */)
4285 (Lisp_Object font, Lisp_Object fold_wildcards)
4287 char name[256];
4288 int namelen, pixel_size = 0;
4290 CHECK_FONT (font);
4292 if (FONT_OBJECT_P (font))
4294 Lisp_Object font_name = AREF (font, FONT_NAME_INDEX);
4296 if (STRINGP (font_name)
4297 && SDATA (font_name)[0] == '-')
4299 if (NILP (fold_wildcards))
4300 return font_name;
4301 lispstpcpy (name, font_name);
4302 namelen = SBYTES (font_name);
4303 goto done;
4305 pixel_size = XFONT_OBJECT (font)->pixel_size;
4307 namelen = font_unparse_xlfd (font, pixel_size, name, 256);
4308 if (namelen < 0)
4309 return Qnil;
4310 done:
4311 if (! NILP (fold_wildcards))
4313 char *p0 = name, *p1;
4315 while ((p1 = strstr (p0, "-*-*")))
4317 strcpy (p1, p1 + 2);
4318 namelen -= 2;
4319 p0 = p1;
4323 return make_string (name, namelen);
4326 void
4327 clear_font_cache (struct frame *f)
4329 struct font_driver_list *driver_list = f->font_driver_list;
4331 for (; driver_list; driver_list = driver_list->next)
4332 if (driver_list->on)
4334 Lisp_Object val, tmp, cache = driver_list->driver->get_cache (f);
4336 val = XCDR (cache);
4337 while (! NILP (val)
4338 && ! EQ (XCAR (XCAR (val)), driver_list->driver->type))
4339 val = XCDR (val);
4340 eassert (! NILP (val));
4341 tmp = XCDR (XCAR (val));
4342 if (XINT (XCAR (tmp)) == 0)
4344 font_clear_cache (f, XCAR (val), driver_list->driver);
4345 XSETCDR (cache, XCDR (val));
4350 DEFUN ("clear-font-cache", Fclear_font_cache, Sclear_font_cache, 0, 0, 0,
4351 doc: /* Clear font cache of each frame. */)
4352 (void)
4354 Lisp_Object list, frame;
4356 FOR_EACH_FRAME (list, frame)
4357 clear_font_cache (XFRAME (frame));
4359 return Qnil;
4363 void
4364 font_fill_lglyph_metrics (Lisp_Object glyph, Lisp_Object font_object)
4366 struct font *font = XFONT_OBJECT (font_object);
4367 unsigned code = font->driver->encode_char (font, LGLYPH_CHAR (glyph));
4368 struct font_metrics metrics;
4370 LGLYPH_SET_CODE (glyph, code);
4371 font->driver->text_extents (font, &code, 1, &metrics);
4372 LGLYPH_SET_LBEARING (glyph, metrics.lbearing);
4373 LGLYPH_SET_RBEARING (glyph, metrics.rbearing);
4374 LGLYPH_SET_WIDTH (glyph, metrics.width);
4375 LGLYPH_SET_ASCENT (glyph, metrics.ascent);
4376 LGLYPH_SET_DESCENT (glyph, metrics.descent);
4380 DEFUN ("font-shape-gstring", Ffont_shape_gstring, Sfont_shape_gstring, 1, 1, 0,
4381 doc: /* Shape the glyph-string GSTRING.
4382 Shaping means substituting glyphs and/or adjusting positions of glyphs
4383 to get the correct visual image of character sequences set in the
4384 header of the glyph-string.
4386 If the shaping was successful, the value is GSTRING itself or a newly
4387 created glyph-string. Otherwise, the value is nil.
4389 See the documentation of `composition-get-gstring' for the format of
4390 GSTRING. */)
4391 (Lisp_Object gstring)
4393 struct font *font;
4394 Lisp_Object font_object, n, glyph;
4395 ptrdiff_t i, from, to;
4397 if (! composition_gstring_p (gstring))
4398 signal_error ("Invalid glyph-string: ", gstring);
4399 if (! NILP (LGSTRING_ID (gstring)))
4400 return gstring;
4401 font_object = LGSTRING_FONT (gstring);
4402 CHECK_FONT_OBJECT (font_object);
4403 font = XFONT_OBJECT (font_object);
4404 if (! font->driver->shape)
4405 return Qnil;
4407 /* Try at most three times with larger gstring each time. */
4408 for (i = 0; i < 3; i++)
4410 n = font->driver->shape (gstring);
4411 if (INTEGERP (n))
4412 break;
4413 gstring = larger_vector (gstring,
4414 LGSTRING_GLYPH_LEN (gstring), -1);
4416 if (i == 3 || XINT (n) == 0)
4417 return Qnil;
4418 if (XINT (n) < LGSTRING_GLYPH_LEN (gstring))
4419 LGSTRING_SET_GLYPH (gstring, XINT (n), Qnil);
4421 /* Check FROM_IDX and TO_IDX of each GLYPH in GSTRING to assure that
4422 GLYPHS covers all characters (except for the last few ones) in
4423 GSTRING. More formally, provided that NCHARS is the number of
4424 characters in GSTRING and GLYPHS[i] is the ith glyph, FROM_IDX
4425 and TO_IDX of each glyph must satisfy these conditions:
4427 GLYPHS[0].FROM_IDX == 0
4428 GLYPHS[i].FROM_IDX <= GLYPHS[i].TO_IDX
4429 if (GLYPHS[i].FROM_IDX == GLYPHS[i-1].FROM_IDX)
4430 ;; GLYPHS[i] and GLYPHS[i-1] belongs to the same grapheme cluster
4431 GLYPHS[i].TO_IDX == GLYPHS[i-1].TO_IDX
4432 else
4433 ;; Be sure to cover all characters.
4434 GLYPHS[i].FROM_IDX == GLYPHS[i-1].TO_IDX + 1 */
4435 glyph = LGSTRING_GLYPH (gstring, 0);
4436 from = LGLYPH_FROM (glyph);
4437 to = LGLYPH_TO (glyph);
4438 if (from != 0 || to < from)
4439 goto shaper_error;
4440 for (i = 1; i < LGSTRING_GLYPH_LEN (gstring); i++)
4442 glyph = LGSTRING_GLYPH (gstring, i);
4443 if (NILP (glyph))
4444 break;
4445 if (! (LGLYPH_FROM (glyph) <= LGLYPH_TO (glyph)
4446 && (LGLYPH_FROM (glyph) == from
4447 ? LGLYPH_TO (glyph) == to
4448 : LGLYPH_FROM (glyph) == to + 1)))
4449 goto shaper_error;
4450 from = LGLYPH_FROM (glyph);
4451 to = LGLYPH_TO (glyph);
4453 return composition_gstring_put_cache (gstring, XINT (n));
4455 shaper_error:
4456 return Qnil;
4459 DEFUN ("font-variation-glyphs", Ffont_variation_glyphs, Sfont_variation_glyphs,
4460 2, 2, 0,
4461 doc: /* Return a list of variation glyphs for CHAR in FONT-OBJECT.
4462 Each element of the value is a cons (VARIATION-SELECTOR . GLYPH-ID),
4463 where
4464 VARIATION-SELECTOR is a character code of variation selection
4465 (#xFE00..#xFE0F or #xE0100..#xE01EF)
4466 GLYPH-ID is a glyph code of the corresponding variation glyph. */)
4467 (Lisp_Object font_object, Lisp_Object character)
4469 unsigned variations[256];
4470 struct font *font;
4471 int i, n;
4472 Lisp_Object val;
4474 CHECK_FONT_OBJECT (font_object);
4475 CHECK_CHARACTER (character);
4476 font = XFONT_OBJECT (font_object);
4477 if (! font->driver->get_variation_glyphs)
4478 return Qnil;
4479 n = font->driver->get_variation_glyphs (font, XINT (character), variations);
4480 if (! n)
4481 return Qnil;
4482 val = Qnil;
4483 for (i = 0; i < 255; i++)
4484 if (variations[i])
4486 int vs = (i < 16 ? 0xFE00 + i : 0xE0100 + (i - 16));
4487 Lisp_Object code = INTEGER_TO_CONS (variations[i]);
4488 val = Fcons (Fcons (make_number (vs), code), val);
4490 return val;
4493 /* Return a description of the font at POSITION in the current buffer.
4494 If the 2nd optional arg CH is non-nil, it is a character to check
4495 the font instead of the character at POSITION.
4497 For a graphical display, return a cons (FONT-OBJECT . GLYPH-CODE).
4498 FONT-OBJECT is the font for the character at POSITION in the current
4499 buffer. This is computed from all the text properties and overlays
4500 that apply to POSITION. POSITION may be nil, in which case,
4501 FONT-SPEC is the font for displaying the character CH with the
4502 default face. GLYPH-CODE is the glyph code in the font to use for
4503 the character.
4505 For a text terminal, return a nonnegative integer glyph code for
4506 the character, or a negative integer if the character is not
4507 displayable. Terminal glyph codes are system-dependent integers
4508 that represent displayable characters: for example, on a Linux x86
4509 console they represent VGA code points.
4511 It returns nil in the following cases:
4513 (1) The window system doesn't have a font for the character (thus
4514 it is displayed by an empty box).
4516 (2) The character code is invalid.
4518 (3) If POSITION is not nil, and the current buffer is not displayed
4519 in any window.
4521 (4) For a text terminal, the terminal does not report glyph codes.
4523 In addition, the returned font name may not take into account of
4524 such redisplay engine hooks as what used in jit-lock-mode if
4525 POSITION is currently not visible. */
4528 DEFUN ("internal-char-font", Finternal_char_font, Sinternal_char_font, 1, 2, 0,
4529 doc: /* For internal use only. */)
4530 (Lisp_Object position, Lisp_Object ch)
4532 ptrdiff_t pos, pos_byte, dummy;
4533 int face_id;
4534 int c;
4535 struct frame *f;
4537 if (NILP (position))
4539 CHECK_CHARACTER (ch);
4540 c = XINT (ch);
4541 f = XFRAME (selected_frame);
4542 face_id = lookup_basic_face (f, DEFAULT_FACE_ID);
4543 pos = -1;
4545 else
4547 Lisp_Object window;
4548 struct window *w;
4550 CHECK_NUMBER_COERCE_MARKER (position);
4551 if (! (BEGV <= XINT (position) && XINT (position) < ZV))
4552 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
4553 pos = XINT (position);
4554 pos_byte = CHAR_TO_BYTE (pos);
4555 if (NILP (ch))
4556 c = FETCH_CHAR (pos_byte);
4557 else
4559 CHECK_NATNUM (ch);
4560 c = XINT (ch);
4562 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
4563 if (NILP (window))
4564 return Qnil;
4565 w = XWINDOW (window);
4566 f = XFRAME (w->frame);
4567 face_id = face_at_buffer_position (w, pos, &dummy,
4568 pos + 100, false, -1);
4570 if (! CHAR_VALID_P (c))
4571 return Qnil;
4573 if (! FRAME_WINDOW_P (f))
4574 return terminal_glyph_code (FRAME_TERMINAL (f), c);
4576 /* We need the basic faces to be valid below, so recompute them if
4577 some code just happened to clear the face cache. */
4578 if (FRAME_FACE_CACHE (f)->used == 0)
4579 recompute_basic_faces (f);
4581 face_id = FACE_FOR_CHAR (f, FACE_FROM_ID (f, face_id), c, pos, Qnil);
4582 struct face *face = FACE_FROM_ID (f, face_id);
4583 if (! face->font)
4584 return Qnil;
4585 unsigned code = face->font->driver->encode_char (face->font, c);
4586 if (code == FONT_INVALID_CODE)
4587 return Qnil;
4588 Lisp_Object font_object;
4589 XSETFONT (font_object, face->font);
4590 return Fcons (font_object, INTEGER_TO_CONS (code));
4593 #if 0
4595 DEFUN ("font-drive-otf", Ffont_drive_otf, Sfont_drive_otf, 6, 6, 0,
4596 doc: /* Apply OpenType features on glyph-string GSTRING-IN.
4597 OTF-FEATURES specifies which features to apply in this format:
4598 (SCRIPT LANGSYS GSUB GPOS)
4599 where
4600 SCRIPT is a symbol specifying a script tag of OpenType,
4601 LANGSYS is a symbol specifying a langsys tag of OpenType,
4602 GSUB and GPOS, if non-nil, are lists of symbols specifying feature tags.
4604 If LANGSYS is nil, the default langsys is selected.
4606 The features are applied in the order they appear in the list. The
4607 symbol `*' means to apply all available features not present in this
4608 list, and the remaining features are ignored. For instance, (vatu
4609 pstf * haln) is to apply vatu and pstf in this order, then to apply
4610 all available features other than vatu, pstf, and haln.
4612 The features are applied to the glyphs in the range FROM and TO of
4613 the glyph-string GSTRING-IN.
4615 If some feature is actually applicable, the resulting glyphs are
4616 produced in the glyph-string GSTRING-OUT from the index INDEX. In
4617 this case, the value is the number of produced glyphs.
4619 If no feature is applicable, no glyph is produced in GSTRING-OUT, and
4620 the value is 0.
4622 If GSTRING-OUT is too short to hold produced glyphs, no glyphs are
4623 produced in GSTRING-OUT, and the value is nil.
4625 See the documentation of `composition-get-gstring' for the format of
4626 glyph-string. */)
4627 (Lisp_Object otf_features, Lisp_Object gstring_in, Lisp_Object from, Lisp_Object to, Lisp_Object gstring_out, Lisp_Object index)
4629 Lisp_Object font_object = LGSTRING_FONT (gstring_in);
4630 Lisp_Object val;
4631 struct font *font;
4632 int len, num;
4634 check_otf_features (otf_features);
4635 CHECK_FONT_OBJECT (font_object);
4636 font = XFONT_OBJECT (font_object);
4637 if (! font->driver->otf_drive)
4638 error ("Font backend %s can't drive OpenType GSUB table",
4639 SDATA (SYMBOL_NAME (font->driver->type)));
4640 CHECK_CONS (otf_features);
4641 CHECK_SYMBOL (XCAR (otf_features));
4642 val = XCDR (otf_features);
4643 CHECK_SYMBOL (XCAR (val));
4644 val = XCDR (otf_features);
4645 if (! NILP (val))
4646 CHECK_CONS (val);
4647 len = check_gstring (gstring_in);
4648 CHECK_VECTOR (gstring_out);
4649 CHECK_NATNUM (from);
4650 CHECK_NATNUM (to);
4651 CHECK_NATNUM (index);
4653 if (XINT (from) >= XINT (to) || XINT (to) > len)
4654 args_out_of_range_3 (from, to, make_number (len));
4655 if (XINT (index) >= ASIZE (gstring_out))
4656 args_out_of_range (index, make_number (ASIZE (gstring_out)));
4657 num = font->driver->otf_drive (font, otf_features,
4658 gstring_in, XINT (from), XINT (to),
4659 gstring_out, XINT (index), 0);
4660 if (num < 0)
4661 return Qnil;
4662 return make_number (num);
4665 DEFUN ("font-otf-alternates", Ffont_otf_alternates, Sfont_otf_alternates,
4666 3, 3, 0,
4667 doc: /* Return a list of alternate glyphs of CHARACTER in FONT-OBJECT.
4668 OTF-FEATURES specifies which features of the font FONT-OBJECT to apply
4669 in this format:
4670 (SCRIPT LANGSYS FEATURE ...)
4671 See the documentation of `font-drive-otf' for more detail.
4673 The value is a list of cons cells of the format (GLYPH-ID . CHARACTER),
4674 where GLYPH-ID is a glyph index of the font, and CHARACTER is a
4675 character code corresponding to the glyph or nil if there's no
4676 corresponding character. */)
4677 (Lisp_Object font_object, Lisp_Object character, Lisp_Object otf_features)
4679 struct font *font = CHECK_FONT_GET_OBJECT (font_object);
4680 Lisp_Object gstring_in, gstring_out, g;
4681 Lisp_Object alternates;
4682 int i, num;
4684 if (! font->driver->otf_drive)
4685 error ("Font backend %s can't drive OpenType GSUB table",
4686 SDATA (SYMBOL_NAME (font->driver->type)));
4687 CHECK_CHARACTER (character);
4688 CHECK_CONS (otf_features);
4690 gstring_in = Ffont_make_gstring (font_object, make_number (1));
4691 g = LGSTRING_GLYPH (gstring_in, 0);
4692 LGLYPH_SET_CHAR (g, XINT (character));
4693 gstring_out = Ffont_make_gstring (font_object, make_number (10));
4694 while ((num = font->driver->otf_drive (font, otf_features, gstring_in, 0, 1,
4695 gstring_out, 0, 1)) < 0)
4696 gstring_out = Ffont_make_gstring (font_object,
4697 make_number (ASIZE (gstring_out) * 2));
4698 alternates = Qnil;
4699 for (i = 0; i < num; i++)
4701 Lisp_Object g = LGSTRING_GLYPH (gstring_out, i);
4702 int c = LGLYPH_CHAR (g);
4703 unsigned code = LGLYPH_CODE (g);
4705 alternates = Fcons (Fcons (make_number (code),
4706 c > 0 ? make_number (c) : Qnil),
4707 alternates);
4709 return Fnreverse (alternates);
4711 #endif /* 0 */
4713 #ifdef FONT_DEBUG
4715 DEFUN ("open-font", Fopen_font, Sopen_font, 1, 3, 0,
4716 doc: /* Open FONT-ENTITY. */)
4717 (Lisp_Object font_entity, Lisp_Object size, Lisp_Object frame)
4719 EMACS_INT isize;
4720 struct frame *f = decode_live_frame (frame);
4722 CHECK_FONT_ENTITY (font_entity);
4724 if (NILP (size))
4725 isize = XINT (AREF (font_entity, FONT_SIZE_INDEX));
4726 else
4728 CHECK_NUMBER_OR_FLOAT (size);
4729 if (FLOATP (size))
4730 isize = POINT_TO_PIXEL (XFLOAT_DATA (size), FRAME_RES_Y (f));
4731 else
4732 isize = XINT (size);
4733 if (! (INT_MIN <= isize && isize <= INT_MAX))
4734 args_out_of_range (font_entity, size);
4735 if (isize == 0)
4736 isize = 120;
4738 return font_open_entity (f, font_entity, isize);
4741 DEFUN ("close-font", Fclose_font, Sclose_font, 1, 2, 0,
4742 doc: /* Close FONT-OBJECT. */)
4743 (Lisp_Object font_object, Lisp_Object frame)
4745 CHECK_FONT_OBJECT (font_object);
4746 font_close_object (decode_live_frame (frame), font_object);
4747 return Qnil;
4750 DEFUN ("query-font", Fquery_font, Squery_font, 1, 1, 0,
4751 doc: /* Return information about FONT-OBJECT.
4752 The value is a vector:
4753 [ NAME FILENAME PIXEL-SIZE SIZE ASCENT DESCENT SPACE-WIDTH AVERAGE-WIDTH
4754 CAPABILITY ]
4756 NAME is the font name, a string (or nil if the font backend doesn't
4757 provide a name).
4759 FILENAME is the font file name, a string (or nil if the font backend
4760 doesn't provide a file name).
4762 PIXEL-SIZE is a pixel size by which the font is opened.
4764 SIZE is a maximum advance width of the font in pixels.
4766 ASCENT, DESCENT, SPACE-WIDTH, AVERAGE-WIDTH are metrics of the font in
4767 pixels.
4769 CAPABILITY is a list whose first element is a symbol representing the
4770 font format (x, opentype, truetype, type1, pcf, or bdf) and the
4771 remaining elements describe the details of the font capability.
4773 If the font is OpenType font, the form of the list is
4774 (opentype GSUB GPOS)
4775 where GSUB shows which "GSUB" features the font supports, and GPOS
4776 shows which "GPOS" features the font supports. Both GSUB and GPOS are
4777 lists of the format:
4778 ((SCRIPT (LANGSYS FEATURE ...) ...) ...)
4780 If the font is not OpenType font, currently the length of the form is
4781 one.
4783 SCRIPT is a symbol representing OpenType script tag.
4785 LANGSYS is a symbol representing OpenType langsys tag, or nil
4786 representing the default langsys.
4788 FEATURE is a symbol representing OpenType feature tag.
4790 If the font is not OpenType font, CAPABILITY is nil. */)
4791 (Lisp_Object font_object)
4793 struct font *font = CHECK_FONT_GET_OBJECT (font_object);
4794 Lisp_Object val = make_uninit_vector (9);
4796 ASET (val, 0, AREF (font_object, FONT_NAME_INDEX));
4797 ASET (val, 1, AREF (font_object, FONT_FILE_INDEX));
4798 ASET (val, 2, make_number (font->pixel_size));
4799 ASET (val, 3, make_number (font->max_width));
4800 ASET (val, 4, make_number (font->ascent));
4801 ASET (val, 5, make_number (font->descent));
4802 ASET (val, 6, make_number (font->space_width));
4803 ASET (val, 7, make_number (font->average_width));
4804 if (font->driver->otf_capability)
4805 ASET (val, 8, Fcons (Qopentype, font->driver->otf_capability (font)));
4806 else
4807 ASET (val, 8, Qnil);
4808 return val;
4811 DEFUN ("font-get-glyphs", Ffont_get_glyphs, Sfont_get_glyphs, 3, 4, 0,
4812 doc:
4813 /* Return a vector of FONT-OBJECT's glyphs for the specified characters.
4814 FROM and TO are positions (integers or markers) specifying a region
4815 of the current buffer, and can be in either order. If the optional
4816 fourth arg OBJECT is not nil, it is a string or a vector containing
4817 the target characters between indices FROM and TO, which are treated
4818 as in `substring'.
4820 Each element is a vector containing information of a glyph in this format:
4821 [FROM-IDX TO-IDX C CODE WIDTH LBEARING RBEARING ASCENT DESCENT ADJUSTMENT]
4822 where
4823 FROM is an index numbers of a character the glyph corresponds to.
4824 TO is the same as FROM.
4825 C is the character of the glyph.
4826 CODE is the glyph-code of C in FONT-OBJECT.
4827 WIDTH thru DESCENT are the metrics (in pixels) of the glyph.
4828 ADJUSTMENT is always nil.
4829 If FONT-OBJECT doesn't have a glyph for a character,
4830 the corresponding element is nil. */)
4831 (Lisp_Object font_object, Lisp_Object from, Lisp_Object to,
4832 Lisp_Object object)
4834 struct font *font = CHECK_FONT_GET_OBJECT (font_object);
4835 ptrdiff_t i, len;
4836 Lisp_Object *chars, vec;
4837 USE_SAFE_ALLOCA;
4839 if (NILP (object))
4841 ptrdiff_t charpos, bytepos;
4843 validate_region (&from, &to);
4844 if (EQ (from, to))
4845 return Qnil;
4846 len = XFASTINT (to) - XFASTINT (from);
4847 SAFE_ALLOCA_LISP (chars, len);
4848 charpos = XFASTINT (from);
4849 bytepos = CHAR_TO_BYTE (charpos);
4850 for (i = 0; charpos < XFASTINT (to); i++)
4852 int c;
4853 FETCH_CHAR_ADVANCE (c, charpos, bytepos);
4854 chars[i] = make_number (c);
4857 else if (STRINGP (object))
4859 const unsigned char *p;
4860 ptrdiff_t ifrom, ito;
4862 validate_subarray (object, from, to, SCHARS (object), &ifrom, &ito);
4863 if (ifrom == ito)
4864 return Qnil;
4865 len = ito - ifrom;
4866 SAFE_ALLOCA_LISP (chars, len);
4867 p = SDATA (object);
4868 if (STRING_MULTIBYTE (object))
4870 int c;
4872 /* Skip IFROM characters from the beginning. */
4873 for (i = 0; i < ifrom; i++)
4874 c = STRING_CHAR_ADVANCE (p);
4876 /* Now fetch an interesting characters. */
4877 for (i = 0; i < len; i++)
4879 c = STRING_CHAR_ADVANCE (p);
4880 chars[i] = make_number (c);
4883 else
4884 for (i = 0; i < len; i++)
4885 chars[i] = make_number (p[ifrom + i]);
4887 else if (VECTORP (object))
4889 ptrdiff_t ifrom, ito;
4891 validate_subarray (object, from, to, ASIZE (object), &ifrom, &ito);
4892 if (ifrom == ito)
4893 return Qnil;
4894 len = ito - ifrom;
4895 for (i = 0; i < len; i++)
4897 Lisp_Object elt = AREF (object, ifrom + i);
4898 CHECK_CHARACTER (elt);
4900 chars = aref_addr (object, ifrom);
4902 else
4903 wrong_type_argument (Qarrayp, object);
4905 vec = make_uninit_vector (len);
4906 for (i = 0; i < len; i++)
4908 Lisp_Object g;
4909 int c = XFASTINT (chars[i]);
4910 unsigned code;
4911 struct font_metrics metrics;
4913 code = font->driver->encode_char (font, c);
4914 if (code == FONT_INVALID_CODE)
4916 ASET (vec, i, Qnil);
4917 continue;
4919 g = LGLYPH_NEW ();
4920 LGLYPH_SET_FROM (g, i);
4921 LGLYPH_SET_TO (g, i);
4922 LGLYPH_SET_CHAR (g, c);
4923 LGLYPH_SET_CODE (g, code);
4924 font->driver->text_extents (font, &code, 1, &metrics);
4925 LGLYPH_SET_WIDTH (g, metrics.width);
4926 LGLYPH_SET_LBEARING (g, metrics.lbearing);
4927 LGLYPH_SET_RBEARING (g, metrics.rbearing);
4928 LGLYPH_SET_ASCENT (g, metrics.ascent);
4929 LGLYPH_SET_DESCENT (g, metrics.descent);
4930 ASET (vec, i, g);
4932 if (! VECTORP (object))
4933 SAFE_FREE ();
4934 return vec;
4937 DEFUN ("font-match-p", Ffont_match_p, Sfont_match_p, 2, 2, 0,
4938 doc: /* Return t if and only if font-spec SPEC matches with FONT.
4939 FONT is a font-spec, font-entity, or font-object. */)
4940 (Lisp_Object spec, Lisp_Object font)
4942 CHECK_FONT_SPEC (spec);
4943 CHECK_FONT (font);
4945 return (font_match_p (spec, font) ? Qt : Qnil);
4948 DEFUN ("font-at", Ffont_at, Sfont_at, 1, 3, 0,
4949 doc: /* Return a font-object for displaying a character at POSITION.
4950 Optional second arg WINDOW, if non-nil, is a window displaying
4951 the current buffer. It defaults to the currently selected window.
4952 Optional third arg STRING, if non-nil, is a string containing the target
4953 character at index specified by POSITION. */)
4954 (Lisp_Object position, Lisp_Object window, Lisp_Object string)
4956 struct window *w = decode_live_window (window);
4958 if (NILP (string))
4960 if (XBUFFER (w->contents) != current_buffer)
4961 error ("Specified window is not displaying the current buffer");
4962 CHECK_NUMBER_COERCE_MARKER (position);
4963 if (! (BEGV <= XINT (position) && XINT (position) < ZV))
4964 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
4966 else
4968 CHECK_NUMBER (position);
4969 CHECK_STRING (string);
4970 if (! (0 <= XINT (position) && XINT (position) < SCHARS (string)))
4971 args_out_of_range (string, position);
4974 return font_at (-1, XINT (position), NULL, w, string);
4977 #if 0
4978 DEFUN ("draw-string", Fdraw_string, Sdraw_string, 2, 2, 0,
4979 doc: /* Draw STRING by FONT-OBJECT on the top left corner of the current frame.
4980 The value is a number of glyphs drawn.
4981 Type C-l to recover what previously shown. */)
4982 (Lisp_Object font_object, Lisp_Object string)
4984 Lisp_Object frame = selected_frame;
4985 struct frame *f = XFRAME (frame);
4986 struct font *font;
4987 struct face *face;
4988 int i, len, width;
4989 unsigned *code;
4991 CHECK_FONT_GET_OBJECT (font_object, font);
4992 CHECK_STRING (string);
4993 len = SCHARS (string);
4994 code = alloca (sizeof (unsigned) * len);
4995 for (i = 0; i < len; i++)
4997 Lisp_Object ch = Faref (string, make_number (i));
4998 Lisp_Object val;
4999 int c = XINT (ch);
5001 code[i] = font->driver->encode_char (font, c);
5002 if (code[i] == FONT_INVALID_CODE)
5003 break;
5005 face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5006 face->fontp = font;
5007 if (font->driver->prepare_face)
5008 font->driver->prepare_face (f, face);
5009 width = font->driver->text_extents (font, code, i, NULL);
5010 len = font->driver->draw_text (f, face, 0, font->ascent, code, i, width);
5011 if (font->driver->done_face)
5012 font->driver->done_face (f, face);
5013 face->fontp = NULL;
5014 return make_number (len);
5016 #endif
5018 DEFUN ("frame-font-cache", Fframe_font_cache, Sframe_font_cache, 0, 1, 0,
5019 doc: /* Return FRAME's font cache. Mainly used for debugging.
5020 If FRAME is omitted or nil, use the selected frame. */)
5021 (Lisp_Object frame)
5023 #ifdef HAVE_WINDOW_SYSTEM
5024 struct frame *f = decode_live_frame (frame);
5026 if (FRAME_WINDOW_P (f))
5027 return FRAME_DISPLAY_INFO (f)->name_list_element;
5028 else
5029 #endif
5030 return Qnil;
5033 #endif /* FONT_DEBUG */
5035 #ifdef HAVE_WINDOW_SYSTEM
5037 DEFUN ("font-info", Ffont_info, Sfont_info, 1, 2, 0,
5038 doc: /* Return information about a font named NAME on frame FRAME.
5039 If FRAME is omitted or nil, use the selected frame.
5041 The returned value is a vector:
5042 [ OPENED-NAME FULL-NAME SIZE HEIGHT BASELINE-OFFSET RELATIVE-COMPOSE
5043 DEFAULT-ASCENT MAX-WIDTH ASCENT DESCENT SPACE-WIDTH AVERAGE-WIDTH
5044 CAPABILITY ]
5045 where
5046 OPENED-NAME is the name used for opening the font,
5047 FULL-NAME is the full name of the font,
5048 SIZE is the pixelsize of the font,
5049 HEIGHT is the pixel-height of the font (i.e., ascent + descent),
5050 BASELINE-OFFSET is the upward offset pixels from ASCII baseline,
5051 RELATIVE-COMPOSE and DEFAULT-ASCENT are the numbers controlling
5052 how to compose characters,
5053 MAX-WIDTH is the maximum advance width of the font,
5054 ASCENT, DESCENT, SPACE-WIDTH, AVERAGE-WIDTH are metrics of the font
5055 in pixels,
5056 FILENAME is the font file name, a string (or nil if the font backend
5057 doesn't provide a file name).
5058 CAPABILITY is a list whose first element is a symbol representing the
5059 font format, one of x, opentype, truetype, type1, pcf, or bdf.
5060 The remaining elements describe the details of the font capabilities,
5061 as follows:
5063 If the font is OpenType font, the form of the list is
5064 (opentype GSUB GPOS)
5065 where GSUB shows which "GSUB" features the font supports, and GPOS
5066 shows which "GPOS" features the font supports. Both GSUB and GPOS are
5067 lists of the form:
5068 ((SCRIPT (LANGSYS FEATURE ...) ...) ...)
5070 where
5071 SCRIPT is a symbol representing OpenType script tag.
5072 LANGSYS is a symbol representing OpenType langsys tag, or nil
5073 representing the default langsys.
5074 FEATURE is a symbol representing OpenType feature tag.
5076 If the font is not an OpenType font, there are no elements
5077 in CAPABILITY except the font format symbol.
5079 If the named font is not yet loaded, return nil. */)
5080 (Lisp_Object name, Lisp_Object frame)
5082 struct frame *f;
5083 struct font *font;
5084 Lisp_Object info;
5085 Lisp_Object font_object;
5087 if (! FONTP (name))
5088 CHECK_STRING (name);
5089 f = decode_window_system_frame (frame);
5091 if (STRINGP (name))
5093 int fontset = fs_query_fontset (name, 0);
5095 if (fontset >= 0)
5096 name = fontset_ascii (fontset);
5097 font_object = font_open_by_name (f, name);
5099 else if (FONT_OBJECT_P (name))
5100 font_object = name;
5101 else if (FONT_ENTITY_P (name))
5102 font_object = font_open_entity (f, name, 0);
5103 else
5105 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
5106 Lisp_Object entity = font_matching_entity (f, face->lface, name);
5108 font_object = ! NILP (entity) ? font_open_entity (f, entity, 0) : Qnil;
5110 if (NILP (font_object))
5111 return Qnil;
5112 font = XFONT_OBJECT (font_object);
5114 info = make_uninit_vector (14);
5115 ASET (info, 0, AREF (font_object, FONT_NAME_INDEX));
5116 ASET (info, 1, AREF (font_object, FONT_FULLNAME_INDEX));
5117 ASET (info, 2, make_number (font->pixel_size));
5118 ASET (info, 3, make_number (font->height));
5119 ASET (info, 4, make_number (font->baseline_offset));
5120 ASET (info, 5, make_number (font->relative_compose));
5121 ASET (info, 6, make_number (font->default_ascent));
5122 ASET (info, 7, make_number (font->max_width));
5123 ASET (info, 8, make_number (font->ascent));
5124 ASET (info, 9, make_number (font->descent));
5125 ASET (info, 10, make_number (font->space_width));
5126 ASET (info, 11, make_number (font->average_width));
5127 ASET (info, 12, AREF (font_object, FONT_FILE_INDEX));
5128 if (font->driver->otf_capability)
5129 ASET (info, 13, Fcons (Qopentype, font->driver->otf_capability (font)));
5130 else
5131 ASET (info, 13, Qnil);
5133 #if 0
5134 /* As font_object is still in FONT_OBJLIST of the entity, we can't
5135 close it now. Perhaps, we should manage font-objects
5136 by `reference-count'. */
5137 font_close_object (f, font_object);
5138 #endif
5139 return info;
5141 #endif
5144 #define BUILD_STYLE_TABLE(TBL) build_style_table (TBL, ARRAYELTS (TBL))
5146 static Lisp_Object
5147 build_style_table (const struct table_entry *entry, int nelement)
5149 int i, j;
5150 Lisp_Object table, elt;
5152 table = make_uninit_vector (nelement);
5153 for (i = 0; i < nelement; i++)
5155 for (j = 0; entry[i].names[j]; j++);
5156 elt = Fmake_vector (make_number (j + 1), Qnil);
5157 ASET (elt, 0, make_number (entry[i].numeric));
5158 for (j = 0; entry[i].names[j]; j++)
5159 ASET (elt, j + 1, intern_c_string (entry[i].names[j]));
5160 ASET (table, i, elt);
5162 return table;
5165 /* The deferred font-log data of the form [ACTION ARG RESULT].
5166 If ACTION is not nil, that is added to the log when font_add_log is
5167 called next time. At that time, ACTION is set back to nil. */
5168 static Lisp_Object Vfont_log_deferred;
5170 /* Prepend the font-related logging data in Vfont_log if it is not
5171 t. ACTION describes a kind of font-related action (e.g. listing,
5172 opening), ARG is the argument for the action, and RESULT is the
5173 result of the action. */
5174 void
5175 font_add_log (const char *action, Lisp_Object arg, Lisp_Object result)
5177 Lisp_Object val;
5178 int i;
5180 if (EQ (Vfont_log, Qt))
5181 return;
5182 if (STRINGP (AREF (Vfont_log_deferred, 0)))
5184 char *str = SSDATA (AREF (Vfont_log_deferred, 0));
5186 ASET (Vfont_log_deferred, 0, Qnil);
5187 font_add_log (str, AREF (Vfont_log_deferred, 1),
5188 AREF (Vfont_log_deferred, 2));
5191 if (FONTP (arg))
5193 Lisp_Object tail, elt;
5194 AUTO_STRING (equal, "=");
5196 val = Ffont_xlfd_name (arg, Qt);
5197 for (tail = AREF (arg, FONT_EXTRA_INDEX); CONSP (tail);
5198 tail = XCDR (tail))
5200 elt = XCAR (tail);
5201 if (EQ (XCAR (elt), QCscript)
5202 && SYMBOLP (XCDR (elt)))
5203 val = concat3 (val, SYMBOL_NAME (QCscript),
5204 concat2 (equal, SYMBOL_NAME (XCDR (elt))));
5205 else if (EQ (XCAR (elt), QClang)
5206 && SYMBOLP (XCDR (elt)))
5207 val = concat3 (val, SYMBOL_NAME (QClang),
5208 concat2 (equal, SYMBOL_NAME (XCDR (elt))));
5209 else if (EQ (XCAR (elt), QCotf)
5210 && CONSP (XCDR (elt)) && SYMBOLP (XCAR (XCDR (elt))))
5211 val = concat3 (val, SYMBOL_NAME (QCotf),
5212 concat2 (equal, SYMBOL_NAME (XCAR (XCDR (elt)))));
5214 arg = val;
5217 if (CONSP (result)
5218 && VECTORP (XCAR (result))
5219 && ASIZE (XCAR (result)) > 0
5220 && FONTP (AREF (XCAR (result), 0)))
5221 result = font_vconcat_entity_vectors (result);
5222 if (FONTP (result))
5224 val = Ffont_xlfd_name (result, Qt);
5225 if (! FONT_SPEC_P (result))
5227 AUTO_STRING (colon, ":");
5228 val = concat3 (SYMBOL_NAME (AREF (result, FONT_TYPE_INDEX)),
5229 colon, val);
5231 result = val;
5233 else if (CONSP (result))
5235 Lisp_Object tail;
5236 result = Fcopy_sequence (result);
5237 for (tail = result; CONSP (tail); tail = XCDR (tail))
5239 val = XCAR (tail);
5240 if (FONTP (val))
5241 val = Ffont_xlfd_name (val, Qt);
5242 XSETCAR (tail, val);
5245 else if (VECTORP (result))
5247 result = Fcopy_sequence (result);
5248 for (i = 0; i < ASIZE (result); i++)
5250 val = AREF (result, i);
5251 if (FONTP (val))
5252 val = Ffont_xlfd_name (val, Qt);
5253 ASET (result, i, val);
5256 Vfont_log = Fcons (list3 (intern (action), arg, result), Vfont_log);
5259 /* Record a font-related logging data to be added to Vfont_log when
5260 font_add_log is called next time. ACTION, ARG, RESULT are the same
5261 as font_add_log. */
5263 void
5264 font_deferred_log (const char *action, Lisp_Object arg, Lisp_Object result)
5266 if (EQ (Vfont_log, Qt))
5267 return;
5268 ASET (Vfont_log_deferred, 0, build_string (action));
5269 ASET (Vfont_log_deferred, 1, arg);
5270 ASET (Vfont_log_deferred, 2, result);
5273 void
5274 syms_of_font (void)
5276 sort_shift_bits[FONT_TYPE_INDEX] = 0;
5277 sort_shift_bits[FONT_SLANT_INDEX] = 2;
5278 sort_shift_bits[FONT_WEIGHT_INDEX] = 9;
5279 sort_shift_bits[FONT_SIZE_INDEX] = 16;
5280 sort_shift_bits[FONT_WIDTH_INDEX] = 23;
5281 /* Note that the other elements in sort_shift_bits are not used. */
5283 staticpro (&font_charset_alist);
5284 font_charset_alist = Qnil;
5286 DEFSYM (Qopentype, "opentype");
5288 /* Important character set symbols. */
5289 DEFSYM (Qascii_0, "ascii-0");
5290 DEFSYM (Qiso8859_1, "iso8859-1");
5291 DEFSYM (Qiso10646_1, "iso10646-1");
5292 DEFSYM (Qunicode_bmp, "unicode-bmp");
5294 /* Symbols representing keys of font extra info. */
5295 DEFSYM (QCotf, ":otf");
5296 DEFSYM (QClang, ":lang");
5297 DEFSYM (QCscript, ":script");
5298 DEFSYM (QCantialias, ":antialias");
5299 DEFSYM (QCfoundry, ":foundry");
5300 DEFSYM (QCadstyle, ":adstyle");
5301 DEFSYM (QCregistry, ":registry");
5302 DEFSYM (QCspacing, ":spacing");
5303 DEFSYM (QCdpi, ":dpi");
5304 DEFSYM (QCscalable, ":scalable");
5305 DEFSYM (QCavgwidth, ":avgwidth");
5306 DEFSYM (QCfont_entity, ":font-entity");
5307 DEFSYM (QCcombining_capability, ":combining-capability");
5309 /* Symbols representing values of font spacing property. */
5310 DEFSYM (Qc, "c");
5311 DEFSYM (Qm, "m");
5312 DEFSYM (Qp, "p");
5313 DEFSYM (Qd, "d");
5315 /* Special ADSTYLE properties to avoid fonts used for Latin
5316 characters; used in xfont.c and ftfont.c. */
5317 DEFSYM (Qja, "ja");
5318 DEFSYM (Qko, "ko");
5320 DEFSYM (QCuser_spec, "user-spec");
5322 staticpro (&scratch_font_spec);
5323 scratch_font_spec = Ffont_spec (0, NULL);
5324 staticpro (&scratch_font_prefer);
5325 scratch_font_prefer = Ffont_spec (0, NULL);
5327 staticpro (&Vfont_log_deferred);
5328 Vfont_log_deferred = Fmake_vector (make_number (3), Qnil);
5330 #if 0
5331 #ifdef HAVE_LIBOTF
5332 staticpro (&otf_list);
5333 otf_list = Qnil;
5334 #endif /* HAVE_LIBOTF */
5335 #endif /* 0 */
5337 defsubr (&Sfontp);
5338 defsubr (&Sfont_spec);
5339 defsubr (&Sfont_get);
5340 #ifdef HAVE_WINDOW_SYSTEM
5341 defsubr (&Sfont_face_attributes);
5342 #endif
5343 defsubr (&Sfont_put);
5344 defsubr (&Slist_fonts);
5345 defsubr (&Sfont_family_list);
5346 defsubr (&Sfind_font);
5347 defsubr (&Sfont_xlfd_name);
5348 defsubr (&Sclear_font_cache);
5349 defsubr (&Sfont_shape_gstring);
5350 defsubr (&Sfont_variation_glyphs);
5351 defsubr (&Sinternal_char_font);
5352 #if 0
5353 defsubr (&Sfont_drive_otf);
5354 defsubr (&Sfont_otf_alternates);
5355 #endif /* 0 */
5357 #ifdef FONT_DEBUG
5358 defsubr (&Sopen_font);
5359 defsubr (&Sclose_font);
5360 defsubr (&Squery_font);
5361 defsubr (&Sfont_get_glyphs);
5362 defsubr (&Sfont_match_p);
5363 defsubr (&Sfont_at);
5364 #if 0
5365 defsubr (&Sdraw_string);
5366 #endif
5367 defsubr (&Sframe_font_cache);
5368 #endif /* FONT_DEBUG */
5369 #ifdef HAVE_WINDOW_SYSTEM
5370 defsubr (&Sfont_info);
5371 #endif
5373 DEFVAR_LISP ("font-encoding-alist", Vfont_encoding_alist,
5374 doc: /*
5375 Alist of fontname patterns vs the corresponding encoding and repertory info.
5376 Each element looks like (REGEXP . (ENCODING . REPERTORY)),
5377 where ENCODING is a charset or a char-table,
5378 and REPERTORY is a charset, a char-table, or nil.
5380 If ENCODING and REPERTORY are the same, the element can have the form
5381 (REGEXP . ENCODING).
5383 ENCODING is for converting a character to a glyph code of the font.
5384 If ENCODING is a charset, encoding a character by the charset gives
5385 the corresponding glyph code. If ENCODING is a char-table, looking up
5386 the table by a character gives the corresponding glyph code.
5388 REPERTORY specifies a repertory of characters supported by the font.
5389 If REPERTORY is a charset, all characters belonging to the charset are
5390 supported. If REPERTORY is a char-table, all characters who have a
5391 non-nil value in the table are supported. If REPERTORY is nil, Emacs
5392 gets the repertory information by an opened font and ENCODING. */);
5393 Vfont_encoding_alist = Qnil;
5395 /* FIXME: These 3 vars are not quite what they appear: setq on them
5396 won't have any effect other than disconnect them from the style
5397 table used by the font display code. So we make them read-only,
5398 to avoid this confusing situation. */
5400 DEFVAR_LISP_NOPRO ("font-weight-table", Vfont_weight_table,
5401 doc: /* Vector of valid font weight values.
5402 Each element has the form:
5403 [NUMERIC-VALUE SYMBOLIC-NAME ALIAS-NAME ...]
5404 NUMERIC-VALUE is an integer, and SYMBOLIC-NAME and ALIAS-NAME are symbols. */);
5405 Vfont_weight_table = BUILD_STYLE_TABLE (weight_table);
5406 XSYMBOL (intern_c_string ("font-weight-table"))->constant = 1;
5408 DEFVAR_LISP_NOPRO ("font-slant-table", Vfont_slant_table,
5409 doc: /* Vector of font slant symbols vs the corresponding numeric values.
5410 See `font-weight-table' for the format of the vector. */);
5411 Vfont_slant_table = BUILD_STYLE_TABLE (slant_table);
5412 XSYMBOL (intern_c_string ("font-slant-table"))->constant = 1;
5414 DEFVAR_LISP_NOPRO ("font-width-table", Vfont_width_table,
5415 doc: /* Alist of font width symbols vs the corresponding numeric values.
5416 See `font-weight-table' for the format of the vector. */);
5417 Vfont_width_table = BUILD_STYLE_TABLE (width_table);
5418 XSYMBOL (intern_c_string ("font-width-table"))->constant = 1;
5420 staticpro (&font_style_table);
5421 font_style_table = make_uninit_vector (3);
5422 ASET (font_style_table, 0, Vfont_weight_table);
5423 ASET (font_style_table, 1, Vfont_slant_table);
5424 ASET (font_style_table, 2, Vfont_width_table);
5426 DEFVAR_LISP ("font-log", Vfont_log, doc: /*
5427 A list that logs font-related actions and results, for debugging.
5428 The default value is t, which means to suppress logging.
5429 Set it to nil to enable logging. If the environment variable
5430 EMACS_FONT_LOG is set at startup, it defaults to nil. */);
5431 Vfont_log = Qnil;
5433 #ifdef HAVE_WINDOW_SYSTEM
5434 #ifdef HAVE_FREETYPE
5435 syms_of_ftfont ();
5436 #ifdef HAVE_X_WINDOWS
5437 #ifdef USE_CAIRO
5438 syms_of_ftcrfont ();
5439 #else
5440 syms_of_xfont ();
5441 syms_of_ftxfont ();
5442 #ifdef HAVE_XFT
5443 syms_of_xftfont ();
5444 #endif /* HAVE_XFT */
5445 #endif /* not USE_CAIRO */
5446 #endif /* HAVE_X_WINDOWS */
5447 #else /* not HAVE_FREETYPE */
5448 #ifdef HAVE_X_WINDOWS
5449 syms_of_xfont ();
5450 #endif /* HAVE_X_WINDOWS */
5451 #endif /* not HAVE_FREETYPE */
5452 #ifdef HAVE_BDFFONT
5453 syms_of_bdffont ();
5454 #endif /* HAVE_BDFFONT */
5455 #ifdef HAVE_NTGUI
5456 syms_of_w32font ();
5457 #endif /* HAVE_NTGUI */
5458 #endif /* HAVE_WINDOW_SYSTEM */
5461 void
5462 init_font (void)
5464 Vfont_log = egetenv ("EMACS_FONT_LOG") ? Qnil : Qt;