(file_name_completion): Check completion-ignored-extensions
[emacs.git] / src / font.c
blob5ad6d0bf8f066be5b82dbc24e1023e1a5fb3eaca
1 /* font.c -- "Font" primitives.
2 Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
3 Copyright (C) 2006, 2007, 2008, 2009
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H13PRO009
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <ctype.h>
27 #include "lisp.h"
28 #include "buffer.h"
29 #include "frame.h"
30 #include "window.h"
31 #include "dispextern.h"
32 #include "charset.h"
33 #include "character.h"
34 #include "composite.h"
35 #include "fontset.h"
36 #include "font.h"
38 #ifdef HAVE_X_WINDOWS
39 #include "xterm.h"
40 #endif /* HAVE_X_WINDOWS */
42 #ifdef HAVE_NTGUI
43 #include "w32term.h"
44 #endif /* HAVE_NTGUI */
46 #ifdef HAVE_NS
47 #include "nsterm.h"
48 #endif /* HAVE_NS */
50 #ifdef HAVE_NS
51 extern Lisp_Object Qfontsize;
52 #endif
54 Lisp_Object Qopentype;
56 /* Important character set strings. */
57 Lisp_Object Qascii_0, Qiso8859_1, Qiso10646_1, Qunicode_bmp, Qunicode_sip;
59 #ifdef HAVE_NS
60 #define DEFAULT_ENCODING Qiso10646_1
61 #else
62 #define DEFAULT_ENCODING Qiso8859_1
63 #endif
65 /* Unicode category `Cf'. */
66 static Lisp_Object QCf;
68 /* Special vector of zero length. This is repeatedly used by (struct
69 font_driver *)->list when a specified font is not found. */
70 static Lisp_Object null_vector;
72 static Lisp_Object Vfont_weight_table, Vfont_slant_table, Vfont_width_table;
74 /* Vector of Vfont_weight_table, Vfont_slant_table, and Vfont_width_table. */
75 static Lisp_Object font_style_table;
77 /* Structure used for tables mapping weight, slant, and width numeric
78 values and their names. */
80 struct table_entry
82 int numeric;
83 /* The first one is a valid name as a face attribute.
84 The second one (if any) is a typical name in XLFD field. */
85 char *names[5];
86 Lisp_Object *symbols;
89 /* Table of weight numeric values and their names. This table must be
90 sorted by numeric values in ascending order. */
92 static struct table_entry weight_table[] =
94 { 0, { "thin" }},
95 { 20, { "ultra-light", "ultralight" }},
96 { 40, { "extra-light", "extralight" }},
97 { 50, { "light" }},
98 { 75, { "semi-light", "semilight", "demilight", "book" }},
99 { 100, { "normal", "medium", "regular", "unspecified" }},
100 { 180, { "semi-bold", "semibold", "demibold", "demi" }},
101 { 200, { "bold" }},
102 { 205, { "extra-bold", "extrabold" }},
103 { 210, { "ultra-bold", "ultrabold", "black" }}
106 /* Table of slant numeric values and their names. This table must be
107 sorted by numeric values in ascending order. */
109 static struct table_entry slant_table[] =
111 { 0, { "reverse-oblique", "ro" }},
112 { 10, { "reverse-italic", "ri" }},
113 { 100, { "normal", "r", "unspecified" }},
114 { 200, { "italic" ,"i", "ot" }},
115 { 210, { "oblique", "o" }}
118 /* Table of width numeric values and their names. This table must be
119 sorted by numeric values in ascending order. */
121 static struct table_entry width_table[] =
123 { 50, { "ultra-condensed", "ultracondensed" }},
124 { 63, { "extra-condensed", "extracondensed" }},
125 { 75, { "condensed", "compressed", "narrow" }},
126 { 87, { "semi-condensed", "semicondensed", "demicondensed" }},
127 { 100, { "normal", "medium", "regular", "unspecified" }},
128 { 113, { "semi-expanded", "semiexpanded", "demiexpanded" }},
129 { 125, { "expanded" }},
130 { 150, { "extra-expanded", "extraexpanded" }},
131 { 200, { "ultra-expanded", "ultraexpanded", "wide" }}
134 extern Lisp_Object Qnormal;
136 /* Symbols representing keys of normal font properties. */
137 extern Lisp_Object QCtype, QCfamily, QCweight, QCslant, QCwidth;
138 extern Lisp_Object QCheight, QCsize, QCname;
140 Lisp_Object QCfoundry, QCadstyle, QCregistry;
141 /* Symbols representing keys of font extra info. */
142 Lisp_Object QCspacing, QCdpi, QCscalable, QCotf, QClang, QCscript, QCavgwidth;
143 Lisp_Object QCantialias, QCfont_entity, QCfc_unknown_spec;
144 /* Symbols representing values of font spacing property. */
145 Lisp_Object Qc, Qm, Qp, Qd;
147 Lisp_Object Vfont_encoding_alist;
149 /* Alist of font registry symbol and the corresponding charsets
150 information. The information is retrieved from
151 Vfont_encoding_alist on demand.
153 Eash element has the form:
154 (REGISTRY . (ENCODING-CHARSET-ID . REPERTORY-CHARSET-ID))
156 (REGISTRY . nil)
158 In the former form, ENCODING-CHARSET-ID is an ID of a charset that
159 encodes a character code to a glyph code of a font, and
160 REPERTORY-CHARSET-ID is an ID of a charset that tells if a
161 character is supported by a font.
163 The latter form means that the information for REGISTRY couldn't be
164 retrieved. */
165 static Lisp_Object font_charset_alist;
167 /* List of all font drivers. Each font-backend (XXXfont.c) calls
168 register_font_driver in syms_of_XXXfont to register its font-driver
169 here. */
170 static struct font_driver_list *font_driver_list;
174 /* Creaters of font-related Lisp object. */
176 Lisp_Object
177 font_make_spec ()
179 Lisp_Object font_spec;
180 struct font_spec *spec
181 = ((struct font_spec *)
182 allocate_pseudovector (VECSIZE (struct font_spec),
183 FONT_SPEC_MAX, PVEC_FONT));
184 XSETFONT (font_spec, spec);
185 return font_spec;
188 Lisp_Object
189 font_make_entity ()
191 Lisp_Object font_entity;
192 struct font_entity *entity
193 = ((struct font_entity *)
194 allocate_pseudovector (VECSIZE (struct font_entity),
195 FONT_ENTITY_MAX, PVEC_FONT));
196 XSETFONT (font_entity, entity);
197 return font_entity;
200 /* Create a font-object whose structure size is SIZE. If ENTITY is
201 not nil, copy properties from ENTITY to the font-object. If
202 PIXELSIZE is positive, set the `size' property to PIXELSIZE. */
203 Lisp_Object
204 font_make_object (size, entity, pixelsize)
205 int size;
206 Lisp_Object entity;
207 int pixelsize;
209 Lisp_Object font_object;
210 struct font *font
211 = (struct font *) allocate_pseudovector (size, FONT_OBJECT_MAX, PVEC_FONT);
212 int i;
214 XSETFONT (font_object, font);
216 if (! NILP (entity))
218 for (i = 1; i < FONT_SPEC_MAX; i++)
219 font->props[i] = AREF (entity, i);
220 if (! NILP (AREF (entity, FONT_EXTRA_INDEX)))
221 font->props[FONT_EXTRA_INDEX]
222 = Fcopy_sequence (AREF (entity, FONT_EXTRA_INDEX));
224 if (size > 0)
225 font->props[FONT_SIZE_INDEX] = make_number (pixelsize);
226 return font_object;
231 static int font_pixel_size P_ ((FRAME_PTR f, Lisp_Object));
232 static Lisp_Object font_open_entity P_ ((FRAME_PTR, Lisp_Object, int));
233 static Lisp_Object font_matching_entity P_ ((FRAME_PTR, Lisp_Object *,
234 Lisp_Object));
236 /* Number of registered font drivers. */
237 static int num_font_drivers;
240 /* Return a Lispy value of a font property value at STR and LEN bytes.
241 If STR is "*", it returns nil.
242 If FORCE_SYMBOL is zero and all characters in STR are digits, it
243 returns an integer. Otherwise, it returns a symbol interned from
244 STR. */
246 Lisp_Object
247 font_intern_prop (str, len, force_symbol)
248 char *str;
249 int len;
250 int force_symbol;
252 int i;
253 Lisp_Object tem;
254 Lisp_Object obarray;
255 int nbytes, nchars;
257 if (len == 1 && *str == '*')
258 return Qnil;
259 if (!force_symbol && len >=1 && isdigit (*str))
261 for (i = 1; i < len; i++)
262 if (! isdigit (str[i]))
263 break;
264 if (i == len)
265 return make_number (atoi (str));
268 /* The following code is copied from the function intern (in
269 lread.c), and modified to suite our purpose. */
270 obarray = Vobarray;
271 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
272 obarray = check_obarray (obarray);
273 parse_str_as_multibyte (str, len, &nchars, &nbytes);
274 if (len == nchars || len != nbytes)
275 /* CONTENTS contains no multibyte sequences or contains an invalid
276 multibyte sequence. We'll make a unibyte string. */
277 tem = oblookup (obarray, str, len, len);
278 else
279 tem = oblookup (obarray, str, nchars, len);
280 if (SYMBOLP (tem))
281 return tem;
282 if (len == nchars || len != nbytes)
283 tem = make_unibyte_string (str, len);
284 else
285 tem = make_multibyte_string (str, nchars, len);
286 return Fintern (tem, obarray);
289 /* Return a pixel size of font-spec SPEC on frame F. */
291 static int
292 font_pixel_size (f, spec)
293 FRAME_PTR f;
294 Lisp_Object spec;
296 #ifdef HAVE_WINDOW_SYSTEM
297 Lisp_Object size = AREF (spec, FONT_SIZE_INDEX);
298 double point_size;
299 int dpi, pixel_size;
300 Lisp_Object val;
302 if (INTEGERP (size))
303 return XINT (size);
304 if (NILP (size))
305 return 0;
306 font_assert (FLOATP (size));
307 point_size = XFLOAT_DATA (size);
308 val = AREF (spec, FONT_DPI_INDEX);
309 if (INTEGERP (val))
310 dpi = XINT (val);
311 else
312 dpi = f->resy;
313 pixel_size = POINT_TO_PIXEL (point_size, dpi);
314 return pixel_size;
315 #else
316 return 1;
317 #endif
321 /* Return a value of PROP's VAL (symbol or integer) to be stored in a
322 font vector. If VAL is not valid (i.e. not registered in
323 font_style_table), return -1 if NOERROR is zero, and return a
324 proper index if NOERROR is nonzero. In that case, register VAL in
325 font_style_table if VAL is a symbol, and return a closest index if
326 VAL is an integer. */
329 font_style_to_value (prop, val, noerror)
330 enum font_property_index prop;
331 Lisp_Object val;
332 int noerror;
334 Lisp_Object table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
335 int len = ASIZE (table);
336 int i, j;
338 if (SYMBOLP (val))
340 unsigned char *s;
341 Lisp_Object args[2], elt;
343 /* At first try exact match. */
344 for (i = 0; i < len; i++)
345 for (j = 1; j < ASIZE (AREF (table, i)); j++)
346 if (EQ (val, AREF (AREF (table, i), j)))
347 return ((XINT (AREF (AREF (table, i), 0)) << 8)
348 | (i << 4) | (j - 1));
349 /* Try also with case-folding match. */
350 s = SDATA (SYMBOL_NAME (val));
351 for (i = 0; i < len; i++)
352 for (j = 1; j < ASIZE (AREF (table, i)); j++)
354 elt = AREF (AREF (table, i), j);
355 if (xstrcasecmp (s, SDATA (SYMBOL_NAME (elt))) == 0)
356 return ((XINT (AREF (AREF (table, i), 0)) << 8)
357 | (i << 4) | (j - 1));
359 if (! noerror)
360 return -1;
361 if (len == 255)
362 abort ();
363 elt = Fmake_vector (make_number (2), make_number (100));
364 ASET (elt, 1, val);
365 args[0] = table;
366 args[1] = Fmake_vector (make_number (1), elt);
367 ASET (font_style_table, prop - FONT_WEIGHT_INDEX, Fvconcat (2, args));
368 return (100 << 8) | (i << 4);
370 else
372 int i, last_n;
373 int numeric = XINT (val);
375 for (i = 0, last_n = -1; i < len; i++)
377 int n = XINT (AREF (AREF (table, i), 0));
379 if (numeric == n)
380 return (n << 8) | (i << 4);
381 if (numeric < n)
383 if (! noerror)
384 return -1;
385 return ((i == 0 || n - numeric < numeric - last_n)
386 ? (n << 8) | (i << 4): (last_n << 8 | ((i - 1) << 4)));
388 last_n = n;
390 if (! noerror)
391 return -1;
392 return ((last_n << 8) | ((i - 1) << 4));
396 Lisp_Object
397 font_style_symbolic (font, prop, for_face)
398 Lisp_Object font;
399 enum font_property_index prop;
400 int for_face;
402 Lisp_Object val = AREF (font, prop);
403 Lisp_Object table, elt;
404 int i;
406 if (NILP (val))
407 return Qnil;
408 table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
409 i = XINT (val) & 0xFF;
410 font_assert (((i >> 4) & 0xF) < ASIZE (table));
411 elt = AREF (table, ((i >> 4) & 0xF));
412 font_assert ((i & 0xF) + 1 < ASIZE (elt));
413 return (for_face ? AREF (elt, 1) : AREF (elt, (i & 0xF) + 1));
416 extern Lisp_Object Vface_alternative_font_family_alist;
418 extern Lisp_Object find_font_encoding P_ ((Lisp_Object));
421 /* Return ENCODING or a cons of ENCODING and REPERTORY of the font
422 FONTNAME. ENCODING is a charset symbol that specifies the encoding
423 of the font. REPERTORY is a charset symbol or nil. */
425 Lisp_Object
426 find_font_encoding (fontname)
427 Lisp_Object fontname;
429 Lisp_Object tail, elt;
431 for (tail = Vfont_encoding_alist; CONSP (tail); tail = XCDR (tail))
433 elt = XCAR (tail);
434 if (CONSP (elt)
435 && STRINGP (XCAR (elt))
436 && fast_string_match_ignore_case (XCAR (elt), fontname) >= 0
437 && (SYMBOLP (XCDR (elt))
438 ? CHARSETP (XCDR (elt))
439 : CONSP (XCDR (elt)) && CHARSETP (XCAR (XCDR (elt)))))
440 return (XCDR (elt));
442 /* We don't know the encoding of this font. Let's assume `ascii'. */
443 return Qascii;
446 /* Return encoding charset and repertory charset for REGISTRY in
447 ENCODING and REPERTORY correspondingly. If correct information for
448 REGISTRY is available, return 0. Otherwise return -1. */
451 font_registry_charsets (registry, encoding, repertory)
452 Lisp_Object registry;
453 struct charset **encoding, **repertory;
455 Lisp_Object val;
456 int encoding_id, repertory_id;
458 val = Fassoc_string (registry, font_charset_alist, Qt);
459 if (! NILP (val))
461 val = XCDR (val);
462 if (NILP (val))
463 return -1;
464 encoding_id = XINT (XCAR (val));
465 repertory_id = XINT (XCDR (val));
467 else
469 val = find_font_encoding (SYMBOL_NAME (registry));
470 if (SYMBOLP (val) && CHARSETP (val))
472 encoding_id = repertory_id = XINT (CHARSET_SYMBOL_ID (val));
474 else if (CONSP (val))
476 if (! CHARSETP (XCAR (val)))
477 goto invalid_entry;
478 encoding_id = XINT (CHARSET_SYMBOL_ID (XCAR (val)));
479 if (NILP (XCDR (val)))
480 repertory_id = -1;
481 else
483 if (! CHARSETP (XCDR (val)))
484 goto invalid_entry;
485 repertory_id = XINT (CHARSET_SYMBOL_ID (XCDR (val)));
488 else
489 goto invalid_entry;
490 val = Fcons (make_number (encoding_id), make_number (repertory_id));
491 font_charset_alist
492 = nconc2 (font_charset_alist, Fcons (Fcons (registry, val), Qnil));
495 if (encoding)
496 *encoding = CHARSET_FROM_ID (encoding_id);
497 if (repertory)
498 *repertory = repertory_id >= 0 ? CHARSET_FROM_ID (repertory_id) : NULL;
499 return 0;
501 invalid_entry:
502 font_charset_alist
503 = nconc2 (font_charset_alist, Fcons (Fcons (registry, Qnil), Qnil));
504 return -1;
508 /* Font property value validaters. See the comment of
509 font_property_table for the meaning of the arguments. */
511 static Lisp_Object font_prop_validate P_ ((int, Lisp_Object, Lisp_Object));
512 static Lisp_Object font_prop_validate_symbol P_ ((Lisp_Object, Lisp_Object));
513 static Lisp_Object font_prop_validate_style P_ ((Lisp_Object, Lisp_Object));
514 static Lisp_Object font_prop_validate_non_neg P_ ((Lisp_Object, Lisp_Object));
515 static Lisp_Object font_prop_validate_spacing P_ ((Lisp_Object, Lisp_Object));
516 static int get_font_prop_index P_ ((Lisp_Object));
518 static Lisp_Object
519 font_prop_validate_symbol (prop, val)
520 Lisp_Object prop, val;
522 if (STRINGP (val))
523 val = Fintern (val, Qnil);
524 if (! SYMBOLP (val))
525 val = Qerror;
526 else if (EQ (prop, QCregistry))
527 val = Fintern (Fdowncase (SYMBOL_NAME (val)), Qnil);
528 return val;
532 static Lisp_Object
533 font_prop_validate_style (style, val)
534 Lisp_Object style, val;
536 enum font_property_index prop = (EQ (style, QCweight) ? FONT_WEIGHT_INDEX
537 : EQ (style, QCslant) ? FONT_SLANT_INDEX
538 : FONT_WIDTH_INDEX);
539 int n;
540 if (INTEGERP (val))
542 n = XINT (val);
543 if (((n >> 4) & 0xF)
544 >= ASIZE (AREF (font_style_table, prop - FONT_WEIGHT_INDEX)))
545 val = Qerror;
546 else
548 Lisp_Object elt = AREF (AREF (font_style_table, prop - FONT_WEIGHT_INDEX), (n >> 4) & 0xF);
550 if ((n & 0xF) + 1 >= ASIZE (elt))
551 val = Qerror;
552 else if (XINT (AREF (elt, 0)) != (n >> 8))
553 val = Qerror;
556 else if (SYMBOLP (val))
558 int n = font_style_to_value (prop, val, 0);
560 val = n >= 0 ? make_number (n) : Qerror;
562 else
563 val = Qerror;
564 return val;
567 static Lisp_Object
568 font_prop_validate_non_neg (prop, val)
569 Lisp_Object prop, val;
571 return (NATNUMP (val) || (FLOATP (val) && XFLOAT_DATA (val) >= 0)
572 ? val : Qerror);
575 static Lisp_Object
576 font_prop_validate_spacing (prop, val)
577 Lisp_Object prop, val;
579 if (NILP (val) || (NATNUMP (val) && XINT (val) <= FONT_SPACING_CHARCELL))
580 return val;
581 if (SYMBOLP (val) && SBYTES (SYMBOL_NAME (val)) == 1)
583 char spacing = SDATA (SYMBOL_NAME (val))[0];
585 if (spacing == 'c' || spacing == 'C')
586 return make_number (FONT_SPACING_CHARCELL);
587 if (spacing == 'm' || spacing == 'M')
588 return make_number (FONT_SPACING_MONO);
589 if (spacing == 'p' || spacing == 'P')
590 return make_number (FONT_SPACING_PROPORTIONAL);
591 if (spacing == 'd' || spacing == 'D')
592 return make_number (FONT_SPACING_DUAL);
594 return Qerror;
597 static Lisp_Object
598 font_prop_validate_otf (prop, val)
599 Lisp_Object prop, val;
601 Lisp_Object tail, tmp;
602 int i;
604 /* VAL = (SCRIPT [ LANGSYS [ GSUB-FEATURES [ GPOS-FEATURES ]]])
605 GSUB-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil
606 GPOS-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil */
607 if (! CONSP (val))
608 return Qerror;
609 if (! SYMBOLP (XCAR (val)))
610 return Qerror;
611 tail = XCDR (val);
612 if (NILP (tail))
613 return val;
614 if (! CONSP (tail) || ! SYMBOLP (XCAR (val)))
615 return Qerror;
616 for (i = 0; i < 2; i++)
618 tail = XCDR (tail);
619 if (NILP (tail))
620 return val;
621 if (! CONSP (tail))
622 return Qerror;
623 for (tmp = XCAR (tail); CONSP (tmp); tmp = XCDR (tmp))
624 if (! SYMBOLP (XCAR (tmp)))
625 return Qerror;
626 if (! NILP (tmp))
627 return Qerror;
629 return val;
632 /* Structure of known font property keys and validater of the
633 values. */
634 struct
636 /* Pointer to the key symbol. */
637 Lisp_Object *key;
638 /* Function to validate PROP's value VAL, or NULL if any value is
639 ok. The value is VAL or its regularized value if VAL is valid,
640 and Qerror if not. */
641 Lisp_Object (*validater) P_ ((Lisp_Object prop, Lisp_Object val));
642 } font_property_table[] =
643 { { &QCtype, font_prop_validate_symbol },
644 { &QCfoundry, font_prop_validate_symbol },
645 { &QCfamily, font_prop_validate_symbol },
646 { &QCadstyle, font_prop_validate_symbol },
647 { &QCregistry, font_prop_validate_symbol },
648 { &QCweight, font_prop_validate_style },
649 { &QCslant, font_prop_validate_style },
650 { &QCwidth, font_prop_validate_style },
651 { &QCsize, font_prop_validate_non_neg },
652 { &QCdpi, font_prop_validate_non_neg },
653 { &QCspacing, font_prop_validate_spacing },
654 { &QCavgwidth, font_prop_validate_non_neg },
655 /* The order of the above entries must match with enum
656 font_property_index. */
657 { &QClang, font_prop_validate_symbol },
658 { &QCscript, font_prop_validate_symbol },
659 { &QCotf, font_prop_validate_otf }
662 /* Size (number of elements) of the above table. */
663 #define FONT_PROPERTY_TABLE_SIZE \
664 ((sizeof font_property_table) / (sizeof *font_property_table))
666 /* Return an index number of font property KEY or -1 if KEY is not an
667 already known property. */
669 static int
670 get_font_prop_index (key)
671 Lisp_Object key;
673 int i;
675 for (i = 0; i < FONT_PROPERTY_TABLE_SIZE; i++)
676 if (EQ (key, *font_property_table[i].key))
677 return i;
678 return -1;
681 /* Validate the font property. The property key is specified by the
682 symbol PROP, or the index IDX (if PROP is nil). If VAL is invalid,
683 signal an error. The value is VAL or the regularized one. */
685 static Lisp_Object
686 font_prop_validate (idx, prop, val)
687 int idx;
688 Lisp_Object prop, val;
690 Lisp_Object validated;
692 if (NILP (val))
693 return val;
694 if (NILP (prop))
695 prop = *font_property_table[idx].key;
696 else
698 idx = get_font_prop_index (prop);
699 if (idx < 0)
700 return val;
702 validated = (font_property_table[idx].validater) (prop, val);
703 if (EQ (validated, Qerror))
704 signal_error ("invalid font property", Fcons (prop, val));
705 return validated;
709 /* Store VAL as a value of extra font property PROP in FONT while
710 keeping the sorting order. Don't check the validity of VAL. */
712 Lisp_Object
713 font_put_extra (font, prop, val)
714 Lisp_Object font, prop, val;
716 Lisp_Object extra = AREF (font, FONT_EXTRA_INDEX);
717 Lisp_Object slot = (NILP (extra) ? Qnil : assq_no_quit (prop, extra));
719 if (NILP (slot))
721 Lisp_Object prev = Qnil;
723 while (CONSP (extra)
724 && NILP (Fstring_lessp (prop, XCAR (XCAR (extra)))))
725 prev = extra, extra = XCDR (extra);
726 if (NILP (prev))
727 ASET (font, FONT_EXTRA_INDEX, Fcons (Fcons (prop, val), extra));
728 else
729 XSETCDR (prev, Fcons (Fcons (prop, val), extra));
730 return val;
732 XSETCDR (slot, val);
733 return val;
737 /* Font name parser and unparser */
739 static int parse_matrix P_ ((char *));
740 static int font_expand_wildcards P_ ((Lisp_Object *, int));
741 static int font_parse_name P_ ((char *, Lisp_Object));
743 /* An enumerator for each field of an XLFD font name. */
744 enum xlfd_field_index
746 XLFD_FOUNDRY_INDEX,
747 XLFD_FAMILY_INDEX,
748 XLFD_WEIGHT_INDEX,
749 XLFD_SLANT_INDEX,
750 XLFD_SWIDTH_INDEX,
751 XLFD_ADSTYLE_INDEX,
752 XLFD_PIXEL_INDEX,
753 XLFD_POINT_INDEX,
754 XLFD_RESX_INDEX,
755 XLFD_RESY_INDEX,
756 XLFD_SPACING_INDEX,
757 XLFD_AVGWIDTH_INDEX,
758 XLFD_REGISTRY_INDEX,
759 XLFD_ENCODING_INDEX,
760 XLFD_LAST_INDEX
763 /* An enumerator for mask bit corresponding to each XLFD field. */
764 enum xlfd_field_mask
766 XLFD_FOUNDRY_MASK = 0x0001,
767 XLFD_FAMILY_MASK = 0x0002,
768 XLFD_WEIGHT_MASK = 0x0004,
769 XLFD_SLANT_MASK = 0x0008,
770 XLFD_SWIDTH_MASK = 0x0010,
771 XLFD_ADSTYLE_MASK = 0x0020,
772 XLFD_PIXEL_MASK = 0x0040,
773 XLFD_POINT_MASK = 0x0080,
774 XLFD_RESX_MASK = 0x0100,
775 XLFD_RESY_MASK = 0x0200,
776 XLFD_SPACING_MASK = 0x0400,
777 XLFD_AVGWIDTH_MASK = 0x0800,
778 XLFD_REGISTRY_MASK = 0x1000,
779 XLFD_ENCODING_MASK = 0x2000
783 /* Parse P pointing the pixel/point size field of the form
784 `[A B C D]' which specifies a transformation matrix:
786 A B 0
787 C D 0
788 0 0 1
790 by which all glyphs of the font are transformed. The spec says
791 that scalar value N for the pixel/point size is equivalent to:
792 A = N * resx/resy, B = C = 0, D = N.
794 Return the scalar value N if the form is valid. Otherwise return
795 -1. */
797 static int
798 parse_matrix (p)
799 char *p;
801 double matrix[4];
802 char *end;
803 int i;
805 for (i = 0, p++; i < 4 && *p && *p != ']'; i++)
807 if (*p == '~')
808 matrix[i] = - strtod (p + 1, &end);
809 else
810 matrix[i] = strtod (p, &end);
811 p = end;
813 return (i == 4 ? (int) matrix[3] : -1);
816 /* Expand a wildcard field in FIELD (the first N fields are filled) to
817 multiple fields to fill in all 14 XLFD fields while restring a
818 field position by its contents. */
820 static int
821 font_expand_wildcards (field, n)
822 Lisp_Object field[XLFD_LAST_INDEX];
823 int n;
825 /* Copy of FIELD. */
826 Lisp_Object tmp[XLFD_LAST_INDEX];
827 /* Array of information about where this element can go. Nth
828 element is for Nth element of FIELD. */
829 struct {
830 /* Minimum possible field. */
831 int from;
832 /* Maxinum possible field. */
833 int to;
834 /* Bit mask of possible field. Nth bit corresponds to Nth field. */
835 int mask;
836 } range[XLFD_LAST_INDEX];
837 int i, j;
838 int range_from, range_to;
839 unsigned range_mask;
841 #define XLFD_SYMBOL_MASK (XLFD_FOUNDRY_MASK | XLFD_FAMILY_MASK \
842 | XLFD_ADSTYLE_MASK | XLFD_REGISTRY_MASK)
843 #define XLFD_NULL_MASK (XLFD_FOUNDRY_MASK | XLFD_ADSTYLE_MASK)
844 #define XLFD_LARGENUM_MASK (XLFD_POINT_MASK | XLFD_RESX_MASK | XLFD_RESY_MASK \
845 | XLFD_AVGWIDTH_MASK)
846 #define XLFD_REGENC_MASK (XLFD_REGISTRY_MASK | XLFD_ENCODING_MASK)
848 /* Initialize RANGE_MASK for FIELD[0] which can be 0th to (14 - N)th
849 field. The value is shifted to left one bit by one in the
850 following loop. */
851 for (i = 0, range_mask = 0; i <= 14 - n; i++)
852 range_mask = (range_mask << 1) | 1;
854 /* The triplet RANGE_FROM, RANGE_TO, and RANGE_MASK is a
855 position-based retriction for FIELD[I]. */
856 for (i = 0, range_from = 0, range_to = 14 - n; i < n;
857 i++, range_from++, range_to++, range_mask <<= 1)
859 Lisp_Object val = field[i];
861 tmp[i] = val;
862 if (NILP (val))
864 /* Wildcard. */
865 range[i].from = range_from;
866 range[i].to = range_to;
867 range[i].mask = range_mask;
869 else
871 /* The triplet FROM, TO, and MASK is a value-based
872 retriction for FIELD[I]. */
873 int from, to;
874 unsigned mask;
876 if (INTEGERP (val))
878 int numeric = XINT (val);
880 if (i + 1 == n)
881 from = to = XLFD_ENCODING_INDEX,
882 mask = XLFD_ENCODING_MASK;
883 else if (numeric == 0)
884 from = XLFD_PIXEL_INDEX, to = XLFD_AVGWIDTH_INDEX,
885 mask = XLFD_PIXEL_MASK | XLFD_LARGENUM_MASK;
886 else if (numeric <= 48)
887 from = to = XLFD_PIXEL_INDEX,
888 mask = XLFD_PIXEL_MASK;
889 else
890 from = XLFD_POINT_INDEX, to = XLFD_AVGWIDTH_INDEX,
891 mask = XLFD_LARGENUM_MASK;
893 else if (SBYTES (SYMBOL_NAME (val)) == 0)
894 from = XLFD_FOUNDRY_INDEX, to = XLFD_ADSTYLE_INDEX,
895 mask = XLFD_NULL_MASK;
896 else if (i == 0)
897 from = to = XLFD_FOUNDRY_INDEX, mask = XLFD_FOUNDRY_MASK;
898 else if (i + 1 == n)
900 Lisp_Object name = SYMBOL_NAME (val);
902 if (SDATA (name)[SBYTES (name) - 1] == '*')
903 from = XLFD_REGISTRY_INDEX, to = XLFD_ENCODING_INDEX,
904 mask = XLFD_REGENC_MASK;
905 else
906 from = to = XLFD_ENCODING_INDEX,
907 mask = XLFD_ENCODING_MASK;
909 else if (range_from <= XLFD_WEIGHT_INDEX
910 && range_to >= XLFD_WEIGHT_INDEX
911 && FONT_WEIGHT_NAME_NUMERIC (val) >= 0)
912 from = to = XLFD_WEIGHT_INDEX, mask = XLFD_WEIGHT_MASK;
913 else if (range_from <= XLFD_SLANT_INDEX
914 && range_to >= XLFD_SLANT_INDEX
915 && FONT_SLANT_NAME_NUMERIC (val) >= 0)
916 from = to = XLFD_SLANT_INDEX, mask = XLFD_SLANT_MASK;
917 else if (range_from <= XLFD_SWIDTH_INDEX
918 && range_to >= XLFD_SWIDTH_INDEX
919 && FONT_WIDTH_NAME_NUMERIC (val) >= 0)
920 from = to = XLFD_SWIDTH_INDEX, mask = XLFD_SWIDTH_MASK;
921 else
923 if (EQ (val, Qc) || EQ (val, Qm) || EQ (val, Qp) || EQ (val, Qd))
924 from = to = XLFD_SPACING_INDEX, mask = XLFD_SPACING_MASK;
925 else
926 from = XLFD_FOUNDRY_INDEX, to = XLFD_ENCODING_INDEX,
927 mask = XLFD_SYMBOL_MASK;
930 /* Merge position-based and value-based restrictions. */
931 mask &= range_mask;
932 while (from < range_from)
933 mask &= ~(1 << from++);
934 while (from < 14 && ! (mask & (1 << from)))
935 from++;
936 while (to > range_to)
937 mask &= ~(1 << to--);
938 while (to >= 0 && ! (mask & (1 << to)))
939 to--;
940 if (from > to)
941 return -1;
942 range[i].from = from;
943 range[i].to = to;
944 range[i].mask = mask;
946 if (from > range_from || to < range_to)
948 /* The range is narrowed by value-based restrictions.
949 Reflect it to the other fields. */
951 /* Following fields should be after FROM. */
952 range_from = from;
953 /* Preceding fields should be before TO. */
954 for (j = i - 1, from--, to--; j >= 0; j--, from--, to--)
956 /* Check FROM for non-wildcard field. */
957 if (! NILP (tmp[j]) && range[j].from < from)
959 while (range[j].from < from)
960 range[j].mask &= ~(1 << range[j].from++);
961 while (from < 14 && ! (range[j].mask & (1 << from)))
962 from++;
963 range[j].from = from;
965 else
966 from = range[j].from;
967 if (range[j].to > to)
969 while (range[j].to > to)
970 range[j].mask &= ~(1 << range[j].to--);
971 while (to >= 0 && ! (range[j].mask & (1 << to)))
972 to--;
973 range[j].to = to;
975 else
976 to = range[j].to;
977 if (from > to)
978 return -1;
984 /* Decide all fileds from restrictions in RANGE. */
985 for (i = j = 0; i < n ; i++)
987 if (j < range[i].from)
989 if (i == 0 || ! NILP (tmp[i - 1]))
990 /* None of TMP[X] corresponds to Jth field. */
991 return -1;
992 for (; j < range[i].from; j++)
993 field[j] = Qnil;
995 field[j++] = tmp[i];
997 if (! NILP (tmp[n - 1]) && j < XLFD_REGISTRY_INDEX)
998 return -1;
999 for (; j < XLFD_LAST_INDEX; j++)
1000 field[j] = Qnil;
1001 if (INTEGERP (field[XLFD_ENCODING_INDEX]))
1002 field[XLFD_ENCODING_INDEX]
1003 = Fintern (Fnumber_to_string (field[XLFD_ENCODING_INDEX]), Qnil);
1004 return 0;
1008 #ifdef ENABLE_CHECKING
1009 /* Match a 14-field XLFD pattern against a full XLFD font name. */
1010 static int
1011 font_match_xlfd (char *pattern, char *name)
1013 while (*pattern && *name)
1015 if (*pattern == *name)
1016 pattern++;
1017 else if (*pattern == '*')
1018 if (*name == pattern[1])
1019 pattern += 2;
1020 else
1022 else
1023 return 0;
1024 name++;
1026 return 1;
1029 /* Make sure the font object matches the XLFD font name. */
1030 static int
1031 font_check_xlfd_parse (Lisp_Object font, char *name)
1033 char name_check[256];
1034 font_unparse_xlfd (font, 0, name_check, 255);
1035 return font_match_xlfd (name_check, name);
1038 #endif
1041 /* Parse NAME (null terminated) as XLFD and store information in FONT
1042 (font-spec or font-entity). Size property of FONT is set as
1043 follows:
1044 specified XLFD fields FONT property
1045 --------------------- -------------
1046 PIXEL_SIZE PIXEL_SIZE (Lisp integer)
1047 POINT_SIZE and RESY calculated pixel size (Lisp integer)
1048 POINT_SIZE POINT_SIZE/10 (Lisp float)
1050 If NAME is successfully parsed, return 0. Otherwise return -1.
1052 FONT is usually a font-spec, but when this function is called from
1053 X font backend driver, it is a font-entity. In that case, NAME is
1054 a fully specified XLFD. */
1057 font_parse_xlfd (name, font)
1058 char *name;
1059 Lisp_Object font;
1061 int len = strlen (name);
1062 int i, j, n;
1063 char *f[XLFD_LAST_INDEX + 1];
1064 Lisp_Object val;
1065 char *p;
1067 if (len > 255 || !len)
1068 /* Maximum XLFD name length is 255. */
1069 return -1;
1070 /* Accept "*-.." as a fully specified XLFD. */
1071 if (name[0] == '*' && (len == 1 || name[1] == '-'))
1072 i = 1, f[XLFD_FOUNDRY_INDEX] = name;
1073 else
1074 i = 0;
1075 for (p = name + i; *p; p++)
1076 if (*p == '-')
1078 f[i++] = p + 1;
1079 if (i == XLFD_LAST_INDEX)
1080 break;
1082 f[i] = name + len;
1084 #define INTERN_FIELD(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 0)
1085 #define INTERN_FIELD_SYM(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 1)
1087 if (i == XLFD_LAST_INDEX)
1089 /* Fully specified XLFD. */
1090 int pixel_size;
1092 ASET (font, FONT_FOUNDRY_INDEX, INTERN_FIELD_SYM (XLFD_FOUNDRY_INDEX));
1093 ASET (font, FONT_FAMILY_INDEX, INTERN_FIELD_SYM (XLFD_FAMILY_INDEX));
1094 for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
1095 i <= XLFD_SWIDTH_INDEX; i++, j++)
1097 val = INTERN_FIELD_SYM (i);
1098 if (! NILP (val))
1100 if ((n = font_style_to_value (j, INTERN_FIELD_SYM (i), 0)) < 0)
1101 return -1;
1102 ASET (font, j, make_number (n));
1105 ASET (font, FONT_ADSTYLE_INDEX, INTERN_FIELD_SYM (XLFD_ADSTYLE_INDEX));
1106 if (strcmp (f[XLFD_REGISTRY_INDEX], "*-*") == 0)
1107 ASET (font, FONT_REGISTRY_INDEX, Qnil);
1108 else
1109 ASET (font, FONT_REGISTRY_INDEX,
1110 font_intern_prop (f[XLFD_REGISTRY_INDEX],
1111 f[XLFD_LAST_INDEX] - f[XLFD_REGISTRY_INDEX],
1112 1));
1113 p = f[XLFD_PIXEL_INDEX];
1114 if (*p == '[' && (pixel_size = parse_matrix (p)) >= 0)
1115 ASET (font, FONT_SIZE_INDEX, make_number (pixel_size));
1116 else
1118 val = INTERN_FIELD (XLFD_PIXEL_INDEX);
1119 if (INTEGERP (val))
1120 ASET (font, FONT_SIZE_INDEX, val);
1121 else
1123 double point_size = -1;
1125 font_assert (FONT_SPEC_P (font));
1126 p = f[XLFD_POINT_INDEX];
1127 if (*p == '[')
1128 point_size = parse_matrix (p);
1129 else if (isdigit (*p))
1130 point_size = atoi (p), point_size /= 10;
1131 if (point_size >= 0)
1132 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1136 ASET (font, FONT_DPI_INDEX, INTERN_FIELD (XLFD_RESY_INDEX));
1137 val = INTERN_FIELD (XLFD_SPACING_INDEX);
1138 if (! NILP (val))
1140 val = font_prop_validate_spacing (QCspacing, val);
1141 if (! INTEGERP (val))
1142 return -1;
1143 ASET (font, FONT_SPACING_INDEX, val);
1145 p = f[XLFD_AVGWIDTH_INDEX];
1146 if (*p == '~')
1147 p++;
1148 ASET (font, FONT_AVGWIDTH_INDEX,
1149 font_intern_prop (p, f[XLFD_REGISTRY_INDEX] - 1 - p, 0));
1151 else
1153 int wild_card_found = 0;
1154 Lisp_Object prop[XLFD_LAST_INDEX];
1156 if (FONT_ENTITY_P (font))
1157 return -1;
1158 for (j = 0; j < i; j++)
1160 if (*f[j] == '*')
1162 if (f[j][1] && f[j][1] != '-')
1163 return -1;
1164 prop[j] = Qnil;
1165 wild_card_found = 1;
1167 else if (j + 1 < i)
1168 prop[j] = INTERN_FIELD (j);
1169 else
1170 prop[j] = font_intern_prop (f[j], f[i] - f[j], 0);
1172 if (! wild_card_found)
1173 return -1;
1174 if (font_expand_wildcards (prop, i) < 0)
1175 return -1;
1177 ASET (font, FONT_FOUNDRY_INDEX, prop[XLFD_FOUNDRY_INDEX]);
1178 ASET (font, FONT_FAMILY_INDEX, prop[XLFD_FAMILY_INDEX]);
1179 for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
1180 i <= XLFD_SWIDTH_INDEX; i++, j++)
1181 if (! NILP (prop[i]))
1183 if ((n = font_style_to_value (j, prop[i], 1)) < 0)
1184 return -1;
1185 ASET (font, j, make_number (n));
1187 ASET (font, FONT_ADSTYLE_INDEX, prop[XLFD_ADSTYLE_INDEX]);
1188 val = prop[XLFD_REGISTRY_INDEX];
1189 if (NILP (val))
1191 val = prop[XLFD_ENCODING_INDEX];
1192 if (! NILP (val))
1193 val = concat2 (build_string ("*-"), SYMBOL_NAME (val));
1195 else if (NILP (prop[XLFD_ENCODING_INDEX]))
1196 val = concat2 (SYMBOL_NAME (val), build_string ("-*"));
1197 else
1198 val = concat3 (SYMBOL_NAME (val), build_string ("-"),
1199 SYMBOL_NAME (prop[XLFD_ENCODING_INDEX]));
1200 if (! NILP (val))
1201 ASET (font, FONT_REGISTRY_INDEX, Fintern (val, Qnil));
1203 if (INTEGERP (prop[XLFD_PIXEL_INDEX]))
1204 ASET (font, FONT_SIZE_INDEX, prop[XLFD_PIXEL_INDEX]);
1205 else if (INTEGERP (prop[XLFD_POINT_INDEX]))
1207 double point_size = XINT (prop[XLFD_POINT_INDEX]);
1209 ASET (font, FONT_SIZE_INDEX, make_float (point_size / 10));
1212 if (INTEGERP (prop[XLFD_RESX_INDEX]))
1213 ASET (font, FONT_DPI_INDEX, prop[XLFD_RESY_INDEX]);
1214 if (! NILP (prop[XLFD_SPACING_INDEX]))
1216 val = font_prop_validate_spacing (QCspacing,
1217 prop[XLFD_SPACING_INDEX]);
1218 if (! INTEGERP (val))
1219 return -1;
1220 ASET (font, FONT_SPACING_INDEX, val);
1222 if (INTEGERP (prop[XLFD_AVGWIDTH_INDEX]))
1223 ASET (font, FONT_AVGWIDTH_INDEX, prop[XLFD_AVGWIDTH_INDEX]);
1226 return 0;
1229 /* Store XLFD name of FONT (font-spec or font-entity) in NAME (NBYTES
1230 length), and return the name length. If FONT_SIZE_INDEX of FONT is
1231 0, use PIXEL_SIZE instead. */
1234 font_unparse_xlfd (font, pixel_size, name, nbytes)
1235 Lisp_Object font;
1236 int pixel_size;
1237 char *name;
1238 int nbytes;
1240 char *f[XLFD_REGISTRY_INDEX + 1];
1241 Lisp_Object val;
1242 int i, j, len = 0;
1244 font_assert (FONTP (font));
1246 for (i = FONT_FOUNDRY_INDEX, j = XLFD_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX;
1247 i++, j++)
1249 if (i == FONT_ADSTYLE_INDEX)
1250 j = XLFD_ADSTYLE_INDEX;
1251 else if (i == FONT_REGISTRY_INDEX)
1252 j = XLFD_REGISTRY_INDEX;
1253 val = AREF (font, i);
1254 if (NILP (val))
1256 if (j == XLFD_REGISTRY_INDEX)
1257 f[j] = "*-*", len += 4;
1258 else
1259 f[j] = "*", len += 2;
1261 else
1263 if (SYMBOLP (val))
1264 val = SYMBOL_NAME (val);
1265 if (j == XLFD_REGISTRY_INDEX
1266 && ! strchr ((char *) SDATA (val), '-'))
1268 /* Change "jisx0208*" and "jisx0208" to "jisx0208*-*". */
1269 if (SDATA (val)[SBYTES (val) - 1] == '*')
1271 f[j] = alloca (SBYTES (val) + 3);
1272 sprintf (f[j], "%s-*", SDATA (val));
1273 len += SBYTES (val) + 3;
1275 else
1277 f[j] = alloca (SBYTES (val) + 4);
1278 sprintf (f[j], "%s*-*", SDATA (val));
1279 len += SBYTES (val) + 4;
1282 else
1283 f[j] = (char *) SDATA (val), len += SBYTES (val) + 1;
1287 for (i = FONT_WEIGHT_INDEX, j = XLFD_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX;
1288 i++, j++)
1290 val = font_style_symbolic (font, i, 0);
1291 if (NILP (val))
1292 f[j] = "*", len += 2;
1293 else
1295 val = SYMBOL_NAME (val);
1296 f[j] = (char *) SDATA (val), len += SBYTES (val) + 1;
1300 val = AREF (font, FONT_SIZE_INDEX);
1301 font_assert (NUMBERP (val) || NILP (val));
1302 if (INTEGERP (val))
1304 i = XINT (val);
1305 if (i <= 0)
1306 i = pixel_size;
1307 if (i > 0)
1309 f[XLFD_PIXEL_INDEX] = alloca (22);
1310 len += sprintf (f[XLFD_PIXEL_INDEX], "%d-*", i) + 1;
1312 else
1313 f[XLFD_PIXEL_INDEX] = "*-*", len += 4;
1315 else if (FLOATP (val))
1317 i = XFLOAT_DATA (val) * 10;
1318 f[XLFD_PIXEL_INDEX] = alloca (12);
1319 len += sprintf (f[XLFD_PIXEL_INDEX], "*-%d", i) + 1;
1321 else
1322 f[XLFD_PIXEL_INDEX] = "*-*", len += 4;
1324 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1326 i = XINT (AREF (font, FONT_DPI_INDEX));
1327 f[XLFD_RESX_INDEX] = alloca (22);
1328 len += sprintf (f[XLFD_RESX_INDEX],
1329 "%d-%d", i, i) + 1;
1331 else
1332 f[XLFD_RESX_INDEX] = "*-*", len += 4;
1333 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1335 int spacing = XINT (AREF (font, FONT_SPACING_INDEX));
1337 f[XLFD_SPACING_INDEX] = (spacing <= FONT_SPACING_PROPORTIONAL ? "p"
1338 : spacing <= FONT_SPACING_DUAL ? "d"
1339 : spacing <= FONT_SPACING_MONO ? "m"
1340 : "c");
1341 len += 2;
1343 else
1344 f[XLFD_SPACING_INDEX] = "*", len += 2;
1345 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1347 f[XLFD_AVGWIDTH_INDEX] = alloca (11);
1348 len += sprintf (f[XLFD_AVGWIDTH_INDEX],
1349 "%d", XINT (AREF (font, FONT_AVGWIDTH_INDEX))) + 1;
1351 else
1352 f[XLFD_AVGWIDTH_INDEX] = "*", len += 2;
1353 len++; /* for terminating '\0'. */
1354 if (len >= nbytes)
1355 return -1;
1356 return sprintf (name, "-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s",
1357 f[XLFD_FOUNDRY_INDEX], f[XLFD_FAMILY_INDEX],
1358 f[XLFD_WEIGHT_INDEX], f[XLFD_SLANT_INDEX],
1359 f[XLFD_SWIDTH_INDEX], f[XLFD_ADSTYLE_INDEX],
1360 f[XLFD_PIXEL_INDEX], f[XLFD_RESX_INDEX],
1361 f[XLFD_SPACING_INDEX], f[XLFD_AVGWIDTH_INDEX],
1362 f[XLFD_REGISTRY_INDEX]);
1365 /* Parse NAME (null terminated) and store information in FONT
1366 (font-spec or font-entity). NAME is supplied in either the
1367 Fontconfig or GTK font name format. If NAME is successfully
1368 parsed, return 0. Otherwise return -1.
1370 The fontconfig format is
1372 FAMILY[-SIZE][:PROP1[=VAL1][:PROP2[=VAL2]...]]
1374 The GTK format is
1376 FAMILY [PROPS...] [SIZE]
1378 This function tries to guess which format it is. */
1381 font_parse_fcname (name, font)
1382 char *name;
1383 Lisp_Object font;
1385 char *p, *q;
1386 char *size_beg = NULL, *size_end = NULL;
1387 char *props_beg = NULL, *family_end = NULL;
1388 int len = strlen (name);
1390 if (len == 0)
1391 return -1;
1393 for (p = name; *p; p++)
1395 if (*p == '\\' && p[1])
1396 p++;
1397 else if (*p == ':')
1399 props_beg = family_end = p;
1400 break;
1402 else if (*p == '-')
1404 int decimal = 0, size_found = 1;
1405 for (q = p + 1; *q && *q != ':'; q++)
1406 if (! isdigit(*q))
1408 if (*q != '.' || decimal)
1410 size_found = 0;
1411 break;
1413 decimal = 1;
1415 if (size_found)
1417 family_end = p;
1418 size_beg = p + 1;
1419 size_end = q;
1420 break;
1425 if (family_end)
1427 /* A fontconfig name with size and/or property data. */
1428 if (family_end > name)
1430 Lisp_Object family;
1431 family = font_intern_prop (name, family_end - name, 1);
1432 ASET (font, FONT_FAMILY_INDEX, family);
1434 if (size_beg)
1436 double point_size = strtod (size_beg, &size_end);
1437 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1438 if (*size_end == ':' && size_end[1])
1439 props_beg = size_end;
1441 if (props_beg)
1443 /* Now parse ":KEY=VAL" patterns. */
1444 Lisp_Object val;
1446 for (p = props_beg; *p; p = q)
1448 for (q = p + 1; *q && *q != '=' && *q != ':'; q++);
1449 if (*q != '=')
1451 /* Must be an enumerated value. */
1452 int word_len;
1453 p = p + 1;
1454 word_len = q - p;
1455 val = font_intern_prop (p, q - p, 1);
1457 #define PROP_MATCH(STR,N) ((word_len == N) && memcmp (p, STR, N) == 0)
1459 if (PROP_MATCH ("light", 5)
1460 || PROP_MATCH ("medium", 6)
1461 || PROP_MATCH ("demibold", 8)
1462 || PROP_MATCH ("bold", 4)
1463 || PROP_MATCH ("black", 5))
1464 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, val);
1465 else if (PROP_MATCH ("roman", 5)
1466 || PROP_MATCH ("italic", 6)
1467 || PROP_MATCH ("oblique", 7))
1468 FONT_SET_STYLE (font, FONT_SLANT_INDEX, val);
1469 else if (PROP_MATCH ("charcell", 8))
1470 ASET (font, FONT_SPACING_INDEX,
1471 make_number (FONT_SPACING_CHARCELL));
1472 else if (PROP_MATCH ("mono", 4))
1473 ASET (font, FONT_SPACING_INDEX,
1474 make_number (FONT_SPACING_MONO));
1475 else if (PROP_MATCH ("proportional", 12))
1476 ASET (font, FONT_SPACING_INDEX,
1477 make_number (FONT_SPACING_PROPORTIONAL));
1478 #undef PROP_MATCH
1480 else
1482 /* KEY=VAL pairs */
1483 Lisp_Object key;
1484 int prop;
1486 if (q - p == 10 && memcmp (p + 1, "pixelsize", 9) == 0)
1487 prop = FONT_SIZE_INDEX;
1488 else
1490 key = font_intern_prop (p, q - p, 1);
1491 prop = get_font_prop_index (key);
1494 p = q + 1;
1495 for (q = p; *q && *q != ':'; q++);
1496 val = font_intern_prop (p, q - p, 0);
1498 if (prop >= FONT_FOUNDRY_INDEX
1499 && prop < FONT_EXTRA_INDEX)
1500 ASET (font, prop, font_prop_validate (prop, Qnil, val));
1501 else
1502 Ffont_put (font, key, val);
1504 p = q;
1508 else
1510 /* Either a fontconfig-style name with no size and property
1511 data, or a GTK-style name. */
1512 Lisp_Object prop;
1513 int word_len, prop_found = 0;
1515 for (p = name; *p; p = *q ? q + 1 : q)
1517 if (isdigit (*p))
1519 int size_found = 1;
1521 for (q = p + 1; *q && *q != ' '; q++)
1522 if (! isdigit (*q))
1524 size_found = 0;
1525 break;
1527 if (size_found)
1529 double point_size = strtod (p, &q);
1530 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1531 continue;
1535 for (q = p + 1; *q && *q != ' '; q++)
1536 if (*q == '\\' && q[1])
1537 q++;
1538 word_len = q - p;
1540 #define PROP_MATCH(STR,N) ((word_len == N) && memcmp (p, STR, N) == 0)
1542 if (PROP_MATCH ("Ultra-Light", 11))
1544 prop_found = 1;
1545 prop = font_intern_prop ("ultra-light", 11, 1);
1546 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
1548 else if (PROP_MATCH ("Light", 5))
1550 prop_found = 1;
1551 prop = font_intern_prop ("light", 5, 1);
1552 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
1554 else if (PROP_MATCH ("Semi-Bold", 9))
1556 prop_found = 1;
1557 prop = font_intern_prop ("semi-bold", 9, 1);
1558 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
1560 else if (PROP_MATCH ("Bold", 4))
1562 prop_found = 1;
1563 prop = font_intern_prop ("bold", 4, 1);
1564 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
1566 else if (PROP_MATCH ("Italic", 6))
1568 prop_found = 1;
1569 prop = font_intern_prop ("italic", 4, 1);
1570 FONT_SET_STYLE (font, FONT_SLANT_INDEX, prop);
1572 else if (PROP_MATCH ("Oblique", 7))
1574 prop_found = 1;
1575 prop = font_intern_prop ("oblique", 7, 1);
1576 FONT_SET_STYLE (font, FONT_SLANT_INDEX, prop);
1578 else {
1579 if (prop_found)
1580 return -1; /* Unknown property in GTK-style font name. */
1581 family_end = q;
1584 #undef PROP_MATCH
1586 if (family_end)
1588 Lisp_Object family;
1589 family = font_intern_prop (name, family_end - name, 1);
1590 ASET (font, FONT_FAMILY_INDEX, family);
1594 return 0;
1597 /* Store fontconfig's font name of FONT (font-spec or font-entity) in
1598 NAME (NBYTES length), and return the name length. If
1599 FONT_SIZE_INDEX of FONT is 0, use PIXEL_SIZE instead. */
1602 font_unparse_fcname (font, pixel_size, name, nbytes)
1603 Lisp_Object font;
1604 int pixel_size;
1605 char *name;
1606 int nbytes;
1608 Lisp_Object family, foundry;
1609 Lisp_Object tail, val;
1610 int point_size;
1611 int i, len = 1;
1612 char *p;
1613 Lisp_Object styles[3];
1614 char *style_names[3] = { "weight", "slant", "width" };
1615 char work[256];
1617 family = AREF (font, FONT_FAMILY_INDEX);
1618 if (! NILP (family))
1620 if (SYMBOLP (family))
1622 family = SYMBOL_NAME (family);
1623 len += SBYTES (family);
1625 else
1626 family = Qnil;
1629 val = AREF (font, FONT_SIZE_INDEX);
1630 if (INTEGERP (val))
1632 if (XINT (val) != 0)
1633 pixel_size = XINT (val);
1634 point_size = -1;
1635 len += 21; /* for ":pixelsize=NUM" */
1637 else if (FLOATP (val))
1639 pixel_size = -1;
1640 point_size = (int) XFLOAT_DATA (val);
1641 len += 11; /* for "-NUM" */
1644 foundry = AREF (font, FONT_FOUNDRY_INDEX);
1645 if (! NILP (foundry))
1647 if (SYMBOLP (foundry))
1649 foundry = SYMBOL_NAME (foundry);
1650 len += 9 + SBYTES (foundry); /* ":foundry=NAME" */
1652 else
1653 foundry = Qnil;
1656 for (i = 0; i < 3; i++)
1658 styles[i] = font_style_symbolic (font, FONT_WEIGHT_INDEX + i, 0);
1659 if (! NILP (styles[i]))
1660 len += sprintf (work, ":%s=%s", style_names[i],
1661 SDATA (SYMBOL_NAME (styles[i])));
1664 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1665 len += sprintf (work, ":dpi=%d", XINT (AREF (font, FONT_DPI_INDEX)));
1666 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1667 len += strlen (":spacing=100");
1668 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1669 len += strlen (":scalable=false"); /* or ":scalable=true" */
1670 for (tail = AREF (font, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail))
1672 Lisp_Object key = XCAR (XCAR (tail)), val = XCDR (XCAR (tail));
1674 len += SBYTES (SYMBOL_NAME (key)) + 1; /* for :KEY= */
1675 if (STRINGP (val))
1676 len += SBYTES (val);
1677 else if (INTEGERP (val))
1678 len += sprintf (work, "%d", XINT (val));
1679 else if (SYMBOLP (val))
1680 len += (NILP (val) ? 5 : 4); /* for "false" or "true" */
1683 if (len > nbytes)
1684 return -1;
1685 p = name;
1686 if (! NILP (family))
1687 p += sprintf (p, "%s", SDATA (family));
1688 if (point_size > 0)
1690 if (p == name)
1691 p += sprintf (p, "%d", point_size);
1692 else
1693 p += sprintf (p, "-%d", point_size);
1695 else if (pixel_size > 0)
1696 p += sprintf (p, ":pixelsize=%d", pixel_size);
1697 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
1698 p += sprintf (p, ":foundry=%s",
1699 SDATA (SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX))));
1700 for (i = 0; i < 3; i++)
1701 if (! NILP (styles[i]))
1702 p += sprintf (p, ":%s=%s", style_names[i],
1703 SDATA (SYMBOL_NAME (styles[i])));
1704 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1705 p += sprintf (p, ":dpi=%d", XINT (AREF (font, FONT_DPI_INDEX)));
1706 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1707 p += sprintf (p, ":spacing=%d", XINT (AREF (font, FONT_SPACING_INDEX)));
1708 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1710 if (XINT (AREF (font, FONT_AVGWIDTH_INDEX)) == 0)
1711 p += sprintf (p, ":scalable=true");
1712 else
1713 p += sprintf (p, ":scalable=false");
1715 return (p - name);
1718 /* Store GTK-style font name of FONT (font-spec or font-entity) in
1719 NAME (NBYTES length), and return the name length. F is the frame
1720 on which the font is displayed; it is used to calculate the point
1721 size. */
1724 font_unparse_gtkname (font, f, name, nbytes)
1725 Lisp_Object font;
1726 struct frame *f;
1727 char *name;
1728 int nbytes;
1730 char *p;
1731 int len = 1;
1732 Lisp_Object family, weight, slant, size;
1733 int point_size = -1;
1735 family = AREF (font, FONT_FAMILY_INDEX);
1736 if (! NILP (family))
1738 if (! SYMBOLP (family))
1739 return -1;
1740 family = SYMBOL_NAME (family);
1741 len += SBYTES (family);
1744 weight = font_style_symbolic (font, FONT_WEIGHT_INDEX, 0);
1745 if (EQ (weight, Qnormal))
1746 weight = Qnil;
1747 else if (! NILP (weight))
1749 weight = SYMBOL_NAME (weight);
1750 len += SBYTES (weight);
1753 slant = font_style_symbolic (font, FONT_SLANT_INDEX, 0);
1754 if (EQ (slant, Qnormal))
1755 slant = Qnil;
1756 else if (! NILP (slant))
1758 slant = SYMBOL_NAME (slant);
1759 len += SBYTES (slant);
1762 size = AREF (font, FONT_SIZE_INDEX);
1763 /* Convert pixel size to point size. */
1764 if (INTEGERP (size))
1766 Lisp_Object font_dpi = AREF (font, FONT_DPI_INDEX);
1767 int dpi = 75;
1768 if (INTEGERP (font_dpi))
1769 dpi = XINT (font_dpi);
1770 else if (f)
1771 dpi = f->resy;
1772 point_size = PIXEL_TO_POINT (XINT (size), dpi);
1773 len += 11;
1775 else if (FLOATP (size))
1777 point_size = (int) XFLOAT_DATA (size);
1778 len += 11;
1781 if (len > nbytes)
1782 return -1;
1784 p = name + sprintf (name, "%s", SDATA (family));
1786 if (! NILP (weight))
1788 char *q = p;
1789 p += sprintf (p, " %s", SDATA (weight));
1790 q[1] = toupper (q[1]);
1793 if (! NILP (slant))
1795 char *q = p;
1796 p += sprintf (p, " %s", SDATA (slant));
1797 q[1] = toupper (q[1]);
1800 if (point_size > 0)
1801 p += sprintf (p, " %d", point_size);
1803 return (p - name);
1806 /* Parse NAME (null terminated) and store information in FONT
1807 (font-spec or font-entity). If NAME is successfully parsed, return
1808 0. Otherwise return -1. */
1810 static int
1811 font_parse_name (name, font)
1812 char *name;
1813 Lisp_Object font;
1815 if (name[0] == '-' || index (name, '*') || index (name, '?'))
1816 return font_parse_xlfd (name, font);
1817 return font_parse_fcname (name, font);
1821 /* Merge FAMILY and REGISTRY into FONT_SPEC. FAMILY may have the form
1822 "FAMILY-FOUNDRY". REGISTRY may not contain charset-encoding
1823 part. */
1825 void
1826 font_parse_family_registry (family, registry, font_spec)
1827 Lisp_Object family, registry, font_spec;
1829 int len;
1830 char *p0, *p1;
1832 if (! NILP (family)
1833 && NILP (AREF (font_spec, FONT_FAMILY_INDEX)))
1835 CHECK_STRING (family);
1836 len = SBYTES (family);
1837 p0 = (char *) SDATA (family);
1838 p1 = index (p0, '-');
1839 if (p1)
1841 if ((*p0 != '*' || p1 - p0 > 1)
1842 && NILP (AREF (font_spec, FONT_FOUNDRY_INDEX)))
1843 Ffont_put (font_spec, QCfoundry, font_intern_prop (p0, p1 - p0, 1));
1844 p1++;
1845 len -= p1 - p0;
1846 Ffont_put (font_spec, QCfamily, font_intern_prop (p1, len, 1));
1848 else
1849 ASET (font_spec, FONT_FAMILY_INDEX, Fintern (family, Qnil));
1851 if (! NILP (registry))
1853 /* Convert "XXX" and "XXX*" to "XXX*-*". */
1854 CHECK_STRING (registry);
1855 len = SBYTES (registry);
1856 p0 = (char *) SDATA (registry);
1857 p1 = index (p0, '-');
1858 if (! p1)
1860 if (SDATA (registry)[len - 1] == '*')
1861 registry = concat2 (registry, build_string ("-*"));
1862 else
1863 registry = concat2 (registry, build_string ("*-*"));
1865 registry = Fdowncase (registry);
1866 ASET (font_spec, FONT_REGISTRY_INDEX, Fintern (registry, Qnil));
1871 /* This part (through the next ^L) is still experimental and not
1872 tested much. We may drastically change codes. */
1874 /* OTF handler */
1876 #if 0
1878 #define LGSTRING_HEADER_SIZE 6
1879 #define LGSTRING_GLYPH_SIZE 8
1881 static int
1882 check_gstring (gstring)
1883 Lisp_Object gstring;
1885 Lisp_Object val;
1886 int i, j;
1888 CHECK_VECTOR (gstring);
1889 val = AREF (gstring, 0);
1890 CHECK_VECTOR (val);
1891 if (ASIZE (val) < LGSTRING_HEADER_SIZE)
1892 goto err;
1893 CHECK_FONT_OBJECT (LGSTRING_FONT (gstring));
1894 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_LBEARING)))
1895 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_LBEARING));
1896 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_RBEARING)))
1897 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_RBEARING));
1898 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_WIDTH)))
1899 CHECK_NATNUM (LGSTRING_SLOT (gstring, LGSTRING_IX_WIDTH));
1900 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT)))
1901 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT));
1902 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT)))
1903 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT));
1905 for (i = 0; i < LGSTRING_GLYPH_LEN (gstring); i++)
1907 val = LGSTRING_GLYPH (gstring, i);
1908 CHECK_VECTOR (val);
1909 if (ASIZE (val) < LGSTRING_GLYPH_SIZE)
1910 goto err;
1911 if (NILP (AREF (val, LGLYPH_IX_CHAR)))
1912 break;
1913 CHECK_NATNUM (AREF (val, LGLYPH_IX_FROM));
1914 CHECK_NATNUM (AREF (val, LGLYPH_IX_TO));
1915 CHECK_CHARACTER (AREF (val, LGLYPH_IX_CHAR));
1916 if (!NILP (AREF (val, LGLYPH_IX_CODE)))
1917 CHECK_NATNUM (AREF (val, LGLYPH_IX_CODE));
1918 if (!NILP (AREF (val, LGLYPH_IX_WIDTH)))
1919 CHECK_NATNUM (AREF (val, LGLYPH_IX_WIDTH));
1920 if (!NILP (AREF (val, LGLYPH_IX_ADJUSTMENT)))
1922 val = AREF (val, LGLYPH_IX_ADJUSTMENT);
1923 CHECK_VECTOR (val);
1924 if (ASIZE (val) < 3)
1925 goto err;
1926 for (j = 0; j < 3; j++)
1927 CHECK_NUMBER (AREF (val, j));
1930 return i;
1931 err:
1932 error ("Invalid glyph-string format");
1933 return -1;
1936 static void
1937 check_otf_features (otf_features)
1938 Lisp_Object otf_features;
1940 Lisp_Object val;
1942 CHECK_CONS (otf_features);
1943 CHECK_SYMBOL (XCAR (otf_features));
1944 otf_features = XCDR (otf_features);
1945 CHECK_CONS (otf_features);
1946 CHECK_SYMBOL (XCAR (otf_features));
1947 otf_features = XCDR (otf_features);
1948 for (val = Fcar (otf_features); ! NILP (val); val = Fcdr (val))
1950 CHECK_SYMBOL (Fcar (val));
1951 if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
1952 error ("Invalid OTF GSUB feature: %s", SYMBOL_NAME (XCAR (val)));
1954 otf_features = XCDR (otf_features);
1955 for (val = Fcar (otf_features); ! NILP (val); val = Fcdr (val))
1957 CHECK_SYMBOL (Fcar (val));
1958 if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
1959 error ("Invalid OTF GPOS feature: %s", SYMBOL_NAME (XCAR (val)));
1963 #ifdef HAVE_LIBOTF
1964 #include <otf.h>
1966 Lisp_Object otf_list;
1968 static Lisp_Object
1969 otf_tag_symbol (tag)
1970 OTF_Tag tag;
1972 char name[5];
1974 OTF_tag_name (tag, name);
1975 return Fintern (make_unibyte_string (name, 4), Qnil);
1978 static OTF *
1979 otf_open (file)
1980 Lisp_Object file;
1982 Lisp_Object val = Fassoc (file, otf_list);
1983 OTF *otf;
1985 if (! NILP (val))
1986 otf = XSAVE_VALUE (XCDR (val))->pointer;
1987 else
1989 otf = STRINGP (file) ? OTF_open ((char *) SDATA (file)) : NULL;
1990 val = make_save_value (otf, 0);
1991 otf_list = Fcons (Fcons (file, val), otf_list);
1993 return otf;
1997 /* Return a list describing which scripts/languages FONT supports by
1998 which GSUB/GPOS features of OpenType tables. See the comment of
1999 (struct font_driver).otf_capability. */
2001 Lisp_Object
2002 font_otf_capability (font)
2003 struct font *font;
2005 OTF *otf;
2006 Lisp_Object capability = Fcons (Qnil, Qnil);
2007 int i;
2009 otf = otf_open (font->props[FONT_FILE_INDEX]);
2010 if (! otf)
2011 return Qnil;
2012 for (i = 0; i < 2; i++)
2014 OTF_GSUB_GPOS *gsub_gpos;
2015 Lisp_Object script_list = Qnil;
2016 int j;
2018 if (OTF_get_features (otf, i == 0) < 0)
2019 continue;
2020 gsub_gpos = i == 0 ? otf->gsub : otf->gpos;
2021 for (j = gsub_gpos->ScriptList.ScriptCount - 1; j >= 0; j--)
2023 OTF_Script *script = gsub_gpos->ScriptList.Script + j;
2024 Lisp_Object langsys_list = Qnil;
2025 Lisp_Object script_tag = otf_tag_symbol (script->ScriptTag);
2026 int k;
2028 for (k = script->LangSysCount; k >= 0; k--)
2030 OTF_LangSys *langsys;
2031 Lisp_Object feature_list = Qnil;
2032 Lisp_Object langsys_tag;
2033 int l;
2035 if (k == script->LangSysCount)
2037 langsys = &script->DefaultLangSys;
2038 langsys_tag = Qnil;
2040 else
2042 langsys = script->LangSys + k;
2043 langsys_tag
2044 = otf_tag_symbol (script->LangSysRecord[k].LangSysTag);
2046 for (l = langsys->FeatureCount - 1; l >= 0; l--)
2048 OTF_Feature *feature
2049 = gsub_gpos->FeatureList.Feature + langsys->FeatureIndex[l];
2050 Lisp_Object feature_tag
2051 = otf_tag_symbol (feature->FeatureTag);
2053 feature_list = Fcons (feature_tag, feature_list);
2055 langsys_list = Fcons (Fcons (langsys_tag, feature_list),
2056 langsys_list);
2058 script_list = Fcons (Fcons (script_tag, langsys_list),
2059 script_list);
2062 if (i == 0)
2063 XSETCAR (capability, script_list);
2064 else
2065 XSETCDR (capability, script_list);
2068 return capability;
2071 /* Parse OTF features in SPEC and write a proper features spec string
2072 in FEATURES for the call of OTF_drive_gsub/gpos (of libotf). It is
2073 assured that the sufficient memory has already allocated for
2074 FEATURES. */
2076 static void
2077 generate_otf_features (spec, features)
2078 Lisp_Object spec;
2079 char *features;
2081 Lisp_Object val;
2082 char *p;
2083 int asterisk;
2085 p = features;
2086 *p = '\0';
2087 for (asterisk = 0; CONSP (spec); spec = XCDR (spec))
2089 val = XCAR (spec);
2090 CHECK_SYMBOL (val);
2091 if (p > features)
2092 *p++ = ',';
2093 if (SREF (SYMBOL_NAME (val), 0) == '*')
2095 asterisk = 1;
2096 *p++ = '*';
2098 else if (! asterisk)
2100 val = SYMBOL_NAME (val);
2101 p += sprintf (p, "%s", SDATA (val));
2103 else
2105 val = SYMBOL_NAME (val);
2106 p += sprintf (p, "~%s", SDATA (val));
2109 if (CONSP (spec))
2110 error ("OTF spec too long");
2113 Lisp_Object
2114 font_otf_DeviceTable (device_table)
2115 OTF_DeviceTable *device_table;
2117 int len = device_table->StartSize - device_table->EndSize + 1;
2119 return Fcons (make_number (len),
2120 make_unibyte_string (device_table->DeltaValue, len));
2123 Lisp_Object
2124 font_otf_ValueRecord (value_format, value_record)
2125 int value_format;
2126 OTF_ValueRecord *value_record;
2128 Lisp_Object val = Fmake_vector (make_number (8), Qnil);
2130 if (value_format & OTF_XPlacement)
2131 ASET (val, 0, make_number (value_record->XPlacement));
2132 if (value_format & OTF_YPlacement)
2133 ASET (val, 1, make_number (value_record->YPlacement));
2134 if (value_format & OTF_XAdvance)
2135 ASET (val, 2, make_number (value_record->XAdvance));
2136 if (value_format & OTF_YAdvance)
2137 ASET (val, 3, make_number (value_record->YAdvance));
2138 if (value_format & OTF_XPlaDevice)
2139 ASET (val, 4, font_otf_DeviceTable (&value_record->XPlaDevice));
2140 if (value_format & OTF_YPlaDevice)
2141 ASET (val, 4, font_otf_DeviceTable (&value_record->YPlaDevice));
2142 if (value_format & OTF_XAdvDevice)
2143 ASET (val, 4, font_otf_DeviceTable (&value_record->XAdvDevice));
2144 if (value_format & OTF_YAdvDevice)
2145 ASET (val, 4, font_otf_DeviceTable (&value_record->YAdvDevice));
2146 return val;
2149 Lisp_Object
2150 font_otf_Anchor (anchor)
2151 OTF_Anchor *anchor;
2153 Lisp_Object val;
2155 val = Fmake_vector (make_number (anchor->AnchorFormat + 1), Qnil);
2156 ASET (val, 0, make_number (anchor->XCoordinate));
2157 ASET (val, 1, make_number (anchor->YCoordinate));
2158 if (anchor->AnchorFormat == 2)
2159 ASET (val, 2, make_number (anchor->f.f1.AnchorPoint));
2160 else
2162 ASET (val, 3, font_otf_DeviceTable (&anchor->f.f2.XDeviceTable));
2163 ASET (val, 4, font_otf_DeviceTable (&anchor->f.f2.YDeviceTable));
2165 return val;
2167 #endif /* HAVE_LIBOTF */
2168 #endif /* 0 */
2171 /* Font sorting */
2173 static unsigned font_score P_ ((Lisp_Object, Lisp_Object *));
2174 static int font_compare P_ ((const void *, const void *));
2175 static Lisp_Object font_sort_entites P_ ((Lisp_Object, Lisp_Object,
2176 Lisp_Object, int));
2178 /* Return a rescaling ratio of FONT_ENTITY. */
2179 extern Lisp_Object Vface_font_rescale_alist;
2181 static double
2182 font_rescale_ratio (font_entity)
2183 Lisp_Object font_entity;
2185 Lisp_Object tail, elt;
2186 Lisp_Object name = Qnil;
2188 for (tail = Vface_font_rescale_alist; CONSP (tail); tail = XCDR (tail))
2190 elt = XCAR (tail);
2191 if (FLOATP (XCDR (elt)))
2193 if (STRINGP (XCAR (elt)))
2195 if (NILP (name))
2196 name = Ffont_xlfd_name (font_entity, Qnil);
2197 if (fast_string_match_ignore_case (XCAR (elt), name) >= 0)
2198 return XFLOAT_DATA (XCDR (elt));
2200 else if (FONT_SPEC_P (XCAR (elt)))
2202 if (font_match_p (XCAR (elt), font_entity))
2203 return XFLOAT_DATA (XCDR (elt));
2207 return 1.0;
2210 /* We sort fonts by scoring each of them against a specified
2211 font-spec. The score value is 32 bit (`unsigned'), and the smaller
2212 the value is, the closer the font is to the font-spec.
2214 The lowest 2 bits of the score is used for driver type. The font
2215 available by the most preferred font driver is 0.
2217 Each 7-bit in the higher 28 bits are used for numeric properties
2218 WEIGHT, SLANT, WIDTH, and SIZE. */
2220 /* How many bits to shift to store the difference value of each font
2221 property in a score. Note that flots for FONT_TYPE_INDEX and
2222 FONT_REGISTRY_INDEX are not used. */
2223 static int sort_shift_bits[FONT_SIZE_INDEX + 1];
2225 /* Score font-entity ENTITY against properties of font-spec SPEC_PROP.
2226 The return value indicates how different ENTITY is compared with
2227 SPEC_PROP. */
2229 static unsigned
2230 font_score (entity, spec_prop)
2231 Lisp_Object entity, *spec_prop;
2233 unsigned score = 0;
2234 int i;
2236 /* Score three style numeric fields. Maximum difference is 127. */
2237 for (i = FONT_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX; i++)
2238 if (! NILP (spec_prop[i]) && ! EQ (AREF (entity, i), spec_prop[i]))
2240 int diff = (XINT (AREF (entity, i)) >> 8) - (XINT (spec_prop[i]) >> 8);
2242 if (diff < 0)
2243 diff = - diff;
2244 if (diff > 0)
2245 score |= min (diff, 127) << sort_shift_bits[i];
2248 /* Score the size. Maximum difference is 127. */
2249 i = FONT_SIZE_INDEX;
2250 if (! NILP (spec_prop[FONT_SIZE_INDEX])
2251 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
2253 /* We use the higher 6-bit for the actual size difference. The
2254 lowest bit is set if the DPI is different. */
2255 int diff;
2256 int pixel_size = XINT (spec_prop[FONT_SIZE_INDEX]);
2258 if (CONSP (Vface_font_rescale_alist))
2259 pixel_size *= font_rescale_ratio (entity);
2260 diff = pixel_size - XINT (AREF (entity, FONT_SIZE_INDEX));
2261 if (diff < 0)
2262 diff = - diff;
2263 diff <<= 1;
2264 if (! NILP (spec_prop[FONT_DPI_INDEX])
2265 && ! EQ (spec_prop[FONT_DPI_INDEX], AREF (entity, FONT_DPI_INDEX)))
2266 diff |= 1;
2267 score |= min (diff, 127) << sort_shift_bits[FONT_SIZE_INDEX];
2270 return score;
2274 /* The comparison function for qsort. */
2276 static int
2277 font_compare (d1, d2)
2278 const void *d1, *d2;
2280 return (*(unsigned *) d1 - *(unsigned *) d2);
2284 /* The structure for elements being sorted by qsort. */
2285 struct font_sort_data
2287 unsigned score;
2288 Lisp_Object entity;
2292 /* Sort font-entities in vector VEC by closeness to font-spec PREFER.
2293 If PREFER specifies a point-size, calculate the corresponding
2294 pixel-size from QCdpi property of PREFER or from the Y-resolution
2295 of FRAME before sorting.
2297 If BEST-ONLY is nonzero, return the best matching entity. Otherwise,
2298 return the sorted VEC. */
2300 static Lisp_Object
2301 font_sort_entites (vec, prefer, frame, best_only)
2302 Lisp_Object vec, prefer, frame;
2303 int best_only;
2305 Lisp_Object prefer_prop[FONT_SPEC_MAX];
2306 int len, i;
2307 struct font_sort_data *data;
2308 unsigned best_score;
2309 Lisp_Object best_entity, driver_type;
2310 int driver_order;
2311 struct frame *f = XFRAME (frame);
2312 struct font_driver_list *list;
2313 USE_SAFE_ALLOCA;
2315 len = ASIZE (vec);
2316 if (len <= 1)
2317 return best_only ? AREF (vec, 0) : vec;
2319 for (i = FONT_WEIGHT_INDEX; i <= FONT_DPI_INDEX; i++)
2320 prefer_prop[i] = AREF (prefer, i);
2321 if (FLOATP (prefer_prop[FONT_SIZE_INDEX]))
2322 prefer_prop[FONT_SIZE_INDEX]
2323 = make_number (font_pixel_size (XFRAME (frame), prefer));
2325 /* Scoring and sorting. */
2326 SAFE_ALLOCA (data, struct font_sort_data *, (sizeof *data) * len);
2327 best_score = 0xFFFFFFFF;
2328 /* We are sure that the length of VEC > 1. */
2329 driver_type = AREF (AREF (vec, 0), FONT_TYPE_INDEX);
2330 for (driver_order = 0, list = f->font_driver_list; list;
2331 driver_order++, list = list->next)
2332 if (EQ (driver_type, list->driver->type))
2333 break;
2334 best_entity = data[0].entity = AREF (vec, 0);
2335 best_score = data[0].score
2336 = font_score (data[0].entity, prefer_prop) | driver_order;
2337 for (i = 0; i < len; i++)
2339 if (!EQ (driver_type, AREF (AREF (vec, i), FONT_TYPE_INDEX)))
2340 for (driver_order = 0, list = f->font_driver_list; list;
2341 driver_order++, list = list->next)
2342 if (EQ (driver_type, list->driver->type))
2343 break;
2344 data[i].entity = AREF (vec, i);
2345 data[i].score = font_score (data[i].entity, prefer_prop) | driver_order;
2346 if (best_only && best_score > data[i].score)
2348 best_score = data[i].score;
2349 best_entity = data[i].entity;
2350 if (best_score == 0)
2351 break;
2354 if (! best_only)
2356 qsort (data, len, sizeof *data, font_compare);
2357 for (i = 0; i < len; i++)
2358 ASET (vec, i, data[i].entity);
2360 else
2361 vec = best_entity;
2362 SAFE_FREE ();
2364 font_add_log ("sort-by", prefer, vec);
2365 return vec;
2369 /* API of Font Service Layer. */
2371 /* Reflect ORDER (see the variable font_sort_order in xfaces.c) to
2372 sort_shift_bits. Finternal_set_font_selection_order calls this
2373 function with font_sort_order after setting up it. */
2375 void
2376 font_update_sort_order (order)
2377 int *order;
2379 int i, shift_bits;
2381 for (i = 0, shift_bits = 23; i < 4; i++, shift_bits -= 7)
2383 int xlfd_idx = order[i];
2385 if (xlfd_idx == XLFD_WEIGHT_INDEX)
2386 sort_shift_bits[FONT_WEIGHT_INDEX] = shift_bits;
2387 else if (xlfd_idx == XLFD_SLANT_INDEX)
2388 sort_shift_bits[FONT_SLANT_INDEX] = shift_bits;
2389 else if (xlfd_idx == XLFD_SWIDTH_INDEX)
2390 sort_shift_bits[FONT_WIDTH_INDEX] = shift_bits;
2391 else
2392 sort_shift_bits[FONT_SIZE_INDEX] = shift_bits;
2396 static int
2397 font_check_otf_features (script, langsys, features, table)
2398 Lisp_Object script, langsys, features, table;
2400 Lisp_Object val;
2401 int negative;
2403 table = assq_no_quit (script, table);
2404 if (NILP (table))
2405 return 0;
2406 table = XCDR (table);
2407 if (! NILP (langsys))
2409 table = assq_no_quit (langsys, table);
2410 if (NILP (table))
2411 return 0;
2413 else
2415 val = assq_no_quit (Qnil, table);
2416 if (NILP (val))
2417 table = XCAR (table);
2418 else
2419 table = val;
2421 table = XCDR (table);
2422 for (negative = 0; CONSP (features); features = XCDR (features))
2424 if (NILP (XCAR (features)))
2426 negative = 1;
2427 continue;
2429 if (NILP (Fmemq (XCAR (features), table)) != negative)
2430 return 0;
2432 return 1;
2435 /* Check if OTF_CAPABILITY satisfies SPEC (otf-spec). */
2437 static int
2438 font_check_otf (Lisp_Object spec, Lisp_Object otf_capability)
2440 Lisp_Object script, langsys = Qnil, gsub = Qnil, gpos = Qnil;
2442 script = XCAR (spec);
2443 spec = XCDR (spec);
2444 if (! NILP (spec))
2446 langsys = XCAR (spec);
2447 spec = XCDR (spec);
2448 if (! NILP (spec))
2450 gsub = XCAR (spec);
2451 spec = XCDR (spec);
2452 if (! NILP (spec))
2453 gpos = XCAR (spec);
2457 if (! NILP (gsub) && ! font_check_otf_features (script, langsys, gsub,
2458 XCAR (otf_capability)))
2459 return 0;
2460 if (! NILP (gpos) && ! font_check_otf_features (script, langsys, gpos,
2461 XCDR (otf_capability)))
2462 return 0;
2463 return 1;
2468 /* Check if FONT (font-entity or font-object) matches with the font
2469 specification SPEC. */
2472 font_match_p (spec, font)
2473 Lisp_Object spec, font;
2475 Lisp_Object prop[FONT_SPEC_MAX], *props;
2476 Lisp_Object extra, font_extra;
2477 int i;
2479 for (i = FONT_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX; i++)
2480 if (! NILP (AREF (spec, i))
2481 && ! NILP (AREF (font, i))
2482 && ! EQ (AREF (spec, i), AREF (font, i)))
2483 return 0;
2484 props = XFONT_SPEC (spec)->props;
2485 if (FLOATP (props[FONT_SIZE_INDEX]))
2487 for (i = FONT_FOUNDRY_INDEX; i < FONT_SIZE_INDEX; i++)
2488 prop[i] = AREF (spec, i);
2489 prop[FONT_SIZE_INDEX]
2490 = make_number (font_pixel_size (XFRAME (selected_frame), spec));
2491 props = prop;
2494 if (font_score (font, props) > 0)
2495 return 0;
2496 extra = AREF (spec, FONT_EXTRA_INDEX);
2497 font_extra = AREF (font, FONT_EXTRA_INDEX);
2498 for (; CONSP (extra); extra = XCDR (extra))
2500 Lisp_Object key = XCAR (XCAR (extra));
2501 Lisp_Object val = XCDR (XCAR (extra)), val2;
2503 if (EQ (key, QClang))
2505 val2 = assq_no_quit (key, font_extra);
2506 if (NILP (val2))
2507 return 0;
2508 val2 = XCDR (val2);
2509 if (CONSP (val))
2511 if (! CONSP (val2))
2512 return 0;
2513 while (CONSP (val))
2514 if (NILP (Fmemq (val, val2)))
2515 return 0;
2517 else
2518 if (CONSP (val2)
2519 ? NILP (Fmemq (val, XCDR (val2)))
2520 : ! EQ (val, val2))
2521 return 0;
2523 else if (EQ (key, QCscript))
2525 val2 = assq_no_quit (val, Vscript_representative_chars);
2526 if (CONSP (val2))
2528 val2 = XCDR (val2);
2529 if (CONSP (val2))
2531 /* All characters in the list must be supported. */
2532 for (; CONSP (val2); val2 = XCDR (val2))
2534 if (! NATNUMP (XCAR (val2)))
2535 continue;
2536 if (font_encode_char (font, XFASTINT (XCAR (val2)))
2537 == FONT_INVALID_CODE)
2538 return 0;
2541 else if (VECTORP (val2))
2543 /* At most one character in the vector must be supported. */
2544 for (i = 0; i < ASIZE (val2); i++)
2546 if (! NATNUMP (AREF (val2, i)))
2547 continue;
2548 if (font_encode_char (font, XFASTINT (AREF (val2, i)))
2549 != FONT_INVALID_CODE)
2550 break;
2552 if (i == ASIZE (val2))
2553 return 0;
2557 else if (EQ (key, QCotf))
2559 struct font *fontp;
2561 if (! FONT_OBJECT_P (font))
2562 return 0;
2563 fontp = XFONT_OBJECT (font);
2564 if (! fontp->driver->otf_capability)
2565 return 0;
2566 val2 = fontp->driver->otf_capability (fontp);
2567 if (NILP (val2) || ! font_check_otf (val, val2))
2568 return 0;
2572 return 1;
2576 /* Font cache
2578 Each font backend has the callback function get_cache, and it
2579 returns a cons cell of which cdr part can be freely used for
2580 caching fonts. The cons cell may be shared by multiple frames
2581 and/or multiple font drivers. So, we arrange the cdr part as this:
2583 ((DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) ...)
2585 where DRIVER-TYPE is a symbol such as `x', `xft', etc., NUM-FRAMES
2586 is a number frames sharing this cache, and FONT-CACHE-DATA is a
2587 cons (FONT-SPEC FONT-ENTITY ...). */
2589 static void font_prepare_cache P_ ((FRAME_PTR, struct font_driver *));
2590 static void font_finish_cache P_ ((FRAME_PTR, struct font_driver *));
2591 static Lisp_Object font_get_cache P_ ((FRAME_PTR, struct font_driver *));
2592 static void font_clear_cache P_ ((FRAME_PTR, Lisp_Object,
2593 struct font_driver *));
2595 static void
2596 font_prepare_cache (f, driver)
2597 FRAME_PTR f;
2598 struct font_driver *driver;
2600 Lisp_Object cache, val;
2602 cache = driver->get_cache (f);
2603 val = XCDR (cache);
2604 while (CONSP (val) && ! EQ (XCAR (XCAR (val)), driver->type))
2605 val = XCDR (val);
2606 if (NILP (val))
2608 val = Fcons (driver->type, Fcons (make_number (1), Qnil));
2609 XSETCDR (cache, Fcons (val, XCDR (cache)));
2611 else
2613 val = XCDR (XCAR (val));
2614 XSETCAR (val, make_number (XINT (XCAR (val)) + 1));
2619 static void
2620 font_finish_cache (f, driver)
2621 FRAME_PTR f;
2622 struct font_driver *driver;
2624 Lisp_Object cache, val, tmp;
2627 cache = driver->get_cache (f);
2628 val = XCDR (cache);
2629 while (CONSP (val) && ! EQ (XCAR (XCAR (val)), driver->type))
2630 cache = val, val = XCDR (val);
2631 font_assert (! NILP (val));
2632 tmp = XCDR (XCAR (val));
2633 XSETCAR (tmp, make_number (XINT (XCAR (tmp)) - 1));
2634 if (XINT (XCAR (tmp)) == 0)
2636 font_clear_cache (f, XCAR (val), driver);
2637 XSETCDR (cache, XCDR (val));
2642 static Lisp_Object
2643 font_get_cache (f, driver)
2644 FRAME_PTR f;
2645 struct font_driver *driver;
2647 Lisp_Object val = driver->get_cache (f);
2648 Lisp_Object type = driver->type;
2650 font_assert (CONSP (val));
2651 for (val = XCDR (val); ! EQ (XCAR (XCAR (val)), type); val = XCDR (val));
2652 font_assert (CONSP (val));
2653 /* VAL = ((DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) ...) */
2654 val = XCDR (XCAR (val));
2655 return val;
2658 static int num_fonts;
2660 static void
2661 font_clear_cache (f, cache, driver)
2662 FRAME_PTR f;
2663 Lisp_Object cache;
2664 struct font_driver *driver;
2666 Lisp_Object tail, elt;
2667 Lisp_Object tail2, entity;
2669 /* CACHE = (DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) */
2670 for (tail = XCDR (XCDR (cache)); CONSP (tail); tail = XCDR (tail))
2672 elt = XCAR (tail);
2673 /* elt should have the form (FONT-SPEC FONT-ENTITY ...) */
2674 if (CONSP (elt) && FONT_SPEC_P (XCAR (elt)))
2676 for (tail2 = XCDR (elt); CONSP (tail2); tail2 = XCDR (tail2))
2678 entity = XCAR (tail2);
2680 if (FONT_ENTITY_P (entity)
2681 && EQ (driver->type, AREF (entity, FONT_TYPE_INDEX)))
2683 Lisp_Object objlist = AREF (entity, FONT_OBJLIST_INDEX);
2685 for (; CONSP (objlist); objlist = XCDR (objlist))
2687 Lisp_Object val = XCAR (objlist);
2688 struct font *font = XFONT_OBJECT (val);
2690 if (! NILP (AREF (val, FONT_TYPE_INDEX)))
2692 font_assert (font && driver == font->driver);
2693 driver->close (f, font);
2694 num_fonts--;
2697 if (driver->free_entity)
2698 driver->free_entity (entity);
2703 XSETCDR (cache, Qnil);
2707 static Lisp_Object scratch_font_spec, scratch_font_prefer;
2709 Lisp_Object
2710 font_delete_unmatched (list, spec, size)
2711 Lisp_Object list, spec;
2712 int size;
2714 Lisp_Object entity, val;
2715 enum font_property_index prop;
2717 for (val = Qnil; CONSP (list); list = XCDR (list))
2719 entity = XCAR (list);
2720 for (prop = FONT_WEIGHT_INDEX; prop < FONT_SIZE_INDEX; prop++)
2721 if (INTEGERP (AREF (spec, prop))
2722 && ((XINT (AREF (spec, prop)) >> 8)
2723 != (XINT (AREF (entity, prop)) >> 8)))
2724 prop = FONT_SPEC_MAX;
2725 if (prop < FONT_SPEC_MAX
2726 && size
2727 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
2729 int diff = XINT (AREF (entity, FONT_SIZE_INDEX)) - size;
2731 if (diff != 0
2732 && (diff < 0 ? -diff > FONT_PIXEL_SIZE_QUANTUM
2733 : 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 val;
2756 /* Return a vector of font-entities matching with SPEC on FRAME. */
2758 Lisp_Object
2759 font_list_entities (frame, spec)
2760 Lisp_Object frame, spec;
2762 FRAME_PTR f = XFRAME (frame);
2763 struct font_driver_list *driver_list = f->font_driver_list;
2764 Lisp_Object ftype, val;
2765 Lisp_Object *vec;
2766 int size;
2767 int need_filtering = 0;
2768 int i;
2770 font_assert (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++)
2784 ASET (scratch_font_spec, i, Qnil);
2785 if (! NILP (AREF (spec, i)))
2786 need_filtering = 1;
2787 if (i == FONT_DPI_INDEX)
2788 /* Skip FONT_SPACING_INDEX */
2789 i++;
2791 ASET (scratch_font_spec, FONT_SPACING_INDEX, AREF (spec, FONT_SPACING_INDEX));
2792 ASET (scratch_font_spec, FONT_EXTRA_INDEX, AREF (spec, FONT_EXTRA_INDEX));
2794 vec = alloca (sizeof (Lisp_Object) * num_font_drivers);
2795 if (! vec)
2796 return null_vector;
2798 for (i = 0; driver_list; driver_list = driver_list->next)
2799 if (driver_list->on
2800 && (NILP (ftype) || EQ (driver_list->driver->type, ftype)))
2802 Lisp_Object cache = font_get_cache (f, driver_list->driver);
2804 ASET (scratch_font_spec, FONT_TYPE_INDEX, driver_list->driver->type);
2805 val = assoc_no_quit (scratch_font_spec, XCDR (cache));
2806 if (CONSP (val))
2807 val = XCDR (val);
2808 else
2810 Lisp_Object copy;
2812 val = driver_list->driver->list (frame, scratch_font_spec);
2813 copy = Fcopy_font_spec (scratch_font_spec);
2814 ASET (copy, FONT_TYPE_INDEX, driver_list->driver->type);
2815 XSETCDR (cache, Fcons (Fcons (copy, val), XCDR (cache)));
2817 if (! NILP (val) && need_filtering)
2818 val = font_delete_unmatched (val, spec, size);
2819 if (! NILP (val))
2820 vec[i++] = val;
2823 val = (i > 0 ? Fvconcat (i, vec) : null_vector);
2824 font_add_log ("list", spec, val);
2825 return (val);
2829 /* Return a font entity matching with SPEC on FRAME. ATTRS, if non
2830 nil, is an array of face's attributes, which specifies preferred
2831 font-related attributes. */
2833 static Lisp_Object
2834 font_matching_entity (f, attrs, spec)
2835 FRAME_PTR f;
2836 Lisp_Object *attrs, spec;
2838 struct font_driver_list *driver_list = f->font_driver_list;
2839 Lisp_Object ftype, size, entity;
2840 Lisp_Object frame;
2841 Lisp_Object work = Fcopy_font_spec (spec);
2843 XSETFRAME (frame, f);
2844 ftype = AREF (spec, FONT_TYPE_INDEX);
2845 size = AREF (spec, FONT_SIZE_INDEX);
2847 if (FLOATP (size))
2848 ASET (work, FONT_SIZE_INDEX, make_number (font_pixel_size (f, spec)));
2849 FONT_SET_STYLE (work, FONT_WEIGHT_INDEX, attrs[LFACE_WEIGHT_INDEX]);
2850 FONT_SET_STYLE (work, FONT_SLANT_INDEX, attrs[LFACE_SLANT_INDEX]);
2851 FONT_SET_STYLE (work, FONT_WIDTH_INDEX, attrs[LFACE_SWIDTH_INDEX]);
2853 entity = Qnil;
2854 for (; driver_list; driver_list = driver_list->next)
2855 if (driver_list->on
2856 && (NILP (ftype) || EQ (driver_list->driver->type, ftype)))
2858 Lisp_Object cache = font_get_cache (f, driver_list->driver);
2859 Lisp_Object copy;
2861 ASET (work, FONT_TYPE_INDEX, driver_list->driver->type);
2862 entity = assoc_no_quit (work, XCDR (cache));
2863 if (CONSP (entity))
2864 entity = XCDR (entity);
2865 else
2867 entity = driver_list->driver->match (frame, work);
2868 copy = Fcopy_font_spec (work);
2869 ASET (copy, FONT_TYPE_INDEX, driver_list->driver->type);
2870 XSETCDR (cache, Fcons (Fcons (copy, entity), XCDR (cache)));
2872 if (! NILP (entity))
2873 break;
2875 font_add_log ("match", work, entity);
2876 return entity;
2880 /* Open a font of ENTITY and PIXEL_SIZE on frame F, and return the
2881 opened font object. */
2883 static Lisp_Object
2884 font_open_entity (f, entity, pixel_size)
2885 FRAME_PTR f;
2886 Lisp_Object entity;
2887 int pixel_size;
2889 struct font_driver_list *driver_list;
2890 Lisp_Object objlist, size, val, font_object;
2891 struct font *font;
2892 int min_width, height;
2893 int scaled_pixel_size;
2895 font_assert (FONT_ENTITY_P (entity));
2896 size = AREF (entity, FONT_SIZE_INDEX);
2897 if (XINT (size) != 0)
2898 scaled_pixel_size = pixel_size = XINT (size);
2899 else if (CONSP (Vface_font_rescale_alist))
2900 scaled_pixel_size = pixel_size * font_rescale_ratio (entity);
2902 for (objlist = AREF (entity, FONT_OBJLIST_INDEX); CONSP (objlist);
2903 objlist = XCDR (objlist))
2904 if (! NILP (AREF (XCAR (objlist), FONT_TYPE_INDEX))
2905 && XFONT_OBJECT (XCAR (objlist))->pixel_size == pixel_size)
2906 return XCAR (objlist);
2908 val = AREF (entity, FONT_TYPE_INDEX);
2909 for (driver_list = f->font_driver_list;
2910 driver_list && ! EQ (driver_list->driver->type, val);
2911 driver_list = driver_list->next);
2912 if (! driver_list)
2913 return Qnil;
2915 font_object = driver_list->driver->open (f, entity, scaled_pixel_size);
2916 ASET (font_object, FONT_SIZE_INDEX, make_number (pixel_size));
2917 font_add_log ("open", entity, font_object);
2918 if (NILP (font_object))
2919 return Qnil;
2920 ASET (entity, FONT_OBJLIST_INDEX,
2921 Fcons (font_object, AREF (entity, FONT_OBJLIST_INDEX)));
2922 ASET (font_object, FONT_OBJLIST_INDEX, Qnil);
2923 num_fonts++;
2925 font = XFONT_OBJECT (font_object);
2926 min_width = (font->min_width ? font->min_width
2927 : font->average_width ? font->average_width
2928 : font->space_width ? font->space_width
2929 : 1);
2930 height = (font->height ? font->height : 1);
2931 #ifdef HAVE_WINDOW_SYSTEM
2932 FRAME_X_DISPLAY_INFO (f)->n_fonts++;
2933 if (FRAME_X_DISPLAY_INFO (f)->n_fonts == 1)
2935 FRAME_SMALLEST_CHAR_WIDTH (f) = min_width;
2936 FRAME_SMALLEST_FONT_HEIGHT (f) = height;
2937 fonts_changed_p = 1;
2939 else
2941 if (FRAME_SMALLEST_CHAR_WIDTH (f) > min_width)
2942 FRAME_SMALLEST_CHAR_WIDTH (f) = min_width, fonts_changed_p = 1;
2943 if (FRAME_SMALLEST_FONT_HEIGHT (f) > height)
2944 FRAME_SMALLEST_FONT_HEIGHT (f) = height, fonts_changed_p = 1;
2946 #endif
2948 return font_object;
2952 /* Close FONT_OBJECT that is opened on frame F. */
2954 void
2955 font_close_object (f, font_object)
2956 FRAME_PTR f;
2957 Lisp_Object font_object;
2959 struct font *font = XFONT_OBJECT (font_object);
2961 if (NILP (AREF (font_object, FONT_TYPE_INDEX)))
2962 /* Already closed. */
2963 return;
2964 font_add_log ("close", font_object, Qnil);
2965 font->driver->close (f, font);
2966 #ifdef HAVE_WINDOW_SYSTEM
2967 font_assert (FRAME_X_DISPLAY_INFO (f)->n_fonts);
2968 FRAME_X_DISPLAY_INFO (f)->n_fonts--;
2969 #endif
2970 num_fonts--;
2974 /* Return 1 if FONT on F has a glyph for character C, 0 if not, -1 if
2975 FONT is a font-entity and it must be opened to check. */
2978 font_has_char (f, font, c)
2979 FRAME_PTR f;
2980 Lisp_Object font;
2981 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 font_assert (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 unsigned
3016 font_encode_char (font_object, c)
3017 Lisp_Object font_object;
3018 int c;
3020 struct font *font;
3022 font_assert (FONT_OBJECT_P (font_object));
3023 font = XFONT_OBJECT (font_object);
3024 return font->driver->encode_char (font, c);
3028 /* Return the name of FONT_OBJECT. */
3030 Lisp_Object
3031 font_get_name (font_object)
3032 Lisp_Object font_object;
3034 font_assert (FONT_OBJECT_P (font_object));
3035 return AREF (font_object, FONT_NAME_INDEX);
3039 /* Return the specification of FONT_OBJECT. */
3041 Lisp_Object
3042 font_get_spec (font_object)
3043 Lisp_Object font_object;
3045 Lisp_Object spec = font_make_spec ();
3046 int i;
3048 for (i = 0; i < FONT_SIZE_INDEX; i++)
3049 ASET (spec, i, AREF (font_object, i));
3050 ASET (spec, FONT_SIZE_INDEX,
3051 make_number (XFONT_OBJECT (font_object)->pixel_size));
3052 return spec;
3056 /* Create a new font spec from FONT_NAME, and return it. If FONT_NAME
3057 could not be parsed by font_parse_name, return Qnil. */
3059 Lisp_Object
3060 font_spec_from_name (font_name)
3061 Lisp_Object font_name;
3063 Lisp_Object spec = Ffont_spec (0, NULL);
3065 CHECK_STRING (font_name);
3066 if (font_parse_name ((char *) SDATA (font_name), spec) == -1)
3067 return Qnil;
3068 font_put_extra (spec, QCname, font_name);
3069 return spec;
3073 void
3074 font_clear_prop (attrs, prop)
3075 Lisp_Object *attrs;
3076 enum font_property_index prop;
3078 Lisp_Object font = attrs[LFACE_FONT_INDEX];
3080 if (! FONTP (font))
3081 return;
3082 if (NILP (AREF (font, prop))
3083 && prop != FONT_FAMILY_INDEX
3084 && prop != FONT_FOUNDRY_INDEX
3085 && prop != FONT_WIDTH_INDEX
3086 && prop != FONT_SIZE_INDEX)
3087 return;
3088 font = Fcopy_font_spec (font);
3089 ASET (font, prop, Qnil);
3090 if (prop == FONT_FAMILY_INDEX || prop == FONT_FOUNDRY_INDEX)
3092 if (prop == FONT_FAMILY_INDEX)
3094 ASET (font, FONT_FOUNDRY_INDEX, Qnil);
3095 /* If we are setting the font family, we must also clear
3096 FONT_WIDTH_INDEX to avoid rejecting families that lack
3097 support for some widths. */
3098 ASET (font, FONT_WIDTH_INDEX, Qnil);
3100 ASET (font, FONT_ADSTYLE_INDEX, Qnil);
3101 ASET (font, FONT_REGISTRY_INDEX, Qnil);
3102 ASET (font, FONT_SIZE_INDEX, Qnil);
3103 ASET (font, FONT_DPI_INDEX, Qnil);
3104 ASET (font, FONT_SPACING_INDEX, Qnil);
3105 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3107 else if (prop == FONT_SIZE_INDEX)
3109 ASET (font, FONT_DPI_INDEX, Qnil);
3110 ASET (font, FONT_SPACING_INDEX, Qnil);
3111 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3113 else if (prop == FONT_WIDTH_INDEX)
3114 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3115 attrs[LFACE_FONT_INDEX] = font;
3118 void
3119 font_update_lface (f, attrs)
3120 FRAME_PTR f;
3121 Lisp_Object *attrs;
3123 Lisp_Object spec;
3125 spec = attrs[LFACE_FONT_INDEX];
3126 if (! FONT_SPEC_P (spec))
3127 return;
3129 if (! NILP (AREF (spec, FONT_FOUNDRY_INDEX)))
3130 attrs[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (spec, FONT_FOUNDRY_INDEX));
3131 if (! NILP (AREF (spec, FONT_FAMILY_INDEX)))
3132 attrs[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (spec, FONT_FAMILY_INDEX));
3133 if (! NILP (AREF (spec, FONT_WEIGHT_INDEX)))
3134 attrs[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (spec);
3135 if (! NILP (AREF (spec, FONT_SLANT_INDEX)))
3136 attrs[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (spec);
3137 if (! NILP (AREF (spec, FONT_WIDTH_INDEX)))
3138 attrs[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (spec);
3139 if (! NILP (AREF (spec, FONT_SIZE_INDEX)))
3141 int point;
3143 if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
3145 Lisp_Object val;
3146 int dpi = f->resy;
3148 val = Ffont_get (spec, QCdpi);
3149 if (! NILP (val))
3150 dpi = XINT (val);
3151 point = PIXEL_TO_POINT (XINT (AREF (spec, FONT_SIZE_INDEX)) * 10,
3152 dpi);
3153 attrs[LFACE_HEIGHT_INDEX] = make_number (point);
3155 else if (FLOATP (AREF (spec, FONT_SIZE_INDEX)))
3157 point = XFLOAT_DATA (AREF (spec, FONT_SIZE_INDEX)) * 10;
3158 attrs[LFACE_HEIGHT_INDEX] = make_number (point);
3164 /* Selecte a font from ENTITIES that supports C and matches best with
3165 ATTRS and PIXEL_SIZE. */
3167 static Lisp_Object
3168 font_select_entity (frame, entities, attrs, pixel_size, c)
3169 Lisp_Object frame, entities, *attrs;
3170 int pixel_size, c;
3172 Lisp_Object font_entity;
3173 Lisp_Object prefer;
3174 Lisp_Object props[FONT_REGISTRY_INDEX + 1] ;
3175 int result, i;
3176 FRAME_PTR f = XFRAME (frame);
3178 if (ASIZE (entities) == 1)
3180 font_entity = AREF (entities, 0);
3181 if (c < 0
3182 || (result = font_has_char (f, font_entity, c)) > 0)
3183 return font_entity;
3184 return Qnil;
3187 /* Sort fonts by properties specified in ATTRS. */
3188 prefer = scratch_font_prefer;
3190 for (i = FONT_WEIGHT_INDEX; i <= FONT_SIZE_INDEX; i++)
3191 ASET (prefer, i, Qnil);
3192 if (FONTP (attrs[LFACE_FONT_INDEX]))
3194 Lisp_Object face_font = attrs[LFACE_FONT_INDEX];
3196 for (i = FONT_WEIGHT_INDEX; i <= FONT_SIZE_INDEX; i++)
3197 ASET (prefer, i, AREF (face_font, i));
3199 if (NILP (AREF (prefer, FONT_WEIGHT_INDEX)))
3200 FONT_SET_STYLE (prefer, FONT_WEIGHT_INDEX, attrs[LFACE_WEIGHT_INDEX]);
3201 if (NILP (AREF (prefer, FONT_SLANT_INDEX)))
3202 FONT_SET_STYLE (prefer, FONT_SLANT_INDEX, attrs[LFACE_SLANT_INDEX]);
3203 if (NILP (AREF (prefer, FONT_WIDTH_INDEX)))
3204 FONT_SET_STYLE (prefer, FONT_WIDTH_INDEX, attrs[LFACE_SWIDTH_INDEX]);
3205 ASET (prefer, FONT_SIZE_INDEX, make_number (pixel_size));
3206 entities = font_sort_entites (entities, prefer, frame, c < 0);
3208 if (c < 0)
3209 return entities;
3211 for (i = 0; i < ASIZE (entities); i++)
3213 int j;
3215 font_entity = AREF (entities, i);
3216 if (i > 0)
3218 for (j = FONT_FOUNDRY_INDEX; j <= FONT_REGISTRY_INDEX; j++)
3219 if (! EQ (AREF (font_entity, j), props[j]))
3220 break;
3221 if (j > FONT_REGISTRY_INDEX)
3222 continue;
3224 for (j = FONT_FOUNDRY_INDEX; j <= FONT_REGISTRY_INDEX; j++)
3225 props[j] = AREF (font_entity, j);
3226 result = font_has_char (f, font_entity, c);
3227 if (result > 0)
3228 return font_entity;
3230 return Qnil;
3233 /* Return a font-entity satisfying SPEC and best matching with face's
3234 font related attributes in ATTRS. C, if not negative, is a
3235 character that the entity must support. */
3237 Lisp_Object
3238 font_find_for_lface (f, attrs, spec, c)
3239 FRAME_PTR f;
3240 Lisp_Object *attrs;
3241 Lisp_Object spec;
3242 int c;
3244 Lisp_Object work;
3245 Lisp_Object frame, entities, val, props[FONT_REGISTRY_INDEX + 1] ;
3246 Lisp_Object size, foundry[3], *family, registry[3], adstyle[3];
3247 int pixel_size;
3248 int i, j, k, l, result;
3250 registry[0] = AREF (spec, FONT_REGISTRY_INDEX);
3251 if (NILP (registry[0]))
3253 registry[0] = DEFAULT_ENCODING;
3254 registry[1] = Qascii_0;
3255 registry[2] = null_vector;
3257 else
3258 registry[1] = null_vector;
3260 if (c >= 0 && ! NILP (AREF (spec, FONT_REGISTRY_INDEX)))
3262 struct charset *encoding, *repertory;
3264 if (font_registry_charsets (AREF (spec, FONT_REGISTRY_INDEX),
3265 &encoding, &repertory) < 0)
3266 return Qnil;
3267 if (repertory)
3269 if (ENCODE_CHAR (repertory, c) == CHARSET_INVALID_CODE (repertory))
3270 return Qnil;
3271 /* Any font of this registry support C. So, let's
3272 suppress the further checking. */
3273 c = -1;
3275 else if (c > encoding->max_char)
3276 return Qnil;
3279 work = Fcopy_font_spec (spec);
3280 XSETFRAME (frame, f);
3281 size = AREF (spec, FONT_SIZE_INDEX);
3282 pixel_size = font_pixel_size (f, spec);
3283 if (pixel_size == 0)
3285 double pt = XINT (attrs[LFACE_HEIGHT_INDEX]);
3287 pixel_size = POINT_TO_PIXEL (pt / 10, f->resy);
3289 ASET (work, FONT_SIZE_INDEX, Qnil);
3290 foundry[0] = AREF (work, FONT_FOUNDRY_INDEX);
3291 if (! NILP (foundry[0]))
3292 foundry[1] = null_vector;
3293 else if (STRINGP (attrs[LFACE_FOUNDRY_INDEX]))
3295 val = attrs[LFACE_FOUNDRY_INDEX];
3296 foundry[0] = font_intern_prop ((char *) SDATA (val), SBYTES (val), 1);
3297 foundry[1] = Qnil;
3298 foundry[2] = null_vector;
3300 else
3301 foundry[0] = Qnil, foundry[1] = null_vector;
3303 adstyle[0] = AREF (work, FONT_ADSTYLE_INDEX);
3304 if (! NILP (adstyle[0]))
3305 adstyle[1] = null_vector;
3306 else if (FONTP (attrs[LFACE_FONT_INDEX]))
3308 Lisp_Object face_font = attrs[LFACE_FONT_INDEX];
3310 if (! NILP (AREF (face_font, FONT_ADSTYLE_INDEX)))
3312 adstyle[0] = AREF (face_font, FONT_ADSTYLE_INDEX);
3313 adstyle[1] = Qnil;
3314 adstyle[2] = null_vector;
3316 else
3317 adstyle[0] = Qnil, adstyle[1] = null_vector;
3319 else
3320 adstyle[0] = Qnil, adstyle[1] = null_vector;
3323 val = AREF (work, FONT_FAMILY_INDEX);
3324 if (NILP (val) && STRINGP (attrs[LFACE_FAMILY_INDEX]))
3326 val = attrs[LFACE_FAMILY_INDEX];
3327 val = font_intern_prop ((char *) SDATA (val), SBYTES (val), 1);
3329 if (NILP (val))
3331 family = alloca ((sizeof family[0]) * 2);
3332 family[0] = Qnil;
3333 family[1] = null_vector; /* terminator. */
3335 else
3337 Lisp_Object alters
3338 = Fassoc_string (val, Vface_alternative_font_family_alist,
3339 #ifndef HAVE_NS
3341 #else
3342 Qnil
3343 #endif
3346 if (! NILP (alters))
3348 family = alloca ((sizeof family[0]) * (XINT (Flength (alters)) + 2));
3349 for (i = 0; CONSP (alters); i++, alters = XCDR (alters))
3350 family[i] = XCAR (alters);
3351 if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
3352 family[i++] = Qnil;
3353 family[i] = null_vector;
3355 else
3357 family = alloca ((sizeof family[0]) * 3);
3358 i = 0;
3359 family[i++] = val;
3360 if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
3361 family[i++] = Qnil;
3362 family[i] = null_vector;
3366 for (i = 0; SYMBOLP (family[i]); i++)
3368 ASET (work, FONT_FAMILY_INDEX, family[i]);
3369 for (j = 0; SYMBOLP (foundry[j]); j++)
3371 ASET (work, FONT_FOUNDRY_INDEX, foundry[j]);
3372 for (k = 0; SYMBOLP (registry[k]); k++)
3374 ASET (work, FONT_REGISTRY_INDEX, registry[k]);
3375 for (l = 0; SYMBOLP (adstyle[l]); l++)
3377 ASET (work, FONT_ADSTYLE_INDEX, adstyle[l]);
3378 entities = font_list_entities (frame, work);
3379 if (ASIZE (entities) > 0)
3381 val = font_select_entity (frame, entities,
3382 attrs, pixel_size, c);
3383 if (! NILP (val))
3384 return val;
3390 return Qnil;
3394 Lisp_Object
3395 font_open_for_lface (f, entity, attrs, spec)
3396 FRAME_PTR f;
3397 Lisp_Object entity;
3398 Lisp_Object *attrs;
3399 Lisp_Object spec;
3401 int size;
3403 if (INTEGERP (AREF (entity, FONT_SIZE_INDEX))
3404 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
3405 size = XINT (AREF (entity, FONT_SIZE_INDEX));
3406 else if (FONT_SPEC_P (spec) && ! NILP (AREF (spec, FONT_SIZE_INDEX)))
3407 size = font_pixel_size (f, spec);
3408 else
3410 double pt;
3411 if (INTEGERP (attrs[LFACE_HEIGHT_INDEX]))
3412 pt = XINT (attrs[LFACE_HEIGHT_INDEX]);
3413 else
3415 struct face *def = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3416 Lisp_Object height = def->lface[LFACE_HEIGHT_INDEX];
3417 if (INTEGERP (height))
3418 pt = XINT (height);
3419 else
3420 abort(); /* We should never end up here. */
3423 pt /= 10;
3424 size = POINT_TO_PIXEL (pt, f->resy);
3425 #ifdef HAVE_NS
3426 if (size == 0)
3428 Lisp_Object ffsize = get_frame_param(f, Qfontsize);
3429 size = NUMBERP (ffsize) ? POINT_TO_PIXEL (XINT (ffsize), f->resy) : 0;
3431 #endif
3433 return font_open_entity (f, entity, size);
3437 /* Find a font satisfying SPEC and best matching with face's
3438 attributes in ATTRS on FRAME, and return the opened
3439 font-object. */
3441 Lisp_Object
3442 font_load_for_lface (f, attrs, spec)
3443 FRAME_PTR f;
3444 Lisp_Object *attrs, spec;
3446 Lisp_Object entity;
3448 entity = font_find_for_lface (f, attrs, spec, -1);
3449 if (NILP (entity))
3451 /* No font is listed for SPEC, but each font-backend may have
3452 the different criteria about "font matching". So, try
3453 it. */
3454 entity = font_matching_entity (f, attrs, spec);
3455 if (NILP (entity))
3456 return Qnil;
3458 return font_open_for_lface (f, entity, attrs, spec);
3462 /* Make FACE on frame F ready to use the font opened for FACE. */
3464 void
3465 font_prepare_for_face (f, face)
3466 FRAME_PTR f;
3467 struct face *face;
3469 if (face->font->driver->prepare_face)
3470 face->font->driver->prepare_face (f, face);
3474 /* Make FACE on frame F stop using the font opened for FACE. */
3476 void
3477 font_done_for_face (f, face)
3478 FRAME_PTR f;
3479 struct face *face;
3481 if (face->font->driver->done_face)
3482 face->font->driver->done_face (f, face);
3483 face->extra = NULL;
3487 /* Open a font matching with font-spec SPEC on frame F. If no proper
3488 font is found, return Qnil. */
3490 Lisp_Object
3491 font_open_by_spec (f, spec)
3492 FRAME_PTR f;
3493 Lisp_Object spec;
3495 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3497 /* We set up the default font-related attributes of a face to prefer
3498 a moderate font. */
3499 attrs[LFACE_FAMILY_INDEX] = attrs[LFACE_FOUNDRY_INDEX] = Qnil;
3500 attrs[LFACE_SWIDTH_INDEX] = attrs[LFACE_WEIGHT_INDEX]
3501 = attrs[LFACE_SLANT_INDEX] = Qnormal;
3502 #ifndef HAVE_NS
3503 attrs[LFACE_HEIGHT_INDEX] = make_number (120);
3504 #else
3505 attrs[LFACE_HEIGHT_INDEX] = make_number (0);
3506 #endif
3507 attrs[LFACE_FONT_INDEX] = Qnil;
3509 return font_load_for_lface (f, attrs, spec);
3513 /* Open a font matching with NAME on frame F. If no proper font is
3514 found, return Qnil. */
3516 Lisp_Object
3517 font_open_by_name (f, name)
3518 FRAME_PTR f;
3519 char *name;
3521 Lisp_Object args[2];
3522 Lisp_Object spec;
3524 args[0] = QCname;
3525 args[1] = make_unibyte_string (name, strlen (name));
3526 spec = Ffont_spec (2, args);
3527 return font_open_by_spec (f, spec);
3531 /* Register font-driver DRIVER. This function is used in two ways.
3533 The first is with frame F non-NULL. In this case, make DRIVER
3534 available (but not yet activated) on F. All frame creaters
3535 (e.g. Fx_create_frame) must call this function at least once with
3536 an available font-driver.
3538 The second is with frame F NULL. In this case, DRIVER is globally
3539 registered in the variable `font_driver_list'. All font-driver
3540 implementations must call this function in its syms_of_XXXX
3541 (e.g. syms_of_xfont). */
3543 void
3544 register_font_driver (driver, f)
3545 struct font_driver *driver;
3546 FRAME_PTR f;
3548 struct font_driver_list *root = f ? f->font_driver_list : font_driver_list;
3549 struct font_driver_list *prev, *list;
3551 if (f && ! driver->draw)
3552 error ("Unusable font driver for a frame: %s",
3553 SDATA (SYMBOL_NAME (driver->type)));
3555 for (prev = NULL, list = root; list; prev = list, list = list->next)
3556 if (EQ (list->driver->type, driver->type))
3557 error ("Duplicated font driver: %s", SDATA (SYMBOL_NAME (driver->type)));
3559 list = xmalloc (sizeof (struct font_driver_list));
3560 list->on = 0;
3561 list->driver = driver;
3562 list->next = NULL;
3563 if (prev)
3564 prev->next = list;
3565 else if (f)
3566 f->font_driver_list = list;
3567 else
3568 font_driver_list = list;
3569 if (! f)
3570 num_font_drivers++;
3573 void
3574 free_font_driver_list (f)
3575 FRAME_PTR f;
3577 struct font_driver_list *list, *next;
3579 for (list = f->font_driver_list; list; list = next)
3581 next = list->next;
3582 xfree (list);
3584 f->font_driver_list = NULL;
3588 /* Make the frame F use font backends listed in NEW_DRIVERS (list of
3589 symbols, e.g. xft, x). If NEW_DRIVERS is t, make F use all
3590 available font drivers. If NEW_DRIVERS is nil, finalize all drivers.
3592 A caller must free all realized faces if any in advance. The
3593 return value is a list of font backends actually made used on
3594 F. */
3596 Lisp_Object
3597 font_update_drivers (f, new_drivers)
3598 FRAME_PTR f;
3599 Lisp_Object new_drivers;
3601 Lisp_Object active_drivers = Qnil;
3602 struct font_driver *driver;
3603 struct font_driver_list *list;
3605 /* At first, turn off non-requested drivers, and turn on requested
3606 drivers. */
3607 for (list = f->font_driver_list; list; list = list->next)
3609 driver = list->driver;
3610 if ((EQ (new_drivers, Qt) || ! NILP (Fmemq (driver->type, new_drivers)))
3611 != list->on)
3613 if (list->on)
3615 if (driver->end_for_frame)
3616 driver->end_for_frame (f);
3617 font_finish_cache (f, driver);
3618 list->on = 0;
3620 else
3622 if (! driver->start_for_frame
3623 || driver->start_for_frame (f) == 0)
3625 font_prepare_cache (f, driver);
3626 list->on = 1;
3632 if (NILP (new_drivers))
3633 return Qnil;
3635 if (! EQ (new_drivers, Qt))
3637 /* Re-order the driver list according to new_drivers. */
3638 struct font_driver_list **list_table, **next;
3639 Lisp_Object tail;
3640 int i;
3642 list_table = alloca (sizeof list_table[0] * (num_font_drivers + 1));
3643 for (i = 0, tail = new_drivers; ! NILP (tail); tail = XCDR (tail))
3645 for (list = f->font_driver_list; list; list = list->next)
3646 if (list->on && EQ (list->driver->type, XCAR (tail)))
3647 break;
3648 if (list)
3649 list_table[i++] = list;
3651 for (list = f->font_driver_list; list; list = list->next)
3652 if (! list->on)
3653 list_table[i++] = list;
3654 list_table[i] = NULL;
3656 next = &f->font_driver_list;
3657 for (i = 0; list_table[i]; i++)
3659 *next = list_table[i];
3660 next = &(*next)->next;
3662 *next = NULL;
3665 for (list = f->font_driver_list; list; list = list->next)
3666 if (list->on)
3667 active_drivers = nconc2 (active_drivers,
3668 Fcons (list->driver->type, Qnil));
3669 return active_drivers;
3673 font_put_frame_data (f, driver, data)
3674 FRAME_PTR f;
3675 struct font_driver *driver;
3676 void *data;
3678 struct font_data_list *list, *prev;
3680 for (prev = NULL, list = f->font_data_list; list;
3681 prev = list, list = list->next)
3682 if (list->driver == driver)
3683 break;
3684 if (! data)
3686 if (list)
3688 if (prev)
3689 prev->next = list->next;
3690 else
3691 f->font_data_list = list->next;
3692 free (list);
3694 return 0;
3697 if (! list)
3699 list = xmalloc (sizeof (struct font_data_list));
3700 list->driver = driver;
3701 list->next = f->font_data_list;
3702 f->font_data_list = list;
3704 list->data = data;
3705 return 0;
3709 void *
3710 font_get_frame_data (f, driver)
3711 FRAME_PTR f;
3712 struct font_driver *driver;
3714 struct font_data_list *list;
3716 for (list = f->font_data_list; list; list = list->next)
3717 if (list->driver == driver)
3718 break;
3719 if (! list)
3720 return NULL;
3721 return list->data;
3725 /* Return the font used to draw character C by FACE at buffer position
3726 POS in window W. If STRING is non-nil, it is a string containing C
3727 at index POS. If C is negative, get C from the current buffer or
3728 STRING. */
3730 Lisp_Object
3731 font_at (c, pos, face, w, string)
3732 int c;
3733 EMACS_INT pos;
3734 struct face *face;
3735 struct window *w;
3736 Lisp_Object string;
3738 FRAME_PTR f;
3739 int multibyte;
3740 Lisp_Object font_object;
3742 multibyte = (NILP (string)
3743 ? ! NILP (current_buffer->enable_multibyte_characters)
3744 : STRING_MULTIBYTE (string));
3745 if (c < 0)
3747 if (NILP (string))
3749 if (multibyte)
3751 EMACS_INT pos_byte = CHAR_TO_BYTE (pos);
3753 c = FETCH_CHAR (pos_byte);
3755 else
3756 c = FETCH_BYTE (pos);
3758 else
3760 unsigned char *str;
3762 multibyte = STRING_MULTIBYTE (string);
3763 if (multibyte)
3765 EMACS_INT pos_byte = string_char_to_byte (string, pos);
3767 str = SDATA (string) + pos_byte;
3768 c = STRING_CHAR (str, 0);
3770 else
3771 c = SDATA (string)[pos];
3775 f = XFRAME (w->frame);
3776 if (! FRAME_WINDOW_P (f))
3777 return Qnil;
3778 if (! face)
3780 int face_id;
3781 EMACS_INT endptr;
3783 if (STRINGP (string))
3784 face_id = face_at_string_position (w, string, pos, 0, -1, -1, &endptr,
3785 DEFAULT_FACE_ID, 0);
3786 else
3787 face_id = face_at_buffer_position (w, pos, -1, -1, &endptr,
3788 pos + 100, 0);
3789 face = FACE_FROM_ID (f, face_id);
3791 if (multibyte)
3793 int face_id = FACE_FOR_CHAR (f, face, c, pos, string);
3794 face = FACE_FROM_ID (f, face_id);
3796 if (! face->font)
3797 return Qnil;
3799 XSETFONT (font_object, face->font);
3800 return font_object;
3804 #ifdef HAVE_WINDOW_SYSTEM
3806 /* Check how many characters after POS (at most to *LIMIT) can be
3807 displayed by the same font on the window W. FACE, if non-NULL, is
3808 the face selected for the character at POS. If STRING is not nil,
3809 it is the string to check instead of the current buffer. In that
3810 case, FACE must be not NULL.
3812 The return value is the font-object for the character at POS.
3813 *LIMIT is set to the position where that font can't be used.
3815 It is assured that the current buffer (or STRING) is multibyte. */
3817 Lisp_Object
3818 font_range (pos, limit, w, face, string)
3819 EMACS_INT pos, *limit;
3820 struct window *w;
3821 struct face *face;
3822 Lisp_Object string;
3824 EMACS_INT pos_byte, ignore, start, start_byte;
3825 int c;
3826 Lisp_Object font_object = Qnil;
3828 if (NILP (string))
3830 pos_byte = CHAR_TO_BYTE (pos);
3831 if (! face)
3833 int face_id;
3835 face_id = face_at_buffer_position (w, pos, 0, 0, &ignore, *limit, 0);
3836 face = FACE_FROM_ID (XFRAME (w->frame), face_id);
3839 else
3841 font_assert (face);
3842 pos_byte = string_char_to_byte (string, pos);
3845 start = pos, start_byte = pos_byte;
3846 while (pos < *limit)
3848 Lisp_Object category;
3850 if (NILP (string))
3851 FETCH_CHAR_ADVANCE_NO_CHECK (c, pos, pos_byte);
3852 else
3853 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, string, pos, pos_byte);
3854 if (NILP (font_object))
3856 font_object = font_for_char (face, c, pos - 1, string);
3857 if (NILP (font_object))
3858 return Qnil;
3859 continue;
3862 category = CHAR_TABLE_REF (Vunicode_category_table, c);
3863 if (! EQ (category, QCf)
3864 && ! CHAR_VARIATION_SELECTOR_P (c)
3865 && font_encode_char (font_object, c) == FONT_INVALID_CODE)
3867 Lisp_Object f = font_for_char (face, c, pos - 1, string);
3868 EMACS_INT i, i_byte;
3871 if (NILP (f))
3873 *limit = pos - 1;
3874 return font_object;
3876 i = start, i_byte = start_byte;
3877 while (i < pos - 1)
3880 if (NILP (string))
3881 FETCH_CHAR_ADVANCE_NO_CHECK (c, i, i_byte);
3882 else
3883 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, string, i, i_byte);
3884 category = CHAR_TABLE_REF (Vunicode_category_table, c);
3885 if (! EQ (category, QCf)
3886 && ! CHAR_VARIATION_SELECTOR_P (c)
3887 && font_encode_char (f, c) == FONT_INVALID_CODE)
3889 *limit = pos - 1;
3890 return font_object;
3893 font_object = f;
3896 return font_object;
3898 #endif
3901 /* Lisp API */
3903 DEFUN ("fontp", Ffontp, Sfontp, 1, 2, 0,
3904 doc: /* Return t if OBJECT is a font-spec, font-entity, or font-object.
3905 Return nil otherwise.
3906 Optional 2nd argument EXTRA-TYPE, if non-nil, specifies to check
3907 which kind of font it is. It must be one of `font-spec', `font-entity',
3908 `font-object'. */)
3909 (object, extra_type)
3910 Lisp_Object object, extra_type;
3912 if (NILP (extra_type))
3913 return (FONTP (object) ? Qt : Qnil);
3914 if (EQ (extra_type, Qfont_spec))
3915 return (FONT_SPEC_P (object) ? Qt : Qnil);
3916 if (EQ (extra_type, Qfont_entity))
3917 return (FONT_ENTITY_P (object) ? Qt : Qnil);
3918 if (EQ (extra_type, Qfont_object))
3919 return (FONT_OBJECT_P (object) ? Qt : Qnil);
3920 wrong_type_argument (intern ("font-extra-type"), extra_type);
3923 DEFUN ("font-spec", Ffont_spec, Sfont_spec, 0, MANY, 0,
3924 doc: /* Return a newly created font-spec with arguments as properties.
3926 ARGS must come in pairs KEY VALUE of font properties. KEY must be a
3927 valid font property name listed below:
3929 `:family', `:weight', `:slant', `:width'
3931 They are the same as face attributes of the same name. See
3932 `set-face-attribute'.
3934 `:foundry'
3936 VALUE must be a string or a symbol specifying the font foundry, e.g. ``misc''.
3938 `:adstyle'
3940 VALUE must be a string or a symbol specifying the additional
3941 typographic style information of a font, e.g. ``sans''.
3943 `:registry'
3945 VALUE must be a string or a symbol specifying the charset registry and
3946 encoding of a font, e.g. ``iso8859-1''.
3948 `:size'
3950 VALUE must be a non-negative integer or a floating point number
3951 specifying the font size. It specifies the font size in pixels (if
3952 VALUE is an integer), or in points (if VALUE is a float).
3954 `:name'
3956 VALUE must be a string of XLFD-style or fontconfig-style font name.
3958 `:script'
3960 VALUE must be a symbol representing a script that the font must
3961 support. It may be a symbol representing a subgroup of a script
3962 listed in the variable `script-representative-chars'.
3964 `:lang'
3966 VALUE must be a symbol of two-letter ISO-639 language names,
3967 e.g. `ja'.
3969 `:otf'
3971 VALUE must be a list (SCRIPT-TAG LANGSYS-TAG GSUB [ GPOS ]) to specify
3972 required OpenType features.
3974 SCRIPT-TAG: OpenType script tag symbol (e.g. `deva').
3975 LANGSYS-TAG: OpenType language system tag symbol,
3976 or nil for the default language system.
3977 GSUB: List of OpenType GSUB feature tag symbols, or nil if none required.
3978 GPOS: List of OpenType GPOS feature tag symbols, or nil if none required.
3980 GSUB and GPOS may contain `nil' element. In such a case, the font
3981 must not have any of the remaining elements.
3983 For instance, if the VALUE is `(thai nil nil (mark))', the font must
3984 be an OpenType font, and whose GPOS table of `thai' script's default
3985 language system must contain `mark' feature.
3987 usage: (font-spec ARGS...) */)
3988 (nargs, args)
3989 int nargs;
3990 Lisp_Object *args;
3992 Lisp_Object spec = font_make_spec ();
3993 int i;
3995 for (i = 0; i < nargs; i += 2)
3997 Lisp_Object key = args[i], val = args[i + 1];
3999 if (EQ (key, QCname))
4001 CHECK_STRING (val);
4002 font_parse_name ((char *) SDATA (val), spec);
4003 font_put_extra (spec, key, val);
4005 else
4007 int idx = get_font_prop_index (key);
4009 if (idx >= 0)
4011 val = font_prop_validate (idx, Qnil, val);
4012 if (idx < FONT_EXTRA_INDEX)
4013 ASET (spec, idx, val);
4014 else
4015 font_put_extra (spec, key, val);
4017 else
4018 font_put_extra (spec, key, font_prop_validate (0, key, val));
4021 return spec;
4024 DEFUN ("copy-font-spec", Fcopy_font_spec, Scopy_font_spec, 1, 1, 0,
4025 doc: /* Return a copy of FONT as a font-spec. */)
4026 (font)
4027 Lisp_Object font;
4029 Lisp_Object new_spec, tail, prev, extra;
4030 int i;
4032 CHECK_FONT (font);
4033 new_spec = font_make_spec ();
4034 for (i = 1; i < FONT_EXTRA_INDEX; i++)
4035 ASET (new_spec, i, AREF (font, i));
4036 extra = Fcopy_sequence (AREF (font, FONT_EXTRA_INDEX));
4037 /* We must remove :font-entity property. */
4038 for (prev = Qnil, tail = extra; CONSP (tail); prev = tail, tail = XCDR (tail))
4039 if (EQ (XCAR (XCAR (tail)), QCfont_entity))
4041 if (NILP (prev))
4042 extra = XCDR (extra);
4043 else
4044 XSETCDR (prev, XCDR (tail));
4045 break;
4047 ASET (new_spec, FONT_EXTRA_INDEX, extra);
4048 return new_spec;
4051 DEFUN ("merge-font-spec", Fmerge_font_spec, Smerge_font_spec, 2, 2, 0,
4052 doc: /* Merge font-specs FROM and TO, and return a new font-spec.
4053 Every specified properties in FROM override the corresponding
4054 properties in TO. */)
4055 (from, to)
4056 Lisp_Object from, to;
4058 Lisp_Object extra, tail;
4059 int i;
4061 CHECK_FONT (from);
4062 CHECK_FONT (to);
4063 to = Fcopy_font_spec (to);
4064 for (i = 0; i < FONT_EXTRA_INDEX; i++)
4065 ASET (to, i, AREF (from, i));
4066 extra = AREF (to, FONT_EXTRA_INDEX);
4067 for (tail = AREF (from, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail))
4068 if (! EQ (XCAR (XCAR (tail)), Qfont_entity))
4070 Lisp_Object slot = assq_no_quit (XCAR (XCAR (tail)), extra);
4072 if (! NILP (slot))
4073 XSETCDR (slot, XCDR (XCAR (tail)));
4074 else
4075 extra = Fcons (Fcons (XCAR (XCAR (tail)), XCDR (XCAR (tail))), extra);
4077 ASET (to, FONT_EXTRA_INDEX, extra);
4078 return to;
4081 DEFUN ("font-get", Ffont_get, Sfont_get, 2, 2, 0,
4082 doc: /* Return the value of FONT's property KEY.
4083 FONT is a font-spec, a font-entity, or a font-object.
4084 KEY must be one of these symbols:
4085 :family, :weight, :slant, :width, :foundry, :adstyle, :registry,
4086 :size, :name, :script
4087 See the documentation of `font-spec' for their meanings.
4088 If FONT is a font-entity or font-object, the value of :script may be
4089 a list of scripts that are supported by the font. */)
4090 (font, key)
4091 Lisp_Object font, key;
4093 int idx;
4095 CHECK_FONT (font);
4096 CHECK_SYMBOL (key);
4098 idx = get_font_prop_index (key);
4099 if (idx >= FONT_WEIGHT_INDEX && idx <= FONT_WIDTH_INDEX)
4100 return font_style_symbolic (font, idx, 0);
4101 if (idx >= 0 && idx < FONT_EXTRA_INDEX)
4102 return AREF (font, idx);
4103 return Fcdr (Fassq (key, AREF (font, FONT_EXTRA_INDEX)));
4106 #ifdef HAVE_WINDOW_SYSTEM
4108 DEFUN ("font-face-attributes", Ffont_face_attributes, Sfont_face_attributes, 1, 2, 0,
4109 doc: /* Return a plist of face attributes generated by FONT.
4110 FONT is a font name, a font-spec, a font-entity, or a font-object.
4111 The return value is a list of the form
4113 \(:family FAMILY :height HEIGHT :weight WEIGHT :slant SLANT :width WIDTH)
4115 where FAMILY, HEIGHT, WEIGHT, SLANT, and WIDTH are face attribute values
4116 compatible with `set-face-attribute'. Some of these key-attribute pairs
4117 may be omitted from the list if they are not specified by FONT.
4119 The optional argument FRAME specifies the frame that the face attributes
4120 are to be displayed on. If omitted, the selected frame is used. */)
4121 (font, frame)
4122 Lisp_Object font, frame;
4124 struct frame *f;
4125 Lisp_Object plist[10];
4126 Lisp_Object val;
4127 int n = 0;
4129 if (NILP (frame))
4130 frame = selected_frame;
4131 CHECK_LIVE_FRAME (frame);
4132 f = XFRAME (frame);
4134 if (STRINGP (font))
4136 int fontset = fs_query_fontset (font, 0);
4137 Lisp_Object name = font;
4138 if (fontset >= 0)
4139 font = fontset_ascii (fontset);
4140 font = font_spec_from_name (name);
4141 if (! FONTP (font))
4142 signal_error ("Invalid font name", name);
4144 else if (! FONTP (font))
4145 signal_error ("Invalid font object", font);
4147 val = AREF (font, FONT_FAMILY_INDEX);
4148 if (! NILP (val))
4150 plist[n++] = QCfamily;
4151 plist[n++] = SYMBOL_NAME (val);
4154 val = AREF (font, FONT_SIZE_INDEX);
4155 if (INTEGERP (val))
4157 Lisp_Object font_dpi = AREF (font, FONT_DPI_INDEX);
4158 int dpi = INTEGERP (font_dpi) ? XINT (font_dpi) : f->resy;
4159 plist[n++] = QCheight;
4160 plist[n++] = make_number (PIXEL_TO_POINT (XINT (val) * 10, dpi));
4162 else if (FLOATP (val))
4164 plist[n++] = QCheight;
4165 plist[n++] = make_number (10 * (int) XFLOAT_DATA (val));
4168 val = FONT_WEIGHT_FOR_FACE (font);
4169 if (! NILP (val))
4171 plist[n++] = QCweight;
4172 plist[n++] = val;
4175 val = FONT_SLANT_FOR_FACE (font);
4176 if (! NILP (val))
4178 plist[n++] = QCslant;
4179 plist[n++] = val;
4182 val = FONT_WIDTH_FOR_FACE (font);
4183 if (! NILP (val))
4185 plist[n++] = QCwidth;
4186 plist[n++] = val;
4189 return Flist (n, plist);
4192 #endif
4194 DEFUN ("font-put", Ffont_put, Sfont_put, 3, 3, 0,
4195 doc: /* Set one property of FONT-SPEC: give property PROP value VAL. */)
4196 (font_spec, prop, val)
4197 Lisp_Object font_spec, prop, val;
4199 int idx;
4201 CHECK_FONT_SPEC (font_spec);
4202 idx = get_font_prop_index (prop);
4203 if (idx >= 0 && idx < FONT_EXTRA_INDEX)
4204 ASET (font_spec, idx, font_prop_validate (idx, Qnil, val));
4205 else
4206 font_put_extra (font_spec, prop, font_prop_validate (0, prop, val));
4207 return val;
4210 DEFUN ("list-fonts", Flist_fonts, Slist_fonts, 1, 4, 0,
4211 doc: /* List available fonts matching FONT-SPEC on the current frame.
4212 Optional 2nd argument FRAME specifies the target frame.
4213 Optional 3rd argument NUM, if non-nil, limits the number of returned fonts.
4214 Optional 4th argument PREFER, if non-nil, is a font-spec to
4215 control the order of the returned list. Fonts are sorted by
4216 how close they are to PREFER. */)
4217 (font_spec, frame, num, prefer)
4218 Lisp_Object font_spec, frame, num, prefer;
4220 Lisp_Object vec, list, tail;
4221 int n = 0, i, len;
4223 if (NILP (frame))
4224 frame = selected_frame;
4225 CHECK_LIVE_FRAME (frame);
4226 CHECK_FONT_SPEC (font_spec);
4227 if (! NILP (num))
4229 CHECK_NUMBER (num);
4230 n = XINT (num);
4231 if (n <= 0)
4232 return Qnil;
4234 if (! NILP (prefer))
4235 CHECK_FONT_SPEC (prefer);
4237 vec = font_list_entities (frame, font_spec);
4238 len = ASIZE (vec);
4239 if (len == 0)
4240 return Qnil;
4241 if (len == 1)
4242 return Fcons (AREF (vec, 0), Qnil);
4244 if (! NILP (prefer))
4245 vec = font_sort_entites (vec, prefer, frame, 0);
4247 list = tail = Fcons (AREF (vec, 0), Qnil);
4248 if (n == 0 || n > len)
4249 n = len;
4250 for (i = 1; i < n; i++)
4252 Lisp_Object val = Fcons (AREF (vec, i), Qnil);
4254 XSETCDR (tail, val);
4255 tail = val;
4257 return list;
4260 DEFUN ("font-family-list", Ffont_family_list, Sfont_family_list, 0, 1, 0,
4261 doc: /* List available font families on the current frame.
4262 Optional argument FRAME, if non-nil, specifies the target frame. */)
4263 (frame)
4264 Lisp_Object frame;
4266 FRAME_PTR f;
4267 struct font_driver_list *driver_list;
4268 Lisp_Object list;
4270 if (NILP (frame))
4271 frame = selected_frame;
4272 CHECK_LIVE_FRAME (frame);
4273 f = XFRAME (frame);
4274 list = Qnil;
4275 for (driver_list = f->font_driver_list; driver_list;
4276 driver_list = driver_list->next)
4277 if (driver_list->driver->list_family)
4279 Lisp_Object val = driver_list->driver->list_family (frame);
4280 Lisp_Object tail = list;
4282 for (; CONSP (val); val = XCDR (val))
4283 if (NILP (Fmemq (XCAR (val), tail))
4284 && SYMBOLP (XCAR (val)))
4285 list = Fcons (SYMBOL_NAME (XCAR (val)), list);
4287 return list;
4290 DEFUN ("find-font", Ffind_font, Sfind_font, 1, 2, 0,
4291 doc: /* Return a font-entity matching with FONT-SPEC on the current frame.
4292 Optional 2nd argument FRAME, if non-nil, specifies the target frame. */)
4293 (font_spec, frame)
4294 Lisp_Object font_spec, frame;
4296 Lisp_Object val = Flist_fonts (font_spec, frame, make_number (1), Qnil);
4298 if (CONSP (val))
4299 val = XCAR (val);
4300 return val;
4303 DEFUN ("font-xlfd-name", Ffont_xlfd_name, Sfont_xlfd_name, 1, 2, 0,
4304 doc: /* Return XLFD name of FONT.
4305 FONT is a font-spec, font-entity, or font-object.
4306 If the name is too long for XLFD (maximum 255 chars), return nil.
4307 If the 2nd optional arg FOLD-WILDCARDS is non-nil,
4308 the consecutive wildcards are folded to one. */)
4309 (font, fold_wildcards)
4310 Lisp_Object font, fold_wildcards;
4312 char name[256];
4313 int pixel_size = 0;
4315 CHECK_FONT (font);
4317 if (FONT_OBJECT_P (font))
4319 Lisp_Object font_name = AREF (font, FONT_NAME_INDEX);
4321 if (STRINGP (font_name)
4322 && SDATA (font_name)[0] == '-')
4324 if (NILP (fold_wildcards))
4325 return font_name;
4326 strcpy (name, (char *) SDATA (font_name));
4327 goto done;
4329 pixel_size = XFONT_OBJECT (font)->pixel_size;
4331 if (font_unparse_xlfd (font, pixel_size, name, 256) < 0)
4332 return Qnil;
4333 done:
4334 if (! NILP (fold_wildcards))
4336 char *p0 = name, *p1;
4338 while ((p1 = strstr (p0, "-*-*")))
4340 strcpy (p1, p1 + 2);
4341 p0 = p1;
4345 return build_string (name);
4348 DEFUN ("clear-font-cache", Fclear_font_cache, Sclear_font_cache, 0, 0, 0,
4349 doc: /* Clear font cache. */)
4352 Lisp_Object list, frame;
4354 FOR_EACH_FRAME (list, frame)
4356 FRAME_PTR f = XFRAME (frame);
4357 struct font_driver_list *driver_list = f->font_driver_list;
4359 for (; driver_list; driver_list = driver_list->next)
4360 if (driver_list->on)
4362 Lisp_Object cache = driver_list->driver->get_cache (f);
4363 Lisp_Object val;
4365 val = XCDR (cache);
4366 while (! NILP (val)
4367 && ! EQ (XCAR (XCAR (val)), driver_list->driver->type))
4368 val = XCDR (val);
4369 font_assert (! NILP (val));
4370 val = XCDR (XCAR (val));
4371 if (XINT (XCAR (val)) == 0)
4373 font_clear_cache (f, XCAR (val), driver_list->driver);
4374 XSETCDR (cache, XCDR (val));
4379 return Qnil;
4383 void
4384 font_fill_lglyph_metrics (glyph, font_object)
4385 Lisp_Object glyph, font_object;
4387 struct font *font = XFONT_OBJECT (font_object);
4388 unsigned code;
4389 /* ecode used in LGLYPH_SET_CODE to avoid compiler warnings. */
4390 EMACS_INT ecode = font->driver->encode_char (font, LGLYPH_CHAR (glyph));
4391 struct font_metrics metrics;
4393 LGLYPH_SET_CODE (glyph, ecode);
4394 code = ecode;
4395 font->driver->text_extents (font, &code, 1, &metrics);
4396 LGLYPH_SET_LBEARING (glyph, metrics.lbearing);
4397 LGLYPH_SET_RBEARING (glyph, metrics.rbearing);
4398 LGLYPH_SET_WIDTH (glyph, metrics.width);
4399 LGLYPH_SET_ASCENT (glyph, metrics.ascent);
4400 LGLYPH_SET_DESCENT (glyph, metrics.descent);
4404 DEFUN ("font-shape-gstring", Ffont_shape_gstring, Sfont_shape_gstring, 1, 1, 0,
4405 doc: /* Shape the glyph-string GSTRING.
4406 Shaping means substituting glyphs and/or adjusting positions of glyphs
4407 to get the correct visual image of character sequences set in the
4408 header of the glyph-string.
4410 If the shaping was successful, the value is GSTRING itself or a newly
4411 created glyph-string. Otherwise, the value is nil. */)
4412 (gstring)
4413 Lisp_Object gstring;
4415 struct font *font;
4416 Lisp_Object font_object, n, glyph;
4417 int i, j, from, to;
4419 if (! composition_gstring_p (gstring))
4420 signal_error ("Invalid glyph-string: ", gstring);
4421 if (! NILP (LGSTRING_ID (gstring)))
4422 return gstring;
4423 font_object = LGSTRING_FONT (gstring);
4424 CHECK_FONT_OBJECT (font_object);
4425 font = XFONT_OBJECT (font_object);
4426 if (! font->driver->shape)
4427 return Qnil;
4429 /* Try at most three times with larger gstring each time. */
4430 for (i = 0; i < 3; i++)
4432 n = font->driver->shape (gstring);
4433 if (INTEGERP (n))
4434 break;
4435 gstring = larger_vector (gstring,
4436 ASIZE (gstring) + LGSTRING_GLYPH_LEN (gstring),
4437 Qnil);
4439 if (i == 3 || XINT (n) == 0)
4440 return Qnil;
4442 glyph = LGSTRING_GLYPH (gstring, 0);
4443 from = LGLYPH_FROM (glyph);
4444 to = LGLYPH_TO (glyph);
4445 for (i = 1, j = 0; i < LGSTRING_GLYPH_LEN (gstring); i++)
4447 Lisp_Object this = LGSTRING_GLYPH (gstring, i);
4449 if (NILP (this))
4450 break;
4451 if (NILP (LGLYPH_ADJUSTMENT (this)))
4453 if (j < i - 1)
4454 for (; j < i; j++)
4456 glyph = LGSTRING_GLYPH (gstring, j);
4457 LGLYPH_SET_FROM (glyph, from);
4458 LGLYPH_SET_TO (glyph, to);
4460 from = LGLYPH_FROM (this);
4461 to = LGLYPH_TO (this);
4462 j = i;
4464 else
4466 if (from > LGLYPH_FROM (this))
4467 from = LGLYPH_FROM (this);
4468 if (to < LGLYPH_TO (this))
4469 to = LGLYPH_TO (this);
4472 if (j < i - 1)
4473 for (; j < i; j++)
4475 glyph = LGSTRING_GLYPH (gstring, j);
4476 LGLYPH_SET_FROM (glyph, from);
4477 LGLYPH_SET_TO (glyph, to);
4479 return composition_gstring_put_cache (gstring, XINT (n));
4482 DEFUN ("font-variation-glyphs", Ffont_variation_glyphs, Sfont_variation_glyphs,
4483 2, 2, 0,
4484 doc: /* Return a list of variation glyphs for CHAR in FONT-OBJECT.
4485 Each element of the value is a cons (VARIATION-SELECTOR . GLYPH-ID),
4486 where
4487 VARIATION-SELECTOR is a chracter code of variation selection
4488 (#xFE00..#xFE0F or #xE0100..#xE01EF)
4489 GLYPH-ID is a glyph code of the corresponding variation glyph. */)
4490 (font_object, character)
4491 Lisp_Object font_object, character;
4493 unsigned variations[256];
4494 struct font *font;
4495 int i, n;
4496 Lisp_Object val;
4498 CHECK_FONT_OBJECT (font_object);
4499 CHECK_CHARACTER (character);
4500 font = XFONT_OBJECT (font_object);
4501 if (! font->driver->get_variation_glyphs)
4502 return Qnil;
4503 n = font->driver->get_variation_glyphs (font, XINT (character), variations);
4504 if (! n)
4505 return Qnil;
4506 val = Qnil;
4507 for (i = 0; i < 255; i++)
4508 if (variations[i])
4510 Lisp_Object code;
4511 int vs = (i < 16 ? 0xFE00 + i : 0xE0100 + (i - 16));
4512 /* Stops GCC whining about limited range of data type. */
4513 EMACS_INT var = variations[i];
4515 if (var > MOST_POSITIVE_FIXNUM)
4516 code = Fcons (make_number ((variations[i]) >> 16),
4517 make_number ((variations[i]) & 0xFFFF));
4518 else
4519 code = make_number (variations[i]);
4520 val = Fcons (Fcons (make_number (vs), code), val);
4522 return val;
4525 #if 0
4527 DEFUN ("font-drive-otf", Ffont_drive_otf, Sfont_drive_otf, 6, 6, 0,
4528 doc: /* Apply OpenType features on glyph-string GSTRING-IN.
4529 OTF-FEATURES specifies which features to apply in this format:
4530 (SCRIPT LANGSYS GSUB GPOS)
4531 where
4532 SCRIPT is a symbol specifying a script tag of OpenType,
4533 LANGSYS is a symbol specifying a langsys tag of OpenType,
4534 GSUB and GPOS, if non-nil, are lists of symbols specifying feature tags.
4536 If LANGYS is nil, the default langsys is selected.
4538 The features are applied in the order they appear in the list. The
4539 symbol `*' means to apply all available features not present in this
4540 list, and the remaining features are ignored. For instance, (vatu
4541 pstf * haln) is to apply vatu and pstf in this order, then to apply
4542 all available features other than vatu, pstf, and haln.
4544 The features are applied to the glyphs in the range FROM and TO of
4545 the glyph-string GSTRING-IN.
4547 If some feature is actually applicable, the resulting glyphs are
4548 produced in the glyph-string GSTRING-OUT from the index INDEX. In
4549 this case, the value is the number of produced glyphs.
4551 If no feature is applicable, no glyph is produced in GSTRING-OUT, and
4552 the value is 0.
4554 If GSTRING-OUT is too short to hold produced glyphs, no glyphs are
4555 produced in GSTRING-OUT, and the value is nil.
4557 See the documentation of `font-make-gstring' for the format of
4558 glyph-string. */)
4559 (otf_features, gstring_in, from, to, gstring_out, index)
4560 Lisp_Object otf_features, gstring_in, from, to, gstring_out, index;
4562 Lisp_Object font_object = LGSTRING_FONT (gstring_in);
4563 Lisp_Object val;
4564 struct font *font;
4565 int len, num;
4567 check_otf_features (otf_features);
4568 CHECK_FONT_OBJECT (font_object);
4569 font = XFONT_OBJECT (font_object);
4570 if (! font->driver->otf_drive)
4571 error ("Font backend %s can't drive OpenType GSUB table",
4572 SDATA (SYMBOL_NAME (font->driver->type)));
4573 CHECK_CONS (otf_features);
4574 CHECK_SYMBOL (XCAR (otf_features));
4575 val = XCDR (otf_features);
4576 CHECK_SYMBOL (XCAR (val));
4577 val = XCDR (otf_features);
4578 if (! NILP (val))
4579 CHECK_CONS (val);
4580 len = check_gstring (gstring_in);
4581 CHECK_VECTOR (gstring_out);
4582 CHECK_NATNUM (from);
4583 CHECK_NATNUM (to);
4584 CHECK_NATNUM (index);
4586 if (XINT (from) >= XINT (to) || XINT (to) > len)
4587 args_out_of_range_3 (from, to, make_number (len));
4588 if (XINT (index) >= ASIZE (gstring_out))
4589 args_out_of_range (index, make_number (ASIZE (gstring_out)));
4590 num = font->driver->otf_drive (font, otf_features,
4591 gstring_in, XINT (from), XINT (to),
4592 gstring_out, XINT (index), 0);
4593 if (num < 0)
4594 return Qnil;
4595 return make_number (num);
4598 DEFUN ("font-otf-alternates", Ffont_otf_alternates, Sfont_otf_alternates,
4599 3, 3, 0,
4600 doc: /* Return a list of alternate glyphs of CHARACTER in FONT-OBJECT.
4601 OTF-FEATURES specifies which features of the font FONT-OBJECT to apply
4602 in this format:
4603 (SCRIPT LANGSYS FEATURE ...)
4604 See the documentation of `font-drive-otf' for more detail.
4606 The value is a list of cons cells of the format (GLYPH-ID . CHARACTER),
4607 where GLYPH-ID is a glyph index of the font, and CHARACTER is a
4608 character code corresponding to the glyph or nil if there's no
4609 corresponding character. */)
4610 (font_object, character, otf_features)
4611 Lisp_Object font_object, character, otf_features;
4613 struct font *font;
4614 Lisp_Object gstring_in, gstring_out, g;
4615 Lisp_Object alternates;
4616 int i, num;
4618 CHECK_FONT_GET_OBJECT (font_object, font);
4619 if (! font->driver->otf_drive)
4620 error ("Font backend %s can't drive OpenType GSUB table",
4621 SDATA (SYMBOL_NAME (font->driver->type)));
4622 CHECK_CHARACTER (character);
4623 CHECK_CONS (otf_features);
4625 gstring_in = Ffont_make_gstring (font_object, make_number (1));
4626 g = LGSTRING_GLYPH (gstring_in, 0);
4627 LGLYPH_SET_CHAR (g, XINT (character));
4628 gstring_out = Ffont_make_gstring (font_object, make_number (10));
4629 while ((num = font->driver->otf_drive (font, otf_features, gstring_in, 0, 1,
4630 gstring_out, 0, 1)) < 0)
4631 gstring_out = Ffont_make_gstring (font_object,
4632 make_number (ASIZE (gstring_out) * 2));
4633 alternates = Qnil;
4634 for (i = 0; i < num; i++)
4636 Lisp_Object g = LGSTRING_GLYPH (gstring_out, i);
4637 int c = LGLYPH_CHAR (g);
4638 unsigned code = LGLYPH_CODE (g);
4640 alternates = Fcons (Fcons (make_number (code),
4641 c > 0 ? make_number (c) : Qnil),
4642 alternates);
4644 return Fnreverse (alternates);
4646 #endif /* 0 */
4648 #ifdef FONT_DEBUG
4650 DEFUN ("open-font", Fopen_font, Sopen_font, 1, 3, 0,
4651 doc: /* Open FONT-ENTITY. */)
4652 (font_entity, size, frame)
4653 Lisp_Object font_entity;
4654 Lisp_Object size;
4655 Lisp_Object frame;
4657 int isize;
4659 CHECK_FONT_ENTITY (font_entity);
4660 if (NILP (frame))
4661 frame = selected_frame;
4662 CHECK_LIVE_FRAME (frame);
4664 if (NILP (size))
4665 isize = XINT (AREF (font_entity, FONT_SIZE_INDEX));
4666 else
4668 CHECK_NUMBER_OR_FLOAT (size);
4669 if (FLOATP (size))
4670 isize = POINT_TO_PIXEL (XFLOAT_DATA (size), XFRAME (frame)->resy);
4671 else
4672 isize = XINT (size);
4673 if (isize == 0)
4674 isize = 120;
4676 return font_open_entity (XFRAME (frame), font_entity, isize);
4679 DEFUN ("close-font", Fclose_font, Sclose_font, 1, 2, 0,
4680 doc: /* Close FONT-OBJECT. */)
4681 (font_object, frame)
4682 Lisp_Object font_object, frame;
4684 CHECK_FONT_OBJECT (font_object);
4685 if (NILP (frame))
4686 frame = selected_frame;
4687 CHECK_LIVE_FRAME (frame);
4688 font_close_object (XFRAME (frame), font_object);
4689 return Qnil;
4692 DEFUN ("query-font", Fquery_font, Squery_font, 1, 1, 0,
4693 doc: /* Return information about FONT-OBJECT.
4694 The value is a vector:
4695 [ NAME FILENAME PIXEL-SIZE SIZE ASCENT DESCENT SPACE-WIDTH AVERAGE-WIDTH
4696 CAPABILITY ]
4698 NAME is a string of the font name (or nil if the font backend doesn't
4699 provide a name).
4701 FILENAME is a string of the font file (or nil if the font backend
4702 doesn't provide a file name).
4704 PIXEL-SIZE is a pixel size by which the font is opened.
4706 SIZE is a maximum advance width of the font in pixels.
4708 ASCENT, DESCENT, SPACE-WIDTH, AVERAGE-WIDTH are metrics of the font in
4709 pixels.
4711 CAPABILITY is a list whose first element is a symbol representing the
4712 font format \(x, opentype, truetype, type1, pcf, or bdf) and the
4713 remaining elements describe the details of the font capability.
4715 If the font is OpenType font, the form of the list is
4716 \(opentype GSUB GPOS)
4717 where GSUB shows which "GSUB" features the font supports, and GPOS
4718 shows which "GPOS" features the font supports. Both GSUB and GPOS are
4719 lists of the format:
4720 \((SCRIPT (LANGSYS FEATURE ...) ...) ...)
4722 If the font is not OpenType font, currently the length of the form is
4723 one.
4725 SCRIPT is a symbol representing OpenType script tag.
4727 LANGSYS is a symbol representing OpenType langsys tag, or nil
4728 representing the default langsys.
4730 FEATURE is a symbol representing OpenType feature tag.
4732 If the font is not OpenType font, CAPABILITY is nil. */)
4733 (font_object)
4734 Lisp_Object font_object;
4736 struct font *font;
4737 Lisp_Object val;
4739 CHECK_FONT_GET_OBJECT (font_object, font);
4741 val = Fmake_vector (make_number (9), Qnil);
4742 ASET (val, 0, AREF (font_object, FONT_NAME_INDEX));
4743 ASET (val, 1, AREF (font_object, FONT_FILE_INDEX));
4744 ASET (val, 2, make_number (font->pixel_size));
4745 ASET (val, 3, make_number (font->max_width));
4746 ASET (val, 4, make_number (font->ascent));
4747 ASET (val, 5, make_number (font->descent));
4748 ASET (val, 6, make_number (font->space_width));
4749 ASET (val, 7, make_number (font->average_width));
4750 if (font->driver->otf_capability)
4751 ASET (val, 8, Fcons (Qopentype, font->driver->otf_capability (font)));
4752 return val;
4755 DEFUN ("get-font-glyphs", Fget_font_glyphs, Sget_font_glyphs, 2, 2, 0,
4756 doc: /* Return a vector of glyphs of FONT-OBJECT for drawing STRING.
4757 Each element is a vector [GLYPH-CODE LBEARING RBEARING WIDTH ASCENT DESCENT]. */)
4758 (font_object, string)
4759 Lisp_Object font_object, string;
4761 struct font *font;
4762 int i, len;
4763 Lisp_Object vec;
4765 CHECK_FONT_GET_OBJECT (font_object, font);
4766 CHECK_STRING (string);
4767 len = SCHARS (string);
4768 vec = Fmake_vector (make_number (len), Qnil);
4769 for (i = 0; i < len; i++)
4771 Lisp_Object ch = Faref (string, make_number (i));
4772 Lisp_Object val;
4773 int c = XINT (ch);
4774 unsigned code;
4775 EMACS_INT cod;
4776 struct font_metrics metrics;
4778 cod = code = font->driver->encode_char (font, c);
4779 if (code == FONT_INVALID_CODE)
4780 continue;
4781 val = Fmake_vector (make_number (6), Qnil);
4782 if (cod <= MOST_POSITIVE_FIXNUM)
4783 ASET (val, 0, make_number (code));
4784 else
4785 ASET (val, 0, Fcons (make_number (code >> 16),
4786 make_number (code & 0xFFFF)));
4787 font->driver->text_extents (font, &code, 1, &metrics);
4788 ASET (val, 1, make_number (metrics.lbearing));
4789 ASET (val, 2, make_number (metrics.rbearing));
4790 ASET (val, 3, make_number (metrics.width));
4791 ASET (val, 4, make_number (metrics.ascent));
4792 ASET (val, 5, make_number (metrics.descent));
4793 ASET (vec, i, val);
4795 return vec;
4798 DEFUN ("font-match-p", Ffont_match_p, Sfont_match_p, 2, 2, 0,
4799 doc: /* Return t if and only if font-spec SPEC matches with FONT.
4800 FONT is a font-spec, font-entity, or font-object. */)
4801 (spec, font)
4802 Lisp_Object spec, font;
4804 CHECK_FONT_SPEC (spec);
4805 CHECK_FONT (font);
4807 return (font_match_p (spec, font) ? Qt : Qnil);
4810 DEFUN ("font-at", Ffont_at, Sfont_at, 1, 3, 0,
4811 doc: /* Return a font-object for displaying a character at POSITION.
4812 Optional second arg WINDOW, if non-nil, is a window displaying
4813 the current buffer. It defaults to the currently selected window. */)
4814 (position, window, string)
4815 Lisp_Object position, window, string;
4817 struct window *w;
4818 EMACS_INT pos;
4820 if (NILP (string))
4822 CHECK_NUMBER_COERCE_MARKER (position);
4823 pos = XINT (position);
4824 if (pos < BEGV || pos >= ZV)
4825 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
4827 else
4829 CHECK_NUMBER (position);
4830 CHECK_STRING (string);
4831 pos = XINT (position);
4832 if (pos < 0 || pos >= SCHARS (string))
4833 args_out_of_range (string, position);
4835 if (NILP (window))
4836 window = selected_window;
4837 CHECK_LIVE_WINDOW (window);
4838 w = XWINDOW (window);
4840 return font_at (-1, pos, NULL, w, string);
4843 #if 0
4844 DEFUN ("draw-string", Fdraw_string, Sdraw_string, 2, 2, 0,
4845 doc: /* Draw STRING by FONT-OBJECT on the top left corner of the current frame.
4846 The value is a number of glyphs drawn.
4847 Type C-l to recover what previously shown. */)
4848 (font_object, string)
4849 Lisp_Object font_object, string;
4851 Lisp_Object frame = selected_frame;
4852 FRAME_PTR f = XFRAME (frame);
4853 struct font *font;
4854 struct face *face;
4855 int i, len, width;
4856 unsigned *code;
4858 CHECK_FONT_GET_OBJECT (font_object, font);
4859 CHECK_STRING (string);
4860 len = SCHARS (string);
4861 code = alloca (sizeof (unsigned) * len);
4862 for (i = 0; i < len; i++)
4864 Lisp_Object ch = Faref (string, make_number (i));
4865 Lisp_Object val;
4866 int c = XINT (ch);
4868 code[i] = font->driver->encode_char (font, c);
4869 if (code[i] == FONT_INVALID_CODE)
4870 break;
4872 face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4873 face->fontp = font;
4874 if (font->driver->prepare_face)
4875 font->driver->prepare_face (f, face);
4876 width = font->driver->text_extents (font, code, i, NULL);
4877 len = font->driver->draw_text (f, face, 0, font->ascent, code, i, width);
4878 if (font->driver->done_face)
4879 font->driver->done_face (f, face);
4880 face->fontp = NULL;
4881 return make_number (len);
4883 #endif
4885 #endif /* FONT_DEBUG */
4887 #ifdef HAVE_WINDOW_SYSTEM
4889 DEFUN ("font-info", Ffont_info, Sfont_info, 1, 2, 0,
4890 doc: /* Return information about a font named NAME on frame FRAME.
4891 If FRAME is omitted or nil, use the selected frame.
4892 The returned value is a vector of OPENED-NAME, FULL-NAME, CHARSET, SIZE,
4893 HEIGHT, BASELINE-OFFSET, RELATIVE-COMPOSE, and DEFAULT-ASCENT,
4894 where
4895 OPENED-NAME is the name used for opening the font,
4896 FULL-NAME is the full name of the font,
4897 SIZE is the maximum bound width of the font,
4898 HEIGHT is the height of the font,
4899 BASELINE-OFFSET is the upward offset pixels from ASCII baseline,
4900 RELATIVE-COMPOSE and DEFAULT-ASCENT are the numbers controlling
4901 how to compose characters.
4902 If the named font is not yet loaded, return nil. */)
4903 (name, frame)
4904 Lisp_Object name, frame;
4906 FRAME_PTR f;
4907 struct font *font;
4908 Lisp_Object info;
4909 Lisp_Object font_object;
4911 (*check_window_system_func) ();
4913 if (! FONTP (name))
4914 CHECK_STRING (name);
4915 if (NILP (frame))
4916 frame = selected_frame;
4917 CHECK_LIVE_FRAME (frame);
4918 f = XFRAME (frame);
4920 if (STRINGP (name))
4922 int fontset = fs_query_fontset (name, 0);
4924 if (fontset >= 0)
4925 name = fontset_ascii (fontset);
4926 font_object = font_open_by_name (f, (char *) SDATA (name));
4928 else if (FONT_OBJECT_P (name))
4929 font_object = name;
4930 else if (FONT_ENTITY_P (name))
4931 font_object = font_open_entity (f, name, 0);
4932 else
4934 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4935 Lisp_Object entity = font_matching_entity (f, face->lface, name);
4937 font_object = ! NILP (entity) ? font_open_entity (f, entity, 0) : Qnil;
4939 if (NILP (font_object))
4940 return Qnil;
4941 font = XFONT_OBJECT (font_object);
4943 info = Fmake_vector (make_number (7), Qnil);
4944 XVECTOR (info)->contents[0] = AREF (font_object, FONT_NAME_INDEX);
4945 XVECTOR (info)->contents[1] = AREF (font_object, FONT_NAME_INDEX);
4946 XVECTOR (info)->contents[2] = make_number (font->pixel_size);
4947 XVECTOR (info)->contents[3] = make_number (font->height);
4948 XVECTOR (info)->contents[4] = make_number (font->baseline_offset);
4949 XVECTOR (info)->contents[5] = make_number (font->relative_compose);
4950 XVECTOR (info)->contents[6] = make_number (font->default_ascent);
4952 #if 0
4953 /* As font_object is still in FONT_OBJLIST of the entity, we can't
4954 close it now. Perhaps, we should manage font-objects
4955 by `reference-count'. */
4956 font_close_object (f, font_object);
4957 #endif
4958 return info;
4960 #endif
4963 #define BUILD_STYLE_TABLE(TBL) \
4964 build_style_table ((TBL), sizeof TBL / sizeof (struct table_entry))
4966 static Lisp_Object
4967 build_style_table (entry, nelement)
4968 struct table_entry *entry;
4969 int nelement;
4971 int i, j;
4972 Lisp_Object table, elt;
4974 table = Fmake_vector (make_number (nelement), Qnil);
4975 for (i = 0; i < nelement; i++)
4977 for (j = 0; entry[i].names[j]; j++);
4978 elt = Fmake_vector (make_number (j + 1), Qnil);
4979 ASET (elt, 0, make_number (entry[i].numeric));
4980 for (j = 0; entry[i].names[j]; j++)
4981 ASET (elt, j + 1, intern (entry[i].names[j]));
4982 ASET (table, i, elt);
4984 return table;
4987 static Lisp_Object Vfont_log;
4988 static int font_log_env_checked;
4990 /* The deferred font-log data of the form [ACTION ARG RESULT].
4991 If ACTION is not nil, that is added to the log when font_add_log is
4992 called next time. At that time, ACTION is set back to nil. */
4993 static Lisp_Object Vfont_log_deferred;
4995 /* Prepend the font-related logging data in Vfont_log if it is not
4996 `t'. ACTION describes a kind of font-related action (e.g. listing,
4997 opening), ARG is the argument for the action, and RESULT is the
4998 result of the action. */
4999 void
5000 font_add_log (action, arg, result)
5001 char *action;
5002 Lisp_Object arg, result;
5004 Lisp_Object tail, val;
5005 int i;
5007 if (! font_log_env_checked)
5009 Vfont_log = egetenv ("EMACS_FONT_LOG") ? Qnil : Qt;
5010 font_log_env_checked = 1;
5012 if (EQ (Vfont_log, Qt))
5013 return;
5014 if (STRINGP (AREF (Vfont_log_deferred, 0)))
5016 char *str = (char *) SDATA (AREF (Vfont_log_deferred, 0));
5018 ASET (Vfont_log_deferred, 0, Qnil);
5019 font_add_log (str, AREF (Vfont_log_deferred, 1),
5020 AREF (Vfont_log_deferred, 2));
5023 if (FONTP (arg))
5025 Lisp_Object tail, elt;
5026 Lisp_Object equalstr = build_string ("=");
5028 val = Ffont_xlfd_name (arg, Qt);
5029 for (tail = AREF (arg, FONT_EXTRA_INDEX); CONSP (tail);
5030 tail = XCDR (tail))
5032 elt = XCAR (tail);
5033 if (EQ (XCAR (elt), QCscript)
5034 && SYMBOLP (XCDR (elt)))
5035 val = concat3 (val, SYMBOL_NAME (QCscript),
5036 concat2 (equalstr, SYMBOL_NAME (XCDR (elt))));
5037 else if (EQ (XCAR (elt), QClang)
5038 && SYMBOLP (XCDR (elt)))
5039 val = concat3 (val, SYMBOL_NAME (QClang),
5040 concat2 (equalstr, SYMBOL_NAME (XCDR (elt))));
5041 else if (EQ (XCAR (elt), QCotf)
5042 && CONSP (XCDR (elt)) && SYMBOLP (XCAR (XCDR (elt))))
5043 val = concat3 (val, SYMBOL_NAME (QCotf),
5044 concat2 (equalstr,
5045 SYMBOL_NAME (XCAR (XCDR (elt)))));
5047 arg = val;
5049 if (FONTP (result))
5051 val = Ffont_xlfd_name (result, Qt);
5052 if (! FONT_SPEC_P (result))
5053 val = concat3 (SYMBOL_NAME (AREF (result, FONT_TYPE_INDEX)),
5054 build_string (":"), val);
5055 result = val;
5057 else if (CONSP (result))
5059 result = Fcopy_sequence (result);
5060 for (tail = result; CONSP (tail); tail = XCDR (tail))
5062 val = XCAR (tail);
5063 if (FONTP (val))
5064 val = Ffont_xlfd_name (val, Qt);
5065 XSETCAR (tail, val);
5068 else if (VECTORP (result))
5070 result = Fcopy_sequence (result);
5071 for (i = 0; i < ASIZE (result); i++)
5073 val = AREF (result, i);
5074 if (FONTP (val))
5075 val = Ffont_xlfd_name (val, Qt);
5076 ASET (result, i, val);
5079 Vfont_log = Fcons (list3 (intern (action), arg, result), Vfont_log);
5082 /* Record a font-related logging data to be added to Vfont_log when
5083 font_add_log is called next time. ACTION, ARG, RESULT are the same
5084 as font_add_log. */
5086 void
5087 font_deferred_log (action, arg, result)
5088 char *action;
5089 Lisp_Object arg, result;
5091 ASET (Vfont_log_deferred, 0, build_string (action));
5092 ASET (Vfont_log_deferred, 1, arg);
5093 ASET (Vfont_log_deferred, 2, result);
5096 extern void syms_of_ftfont P_ (());
5097 extern void syms_of_xfont P_ (());
5098 extern void syms_of_xftfont P_ (());
5099 extern void syms_of_ftxfont P_ (());
5100 extern void syms_of_bdffont P_ (());
5101 extern void syms_of_w32font P_ (());
5102 extern void syms_of_atmfont P_ (());
5103 extern void syms_of_nsfont P_ (());
5105 void
5106 syms_of_font ()
5108 sort_shift_bits[FONT_TYPE_INDEX] = 0;
5109 sort_shift_bits[FONT_SLANT_INDEX] = 2;
5110 sort_shift_bits[FONT_WEIGHT_INDEX] = 9;
5111 sort_shift_bits[FONT_SIZE_INDEX] = 16;
5112 sort_shift_bits[FONT_WIDTH_INDEX] = 23;
5113 /* Note that the other elements in sort_shift_bits are not used. */
5115 staticpro (&font_charset_alist);
5116 font_charset_alist = Qnil;
5118 DEFSYM (Qopentype, "opentype");
5120 DEFSYM (Qascii_0, "ascii-0");
5121 DEFSYM (Qiso8859_1, "iso8859-1");
5122 DEFSYM (Qiso10646_1, "iso10646-1");
5123 DEFSYM (Qunicode_bmp, "unicode-bmp");
5124 DEFSYM (Qunicode_sip, "unicode-sip");
5126 DEFSYM (QCf, "Cf");
5128 DEFSYM (QCotf, ":otf");
5129 DEFSYM (QClang, ":lang");
5130 DEFSYM (QCscript, ":script");
5131 DEFSYM (QCantialias, ":antialias");
5133 DEFSYM (QCfoundry, ":foundry");
5134 DEFSYM (QCadstyle, ":adstyle");
5135 DEFSYM (QCregistry, ":registry");
5136 DEFSYM (QCspacing, ":spacing");
5137 DEFSYM (QCdpi, ":dpi");
5138 DEFSYM (QCscalable, ":scalable");
5139 DEFSYM (QCavgwidth, ":avgwidth");
5140 DEFSYM (QCfont_entity, ":font-entity");
5141 DEFSYM (QCfc_unknown_spec, ":fc-unknown-spec");
5143 DEFSYM (Qc, "c");
5144 DEFSYM (Qm, "m");
5145 DEFSYM (Qp, "p");
5146 DEFSYM (Qd, "d");
5148 staticpro (&null_vector);
5149 null_vector = Fmake_vector (make_number (0), Qnil);
5151 staticpro (&scratch_font_spec);
5152 scratch_font_spec = Ffont_spec (0, NULL);
5153 staticpro (&scratch_font_prefer);
5154 scratch_font_prefer = Ffont_spec (0, NULL);
5156 staticpro (&Vfont_log_deferred);
5157 Vfont_log_deferred = Fmake_vector (make_number (3), Qnil);
5159 #if 0
5160 #ifdef HAVE_LIBOTF
5161 staticpro (&otf_list);
5162 otf_list = Qnil;
5163 #endif /* HAVE_LIBOTF */
5164 #endif /* 0 */
5166 defsubr (&Sfontp);
5167 defsubr (&Sfont_spec);
5168 defsubr (&Sfont_get);
5169 #ifdef HAVE_WINDOW_SYSTEM
5170 defsubr (&Sfont_face_attributes);
5171 #endif
5172 defsubr (&Sfont_put);
5173 defsubr (&Slist_fonts);
5174 defsubr (&Sfont_family_list);
5175 defsubr (&Sfind_font);
5176 defsubr (&Sfont_xlfd_name);
5177 defsubr (&Sclear_font_cache);
5178 defsubr (&Sfont_shape_gstring);
5179 defsubr (&Sfont_variation_glyphs);
5180 #if 0
5181 defsubr (&Sfont_drive_otf);
5182 defsubr (&Sfont_otf_alternates);
5183 #endif /* 0 */
5185 #ifdef FONT_DEBUG
5186 defsubr (&Sopen_font);
5187 defsubr (&Sclose_font);
5188 defsubr (&Squery_font);
5189 defsubr (&Sget_font_glyphs);
5190 defsubr (&Sfont_match_p);
5191 defsubr (&Sfont_at);
5192 #if 0
5193 defsubr (&Sdraw_string);
5194 #endif
5195 #endif /* FONT_DEBUG */
5196 #ifdef HAVE_WINDOW_SYSTEM
5197 defsubr (&Sfont_info);
5198 #endif
5200 DEFVAR_LISP ("font-encoding-alist", &Vfont_encoding_alist,
5201 doc: /*
5202 Alist of fontname patterns vs the corresponding encoding and repertory info.
5203 Each element looks like (REGEXP . (ENCODING . REPERTORY)),
5204 where ENCODING is a charset or a char-table,
5205 and REPERTORY is a charset, a char-table, or nil.
5207 If ENCODING and REPERTORY are the same, the element can have the form
5208 \(REGEXP . ENCODING).
5210 ENCODING is for converting a character to a glyph code of the font.
5211 If ENCODING is a charset, encoding a character by the charset gives
5212 the corresponding glyph code. If ENCODING is a char-table, looking up
5213 the table by a character gives the corresponding glyph code.
5215 REPERTORY specifies a repertory of characters supported by the font.
5216 If REPERTORY is a charset, all characters beloging to the charset are
5217 supported. If REPERTORY is a char-table, all characters who have a
5218 non-nil value in the table are supported. If REPERTORY is nil, Emacs
5219 gets the repertory information by an opened font and ENCODING. */);
5220 Vfont_encoding_alist = Qnil;
5222 DEFVAR_LISP_NOPRO ("font-weight-table", &Vfont_weight_table,
5223 doc: /* Vector of valid font weight values.
5224 Each element has the form:
5225 [NUMERIC-VALUE SYMBOLIC-NAME ALIAS-NAME ...]
5226 NUMERIC-VALUE is an integer, and SYMBOLIC-NAME and ALIAS-NAME are symbols. */);
5227 Vfont_weight_table = BUILD_STYLE_TABLE (weight_table);
5229 DEFVAR_LISP_NOPRO ("font-slant-table", &Vfont_slant_table,
5230 doc: /* Vector of font slant symbols vs the corresponding numeric values.
5231 See `font-weight-table' for the format of the vector. */);
5232 Vfont_slant_table = BUILD_STYLE_TABLE (slant_table);
5234 DEFVAR_LISP_NOPRO ("font-width-table", &Vfont_width_table,
5235 doc: /* Alist of font width symbols vs the corresponding numeric values.
5236 See `font-weight-table' for the format of the vector. */);
5237 Vfont_width_table = BUILD_STYLE_TABLE (width_table);
5239 staticpro (&font_style_table);
5240 font_style_table = Fmake_vector (make_number (3), Qnil);
5241 ASET (font_style_table, 0, Vfont_weight_table);
5242 ASET (font_style_table, 1, Vfont_slant_table);
5243 ASET (font_style_table, 2, Vfont_width_table);
5245 DEFVAR_LISP ("font-log", &Vfont_log, doc: /*
5246 *Logging list of font related actions and results.
5247 The value t means to suppress the logging.
5248 The initial value is set to nil if the environment variable
5249 EMACS_FONT_LOG is set. Otherwise, it is set to t. */);
5250 Vfont_log = Qnil;
5252 #ifdef HAVE_WINDOW_SYSTEM
5253 #ifdef HAVE_FREETYPE
5254 syms_of_ftfont ();
5255 #ifdef HAVE_X_WINDOWS
5256 syms_of_xfont ();
5257 syms_of_ftxfont ();
5258 #ifdef HAVE_XFT
5259 syms_of_xftfont ();
5260 #endif /* HAVE_XFT */
5261 #endif /* HAVE_X_WINDOWS */
5262 #else /* not HAVE_FREETYPE */
5263 #ifdef HAVE_X_WINDOWS
5264 syms_of_xfont ();
5265 #endif /* HAVE_X_WINDOWS */
5266 #endif /* not HAVE_FREETYPE */
5267 #ifdef HAVE_BDFFONT
5268 syms_of_bdffont ();
5269 #endif /* HAVE_BDFFONT */
5270 #ifdef WINDOWSNT
5271 syms_of_w32font ();
5272 #endif /* WINDOWSNT */
5273 #ifdef HAVE_NS
5274 syms_of_nsfont ();
5275 #endif /* HAVE_NS */
5276 #endif /* HAVE_WINDOW_SYSTEM */
5279 /* arch-tag: 74c9475d-5976-4c93-a327-942ae3072846
5280 (do not change this comment) */