lisp/emacs-lisp/eieio-core.el (eieio-backward-compatibility): New var.
[emacs.git] / src / font.c
blobd10d2280a5bfecfeb816f7af9d9c78b636fba4b2
1 /* font.c -- "Font" primitives.
3 Copyright (C) 2006-2014 Free Software Foundation, Inc.
4 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H13PRO009
8 This file is part of GNU Emacs.
10 GNU Emacs is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 GNU Emacs is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
23 #include <config.h>
24 #include <float.h>
25 #include <stdio.h>
27 #include <c-ctype.h>
29 #include "lisp.h"
30 #include "character.h"
31 #include "buffer.h"
32 #include "frame.h"
33 #include "window.h"
34 #include "dispextern.h"
35 #include "charset.h"
36 #include "composite.h"
37 #include "fontset.h"
38 #include "font.h"
40 #ifdef HAVE_WINDOW_SYSTEM
41 #include TERM_HEADER
42 #endif /* HAVE_WINDOW_SYSTEM */
44 Lisp_Object Qopentype;
46 /* Important character set strings. */
47 Lisp_Object Qascii_0, Qiso8859_1, Qiso10646_1, Qunicode_bmp, Qunicode_sip;
49 #define DEFAULT_ENCODING Qiso8859_1
51 /* Unicode category `Cf'. */
52 static Lisp_Object QCf;
54 /* Vector of Vfont_weight_table, Vfont_slant_table, and Vfont_width_table. */
55 static Lisp_Object font_style_table;
57 /* Structure used for tables mapping weight, slant, and width numeric
58 values and their names. */
60 struct table_entry
62 int numeric;
63 /* The first one is a valid name as a face attribute.
64 The second one (if any) is a typical name in XLFD field. */
65 const char *names[5];
68 /* Table of weight numeric values and their names. This table must be
69 sorted by numeric values in ascending order. */
71 static const struct table_entry weight_table[] =
73 { 0, { "thin" }},
74 { 20, { "ultra-light", "ultralight" }},
75 { 40, { "extra-light", "extralight" }},
76 { 50, { "light" }},
77 { 75, { "semi-light", "semilight", "demilight", "book" }},
78 { 100, { "normal", "medium", "regular", "unspecified" }},
79 { 180, { "semi-bold", "semibold", "demibold", "demi" }},
80 { 200, { "bold" }},
81 { 205, { "extra-bold", "extrabold" }},
82 { 210, { "ultra-bold", "ultrabold", "black" }}
85 /* Table of slant numeric values and their names. This table must be
86 sorted by numeric values in ascending order. */
88 static const struct table_entry slant_table[] =
90 { 0, { "reverse-oblique", "ro" }},
91 { 10, { "reverse-italic", "ri" }},
92 { 100, { "normal", "r", "unspecified" }},
93 { 200, { "italic" ,"i", "ot" }},
94 { 210, { "oblique", "o" }}
97 /* Table of width numeric values and their names. This table must be
98 sorted by numeric values in ascending order. */
100 static const struct table_entry width_table[] =
102 { 50, { "ultra-condensed", "ultracondensed" }},
103 { 63, { "extra-condensed", "extracondensed" }},
104 { 75, { "condensed", "compressed", "narrow" }},
105 { 87, { "semi-condensed", "semicondensed", "demicondensed" }},
106 { 100, { "normal", "medium", "regular", "unspecified" }},
107 { 113, { "semi-expanded", "semiexpanded", "demiexpanded" }},
108 { 125, { "expanded" }},
109 { 150, { "extra-expanded", "extraexpanded" }},
110 { 200, { "ultra-expanded", "ultraexpanded", "wide" }}
113 Lisp_Object QCfoundry;
114 static Lisp_Object QCadstyle, QCregistry;
115 /* Symbols representing keys of font extra info. */
116 Lisp_Object QCspacing, QCdpi, QCscalable, QCotf, QClang, QCscript, QCavgwidth;
117 Lisp_Object QCantialias, QCfont_entity;
118 static Lisp_Object QCfc_unknown_spec;
119 /* Symbols representing values of font spacing property. */
120 static Lisp_Object Qc, Qm, Qd;
121 Lisp_Object Qp;
122 /* Special ADSTYLE properties to avoid fonts used for Latin
123 characters; used in xfont.c and ftfont.c. */
124 Lisp_Object Qja, Qko;
126 static Lisp_Object QCuser_spec;
128 /* Alist of font registry symbols and the corresponding charset
129 information. The information is retrieved from
130 Vfont_encoding_alist on demand.
132 Eash element has the form:
133 (REGISTRY . (ENCODING-CHARSET-ID . REPERTORY-CHARSET-ID))
135 (REGISTRY . nil)
137 In the former form, ENCODING-CHARSET-ID is an ID of a charset that
138 encodes a character code to a glyph code of a font, and
139 REPERTORY-CHARSET-ID is an ID of a charset that tells if a
140 character is supported by a font.
142 The latter form means that the information for REGISTRY couldn't be
143 retrieved. */
144 static Lisp_Object font_charset_alist;
146 /* List of all font drivers. Each font-backend (XXXfont.c) calls
147 register_font_driver in syms_of_XXXfont to register its font-driver
148 here. */
149 static struct font_driver_list *font_driver_list;
151 #ifdef ENABLE_CHECKING
153 /* Used to catch bogus pointers in font objects. */
155 bool
156 valid_font_driver (struct font_driver *drv)
158 Lisp_Object tail, frame;
159 struct font_driver_list *fdl;
161 for (fdl = font_driver_list; fdl; fdl = fdl->next)
162 if (fdl->driver == drv)
163 return true;
164 FOR_EACH_FRAME (tail, frame)
165 for (fdl = XFRAME (frame)->font_driver_list; fdl; fdl = fdl->next)
166 if (fdl->driver == drv)
167 return true;
168 return false;
171 #endif /* ENABLE_CHECKING */
173 /* Creators of font-related Lisp object. */
175 static Lisp_Object
176 font_make_spec (void)
178 Lisp_Object font_spec;
179 struct font_spec *spec
180 = ((struct font_spec *)
181 allocate_pseudovector (VECSIZE (struct font_spec),
182 FONT_SPEC_MAX, PVEC_FONT));
183 XSETFONT (font_spec, spec);
184 return font_spec;
187 Lisp_Object
188 font_make_entity (void)
190 Lisp_Object font_entity;
191 struct font_entity *entity
192 = ((struct font_entity *)
193 allocate_pseudovector (VECSIZE (struct font_entity),
194 FONT_ENTITY_MAX, PVEC_FONT));
195 XSETFONT (font_entity, entity);
196 return font_entity;
199 /* Create a font-object whose structure size is SIZE. If ENTITY is
200 not nil, copy properties from ENTITY to the font-object. If
201 PIXELSIZE is positive, set the `size' property to PIXELSIZE. */
202 Lisp_Object
203 font_make_object (int size, Lisp_Object entity, int pixelsize)
205 Lisp_Object font_object;
206 struct font *font
207 = (struct font *) allocate_pseudovector (size, FONT_OBJECT_MAX, PVEC_FONT);
208 int i;
210 /* 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;
228 #if defined (HAVE_XFT) || defined (HAVE_FREETYPE) || defined (HAVE_NS)
230 static int font_unparse_fcname (Lisp_Object, int, char *, int);
232 /* Like above, but also set `type', `name' and `fullname' properties
233 of font-object. */
235 Lisp_Object
236 font_build_object (int vectorsize, Lisp_Object type,
237 Lisp_Object entity, double pixelsize)
239 int len;
240 char name[256];
241 Lisp_Object font_object = font_make_object (vectorsize, entity, pixelsize);
243 ASET (font_object, FONT_TYPE_INDEX, type);
244 len = font_unparse_xlfd (entity, pixelsize, name, sizeof name);
245 if (len > 0)
246 ASET (font_object, FONT_NAME_INDEX, make_string (name, len));
247 len = font_unparse_fcname (entity, pixelsize, name, sizeof name);
248 if (len > 0)
249 ASET (font_object, FONT_FULLNAME_INDEX, make_string (name, len));
250 else
251 ASET (font_object, FONT_FULLNAME_INDEX,
252 AREF (font_object, FONT_NAME_INDEX));
253 return font_object;
256 #endif /* HAVE_XFT || HAVE_FREETYPE || HAVE_NS */
258 static int font_pixel_size (struct frame *f, Lisp_Object);
259 static Lisp_Object font_open_entity (struct frame *, Lisp_Object, int);
260 static Lisp_Object font_matching_entity (struct frame *, Lisp_Object *,
261 Lisp_Object);
262 static unsigned font_encode_char (Lisp_Object, int);
264 /* Number of registered font drivers. */
265 static int num_font_drivers;
268 /* Return a Lispy value of a font property value at STR and LEN bytes.
269 If STR is "*", return nil. If FORCE_SYMBOL, or if STR does not
270 consist entirely of one or more digits, return a symbol interned
271 from STR. Otherwise, return an integer. */
273 Lisp_Object
274 font_intern_prop (const char *str, ptrdiff_t len, bool force_symbol)
276 ptrdiff_t i, nbytes, nchars;
277 Lisp_Object tem, name, obarray;
279 if (len == 1 && *str == '*')
280 return Qnil;
281 if (!force_symbol && 0 < len && '0' <= *str && *str <= '9')
283 for (i = 1; i < len; i++)
284 if (! ('0' <= str[i] && str[i] <= '9'))
285 break;
286 if (i == len)
288 EMACS_INT n;
290 i = 0;
291 for (n = 0; (n += str[i++] - '0') <= MOST_POSITIVE_FIXNUM; n *= 10)
293 if (i == len)
294 return make_number (n);
295 if (MOST_POSITIVE_FIXNUM / 10 < n)
296 break;
299 xsignal1 (Qoverflow_error, make_string (str, len));
303 /* This code is similar to intern function from lread.c. */
304 obarray = check_obarray (Vobarray);
305 parse_str_as_multibyte ((unsigned char *) str, len, &nchars, &nbytes);
306 tem = oblookup (obarray, str,
307 (len == nchars || len != nbytes) ? len : nchars, len);
308 if (SYMBOLP (tem))
309 return tem;
310 name = make_specified_string (str, nchars, len,
311 len != nchars && len == nbytes);
312 return intern_driver (name, obarray, XINT (tem));
315 /* Return a pixel size of font-spec SPEC on frame F. */
317 static int
318 font_pixel_size (struct frame *f, Lisp_Object spec)
320 #ifdef HAVE_WINDOW_SYSTEM
321 Lisp_Object size = AREF (spec, FONT_SIZE_INDEX);
322 double point_size;
323 int dpi, pixel_size;
324 Lisp_Object val;
326 if (INTEGERP (size))
327 return XINT (size);
328 if (NILP (size))
329 return 0;
330 eassert (FLOATP (size));
331 point_size = XFLOAT_DATA (size);
332 val = AREF (spec, FONT_DPI_INDEX);
333 if (INTEGERP (val))
334 dpi = XINT (val);
335 else
336 dpi = FRAME_RES_Y (f);
337 pixel_size = POINT_TO_PIXEL (point_size, dpi);
338 return pixel_size;
339 #else
340 return 1;
341 #endif
345 /* Return a value of PROP's VAL (symbol or integer) to be stored in a
346 font vector. If VAL is not valid (i.e. not registered in
347 font_style_table), return -1 if NOERROR is zero, and return a
348 proper index if NOERROR is nonzero. In that case, register VAL in
349 font_style_table if VAL is a symbol, and return the closest index if
350 VAL is an integer. */
353 font_style_to_value (enum font_property_index prop, Lisp_Object val,
354 bool noerror)
356 Lisp_Object table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
357 int len;
359 CHECK_VECTOR (table);
360 len = ASIZE (table);
362 if (SYMBOLP (val))
364 int i, j;
365 char *s;
366 Lisp_Object elt;
368 /* At first try exact match. */
369 for (i = 0; i < len; i++)
371 CHECK_VECTOR (AREF (table, i));
372 for (j = 1; j < ASIZE (AREF (table, i)); j++)
373 if (EQ (val, AREF (AREF (table, i), j)))
375 CHECK_NUMBER (AREF (AREF (table, i), 0));
376 return ((XINT (AREF (AREF (table, i), 0)) << 8)
377 | (i << 4) | (j - 1));
380 /* Try also with case-folding match. */
381 s = SSDATA (SYMBOL_NAME (val));
382 for (i = 0; i < len; i++)
383 for (j = 1; j < ASIZE (AREF (table, i)); j++)
385 elt = AREF (AREF (table, i), j);
386 if (xstrcasecmp (s, SSDATA (SYMBOL_NAME (elt))) == 0)
388 CHECK_NUMBER (AREF (AREF (table, i), 0));
389 return ((XINT (AREF (AREF (table, i), 0)) << 8)
390 | (i << 4) | (j - 1));
393 if (! noerror)
394 return -1;
395 eassert (len < 255);
396 elt = Fmake_vector (make_number (2), make_number (100));
397 ASET (elt, 1, val);
398 ASET (font_style_table, prop - FONT_WEIGHT_INDEX,
399 Fvconcat (2, ((Lisp_Object [])
400 { table, Fmake_vector (make_number (1), elt) })));
401 return (100 << 8) | (i << 4);
403 else
405 int i, last_n;
406 EMACS_INT numeric = XINT (val);
408 for (i = 0, last_n = -1; i < len; i++)
410 int n;
412 CHECK_VECTOR (AREF (table, i));
413 CHECK_NUMBER (AREF (AREF (table, i), 0));
414 n = XINT (AREF (AREF (table, i), 0));
415 if (numeric == n)
416 return (n << 8) | (i << 4);
417 if (numeric < n)
419 if (! noerror)
420 return -1;
421 return ((i == 0 || n - numeric < numeric - last_n)
422 ? (n << 8) | (i << 4): (last_n << 8 | ((i - 1) << 4)));
424 last_n = n;
426 if (! noerror)
427 return -1;
428 return ((last_n << 8) | ((i - 1) << 4));
432 Lisp_Object
433 font_style_symbolic (Lisp_Object font, enum font_property_index prop,
434 bool for_face)
436 Lisp_Object val = AREF (font, prop);
437 Lisp_Object table, elt;
438 int i;
440 if (NILP (val))
441 return Qnil;
442 table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
443 CHECK_VECTOR (table);
444 i = XINT (val) & 0xFF;
445 eassert (((i >> 4) & 0xF) < ASIZE (table));
446 elt = AREF (table, ((i >> 4) & 0xF));
447 CHECK_VECTOR (elt);
448 eassert ((i & 0xF) + 1 < ASIZE (elt));
449 elt = (for_face ? AREF (elt, 1) : AREF (elt, (i & 0xF) + 1));
450 CHECK_SYMBOL (elt);
451 return elt;
454 /* Return ENCODING or a cons of ENCODING and REPERTORY of the font
455 FONTNAME. ENCODING is a charset symbol that specifies the encoding
456 of the font. REPERTORY is a charset symbol or nil. */
458 Lisp_Object
459 find_font_encoding (Lisp_Object fontname)
461 Lisp_Object tail, elt;
463 for (tail = Vfont_encoding_alist; CONSP (tail); tail = XCDR (tail))
465 elt = XCAR (tail);
466 if (CONSP (elt)
467 && STRINGP (XCAR (elt))
468 && fast_string_match_ignore_case (XCAR (elt), fontname) >= 0
469 && (SYMBOLP (XCDR (elt))
470 ? CHARSETP (XCDR (elt))
471 : CONSP (XCDR (elt)) && CHARSETP (XCAR (XCDR (elt)))))
472 return (XCDR (elt));
474 return Qnil;
477 /* Return encoding charset and repertory charset for REGISTRY in
478 ENCODING and REPERTORY correspondingly. If correct information for
479 REGISTRY is available, return 0. Otherwise return -1. */
482 font_registry_charsets (Lisp_Object registry, struct charset **encoding, struct charset **repertory)
484 Lisp_Object val;
485 int encoding_id, repertory_id;
487 val = Fassoc_string (registry, font_charset_alist, Qt);
488 if (! NILP (val))
490 val = XCDR (val);
491 if (NILP (val))
492 return -1;
493 encoding_id = XINT (XCAR (val));
494 repertory_id = XINT (XCDR (val));
496 else
498 val = find_font_encoding (SYMBOL_NAME (registry));
499 if (SYMBOLP (val) && CHARSETP (val))
501 encoding_id = repertory_id = XINT (CHARSET_SYMBOL_ID (val));
503 else if (CONSP (val))
505 if (! CHARSETP (XCAR (val)))
506 goto invalid_entry;
507 encoding_id = XINT (CHARSET_SYMBOL_ID (XCAR (val)));
508 if (NILP (XCDR (val)))
509 repertory_id = -1;
510 else
512 if (! CHARSETP (XCDR (val)))
513 goto invalid_entry;
514 repertory_id = XINT (CHARSET_SYMBOL_ID (XCDR (val)));
517 else
518 goto invalid_entry;
519 val = Fcons (make_number (encoding_id), make_number (repertory_id));
520 font_charset_alist
521 = nconc2 (font_charset_alist, list1 (Fcons (registry, val)));
524 if (encoding)
525 *encoding = CHARSET_FROM_ID (encoding_id);
526 if (repertory)
527 *repertory = repertory_id >= 0 ? CHARSET_FROM_ID (repertory_id) : NULL;
528 return 0;
530 invalid_entry:
531 font_charset_alist
532 = nconc2 (font_charset_alist, list1 (Fcons (registry, Qnil)));
533 return -1;
537 /* Font property value validators. See the comment of
538 font_property_table for the meaning of the arguments. */
540 static Lisp_Object font_prop_validate (int, Lisp_Object, Lisp_Object);
541 static Lisp_Object font_prop_validate_symbol (Lisp_Object, Lisp_Object);
542 static Lisp_Object font_prop_validate_style (Lisp_Object, Lisp_Object);
543 static Lisp_Object font_prop_validate_non_neg (Lisp_Object, Lisp_Object);
544 static Lisp_Object font_prop_validate_spacing (Lisp_Object, Lisp_Object);
545 static int get_font_prop_index (Lisp_Object);
547 static Lisp_Object
548 font_prop_validate_symbol (Lisp_Object prop, Lisp_Object val)
550 if (STRINGP (val))
551 val = Fintern (val, Qnil);
552 if (! SYMBOLP (val))
553 val = Qerror;
554 else if (EQ (prop, QCregistry))
555 val = Fintern (Fdowncase (SYMBOL_NAME (val)), Qnil);
556 return val;
560 static Lisp_Object
561 font_prop_validate_style (Lisp_Object style, Lisp_Object val)
563 enum font_property_index prop = (EQ (style, QCweight) ? FONT_WEIGHT_INDEX
564 : EQ (style, QCslant) ? FONT_SLANT_INDEX
565 : FONT_WIDTH_INDEX);
566 if (INTEGERP (val))
568 EMACS_INT n = XINT (val);
569 CHECK_VECTOR (AREF (font_style_table, prop - FONT_WEIGHT_INDEX));
570 if (((n >> 4) & 0xF)
571 >= ASIZE (AREF (font_style_table, prop - FONT_WEIGHT_INDEX)))
572 val = Qerror;
573 else
575 Lisp_Object elt = AREF (AREF (font_style_table, prop - FONT_WEIGHT_INDEX), (n >> 4) & 0xF);
577 CHECK_VECTOR (elt);
578 if ((n & 0xF) + 1 >= ASIZE (elt))
579 val = Qerror;
580 else
582 CHECK_NUMBER (AREF (elt, 0));
583 if (XINT (AREF (elt, 0)) != (n >> 8))
584 val = Qerror;
588 else if (SYMBOLP (val))
590 int n = font_style_to_value (prop, val, 0);
592 val = n >= 0 ? make_number (n) : Qerror;
594 else
595 val = Qerror;
596 return val;
599 static Lisp_Object
600 font_prop_validate_non_neg (Lisp_Object prop, Lisp_Object val)
602 return (NATNUMP (val) || (FLOATP (val) && XFLOAT_DATA (val) >= 0)
603 ? val : Qerror);
606 static Lisp_Object
607 font_prop_validate_spacing (Lisp_Object prop, Lisp_Object val)
609 if (NILP (val) || (NATNUMP (val) && XINT (val) <= FONT_SPACING_CHARCELL))
610 return val;
611 if (SYMBOLP (val) && SBYTES (SYMBOL_NAME (val)) == 1)
613 char spacing = SDATA (SYMBOL_NAME (val))[0];
615 if (spacing == 'c' || spacing == 'C')
616 return make_number (FONT_SPACING_CHARCELL);
617 if (spacing == 'm' || spacing == 'M')
618 return make_number (FONT_SPACING_MONO);
619 if (spacing == 'p' || spacing == 'P')
620 return make_number (FONT_SPACING_PROPORTIONAL);
621 if (spacing == 'd' || spacing == 'D')
622 return make_number (FONT_SPACING_DUAL);
624 return Qerror;
627 static Lisp_Object
628 font_prop_validate_otf (Lisp_Object prop, Lisp_Object val)
630 Lisp_Object tail, tmp;
631 int i;
633 /* VAL = (SCRIPT [ LANGSYS [ GSUB-FEATURES [ GPOS-FEATURES ]]])
634 GSUB-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil
635 GPOS-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil */
636 if (! CONSP (val))
637 return Qerror;
638 if (! SYMBOLP (XCAR (val)))
639 return Qerror;
640 tail = XCDR (val);
641 if (NILP (tail))
642 return val;
643 if (! CONSP (tail) || ! SYMBOLP (XCAR (val)))
644 return Qerror;
645 for (i = 0; i < 2; i++)
647 tail = XCDR (tail);
648 if (NILP (tail))
649 return val;
650 if (! CONSP (tail))
651 return Qerror;
652 for (tmp = XCAR (tail); CONSP (tmp); tmp = XCDR (tmp))
653 if (! SYMBOLP (XCAR (tmp)))
654 return Qerror;
655 if (! NILP (tmp))
656 return Qerror;
658 return val;
661 /* Structure of known font property keys and validator of the
662 values. */
663 static const struct
665 /* Pointer to the key symbol. */
666 Lisp_Object *key;
667 /* Function to validate PROP's value VAL, or NULL if any value is
668 ok. The value is VAL or its regularized value if VAL is valid,
669 and Qerror if not. */
670 Lisp_Object (*validator) (Lisp_Object prop, Lisp_Object val);
671 } font_property_table[] =
672 { { &QCtype, font_prop_validate_symbol },
673 { &QCfoundry, font_prop_validate_symbol },
674 { &QCfamily, font_prop_validate_symbol },
675 { &QCadstyle, font_prop_validate_symbol },
676 { &QCregistry, font_prop_validate_symbol },
677 { &QCweight, font_prop_validate_style },
678 { &QCslant, font_prop_validate_style },
679 { &QCwidth, font_prop_validate_style },
680 { &QCsize, font_prop_validate_non_neg },
681 { &QCdpi, font_prop_validate_non_neg },
682 { &QCspacing, font_prop_validate_spacing },
683 { &QCavgwidth, font_prop_validate_non_neg },
684 /* The order of the above entries must match with enum
685 font_property_index. */
686 { &QClang, font_prop_validate_symbol },
687 { &QCscript, font_prop_validate_symbol },
688 { &QCotf, font_prop_validate_otf }
691 /* Return an index number of font property KEY or -1 if KEY is not an
692 already known property. */
694 static int
695 get_font_prop_index (Lisp_Object key)
697 int i;
699 for (i = 0; i < ARRAYELTS (font_property_table); i++)
700 if (EQ (key, *font_property_table[i].key))
701 return i;
702 return -1;
705 /* Validate the font property. The property key is specified by the
706 symbol PROP, or the index IDX (if PROP is nil). If VAL is invalid,
707 signal an error. The value is VAL or the regularized one. */
709 static Lisp_Object
710 font_prop_validate (int idx, Lisp_Object prop, Lisp_Object val)
712 Lisp_Object validated;
714 if (NILP (val))
715 return val;
716 if (NILP (prop))
717 prop = *font_property_table[idx].key;
718 else
720 idx = get_font_prop_index (prop);
721 if (idx < 0)
722 return val;
724 validated = (font_property_table[idx].validator) (prop, val);
725 if (EQ (validated, Qerror))
726 signal_error ("invalid font property", Fcons (prop, val));
727 return validated;
731 /* Store VAL as a value of extra font property PROP in FONT while
732 keeping the sorting order. Don't check the validity of VAL. */
734 Lisp_Object
735 font_put_extra (Lisp_Object font, Lisp_Object prop, Lisp_Object val)
737 Lisp_Object extra = AREF (font, FONT_EXTRA_INDEX);
738 Lisp_Object slot = (NILP (extra) ? Qnil : assq_no_quit (prop, extra));
740 if (NILP (slot))
742 Lisp_Object prev = Qnil;
744 while (CONSP (extra)
745 && NILP (Fstring_lessp (prop, XCAR (XCAR (extra)))))
746 prev = extra, extra = XCDR (extra);
748 if (NILP (prev))
749 ASET (font, FONT_EXTRA_INDEX, Fcons (Fcons (prop, val), extra));
750 else
751 XSETCDR (prev, Fcons (Fcons (prop, val), extra));
753 return val;
755 XSETCDR (slot, val);
756 if (NILP (val))
757 ASET (font, FONT_EXTRA_INDEX, Fdelq (slot, extra));
758 return val;
762 /* Font name parser and unparser. */
764 static int parse_matrix (const char *);
765 static int font_expand_wildcards (Lisp_Object *, int);
766 static int font_parse_name (char *, ptrdiff_t, Lisp_Object);
768 /* An enumerator for each field of an XLFD font name. */
769 enum xlfd_field_index
771 XLFD_FOUNDRY_INDEX,
772 XLFD_FAMILY_INDEX,
773 XLFD_WEIGHT_INDEX,
774 XLFD_SLANT_INDEX,
775 XLFD_SWIDTH_INDEX,
776 XLFD_ADSTYLE_INDEX,
777 XLFD_PIXEL_INDEX,
778 XLFD_POINT_INDEX,
779 XLFD_RESX_INDEX,
780 XLFD_RESY_INDEX,
781 XLFD_SPACING_INDEX,
782 XLFD_AVGWIDTH_INDEX,
783 XLFD_REGISTRY_INDEX,
784 XLFD_ENCODING_INDEX,
785 XLFD_LAST_INDEX
788 /* An enumerator for mask bit corresponding to each XLFD field. */
789 enum xlfd_field_mask
791 XLFD_FOUNDRY_MASK = 0x0001,
792 XLFD_FAMILY_MASK = 0x0002,
793 XLFD_WEIGHT_MASK = 0x0004,
794 XLFD_SLANT_MASK = 0x0008,
795 XLFD_SWIDTH_MASK = 0x0010,
796 XLFD_ADSTYLE_MASK = 0x0020,
797 XLFD_PIXEL_MASK = 0x0040,
798 XLFD_POINT_MASK = 0x0080,
799 XLFD_RESX_MASK = 0x0100,
800 XLFD_RESY_MASK = 0x0200,
801 XLFD_SPACING_MASK = 0x0400,
802 XLFD_AVGWIDTH_MASK = 0x0800,
803 XLFD_REGISTRY_MASK = 0x1000,
804 XLFD_ENCODING_MASK = 0x2000
808 /* Parse P pointing to the pixel/point size field of the form
809 `[A B C D]' which specifies a transformation matrix:
811 A B 0
812 C D 0
813 0 0 1
815 by which all glyphs of the font are transformed. The spec says
816 that scalar value N for the pixel/point size is equivalent to:
817 A = N * resx/resy, B = C = 0, D = N.
819 Return the scalar value N if the form is valid. Otherwise return
820 -1. */
822 static int
823 parse_matrix (const char *p)
825 double matrix[4];
826 char *end;
827 int i;
829 for (i = 0, p++; i < 4 && *p && *p != ']'; i++)
831 if (*p == '~')
832 matrix[i] = - strtod (p + 1, &end);
833 else
834 matrix[i] = strtod (p, &end);
835 p = end;
837 return (i == 4 ? (int) matrix[3] : -1);
840 /* Expand a wildcard field in FIELD (the first N fields are filled) to
841 multiple fields to fill in all 14 XLFD fields while restricting a
842 field position by its contents. */
844 static int
845 font_expand_wildcards (Lisp_Object *field, int n)
847 /* Copy of FIELD. */
848 Lisp_Object tmp[XLFD_LAST_INDEX];
849 /* Array of information about where this element can go. Nth
850 element is for Nth element of FIELD. */
851 struct {
852 /* Minimum possible field. */
853 int from;
854 /* Maximum possible field. */
855 int to;
856 /* Bit mask of possible field. Nth bit corresponds to Nth field. */
857 int mask;
858 } range[XLFD_LAST_INDEX];
859 int i, j;
860 int range_from, range_to;
861 unsigned range_mask;
863 #define XLFD_SYMBOL_MASK (XLFD_FOUNDRY_MASK | XLFD_FAMILY_MASK \
864 | XLFD_ADSTYLE_MASK | XLFD_REGISTRY_MASK)
865 #define XLFD_NULL_MASK (XLFD_FOUNDRY_MASK | XLFD_ADSTYLE_MASK)
866 #define XLFD_LARGENUM_MASK (XLFD_POINT_MASK | XLFD_RESX_MASK | XLFD_RESY_MASK \
867 | XLFD_AVGWIDTH_MASK)
868 #define XLFD_REGENC_MASK (XLFD_REGISTRY_MASK | XLFD_ENCODING_MASK)
870 /* Initialize RANGE_MASK for FIELD[0] which can be 0th to (14 - N)th
871 field. The value is shifted to left one bit by one in the
872 following loop. */
873 for (i = 0, range_mask = 0; i <= 14 - n; i++)
874 range_mask = (range_mask << 1) | 1;
876 /* The triplet RANGE_FROM, RANGE_TO, and RANGE_MASK is a
877 position-based restriction for FIELD[I]. */
878 for (i = 0, range_from = 0, range_to = 14 - n; i < n;
879 i++, range_from++, range_to++, range_mask <<= 1)
881 Lisp_Object val = field[i];
883 tmp[i] = val;
884 if (NILP (val))
886 /* Wildcard. */
887 range[i].from = range_from;
888 range[i].to = range_to;
889 range[i].mask = range_mask;
891 else
893 /* The triplet FROM, TO, and MASK is a value-based
894 restriction for FIELD[I]. */
895 int from, to;
896 unsigned mask;
898 if (INTEGERP (val))
900 EMACS_INT numeric = XINT (val);
902 if (i + 1 == n)
903 from = to = XLFD_ENCODING_INDEX,
904 mask = XLFD_ENCODING_MASK;
905 else if (numeric == 0)
906 from = XLFD_PIXEL_INDEX, to = XLFD_AVGWIDTH_INDEX,
907 mask = XLFD_PIXEL_MASK | XLFD_LARGENUM_MASK;
908 else if (numeric <= 48)
909 from = to = XLFD_PIXEL_INDEX,
910 mask = XLFD_PIXEL_MASK;
911 else
912 from = XLFD_POINT_INDEX, to = XLFD_AVGWIDTH_INDEX,
913 mask = XLFD_LARGENUM_MASK;
915 else if (SBYTES (SYMBOL_NAME (val)) == 0)
916 from = XLFD_FOUNDRY_INDEX, to = XLFD_ADSTYLE_INDEX,
917 mask = XLFD_NULL_MASK;
918 else if (i == 0)
919 from = to = XLFD_FOUNDRY_INDEX, mask = XLFD_FOUNDRY_MASK;
920 else if (i + 1 == n)
922 Lisp_Object name = SYMBOL_NAME (val);
924 if (SDATA (name)[SBYTES (name) - 1] == '*')
925 from = XLFD_REGISTRY_INDEX, to = XLFD_ENCODING_INDEX,
926 mask = XLFD_REGENC_MASK;
927 else
928 from = to = XLFD_ENCODING_INDEX,
929 mask = XLFD_ENCODING_MASK;
931 else if (range_from <= XLFD_WEIGHT_INDEX
932 && range_to >= XLFD_WEIGHT_INDEX
933 && FONT_WEIGHT_NAME_NUMERIC (val) >= 0)
934 from = to = XLFD_WEIGHT_INDEX, mask = XLFD_WEIGHT_MASK;
935 else if (range_from <= XLFD_SLANT_INDEX
936 && range_to >= XLFD_SLANT_INDEX
937 && FONT_SLANT_NAME_NUMERIC (val) >= 0)
938 from = to = XLFD_SLANT_INDEX, mask = XLFD_SLANT_MASK;
939 else if (range_from <= XLFD_SWIDTH_INDEX
940 && range_to >= XLFD_SWIDTH_INDEX
941 && FONT_WIDTH_NAME_NUMERIC (val) >= 0)
942 from = to = XLFD_SWIDTH_INDEX, mask = XLFD_SWIDTH_MASK;
943 else
945 if (EQ (val, Qc) || EQ (val, Qm) || EQ (val, Qp) || EQ (val, Qd))
946 from = to = XLFD_SPACING_INDEX, mask = XLFD_SPACING_MASK;
947 else
948 from = XLFD_FOUNDRY_INDEX, to = XLFD_ENCODING_INDEX,
949 mask = XLFD_SYMBOL_MASK;
952 /* Merge position-based and value-based restrictions. */
953 mask &= range_mask;
954 while (from < range_from)
955 mask &= ~(1 << from++);
956 while (from < 14 && ! (mask & (1 << from)))
957 from++;
958 while (to > range_to)
959 mask &= ~(1 << to--);
960 while (to >= 0 && ! (mask & (1 << to)))
961 to--;
962 if (from > to)
963 return -1;
964 range[i].from = from;
965 range[i].to = to;
966 range[i].mask = mask;
968 if (from > range_from || to < range_to)
970 /* The range is narrowed by value-based restrictions.
971 Reflect it to the other fields. */
973 /* Following fields should be after FROM. */
974 range_from = from;
975 /* Preceding fields should be before TO. */
976 for (j = i - 1, from--, to--; j >= 0; j--, from--, to--)
978 /* Check FROM for non-wildcard field. */
979 if (! NILP (tmp[j]) && range[j].from < from)
981 while (range[j].from < from)
982 range[j].mask &= ~(1 << range[j].from++);
983 while (from < 14 && ! (range[j].mask & (1 << from)))
984 from++;
985 range[j].from = from;
987 else
988 from = range[j].from;
989 if (range[j].to > to)
991 while (range[j].to > to)
992 range[j].mask &= ~(1 << range[j].to--);
993 while (to >= 0 && ! (range[j].mask & (1 << to)))
994 to--;
995 range[j].to = to;
997 else
998 to = range[j].to;
999 if (from > to)
1000 return -1;
1006 /* Decide all fields from restrictions in RANGE. */
1007 for (i = j = 0; i < n ; i++)
1009 if (j < range[i].from)
1011 if (i == 0 || ! NILP (tmp[i - 1]))
1012 /* None of TMP[X] corresponds to Jth field. */
1013 return -1;
1014 for (; j < range[i].from; j++)
1015 field[j] = Qnil;
1017 field[j++] = tmp[i];
1019 if (! NILP (tmp[n - 1]) && j < XLFD_REGISTRY_INDEX)
1020 return -1;
1021 for (; j < XLFD_LAST_INDEX; j++)
1022 field[j] = Qnil;
1023 if (INTEGERP (field[XLFD_ENCODING_INDEX]))
1024 field[XLFD_ENCODING_INDEX]
1025 = Fintern (Fnumber_to_string (field[XLFD_ENCODING_INDEX]), Qnil);
1026 return 0;
1030 /* Parse NAME (null terminated) as XLFD and store information in FONT
1031 (font-spec or font-entity). Size property of FONT is set as
1032 follows:
1033 specified XLFD fields FONT property
1034 --------------------- -------------
1035 PIXEL_SIZE PIXEL_SIZE (Lisp integer)
1036 POINT_SIZE and RESY calculated pixel size (Lisp integer)
1037 POINT_SIZE POINT_SIZE/10 (Lisp float)
1039 If NAME is successfully parsed, return 0. Otherwise return -1.
1041 FONT is usually a font-spec, but when this function is called from
1042 X font backend driver, it is a font-entity. In that case, NAME is
1043 a fully specified XLFD. */
1046 font_parse_xlfd (char *name, ptrdiff_t len, Lisp_Object font)
1048 int i, j, n;
1049 char *f[XLFD_LAST_INDEX + 1];
1050 Lisp_Object val;
1051 char *p;
1053 if (len > 255 || !len)
1054 /* Maximum XLFD name length is 255. */
1055 return -1;
1056 /* Accept "*-.." as a fully specified XLFD. */
1057 if (name[0] == '*' && (len == 1 || name[1] == '-'))
1058 i = 1, f[XLFD_FOUNDRY_INDEX] = name;
1059 else
1060 i = 0;
1061 for (p = name + i; *p; p++)
1062 if (*p == '-')
1064 f[i++] = p + 1;
1065 if (i == XLFD_LAST_INDEX)
1066 break;
1068 f[i] = name + len;
1070 #define INTERN_FIELD(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 0)
1071 #define INTERN_FIELD_SYM(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 1)
1073 if (i == XLFD_LAST_INDEX)
1075 /* Fully specified XLFD. */
1076 int pixel_size;
1078 ASET (font, FONT_FOUNDRY_INDEX, INTERN_FIELD_SYM (XLFD_FOUNDRY_INDEX));
1079 ASET (font, FONT_FAMILY_INDEX, INTERN_FIELD_SYM (XLFD_FAMILY_INDEX));
1080 for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
1081 i <= XLFD_SWIDTH_INDEX; i++, j++)
1083 val = INTERN_FIELD_SYM (i);
1084 if (! NILP (val))
1086 if ((n = font_style_to_value (j, INTERN_FIELD_SYM (i), 0)) < 0)
1087 return -1;
1088 ASET (font, j, make_number (n));
1091 ASET (font, FONT_ADSTYLE_INDEX, INTERN_FIELD_SYM (XLFD_ADSTYLE_INDEX));
1092 if (strcmp (f[XLFD_REGISTRY_INDEX], "*-*") == 0)
1093 ASET (font, FONT_REGISTRY_INDEX, Qnil);
1094 else
1095 ASET (font, FONT_REGISTRY_INDEX,
1096 font_intern_prop (f[XLFD_REGISTRY_INDEX],
1097 f[XLFD_LAST_INDEX] - f[XLFD_REGISTRY_INDEX],
1098 1));
1099 p = f[XLFD_PIXEL_INDEX];
1100 if (*p == '[' && (pixel_size = parse_matrix (p)) >= 0)
1101 ASET (font, FONT_SIZE_INDEX, make_number (pixel_size));
1102 else
1104 val = INTERN_FIELD (XLFD_PIXEL_INDEX);
1105 if (INTEGERP (val))
1106 ASET (font, FONT_SIZE_INDEX, val);
1107 else if (FONT_ENTITY_P (font))
1108 return -1;
1109 else
1111 double point_size = -1;
1113 eassert (FONT_SPEC_P (font));
1114 p = f[XLFD_POINT_INDEX];
1115 if (*p == '[')
1116 point_size = parse_matrix (p);
1117 else if (c_isdigit (*p))
1118 point_size = atoi (p), point_size /= 10;
1119 if (point_size >= 0)
1120 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1124 val = INTERN_FIELD (XLFD_RESY_INDEX);
1125 if (! NILP (val) && ! INTEGERP (val))
1126 return -1;
1127 ASET (font, FONT_DPI_INDEX, val);
1128 val = INTERN_FIELD (XLFD_SPACING_INDEX);
1129 if (! NILP (val))
1131 val = font_prop_validate_spacing (QCspacing, val);
1132 if (! INTEGERP (val))
1133 return -1;
1134 ASET (font, FONT_SPACING_INDEX, val);
1136 p = f[XLFD_AVGWIDTH_INDEX];
1137 if (*p == '~')
1138 p++;
1139 val = font_intern_prop (p, f[XLFD_REGISTRY_INDEX] - 1 - p, 0);
1140 if (! NILP (val) && ! INTEGERP (val))
1141 return -1;
1142 ASET (font, FONT_AVGWIDTH_INDEX, val);
1144 else
1146 bool wild_card_found = 0;
1147 Lisp_Object prop[XLFD_LAST_INDEX];
1149 if (FONT_ENTITY_P (font))
1150 return -1;
1151 for (j = 0; j < i; j++)
1153 if (*f[j] == '*')
1155 if (f[j][1] && f[j][1] != '-')
1156 return -1;
1157 prop[j] = Qnil;
1158 wild_card_found = 1;
1160 else if (j + 1 < i)
1161 prop[j] = INTERN_FIELD (j);
1162 else
1163 prop[j] = font_intern_prop (f[j], f[i] - f[j], 0);
1165 if (! wild_card_found)
1166 return -1;
1167 if (font_expand_wildcards (prop, i) < 0)
1168 return -1;
1170 ASET (font, FONT_FOUNDRY_INDEX, prop[XLFD_FOUNDRY_INDEX]);
1171 ASET (font, FONT_FAMILY_INDEX, prop[XLFD_FAMILY_INDEX]);
1172 for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
1173 i <= XLFD_SWIDTH_INDEX; i++, j++)
1174 if (! NILP (prop[i]))
1176 if ((n = font_style_to_value (j, prop[i], 1)) < 0)
1177 return -1;
1178 ASET (font, j, make_number (n));
1180 ASET (font, FONT_ADSTYLE_INDEX, prop[XLFD_ADSTYLE_INDEX]);
1181 val = prop[XLFD_REGISTRY_INDEX];
1182 if (NILP (val))
1184 val = prop[XLFD_ENCODING_INDEX];
1185 if (! NILP (val))
1187 AUTO_STRING (star_dash, "*-");
1188 val = concat2 (star_dash, SYMBOL_NAME (val));
1191 else if (NILP (prop[XLFD_ENCODING_INDEX]))
1193 AUTO_STRING (dash_star, "-*");
1194 val = concat2 (SYMBOL_NAME (val), dash_star);
1196 else
1198 AUTO_STRING (dash, "-");
1199 val = concat3 (SYMBOL_NAME (val), dash,
1200 SYMBOL_NAME (prop[XLFD_ENCODING_INDEX]));
1202 if (! NILP (val))
1203 ASET (font, FONT_REGISTRY_INDEX, Fintern (val, Qnil));
1205 if (INTEGERP (prop[XLFD_PIXEL_INDEX]))
1206 ASET (font, FONT_SIZE_INDEX, prop[XLFD_PIXEL_INDEX]);
1207 else if (INTEGERP (prop[XLFD_POINT_INDEX]))
1209 double point_size = XINT (prop[XLFD_POINT_INDEX]);
1211 ASET (font, FONT_SIZE_INDEX, make_float (point_size / 10));
1214 if (INTEGERP (prop[XLFD_RESX_INDEX]))
1215 ASET (font, FONT_DPI_INDEX, prop[XLFD_RESY_INDEX]);
1216 if (! NILP (prop[XLFD_SPACING_INDEX]))
1218 val = font_prop_validate_spacing (QCspacing,
1219 prop[XLFD_SPACING_INDEX]);
1220 if (! INTEGERP (val))
1221 return -1;
1222 ASET (font, FONT_SPACING_INDEX, val);
1224 if (INTEGERP (prop[XLFD_AVGWIDTH_INDEX]))
1225 ASET (font, FONT_AVGWIDTH_INDEX, prop[XLFD_AVGWIDTH_INDEX]);
1228 return 0;
1231 /* Store XLFD name of FONT (font-spec or font-entity) in NAME (NBYTES
1232 length), and return the name length. If FONT_SIZE_INDEX of FONT is
1233 0, use PIXEL_SIZE instead. */
1235 ptrdiff_t
1236 font_unparse_xlfd (Lisp_Object font, int pixel_size, char *name, int nbytes)
1238 char *p;
1239 const char *f[XLFD_REGISTRY_INDEX + 1];
1240 Lisp_Object val;
1241 int i, j, len;
1243 eassert (FONTP (font));
1245 for (i = FONT_FOUNDRY_INDEX, j = XLFD_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX;
1246 i++, j++)
1248 if (i == FONT_ADSTYLE_INDEX)
1249 j = XLFD_ADSTYLE_INDEX;
1250 else if (i == FONT_REGISTRY_INDEX)
1251 j = XLFD_REGISTRY_INDEX;
1252 val = AREF (font, i);
1253 if (NILP (val))
1255 if (j == XLFD_REGISTRY_INDEX)
1256 f[j] = "*-*";
1257 else
1258 f[j] = "*";
1260 else
1262 if (SYMBOLP (val))
1263 val = SYMBOL_NAME (val);
1264 if (j == XLFD_REGISTRY_INDEX
1265 && ! strchr (SSDATA (val), '-'))
1267 /* Change "jisx0208*" and "jisx0208" to "jisx0208*-*". */
1268 ptrdiff_t alloc = SBYTES (val) + 4;
1269 if (nbytes <= alloc)
1270 return -1;
1271 f[j] = p = alloca (alloc);
1272 sprintf (p, "%s%s-*", SDATA (val),
1273 &"*"[SDATA (val)[SBYTES (val) - 1] == '*']);
1275 else
1276 f[j] = SSDATA (val);
1280 for (i = FONT_WEIGHT_INDEX, j = XLFD_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX;
1281 i++, j++)
1283 val = font_style_symbolic (font, i, 0);
1284 if (NILP (val))
1285 f[j] = "*";
1286 else
1288 int c, k, l;
1289 ptrdiff_t alloc;
1291 val = SYMBOL_NAME (val);
1292 alloc = SBYTES (val) + 1;
1293 if (nbytes <= alloc)
1294 return -1;
1295 f[j] = p = alloca (alloc);
1296 /* Copy the name while excluding '-', '?', ',', and '"'. */
1297 for (k = l = 0; k < alloc; k++)
1299 c = SREF (val, k);
1300 if (c != '-' && c != '?' && c != ',' && c != '"')
1301 p[l++] = c;
1306 val = AREF (font, FONT_SIZE_INDEX);
1307 eassert (NUMBERP (val) || NILP (val));
1308 char font_size_index_buf[sizeof "-*"
1309 + max (INT_STRLEN_BOUND (EMACS_INT),
1310 1 + DBL_MAX_10_EXP + 1)];
1311 if (INTEGERP (val))
1313 EMACS_INT v = XINT (val);
1314 if (v <= 0)
1315 v = pixel_size;
1316 if (v > 0)
1318 f[XLFD_PIXEL_INDEX] = p = font_size_index_buf;
1319 sprintf (p, "%"pI"d-*", v);
1321 else
1322 f[XLFD_PIXEL_INDEX] = "*-*";
1324 else if (FLOATP (val))
1326 double v = XFLOAT_DATA (val) * 10;
1327 f[XLFD_PIXEL_INDEX] = p = font_size_index_buf;
1328 sprintf (p, "*-%.0f", v);
1330 else
1331 f[XLFD_PIXEL_INDEX] = "*-*";
1333 char dpi_index_buf[sizeof "-" + 2 * INT_STRLEN_BOUND (EMACS_INT)];
1334 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1336 EMACS_INT v = XINT (AREF (font, FONT_DPI_INDEX));
1337 f[XLFD_RESX_INDEX] = p = dpi_index_buf;
1338 sprintf (p, "%"pI"d-%"pI"d", v, v);
1340 else
1341 f[XLFD_RESX_INDEX] = "*-*";
1343 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1345 EMACS_INT spacing = XINT (AREF (font, FONT_SPACING_INDEX));
1347 f[XLFD_SPACING_INDEX] = (spacing <= FONT_SPACING_PROPORTIONAL ? "p"
1348 : spacing <= FONT_SPACING_DUAL ? "d"
1349 : spacing <= FONT_SPACING_MONO ? "m"
1350 : "c");
1352 else
1353 f[XLFD_SPACING_INDEX] = "*";
1355 char avgwidth_index_buf[INT_BUFSIZE_BOUND (EMACS_INT)];
1356 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1358 f[XLFD_AVGWIDTH_INDEX] = p = avgwidth_index_buf;
1359 sprintf (p, "%"pI"d", XINT (AREF (font, FONT_AVGWIDTH_INDEX)));
1361 else
1362 f[XLFD_AVGWIDTH_INDEX] = "*";
1364 len = snprintf (name, nbytes, "-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s",
1365 f[XLFD_FOUNDRY_INDEX], f[XLFD_FAMILY_INDEX],
1366 f[XLFD_WEIGHT_INDEX], f[XLFD_SLANT_INDEX],
1367 f[XLFD_SWIDTH_INDEX], f[XLFD_ADSTYLE_INDEX],
1368 f[XLFD_PIXEL_INDEX], f[XLFD_RESX_INDEX],
1369 f[XLFD_SPACING_INDEX], f[XLFD_AVGWIDTH_INDEX],
1370 f[XLFD_REGISTRY_INDEX]);
1371 return len < nbytes ? len : -1;
1374 /* Parse NAME (null terminated) and store information in FONT
1375 (font-spec or font-entity). NAME is supplied in either the
1376 Fontconfig or GTK font name format. If NAME is successfully
1377 parsed, return 0. Otherwise return -1.
1379 The fontconfig format is
1381 FAMILY[-SIZE][:PROP1[=VAL1][:PROP2[=VAL2]...]]
1383 The GTK format is
1385 FAMILY [PROPS...] [SIZE]
1387 This function tries to guess which format it is. */
1389 static int
1390 font_parse_fcname (char *name, ptrdiff_t len, Lisp_Object font)
1392 char *p, *q;
1393 char *size_beg = NULL, *size_end = NULL;
1394 char *props_beg = NULL, *family_end = NULL;
1396 if (len == 0)
1397 return -1;
1399 for (p = name; *p; p++)
1401 if (*p == '\\' && p[1])
1402 p++;
1403 else if (*p == ':')
1405 props_beg = family_end = p;
1406 break;
1408 else if (*p == '-')
1410 bool decimal = 0, size_found = 1;
1411 for (q = p + 1; *q && *q != ':'; q++)
1412 if (! c_isdigit (*q))
1414 if (*q != '.' || decimal)
1416 size_found = 0;
1417 break;
1419 decimal = 1;
1421 if (size_found)
1423 family_end = p;
1424 size_beg = p + 1;
1425 size_end = q;
1426 break;
1431 if (family_end)
1433 Lisp_Object extra_props = Qnil;
1435 /* A fontconfig name with size and/or property data. */
1436 if (family_end > name)
1438 Lisp_Object family;
1439 family = font_intern_prop (name, family_end - name, 1);
1440 ASET (font, FONT_FAMILY_INDEX, family);
1442 if (size_beg)
1444 double point_size = strtod (size_beg, &size_end);
1445 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1446 if (*size_end == ':' && size_end[1])
1447 props_beg = size_end;
1449 if (props_beg)
1451 /* Now parse ":KEY=VAL" patterns. */
1452 Lisp_Object val;
1454 for (p = props_beg; *p; p = q)
1456 for (q = p + 1; *q && *q != '=' && *q != ':'; q++);
1457 if (*q != '=')
1459 /* Must be an enumerated value. */
1460 ptrdiff_t word_len;
1461 p = p + 1;
1462 word_len = q - p;
1463 val = font_intern_prop (p, q - p, 1);
1465 #define PROP_MATCH(STR) (word_len == strlen (STR) \
1466 && memcmp (p, STR, strlen (STR)) == 0)
1468 if (PROP_MATCH ("light")
1469 || PROP_MATCH ("medium")
1470 || PROP_MATCH ("demibold")
1471 || PROP_MATCH ("bold")
1472 || PROP_MATCH ("black"))
1473 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, val);
1474 else if (PROP_MATCH ("roman")
1475 || PROP_MATCH ("italic")
1476 || PROP_MATCH ("oblique"))
1477 FONT_SET_STYLE (font, FONT_SLANT_INDEX, val);
1478 else if (PROP_MATCH ("charcell"))
1479 ASET (font, FONT_SPACING_INDEX,
1480 make_number (FONT_SPACING_CHARCELL));
1481 else if (PROP_MATCH ("mono"))
1482 ASET (font, FONT_SPACING_INDEX,
1483 make_number (FONT_SPACING_MONO));
1484 else if (PROP_MATCH ("proportional"))
1485 ASET (font, FONT_SPACING_INDEX,
1486 make_number (FONT_SPACING_PROPORTIONAL));
1487 #undef PROP_MATCH
1489 else
1491 /* KEY=VAL pairs */
1492 Lisp_Object key;
1493 int prop;
1495 if (q - p == 10 && memcmp (p + 1, "pixelsize", 9) == 0)
1496 prop = FONT_SIZE_INDEX;
1497 else
1499 key = font_intern_prop (p, q - p, 1);
1500 prop = get_font_prop_index (key);
1503 p = q + 1;
1504 for (q = p; *q && *q != ':'; q++);
1505 val = font_intern_prop (p, q - p, 0);
1507 if (prop >= FONT_FOUNDRY_INDEX
1508 && prop < FONT_EXTRA_INDEX)
1509 ASET (font, prop, font_prop_validate (prop, Qnil, val));
1510 else
1512 extra_props = nconc2 (extra_props,
1513 list1 (Fcons (key, val)));
1516 p = q;
1520 if (! NILP (extra_props))
1522 struct font_driver_list *driver_list = font_driver_list;
1523 for ( ; driver_list; driver_list = driver_list->next)
1524 if (driver_list->driver->filter_properties)
1525 (*driver_list->driver->filter_properties) (font, extra_props);
1529 else
1531 /* Either a fontconfig-style name with no size and property
1532 data, or a GTK-style name. */
1533 Lisp_Object weight = Qnil, slant = Qnil;
1534 Lisp_Object width = Qnil, size = Qnil;
1535 char *word_start;
1536 ptrdiff_t word_len;
1538 /* Scan backwards from the end, looking for a size. */
1539 for (p = name + len - 1; p >= name; p--)
1540 if (!c_isdigit (*p))
1541 break;
1543 if ((p < name + len - 1) && ((p + 1 == name) || *p == ' '))
1544 /* Found a font size. */
1545 size = make_float (strtod (p + 1, NULL));
1546 else
1547 p = name + len;
1549 /* Now P points to the termination of the string, sans size.
1550 Scan backwards, looking for font properties. */
1551 for (; p > name; p = q)
1553 for (q = p - 1; q >= name; q--)
1555 if (q > name && *(q-1) == '\\')
1556 --q; /* Skip quoting backslashes. */
1557 else if (*q == ' ')
1558 break;
1561 word_start = q + 1;
1562 word_len = p - word_start;
1564 #define PROP_MATCH(STR) \
1565 (word_len == strlen (STR) \
1566 && memcmp (word_start, STR, strlen (STR)) == 0)
1567 #define PROP_SAVE(VAR, STR) \
1568 (VAR = NILP (VAR) ? font_intern_prop (STR, strlen (STR), 1) : VAR)
1570 if (PROP_MATCH ("Ultra-Light"))
1571 PROP_SAVE (weight, "ultra-light");
1572 else if (PROP_MATCH ("Light"))
1573 PROP_SAVE (weight, "light");
1574 else if (PROP_MATCH ("Book"))
1575 PROP_SAVE (weight, "book");
1576 else if (PROP_MATCH ("Medium"))
1577 PROP_SAVE (weight, "medium");
1578 else if (PROP_MATCH ("Semi-Bold"))
1579 PROP_SAVE (weight, "semi-bold");
1580 else if (PROP_MATCH ("Bold"))
1581 PROP_SAVE (weight, "bold");
1582 else if (PROP_MATCH ("Italic"))
1583 PROP_SAVE (slant, "italic");
1584 else if (PROP_MATCH ("Oblique"))
1585 PROP_SAVE (slant, "oblique");
1586 else if (PROP_MATCH ("Semi-Condensed"))
1587 PROP_SAVE (width, "semi-condensed");
1588 else if (PROP_MATCH ("Condensed"))
1589 PROP_SAVE (width, "condensed");
1590 /* An unknown word must be part of the font name. */
1591 else
1593 family_end = p;
1594 break;
1597 #undef PROP_MATCH
1598 #undef PROP_SAVE
1600 if (family_end)
1601 ASET (font, FONT_FAMILY_INDEX,
1602 font_intern_prop (name, family_end - name, 1));
1603 if (!NILP (size))
1604 ASET (font, FONT_SIZE_INDEX, size);
1605 if (!NILP (weight))
1606 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, weight);
1607 if (!NILP (slant))
1608 FONT_SET_STYLE (font, FONT_SLANT_INDEX, slant);
1609 if (!NILP (width))
1610 FONT_SET_STYLE (font, FONT_WIDTH_INDEX, width);
1613 return 0;
1616 #if defined HAVE_XFT || defined HAVE_FREETYPE || defined HAVE_NS
1618 /* Store fontconfig's font name of FONT (font-spec or font-entity) in
1619 NAME (NBYTES length), and return the name length. If
1620 FONT_SIZE_INDEX of FONT is 0, use PIXEL_SIZE instead.
1621 Return a negative value on error. */
1623 static int
1624 font_unparse_fcname (Lisp_Object font, int pixel_size, char *name, int nbytes)
1626 Lisp_Object family, foundry;
1627 Lisp_Object val;
1628 int point_size;
1629 int i;
1630 char *p;
1631 char *lim;
1632 Lisp_Object styles[3];
1633 const char *style_names[3] = { "weight", "slant", "width" };
1635 family = AREF (font, FONT_FAMILY_INDEX);
1636 if (! NILP (family))
1638 if (SYMBOLP (family))
1639 family = SYMBOL_NAME (family);
1640 else
1641 family = Qnil;
1644 val = AREF (font, FONT_SIZE_INDEX);
1645 if (INTEGERP (val))
1647 if (XINT (val) != 0)
1648 pixel_size = XINT (val);
1649 point_size = -1;
1651 else
1653 eassert (FLOATP (val));
1654 pixel_size = -1;
1655 point_size = (int) XFLOAT_DATA (val);
1658 foundry = AREF (font, FONT_FOUNDRY_INDEX);
1659 if (! NILP (foundry))
1661 if (SYMBOLP (foundry))
1662 foundry = SYMBOL_NAME (foundry);
1663 else
1664 foundry = Qnil;
1667 for (i = 0; i < 3; i++)
1668 styles[i] = font_style_symbolic (font, FONT_WEIGHT_INDEX + i, 0);
1670 p = name;
1671 lim = name + nbytes;
1672 if (! NILP (family))
1674 int len = snprintf (p, lim - p, "%s", SSDATA (family));
1675 if (! (0 <= len && len < lim - p))
1676 return -1;
1677 p += len;
1679 if (point_size > 0)
1681 int len = snprintf (p, lim - p, &"-%d"[p == name], point_size);
1682 if (! (0 <= len && len < lim - p))
1683 return -1;
1684 p += len;
1686 else if (pixel_size > 0)
1688 int len = snprintf (p, lim - p, ":pixelsize=%d", pixel_size);
1689 if (! (0 <= len && len < lim - p))
1690 return -1;
1691 p += len;
1693 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
1695 int len = snprintf (p, lim - p, ":foundry=%s",
1696 SSDATA (SYMBOL_NAME (AREF (font,
1697 FONT_FOUNDRY_INDEX))));
1698 if (! (0 <= len && len < lim - p))
1699 return -1;
1700 p += len;
1702 for (i = 0; i < 3; i++)
1703 if (! NILP (styles[i]))
1705 int len = snprintf (p, lim - p, ":%s=%s", style_names[i],
1706 SSDATA (SYMBOL_NAME (styles[i])));
1707 if (! (0 <= len && len < lim - p))
1708 return -1;
1709 p += len;
1712 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1714 int len = snprintf (p, lim - p, ":dpi=%"pI"d",
1715 XINT (AREF (font, FONT_DPI_INDEX)));
1716 if (! (0 <= len && len < lim - p))
1717 return -1;
1718 p += len;
1721 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1723 int len = snprintf (p, lim - p, ":spacing=%"pI"d",
1724 XINT (AREF (font, FONT_SPACING_INDEX)));
1725 if (! (0 <= len && len < lim - p))
1726 return -1;
1727 p += len;
1730 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1732 int len = snprintf (p, lim - p,
1733 (XINT (AREF (font, FONT_AVGWIDTH_INDEX)) == 0
1734 ? ":scalable=true"
1735 : ":scalable=false"));
1736 if (! (0 <= len && len < lim - p))
1737 return -1;
1738 p += len;
1741 return (p - name);
1744 #endif
1746 /* Parse NAME (null terminated) and store information in FONT
1747 (font-spec or font-entity). If NAME is successfully parsed, return
1748 0. Otherwise return -1. */
1750 static int
1751 font_parse_name (char *name, ptrdiff_t namelen, Lisp_Object font)
1753 if (name[0] == '-' || strchr (name, '*') || strchr (name, '?'))
1754 return font_parse_xlfd (name, namelen, font);
1755 return font_parse_fcname (name, namelen, font);
1759 /* Merge FAMILY and REGISTRY into FONT_SPEC. FAMILY may have the form
1760 "FAMILY-FOUNDRY". REGISTRY may not contain charset-encoding
1761 part. */
1763 void
1764 font_parse_family_registry (Lisp_Object family, Lisp_Object registry, Lisp_Object font_spec)
1766 ptrdiff_t len;
1767 char *p0, *p1;
1769 if (! NILP (family)
1770 && NILP (AREF (font_spec, FONT_FAMILY_INDEX)))
1772 CHECK_STRING (family);
1773 len = SBYTES (family);
1774 p0 = SSDATA (family);
1775 p1 = strchr (p0, '-');
1776 if (p1)
1778 if ((*p0 != '*' && p1 - p0 > 0)
1779 && NILP (AREF (font_spec, FONT_FOUNDRY_INDEX)))
1780 Ffont_put (font_spec, QCfoundry, font_intern_prop (p0, p1 - p0, 1));
1781 p1++;
1782 len -= p1 - p0;
1783 Ffont_put (font_spec, QCfamily, font_intern_prop (p1, len, 1));
1785 else
1786 ASET (font_spec, FONT_FAMILY_INDEX, Fintern (family, Qnil));
1788 if (! NILP (registry))
1790 /* Convert "XXX" and "XXX*" to "XXX*-*". */
1791 CHECK_STRING (registry);
1792 len = SBYTES (registry);
1793 p0 = SSDATA (registry);
1794 p1 = strchr (p0, '-');
1795 if (! p1)
1797 AUTO_STRING (extra, ("*-*" + (len && p0[len - 1] == '*')));
1798 registry = concat2 (registry, extra);
1800 registry = Fdowncase (registry);
1801 ASET (font_spec, FONT_REGISTRY_INDEX, Fintern (registry, Qnil));
1806 /* This part (through the next ^L) is still experimental and not
1807 tested much. We may drastically change codes. */
1809 /* OTF handler. */
1811 #if 0
1813 #define LGSTRING_HEADER_SIZE 6
1814 #define LGSTRING_GLYPH_SIZE 8
1816 static int
1817 check_gstring (Lisp_Object gstring)
1819 Lisp_Object val;
1820 ptrdiff_t i;
1821 int j;
1823 CHECK_VECTOR (gstring);
1824 val = AREF (gstring, 0);
1825 CHECK_VECTOR (val);
1826 if (ASIZE (val) < LGSTRING_HEADER_SIZE)
1827 goto err;
1828 CHECK_FONT_OBJECT (LGSTRING_FONT (gstring));
1829 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_LBEARING)))
1830 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_LBEARING));
1831 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_RBEARING)))
1832 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_RBEARING));
1833 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_WIDTH)))
1834 CHECK_NATNUM (LGSTRING_SLOT (gstring, LGSTRING_IX_WIDTH));
1835 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT)))
1836 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT));
1837 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT)))
1838 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT));
1840 for (i = 0; i < LGSTRING_GLYPH_LEN (gstring); i++)
1842 val = LGSTRING_GLYPH (gstring, i);
1843 CHECK_VECTOR (val);
1844 if (ASIZE (val) < LGSTRING_GLYPH_SIZE)
1845 goto err;
1846 if (NILP (AREF (val, LGLYPH_IX_CHAR)))
1847 break;
1848 CHECK_NATNUM (AREF (val, LGLYPH_IX_FROM));
1849 CHECK_NATNUM (AREF (val, LGLYPH_IX_TO));
1850 CHECK_CHARACTER (AREF (val, LGLYPH_IX_CHAR));
1851 if (!NILP (AREF (val, LGLYPH_IX_CODE)))
1852 CHECK_NATNUM (AREF (val, LGLYPH_IX_CODE));
1853 if (!NILP (AREF (val, LGLYPH_IX_WIDTH)))
1854 CHECK_NATNUM (AREF (val, LGLYPH_IX_WIDTH));
1855 if (!NILP (AREF (val, LGLYPH_IX_ADJUSTMENT)))
1857 val = AREF (val, LGLYPH_IX_ADJUSTMENT);
1858 CHECK_VECTOR (val);
1859 if (ASIZE (val) < 3)
1860 goto err;
1861 for (j = 0; j < 3; j++)
1862 CHECK_NUMBER (AREF (val, j));
1865 return i;
1866 err:
1867 error ("Invalid glyph-string format");
1868 return -1;
1871 static void
1872 check_otf_features (Lisp_Object otf_features)
1874 Lisp_Object val;
1876 CHECK_CONS (otf_features);
1877 CHECK_SYMBOL (XCAR (otf_features));
1878 otf_features = XCDR (otf_features);
1879 CHECK_CONS (otf_features);
1880 CHECK_SYMBOL (XCAR (otf_features));
1881 otf_features = XCDR (otf_features);
1882 for (val = Fcar (otf_features); CONSP (val); val = XCDR (val))
1884 CHECK_SYMBOL (XCAR (val));
1885 if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
1886 error ("Invalid OTF GSUB feature: %s",
1887 SDATA (SYMBOL_NAME (XCAR (val))));
1889 otf_features = XCDR (otf_features);
1890 for (val = Fcar (otf_features); CONSP (val); val = XCDR (val))
1892 CHECK_SYMBOL (XCAR (val));
1893 if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
1894 error ("Invalid OTF GPOS feature: %s",
1895 SDATA (SYMBOL_NAME (XCAR (val))));
1899 #ifdef HAVE_LIBOTF
1900 #include <otf.h>
1902 Lisp_Object otf_list;
1904 static Lisp_Object
1905 otf_tag_symbol (OTF_Tag tag)
1907 char name[5];
1909 OTF_tag_name (tag, name);
1910 return Fintern (make_unibyte_string (name, 4), Qnil);
1913 static OTF *
1914 otf_open (Lisp_Object file)
1916 Lisp_Object val = Fassoc (file, otf_list);
1917 OTF *otf;
1919 if (! NILP (val))
1920 otf = XSAVE_POINTER (XCDR (val), 0);
1921 else
1923 otf = STRINGP (file) ? OTF_open (SSDATA (file)) : NULL;
1924 val = make_save_ptr (otf);
1925 otf_list = Fcons (Fcons (file, val), otf_list);
1927 return otf;
1931 /* Return a list describing which scripts/languages FONT supports by
1932 which GSUB/GPOS features of OpenType tables. See the comment of
1933 (struct font_driver).otf_capability. */
1935 Lisp_Object
1936 font_otf_capability (struct font *font)
1938 OTF *otf;
1939 Lisp_Object capability = Fcons (Qnil, Qnil);
1940 int i;
1942 otf = otf_open (font->props[FONT_FILE_INDEX]);
1943 if (! otf)
1944 return Qnil;
1945 for (i = 0; i < 2; i++)
1947 OTF_GSUB_GPOS *gsub_gpos;
1948 Lisp_Object script_list = Qnil;
1949 int j;
1951 if (OTF_get_features (otf, i == 0) < 0)
1952 continue;
1953 gsub_gpos = i == 0 ? otf->gsub : otf->gpos;
1954 for (j = gsub_gpos->ScriptList.ScriptCount - 1; j >= 0; j--)
1956 OTF_Script *script = gsub_gpos->ScriptList.Script + j;
1957 Lisp_Object langsys_list = Qnil;
1958 Lisp_Object script_tag = otf_tag_symbol (script->ScriptTag);
1959 int k;
1961 for (k = script->LangSysCount; k >= 0; k--)
1963 OTF_LangSys *langsys;
1964 Lisp_Object feature_list = Qnil;
1965 Lisp_Object langsys_tag;
1966 int l;
1968 if (k == script->LangSysCount)
1970 langsys = &script->DefaultLangSys;
1971 langsys_tag = Qnil;
1973 else
1975 langsys = script->LangSys + k;
1976 langsys_tag
1977 = otf_tag_symbol (script->LangSysRecord[k].LangSysTag);
1979 for (l = langsys->FeatureCount - 1; l >= 0; l--)
1981 OTF_Feature *feature
1982 = gsub_gpos->FeatureList.Feature + langsys->FeatureIndex[l];
1983 Lisp_Object feature_tag
1984 = otf_tag_symbol (feature->FeatureTag);
1986 feature_list = Fcons (feature_tag, feature_list);
1988 langsys_list = Fcons (Fcons (langsys_tag, feature_list),
1989 langsys_list);
1991 script_list = Fcons (Fcons (script_tag, langsys_list),
1992 script_list);
1995 if (i == 0)
1996 XSETCAR (capability, script_list);
1997 else
1998 XSETCDR (capability, script_list);
2001 return capability;
2004 /* Parse OTF features in SPEC and write a proper features spec string
2005 in FEATURES for the call of OTF_drive_gsub/gpos (of libotf). It is
2006 assured that the sufficient memory has already allocated for
2007 FEATURES. */
2009 static void
2010 generate_otf_features (Lisp_Object spec, char *features)
2012 Lisp_Object val;
2013 char *p;
2014 bool asterisk;
2016 p = features;
2017 *p = '\0';
2018 for (asterisk = 0; CONSP (spec); spec = XCDR (spec))
2020 val = XCAR (spec);
2021 CHECK_SYMBOL (val);
2022 if (p > features)
2023 *p++ = ',';
2024 if (SREF (SYMBOL_NAME (val), 0) == '*')
2026 asterisk = 1;
2027 *p++ = '*';
2029 else if (! asterisk)
2031 val = SYMBOL_NAME (val);
2032 p += esprintf (p, "%s", SDATA (val));
2034 else
2036 val = SYMBOL_NAME (val);
2037 p += esprintf (p, "~%s", SDATA (val));
2040 if (CONSP (spec))
2041 error ("OTF spec too long");
2044 Lisp_Object
2045 font_otf_DeviceTable (OTF_DeviceTable *device_table)
2047 int len = device_table->StartSize - device_table->EndSize + 1;
2049 return Fcons (make_number (len),
2050 make_unibyte_string (device_table->DeltaValue, len));
2053 Lisp_Object
2054 font_otf_ValueRecord (int value_format, OTF_ValueRecord *value_record)
2056 Lisp_Object val = Fmake_vector (make_number (8), Qnil);
2058 if (value_format & OTF_XPlacement)
2059 ASET (val, 0, make_number (value_record->XPlacement));
2060 if (value_format & OTF_YPlacement)
2061 ASET (val, 1, make_number (value_record->YPlacement));
2062 if (value_format & OTF_XAdvance)
2063 ASET (val, 2, make_number (value_record->XAdvance));
2064 if (value_format & OTF_YAdvance)
2065 ASET (val, 3, make_number (value_record->YAdvance));
2066 if (value_format & OTF_XPlaDevice)
2067 ASET (val, 4, font_otf_DeviceTable (&value_record->XPlaDevice));
2068 if (value_format & OTF_YPlaDevice)
2069 ASET (val, 4, font_otf_DeviceTable (&value_record->YPlaDevice));
2070 if (value_format & OTF_XAdvDevice)
2071 ASET (val, 4, font_otf_DeviceTable (&value_record->XAdvDevice));
2072 if (value_format & OTF_YAdvDevice)
2073 ASET (val, 4, font_otf_DeviceTable (&value_record->YAdvDevice));
2074 return val;
2077 Lisp_Object
2078 font_otf_Anchor (OTF_Anchor *anchor)
2080 Lisp_Object val;
2082 val = Fmake_vector (make_number (anchor->AnchorFormat + 1), Qnil);
2083 ASET (val, 0, make_number (anchor->XCoordinate));
2084 ASET (val, 1, make_number (anchor->YCoordinate));
2085 if (anchor->AnchorFormat == 2)
2086 ASET (val, 2, make_number (anchor->f.f1.AnchorPoint));
2087 else
2089 ASET (val, 3, font_otf_DeviceTable (&anchor->f.f2.XDeviceTable));
2090 ASET (val, 4, font_otf_DeviceTable (&anchor->f.f2.YDeviceTable));
2092 return val;
2094 #endif /* HAVE_LIBOTF */
2095 #endif /* 0 */
2098 /* Font sorting. */
2100 static double
2101 font_rescale_ratio (Lisp_Object font_entity)
2103 Lisp_Object tail, elt;
2104 Lisp_Object name = Qnil;
2106 for (tail = Vface_font_rescale_alist; CONSP (tail); tail = XCDR (tail))
2108 elt = XCAR (tail);
2109 if (FLOATP (XCDR (elt)))
2111 if (STRINGP (XCAR (elt)))
2113 if (NILP (name))
2114 name = Ffont_xlfd_name (font_entity, Qnil);
2115 if (fast_string_match_ignore_case (XCAR (elt), name) >= 0)
2116 return XFLOAT_DATA (XCDR (elt));
2118 else if (FONT_SPEC_P (XCAR (elt)))
2120 if (font_match_p (XCAR (elt), font_entity))
2121 return XFLOAT_DATA (XCDR (elt));
2125 return 1.0;
2128 /* We sort fonts by scoring each of them against a specified
2129 font-spec. The score value is 32 bit (`unsigned'), and the smaller
2130 the value is, the closer the font is to the font-spec.
2132 The lowest 2 bits of the score are used for driver type. The font
2133 available by the most preferred font driver is 0.
2135 The 4 7-bit fields in the higher 28 bits are used for numeric properties
2136 WEIGHT, SLANT, WIDTH, and SIZE. */
2138 /* How many bits to shift to store the difference value of each font
2139 property in a score. Note that floats for FONT_TYPE_INDEX and
2140 FONT_REGISTRY_INDEX are not used. */
2141 static int sort_shift_bits[FONT_SIZE_INDEX + 1];
2143 /* Score font-entity ENTITY against properties of font-spec SPEC_PROP.
2144 The return value indicates how different ENTITY is compared with
2145 SPEC_PROP. */
2147 static unsigned
2148 font_score (Lisp_Object entity, Lisp_Object *spec_prop)
2150 unsigned score = 0;
2151 int i;
2153 /* Score three style numeric fields. Maximum difference is 127. */
2154 for (i = FONT_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX; i++)
2155 if (! NILP (spec_prop[i]) && ! EQ (AREF (entity, i), spec_prop[i]))
2157 EMACS_INT diff = ((XINT (AREF (entity, i)) >> 8)
2158 - (XINT (spec_prop[i]) >> 8));
2159 score |= min (eabs (diff), 127) << sort_shift_bits[i];
2162 /* Score the size. Maximum difference is 127. */
2163 i = FONT_SIZE_INDEX;
2164 if (! NILP (spec_prop[FONT_SIZE_INDEX])
2165 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
2167 /* We use the higher 6-bit for the actual size difference. The
2168 lowest bit is set if the DPI is different. */
2169 EMACS_INT diff;
2170 EMACS_INT pixel_size = XINT (spec_prop[FONT_SIZE_INDEX]);
2171 EMACS_INT entity_size = XINT (AREF (entity, FONT_SIZE_INDEX));
2173 if (CONSP (Vface_font_rescale_alist))
2174 pixel_size *= font_rescale_ratio (entity);
2175 if (pixel_size * 2 < entity_size || entity_size * 2 < pixel_size)
2176 /* This size is wrong by more than a factor 2: reject it! */
2177 return 0xFFFFFFFF;
2178 diff = eabs (pixel_size - entity_size) << 1;
2179 if (! NILP (spec_prop[FONT_DPI_INDEX])
2180 && ! EQ (spec_prop[FONT_DPI_INDEX], AREF (entity, FONT_DPI_INDEX)))
2181 diff |= 1;
2182 if (! NILP (spec_prop[FONT_AVGWIDTH_INDEX])
2183 && ! EQ (spec_prop[FONT_AVGWIDTH_INDEX], AREF (entity, FONT_AVGWIDTH_INDEX)))
2184 diff |= 1;
2185 score |= min (diff, 127) << sort_shift_bits[FONT_SIZE_INDEX];
2188 return score;
2192 /* Concatenate all elements of LIST into one vector. LIST is a list
2193 of font-entity vectors. */
2195 static Lisp_Object
2196 font_vconcat_entity_vectors (Lisp_Object list)
2198 EMACS_INT nargs = XFASTINT (Flength (list));
2199 Lisp_Object *args;
2200 USE_SAFE_ALLOCA;
2201 SAFE_ALLOCA_LISP (args, nargs);
2202 ptrdiff_t i;
2204 for (i = 0; i < nargs; i++, list = XCDR (list))
2205 args[i] = XCAR (list);
2206 Lisp_Object result = Fvconcat (nargs, args);
2207 SAFE_FREE ();
2208 return result;
2212 /* The structure for elements being sorted by qsort. */
2213 struct font_sort_data
2215 unsigned score;
2216 int font_driver_preference;
2217 Lisp_Object entity;
2221 /* The comparison function for qsort. */
2223 static int
2224 font_compare (const void *d1, const void *d2)
2226 const struct font_sort_data *data1 = d1;
2227 const struct font_sort_data *data2 = d2;
2229 if (data1->score < data2->score)
2230 return -1;
2231 else if (data1->score > data2->score)
2232 return 1;
2233 return (data1->font_driver_preference - data2->font_driver_preference);
2237 /* Sort each font-entity vector in LIST by closeness to font-spec PREFER.
2238 If PREFER specifies a point-size, calculate the corresponding
2239 pixel-size from QCdpi property of PREFER or from the Y-resolution
2240 of FRAME before sorting.
2242 If BEST-ONLY is nonzero, return the best matching entity (that
2243 supports the character BEST-ONLY if BEST-ONLY is positive, or any
2244 if BEST-ONLY is negative). Otherwise, return the sorted result as
2245 a single vector of font-entities.
2247 This function does no optimization for the case that the total
2248 number of elements is 1. The caller should avoid calling this in
2249 such a case. */
2251 static Lisp_Object
2252 font_sort_entities (Lisp_Object list, Lisp_Object prefer,
2253 struct frame *f, int best_only)
2255 Lisp_Object prefer_prop[FONT_SPEC_MAX];
2256 int len, maxlen, i;
2257 struct font_sort_data *data;
2258 unsigned best_score;
2259 Lisp_Object best_entity;
2260 Lisp_Object tail, vec IF_LINT (= Qnil);
2261 USE_SAFE_ALLOCA;
2263 for (i = FONT_WEIGHT_INDEX; i <= FONT_AVGWIDTH_INDEX; i++)
2264 prefer_prop[i] = AREF (prefer, i);
2265 if (FLOATP (prefer_prop[FONT_SIZE_INDEX]))
2266 prefer_prop[FONT_SIZE_INDEX]
2267 = make_number (font_pixel_size (f, prefer));
2269 if (NILP (XCDR (list)))
2271 /* What we have to take care of is this single vector. */
2272 vec = XCAR (list);
2273 maxlen = ASIZE (vec);
2275 else if (best_only)
2277 /* We don't have to perform sort, so there's no need of creating
2278 a single vector. But, we must find the length of the longest
2279 vector. */
2280 maxlen = 0;
2281 for (tail = list; CONSP (tail); tail = XCDR (tail))
2282 if (maxlen < ASIZE (XCAR (tail)))
2283 maxlen = ASIZE (XCAR (tail));
2285 else
2287 /* We have to create a single vector to sort it. */
2288 vec = font_vconcat_entity_vectors (list);
2289 maxlen = ASIZE (vec);
2292 data = SAFE_ALLOCA (maxlen * sizeof *data);
2293 best_score = 0xFFFFFFFF;
2294 best_entity = Qnil;
2296 for (tail = list; CONSP (tail); tail = XCDR (tail))
2298 int font_driver_preference = 0;
2299 Lisp_Object current_font_driver;
2301 if (best_only)
2302 vec = XCAR (tail);
2303 len = ASIZE (vec);
2305 /* We are sure that the length of VEC > 0. */
2306 current_font_driver = AREF (AREF (vec, 0), FONT_TYPE_INDEX);
2307 /* Score the elements. */
2308 for (i = 0; i < len; i++)
2310 data[i].entity = AREF (vec, i);
2311 data[i].score
2312 = ((best_only <= 0 || font_has_char (f, data[i].entity, best_only)
2313 > 0)
2314 ? font_score (data[i].entity, prefer_prop)
2315 : 0xFFFFFFFF);
2316 if (best_only && best_score > data[i].score)
2318 best_score = data[i].score;
2319 best_entity = data[i].entity;
2320 if (best_score == 0)
2321 break;
2323 if (! EQ (current_font_driver, AREF (AREF (vec, i), FONT_TYPE_INDEX)))
2325 current_font_driver = AREF (AREF (vec, i), FONT_TYPE_INDEX);
2326 font_driver_preference++;
2328 data[i].font_driver_preference = font_driver_preference;
2331 /* Sort if necessary. */
2332 if (! best_only)
2334 qsort (data, len, sizeof *data, font_compare);
2335 for (i = 0; i < len; i++)
2336 ASET (vec, i, data[i].entity);
2337 break;
2339 else
2340 vec = best_entity;
2343 SAFE_FREE ();
2345 FONT_ADD_LOG ("sort-by", prefer, vec);
2346 return vec;
2350 /* API of Font Service Layer. */
2352 /* Reflect ORDER (see the variable font_sort_order in xfaces.c) to
2353 sort_shift_bits. Finternal_set_font_selection_order calls this
2354 function with font_sort_order after setting up it. */
2356 void
2357 font_update_sort_order (int *order)
2359 int i, shift_bits;
2361 for (i = 0, shift_bits = 23; i < 4; i++, shift_bits -= 7)
2363 int xlfd_idx = order[i];
2365 if (xlfd_idx == XLFD_WEIGHT_INDEX)
2366 sort_shift_bits[FONT_WEIGHT_INDEX] = shift_bits;
2367 else if (xlfd_idx == XLFD_SLANT_INDEX)
2368 sort_shift_bits[FONT_SLANT_INDEX] = shift_bits;
2369 else if (xlfd_idx == XLFD_SWIDTH_INDEX)
2370 sort_shift_bits[FONT_WIDTH_INDEX] = shift_bits;
2371 else
2372 sort_shift_bits[FONT_SIZE_INDEX] = shift_bits;
2376 static bool
2377 font_check_otf_features (Lisp_Object script, Lisp_Object langsys,
2378 Lisp_Object features, Lisp_Object table)
2380 Lisp_Object val;
2381 bool negative;
2383 table = assq_no_quit (script, table);
2384 if (NILP (table))
2385 return 0;
2386 table = XCDR (table);
2387 if (! NILP (langsys))
2389 table = assq_no_quit (langsys, table);
2390 if (NILP (table))
2391 return 0;
2393 else
2395 val = assq_no_quit (Qnil, table);
2396 if (NILP (val))
2397 table = XCAR (table);
2398 else
2399 table = val;
2401 table = XCDR (table);
2402 for (negative = 0; CONSP (features); features = XCDR (features))
2404 if (NILP (XCAR (features)))
2406 negative = 1;
2407 continue;
2409 if (NILP (Fmemq (XCAR (features), table)) != negative)
2410 return 0;
2412 return 1;
2415 /* Check if OTF_CAPABILITY satisfies SPEC (otf-spec). */
2417 static bool
2418 font_check_otf (Lisp_Object spec, Lisp_Object otf_capability)
2420 Lisp_Object script, langsys = Qnil, gsub = Qnil, gpos = Qnil;
2422 script = XCAR (spec);
2423 spec = XCDR (spec);
2424 if (! NILP (spec))
2426 langsys = XCAR (spec);
2427 spec = XCDR (spec);
2428 if (! NILP (spec))
2430 gsub = XCAR (spec);
2431 spec = XCDR (spec);
2432 if (! NILP (spec))
2433 gpos = XCAR (spec);
2437 if (! NILP (gsub) && ! font_check_otf_features (script, langsys, gsub,
2438 XCAR (otf_capability)))
2439 return 0;
2440 if (! NILP (gpos) && ! font_check_otf_features (script, langsys, gpos,
2441 XCDR (otf_capability)))
2442 return 0;
2443 return 1;
2448 /* Check if FONT (font-entity or font-object) matches with the font
2449 specification SPEC. */
2451 bool
2452 font_match_p (Lisp_Object spec, Lisp_Object font)
2454 Lisp_Object prop[FONT_SPEC_MAX], *props;
2455 Lisp_Object extra, font_extra;
2456 int i;
2458 for (i = FONT_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX; i++)
2459 if (! NILP (AREF (spec, i))
2460 && ! NILP (AREF (font, i))
2461 && ! EQ (AREF (spec, i), AREF (font, i)))
2462 return 0;
2463 props = XFONT_SPEC (spec)->props;
2464 if (FLOATP (props[FONT_SIZE_INDEX]))
2466 for (i = FONT_FOUNDRY_INDEX; i < FONT_SIZE_INDEX; i++)
2467 prop[i] = AREF (spec, i);
2468 prop[FONT_SIZE_INDEX]
2469 = make_number (font_pixel_size (XFRAME (selected_frame), spec));
2470 props = prop;
2473 if (font_score (font, props) > 0)
2474 return 0;
2475 extra = AREF (spec, FONT_EXTRA_INDEX);
2476 font_extra = AREF (font, FONT_EXTRA_INDEX);
2477 for (; CONSP (extra); extra = XCDR (extra))
2479 Lisp_Object key = XCAR (XCAR (extra));
2480 Lisp_Object val = XCDR (XCAR (extra)), val2;
2482 if (EQ (key, QClang))
2484 val2 = assq_no_quit (key, font_extra);
2485 if (NILP (val2))
2486 return 0;
2487 val2 = XCDR (val2);
2488 if (CONSP (val))
2490 if (! CONSP (val2))
2491 return 0;
2492 while (CONSP (val))
2493 if (NILP (Fmemq (val, val2)))
2494 return 0;
2496 else
2497 if (CONSP (val2)
2498 ? NILP (Fmemq (val, XCDR (val2)))
2499 : ! EQ (val, val2))
2500 return 0;
2502 else if (EQ (key, QCscript))
2504 val2 = assq_no_quit (val, Vscript_representative_chars);
2505 if (CONSP (val2))
2507 val2 = XCDR (val2);
2508 if (CONSP (val2))
2510 /* All characters in the list must be supported. */
2511 for (; CONSP (val2); val2 = XCDR (val2))
2513 if (! CHARACTERP (XCAR (val2)))
2514 continue;
2515 if (font_encode_char (font, XFASTINT (XCAR (val2)))
2516 == FONT_INVALID_CODE)
2517 return 0;
2520 else if (VECTORP (val2))
2522 /* At most one character in the vector must be supported. */
2523 for (i = 0; i < ASIZE (val2); i++)
2525 if (! CHARACTERP (AREF (val2, i)))
2526 continue;
2527 if (font_encode_char (font, XFASTINT (AREF (val2, i)))
2528 != FONT_INVALID_CODE)
2529 break;
2531 if (i == ASIZE (val2))
2532 return 0;
2536 else if (EQ (key, QCotf))
2538 struct font *fontp;
2540 if (! FONT_OBJECT_P (font))
2541 return 0;
2542 fontp = XFONT_OBJECT (font);
2543 if (! fontp->driver->otf_capability)
2544 return 0;
2545 val2 = fontp->driver->otf_capability (fontp);
2546 if (NILP (val2) || ! font_check_otf (val, val2))
2547 return 0;
2551 return 1;
2555 /* Font cache
2557 Each font backend has the callback function get_cache, and it
2558 returns a cons cell of which cdr part can be freely used for
2559 caching fonts. The cons cell may be shared by multiple frames
2560 and/or multiple font drivers. So, we arrange the cdr part as this:
2562 ((DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) ...)
2564 where DRIVER-TYPE is a symbol such as `x', `xft', etc., NUM-FRAMES
2565 is a number frames sharing this cache, and FONT-CACHE-DATA is a
2566 cons (FONT-SPEC . [FONT-ENTITY ...]). */
2568 static void font_prepare_cache (struct frame *, struct font_driver *);
2569 static void font_finish_cache (struct frame *, struct font_driver *);
2570 static Lisp_Object font_get_cache (struct frame *, struct font_driver *);
2571 static void font_clear_cache (struct frame *, Lisp_Object,
2572 struct font_driver *);
2574 static void
2575 font_prepare_cache (struct frame *f, struct font_driver *driver)
2577 Lisp_Object cache, val;
2579 cache = driver->get_cache (f);
2580 val = XCDR (cache);
2581 while (CONSP (val) && ! EQ (XCAR (XCAR (val)), driver->type))
2582 val = XCDR (val);
2583 if (NILP (val))
2585 val = list2 (driver->type, make_number (1));
2586 XSETCDR (cache, Fcons (val, XCDR (cache)));
2588 else
2590 val = XCDR (XCAR (val));
2591 XSETCAR (val, make_number (XINT (XCAR (val)) + 1));
2596 static void
2597 font_finish_cache (struct frame *f, struct font_driver *driver)
2599 Lisp_Object cache, val, tmp;
2602 cache = driver->get_cache (f);
2603 val = XCDR (cache);
2604 while (CONSP (val) && ! EQ (XCAR (XCAR (val)), driver->type))
2605 cache = val, val = XCDR (val);
2606 eassert (! NILP (val));
2607 tmp = XCDR (XCAR (val));
2608 XSETCAR (tmp, make_number (XINT (XCAR (tmp)) - 1));
2609 if (XINT (XCAR (tmp)) == 0)
2611 font_clear_cache (f, XCAR (val), driver);
2612 XSETCDR (cache, XCDR (val));
2617 static Lisp_Object
2618 font_get_cache (struct frame *f, struct font_driver *driver)
2620 Lisp_Object val = driver->get_cache (f);
2621 Lisp_Object type = driver->type;
2623 eassert (CONSP (val));
2624 for (val = XCDR (val); ! EQ (XCAR (XCAR (val)), type); val = XCDR (val));
2625 eassert (CONSP (val));
2626 /* VAL = ((DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) ...) */
2627 val = XCDR (XCAR (val));
2628 return val;
2632 static void
2633 font_clear_cache (struct frame *f, Lisp_Object cache, struct font_driver *driver)
2635 Lisp_Object tail, elt;
2636 Lisp_Object entity;
2637 ptrdiff_t i;
2639 /* CACHE = (DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) */
2640 for (tail = XCDR (XCDR (cache)); CONSP (tail); tail = XCDR (tail))
2642 elt = XCAR (tail);
2643 /* elt should have the form (FONT-SPEC . [FONT-ENTITY ...]) */
2644 if (CONSP (elt) && FONT_SPEC_P (XCAR (elt)))
2646 elt = XCDR (elt);
2647 eassert (VECTORP (elt));
2648 for (i = 0; i < ASIZE (elt); i++)
2650 entity = AREF (elt, i);
2652 if (FONT_ENTITY_P (entity)
2653 && EQ (driver->type, AREF (entity, FONT_TYPE_INDEX)))
2655 Lisp_Object objlist = AREF (entity, FONT_OBJLIST_INDEX);
2657 for (; CONSP (objlist); objlist = XCDR (objlist))
2659 Lisp_Object val = XCAR (objlist);
2660 struct font *font = XFONT_OBJECT (val);
2662 if (! NILP (AREF (val, FONT_TYPE_INDEX)))
2664 eassert (font && driver == font->driver);
2665 driver->close (font);
2668 if (driver->free_entity)
2669 driver->free_entity (entity);
2674 XSETCDR (cache, Qnil);
2678 static Lisp_Object scratch_font_spec, scratch_font_prefer;
2680 /* Check each font-entity in VEC, and return a list of font-entities
2681 that satisfy these conditions:
2682 (1) matches with SPEC and SIZE if SPEC is not nil, and
2683 (2) doesn't match with any regexps in Vface_ignored_fonts (if non-nil).
2686 static Lisp_Object
2687 font_delete_unmatched (Lisp_Object vec, Lisp_Object spec, int size)
2689 Lisp_Object entity, val;
2690 enum font_property_index prop;
2691 ptrdiff_t i;
2693 for (val = Qnil, i = ASIZE (vec) - 1; i >= 0; i--)
2695 entity = AREF (vec, i);
2696 if (! NILP (Vface_ignored_fonts))
2698 char name[256];
2699 ptrdiff_t namelen;
2700 Lisp_Object tail, regexp;
2702 namelen = font_unparse_xlfd (entity, 0, name, 256);
2703 if (namelen >= 0)
2705 for (tail = Vface_ignored_fonts; CONSP (tail); tail = XCDR (tail))
2707 regexp = XCAR (tail);
2708 if (STRINGP (regexp)
2709 && fast_c_string_match_ignore_case (regexp, name,
2710 namelen) >= 0)
2711 break;
2713 if (CONSP (tail))
2714 continue;
2717 if (NILP (spec))
2719 val = Fcons (entity, val);
2720 continue;
2722 for (prop = FONT_WEIGHT_INDEX; prop < FONT_SIZE_INDEX; prop++)
2723 if (INTEGERP (AREF (spec, prop))
2724 && ((XINT (AREF (spec, prop)) >> 8)
2725 != (XINT (AREF (entity, prop)) >> 8)))
2726 prop = FONT_SPEC_MAX;
2727 if (prop < FONT_SPEC_MAX
2728 && size
2729 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
2731 int diff = XINT (AREF (entity, FONT_SIZE_INDEX)) - size;
2733 if (eabs (diff) > FONT_PIXEL_SIZE_QUANTUM)
2734 prop = FONT_SPEC_MAX;
2736 if (prop < FONT_SPEC_MAX
2737 && INTEGERP (AREF (spec, FONT_DPI_INDEX))
2738 && INTEGERP (AREF (entity, FONT_DPI_INDEX))
2739 && XINT (AREF (entity, FONT_DPI_INDEX)) != 0
2740 && ! EQ (AREF (spec, FONT_DPI_INDEX), AREF (entity, FONT_DPI_INDEX)))
2741 prop = FONT_SPEC_MAX;
2742 if (prop < FONT_SPEC_MAX
2743 && INTEGERP (AREF (spec, FONT_AVGWIDTH_INDEX))
2744 && INTEGERP (AREF (entity, FONT_AVGWIDTH_INDEX))
2745 && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) != 0
2746 && ! EQ (AREF (spec, FONT_AVGWIDTH_INDEX),
2747 AREF (entity, FONT_AVGWIDTH_INDEX)))
2748 prop = FONT_SPEC_MAX;
2749 if (prop < FONT_SPEC_MAX)
2750 val = Fcons (entity, val);
2752 return (Fvconcat (1, &val));
2756 /* Return a list of vectors of font-entities matching with SPEC on
2757 FRAME. Each elements in the list is a vector of entities from the
2758 same font-driver. */
2760 Lisp_Object
2761 font_list_entities (struct frame *f, Lisp_Object spec)
2763 struct font_driver_list *driver_list = f->font_driver_list;
2764 Lisp_Object ftype, val;
2765 Lisp_Object list = Qnil;
2766 int size;
2767 bool need_filtering = 0;
2768 int i;
2770 eassert (FONT_SPEC_P (spec));
2772 if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
2773 size = XINT (AREF (spec, FONT_SIZE_INDEX));
2774 else if (FLOATP (AREF (spec, FONT_SIZE_INDEX)))
2775 size = font_pixel_size (f, spec);
2776 else
2777 size = 0;
2779 ftype = AREF (spec, FONT_TYPE_INDEX);
2780 for (i = FONT_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX; i++)
2781 ASET (scratch_font_spec, i, AREF (spec, i));
2782 for (i = FONT_WEIGHT_INDEX; i < FONT_EXTRA_INDEX; i++)
2783 if (i != FONT_SPACING_INDEX)
2785 ASET (scratch_font_spec, i, Qnil);
2786 if (! NILP (AREF (spec, i)))
2787 need_filtering = 1;
2789 ASET (scratch_font_spec, FONT_SPACING_INDEX, AREF (spec, FONT_SPACING_INDEX));
2790 ASET (scratch_font_spec, FONT_EXTRA_INDEX, AREF (spec, FONT_EXTRA_INDEX));
2792 for (; driver_list; driver_list = driver_list->next)
2793 if (driver_list->on
2794 && (NILP (ftype) || EQ (driver_list->driver->type, ftype)))
2796 Lisp_Object cache = font_get_cache (f, driver_list->driver);
2798 ASET (scratch_font_spec, FONT_TYPE_INDEX, driver_list->driver->type);
2799 val = assoc_no_quit (scratch_font_spec, XCDR (cache));
2800 if (CONSP (val))
2801 val = XCDR (val);
2802 else
2804 val = driver_list->driver->list (f, scratch_font_spec);
2805 if (!NILP (val))
2807 Lisp_Object copy = copy_font_spec (scratch_font_spec);
2809 val = Fvconcat (1, &val);
2810 ASET (copy, FONT_TYPE_INDEX, driver_list->driver->type);
2811 XSETCDR (cache, Fcons (Fcons (copy, val), XCDR (cache)));
2814 if (VECTORP (val) && ASIZE (val) > 0
2815 && (need_filtering
2816 || ! NILP (Vface_ignored_fonts)))
2817 val = font_delete_unmatched (val, need_filtering ? spec : Qnil, size);
2818 if (VECTORP (val) && ASIZE (val) > 0)
2819 list = Fcons (val, list);
2822 list = Fnreverse (list);
2823 FONT_ADD_LOG ("list", spec, list);
2824 return list;
2828 /* Return a font entity matching with SPEC on FRAME. ATTRS, if non
2829 nil, is an array of face's attributes, which specifies preferred
2830 font-related attributes. */
2832 static Lisp_Object
2833 font_matching_entity (struct frame *f, Lisp_Object *attrs, Lisp_Object spec)
2835 struct font_driver_list *driver_list = f->font_driver_list;
2836 Lisp_Object ftype, size, entity;
2837 Lisp_Object work = copy_font_spec (spec);
2839 ftype = AREF (spec, FONT_TYPE_INDEX);
2840 size = AREF (spec, FONT_SIZE_INDEX);
2842 if (FLOATP (size))
2843 ASET (work, FONT_SIZE_INDEX, make_number (font_pixel_size (f, spec)));
2844 FONT_SET_STYLE (work, FONT_WEIGHT_INDEX, attrs[LFACE_WEIGHT_INDEX]);
2845 FONT_SET_STYLE (work, FONT_SLANT_INDEX, attrs[LFACE_SLANT_INDEX]);
2846 FONT_SET_STYLE (work, FONT_WIDTH_INDEX, attrs[LFACE_SWIDTH_INDEX]);
2848 entity = Qnil;
2849 for (; driver_list; driver_list = driver_list->next)
2850 if (driver_list->on
2851 && (NILP (ftype) || EQ (driver_list->driver->type, ftype)))
2853 Lisp_Object cache = font_get_cache (f, driver_list->driver);
2855 ASET (work, FONT_TYPE_INDEX, driver_list->driver->type);
2856 entity = assoc_no_quit (work, XCDR (cache));
2857 if (CONSP (entity))
2858 entity = AREF (XCDR (entity), 0);
2859 else
2861 entity = driver_list->driver->match (f, work);
2862 if (!NILP (entity))
2864 Lisp_Object copy = copy_font_spec (work);
2865 Lisp_Object match = Fvector (1, &entity);
2867 ASET (copy, FONT_TYPE_INDEX, driver_list->driver->type);
2868 XSETCDR (cache, Fcons (Fcons (copy, match), XCDR (cache)));
2871 if (! NILP (entity))
2872 break;
2874 FONT_ADD_LOG ("match", work, entity);
2875 return entity;
2879 /* Open a font of ENTITY and PIXEL_SIZE on frame F, and return the
2880 opened font object. */
2882 static Lisp_Object
2883 font_open_entity (struct frame *f, Lisp_Object entity, int pixel_size)
2885 struct font_driver_list *driver_list;
2886 Lisp_Object objlist, size, val, font_object;
2887 struct font *font;
2888 int min_width, height, psize;
2890 eassert (FONT_ENTITY_P (entity));
2891 size = AREF (entity, FONT_SIZE_INDEX);
2892 if (XINT (size) != 0)
2893 pixel_size = XINT (size);
2895 val = AREF (entity, FONT_TYPE_INDEX);
2896 for (driver_list = f->font_driver_list;
2897 driver_list && ! EQ (driver_list->driver->type, val);
2898 driver_list = driver_list->next);
2899 if (! driver_list)
2900 return Qnil;
2902 for (objlist = AREF (entity, FONT_OBJLIST_INDEX); CONSP (objlist);
2903 objlist = XCDR (objlist))
2905 Lisp_Object fn = XCAR (objlist);
2906 if (! NILP (AREF (fn, FONT_TYPE_INDEX))
2907 && XFONT_OBJECT (fn)->pixel_size == pixel_size)
2909 if (driver_list->driver->cached_font_ok == NULL
2910 || driver_list->driver->cached_font_ok (f, fn, entity))
2911 return fn;
2915 /* We always open a font of manageable size; i.e non-zero average
2916 width and height. */
2917 for (psize = pixel_size; ; psize++)
2919 font_object = driver_list->driver->open (f, entity, psize);
2920 if (NILP (font_object))
2921 return Qnil;
2922 font = XFONT_OBJECT (font_object);
2923 if (font->average_width > 0 && font->height > 0)
2924 break;
2926 ASET (font_object, FONT_SIZE_INDEX, make_number (pixel_size));
2927 FONT_ADD_LOG ("open", entity, font_object);
2928 ASET (entity, FONT_OBJLIST_INDEX,
2929 Fcons (font_object, AREF (entity, FONT_OBJLIST_INDEX)));
2931 font = XFONT_OBJECT (font_object);
2932 min_width = (font->min_width ? font->min_width
2933 : font->average_width ? font->average_width
2934 : font->space_width ? font->space_width
2935 : 1);
2936 height = (font->height ? font->height : 1);
2937 #ifdef HAVE_WINDOW_SYSTEM
2938 FRAME_DISPLAY_INFO (f)->n_fonts++;
2939 if (FRAME_DISPLAY_INFO (f)->n_fonts == 1)
2941 FRAME_SMALLEST_CHAR_WIDTH (f) = min_width;
2942 FRAME_SMALLEST_FONT_HEIGHT (f) = height;
2943 f->fonts_changed = 1;
2945 else
2947 if (FRAME_SMALLEST_CHAR_WIDTH (f) > min_width)
2948 FRAME_SMALLEST_CHAR_WIDTH (f) = min_width, f->fonts_changed = 1;
2949 if (FRAME_SMALLEST_FONT_HEIGHT (f) > height)
2950 FRAME_SMALLEST_FONT_HEIGHT (f) = height, f->fonts_changed = 1;
2952 #endif
2954 return font_object;
2958 /* Close FONT_OBJECT that is opened on frame F. */
2960 static void
2961 font_close_object (struct frame *f, Lisp_Object font_object)
2963 struct font *font = XFONT_OBJECT (font_object);
2965 if (NILP (AREF (font_object, FONT_TYPE_INDEX)))
2966 /* Already closed. */
2967 return;
2968 FONT_ADD_LOG ("close", font_object, Qnil);
2969 font->driver->close (font);
2970 #ifdef HAVE_WINDOW_SYSTEM
2971 eassert (FRAME_DISPLAY_INFO (f)->n_fonts);
2972 FRAME_DISPLAY_INFO (f)->n_fonts--;
2973 #endif
2977 /* Return 1 if FONT on F has a glyph for character C, 0 if not, -1 if
2978 FONT is a font-entity and it must be opened to check. */
2981 font_has_char (struct frame *f, Lisp_Object font, int c)
2983 struct font *fontp;
2985 if (FONT_ENTITY_P (font))
2987 Lisp_Object type = AREF (font, FONT_TYPE_INDEX);
2988 struct font_driver_list *driver_list;
2990 for (driver_list = f->font_driver_list;
2991 driver_list && ! EQ (driver_list->driver->type, type);
2992 driver_list = driver_list->next);
2993 if (! driver_list)
2994 return 0;
2995 if (! driver_list->driver->has_char)
2996 return -1;
2997 return driver_list->driver->has_char (font, c);
3000 eassert (FONT_OBJECT_P (font));
3001 fontp = XFONT_OBJECT (font);
3002 if (fontp->driver->has_char)
3004 int result = fontp->driver->has_char (font, c);
3006 if (result >= 0)
3007 return result;
3009 return (fontp->driver->encode_char (fontp, c) != FONT_INVALID_CODE);
3013 /* Return the glyph ID of FONT_OBJECT for character C. */
3015 static unsigned
3016 font_encode_char (Lisp_Object font_object, int c)
3018 struct font *font;
3020 eassert (FONT_OBJECT_P (font_object));
3021 font = XFONT_OBJECT (font_object);
3022 return font->driver->encode_char (font, c);
3026 /* Return the name of FONT_OBJECT. */
3028 Lisp_Object
3029 font_get_name (Lisp_Object font_object)
3031 eassert (FONT_OBJECT_P (font_object));
3032 return AREF (font_object, FONT_NAME_INDEX);
3036 /* Create a new font spec from FONT_NAME, and return it. If FONT_NAME
3037 could not be parsed by font_parse_name, return Qnil. */
3039 Lisp_Object
3040 font_spec_from_name (Lisp_Object font_name)
3042 Lisp_Object spec = Ffont_spec (0, NULL);
3044 CHECK_STRING (font_name);
3045 if (font_parse_name (SSDATA (font_name), SBYTES (font_name), spec) == -1)
3046 return Qnil;
3047 font_put_extra (spec, QCname, font_name);
3048 font_put_extra (spec, QCuser_spec, font_name);
3049 return spec;
3053 void
3054 font_clear_prop (Lisp_Object *attrs, enum font_property_index prop)
3056 Lisp_Object font = attrs[LFACE_FONT_INDEX];
3058 if (! FONTP (font))
3059 return;
3061 if (! NILP (Ffont_get (font, QCname)))
3063 font = copy_font_spec (font);
3064 font_put_extra (font, QCname, Qnil);
3067 if (NILP (AREF (font, prop))
3068 && prop != FONT_FAMILY_INDEX
3069 && prop != FONT_FOUNDRY_INDEX
3070 && prop != FONT_WIDTH_INDEX
3071 && prop != FONT_SIZE_INDEX)
3072 return;
3073 if (EQ (font, attrs[LFACE_FONT_INDEX]))
3074 font = copy_font_spec (font);
3075 ASET (font, prop, Qnil);
3076 if (prop == FONT_FAMILY_INDEX || prop == FONT_FOUNDRY_INDEX)
3078 if (prop == FONT_FAMILY_INDEX)
3080 ASET (font, FONT_FOUNDRY_INDEX, Qnil);
3081 /* If we are setting the font family, we must also clear
3082 FONT_WIDTH_INDEX to avoid rejecting families that lack
3083 support for some widths. */
3084 ASET (font, FONT_WIDTH_INDEX, Qnil);
3086 ASET (font, FONT_ADSTYLE_INDEX, Qnil);
3087 ASET (font, FONT_REGISTRY_INDEX, Qnil);
3088 ASET (font, FONT_SIZE_INDEX, Qnil);
3089 ASET (font, FONT_DPI_INDEX, Qnil);
3090 ASET (font, FONT_SPACING_INDEX, Qnil);
3091 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3093 else if (prop == FONT_SIZE_INDEX)
3095 ASET (font, FONT_DPI_INDEX, Qnil);
3096 ASET (font, FONT_SPACING_INDEX, Qnil);
3097 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3099 else if (prop == FONT_WIDTH_INDEX)
3100 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3101 attrs[LFACE_FONT_INDEX] = font;
3104 /* Select a font from ENTITIES (list of font-entity vectors) that
3105 supports C and is the best match for ATTRS and PIXEL_SIZE. */
3107 static Lisp_Object
3108 font_select_entity (struct frame *f, Lisp_Object entities,
3109 Lisp_Object *attrs, int pixel_size, int c)
3111 Lisp_Object font_entity;
3112 Lisp_Object prefer;
3113 int i;
3115 if (NILP (XCDR (entities))
3116 && ASIZE (XCAR (entities)) == 1)
3118 font_entity = AREF (XCAR (entities), 0);
3119 if (c < 0 || font_has_char (f, font_entity, c) > 0)
3120 return font_entity;
3121 return Qnil;
3124 /* Sort fonts by properties specified in ATTRS. */
3125 prefer = scratch_font_prefer;
3127 for (i = FONT_WEIGHT_INDEX; i <= FONT_SIZE_INDEX; i++)
3128 ASET (prefer, i, Qnil);
3129 if (FONTP (attrs[LFACE_FONT_INDEX]))
3131 Lisp_Object face_font = attrs[LFACE_FONT_INDEX];
3133 for (i = FONT_WEIGHT_INDEX; i <= FONT_SIZE_INDEX; i++)
3134 ASET (prefer, i, AREF (face_font, i));
3136 if (NILP (AREF (prefer, FONT_WEIGHT_INDEX)))
3137 FONT_SET_STYLE (prefer, FONT_WEIGHT_INDEX, attrs[LFACE_WEIGHT_INDEX]);
3138 if (NILP (AREF (prefer, FONT_SLANT_INDEX)))
3139 FONT_SET_STYLE (prefer, FONT_SLANT_INDEX, attrs[LFACE_SLANT_INDEX]);
3140 if (NILP (AREF (prefer, FONT_WIDTH_INDEX)))
3141 FONT_SET_STYLE (prefer, FONT_WIDTH_INDEX, attrs[LFACE_SWIDTH_INDEX]);
3142 ASET (prefer, FONT_SIZE_INDEX, make_number (pixel_size));
3144 return font_sort_entities (entities, prefer, f, c);
3147 /* Return a font-entity that satisfies SPEC and is the best match for
3148 face's font related attributes in ATTRS. C, if not negative, is a
3149 character that the entity must support. */
3151 Lisp_Object
3152 font_find_for_lface (struct frame *f, Lisp_Object *attrs, Lisp_Object spec, int c)
3154 Lisp_Object work;
3155 Lisp_Object entities, val;
3156 Lisp_Object foundry[3], *family, registry[3], adstyle[3];
3157 int pixel_size;
3158 int i, j, k, l;
3159 USE_SAFE_ALLOCA;
3161 registry[0] = AREF (spec, FONT_REGISTRY_INDEX);
3162 if (NILP (registry[0]))
3164 registry[0] = DEFAULT_ENCODING;
3165 registry[1] = Qascii_0;
3166 registry[2] = zero_vector;
3168 else
3169 registry[1] = zero_vector;
3171 if (c >= 0 && ! NILP (AREF (spec, FONT_REGISTRY_INDEX)))
3173 struct charset *encoding, *repertory;
3175 if (font_registry_charsets (AREF (spec, FONT_REGISTRY_INDEX),
3176 &encoding, &repertory) < 0)
3177 return Qnil;
3178 if (repertory
3179 && ENCODE_CHAR (repertory, c) == CHARSET_INVALID_CODE (repertory))
3180 return Qnil;
3181 else if (c > encoding->max_char)
3182 return Qnil;
3185 work = copy_font_spec (spec);
3186 ASET (work, FONT_TYPE_INDEX, AREF (spec, FONT_TYPE_INDEX));
3187 pixel_size = font_pixel_size (f, spec);
3188 if (pixel_size == 0 && INTEGERP (attrs[LFACE_HEIGHT_INDEX]))
3190 double pt = XINT (attrs[LFACE_HEIGHT_INDEX]);
3192 pixel_size = POINT_TO_PIXEL (pt / 10, FRAME_RES_Y (f));
3193 if (pixel_size < 1)
3194 pixel_size = 1;
3196 ASET (work, FONT_SIZE_INDEX, Qnil);
3197 foundry[0] = AREF (work, FONT_FOUNDRY_INDEX);
3198 if (! NILP (foundry[0]))
3199 foundry[1] = zero_vector;
3200 else if (STRINGP (attrs[LFACE_FOUNDRY_INDEX]))
3202 val = attrs[LFACE_FOUNDRY_INDEX];
3203 foundry[0] = font_intern_prop (SSDATA (val), SBYTES (val), 1);
3204 foundry[1] = Qnil;
3205 foundry[2] = zero_vector;
3207 else
3208 foundry[0] = Qnil, foundry[1] = zero_vector;
3210 adstyle[0] = AREF (work, FONT_ADSTYLE_INDEX);
3211 if (! NILP (adstyle[0]))
3212 adstyle[1] = zero_vector;
3213 else if (FONTP (attrs[LFACE_FONT_INDEX]))
3215 Lisp_Object face_font = attrs[LFACE_FONT_INDEX];
3217 if (! NILP (AREF (face_font, FONT_ADSTYLE_INDEX)))
3219 adstyle[0] = AREF (face_font, FONT_ADSTYLE_INDEX);
3220 adstyle[1] = Qnil;
3221 adstyle[2] = zero_vector;
3223 else
3224 adstyle[0] = Qnil, adstyle[1] = zero_vector;
3226 else
3227 adstyle[0] = Qnil, adstyle[1] = zero_vector;
3230 val = AREF (work, FONT_FAMILY_INDEX);
3231 if (NILP (val) && STRINGP (attrs[LFACE_FAMILY_INDEX]))
3233 val = attrs[LFACE_FAMILY_INDEX];
3234 val = font_intern_prop (SSDATA (val), SBYTES (val), 1);
3236 Lisp_Object familybuf[3];
3237 if (NILP (val))
3239 family = familybuf;
3240 family[0] = Qnil;
3241 family[1] = zero_vector; /* terminator. */
3243 else
3245 Lisp_Object alters
3246 = Fassoc_string (val, Vface_alternative_font_family_alist, Qt);
3248 if (! NILP (alters))
3250 EMACS_INT alterslen = XFASTINT (Flength (alters));
3251 SAFE_ALLOCA_LISP (family, alterslen + 2);
3252 for (i = 0; CONSP (alters); i++, alters = XCDR (alters))
3253 family[i] = XCAR (alters);
3254 if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
3255 family[i++] = Qnil;
3256 family[i] = zero_vector;
3258 else
3260 family = familybuf;
3261 i = 0;
3262 family[i++] = val;
3263 if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
3264 family[i++] = Qnil;
3265 family[i] = zero_vector;
3269 for (i = 0; SYMBOLP (family[i]); i++)
3271 ASET (work, FONT_FAMILY_INDEX, family[i]);
3272 for (j = 0; SYMBOLP (foundry[j]); j++)
3274 ASET (work, FONT_FOUNDRY_INDEX, foundry[j]);
3275 for (k = 0; SYMBOLP (registry[k]); k++)
3277 ASET (work, FONT_REGISTRY_INDEX, registry[k]);
3278 for (l = 0; SYMBOLP (adstyle[l]); l++)
3280 ASET (work, FONT_ADSTYLE_INDEX, adstyle[l]);
3281 entities = font_list_entities (f, work);
3282 if (! NILP (entities))
3284 val = font_select_entity (f, entities,
3285 attrs, pixel_size, c);
3286 if (! NILP (val))
3288 SAFE_FREE ();
3289 return val;
3297 SAFE_FREE ();
3298 return Qnil;
3302 Lisp_Object
3303 font_open_for_lface (struct frame *f, Lisp_Object entity, Lisp_Object *attrs, Lisp_Object spec)
3305 int size;
3307 if (INTEGERP (AREF (entity, FONT_SIZE_INDEX))
3308 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
3309 size = XINT (AREF (entity, FONT_SIZE_INDEX));
3310 else
3312 if (FONT_SPEC_P (spec) && ! NILP (AREF (spec, FONT_SIZE_INDEX)))
3313 size = font_pixel_size (f, spec);
3314 else
3316 double pt;
3317 if (INTEGERP (attrs[LFACE_HEIGHT_INDEX]))
3318 pt = XINT (attrs[LFACE_HEIGHT_INDEX]);
3319 else
3321 struct face *def = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3322 Lisp_Object height = def->lface[LFACE_HEIGHT_INDEX];
3323 eassert (INTEGERP (height));
3324 pt = XINT (height);
3327 pt /= 10;
3328 size = POINT_TO_PIXEL (pt, FRAME_RES_Y (f));
3329 #ifdef HAVE_NS
3330 if (size == 0)
3332 Lisp_Object ffsize = get_frame_param (f, Qfontsize);
3333 size = (NUMBERP (ffsize)
3334 ? POINT_TO_PIXEL (XINT (ffsize), FRAME_RES_Y (f)) : 0);
3336 #endif
3338 size *= font_rescale_ratio (entity);
3341 return font_open_entity (f, entity, size);
3345 /* Find a font that satisfies SPEC and is the best match for
3346 face's attributes in ATTRS on FRAME, and return the opened
3347 font-object. */
3349 Lisp_Object
3350 font_load_for_lface (struct frame *f, Lisp_Object *attrs, Lisp_Object spec)
3352 Lisp_Object entity, name;
3354 entity = font_find_for_lface (f, attrs, spec, -1);
3355 if (NILP (entity))
3357 /* No font is listed for SPEC, but each font-backend may have
3358 different criteria about "font matching". So, try it. */
3359 entity = font_matching_entity (f, attrs, spec);
3360 if (NILP (entity))
3361 return Qnil;
3363 /* Don't lose the original name that was put in initially. We need
3364 it to re-apply the font when font parameters (like hinting or dpi) have
3365 changed. */
3366 entity = font_open_for_lface (f, entity, attrs, spec);
3367 if (!NILP (entity))
3369 name = Ffont_get (spec, QCuser_spec);
3370 if (STRINGP (name)) font_put_extra (entity, QCuser_spec, name);
3372 return entity;
3376 /* Make FACE on frame F ready to use the font opened for FACE. */
3378 void
3379 font_prepare_for_face (struct frame *f, struct face *face)
3381 if (face->font->driver->prepare_face)
3382 face->font->driver->prepare_face (f, face);
3386 /* Make FACE on frame F stop using the font opened for FACE. */
3388 void
3389 font_done_for_face (struct frame *f, struct face *face)
3391 if (face->font->driver->done_face)
3392 face->font->driver->done_face (f, face);
3396 /* Open a font that is a match for font-spec SPEC on frame F. If no proper
3397 font is found, return Qnil. */
3399 Lisp_Object
3400 font_open_by_spec (struct frame *f, Lisp_Object spec)
3402 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3404 /* We set up the default font-related attributes of a face to prefer
3405 a moderate font. */
3406 attrs[LFACE_FAMILY_INDEX] = attrs[LFACE_FOUNDRY_INDEX] = Qnil;
3407 attrs[LFACE_SWIDTH_INDEX] = attrs[LFACE_WEIGHT_INDEX]
3408 = attrs[LFACE_SLANT_INDEX] = Qnormal;
3409 #ifndef HAVE_NS
3410 attrs[LFACE_HEIGHT_INDEX] = make_number (120);
3411 #else
3412 attrs[LFACE_HEIGHT_INDEX] = make_number (0);
3413 #endif
3414 attrs[LFACE_FONT_INDEX] = Qnil;
3416 return font_load_for_lface (f, attrs, spec);
3420 /* Open a font that matches NAME on frame F. If no proper font is
3421 found, return Qnil. */
3423 Lisp_Object
3424 font_open_by_name (struct frame *f, Lisp_Object name)
3426 Lisp_Object args[2];
3427 Lisp_Object spec, ret;
3429 args[0] = QCname;
3430 args[1] = name;
3431 spec = Ffont_spec (2, args);
3432 ret = font_open_by_spec (f, spec);
3433 /* Do not lose name originally put in. */
3434 if (!NILP (ret))
3435 font_put_extra (ret, QCuser_spec, args[1]);
3437 return ret;
3441 /* Register font-driver DRIVER. This function is used in two ways.
3443 The first is with frame F non-NULL. In this case, make DRIVER
3444 available (but not yet activated) on F. All frame creators
3445 (e.g. Fx_create_frame) must call this function at least once with
3446 an available font-driver.
3448 The second is with frame F NULL. In this case, DRIVER is globally
3449 registered in the variable `font_driver_list'. All font-driver
3450 implementations must call this function in its syms_of_XXXX
3451 (e.g. syms_of_xfont). */
3453 void
3454 register_font_driver (struct font_driver *driver, struct frame *f)
3456 struct font_driver_list *root = f ? f->font_driver_list : font_driver_list;
3457 struct font_driver_list *prev, *list;
3459 #ifdef HAVE_WINDOW_SYSTEM
3460 if (f && ! driver->draw)
3461 error ("Unusable font driver for a frame: %s",
3462 SDATA (SYMBOL_NAME (driver->type)));
3463 #endif /* HAVE_WINDOW_SYSTEM */
3465 for (prev = NULL, list = root; list; prev = list, list = list->next)
3466 if (EQ (list->driver->type, driver->type))
3467 error ("Duplicated font driver: %s", SDATA (SYMBOL_NAME (driver->type)));
3469 list = xmalloc (sizeof *list);
3470 list->on = 0;
3471 list->driver = driver;
3472 list->next = NULL;
3473 if (prev)
3474 prev->next = list;
3475 else if (f)
3476 f->font_driver_list = list;
3477 else
3478 font_driver_list = list;
3479 if (! f)
3480 num_font_drivers++;
3483 void
3484 free_font_driver_list (struct frame *f)
3486 struct font_driver_list *list, *next;
3488 for (list = f->font_driver_list; list; list = next)
3490 next = list->next;
3491 xfree (list);
3493 f->font_driver_list = NULL;
3497 /* Make the frame F use font backends listed in NEW_DRIVERS (list of
3498 symbols, e.g. xft, x). If NEW_DRIVERS is t, make F use all
3499 available font drivers. If NEW_DRIVERS is nil, finalize all drivers.
3501 A caller must free all realized faces if any in advance. The
3502 return value is a list of font backends actually made used on
3503 F. */
3505 Lisp_Object
3506 font_update_drivers (struct frame *f, Lisp_Object new_drivers)
3508 Lisp_Object active_drivers = Qnil;
3509 struct font_driver_list *list;
3511 /* At first, turn off non-requested drivers, and turn on requested
3512 drivers. */
3513 for (list = f->font_driver_list; list; list = list->next)
3515 struct font_driver *driver = list->driver;
3516 if ((EQ (new_drivers, Qt) || ! NILP (Fmemq (driver->type, new_drivers)))
3517 != list->on)
3519 if (list->on)
3521 if (driver->end_for_frame)
3522 driver->end_for_frame (f);
3523 font_finish_cache (f, driver);
3524 list->on = 0;
3526 else
3528 if (! driver->start_for_frame
3529 || driver->start_for_frame (f) == 0)
3531 font_prepare_cache (f, driver);
3532 list->on = 1;
3538 if (NILP (new_drivers))
3539 return Qnil;
3541 if (! EQ (new_drivers, Qt))
3543 /* Re-order the driver list according to new_drivers. */
3544 struct font_driver_list **list_table, **next;
3545 Lisp_Object tail;
3546 int i;
3547 USE_SAFE_ALLOCA;
3549 SAFE_NALLOCA (list_table, 1, num_font_drivers + 1);
3550 for (i = 0, tail = new_drivers; ! NILP (tail); tail = XCDR (tail))
3552 for (list = f->font_driver_list; list; list = list->next)
3553 if (list->on && EQ (list->driver->type, XCAR (tail)))
3554 break;
3555 if (list)
3556 list_table[i++] = list;
3558 for (list = f->font_driver_list; list; list = list->next)
3559 if (! list->on)
3560 list_table[i++] = list;
3561 list_table[i] = NULL;
3563 next = &f->font_driver_list;
3564 for (i = 0; list_table[i]; i++)
3566 *next = list_table[i];
3567 next = &(*next)->next;
3569 *next = NULL;
3570 SAFE_FREE ();
3572 if (! f->font_driver_list->on)
3573 { /* None of the drivers is enabled: enable them all.
3574 Happens if you set the list of drivers to (xft x) in your .emacs
3575 and then use it under w32 or ns. */
3576 for (list = f->font_driver_list; list; list = list->next)
3578 struct font_driver *driver = list->driver;
3579 eassert (! list->on);
3580 if (! driver->start_for_frame
3581 || driver->start_for_frame (f) == 0)
3583 font_prepare_cache (f, driver);
3584 list->on = 1;
3590 for (list = f->font_driver_list; list; list = list->next)
3591 if (list->on)
3592 active_drivers = nconc2 (active_drivers, list1 (list->driver->type));
3593 return active_drivers;
3596 #if defined (HAVE_XFT) || defined (HAVE_FREETYPE)
3598 static void
3599 fset_font_data (struct frame *f, Lisp_Object val)
3601 f->font_data = val;
3604 void
3605 font_put_frame_data (struct frame *f, Lisp_Object driver, void *data)
3607 Lisp_Object val = assq_no_quit (driver, f->font_data);
3609 if (!data)
3610 fset_font_data (f, Fdelq (val, f->font_data));
3611 else
3613 if (NILP (val))
3614 fset_font_data (f, Fcons (Fcons (driver, make_save_ptr (data)),
3615 f->font_data));
3616 else
3617 XSETCDR (val, make_save_ptr (data));
3621 void *
3622 font_get_frame_data (struct frame *f, Lisp_Object driver)
3624 Lisp_Object val = assq_no_quit (driver, f->font_data);
3626 return NILP (val) ? NULL : XSAVE_POINTER (XCDR (val), 0);
3629 #endif /* HAVE_XFT || HAVE_FREETYPE */
3631 /* Sets attributes on a font. Any properties that appear in ALIST and
3632 BOOLEAN_PROPERTIES or NON_BOOLEAN_PROPERTIES are set on the font.
3633 BOOLEAN_PROPERTIES and NON_BOOLEAN_PROPERTIES are NULL-terminated
3634 arrays of strings. This function is intended for use by the font
3635 drivers to implement their specific font_filter_properties. */
3636 void
3637 font_filter_properties (Lisp_Object font,
3638 Lisp_Object alist,
3639 const char *const boolean_properties[],
3640 const char *const non_boolean_properties[])
3642 Lisp_Object it;
3643 int i;
3645 /* Set boolean values to Qt or Qnil. */
3646 for (i = 0; boolean_properties[i] != NULL; ++i)
3647 for (it = alist; ! NILP (it); it = XCDR (it))
3649 Lisp_Object key = XCAR (XCAR (it));
3650 Lisp_Object val = XCDR (XCAR (it));
3651 char *keystr = SSDATA (SYMBOL_NAME (key));
3653 if (strcmp (boolean_properties[i], keystr) == 0)
3655 const char *str = INTEGERP (val) ? (XINT (val) ? "true" : "false")
3656 : SYMBOLP (val) ? SSDATA (SYMBOL_NAME (val))
3657 : "true";
3659 if (strcmp ("false", str) == 0 || strcmp ("False", str) == 0
3660 || strcmp ("FALSE", str) == 0 || strcmp ("FcFalse", str) == 0
3661 || strcmp ("off", str) == 0 || strcmp ("OFF", str) == 0
3662 || strcmp ("Off", str) == 0)
3663 val = Qnil;
3664 else
3665 val = Qt;
3667 Ffont_put (font, key, val);
3671 for (i = 0; non_boolean_properties[i] != NULL; ++i)
3672 for (it = alist; ! NILP (it); it = XCDR (it))
3674 Lisp_Object key = XCAR (XCAR (it));
3675 Lisp_Object val = XCDR (XCAR (it));
3676 char *keystr = SSDATA (SYMBOL_NAME (key));
3677 if (strcmp (non_boolean_properties[i], keystr) == 0)
3678 Ffont_put (font, key, val);
3683 /* Return the font used to draw character C by FACE at buffer position
3684 POS in window W. If STRING is non-nil, it is a string containing C
3685 at index POS. If C is negative, get C from the current buffer or
3686 STRING. */
3688 static Lisp_Object
3689 font_at (int c, ptrdiff_t pos, struct face *face, struct window *w,
3690 Lisp_Object string)
3692 struct frame *f;
3693 bool multibyte;
3694 Lisp_Object font_object;
3696 multibyte = (NILP (string)
3697 ? ! NILP (BVAR (current_buffer, enable_multibyte_characters))
3698 : STRING_MULTIBYTE (string));
3699 if (c < 0)
3701 if (NILP (string))
3703 if (multibyte)
3705 ptrdiff_t pos_byte = CHAR_TO_BYTE (pos);
3707 c = FETCH_CHAR (pos_byte);
3709 else
3710 c = FETCH_BYTE (pos);
3712 else
3714 unsigned char *str;
3716 multibyte = STRING_MULTIBYTE (string);
3717 if (multibyte)
3719 ptrdiff_t pos_byte = string_char_to_byte (string, pos);
3721 str = SDATA (string) + pos_byte;
3722 c = STRING_CHAR (str);
3724 else
3725 c = SDATA (string)[pos];
3729 f = XFRAME (w->frame);
3730 if (! FRAME_WINDOW_P (f))
3731 return Qnil;
3732 if (! face)
3734 int face_id;
3735 ptrdiff_t endptr;
3737 if (STRINGP (string))
3738 face_id = face_at_string_position (w, string, pos, 0, &endptr,
3739 DEFAULT_FACE_ID, 0);
3740 else
3741 face_id = face_at_buffer_position (w, pos, &endptr,
3742 pos + 100, 0, -1);
3743 face = FACE_FROM_ID (f, face_id);
3745 if (multibyte)
3747 int face_id = FACE_FOR_CHAR (f, face, c, pos, string);
3748 face = FACE_FROM_ID (f, face_id);
3750 if (! face->font)
3751 return Qnil;
3753 XSETFONT (font_object, face->font);
3754 return font_object;
3758 #ifdef HAVE_WINDOW_SYSTEM
3760 /* Check how many characters after character/byte position POS/POS_BYTE
3761 (at most to *LIMIT) can be displayed by the same font in the window W.
3762 FACE, if non-NULL, is the face selected for the character at POS.
3763 If STRING is not nil, it is the string to check instead of the current
3764 buffer. In that case, FACE must be not NULL.
3766 The return value is the font-object for the character at POS.
3767 *LIMIT is set to the position where that font can't be used.
3769 It is assured that the current buffer (or STRING) is multibyte. */
3771 Lisp_Object
3772 font_range (ptrdiff_t pos, ptrdiff_t pos_byte, ptrdiff_t *limit,
3773 struct window *w, struct face *face, Lisp_Object string)
3775 ptrdiff_t ignore;
3776 int c;
3777 Lisp_Object font_object = Qnil;
3779 if (NILP (string))
3781 if (! face)
3783 int face_id;
3785 face_id = face_at_buffer_position (w, pos, &ignore,
3786 *limit, 0, -1);
3787 face = FACE_FROM_ID (XFRAME (w->frame), face_id);
3790 else
3791 eassert (face);
3793 while (pos < *limit)
3795 Lisp_Object category;
3797 if (NILP (string))
3798 FETCH_CHAR_ADVANCE_NO_CHECK (c, pos, pos_byte);
3799 else
3800 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, string, pos, pos_byte);
3801 category = CHAR_TABLE_REF (Vunicode_category_table, c);
3802 if (INTEGERP (category)
3803 && (XINT (category) == UNICODE_CATEGORY_Cf
3804 || CHAR_VARIATION_SELECTOR_P (c)))
3805 continue;
3806 if (NILP (font_object))
3808 font_object = font_for_char (face, c, pos - 1, string);
3809 if (NILP (font_object))
3810 return Qnil;
3811 continue;
3813 if (font_encode_char (font_object, c) == FONT_INVALID_CODE)
3814 *limit = pos - 1;
3816 return font_object;
3818 #endif
3821 /* Lisp API. */
3823 DEFUN ("fontp", Ffontp, Sfontp, 1, 2, 0,
3824 doc: /* Return t if OBJECT is a font-spec, font-entity, or font-object.
3825 Return nil otherwise.
3826 Optional 2nd argument EXTRA-TYPE, if non-nil, specifies to check
3827 which kind of font it is. It must be one of `font-spec', `font-entity',
3828 `font-object'. */)
3829 (Lisp_Object object, Lisp_Object extra_type)
3831 if (NILP (extra_type))
3832 return (FONTP (object) ? Qt : Qnil);
3833 if (EQ (extra_type, Qfont_spec))
3834 return (FONT_SPEC_P (object) ? Qt : Qnil);
3835 if (EQ (extra_type, Qfont_entity))
3836 return (FONT_ENTITY_P (object) ? Qt : Qnil);
3837 if (EQ (extra_type, Qfont_object))
3838 return (FONT_OBJECT_P (object) ? Qt : Qnil);
3839 wrong_type_argument (intern ("font-extra-type"), extra_type);
3842 DEFUN ("font-spec", Ffont_spec, Sfont_spec, 0, MANY, 0,
3843 doc: /* Return a newly created font-spec with arguments as properties.
3845 ARGS must come in pairs KEY VALUE of font properties. KEY must be a
3846 valid font property name listed below:
3848 `:family', `:weight', `:slant', `:width'
3850 They are the same as face attributes of the same name. See
3851 `set-face-attribute'.
3853 `:foundry'
3855 VALUE must be a string or a symbol specifying the font foundry, e.g. ``misc''.
3857 `:adstyle'
3859 VALUE must be a string or a symbol specifying the additional
3860 typographic style information of a font, e.g. ``sans''.
3862 `:registry'
3864 VALUE must be a string or a symbol specifying the charset registry and
3865 encoding of a font, e.g. ``iso8859-1''.
3867 `:size'
3869 VALUE must be a non-negative integer or a floating point number
3870 specifying the font size. It specifies the font size in pixels (if
3871 VALUE is an integer), or in points (if VALUE is a float).
3873 `:name'
3875 VALUE must be a string of XLFD-style or fontconfig-style font name.
3877 `:script'
3879 VALUE must be a symbol representing a script that the font must
3880 support. It may be a symbol representing a subgroup of a script
3881 listed in the variable `script-representative-chars'.
3883 `:lang'
3885 VALUE must be a symbol of two-letter ISO-639 language names,
3886 e.g. `ja'.
3888 `:otf'
3890 VALUE must be a list (SCRIPT-TAG LANGSYS-TAG GSUB [ GPOS ]) to specify
3891 required OpenType features.
3893 SCRIPT-TAG: OpenType script tag symbol (e.g. `deva').
3894 LANGSYS-TAG: OpenType language system tag symbol,
3895 or nil for the default language system.
3896 GSUB: List of OpenType GSUB feature tag symbols, or nil if none required.
3897 GPOS: List of OpenType GPOS feature tag symbols, or nil if none required.
3899 GSUB and GPOS may contain `nil' element. In such a case, the font
3900 must not have any of the remaining elements.
3902 For instance, if the VALUE is `(thai nil nil (mark))', the font must
3903 be an OpenType font whose GPOS table of `thai' script's default
3904 language system must contain `mark' feature.
3906 usage: (font-spec ARGS...) */)
3907 (ptrdiff_t nargs, Lisp_Object *args)
3909 Lisp_Object spec = font_make_spec ();
3910 ptrdiff_t i;
3912 for (i = 0; i < nargs; i += 2)
3914 Lisp_Object key = args[i], val;
3916 CHECK_SYMBOL (key);
3917 if (i + 1 >= nargs)
3918 error ("No value for key `%s'", SDATA (SYMBOL_NAME (key)));
3919 val = args[i + 1];
3921 if (EQ (key, QCname))
3923 CHECK_STRING (val);
3924 if (font_parse_name (SSDATA (val), SBYTES (val), spec) < 0)
3925 error ("Invalid font name: %s", SSDATA (val));
3926 font_put_extra (spec, key, val);
3928 else
3930 int idx = get_font_prop_index (key);
3932 if (idx >= 0)
3934 val = font_prop_validate (idx, Qnil, val);
3935 if (idx < FONT_EXTRA_INDEX)
3936 ASET (spec, idx, val);
3937 else
3938 font_put_extra (spec, key, val);
3940 else
3941 font_put_extra (spec, key, font_prop_validate (0, key, val));
3944 return spec;
3947 /* Return a copy of FONT as a font-spec. For the sake of speed, this code
3948 relies on an internal stuff exposed from alloc.c and should be handled
3949 with care. */
3951 Lisp_Object
3952 copy_font_spec (Lisp_Object font)
3954 enum { font_spec_size = VECSIZE (struct font_spec) };
3955 Lisp_Object new_spec, tail, *pcdr;
3956 struct font_spec *spec;
3958 CHECK_FONT (font);
3960 /* Make an uninitialized font-spec object. */
3961 spec = (struct font_spec *) allocate_vector (font_spec_size);
3962 XSETPVECTYPESIZE (spec, PVEC_FONT, FONT_SPEC_MAX,
3963 font_spec_size - FONT_SPEC_MAX);
3965 spec->props[FONT_TYPE_INDEX] = spec->props[FONT_EXTRA_INDEX] = Qnil;
3967 /* Copy basic properties FONT_FOUNDRY_INDEX..FONT_AVGWIDTH_INDEX. */
3968 memcpy (spec->props + 1, XVECTOR (font)->contents + 1,
3969 (FONT_EXTRA_INDEX - 1) * word_size);
3971 /* Copy an alist of extra information but discard :font-entity property. */
3972 pcdr = spec->props + FONT_EXTRA_INDEX;
3973 for (tail = AREF (font, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail))
3974 if (!EQ (XCAR (XCAR (tail)), QCfont_entity))
3975 *pcdr = Fcons (XCAR (tail), Qnil), pcdr = xcdr_addr (*pcdr);
3977 XSETFONT (new_spec, spec);
3978 return new_spec;
3981 /* Merge font-specs FROM and TO, and return a new font-spec.
3982 Every specified property in FROM overrides the corresponding
3983 property in TO. */
3984 Lisp_Object
3985 merge_font_spec (Lisp_Object from, Lisp_Object to)
3987 Lisp_Object extra, tail;
3988 int i;
3990 CHECK_FONT (from);
3991 CHECK_FONT (to);
3992 to = copy_font_spec (to);
3993 for (i = 0; i < FONT_EXTRA_INDEX; i++)
3994 ASET (to, i, AREF (from, i));
3995 extra = AREF (to, FONT_EXTRA_INDEX);
3996 for (tail = AREF (from, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail))
3997 if (! EQ (XCAR (XCAR (tail)), Qfont_entity))
3999 Lisp_Object slot = assq_no_quit (XCAR (XCAR (tail)), extra);
4001 if (! NILP (slot))
4002 XSETCDR (slot, XCDR (XCAR (tail)));
4003 else
4004 extra = Fcons (Fcons (XCAR (XCAR (tail)), XCDR (XCAR (tail))), extra);
4006 ASET (to, FONT_EXTRA_INDEX, extra);
4007 return to;
4010 DEFUN ("font-get", Ffont_get, Sfont_get, 2, 2, 0,
4011 doc: /* Return the value of FONT's property KEY.
4012 FONT is a font-spec, a font-entity, or a font-object.
4013 KEY is any symbol, but these are reserved for specific meanings:
4014 :family, :weight, :slant, :width, :foundry, :adstyle, :registry,
4015 :size, :name, :script, :otf
4016 See the documentation of `font-spec' for their meanings.
4017 In addition, if FONT is a font-entity or a font-object, values of
4018 :script and :otf are different from those of a font-spec as below:
4020 The value of :script may be a list of scripts that are supported by the font.
4022 The value of :otf is a cons (GSUB . GPOS) where GSUB and GPOS are lists
4023 representing the OpenType features supported by the font by this form:
4024 ((SCRIPT (LANGSYS FEATURE ...) ...) ...)
4025 SCRIPT, LANGSYS, and FEATURE are all symbols representing OpenType
4026 Layout tags. */)
4027 (Lisp_Object font, Lisp_Object key)
4029 int idx;
4030 Lisp_Object val;
4032 CHECK_FONT (font);
4033 CHECK_SYMBOL (key);
4035 idx = get_font_prop_index (key);
4036 if (idx >= FONT_WEIGHT_INDEX && idx <= FONT_WIDTH_INDEX)
4037 return font_style_symbolic (font, idx, 0);
4038 if (idx >= 0 && idx < FONT_EXTRA_INDEX)
4039 return AREF (font, idx);
4040 val = Fassq (key, AREF (font, FONT_EXTRA_INDEX));
4041 if (NILP (val) && EQ (key, QCotf) && FONT_OBJECT_P (font))
4043 struct font *fontp = XFONT_OBJECT (font);
4045 if (fontp->driver->otf_capability)
4046 val = fontp->driver->otf_capability (fontp);
4047 else
4048 val = Fcons (Qnil, Qnil);
4050 else
4051 val = Fcdr (val);
4052 return val;
4055 #ifdef HAVE_WINDOW_SYSTEM
4057 DEFUN ("font-face-attributes", Ffont_face_attributes, Sfont_face_attributes, 1, 2, 0,
4058 doc: /* Return a plist of face attributes generated by FONT.
4059 FONT is a font name, a font-spec, a font-entity, or a font-object.
4060 The return value is a list of the form
4062 \(:family FAMILY :height HEIGHT :weight WEIGHT :slant SLANT :width WIDTH)
4064 where FAMILY, HEIGHT, WEIGHT, SLANT, and WIDTH are face attribute values
4065 compatible with `set-face-attribute'. Some of these key-attribute pairs
4066 may be omitted from the list if they are not specified by FONT.
4068 The optional argument FRAME specifies the frame that the face attributes
4069 are to be displayed on. If omitted, the selected frame is used. */)
4070 (Lisp_Object font, Lisp_Object frame)
4072 struct frame *f = decode_live_frame (frame);
4073 Lisp_Object plist[10];
4074 Lisp_Object val;
4075 int n = 0;
4077 if (STRINGP (font))
4079 int fontset = fs_query_fontset (font, 0);
4080 Lisp_Object name = font;
4081 if (fontset >= 0)
4082 font = fontset_ascii (fontset);
4083 font = font_spec_from_name (name);
4084 if (! FONTP (font))
4085 signal_error ("Invalid font name", name);
4087 else if (! FONTP (font))
4088 signal_error ("Invalid font object", font);
4090 val = AREF (font, FONT_FAMILY_INDEX);
4091 if (! NILP (val))
4093 plist[n++] = QCfamily;
4094 plist[n++] = SYMBOL_NAME (val);
4097 val = AREF (font, FONT_SIZE_INDEX);
4098 if (INTEGERP (val))
4100 Lisp_Object font_dpi = AREF (font, FONT_DPI_INDEX);
4101 int dpi = INTEGERP (font_dpi) ? XINT (font_dpi) : FRAME_RES_Y (f);
4102 plist[n++] = QCheight;
4103 plist[n++] = make_number (PIXEL_TO_POINT (XINT (val) * 10, dpi));
4105 else if (FLOATP (val))
4107 plist[n++] = QCheight;
4108 plist[n++] = make_number (10 * (int) XFLOAT_DATA (val));
4111 val = FONT_WEIGHT_FOR_FACE (font);
4112 if (! NILP (val))
4114 plist[n++] = QCweight;
4115 plist[n++] = val;
4118 val = FONT_SLANT_FOR_FACE (font);
4119 if (! NILP (val))
4121 plist[n++] = QCslant;
4122 plist[n++] = val;
4125 val = FONT_WIDTH_FOR_FACE (font);
4126 if (! NILP (val))
4128 plist[n++] = QCwidth;
4129 plist[n++] = val;
4132 return Flist (n, plist);
4135 #endif
4137 DEFUN ("font-put", Ffont_put, Sfont_put, 3, 3, 0,
4138 doc: /* Set one property of FONT: give property KEY value VAL.
4139 FONT is a font-spec, a font-entity, or a font-object.
4141 If FONT is a font-spec, KEY can be any symbol. But if KEY is the one
4142 accepted by the function `font-spec' (which see), VAL must be what
4143 allowed in `font-spec'.
4145 If FONT is a font-entity or a font-object, KEY must not be the one
4146 accepted by `font-spec'. */)
4147 (Lisp_Object font, Lisp_Object prop, Lisp_Object val)
4149 int idx;
4151 idx = get_font_prop_index (prop);
4152 if (idx >= 0 && idx < FONT_EXTRA_INDEX)
4154 CHECK_FONT_SPEC (font);
4155 ASET (font, idx, font_prop_validate (idx, Qnil, val));
4157 else
4159 if (EQ (prop, QCname)
4160 || EQ (prop, QCscript)
4161 || EQ (prop, QClang)
4162 || EQ (prop, QCotf))
4163 CHECK_FONT_SPEC (font);
4164 else
4165 CHECK_FONT (font);
4166 font_put_extra (font, prop, font_prop_validate (0, prop, val));
4168 return val;
4171 DEFUN ("list-fonts", Flist_fonts, Slist_fonts, 1, 4, 0,
4172 doc: /* List available fonts matching FONT-SPEC on the current frame.
4173 Optional 2nd argument FRAME specifies the target frame.
4174 Optional 3rd argument NUM, if non-nil, limits the number of returned fonts.
4175 Optional 4th argument PREFER, if non-nil, is a font-spec to
4176 control the order of the returned list. Fonts are sorted by
4177 how close they are to PREFER. */)
4178 (Lisp_Object font_spec, Lisp_Object frame, Lisp_Object num, Lisp_Object prefer)
4180 struct frame *f = decode_live_frame (frame);
4181 Lisp_Object vec, list;
4182 EMACS_INT n = 0;
4184 CHECK_FONT_SPEC (font_spec);
4185 if (! NILP (num))
4187 CHECK_NUMBER (num);
4188 n = XINT (num);
4189 if (n <= 0)
4190 return Qnil;
4192 if (! NILP (prefer))
4193 CHECK_FONT_SPEC (prefer);
4195 list = font_list_entities (f, font_spec);
4196 if (NILP (list))
4197 return Qnil;
4198 if (NILP (XCDR (list))
4199 && ASIZE (XCAR (list)) == 1)
4200 return list1 (AREF (XCAR (list), 0));
4202 if (! NILP (prefer))
4203 vec = font_sort_entities (list, prefer, f, 0);
4204 else
4205 vec = font_vconcat_entity_vectors (list);
4206 if (n == 0 || n >= ASIZE (vec))
4208 Lisp_Object args[2];
4210 args[0] = vec;
4211 args[1] = Qnil;
4212 list = Fappend (2, args);
4214 else
4216 for (list = Qnil, n--; n >= 0; n--)
4217 list = Fcons (AREF (vec, n), list);
4219 return list;
4222 DEFUN ("font-family-list", Ffont_family_list, Sfont_family_list, 0, 1, 0,
4223 doc: /* List available font families on the current frame.
4224 If FRAME is omitted or nil, the selected frame is used. */)
4225 (Lisp_Object frame)
4227 struct frame *f = decode_live_frame (frame);
4228 struct font_driver_list *driver_list;
4229 Lisp_Object list = Qnil;
4231 for (driver_list = f->font_driver_list; driver_list;
4232 driver_list = driver_list->next)
4233 if (driver_list->driver->list_family)
4235 Lisp_Object val = driver_list->driver->list_family (f);
4236 Lisp_Object tail = list;
4238 for (; CONSP (val); val = XCDR (val))
4239 if (NILP (Fmemq (XCAR (val), tail))
4240 && SYMBOLP (XCAR (val)))
4241 list = Fcons (SYMBOL_NAME (XCAR (val)), list);
4243 return list;
4246 DEFUN ("find-font", Ffind_font, Sfind_font, 1, 2, 0,
4247 doc: /* Return a font-entity matching with FONT-SPEC on the current frame.
4248 Optional 2nd argument FRAME, if non-nil, specifies the target frame. */)
4249 (Lisp_Object font_spec, Lisp_Object frame)
4251 Lisp_Object val = Flist_fonts (font_spec, frame, make_number (1), Qnil);
4253 if (CONSP (val))
4254 val = XCAR (val);
4255 return val;
4258 DEFUN ("font-xlfd-name", Ffont_xlfd_name, Sfont_xlfd_name, 1, 2, 0,
4259 doc: /* Return XLFD name of FONT.
4260 FONT is a font-spec, font-entity, or font-object.
4261 If the name is too long for XLFD (maximum 255 chars), return nil.
4262 If the 2nd optional arg FOLD-WILDCARDS is non-nil,
4263 the consecutive wildcards are folded into one. */)
4264 (Lisp_Object font, Lisp_Object fold_wildcards)
4266 char name[256];
4267 int namelen, pixel_size = 0;
4269 CHECK_FONT (font);
4271 if (FONT_OBJECT_P (font))
4273 Lisp_Object font_name = AREF (font, FONT_NAME_INDEX);
4275 if (STRINGP (font_name)
4276 && SDATA (font_name)[0] == '-')
4278 if (NILP (fold_wildcards))
4279 return font_name;
4280 lispstpcpy (name, font_name);
4281 namelen = SBYTES (font_name);
4282 goto done;
4284 pixel_size = XFONT_OBJECT (font)->pixel_size;
4286 namelen = font_unparse_xlfd (font, pixel_size, name, 256);
4287 if (namelen < 0)
4288 return Qnil;
4289 done:
4290 if (! NILP (fold_wildcards))
4292 char *p0 = name, *p1;
4294 while ((p1 = strstr (p0, "-*-*")))
4296 strcpy (p1, p1 + 2);
4297 namelen -= 2;
4298 p0 = p1;
4302 return make_string (name, namelen);
4305 void
4306 clear_font_cache (struct frame *f)
4308 struct font_driver_list *driver_list = f->font_driver_list;
4310 for (; driver_list; driver_list = driver_list->next)
4311 if (driver_list->on)
4313 Lisp_Object val, tmp, cache = driver_list->driver->get_cache (f);
4315 val = XCDR (cache);
4316 while (! NILP (val)
4317 && ! EQ (XCAR (XCAR (val)), driver_list->driver->type))
4318 val = XCDR (val);
4319 eassert (! NILP (val));
4320 tmp = XCDR (XCAR (val));
4321 if (XINT (XCAR (tmp)) == 0)
4323 font_clear_cache (f, XCAR (val), driver_list->driver);
4324 XSETCDR (cache, XCDR (val));
4329 DEFUN ("clear-font-cache", Fclear_font_cache, Sclear_font_cache, 0, 0, 0,
4330 doc: /* Clear font cache of each frame. */)
4331 (void)
4333 Lisp_Object list, frame;
4335 FOR_EACH_FRAME (list, frame)
4336 clear_font_cache (XFRAME (frame));
4338 return Qnil;
4342 void
4343 font_fill_lglyph_metrics (Lisp_Object glyph, Lisp_Object font_object)
4345 struct font *font = XFONT_OBJECT (font_object);
4346 unsigned code = font->driver->encode_char (font, LGLYPH_CHAR (glyph));
4347 struct font_metrics metrics;
4349 LGLYPH_SET_CODE (glyph, code);
4350 font->driver->text_extents (font, &code, 1, &metrics);
4351 LGLYPH_SET_LBEARING (glyph, metrics.lbearing);
4352 LGLYPH_SET_RBEARING (glyph, metrics.rbearing);
4353 LGLYPH_SET_WIDTH (glyph, metrics.width);
4354 LGLYPH_SET_ASCENT (glyph, metrics.ascent);
4355 LGLYPH_SET_DESCENT (glyph, metrics.descent);
4359 DEFUN ("font-shape-gstring", Ffont_shape_gstring, Sfont_shape_gstring, 1, 1, 0,
4360 doc: /* Shape the glyph-string GSTRING.
4361 Shaping means substituting glyphs and/or adjusting positions of glyphs
4362 to get the correct visual image of character sequences set in the
4363 header of the glyph-string.
4365 If the shaping was successful, the value is GSTRING itself or a newly
4366 created glyph-string. Otherwise, the value is nil.
4368 See the documentation of `composition-get-gstring' for the format of
4369 GSTRING. */)
4370 (Lisp_Object gstring)
4372 struct font *font;
4373 Lisp_Object font_object, n, glyph;
4374 ptrdiff_t i, from, to;
4376 if (! composition_gstring_p (gstring))
4377 signal_error ("Invalid glyph-string: ", gstring);
4378 if (! NILP (LGSTRING_ID (gstring)))
4379 return gstring;
4380 font_object = LGSTRING_FONT (gstring);
4381 CHECK_FONT_OBJECT (font_object);
4382 font = XFONT_OBJECT (font_object);
4383 if (! font->driver->shape)
4384 return Qnil;
4386 /* Try at most three times with larger gstring each time. */
4387 for (i = 0; i < 3; i++)
4389 n = font->driver->shape (gstring);
4390 if (INTEGERP (n))
4391 break;
4392 gstring = larger_vector (gstring,
4393 LGSTRING_GLYPH_LEN (gstring), -1);
4395 if (i == 3 || XINT (n) == 0)
4396 return Qnil;
4397 if (XINT (n) < LGSTRING_GLYPH_LEN (gstring))
4398 LGSTRING_SET_GLYPH (gstring, XINT (n), Qnil);
4400 /* Check FROM_IDX and TO_IDX of each GLYPH in GSTRING to assure that
4401 GLYPHS covers all characters (except for the last few ones) in
4402 GSTRING. More formally, provided that NCHARS is the number of
4403 characters in GSTRING and GLYPHS[i] is the ith glyph, FROM_IDX
4404 and TO_IDX of each glyph must satisfy these conditions:
4406 GLYPHS[0].FROM_IDX == 0
4407 GLYPHS[i].FROM_IDX <= GLYPHS[i].TO_IDX
4408 if (GLYPHS[i].FROM_IDX == GLYPHS[i-1].FROM_IDX)
4409 ;; GLYPHS[i] and GLYPHS[i-1] belongs to the same grapheme cluster
4410 GLYPHS[i].TO_IDX == GLYPHS[i-1].TO_IDX
4411 else
4412 ;; Be sure to cover all characters.
4413 GLYPHS[i].FROM_IDX == GLYPHS[i-1].TO_IDX + 1 */
4414 glyph = LGSTRING_GLYPH (gstring, 0);
4415 from = LGLYPH_FROM (glyph);
4416 to = LGLYPH_TO (glyph);
4417 if (from != 0 || to < from)
4418 goto shaper_error;
4419 for (i = 1; i < LGSTRING_GLYPH_LEN (gstring); i++)
4421 glyph = LGSTRING_GLYPH (gstring, i);
4422 if (NILP (glyph))
4423 break;
4424 if (! (LGLYPH_FROM (glyph) <= LGLYPH_TO (glyph)
4425 && (LGLYPH_FROM (glyph) == from
4426 ? LGLYPH_TO (glyph) == to
4427 : LGLYPH_FROM (glyph) == to + 1)))
4428 goto shaper_error;
4429 from = LGLYPH_FROM (glyph);
4430 to = LGLYPH_TO (glyph);
4432 return composition_gstring_put_cache (gstring, XINT (n));
4434 shaper_error:
4435 return Qnil;
4438 DEFUN ("font-variation-glyphs", Ffont_variation_glyphs, Sfont_variation_glyphs,
4439 2, 2, 0,
4440 doc: /* Return a list of variation glyphs for CHAR in FONT-OBJECT.
4441 Each element of the value is a cons (VARIATION-SELECTOR . GLYPH-ID),
4442 where
4443 VARIATION-SELECTOR is a character code of variation selection
4444 (#xFE00..#xFE0F or #xE0100..#xE01EF)
4445 GLYPH-ID is a glyph code of the corresponding variation glyph. */)
4446 (Lisp_Object font_object, Lisp_Object character)
4448 unsigned variations[256];
4449 struct font *font;
4450 int i, n;
4451 Lisp_Object val;
4453 CHECK_FONT_OBJECT (font_object);
4454 CHECK_CHARACTER (character);
4455 font = XFONT_OBJECT (font_object);
4456 if (! font->driver->get_variation_glyphs)
4457 return Qnil;
4458 n = font->driver->get_variation_glyphs (font, XINT (character), variations);
4459 if (! n)
4460 return Qnil;
4461 val = Qnil;
4462 for (i = 0; i < 255; i++)
4463 if (variations[i])
4465 int vs = (i < 16 ? 0xFE00 + i : 0xE0100 + (i - 16));
4466 Lisp_Object code = INTEGER_TO_CONS (variations[i]);
4467 val = Fcons (Fcons (make_number (vs), code), val);
4469 return val;
4472 #if 0
4474 DEFUN ("font-drive-otf", Ffont_drive_otf, Sfont_drive_otf, 6, 6, 0,
4475 doc: /* Apply OpenType features on glyph-string GSTRING-IN.
4476 OTF-FEATURES specifies which features to apply in this format:
4477 (SCRIPT LANGSYS GSUB GPOS)
4478 where
4479 SCRIPT is a symbol specifying a script tag of OpenType,
4480 LANGSYS is a symbol specifying a langsys tag of OpenType,
4481 GSUB and GPOS, if non-nil, are lists of symbols specifying feature tags.
4483 If LANGSYS is nil, the default langsys is selected.
4485 The features are applied in the order they appear in the list. The
4486 symbol `*' means to apply all available features not present in this
4487 list, and the remaining features are ignored. For instance, (vatu
4488 pstf * haln) is to apply vatu and pstf in this order, then to apply
4489 all available features other than vatu, pstf, and haln.
4491 The features are applied to the glyphs in the range FROM and TO of
4492 the glyph-string GSTRING-IN.
4494 If some feature is actually applicable, the resulting glyphs are
4495 produced in the glyph-string GSTRING-OUT from the index INDEX. In
4496 this case, the value is the number of produced glyphs.
4498 If no feature is applicable, no glyph is produced in GSTRING-OUT, and
4499 the value is 0.
4501 If GSTRING-OUT is too short to hold produced glyphs, no glyphs are
4502 produced in GSTRING-OUT, and the value is nil.
4504 See the documentation of `composition-get-gstring' for the format of
4505 glyph-string. */)
4506 (Lisp_Object otf_features, Lisp_Object gstring_in, Lisp_Object from, Lisp_Object to, Lisp_Object gstring_out, Lisp_Object index)
4508 Lisp_Object font_object = LGSTRING_FONT (gstring_in);
4509 Lisp_Object val;
4510 struct font *font;
4511 int len, num;
4513 check_otf_features (otf_features);
4514 CHECK_FONT_OBJECT (font_object);
4515 font = XFONT_OBJECT (font_object);
4516 if (! font->driver->otf_drive)
4517 error ("Font backend %s can't drive OpenType GSUB table",
4518 SDATA (SYMBOL_NAME (font->driver->type)));
4519 CHECK_CONS (otf_features);
4520 CHECK_SYMBOL (XCAR (otf_features));
4521 val = XCDR (otf_features);
4522 CHECK_SYMBOL (XCAR (val));
4523 val = XCDR (otf_features);
4524 if (! NILP (val))
4525 CHECK_CONS (val);
4526 len = check_gstring (gstring_in);
4527 CHECK_VECTOR (gstring_out);
4528 CHECK_NATNUM (from);
4529 CHECK_NATNUM (to);
4530 CHECK_NATNUM (index);
4532 if (XINT (from) >= XINT (to) || XINT (to) > len)
4533 args_out_of_range_3 (from, to, make_number (len));
4534 if (XINT (index) >= ASIZE (gstring_out))
4535 args_out_of_range (index, make_number (ASIZE (gstring_out)));
4536 num = font->driver->otf_drive (font, otf_features,
4537 gstring_in, XINT (from), XINT (to),
4538 gstring_out, XINT (index), 0);
4539 if (num < 0)
4540 return Qnil;
4541 return make_number (num);
4544 DEFUN ("font-otf-alternates", Ffont_otf_alternates, Sfont_otf_alternates,
4545 3, 3, 0,
4546 doc: /* Return a list of alternate glyphs of CHARACTER in FONT-OBJECT.
4547 OTF-FEATURES specifies which features of the font FONT-OBJECT to apply
4548 in this format:
4549 (SCRIPT LANGSYS FEATURE ...)
4550 See the documentation of `font-drive-otf' for more detail.
4552 The value is a list of cons cells of the format (GLYPH-ID . CHARACTER),
4553 where GLYPH-ID is a glyph index of the font, and CHARACTER is a
4554 character code corresponding to the glyph or nil if there's no
4555 corresponding character. */)
4556 (Lisp_Object font_object, Lisp_Object character, Lisp_Object otf_features)
4558 struct font *font;
4559 Lisp_Object gstring_in, gstring_out, g;
4560 Lisp_Object alternates;
4561 int i, num;
4563 CHECK_FONT_GET_OBJECT (font_object, font);
4564 if (! font->driver->otf_drive)
4565 error ("Font backend %s can't drive OpenType GSUB table",
4566 SDATA (SYMBOL_NAME (font->driver->type)));
4567 CHECK_CHARACTER (character);
4568 CHECK_CONS (otf_features);
4570 gstring_in = Ffont_make_gstring (font_object, make_number (1));
4571 g = LGSTRING_GLYPH (gstring_in, 0);
4572 LGLYPH_SET_CHAR (g, XINT (character));
4573 gstring_out = Ffont_make_gstring (font_object, make_number (10));
4574 while ((num = font->driver->otf_drive (font, otf_features, gstring_in, 0, 1,
4575 gstring_out, 0, 1)) < 0)
4576 gstring_out = Ffont_make_gstring (font_object,
4577 make_number (ASIZE (gstring_out) * 2));
4578 alternates = Qnil;
4579 for (i = 0; i < num; i++)
4581 Lisp_Object g = LGSTRING_GLYPH (gstring_out, i);
4582 int c = LGLYPH_CHAR (g);
4583 unsigned code = LGLYPH_CODE (g);
4585 alternates = Fcons (Fcons (make_number (code),
4586 c > 0 ? make_number (c) : Qnil),
4587 alternates);
4589 return Fnreverse (alternates);
4591 #endif /* 0 */
4593 #ifdef FONT_DEBUG
4595 DEFUN ("open-font", Fopen_font, Sopen_font, 1, 3, 0,
4596 doc: /* Open FONT-ENTITY. */)
4597 (Lisp_Object font_entity, Lisp_Object size, Lisp_Object frame)
4599 EMACS_INT isize;
4600 struct frame *f = decode_live_frame (frame);
4602 CHECK_FONT_ENTITY (font_entity);
4604 if (NILP (size))
4605 isize = XINT (AREF (font_entity, FONT_SIZE_INDEX));
4606 else
4608 CHECK_NUMBER_OR_FLOAT (size);
4609 if (FLOATP (size))
4610 isize = POINT_TO_PIXEL (XFLOAT_DATA (size), FRAME_RES_Y (f));
4611 else
4612 isize = XINT (size);
4613 if (! (INT_MIN <= isize && isize <= INT_MAX))
4614 args_out_of_range (font_entity, size);
4615 if (isize == 0)
4616 isize = 120;
4618 return font_open_entity (f, font_entity, isize);
4621 DEFUN ("close-font", Fclose_font, Sclose_font, 1, 2, 0,
4622 doc: /* Close FONT-OBJECT. */)
4623 (Lisp_Object font_object, Lisp_Object frame)
4625 CHECK_FONT_OBJECT (font_object);
4626 font_close_object (decode_live_frame (frame), font_object);
4627 return Qnil;
4630 DEFUN ("query-font", Fquery_font, Squery_font, 1, 1, 0,
4631 doc: /* Return information about FONT-OBJECT.
4632 The value is a vector:
4633 [ NAME FILENAME PIXEL-SIZE SIZE ASCENT DESCENT SPACE-WIDTH AVERAGE-WIDTH
4634 CAPABILITY ]
4636 NAME is the font name, a string (or nil if the font backend doesn't
4637 provide a name).
4639 FILENAME is the font file name, a string (or nil if the font backend
4640 doesn't provide a file name).
4642 PIXEL-SIZE is a pixel size by which the font is opened.
4644 SIZE is a maximum advance width of the font in pixels.
4646 ASCENT, DESCENT, SPACE-WIDTH, AVERAGE-WIDTH are metrics of the font in
4647 pixels.
4649 CAPABILITY is a list whose first element is a symbol representing the
4650 font format \(x, opentype, truetype, type1, pcf, or bdf) and the
4651 remaining elements describe the details of the font capability.
4653 If the font is OpenType font, the form of the list is
4654 \(opentype GSUB GPOS)
4655 where GSUB shows which "GSUB" features the font supports, and GPOS
4656 shows which "GPOS" features the font supports. Both GSUB and GPOS are
4657 lists of the format:
4658 \((SCRIPT (LANGSYS FEATURE ...) ...) ...)
4660 If the font is not OpenType font, currently the length of the form is
4661 one.
4663 SCRIPT is a symbol representing OpenType script tag.
4665 LANGSYS is a symbol representing OpenType langsys tag, or nil
4666 representing the default langsys.
4668 FEATURE is a symbol representing OpenType feature tag.
4670 If the font is not OpenType font, CAPABILITY is nil. */)
4671 (Lisp_Object font_object)
4673 struct font *font;
4674 Lisp_Object val;
4676 CHECK_FONT_GET_OBJECT (font_object, font);
4678 val = make_uninit_vector (9);
4679 ASET (val, 0, AREF (font_object, FONT_NAME_INDEX));
4680 ASET (val, 1, AREF (font_object, FONT_FILE_INDEX));
4681 ASET (val, 2, make_number (font->pixel_size));
4682 ASET (val, 3, make_number (font->max_width));
4683 ASET (val, 4, make_number (font->ascent));
4684 ASET (val, 5, make_number (font->descent));
4685 ASET (val, 6, make_number (font->space_width));
4686 ASET (val, 7, make_number (font->average_width));
4687 if (font->driver->otf_capability)
4688 ASET (val, 8, Fcons (Qopentype, font->driver->otf_capability (font)));
4689 else
4690 ASET (val, 8, Qnil);
4691 return val;
4694 DEFUN ("font-get-glyphs", Ffont_get_glyphs, Sfont_get_glyphs, 3, 4, 0,
4695 doc:
4696 /* Return a vector of FONT-OBJECT's glyphs for the specified characters.
4697 FROM and TO are positions (integers or markers) specifying a region
4698 of the current buffer, and can be in either order. If the optional
4699 fourth arg OBJECT is not nil, it is a string or a vector containing
4700 the target characters between indices FROM and TO, which are treated
4701 as in `substring'.
4703 Each element is a vector containing information of a glyph in this format:
4704 [FROM-IDX TO-IDX C CODE WIDTH LBEARING RBEARING ASCENT DESCENT ADJUSTMENT]
4705 where
4706 FROM is an index numbers of a character the glyph corresponds to.
4707 TO is the same as FROM.
4708 C is the character of the glyph.
4709 CODE is the glyph-code of C in FONT-OBJECT.
4710 WIDTH thru DESCENT are the metrics (in pixels) of the glyph.
4711 ADJUSTMENT is always nil.
4712 If FONT-OBJECT doesn't have a glyph for a character,
4713 the corresponding element is nil. */)
4714 (Lisp_Object font_object, Lisp_Object from, Lisp_Object to,
4715 Lisp_Object object)
4717 struct font *font;
4718 ptrdiff_t i, len;
4719 Lisp_Object *chars, vec;
4720 USE_SAFE_ALLOCA;
4722 CHECK_FONT_GET_OBJECT (font_object, font);
4723 if (NILP (object))
4725 ptrdiff_t charpos, bytepos;
4727 validate_region (&from, &to);
4728 if (EQ (from, to))
4729 return Qnil;
4730 len = XFASTINT (to) - XFASTINT (from);
4731 SAFE_ALLOCA_LISP (chars, len);
4732 charpos = XFASTINT (from);
4733 bytepos = CHAR_TO_BYTE (charpos);
4734 for (i = 0; charpos < XFASTINT (to); i++)
4736 int c;
4737 FETCH_CHAR_ADVANCE (c, charpos, bytepos);
4738 chars[i] = make_number (c);
4741 else if (STRINGP (object))
4743 const unsigned char *p;
4744 ptrdiff_t ifrom, ito;
4746 validate_subarray (object, from, to, SCHARS (object), &ifrom, &ito);
4747 if (ifrom == ito)
4748 return Qnil;
4749 len = ito - ifrom;
4750 SAFE_ALLOCA_LISP (chars, len);
4751 p = SDATA (object);
4752 if (STRING_MULTIBYTE (object))
4754 int c;
4756 /* Skip IFROM characters from the beginning. */
4757 for (i = 0; i < ifrom; i++)
4758 c = STRING_CHAR_ADVANCE (p);
4760 /* Now fetch an interesting characters. */
4761 for (i = 0; i < len; i++)
4763 c = STRING_CHAR_ADVANCE (p);
4764 chars[i] = make_number (c);
4767 else
4768 for (i = 0; i < len; i++)
4769 chars[i] = make_number (p[ifrom + i]);
4771 else if (VECTORP (object))
4773 ptrdiff_t ifrom, ito;
4775 validate_subarray (object, from, to, ASIZE (object), &ifrom, &ito);
4776 if (ifrom == ito)
4777 return Qnil;
4778 len = ito - ifrom;
4779 for (i = 0; i < len; i++)
4781 Lisp_Object elt = AREF (object, ifrom + i);
4782 CHECK_CHARACTER (elt);
4784 chars = aref_addr (object, ifrom);
4786 else
4787 wrong_type_argument (Qarrayp, object);
4789 vec = make_uninit_vector (len);
4790 for (i = 0; i < len; i++)
4792 Lisp_Object g;
4793 int c = XFASTINT (chars[i]);
4794 unsigned code;
4795 struct font_metrics metrics;
4797 code = font->driver->encode_char (font, c);
4798 if (code == FONT_INVALID_CODE)
4800 ASET (vec, i, Qnil);
4801 continue;
4803 g = LGLYPH_NEW ();
4804 LGLYPH_SET_FROM (g, i);
4805 LGLYPH_SET_TO (g, i);
4806 LGLYPH_SET_CHAR (g, c);
4807 LGLYPH_SET_CODE (g, code);
4808 font->driver->text_extents (font, &code, 1, &metrics);
4809 LGLYPH_SET_WIDTH (g, metrics.width);
4810 LGLYPH_SET_LBEARING (g, metrics.lbearing);
4811 LGLYPH_SET_RBEARING (g, metrics.rbearing);
4812 LGLYPH_SET_ASCENT (g, metrics.ascent);
4813 LGLYPH_SET_DESCENT (g, metrics.descent);
4814 ASET (vec, i, g);
4816 if (! VECTORP (object))
4817 SAFE_FREE ();
4818 return vec;
4821 DEFUN ("font-match-p", Ffont_match_p, Sfont_match_p, 2, 2, 0,
4822 doc: /* Return t if and only if font-spec SPEC matches with FONT.
4823 FONT is a font-spec, font-entity, or font-object. */)
4824 (Lisp_Object spec, Lisp_Object font)
4826 CHECK_FONT_SPEC (spec);
4827 CHECK_FONT (font);
4829 return (font_match_p (spec, font) ? Qt : Qnil);
4832 DEFUN ("font-at", Ffont_at, Sfont_at, 1, 3, 0,
4833 doc: /* Return a font-object for displaying a character at POSITION.
4834 Optional second arg WINDOW, if non-nil, is a window displaying
4835 the current buffer. It defaults to the currently selected window.
4836 Optional third arg STRING, if non-nil, is a string containing the target
4837 character at index specified by POSITION. */)
4838 (Lisp_Object position, Lisp_Object window, Lisp_Object string)
4840 struct window *w = decode_live_window (window);
4842 if (NILP (string))
4844 if (XBUFFER (w->contents) != current_buffer)
4845 error ("Specified window is not displaying the current buffer");
4846 CHECK_NUMBER_COERCE_MARKER (position);
4847 if (! (BEGV <= XINT (position) && XINT (position) < ZV))
4848 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
4850 else
4852 CHECK_NUMBER (position);
4853 CHECK_STRING (string);
4854 if (! (0 <= XINT (position) && XINT (position) < SCHARS (string)))
4855 args_out_of_range (string, position);
4858 return font_at (-1, XINT (position), NULL, w, string);
4861 #if 0
4862 DEFUN ("draw-string", Fdraw_string, Sdraw_string, 2, 2, 0,
4863 doc: /* Draw STRING by FONT-OBJECT on the top left corner of the current frame.
4864 The value is a number of glyphs drawn.
4865 Type C-l to recover what previously shown. */)
4866 (Lisp_Object font_object, Lisp_Object string)
4868 Lisp_Object frame = selected_frame;
4869 struct frame *f = XFRAME (frame);
4870 struct font *font;
4871 struct face *face;
4872 int i, len, width;
4873 unsigned *code;
4875 CHECK_FONT_GET_OBJECT (font_object, font);
4876 CHECK_STRING (string);
4877 len = SCHARS (string);
4878 code = alloca (sizeof (unsigned) * len);
4879 for (i = 0; i < len; i++)
4881 Lisp_Object ch = Faref (string, make_number (i));
4882 Lisp_Object val;
4883 int c = XINT (ch);
4885 code[i] = font->driver->encode_char (font, c);
4886 if (code[i] == FONT_INVALID_CODE)
4887 break;
4889 face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4890 face->fontp = font;
4891 if (font->driver->prepare_face)
4892 font->driver->prepare_face (f, face);
4893 width = font->driver->text_extents (font, code, i, NULL);
4894 len = font->driver->draw_text (f, face, 0, font->ascent, code, i, width);
4895 if (font->driver->done_face)
4896 font->driver->done_face (f, face);
4897 face->fontp = NULL;
4898 return make_number (len);
4900 #endif
4902 DEFUN ("frame-font-cache", Fframe_font_cache, Sframe_font_cache, 0, 1, 0,
4903 doc: /* Return FRAME's font cache. Mainly used for debugging.
4904 If FRAME is omitted or nil, use the selected frame. */)
4905 (Lisp_Object frame)
4907 #ifdef HAVE_WINDOW_SYSTEM
4908 struct frame *f = decode_live_frame (frame);
4910 if (FRAME_WINDOW_P (f))
4911 return FRAME_DISPLAY_INFO (f)->name_list_element;
4912 else
4913 #endif
4914 return Qnil;
4917 #endif /* FONT_DEBUG */
4919 #ifdef HAVE_WINDOW_SYSTEM
4921 DEFUN ("font-info", Ffont_info, Sfont_info, 1, 2, 0,
4922 doc: /* Return information about a font named NAME on frame FRAME.
4923 If FRAME is omitted or nil, use the selected frame.
4925 The returned value is a vector:
4926 [ OPENED-NAME FULL-NAME SIZE HEIGHT BASELINE-OFFSET RELATIVE-COMPOSE
4927 DEFAULT-ASCENT MAX-WIDTH ASCENT DESCENT SPACE-WIDTH AVERAGE-WIDTH
4928 CAPABILITY ]
4929 where
4930 OPENED-NAME is the name used for opening the font,
4931 FULL-NAME is the full name of the font,
4932 SIZE is the pixelsize of the font,
4933 HEIGHT is the pixel-height of the font (i.e., ascent + descent),
4934 BASELINE-OFFSET is the upward offset pixels from ASCII baseline,
4935 RELATIVE-COMPOSE and DEFAULT-ASCENT are the numbers controlling
4936 how to compose characters,
4937 MAX-WIDTH is the maximum advance width of the font,
4938 ASCENT, DESCENT, SPACE-WIDTH, AVERAGE-WIDTH are metrics of the font
4939 in pixels,
4940 FILENAME is the font file name, a string (or nil if the font backend
4941 doesn't provide a file name).
4942 CAPABILITY is a list whose first element is a symbol representing the
4943 font format, one of x, opentype, truetype, type1, pcf, or bdf.
4944 The remaining elements describe the details of the font capabilities,
4945 as follows:
4947 If the font is OpenType font, the form of the list is
4948 \(opentype GSUB GPOS)
4949 where GSUB shows which "GSUB" features the font supports, and GPOS
4950 shows which "GPOS" features the font supports. Both GSUB and GPOS are
4951 lists of the form:
4952 \((SCRIPT (LANGSYS FEATURE ...) ...) ...)
4954 where
4955 SCRIPT is a symbol representing OpenType script tag.
4956 LANGSYS is a symbol representing OpenType langsys tag, or nil
4957 representing the default langsys.
4958 FEATURE is a symbol representing OpenType feature tag.
4960 If the font is not an OpenType font, there are no elements
4961 in CAPABILITY except the font format symbol.
4963 If the named font is not yet loaded, return nil. */)
4964 (Lisp_Object name, Lisp_Object frame)
4966 struct frame *f;
4967 struct font *font;
4968 Lisp_Object info;
4969 Lisp_Object font_object;
4971 if (! FONTP (name))
4972 CHECK_STRING (name);
4973 f = decode_window_system_frame (frame);
4975 if (STRINGP (name))
4977 int fontset = fs_query_fontset (name, 0);
4979 if (fontset >= 0)
4980 name = fontset_ascii (fontset);
4981 font_object = font_open_by_name (f, name);
4983 else if (FONT_OBJECT_P (name))
4984 font_object = name;
4985 else if (FONT_ENTITY_P (name))
4986 font_object = font_open_entity (f, name, 0);
4987 else
4989 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4990 Lisp_Object entity = font_matching_entity (f, face->lface, name);
4992 font_object = ! NILP (entity) ? font_open_entity (f, entity, 0) : Qnil;
4994 if (NILP (font_object))
4995 return Qnil;
4996 font = XFONT_OBJECT (font_object);
4998 info = make_uninit_vector (14);
4999 ASET (info, 0, AREF (font_object, FONT_NAME_INDEX));
5000 ASET (info, 1, AREF (font_object, FONT_FULLNAME_INDEX));
5001 ASET (info, 2, make_number (font->pixel_size));
5002 ASET (info, 3, make_number (font->height));
5003 ASET (info, 4, make_number (font->baseline_offset));
5004 ASET (info, 5, make_number (font->relative_compose));
5005 ASET (info, 6, make_number (font->default_ascent));
5006 ASET (info, 7, make_number (font->max_width));
5007 ASET (info, 8, make_number (font->ascent));
5008 ASET (info, 9, make_number (font->descent));
5009 ASET (info, 10, make_number (font->space_width));
5010 ASET (info, 11, make_number (font->average_width));
5011 ASET (info, 12, AREF (font_object, FONT_FILE_INDEX));
5012 if (font->driver->otf_capability)
5013 ASET (info, 13, Fcons (Qopentype, font->driver->otf_capability (font)));
5014 else
5015 ASET (info, 13, Qnil);
5017 #if 0
5018 /* As font_object is still in FONT_OBJLIST of the entity, we can't
5019 close it now. Perhaps, we should manage font-objects
5020 by `reference-count'. */
5021 font_close_object (f, font_object);
5022 #endif
5023 return info;
5025 #endif
5028 #define BUILD_STYLE_TABLE(TBL) build_style_table (TBL, ARRAYELTS (TBL))
5030 static Lisp_Object
5031 build_style_table (const struct table_entry *entry, int nelement)
5033 int i, j;
5034 Lisp_Object table, elt;
5036 table = make_uninit_vector (nelement);
5037 for (i = 0; i < nelement; i++)
5039 for (j = 0; entry[i].names[j]; j++);
5040 elt = Fmake_vector (make_number (j + 1), Qnil);
5041 ASET (elt, 0, make_number (entry[i].numeric));
5042 for (j = 0; entry[i].names[j]; j++)
5043 ASET (elt, j + 1, intern_c_string (entry[i].names[j]));
5044 ASET (table, i, elt);
5046 return table;
5049 /* The deferred font-log data of the form [ACTION ARG RESULT].
5050 If ACTION is not nil, that is added to the log when font_add_log is
5051 called next time. At that time, ACTION is set back to nil. */
5052 static Lisp_Object Vfont_log_deferred;
5054 /* Prepend the font-related logging data in Vfont_log if it is not
5055 `t'. ACTION describes a kind of font-related action (e.g. listing,
5056 opening), ARG is the argument for the action, and RESULT is the
5057 result of the action. */
5058 void
5059 font_add_log (const char *action, Lisp_Object arg, Lisp_Object result)
5061 Lisp_Object val;
5062 int i;
5064 if (EQ (Vfont_log, Qt))
5065 return;
5066 if (STRINGP (AREF (Vfont_log_deferred, 0)))
5068 char *str = SSDATA (AREF (Vfont_log_deferred, 0));
5070 ASET (Vfont_log_deferred, 0, Qnil);
5071 font_add_log (str, AREF (Vfont_log_deferred, 1),
5072 AREF (Vfont_log_deferred, 2));
5075 if (FONTP (arg))
5077 Lisp_Object tail, elt;
5078 AUTO_STRING (equal, "=");
5080 val = Ffont_xlfd_name (arg, Qt);
5081 for (tail = AREF (arg, FONT_EXTRA_INDEX); CONSP (tail);
5082 tail = XCDR (tail))
5084 elt = XCAR (tail);
5085 if (EQ (XCAR (elt), QCscript)
5086 && SYMBOLP (XCDR (elt)))
5087 val = concat3 (val, SYMBOL_NAME (QCscript),
5088 concat2 (equal, SYMBOL_NAME (XCDR (elt))));
5089 else if (EQ (XCAR (elt), QClang)
5090 && SYMBOLP (XCDR (elt)))
5091 val = concat3 (val, SYMBOL_NAME (QClang),
5092 concat2 (equal, SYMBOL_NAME (XCDR (elt))));
5093 else if (EQ (XCAR (elt), QCotf)
5094 && CONSP (XCDR (elt)) && SYMBOLP (XCAR (XCDR (elt))))
5095 val = concat3 (val, SYMBOL_NAME (QCotf),
5096 concat2 (equal, SYMBOL_NAME (XCAR (XCDR (elt)))));
5098 arg = val;
5101 if (CONSP (result)
5102 && VECTORP (XCAR (result))
5103 && ASIZE (XCAR (result)) > 0
5104 && FONTP (AREF (XCAR (result), 0)))
5105 result = font_vconcat_entity_vectors (result);
5106 if (FONTP (result))
5108 val = Ffont_xlfd_name (result, Qt);
5109 if (! FONT_SPEC_P (result))
5111 AUTO_STRING (colon, ":");
5112 val = concat3 (SYMBOL_NAME (AREF (result, FONT_TYPE_INDEX)),
5113 colon, val);
5115 result = val;
5117 else if (CONSP (result))
5119 Lisp_Object tail;
5120 result = Fcopy_sequence (result);
5121 for (tail = result; CONSP (tail); tail = XCDR (tail))
5123 val = XCAR (tail);
5124 if (FONTP (val))
5125 val = Ffont_xlfd_name (val, Qt);
5126 XSETCAR (tail, val);
5129 else if (VECTORP (result))
5131 result = Fcopy_sequence (result);
5132 for (i = 0; i < ASIZE (result); i++)
5134 val = AREF (result, i);
5135 if (FONTP (val))
5136 val = Ffont_xlfd_name (val, Qt);
5137 ASET (result, i, val);
5140 Vfont_log = Fcons (list3 (intern (action), arg, result), Vfont_log);
5143 /* Record a font-related logging data to be added to Vfont_log when
5144 font_add_log is called next time. ACTION, ARG, RESULT are the same
5145 as font_add_log. */
5147 void
5148 font_deferred_log (const char *action, Lisp_Object arg, Lisp_Object result)
5150 if (EQ (Vfont_log, Qt))
5151 return;
5152 ASET (Vfont_log_deferred, 0, build_string (action));
5153 ASET (Vfont_log_deferred, 1, arg);
5154 ASET (Vfont_log_deferred, 2, result);
5157 void
5158 syms_of_font (void)
5160 sort_shift_bits[FONT_TYPE_INDEX] = 0;
5161 sort_shift_bits[FONT_SLANT_INDEX] = 2;
5162 sort_shift_bits[FONT_WEIGHT_INDEX] = 9;
5163 sort_shift_bits[FONT_SIZE_INDEX] = 16;
5164 sort_shift_bits[FONT_WIDTH_INDEX] = 23;
5165 /* Note that the other elements in sort_shift_bits are not used. */
5167 staticpro (&font_charset_alist);
5168 font_charset_alist = Qnil;
5170 DEFSYM (Qopentype, "opentype");
5172 DEFSYM (Qascii_0, "ascii-0");
5173 DEFSYM (Qiso8859_1, "iso8859-1");
5174 DEFSYM (Qiso10646_1, "iso10646-1");
5175 DEFSYM (Qunicode_bmp, "unicode-bmp");
5176 DEFSYM (Qunicode_sip, "unicode-sip");
5178 DEFSYM (QCf, "Cf");
5180 DEFSYM (QCotf, ":otf");
5181 DEFSYM (QClang, ":lang");
5182 DEFSYM (QCscript, ":script");
5183 DEFSYM (QCantialias, ":antialias");
5185 DEFSYM (QCfoundry, ":foundry");
5186 DEFSYM (QCadstyle, ":adstyle");
5187 DEFSYM (QCregistry, ":registry");
5188 DEFSYM (QCspacing, ":spacing");
5189 DEFSYM (QCdpi, ":dpi");
5190 DEFSYM (QCscalable, ":scalable");
5191 DEFSYM (QCavgwidth, ":avgwidth");
5192 DEFSYM (QCfont_entity, ":font-entity");
5193 DEFSYM (QCfc_unknown_spec, ":fc-unknown-spec");
5195 DEFSYM (Qc, "c");
5196 DEFSYM (Qm, "m");
5197 DEFSYM (Qp, "p");
5198 DEFSYM (Qd, "d");
5200 DEFSYM (Qja, "ja");
5201 DEFSYM (Qko, "ko");
5203 DEFSYM (QCuser_spec, "user-spec");
5205 staticpro (&scratch_font_spec);
5206 scratch_font_spec = Ffont_spec (0, NULL);
5207 staticpro (&scratch_font_prefer);
5208 scratch_font_prefer = Ffont_spec (0, NULL);
5210 staticpro (&Vfont_log_deferred);
5211 Vfont_log_deferred = Fmake_vector (make_number (3), Qnil);
5213 #if 0
5214 #ifdef HAVE_LIBOTF
5215 staticpro (&otf_list);
5216 otf_list = Qnil;
5217 #endif /* HAVE_LIBOTF */
5218 #endif /* 0 */
5220 defsubr (&Sfontp);
5221 defsubr (&Sfont_spec);
5222 defsubr (&Sfont_get);
5223 #ifdef HAVE_WINDOW_SYSTEM
5224 defsubr (&Sfont_face_attributes);
5225 #endif
5226 defsubr (&Sfont_put);
5227 defsubr (&Slist_fonts);
5228 defsubr (&Sfont_family_list);
5229 defsubr (&Sfind_font);
5230 defsubr (&Sfont_xlfd_name);
5231 defsubr (&Sclear_font_cache);
5232 defsubr (&Sfont_shape_gstring);
5233 defsubr (&Sfont_variation_glyphs);
5234 #if 0
5235 defsubr (&Sfont_drive_otf);
5236 defsubr (&Sfont_otf_alternates);
5237 #endif /* 0 */
5239 #ifdef FONT_DEBUG
5240 defsubr (&Sopen_font);
5241 defsubr (&Sclose_font);
5242 defsubr (&Squery_font);
5243 defsubr (&Sfont_get_glyphs);
5244 defsubr (&Sfont_match_p);
5245 defsubr (&Sfont_at);
5246 #if 0
5247 defsubr (&Sdraw_string);
5248 #endif
5249 defsubr (&Sframe_font_cache);
5250 #endif /* FONT_DEBUG */
5251 #ifdef HAVE_WINDOW_SYSTEM
5252 defsubr (&Sfont_info);
5253 #endif
5255 DEFVAR_LISP ("font-encoding-alist", Vfont_encoding_alist,
5256 doc: /*
5257 Alist of fontname patterns vs the corresponding encoding and repertory info.
5258 Each element looks like (REGEXP . (ENCODING . REPERTORY)),
5259 where ENCODING is a charset or a char-table,
5260 and REPERTORY is a charset, a char-table, or nil.
5262 If ENCODING and REPERTORY are the same, the element can have the form
5263 \(REGEXP . ENCODING).
5265 ENCODING is for converting a character to a glyph code of the font.
5266 If ENCODING is a charset, encoding a character by the charset gives
5267 the corresponding glyph code. If ENCODING is a char-table, looking up
5268 the table by a character gives the corresponding glyph code.
5270 REPERTORY specifies a repertory of characters supported by the font.
5271 If REPERTORY is a charset, all characters belonging to the charset are
5272 supported. If REPERTORY is a char-table, all characters who have a
5273 non-nil value in the table are supported. If REPERTORY is nil, Emacs
5274 gets the repertory information by an opened font and ENCODING. */);
5275 Vfont_encoding_alist = Qnil;
5277 /* FIXME: These 3 vars are not quite what they appear: setq on them
5278 won't have any effect other than disconnect them from the style
5279 table used by the font display code. So we make them read-only,
5280 to avoid this confusing situation. */
5282 DEFVAR_LISP_NOPRO ("font-weight-table", Vfont_weight_table,
5283 doc: /* Vector of valid font weight values.
5284 Each element has the form:
5285 [NUMERIC-VALUE SYMBOLIC-NAME ALIAS-NAME ...]
5286 NUMERIC-VALUE is an integer, and SYMBOLIC-NAME and ALIAS-NAME are symbols. */);
5287 Vfont_weight_table = BUILD_STYLE_TABLE (weight_table);
5288 XSYMBOL (intern_c_string ("font-weight-table"))->constant = 1;
5290 DEFVAR_LISP_NOPRO ("font-slant-table", Vfont_slant_table,
5291 doc: /* Vector of font slant symbols vs the corresponding numeric values.
5292 See `font-weight-table' for the format of the vector. */);
5293 Vfont_slant_table = BUILD_STYLE_TABLE (slant_table);
5294 XSYMBOL (intern_c_string ("font-slant-table"))->constant = 1;
5296 DEFVAR_LISP_NOPRO ("font-width-table", Vfont_width_table,
5297 doc: /* Alist of font width symbols vs the corresponding numeric values.
5298 See `font-weight-table' for the format of the vector. */);
5299 Vfont_width_table = BUILD_STYLE_TABLE (width_table);
5300 XSYMBOL (intern_c_string ("font-width-table"))->constant = 1;
5302 staticpro (&font_style_table);
5303 font_style_table = make_uninit_vector (3);
5304 ASET (font_style_table, 0, Vfont_weight_table);
5305 ASET (font_style_table, 1, Vfont_slant_table);
5306 ASET (font_style_table, 2, Vfont_width_table);
5308 DEFVAR_LISP ("font-log", Vfont_log, doc: /*
5309 *Logging list of font related actions and results.
5310 The value t means to suppress the logging.
5311 The initial value is set to nil if the environment variable
5312 EMACS_FONT_LOG is set. Otherwise, it is set to t. */);
5313 Vfont_log = Qnil;
5315 #ifdef HAVE_WINDOW_SYSTEM
5316 #ifdef HAVE_FREETYPE
5317 syms_of_ftfont ();
5318 #ifdef HAVE_X_WINDOWS
5319 syms_of_xfont ();
5320 syms_of_ftxfont ();
5321 #ifdef HAVE_XFT
5322 syms_of_xftfont ();
5323 #endif /* HAVE_XFT */
5324 #endif /* HAVE_X_WINDOWS */
5325 #else /* not HAVE_FREETYPE */
5326 #ifdef HAVE_X_WINDOWS
5327 syms_of_xfont ();
5328 #endif /* HAVE_X_WINDOWS */
5329 #endif /* not HAVE_FREETYPE */
5330 #ifdef HAVE_BDFFONT
5331 syms_of_bdffont ();
5332 #endif /* HAVE_BDFFONT */
5333 #ifdef HAVE_NTGUI
5334 syms_of_w32font ();
5335 #endif /* HAVE_NTGUI */
5336 #endif /* HAVE_WINDOW_SYSTEM */
5339 void
5340 init_font (void)
5342 Vfont_log = egetenv ("EMACS_FONT_LOG") ? Qnil : Qt;