Merge from emacs-24; up to 2013-01-03T02:37:57Z!rgm@gnu.org
[emacs.git] / src / font.c
blob1e1670b21afb5f72e82803f5bd58871167cdebd8
1 /* font.c -- "Font" primitives.
3 Copyright (C) 2006-2013 Free Software Foundation, Inc.
4 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H13PRO009
8 This file is part of GNU Emacs.
10 GNU Emacs is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 GNU Emacs is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
23 #include <config.h>
24 #include <float.h>
25 #include <stdio.h>
27 #include <c-ctype.h>
29 #include "lisp.h"
30 #include "character.h"
31 #include "buffer.h"
32 #include "frame.h"
33 #include "window.h"
34 #include "dispextern.h"
35 #include "charset.h"
36 #include "composite.h"
37 #include "fontset.h"
38 #include "font.h"
40 #ifdef HAVE_WINDOW_SYSTEM
41 #include TERM_HEADER
42 #endif /* HAVE_WINDOW_SYSTEM */
44 Lisp_Object Qopentype;
46 /* Important character set strings. */
47 Lisp_Object Qascii_0, Qiso8859_1, Qiso10646_1, Qunicode_bmp, Qunicode_sip;
49 #define DEFAULT_ENCODING Qiso8859_1
51 /* Unicode category `Cf'. */
52 static Lisp_Object QCf;
54 /* Vector of Vfont_weight_table, Vfont_slant_table, and Vfont_width_table. */
55 static Lisp_Object font_style_table;
57 /* Structure used for tables mapping weight, slant, and width numeric
58 values and their names. */
60 struct table_entry
62 int numeric;
63 /* The first one is a valid name as a face attribute.
64 The second one (if any) is a typical name in XLFD field. */
65 const char *names[5];
68 /* Table of weight numeric values and their names. This table must be
69 sorted by numeric values in ascending order. */
71 static const struct table_entry weight_table[] =
73 { 0, { "thin" }},
74 { 20, { "ultra-light", "ultralight" }},
75 { 40, { "extra-light", "extralight" }},
76 { 50, { "light" }},
77 { 75, { "semi-light", "semilight", "demilight", "book" }},
78 { 100, { "normal", "medium", "regular", "unspecified" }},
79 { 180, { "semi-bold", "semibold", "demibold", "demi" }},
80 { 200, { "bold" }},
81 { 205, { "extra-bold", "extrabold" }},
82 { 210, { "ultra-bold", "ultrabold", "black" }}
85 /* Table of slant numeric values and their names. This table must be
86 sorted by numeric values in ascending order. */
88 static const struct table_entry slant_table[] =
90 { 0, { "reverse-oblique", "ro" }},
91 { 10, { "reverse-italic", "ri" }},
92 { 100, { "normal", "r", "unspecified" }},
93 { 200, { "italic" ,"i", "ot" }},
94 { 210, { "oblique", "o" }}
97 /* Table of width numeric values and their names. This table must be
98 sorted by numeric values in ascending order. */
100 static const struct table_entry width_table[] =
102 { 50, { "ultra-condensed", "ultracondensed" }},
103 { 63, { "extra-condensed", "extracondensed" }},
104 { 75, { "condensed", "compressed", "narrow" }},
105 { 87, { "semi-condensed", "semicondensed", "demicondensed" }},
106 { 100, { "normal", "medium", "regular", "unspecified" }},
107 { 113, { "semi-expanded", "semiexpanded", "demiexpanded" }},
108 { 125, { "expanded" }},
109 { 150, { "extra-expanded", "extraexpanded" }},
110 { 200, { "ultra-expanded", "ultraexpanded", "wide" }}
113 Lisp_Object QCfoundry;
114 static Lisp_Object QCadstyle, QCregistry;
115 /* Symbols representing keys of font extra info. */
116 Lisp_Object QCspacing, QCdpi, QCscalable, QCotf, QClang, QCscript, QCavgwidth;
117 Lisp_Object QCantialias, QCfont_entity;
118 static Lisp_Object QCfc_unknown_spec;
119 /* Symbols representing values of font spacing property. */
120 static Lisp_Object Qc, Qm, Qd;
121 Lisp_Object Qp;
122 /* Special ADSTYLE properties to avoid fonts used for Latin
123 characters; used in xfont.c and ftfont.c. */
124 Lisp_Object Qja, Qko;
126 static Lisp_Object QCuser_spec;
128 /* Alist of font registry symbols and the corresponding charset
129 information. The information is retrieved from
130 Vfont_encoding_alist on demand.
132 Eash element has the form:
133 (REGISTRY . (ENCODING-CHARSET-ID . REPERTORY-CHARSET-ID))
135 (REGISTRY . nil)
137 In the former form, ENCODING-CHARSET-ID is an ID of a charset that
138 encodes a character code to a glyph code of a font, and
139 REPERTORY-CHARSET-ID is an ID of a charset that tells if a
140 character is supported by a font.
142 The latter form means that the information for REGISTRY couldn't be
143 retrieved. */
144 static Lisp_Object font_charset_alist;
146 /* List of all font drivers. Each font-backend (XXXfont.c) calls
147 register_font_driver in syms_of_XXXfont to register its font-driver
148 here. */
149 static struct font_driver_list *font_driver_list;
153 /* Creators of font-related Lisp object. */
155 static Lisp_Object
156 font_make_spec (void)
158 Lisp_Object font_spec;
159 struct font_spec *spec
160 = ((struct font_spec *)
161 allocate_pseudovector (VECSIZE (struct font_spec),
162 FONT_SPEC_MAX, PVEC_FONT));
163 XSETFONT (font_spec, spec);
164 return font_spec;
167 Lisp_Object
168 font_make_entity (void)
170 Lisp_Object font_entity;
171 struct font_entity *entity
172 = ((struct font_entity *)
173 allocate_pseudovector (VECSIZE (struct font_entity),
174 FONT_ENTITY_MAX, PVEC_FONT));
175 XSETFONT (font_entity, entity);
176 return font_entity;
179 /* Create a font-object whose structure size is SIZE. If ENTITY is
180 not nil, copy properties from ENTITY to the font-object. If
181 PIXELSIZE is positive, set the `size' property to PIXELSIZE. */
182 Lisp_Object
183 font_make_object (int size, Lisp_Object entity, int pixelsize)
185 Lisp_Object font_object;
186 struct font *font
187 = (struct font *) allocate_pseudovector (size, FONT_OBJECT_MAX, PVEC_FONT);
188 int i;
190 XSETFONT (font_object, font);
192 if (! NILP (entity))
194 for (i = 1; i < FONT_SPEC_MAX; i++)
195 font->props[i] = AREF (entity, i);
196 if (! NILP (AREF (entity, FONT_EXTRA_INDEX)))
197 font->props[FONT_EXTRA_INDEX]
198 = Fcopy_alist (AREF (entity, FONT_EXTRA_INDEX));
200 if (size > 0)
201 font->props[FONT_SIZE_INDEX] = make_number (pixelsize);
202 return font_object;
207 static int font_pixel_size (struct frame *f, Lisp_Object);
208 static Lisp_Object font_open_entity (struct frame *, Lisp_Object, int);
209 static Lisp_Object font_matching_entity (struct frame *, Lisp_Object *,
210 Lisp_Object);
211 static unsigned font_encode_char (Lisp_Object, int);
213 /* Number of registered font drivers. */
214 static int num_font_drivers;
217 /* Return a Lispy value of a font property value at STR and LEN bytes.
218 If STR is "*", return nil. If FORCE_SYMBOL, or if STR does not
219 consist entirely of one or more digits, return a symbol interned
220 from STR. Otherwise, return an integer. */
222 Lisp_Object
223 font_intern_prop (const char *str, ptrdiff_t len, bool force_symbol)
225 ptrdiff_t i;
226 Lisp_Object tem;
227 Lisp_Object obarray;
228 ptrdiff_t nbytes, nchars;
230 if (len == 1 && *str == '*')
231 return Qnil;
232 if (!force_symbol && 0 < len && '0' <= *str && *str <= '9')
234 for (i = 1; i < len; i++)
235 if (! ('0' <= str[i] && str[i] <= '9'))
236 break;
237 if (i == len)
239 EMACS_INT n;
241 i = 0;
242 for (n = 0; (n += str[i++] - '0') <= MOST_POSITIVE_FIXNUM; n *= 10)
244 if (i == len)
245 return make_number (n);
246 if (MOST_POSITIVE_FIXNUM / 10 < n)
247 break;
250 xsignal1 (Qoverflow_error, make_string (str, len));
254 /* This code is similar to intern function from lread.c. */
255 obarray = check_obarray (Vobarray);
256 parse_str_as_multibyte ((unsigned char *) str, len, &nchars, &nbytes);
257 tem = oblookup (obarray, str,
258 (len == nchars || len != nbytes) ? len : nchars, len);
260 if (SYMBOLP (tem))
261 return tem;
262 if (len == nchars || len != nbytes)
263 tem = make_unibyte_string (str, len);
264 else
265 tem = make_multibyte_string (str, nchars, len);
266 return Fintern (tem, obarray);
269 /* Return a pixel size of font-spec SPEC on frame F. */
271 static int
272 font_pixel_size (struct frame *f, Lisp_Object spec)
274 #ifdef HAVE_WINDOW_SYSTEM
275 Lisp_Object size = AREF (spec, FONT_SIZE_INDEX);
276 double point_size;
277 int dpi, pixel_size;
278 Lisp_Object val;
280 if (INTEGERP (size))
281 return XINT (size);
282 if (NILP (size))
283 return 0;
284 eassert (FLOATP (size));
285 point_size = XFLOAT_DATA (size);
286 val = AREF (spec, FONT_DPI_INDEX);
287 if (INTEGERP (val))
288 dpi = XINT (val);
289 else
290 dpi = FRAME_RES_Y (f);
291 pixel_size = POINT_TO_PIXEL (point_size, dpi);
292 return pixel_size;
293 #else
294 return 1;
295 #endif
299 /* Return a value of PROP's VAL (symbol or integer) to be stored in a
300 font vector. If VAL is not valid (i.e. not registered in
301 font_style_table), return -1 if NOERROR is zero, and return a
302 proper index if NOERROR is nonzero. In that case, register VAL in
303 font_style_table if VAL is a symbol, and return the closest index if
304 VAL is an integer. */
307 font_style_to_value (enum font_property_index prop, Lisp_Object val,
308 bool noerror)
310 Lisp_Object table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
311 int len;
313 CHECK_VECTOR (table);
314 len = ASIZE (table);
316 if (SYMBOLP (val))
318 int i, j;
319 char *s;
320 Lisp_Object args[2], elt;
322 /* At first try exact match. */
323 for (i = 0; i < len; i++)
325 CHECK_VECTOR (AREF (table, i));
326 for (j = 1; j < ASIZE (AREF (table, i)); j++)
327 if (EQ (val, AREF (AREF (table, i), j)))
329 CHECK_NUMBER (AREF (AREF (table, i), 0));
330 return ((XINT (AREF (AREF (table, i), 0)) << 8)
331 | (i << 4) | (j - 1));
334 /* Try also with case-folding match. */
335 s = SSDATA (SYMBOL_NAME (val));
336 for (i = 0; i < len; i++)
337 for (j = 1; j < ASIZE (AREF (table, i)); j++)
339 elt = AREF (AREF (table, i), j);
340 if (xstrcasecmp (s, SSDATA (SYMBOL_NAME (elt))) == 0)
342 CHECK_NUMBER (AREF (AREF (table, i), 0));
343 return ((XINT (AREF (AREF (table, i), 0)) << 8)
344 | (i << 4) | (j - 1));
347 if (! noerror)
348 return -1;
349 eassert (len < 255);
350 elt = Fmake_vector (make_number (2), make_number (100));
351 ASET (elt, 1, val);
352 args[0] = table;
353 args[1] = Fmake_vector (make_number (1), elt);
354 ASET (font_style_table, prop - FONT_WEIGHT_INDEX, Fvconcat (2, args));
355 return (100 << 8) | (i << 4);
357 else
359 int i, last_n;
360 EMACS_INT numeric = XINT (val);
362 for (i = 0, last_n = -1; i < len; i++)
364 int n;
366 CHECK_VECTOR (AREF (table, i));
367 CHECK_NUMBER (AREF (AREF (table, i), 0));
368 n = XINT (AREF (AREF (table, i), 0));
369 if (numeric == n)
370 return (n << 8) | (i << 4);
371 if (numeric < n)
373 if (! noerror)
374 return -1;
375 return ((i == 0 || n - numeric < numeric - last_n)
376 ? (n << 8) | (i << 4): (last_n << 8 | ((i - 1) << 4)));
378 last_n = n;
380 if (! noerror)
381 return -1;
382 return ((last_n << 8) | ((i - 1) << 4));
386 Lisp_Object
387 font_style_symbolic (Lisp_Object font, enum font_property_index prop,
388 bool for_face)
390 Lisp_Object val = AREF (font, prop);
391 Lisp_Object table, elt;
392 int i;
394 if (NILP (val))
395 return Qnil;
396 table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
397 CHECK_VECTOR (table);
398 i = XINT (val) & 0xFF;
399 eassert (((i >> 4) & 0xF) < ASIZE (table));
400 elt = AREF (table, ((i >> 4) & 0xF));
401 CHECK_VECTOR (elt);
402 eassert ((i & 0xF) + 1 < ASIZE (elt));
403 elt = (for_face ? AREF (elt, 1) : AREF (elt, (i & 0xF) + 1));
404 CHECK_SYMBOL (elt);
405 return elt;
408 /* Return ENCODING or a cons of ENCODING and REPERTORY of the font
409 FONTNAME. ENCODING is a charset symbol that specifies the encoding
410 of the font. REPERTORY is a charset symbol or nil. */
412 Lisp_Object
413 find_font_encoding (Lisp_Object fontname)
415 Lisp_Object tail, elt;
417 for (tail = Vfont_encoding_alist; CONSP (tail); tail = XCDR (tail))
419 elt = XCAR (tail);
420 if (CONSP (elt)
421 && STRINGP (XCAR (elt))
422 && fast_string_match_ignore_case (XCAR (elt), fontname) >= 0
423 && (SYMBOLP (XCDR (elt))
424 ? CHARSETP (XCDR (elt))
425 : CONSP (XCDR (elt)) && CHARSETP (XCAR (XCDR (elt)))))
426 return (XCDR (elt));
428 return Qnil;
431 /* Return encoding charset and repertory charset for REGISTRY in
432 ENCODING and REPERTORY correspondingly. If correct information for
433 REGISTRY is available, return 0. Otherwise return -1. */
436 font_registry_charsets (Lisp_Object registry, struct charset **encoding, struct charset **repertory)
438 Lisp_Object val;
439 int encoding_id, repertory_id;
441 val = Fassoc_string (registry, font_charset_alist, Qt);
442 if (! NILP (val))
444 val = XCDR (val);
445 if (NILP (val))
446 return -1;
447 encoding_id = XINT (XCAR (val));
448 repertory_id = XINT (XCDR (val));
450 else
452 val = find_font_encoding (SYMBOL_NAME (registry));
453 if (SYMBOLP (val) && CHARSETP (val))
455 encoding_id = repertory_id = XINT (CHARSET_SYMBOL_ID (val));
457 else if (CONSP (val))
459 if (! CHARSETP (XCAR (val)))
460 goto invalid_entry;
461 encoding_id = XINT (CHARSET_SYMBOL_ID (XCAR (val)));
462 if (NILP (XCDR (val)))
463 repertory_id = -1;
464 else
466 if (! CHARSETP (XCDR (val)))
467 goto invalid_entry;
468 repertory_id = XINT (CHARSET_SYMBOL_ID (XCDR (val)));
471 else
472 goto invalid_entry;
473 val = Fcons (make_number (encoding_id), make_number (repertory_id));
474 font_charset_alist
475 = nconc2 (font_charset_alist, list1 (Fcons (registry, val)));
478 if (encoding)
479 *encoding = CHARSET_FROM_ID (encoding_id);
480 if (repertory)
481 *repertory = repertory_id >= 0 ? CHARSET_FROM_ID (repertory_id) : NULL;
482 return 0;
484 invalid_entry:
485 font_charset_alist
486 = nconc2 (font_charset_alist, list1 (Fcons (registry, Qnil)));
487 return -1;
491 /* Font property value validators. See the comment of
492 font_property_table for the meaning of the arguments. */
494 static Lisp_Object font_prop_validate (int, Lisp_Object, Lisp_Object);
495 static Lisp_Object font_prop_validate_symbol (Lisp_Object, Lisp_Object);
496 static Lisp_Object font_prop_validate_style (Lisp_Object, Lisp_Object);
497 static Lisp_Object font_prop_validate_non_neg (Lisp_Object, Lisp_Object);
498 static Lisp_Object font_prop_validate_spacing (Lisp_Object, Lisp_Object);
499 static int get_font_prop_index (Lisp_Object);
501 static Lisp_Object
502 font_prop_validate_symbol (Lisp_Object prop, Lisp_Object val)
504 if (STRINGP (val))
505 val = Fintern (val, Qnil);
506 if (! SYMBOLP (val))
507 val = Qerror;
508 else if (EQ (prop, QCregistry))
509 val = Fintern (Fdowncase (SYMBOL_NAME (val)), Qnil);
510 return val;
514 static Lisp_Object
515 font_prop_validate_style (Lisp_Object style, Lisp_Object val)
517 enum font_property_index prop = (EQ (style, QCweight) ? FONT_WEIGHT_INDEX
518 : EQ (style, QCslant) ? FONT_SLANT_INDEX
519 : FONT_WIDTH_INDEX);
520 if (INTEGERP (val))
522 EMACS_INT n = XINT (val);
523 CHECK_VECTOR (AREF (font_style_table, prop - FONT_WEIGHT_INDEX));
524 if (((n >> 4) & 0xF)
525 >= ASIZE (AREF (font_style_table, prop - FONT_WEIGHT_INDEX)))
526 val = Qerror;
527 else
529 Lisp_Object elt = AREF (AREF (font_style_table, prop - FONT_WEIGHT_INDEX), (n >> 4) & 0xF);
531 CHECK_VECTOR (elt);
532 if ((n & 0xF) + 1 >= ASIZE (elt))
533 val = Qerror;
534 else
536 CHECK_NUMBER (AREF (elt, 0));
537 if (XINT (AREF (elt, 0)) != (n >> 8))
538 val = Qerror;
542 else if (SYMBOLP (val))
544 int n = font_style_to_value (prop, val, 0);
546 val = n >= 0 ? make_number (n) : Qerror;
548 else
549 val = Qerror;
550 return val;
553 static Lisp_Object
554 font_prop_validate_non_neg (Lisp_Object prop, Lisp_Object val)
556 return (NATNUMP (val) || (FLOATP (val) && XFLOAT_DATA (val) >= 0)
557 ? val : Qerror);
560 static Lisp_Object
561 font_prop_validate_spacing (Lisp_Object prop, Lisp_Object val)
563 if (NILP (val) || (NATNUMP (val) && XINT (val) <= FONT_SPACING_CHARCELL))
564 return val;
565 if (SYMBOLP (val) && SBYTES (SYMBOL_NAME (val)) == 1)
567 char spacing = SDATA (SYMBOL_NAME (val))[0];
569 if (spacing == 'c' || spacing == 'C')
570 return make_number (FONT_SPACING_CHARCELL);
571 if (spacing == 'm' || spacing == 'M')
572 return make_number (FONT_SPACING_MONO);
573 if (spacing == 'p' || spacing == 'P')
574 return make_number (FONT_SPACING_PROPORTIONAL);
575 if (spacing == 'd' || spacing == 'D')
576 return make_number (FONT_SPACING_DUAL);
578 return Qerror;
581 static Lisp_Object
582 font_prop_validate_otf (Lisp_Object prop, Lisp_Object val)
584 Lisp_Object tail, tmp;
585 int i;
587 /* VAL = (SCRIPT [ LANGSYS [ GSUB-FEATURES [ GPOS-FEATURES ]]])
588 GSUB-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil
589 GPOS-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil */
590 if (! CONSP (val))
591 return Qerror;
592 if (! SYMBOLP (XCAR (val)))
593 return Qerror;
594 tail = XCDR (val);
595 if (NILP (tail))
596 return val;
597 if (! CONSP (tail) || ! SYMBOLP (XCAR (val)))
598 return Qerror;
599 for (i = 0; i < 2; i++)
601 tail = XCDR (tail);
602 if (NILP (tail))
603 return val;
604 if (! CONSP (tail))
605 return Qerror;
606 for (tmp = XCAR (tail); CONSP (tmp); tmp = XCDR (tmp))
607 if (! SYMBOLP (XCAR (tmp)))
608 return Qerror;
609 if (! NILP (tmp))
610 return Qerror;
612 return val;
615 /* Structure of known font property keys and validator of the
616 values. */
617 static const struct
619 /* Pointer to the key symbol. */
620 Lisp_Object *key;
621 /* Function to validate PROP's value VAL, or NULL if any value is
622 ok. The value is VAL or its regularized value if VAL is valid,
623 and Qerror if not. */
624 Lisp_Object (*validator) (Lisp_Object prop, Lisp_Object val);
625 } font_property_table[] =
626 { { &QCtype, font_prop_validate_symbol },
627 { &QCfoundry, font_prop_validate_symbol },
628 { &QCfamily, font_prop_validate_symbol },
629 { &QCadstyle, font_prop_validate_symbol },
630 { &QCregistry, font_prop_validate_symbol },
631 { &QCweight, font_prop_validate_style },
632 { &QCslant, font_prop_validate_style },
633 { &QCwidth, font_prop_validate_style },
634 { &QCsize, font_prop_validate_non_neg },
635 { &QCdpi, font_prop_validate_non_neg },
636 { &QCspacing, font_prop_validate_spacing },
637 { &QCavgwidth, font_prop_validate_non_neg },
638 /* The order of the above entries must match with enum
639 font_property_index. */
640 { &QClang, font_prop_validate_symbol },
641 { &QCscript, font_prop_validate_symbol },
642 { &QCotf, font_prop_validate_otf }
645 /* Size (number of elements) of the above table. */
646 #define FONT_PROPERTY_TABLE_SIZE \
647 ((sizeof font_property_table) / (sizeof *font_property_table))
649 /* Return an index number of font property KEY or -1 if KEY is not an
650 already known property. */
652 static int
653 get_font_prop_index (Lisp_Object key)
655 int i;
657 for (i = 0; i < FONT_PROPERTY_TABLE_SIZE; i++)
658 if (EQ (key, *font_property_table[i].key))
659 return i;
660 return -1;
663 /* Validate the font property. The property key is specified by the
664 symbol PROP, or the index IDX (if PROP is nil). If VAL is invalid,
665 signal an error. The value is VAL or the regularized one. */
667 static Lisp_Object
668 font_prop_validate (int idx, Lisp_Object prop, Lisp_Object val)
670 Lisp_Object validated;
672 if (NILP (val))
673 return val;
674 if (NILP (prop))
675 prop = *font_property_table[idx].key;
676 else
678 idx = get_font_prop_index (prop);
679 if (idx < 0)
680 return val;
682 validated = (font_property_table[idx].validator) (prop, val);
683 if (EQ (validated, Qerror))
684 signal_error ("invalid font property", Fcons (prop, val));
685 return validated;
689 /* Store VAL as a value of extra font property PROP in FONT while
690 keeping the sorting order. Don't check the validity of VAL. */
692 Lisp_Object
693 font_put_extra (Lisp_Object font, Lisp_Object prop, Lisp_Object val)
695 Lisp_Object extra = AREF (font, FONT_EXTRA_INDEX);
696 Lisp_Object slot = (NILP (extra) ? Qnil : assq_no_quit (prop, extra));
698 if (NILP (slot))
700 Lisp_Object prev = Qnil;
702 while (CONSP (extra)
703 && NILP (Fstring_lessp (prop, XCAR (XCAR (extra)))))
704 prev = extra, extra = XCDR (extra);
706 if (NILP (prev))
707 ASET (font, FONT_EXTRA_INDEX, Fcons (Fcons (prop, val), extra));
708 else
709 XSETCDR (prev, Fcons (Fcons (prop, val), extra));
711 return val;
713 XSETCDR (slot, val);
714 if (NILP (val))
715 ASET (font, FONT_EXTRA_INDEX, Fdelq (slot, extra));
716 return val;
720 /* Font name parser and unparser. */
722 static int parse_matrix (const char *);
723 static int font_expand_wildcards (Lisp_Object *, int);
724 static int font_parse_name (char *, ptrdiff_t, Lisp_Object);
726 /* An enumerator for each field of an XLFD font name. */
727 enum xlfd_field_index
729 XLFD_FOUNDRY_INDEX,
730 XLFD_FAMILY_INDEX,
731 XLFD_WEIGHT_INDEX,
732 XLFD_SLANT_INDEX,
733 XLFD_SWIDTH_INDEX,
734 XLFD_ADSTYLE_INDEX,
735 XLFD_PIXEL_INDEX,
736 XLFD_POINT_INDEX,
737 XLFD_RESX_INDEX,
738 XLFD_RESY_INDEX,
739 XLFD_SPACING_INDEX,
740 XLFD_AVGWIDTH_INDEX,
741 XLFD_REGISTRY_INDEX,
742 XLFD_ENCODING_INDEX,
743 XLFD_LAST_INDEX
746 /* An enumerator for mask bit corresponding to each XLFD field. */
747 enum xlfd_field_mask
749 XLFD_FOUNDRY_MASK = 0x0001,
750 XLFD_FAMILY_MASK = 0x0002,
751 XLFD_WEIGHT_MASK = 0x0004,
752 XLFD_SLANT_MASK = 0x0008,
753 XLFD_SWIDTH_MASK = 0x0010,
754 XLFD_ADSTYLE_MASK = 0x0020,
755 XLFD_PIXEL_MASK = 0x0040,
756 XLFD_POINT_MASK = 0x0080,
757 XLFD_RESX_MASK = 0x0100,
758 XLFD_RESY_MASK = 0x0200,
759 XLFD_SPACING_MASK = 0x0400,
760 XLFD_AVGWIDTH_MASK = 0x0800,
761 XLFD_REGISTRY_MASK = 0x1000,
762 XLFD_ENCODING_MASK = 0x2000
766 /* Parse P pointing to the pixel/point size field of the form
767 `[A B C D]' which specifies a transformation matrix:
769 A B 0
770 C D 0
771 0 0 1
773 by which all glyphs of the font are transformed. The spec says
774 that scalar value N for the pixel/point size is equivalent to:
775 A = N * resx/resy, B = C = 0, D = N.
777 Return the scalar value N if the form is valid. Otherwise return
778 -1. */
780 static int
781 parse_matrix (const char *p)
783 double matrix[4];
784 char *end;
785 int i;
787 for (i = 0, p++; i < 4 && *p && *p != ']'; i++)
789 if (*p == '~')
790 matrix[i] = - strtod (p + 1, &end);
791 else
792 matrix[i] = strtod (p, &end);
793 p = end;
795 return (i == 4 ? (int) matrix[3] : -1);
798 /* Expand a wildcard field in FIELD (the first N fields are filled) to
799 multiple fields to fill in all 14 XLFD fields while restricting a
800 field position by its contents. */
802 static int
803 font_expand_wildcards (Lisp_Object *field, int n)
805 /* Copy of FIELD. */
806 Lisp_Object tmp[XLFD_LAST_INDEX];
807 /* Array of information about where this element can go. Nth
808 element is for Nth element of FIELD. */
809 struct {
810 /* Minimum possible field. */
811 int from;
812 /* Maximum possible field. */
813 int to;
814 /* Bit mask of possible field. Nth bit corresponds to Nth field. */
815 int mask;
816 } range[XLFD_LAST_INDEX];
817 int i, j;
818 int range_from, range_to;
819 unsigned range_mask;
821 #define XLFD_SYMBOL_MASK (XLFD_FOUNDRY_MASK | XLFD_FAMILY_MASK \
822 | XLFD_ADSTYLE_MASK | XLFD_REGISTRY_MASK)
823 #define XLFD_NULL_MASK (XLFD_FOUNDRY_MASK | XLFD_ADSTYLE_MASK)
824 #define XLFD_LARGENUM_MASK (XLFD_POINT_MASK | XLFD_RESX_MASK | XLFD_RESY_MASK \
825 | XLFD_AVGWIDTH_MASK)
826 #define XLFD_REGENC_MASK (XLFD_REGISTRY_MASK | XLFD_ENCODING_MASK)
828 /* Initialize RANGE_MASK for FIELD[0] which can be 0th to (14 - N)th
829 field. The value is shifted to left one bit by one in the
830 following loop. */
831 for (i = 0, range_mask = 0; i <= 14 - n; i++)
832 range_mask = (range_mask << 1) | 1;
834 /* The triplet RANGE_FROM, RANGE_TO, and RANGE_MASK is a
835 position-based restriction for FIELD[I]. */
836 for (i = 0, range_from = 0, range_to = 14 - n; i < n;
837 i++, range_from++, range_to++, range_mask <<= 1)
839 Lisp_Object val = field[i];
841 tmp[i] = val;
842 if (NILP (val))
844 /* Wildcard. */
845 range[i].from = range_from;
846 range[i].to = range_to;
847 range[i].mask = range_mask;
849 else
851 /* The triplet FROM, TO, and MASK is a value-based
852 restriction for FIELD[I]. */
853 int from, to;
854 unsigned mask;
856 if (INTEGERP (val))
858 EMACS_INT numeric = XINT (val);
860 if (i + 1 == n)
861 from = to = XLFD_ENCODING_INDEX,
862 mask = XLFD_ENCODING_MASK;
863 else if (numeric == 0)
864 from = XLFD_PIXEL_INDEX, to = XLFD_AVGWIDTH_INDEX,
865 mask = XLFD_PIXEL_MASK | XLFD_LARGENUM_MASK;
866 else if (numeric <= 48)
867 from = to = XLFD_PIXEL_INDEX,
868 mask = XLFD_PIXEL_MASK;
869 else
870 from = XLFD_POINT_INDEX, to = XLFD_AVGWIDTH_INDEX,
871 mask = XLFD_LARGENUM_MASK;
873 else if (SBYTES (SYMBOL_NAME (val)) == 0)
874 from = XLFD_FOUNDRY_INDEX, to = XLFD_ADSTYLE_INDEX,
875 mask = XLFD_NULL_MASK;
876 else if (i == 0)
877 from = to = XLFD_FOUNDRY_INDEX, mask = XLFD_FOUNDRY_MASK;
878 else if (i + 1 == n)
880 Lisp_Object name = SYMBOL_NAME (val);
882 if (SDATA (name)[SBYTES (name) - 1] == '*')
883 from = XLFD_REGISTRY_INDEX, to = XLFD_ENCODING_INDEX,
884 mask = XLFD_REGENC_MASK;
885 else
886 from = to = XLFD_ENCODING_INDEX,
887 mask = XLFD_ENCODING_MASK;
889 else if (range_from <= XLFD_WEIGHT_INDEX
890 && range_to >= XLFD_WEIGHT_INDEX
891 && FONT_WEIGHT_NAME_NUMERIC (val) >= 0)
892 from = to = XLFD_WEIGHT_INDEX, mask = XLFD_WEIGHT_MASK;
893 else if (range_from <= XLFD_SLANT_INDEX
894 && range_to >= XLFD_SLANT_INDEX
895 && FONT_SLANT_NAME_NUMERIC (val) >= 0)
896 from = to = XLFD_SLANT_INDEX, mask = XLFD_SLANT_MASK;
897 else if (range_from <= XLFD_SWIDTH_INDEX
898 && range_to >= XLFD_SWIDTH_INDEX
899 && FONT_WIDTH_NAME_NUMERIC (val) >= 0)
900 from = to = XLFD_SWIDTH_INDEX, mask = XLFD_SWIDTH_MASK;
901 else
903 if (EQ (val, Qc) || EQ (val, Qm) || EQ (val, Qp) || EQ (val, Qd))
904 from = to = XLFD_SPACING_INDEX, mask = XLFD_SPACING_MASK;
905 else
906 from = XLFD_FOUNDRY_INDEX, to = XLFD_ENCODING_INDEX,
907 mask = XLFD_SYMBOL_MASK;
910 /* Merge position-based and value-based restrictions. */
911 mask &= range_mask;
912 while (from < range_from)
913 mask &= ~(1 << from++);
914 while (from < 14 && ! (mask & (1 << from)))
915 from++;
916 while (to > range_to)
917 mask &= ~(1 << to--);
918 while (to >= 0 && ! (mask & (1 << to)))
919 to--;
920 if (from > to)
921 return -1;
922 range[i].from = from;
923 range[i].to = to;
924 range[i].mask = mask;
926 if (from > range_from || to < range_to)
928 /* The range is narrowed by value-based restrictions.
929 Reflect it to the other fields. */
931 /* Following fields should be after FROM. */
932 range_from = from;
933 /* Preceding fields should be before TO. */
934 for (j = i - 1, from--, to--; j >= 0; j--, from--, to--)
936 /* Check FROM for non-wildcard field. */
937 if (! NILP (tmp[j]) && range[j].from < from)
939 while (range[j].from < from)
940 range[j].mask &= ~(1 << range[j].from++);
941 while (from < 14 && ! (range[j].mask & (1 << from)))
942 from++;
943 range[j].from = from;
945 else
946 from = range[j].from;
947 if (range[j].to > to)
949 while (range[j].to > to)
950 range[j].mask &= ~(1 << range[j].to--);
951 while (to >= 0 && ! (range[j].mask & (1 << to)))
952 to--;
953 range[j].to = to;
955 else
956 to = range[j].to;
957 if (from > to)
958 return -1;
964 /* Decide all fields from restrictions in RANGE. */
965 for (i = j = 0; i < n ; i++)
967 if (j < range[i].from)
969 if (i == 0 || ! NILP (tmp[i - 1]))
970 /* None of TMP[X] corresponds to Jth field. */
971 return -1;
972 for (; j < range[i].from; j++)
973 field[j] = Qnil;
975 field[j++] = tmp[i];
977 if (! NILP (tmp[n - 1]) && j < XLFD_REGISTRY_INDEX)
978 return -1;
979 for (; j < XLFD_LAST_INDEX; j++)
980 field[j] = Qnil;
981 if (INTEGERP (field[XLFD_ENCODING_INDEX]))
982 field[XLFD_ENCODING_INDEX]
983 = Fintern (Fnumber_to_string (field[XLFD_ENCODING_INDEX]), Qnil);
984 return 0;
988 /* Parse NAME (null terminated) as XLFD and store information in FONT
989 (font-spec or font-entity). Size property of FONT is set as
990 follows:
991 specified XLFD fields FONT property
992 --------------------- -------------
993 PIXEL_SIZE PIXEL_SIZE (Lisp integer)
994 POINT_SIZE and RESY calculated pixel size (Lisp integer)
995 POINT_SIZE POINT_SIZE/10 (Lisp float)
997 If NAME is successfully parsed, return 0. Otherwise return -1.
999 FONT is usually a font-spec, but when this function is called from
1000 X font backend driver, it is a font-entity. In that case, NAME is
1001 a fully specified XLFD. */
1004 font_parse_xlfd (char *name, ptrdiff_t len, Lisp_Object font)
1006 int i, j, n;
1007 char *f[XLFD_LAST_INDEX + 1];
1008 Lisp_Object val;
1009 char *p;
1011 if (len > 255 || !len)
1012 /* Maximum XLFD name length is 255. */
1013 return -1;
1014 /* Accept "*-.." as a fully specified XLFD. */
1015 if (name[0] == '*' && (len == 1 || name[1] == '-'))
1016 i = 1, f[XLFD_FOUNDRY_INDEX] = name;
1017 else
1018 i = 0;
1019 for (p = name + i; *p; p++)
1020 if (*p == '-')
1022 f[i++] = p + 1;
1023 if (i == XLFD_LAST_INDEX)
1024 break;
1026 f[i] = name + len;
1028 #define INTERN_FIELD(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 0)
1029 #define INTERN_FIELD_SYM(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 1)
1031 if (i == XLFD_LAST_INDEX)
1033 /* Fully specified XLFD. */
1034 int pixel_size;
1036 ASET (font, FONT_FOUNDRY_INDEX, INTERN_FIELD_SYM (XLFD_FOUNDRY_INDEX));
1037 ASET (font, FONT_FAMILY_INDEX, INTERN_FIELD_SYM (XLFD_FAMILY_INDEX));
1038 for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
1039 i <= XLFD_SWIDTH_INDEX; i++, j++)
1041 val = INTERN_FIELD_SYM (i);
1042 if (! NILP (val))
1044 if ((n = font_style_to_value (j, INTERN_FIELD_SYM (i), 0)) < 0)
1045 return -1;
1046 ASET (font, j, make_number (n));
1049 ASET (font, FONT_ADSTYLE_INDEX, INTERN_FIELD_SYM (XLFD_ADSTYLE_INDEX));
1050 if (strcmp (f[XLFD_REGISTRY_INDEX], "*-*") == 0)
1051 ASET (font, FONT_REGISTRY_INDEX, Qnil);
1052 else
1053 ASET (font, FONT_REGISTRY_INDEX,
1054 font_intern_prop (f[XLFD_REGISTRY_INDEX],
1055 f[XLFD_LAST_INDEX] - f[XLFD_REGISTRY_INDEX],
1056 1));
1057 p = f[XLFD_PIXEL_INDEX];
1058 if (*p == '[' && (pixel_size = parse_matrix (p)) >= 0)
1059 ASET (font, FONT_SIZE_INDEX, make_number (pixel_size));
1060 else
1062 val = INTERN_FIELD (XLFD_PIXEL_INDEX);
1063 if (INTEGERP (val))
1064 ASET (font, FONT_SIZE_INDEX, val);
1065 else if (FONT_ENTITY_P (font))
1066 return -1;
1067 else
1069 double point_size = -1;
1071 eassert (FONT_SPEC_P (font));
1072 p = f[XLFD_POINT_INDEX];
1073 if (*p == '[')
1074 point_size = parse_matrix (p);
1075 else if (c_isdigit (*p))
1076 point_size = atoi (p), point_size /= 10;
1077 if (point_size >= 0)
1078 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1082 val = INTERN_FIELD (XLFD_RESY_INDEX);
1083 if (! NILP (val) && ! INTEGERP (val))
1084 return -1;
1085 ASET (font, FONT_DPI_INDEX, val);
1086 val = INTERN_FIELD (XLFD_SPACING_INDEX);
1087 if (! NILP (val))
1089 val = font_prop_validate_spacing (QCspacing, val);
1090 if (! INTEGERP (val))
1091 return -1;
1092 ASET (font, FONT_SPACING_INDEX, val);
1094 p = f[XLFD_AVGWIDTH_INDEX];
1095 if (*p == '~')
1096 p++;
1097 val = font_intern_prop (p, f[XLFD_REGISTRY_INDEX] - 1 - p, 0);
1098 if (! NILP (val) && ! INTEGERP (val))
1099 return -1;
1100 ASET (font, FONT_AVGWIDTH_INDEX, val);
1102 else
1104 bool wild_card_found = 0;
1105 Lisp_Object prop[XLFD_LAST_INDEX];
1107 if (FONT_ENTITY_P (font))
1108 return -1;
1109 for (j = 0; j < i; j++)
1111 if (*f[j] == '*')
1113 if (f[j][1] && f[j][1] != '-')
1114 return -1;
1115 prop[j] = Qnil;
1116 wild_card_found = 1;
1118 else if (j + 1 < i)
1119 prop[j] = INTERN_FIELD (j);
1120 else
1121 prop[j] = font_intern_prop (f[j], f[i] - f[j], 0);
1123 if (! wild_card_found)
1124 return -1;
1125 if (font_expand_wildcards (prop, i) < 0)
1126 return -1;
1128 ASET (font, FONT_FOUNDRY_INDEX, prop[XLFD_FOUNDRY_INDEX]);
1129 ASET (font, FONT_FAMILY_INDEX, prop[XLFD_FAMILY_INDEX]);
1130 for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
1131 i <= XLFD_SWIDTH_INDEX; i++, j++)
1132 if (! NILP (prop[i]))
1134 if ((n = font_style_to_value (j, prop[i], 1)) < 0)
1135 return -1;
1136 ASET (font, j, make_number (n));
1138 ASET (font, FONT_ADSTYLE_INDEX, prop[XLFD_ADSTYLE_INDEX]);
1139 val = prop[XLFD_REGISTRY_INDEX];
1140 if (NILP (val))
1142 val = prop[XLFD_ENCODING_INDEX];
1143 if (! NILP (val))
1144 val = concat2 (build_string ("*-"), SYMBOL_NAME (val));
1146 else if (NILP (prop[XLFD_ENCODING_INDEX]))
1147 val = concat2 (SYMBOL_NAME (val), build_string ("-*"));
1148 else
1149 val = concat3 (SYMBOL_NAME (val), build_string ("-"),
1150 SYMBOL_NAME (prop[XLFD_ENCODING_INDEX]));
1151 if (! NILP (val))
1152 ASET (font, FONT_REGISTRY_INDEX, Fintern (val, Qnil));
1154 if (INTEGERP (prop[XLFD_PIXEL_INDEX]))
1155 ASET (font, FONT_SIZE_INDEX, prop[XLFD_PIXEL_INDEX]);
1156 else if (INTEGERP (prop[XLFD_POINT_INDEX]))
1158 double point_size = XINT (prop[XLFD_POINT_INDEX]);
1160 ASET (font, FONT_SIZE_INDEX, make_float (point_size / 10));
1163 if (INTEGERP (prop[XLFD_RESX_INDEX]))
1164 ASET (font, FONT_DPI_INDEX, prop[XLFD_RESY_INDEX]);
1165 if (! NILP (prop[XLFD_SPACING_INDEX]))
1167 val = font_prop_validate_spacing (QCspacing,
1168 prop[XLFD_SPACING_INDEX]);
1169 if (! INTEGERP (val))
1170 return -1;
1171 ASET (font, FONT_SPACING_INDEX, val);
1173 if (INTEGERP (prop[XLFD_AVGWIDTH_INDEX]))
1174 ASET (font, FONT_AVGWIDTH_INDEX, prop[XLFD_AVGWIDTH_INDEX]);
1177 return 0;
1180 /* Store XLFD name of FONT (font-spec or font-entity) in NAME (NBYTES
1181 length), and return the name length. If FONT_SIZE_INDEX of FONT is
1182 0, use PIXEL_SIZE instead. */
1184 ptrdiff_t
1185 font_unparse_xlfd (Lisp_Object font, int pixel_size, char *name, int nbytes)
1187 char *p;
1188 const char *f[XLFD_REGISTRY_INDEX + 1];
1189 Lisp_Object val;
1190 int i, j, len;
1192 eassert (FONTP (font));
1194 for (i = FONT_FOUNDRY_INDEX, j = XLFD_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX;
1195 i++, j++)
1197 if (i == FONT_ADSTYLE_INDEX)
1198 j = XLFD_ADSTYLE_INDEX;
1199 else if (i == FONT_REGISTRY_INDEX)
1200 j = XLFD_REGISTRY_INDEX;
1201 val = AREF (font, i);
1202 if (NILP (val))
1204 if (j == XLFD_REGISTRY_INDEX)
1205 f[j] = "*-*";
1206 else
1207 f[j] = "*";
1209 else
1211 if (SYMBOLP (val))
1212 val = SYMBOL_NAME (val);
1213 if (j == XLFD_REGISTRY_INDEX
1214 && ! strchr (SSDATA (val), '-'))
1216 /* Change "jisx0208*" and "jisx0208" to "jisx0208*-*". */
1217 ptrdiff_t alloc = SBYTES (val) + 4;
1218 if (nbytes <= alloc)
1219 return -1;
1220 f[j] = p = alloca (alloc);
1221 sprintf (p, "%s%s-*", SDATA (val),
1222 &"*"[SDATA (val)[SBYTES (val) - 1] == '*']);
1224 else
1225 f[j] = SSDATA (val);
1229 for (i = FONT_WEIGHT_INDEX, j = XLFD_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX;
1230 i++, j++)
1232 val = font_style_symbolic (font, i, 0);
1233 if (NILP (val))
1234 f[j] = "*";
1235 else
1237 int c, k, l;
1238 ptrdiff_t alloc;
1240 val = SYMBOL_NAME (val);
1241 alloc = SBYTES (val) + 1;
1242 if (nbytes <= alloc)
1243 return -1;
1244 f[j] = p = alloca (alloc);
1245 /* Copy the name while excluding '-', '?', ',', and '"'. */
1246 for (k = l = 0; k < alloc; k++)
1248 c = SREF (val, k);
1249 if (c != '-' && c != '?' && c != ',' && c != '"')
1250 p[l++] = c;
1255 val = AREF (font, FONT_SIZE_INDEX);
1256 eassert (NUMBERP (val) || NILP (val));
1257 if (INTEGERP (val))
1259 EMACS_INT v = XINT (val);
1260 if (v <= 0)
1261 v = pixel_size;
1262 if (v > 0)
1264 f[XLFD_PIXEL_INDEX] = p =
1265 alloca (sizeof "-*" + INT_STRLEN_BOUND (EMACS_INT));
1266 sprintf (p, "%"pI"d-*", v);
1268 else
1269 f[XLFD_PIXEL_INDEX] = "*-*";
1271 else if (FLOATP (val))
1273 double v = XFLOAT_DATA (val) * 10;
1274 f[XLFD_PIXEL_INDEX] = p = alloca (sizeof "*-" + 1 + DBL_MAX_10_EXP + 1);
1275 sprintf (p, "*-%.0f", v);
1277 else
1278 f[XLFD_PIXEL_INDEX] = "*-*";
1280 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1282 EMACS_INT v = XINT (AREF (font, FONT_DPI_INDEX));
1283 f[XLFD_RESX_INDEX] = p =
1284 alloca (sizeof "-" + 2 * INT_STRLEN_BOUND (EMACS_INT));
1285 sprintf (p, "%"pI"d-%"pI"d", v, v);
1287 else
1288 f[XLFD_RESX_INDEX] = "*-*";
1289 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1291 EMACS_INT spacing = XINT (AREF (font, FONT_SPACING_INDEX));
1293 f[XLFD_SPACING_INDEX] = (spacing <= FONT_SPACING_PROPORTIONAL ? "p"
1294 : spacing <= FONT_SPACING_DUAL ? "d"
1295 : spacing <= FONT_SPACING_MONO ? "m"
1296 : "c");
1298 else
1299 f[XLFD_SPACING_INDEX] = "*";
1300 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1302 f[XLFD_AVGWIDTH_INDEX] = p = alloca (INT_BUFSIZE_BOUND (EMACS_INT));
1303 sprintf (p, "%"pI"d", XINT (AREF (font, FONT_AVGWIDTH_INDEX)));
1305 else
1306 f[XLFD_AVGWIDTH_INDEX] = "*";
1307 len = snprintf (name, nbytes, "-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s",
1308 f[XLFD_FOUNDRY_INDEX], f[XLFD_FAMILY_INDEX],
1309 f[XLFD_WEIGHT_INDEX], f[XLFD_SLANT_INDEX],
1310 f[XLFD_SWIDTH_INDEX], f[XLFD_ADSTYLE_INDEX],
1311 f[XLFD_PIXEL_INDEX], f[XLFD_RESX_INDEX],
1312 f[XLFD_SPACING_INDEX], f[XLFD_AVGWIDTH_INDEX],
1313 f[XLFD_REGISTRY_INDEX]);
1314 return len < nbytes ? len : -1;
1317 /* Parse NAME (null terminated) and store information in FONT
1318 (font-spec or font-entity). NAME is supplied in either the
1319 Fontconfig or GTK font name format. If NAME is successfully
1320 parsed, return 0. Otherwise return -1.
1322 The fontconfig format is
1324 FAMILY[-SIZE][:PROP1[=VAL1][:PROP2[=VAL2]...]]
1326 The GTK format is
1328 FAMILY [PROPS...] [SIZE]
1330 This function tries to guess which format it is. */
1332 static int
1333 font_parse_fcname (char *name, ptrdiff_t len, Lisp_Object font)
1335 char *p, *q;
1336 char *size_beg = NULL, *size_end = NULL;
1337 char *props_beg = NULL, *family_end = NULL;
1339 if (len == 0)
1340 return -1;
1342 for (p = name; *p; p++)
1344 if (*p == '\\' && p[1])
1345 p++;
1346 else if (*p == ':')
1348 props_beg = family_end = p;
1349 break;
1351 else if (*p == '-')
1353 bool decimal = 0, size_found = 1;
1354 for (q = p + 1; *q && *q != ':'; q++)
1355 if (! c_isdigit (*q))
1357 if (*q != '.' || decimal)
1359 size_found = 0;
1360 break;
1362 decimal = 1;
1364 if (size_found)
1366 family_end = p;
1367 size_beg = p + 1;
1368 size_end = q;
1369 break;
1374 if (family_end)
1376 Lisp_Object extra_props = Qnil;
1378 /* A fontconfig name with size and/or property data. */
1379 if (family_end > name)
1381 Lisp_Object family;
1382 family = font_intern_prop (name, family_end - name, 1);
1383 ASET (font, FONT_FAMILY_INDEX, family);
1385 if (size_beg)
1387 double point_size = strtod (size_beg, &size_end);
1388 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1389 if (*size_end == ':' && size_end[1])
1390 props_beg = size_end;
1392 if (props_beg)
1394 /* Now parse ":KEY=VAL" patterns. */
1395 Lisp_Object val;
1397 for (p = props_beg; *p; p = q)
1399 for (q = p + 1; *q && *q != '=' && *q != ':'; q++);
1400 if (*q != '=')
1402 /* Must be an enumerated value. */
1403 ptrdiff_t word_len;
1404 p = p + 1;
1405 word_len = q - p;
1406 val = font_intern_prop (p, q - p, 1);
1408 #define PROP_MATCH(STR) (word_len == strlen (STR) \
1409 && memcmp (p, STR, strlen (STR)) == 0)
1411 if (PROP_MATCH ("light")
1412 || PROP_MATCH ("medium")
1413 || PROP_MATCH ("demibold")
1414 || PROP_MATCH ("bold")
1415 || PROP_MATCH ("black"))
1416 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, val);
1417 else if (PROP_MATCH ("roman")
1418 || PROP_MATCH ("italic")
1419 || PROP_MATCH ("oblique"))
1420 FONT_SET_STYLE (font, FONT_SLANT_INDEX, val);
1421 else if (PROP_MATCH ("charcell"))
1422 ASET (font, FONT_SPACING_INDEX,
1423 make_number (FONT_SPACING_CHARCELL));
1424 else if (PROP_MATCH ("mono"))
1425 ASET (font, FONT_SPACING_INDEX,
1426 make_number (FONT_SPACING_MONO));
1427 else if (PROP_MATCH ("proportional"))
1428 ASET (font, FONT_SPACING_INDEX,
1429 make_number (FONT_SPACING_PROPORTIONAL));
1430 #undef PROP_MATCH
1432 else
1434 /* KEY=VAL pairs */
1435 Lisp_Object key;
1436 int prop;
1438 if (q - p == 10 && memcmp (p + 1, "pixelsize", 9) == 0)
1439 prop = FONT_SIZE_INDEX;
1440 else
1442 key = font_intern_prop (p, q - p, 1);
1443 prop = get_font_prop_index (key);
1446 p = q + 1;
1447 for (q = p; *q && *q != ':'; q++);
1448 val = font_intern_prop (p, q - p, 0);
1450 if (prop >= FONT_FOUNDRY_INDEX
1451 && prop < FONT_EXTRA_INDEX)
1452 ASET (font, prop, font_prop_validate (prop, Qnil, val));
1453 else
1455 extra_props = nconc2 (extra_props,
1456 list1 (Fcons (key, val)));
1459 p = q;
1463 if (! NILP (extra_props))
1465 struct font_driver_list *driver_list = font_driver_list;
1466 for ( ; driver_list; driver_list = driver_list->next)
1467 if (driver_list->driver->filter_properties)
1468 (*driver_list->driver->filter_properties) (font, extra_props);
1472 else
1474 /* Either a fontconfig-style name with no size and property
1475 data, or a GTK-style name. */
1476 Lisp_Object weight = Qnil, slant = Qnil;
1477 Lisp_Object width = Qnil, size = Qnil;
1478 char *word_start;
1479 ptrdiff_t word_len;
1481 /* Scan backwards from the end, looking for a size. */
1482 for (p = name + len - 1; p >= name; p--)
1483 if (!c_isdigit (*p))
1484 break;
1486 if ((p < name + len - 1) && ((p + 1 == name) || *p == ' '))
1487 /* Found a font size. */
1488 size = make_float (strtod (p + 1, NULL));
1489 else
1490 p = name + len;
1492 /* Now P points to the termination of the string, sans size.
1493 Scan backwards, looking for font properties. */
1494 for (; p > name; p = q)
1496 for (q = p - 1; q >= name; q--)
1498 if (q > name && *(q-1) == '\\')
1499 --q; /* Skip quoting backslashes. */
1500 else if (*q == ' ')
1501 break;
1504 word_start = q + 1;
1505 word_len = p - word_start;
1507 #define PROP_MATCH(STR) \
1508 (word_len == strlen (STR) \
1509 && memcmp (word_start, STR, strlen (STR)) == 0)
1510 #define PROP_SAVE(VAR, STR) \
1511 (VAR = NILP (VAR) ? font_intern_prop (STR, strlen (STR), 1) : VAR)
1513 if (PROP_MATCH ("Ultra-Light"))
1514 PROP_SAVE (weight, "ultra-light");
1515 else if (PROP_MATCH ("Light"))
1516 PROP_SAVE (weight, "light");
1517 else if (PROP_MATCH ("Book"))
1518 PROP_SAVE (weight, "book");
1519 else if (PROP_MATCH ("Medium"))
1520 PROP_SAVE (weight, "medium");
1521 else if (PROP_MATCH ("Semi-Bold"))
1522 PROP_SAVE (weight, "semi-bold");
1523 else if (PROP_MATCH ("Bold"))
1524 PROP_SAVE (weight, "bold");
1525 else if (PROP_MATCH ("Italic"))
1526 PROP_SAVE (slant, "italic");
1527 else if (PROP_MATCH ("Oblique"))
1528 PROP_SAVE (slant, "oblique");
1529 else if (PROP_MATCH ("Semi-Condensed"))
1530 PROP_SAVE (width, "semi-condensed");
1531 else if (PROP_MATCH ("Condensed"))
1532 PROP_SAVE (width, "condensed");
1533 /* An unknown word must be part of the font name. */
1534 else
1536 family_end = p;
1537 break;
1540 #undef PROP_MATCH
1541 #undef PROP_SAVE
1543 if (family_end)
1544 ASET (font, FONT_FAMILY_INDEX,
1545 font_intern_prop (name, family_end - name, 1));
1546 if (!NILP (size))
1547 ASET (font, FONT_SIZE_INDEX, size);
1548 if (!NILP (weight))
1549 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, weight);
1550 if (!NILP (slant))
1551 FONT_SET_STYLE (font, FONT_SLANT_INDEX, slant);
1552 if (!NILP (width))
1553 FONT_SET_STYLE (font, FONT_WIDTH_INDEX, width);
1556 return 0;
1559 /* Store fontconfig's font name of FONT (font-spec or font-entity) in
1560 NAME (NBYTES length), and return the name length. If
1561 FONT_SIZE_INDEX of FONT is 0, use PIXEL_SIZE instead. */
1564 font_unparse_fcname (Lisp_Object font, int pixel_size, char *name, int nbytes)
1566 Lisp_Object family, foundry;
1567 Lisp_Object val;
1568 int point_size;
1569 int i;
1570 char *p;
1571 char *lim;
1572 Lisp_Object styles[3];
1573 const char *style_names[3] = { "weight", "slant", "width" };
1575 family = AREF (font, FONT_FAMILY_INDEX);
1576 if (! NILP (family))
1578 if (SYMBOLP (family))
1579 family = SYMBOL_NAME (family);
1580 else
1581 family = Qnil;
1584 val = AREF (font, FONT_SIZE_INDEX);
1585 if (INTEGERP (val))
1587 if (XINT (val) != 0)
1588 pixel_size = XINT (val);
1589 point_size = -1;
1591 else
1593 eassert (FLOATP (val));
1594 pixel_size = -1;
1595 point_size = (int) XFLOAT_DATA (val);
1598 foundry = AREF (font, FONT_FOUNDRY_INDEX);
1599 if (! NILP (foundry))
1601 if (SYMBOLP (foundry))
1602 foundry = SYMBOL_NAME (foundry);
1603 else
1604 foundry = Qnil;
1607 for (i = 0; i < 3; i++)
1608 styles[i] = font_style_symbolic (font, FONT_WEIGHT_INDEX + i, 0);
1610 p = name;
1611 lim = name + nbytes;
1612 if (! NILP (family))
1614 int len = snprintf (p, lim - p, "%s", SSDATA (family));
1615 if (! (0 <= len && len < lim - p))
1616 return -1;
1617 p += len;
1619 if (point_size > 0)
1621 int len = snprintf (p, lim - p, &"-%d"[p == name], point_size);
1622 if (! (0 <= len && len < lim - p))
1623 return -1;
1624 p += len;
1626 else if (pixel_size > 0)
1628 int len = snprintf (p, lim - p, ":pixelsize=%d", pixel_size);
1629 if (! (0 <= len && len < lim - p))
1630 return -1;
1631 p += len;
1633 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
1635 int len = snprintf (p, lim - p, ":foundry=%s",
1636 SSDATA (SYMBOL_NAME (AREF (font,
1637 FONT_FOUNDRY_INDEX))));
1638 if (! (0 <= len && len < lim - p))
1639 return -1;
1640 p += len;
1642 for (i = 0; i < 3; i++)
1643 if (! NILP (styles[i]))
1645 int len = snprintf (p, lim - p, ":%s=%s", style_names[i],
1646 SSDATA (SYMBOL_NAME (styles[i])));
1647 if (! (0 <= len && len < lim - p))
1648 return -1;
1649 p += len;
1652 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1654 int len = snprintf (p, lim - p, ":dpi=%"pI"d",
1655 XINT (AREF (font, FONT_DPI_INDEX)));
1656 if (! (0 <= len && len < lim - p))
1657 return -1;
1658 p += len;
1661 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1663 int len = snprintf (p, lim - p, ":spacing=%"pI"d",
1664 XINT (AREF (font, FONT_SPACING_INDEX)));
1665 if (! (0 <= len && len < lim - p))
1666 return -1;
1667 p += len;
1670 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1672 int len = snprintf (p, lim - p,
1673 (XINT (AREF (font, FONT_AVGWIDTH_INDEX)) == 0
1674 ? ":scalable=true"
1675 : ":scalable=false"));
1676 if (! (0 <= len && len < lim - p))
1677 return -1;
1678 p += len;
1681 return (p - name);
1684 /* Parse NAME (null terminated) and store information in FONT
1685 (font-spec or font-entity). If NAME is successfully parsed, return
1686 0. Otherwise return -1. */
1688 static int
1689 font_parse_name (char *name, ptrdiff_t namelen, Lisp_Object font)
1691 if (name[0] == '-' || strchr (name, '*') || strchr (name, '?'))
1692 return font_parse_xlfd (name, namelen, font);
1693 return font_parse_fcname (name, namelen, font);
1697 /* Merge FAMILY and REGISTRY into FONT_SPEC. FAMILY may have the form
1698 "FAMILY-FOUNDRY". REGISTRY may not contain charset-encoding
1699 part. */
1701 void
1702 font_parse_family_registry (Lisp_Object family, Lisp_Object registry, Lisp_Object font_spec)
1704 int len;
1705 char *p0, *p1;
1707 if (! NILP (family)
1708 && NILP (AREF (font_spec, FONT_FAMILY_INDEX)))
1710 CHECK_STRING (family);
1711 len = SBYTES (family);
1712 p0 = SSDATA (family);
1713 p1 = strchr (p0, '-');
1714 if (p1)
1716 if ((*p0 != '*' && p1 - p0 > 0)
1717 && NILP (AREF (font_spec, FONT_FOUNDRY_INDEX)))
1718 Ffont_put (font_spec, QCfoundry, font_intern_prop (p0, p1 - p0, 1));
1719 p1++;
1720 len -= p1 - p0;
1721 Ffont_put (font_spec, QCfamily, font_intern_prop (p1, len, 1));
1723 else
1724 ASET (font_spec, FONT_FAMILY_INDEX, Fintern (family, Qnil));
1726 if (! NILP (registry))
1728 /* Convert "XXX" and "XXX*" to "XXX*-*". */
1729 CHECK_STRING (registry);
1730 len = SBYTES (registry);
1731 p0 = SSDATA (registry);
1732 p1 = strchr (p0, '-');
1733 if (! p1)
1735 if (SDATA (registry)[len - 1] == '*')
1736 registry = concat2 (registry, build_string ("-*"));
1737 else
1738 registry = concat2 (registry, build_string ("*-*"));
1740 registry = Fdowncase (registry);
1741 ASET (font_spec, FONT_REGISTRY_INDEX, Fintern (registry, Qnil));
1746 /* This part (through the next ^L) is still experimental and not
1747 tested much. We may drastically change codes. */
1749 /* OTF handler. */
1751 #if 0
1753 #define LGSTRING_HEADER_SIZE 6
1754 #define LGSTRING_GLYPH_SIZE 8
1756 static int
1757 check_gstring (Lisp_Object gstring)
1759 Lisp_Object val;
1760 ptrdiff_t i;
1761 int j;
1763 CHECK_VECTOR (gstring);
1764 val = AREF (gstring, 0);
1765 CHECK_VECTOR (val);
1766 if (ASIZE (val) < LGSTRING_HEADER_SIZE)
1767 goto err;
1768 CHECK_FONT_OBJECT (LGSTRING_FONT (gstring));
1769 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_LBEARING)))
1770 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_LBEARING));
1771 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_RBEARING)))
1772 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_RBEARING));
1773 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_WIDTH)))
1774 CHECK_NATNUM (LGSTRING_SLOT (gstring, LGSTRING_IX_WIDTH));
1775 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT)))
1776 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT));
1777 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT)))
1778 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT));
1780 for (i = 0; i < LGSTRING_GLYPH_LEN (gstring); i++)
1782 val = LGSTRING_GLYPH (gstring, i);
1783 CHECK_VECTOR (val);
1784 if (ASIZE (val) < LGSTRING_GLYPH_SIZE)
1785 goto err;
1786 if (NILP (AREF (val, LGLYPH_IX_CHAR)))
1787 break;
1788 CHECK_NATNUM (AREF (val, LGLYPH_IX_FROM));
1789 CHECK_NATNUM (AREF (val, LGLYPH_IX_TO));
1790 CHECK_CHARACTER (AREF (val, LGLYPH_IX_CHAR));
1791 if (!NILP (AREF (val, LGLYPH_IX_CODE)))
1792 CHECK_NATNUM (AREF (val, LGLYPH_IX_CODE));
1793 if (!NILP (AREF (val, LGLYPH_IX_WIDTH)))
1794 CHECK_NATNUM (AREF (val, LGLYPH_IX_WIDTH));
1795 if (!NILP (AREF (val, LGLYPH_IX_ADJUSTMENT)))
1797 val = AREF (val, LGLYPH_IX_ADJUSTMENT);
1798 CHECK_VECTOR (val);
1799 if (ASIZE (val) < 3)
1800 goto err;
1801 for (j = 0; j < 3; j++)
1802 CHECK_NUMBER (AREF (val, j));
1805 return i;
1806 err:
1807 error ("Invalid glyph-string format");
1808 return -1;
1811 static void
1812 check_otf_features (Lisp_Object otf_features)
1814 Lisp_Object val;
1816 CHECK_CONS (otf_features);
1817 CHECK_SYMBOL (XCAR (otf_features));
1818 otf_features = XCDR (otf_features);
1819 CHECK_CONS (otf_features);
1820 CHECK_SYMBOL (XCAR (otf_features));
1821 otf_features = XCDR (otf_features);
1822 for (val = Fcar (otf_features); CONSP (val); val = XCDR (val))
1824 CHECK_SYMBOL (XCAR (val));
1825 if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
1826 error ("Invalid OTF GSUB feature: %s",
1827 SDATA (SYMBOL_NAME (XCAR (val))));
1829 otf_features = XCDR (otf_features);
1830 for (val = Fcar (otf_features); CONSP (val); val = XCDR (val))
1832 CHECK_SYMBOL (XCAR (val));
1833 if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
1834 error ("Invalid OTF GPOS feature: %s",
1835 SDATA (SYMBOL_NAME (XCAR (val))));
1839 #ifdef HAVE_LIBOTF
1840 #include <otf.h>
1842 Lisp_Object otf_list;
1844 static Lisp_Object
1845 otf_tag_symbol (OTF_Tag tag)
1847 char name[5];
1849 OTF_tag_name (tag, name);
1850 return Fintern (make_unibyte_string (name, 4), Qnil);
1853 static OTF *
1854 otf_open (Lisp_Object file)
1856 Lisp_Object val = Fassoc (file, otf_list);
1857 OTF *otf;
1859 if (! NILP (val))
1860 otf = XSAVE_POINTER (XCDR (val), 0);
1861 else
1863 otf = STRINGP (file) ? OTF_open (SSDATA (file)) : NULL;
1864 val = make_save_ptr (otf);
1865 otf_list = Fcons (Fcons (file, val), otf_list);
1867 return otf;
1871 /* Return a list describing which scripts/languages FONT supports by
1872 which GSUB/GPOS features of OpenType tables. See the comment of
1873 (struct font_driver).otf_capability. */
1875 Lisp_Object
1876 font_otf_capability (struct font *font)
1878 OTF *otf;
1879 Lisp_Object capability = Fcons (Qnil, Qnil);
1880 int i;
1882 otf = otf_open (font->props[FONT_FILE_INDEX]);
1883 if (! otf)
1884 return Qnil;
1885 for (i = 0; i < 2; i++)
1887 OTF_GSUB_GPOS *gsub_gpos;
1888 Lisp_Object script_list = Qnil;
1889 int j;
1891 if (OTF_get_features (otf, i == 0) < 0)
1892 continue;
1893 gsub_gpos = i == 0 ? otf->gsub : otf->gpos;
1894 for (j = gsub_gpos->ScriptList.ScriptCount - 1; j >= 0; j--)
1896 OTF_Script *script = gsub_gpos->ScriptList.Script + j;
1897 Lisp_Object langsys_list = Qnil;
1898 Lisp_Object script_tag = otf_tag_symbol (script->ScriptTag);
1899 int k;
1901 for (k = script->LangSysCount; k >= 0; k--)
1903 OTF_LangSys *langsys;
1904 Lisp_Object feature_list = Qnil;
1905 Lisp_Object langsys_tag;
1906 int l;
1908 if (k == script->LangSysCount)
1910 langsys = &script->DefaultLangSys;
1911 langsys_tag = Qnil;
1913 else
1915 langsys = script->LangSys + k;
1916 langsys_tag
1917 = otf_tag_symbol (script->LangSysRecord[k].LangSysTag);
1919 for (l = langsys->FeatureCount - 1; l >= 0; l--)
1921 OTF_Feature *feature
1922 = gsub_gpos->FeatureList.Feature + langsys->FeatureIndex[l];
1923 Lisp_Object feature_tag
1924 = otf_tag_symbol (feature->FeatureTag);
1926 feature_list = Fcons (feature_tag, feature_list);
1928 langsys_list = Fcons (Fcons (langsys_tag, feature_list),
1929 langsys_list);
1931 script_list = Fcons (Fcons (script_tag, langsys_list),
1932 script_list);
1935 if (i == 0)
1936 XSETCAR (capability, script_list);
1937 else
1938 XSETCDR (capability, script_list);
1941 return capability;
1944 /* Parse OTF features in SPEC and write a proper features spec string
1945 in FEATURES for the call of OTF_drive_gsub/gpos (of libotf). It is
1946 assured that the sufficient memory has already allocated for
1947 FEATURES. */
1949 static void
1950 generate_otf_features (Lisp_Object spec, char *features)
1952 Lisp_Object val;
1953 char *p;
1954 bool asterisk;
1956 p = features;
1957 *p = '\0';
1958 for (asterisk = 0; CONSP (spec); spec = XCDR (spec))
1960 val = XCAR (spec);
1961 CHECK_SYMBOL (val);
1962 if (p > features)
1963 *p++ = ',';
1964 if (SREF (SYMBOL_NAME (val), 0) == '*')
1966 asterisk = 1;
1967 *p++ = '*';
1969 else if (! asterisk)
1971 val = SYMBOL_NAME (val);
1972 p += esprintf (p, "%s", SDATA (val));
1974 else
1976 val = SYMBOL_NAME (val);
1977 p += esprintf (p, "~%s", SDATA (val));
1980 if (CONSP (spec))
1981 error ("OTF spec too long");
1984 Lisp_Object
1985 font_otf_DeviceTable (OTF_DeviceTable *device_table)
1987 int len = device_table->StartSize - device_table->EndSize + 1;
1989 return Fcons (make_number (len),
1990 make_unibyte_string (device_table->DeltaValue, len));
1993 Lisp_Object
1994 font_otf_ValueRecord (int value_format, OTF_ValueRecord *value_record)
1996 Lisp_Object val = Fmake_vector (make_number (8), Qnil);
1998 if (value_format & OTF_XPlacement)
1999 ASET (val, 0, make_number (value_record->XPlacement));
2000 if (value_format & OTF_YPlacement)
2001 ASET (val, 1, make_number (value_record->YPlacement));
2002 if (value_format & OTF_XAdvance)
2003 ASET (val, 2, make_number (value_record->XAdvance));
2004 if (value_format & OTF_YAdvance)
2005 ASET (val, 3, make_number (value_record->YAdvance));
2006 if (value_format & OTF_XPlaDevice)
2007 ASET (val, 4, font_otf_DeviceTable (&value_record->XPlaDevice));
2008 if (value_format & OTF_YPlaDevice)
2009 ASET (val, 4, font_otf_DeviceTable (&value_record->YPlaDevice));
2010 if (value_format & OTF_XAdvDevice)
2011 ASET (val, 4, font_otf_DeviceTable (&value_record->XAdvDevice));
2012 if (value_format & OTF_YAdvDevice)
2013 ASET (val, 4, font_otf_DeviceTable (&value_record->YAdvDevice));
2014 return val;
2017 Lisp_Object
2018 font_otf_Anchor (OTF_Anchor *anchor)
2020 Lisp_Object val;
2022 val = Fmake_vector (make_number (anchor->AnchorFormat + 1), Qnil);
2023 ASET (val, 0, make_number (anchor->XCoordinate));
2024 ASET (val, 1, make_number (anchor->YCoordinate));
2025 if (anchor->AnchorFormat == 2)
2026 ASET (val, 2, make_number (anchor->f.f1.AnchorPoint));
2027 else
2029 ASET (val, 3, font_otf_DeviceTable (&anchor->f.f2.XDeviceTable));
2030 ASET (val, 4, font_otf_DeviceTable (&anchor->f.f2.YDeviceTable));
2032 return val;
2034 #endif /* HAVE_LIBOTF */
2035 #endif /* 0 */
2038 /* Font sorting. */
2040 static double
2041 font_rescale_ratio (Lisp_Object font_entity)
2043 Lisp_Object tail, elt;
2044 Lisp_Object name = Qnil;
2046 for (tail = Vface_font_rescale_alist; CONSP (tail); tail = XCDR (tail))
2048 elt = XCAR (tail);
2049 if (FLOATP (XCDR (elt)))
2051 if (STRINGP (XCAR (elt)))
2053 if (NILP (name))
2054 name = Ffont_xlfd_name (font_entity, Qnil);
2055 if (fast_string_match_ignore_case (XCAR (elt), name) >= 0)
2056 return XFLOAT_DATA (XCDR (elt));
2058 else if (FONT_SPEC_P (XCAR (elt)))
2060 if (font_match_p (XCAR (elt), font_entity))
2061 return XFLOAT_DATA (XCDR (elt));
2065 return 1.0;
2068 /* We sort fonts by scoring each of them against a specified
2069 font-spec. The score value is 32 bit (`unsigned'), and the smaller
2070 the value is, the closer the font is to the font-spec.
2072 The lowest 2 bits of the score are used for driver type. The font
2073 available by the most preferred font driver is 0.
2075 The 4 7-bit fields in the higher 28 bits are used for numeric properties
2076 WEIGHT, SLANT, WIDTH, and SIZE. */
2078 /* How many bits to shift to store the difference value of each font
2079 property in a score. Note that floats for FONT_TYPE_INDEX and
2080 FONT_REGISTRY_INDEX are not used. */
2081 static int sort_shift_bits[FONT_SIZE_INDEX + 1];
2083 /* Score font-entity ENTITY against properties of font-spec SPEC_PROP.
2084 The return value indicates how different ENTITY is compared with
2085 SPEC_PROP. */
2087 static unsigned
2088 font_score (Lisp_Object entity, Lisp_Object *spec_prop)
2090 unsigned score = 0;
2091 int i;
2093 /* Score three style numeric fields. Maximum difference is 127. */
2094 for (i = FONT_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX; i++)
2095 if (! NILP (spec_prop[i]) && ! EQ (AREF (entity, i), spec_prop[i]))
2097 EMACS_INT diff = ((XINT (AREF (entity, i)) >> 8)
2098 - (XINT (spec_prop[i]) >> 8));
2099 score |= min (eabs (diff), 127) << sort_shift_bits[i];
2102 /* Score the size. Maximum difference is 127. */
2103 i = FONT_SIZE_INDEX;
2104 if (! NILP (spec_prop[FONT_SIZE_INDEX])
2105 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
2107 /* We use the higher 6-bit for the actual size difference. The
2108 lowest bit is set if the DPI is different. */
2109 EMACS_INT diff;
2110 EMACS_INT pixel_size = XINT (spec_prop[FONT_SIZE_INDEX]);
2112 if (CONSP (Vface_font_rescale_alist))
2113 pixel_size *= font_rescale_ratio (entity);
2114 diff = eabs (pixel_size - XINT (AREF (entity, FONT_SIZE_INDEX))) << 1;
2115 if (! NILP (spec_prop[FONT_DPI_INDEX])
2116 && ! EQ (spec_prop[FONT_DPI_INDEX], AREF (entity, FONT_DPI_INDEX)))
2117 diff |= 1;
2118 if (! NILP (spec_prop[FONT_AVGWIDTH_INDEX])
2119 && ! EQ (spec_prop[FONT_AVGWIDTH_INDEX], AREF (entity, FONT_AVGWIDTH_INDEX)))
2120 diff |= 1;
2121 score |= min (diff, 127) << sort_shift_bits[FONT_SIZE_INDEX];
2124 return score;
2128 /* Concatenate all elements of LIST into one vector. LIST is a list
2129 of font-entity vectors. */
2131 static Lisp_Object
2132 font_vconcat_entity_vectors (Lisp_Object list)
2134 int nargs = XINT (Flength (list));
2135 Lisp_Object *args = alloca (word_size * nargs);
2136 int i;
2138 for (i = 0; i < nargs; i++, list = XCDR (list))
2139 args[i] = XCAR (list);
2140 return Fvconcat (nargs, args);
2144 /* The structure for elements being sorted by qsort. */
2145 struct font_sort_data
2147 unsigned score;
2148 int font_driver_preference;
2149 Lisp_Object entity;
2153 /* The comparison function for qsort. */
2155 static int
2156 font_compare (const void *d1, const void *d2)
2158 const struct font_sort_data *data1 = d1;
2159 const struct font_sort_data *data2 = d2;
2161 if (data1->score < data2->score)
2162 return -1;
2163 else if (data1->score > data2->score)
2164 return 1;
2165 return (data1->font_driver_preference - data2->font_driver_preference);
2169 /* Sort each font-entity vector in LIST by closeness to font-spec PREFER.
2170 If PREFER specifies a point-size, calculate the corresponding
2171 pixel-size from QCdpi property of PREFER or from the Y-resolution
2172 of FRAME before sorting.
2174 If BEST-ONLY is nonzero, return the best matching entity (that
2175 supports the character BEST-ONLY if BEST-ONLY is positive, or any
2176 if BEST-ONLY is negative). Otherwise, return the sorted result as
2177 a single vector of font-entities.
2179 This function does no optimization for the case that the total
2180 number of elements is 1. The caller should avoid calling this in
2181 such a case. */
2183 static Lisp_Object
2184 font_sort_entities (Lisp_Object list, Lisp_Object prefer,
2185 struct frame *f, int best_only)
2187 Lisp_Object prefer_prop[FONT_SPEC_MAX];
2188 int len, maxlen, i;
2189 struct font_sort_data *data;
2190 unsigned best_score;
2191 Lisp_Object best_entity;
2192 Lisp_Object tail, vec IF_LINT (= Qnil);
2193 USE_SAFE_ALLOCA;
2195 for (i = FONT_WEIGHT_INDEX; i <= FONT_AVGWIDTH_INDEX; i++)
2196 prefer_prop[i] = AREF (prefer, i);
2197 if (FLOATP (prefer_prop[FONT_SIZE_INDEX]))
2198 prefer_prop[FONT_SIZE_INDEX]
2199 = make_number (font_pixel_size (f, prefer));
2201 if (NILP (XCDR (list)))
2203 /* What we have to take care of is this single vector. */
2204 vec = XCAR (list);
2205 maxlen = ASIZE (vec);
2207 else if (best_only)
2209 /* We don't have to perform sort, so there's no need of creating
2210 a single vector. But, we must find the length of the longest
2211 vector. */
2212 maxlen = 0;
2213 for (tail = list; CONSP (tail); tail = XCDR (tail))
2214 if (maxlen < ASIZE (XCAR (tail)))
2215 maxlen = ASIZE (XCAR (tail));
2217 else
2219 /* We have to create a single vector to sort it. */
2220 vec = font_vconcat_entity_vectors (list);
2221 maxlen = ASIZE (vec);
2224 data = SAFE_ALLOCA (maxlen * sizeof *data);
2225 best_score = 0xFFFFFFFF;
2226 best_entity = Qnil;
2228 for (tail = list; CONSP (tail); tail = XCDR (tail))
2230 int font_driver_preference = 0;
2231 Lisp_Object current_font_driver;
2233 if (best_only)
2234 vec = XCAR (tail);
2235 len = ASIZE (vec);
2237 /* We are sure that the length of VEC > 0. */
2238 current_font_driver = AREF (AREF (vec, 0), FONT_TYPE_INDEX);
2239 /* Score the elements. */
2240 for (i = 0; i < len; i++)
2242 data[i].entity = AREF (vec, i);
2243 data[i].score
2244 = ((best_only <= 0 || font_has_char (f, data[i].entity, best_only)
2245 > 0)
2246 ? font_score (data[i].entity, prefer_prop)
2247 : 0xFFFFFFFF);
2248 if (best_only && best_score > data[i].score)
2250 best_score = data[i].score;
2251 best_entity = data[i].entity;
2252 if (best_score == 0)
2253 break;
2255 if (! EQ (current_font_driver, AREF (AREF (vec, i), FONT_TYPE_INDEX)))
2257 current_font_driver = AREF (AREF (vec, i), FONT_TYPE_INDEX);
2258 font_driver_preference++;
2260 data[i].font_driver_preference = font_driver_preference;
2263 /* Sort if necessary. */
2264 if (! best_only)
2266 qsort (data, len, sizeof *data, font_compare);
2267 for (i = 0; i < len; i++)
2268 ASET (vec, i, data[i].entity);
2269 break;
2271 else
2272 vec = best_entity;
2275 SAFE_FREE ();
2277 FONT_ADD_LOG ("sort-by", prefer, vec);
2278 return vec;
2282 /* API of Font Service Layer. */
2284 /* Reflect ORDER (see the variable font_sort_order in xfaces.c) to
2285 sort_shift_bits. Finternal_set_font_selection_order calls this
2286 function with font_sort_order after setting up it. */
2288 void
2289 font_update_sort_order (int *order)
2291 int i, shift_bits;
2293 for (i = 0, shift_bits = 23; i < 4; i++, shift_bits -= 7)
2295 int xlfd_idx = order[i];
2297 if (xlfd_idx == XLFD_WEIGHT_INDEX)
2298 sort_shift_bits[FONT_WEIGHT_INDEX] = shift_bits;
2299 else if (xlfd_idx == XLFD_SLANT_INDEX)
2300 sort_shift_bits[FONT_SLANT_INDEX] = shift_bits;
2301 else if (xlfd_idx == XLFD_SWIDTH_INDEX)
2302 sort_shift_bits[FONT_WIDTH_INDEX] = shift_bits;
2303 else
2304 sort_shift_bits[FONT_SIZE_INDEX] = shift_bits;
2308 static bool
2309 font_check_otf_features (Lisp_Object script, Lisp_Object langsys,
2310 Lisp_Object features, Lisp_Object table)
2312 Lisp_Object val;
2313 bool negative;
2315 table = assq_no_quit (script, table);
2316 if (NILP (table))
2317 return 0;
2318 table = XCDR (table);
2319 if (! NILP (langsys))
2321 table = assq_no_quit (langsys, table);
2322 if (NILP (table))
2323 return 0;
2325 else
2327 val = assq_no_quit (Qnil, table);
2328 if (NILP (val))
2329 table = XCAR (table);
2330 else
2331 table = val;
2333 table = XCDR (table);
2334 for (negative = 0; CONSP (features); features = XCDR (features))
2336 if (NILP (XCAR (features)))
2338 negative = 1;
2339 continue;
2341 if (NILP (Fmemq (XCAR (features), table)) != negative)
2342 return 0;
2344 return 1;
2347 /* Check if OTF_CAPABILITY satisfies SPEC (otf-spec). */
2349 static bool
2350 font_check_otf (Lisp_Object spec, Lisp_Object otf_capability)
2352 Lisp_Object script, langsys = Qnil, gsub = Qnil, gpos = Qnil;
2354 script = XCAR (spec);
2355 spec = XCDR (spec);
2356 if (! NILP (spec))
2358 langsys = XCAR (spec);
2359 spec = XCDR (spec);
2360 if (! NILP (spec))
2362 gsub = XCAR (spec);
2363 spec = XCDR (spec);
2364 if (! NILP (spec))
2365 gpos = XCAR (spec);
2369 if (! NILP (gsub) && ! font_check_otf_features (script, langsys, gsub,
2370 XCAR (otf_capability)))
2371 return 0;
2372 if (! NILP (gpos) && ! font_check_otf_features (script, langsys, gpos,
2373 XCDR (otf_capability)))
2374 return 0;
2375 return 1;
2380 /* Check if FONT (font-entity or font-object) matches with the font
2381 specification SPEC. */
2383 bool
2384 font_match_p (Lisp_Object spec, Lisp_Object font)
2386 Lisp_Object prop[FONT_SPEC_MAX], *props;
2387 Lisp_Object extra, font_extra;
2388 int i;
2390 for (i = FONT_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX; i++)
2391 if (! NILP (AREF (spec, i))
2392 && ! NILP (AREF (font, i))
2393 && ! EQ (AREF (spec, i), AREF (font, i)))
2394 return 0;
2395 props = XFONT_SPEC (spec)->props;
2396 if (FLOATP (props[FONT_SIZE_INDEX]))
2398 for (i = FONT_FOUNDRY_INDEX; i < FONT_SIZE_INDEX; i++)
2399 prop[i] = AREF (spec, i);
2400 prop[FONT_SIZE_INDEX]
2401 = make_number (font_pixel_size (XFRAME (selected_frame), spec));
2402 props = prop;
2405 if (font_score (font, props) > 0)
2406 return 0;
2407 extra = AREF (spec, FONT_EXTRA_INDEX);
2408 font_extra = AREF (font, FONT_EXTRA_INDEX);
2409 for (; CONSP (extra); extra = XCDR (extra))
2411 Lisp_Object key = XCAR (XCAR (extra));
2412 Lisp_Object val = XCDR (XCAR (extra)), val2;
2414 if (EQ (key, QClang))
2416 val2 = assq_no_quit (key, font_extra);
2417 if (NILP (val2))
2418 return 0;
2419 val2 = XCDR (val2);
2420 if (CONSP (val))
2422 if (! CONSP (val2))
2423 return 0;
2424 while (CONSP (val))
2425 if (NILP (Fmemq (val, val2)))
2426 return 0;
2428 else
2429 if (CONSP (val2)
2430 ? NILP (Fmemq (val, XCDR (val2)))
2431 : ! EQ (val, val2))
2432 return 0;
2434 else if (EQ (key, QCscript))
2436 val2 = assq_no_quit (val, Vscript_representative_chars);
2437 if (CONSP (val2))
2439 val2 = XCDR (val2);
2440 if (CONSP (val2))
2442 /* All characters in the list must be supported. */
2443 for (; CONSP (val2); val2 = XCDR (val2))
2445 if (! CHARACTERP (XCAR (val2)))
2446 continue;
2447 if (font_encode_char (font, XFASTINT (XCAR (val2)))
2448 == FONT_INVALID_CODE)
2449 return 0;
2452 else if (VECTORP (val2))
2454 /* At most one character in the vector must be supported. */
2455 for (i = 0; i < ASIZE (val2); i++)
2457 if (! CHARACTERP (AREF (val2, i)))
2458 continue;
2459 if (font_encode_char (font, XFASTINT (AREF (val2, i)))
2460 != FONT_INVALID_CODE)
2461 break;
2463 if (i == ASIZE (val2))
2464 return 0;
2468 else if (EQ (key, QCotf))
2470 struct font *fontp;
2472 if (! FONT_OBJECT_P (font))
2473 return 0;
2474 fontp = XFONT_OBJECT (font);
2475 if (! fontp->driver->otf_capability)
2476 return 0;
2477 val2 = fontp->driver->otf_capability (fontp);
2478 if (NILP (val2) || ! font_check_otf (val, val2))
2479 return 0;
2483 return 1;
2487 /* Font cache
2489 Each font backend has the callback function get_cache, and it
2490 returns a cons cell of which cdr part can be freely used for
2491 caching fonts. The cons cell may be shared by multiple frames
2492 and/or multiple font drivers. So, we arrange the cdr part as this:
2494 ((DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) ...)
2496 where DRIVER-TYPE is a symbol such as `x', `xft', etc., NUM-FRAMES
2497 is a number frames sharing this cache, and FONT-CACHE-DATA is a
2498 cons (FONT-SPEC FONT-ENTITY ...). */
2500 static void font_prepare_cache (struct frame *, struct font_driver *);
2501 static void font_finish_cache (struct frame *, struct font_driver *);
2502 static Lisp_Object font_get_cache (struct frame *, struct font_driver *);
2503 static void font_clear_cache (struct frame *, Lisp_Object,
2504 struct font_driver *);
2506 static void
2507 font_prepare_cache (struct frame *f, struct font_driver *driver)
2509 Lisp_Object cache, val;
2511 cache = driver->get_cache (f);
2512 val = XCDR (cache);
2513 while (CONSP (val) && ! EQ (XCAR (XCAR (val)), driver->type))
2514 val = XCDR (val);
2515 if (NILP (val))
2517 val = list2 (driver->type, make_number (1));
2518 XSETCDR (cache, Fcons (val, XCDR (cache)));
2520 else
2522 val = XCDR (XCAR (val));
2523 XSETCAR (val, make_number (XINT (XCAR (val)) + 1));
2528 static void
2529 font_finish_cache (struct frame *f, struct font_driver *driver)
2531 Lisp_Object cache, val, tmp;
2534 cache = driver->get_cache (f);
2535 val = XCDR (cache);
2536 while (CONSP (val) && ! EQ (XCAR (XCAR (val)), driver->type))
2537 cache = val, val = XCDR (val);
2538 eassert (! NILP (val));
2539 tmp = XCDR (XCAR (val));
2540 XSETCAR (tmp, make_number (XINT (XCAR (tmp)) - 1));
2541 if (XINT (XCAR (tmp)) == 0)
2543 font_clear_cache (f, XCAR (val), driver);
2544 XSETCDR (cache, XCDR (val));
2549 static Lisp_Object
2550 font_get_cache (struct frame *f, struct font_driver *driver)
2552 Lisp_Object val = driver->get_cache (f);
2553 Lisp_Object type = driver->type;
2555 eassert (CONSP (val));
2556 for (val = XCDR (val); ! EQ (XCAR (XCAR (val)), type); val = XCDR (val));
2557 eassert (CONSP (val));
2558 /* VAL = ((DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) ...) */
2559 val = XCDR (XCAR (val));
2560 return val;
2564 static void
2565 font_clear_cache (struct frame *f, Lisp_Object cache, struct font_driver *driver)
2567 Lisp_Object tail, elt;
2568 Lisp_Object tail2, entity;
2570 /* CACHE = (DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) */
2571 for (tail = XCDR (XCDR (cache)); CONSP (tail); tail = XCDR (tail))
2573 elt = XCAR (tail);
2574 /* elt should have the form (FONT-SPEC FONT-ENTITY ...) */
2575 if (CONSP (elt) && FONT_SPEC_P (XCAR (elt)))
2577 for (tail2 = XCDR (elt); CONSP (tail2); tail2 = XCDR (tail2))
2579 entity = XCAR (tail2);
2581 if (FONT_ENTITY_P (entity)
2582 && EQ (driver->type, AREF (entity, FONT_TYPE_INDEX)))
2584 Lisp_Object objlist = AREF (entity, FONT_OBJLIST_INDEX);
2586 for (; CONSP (objlist); objlist = XCDR (objlist))
2588 Lisp_Object val = XCAR (objlist);
2589 struct font *font = XFONT_OBJECT (val);
2591 if (! NILP (AREF (val, FONT_TYPE_INDEX)))
2593 eassert (font && driver == font->driver);
2594 driver->close (font);
2597 if (driver->free_entity)
2598 driver->free_entity (entity);
2603 XSETCDR (cache, Qnil);
2607 static Lisp_Object scratch_font_spec, scratch_font_prefer;
2609 /* Check each font-entity in VEC, and return a list of font-entities
2610 that satisfy these conditions:
2611 (1) matches with SPEC and SIZE if SPEC is not nil, and
2612 (2) doesn't match with any regexps in Vface_ignored_fonts (if non-nil).
2615 static Lisp_Object
2616 font_delete_unmatched (Lisp_Object vec, Lisp_Object spec, int size)
2618 Lisp_Object entity, val;
2619 enum font_property_index prop;
2620 int i;
2622 for (val = Qnil, i = ASIZE (vec) - 1; i >= 0; i--)
2624 entity = AREF (vec, i);
2625 if (! NILP (Vface_ignored_fonts))
2627 char name[256];
2628 ptrdiff_t namelen;
2629 Lisp_Object tail, regexp;
2631 namelen = font_unparse_xlfd (entity, 0, name, 256);
2632 if (namelen >= 0)
2634 for (tail = Vface_ignored_fonts; CONSP (tail); tail = XCDR (tail))
2636 regexp = XCAR (tail);
2637 if (STRINGP (regexp)
2638 && fast_c_string_match_ignore_case (regexp, name,
2639 namelen) >= 0)
2640 break;
2642 if (CONSP (tail))
2643 continue;
2646 if (NILP (spec))
2648 val = Fcons (entity, val);
2649 continue;
2651 for (prop = FONT_WEIGHT_INDEX; prop < FONT_SIZE_INDEX; prop++)
2652 if (INTEGERP (AREF (spec, prop))
2653 && ((XINT (AREF (spec, prop)) >> 8)
2654 != (XINT (AREF (entity, prop)) >> 8)))
2655 prop = FONT_SPEC_MAX;
2656 if (prop < FONT_SPEC_MAX
2657 && size
2658 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
2660 int diff = XINT (AREF (entity, FONT_SIZE_INDEX)) - size;
2662 if (eabs (diff) > FONT_PIXEL_SIZE_QUANTUM)
2663 prop = FONT_SPEC_MAX;
2665 if (prop < FONT_SPEC_MAX
2666 && INTEGERP (AREF (spec, FONT_DPI_INDEX))
2667 && INTEGERP (AREF (entity, FONT_DPI_INDEX))
2668 && XINT (AREF (entity, FONT_DPI_INDEX)) != 0
2669 && ! EQ (AREF (spec, FONT_DPI_INDEX), AREF (entity, FONT_DPI_INDEX)))
2670 prop = FONT_SPEC_MAX;
2671 if (prop < FONT_SPEC_MAX
2672 && INTEGERP (AREF (spec, FONT_AVGWIDTH_INDEX))
2673 && INTEGERP (AREF (entity, FONT_AVGWIDTH_INDEX))
2674 && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) != 0
2675 && ! EQ (AREF (spec, FONT_AVGWIDTH_INDEX),
2676 AREF (entity, FONT_AVGWIDTH_INDEX)))
2677 prop = FONT_SPEC_MAX;
2678 if (prop < FONT_SPEC_MAX)
2679 val = Fcons (entity, val);
2681 return (Fvconcat (1, &val));
2685 /* Return a list of vectors of font-entities matching with SPEC on
2686 FRAME. Each elements in the list is a vector of entities from the
2687 same font-driver. */
2689 Lisp_Object
2690 font_list_entities (struct frame *f, Lisp_Object spec)
2692 struct font_driver_list *driver_list = f->font_driver_list;
2693 Lisp_Object ftype, val;
2694 Lisp_Object list = Qnil;
2695 int size;
2696 bool need_filtering = 0;
2697 int i;
2699 eassert (FONT_SPEC_P (spec));
2701 if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
2702 size = XINT (AREF (spec, FONT_SIZE_INDEX));
2703 else if (FLOATP (AREF (spec, FONT_SIZE_INDEX)))
2704 size = font_pixel_size (f, spec);
2705 else
2706 size = 0;
2708 ftype = AREF (spec, FONT_TYPE_INDEX);
2709 for (i = FONT_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX; i++)
2710 ASET (scratch_font_spec, i, AREF (spec, i));
2711 for (i = FONT_WEIGHT_INDEX; i < FONT_EXTRA_INDEX; i++)
2712 if (i != FONT_SPACING_INDEX)
2714 ASET (scratch_font_spec, i, Qnil);
2715 if (! NILP (AREF (spec, i)))
2716 need_filtering = 1;
2718 ASET (scratch_font_spec, FONT_SPACING_INDEX, AREF (spec, FONT_SPACING_INDEX));
2719 ASET (scratch_font_spec, FONT_EXTRA_INDEX, AREF (spec, FONT_EXTRA_INDEX));
2721 for (i = 0; driver_list; driver_list = driver_list->next)
2722 if (driver_list->on
2723 && (NILP (ftype) || EQ (driver_list->driver->type, ftype)))
2725 Lisp_Object cache = font_get_cache (f, driver_list->driver);
2727 ASET (scratch_font_spec, FONT_TYPE_INDEX, driver_list->driver->type);
2728 val = assoc_no_quit (scratch_font_spec, XCDR (cache));
2729 if (CONSP (val))
2730 val = XCDR (val);
2731 else
2733 Lisp_Object copy;
2735 val = driver_list->driver->list (f, scratch_font_spec);
2736 if (NILP (val))
2737 val = zero_vector;
2738 else
2739 val = Fvconcat (1, &val);
2740 copy = copy_font_spec (scratch_font_spec);
2741 ASET (copy, FONT_TYPE_INDEX, driver_list->driver->type);
2742 XSETCDR (cache, Fcons (Fcons (copy, val), XCDR (cache)));
2744 if (ASIZE (val) > 0
2745 && (need_filtering
2746 || ! NILP (Vface_ignored_fonts)))
2747 val = font_delete_unmatched (val, need_filtering ? spec : Qnil, size);
2748 if (ASIZE (val) > 0)
2749 list = Fcons (val, list);
2752 list = Fnreverse (list);
2753 FONT_ADD_LOG ("list", spec, list);
2754 return list;
2758 /* Return a font entity matching with SPEC on FRAME. ATTRS, if non
2759 nil, is an array of face's attributes, which specifies preferred
2760 font-related attributes. */
2762 static Lisp_Object
2763 font_matching_entity (struct frame *f, Lisp_Object *attrs, Lisp_Object spec)
2765 struct font_driver_list *driver_list = f->font_driver_list;
2766 Lisp_Object ftype, size, entity;
2767 Lisp_Object work = copy_font_spec (spec);
2769 ftype = AREF (spec, FONT_TYPE_INDEX);
2770 size = AREF (spec, FONT_SIZE_INDEX);
2772 if (FLOATP (size))
2773 ASET (work, FONT_SIZE_INDEX, make_number (font_pixel_size (f, spec)));
2774 FONT_SET_STYLE (work, FONT_WEIGHT_INDEX, attrs[LFACE_WEIGHT_INDEX]);
2775 FONT_SET_STYLE (work, FONT_SLANT_INDEX, attrs[LFACE_SLANT_INDEX]);
2776 FONT_SET_STYLE (work, FONT_WIDTH_INDEX, attrs[LFACE_SWIDTH_INDEX]);
2778 entity = Qnil;
2779 for (; driver_list; driver_list = driver_list->next)
2780 if (driver_list->on
2781 && (NILP (ftype) || EQ (driver_list->driver->type, ftype)))
2783 Lisp_Object cache = font_get_cache (f, driver_list->driver);
2784 Lisp_Object copy;
2786 ASET (work, FONT_TYPE_INDEX, driver_list->driver->type);
2787 entity = assoc_no_quit (work, XCDR (cache));
2788 if (CONSP (entity))
2789 entity = XCDR (entity);
2790 else
2792 entity = driver_list->driver->match (f, work);
2793 copy = copy_font_spec (work);
2794 ASET (copy, FONT_TYPE_INDEX, driver_list->driver->type);
2795 XSETCDR (cache, Fcons (Fcons (copy, entity), XCDR (cache)));
2797 if (! NILP (entity))
2798 break;
2800 FONT_ADD_LOG ("match", work, entity);
2801 return entity;
2805 /* Open a font of ENTITY and PIXEL_SIZE on frame F, and return the
2806 opened font object. */
2808 static Lisp_Object
2809 font_open_entity (struct frame *f, Lisp_Object entity, int pixel_size)
2811 struct font_driver_list *driver_list;
2812 Lisp_Object objlist, size, val, font_object;
2813 struct font *font;
2814 int min_width, height, psize;
2816 eassert (FONT_ENTITY_P (entity));
2817 size = AREF (entity, FONT_SIZE_INDEX);
2818 if (XINT (size) != 0)
2819 pixel_size = XINT (size);
2821 val = AREF (entity, FONT_TYPE_INDEX);
2822 for (driver_list = f->font_driver_list;
2823 driver_list && ! EQ (driver_list->driver->type, val);
2824 driver_list = driver_list->next);
2825 if (! driver_list)
2826 return Qnil;
2828 for (objlist = AREF (entity, FONT_OBJLIST_INDEX); CONSP (objlist);
2829 objlist = XCDR (objlist))
2831 Lisp_Object fn = XCAR (objlist);
2832 if (! NILP (AREF (fn, FONT_TYPE_INDEX))
2833 && XFONT_OBJECT (fn)->pixel_size == pixel_size)
2835 if (driver_list->driver->cached_font_ok == NULL
2836 || driver_list->driver->cached_font_ok (f, fn, entity))
2837 return fn;
2841 /* We always open a font of manageable size; i.e non-zero average
2842 width and height. */
2843 for (psize = pixel_size; ; psize++)
2845 font_object = driver_list->driver->open (f, entity, psize);
2846 if (NILP (font_object))
2847 return Qnil;
2848 font = XFONT_OBJECT (font_object);
2849 if (font->average_width > 0 && font->height > 0)
2850 break;
2852 ASET (font_object, FONT_SIZE_INDEX, make_number (pixel_size));
2853 FONT_ADD_LOG ("open", entity, font_object);
2854 ASET (entity, FONT_OBJLIST_INDEX,
2855 Fcons (font_object, AREF (entity, FONT_OBJLIST_INDEX)));
2857 font = XFONT_OBJECT (font_object);
2858 min_width = (font->min_width ? font->min_width
2859 : font->average_width ? font->average_width
2860 : font->space_width ? font->space_width
2861 : 1);
2862 height = (font->height ? font->height : 1);
2863 #ifdef HAVE_WINDOW_SYSTEM
2864 FRAME_DISPLAY_INFO (f)->n_fonts++;
2865 if (FRAME_DISPLAY_INFO (f)->n_fonts == 1)
2867 FRAME_SMALLEST_CHAR_WIDTH (f) = min_width;
2868 FRAME_SMALLEST_FONT_HEIGHT (f) = height;
2869 f->fonts_changed = 1;
2871 else
2873 if (FRAME_SMALLEST_CHAR_WIDTH (f) > min_width)
2874 FRAME_SMALLEST_CHAR_WIDTH (f) = min_width, f->fonts_changed = 1;
2875 if (FRAME_SMALLEST_FONT_HEIGHT (f) > height)
2876 FRAME_SMALLEST_FONT_HEIGHT (f) = height, f->fonts_changed = 1;
2878 #endif
2880 return font_object;
2884 /* Close FONT_OBJECT that is opened on frame F. */
2886 static void
2887 font_close_object (struct frame *f, Lisp_Object font_object)
2889 struct font *font = XFONT_OBJECT (font_object);
2891 if (NILP (AREF (font_object, FONT_TYPE_INDEX)))
2892 /* Already closed. */
2893 return;
2894 FONT_ADD_LOG ("close", font_object, Qnil);
2895 font->driver->close (font);
2896 #ifdef HAVE_WINDOW_SYSTEM
2897 eassert (FRAME_DISPLAY_INFO (f)->n_fonts);
2898 FRAME_DISPLAY_INFO (f)->n_fonts--;
2899 #endif
2903 /* Return 1 if FONT on F has a glyph for character C, 0 if not, -1 if
2904 FONT is a font-entity and it must be opened to check. */
2907 font_has_char (struct frame *f, Lisp_Object font, int c)
2909 struct font *fontp;
2911 if (FONT_ENTITY_P (font))
2913 Lisp_Object type = AREF (font, FONT_TYPE_INDEX);
2914 struct font_driver_list *driver_list;
2916 for (driver_list = f->font_driver_list;
2917 driver_list && ! EQ (driver_list->driver->type, type);
2918 driver_list = driver_list->next);
2919 if (! driver_list)
2920 return 0;
2921 if (! driver_list->driver->has_char)
2922 return -1;
2923 return driver_list->driver->has_char (font, c);
2926 eassert (FONT_OBJECT_P (font));
2927 fontp = XFONT_OBJECT (font);
2928 if (fontp->driver->has_char)
2930 int result = fontp->driver->has_char (font, c);
2932 if (result >= 0)
2933 return result;
2935 return (fontp->driver->encode_char (fontp, c) != FONT_INVALID_CODE);
2939 /* Return the glyph ID of FONT_OBJECT for character C. */
2941 static unsigned
2942 font_encode_char (Lisp_Object font_object, int c)
2944 struct font *font;
2946 eassert (FONT_OBJECT_P (font_object));
2947 font = XFONT_OBJECT (font_object);
2948 return font->driver->encode_char (font, c);
2952 /* Return the name of FONT_OBJECT. */
2954 Lisp_Object
2955 font_get_name (Lisp_Object font_object)
2957 eassert (FONT_OBJECT_P (font_object));
2958 return AREF (font_object, FONT_NAME_INDEX);
2962 /* Create a new font spec from FONT_NAME, and return it. If FONT_NAME
2963 could not be parsed by font_parse_name, return Qnil. */
2965 Lisp_Object
2966 font_spec_from_name (Lisp_Object font_name)
2968 Lisp_Object spec = Ffont_spec (0, NULL);
2970 CHECK_STRING (font_name);
2971 if (font_parse_name (SSDATA (font_name), SBYTES (font_name), spec) == -1)
2972 return Qnil;
2973 font_put_extra (spec, QCname, font_name);
2974 font_put_extra (spec, QCuser_spec, font_name);
2975 return spec;
2979 void
2980 font_clear_prop (Lisp_Object *attrs, enum font_property_index prop)
2982 Lisp_Object font = attrs[LFACE_FONT_INDEX];
2984 if (! FONTP (font))
2985 return;
2987 if (! NILP (Ffont_get (font, QCname)))
2989 font = copy_font_spec (font);
2990 font_put_extra (font, QCname, Qnil);
2993 if (NILP (AREF (font, prop))
2994 && prop != FONT_FAMILY_INDEX
2995 && prop != FONT_FOUNDRY_INDEX
2996 && prop != FONT_WIDTH_INDEX
2997 && prop != FONT_SIZE_INDEX)
2998 return;
2999 if (EQ (font, attrs[LFACE_FONT_INDEX]))
3000 font = copy_font_spec (font);
3001 ASET (font, prop, Qnil);
3002 if (prop == FONT_FAMILY_INDEX || prop == FONT_FOUNDRY_INDEX)
3004 if (prop == FONT_FAMILY_INDEX)
3006 ASET (font, FONT_FOUNDRY_INDEX, Qnil);
3007 /* If we are setting the font family, we must also clear
3008 FONT_WIDTH_INDEX to avoid rejecting families that lack
3009 support for some widths. */
3010 ASET (font, FONT_WIDTH_INDEX, Qnil);
3012 ASET (font, FONT_ADSTYLE_INDEX, Qnil);
3013 ASET (font, FONT_REGISTRY_INDEX, Qnil);
3014 ASET (font, FONT_SIZE_INDEX, Qnil);
3015 ASET (font, FONT_DPI_INDEX, Qnil);
3016 ASET (font, FONT_SPACING_INDEX, Qnil);
3017 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3019 else if (prop == FONT_SIZE_INDEX)
3021 ASET (font, FONT_DPI_INDEX, Qnil);
3022 ASET (font, FONT_SPACING_INDEX, Qnil);
3023 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3025 else if (prop == FONT_WIDTH_INDEX)
3026 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3027 attrs[LFACE_FONT_INDEX] = font;
3030 /* Select a font from ENTITIES (list of font-entity vectors) that
3031 supports C and is the best match for ATTRS and PIXEL_SIZE. */
3033 static Lisp_Object
3034 font_select_entity (struct frame *f, Lisp_Object entities,
3035 Lisp_Object *attrs, int pixel_size, int c)
3037 Lisp_Object font_entity;
3038 Lisp_Object prefer;
3039 int i;
3041 if (NILP (XCDR (entities))
3042 && ASIZE (XCAR (entities)) == 1)
3044 font_entity = AREF (XCAR (entities), 0);
3045 if (c < 0 || font_has_char (f, font_entity, c) > 0)
3046 return font_entity;
3047 return Qnil;
3050 /* Sort fonts by properties specified in ATTRS. */
3051 prefer = scratch_font_prefer;
3053 for (i = FONT_WEIGHT_INDEX; i <= FONT_SIZE_INDEX; i++)
3054 ASET (prefer, i, Qnil);
3055 if (FONTP (attrs[LFACE_FONT_INDEX]))
3057 Lisp_Object face_font = attrs[LFACE_FONT_INDEX];
3059 for (i = FONT_WEIGHT_INDEX; i <= FONT_SIZE_INDEX; i++)
3060 ASET (prefer, i, AREF (face_font, i));
3062 if (NILP (AREF (prefer, FONT_WEIGHT_INDEX)))
3063 FONT_SET_STYLE (prefer, FONT_WEIGHT_INDEX, attrs[LFACE_WEIGHT_INDEX]);
3064 if (NILP (AREF (prefer, FONT_SLANT_INDEX)))
3065 FONT_SET_STYLE (prefer, FONT_SLANT_INDEX, attrs[LFACE_SLANT_INDEX]);
3066 if (NILP (AREF (prefer, FONT_WIDTH_INDEX)))
3067 FONT_SET_STYLE (prefer, FONT_WIDTH_INDEX, attrs[LFACE_SWIDTH_INDEX]);
3068 ASET (prefer, FONT_SIZE_INDEX, make_number (pixel_size));
3070 return font_sort_entities (entities, prefer, f, c);
3073 /* Return a font-entity that satisfies SPEC and is the best match for
3074 face's font related attributes in ATTRS. C, if not negative, is a
3075 character that the entity must support. */
3077 Lisp_Object
3078 font_find_for_lface (struct frame *f, Lisp_Object *attrs, Lisp_Object spec, int c)
3080 Lisp_Object work;
3081 Lisp_Object entities, val;
3082 Lisp_Object foundry[3], *family, registry[3], adstyle[3];
3083 int pixel_size;
3084 int i, j, k, l;
3085 USE_SAFE_ALLOCA;
3087 registry[0] = AREF (spec, FONT_REGISTRY_INDEX);
3088 if (NILP (registry[0]))
3090 registry[0] = DEFAULT_ENCODING;
3091 registry[1] = Qascii_0;
3092 registry[2] = zero_vector;
3094 else
3095 registry[1] = zero_vector;
3097 if (c >= 0 && ! NILP (AREF (spec, FONT_REGISTRY_INDEX)))
3099 struct charset *encoding, *repertory;
3101 if (font_registry_charsets (AREF (spec, FONT_REGISTRY_INDEX),
3102 &encoding, &repertory) < 0)
3103 return Qnil;
3104 if (repertory
3105 && ENCODE_CHAR (repertory, c) == CHARSET_INVALID_CODE (repertory))
3106 return Qnil;
3107 else if (c > encoding->max_char)
3108 return Qnil;
3111 work = copy_font_spec (spec);
3112 ASET (work, FONT_TYPE_INDEX, AREF (spec, FONT_TYPE_INDEX));
3113 pixel_size = font_pixel_size (f, spec);
3114 if (pixel_size == 0 && INTEGERP (attrs[LFACE_HEIGHT_INDEX]))
3116 double pt = XINT (attrs[LFACE_HEIGHT_INDEX]);
3118 pixel_size = POINT_TO_PIXEL (pt / 10, FRAME_RES_Y (f));
3119 if (pixel_size < 1)
3120 pixel_size = 1;
3122 ASET (work, FONT_SIZE_INDEX, Qnil);
3123 foundry[0] = AREF (work, FONT_FOUNDRY_INDEX);
3124 if (! NILP (foundry[0]))
3125 foundry[1] = zero_vector;
3126 else if (STRINGP (attrs[LFACE_FOUNDRY_INDEX]))
3128 val = attrs[LFACE_FOUNDRY_INDEX];
3129 foundry[0] = font_intern_prop (SSDATA (val), SBYTES (val), 1);
3130 foundry[1] = Qnil;
3131 foundry[2] = zero_vector;
3133 else
3134 foundry[0] = Qnil, foundry[1] = zero_vector;
3136 adstyle[0] = AREF (work, FONT_ADSTYLE_INDEX);
3137 if (! NILP (adstyle[0]))
3138 adstyle[1] = zero_vector;
3139 else if (FONTP (attrs[LFACE_FONT_INDEX]))
3141 Lisp_Object face_font = attrs[LFACE_FONT_INDEX];
3143 if (! NILP (AREF (face_font, FONT_ADSTYLE_INDEX)))
3145 adstyle[0] = AREF (face_font, FONT_ADSTYLE_INDEX);
3146 adstyle[1] = Qnil;
3147 adstyle[2] = zero_vector;
3149 else
3150 adstyle[0] = Qnil, adstyle[1] = zero_vector;
3152 else
3153 adstyle[0] = Qnil, adstyle[1] = zero_vector;
3156 val = AREF (work, FONT_FAMILY_INDEX);
3157 if (NILP (val) && STRINGP (attrs[LFACE_FAMILY_INDEX]))
3159 val = attrs[LFACE_FAMILY_INDEX];
3160 val = font_intern_prop (SSDATA (val), SBYTES (val), 1);
3162 if (NILP (val))
3164 family = alloca ((sizeof family[0]) * 2);
3165 family[0] = Qnil;
3166 family[1] = zero_vector; /* terminator. */
3168 else
3170 Lisp_Object alters
3171 = Fassoc_string (val, Vface_alternative_font_family_alist, Qt);
3173 if (! NILP (alters))
3175 EMACS_INT alterslen = XFASTINT (Flength (alters));
3176 SAFE_ALLOCA_LISP (family, alterslen + 2);
3177 for (i = 0; CONSP (alters); i++, alters = XCDR (alters))
3178 family[i] = XCAR (alters);
3179 if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
3180 family[i++] = Qnil;
3181 family[i] = zero_vector;
3183 else
3185 family = alloca ((sizeof family[0]) * 3);
3186 i = 0;
3187 family[i++] = val;
3188 if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
3189 family[i++] = Qnil;
3190 family[i] = zero_vector;
3194 for (i = 0; SYMBOLP (family[i]); i++)
3196 ASET (work, FONT_FAMILY_INDEX, family[i]);
3197 for (j = 0; SYMBOLP (foundry[j]); j++)
3199 ASET (work, FONT_FOUNDRY_INDEX, foundry[j]);
3200 for (k = 0; SYMBOLP (registry[k]); k++)
3202 ASET (work, FONT_REGISTRY_INDEX, registry[k]);
3203 for (l = 0; SYMBOLP (adstyle[l]); l++)
3205 ASET (work, FONT_ADSTYLE_INDEX, adstyle[l]);
3206 entities = font_list_entities (f, work);
3207 if (! NILP (entities))
3209 val = font_select_entity (f, entities,
3210 attrs, pixel_size, c);
3211 if (! NILP (val))
3212 return val;
3219 SAFE_FREE ();
3220 return Qnil;
3224 Lisp_Object
3225 font_open_for_lface (struct frame *f, Lisp_Object entity, Lisp_Object *attrs, Lisp_Object spec)
3227 int size;
3229 if (INTEGERP (AREF (entity, FONT_SIZE_INDEX))
3230 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
3231 size = XINT (AREF (entity, FONT_SIZE_INDEX));
3232 else
3234 if (FONT_SPEC_P (spec) && ! NILP (AREF (spec, FONT_SIZE_INDEX)))
3235 size = font_pixel_size (f, spec);
3236 else
3238 double pt;
3239 if (INTEGERP (attrs[LFACE_HEIGHT_INDEX]))
3240 pt = XINT (attrs[LFACE_HEIGHT_INDEX]);
3241 else
3243 struct face *def = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3244 Lisp_Object height = def->lface[LFACE_HEIGHT_INDEX];
3245 eassert (INTEGERP (height));
3246 pt = XINT (height);
3249 pt /= 10;
3250 size = POINT_TO_PIXEL (pt, FRAME_RES_Y (f));
3251 #ifdef HAVE_NS
3252 if (size == 0)
3254 Lisp_Object ffsize = get_frame_param (f, Qfontsize);
3255 size = (NUMBERP (ffsize)
3256 ? POINT_TO_PIXEL (XINT (ffsize), FRAME_RES_Y (f)) : 0);
3258 #endif
3260 size *= font_rescale_ratio (entity);
3263 return font_open_entity (f, entity, size);
3267 /* Find a font that satisfies SPEC and is the best match for
3268 face's attributes in ATTRS on FRAME, and return the opened
3269 font-object. */
3271 Lisp_Object
3272 font_load_for_lface (struct frame *f, Lisp_Object *attrs, Lisp_Object spec)
3274 Lisp_Object entity, name;
3276 entity = font_find_for_lface (f, attrs, spec, -1);
3277 if (NILP (entity))
3279 /* No font is listed for SPEC, but each font-backend may have
3280 different criteria about "font matching". So, try it. */
3281 entity = font_matching_entity (f, attrs, spec);
3282 if (NILP (entity))
3283 return Qnil;
3285 /* Don't lose the original name that was put in initially. We need
3286 it to re-apply the font when font parameters (like hinting or dpi) have
3287 changed. */
3288 entity = font_open_for_lface (f, entity, attrs, spec);
3289 if (!NILP (entity))
3291 name = Ffont_get (spec, QCuser_spec);
3292 if (STRINGP (name)) font_put_extra (entity, QCuser_spec, name);
3294 return entity;
3298 /* Make FACE on frame F ready to use the font opened for FACE. */
3300 void
3301 font_prepare_for_face (struct frame *f, struct face *face)
3303 if (face->font->driver->prepare_face)
3304 face->font->driver->prepare_face (f, face);
3308 /* Make FACE on frame F stop using the font opened for FACE. */
3310 void
3311 font_done_for_face (struct frame *f, struct face *face)
3313 if (face->font->driver->done_face)
3314 face->font->driver->done_face (f, face);
3315 face->extra = NULL;
3319 /* Open a font that is a match for font-spec SPEC on frame F. If no proper
3320 font is found, return Qnil. */
3322 Lisp_Object
3323 font_open_by_spec (struct frame *f, Lisp_Object spec)
3325 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3327 /* We set up the default font-related attributes of a face to prefer
3328 a moderate font. */
3329 attrs[LFACE_FAMILY_INDEX] = attrs[LFACE_FOUNDRY_INDEX] = Qnil;
3330 attrs[LFACE_SWIDTH_INDEX] = attrs[LFACE_WEIGHT_INDEX]
3331 = attrs[LFACE_SLANT_INDEX] = Qnormal;
3332 #ifndef HAVE_NS
3333 attrs[LFACE_HEIGHT_INDEX] = make_number (120);
3334 #else
3335 attrs[LFACE_HEIGHT_INDEX] = make_number (0);
3336 #endif
3337 attrs[LFACE_FONT_INDEX] = Qnil;
3339 return font_load_for_lface (f, attrs, spec);
3343 /* Open a font that matches NAME on frame F. If no proper font is
3344 found, return Qnil. */
3346 Lisp_Object
3347 font_open_by_name (struct frame *f, Lisp_Object name)
3349 Lisp_Object args[2];
3350 Lisp_Object spec, ret;
3352 args[0] = QCname;
3353 args[1] = name;
3354 spec = Ffont_spec (2, args);
3355 ret = font_open_by_spec (f, spec);
3356 /* Do not lose name originally put in. */
3357 if (!NILP (ret))
3358 font_put_extra (ret, QCuser_spec, args[1]);
3360 return ret;
3364 /* Register font-driver DRIVER. This function is used in two ways.
3366 The first is with frame F non-NULL. In this case, make DRIVER
3367 available (but not yet activated) on F. All frame creators
3368 (e.g. Fx_create_frame) must call this function at least once with
3369 an available font-driver.
3371 The second is with frame F NULL. In this case, DRIVER is globally
3372 registered in the variable `font_driver_list'. All font-driver
3373 implementations must call this function in its syms_of_XXXX
3374 (e.g. syms_of_xfont). */
3376 void
3377 register_font_driver (struct font_driver *driver, struct frame *f)
3379 struct font_driver_list *root = f ? f->font_driver_list : font_driver_list;
3380 struct font_driver_list *prev, *list;
3382 #ifdef HAVE_WINDOW_SYSTEM
3383 if (f && ! driver->draw)
3384 error ("Unusable font driver for a frame: %s",
3385 SDATA (SYMBOL_NAME (driver->type)));
3386 #endif /* HAVE_WINDOW_SYSTEM */
3388 for (prev = NULL, list = root; list; prev = list, list = list->next)
3389 if (EQ (list->driver->type, driver->type))
3390 error ("Duplicated font driver: %s", SDATA (SYMBOL_NAME (driver->type)));
3392 list = xmalloc (sizeof *list);
3393 list->on = 0;
3394 list->driver = driver;
3395 list->next = NULL;
3396 if (prev)
3397 prev->next = list;
3398 else if (f)
3399 f->font_driver_list = list;
3400 else
3401 font_driver_list = list;
3402 if (! f)
3403 num_font_drivers++;
3406 void
3407 free_font_driver_list (struct frame *f)
3409 struct font_driver_list *list, *next;
3411 for (list = f->font_driver_list; list; list = next)
3413 next = list->next;
3414 xfree (list);
3416 f->font_driver_list = NULL;
3420 /* Make the frame F use font backends listed in NEW_DRIVERS (list of
3421 symbols, e.g. xft, x). If NEW_DRIVERS is t, make F use all
3422 available font drivers. If NEW_DRIVERS is nil, finalize all drivers.
3424 A caller must free all realized faces if any in advance. The
3425 return value is a list of font backends actually made used on
3426 F. */
3428 Lisp_Object
3429 font_update_drivers (struct frame *f, Lisp_Object new_drivers)
3431 Lisp_Object active_drivers = Qnil;
3432 struct font_driver_list *list;
3434 /* At first, turn off non-requested drivers, and turn on requested
3435 drivers. */
3436 for (list = f->font_driver_list; list; list = list->next)
3438 struct font_driver *driver = list->driver;
3439 if ((EQ (new_drivers, Qt) || ! NILP (Fmemq (driver->type, new_drivers)))
3440 != list->on)
3442 if (list->on)
3444 if (driver->end_for_frame)
3445 driver->end_for_frame (f);
3446 font_finish_cache (f, driver);
3447 list->on = 0;
3449 else
3451 if (! driver->start_for_frame
3452 || driver->start_for_frame (f) == 0)
3454 font_prepare_cache (f, driver);
3455 list->on = 1;
3461 if (NILP (new_drivers))
3462 return Qnil;
3464 if (! EQ (new_drivers, Qt))
3466 /* Re-order the driver list according to new_drivers. */
3467 struct font_driver_list **list_table, **next;
3468 Lisp_Object tail;
3469 int i;
3471 list_table = alloca (sizeof list_table[0] * (num_font_drivers + 1));
3472 for (i = 0, tail = new_drivers; ! NILP (tail); tail = XCDR (tail))
3474 for (list = f->font_driver_list; list; list = list->next)
3475 if (list->on && EQ (list->driver->type, XCAR (tail)))
3476 break;
3477 if (list)
3478 list_table[i++] = list;
3480 for (list = f->font_driver_list; list; list = list->next)
3481 if (! list->on)
3482 list_table[i++] = list;
3483 list_table[i] = NULL;
3485 next = &f->font_driver_list;
3486 for (i = 0; list_table[i]; i++)
3488 *next = list_table[i];
3489 next = &(*next)->next;
3491 *next = NULL;
3493 if (! f->font_driver_list->on)
3494 { /* None of the drivers is enabled: enable them all.
3495 Happens if you set the list of drivers to (xft x) in your .emacs
3496 and then use it under w32 or ns. */
3497 for (list = f->font_driver_list; list; list = list->next)
3499 struct font_driver *driver = list->driver;
3500 eassert (! list->on);
3501 if (! driver->start_for_frame
3502 || driver->start_for_frame (f) == 0)
3504 font_prepare_cache (f, driver);
3505 list->on = 1;
3511 for (list = f->font_driver_list; list; list = list->next)
3512 if (list->on)
3513 active_drivers = nconc2 (active_drivers, list1 (list->driver->type));
3514 return active_drivers;
3518 font_put_frame_data (struct frame *f, struct font_driver *driver, void *data)
3520 struct font_data_list *list, *prev;
3522 for (prev = NULL, list = f->font_data_list; list;
3523 prev = list, list = list->next)
3524 if (list->driver == driver)
3525 break;
3526 if (! data)
3528 if (list)
3530 if (prev)
3531 prev->next = list->next;
3532 else
3533 f->font_data_list = list->next;
3534 xfree (list);
3536 return 0;
3539 if (! list)
3541 list = xmalloc (sizeof *list);
3542 list->driver = driver;
3543 list->next = f->font_data_list;
3544 f->font_data_list = list;
3546 list->data = data;
3547 return 0;
3551 void *
3552 font_get_frame_data (struct frame *f, struct font_driver *driver)
3554 struct font_data_list *list;
3556 for (list = f->font_data_list; list; list = list->next)
3557 if (list->driver == driver)
3558 break;
3559 if (! list)
3560 return NULL;
3561 return list->data;
3565 /* Sets attributes on a font. Any properties that appear in ALIST and
3566 BOOLEAN_PROPERTIES or NON_BOOLEAN_PROPERTIES are set on the font.
3567 BOOLEAN_PROPERTIES and NON_BOOLEAN_PROPERTIES are NULL-terminated
3568 arrays of strings. This function is intended for use by the font
3569 drivers to implement their specific font_filter_properties. */
3570 void
3571 font_filter_properties (Lisp_Object font,
3572 Lisp_Object alist,
3573 const char *const boolean_properties[],
3574 const char *const non_boolean_properties[])
3576 Lisp_Object it;
3577 int i;
3579 /* Set boolean values to Qt or Qnil. */
3580 for (i = 0; boolean_properties[i] != NULL; ++i)
3581 for (it = alist; ! NILP (it); it = XCDR (it))
3583 Lisp_Object key = XCAR (XCAR (it));
3584 Lisp_Object val = XCDR (XCAR (it));
3585 char *keystr = SSDATA (SYMBOL_NAME (key));
3587 if (strcmp (boolean_properties[i], keystr) == 0)
3589 const char *str = INTEGERP (val) ? (XINT (val) ? "true" : "false")
3590 : SYMBOLP (val) ? SSDATA (SYMBOL_NAME (val))
3591 : "true";
3593 if (strcmp ("false", str) == 0 || strcmp ("False", str) == 0
3594 || strcmp ("FALSE", str) == 0 || strcmp ("FcFalse", str) == 0
3595 || strcmp ("off", str) == 0 || strcmp ("OFF", str) == 0
3596 || strcmp ("Off", str) == 0)
3597 val = Qnil;
3598 else
3599 val = Qt;
3601 Ffont_put (font, key, val);
3605 for (i = 0; non_boolean_properties[i] != NULL; ++i)
3606 for (it = alist; ! NILP (it); it = XCDR (it))
3608 Lisp_Object key = XCAR (XCAR (it));
3609 Lisp_Object val = XCDR (XCAR (it));
3610 char *keystr = SSDATA (SYMBOL_NAME (key));
3611 if (strcmp (non_boolean_properties[i], keystr) == 0)
3612 Ffont_put (font, key, val);
3617 /* Return the font used to draw character C by FACE at buffer position
3618 POS in window W. If STRING is non-nil, it is a string containing C
3619 at index POS. If C is negative, get C from the current buffer or
3620 STRING. */
3622 static Lisp_Object
3623 font_at (int c, ptrdiff_t pos, struct face *face, struct window *w,
3624 Lisp_Object string)
3626 struct frame *f;
3627 bool multibyte;
3628 Lisp_Object font_object;
3630 multibyte = (NILP (string)
3631 ? ! NILP (BVAR (current_buffer, enable_multibyte_characters))
3632 : STRING_MULTIBYTE (string));
3633 if (c < 0)
3635 if (NILP (string))
3637 if (multibyte)
3639 ptrdiff_t pos_byte = CHAR_TO_BYTE (pos);
3641 c = FETCH_CHAR (pos_byte);
3643 else
3644 c = FETCH_BYTE (pos);
3646 else
3648 unsigned char *str;
3650 multibyte = STRING_MULTIBYTE (string);
3651 if (multibyte)
3653 ptrdiff_t pos_byte = string_char_to_byte (string, pos);
3655 str = SDATA (string) + pos_byte;
3656 c = STRING_CHAR (str);
3658 else
3659 c = SDATA (string)[pos];
3663 f = XFRAME (w->frame);
3664 if (! FRAME_WINDOW_P (f))
3665 return Qnil;
3666 if (! face)
3668 int face_id;
3669 ptrdiff_t endptr;
3671 if (STRINGP (string))
3672 face_id = face_at_string_position (w, string, pos, 0, &endptr,
3673 DEFAULT_FACE_ID, 0);
3674 else
3675 face_id = face_at_buffer_position (w, pos, &endptr,
3676 pos + 100, 0, -1);
3677 face = FACE_FROM_ID (f, face_id);
3679 if (multibyte)
3681 int face_id = FACE_FOR_CHAR (f, face, c, pos, string);
3682 face = FACE_FROM_ID (f, face_id);
3684 if (! face->font)
3685 return Qnil;
3687 XSETFONT (font_object, face->font);
3688 return font_object;
3692 #ifdef HAVE_WINDOW_SYSTEM
3694 /* Check how many characters after character/byte position POS/POS_BYTE
3695 (at most to *LIMIT) can be displayed by the same font in the window W.
3696 FACE, if non-NULL, is the face selected for the character at POS.
3697 If STRING is not nil, it is the string to check instead of the current
3698 buffer. In that case, FACE must be not NULL.
3700 The return value is the font-object for the character at POS.
3701 *LIMIT is set to the position where that font can't be used.
3703 It is assured that the current buffer (or STRING) is multibyte. */
3705 Lisp_Object
3706 font_range (ptrdiff_t pos, ptrdiff_t pos_byte, ptrdiff_t *limit,
3707 struct window *w, struct face *face, Lisp_Object string)
3709 ptrdiff_t ignore;
3710 int c;
3711 Lisp_Object font_object = Qnil;
3713 if (NILP (string))
3715 if (! face)
3717 int face_id;
3719 face_id = face_at_buffer_position (w, pos, &ignore,
3720 *limit, 0, -1);
3721 face = FACE_FROM_ID (XFRAME (w->frame), face_id);
3724 else
3725 eassert (face);
3727 while (pos < *limit)
3729 Lisp_Object category;
3731 if (NILP (string))
3732 FETCH_CHAR_ADVANCE_NO_CHECK (c, pos, pos_byte);
3733 else
3734 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, string, pos, pos_byte);
3735 category = CHAR_TABLE_REF (Vunicode_category_table, c);
3736 if (INTEGERP (category)
3737 && (XINT (category) == UNICODE_CATEGORY_Cf
3738 || CHAR_VARIATION_SELECTOR_P (c)))
3739 continue;
3740 if (NILP (font_object))
3742 font_object = font_for_char (face, c, pos - 1, string);
3743 if (NILP (font_object))
3744 return Qnil;
3745 continue;
3747 if (font_encode_char (font_object, c) == FONT_INVALID_CODE)
3748 *limit = pos - 1;
3750 return font_object;
3752 #endif
3755 /* Lisp API. */
3757 DEFUN ("fontp", Ffontp, Sfontp, 1, 2, 0,
3758 doc: /* Return t if OBJECT is a font-spec, font-entity, or font-object.
3759 Return nil otherwise.
3760 Optional 2nd argument EXTRA-TYPE, if non-nil, specifies to check
3761 which kind of font it is. It must be one of `font-spec', `font-entity',
3762 `font-object'. */)
3763 (Lisp_Object object, Lisp_Object extra_type)
3765 if (NILP (extra_type))
3766 return (FONTP (object) ? Qt : Qnil);
3767 if (EQ (extra_type, Qfont_spec))
3768 return (FONT_SPEC_P (object) ? Qt : Qnil);
3769 if (EQ (extra_type, Qfont_entity))
3770 return (FONT_ENTITY_P (object) ? Qt : Qnil);
3771 if (EQ (extra_type, Qfont_object))
3772 return (FONT_OBJECT_P (object) ? Qt : Qnil);
3773 wrong_type_argument (intern ("font-extra-type"), extra_type);
3776 DEFUN ("font-spec", Ffont_spec, Sfont_spec, 0, MANY, 0,
3777 doc: /* Return a newly created font-spec with arguments as properties.
3779 ARGS must come in pairs KEY VALUE of font properties. KEY must be a
3780 valid font property name listed below:
3782 `:family', `:weight', `:slant', `:width'
3784 They are the same as face attributes of the same name. See
3785 `set-face-attribute'.
3787 `:foundry'
3789 VALUE must be a string or a symbol specifying the font foundry, e.g. ``misc''.
3791 `:adstyle'
3793 VALUE must be a string or a symbol specifying the additional
3794 typographic style information of a font, e.g. ``sans''.
3796 `:registry'
3798 VALUE must be a string or a symbol specifying the charset registry and
3799 encoding of a font, e.g. ``iso8859-1''.
3801 `:size'
3803 VALUE must be a non-negative integer or a floating point number
3804 specifying the font size. It specifies the font size in pixels (if
3805 VALUE is an integer), or in points (if VALUE is a float).
3807 `:name'
3809 VALUE must be a string of XLFD-style or fontconfig-style font name.
3811 `:script'
3813 VALUE must be a symbol representing a script that the font must
3814 support. It may be a symbol representing a subgroup of a script
3815 listed in the variable `script-representative-chars'.
3817 `:lang'
3819 VALUE must be a symbol of two-letter ISO-639 language names,
3820 e.g. `ja'.
3822 `:otf'
3824 VALUE must be a list (SCRIPT-TAG LANGSYS-TAG GSUB [ GPOS ]) to specify
3825 required OpenType features.
3827 SCRIPT-TAG: OpenType script tag symbol (e.g. `deva').
3828 LANGSYS-TAG: OpenType language system tag symbol,
3829 or nil for the default language system.
3830 GSUB: List of OpenType GSUB feature tag symbols, or nil if none required.
3831 GPOS: List of OpenType GPOS feature tag symbols, or nil if none required.
3833 GSUB and GPOS may contain `nil' element. In such a case, the font
3834 must not have any of the remaining elements.
3836 For instance, if the VALUE is `(thai nil nil (mark))', the font must
3837 be an OpenType font whose GPOS table of `thai' script's default
3838 language system must contain `mark' feature.
3840 usage: (font-spec ARGS...) */)
3841 (ptrdiff_t nargs, Lisp_Object *args)
3843 Lisp_Object spec = font_make_spec ();
3844 ptrdiff_t i;
3846 for (i = 0; i < nargs; i += 2)
3848 Lisp_Object key = args[i], val;
3850 CHECK_SYMBOL (key);
3851 if (i + 1 >= nargs)
3852 error ("No value for key `%s'", SDATA (SYMBOL_NAME (key)));
3853 val = args[i + 1];
3855 if (EQ (key, QCname))
3857 CHECK_STRING (val);
3858 if (font_parse_name (SSDATA (val), SBYTES (val), spec) < 0)
3859 error ("Invalid font name: %s", SSDATA (val));
3860 font_put_extra (spec, key, val);
3862 else
3864 int idx = get_font_prop_index (key);
3866 if (idx >= 0)
3868 val = font_prop_validate (idx, Qnil, val);
3869 if (idx < FONT_EXTRA_INDEX)
3870 ASET (spec, idx, val);
3871 else
3872 font_put_extra (spec, key, val);
3874 else
3875 font_put_extra (spec, key, font_prop_validate (0, key, val));
3878 return spec;
3881 /* Return a copy of FONT as a font-spec. */
3882 Lisp_Object
3883 copy_font_spec (Lisp_Object font)
3885 Lisp_Object new_spec, tail, prev, extra;
3886 int i;
3888 CHECK_FONT (font);
3889 new_spec = font_make_spec ();
3890 for (i = 1; i < FONT_EXTRA_INDEX; i++)
3891 ASET (new_spec, i, AREF (font, i));
3892 extra = Fcopy_alist (AREF (font, FONT_EXTRA_INDEX));
3893 /* We must remove :font-entity property. */
3894 for (prev = Qnil, tail = extra; CONSP (tail); prev = tail, tail = XCDR (tail))
3895 if (EQ (XCAR (XCAR (tail)), QCfont_entity))
3897 if (NILP (prev))
3898 extra = XCDR (extra);
3899 else
3900 XSETCDR (prev, XCDR (tail));
3901 break;
3903 ASET (new_spec, FONT_EXTRA_INDEX, extra);
3904 return new_spec;
3907 /* Merge font-specs FROM and TO, and return a new font-spec.
3908 Every specified property in FROM overrides the corresponding
3909 property in TO. */
3910 Lisp_Object
3911 merge_font_spec (Lisp_Object from, Lisp_Object to)
3913 Lisp_Object extra, tail;
3914 int i;
3916 CHECK_FONT (from);
3917 CHECK_FONT (to);
3918 to = copy_font_spec (to);
3919 for (i = 0; i < FONT_EXTRA_INDEX; i++)
3920 ASET (to, i, AREF (from, i));
3921 extra = AREF (to, FONT_EXTRA_INDEX);
3922 for (tail = AREF (from, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail))
3923 if (! EQ (XCAR (XCAR (tail)), Qfont_entity))
3925 Lisp_Object slot = assq_no_quit (XCAR (XCAR (tail)), extra);
3927 if (! NILP (slot))
3928 XSETCDR (slot, XCDR (XCAR (tail)));
3929 else
3930 extra = Fcons (Fcons (XCAR (XCAR (tail)), XCDR (XCAR (tail))), extra);
3932 ASET (to, FONT_EXTRA_INDEX, extra);
3933 return to;
3936 DEFUN ("font-get", Ffont_get, Sfont_get, 2, 2, 0,
3937 doc: /* Return the value of FONT's property KEY.
3938 FONT is a font-spec, a font-entity, or a font-object.
3939 KEY is any symbol, but these are reserved for specific meanings:
3940 :family, :weight, :slant, :width, :foundry, :adstyle, :registry,
3941 :size, :name, :script, :otf
3942 See the documentation of `font-spec' for their meanings.
3943 In addition, if FONT is a font-entity or a font-object, values of
3944 :script and :otf are different from those of a font-spec as below:
3946 The value of :script may be a list of scripts that are supported by the font.
3948 The value of :otf is a cons (GSUB . GPOS) where GSUB and GPOS are lists
3949 representing the OpenType features supported by the font by this form:
3950 ((SCRIPT (LANGSYS FEATURE ...) ...) ...)
3951 SCRIPT, LANGSYS, and FEATURE are all symbols representing OpenType
3952 Layout tags. */)
3953 (Lisp_Object font, Lisp_Object key)
3955 int idx;
3956 Lisp_Object val;
3958 CHECK_FONT (font);
3959 CHECK_SYMBOL (key);
3961 idx = get_font_prop_index (key);
3962 if (idx >= FONT_WEIGHT_INDEX && idx <= FONT_WIDTH_INDEX)
3963 return font_style_symbolic (font, idx, 0);
3964 if (idx >= 0 && idx < FONT_EXTRA_INDEX)
3965 return AREF (font, idx);
3966 val = Fassq (key, AREF (font, FONT_EXTRA_INDEX));
3967 if (NILP (val) && EQ (key, QCotf) && FONT_OBJECT_P (font))
3969 struct font *fontp = XFONT_OBJECT (font);
3971 if (fontp->driver->otf_capability)
3972 val = fontp->driver->otf_capability (fontp);
3973 else
3974 val = Fcons (Qnil, Qnil);
3976 else
3977 val = Fcdr (val);
3978 return val;
3981 #ifdef HAVE_WINDOW_SYSTEM
3983 DEFUN ("font-face-attributes", Ffont_face_attributes, Sfont_face_attributes, 1, 2, 0,
3984 doc: /* Return a plist of face attributes generated by FONT.
3985 FONT is a font name, a font-spec, a font-entity, or a font-object.
3986 The return value is a list of the form
3988 \(:family FAMILY :height HEIGHT :weight WEIGHT :slant SLANT :width WIDTH)
3990 where FAMILY, HEIGHT, WEIGHT, SLANT, and WIDTH are face attribute values
3991 compatible with `set-face-attribute'. Some of these key-attribute pairs
3992 may be omitted from the list if they are not specified by FONT.
3994 The optional argument FRAME specifies the frame that the face attributes
3995 are to be displayed on. If omitted, the selected frame is used. */)
3996 (Lisp_Object font, Lisp_Object frame)
3998 struct frame *f = decode_live_frame (frame);
3999 Lisp_Object plist[10];
4000 Lisp_Object val;
4001 int n = 0;
4003 if (STRINGP (font))
4005 int fontset = fs_query_fontset (font, 0);
4006 Lisp_Object name = font;
4007 if (fontset >= 0)
4008 font = fontset_ascii (fontset);
4009 font = font_spec_from_name (name);
4010 if (! FONTP (font))
4011 signal_error ("Invalid font name", name);
4013 else if (! FONTP (font))
4014 signal_error ("Invalid font object", font);
4016 val = AREF (font, FONT_FAMILY_INDEX);
4017 if (! NILP (val))
4019 plist[n++] = QCfamily;
4020 plist[n++] = SYMBOL_NAME (val);
4023 val = AREF (font, FONT_SIZE_INDEX);
4024 if (INTEGERP (val))
4026 Lisp_Object font_dpi = AREF (font, FONT_DPI_INDEX);
4027 int dpi = INTEGERP (font_dpi) ? XINT (font_dpi) : FRAME_RES_Y (f);
4028 plist[n++] = QCheight;
4029 plist[n++] = make_number (PIXEL_TO_POINT (XINT (val) * 10, dpi));
4031 else if (FLOATP (val))
4033 plist[n++] = QCheight;
4034 plist[n++] = make_number (10 * (int) XFLOAT_DATA (val));
4037 val = FONT_WEIGHT_FOR_FACE (font);
4038 if (! NILP (val))
4040 plist[n++] = QCweight;
4041 plist[n++] = val;
4044 val = FONT_SLANT_FOR_FACE (font);
4045 if (! NILP (val))
4047 plist[n++] = QCslant;
4048 plist[n++] = val;
4051 val = FONT_WIDTH_FOR_FACE (font);
4052 if (! NILP (val))
4054 plist[n++] = QCwidth;
4055 plist[n++] = val;
4058 return Flist (n, plist);
4061 #endif
4063 DEFUN ("font-put", Ffont_put, Sfont_put, 3, 3, 0,
4064 doc: /* Set one property of FONT: give property KEY value VAL.
4065 FONT is a font-spec, a font-entity, or a font-object.
4067 If FONT is a font-spec, KEY can be any symbol. But if KEY is the one
4068 accepted by the function `font-spec' (which see), VAL must be what
4069 allowed in `font-spec'.
4071 If FONT is a font-entity or a font-object, KEY must not be the one
4072 accepted by `font-spec'. */)
4073 (Lisp_Object font, Lisp_Object prop, Lisp_Object val)
4075 int idx;
4077 idx = get_font_prop_index (prop);
4078 if (idx >= 0 && idx < FONT_EXTRA_INDEX)
4080 CHECK_FONT_SPEC (font);
4081 ASET (font, idx, font_prop_validate (idx, Qnil, val));
4083 else
4085 if (EQ (prop, QCname)
4086 || EQ (prop, QCscript)
4087 || EQ (prop, QClang)
4088 || EQ (prop, QCotf))
4089 CHECK_FONT_SPEC (font);
4090 else
4091 CHECK_FONT (font);
4092 font_put_extra (font, prop, font_prop_validate (0, prop, val));
4094 return val;
4097 DEFUN ("list-fonts", Flist_fonts, Slist_fonts, 1, 4, 0,
4098 doc: /* List available fonts matching FONT-SPEC on the current frame.
4099 Optional 2nd argument FRAME specifies the target frame.
4100 Optional 3rd argument NUM, if non-nil, limits the number of returned fonts.
4101 Optional 4th argument PREFER, if non-nil, is a font-spec to
4102 control the order of the returned list. Fonts are sorted by
4103 how close they are to PREFER. */)
4104 (Lisp_Object font_spec, Lisp_Object frame, Lisp_Object num, Lisp_Object prefer)
4106 struct frame *f = decode_live_frame (frame);
4107 Lisp_Object vec, list;
4108 EMACS_INT n = 0;
4110 CHECK_FONT_SPEC (font_spec);
4111 if (! NILP (num))
4113 CHECK_NUMBER (num);
4114 n = XINT (num);
4115 if (n <= 0)
4116 return Qnil;
4118 if (! NILP (prefer))
4119 CHECK_FONT_SPEC (prefer);
4121 list = font_list_entities (f, font_spec);
4122 if (NILP (list))
4123 return Qnil;
4124 if (NILP (XCDR (list))
4125 && ASIZE (XCAR (list)) == 1)
4126 return list1 (AREF (XCAR (list), 0));
4128 if (! NILP (prefer))
4129 vec = font_sort_entities (list, prefer, f, 0);
4130 else
4131 vec = font_vconcat_entity_vectors (list);
4132 if (n == 0 || n >= ASIZE (vec))
4134 Lisp_Object args[2];
4136 args[0] = vec;
4137 args[1] = Qnil;
4138 list = Fappend (2, args);
4140 else
4142 for (list = Qnil, n--; n >= 0; n--)
4143 list = Fcons (AREF (vec, n), list);
4145 return list;
4148 DEFUN ("font-family-list", Ffont_family_list, Sfont_family_list, 0, 1, 0,
4149 doc: /* List available font families on the current frame.
4150 If FRAME is omitted or nil, the selected frame is used. */)
4151 (Lisp_Object frame)
4153 struct frame *f = decode_live_frame (frame);
4154 struct font_driver_list *driver_list;
4155 Lisp_Object list = Qnil;
4157 for (driver_list = f->font_driver_list; driver_list;
4158 driver_list = driver_list->next)
4159 if (driver_list->driver->list_family)
4161 Lisp_Object val = driver_list->driver->list_family (f);
4162 Lisp_Object tail = list;
4164 for (; CONSP (val); val = XCDR (val))
4165 if (NILP (Fmemq (XCAR (val), tail))
4166 && SYMBOLP (XCAR (val)))
4167 list = Fcons (SYMBOL_NAME (XCAR (val)), list);
4169 return list;
4172 DEFUN ("find-font", Ffind_font, Sfind_font, 1, 2, 0,
4173 doc: /* Return a font-entity matching with FONT-SPEC on the current frame.
4174 Optional 2nd argument FRAME, if non-nil, specifies the target frame. */)
4175 (Lisp_Object font_spec, Lisp_Object frame)
4177 Lisp_Object val = Flist_fonts (font_spec, frame, make_number (1), Qnil);
4179 if (CONSP (val))
4180 val = XCAR (val);
4181 return val;
4184 DEFUN ("font-xlfd-name", Ffont_xlfd_name, Sfont_xlfd_name, 1, 2, 0,
4185 doc: /* Return XLFD name of FONT.
4186 FONT is a font-spec, font-entity, or font-object.
4187 If the name is too long for XLFD (maximum 255 chars), return nil.
4188 If the 2nd optional arg FOLD-WILDCARDS is non-nil,
4189 the consecutive wildcards are folded into one. */)
4190 (Lisp_Object font, Lisp_Object fold_wildcards)
4192 char name[256];
4193 int namelen, pixel_size = 0;
4195 CHECK_FONT (font);
4197 if (FONT_OBJECT_P (font))
4199 Lisp_Object font_name = AREF (font, FONT_NAME_INDEX);
4201 if (STRINGP (font_name)
4202 && SDATA (font_name)[0] == '-')
4204 if (NILP (fold_wildcards))
4205 return font_name;
4206 strcpy (name, SSDATA (font_name));
4207 namelen = SBYTES (font_name);
4208 goto done;
4210 pixel_size = XFONT_OBJECT (font)->pixel_size;
4212 namelen = font_unparse_xlfd (font, pixel_size, name, 256);
4213 if (namelen < 0)
4214 return Qnil;
4215 done:
4216 if (! NILP (fold_wildcards))
4218 char *p0 = name, *p1;
4220 while ((p1 = strstr (p0, "-*-*")))
4222 strcpy (p1, p1 + 2);
4223 namelen -= 2;
4224 p0 = p1;
4228 return make_string (name, namelen);
4231 void
4232 clear_font_cache (struct frame *f)
4234 struct font_driver_list *driver_list = f->font_driver_list;
4236 for (; driver_list; driver_list = driver_list->next)
4237 if (driver_list->on)
4239 Lisp_Object val, tmp, cache = driver_list->driver->get_cache (f);
4241 val = XCDR (cache);
4242 while (! NILP (val)
4243 && ! EQ (XCAR (XCAR (val)), driver_list->driver->type))
4244 val = XCDR (val);
4245 eassert (! NILP (val));
4246 tmp = XCDR (XCAR (val));
4247 if (XINT (XCAR (tmp)) == 0)
4249 font_clear_cache (f, XCAR (val), driver_list->driver);
4250 XSETCDR (cache, XCDR (val));
4255 DEFUN ("clear-font-cache", Fclear_font_cache, Sclear_font_cache, 0, 0, 0,
4256 doc: /* Clear font cache of each frame. */)
4257 (void)
4259 Lisp_Object list, frame;
4261 FOR_EACH_FRAME (list, frame)
4262 clear_font_cache (XFRAME (frame));
4264 return Qnil;
4268 void
4269 font_fill_lglyph_metrics (Lisp_Object glyph, Lisp_Object font_object)
4271 struct font *font = XFONT_OBJECT (font_object);
4272 unsigned code = font->driver->encode_char (font, LGLYPH_CHAR (glyph));
4273 struct font_metrics metrics;
4275 LGLYPH_SET_CODE (glyph, code);
4276 font->driver->text_extents (font, &code, 1, &metrics);
4277 LGLYPH_SET_LBEARING (glyph, metrics.lbearing);
4278 LGLYPH_SET_RBEARING (glyph, metrics.rbearing);
4279 LGLYPH_SET_WIDTH (glyph, metrics.width);
4280 LGLYPH_SET_ASCENT (glyph, metrics.ascent);
4281 LGLYPH_SET_DESCENT (glyph, metrics.descent);
4285 DEFUN ("font-shape-gstring", Ffont_shape_gstring, Sfont_shape_gstring, 1, 1, 0,
4286 doc: /* Shape the glyph-string GSTRING.
4287 Shaping means substituting glyphs and/or adjusting positions of glyphs
4288 to get the correct visual image of character sequences set in the
4289 header of the glyph-string.
4291 If the shaping was successful, the value is GSTRING itself or a newly
4292 created glyph-string. Otherwise, the value is nil.
4294 See the documentation of `composition-get-gstring' for the format of
4295 GSTRING. */)
4296 (Lisp_Object gstring)
4298 struct font *font;
4299 Lisp_Object font_object, n, glyph;
4300 ptrdiff_t i, from, to;
4302 if (! composition_gstring_p (gstring))
4303 signal_error ("Invalid glyph-string: ", gstring);
4304 if (! NILP (LGSTRING_ID (gstring)))
4305 return gstring;
4306 font_object = LGSTRING_FONT (gstring);
4307 CHECK_FONT_OBJECT (font_object);
4308 font = XFONT_OBJECT (font_object);
4309 if (! font->driver->shape)
4310 return Qnil;
4312 /* Try at most three times with larger gstring each time. */
4313 for (i = 0; i < 3; i++)
4315 n = font->driver->shape (gstring);
4316 if (INTEGERP (n))
4317 break;
4318 gstring = larger_vector (gstring,
4319 LGSTRING_GLYPH_LEN (gstring), -1);
4321 if (i == 3 || XINT (n) == 0)
4322 return Qnil;
4323 if (XINT (n) < LGSTRING_GLYPH_LEN (gstring))
4324 LGSTRING_SET_GLYPH (gstring, XINT (n), Qnil);
4326 /* Check FROM_IDX and TO_IDX of each GLYPH in GSTRING to assure that
4327 GLYPHS covers all characters (except for the last few ones) in
4328 GSTRING. More formally, provided that NCHARS is the number of
4329 characters in GSTRING and GLYPHS[i] is the ith glyph, FROM_IDX
4330 and TO_IDX of each glyph must satisfy these conditions:
4332 GLYPHS[0].FROM_IDX == 0
4333 GLYPHS[i].FROM_IDX <= GLYPHS[i].TO_IDX
4334 if (GLYPHS[i].FROM_IDX == GLYPHS[i-1].FROM_IDX)
4335 ;; GLYPHS[i] and GLYPHS[i-1] belongs to the same grapheme cluster
4336 GLYPHS[i].TO_IDX == GLYPHS[i-1].TO_IDX
4337 else
4338 ;; Be sure to cover all characters.
4339 GLYPHS[i].FROM_IDX == GLYPHS[i-1].TO_IDX + 1 */
4340 glyph = LGSTRING_GLYPH (gstring, 0);
4341 from = LGLYPH_FROM (glyph);
4342 to = LGLYPH_TO (glyph);
4343 if (from != 0 || to < from)
4344 goto shaper_error;
4345 for (i = 1; i < LGSTRING_GLYPH_LEN (gstring); i++)
4347 glyph = LGSTRING_GLYPH (gstring, i);
4348 if (NILP (glyph))
4349 break;
4350 if (! (LGLYPH_FROM (glyph) <= LGLYPH_TO (glyph)
4351 && (LGLYPH_FROM (glyph) == from
4352 ? LGLYPH_TO (glyph) == to
4353 : LGLYPH_FROM (glyph) == to + 1)))
4354 goto shaper_error;
4355 from = LGLYPH_FROM (glyph);
4356 to = LGLYPH_TO (glyph);
4358 return composition_gstring_put_cache (gstring, XINT (n));
4360 shaper_error:
4361 return Qnil;
4364 DEFUN ("font-variation-glyphs", Ffont_variation_glyphs, Sfont_variation_glyphs,
4365 2, 2, 0,
4366 doc: /* Return a list of variation glyphs for CHAR in FONT-OBJECT.
4367 Each element of the value is a cons (VARIATION-SELECTOR . GLYPH-ID),
4368 where
4369 VARIATION-SELECTOR is a character code of variation selection
4370 (#xFE00..#xFE0F or #xE0100..#xE01EF)
4371 GLYPH-ID is a glyph code of the corresponding variation glyph. */)
4372 (Lisp_Object font_object, Lisp_Object character)
4374 unsigned variations[256];
4375 struct font *font;
4376 int i, n;
4377 Lisp_Object val;
4379 CHECK_FONT_OBJECT (font_object);
4380 CHECK_CHARACTER (character);
4381 font = XFONT_OBJECT (font_object);
4382 if (! font->driver->get_variation_glyphs)
4383 return Qnil;
4384 n = font->driver->get_variation_glyphs (font, XINT (character), variations);
4385 if (! n)
4386 return Qnil;
4387 val = Qnil;
4388 for (i = 0; i < 255; i++)
4389 if (variations[i])
4391 int vs = (i < 16 ? 0xFE00 + i : 0xE0100 + (i - 16));
4392 Lisp_Object code = INTEGER_TO_CONS (variations[i]);
4393 val = Fcons (Fcons (make_number (vs), code), val);
4395 return val;
4398 #if 0
4400 DEFUN ("font-drive-otf", Ffont_drive_otf, Sfont_drive_otf, 6, 6, 0,
4401 doc: /* Apply OpenType features on glyph-string GSTRING-IN.
4402 OTF-FEATURES specifies which features to apply in this format:
4403 (SCRIPT LANGSYS GSUB GPOS)
4404 where
4405 SCRIPT is a symbol specifying a script tag of OpenType,
4406 LANGSYS is a symbol specifying a langsys tag of OpenType,
4407 GSUB and GPOS, if non-nil, are lists of symbols specifying feature tags.
4409 If LANGYS is nil, the default langsys is selected.
4411 The features are applied in the order they appear in the list. The
4412 symbol `*' means to apply all available features not present in this
4413 list, and the remaining features are ignored. For instance, (vatu
4414 pstf * haln) is to apply vatu and pstf in this order, then to apply
4415 all available features other than vatu, pstf, and haln.
4417 The features are applied to the glyphs in the range FROM and TO of
4418 the glyph-string GSTRING-IN.
4420 If some feature is actually applicable, the resulting glyphs are
4421 produced in the glyph-string GSTRING-OUT from the index INDEX. In
4422 this case, the value is the number of produced glyphs.
4424 If no feature is applicable, no glyph is produced in GSTRING-OUT, and
4425 the value is 0.
4427 If GSTRING-OUT is too short to hold produced glyphs, no glyphs are
4428 produced in GSTRING-OUT, and the value is nil.
4430 See the documentation of `composition-get-gstring' for the format of
4431 glyph-string. */)
4432 (Lisp_Object otf_features, Lisp_Object gstring_in, Lisp_Object from, Lisp_Object to, Lisp_Object gstring_out, Lisp_Object index)
4434 Lisp_Object font_object = LGSTRING_FONT (gstring_in);
4435 Lisp_Object val;
4436 struct font *font;
4437 int len, num;
4439 check_otf_features (otf_features);
4440 CHECK_FONT_OBJECT (font_object);
4441 font = XFONT_OBJECT (font_object);
4442 if (! font->driver->otf_drive)
4443 error ("Font backend %s can't drive OpenType GSUB table",
4444 SDATA (SYMBOL_NAME (font->driver->type)));
4445 CHECK_CONS (otf_features);
4446 CHECK_SYMBOL (XCAR (otf_features));
4447 val = XCDR (otf_features);
4448 CHECK_SYMBOL (XCAR (val));
4449 val = XCDR (otf_features);
4450 if (! NILP (val))
4451 CHECK_CONS (val);
4452 len = check_gstring (gstring_in);
4453 CHECK_VECTOR (gstring_out);
4454 CHECK_NATNUM (from);
4455 CHECK_NATNUM (to);
4456 CHECK_NATNUM (index);
4458 if (XINT (from) >= XINT (to) || XINT (to) > len)
4459 args_out_of_range_3 (from, to, make_number (len));
4460 if (XINT (index) >= ASIZE (gstring_out))
4461 args_out_of_range (index, make_number (ASIZE (gstring_out)));
4462 num = font->driver->otf_drive (font, otf_features,
4463 gstring_in, XINT (from), XINT (to),
4464 gstring_out, XINT (index), 0);
4465 if (num < 0)
4466 return Qnil;
4467 return make_number (num);
4470 DEFUN ("font-otf-alternates", Ffont_otf_alternates, Sfont_otf_alternates,
4471 3, 3, 0,
4472 doc: /* Return a list of alternate glyphs of CHARACTER in FONT-OBJECT.
4473 OTF-FEATURES specifies which features of the font FONT-OBJECT to apply
4474 in this format:
4475 (SCRIPT LANGSYS FEATURE ...)
4476 See the documentation of `font-drive-otf' for more detail.
4478 The value is a list of cons cells of the format (GLYPH-ID . CHARACTER),
4479 where GLYPH-ID is a glyph index of the font, and CHARACTER is a
4480 character code corresponding to the glyph or nil if there's no
4481 corresponding character. */)
4482 (Lisp_Object font_object, Lisp_Object character, Lisp_Object otf_features)
4484 struct font *font;
4485 Lisp_Object gstring_in, gstring_out, g;
4486 Lisp_Object alternates;
4487 int i, num;
4489 CHECK_FONT_GET_OBJECT (font_object, font);
4490 if (! font->driver->otf_drive)
4491 error ("Font backend %s can't drive OpenType GSUB table",
4492 SDATA (SYMBOL_NAME (font->driver->type)));
4493 CHECK_CHARACTER (character);
4494 CHECK_CONS (otf_features);
4496 gstring_in = Ffont_make_gstring (font_object, make_number (1));
4497 g = LGSTRING_GLYPH (gstring_in, 0);
4498 LGLYPH_SET_CHAR (g, XINT (character));
4499 gstring_out = Ffont_make_gstring (font_object, make_number (10));
4500 while ((num = font->driver->otf_drive (font, otf_features, gstring_in, 0, 1,
4501 gstring_out, 0, 1)) < 0)
4502 gstring_out = Ffont_make_gstring (font_object,
4503 make_number (ASIZE (gstring_out) * 2));
4504 alternates = Qnil;
4505 for (i = 0; i < num; i++)
4507 Lisp_Object g = LGSTRING_GLYPH (gstring_out, i);
4508 int c = LGLYPH_CHAR (g);
4509 unsigned code = LGLYPH_CODE (g);
4511 alternates = Fcons (Fcons (make_number (code),
4512 c > 0 ? make_number (c) : Qnil),
4513 alternates);
4515 return Fnreverse (alternates);
4517 #endif /* 0 */
4519 #ifdef FONT_DEBUG
4521 DEFUN ("open-font", Fopen_font, Sopen_font, 1, 3, 0,
4522 doc: /* Open FONT-ENTITY. */)
4523 (Lisp_Object font_entity, Lisp_Object size, Lisp_Object frame)
4525 EMACS_INT isize;
4526 struct frame *f = decode_live_frame (frame);
4528 CHECK_FONT_ENTITY (font_entity);
4530 if (NILP (size))
4531 isize = XINT (AREF (font_entity, FONT_SIZE_INDEX));
4532 else
4534 CHECK_NUMBER_OR_FLOAT (size);
4535 if (FLOATP (size))
4536 isize = POINT_TO_PIXEL (XFLOAT_DATA (size), FRAME_RES_Y (f));
4537 else
4538 isize = XINT (size);
4539 if (! (INT_MIN <= isize && isize <= INT_MAX))
4540 args_out_of_range (font_entity, size);
4541 if (isize == 0)
4542 isize = 120;
4544 return font_open_entity (f, font_entity, isize);
4547 DEFUN ("close-font", Fclose_font, Sclose_font, 1, 2, 0,
4548 doc: /* Close FONT-OBJECT. */)
4549 (Lisp_Object font_object, Lisp_Object frame)
4551 CHECK_FONT_OBJECT (font_object);
4552 font_close_object (decode_live_frame (frame), font_object);
4553 return Qnil;
4556 DEFUN ("query-font", Fquery_font, Squery_font, 1, 1, 0,
4557 doc: /* Return information about FONT-OBJECT.
4558 The value is a vector:
4559 [ NAME FILENAME PIXEL-SIZE SIZE ASCENT DESCENT SPACE-WIDTH AVERAGE-WIDTH
4560 CAPABILITY ]
4562 NAME is the font name, a string (or nil if the font backend doesn't
4563 provide a name).
4565 FILENAME is the font file name, a string (or nil if the font backend
4566 doesn't provide a file name).
4568 PIXEL-SIZE is a pixel size by which the font is opened.
4570 SIZE is a maximum advance width of the font in pixels.
4572 ASCENT, DESCENT, SPACE-WIDTH, AVERAGE-WIDTH are metrics of the font in
4573 pixels.
4575 CAPABILITY is a list whose first element is a symbol representing the
4576 font format \(x, opentype, truetype, type1, pcf, or bdf) and the
4577 remaining elements describe the details of the font capability.
4579 If the font is OpenType font, the form of the list is
4580 \(opentype GSUB GPOS)
4581 where GSUB shows which "GSUB" features the font supports, and GPOS
4582 shows which "GPOS" features the font supports. Both GSUB and GPOS are
4583 lists of the format:
4584 \((SCRIPT (LANGSYS FEATURE ...) ...) ...)
4586 If the font is not OpenType font, currently the length of the form is
4587 one.
4589 SCRIPT is a symbol representing OpenType script tag.
4591 LANGSYS is a symbol representing OpenType langsys tag, or nil
4592 representing the default langsys.
4594 FEATURE is a symbol representing OpenType feature tag.
4596 If the font is not OpenType font, CAPABILITY is nil. */)
4597 (Lisp_Object font_object)
4599 struct font *font;
4600 Lisp_Object val;
4602 CHECK_FONT_GET_OBJECT (font_object, font);
4604 val = make_uninit_vector (9);
4605 ASET (val, 0, AREF (font_object, FONT_NAME_INDEX));
4606 ASET (val, 1, AREF (font_object, FONT_FILE_INDEX));
4607 ASET (val, 2, make_number (font->pixel_size));
4608 ASET (val, 3, make_number (font->max_width));
4609 ASET (val, 4, make_number (font->ascent));
4610 ASET (val, 5, make_number (font->descent));
4611 ASET (val, 6, make_number (font->space_width));
4612 ASET (val, 7, make_number (font->average_width));
4613 if (font->driver->otf_capability)
4614 ASET (val, 8, Fcons (Qopentype, font->driver->otf_capability (font)));
4615 else
4616 ASET (val, 8, Qnil);
4617 return val;
4620 DEFUN ("font-get-glyphs", Ffont_get_glyphs, Sfont_get_glyphs, 3, 4, 0,
4621 doc:
4622 /* Return a vector of FONT-OBJECT's glyphs for the specified characters.
4623 FROM and TO are positions (integers or markers) specifying a region
4624 of the current buffer.
4625 If the optional fourth arg OBJECT is not nil, it is a string or a
4626 vector containing the target characters.
4628 Each element is a vector containing information of a glyph in this format:
4629 [FROM-IDX TO-IDX C CODE WIDTH LBEARING RBEARING ASCENT DESCENT ADJUSTMENT]
4630 where
4631 FROM is an index numbers of a character the glyph corresponds to.
4632 TO is the same as FROM.
4633 C is the character of the glyph.
4634 CODE is the glyph-code of C in FONT-OBJECT.
4635 WIDTH thru DESCENT are the metrics (in pixels) of the glyph.
4636 ADJUSTMENT is always nil.
4637 If FONT-OBJECT doesn't have a glyph for a character,
4638 the corresponding element is nil. */)
4639 (Lisp_Object font_object, Lisp_Object from, Lisp_Object to,
4640 Lisp_Object object)
4642 struct font *font;
4643 ptrdiff_t i, len;
4644 Lisp_Object *chars, vec;
4645 USE_SAFE_ALLOCA;
4647 CHECK_FONT_GET_OBJECT (font_object, font);
4648 if (NILP (object))
4650 ptrdiff_t charpos, bytepos;
4652 validate_region (&from, &to);
4653 if (EQ (from, to))
4654 return Qnil;
4655 len = XFASTINT (to) - XFASTINT (from);
4656 SAFE_ALLOCA_LISP (chars, len);
4657 charpos = XFASTINT (from);
4658 bytepos = CHAR_TO_BYTE (charpos);
4659 for (i = 0; charpos < XFASTINT (to); i++)
4661 int c;
4662 FETCH_CHAR_ADVANCE (c, charpos, bytepos);
4663 chars[i] = make_number (c);
4666 else if (STRINGP (object))
4668 const unsigned char *p;
4670 CHECK_NUMBER (from);
4671 CHECK_NUMBER (to);
4672 if (XINT (from) < 0 || XINT (from) > XINT (to)
4673 || XINT (to) > SCHARS (object))
4674 args_out_of_range_3 (object, from, to);
4675 if (EQ (from, to))
4676 return Qnil;
4677 len = XFASTINT (to) - XFASTINT (from);
4678 SAFE_ALLOCA_LISP (chars, len);
4679 p = SDATA (object);
4680 if (STRING_MULTIBYTE (object))
4681 for (i = 0; i < len; i++)
4683 int c = STRING_CHAR_ADVANCE (p);
4684 chars[i] = make_number (c);
4686 else
4687 for (i = 0; i < len; i++)
4688 chars[i] = make_number (p[i]);
4690 else
4692 CHECK_VECTOR (object);
4693 CHECK_NUMBER (from);
4694 CHECK_NUMBER (to);
4695 if (XINT (from) < 0 || XINT (from) > XINT (to)
4696 || XINT (to) > ASIZE (object))
4697 args_out_of_range_3 (object, from, to);
4698 if (EQ (from, to))
4699 return Qnil;
4700 len = XFASTINT (to) - XFASTINT (from);
4701 for (i = 0; i < len; i++)
4703 Lisp_Object elt = AREF (object, XFASTINT (from) + i);
4704 CHECK_CHARACTER (elt);
4706 chars = aref_addr (object, XFASTINT (from));
4709 vec = make_uninit_vector (len);
4710 for (i = 0; i < len; i++)
4712 Lisp_Object g;
4713 int c = XFASTINT (chars[i]);
4714 unsigned code;
4715 struct font_metrics metrics;
4717 code = font->driver->encode_char (font, c);
4718 if (code == FONT_INVALID_CODE)
4720 ASET (vec, i, Qnil);
4721 continue;
4723 g = LGLYPH_NEW ();
4724 LGLYPH_SET_FROM (g, i);
4725 LGLYPH_SET_TO (g, i);
4726 LGLYPH_SET_CHAR (g, c);
4727 LGLYPH_SET_CODE (g, code);
4728 font->driver->text_extents (font, &code, 1, &metrics);
4729 LGLYPH_SET_WIDTH (g, metrics.width);
4730 LGLYPH_SET_LBEARING (g, metrics.lbearing);
4731 LGLYPH_SET_RBEARING (g, metrics.rbearing);
4732 LGLYPH_SET_ASCENT (g, metrics.ascent);
4733 LGLYPH_SET_DESCENT (g, metrics.descent);
4734 ASET (vec, i, g);
4736 if (! VECTORP (object))
4737 SAFE_FREE ();
4738 return vec;
4741 DEFUN ("font-match-p", Ffont_match_p, Sfont_match_p, 2, 2, 0,
4742 doc: /* Return t if and only if font-spec SPEC matches with FONT.
4743 FONT is a font-spec, font-entity, or font-object. */)
4744 (Lisp_Object spec, Lisp_Object font)
4746 CHECK_FONT_SPEC (spec);
4747 CHECK_FONT (font);
4749 return (font_match_p (spec, font) ? Qt : Qnil);
4752 DEFUN ("font-at", Ffont_at, Sfont_at, 1, 3, 0,
4753 doc: /* Return a font-object for displaying a character at POSITION.
4754 Optional second arg WINDOW, if non-nil, is a window displaying
4755 the current buffer. It defaults to the currently selected window.
4756 Optional third arg STRING, if non-nil, is a string containing the target
4757 character at index specified by POSITION. */)
4758 (Lisp_Object position, Lisp_Object window, Lisp_Object string)
4760 struct window *w = decode_live_window (window);
4762 if (NILP (string))
4764 if (XBUFFER (w->contents) != current_buffer)
4765 error ("Specified window is not displaying the current buffer.");
4766 CHECK_NUMBER_COERCE_MARKER (position);
4767 if (! (BEGV <= XINT (position) && XINT (position) < ZV))
4768 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
4770 else
4772 CHECK_NUMBER (position);
4773 CHECK_STRING (string);
4774 if (! (0 <= XINT (position) && XINT (position) < SCHARS (string)))
4775 args_out_of_range (string, position);
4778 return font_at (-1, XINT (position), NULL, w, string);
4781 #if 0
4782 DEFUN ("draw-string", Fdraw_string, Sdraw_string, 2, 2, 0,
4783 doc: /* Draw STRING by FONT-OBJECT on the top left corner of the current frame.
4784 The value is a number of glyphs drawn.
4785 Type C-l to recover what previously shown. */)
4786 (Lisp_Object font_object, Lisp_Object string)
4788 Lisp_Object frame = selected_frame;
4789 struct frame *f = XFRAME (frame);
4790 struct font *font;
4791 struct face *face;
4792 int i, len, width;
4793 unsigned *code;
4795 CHECK_FONT_GET_OBJECT (font_object, font);
4796 CHECK_STRING (string);
4797 len = SCHARS (string);
4798 code = alloca (sizeof (unsigned) * len);
4799 for (i = 0; i < len; i++)
4801 Lisp_Object ch = Faref (string, make_number (i));
4802 Lisp_Object val;
4803 int c = XINT (ch);
4805 code[i] = font->driver->encode_char (font, c);
4806 if (code[i] == FONT_INVALID_CODE)
4807 break;
4809 face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4810 face->fontp = font;
4811 if (font->driver->prepare_face)
4812 font->driver->prepare_face (f, face);
4813 width = font->driver->text_extents (font, code, i, NULL);
4814 len = font->driver->draw_text (f, face, 0, font->ascent, code, i, width);
4815 if (font->driver->done_face)
4816 font->driver->done_face (f, face);
4817 face->fontp = NULL;
4818 return make_number (len);
4820 #endif
4822 #endif /* FONT_DEBUG */
4824 #ifdef HAVE_WINDOW_SYSTEM
4826 DEFUN ("font-info", Ffont_info, Sfont_info, 1, 2, 0,
4827 doc: /* Return information about a font named NAME on frame FRAME.
4828 If FRAME is omitted or nil, use the selected frame.
4829 The returned value is a vector of OPENED-NAME, FULL-NAME, SIZE,
4830 HEIGHT, BASELINE-OFFSET, RELATIVE-COMPOSE, and DEFAULT-ASCENT,
4831 where
4832 OPENED-NAME is the name used for opening the font,
4833 FULL-NAME is the full name of the font,
4834 SIZE is the pixelsize of the font,
4835 HEIGHT is the pixel-height of the font (i.e., ascent + descent),
4836 BASELINE-OFFSET is the upward offset pixels from ASCII baseline,
4837 RELATIVE-COMPOSE and DEFAULT-ASCENT are the numbers controlling
4838 how to compose characters.
4839 If the named font is not yet loaded, return nil. */)
4840 (Lisp_Object name, Lisp_Object frame)
4842 struct frame *f;
4843 struct font *font;
4844 Lisp_Object info;
4845 Lisp_Object font_object;
4847 if (! FONTP (name))
4848 CHECK_STRING (name);
4849 f = decode_window_system_frame (frame);
4851 if (STRINGP (name))
4853 int fontset = fs_query_fontset (name, 0);
4855 if (fontset >= 0)
4856 name = fontset_ascii (fontset);
4857 font_object = font_open_by_name (f, name);
4859 else if (FONT_OBJECT_P (name))
4860 font_object = name;
4861 else if (FONT_ENTITY_P (name))
4862 font_object = font_open_entity (f, name, 0);
4863 else
4865 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4866 Lisp_Object entity = font_matching_entity (f, face->lface, name);
4868 font_object = ! NILP (entity) ? font_open_entity (f, entity, 0) : Qnil;
4870 if (NILP (font_object))
4871 return Qnil;
4872 font = XFONT_OBJECT (font_object);
4874 info = make_uninit_vector (7);
4875 ASET (info, 0, AREF (font_object, FONT_NAME_INDEX));
4876 ASET (info, 1, AREF (font_object, FONT_FULLNAME_INDEX));
4877 ASET (info, 2, make_number (font->pixel_size));
4878 ASET (info, 3, make_number (font->height));
4879 ASET (info, 4, make_number (font->baseline_offset));
4880 ASET (info, 5, make_number (font->relative_compose));
4881 ASET (info, 6, make_number (font->default_ascent));
4883 #if 0
4884 /* As font_object is still in FONT_OBJLIST of the entity, we can't
4885 close it now. Perhaps, we should manage font-objects
4886 by `reference-count'. */
4887 font_close_object (f, font_object);
4888 #endif
4889 return info;
4891 #endif
4894 #define BUILD_STYLE_TABLE(TBL) \
4895 build_style_table ((TBL), sizeof TBL / sizeof (struct table_entry))
4897 static Lisp_Object
4898 build_style_table (const struct table_entry *entry, int nelement)
4900 int i, j;
4901 Lisp_Object table, elt;
4903 table = make_uninit_vector (nelement);
4904 for (i = 0; i < nelement; i++)
4906 for (j = 0; entry[i].names[j]; j++);
4907 elt = Fmake_vector (make_number (j + 1), Qnil);
4908 ASET (elt, 0, make_number (entry[i].numeric));
4909 for (j = 0; entry[i].names[j]; j++)
4910 ASET (elt, j + 1, intern_c_string (entry[i].names[j]));
4911 ASET (table, i, elt);
4913 return table;
4916 /* The deferred font-log data of the form [ACTION ARG RESULT].
4917 If ACTION is not nil, that is added to the log when font_add_log is
4918 called next time. At that time, ACTION is set back to nil. */
4919 static Lisp_Object Vfont_log_deferred;
4921 /* Prepend the font-related logging data in Vfont_log if it is not
4922 `t'. ACTION describes a kind of font-related action (e.g. listing,
4923 opening), ARG is the argument for the action, and RESULT is the
4924 result of the action. */
4925 void
4926 font_add_log (const char *action, Lisp_Object arg, Lisp_Object result)
4928 Lisp_Object val;
4929 int i;
4931 if (EQ (Vfont_log, Qt))
4932 return;
4933 if (STRINGP (AREF (Vfont_log_deferred, 0)))
4935 char *str = SSDATA (AREF (Vfont_log_deferred, 0));
4937 ASET (Vfont_log_deferred, 0, Qnil);
4938 font_add_log (str, AREF (Vfont_log_deferred, 1),
4939 AREF (Vfont_log_deferred, 2));
4942 if (FONTP (arg))
4944 Lisp_Object tail, elt;
4945 Lisp_Object equalstr = build_string ("=");
4947 val = Ffont_xlfd_name (arg, Qt);
4948 for (tail = AREF (arg, FONT_EXTRA_INDEX); CONSP (tail);
4949 tail = XCDR (tail))
4951 elt = XCAR (tail);
4952 if (EQ (XCAR (elt), QCscript)
4953 && SYMBOLP (XCDR (elt)))
4954 val = concat3 (val, SYMBOL_NAME (QCscript),
4955 concat2 (equalstr, SYMBOL_NAME (XCDR (elt))));
4956 else if (EQ (XCAR (elt), QClang)
4957 && SYMBOLP (XCDR (elt)))
4958 val = concat3 (val, SYMBOL_NAME (QClang),
4959 concat2 (equalstr, SYMBOL_NAME (XCDR (elt))));
4960 else if (EQ (XCAR (elt), QCotf)
4961 && CONSP (XCDR (elt)) && SYMBOLP (XCAR (XCDR (elt))))
4962 val = concat3 (val, SYMBOL_NAME (QCotf),
4963 concat2 (equalstr,
4964 SYMBOL_NAME (XCAR (XCDR (elt)))));
4966 arg = val;
4969 if (CONSP (result)
4970 && VECTORP (XCAR (result))
4971 && ASIZE (XCAR (result)) > 0
4972 && FONTP (AREF (XCAR (result), 0)))
4973 result = font_vconcat_entity_vectors (result);
4974 if (FONTP (result))
4976 val = Ffont_xlfd_name (result, Qt);
4977 if (! FONT_SPEC_P (result))
4978 val = concat3 (SYMBOL_NAME (AREF (result, FONT_TYPE_INDEX)),
4979 build_string (":"), val);
4980 result = val;
4982 else if (CONSP (result))
4984 Lisp_Object tail;
4985 result = Fcopy_sequence (result);
4986 for (tail = result; CONSP (tail); tail = XCDR (tail))
4988 val = XCAR (tail);
4989 if (FONTP (val))
4990 val = Ffont_xlfd_name (val, Qt);
4991 XSETCAR (tail, val);
4994 else if (VECTORP (result))
4996 result = Fcopy_sequence (result);
4997 for (i = 0; i < ASIZE (result); i++)
4999 val = AREF (result, i);
5000 if (FONTP (val))
5001 val = Ffont_xlfd_name (val, Qt);
5002 ASET (result, i, val);
5005 Vfont_log = Fcons (list3 (intern (action), arg, result), Vfont_log);
5008 /* Record a font-related logging data to be added to Vfont_log when
5009 font_add_log is called next time. ACTION, ARG, RESULT are the same
5010 as font_add_log. */
5012 void
5013 font_deferred_log (const char *action, Lisp_Object arg, Lisp_Object result)
5015 if (EQ (Vfont_log, Qt))
5016 return;
5017 ASET (Vfont_log_deferred, 0, build_string (action));
5018 ASET (Vfont_log_deferred, 1, arg);
5019 ASET (Vfont_log_deferred, 2, result);
5022 void
5023 syms_of_font (void)
5025 sort_shift_bits[FONT_TYPE_INDEX] = 0;
5026 sort_shift_bits[FONT_SLANT_INDEX] = 2;
5027 sort_shift_bits[FONT_WEIGHT_INDEX] = 9;
5028 sort_shift_bits[FONT_SIZE_INDEX] = 16;
5029 sort_shift_bits[FONT_WIDTH_INDEX] = 23;
5030 /* Note that the other elements in sort_shift_bits are not used. */
5032 staticpro (&font_charset_alist);
5033 font_charset_alist = Qnil;
5035 DEFSYM (Qopentype, "opentype");
5037 DEFSYM (Qascii_0, "ascii-0");
5038 DEFSYM (Qiso8859_1, "iso8859-1");
5039 DEFSYM (Qiso10646_1, "iso10646-1");
5040 DEFSYM (Qunicode_bmp, "unicode-bmp");
5041 DEFSYM (Qunicode_sip, "unicode-sip");
5043 DEFSYM (QCf, "Cf");
5045 DEFSYM (QCotf, ":otf");
5046 DEFSYM (QClang, ":lang");
5047 DEFSYM (QCscript, ":script");
5048 DEFSYM (QCantialias, ":antialias");
5050 DEFSYM (QCfoundry, ":foundry");
5051 DEFSYM (QCadstyle, ":adstyle");
5052 DEFSYM (QCregistry, ":registry");
5053 DEFSYM (QCspacing, ":spacing");
5054 DEFSYM (QCdpi, ":dpi");
5055 DEFSYM (QCscalable, ":scalable");
5056 DEFSYM (QCavgwidth, ":avgwidth");
5057 DEFSYM (QCfont_entity, ":font-entity");
5058 DEFSYM (QCfc_unknown_spec, ":fc-unknown-spec");
5060 DEFSYM (Qc, "c");
5061 DEFSYM (Qm, "m");
5062 DEFSYM (Qp, "p");
5063 DEFSYM (Qd, "d");
5065 DEFSYM (Qja, "ja");
5066 DEFSYM (Qko, "ko");
5068 DEFSYM (QCuser_spec, "user-spec");
5070 staticpro (&scratch_font_spec);
5071 scratch_font_spec = Ffont_spec (0, NULL);
5072 staticpro (&scratch_font_prefer);
5073 scratch_font_prefer = Ffont_spec (0, NULL);
5075 staticpro (&Vfont_log_deferred);
5076 Vfont_log_deferred = Fmake_vector (make_number (3), Qnil);
5078 #if 0
5079 #ifdef HAVE_LIBOTF
5080 staticpro (&otf_list);
5081 otf_list = Qnil;
5082 #endif /* HAVE_LIBOTF */
5083 #endif /* 0 */
5085 defsubr (&Sfontp);
5086 defsubr (&Sfont_spec);
5087 defsubr (&Sfont_get);
5088 #ifdef HAVE_WINDOW_SYSTEM
5089 defsubr (&Sfont_face_attributes);
5090 #endif
5091 defsubr (&Sfont_put);
5092 defsubr (&Slist_fonts);
5093 defsubr (&Sfont_family_list);
5094 defsubr (&Sfind_font);
5095 defsubr (&Sfont_xlfd_name);
5096 defsubr (&Sclear_font_cache);
5097 defsubr (&Sfont_shape_gstring);
5098 defsubr (&Sfont_variation_glyphs);
5099 #if 0
5100 defsubr (&Sfont_drive_otf);
5101 defsubr (&Sfont_otf_alternates);
5102 #endif /* 0 */
5104 #ifdef FONT_DEBUG
5105 defsubr (&Sopen_font);
5106 defsubr (&Sclose_font);
5107 defsubr (&Squery_font);
5108 defsubr (&Sfont_get_glyphs);
5109 defsubr (&Sfont_match_p);
5110 defsubr (&Sfont_at);
5111 #if 0
5112 defsubr (&Sdraw_string);
5113 #endif
5114 #endif /* FONT_DEBUG */
5115 #ifdef HAVE_WINDOW_SYSTEM
5116 defsubr (&Sfont_info);
5117 #endif
5119 DEFVAR_LISP ("font-encoding-alist", Vfont_encoding_alist,
5120 doc: /*
5121 Alist of fontname patterns vs the corresponding encoding and repertory info.
5122 Each element looks like (REGEXP . (ENCODING . REPERTORY)),
5123 where ENCODING is a charset or a char-table,
5124 and REPERTORY is a charset, a char-table, or nil.
5126 If ENCODING and REPERTORY are the same, the element can have the form
5127 \(REGEXP . ENCODING).
5129 ENCODING is for converting a character to a glyph code of the font.
5130 If ENCODING is a charset, encoding a character by the charset gives
5131 the corresponding glyph code. If ENCODING is a char-table, looking up
5132 the table by a character gives the corresponding glyph code.
5134 REPERTORY specifies a repertory of characters supported by the font.
5135 If REPERTORY is a charset, all characters belonging to the charset are
5136 supported. If REPERTORY is a char-table, all characters who have a
5137 non-nil value in the table are supported. If REPERTORY is nil, Emacs
5138 gets the repertory information by an opened font and ENCODING. */);
5139 Vfont_encoding_alist = Qnil;
5141 /* FIXME: These 3 vars are not quite what they appear: setq on them
5142 won't have any effect other than disconnect them from the style
5143 table used by the font display code. So we make them read-only,
5144 to avoid this confusing situation. */
5146 DEFVAR_LISP_NOPRO ("font-weight-table", Vfont_weight_table,
5147 doc: /* Vector of valid font weight values.
5148 Each element has the form:
5149 [NUMERIC-VALUE SYMBOLIC-NAME ALIAS-NAME ...]
5150 NUMERIC-VALUE is an integer, and SYMBOLIC-NAME and ALIAS-NAME are symbols. */);
5151 Vfont_weight_table = BUILD_STYLE_TABLE (weight_table);
5152 XSYMBOL (intern_c_string ("font-weight-table"))->constant = 1;
5154 DEFVAR_LISP_NOPRO ("font-slant-table", Vfont_slant_table,
5155 doc: /* Vector of font slant symbols vs the corresponding numeric values.
5156 See `font-weight-table' for the format of the vector. */);
5157 Vfont_slant_table = BUILD_STYLE_TABLE (slant_table);
5158 XSYMBOL (intern_c_string ("font-slant-table"))->constant = 1;
5160 DEFVAR_LISP_NOPRO ("font-width-table", Vfont_width_table,
5161 doc: /* Alist of font width symbols vs the corresponding numeric values.
5162 See `font-weight-table' for the format of the vector. */);
5163 Vfont_width_table = BUILD_STYLE_TABLE (width_table);
5164 XSYMBOL (intern_c_string ("font-width-table"))->constant = 1;
5166 staticpro (&font_style_table);
5167 font_style_table = make_uninit_vector (3);
5168 ASET (font_style_table, 0, Vfont_weight_table);
5169 ASET (font_style_table, 1, Vfont_slant_table);
5170 ASET (font_style_table, 2, Vfont_width_table);
5172 DEFVAR_LISP ("font-log", Vfont_log, doc: /*
5173 *Logging list of font related actions and results.
5174 The value t means to suppress the logging.
5175 The initial value is set to nil if the environment variable
5176 EMACS_FONT_LOG is set. Otherwise, it is set to t. */);
5177 Vfont_log = Qnil;
5179 #ifdef HAVE_WINDOW_SYSTEM
5180 #ifdef HAVE_FREETYPE
5181 syms_of_ftfont ();
5182 #ifdef HAVE_X_WINDOWS
5183 syms_of_xfont ();
5184 syms_of_ftxfont ();
5185 #ifdef HAVE_XFT
5186 syms_of_xftfont ();
5187 #endif /* HAVE_XFT */
5188 #endif /* HAVE_X_WINDOWS */
5189 #else /* not HAVE_FREETYPE */
5190 #ifdef HAVE_X_WINDOWS
5191 syms_of_xfont ();
5192 #endif /* HAVE_X_WINDOWS */
5193 #endif /* not HAVE_FREETYPE */
5194 #ifdef HAVE_BDFFONT
5195 syms_of_bdffont ();
5196 #endif /* HAVE_BDFFONT */
5197 #ifdef HAVE_NTGUI
5198 syms_of_w32font ();
5199 #endif /* HAVE_NTGUI */
5200 #endif /* HAVE_WINDOW_SYSTEM */
5203 void
5204 init_font (void)
5206 Vfont_log = egetenv ("EMACS_FONT_LOG") ? Qnil : Qt;