Update copyright year to 2015
[emacs.git] / src / font.c
blobf07fbe3bb115c460775c420b47b6511527733efa
1 /* font.c -- "Font" primitives.
3 Copyright (C) 2006-2015 Free Software Foundation, Inc.
4 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H13PRO009
8 This file is part of GNU Emacs.
10 GNU Emacs is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 GNU Emacs is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
23 #include <config.h>
24 #include <float.h>
25 #include <stdio.h>
27 #include <c-ctype.h>
29 #include "lisp.h"
30 #include "character.h"
31 #include "buffer.h"
32 #include "frame.h"
33 #include "window.h"
34 #include "dispextern.h"
35 #include "charset.h"
36 #include "composite.h"
37 #include "fontset.h"
38 #include "font.h"
40 #ifdef HAVE_WINDOW_SYSTEM
41 #include TERM_HEADER
42 #endif /* HAVE_WINDOW_SYSTEM */
44 Lisp_Object Qopentype;
46 /* Important character set strings. */
47 Lisp_Object Qascii_0, Qiso8859_1, Qiso10646_1, Qunicode_bmp, Qunicode_sip;
49 #define DEFAULT_ENCODING Qiso8859_1
51 /* Unicode category `Cf'. */
52 static Lisp_Object QCf;
54 /* Vector of Vfont_weight_table, Vfont_slant_table, and Vfont_width_table. */
55 static Lisp_Object font_style_table;
57 /* Structure used for tables mapping weight, slant, and width numeric
58 values and their names. */
60 struct table_entry
62 int numeric;
63 /* The first one is a valid name as a face attribute.
64 The second one (if any) is a typical name in XLFD field. */
65 const char *names[5];
68 /* Table of weight numeric values and their names. This table must be
69 sorted by numeric values in ascending order. */
71 static const struct table_entry weight_table[] =
73 { 0, { "thin" }},
74 { 20, { "ultra-light", "ultralight" }},
75 { 40, { "extra-light", "extralight" }},
76 { 50, { "light" }},
77 { 75, { "semi-light", "semilight", "demilight", "book" }},
78 { 100, { "normal", "medium", "regular", "unspecified" }},
79 { 180, { "semi-bold", "semibold", "demibold", "demi" }},
80 { 200, { "bold" }},
81 { 205, { "extra-bold", "extrabold" }},
82 { 210, { "ultra-bold", "ultrabold", "black" }}
85 /* Table of slant numeric values and their names. This table must be
86 sorted by numeric values in ascending order. */
88 static const struct table_entry slant_table[] =
90 { 0, { "reverse-oblique", "ro" }},
91 { 10, { "reverse-italic", "ri" }},
92 { 100, { "normal", "r", "unspecified" }},
93 { 200, { "italic" ,"i", "ot" }},
94 { 210, { "oblique", "o" }}
97 /* Table of width numeric values and their names. This table must be
98 sorted by numeric values in ascending order. */
100 static const struct table_entry width_table[] =
102 { 50, { "ultra-condensed", "ultracondensed" }},
103 { 63, { "extra-condensed", "extracondensed" }},
104 { 75, { "condensed", "compressed", "narrow" }},
105 { 87, { "semi-condensed", "semicondensed", "demicondensed" }},
106 { 100, { "normal", "medium", "regular", "unspecified" }},
107 { 113, { "semi-expanded", "semiexpanded", "demiexpanded" }},
108 { 125, { "expanded" }},
109 { 150, { "extra-expanded", "extraexpanded" }},
110 { 200, { "ultra-expanded", "ultraexpanded", "wide" }}
113 Lisp_Object QCfoundry;
114 static Lisp_Object QCadstyle, QCregistry;
115 /* Symbols representing keys of font extra info. */
116 Lisp_Object QCspacing, QCdpi, QCscalable, QCotf, QClang, QCscript, QCavgwidth;
117 Lisp_Object QCantialias, QCfont_entity;
118 static Lisp_Object QCfc_unknown_spec;
119 /* Symbols representing values of font spacing property. */
120 static Lisp_Object Qc, Qm, Qd;
121 Lisp_Object Qp;
122 /* Special ADSTYLE properties to avoid fonts used for Latin
123 characters; used in xfont.c and ftfont.c. */
124 Lisp_Object Qja, Qko;
126 static Lisp_Object QCuser_spec;
128 /* Alist of font registry symbols and the corresponding charset
129 information. The information is retrieved from
130 Vfont_encoding_alist on demand.
132 Eash element has the form:
133 (REGISTRY . (ENCODING-CHARSET-ID . REPERTORY-CHARSET-ID))
135 (REGISTRY . nil)
137 In the former form, ENCODING-CHARSET-ID is an ID of a charset that
138 encodes a character code to a glyph code of a font, and
139 REPERTORY-CHARSET-ID is an ID of a charset that tells if a
140 character is supported by a font.
142 The latter form means that the information for REGISTRY couldn't be
143 retrieved. */
144 static Lisp_Object font_charset_alist;
146 /* List of all font drivers. Each font-backend (XXXfont.c) calls
147 register_font_driver in syms_of_XXXfont to register its font-driver
148 here. */
149 static struct font_driver_list *font_driver_list;
151 #ifdef ENABLE_CHECKING
153 /* Used to catch bogus pointers in font objects. */
155 bool
156 valid_font_driver (struct font_driver *drv)
158 Lisp_Object tail, frame;
159 struct font_driver_list *fdl;
161 for (fdl = font_driver_list; fdl; fdl = fdl->next)
162 if (fdl->driver == drv)
163 return true;
164 FOR_EACH_FRAME (tail, frame)
165 for (fdl = XFRAME (frame)->font_driver_list; fdl; fdl = fdl->next)
166 if (fdl->driver == drv)
167 return true;
168 return false;
171 #endif /* ENABLE_CHECKING */
173 /* Creators of font-related Lisp object. */
175 static Lisp_Object
176 font_make_spec (void)
178 Lisp_Object font_spec;
179 struct font_spec *spec
180 = ((struct font_spec *)
181 allocate_pseudovector (VECSIZE (struct font_spec),
182 FONT_SPEC_MAX, PVEC_FONT));
183 XSETFONT (font_spec, spec);
184 return font_spec;
187 Lisp_Object
188 font_make_entity (void)
190 Lisp_Object font_entity;
191 struct font_entity *entity
192 = ((struct font_entity *)
193 allocate_pseudovector (VECSIZE (struct font_entity),
194 FONT_ENTITY_MAX, PVEC_FONT));
195 XSETFONT (font_entity, entity);
196 return font_entity;
199 /* Create a font-object whose structure size is SIZE. If ENTITY is
200 not nil, copy properties from ENTITY to the font-object. If
201 PIXELSIZE is positive, set the `size' property to PIXELSIZE. */
202 Lisp_Object
203 font_make_object (int size, Lisp_Object entity, int pixelsize)
205 Lisp_Object font_object;
206 struct font *font
207 = (struct font *) allocate_pseudovector (size, FONT_OBJECT_MAX, PVEC_FONT);
208 int i;
210 /* GC can happen before the driver is set up,
211 so avoid dangling pointer here (Bug#17771). */
212 font->driver = NULL;
213 XSETFONT (font_object, font);
215 if (! NILP (entity))
217 for (i = 1; i < FONT_SPEC_MAX; i++)
218 font->props[i] = AREF (entity, i);
219 if (! NILP (AREF (entity, FONT_EXTRA_INDEX)))
220 font->props[FONT_EXTRA_INDEX]
221 = Fcopy_alist (AREF (entity, FONT_EXTRA_INDEX));
223 if (size > 0)
224 font->props[FONT_SIZE_INDEX] = make_number (pixelsize);
225 return font_object;
230 static int font_pixel_size (struct frame *f, Lisp_Object);
231 static Lisp_Object font_open_entity (struct frame *, Lisp_Object, int);
232 static Lisp_Object font_matching_entity (struct frame *, Lisp_Object *,
233 Lisp_Object);
234 static unsigned font_encode_char (Lisp_Object, int);
236 /* Number of registered font drivers. */
237 static int num_font_drivers;
240 /* Return a Lispy value of a font property value at STR and LEN bytes.
241 If STR is "*", return nil. If FORCE_SYMBOL, or if STR does not
242 consist entirely of one or more digits, return a symbol interned
243 from STR. Otherwise, return an integer. */
245 Lisp_Object
246 font_intern_prop (const char *str, ptrdiff_t len, bool force_symbol)
248 ptrdiff_t i;
249 Lisp_Object tem;
250 Lisp_Object obarray;
251 ptrdiff_t nbytes, nchars;
253 if (len == 1 && *str == '*')
254 return Qnil;
255 if (!force_symbol && 0 < len && '0' <= *str && *str <= '9')
257 for (i = 1; i < len; i++)
258 if (! ('0' <= str[i] && str[i] <= '9'))
259 break;
260 if (i == len)
262 EMACS_INT n;
264 i = 0;
265 for (n = 0; (n += str[i++] - '0') <= MOST_POSITIVE_FIXNUM; n *= 10)
267 if (i == len)
268 return make_number (n);
269 if (MOST_POSITIVE_FIXNUM / 10 < n)
270 break;
273 xsignal1 (Qoverflow_error, make_string (str, len));
277 /* This code is similar to intern function from lread.c. */
278 obarray = check_obarray (Vobarray);
279 parse_str_as_multibyte ((unsigned char *) str, len, &nchars, &nbytes);
280 tem = oblookup (obarray, str,
281 (len == nchars || len != nbytes) ? len : nchars, len);
283 if (SYMBOLP (tem))
284 return tem;
285 if (len == nchars || len != nbytes)
286 tem = make_unibyte_string (str, len);
287 else
288 tem = make_multibyte_string (str, nchars, len);
289 return Fintern (tem, obarray);
292 /* Return a pixel size of font-spec SPEC on frame F. */
294 static int
295 font_pixel_size (struct frame *f, Lisp_Object spec)
297 #ifdef HAVE_WINDOW_SYSTEM
298 Lisp_Object size = AREF (spec, FONT_SIZE_INDEX);
299 double point_size;
300 int dpi, pixel_size;
301 Lisp_Object val;
303 if (INTEGERP (size))
304 return XINT (size);
305 if (NILP (size))
306 return 0;
307 eassert (FLOATP (size));
308 point_size = XFLOAT_DATA (size);
309 val = AREF (spec, FONT_DPI_INDEX);
310 if (INTEGERP (val))
311 dpi = XINT (val);
312 else
313 dpi = FRAME_RES_Y (f);
314 pixel_size = POINT_TO_PIXEL (point_size, dpi);
315 return pixel_size;
316 #else
317 return 1;
318 #endif
322 /* Return a value of PROP's VAL (symbol or integer) to be stored in a
323 font vector. If VAL is not valid (i.e. not registered in
324 font_style_table), return -1 if NOERROR is zero, and return a
325 proper index if NOERROR is nonzero. In that case, register VAL in
326 font_style_table if VAL is a symbol, and return the closest index if
327 VAL is an integer. */
330 font_style_to_value (enum font_property_index prop, Lisp_Object val,
331 bool noerror)
333 Lisp_Object table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
334 int len;
336 CHECK_VECTOR (table);
337 len = ASIZE (table);
339 if (SYMBOLP (val))
341 int i, j;
342 char *s;
343 Lisp_Object args[2], elt;
345 /* At first try exact match. */
346 for (i = 0; i < len; i++)
348 CHECK_VECTOR (AREF (table, i));
349 for (j = 1; j < ASIZE (AREF (table, i)); j++)
350 if (EQ (val, AREF (AREF (table, i), j)))
352 CHECK_NUMBER (AREF (AREF (table, i), 0));
353 return ((XINT (AREF (AREF (table, i), 0)) << 8)
354 | (i << 4) | (j - 1));
357 /* Try also with case-folding match. */
358 s = SSDATA (SYMBOL_NAME (val));
359 for (i = 0; i < len; i++)
360 for (j = 1; j < ASIZE (AREF (table, i)); j++)
362 elt = AREF (AREF (table, i), j);
363 if (xstrcasecmp (s, SSDATA (SYMBOL_NAME (elt))) == 0)
365 CHECK_NUMBER (AREF (AREF (table, i), 0));
366 return ((XINT (AREF (AREF (table, i), 0)) << 8)
367 | (i << 4) | (j - 1));
370 if (! noerror)
371 return -1;
372 eassert (len < 255);
373 elt = Fmake_vector (make_number (2), make_number (100));
374 ASET (elt, 1, val);
375 args[0] = table;
376 args[1] = Fmake_vector (make_number (1), elt);
377 ASET (font_style_table, prop - FONT_WEIGHT_INDEX, Fvconcat (2, args));
378 return (100 << 8) | (i << 4);
380 else
382 int i, last_n;
383 EMACS_INT numeric = XINT (val);
385 for (i = 0, last_n = -1; i < len; i++)
387 int n;
389 CHECK_VECTOR (AREF (table, i));
390 CHECK_NUMBER (AREF (AREF (table, i), 0));
391 n = XINT (AREF (AREF (table, i), 0));
392 if (numeric == n)
393 return (n << 8) | (i << 4);
394 if (numeric < n)
396 if (! noerror)
397 return -1;
398 return ((i == 0 || n - numeric < numeric - last_n)
399 ? (n << 8) | (i << 4): (last_n << 8 | ((i - 1) << 4)));
401 last_n = n;
403 if (! noerror)
404 return -1;
405 return ((last_n << 8) | ((i - 1) << 4));
409 Lisp_Object
410 font_style_symbolic (Lisp_Object font, enum font_property_index prop,
411 bool for_face)
413 Lisp_Object val = AREF (font, prop);
414 Lisp_Object table, elt;
415 int i;
417 if (NILP (val))
418 return Qnil;
419 table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
420 CHECK_VECTOR (table);
421 i = XINT (val) & 0xFF;
422 eassert (((i >> 4) & 0xF) < ASIZE (table));
423 elt = AREF (table, ((i >> 4) & 0xF));
424 CHECK_VECTOR (elt);
425 eassert ((i & 0xF) + 1 < ASIZE (elt));
426 elt = (for_face ? AREF (elt, 1) : AREF (elt, (i & 0xF) + 1));
427 CHECK_SYMBOL (elt);
428 return elt;
431 /* Return ENCODING or a cons of ENCODING and REPERTORY of the font
432 FONTNAME. ENCODING is a charset symbol that specifies the encoding
433 of the font. REPERTORY is a charset symbol or nil. */
435 Lisp_Object
436 find_font_encoding (Lisp_Object fontname)
438 Lisp_Object tail, elt;
440 for (tail = Vfont_encoding_alist; CONSP (tail); tail = XCDR (tail))
442 elt = XCAR (tail);
443 if (CONSP (elt)
444 && STRINGP (XCAR (elt))
445 && fast_string_match_ignore_case (XCAR (elt), fontname) >= 0
446 && (SYMBOLP (XCDR (elt))
447 ? CHARSETP (XCDR (elt))
448 : CONSP (XCDR (elt)) && CHARSETP (XCAR (XCDR (elt)))))
449 return (XCDR (elt));
451 return Qnil;
454 /* Return encoding charset and repertory charset for REGISTRY in
455 ENCODING and REPERTORY correspondingly. If correct information for
456 REGISTRY is available, return 0. Otherwise return -1. */
459 font_registry_charsets (Lisp_Object registry, struct charset **encoding, struct charset **repertory)
461 Lisp_Object val;
462 int encoding_id, repertory_id;
464 val = Fassoc_string (registry, font_charset_alist, Qt);
465 if (! NILP (val))
467 val = XCDR (val);
468 if (NILP (val))
469 return -1;
470 encoding_id = XINT (XCAR (val));
471 repertory_id = XINT (XCDR (val));
473 else
475 val = find_font_encoding (SYMBOL_NAME (registry));
476 if (SYMBOLP (val) && CHARSETP (val))
478 encoding_id = repertory_id = XINT (CHARSET_SYMBOL_ID (val));
480 else if (CONSP (val))
482 if (! CHARSETP (XCAR (val)))
483 goto invalid_entry;
484 encoding_id = XINT (CHARSET_SYMBOL_ID (XCAR (val)));
485 if (NILP (XCDR (val)))
486 repertory_id = -1;
487 else
489 if (! CHARSETP (XCDR (val)))
490 goto invalid_entry;
491 repertory_id = XINT (CHARSET_SYMBOL_ID (XCDR (val)));
494 else
495 goto invalid_entry;
496 val = Fcons (make_number (encoding_id), make_number (repertory_id));
497 font_charset_alist
498 = nconc2 (font_charset_alist, list1 (Fcons (registry, val)));
501 if (encoding)
502 *encoding = CHARSET_FROM_ID (encoding_id);
503 if (repertory)
504 *repertory = repertory_id >= 0 ? CHARSET_FROM_ID (repertory_id) : NULL;
505 return 0;
507 invalid_entry:
508 font_charset_alist
509 = nconc2 (font_charset_alist, list1 (Fcons (registry, Qnil)));
510 return -1;
514 /* Font property value validators. See the comment of
515 font_property_table for the meaning of the arguments. */
517 static Lisp_Object font_prop_validate (int, Lisp_Object, Lisp_Object);
518 static Lisp_Object font_prop_validate_symbol (Lisp_Object, Lisp_Object);
519 static Lisp_Object font_prop_validate_style (Lisp_Object, Lisp_Object);
520 static Lisp_Object font_prop_validate_non_neg (Lisp_Object, Lisp_Object);
521 static Lisp_Object font_prop_validate_spacing (Lisp_Object, Lisp_Object);
522 static int get_font_prop_index (Lisp_Object);
524 static Lisp_Object
525 font_prop_validate_symbol (Lisp_Object prop, Lisp_Object val)
527 if (STRINGP (val))
528 val = Fintern (val, Qnil);
529 if (! SYMBOLP (val))
530 val = Qerror;
531 else if (EQ (prop, QCregistry))
532 val = Fintern (Fdowncase (SYMBOL_NAME (val)), Qnil);
533 return val;
537 static Lisp_Object
538 font_prop_validate_style (Lisp_Object style, Lisp_Object val)
540 enum font_property_index prop = (EQ (style, QCweight) ? FONT_WEIGHT_INDEX
541 : EQ (style, QCslant) ? FONT_SLANT_INDEX
542 : FONT_WIDTH_INDEX);
543 if (INTEGERP (val))
545 EMACS_INT n = XINT (val);
546 CHECK_VECTOR (AREF (font_style_table, prop - FONT_WEIGHT_INDEX));
547 if (((n >> 4) & 0xF)
548 >= ASIZE (AREF (font_style_table, prop - FONT_WEIGHT_INDEX)))
549 val = Qerror;
550 else
552 Lisp_Object elt = AREF (AREF (font_style_table, prop - FONT_WEIGHT_INDEX), (n >> 4) & 0xF);
554 CHECK_VECTOR (elt);
555 if ((n & 0xF) + 1 >= ASIZE (elt))
556 val = Qerror;
557 else
559 CHECK_NUMBER (AREF (elt, 0));
560 if (XINT (AREF (elt, 0)) != (n >> 8))
561 val = Qerror;
565 else if (SYMBOLP (val))
567 int n = font_style_to_value (prop, val, 0);
569 val = n >= 0 ? make_number (n) : Qerror;
571 else
572 val = Qerror;
573 return val;
576 static Lisp_Object
577 font_prop_validate_non_neg (Lisp_Object prop, Lisp_Object val)
579 return (NATNUMP (val) || (FLOATP (val) && XFLOAT_DATA (val) >= 0)
580 ? val : Qerror);
583 static Lisp_Object
584 font_prop_validate_spacing (Lisp_Object prop, Lisp_Object val)
586 if (NILP (val) || (NATNUMP (val) && XINT (val) <= FONT_SPACING_CHARCELL))
587 return val;
588 if (SYMBOLP (val) && SBYTES (SYMBOL_NAME (val)) == 1)
590 char spacing = SDATA (SYMBOL_NAME (val))[0];
592 if (spacing == 'c' || spacing == 'C')
593 return make_number (FONT_SPACING_CHARCELL);
594 if (spacing == 'm' || spacing == 'M')
595 return make_number (FONT_SPACING_MONO);
596 if (spacing == 'p' || spacing == 'P')
597 return make_number (FONT_SPACING_PROPORTIONAL);
598 if (spacing == 'd' || spacing == 'D')
599 return make_number (FONT_SPACING_DUAL);
601 return Qerror;
604 static Lisp_Object
605 font_prop_validate_otf (Lisp_Object prop, Lisp_Object val)
607 Lisp_Object tail, tmp;
608 int i;
610 /* VAL = (SCRIPT [ LANGSYS [ GSUB-FEATURES [ GPOS-FEATURES ]]])
611 GSUB-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil
612 GPOS-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil */
613 if (! CONSP (val))
614 return Qerror;
615 if (! SYMBOLP (XCAR (val)))
616 return Qerror;
617 tail = XCDR (val);
618 if (NILP (tail))
619 return val;
620 if (! CONSP (tail) || ! SYMBOLP (XCAR (val)))
621 return Qerror;
622 for (i = 0; i < 2; i++)
624 tail = XCDR (tail);
625 if (NILP (tail))
626 return val;
627 if (! CONSP (tail))
628 return Qerror;
629 for (tmp = XCAR (tail); CONSP (tmp); tmp = XCDR (tmp))
630 if (! SYMBOLP (XCAR (tmp)))
631 return Qerror;
632 if (! NILP (tmp))
633 return Qerror;
635 return val;
638 /* Structure of known font property keys and validator of the
639 values. */
640 static const struct
642 /* Pointer to the key symbol. */
643 Lisp_Object *key;
644 /* Function to validate PROP's value VAL, or NULL if any value is
645 ok. The value is VAL or its regularized value if VAL is valid,
646 and Qerror if not. */
647 Lisp_Object (*validator) (Lisp_Object prop, Lisp_Object val);
648 } font_property_table[] =
649 { { &QCtype, font_prop_validate_symbol },
650 { &QCfoundry, font_prop_validate_symbol },
651 { &QCfamily, font_prop_validate_symbol },
652 { &QCadstyle, font_prop_validate_symbol },
653 { &QCregistry, font_prop_validate_symbol },
654 { &QCweight, font_prop_validate_style },
655 { &QCslant, font_prop_validate_style },
656 { &QCwidth, font_prop_validate_style },
657 { &QCsize, font_prop_validate_non_neg },
658 { &QCdpi, font_prop_validate_non_neg },
659 { &QCspacing, font_prop_validate_spacing },
660 { &QCavgwidth, font_prop_validate_non_neg },
661 /* The order of the above entries must match with enum
662 font_property_index. */
663 { &QClang, font_prop_validate_symbol },
664 { &QCscript, font_prop_validate_symbol },
665 { &QCotf, font_prop_validate_otf }
668 /* Size (number of elements) of the above table. */
669 #define FONT_PROPERTY_TABLE_SIZE \
670 ((sizeof font_property_table) / (sizeof *font_property_table))
672 /* Return an index number of font property KEY or -1 if KEY is not an
673 already known property. */
675 static int
676 get_font_prop_index (Lisp_Object key)
678 int i;
680 for (i = 0; i < FONT_PROPERTY_TABLE_SIZE; i++)
681 if (EQ (key, *font_property_table[i].key))
682 return i;
683 return -1;
686 /* Validate the font property. The property key is specified by the
687 symbol PROP, or the index IDX (if PROP is nil). If VAL is invalid,
688 signal an error. The value is VAL or the regularized one. */
690 static Lisp_Object
691 font_prop_validate (int idx, Lisp_Object prop, Lisp_Object val)
693 Lisp_Object validated;
695 if (NILP (val))
696 return val;
697 if (NILP (prop))
698 prop = *font_property_table[idx].key;
699 else
701 idx = get_font_prop_index (prop);
702 if (idx < 0)
703 return val;
705 validated = (font_property_table[idx].validator) (prop, val);
706 if (EQ (validated, Qerror))
707 signal_error ("invalid font property", Fcons (prop, val));
708 return validated;
712 /* Store VAL as a value of extra font property PROP in FONT while
713 keeping the sorting order. Don't check the validity of VAL. */
715 Lisp_Object
716 font_put_extra (Lisp_Object font, Lisp_Object prop, Lisp_Object val)
718 Lisp_Object extra = AREF (font, FONT_EXTRA_INDEX);
719 Lisp_Object slot = (NILP (extra) ? Qnil : assq_no_quit (prop, extra));
721 if (NILP (slot))
723 Lisp_Object prev = Qnil;
725 while (CONSP (extra)
726 && NILP (Fstring_lessp (prop, XCAR (XCAR (extra)))))
727 prev = extra, extra = XCDR (extra);
729 if (NILP (prev))
730 ASET (font, FONT_EXTRA_INDEX, Fcons (Fcons (prop, val), extra));
731 else
732 XSETCDR (prev, Fcons (Fcons (prop, val), extra));
734 return val;
736 XSETCDR (slot, val);
737 if (NILP (val))
738 ASET (font, FONT_EXTRA_INDEX, Fdelq (slot, extra));
739 return val;
743 /* Font name parser and unparser. */
745 static int parse_matrix (const char *);
746 static int font_expand_wildcards (Lisp_Object *, int);
747 static int font_parse_name (char *, ptrdiff_t, Lisp_Object);
749 /* An enumerator for each field of an XLFD font name. */
750 enum xlfd_field_index
752 XLFD_FOUNDRY_INDEX,
753 XLFD_FAMILY_INDEX,
754 XLFD_WEIGHT_INDEX,
755 XLFD_SLANT_INDEX,
756 XLFD_SWIDTH_INDEX,
757 XLFD_ADSTYLE_INDEX,
758 XLFD_PIXEL_INDEX,
759 XLFD_POINT_INDEX,
760 XLFD_RESX_INDEX,
761 XLFD_RESY_INDEX,
762 XLFD_SPACING_INDEX,
763 XLFD_AVGWIDTH_INDEX,
764 XLFD_REGISTRY_INDEX,
765 XLFD_ENCODING_INDEX,
766 XLFD_LAST_INDEX
769 /* An enumerator for mask bit corresponding to each XLFD field. */
770 enum xlfd_field_mask
772 XLFD_FOUNDRY_MASK = 0x0001,
773 XLFD_FAMILY_MASK = 0x0002,
774 XLFD_WEIGHT_MASK = 0x0004,
775 XLFD_SLANT_MASK = 0x0008,
776 XLFD_SWIDTH_MASK = 0x0010,
777 XLFD_ADSTYLE_MASK = 0x0020,
778 XLFD_PIXEL_MASK = 0x0040,
779 XLFD_POINT_MASK = 0x0080,
780 XLFD_RESX_MASK = 0x0100,
781 XLFD_RESY_MASK = 0x0200,
782 XLFD_SPACING_MASK = 0x0400,
783 XLFD_AVGWIDTH_MASK = 0x0800,
784 XLFD_REGISTRY_MASK = 0x1000,
785 XLFD_ENCODING_MASK = 0x2000
789 /* Parse P pointing to the pixel/point size field of the form
790 `[A B C D]' which specifies a transformation matrix:
792 A B 0
793 C D 0
794 0 0 1
796 by which all glyphs of the font are transformed. The spec says
797 that scalar value N for the pixel/point size is equivalent to:
798 A = N * resx/resy, B = C = 0, D = N.
800 Return the scalar value N if the form is valid. Otherwise return
801 -1. */
803 static int
804 parse_matrix (const char *p)
806 double matrix[4];
807 char *end;
808 int i;
810 for (i = 0, p++; i < 4 && *p && *p != ']'; i++)
812 if (*p == '~')
813 matrix[i] = - strtod (p + 1, &end);
814 else
815 matrix[i] = strtod (p, &end);
816 p = end;
818 return (i == 4 ? (int) matrix[3] : -1);
821 /* Expand a wildcard field in FIELD (the first N fields are filled) to
822 multiple fields to fill in all 14 XLFD fields while restricting a
823 field position by its contents. */
825 static int
826 font_expand_wildcards (Lisp_Object *field, int n)
828 /* Copy of FIELD. */
829 Lisp_Object tmp[XLFD_LAST_INDEX];
830 /* Array of information about where this element can go. Nth
831 element is for Nth element of FIELD. */
832 struct {
833 /* Minimum possible field. */
834 int from;
835 /* Maximum possible field. */
836 int to;
837 /* Bit mask of possible field. Nth bit corresponds to Nth field. */
838 int mask;
839 } range[XLFD_LAST_INDEX];
840 int i, j;
841 int range_from, range_to;
842 unsigned range_mask;
844 #define XLFD_SYMBOL_MASK (XLFD_FOUNDRY_MASK | XLFD_FAMILY_MASK \
845 | XLFD_ADSTYLE_MASK | XLFD_REGISTRY_MASK)
846 #define XLFD_NULL_MASK (XLFD_FOUNDRY_MASK | XLFD_ADSTYLE_MASK)
847 #define XLFD_LARGENUM_MASK (XLFD_POINT_MASK | XLFD_RESX_MASK | XLFD_RESY_MASK \
848 | XLFD_AVGWIDTH_MASK)
849 #define XLFD_REGENC_MASK (XLFD_REGISTRY_MASK | XLFD_ENCODING_MASK)
851 /* Initialize RANGE_MASK for FIELD[0] which can be 0th to (14 - N)th
852 field. The value is shifted to left one bit by one in the
853 following loop. */
854 for (i = 0, range_mask = 0; i <= 14 - n; i++)
855 range_mask = (range_mask << 1) | 1;
857 /* The triplet RANGE_FROM, RANGE_TO, and RANGE_MASK is a
858 position-based restriction for FIELD[I]. */
859 for (i = 0, range_from = 0, range_to = 14 - n; i < n;
860 i++, range_from++, range_to++, range_mask <<= 1)
862 Lisp_Object val = field[i];
864 tmp[i] = val;
865 if (NILP (val))
867 /* Wildcard. */
868 range[i].from = range_from;
869 range[i].to = range_to;
870 range[i].mask = range_mask;
872 else
874 /* The triplet FROM, TO, and MASK is a value-based
875 restriction for FIELD[I]. */
876 int from, to;
877 unsigned mask;
879 if (INTEGERP (val))
881 EMACS_INT numeric = XINT (val);
883 if (i + 1 == n)
884 from = to = XLFD_ENCODING_INDEX,
885 mask = XLFD_ENCODING_MASK;
886 else if (numeric == 0)
887 from = XLFD_PIXEL_INDEX, to = XLFD_AVGWIDTH_INDEX,
888 mask = XLFD_PIXEL_MASK | XLFD_LARGENUM_MASK;
889 else if (numeric <= 48)
890 from = to = XLFD_PIXEL_INDEX,
891 mask = XLFD_PIXEL_MASK;
892 else
893 from = XLFD_POINT_INDEX, to = XLFD_AVGWIDTH_INDEX,
894 mask = XLFD_LARGENUM_MASK;
896 else if (SBYTES (SYMBOL_NAME (val)) == 0)
897 from = XLFD_FOUNDRY_INDEX, to = XLFD_ADSTYLE_INDEX,
898 mask = XLFD_NULL_MASK;
899 else if (i == 0)
900 from = to = XLFD_FOUNDRY_INDEX, mask = XLFD_FOUNDRY_MASK;
901 else if (i + 1 == n)
903 Lisp_Object name = SYMBOL_NAME (val);
905 if (SDATA (name)[SBYTES (name) - 1] == '*')
906 from = XLFD_REGISTRY_INDEX, to = XLFD_ENCODING_INDEX,
907 mask = XLFD_REGENC_MASK;
908 else
909 from = to = XLFD_ENCODING_INDEX,
910 mask = XLFD_ENCODING_MASK;
912 else if (range_from <= XLFD_WEIGHT_INDEX
913 && range_to >= XLFD_WEIGHT_INDEX
914 && FONT_WEIGHT_NAME_NUMERIC (val) >= 0)
915 from = to = XLFD_WEIGHT_INDEX, mask = XLFD_WEIGHT_MASK;
916 else if (range_from <= XLFD_SLANT_INDEX
917 && range_to >= XLFD_SLANT_INDEX
918 && FONT_SLANT_NAME_NUMERIC (val) >= 0)
919 from = to = XLFD_SLANT_INDEX, mask = XLFD_SLANT_MASK;
920 else if (range_from <= XLFD_SWIDTH_INDEX
921 && range_to >= XLFD_SWIDTH_INDEX
922 && FONT_WIDTH_NAME_NUMERIC (val) >= 0)
923 from = to = XLFD_SWIDTH_INDEX, mask = XLFD_SWIDTH_MASK;
924 else
926 if (EQ (val, Qc) || EQ (val, Qm) || EQ (val, Qp) || EQ (val, Qd))
927 from = to = XLFD_SPACING_INDEX, mask = XLFD_SPACING_MASK;
928 else
929 from = XLFD_FOUNDRY_INDEX, to = XLFD_ENCODING_INDEX,
930 mask = XLFD_SYMBOL_MASK;
933 /* Merge position-based and value-based restrictions. */
934 mask &= range_mask;
935 while (from < range_from)
936 mask &= ~(1 << from++);
937 while (from < 14 && ! (mask & (1 << from)))
938 from++;
939 while (to > range_to)
940 mask &= ~(1 << to--);
941 while (to >= 0 && ! (mask & (1 << to)))
942 to--;
943 if (from > to)
944 return -1;
945 range[i].from = from;
946 range[i].to = to;
947 range[i].mask = mask;
949 if (from > range_from || to < range_to)
951 /* The range is narrowed by value-based restrictions.
952 Reflect it to the other fields. */
954 /* Following fields should be after FROM. */
955 range_from = from;
956 /* Preceding fields should be before TO. */
957 for (j = i - 1, from--, to--; j >= 0; j--, from--, to--)
959 /* Check FROM for non-wildcard field. */
960 if (! NILP (tmp[j]) && range[j].from < from)
962 while (range[j].from < from)
963 range[j].mask &= ~(1 << range[j].from++);
964 while (from < 14 && ! (range[j].mask & (1 << from)))
965 from++;
966 range[j].from = from;
968 else
969 from = range[j].from;
970 if (range[j].to > to)
972 while (range[j].to > to)
973 range[j].mask &= ~(1 << range[j].to--);
974 while (to >= 0 && ! (range[j].mask & (1 << to)))
975 to--;
976 range[j].to = to;
978 else
979 to = range[j].to;
980 if (from > to)
981 return -1;
987 /* Decide all fields from restrictions in RANGE. */
988 for (i = j = 0; i < n ; i++)
990 if (j < range[i].from)
992 if (i == 0 || ! NILP (tmp[i - 1]))
993 /* None of TMP[X] corresponds to Jth field. */
994 return -1;
995 for (; j < range[i].from; j++)
996 field[j] = Qnil;
998 field[j++] = tmp[i];
1000 if (! NILP (tmp[n - 1]) && j < XLFD_REGISTRY_INDEX)
1001 return -1;
1002 for (; j < XLFD_LAST_INDEX; j++)
1003 field[j] = Qnil;
1004 if (INTEGERP (field[XLFD_ENCODING_INDEX]))
1005 field[XLFD_ENCODING_INDEX]
1006 = Fintern (Fnumber_to_string (field[XLFD_ENCODING_INDEX]), Qnil);
1007 return 0;
1011 /* Parse NAME (null terminated) as XLFD and store information in FONT
1012 (font-spec or font-entity). Size property of FONT is set as
1013 follows:
1014 specified XLFD fields FONT property
1015 --------------------- -------------
1016 PIXEL_SIZE PIXEL_SIZE (Lisp integer)
1017 POINT_SIZE and RESY calculated pixel size (Lisp integer)
1018 POINT_SIZE POINT_SIZE/10 (Lisp float)
1020 If NAME is successfully parsed, return 0. Otherwise return -1.
1022 FONT is usually a font-spec, but when this function is called from
1023 X font backend driver, it is a font-entity. In that case, NAME is
1024 a fully specified XLFD. */
1027 font_parse_xlfd (char *name, ptrdiff_t len, Lisp_Object font)
1029 int i, j, n;
1030 char *f[XLFD_LAST_INDEX + 1];
1031 Lisp_Object val;
1032 char *p;
1034 if (len > 255 || !len)
1035 /* Maximum XLFD name length is 255. */
1036 return -1;
1037 /* Accept "*-.." as a fully specified XLFD. */
1038 if (name[0] == '*' && (len == 1 || name[1] == '-'))
1039 i = 1, f[XLFD_FOUNDRY_INDEX] = name;
1040 else
1041 i = 0;
1042 for (p = name + i; *p; p++)
1043 if (*p == '-')
1045 f[i++] = p + 1;
1046 if (i == XLFD_LAST_INDEX)
1047 break;
1049 f[i] = name + len;
1051 #define INTERN_FIELD(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 0)
1052 #define INTERN_FIELD_SYM(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 1)
1054 if (i == XLFD_LAST_INDEX)
1056 /* Fully specified XLFD. */
1057 int pixel_size;
1059 ASET (font, FONT_FOUNDRY_INDEX, INTERN_FIELD_SYM (XLFD_FOUNDRY_INDEX));
1060 ASET (font, FONT_FAMILY_INDEX, INTERN_FIELD_SYM (XLFD_FAMILY_INDEX));
1061 for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
1062 i <= XLFD_SWIDTH_INDEX; i++, j++)
1064 val = INTERN_FIELD_SYM (i);
1065 if (! NILP (val))
1067 if ((n = font_style_to_value (j, INTERN_FIELD_SYM (i), 0)) < 0)
1068 return -1;
1069 ASET (font, j, make_number (n));
1072 ASET (font, FONT_ADSTYLE_INDEX, INTERN_FIELD_SYM (XLFD_ADSTYLE_INDEX));
1073 if (strcmp (f[XLFD_REGISTRY_INDEX], "*-*") == 0)
1074 ASET (font, FONT_REGISTRY_INDEX, Qnil);
1075 else
1076 ASET (font, FONT_REGISTRY_INDEX,
1077 font_intern_prop (f[XLFD_REGISTRY_INDEX],
1078 f[XLFD_LAST_INDEX] - f[XLFD_REGISTRY_INDEX],
1079 1));
1080 p = f[XLFD_PIXEL_INDEX];
1081 if (*p == '[' && (pixel_size = parse_matrix (p)) >= 0)
1082 ASET (font, FONT_SIZE_INDEX, make_number (pixel_size));
1083 else
1085 val = INTERN_FIELD (XLFD_PIXEL_INDEX);
1086 if (INTEGERP (val))
1087 ASET (font, FONT_SIZE_INDEX, val);
1088 else if (FONT_ENTITY_P (font))
1089 return -1;
1090 else
1092 double point_size = -1;
1094 eassert (FONT_SPEC_P (font));
1095 p = f[XLFD_POINT_INDEX];
1096 if (*p == '[')
1097 point_size = parse_matrix (p);
1098 else if (c_isdigit (*p))
1099 point_size = atoi (p), point_size /= 10;
1100 if (point_size >= 0)
1101 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1105 val = INTERN_FIELD (XLFD_RESY_INDEX);
1106 if (! NILP (val) && ! INTEGERP (val))
1107 return -1;
1108 ASET (font, FONT_DPI_INDEX, val);
1109 val = INTERN_FIELD (XLFD_SPACING_INDEX);
1110 if (! NILP (val))
1112 val = font_prop_validate_spacing (QCspacing, val);
1113 if (! INTEGERP (val))
1114 return -1;
1115 ASET (font, FONT_SPACING_INDEX, val);
1117 p = f[XLFD_AVGWIDTH_INDEX];
1118 if (*p == '~')
1119 p++;
1120 val = font_intern_prop (p, f[XLFD_REGISTRY_INDEX] - 1 - p, 0);
1121 if (! NILP (val) && ! INTEGERP (val))
1122 return -1;
1123 ASET (font, FONT_AVGWIDTH_INDEX, val);
1125 else
1127 bool wild_card_found = 0;
1128 Lisp_Object prop[XLFD_LAST_INDEX];
1130 if (FONT_ENTITY_P (font))
1131 return -1;
1132 for (j = 0; j < i; j++)
1134 if (*f[j] == '*')
1136 if (f[j][1] && f[j][1] != '-')
1137 return -1;
1138 prop[j] = Qnil;
1139 wild_card_found = 1;
1141 else if (j + 1 < i)
1142 prop[j] = INTERN_FIELD (j);
1143 else
1144 prop[j] = font_intern_prop (f[j], f[i] - f[j], 0);
1146 if (! wild_card_found)
1147 return -1;
1148 if (font_expand_wildcards (prop, i) < 0)
1149 return -1;
1151 ASET (font, FONT_FOUNDRY_INDEX, prop[XLFD_FOUNDRY_INDEX]);
1152 ASET (font, FONT_FAMILY_INDEX, prop[XLFD_FAMILY_INDEX]);
1153 for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
1154 i <= XLFD_SWIDTH_INDEX; i++, j++)
1155 if (! NILP (prop[i]))
1157 if ((n = font_style_to_value (j, prop[i], 1)) < 0)
1158 return -1;
1159 ASET (font, j, make_number (n));
1161 ASET (font, FONT_ADSTYLE_INDEX, prop[XLFD_ADSTYLE_INDEX]);
1162 val = prop[XLFD_REGISTRY_INDEX];
1163 if (NILP (val))
1165 val = prop[XLFD_ENCODING_INDEX];
1166 if (! NILP (val))
1167 val = concat2 (build_string ("*-"), SYMBOL_NAME (val));
1169 else if (NILP (prop[XLFD_ENCODING_INDEX]))
1170 val = concat2 (SYMBOL_NAME (val), build_string ("-*"));
1171 else
1172 val = concat3 (SYMBOL_NAME (val), build_string ("-"),
1173 SYMBOL_NAME (prop[XLFD_ENCODING_INDEX]));
1174 if (! NILP (val))
1175 ASET (font, FONT_REGISTRY_INDEX, Fintern (val, Qnil));
1177 if (INTEGERP (prop[XLFD_PIXEL_INDEX]))
1178 ASET (font, FONT_SIZE_INDEX, prop[XLFD_PIXEL_INDEX]);
1179 else if (INTEGERP (prop[XLFD_POINT_INDEX]))
1181 double point_size = XINT (prop[XLFD_POINT_INDEX]);
1183 ASET (font, FONT_SIZE_INDEX, make_float (point_size / 10));
1186 if (INTEGERP (prop[XLFD_RESX_INDEX]))
1187 ASET (font, FONT_DPI_INDEX, prop[XLFD_RESY_INDEX]);
1188 if (! NILP (prop[XLFD_SPACING_INDEX]))
1190 val = font_prop_validate_spacing (QCspacing,
1191 prop[XLFD_SPACING_INDEX]);
1192 if (! INTEGERP (val))
1193 return -1;
1194 ASET (font, FONT_SPACING_INDEX, val);
1196 if (INTEGERP (prop[XLFD_AVGWIDTH_INDEX]))
1197 ASET (font, FONT_AVGWIDTH_INDEX, prop[XLFD_AVGWIDTH_INDEX]);
1200 return 0;
1203 /* Store XLFD name of FONT (font-spec or font-entity) in NAME (NBYTES
1204 length), and return the name length. If FONT_SIZE_INDEX of FONT is
1205 0, use PIXEL_SIZE instead. */
1207 ptrdiff_t
1208 font_unparse_xlfd (Lisp_Object font, int pixel_size, char *name, int nbytes)
1210 char *p;
1211 const char *f[XLFD_REGISTRY_INDEX + 1];
1212 Lisp_Object val;
1213 int i, j, len;
1215 eassert (FONTP (font));
1217 for (i = FONT_FOUNDRY_INDEX, j = XLFD_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX;
1218 i++, j++)
1220 if (i == FONT_ADSTYLE_INDEX)
1221 j = XLFD_ADSTYLE_INDEX;
1222 else if (i == FONT_REGISTRY_INDEX)
1223 j = XLFD_REGISTRY_INDEX;
1224 val = AREF (font, i);
1225 if (NILP (val))
1227 if (j == XLFD_REGISTRY_INDEX)
1228 f[j] = "*-*";
1229 else
1230 f[j] = "*";
1232 else
1234 if (SYMBOLP (val))
1235 val = SYMBOL_NAME (val);
1236 if (j == XLFD_REGISTRY_INDEX
1237 && ! strchr (SSDATA (val), '-'))
1239 /* Change "jisx0208*" and "jisx0208" to "jisx0208*-*". */
1240 ptrdiff_t alloc = SBYTES (val) + 4;
1241 if (nbytes <= alloc)
1242 return -1;
1243 f[j] = p = alloca (alloc);
1244 sprintf (p, "%s%s-*", SDATA (val),
1245 &"*"[SDATA (val)[SBYTES (val) - 1] == '*']);
1247 else
1248 f[j] = SSDATA (val);
1252 for (i = FONT_WEIGHT_INDEX, j = XLFD_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX;
1253 i++, j++)
1255 val = font_style_symbolic (font, i, 0);
1256 if (NILP (val))
1257 f[j] = "*";
1258 else
1260 int c, k, l;
1261 ptrdiff_t alloc;
1263 val = SYMBOL_NAME (val);
1264 alloc = SBYTES (val) + 1;
1265 if (nbytes <= alloc)
1266 return -1;
1267 f[j] = p = alloca (alloc);
1268 /* Copy the name while excluding '-', '?', ',', and '"'. */
1269 for (k = l = 0; k < alloc; k++)
1271 c = SREF (val, k);
1272 if (c != '-' && c != '?' && c != ',' && c != '"')
1273 p[l++] = c;
1278 val = AREF (font, FONT_SIZE_INDEX);
1279 eassert (NUMBERP (val) || NILP (val));
1280 if (INTEGERP (val))
1282 EMACS_INT v = XINT (val);
1283 if (v <= 0)
1284 v = pixel_size;
1285 if (v > 0)
1287 f[XLFD_PIXEL_INDEX] = p =
1288 alloca (sizeof "-*" + INT_STRLEN_BOUND (EMACS_INT));
1289 sprintf (p, "%"pI"d-*", v);
1291 else
1292 f[XLFD_PIXEL_INDEX] = "*-*";
1294 else if (FLOATP (val))
1296 double v = XFLOAT_DATA (val) * 10;
1297 f[XLFD_PIXEL_INDEX] = p = alloca (sizeof "*-" + 1 + DBL_MAX_10_EXP + 1);
1298 sprintf (p, "*-%.0f", v);
1300 else
1301 f[XLFD_PIXEL_INDEX] = "*-*";
1303 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1305 EMACS_INT v = XINT (AREF (font, FONT_DPI_INDEX));
1306 f[XLFD_RESX_INDEX] = p =
1307 alloca (sizeof "-" + 2 * INT_STRLEN_BOUND (EMACS_INT));
1308 sprintf (p, "%"pI"d-%"pI"d", v, v);
1310 else
1311 f[XLFD_RESX_INDEX] = "*-*";
1312 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1314 EMACS_INT spacing = XINT (AREF (font, FONT_SPACING_INDEX));
1316 f[XLFD_SPACING_INDEX] = (spacing <= FONT_SPACING_PROPORTIONAL ? "p"
1317 : spacing <= FONT_SPACING_DUAL ? "d"
1318 : spacing <= FONT_SPACING_MONO ? "m"
1319 : "c");
1321 else
1322 f[XLFD_SPACING_INDEX] = "*";
1323 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1325 f[XLFD_AVGWIDTH_INDEX] = p = alloca (INT_BUFSIZE_BOUND (EMACS_INT));
1326 sprintf (p, "%"pI"d", XINT (AREF (font, FONT_AVGWIDTH_INDEX)));
1328 else
1329 f[XLFD_AVGWIDTH_INDEX] = "*";
1330 len = snprintf (name, nbytes, "-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s",
1331 f[XLFD_FOUNDRY_INDEX], f[XLFD_FAMILY_INDEX],
1332 f[XLFD_WEIGHT_INDEX], f[XLFD_SLANT_INDEX],
1333 f[XLFD_SWIDTH_INDEX], f[XLFD_ADSTYLE_INDEX],
1334 f[XLFD_PIXEL_INDEX], f[XLFD_RESX_INDEX],
1335 f[XLFD_SPACING_INDEX], f[XLFD_AVGWIDTH_INDEX],
1336 f[XLFD_REGISTRY_INDEX]);
1337 return len < nbytes ? len : -1;
1340 /* Parse NAME (null terminated) and store information in FONT
1341 (font-spec or font-entity). NAME is supplied in either the
1342 Fontconfig or GTK font name format. If NAME is successfully
1343 parsed, return 0. Otherwise return -1.
1345 The fontconfig format is
1347 FAMILY[-SIZE][:PROP1[=VAL1][:PROP2[=VAL2]...]]
1349 The GTK format is
1351 FAMILY [PROPS...] [SIZE]
1353 This function tries to guess which format it is. */
1355 static int
1356 font_parse_fcname (char *name, ptrdiff_t len, Lisp_Object font)
1358 char *p, *q;
1359 char *size_beg = NULL, *size_end = NULL;
1360 char *props_beg = NULL, *family_end = NULL;
1362 if (len == 0)
1363 return -1;
1365 for (p = name; *p; p++)
1367 if (*p == '\\' && p[1])
1368 p++;
1369 else if (*p == ':')
1371 props_beg = family_end = p;
1372 break;
1374 else if (*p == '-')
1376 bool decimal = 0, size_found = 1;
1377 for (q = p + 1; *q && *q != ':'; q++)
1378 if (! c_isdigit (*q))
1380 if (*q != '.' || decimal)
1382 size_found = 0;
1383 break;
1385 decimal = 1;
1387 if (size_found)
1389 family_end = p;
1390 size_beg = p + 1;
1391 size_end = q;
1392 break;
1397 if (family_end)
1399 Lisp_Object extra_props = Qnil;
1401 /* A fontconfig name with size and/or property data. */
1402 if (family_end > name)
1404 Lisp_Object family;
1405 family = font_intern_prop (name, family_end - name, 1);
1406 ASET (font, FONT_FAMILY_INDEX, family);
1408 if (size_beg)
1410 double point_size = strtod (size_beg, &size_end);
1411 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1412 if (*size_end == ':' && size_end[1])
1413 props_beg = size_end;
1415 if (props_beg)
1417 /* Now parse ":KEY=VAL" patterns. */
1418 Lisp_Object val;
1420 for (p = props_beg; *p; p = q)
1422 for (q = p + 1; *q && *q != '=' && *q != ':'; q++);
1423 if (*q != '=')
1425 /* Must be an enumerated value. */
1426 ptrdiff_t word_len;
1427 p = p + 1;
1428 word_len = q - p;
1429 val = font_intern_prop (p, q - p, 1);
1431 #define PROP_MATCH(STR) (word_len == strlen (STR) \
1432 && memcmp (p, STR, strlen (STR)) == 0)
1434 if (PROP_MATCH ("light")
1435 || PROP_MATCH ("medium")
1436 || PROP_MATCH ("demibold")
1437 || PROP_MATCH ("bold")
1438 || PROP_MATCH ("black"))
1439 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, val);
1440 else if (PROP_MATCH ("roman")
1441 || PROP_MATCH ("italic")
1442 || PROP_MATCH ("oblique"))
1443 FONT_SET_STYLE (font, FONT_SLANT_INDEX, val);
1444 else if (PROP_MATCH ("charcell"))
1445 ASET (font, FONT_SPACING_INDEX,
1446 make_number (FONT_SPACING_CHARCELL));
1447 else if (PROP_MATCH ("mono"))
1448 ASET (font, FONT_SPACING_INDEX,
1449 make_number (FONT_SPACING_MONO));
1450 else if (PROP_MATCH ("proportional"))
1451 ASET (font, FONT_SPACING_INDEX,
1452 make_number (FONT_SPACING_PROPORTIONAL));
1453 #undef PROP_MATCH
1455 else
1457 /* KEY=VAL pairs */
1458 Lisp_Object key;
1459 int prop;
1461 if (q - p == 10 && memcmp (p + 1, "pixelsize", 9) == 0)
1462 prop = FONT_SIZE_INDEX;
1463 else
1465 key = font_intern_prop (p, q - p, 1);
1466 prop = get_font_prop_index (key);
1469 p = q + 1;
1470 for (q = p; *q && *q != ':'; q++);
1471 val = font_intern_prop (p, q - p, 0);
1473 if (prop >= FONT_FOUNDRY_INDEX
1474 && prop < FONT_EXTRA_INDEX)
1475 ASET (font, prop, font_prop_validate (prop, Qnil, val));
1476 else
1478 extra_props = nconc2 (extra_props,
1479 list1 (Fcons (key, val)));
1482 p = q;
1486 if (! NILP (extra_props))
1488 struct font_driver_list *driver_list = font_driver_list;
1489 for ( ; driver_list; driver_list = driver_list->next)
1490 if (driver_list->driver->filter_properties)
1491 (*driver_list->driver->filter_properties) (font, extra_props);
1495 else
1497 /* Either a fontconfig-style name with no size and property
1498 data, or a GTK-style name. */
1499 Lisp_Object weight = Qnil, slant = Qnil;
1500 Lisp_Object width = Qnil, size = Qnil;
1501 char *word_start;
1502 ptrdiff_t word_len;
1504 /* Scan backwards from the end, looking for a size. */
1505 for (p = name + len - 1; p >= name; p--)
1506 if (!c_isdigit (*p))
1507 break;
1509 if ((p < name + len - 1) && ((p + 1 == name) || *p == ' '))
1510 /* Found a font size. */
1511 size = make_float (strtod (p + 1, NULL));
1512 else
1513 p = name + len;
1515 /* Now P points to the termination of the string, sans size.
1516 Scan backwards, looking for font properties. */
1517 for (; p > name; p = q)
1519 for (q = p - 1; q >= name; q--)
1521 if (q > name && *(q-1) == '\\')
1522 --q; /* Skip quoting backslashes. */
1523 else if (*q == ' ')
1524 break;
1527 word_start = q + 1;
1528 word_len = p - word_start;
1530 #define PROP_MATCH(STR) \
1531 (word_len == strlen (STR) \
1532 && memcmp (word_start, STR, strlen (STR)) == 0)
1533 #define PROP_SAVE(VAR, STR) \
1534 (VAR = NILP (VAR) ? font_intern_prop (STR, strlen (STR), 1) : VAR)
1536 if (PROP_MATCH ("Ultra-Light"))
1537 PROP_SAVE (weight, "ultra-light");
1538 else if (PROP_MATCH ("Light"))
1539 PROP_SAVE (weight, "light");
1540 else if (PROP_MATCH ("Book"))
1541 PROP_SAVE (weight, "book");
1542 else if (PROP_MATCH ("Medium"))
1543 PROP_SAVE (weight, "medium");
1544 else if (PROP_MATCH ("Semi-Bold"))
1545 PROP_SAVE (weight, "semi-bold");
1546 else if (PROP_MATCH ("Bold"))
1547 PROP_SAVE (weight, "bold");
1548 else if (PROP_MATCH ("Italic"))
1549 PROP_SAVE (slant, "italic");
1550 else if (PROP_MATCH ("Oblique"))
1551 PROP_SAVE (slant, "oblique");
1552 else if (PROP_MATCH ("Semi-Condensed"))
1553 PROP_SAVE (width, "semi-condensed");
1554 else if (PROP_MATCH ("Condensed"))
1555 PROP_SAVE (width, "condensed");
1556 /* An unknown word must be part of the font name. */
1557 else
1559 family_end = p;
1560 break;
1563 #undef PROP_MATCH
1564 #undef PROP_SAVE
1566 if (family_end)
1567 ASET (font, FONT_FAMILY_INDEX,
1568 font_intern_prop (name, family_end - name, 1));
1569 if (!NILP (size))
1570 ASET (font, FONT_SIZE_INDEX, size);
1571 if (!NILP (weight))
1572 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, weight);
1573 if (!NILP (slant))
1574 FONT_SET_STYLE (font, FONT_SLANT_INDEX, slant);
1575 if (!NILP (width))
1576 FONT_SET_STYLE (font, FONT_WIDTH_INDEX, width);
1579 return 0;
1582 /* Store fontconfig's font name of FONT (font-spec or font-entity) in
1583 NAME (NBYTES length), and return the name length. If
1584 FONT_SIZE_INDEX of FONT is 0, use PIXEL_SIZE instead. */
1587 font_unparse_fcname (Lisp_Object font, int pixel_size, char *name, int nbytes)
1589 Lisp_Object family, foundry;
1590 Lisp_Object val;
1591 int point_size;
1592 int i;
1593 char *p;
1594 char *lim;
1595 Lisp_Object styles[3];
1596 const char *style_names[3] = { "weight", "slant", "width" };
1598 family = AREF (font, FONT_FAMILY_INDEX);
1599 if (! NILP (family))
1601 if (SYMBOLP (family))
1602 family = SYMBOL_NAME (family);
1603 else
1604 family = Qnil;
1607 val = AREF (font, FONT_SIZE_INDEX);
1608 if (INTEGERP (val))
1610 if (XINT (val) != 0)
1611 pixel_size = XINT (val);
1612 point_size = -1;
1614 else
1616 eassert (FLOATP (val));
1617 pixel_size = -1;
1618 point_size = (int) XFLOAT_DATA (val);
1621 foundry = AREF (font, FONT_FOUNDRY_INDEX);
1622 if (! NILP (foundry))
1624 if (SYMBOLP (foundry))
1625 foundry = SYMBOL_NAME (foundry);
1626 else
1627 foundry = Qnil;
1630 for (i = 0; i < 3; i++)
1631 styles[i] = font_style_symbolic (font, FONT_WEIGHT_INDEX + i, 0);
1633 p = name;
1634 lim = name + nbytes;
1635 if (! NILP (family))
1637 int len = snprintf (p, lim - p, "%s", SSDATA (family));
1638 if (! (0 <= len && len < lim - p))
1639 return -1;
1640 p += len;
1642 if (point_size > 0)
1644 int len = snprintf (p, lim - p, &"-%d"[p == name], point_size);
1645 if (! (0 <= len && len < lim - p))
1646 return -1;
1647 p += len;
1649 else if (pixel_size > 0)
1651 int len = snprintf (p, lim - p, ":pixelsize=%d", pixel_size);
1652 if (! (0 <= len && len < lim - p))
1653 return -1;
1654 p += len;
1656 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
1658 int len = snprintf (p, lim - p, ":foundry=%s",
1659 SSDATA (SYMBOL_NAME (AREF (font,
1660 FONT_FOUNDRY_INDEX))));
1661 if (! (0 <= len && len < lim - p))
1662 return -1;
1663 p += len;
1665 for (i = 0; i < 3; i++)
1666 if (! NILP (styles[i]))
1668 int len = snprintf (p, lim - p, ":%s=%s", style_names[i],
1669 SSDATA (SYMBOL_NAME (styles[i])));
1670 if (! (0 <= len && len < lim - p))
1671 return -1;
1672 p += len;
1675 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1677 int len = snprintf (p, lim - p, ":dpi=%"pI"d",
1678 XINT (AREF (font, FONT_DPI_INDEX)));
1679 if (! (0 <= len && len < lim - p))
1680 return -1;
1681 p += len;
1684 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1686 int len = snprintf (p, lim - p, ":spacing=%"pI"d",
1687 XINT (AREF (font, FONT_SPACING_INDEX)));
1688 if (! (0 <= len && len < lim - p))
1689 return -1;
1690 p += len;
1693 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1695 int len = snprintf (p, lim - p,
1696 (XINT (AREF (font, FONT_AVGWIDTH_INDEX)) == 0
1697 ? ":scalable=true"
1698 : ":scalable=false"));
1699 if (! (0 <= len && len < lim - p))
1700 return -1;
1701 p += len;
1704 return (p - name);
1707 /* Parse NAME (null terminated) and store information in FONT
1708 (font-spec or font-entity). If NAME is successfully parsed, return
1709 0. Otherwise return -1. */
1711 static int
1712 font_parse_name (char *name, ptrdiff_t namelen, Lisp_Object font)
1714 if (name[0] == '-' || strchr (name, '*') || strchr (name, '?'))
1715 return font_parse_xlfd (name, namelen, font);
1716 return font_parse_fcname (name, namelen, font);
1720 /* Merge FAMILY and REGISTRY into FONT_SPEC. FAMILY may have the form
1721 "FAMILY-FOUNDRY". REGISTRY may not contain charset-encoding
1722 part. */
1724 void
1725 font_parse_family_registry (Lisp_Object family, Lisp_Object registry, Lisp_Object font_spec)
1727 ptrdiff_t len;
1728 char *p0, *p1;
1730 if (! NILP (family)
1731 && NILP (AREF (font_spec, FONT_FAMILY_INDEX)))
1733 CHECK_STRING (family);
1734 len = SBYTES (family);
1735 p0 = SSDATA (family);
1736 p1 = strchr (p0, '-');
1737 if (p1)
1739 if ((*p0 != '*' && p1 - p0 > 0)
1740 && NILP (AREF (font_spec, FONT_FOUNDRY_INDEX)))
1741 Ffont_put (font_spec, QCfoundry, font_intern_prop (p0, p1 - p0, 1));
1742 p1++;
1743 len -= p1 - p0;
1744 Ffont_put (font_spec, QCfamily, font_intern_prop (p1, len, 1));
1746 else
1747 ASET (font_spec, FONT_FAMILY_INDEX, Fintern (family, Qnil));
1749 if (! NILP (registry))
1751 /* Convert "XXX" and "XXX*" to "XXX*-*". */
1752 CHECK_STRING (registry);
1753 len = SBYTES (registry);
1754 p0 = SSDATA (registry);
1755 p1 = strchr (p0, '-');
1756 if (! p1)
1758 if (SDATA (registry)[len - 1] == '*')
1759 registry = concat2 (registry, build_string ("-*"));
1760 else
1761 registry = concat2 (registry, build_string ("*-*"));
1763 registry = Fdowncase (registry);
1764 ASET (font_spec, FONT_REGISTRY_INDEX, Fintern (registry, Qnil));
1769 /* This part (through the next ^L) is still experimental and not
1770 tested much. We may drastically change codes. */
1772 /* OTF handler. */
1774 #if 0
1776 #define LGSTRING_HEADER_SIZE 6
1777 #define LGSTRING_GLYPH_SIZE 8
1779 static int
1780 check_gstring (Lisp_Object gstring)
1782 Lisp_Object val;
1783 ptrdiff_t i;
1784 int j;
1786 CHECK_VECTOR (gstring);
1787 val = AREF (gstring, 0);
1788 CHECK_VECTOR (val);
1789 if (ASIZE (val) < LGSTRING_HEADER_SIZE)
1790 goto err;
1791 CHECK_FONT_OBJECT (LGSTRING_FONT (gstring));
1792 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_LBEARING)))
1793 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_LBEARING));
1794 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_RBEARING)))
1795 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_RBEARING));
1796 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_WIDTH)))
1797 CHECK_NATNUM (LGSTRING_SLOT (gstring, LGSTRING_IX_WIDTH));
1798 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT)))
1799 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT));
1800 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT)))
1801 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT));
1803 for (i = 0; i < LGSTRING_GLYPH_LEN (gstring); i++)
1805 val = LGSTRING_GLYPH (gstring, i);
1806 CHECK_VECTOR (val);
1807 if (ASIZE (val) < LGSTRING_GLYPH_SIZE)
1808 goto err;
1809 if (NILP (AREF (val, LGLYPH_IX_CHAR)))
1810 break;
1811 CHECK_NATNUM (AREF (val, LGLYPH_IX_FROM));
1812 CHECK_NATNUM (AREF (val, LGLYPH_IX_TO));
1813 CHECK_CHARACTER (AREF (val, LGLYPH_IX_CHAR));
1814 if (!NILP (AREF (val, LGLYPH_IX_CODE)))
1815 CHECK_NATNUM (AREF (val, LGLYPH_IX_CODE));
1816 if (!NILP (AREF (val, LGLYPH_IX_WIDTH)))
1817 CHECK_NATNUM (AREF (val, LGLYPH_IX_WIDTH));
1818 if (!NILP (AREF (val, LGLYPH_IX_ADJUSTMENT)))
1820 val = AREF (val, LGLYPH_IX_ADJUSTMENT);
1821 CHECK_VECTOR (val);
1822 if (ASIZE (val) < 3)
1823 goto err;
1824 for (j = 0; j < 3; j++)
1825 CHECK_NUMBER (AREF (val, j));
1828 return i;
1829 err:
1830 error ("Invalid glyph-string format");
1831 return -1;
1834 static void
1835 check_otf_features (Lisp_Object otf_features)
1837 Lisp_Object val;
1839 CHECK_CONS (otf_features);
1840 CHECK_SYMBOL (XCAR (otf_features));
1841 otf_features = XCDR (otf_features);
1842 CHECK_CONS (otf_features);
1843 CHECK_SYMBOL (XCAR (otf_features));
1844 otf_features = XCDR (otf_features);
1845 for (val = Fcar (otf_features); CONSP (val); val = XCDR (val))
1847 CHECK_SYMBOL (XCAR (val));
1848 if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
1849 error ("Invalid OTF GSUB feature: %s",
1850 SDATA (SYMBOL_NAME (XCAR (val))));
1852 otf_features = XCDR (otf_features);
1853 for (val = Fcar (otf_features); CONSP (val); val = XCDR (val))
1855 CHECK_SYMBOL (XCAR (val));
1856 if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
1857 error ("Invalid OTF GPOS feature: %s",
1858 SDATA (SYMBOL_NAME (XCAR (val))));
1862 #ifdef HAVE_LIBOTF
1863 #include <otf.h>
1865 Lisp_Object otf_list;
1867 static Lisp_Object
1868 otf_tag_symbol (OTF_Tag tag)
1870 char name[5];
1872 OTF_tag_name (tag, name);
1873 return Fintern (make_unibyte_string (name, 4), Qnil);
1876 static OTF *
1877 otf_open (Lisp_Object file)
1879 Lisp_Object val = Fassoc (file, otf_list);
1880 OTF *otf;
1882 if (! NILP (val))
1883 otf = XSAVE_POINTER (XCDR (val), 0);
1884 else
1886 otf = STRINGP (file) ? OTF_open (SSDATA (file)) : NULL;
1887 val = make_save_ptr (otf);
1888 otf_list = Fcons (Fcons (file, val), otf_list);
1890 return otf;
1894 /* Return a list describing which scripts/languages FONT supports by
1895 which GSUB/GPOS features of OpenType tables. See the comment of
1896 (struct font_driver).otf_capability. */
1898 Lisp_Object
1899 font_otf_capability (struct font *font)
1901 OTF *otf;
1902 Lisp_Object capability = Fcons (Qnil, Qnil);
1903 int i;
1905 otf = otf_open (font->props[FONT_FILE_INDEX]);
1906 if (! otf)
1907 return Qnil;
1908 for (i = 0; i < 2; i++)
1910 OTF_GSUB_GPOS *gsub_gpos;
1911 Lisp_Object script_list = Qnil;
1912 int j;
1914 if (OTF_get_features (otf, i == 0) < 0)
1915 continue;
1916 gsub_gpos = i == 0 ? otf->gsub : otf->gpos;
1917 for (j = gsub_gpos->ScriptList.ScriptCount - 1; j >= 0; j--)
1919 OTF_Script *script = gsub_gpos->ScriptList.Script + j;
1920 Lisp_Object langsys_list = Qnil;
1921 Lisp_Object script_tag = otf_tag_symbol (script->ScriptTag);
1922 int k;
1924 for (k = script->LangSysCount; k >= 0; k--)
1926 OTF_LangSys *langsys;
1927 Lisp_Object feature_list = Qnil;
1928 Lisp_Object langsys_tag;
1929 int l;
1931 if (k == script->LangSysCount)
1933 langsys = &script->DefaultLangSys;
1934 langsys_tag = Qnil;
1936 else
1938 langsys = script->LangSys + k;
1939 langsys_tag
1940 = otf_tag_symbol (script->LangSysRecord[k].LangSysTag);
1942 for (l = langsys->FeatureCount - 1; l >= 0; l--)
1944 OTF_Feature *feature
1945 = gsub_gpos->FeatureList.Feature + langsys->FeatureIndex[l];
1946 Lisp_Object feature_tag
1947 = otf_tag_symbol (feature->FeatureTag);
1949 feature_list = Fcons (feature_tag, feature_list);
1951 langsys_list = Fcons (Fcons (langsys_tag, feature_list),
1952 langsys_list);
1954 script_list = Fcons (Fcons (script_tag, langsys_list),
1955 script_list);
1958 if (i == 0)
1959 XSETCAR (capability, script_list);
1960 else
1961 XSETCDR (capability, script_list);
1964 return capability;
1967 /* Parse OTF features in SPEC and write a proper features spec string
1968 in FEATURES for the call of OTF_drive_gsub/gpos (of libotf). It is
1969 assured that the sufficient memory has already allocated for
1970 FEATURES. */
1972 static void
1973 generate_otf_features (Lisp_Object spec, char *features)
1975 Lisp_Object val;
1976 char *p;
1977 bool asterisk;
1979 p = features;
1980 *p = '\0';
1981 for (asterisk = 0; CONSP (spec); spec = XCDR (spec))
1983 val = XCAR (spec);
1984 CHECK_SYMBOL (val);
1985 if (p > features)
1986 *p++ = ',';
1987 if (SREF (SYMBOL_NAME (val), 0) == '*')
1989 asterisk = 1;
1990 *p++ = '*';
1992 else if (! asterisk)
1994 val = SYMBOL_NAME (val);
1995 p += esprintf (p, "%s", SDATA (val));
1997 else
1999 val = SYMBOL_NAME (val);
2000 p += esprintf (p, "~%s", SDATA (val));
2003 if (CONSP (spec))
2004 error ("OTF spec too long");
2007 Lisp_Object
2008 font_otf_DeviceTable (OTF_DeviceTable *device_table)
2010 int len = device_table->StartSize - device_table->EndSize + 1;
2012 return Fcons (make_number (len),
2013 make_unibyte_string (device_table->DeltaValue, len));
2016 Lisp_Object
2017 font_otf_ValueRecord (int value_format, OTF_ValueRecord *value_record)
2019 Lisp_Object val = Fmake_vector (make_number (8), Qnil);
2021 if (value_format & OTF_XPlacement)
2022 ASET (val, 0, make_number (value_record->XPlacement));
2023 if (value_format & OTF_YPlacement)
2024 ASET (val, 1, make_number (value_record->YPlacement));
2025 if (value_format & OTF_XAdvance)
2026 ASET (val, 2, make_number (value_record->XAdvance));
2027 if (value_format & OTF_YAdvance)
2028 ASET (val, 3, make_number (value_record->YAdvance));
2029 if (value_format & OTF_XPlaDevice)
2030 ASET (val, 4, font_otf_DeviceTable (&value_record->XPlaDevice));
2031 if (value_format & OTF_YPlaDevice)
2032 ASET (val, 4, font_otf_DeviceTable (&value_record->YPlaDevice));
2033 if (value_format & OTF_XAdvDevice)
2034 ASET (val, 4, font_otf_DeviceTable (&value_record->XAdvDevice));
2035 if (value_format & OTF_YAdvDevice)
2036 ASET (val, 4, font_otf_DeviceTable (&value_record->YAdvDevice));
2037 return val;
2040 Lisp_Object
2041 font_otf_Anchor (OTF_Anchor *anchor)
2043 Lisp_Object val;
2045 val = Fmake_vector (make_number (anchor->AnchorFormat + 1), Qnil);
2046 ASET (val, 0, make_number (anchor->XCoordinate));
2047 ASET (val, 1, make_number (anchor->YCoordinate));
2048 if (anchor->AnchorFormat == 2)
2049 ASET (val, 2, make_number (anchor->f.f1.AnchorPoint));
2050 else
2052 ASET (val, 3, font_otf_DeviceTable (&anchor->f.f2.XDeviceTable));
2053 ASET (val, 4, font_otf_DeviceTable (&anchor->f.f2.YDeviceTable));
2055 return val;
2057 #endif /* HAVE_LIBOTF */
2058 #endif /* 0 */
2061 /* Font sorting. */
2063 static double
2064 font_rescale_ratio (Lisp_Object font_entity)
2066 Lisp_Object tail, elt;
2067 Lisp_Object name = Qnil;
2069 for (tail = Vface_font_rescale_alist; CONSP (tail); tail = XCDR (tail))
2071 elt = XCAR (tail);
2072 if (FLOATP (XCDR (elt)))
2074 if (STRINGP (XCAR (elt)))
2076 if (NILP (name))
2077 name = Ffont_xlfd_name (font_entity, Qnil);
2078 if (fast_string_match_ignore_case (XCAR (elt), name) >= 0)
2079 return XFLOAT_DATA (XCDR (elt));
2081 else if (FONT_SPEC_P (XCAR (elt)))
2083 if (font_match_p (XCAR (elt), font_entity))
2084 return XFLOAT_DATA (XCDR (elt));
2088 return 1.0;
2091 /* We sort fonts by scoring each of them against a specified
2092 font-spec. The score value is 32 bit (`unsigned'), and the smaller
2093 the value is, the closer the font is to the font-spec.
2095 The lowest 2 bits of the score are used for driver type. The font
2096 available by the most preferred font driver is 0.
2098 The 4 7-bit fields in the higher 28 bits are used for numeric properties
2099 WEIGHT, SLANT, WIDTH, and SIZE. */
2101 /* How many bits to shift to store the difference value of each font
2102 property in a score. Note that floats for FONT_TYPE_INDEX and
2103 FONT_REGISTRY_INDEX are not used. */
2104 static int sort_shift_bits[FONT_SIZE_INDEX + 1];
2106 /* Score font-entity ENTITY against properties of font-spec SPEC_PROP.
2107 The return value indicates how different ENTITY is compared with
2108 SPEC_PROP. */
2110 static unsigned
2111 font_score (Lisp_Object entity, Lisp_Object *spec_prop)
2113 unsigned score = 0;
2114 int i;
2116 /* Score three style numeric fields. Maximum difference is 127. */
2117 for (i = FONT_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX; i++)
2118 if (! NILP (spec_prop[i]) && ! EQ (AREF (entity, i), spec_prop[i]))
2120 EMACS_INT diff = ((XINT (AREF (entity, i)) >> 8)
2121 - (XINT (spec_prop[i]) >> 8));
2122 score |= min (eabs (diff), 127) << sort_shift_bits[i];
2125 /* Score the size. Maximum difference is 127. */
2126 i = FONT_SIZE_INDEX;
2127 if (! NILP (spec_prop[FONT_SIZE_INDEX])
2128 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
2130 /* We use the higher 6-bit for the actual size difference. The
2131 lowest bit is set if the DPI is different. */
2132 EMACS_INT diff;
2133 EMACS_INT pixel_size = XINT (spec_prop[FONT_SIZE_INDEX]);
2135 if (CONSP (Vface_font_rescale_alist))
2136 pixel_size *= font_rescale_ratio (entity);
2137 diff = eabs (pixel_size - XINT (AREF (entity, FONT_SIZE_INDEX))) << 1;
2138 if (! NILP (spec_prop[FONT_DPI_INDEX])
2139 && ! EQ (spec_prop[FONT_DPI_INDEX], AREF (entity, FONT_DPI_INDEX)))
2140 diff |= 1;
2141 if (! NILP (spec_prop[FONT_AVGWIDTH_INDEX])
2142 && ! EQ (spec_prop[FONT_AVGWIDTH_INDEX], AREF (entity, FONT_AVGWIDTH_INDEX)))
2143 diff |= 1;
2144 score |= min (diff, 127) << sort_shift_bits[FONT_SIZE_INDEX];
2147 return score;
2151 /* Concatenate all elements of LIST into one vector. LIST is a list
2152 of font-entity vectors. */
2154 static Lisp_Object
2155 font_vconcat_entity_vectors (Lisp_Object list)
2157 int nargs = XINT (Flength (list));
2158 Lisp_Object *args = alloca (word_size * nargs);
2159 int i;
2161 for (i = 0; i < nargs; i++, list = XCDR (list))
2162 args[i] = XCAR (list);
2163 return Fvconcat (nargs, args);
2167 /* The structure for elements being sorted by qsort. */
2168 struct font_sort_data
2170 unsigned score;
2171 int font_driver_preference;
2172 Lisp_Object entity;
2176 /* The comparison function for qsort. */
2178 static int
2179 font_compare (const void *d1, const void *d2)
2181 const struct font_sort_data *data1 = d1;
2182 const struct font_sort_data *data2 = d2;
2184 if (data1->score < data2->score)
2185 return -1;
2186 else if (data1->score > data2->score)
2187 return 1;
2188 return (data1->font_driver_preference - data2->font_driver_preference);
2192 /* Sort each font-entity vector in LIST by closeness to font-spec PREFER.
2193 If PREFER specifies a point-size, calculate the corresponding
2194 pixel-size from QCdpi property of PREFER or from the Y-resolution
2195 of FRAME before sorting.
2197 If BEST-ONLY is nonzero, return the best matching entity (that
2198 supports the character BEST-ONLY if BEST-ONLY is positive, or any
2199 if BEST-ONLY is negative). Otherwise, return the sorted result as
2200 a single vector of font-entities.
2202 This function does no optimization for the case that the total
2203 number of elements is 1. The caller should avoid calling this in
2204 such a case. */
2206 static Lisp_Object
2207 font_sort_entities (Lisp_Object list, Lisp_Object prefer,
2208 struct frame *f, int best_only)
2210 Lisp_Object prefer_prop[FONT_SPEC_MAX];
2211 int len, maxlen, i;
2212 struct font_sort_data *data;
2213 unsigned best_score;
2214 Lisp_Object best_entity;
2215 Lisp_Object tail, vec IF_LINT (= Qnil);
2216 USE_SAFE_ALLOCA;
2218 for (i = FONT_WEIGHT_INDEX; i <= FONT_AVGWIDTH_INDEX; i++)
2219 prefer_prop[i] = AREF (prefer, i);
2220 if (FLOATP (prefer_prop[FONT_SIZE_INDEX]))
2221 prefer_prop[FONT_SIZE_INDEX]
2222 = make_number (font_pixel_size (f, prefer));
2224 if (NILP (XCDR (list)))
2226 /* What we have to take care of is this single vector. */
2227 vec = XCAR (list);
2228 maxlen = ASIZE (vec);
2230 else if (best_only)
2232 /* We don't have to perform sort, so there's no need of creating
2233 a single vector. But, we must find the length of the longest
2234 vector. */
2235 maxlen = 0;
2236 for (tail = list; CONSP (tail); tail = XCDR (tail))
2237 if (maxlen < ASIZE (XCAR (tail)))
2238 maxlen = ASIZE (XCAR (tail));
2240 else
2242 /* We have to create a single vector to sort it. */
2243 vec = font_vconcat_entity_vectors (list);
2244 maxlen = ASIZE (vec);
2247 data = SAFE_ALLOCA (maxlen * sizeof *data);
2248 best_score = 0xFFFFFFFF;
2249 best_entity = Qnil;
2251 for (tail = list; CONSP (tail); tail = XCDR (tail))
2253 int font_driver_preference = 0;
2254 Lisp_Object current_font_driver;
2256 if (best_only)
2257 vec = XCAR (tail);
2258 len = ASIZE (vec);
2260 /* We are sure that the length of VEC > 0. */
2261 current_font_driver = AREF (AREF (vec, 0), FONT_TYPE_INDEX);
2262 /* Score the elements. */
2263 for (i = 0; i < len; i++)
2265 data[i].entity = AREF (vec, i);
2266 data[i].score
2267 = ((best_only <= 0 || font_has_char (f, data[i].entity, best_only)
2268 > 0)
2269 ? font_score (data[i].entity, prefer_prop)
2270 : 0xFFFFFFFF);
2271 if (best_only && best_score > data[i].score)
2273 best_score = data[i].score;
2274 best_entity = data[i].entity;
2275 if (best_score == 0)
2276 break;
2278 if (! EQ (current_font_driver, AREF (AREF (vec, i), FONT_TYPE_INDEX)))
2280 current_font_driver = AREF (AREF (vec, i), FONT_TYPE_INDEX);
2281 font_driver_preference++;
2283 data[i].font_driver_preference = font_driver_preference;
2286 /* Sort if necessary. */
2287 if (! best_only)
2289 qsort (data, len, sizeof *data, font_compare);
2290 for (i = 0; i < len; i++)
2291 ASET (vec, i, data[i].entity);
2292 break;
2294 else
2295 vec = best_entity;
2298 SAFE_FREE ();
2300 FONT_ADD_LOG ("sort-by", prefer, vec);
2301 return vec;
2305 /* API of Font Service Layer. */
2307 /* Reflect ORDER (see the variable font_sort_order in xfaces.c) to
2308 sort_shift_bits. Finternal_set_font_selection_order calls this
2309 function with font_sort_order after setting up it. */
2311 void
2312 font_update_sort_order (int *order)
2314 int i, shift_bits;
2316 for (i = 0, shift_bits = 23; i < 4; i++, shift_bits -= 7)
2318 int xlfd_idx = order[i];
2320 if (xlfd_idx == XLFD_WEIGHT_INDEX)
2321 sort_shift_bits[FONT_WEIGHT_INDEX] = shift_bits;
2322 else if (xlfd_idx == XLFD_SLANT_INDEX)
2323 sort_shift_bits[FONT_SLANT_INDEX] = shift_bits;
2324 else if (xlfd_idx == XLFD_SWIDTH_INDEX)
2325 sort_shift_bits[FONT_WIDTH_INDEX] = shift_bits;
2326 else
2327 sort_shift_bits[FONT_SIZE_INDEX] = shift_bits;
2331 static bool
2332 font_check_otf_features (Lisp_Object script, Lisp_Object langsys,
2333 Lisp_Object features, Lisp_Object table)
2335 Lisp_Object val;
2336 bool negative;
2338 table = assq_no_quit (script, table);
2339 if (NILP (table))
2340 return 0;
2341 table = XCDR (table);
2342 if (! NILP (langsys))
2344 table = assq_no_quit (langsys, table);
2345 if (NILP (table))
2346 return 0;
2348 else
2350 val = assq_no_quit (Qnil, table);
2351 if (NILP (val))
2352 table = XCAR (table);
2353 else
2354 table = val;
2356 table = XCDR (table);
2357 for (negative = 0; CONSP (features); features = XCDR (features))
2359 if (NILP (XCAR (features)))
2361 negative = 1;
2362 continue;
2364 if (NILP (Fmemq (XCAR (features), table)) != negative)
2365 return 0;
2367 return 1;
2370 /* Check if OTF_CAPABILITY satisfies SPEC (otf-spec). */
2372 static bool
2373 font_check_otf (Lisp_Object spec, Lisp_Object otf_capability)
2375 Lisp_Object script, langsys = Qnil, gsub = Qnil, gpos = Qnil;
2377 script = XCAR (spec);
2378 spec = XCDR (spec);
2379 if (! NILP (spec))
2381 langsys = XCAR (spec);
2382 spec = XCDR (spec);
2383 if (! NILP (spec))
2385 gsub = XCAR (spec);
2386 spec = XCDR (spec);
2387 if (! NILP (spec))
2388 gpos = XCAR (spec);
2392 if (! NILP (gsub) && ! font_check_otf_features (script, langsys, gsub,
2393 XCAR (otf_capability)))
2394 return 0;
2395 if (! NILP (gpos) && ! font_check_otf_features (script, langsys, gpos,
2396 XCDR (otf_capability)))
2397 return 0;
2398 return 1;
2403 /* Check if FONT (font-entity or font-object) matches with the font
2404 specification SPEC. */
2406 bool
2407 font_match_p (Lisp_Object spec, Lisp_Object font)
2409 Lisp_Object prop[FONT_SPEC_MAX], *props;
2410 Lisp_Object extra, font_extra;
2411 int i;
2413 for (i = FONT_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX; i++)
2414 if (! NILP (AREF (spec, i))
2415 && ! NILP (AREF (font, i))
2416 && ! EQ (AREF (spec, i), AREF (font, i)))
2417 return 0;
2418 props = XFONT_SPEC (spec)->props;
2419 if (FLOATP (props[FONT_SIZE_INDEX]))
2421 for (i = FONT_FOUNDRY_INDEX; i < FONT_SIZE_INDEX; i++)
2422 prop[i] = AREF (spec, i);
2423 prop[FONT_SIZE_INDEX]
2424 = make_number (font_pixel_size (XFRAME (selected_frame), spec));
2425 props = prop;
2428 if (font_score (font, props) > 0)
2429 return 0;
2430 extra = AREF (spec, FONT_EXTRA_INDEX);
2431 font_extra = AREF (font, FONT_EXTRA_INDEX);
2432 for (; CONSP (extra); extra = XCDR (extra))
2434 Lisp_Object key = XCAR (XCAR (extra));
2435 Lisp_Object val = XCDR (XCAR (extra)), val2;
2437 if (EQ (key, QClang))
2439 val2 = assq_no_quit (key, font_extra);
2440 if (NILP (val2))
2441 return 0;
2442 val2 = XCDR (val2);
2443 if (CONSP (val))
2445 if (! CONSP (val2))
2446 return 0;
2447 while (CONSP (val))
2448 if (NILP (Fmemq (val, val2)))
2449 return 0;
2451 else
2452 if (CONSP (val2)
2453 ? NILP (Fmemq (val, XCDR (val2)))
2454 : ! EQ (val, val2))
2455 return 0;
2457 else if (EQ (key, QCscript))
2459 val2 = assq_no_quit (val, Vscript_representative_chars);
2460 if (CONSP (val2))
2462 val2 = XCDR (val2);
2463 if (CONSP (val2))
2465 /* All characters in the list must be supported. */
2466 for (; CONSP (val2); val2 = XCDR (val2))
2468 if (! CHARACTERP (XCAR (val2)))
2469 continue;
2470 if (font_encode_char (font, XFASTINT (XCAR (val2)))
2471 == FONT_INVALID_CODE)
2472 return 0;
2475 else if (VECTORP (val2))
2477 /* At most one character in the vector must be supported. */
2478 for (i = 0; i < ASIZE (val2); i++)
2480 if (! CHARACTERP (AREF (val2, i)))
2481 continue;
2482 if (font_encode_char (font, XFASTINT (AREF (val2, i)))
2483 != FONT_INVALID_CODE)
2484 break;
2486 if (i == ASIZE (val2))
2487 return 0;
2491 else if (EQ (key, QCotf))
2493 struct font *fontp;
2495 if (! FONT_OBJECT_P (font))
2496 return 0;
2497 fontp = XFONT_OBJECT (font);
2498 if (! fontp->driver->otf_capability)
2499 return 0;
2500 val2 = fontp->driver->otf_capability (fontp);
2501 if (NILP (val2) || ! font_check_otf (val, val2))
2502 return 0;
2506 return 1;
2510 /* Font cache
2512 Each font backend has the callback function get_cache, and it
2513 returns a cons cell of which cdr part can be freely used for
2514 caching fonts. The cons cell may be shared by multiple frames
2515 and/or multiple font drivers. So, we arrange the cdr part as this:
2517 ((DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) ...)
2519 where DRIVER-TYPE is a symbol such as `x', `xft', etc., NUM-FRAMES
2520 is a number frames sharing this cache, and FONT-CACHE-DATA is a
2521 cons (FONT-SPEC . [FONT-ENTITY ...]). */
2523 static void font_prepare_cache (struct frame *, struct font_driver *);
2524 static void font_finish_cache (struct frame *, struct font_driver *);
2525 static Lisp_Object font_get_cache (struct frame *, struct font_driver *);
2526 static void font_clear_cache (struct frame *, Lisp_Object,
2527 struct font_driver *);
2529 static void
2530 font_prepare_cache (struct frame *f, struct font_driver *driver)
2532 Lisp_Object cache, val;
2534 cache = driver->get_cache (f);
2535 val = XCDR (cache);
2536 while (CONSP (val) && ! EQ (XCAR (XCAR (val)), driver->type))
2537 val = XCDR (val);
2538 if (NILP (val))
2540 val = list2 (driver->type, make_number (1));
2541 XSETCDR (cache, Fcons (val, XCDR (cache)));
2543 else
2545 val = XCDR (XCAR (val));
2546 XSETCAR (val, make_number (XINT (XCAR (val)) + 1));
2551 static void
2552 font_finish_cache (struct frame *f, struct font_driver *driver)
2554 Lisp_Object cache, val, tmp;
2557 cache = driver->get_cache (f);
2558 val = XCDR (cache);
2559 while (CONSP (val) && ! EQ (XCAR (XCAR (val)), driver->type))
2560 cache = val, val = XCDR (val);
2561 eassert (! NILP (val));
2562 tmp = XCDR (XCAR (val));
2563 XSETCAR (tmp, make_number (XINT (XCAR (tmp)) - 1));
2564 if (XINT (XCAR (tmp)) == 0)
2566 font_clear_cache (f, XCAR (val), driver);
2567 XSETCDR (cache, XCDR (val));
2572 static Lisp_Object
2573 font_get_cache (struct frame *f, struct font_driver *driver)
2575 Lisp_Object val = driver->get_cache (f);
2576 Lisp_Object type = driver->type;
2578 eassert (CONSP (val));
2579 for (val = XCDR (val); ! EQ (XCAR (XCAR (val)), type); val = XCDR (val));
2580 eassert (CONSP (val));
2581 /* VAL = ((DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) ...) */
2582 val = XCDR (XCAR (val));
2583 return val;
2587 static void
2588 font_clear_cache (struct frame *f, Lisp_Object cache, struct font_driver *driver)
2590 Lisp_Object tail, elt;
2591 Lisp_Object entity;
2592 ptrdiff_t i;
2594 /* CACHE = (DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) */
2595 for (tail = XCDR (XCDR (cache)); CONSP (tail); tail = XCDR (tail))
2597 elt = XCAR (tail);
2598 /* elt should have the form (FONT-SPEC . [FONT-ENTITY ...]) */
2599 if (CONSP (elt) && FONT_SPEC_P (XCAR (elt)))
2601 elt = XCDR (elt);
2602 eassert (VECTORP (elt));
2603 for (i = 0; i < ASIZE (elt); i++)
2605 entity = AREF (elt, i);
2607 if (FONT_ENTITY_P (entity)
2608 && EQ (driver->type, AREF (entity, FONT_TYPE_INDEX)))
2610 Lisp_Object objlist = AREF (entity, FONT_OBJLIST_INDEX);
2612 for (; CONSP (objlist); objlist = XCDR (objlist))
2614 Lisp_Object val = XCAR (objlist);
2615 struct font *font = XFONT_OBJECT (val);
2617 if (! NILP (AREF (val, FONT_TYPE_INDEX)))
2619 eassert (font && driver == font->driver);
2620 driver->close (font);
2623 if (driver->free_entity)
2624 driver->free_entity (entity);
2629 XSETCDR (cache, Qnil);
2633 static Lisp_Object scratch_font_spec, scratch_font_prefer;
2635 /* Check each font-entity in VEC, and return a list of font-entities
2636 that satisfy these conditions:
2637 (1) matches with SPEC and SIZE if SPEC is not nil, and
2638 (2) doesn't match with any regexps in Vface_ignored_fonts (if non-nil).
2641 static Lisp_Object
2642 font_delete_unmatched (Lisp_Object vec, Lisp_Object spec, int size)
2644 Lisp_Object entity, val;
2645 enum font_property_index prop;
2646 int i;
2648 for (val = Qnil, i = ASIZE (vec) - 1; i >= 0; i--)
2650 entity = AREF (vec, i);
2651 if (! NILP (Vface_ignored_fonts))
2653 char name[256];
2654 ptrdiff_t namelen;
2655 Lisp_Object tail, regexp;
2657 namelen = font_unparse_xlfd (entity, 0, name, 256);
2658 if (namelen >= 0)
2660 for (tail = Vface_ignored_fonts; CONSP (tail); tail = XCDR (tail))
2662 regexp = XCAR (tail);
2663 if (STRINGP (regexp)
2664 && fast_c_string_match_ignore_case (regexp, name,
2665 namelen) >= 0)
2666 break;
2668 if (CONSP (tail))
2669 continue;
2672 if (NILP (spec))
2674 val = Fcons (entity, val);
2675 continue;
2677 for (prop = FONT_WEIGHT_INDEX; prop < FONT_SIZE_INDEX; prop++)
2678 if (INTEGERP (AREF (spec, prop))
2679 && ((XINT (AREF (spec, prop)) >> 8)
2680 != (XINT (AREF (entity, prop)) >> 8)))
2681 prop = FONT_SPEC_MAX;
2682 if (prop < FONT_SPEC_MAX
2683 && size
2684 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
2686 int diff = XINT (AREF (entity, FONT_SIZE_INDEX)) - size;
2688 if (eabs (diff) > FONT_PIXEL_SIZE_QUANTUM)
2689 prop = FONT_SPEC_MAX;
2691 if (prop < FONT_SPEC_MAX
2692 && INTEGERP (AREF (spec, FONT_DPI_INDEX))
2693 && INTEGERP (AREF (entity, FONT_DPI_INDEX))
2694 && XINT (AREF (entity, FONT_DPI_INDEX)) != 0
2695 && ! EQ (AREF (spec, FONT_DPI_INDEX), AREF (entity, FONT_DPI_INDEX)))
2696 prop = FONT_SPEC_MAX;
2697 if (prop < FONT_SPEC_MAX
2698 && INTEGERP (AREF (spec, FONT_AVGWIDTH_INDEX))
2699 && INTEGERP (AREF (entity, FONT_AVGWIDTH_INDEX))
2700 && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) != 0
2701 && ! EQ (AREF (spec, FONT_AVGWIDTH_INDEX),
2702 AREF (entity, FONT_AVGWIDTH_INDEX)))
2703 prop = FONT_SPEC_MAX;
2704 if (prop < FONT_SPEC_MAX)
2705 val = Fcons (entity, val);
2707 return (Fvconcat (1, &val));
2711 /* Return a list of vectors of font-entities matching with SPEC on
2712 FRAME. Each elements in the list is a vector of entities from the
2713 same font-driver. */
2715 Lisp_Object
2716 font_list_entities (struct frame *f, Lisp_Object spec)
2718 struct font_driver_list *driver_list = f->font_driver_list;
2719 Lisp_Object ftype, val;
2720 Lisp_Object list = Qnil;
2721 int size;
2722 bool need_filtering = 0;
2723 int i;
2725 eassert (FONT_SPEC_P (spec));
2727 if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
2728 size = XINT (AREF (spec, FONT_SIZE_INDEX));
2729 else if (FLOATP (AREF (spec, FONT_SIZE_INDEX)))
2730 size = font_pixel_size (f, spec);
2731 else
2732 size = 0;
2734 ftype = AREF (spec, FONT_TYPE_INDEX);
2735 for (i = FONT_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX; i++)
2736 ASET (scratch_font_spec, i, AREF (spec, i));
2737 for (i = FONT_WEIGHT_INDEX; i < FONT_EXTRA_INDEX; i++)
2738 if (i != FONT_SPACING_INDEX)
2740 ASET (scratch_font_spec, i, Qnil);
2741 if (! NILP (AREF (spec, i)))
2742 need_filtering = 1;
2744 ASET (scratch_font_spec, FONT_SPACING_INDEX, AREF (spec, FONT_SPACING_INDEX));
2745 ASET (scratch_font_spec, FONT_EXTRA_INDEX, AREF (spec, FONT_EXTRA_INDEX));
2747 for (; driver_list; driver_list = driver_list->next)
2748 if (driver_list->on
2749 && (NILP (ftype) || EQ (driver_list->driver->type, ftype)))
2751 Lisp_Object cache = font_get_cache (f, driver_list->driver);
2753 ASET (scratch_font_spec, FONT_TYPE_INDEX, driver_list->driver->type);
2754 val = assoc_no_quit (scratch_font_spec, XCDR (cache));
2755 if (CONSP (val))
2756 val = XCDR (val);
2757 else
2759 val = driver_list->driver->list (f, scratch_font_spec);
2760 if (!NILP (val))
2762 Lisp_Object copy = copy_font_spec (scratch_font_spec);
2764 val = Fvconcat (1, &val);
2765 ASET (copy, FONT_TYPE_INDEX, driver_list->driver->type);
2766 XSETCDR (cache, Fcons (Fcons (copy, val), XCDR (cache)));
2769 if (VECTORP (val) && ASIZE (val) > 0
2770 && (need_filtering
2771 || ! NILP (Vface_ignored_fonts)))
2772 val = font_delete_unmatched (val, need_filtering ? spec : Qnil, size);
2773 if (VECTORP (val) && ASIZE (val) > 0)
2774 list = Fcons (val, list);
2777 list = Fnreverse (list);
2778 FONT_ADD_LOG ("list", spec, list);
2779 return list;
2783 /* Return a font entity matching with SPEC on FRAME. ATTRS, if non
2784 nil, is an array of face's attributes, which specifies preferred
2785 font-related attributes. */
2787 static Lisp_Object
2788 font_matching_entity (struct frame *f, Lisp_Object *attrs, Lisp_Object spec)
2790 struct font_driver_list *driver_list = f->font_driver_list;
2791 Lisp_Object ftype, size, entity;
2792 Lisp_Object work = copy_font_spec (spec);
2794 ftype = AREF (spec, FONT_TYPE_INDEX);
2795 size = AREF (spec, FONT_SIZE_INDEX);
2797 if (FLOATP (size))
2798 ASET (work, FONT_SIZE_INDEX, make_number (font_pixel_size (f, spec)));
2799 FONT_SET_STYLE (work, FONT_WEIGHT_INDEX, attrs[LFACE_WEIGHT_INDEX]);
2800 FONT_SET_STYLE (work, FONT_SLANT_INDEX, attrs[LFACE_SLANT_INDEX]);
2801 FONT_SET_STYLE (work, FONT_WIDTH_INDEX, attrs[LFACE_SWIDTH_INDEX]);
2803 entity = Qnil;
2804 for (; driver_list; driver_list = driver_list->next)
2805 if (driver_list->on
2806 && (NILP (ftype) || EQ (driver_list->driver->type, ftype)))
2808 Lisp_Object cache = font_get_cache (f, driver_list->driver);
2810 ASET (work, FONT_TYPE_INDEX, driver_list->driver->type);
2811 entity = assoc_no_quit (work, XCDR (cache));
2812 if (CONSP (entity))
2813 entity = AREF (XCDR (entity), 0);
2814 else
2816 entity = driver_list->driver->match (f, work);
2817 if (!NILP (entity))
2819 Lisp_Object copy = copy_font_spec (work);
2820 Lisp_Object match = Fvector (1, &entity);
2822 ASET (copy, FONT_TYPE_INDEX, driver_list->driver->type);
2823 XSETCDR (cache, Fcons (Fcons (copy, match), XCDR (cache)));
2826 if (! NILP (entity))
2827 break;
2829 FONT_ADD_LOG ("match", work, entity);
2830 return entity;
2834 /* Open a font of ENTITY and PIXEL_SIZE on frame F, and return the
2835 opened font object. */
2837 static Lisp_Object
2838 font_open_entity (struct frame *f, Lisp_Object entity, int pixel_size)
2840 struct font_driver_list *driver_list;
2841 Lisp_Object objlist, size, val, font_object;
2842 struct font *font;
2843 int min_width, height, psize;
2845 eassert (FONT_ENTITY_P (entity));
2846 size = AREF (entity, FONT_SIZE_INDEX);
2847 if (XINT (size) != 0)
2848 pixel_size = XINT (size);
2850 val = AREF (entity, FONT_TYPE_INDEX);
2851 for (driver_list = f->font_driver_list;
2852 driver_list && ! EQ (driver_list->driver->type, val);
2853 driver_list = driver_list->next);
2854 if (! driver_list)
2855 return Qnil;
2857 for (objlist = AREF (entity, FONT_OBJLIST_INDEX); CONSP (objlist);
2858 objlist = XCDR (objlist))
2860 Lisp_Object fn = XCAR (objlist);
2861 if (! NILP (AREF (fn, FONT_TYPE_INDEX))
2862 && XFONT_OBJECT (fn)->pixel_size == pixel_size)
2864 if (driver_list->driver->cached_font_ok == NULL
2865 || driver_list->driver->cached_font_ok (f, fn, entity))
2866 return fn;
2870 /* We always open a font of manageable size; i.e non-zero average
2871 width and height. */
2872 for (psize = pixel_size; ; psize++)
2874 font_object = driver_list->driver->open (f, entity, psize);
2875 if (NILP (font_object))
2876 return Qnil;
2877 font = XFONT_OBJECT (font_object);
2878 if (font->average_width > 0 && font->height > 0)
2879 break;
2881 ASET (font_object, FONT_SIZE_INDEX, make_number (pixel_size));
2882 FONT_ADD_LOG ("open", entity, font_object);
2883 ASET (entity, FONT_OBJLIST_INDEX,
2884 Fcons (font_object, AREF (entity, FONT_OBJLIST_INDEX)));
2886 font = XFONT_OBJECT (font_object);
2887 min_width = (font->min_width ? font->min_width
2888 : font->average_width ? font->average_width
2889 : font->space_width ? font->space_width
2890 : 1);
2891 height = (font->height ? font->height : 1);
2892 #ifdef HAVE_WINDOW_SYSTEM
2893 FRAME_DISPLAY_INFO (f)->n_fonts++;
2894 if (FRAME_DISPLAY_INFO (f)->n_fonts == 1)
2896 FRAME_SMALLEST_CHAR_WIDTH (f) = min_width;
2897 FRAME_SMALLEST_FONT_HEIGHT (f) = height;
2898 f->fonts_changed = 1;
2900 else
2902 if (FRAME_SMALLEST_CHAR_WIDTH (f) > min_width)
2903 FRAME_SMALLEST_CHAR_WIDTH (f) = min_width, f->fonts_changed = 1;
2904 if (FRAME_SMALLEST_FONT_HEIGHT (f) > height)
2905 FRAME_SMALLEST_FONT_HEIGHT (f) = height, f->fonts_changed = 1;
2907 #endif
2909 return font_object;
2913 /* Close FONT_OBJECT that is opened on frame F. */
2915 static void
2916 font_close_object (struct frame *f, Lisp_Object font_object)
2918 struct font *font = XFONT_OBJECT (font_object);
2920 if (NILP (AREF (font_object, FONT_TYPE_INDEX)))
2921 /* Already closed. */
2922 return;
2923 FONT_ADD_LOG ("close", font_object, Qnil);
2924 font->driver->close (font);
2925 #ifdef HAVE_WINDOW_SYSTEM
2926 eassert (FRAME_DISPLAY_INFO (f)->n_fonts);
2927 FRAME_DISPLAY_INFO (f)->n_fonts--;
2928 #endif
2932 /* Return 1 if FONT on F has a glyph for character C, 0 if not, -1 if
2933 FONT is a font-entity and it must be opened to check. */
2936 font_has_char (struct frame *f, Lisp_Object font, int c)
2938 struct font *fontp;
2940 if (FONT_ENTITY_P (font))
2942 Lisp_Object type = AREF (font, FONT_TYPE_INDEX);
2943 struct font_driver_list *driver_list;
2945 for (driver_list = f->font_driver_list;
2946 driver_list && ! EQ (driver_list->driver->type, type);
2947 driver_list = driver_list->next);
2948 if (! driver_list)
2949 return 0;
2950 if (! driver_list->driver->has_char)
2951 return -1;
2952 return driver_list->driver->has_char (font, c);
2955 eassert (FONT_OBJECT_P (font));
2956 fontp = XFONT_OBJECT (font);
2957 if (fontp->driver->has_char)
2959 int result = fontp->driver->has_char (font, c);
2961 if (result >= 0)
2962 return result;
2964 return (fontp->driver->encode_char (fontp, c) != FONT_INVALID_CODE);
2968 /* Return the glyph ID of FONT_OBJECT for character C. */
2970 static unsigned
2971 font_encode_char (Lisp_Object font_object, int c)
2973 struct font *font;
2975 eassert (FONT_OBJECT_P (font_object));
2976 font = XFONT_OBJECT (font_object);
2977 return font->driver->encode_char (font, c);
2981 /* Return the name of FONT_OBJECT. */
2983 Lisp_Object
2984 font_get_name (Lisp_Object font_object)
2986 eassert (FONT_OBJECT_P (font_object));
2987 return AREF (font_object, FONT_NAME_INDEX);
2991 /* Create a new font spec from FONT_NAME, and return it. If FONT_NAME
2992 could not be parsed by font_parse_name, return Qnil. */
2994 Lisp_Object
2995 font_spec_from_name (Lisp_Object font_name)
2997 Lisp_Object spec = Ffont_spec (0, NULL);
2999 CHECK_STRING (font_name);
3000 if (font_parse_name (SSDATA (font_name), SBYTES (font_name), spec) == -1)
3001 return Qnil;
3002 font_put_extra (spec, QCname, font_name);
3003 font_put_extra (spec, QCuser_spec, font_name);
3004 return spec;
3008 void
3009 font_clear_prop (Lisp_Object *attrs, enum font_property_index prop)
3011 Lisp_Object font = attrs[LFACE_FONT_INDEX];
3013 if (! FONTP (font))
3014 return;
3016 if (! NILP (Ffont_get (font, QCname)))
3018 font = copy_font_spec (font);
3019 font_put_extra (font, QCname, Qnil);
3022 if (NILP (AREF (font, prop))
3023 && prop != FONT_FAMILY_INDEX
3024 && prop != FONT_FOUNDRY_INDEX
3025 && prop != FONT_WIDTH_INDEX
3026 && prop != FONT_SIZE_INDEX)
3027 return;
3028 if (EQ (font, attrs[LFACE_FONT_INDEX]))
3029 font = copy_font_spec (font);
3030 ASET (font, prop, Qnil);
3031 if (prop == FONT_FAMILY_INDEX || prop == FONT_FOUNDRY_INDEX)
3033 if (prop == FONT_FAMILY_INDEX)
3035 ASET (font, FONT_FOUNDRY_INDEX, Qnil);
3036 /* If we are setting the font family, we must also clear
3037 FONT_WIDTH_INDEX to avoid rejecting families that lack
3038 support for some widths. */
3039 ASET (font, FONT_WIDTH_INDEX, Qnil);
3041 ASET (font, FONT_ADSTYLE_INDEX, Qnil);
3042 ASET (font, FONT_REGISTRY_INDEX, Qnil);
3043 ASET (font, FONT_SIZE_INDEX, Qnil);
3044 ASET (font, FONT_DPI_INDEX, Qnil);
3045 ASET (font, FONT_SPACING_INDEX, Qnil);
3046 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3048 else if (prop == FONT_SIZE_INDEX)
3050 ASET (font, FONT_DPI_INDEX, Qnil);
3051 ASET (font, FONT_SPACING_INDEX, Qnil);
3052 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3054 else if (prop == FONT_WIDTH_INDEX)
3055 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3056 attrs[LFACE_FONT_INDEX] = font;
3059 /* Select a font from ENTITIES (list of font-entity vectors) that
3060 supports C and is the best match for ATTRS and PIXEL_SIZE. */
3062 static Lisp_Object
3063 font_select_entity (struct frame *f, Lisp_Object entities,
3064 Lisp_Object *attrs, int pixel_size, int c)
3066 Lisp_Object font_entity;
3067 Lisp_Object prefer;
3068 int i;
3070 if (NILP (XCDR (entities))
3071 && ASIZE (XCAR (entities)) == 1)
3073 font_entity = AREF (XCAR (entities), 0);
3074 if (c < 0 || font_has_char (f, font_entity, c) > 0)
3075 return font_entity;
3076 return Qnil;
3079 /* Sort fonts by properties specified in ATTRS. */
3080 prefer = scratch_font_prefer;
3082 for (i = FONT_WEIGHT_INDEX; i <= FONT_SIZE_INDEX; i++)
3083 ASET (prefer, i, Qnil);
3084 if (FONTP (attrs[LFACE_FONT_INDEX]))
3086 Lisp_Object face_font = attrs[LFACE_FONT_INDEX];
3088 for (i = FONT_WEIGHT_INDEX; i <= FONT_SIZE_INDEX; i++)
3089 ASET (prefer, i, AREF (face_font, i));
3091 if (NILP (AREF (prefer, FONT_WEIGHT_INDEX)))
3092 FONT_SET_STYLE (prefer, FONT_WEIGHT_INDEX, attrs[LFACE_WEIGHT_INDEX]);
3093 if (NILP (AREF (prefer, FONT_SLANT_INDEX)))
3094 FONT_SET_STYLE (prefer, FONT_SLANT_INDEX, attrs[LFACE_SLANT_INDEX]);
3095 if (NILP (AREF (prefer, FONT_WIDTH_INDEX)))
3096 FONT_SET_STYLE (prefer, FONT_WIDTH_INDEX, attrs[LFACE_SWIDTH_INDEX]);
3097 ASET (prefer, FONT_SIZE_INDEX, make_number (pixel_size));
3099 return font_sort_entities (entities, prefer, f, c);
3102 /* Return a font-entity that satisfies SPEC and is the best match for
3103 face's font related attributes in ATTRS. C, if not negative, is a
3104 character that the entity must support. */
3106 Lisp_Object
3107 font_find_for_lface (struct frame *f, Lisp_Object *attrs, Lisp_Object spec, int c)
3109 Lisp_Object work;
3110 Lisp_Object entities, val;
3111 Lisp_Object foundry[3], *family, registry[3], adstyle[3];
3112 int pixel_size;
3113 int i, j, k, l;
3114 USE_SAFE_ALLOCA;
3116 registry[0] = AREF (spec, FONT_REGISTRY_INDEX);
3117 if (NILP (registry[0]))
3119 registry[0] = DEFAULT_ENCODING;
3120 registry[1] = Qascii_0;
3121 registry[2] = zero_vector;
3123 else
3124 registry[1] = zero_vector;
3126 if (c >= 0 && ! NILP (AREF (spec, FONT_REGISTRY_INDEX)))
3128 struct charset *encoding, *repertory;
3130 if (font_registry_charsets (AREF (spec, FONT_REGISTRY_INDEX),
3131 &encoding, &repertory) < 0)
3132 return Qnil;
3133 if (repertory
3134 && ENCODE_CHAR (repertory, c) == CHARSET_INVALID_CODE (repertory))
3135 return Qnil;
3136 else if (c > encoding->max_char)
3137 return Qnil;
3140 work = copy_font_spec (spec);
3141 ASET (work, FONT_TYPE_INDEX, AREF (spec, FONT_TYPE_INDEX));
3142 pixel_size = font_pixel_size (f, spec);
3143 if (pixel_size == 0 && INTEGERP (attrs[LFACE_HEIGHT_INDEX]))
3145 double pt = XINT (attrs[LFACE_HEIGHT_INDEX]);
3147 pixel_size = POINT_TO_PIXEL (pt / 10, FRAME_RES_Y (f));
3148 if (pixel_size < 1)
3149 pixel_size = 1;
3151 ASET (work, FONT_SIZE_INDEX, Qnil);
3152 foundry[0] = AREF (work, FONT_FOUNDRY_INDEX);
3153 if (! NILP (foundry[0]))
3154 foundry[1] = zero_vector;
3155 else if (STRINGP (attrs[LFACE_FOUNDRY_INDEX]))
3157 val = attrs[LFACE_FOUNDRY_INDEX];
3158 foundry[0] = font_intern_prop (SSDATA (val), SBYTES (val), 1);
3159 foundry[1] = Qnil;
3160 foundry[2] = zero_vector;
3162 else
3163 foundry[0] = Qnil, foundry[1] = zero_vector;
3165 adstyle[0] = AREF (work, FONT_ADSTYLE_INDEX);
3166 if (! NILP (adstyle[0]))
3167 adstyle[1] = zero_vector;
3168 else if (FONTP (attrs[LFACE_FONT_INDEX]))
3170 Lisp_Object face_font = attrs[LFACE_FONT_INDEX];
3172 if (! NILP (AREF (face_font, FONT_ADSTYLE_INDEX)))
3174 adstyle[0] = AREF (face_font, FONT_ADSTYLE_INDEX);
3175 adstyle[1] = Qnil;
3176 adstyle[2] = zero_vector;
3178 else
3179 adstyle[0] = Qnil, adstyle[1] = zero_vector;
3181 else
3182 adstyle[0] = Qnil, adstyle[1] = zero_vector;
3185 val = AREF (work, FONT_FAMILY_INDEX);
3186 if (NILP (val) && STRINGP (attrs[LFACE_FAMILY_INDEX]))
3188 val = attrs[LFACE_FAMILY_INDEX];
3189 val = font_intern_prop (SSDATA (val), SBYTES (val), 1);
3191 if (NILP (val))
3193 family = alloca ((sizeof family[0]) * 2);
3194 family[0] = Qnil;
3195 family[1] = zero_vector; /* terminator. */
3197 else
3199 Lisp_Object alters
3200 = Fassoc_string (val, Vface_alternative_font_family_alist, Qt);
3202 if (! NILP (alters))
3204 EMACS_INT alterslen = XFASTINT (Flength (alters));
3205 SAFE_ALLOCA_LISP (family, alterslen + 2);
3206 for (i = 0; CONSP (alters); i++, alters = XCDR (alters))
3207 family[i] = XCAR (alters);
3208 if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
3209 family[i++] = Qnil;
3210 family[i] = zero_vector;
3212 else
3214 family = alloca ((sizeof family[0]) * 3);
3215 i = 0;
3216 family[i++] = val;
3217 if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
3218 family[i++] = Qnil;
3219 family[i] = zero_vector;
3223 for (i = 0; SYMBOLP (family[i]); i++)
3225 ASET (work, FONT_FAMILY_INDEX, family[i]);
3226 for (j = 0; SYMBOLP (foundry[j]); j++)
3228 ASET (work, FONT_FOUNDRY_INDEX, foundry[j]);
3229 for (k = 0; SYMBOLP (registry[k]); k++)
3231 ASET (work, FONT_REGISTRY_INDEX, registry[k]);
3232 for (l = 0; SYMBOLP (adstyle[l]); l++)
3234 ASET (work, FONT_ADSTYLE_INDEX, adstyle[l]);
3235 entities = font_list_entities (f, work);
3236 if (! NILP (entities))
3238 val = font_select_entity (f, entities,
3239 attrs, pixel_size, c);
3240 if (! NILP (val))
3242 SAFE_FREE ();
3243 return val;
3251 SAFE_FREE ();
3252 return Qnil;
3256 Lisp_Object
3257 font_open_for_lface (struct frame *f, Lisp_Object entity, Lisp_Object *attrs, Lisp_Object spec)
3259 int size;
3261 if (INTEGERP (AREF (entity, FONT_SIZE_INDEX))
3262 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
3263 size = XINT (AREF (entity, FONT_SIZE_INDEX));
3264 else
3266 if (FONT_SPEC_P (spec) && ! NILP (AREF (spec, FONT_SIZE_INDEX)))
3267 size = font_pixel_size (f, spec);
3268 else
3270 double pt;
3271 if (INTEGERP (attrs[LFACE_HEIGHT_INDEX]))
3272 pt = XINT (attrs[LFACE_HEIGHT_INDEX]);
3273 else
3275 struct face *def = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3276 Lisp_Object height = def->lface[LFACE_HEIGHT_INDEX];
3277 eassert (INTEGERP (height));
3278 pt = XINT (height);
3281 pt /= 10;
3282 size = POINT_TO_PIXEL (pt, FRAME_RES_Y (f));
3283 #ifdef HAVE_NS
3284 if (size == 0)
3286 Lisp_Object ffsize = get_frame_param (f, Qfontsize);
3287 size = (NUMBERP (ffsize)
3288 ? POINT_TO_PIXEL (XINT (ffsize), FRAME_RES_Y (f)) : 0);
3290 #endif
3292 size *= font_rescale_ratio (entity);
3295 return font_open_entity (f, entity, size);
3299 /* Find a font that satisfies SPEC and is the best match for
3300 face's attributes in ATTRS on FRAME, and return the opened
3301 font-object. */
3303 Lisp_Object
3304 font_load_for_lface (struct frame *f, Lisp_Object *attrs, Lisp_Object spec)
3306 Lisp_Object entity, name;
3308 entity = font_find_for_lface (f, attrs, spec, -1);
3309 if (NILP (entity))
3311 /* No font is listed for SPEC, but each font-backend may have
3312 different criteria about "font matching". So, try it. */
3313 entity = font_matching_entity (f, attrs, spec);
3314 if (NILP (entity))
3315 return Qnil;
3317 /* Don't lose the original name that was put in initially. We need
3318 it to re-apply the font when font parameters (like hinting or dpi) have
3319 changed. */
3320 entity = font_open_for_lface (f, entity, attrs, spec);
3321 if (!NILP (entity))
3323 name = Ffont_get (spec, QCuser_spec);
3324 if (STRINGP (name)) font_put_extra (entity, QCuser_spec, name);
3326 return entity;
3330 /* Make FACE on frame F ready to use the font opened for FACE. */
3332 void
3333 font_prepare_for_face (struct frame *f, struct face *face)
3335 if (face->font->driver->prepare_face)
3336 face->font->driver->prepare_face (f, face);
3340 /* Make FACE on frame F stop using the font opened for FACE. */
3342 void
3343 font_done_for_face (struct frame *f, struct face *face)
3345 if (face->font->driver->done_face)
3346 face->font->driver->done_face (f, face);
3347 face->extra = NULL;
3351 /* Open a font that is a match for font-spec SPEC on frame F. If no proper
3352 font is found, return Qnil. */
3354 Lisp_Object
3355 font_open_by_spec (struct frame *f, Lisp_Object spec)
3357 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3359 /* We set up the default font-related attributes of a face to prefer
3360 a moderate font. */
3361 attrs[LFACE_FAMILY_INDEX] = attrs[LFACE_FOUNDRY_INDEX] = Qnil;
3362 attrs[LFACE_SWIDTH_INDEX] = attrs[LFACE_WEIGHT_INDEX]
3363 = attrs[LFACE_SLANT_INDEX] = Qnormal;
3364 #ifndef HAVE_NS
3365 attrs[LFACE_HEIGHT_INDEX] = make_number (120);
3366 #else
3367 attrs[LFACE_HEIGHT_INDEX] = make_number (0);
3368 #endif
3369 attrs[LFACE_FONT_INDEX] = Qnil;
3371 return font_load_for_lface (f, attrs, spec);
3375 /* Open a font that matches NAME on frame F. If no proper font is
3376 found, return Qnil. */
3378 Lisp_Object
3379 font_open_by_name (struct frame *f, Lisp_Object name)
3381 Lisp_Object args[2];
3382 Lisp_Object spec, ret;
3384 args[0] = QCname;
3385 args[1] = name;
3386 spec = Ffont_spec (2, args);
3387 ret = font_open_by_spec (f, spec);
3388 /* Do not lose name originally put in. */
3389 if (!NILP (ret))
3390 font_put_extra (ret, QCuser_spec, args[1]);
3392 return ret;
3396 /* Register font-driver DRIVER. This function is used in two ways.
3398 The first is with frame F non-NULL. In this case, make DRIVER
3399 available (but not yet activated) on F. All frame creators
3400 (e.g. Fx_create_frame) must call this function at least once with
3401 an available font-driver.
3403 The second is with frame F NULL. In this case, DRIVER is globally
3404 registered in the variable `font_driver_list'. All font-driver
3405 implementations must call this function in its syms_of_XXXX
3406 (e.g. syms_of_xfont). */
3408 void
3409 register_font_driver (struct font_driver *driver, struct frame *f)
3411 struct font_driver_list *root = f ? f->font_driver_list : font_driver_list;
3412 struct font_driver_list *prev, *list;
3414 #ifdef HAVE_WINDOW_SYSTEM
3415 if (f && ! driver->draw)
3416 error ("Unusable font driver for a frame: %s",
3417 SDATA (SYMBOL_NAME (driver->type)));
3418 #endif /* HAVE_WINDOW_SYSTEM */
3420 for (prev = NULL, list = root; list; prev = list, list = list->next)
3421 if (EQ (list->driver->type, driver->type))
3422 error ("Duplicated font driver: %s", SDATA (SYMBOL_NAME (driver->type)));
3424 list = xmalloc (sizeof *list);
3425 list->on = 0;
3426 list->driver = driver;
3427 list->next = NULL;
3428 if (prev)
3429 prev->next = list;
3430 else if (f)
3431 f->font_driver_list = list;
3432 else
3433 font_driver_list = list;
3434 if (! f)
3435 num_font_drivers++;
3438 void
3439 free_font_driver_list (struct frame *f)
3441 struct font_driver_list *list, *next;
3443 for (list = f->font_driver_list; list; list = next)
3445 next = list->next;
3446 xfree (list);
3448 f->font_driver_list = NULL;
3452 /* Make the frame F use font backends listed in NEW_DRIVERS (list of
3453 symbols, e.g. xft, x). If NEW_DRIVERS is t, make F use all
3454 available font drivers. If NEW_DRIVERS is nil, finalize all drivers.
3456 A caller must free all realized faces if any in advance. The
3457 return value is a list of font backends actually made used on
3458 F. */
3460 Lisp_Object
3461 font_update_drivers (struct frame *f, Lisp_Object new_drivers)
3463 Lisp_Object active_drivers = Qnil;
3464 struct font_driver_list *list;
3466 /* At first, turn off non-requested drivers, and turn on requested
3467 drivers. */
3468 for (list = f->font_driver_list; list; list = list->next)
3470 struct font_driver *driver = list->driver;
3471 if ((EQ (new_drivers, Qt) || ! NILP (Fmemq (driver->type, new_drivers)))
3472 != list->on)
3474 if (list->on)
3476 if (driver->end_for_frame)
3477 driver->end_for_frame (f);
3478 font_finish_cache (f, driver);
3479 list->on = 0;
3481 else
3483 if (! driver->start_for_frame
3484 || driver->start_for_frame (f) == 0)
3486 font_prepare_cache (f, driver);
3487 list->on = 1;
3493 if (NILP (new_drivers))
3494 return Qnil;
3496 if (! EQ (new_drivers, Qt))
3498 /* Re-order the driver list according to new_drivers. */
3499 struct font_driver_list **list_table, **next;
3500 Lisp_Object tail;
3501 int i;
3503 list_table = alloca (sizeof list_table[0] * (num_font_drivers + 1));
3504 for (i = 0, tail = new_drivers; ! NILP (tail); tail = XCDR (tail))
3506 for (list = f->font_driver_list; list; list = list->next)
3507 if (list->on && EQ (list->driver->type, XCAR (tail)))
3508 break;
3509 if (list)
3510 list_table[i++] = list;
3512 for (list = f->font_driver_list; list; list = list->next)
3513 if (! list->on)
3514 list_table[i++] = list;
3515 list_table[i] = NULL;
3517 next = &f->font_driver_list;
3518 for (i = 0; list_table[i]; i++)
3520 *next = list_table[i];
3521 next = &(*next)->next;
3523 *next = NULL;
3525 if (! f->font_driver_list->on)
3526 { /* None of the drivers is enabled: enable them all.
3527 Happens if you set the list of drivers to (xft x) in your .emacs
3528 and then use it under w32 or ns. */
3529 for (list = f->font_driver_list; list; list = list->next)
3531 struct font_driver *driver = list->driver;
3532 eassert (! list->on);
3533 if (! driver->start_for_frame
3534 || driver->start_for_frame (f) == 0)
3536 font_prepare_cache (f, driver);
3537 list->on = 1;
3543 for (list = f->font_driver_list; list; list = list->next)
3544 if (list->on)
3545 active_drivers = nconc2 (active_drivers, list1 (list->driver->type));
3546 return active_drivers;
3550 font_put_frame_data (struct frame *f, struct font_driver *driver, void *data)
3552 struct font_data_list *list, *prev;
3554 for (prev = NULL, list = f->font_data_list; list;
3555 prev = list, list = list->next)
3556 if (list->driver == driver)
3557 break;
3558 if (! data)
3560 if (list)
3562 if (prev)
3563 prev->next = list->next;
3564 else
3565 f->font_data_list = list->next;
3566 xfree (list);
3568 return 0;
3571 if (! list)
3573 list = xmalloc (sizeof *list);
3574 list->driver = driver;
3575 list->next = f->font_data_list;
3576 f->font_data_list = list;
3578 list->data = data;
3579 return 0;
3583 void *
3584 font_get_frame_data (struct frame *f, struct font_driver *driver)
3586 struct font_data_list *list;
3588 for (list = f->font_data_list; list; list = list->next)
3589 if (list->driver == driver)
3590 break;
3591 if (! list)
3592 return NULL;
3593 return list->data;
3597 /* Sets attributes on a font. Any properties that appear in ALIST and
3598 BOOLEAN_PROPERTIES or NON_BOOLEAN_PROPERTIES are set on the font.
3599 BOOLEAN_PROPERTIES and NON_BOOLEAN_PROPERTIES are NULL-terminated
3600 arrays of strings. This function is intended for use by the font
3601 drivers to implement their specific font_filter_properties. */
3602 void
3603 font_filter_properties (Lisp_Object font,
3604 Lisp_Object alist,
3605 const char *const boolean_properties[],
3606 const char *const non_boolean_properties[])
3608 Lisp_Object it;
3609 int i;
3611 /* Set boolean values to Qt or Qnil. */
3612 for (i = 0; boolean_properties[i] != NULL; ++i)
3613 for (it = alist; ! NILP (it); it = XCDR (it))
3615 Lisp_Object key = XCAR (XCAR (it));
3616 Lisp_Object val = XCDR (XCAR (it));
3617 char *keystr = SSDATA (SYMBOL_NAME (key));
3619 if (strcmp (boolean_properties[i], keystr) == 0)
3621 const char *str = INTEGERP (val) ? (XINT (val) ? "true" : "false")
3622 : SYMBOLP (val) ? SSDATA (SYMBOL_NAME (val))
3623 : "true";
3625 if (strcmp ("false", str) == 0 || strcmp ("False", str) == 0
3626 || strcmp ("FALSE", str) == 0 || strcmp ("FcFalse", str) == 0
3627 || strcmp ("off", str) == 0 || strcmp ("OFF", str) == 0
3628 || strcmp ("Off", str) == 0)
3629 val = Qnil;
3630 else
3631 val = Qt;
3633 Ffont_put (font, key, val);
3637 for (i = 0; non_boolean_properties[i] != NULL; ++i)
3638 for (it = alist; ! NILP (it); it = XCDR (it))
3640 Lisp_Object key = XCAR (XCAR (it));
3641 Lisp_Object val = XCDR (XCAR (it));
3642 char *keystr = SSDATA (SYMBOL_NAME (key));
3643 if (strcmp (non_boolean_properties[i], keystr) == 0)
3644 Ffont_put (font, key, val);
3649 /* Return the font used to draw character C by FACE at buffer position
3650 POS in window W. If STRING is non-nil, it is a string containing C
3651 at index POS. If C is negative, get C from the current buffer or
3652 STRING. */
3654 static Lisp_Object
3655 font_at (int c, ptrdiff_t pos, struct face *face, struct window *w,
3656 Lisp_Object string)
3658 struct frame *f;
3659 bool multibyte;
3660 Lisp_Object font_object;
3662 multibyte = (NILP (string)
3663 ? ! NILP (BVAR (current_buffer, enable_multibyte_characters))
3664 : STRING_MULTIBYTE (string));
3665 if (c < 0)
3667 if (NILP (string))
3669 if (multibyte)
3671 ptrdiff_t pos_byte = CHAR_TO_BYTE (pos);
3673 c = FETCH_CHAR (pos_byte);
3675 else
3676 c = FETCH_BYTE (pos);
3678 else
3680 unsigned char *str;
3682 multibyte = STRING_MULTIBYTE (string);
3683 if (multibyte)
3685 ptrdiff_t pos_byte = string_char_to_byte (string, pos);
3687 str = SDATA (string) + pos_byte;
3688 c = STRING_CHAR (str);
3690 else
3691 c = SDATA (string)[pos];
3695 f = XFRAME (w->frame);
3696 if (! FRAME_WINDOW_P (f))
3697 return Qnil;
3698 if (! face)
3700 int face_id;
3701 ptrdiff_t endptr;
3703 if (STRINGP (string))
3704 face_id = face_at_string_position (w, string, pos, 0, &endptr,
3705 DEFAULT_FACE_ID, 0);
3706 else
3707 face_id = face_at_buffer_position (w, pos, &endptr,
3708 pos + 100, 0, -1);
3709 face = FACE_FROM_ID (f, face_id);
3711 if (multibyte)
3713 int face_id = FACE_FOR_CHAR (f, face, c, pos, string);
3714 face = FACE_FROM_ID (f, face_id);
3716 if (! face->font)
3717 return Qnil;
3719 XSETFONT (font_object, face->font);
3720 return font_object;
3724 #ifdef HAVE_WINDOW_SYSTEM
3726 /* Check how many characters after character/byte position POS/POS_BYTE
3727 (at most to *LIMIT) can be displayed by the same font in the window W.
3728 FACE, if non-NULL, is the face selected for the character at POS.
3729 If STRING is not nil, it is the string to check instead of the current
3730 buffer. In that case, FACE must be not NULL.
3732 The return value is the font-object for the character at POS.
3733 *LIMIT is set to the position where that font can't be used.
3735 It is assured that the current buffer (or STRING) is multibyte. */
3737 Lisp_Object
3738 font_range (ptrdiff_t pos, ptrdiff_t pos_byte, ptrdiff_t *limit,
3739 struct window *w, struct face *face, Lisp_Object string)
3741 ptrdiff_t ignore;
3742 int c;
3743 Lisp_Object font_object = Qnil;
3745 if (NILP (string))
3747 if (! face)
3749 int face_id;
3751 face_id = face_at_buffer_position (w, pos, &ignore,
3752 *limit, 0, -1);
3753 face = FACE_FROM_ID (XFRAME (w->frame), face_id);
3756 else
3757 eassert (face);
3759 while (pos < *limit)
3761 Lisp_Object category;
3763 if (NILP (string))
3764 FETCH_CHAR_ADVANCE_NO_CHECK (c, pos, pos_byte);
3765 else
3766 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, string, pos, pos_byte);
3767 category = CHAR_TABLE_REF (Vunicode_category_table, c);
3768 if (INTEGERP (category)
3769 && (XINT (category) == UNICODE_CATEGORY_Cf
3770 || CHAR_VARIATION_SELECTOR_P (c)))
3771 continue;
3772 if (NILP (font_object))
3774 font_object = font_for_char (face, c, pos - 1, string);
3775 if (NILP (font_object))
3776 return Qnil;
3777 continue;
3779 if (font_encode_char (font_object, c) == FONT_INVALID_CODE)
3780 *limit = pos - 1;
3782 return font_object;
3784 #endif
3787 /* Lisp API. */
3789 DEFUN ("fontp", Ffontp, Sfontp, 1, 2, 0,
3790 doc: /* Return t if OBJECT is a font-spec, font-entity, or font-object.
3791 Return nil otherwise.
3792 Optional 2nd argument EXTRA-TYPE, if non-nil, specifies to check
3793 which kind of font it is. It must be one of `font-spec', `font-entity',
3794 `font-object'. */)
3795 (Lisp_Object object, Lisp_Object extra_type)
3797 if (NILP (extra_type))
3798 return (FONTP (object) ? Qt : Qnil);
3799 if (EQ (extra_type, Qfont_spec))
3800 return (FONT_SPEC_P (object) ? Qt : Qnil);
3801 if (EQ (extra_type, Qfont_entity))
3802 return (FONT_ENTITY_P (object) ? Qt : Qnil);
3803 if (EQ (extra_type, Qfont_object))
3804 return (FONT_OBJECT_P (object) ? Qt : Qnil);
3805 wrong_type_argument (intern ("font-extra-type"), extra_type);
3808 DEFUN ("font-spec", Ffont_spec, Sfont_spec, 0, MANY, 0,
3809 doc: /* Return a newly created font-spec with arguments as properties.
3811 ARGS must come in pairs KEY VALUE of font properties. KEY must be a
3812 valid font property name listed below:
3814 `:family', `:weight', `:slant', `:width'
3816 They are the same as face attributes of the same name. See
3817 `set-face-attribute'.
3819 `:foundry'
3821 VALUE must be a string or a symbol specifying the font foundry, e.g. ``misc''.
3823 `:adstyle'
3825 VALUE must be a string or a symbol specifying the additional
3826 typographic style information of a font, e.g. ``sans''.
3828 `:registry'
3830 VALUE must be a string or a symbol specifying the charset registry and
3831 encoding of a font, e.g. ``iso8859-1''.
3833 `:size'
3835 VALUE must be a non-negative integer or a floating point number
3836 specifying the font size. It specifies the font size in pixels (if
3837 VALUE is an integer), or in points (if VALUE is a float).
3839 `:name'
3841 VALUE must be a string of XLFD-style or fontconfig-style font name.
3843 `:script'
3845 VALUE must be a symbol representing a script that the font must
3846 support. It may be a symbol representing a subgroup of a script
3847 listed in the variable `script-representative-chars'.
3849 `:lang'
3851 VALUE must be a symbol of two-letter ISO-639 language names,
3852 e.g. `ja'.
3854 `:otf'
3856 VALUE must be a list (SCRIPT-TAG LANGSYS-TAG GSUB [ GPOS ]) to specify
3857 required OpenType features.
3859 SCRIPT-TAG: OpenType script tag symbol (e.g. `deva').
3860 LANGSYS-TAG: OpenType language system tag symbol,
3861 or nil for the default language system.
3862 GSUB: List of OpenType GSUB feature tag symbols, or nil if none required.
3863 GPOS: List of OpenType GPOS feature tag symbols, or nil if none required.
3865 GSUB and GPOS may contain `nil' element. In such a case, the font
3866 must not have any of the remaining elements.
3868 For instance, if the VALUE is `(thai nil nil (mark))', the font must
3869 be an OpenType font whose GPOS table of `thai' script's default
3870 language system must contain `mark' feature.
3872 usage: (font-spec ARGS...) */)
3873 (ptrdiff_t nargs, Lisp_Object *args)
3875 Lisp_Object spec = font_make_spec ();
3876 ptrdiff_t i;
3878 for (i = 0; i < nargs; i += 2)
3880 Lisp_Object key = args[i], val;
3882 CHECK_SYMBOL (key);
3883 if (i + 1 >= nargs)
3884 error ("No value for key `%s'", SDATA (SYMBOL_NAME (key)));
3885 val = args[i + 1];
3887 if (EQ (key, QCname))
3889 CHECK_STRING (val);
3890 if (font_parse_name (SSDATA (val), SBYTES (val), spec) < 0)
3891 error ("Invalid font name: %s", SSDATA (val));
3892 font_put_extra (spec, key, val);
3894 else
3896 int idx = get_font_prop_index (key);
3898 if (idx >= 0)
3900 val = font_prop_validate (idx, Qnil, val);
3901 if (idx < FONT_EXTRA_INDEX)
3902 ASET (spec, idx, val);
3903 else
3904 font_put_extra (spec, key, val);
3906 else
3907 font_put_extra (spec, key, font_prop_validate (0, key, val));
3910 return spec;
3913 /* Return a copy of FONT as a font-spec. */
3914 Lisp_Object
3915 copy_font_spec (Lisp_Object font)
3917 Lisp_Object new_spec, tail, prev, extra;
3918 int i;
3920 CHECK_FONT (font);
3921 new_spec = font_make_spec ();
3922 for (i = 1; i < FONT_EXTRA_INDEX; i++)
3923 ASET (new_spec, i, AREF (font, i));
3924 extra = Fcopy_alist (AREF (font, FONT_EXTRA_INDEX));
3925 /* We must remove :font-entity property. */
3926 for (prev = Qnil, tail = extra; CONSP (tail); prev = tail, tail = XCDR (tail))
3927 if (EQ (XCAR (XCAR (tail)), QCfont_entity))
3929 if (NILP (prev))
3930 extra = XCDR (extra);
3931 else
3932 XSETCDR (prev, XCDR (tail));
3933 break;
3935 ASET (new_spec, FONT_EXTRA_INDEX, extra);
3936 return new_spec;
3939 /* Merge font-specs FROM and TO, and return a new font-spec.
3940 Every specified property in FROM overrides the corresponding
3941 property in TO. */
3942 Lisp_Object
3943 merge_font_spec (Lisp_Object from, Lisp_Object to)
3945 Lisp_Object extra, tail;
3946 int i;
3948 CHECK_FONT (from);
3949 CHECK_FONT (to);
3950 to = copy_font_spec (to);
3951 for (i = 0; i < FONT_EXTRA_INDEX; i++)
3952 ASET (to, i, AREF (from, i));
3953 extra = AREF (to, FONT_EXTRA_INDEX);
3954 for (tail = AREF (from, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail))
3955 if (! EQ (XCAR (XCAR (tail)), Qfont_entity))
3957 Lisp_Object slot = assq_no_quit (XCAR (XCAR (tail)), extra);
3959 if (! NILP (slot))
3960 XSETCDR (slot, XCDR (XCAR (tail)));
3961 else
3962 extra = Fcons (Fcons (XCAR (XCAR (tail)), XCDR (XCAR (tail))), extra);
3964 ASET (to, FONT_EXTRA_INDEX, extra);
3965 return to;
3968 DEFUN ("font-get", Ffont_get, Sfont_get, 2, 2, 0,
3969 doc: /* Return the value of FONT's property KEY.
3970 FONT is a font-spec, a font-entity, or a font-object.
3971 KEY is any symbol, but these are reserved for specific meanings:
3972 :family, :weight, :slant, :width, :foundry, :adstyle, :registry,
3973 :size, :name, :script, :otf
3974 See the documentation of `font-spec' for their meanings.
3975 In addition, if FONT is a font-entity or a font-object, values of
3976 :script and :otf are different from those of a font-spec as below:
3978 The value of :script may be a list of scripts that are supported by the font.
3980 The value of :otf is a cons (GSUB . GPOS) where GSUB and GPOS are lists
3981 representing the OpenType features supported by the font by this form:
3982 ((SCRIPT (LANGSYS FEATURE ...) ...) ...)
3983 SCRIPT, LANGSYS, and FEATURE are all symbols representing OpenType
3984 Layout tags. */)
3985 (Lisp_Object font, Lisp_Object key)
3987 int idx;
3988 Lisp_Object val;
3990 CHECK_FONT (font);
3991 CHECK_SYMBOL (key);
3993 idx = get_font_prop_index (key);
3994 if (idx >= FONT_WEIGHT_INDEX && idx <= FONT_WIDTH_INDEX)
3995 return font_style_symbolic (font, idx, 0);
3996 if (idx >= 0 && idx < FONT_EXTRA_INDEX)
3997 return AREF (font, idx);
3998 val = Fassq (key, AREF (font, FONT_EXTRA_INDEX));
3999 if (NILP (val) && EQ (key, QCotf) && FONT_OBJECT_P (font))
4001 struct font *fontp = XFONT_OBJECT (font);
4003 if (fontp->driver->otf_capability)
4004 val = fontp->driver->otf_capability (fontp);
4005 else
4006 val = Fcons (Qnil, Qnil);
4008 else
4009 val = Fcdr (val);
4010 return val;
4013 #ifdef HAVE_WINDOW_SYSTEM
4015 DEFUN ("font-face-attributes", Ffont_face_attributes, Sfont_face_attributes, 1, 2, 0,
4016 doc: /* Return a plist of face attributes generated by FONT.
4017 FONT is a font name, a font-spec, a font-entity, or a font-object.
4018 The return value is a list of the form
4020 \(:family FAMILY :height HEIGHT :weight WEIGHT :slant SLANT :width WIDTH)
4022 where FAMILY, HEIGHT, WEIGHT, SLANT, and WIDTH are face attribute values
4023 compatible with `set-face-attribute'. Some of these key-attribute pairs
4024 may be omitted from the list if they are not specified by FONT.
4026 The optional argument FRAME specifies the frame that the face attributes
4027 are to be displayed on. If omitted, the selected frame is used. */)
4028 (Lisp_Object font, Lisp_Object frame)
4030 struct frame *f = decode_live_frame (frame);
4031 Lisp_Object plist[10];
4032 Lisp_Object val;
4033 int n = 0;
4035 if (STRINGP (font))
4037 int fontset = fs_query_fontset (font, 0);
4038 Lisp_Object name = font;
4039 if (fontset >= 0)
4040 font = fontset_ascii (fontset);
4041 font = font_spec_from_name (name);
4042 if (! FONTP (font))
4043 signal_error ("Invalid font name", name);
4045 else if (! FONTP (font))
4046 signal_error ("Invalid font object", font);
4048 val = AREF (font, FONT_FAMILY_INDEX);
4049 if (! NILP (val))
4051 plist[n++] = QCfamily;
4052 plist[n++] = SYMBOL_NAME (val);
4055 val = AREF (font, FONT_SIZE_INDEX);
4056 if (INTEGERP (val))
4058 Lisp_Object font_dpi = AREF (font, FONT_DPI_INDEX);
4059 int dpi = INTEGERP (font_dpi) ? XINT (font_dpi) : FRAME_RES_Y (f);
4060 plist[n++] = QCheight;
4061 plist[n++] = make_number (PIXEL_TO_POINT (XINT (val) * 10, dpi));
4063 else if (FLOATP (val))
4065 plist[n++] = QCheight;
4066 plist[n++] = make_number (10 * (int) XFLOAT_DATA (val));
4069 val = FONT_WEIGHT_FOR_FACE (font);
4070 if (! NILP (val))
4072 plist[n++] = QCweight;
4073 plist[n++] = val;
4076 val = FONT_SLANT_FOR_FACE (font);
4077 if (! NILP (val))
4079 plist[n++] = QCslant;
4080 plist[n++] = val;
4083 val = FONT_WIDTH_FOR_FACE (font);
4084 if (! NILP (val))
4086 plist[n++] = QCwidth;
4087 plist[n++] = val;
4090 return Flist (n, plist);
4093 #endif
4095 DEFUN ("font-put", Ffont_put, Sfont_put, 3, 3, 0,
4096 doc: /* Set one property of FONT: give property KEY value VAL.
4097 FONT is a font-spec, a font-entity, or a font-object.
4099 If FONT is a font-spec, KEY can be any symbol. But if KEY is the one
4100 accepted by the function `font-spec' (which see), VAL must be what
4101 allowed in `font-spec'.
4103 If FONT is a font-entity or a font-object, KEY must not be the one
4104 accepted by `font-spec'. */)
4105 (Lisp_Object font, Lisp_Object prop, Lisp_Object val)
4107 int idx;
4109 idx = get_font_prop_index (prop);
4110 if (idx >= 0 && idx < FONT_EXTRA_INDEX)
4112 CHECK_FONT_SPEC (font);
4113 ASET (font, idx, font_prop_validate (idx, Qnil, val));
4115 else
4117 if (EQ (prop, QCname)
4118 || EQ (prop, QCscript)
4119 || EQ (prop, QClang)
4120 || EQ (prop, QCotf))
4121 CHECK_FONT_SPEC (font);
4122 else
4123 CHECK_FONT (font);
4124 font_put_extra (font, prop, font_prop_validate (0, prop, val));
4126 return val;
4129 DEFUN ("list-fonts", Flist_fonts, Slist_fonts, 1, 4, 0,
4130 doc: /* List available fonts matching FONT-SPEC on the current frame.
4131 Optional 2nd argument FRAME specifies the target frame.
4132 Optional 3rd argument NUM, if non-nil, limits the number of returned fonts.
4133 Optional 4th argument PREFER, if non-nil, is a font-spec to
4134 control the order of the returned list. Fonts are sorted by
4135 how close they are to PREFER. */)
4136 (Lisp_Object font_spec, Lisp_Object frame, Lisp_Object num, Lisp_Object prefer)
4138 struct frame *f = decode_live_frame (frame);
4139 Lisp_Object vec, list;
4140 EMACS_INT n = 0;
4142 CHECK_FONT_SPEC (font_spec);
4143 if (! NILP (num))
4145 CHECK_NUMBER (num);
4146 n = XINT (num);
4147 if (n <= 0)
4148 return Qnil;
4150 if (! NILP (prefer))
4151 CHECK_FONT_SPEC (prefer);
4153 list = font_list_entities (f, font_spec);
4154 if (NILP (list))
4155 return Qnil;
4156 if (NILP (XCDR (list))
4157 && ASIZE (XCAR (list)) == 1)
4158 return list1 (AREF (XCAR (list), 0));
4160 if (! NILP (prefer))
4161 vec = font_sort_entities (list, prefer, f, 0);
4162 else
4163 vec = font_vconcat_entity_vectors (list);
4164 if (n == 0 || n >= ASIZE (vec))
4166 Lisp_Object args[2];
4168 args[0] = vec;
4169 args[1] = Qnil;
4170 list = Fappend (2, args);
4172 else
4174 for (list = Qnil, n--; n >= 0; n--)
4175 list = Fcons (AREF (vec, n), list);
4177 return list;
4180 DEFUN ("font-family-list", Ffont_family_list, Sfont_family_list, 0, 1, 0,
4181 doc: /* List available font families on the current frame.
4182 If FRAME is omitted or nil, the selected frame is used. */)
4183 (Lisp_Object frame)
4185 struct frame *f = decode_live_frame (frame);
4186 struct font_driver_list *driver_list;
4187 Lisp_Object list = Qnil;
4189 for (driver_list = f->font_driver_list; driver_list;
4190 driver_list = driver_list->next)
4191 if (driver_list->driver->list_family)
4193 Lisp_Object val = driver_list->driver->list_family (f);
4194 Lisp_Object tail = list;
4196 for (; CONSP (val); val = XCDR (val))
4197 if (NILP (Fmemq (XCAR (val), tail))
4198 && SYMBOLP (XCAR (val)))
4199 list = Fcons (SYMBOL_NAME (XCAR (val)), list);
4201 return list;
4204 DEFUN ("find-font", Ffind_font, Sfind_font, 1, 2, 0,
4205 doc: /* Return a font-entity matching with FONT-SPEC on the current frame.
4206 Optional 2nd argument FRAME, if non-nil, specifies the target frame. */)
4207 (Lisp_Object font_spec, Lisp_Object frame)
4209 Lisp_Object val = Flist_fonts (font_spec, frame, make_number (1), Qnil);
4211 if (CONSP (val))
4212 val = XCAR (val);
4213 return val;
4216 DEFUN ("font-xlfd-name", Ffont_xlfd_name, Sfont_xlfd_name, 1, 2, 0,
4217 doc: /* Return XLFD name of FONT.
4218 FONT is a font-spec, font-entity, or font-object.
4219 If the name is too long for XLFD (maximum 255 chars), return nil.
4220 If the 2nd optional arg FOLD-WILDCARDS is non-nil,
4221 the consecutive wildcards are folded into one. */)
4222 (Lisp_Object font, Lisp_Object fold_wildcards)
4224 char name[256];
4225 int namelen, pixel_size = 0;
4227 CHECK_FONT (font);
4229 if (FONT_OBJECT_P (font))
4231 Lisp_Object font_name = AREF (font, FONT_NAME_INDEX);
4233 if (STRINGP (font_name)
4234 && SDATA (font_name)[0] == '-')
4236 if (NILP (fold_wildcards))
4237 return font_name;
4238 strcpy (name, SSDATA (font_name));
4239 namelen = SBYTES (font_name);
4240 goto done;
4242 pixel_size = XFONT_OBJECT (font)->pixel_size;
4244 namelen = font_unparse_xlfd (font, pixel_size, name, 256);
4245 if (namelen < 0)
4246 return Qnil;
4247 done:
4248 if (! NILP (fold_wildcards))
4250 char *p0 = name, *p1;
4252 while ((p1 = strstr (p0, "-*-*")))
4254 strcpy (p1, p1 + 2);
4255 namelen -= 2;
4256 p0 = p1;
4260 return make_string (name, namelen);
4263 void
4264 clear_font_cache (struct frame *f)
4266 struct font_driver_list *driver_list = f->font_driver_list;
4268 for (; driver_list; driver_list = driver_list->next)
4269 if (driver_list->on)
4271 Lisp_Object val, tmp, cache = driver_list->driver->get_cache (f);
4273 val = XCDR (cache);
4274 while (! NILP (val)
4275 && ! EQ (XCAR (XCAR (val)), driver_list->driver->type))
4276 val = XCDR (val);
4277 eassert (! NILP (val));
4278 tmp = XCDR (XCAR (val));
4279 if (XINT (XCAR (tmp)) == 0)
4281 font_clear_cache (f, XCAR (val), driver_list->driver);
4282 XSETCDR (cache, XCDR (val));
4287 DEFUN ("clear-font-cache", Fclear_font_cache, Sclear_font_cache, 0, 0, 0,
4288 doc: /* Clear font cache of each frame. */)
4289 (void)
4291 Lisp_Object list, frame;
4293 FOR_EACH_FRAME (list, frame)
4294 clear_font_cache (XFRAME (frame));
4296 return Qnil;
4300 void
4301 font_fill_lglyph_metrics (Lisp_Object glyph, Lisp_Object font_object)
4303 struct font *font = XFONT_OBJECT (font_object);
4304 unsigned code = font->driver->encode_char (font, LGLYPH_CHAR (glyph));
4305 struct font_metrics metrics;
4307 LGLYPH_SET_CODE (glyph, code);
4308 font->driver->text_extents (font, &code, 1, &metrics);
4309 LGLYPH_SET_LBEARING (glyph, metrics.lbearing);
4310 LGLYPH_SET_RBEARING (glyph, metrics.rbearing);
4311 LGLYPH_SET_WIDTH (glyph, metrics.width);
4312 LGLYPH_SET_ASCENT (glyph, metrics.ascent);
4313 LGLYPH_SET_DESCENT (glyph, metrics.descent);
4317 DEFUN ("font-shape-gstring", Ffont_shape_gstring, Sfont_shape_gstring, 1, 1, 0,
4318 doc: /* Shape the glyph-string GSTRING.
4319 Shaping means substituting glyphs and/or adjusting positions of glyphs
4320 to get the correct visual image of character sequences set in the
4321 header of the glyph-string.
4323 If the shaping was successful, the value is GSTRING itself or a newly
4324 created glyph-string. Otherwise, the value is nil.
4326 See the documentation of `composition-get-gstring' for the format of
4327 GSTRING. */)
4328 (Lisp_Object gstring)
4330 struct font *font;
4331 Lisp_Object font_object, n, glyph;
4332 ptrdiff_t i, from, to;
4334 if (! composition_gstring_p (gstring))
4335 signal_error ("Invalid glyph-string: ", gstring);
4336 if (! NILP (LGSTRING_ID (gstring)))
4337 return gstring;
4338 font_object = LGSTRING_FONT (gstring);
4339 CHECK_FONT_OBJECT (font_object);
4340 font = XFONT_OBJECT (font_object);
4341 if (! font->driver->shape)
4342 return Qnil;
4344 /* Try at most three times with larger gstring each time. */
4345 for (i = 0; i < 3; i++)
4347 n = font->driver->shape (gstring);
4348 if (INTEGERP (n))
4349 break;
4350 gstring = larger_vector (gstring,
4351 LGSTRING_GLYPH_LEN (gstring), -1);
4353 if (i == 3 || XINT (n) == 0)
4354 return Qnil;
4355 if (XINT (n) < LGSTRING_GLYPH_LEN (gstring))
4356 LGSTRING_SET_GLYPH (gstring, XINT (n), Qnil);
4358 /* Check FROM_IDX and TO_IDX of each GLYPH in GSTRING to assure that
4359 GLYPHS covers all characters (except for the last few ones) in
4360 GSTRING. More formally, provided that NCHARS is the number of
4361 characters in GSTRING and GLYPHS[i] is the ith glyph, FROM_IDX
4362 and TO_IDX of each glyph must satisfy these conditions:
4364 GLYPHS[0].FROM_IDX == 0
4365 GLYPHS[i].FROM_IDX <= GLYPHS[i].TO_IDX
4366 if (GLYPHS[i].FROM_IDX == GLYPHS[i-1].FROM_IDX)
4367 ;; GLYPHS[i] and GLYPHS[i-1] belongs to the same grapheme cluster
4368 GLYPHS[i].TO_IDX == GLYPHS[i-1].TO_IDX
4369 else
4370 ;; Be sure to cover all characters.
4371 GLYPHS[i].FROM_IDX == GLYPHS[i-1].TO_IDX + 1 */
4372 glyph = LGSTRING_GLYPH (gstring, 0);
4373 from = LGLYPH_FROM (glyph);
4374 to = LGLYPH_TO (glyph);
4375 if (from != 0 || to < from)
4376 goto shaper_error;
4377 for (i = 1; i < LGSTRING_GLYPH_LEN (gstring); i++)
4379 glyph = LGSTRING_GLYPH (gstring, i);
4380 if (NILP (glyph))
4381 break;
4382 if (! (LGLYPH_FROM (glyph) <= LGLYPH_TO (glyph)
4383 && (LGLYPH_FROM (glyph) == from
4384 ? LGLYPH_TO (glyph) == to
4385 : LGLYPH_FROM (glyph) == to + 1)))
4386 goto shaper_error;
4387 from = LGLYPH_FROM (glyph);
4388 to = LGLYPH_TO (glyph);
4390 return composition_gstring_put_cache (gstring, XINT (n));
4392 shaper_error:
4393 return Qnil;
4396 DEFUN ("font-variation-glyphs", Ffont_variation_glyphs, Sfont_variation_glyphs,
4397 2, 2, 0,
4398 doc: /* Return a list of variation glyphs for CHAR in FONT-OBJECT.
4399 Each element of the value is a cons (VARIATION-SELECTOR . GLYPH-ID),
4400 where
4401 VARIATION-SELECTOR is a character code of variation selection
4402 (#xFE00..#xFE0F or #xE0100..#xE01EF)
4403 GLYPH-ID is a glyph code of the corresponding variation glyph. */)
4404 (Lisp_Object font_object, Lisp_Object character)
4406 unsigned variations[256];
4407 struct font *font;
4408 int i, n;
4409 Lisp_Object val;
4411 CHECK_FONT_OBJECT (font_object);
4412 CHECK_CHARACTER (character);
4413 font = XFONT_OBJECT (font_object);
4414 if (! font->driver->get_variation_glyphs)
4415 return Qnil;
4416 n = font->driver->get_variation_glyphs (font, XINT (character), variations);
4417 if (! n)
4418 return Qnil;
4419 val = Qnil;
4420 for (i = 0; i < 255; i++)
4421 if (variations[i])
4423 int vs = (i < 16 ? 0xFE00 + i : 0xE0100 + (i - 16));
4424 Lisp_Object code = INTEGER_TO_CONS (variations[i]);
4425 val = Fcons (Fcons (make_number (vs), code), val);
4427 return val;
4430 #if 0
4432 DEFUN ("font-drive-otf", Ffont_drive_otf, Sfont_drive_otf, 6, 6, 0,
4433 doc: /* Apply OpenType features on glyph-string GSTRING-IN.
4434 OTF-FEATURES specifies which features to apply in this format:
4435 (SCRIPT LANGSYS GSUB GPOS)
4436 where
4437 SCRIPT is a symbol specifying a script tag of OpenType,
4438 LANGSYS is a symbol specifying a langsys tag of OpenType,
4439 GSUB and GPOS, if non-nil, are lists of symbols specifying feature tags.
4441 If LANGSYS is nil, the default langsys is selected.
4443 The features are applied in the order they appear in the list. The
4444 symbol `*' means to apply all available features not present in this
4445 list, and the remaining features are ignored. For instance, (vatu
4446 pstf * haln) is to apply vatu and pstf in this order, then to apply
4447 all available features other than vatu, pstf, and haln.
4449 The features are applied to the glyphs in the range FROM and TO of
4450 the glyph-string GSTRING-IN.
4452 If some feature is actually applicable, the resulting glyphs are
4453 produced in the glyph-string GSTRING-OUT from the index INDEX. In
4454 this case, the value is the number of produced glyphs.
4456 If no feature is applicable, no glyph is produced in GSTRING-OUT, and
4457 the value is 0.
4459 If GSTRING-OUT is too short to hold produced glyphs, no glyphs are
4460 produced in GSTRING-OUT, and the value is nil.
4462 See the documentation of `composition-get-gstring' for the format of
4463 glyph-string. */)
4464 (Lisp_Object otf_features, Lisp_Object gstring_in, Lisp_Object from, Lisp_Object to, Lisp_Object gstring_out, Lisp_Object index)
4466 Lisp_Object font_object = LGSTRING_FONT (gstring_in);
4467 Lisp_Object val;
4468 struct font *font;
4469 int len, num;
4471 check_otf_features (otf_features);
4472 CHECK_FONT_OBJECT (font_object);
4473 font = XFONT_OBJECT (font_object);
4474 if (! font->driver->otf_drive)
4475 error ("Font backend %s can't drive OpenType GSUB table",
4476 SDATA (SYMBOL_NAME (font->driver->type)));
4477 CHECK_CONS (otf_features);
4478 CHECK_SYMBOL (XCAR (otf_features));
4479 val = XCDR (otf_features);
4480 CHECK_SYMBOL (XCAR (val));
4481 val = XCDR (otf_features);
4482 if (! NILP (val))
4483 CHECK_CONS (val);
4484 len = check_gstring (gstring_in);
4485 CHECK_VECTOR (gstring_out);
4486 CHECK_NATNUM (from);
4487 CHECK_NATNUM (to);
4488 CHECK_NATNUM (index);
4490 if (XINT (from) >= XINT (to) || XINT (to) > len)
4491 args_out_of_range_3 (from, to, make_number (len));
4492 if (XINT (index) >= ASIZE (gstring_out))
4493 args_out_of_range (index, make_number (ASIZE (gstring_out)));
4494 num = font->driver->otf_drive (font, otf_features,
4495 gstring_in, XINT (from), XINT (to),
4496 gstring_out, XINT (index), 0);
4497 if (num < 0)
4498 return Qnil;
4499 return make_number (num);
4502 DEFUN ("font-otf-alternates", Ffont_otf_alternates, Sfont_otf_alternates,
4503 3, 3, 0,
4504 doc: /* Return a list of alternate glyphs of CHARACTER in FONT-OBJECT.
4505 OTF-FEATURES specifies which features of the font FONT-OBJECT to apply
4506 in this format:
4507 (SCRIPT LANGSYS FEATURE ...)
4508 See the documentation of `font-drive-otf' for more detail.
4510 The value is a list of cons cells of the format (GLYPH-ID . CHARACTER),
4511 where GLYPH-ID is a glyph index of the font, and CHARACTER is a
4512 character code corresponding to the glyph or nil if there's no
4513 corresponding character. */)
4514 (Lisp_Object font_object, Lisp_Object character, Lisp_Object otf_features)
4516 struct font *font;
4517 Lisp_Object gstring_in, gstring_out, g;
4518 Lisp_Object alternates;
4519 int i, num;
4521 CHECK_FONT_GET_OBJECT (font_object, font);
4522 if (! font->driver->otf_drive)
4523 error ("Font backend %s can't drive OpenType GSUB table",
4524 SDATA (SYMBOL_NAME (font->driver->type)));
4525 CHECK_CHARACTER (character);
4526 CHECK_CONS (otf_features);
4528 gstring_in = Ffont_make_gstring (font_object, make_number (1));
4529 g = LGSTRING_GLYPH (gstring_in, 0);
4530 LGLYPH_SET_CHAR (g, XINT (character));
4531 gstring_out = Ffont_make_gstring (font_object, make_number (10));
4532 while ((num = font->driver->otf_drive (font, otf_features, gstring_in, 0, 1,
4533 gstring_out, 0, 1)) < 0)
4534 gstring_out = Ffont_make_gstring (font_object,
4535 make_number (ASIZE (gstring_out) * 2));
4536 alternates = Qnil;
4537 for (i = 0; i < num; i++)
4539 Lisp_Object g = LGSTRING_GLYPH (gstring_out, i);
4540 int c = LGLYPH_CHAR (g);
4541 unsigned code = LGLYPH_CODE (g);
4543 alternates = Fcons (Fcons (make_number (code),
4544 c > 0 ? make_number (c) : Qnil),
4545 alternates);
4547 return Fnreverse (alternates);
4549 #endif /* 0 */
4551 #ifdef FONT_DEBUG
4553 DEFUN ("open-font", Fopen_font, Sopen_font, 1, 3, 0,
4554 doc: /* Open FONT-ENTITY. */)
4555 (Lisp_Object font_entity, Lisp_Object size, Lisp_Object frame)
4557 EMACS_INT isize;
4558 struct frame *f = decode_live_frame (frame);
4560 CHECK_FONT_ENTITY (font_entity);
4562 if (NILP (size))
4563 isize = XINT (AREF (font_entity, FONT_SIZE_INDEX));
4564 else
4566 CHECK_NUMBER_OR_FLOAT (size);
4567 if (FLOATP (size))
4568 isize = POINT_TO_PIXEL (XFLOAT_DATA (size), FRAME_RES_Y (f));
4569 else
4570 isize = XINT (size);
4571 if (! (INT_MIN <= isize && isize <= INT_MAX))
4572 args_out_of_range (font_entity, size);
4573 if (isize == 0)
4574 isize = 120;
4576 return font_open_entity (f, font_entity, isize);
4579 DEFUN ("close-font", Fclose_font, Sclose_font, 1, 2, 0,
4580 doc: /* Close FONT-OBJECT. */)
4581 (Lisp_Object font_object, Lisp_Object frame)
4583 CHECK_FONT_OBJECT (font_object);
4584 font_close_object (decode_live_frame (frame), font_object);
4585 return Qnil;
4588 DEFUN ("query-font", Fquery_font, Squery_font, 1, 1, 0,
4589 doc: /* Return information about FONT-OBJECT.
4590 The value is a vector:
4591 [ NAME FILENAME PIXEL-SIZE SIZE ASCENT DESCENT SPACE-WIDTH AVERAGE-WIDTH
4592 CAPABILITY ]
4594 NAME is the font name, a string (or nil if the font backend doesn't
4595 provide a name).
4597 FILENAME is the font file name, a string (or nil if the font backend
4598 doesn't provide a file name).
4600 PIXEL-SIZE is a pixel size by which the font is opened.
4602 SIZE is a maximum advance width of the font in pixels.
4604 ASCENT, DESCENT, SPACE-WIDTH, AVERAGE-WIDTH are metrics of the font in
4605 pixels.
4607 CAPABILITY is a list whose first element is a symbol representing the
4608 font format \(x, opentype, truetype, type1, pcf, or bdf) and the
4609 remaining elements describe the details of the font capability.
4611 If the font is OpenType font, the form of the list is
4612 \(opentype GSUB GPOS)
4613 where GSUB shows which "GSUB" features the font supports, and GPOS
4614 shows which "GPOS" features the font supports. Both GSUB and GPOS are
4615 lists of the format:
4616 \((SCRIPT (LANGSYS FEATURE ...) ...) ...)
4618 If the font is not OpenType font, currently the length of the form is
4619 one.
4621 SCRIPT is a symbol representing OpenType script tag.
4623 LANGSYS is a symbol representing OpenType langsys tag, or nil
4624 representing the default langsys.
4626 FEATURE is a symbol representing OpenType feature tag.
4628 If the font is not OpenType font, CAPABILITY is nil. */)
4629 (Lisp_Object font_object)
4631 struct font *font;
4632 Lisp_Object val;
4634 CHECK_FONT_GET_OBJECT (font_object, font);
4636 val = make_uninit_vector (9);
4637 ASET (val, 0, AREF (font_object, FONT_NAME_INDEX));
4638 ASET (val, 1, AREF (font_object, FONT_FILE_INDEX));
4639 ASET (val, 2, make_number (font->pixel_size));
4640 ASET (val, 3, make_number (font->max_width));
4641 ASET (val, 4, make_number (font->ascent));
4642 ASET (val, 5, make_number (font->descent));
4643 ASET (val, 6, make_number (font->space_width));
4644 ASET (val, 7, make_number (font->average_width));
4645 if (font->driver->otf_capability)
4646 ASET (val, 8, Fcons (Qopentype, font->driver->otf_capability (font)));
4647 else
4648 ASET (val, 8, Qnil);
4649 return val;
4652 DEFUN ("font-get-glyphs", Ffont_get_glyphs, Sfont_get_glyphs, 3, 4, 0,
4653 doc:
4654 /* Return a vector of FONT-OBJECT's glyphs for the specified characters.
4655 FROM and TO are positions (integers or markers) specifying a region
4656 of the current buffer.
4657 If the optional fourth arg OBJECT is not nil, it is a string or a
4658 vector containing the target characters.
4660 Each element is a vector containing information of a glyph in this format:
4661 [FROM-IDX TO-IDX C CODE WIDTH LBEARING RBEARING ASCENT DESCENT ADJUSTMENT]
4662 where
4663 FROM is an index numbers of a character the glyph corresponds to.
4664 TO is the same as FROM.
4665 C is the character of the glyph.
4666 CODE is the glyph-code of C in FONT-OBJECT.
4667 WIDTH thru DESCENT are the metrics (in pixels) of the glyph.
4668 ADJUSTMENT is always nil.
4669 If FONT-OBJECT doesn't have a glyph for a character,
4670 the corresponding element is nil. */)
4671 (Lisp_Object font_object, Lisp_Object from, Lisp_Object to,
4672 Lisp_Object object)
4674 struct font *font;
4675 ptrdiff_t i, len;
4676 Lisp_Object *chars, vec;
4677 USE_SAFE_ALLOCA;
4679 CHECK_FONT_GET_OBJECT (font_object, font);
4680 if (NILP (object))
4682 ptrdiff_t charpos, bytepos;
4684 validate_region (&from, &to);
4685 if (EQ (from, to))
4686 return Qnil;
4687 len = XFASTINT (to) - XFASTINT (from);
4688 SAFE_ALLOCA_LISP (chars, len);
4689 charpos = XFASTINT (from);
4690 bytepos = CHAR_TO_BYTE (charpos);
4691 for (i = 0; charpos < XFASTINT (to); i++)
4693 int c;
4694 FETCH_CHAR_ADVANCE (c, charpos, bytepos);
4695 chars[i] = make_number (c);
4698 else if (STRINGP (object))
4700 const unsigned char *p;
4702 CHECK_NUMBER (from);
4703 CHECK_NUMBER (to);
4704 if (XINT (from) < 0 || XINT (from) > XINT (to)
4705 || XINT (to) > SCHARS (object))
4706 args_out_of_range_3 (object, from, to);
4707 if (EQ (from, to))
4708 return Qnil;
4709 len = XFASTINT (to) - XFASTINT (from);
4710 SAFE_ALLOCA_LISP (chars, len);
4711 p = SDATA (object);
4712 if (STRING_MULTIBYTE (object))
4713 for (i = 0; i < len; i++)
4715 int c = STRING_CHAR_ADVANCE (p);
4716 chars[i] = make_number (c);
4718 else
4719 for (i = 0; i < len; i++)
4720 chars[i] = make_number (p[i]);
4722 else
4724 CHECK_VECTOR (object);
4725 CHECK_NUMBER (from);
4726 CHECK_NUMBER (to);
4727 if (XINT (from) < 0 || XINT (from) > XINT (to)
4728 || XINT (to) > ASIZE (object))
4729 args_out_of_range_3 (object, from, to);
4730 if (EQ (from, to))
4731 return Qnil;
4732 len = XFASTINT (to) - XFASTINT (from);
4733 for (i = 0; i < len; i++)
4735 Lisp_Object elt = AREF (object, XFASTINT (from) + i);
4736 CHECK_CHARACTER (elt);
4738 chars = aref_addr (object, XFASTINT (from));
4741 vec = make_uninit_vector (len);
4742 for (i = 0; i < len; i++)
4744 Lisp_Object g;
4745 int c = XFASTINT (chars[i]);
4746 unsigned code;
4747 struct font_metrics metrics;
4749 code = font->driver->encode_char (font, c);
4750 if (code == FONT_INVALID_CODE)
4752 ASET (vec, i, Qnil);
4753 continue;
4755 g = LGLYPH_NEW ();
4756 LGLYPH_SET_FROM (g, i);
4757 LGLYPH_SET_TO (g, i);
4758 LGLYPH_SET_CHAR (g, c);
4759 LGLYPH_SET_CODE (g, code);
4760 font->driver->text_extents (font, &code, 1, &metrics);
4761 LGLYPH_SET_WIDTH (g, metrics.width);
4762 LGLYPH_SET_LBEARING (g, metrics.lbearing);
4763 LGLYPH_SET_RBEARING (g, metrics.rbearing);
4764 LGLYPH_SET_ASCENT (g, metrics.ascent);
4765 LGLYPH_SET_DESCENT (g, metrics.descent);
4766 ASET (vec, i, g);
4768 if (! VECTORP (object))
4769 SAFE_FREE ();
4770 return vec;
4773 DEFUN ("font-match-p", Ffont_match_p, Sfont_match_p, 2, 2, 0,
4774 doc: /* Return t if and only if font-spec SPEC matches with FONT.
4775 FONT is a font-spec, font-entity, or font-object. */)
4776 (Lisp_Object spec, Lisp_Object font)
4778 CHECK_FONT_SPEC (spec);
4779 CHECK_FONT (font);
4781 return (font_match_p (spec, font) ? Qt : Qnil);
4784 DEFUN ("font-at", Ffont_at, Sfont_at, 1, 3, 0,
4785 doc: /* Return a font-object for displaying a character at POSITION.
4786 Optional second arg WINDOW, if non-nil, is a window displaying
4787 the current buffer. It defaults to the currently selected window.
4788 Optional third arg STRING, if non-nil, is a string containing the target
4789 character at index specified by POSITION. */)
4790 (Lisp_Object position, Lisp_Object window, Lisp_Object string)
4792 struct window *w = decode_live_window (window);
4794 if (NILP (string))
4796 if (XBUFFER (w->contents) != current_buffer)
4797 error ("Specified window is not displaying the current buffer");
4798 CHECK_NUMBER_COERCE_MARKER (position);
4799 if (! (BEGV <= XINT (position) && XINT (position) < ZV))
4800 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
4802 else
4804 CHECK_NUMBER (position);
4805 CHECK_STRING (string);
4806 if (! (0 <= XINT (position) && XINT (position) < SCHARS (string)))
4807 args_out_of_range (string, position);
4810 return font_at (-1, XINT (position), NULL, w, string);
4813 #if 0
4814 DEFUN ("draw-string", Fdraw_string, Sdraw_string, 2, 2, 0,
4815 doc: /* Draw STRING by FONT-OBJECT on the top left corner of the current frame.
4816 The value is a number of glyphs drawn.
4817 Type C-l to recover what previously shown. */)
4818 (Lisp_Object font_object, Lisp_Object string)
4820 Lisp_Object frame = selected_frame;
4821 struct frame *f = XFRAME (frame);
4822 struct font *font;
4823 struct face *face;
4824 int i, len, width;
4825 unsigned *code;
4827 CHECK_FONT_GET_OBJECT (font_object, font);
4828 CHECK_STRING (string);
4829 len = SCHARS (string);
4830 code = alloca (sizeof (unsigned) * len);
4831 for (i = 0; i < len; i++)
4833 Lisp_Object ch = Faref (string, make_number (i));
4834 Lisp_Object val;
4835 int c = XINT (ch);
4837 code[i] = font->driver->encode_char (font, c);
4838 if (code[i] == FONT_INVALID_CODE)
4839 break;
4841 face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4842 face->fontp = font;
4843 if (font->driver->prepare_face)
4844 font->driver->prepare_face (f, face);
4845 width = font->driver->text_extents (font, code, i, NULL);
4846 len = font->driver->draw_text (f, face, 0, font->ascent, code, i, width);
4847 if (font->driver->done_face)
4848 font->driver->done_face (f, face);
4849 face->fontp = NULL;
4850 return make_number (len);
4852 #endif
4854 DEFUN ("frame-font-cache", Fframe_font_cache, Sframe_font_cache, 0, 1, 0,
4855 doc: /* Return FRAME's font cache. Mainly used for debugging.
4856 If FRAME is omitted or nil, use the selected frame. */)
4857 (Lisp_Object frame)
4859 #ifdef HAVE_WINDOW_SYSTEM
4860 struct frame *f = decode_live_frame (frame);
4862 if (FRAME_WINDOW_P (f))
4863 return FRAME_DISPLAY_INFO (f)->name_list_element;
4864 else
4865 #endif
4866 return Qnil;
4869 #endif /* FONT_DEBUG */
4871 #ifdef HAVE_WINDOW_SYSTEM
4873 DEFUN ("font-info", Ffont_info, Sfont_info, 1, 2, 0,
4874 doc: /* Return information about a font named NAME on frame FRAME.
4875 If FRAME is omitted or nil, use the selected frame.
4876 The returned value is a vector of OPENED-NAME, FULL-NAME, SIZE,
4877 HEIGHT, BASELINE-OFFSET, RELATIVE-COMPOSE, and DEFAULT-ASCENT,
4878 where
4879 OPENED-NAME is the name used for opening the font,
4880 FULL-NAME is the full name of the font,
4881 SIZE is the pixelsize of the font,
4882 HEIGHT is the pixel-height of the font (i.e., ascent + descent),
4883 BASELINE-OFFSET is the upward offset pixels from ASCII baseline,
4884 RELATIVE-COMPOSE and DEFAULT-ASCENT are the numbers controlling
4885 how to compose characters.
4886 If the named font is not yet loaded, return nil. */)
4887 (Lisp_Object name, Lisp_Object frame)
4889 struct frame *f;
4890 struct font *font;
4891 Lisp_Object info;
4892 Lisp_Object font_object;
4894 if (! FONTP (name))
4895 CHECK_STRING (name);
4896 f = decode_window_system_frame (frame);
4898 if (STRINGP (name))
4900 int fontset = fs_query_fontset (name, 0);
4902 if (fontset >= 0)
4903 name = fontset_ascii (fontset);
4904 font_object = font_open_by_name (f, name);
4906 else if (FONT_OBJECT_P (name))
4907 font_object = name;
4908 else if (FONT_ENTITY_P (name))
4909 font_object = font_open_entity (f, name, 0);
4910 else
4912 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4913 Lisp_Object entity = font_matching_entity (f, face->lface, name);
4915 font_object = ! NILP (entity) ? font_open_entity (f, entity, 0) : Qnil;
4917 if (NILP (font_object))
4918 return Qnil;
4919 font = XFONT_OBJECT (font_object);
4921 info = make_uninit_vector (7);
4922 ASET (info, 0, AREF (font_object, FONT_NAME_INDEX));
4923 ASET (info, 1, AREF (font_object, FONT_FULLNAME_INDEX));
4924 ASET (info, 2, make_number (font->pixel_size));
4925 ASET (info, 3, make_number (font->height));
4926 ASET (info, 4, make_number (font->baseline_offset));
4927 ASET (info, 5, make_number (font->relative_compose));
4928 ASET (info, 6, make_number (font->default_ascent));
4930 #if 0
4931 /* As font_object is still in FONT_OBJLIST of the entity, we can't
4932 close it now. Perhaps, we should manage font-objects
4933 by `reference-count'. */
4934 font_close_object (f, font_object);
4935 #endif
4936 return info;
4938 #endif
4941 #define BUILD_STYLE_TABLE(TBL) \
4942 build_style_table ((TBL), sizeof TBL / sizeof (struct table_entry))
4944 static Lisp_Object
4945 build_style_table (const struct table_entry *entry, int nelement)
4947 int i, j;
4948 Lisp_Object table, elt;
4950 table = make_uninit_vector (nelement);
4951 for (i = 0; i < nelement; i++)
4953 for (j = 0; entry[i].names[j]; j++);
4954 elt = Fmake_vector (make_number (j + 1), Qnil);
4955 ASET (elt, 0, make_number (entry[i].numeric));
4956 for (j = 0; entry[i].names[j]; j++)
4957 ASET (elt, j + 1, intern_c_string (entry[i].names[j]));
4958 ASET (table, i, elt);
4960 return table;
4963 /* The deferred font-log data of the form [ACTION ARG RESULT].
4964 If ACTION is not nil, that is added to the log when font_add_log is
4965 called next time. At that time, ACTION is set back to nil. */
4966 static Lisp_Object Vfont_log_deferred;
4968 /* Prepend the font-related logging data in Vfont_log if it is not
4969 `t'. ACTION describes a kind of font-related action (e.g. listing,
4970 opening), ARG is the argument for the action, and RESULT is the
4971 result of the action. */
4972 void
4973 font_add_log (const char *action, Lisp_Object arg, Lisp_Object result)
4975 Lisp_Object val;
4976 int i;
4978 if (EQ (Vfont_log, Qt))
4979 return;
4980 if (STRINGP (AREF (Vfont_log_deferred, 0)))
4982 char *str = SSDATA (AREF (Vfont_log_deferred, 0));
4984 ASET (Vfont_log_deferred, 0, Qnil);
4985 font_add_log (str, AREF (Vfont_log_deferred, 1),
4986 AREF (Vfont_log_deferred, 2));
4989 if (FONTP (arg))
4991 Lisp_Object tail, elt;
4992 Lisp_Object equalstr = build_string ("=");
4994 val = Ffont_xlfd_name (arg, Qt);
4995 for (tail = AREF (arg, FONT_EXTRA_INDEX); CONSP (tail);
4996 tail = XCDR (tail))
4998 elt = XCAR (tail);
4999 if (EQ (XCAR (elt), QCscript)
5000 && SYMBOLP (XCDR (elt)))
5001 val = concat3 (val, SYMBOL_NAME (QCscript),
5002 concat2 (equalstr, SYMBOL_NAME (XCDR (elt))));
5003 else if (EQ (XCAR (elt), QClang)
5004 && SYMBOLP (XCDR (elt)))
5005 val = concat3 (val, SYMBOL_NAME (QClang),
5006 concat2 (equalstr, SYMBOL_NAME (XCDR (elt))));
5007 else if (EQ (XCAR (elt), QCotf)
5008 && CONSP (XCDR (elt)) && SYMBOLP (XCAR (XCDR (elt))))
5009 val = concat3 (val, SYMBOL_NAME (QCotf),
5010 concat2 (equalstr,
5011 SYMBOL_NAME (XCAR (XCDR (elt)))));
5013 arg = val;
5016 if (CONSP (result)
5017 && VECTORP (XCAR (result))
5018 && ASIZE (XCAR (result)) > 0
5019 && FONTP (AREF (XCAR (result), 0)))
5020 result = font_vconcat_entity_vectors (result);
5021 if (FONTP (result))
5023 val = Ffont_xlfd_name (result, Qt);
5024 if (! FONT_SPEC_P (result))
5025 val = concat3 (SYMBOL_NAME (AREF (result, FONT_TYPE_INDEX)),
5026 build_string (":"), val);
5027 result = val;
5029 else if (CONSP (result))
5031 Lisp_Object tail;
5032 result = Fcopy_sequence (result);
5033 for (tail = result; CONSP (tail); tail = XCDR (tail))
5035 val = XCAR (tail);
5036 if (FONTP (val))
5037 val = Ffont_xlfd_name (val, Qt);
5038 XSETCAR (tail, val);
5041 else if (VECTORP (result))
5043 result = Fcopy_sequence (result);
5044 for (i = 0; i < ASIZE (result); i++)
5046 val = AREF (result, i);
5047 if (FONTP (val))
5048 val = Ffont_xlfd_name (val, Qt);
5049 ASET (result, i, val);
5052 Vfont_log = Fcons (list3 (intern (action), arg, result), Vfont_log);
5055 /* Record a font-related logging data to be added to Vfont_log when
5056 font_add_log is called next time. ACTION, ARG, RESULT are the same
5057 as font_add_log. */
5059 void
5060 font_deferred_log (const char *action, Lisp_Object arg, Lisp_Object result)
5062 if (EQ (Vfont_log, Qt))
5063 return;
5064 ASET (Vfont_log_deferred, 0, build_string (action));
5065 ASET (Vfont_log_deferred, 1, arg);
5066 ASET (Vfont_log_deferred, 2, result);
5069 void
5070 syms_of_font (void)
5072 sort_shift_bits[FONT_TYPE_INDEX] = 0;
5073 sort_shift_bits[FONT_SLANT_INDEX] = 2;
5074 sort_shift_bits[FONT_WEIGHT_INDEX] = 9;
5075 sort_shift_bits[FONT_SIZE_INDEX] = 16;
5076 sort_shift_bits[FONT_WIDTH_INDEX] = 23;
5077 /* Note that the other elements in sort_shift_bits are not used. */
5079 staticpro (&font_charset_alist);
5080 font_charset_alist = Qnil;
5082 DEFSYM (Qopentype, "opentype");
5084 DEFSYM (Qascii_0, "ascii-0");
5085 DEFSYM (Qiso8859_1, "iso8859-1");
5086 DEFSYM (Qiso10646_1, "iso10646-1");
5087 DEFSYM (Qunicode_bmp, "unicode-bmp");
5088 DEFSYM (Qunicode_sip, "unicode-sip");
5090 DEFSYM (QCf, "Cf");
5092 DEFSYM (QCotf, ":otf");
5093 DEFSYM (QClang, ":lang");
5094 DEFSYM (QCscript, ":script");
5095 DEFSYM (QCantialias, ":antialias");
5097 DEFSYM (QCfoundry, ":foundry");
5098 DEFSYM (QCadstyle, ":adstyle");
5099 DEFSYM (QCregistry, ":registry");
5100 DEFSYM (QCspacing, ":spacing");
5101 DEFSYM (QCdpi, ":dpi");
5102 DEFSYM (QCscalable, ":scalable");
5103 DEFSYM (QCavgwidth, ":avgwidth");
5104 DEFSYM (QCfont_entity, ":font-entity");
5105 DEFSYM (QCfc_unknown_spec, ":fc-unknown-spec");
5107 DEFSYM (Qc, "c");
5108 DEFSYM (Qm, "m");
5109 DEFSYM (Qp, "p");
5110 DEFSYM (Qd, "d");
5112 DEFSYM (Qja, "ja");
5113 DEFSYM (Qko, "ko");
5115 DEFSYM (QCuser_spec, "user-spec");
5117 staticpro (&scratch_font_spec);
5118 scratch_font_spec = Ffont_spec (0, NULL);
5119 staticpro (&scratch_font_prefer);
5120 scratch_font_prefer = Ffont_spec (0, NULL);
5122 staticpro (&Vfont_log_deferred);
5123 Vfont_log_deferred = Fmake_vector (make_number (3), Qnil);
5125 #if 0
5126 #ifdef HAVE_LIBOTF
5127 staticpro (&otf_list);
5128 otf_list = Qnil;
5129 #endif /* HAVE_LIBOTF */
5130 #endif /* 0 */
5132 defsubr (&Sfontp);
5133 defsubr (&Sfont_spec);
5134 defsubr (&Sfont_get);
5135 #ifdef HAVE_WINDOW_SYSTEM
5136 defsubr (&Sfont_face_attributes);
5137 #endif
5138 defsubr (&Sfont_put);
5139 defsubr (&Slist_fonts);
5140 defsubr (&Sfont_family_list);
5141 defsubr (&Sfind_font);
5142 defsubr (&Sfont_xlfd_name);
5143 defsubr (&Sclear_font_cache);
5144 defsubr (&Sfont_shape_gstring);
5145 defsubr (&Sfont_variation_glyphs);
5146 #if 0
5147 defsubr (&Sfont_drive_otf);
5148 defsubr (&Sfont_otf_alternates);
5149 #endif /* 0 */
5151 #ifdef FONT_DEBUG
5152 defsubr (&Sopen_font);
5153 defsubr (&Sclose_font);
5154 defsubr (&Squery_font);
5155 defsubr (&Sfont_get_glyphs);
5156 defsubr (&Sfont_match_p);
5157 defsubr (&Sfont_at);
5158 #if 0
5159 defsubr (&Sdraw_string);
5160 #endif
5161 defsubr (&Sframe_font_cache);
5162 #endif /* FONT_DEBUG */
5163 #ifdef HAVE_WINDOW_SYSTEM
5164 defsubr (&Sfont_info);
5165 #endif
5167 DEFVAR_LISP ("font-encoding-alist", Vfont_encoding_alist,
5168 doc: /*
5169 Alist of fontname patterns vs the corresponding encoding and repertory info.
5170 Each element looks like (REGEXP . (ENCODING . REPERTORY)),
5171 where ENCODING is a charset or a char-table,
5172 and REPERTORY is a charset, a char-table, or nil.
5174 If ENCODING and REPERTORY are the same, the element can have the form
5175 \(REGEXP . ENCODING).
5177 ENCODING is for converting a character to a glyph code of the font.
5178 If ENCODING is a charset, encoding a character by the charset gives
5179 the corresponding glyph code. If ENCODING is a char-table, looking up
5180 the table by a character gives the corresponding glyph code.
5182 REPERTORY specifies a repertory of characters supported by the font.
5183 If REPERTORY is a charset, all characters belonging to the charset are
5184 supported. If REPERTORY is a char-table, all characters who have a
5185 non-nil value in the table are supported. If REPERTORY is nil, Emacs
5186 gets the repertory information by an opened font and ENCODING. */);
5187 Vfont_encoding_alist = Qnil;
5189 /* FIXME: These 3 vars are not quite what they appear: setq on them
5190 won't have any effect other than disconnect them from the style
5191 table used by the font display code. So we make them read-only,
5192 to avoid this confusing situation. */
5194 DEFVAR_LISP_NOPRO ("font-weight-table", Vfont_weight_table,
5195 doc: /* Vector of valid font weight values.
5196 Each element has the form:
5197 [NUMERIC-VALUE SYMBOLIC-NAME ALIAS-NAME ...]
5198 NUMERIC-VALUE is an integer, and SYMBOLIC-NAME and ALIAS-NAME are symbols. */);
5199 Vfont_weight_table = BUILD_STYLE_TABLE (weight_table);
5200 XSYMBOL (intern_c_string ("font-weight-table"))->constant = 1;
5202 DEFVAR_LISP_NOPRO ("font-slant-table", Vfont_slant_table,
5203 doc: /* Vector of font slant symbols vs the corresponding numeric values.
5204 See `font-weight-table' for the format of the vector. */);
5205 Vfont_slant_table = BUILD_STYLE_TABLE (slant_table);
5206 XSYMBOL (intern_c_string ("font-slant-table"))->constant = 1;
5208 DEFVAR_LISP_NOPRO ("font-width-table", Vfont_width_table,
5209 doc: /* Alist of font width symbols vs the corresponding numeric values.
5210 See `font-weight-table' for the format of the vector. */);
5211 Vfont_width_table = BUILD_STYLE_TABLE (width_table);
5212 XSYMBOL (intern_c_string ("font-width-table"))->constant = 1;
5214 staticpro (&font_style_table);
5215 font_style_table = make_uninit_vector (3);
5216 ASET (font_style_table, 0, Vfont_weight_table);
5217 ASET (font_style_table, 1, Vfont_slant_table);
5218 ASET (font_style_table, 2, Vfont_width_table);
5220 DEFVAR_LISP ("font-log", Vfont_log, doc: /*
5221 *Logging list of font related actions and results.
5222 The value t means to suppress the logging.
5223 The initial value is set to nil if the environment variable
5224 EMACS_FONT_LOG is set. Otherwise, it is set to t. */);
5225 Vfont_log = Qnil;
5227 #ifdef HAVE_WINDOW_SYSTEM
5228 #ifdef HAVE_FREETYPE
5229 syms_of_ftfont ();
5230 #ifdef HAVE_X_WINDOWS
5231 syms_of_xfont ();
5232 syms_of_ftxfont ();
5233 #ifdef HAVE_XFT
5234 syms_of_xftfont ();
5235 #endif /* HAVE_XFT */
5236 #endif /* HAVE_X_WINDOWS */
5237 #else /* not HAVE_FREETYPE */
5238 #ifdef HAVE_X_WINDOWS
5239 syms_of_xfont ();
5240 #endif /* HAVE_X_WINDOWS */
5241 #endif /* not HAVE_FREETYPE */
5242 #ifdef HAVE_BDFFONT
5243 syms_of_bdffont ();
5244 #endif /* HAVE_BDFFONT */
5245 #ifdef HAVE_NTGUI
5246 syms_of_w32font ();
5247 #endif /* HAVE_NTGUI */
5248 #endif /* HAVE_WINDOW_SYSTEM */
5251 void
5252 init_font (void)
5254 Vfont_log = egetenv ("EMACS_FONT_LOG") ? Qnil : Qt;