(fill_gstring_header): Fix copy/paste typo.
[emacs.git] / src / font.c
bloba8753ba7d62df064b3831ed8a1face3a9758680e
1 /* font.c -- "Font" primitives.
2 Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
3 Copyright (C) 2006, 2007, 2008
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 Lisp_Object Qfont_spec, Qfont_entity, Qfont_object;
52 #ifdef HAVE_NS
53 extern Lisp_Object Qfontsize;
54 #endif
56 Lisp_Object Qopentype;
58 /* Important character set strings. */
59 Lisp_Object Qascii_0, Qiso8859_1, Qiso10646_1, Qunicode_bmp, Qunicode_sip;
61 #ifdef HAVE_NS
62 #define DEFAULT_ENCODING Qiso10646_1
63 #else
64 #define DEFAULT_ENCODING Qiso8859_1
65 #endif
67 /* Unicode category `Cf'. */
68 static Lisp_Object QCf;
70 /* Special vector of zero length. This is repeatedly used by (struct
71 font_driver *)->list when a specified font is not found. */
72 static Lisp_Object null_vector;
74 static Lisp_Object Vfont_weight_table, Vfont_slant_table, Vfont_width_table;
76 /* Vector of Vfont_weight_table, Vfont_slant_table, and Vfont_width_table. */
77 static Lisp_Object font_style_table;
79 /* Structure used for tables mapping weight, slant, and width numeric
80 values and their names. */
82 struct table_entry
84 int numeric;
85 /* The first one is a valid name as a face attribute.
86 The second one (if any) is a typical name in XLFD field. */
87 char *names[5];
88 Lisp_Object *symbols;
91 /* Table of weight numeric values and their names. This table must be
92 sorted by numeric values in ascending order. */
94 static struct table_entry weight_table[] =
96 { 0, { "thin" }},
97 { 20, { "ultra-light", "ultralight" }},
98 { 40, { "extra-light", "extralight" }},
99 { 50, { "light" }},
100 { 75, { "semi-light", "semilight", "demilight", "book" }},
101 { 100, { "normal", "medium", "regular" }},
102 { 180, { "semi-bold", "semibold", "demibold", "demi" }},
103 { 200, { "bold" }},
104 { 205, { "extra-bold", "extrabold" }},
105 { 210, { "ultra-bold", "ultrabold", "black" }}
108 /* Table of slant numeric values and their names. This table must be
109 sorted by numeric values in ascending order. */
111 static struct table_entry slant_table[] =
113 { 0, { "reverse-oblique", "ro" }},
114 { 10, { "reverse-italic", "ri" }},
115 { 100, { "normal", "r" }},
116 { 200, { "italic" ,"i", "ot" }},
117 { 210, { "oblique", "o" }}
120 /* Table of width numeric values and their names. This table must be
121 sorted by numeric values in ascending order. */
123 static struct table_entry width_table[] =
125 { 50, { "ultra-condensed", "ultracondensed" }},
126 { 63, { "extra-condensed", "extracondensed" }},
127 { 75, { "condensed", "compressed", "narrow" }},
128 { 87, { "semi-condensed", "semicondensed", "demicondensed" }},
129 { 100, { "normal", "medium", "regular" }},
130 { 113, { "semi-expanded", "semiexpanded", "demiexpanded" }},
131 { 125, { "expanded" }},
132 { 150, { "extra-expanded", "extraexpanded" }},
133 { 200, { "ultra-expanded", "ultraexpanded", "wide" }}
136 extern Lisp_Object Qnormal;
138 /* Symbols representing keys of normal font properties. */
139 extern Lisp_Object QCtype, QCfamily, QCweight, QCslant, QCwidth;
140 extern Lisp_Object QCheight, QCsize, QCname;
142 Lisp_Object QCfoundry, QCadstyle, QCregistry;
143 /* Symbols representing keys of font extra info. */
144 Lisp_Object QCspacing, QCdpi, QCscalable, QCotf, QClang, QCscript, QCavgwidth;
145 Lisp_Object QCantialias, QCfont_entity, QCfc_unknown_spec;
146 /* Symbols representing values of font spacing property. */
147 Lisp_Object Qc, Qm, Qp, Qd;
149 Lisp_Object Vfont_encoding_alist;
151 /* Alist of font registry symbol and the corresponding charsets
152 information. The information is retrieved from
153 Vfont_encoding_alist on demand.
155 Eash element has the form:
156 (REGISTRY . (ENCODING-CHARSET-ID . REPERTORY-CHARSET-ID))
158 (REGISTRY . nil)
160 In the former form, ENCODING-CHARSET-ID is an ID of a charset that
161 encodes a character code to a glyph code of a font, and
162 REPERTORY-CHARSET-ID is an ID of a charset that tells if a
163 character is supported by a font.
165 The latter form means that the information for REGISTRY couldn't be
166 retrieved. */
167 static Lisp_Object font_charset_alist;
169 /* List of all font drivers. Each font-backend (XXXfont.c) calls
170 register_font_driver in syms_of_XXXfont to register its font-driver
171 here. */
172 static struct font_driver_list *font_driver_list;
176 /* Creaters of font-related Lisp object. */
178 Lisp_Object
179 font_make_spec ()
181 Lisp_Object font_spec;
182 struct font_spec *spec
183 = ((struct font_spec *)
184 allocate_pseudovector (VECSIZE (struct font_spec),
185 FONT_SPEC_MAX, PVEC_FONT));
186 XSETFONT (font_spec, spec);
187 return font_spec;
190 Lisp_Object
191 font_make_entity ()
193 Lisp_Object font_entity;
194 struct font_entity *entity
195 = ((struct font_entity *)
196 allocate_pseudovector (VECSIZE (struct font_entity),
197 FONT_ENTITY_MAX, PVEC_FONT));
198 XSETFONT (font_entity, entity);
199 return font_entity;
202 /* Create a font-object whose structure size is SIZE. If ENTITY is
203 not nil, copy properties from ENTITY to the font-object. If
204 PIXELSIZE is positive, set the `size' property to PIXELSIZE. */
205 Lisp_Object
206 font_make_object (size, entity, pixelsize)
207 int size;
208 Lisp_Object entity;
209 int pixelsize;
211 Lisp_Object font_object;
212 struct font *font
213 = (struct font *) allocate_pseudovector (size, FONT_OBJECT_MAX, PVEC_FONT);
214 int i;
216 XSETFONT (font_object, font);
218 if (! NILP (entity))
220 for (i = 1; i < FONT_SPEC_MAX; i++)
221 font->props[i] = AREF (entity, i);
222 if (! NILP (AREF (entity, FONT_EXTRA_INDEX)))
223 font->props[FONT_EXTRA_INDEX]
224 = Fcopy_sequence (AREF (entity, FONT_EXTRA_INDEX));
226 if (size > 0)
227 font->props[FONT_SIZE_INDEX] = make_number (pixelsize);
228 return font_object;
233 static int font_pixel_size P_ ((FRAME_PTR f, Lisp_Object));
234 static Lisp_Object font_open_entity P_ ((FRAME_PTR, Lisp_Object, int));
235 static Lisp_Object font_matching_entity P_ ((FRAME_PTR, Lisp_Object *,
236 Lisp_Object));
238 /* Number of registered font drivers. */
239 static int num_font_drivers;
242 /* Return a Lispy value of a font property value at STR and LEN bytes.
243 If STR is "*", it returns nil.
244 If FORCE_SYMBOL is zero and all characters in STR are digits, it
245 returns an integer. Otherwise, it returns a symbol interned from
246 STR. */
248 Lisp_Object
249 font_intern_prop (str, len, force_symbol)
250 char *str;
251 int len;
252 int force_symbol;
254 int i;
255 Lisp_Object tem;
256 Lisp_Object obarray;
258 if (len == 1 && *str == '*')
259 return Qnil;
260 if (!force_symbol && len >=1 && isdigit (*str))
262 for (i = 1; i < len; i++)
263 if (! isdigit (str[i]))
264 break;
265 if (i == len)
266 return make_number (atoi (str));
269 /* The following code is copied from the function intern (in lread.c). */
270 obarray = Vobarray;
271 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
272 obarray = check_obarray (obarray);
273 tem = oblookup (obarray, str, len, len);
274 if (SYMBOLP (tem))
275 return tem;
276 return Fintern (make_unibyte_string (str, len), obarray);
279 /* Return a pixel size of font-spec SPEC on frame F. */
281 static int
282 font_pixel_size (f, spec)
283 FRAME_PTR f;
284 Lisp_Object spec;
286 #ifdef HAVE_WINDOW_SYSTEM
287 Lisp_Object size = AREF (spec, FONT_SIZE_INDEX);
288 double point_size;
289 int dpi, pixel_size;
290 Lisp_Object val;
292 if (INTEGERP (size))
293 return XINT (size);
294 if (NILP (size))
295 return 0;
296 font_assert (FLOATP (size));
297 point_size = XFLOAT_DATA (size);
298 val = AREF (spec, FONT_DPI_INDEX);
299 if (INTEGERP (val))
300 dpi = XINT (val);
301 else
302 dpi = f->resy;
303 pixel_size = POINT_TO_PIXEL (point_size, dpi);
304 return pixel_size;
305 #else
306 return 1;
307 #endif
311 /* Return a value of PROP's VAL (symbol or integer) to be stored in a
312 font vector. If VAL is not valid (i.e. not registered in
313 font_style_table), return -1 if NOERROR is zero, and return a
314 proper index if NOERROR is nonzero. In that case, register VAL in
315 font_style_table if VAL is a symbol, and return a closest index if
316 VAL is an integer. */
319 font_style_to_value (prop, val, noerror)
320 enum font_property_index prop;
321 Lisp_Object val;
322 int noerror;
324 Lisp_Object table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
325 int len = ASIZE (table);
326 int i, j;
328 if (SYMBOLP (val))
330 unsigned char *s;
331 Lisp_Object args[2], elt;
333 /* At first try exact match. */
334 for (i = 0; i < len; i++)
335 for (j = 1; j < ASIZE (AREF (table, i)); j++)
336 if (EQ (val, AREF (AREF (table, i), j)))
337 return ((XINT (AREF (AREF (table, i), 0)) << 8)
338 | (i << 4) | (j - 1));
339 /* Try also with case-folding match. */
340 s = SDATA (SYMBOL_NAME (val));
341 for (i = 0; i < len; i++)
342 for (j = 1; j < ASIZE (AREF (table, i)); j++)
344 elt = AREF (AREF (table, i), j);
345 if (xstrcasecmp (s, SDATA (SYMBOL_NAME (elt))) == 0)
346 return ((XINT (AREF (AREF (table, i), 0)) << 8)
347 | (i << 4) | (j - 1));
349 if (! noerror)
350 return -1;
351 if (len == 255)
352 abort ();
353 elt = Fmake_vector (make_number (2), make_number (255));
354 ASET (elt, 1, val);
355 args[0] = table;
356 args[1] = Fmake_vector (make_number (1), elt);
357 ASET (font_style_table, prop - FONT_WEIGHT_INDEX, Fvconcat (2, args));
358 return (255 << 8) | (i << 4);
360 else
362 int i, last_n;
363 int numeric = XINT (val);
365 for (i = 0, last_n = -1; i < len; i++)
367 int n = XINT (AREF (AREF (table, i), 0));
369 if (numeric == n)
370 return (n << 8) | (i << 4);
371 if (numeric < n)
373 if (! noerror)
374 return -1;
375 return ((i == 0 || n - numeric < numeric - last_n)
376 ? (n << 8) | (i << 4): (last_n << 8 | ((i - 1) << 4)));
378 last_n = n;
380 if (! noerror)
381 return -1;
382 return ((last_n << 8) | ((i - 1) << 4));
386 Lisp_Object
387 font_style_symbolic (font, prop, for_face)
388 Lisp_Object font;
389 enum font_property_index prop;
390 int for_face;
392 Lisp_Object val = AREF (font, prop);
393 Lisp_Object table, elt;
394 int i;
396 if (NILP (val))
397 return Qnil;
398 table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
399 i = XINT (val) & 0xFF;
400 font_assert (((i >> 4) & 0xF) < ASIZE (table));
401 elt = AREF (table, ((i >> 4) & 0xF));
402 font_assert ((i & 0xF) + 1 < ASIZE (elt));
403 return (for_face ? AREF (elt, 1) : AREF (elt, (i & 0xF) + 1));
406 extern Lisp_Object Vface_alternative_font_family_alist;
408 extern Lisp_Object find_font_encoding P_ ((Lisp_Object));
411 /* Return ENCODING or a cons of ENCODING and REPERTORY of the font
412 FONTNAME. ENCODING is a charset symbol that specifies the encoding
413 of the font. REPERTORY is a charset symbol or nil. */
415 Lisp_Object
416 find_font_encoding (fontname)
417 Lisp_Object fontname;
419 Lisp_Object tail, elt;
421 for (tail = Vfont_encoding_alist; CONSP (tail); tail = XCDR (tail))
423 elt = XCAR (tail);
424 if (CONSP (elt)
425 && STRINGP (XCAR (elt))
426 && fast_string_match_ignore_case (XCAR (elt), fontname) >= 0
427 && (SYMBOLP (XCDR (elt))
428 ? CHARSETP (XCDR (elt))
429 : CONSP (XCDR (elt)) && CHARSETP (XCAR (XCDR (elt)))))
430 return (XCDR (elt));
432 /* We don't know the encoding of this font. Let's assume `ascii'. */
433 return Qascii;
436 /* Return encoding charset and repertory charset for REGISTRY in
437 ENCODING and REPERTORY correspondingly. If correct information for
438 REGISTRY is available, return 0. Otherwise return -1. */
441 font_registry_charsets (registry, encoding, repertory)
442 Lisp_Object registry;
443 struct charset **encoding, **repertory;
445 Lisp_Object val;
446 int encoding_id, repertory_id;
448 val = Fassoc_string (registry, font_charset_alist, Qt);
449 if (! NILP (val))
451 val = XCDR (val);
452 if (NILP (val))
453 return -1;
454 encoding_id = XINT (XCAR (val));
455 repertory_id = XINT (XCDR (val));
457 else
459 val = find_font_encoding (SYMBOL_NAME (registry));
460 if (SYMBOLP (val) && CHARSETP (val))
462 encoding_id = repertory_id = XINT (CHARSET_SYMBOL_ID (val));
464 else if (CONSP (val))
466 if (! CHARSETP (XCAR (val)))
467 goto invalid_entry;
468 encoding_id = XINT (CHARSET_SYMBOL_ID (XCAR (val)));
469 if (NILP (XCDR (val)))
470 repertory_id = -1;
471 else
473 if (! CHARSETP (XCDR (val)))
474 goto invalid_entry;
475 repertory_id = XINT (CHARSET_SYMBOL_ID (XCDR (val)));
478 else
479 goto invalid_entry;
480 val = Fcons (make_number (encoding_id), make_number (repertory_id));
481 font_charset_alist
482 = nconc2 (font_charset_alist, Fcons (Fcons (registry, val), Qnil));
485 if (encoding)
486 *encoding = CHARSET_FROM_ID (encoding_id);
487 if (repertory)
488 *repertory = repertory_id >= 0 ? CHARSET_FROM_ID (repertory_id) : NULL;
489 return 0;
491 invalid_entry:
492 font_charset_alist
493 = nconc2 (font_charset_alist, Fcons (Fcons (registry, Qnil), Qnil));
494 return -1;
498 /* Font property value validaters. See the comment of
499 font_property_table for the meaning of the arguments. */
501 static Lisp_Object font_prop_validate P_ ((int, Lisp_Object, Lisp_Object));
502 static Lisp_Object font_prop_validate_symbol P_ ((Lisp_Object, Lisp_Object));
503 static Lisp_Object font_prop_validate_style P_ ((Lisp_Object, Lisp_Object));
504 static Lisp_Object font_prop_validate_non_neg P_ ((Lisp_Object, Lisp_Object));
505 static Lisp_Object font_prop_validate_spacing P_ ((Lisp_Object, Lisp_Object));
506 static int get_font_prop_index P_ ((Lisp_Object));
508 static Lisp_Object
509 font_prop_validate_symbol (prop, val)
510 Lisp_Object prop, val;
512 if (STRINGP (val))
513 val = Fintern (val, Qnil);
514 if (! SYMBOLP (val))
515 val = Qerror;
516 else if (EQ (prop, QCregistry))
517 val = Fintern (Fdowncase (SYMBOL_NAME (val)), Qnil);
518 return val;
522 static Lisp_Object
523 font_prop_validate_style (style, val)
524 Lisp_Object style, val;
526 enum font_property_index prop = (EQ (style, QCweight) ? FONT_WEIGHT_INDEX
527 : EQ (style, QCslant) ? FONT_SLANT_INDEX
528 : FONT_WIDTH_INDEX);
529 int n;
530 if (INTEGERP (val))
532 n = XINT (val);
533 if (((n >> 4) & 0xF)
534 >= ASIZE (AREF (font_style_table, prop - FONT_WEIGHT_INDEX)))
535 val = Qerror;
536 else
538 Lisp_Object elt = AREF (AREF (font_style_table, prop - FONT_WEIGHT_INDEX), (n >> 4) & 0xF);
540 if ((n & 0xF) + 1 >= ASIZE (elt))
541 val = Qerror;
542 else if (XINT (AREF (elt, 0)) != (n >> 8))
543 val = Qerror;
546 else if (SYMBOLP (val))
548 int n = font_style_to_value (prop, val, 0);
550 val = n >= 0 ? make_number (n) : Qerror;
552 else
553 val = Qerror;
554 return val;
557 static Lisp_Object
558 font_prop_validate_non_neg (prop, val)
559 Lisp_Object prop, val;
561 return (NATNUMP (val) || (FLOATP (val) && XFLOAT_DATA (val) >= 0)
562 ? val : Qerror);
565 static Lisp_Object
566 font_prop_validate_spacing (prop, val)
567 Lisp_Object prop, val;
569 if (NILP (val) || (NATNUMP (val) && XINT (val) <= FONT_SPACING_CHARCELL))
570 return val;
571 if (SYMBOLP (val) && SBYTES (SYMBOL_NAME (val)) == 1)
573 char spacing = SDATA (SYMBOL_NAME (val))[0];
575 if (spacing == 'c' || spacing == 'C')
576 return make_number (FONT_SPACING_CHARCELL);
577 if (spacing == 'm' || spacing == 'M')
578 return make_number (FONT_SPACING_MONO);
579 if (spacing == 'p' || spacing == 'P')
580 return make_number (FONT_SPACING_PROPORTIONAL);
581 if (spacing == 'd' || spacing == 'D')
582 return make_number (FONT_SPACING_DUAL);
584 return Qerror;
587 static Lisp_Object
588 font_prop_validate_otf (prop, val)
589 Lisp_Object prop, val;
591 Lisp_Object tail, tmp;
592 int i;
594 /* VAL = (SCRIPT [ LANGSYS [ GSUB-FEATURES [ GPOS-FEATURES ]]])
595 GSUB-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil
596 GPOS-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil */
597 if (! CONSP (val))
598 return Qerror;
599 if (! SYMBOLP (XCAR (val)))
600 return Qerror;
601 tail = XCDR (val);
602 if (NILP (tail))
603 return val;
604 if (! CONSP (tail) || ! SYMBOLP (XCAR (val)))
605 return Qerror;
606 for (i = 0; i < 2; i++)
608 tail = XCDR (tail);
609 if (NILP (tail))
610 return val;
611 if (! CONSP (tail))
612 return Qerror;
613 for (tmp = XCAR (tail); CONSP (tmp); tmp = XCDR (tmp))
614 if (! SYMBOLP (XCAR (tmp)))
615 return Qerror;
616 if (! NILP (tmp))
617 return Qerror;
619 return val;
622 /* Structure of known font property keys and validater of the
623 values. */
624 struct
626 /* Pointer to the key symbol. */
627 Lisp_Object *key;
628 /* Function to validate PROP's value VAL, or NULL if any value is
629 ok. The value is VAL or its regularized value if VAL is valid,
630 and Qerror if not. */
631 Lisp_Object (*validater) P_ ((Lisp_Object prop, Lisp_Object val));
632 } font_property_table[] =
633 { { &QCtype, font_prop_validate_symbol },
634 { &QCfoundry, font_prop_validate_symbol },
635 { &QCfamily, font_prop_validate_symbol },
636 { &QCadstyle, font_prop_validate_symbol },
637 { &QCregistry, font_prop_validate_symbol },
638 { &QCweight, font_prop_validate_style },
639 { &QCslant, font_prop_validate_style },
640 { &QCwidth, font_prop_validate_style },
641 { &QCsize, font_prop_validate_non_neg },
642 { &QCdpi, font_prop_validate_non_neg },
643 { &QCspacing, font_prop_validate_spacing },
644 { &QCavgwidth, font_prop_validate_non_neg },
645 /* The order of the above entries must match with enum
646 font_property_index. */
647 { &QClang, font_prop_validate_symbol },
648 { &QCscript, font_prop_validate_symbol },
649 { &QCotf, font_prop_validate_otf }
652 /* Size (number of elements) of the above table. */
653 #define FONT_PROPERTY_TABLE_SIZE \
654 ((sizeof font_property_table) / (sizeof *font_property_table))
656 /* Return an index number of font property KEY or -1 if KEY is not an
657 already known property. */
659 static int
660 get_font_prop_index (key)
661 Lisp_Object key;
663 int i;
665 for (i = 0; i < FONT_PROPERTY_TABLE_SIZE; i++)
666 if (EQ (key, *font_property_table[i].key))
667 return i;
668 return -1;
671 /* Validate the font property. The property key is specified by the
672 symbol PROP, or the index IDX (if PROP is nil). If VAL is invalid,
673 signal an error. The value is VAL or the regularized one. */
675 static Lisp_Object
676 font_prop_validate (idx, prop, val)
677 int idx;
678 Lisp_Object prop, val;
680 Lisp_Object validated;
682 if (NILP (val))
683 return val;
684 if (NILP (prop))
685 prop = *font_property_table[idx].key;
686 else
688 idx = get_font_prop_index (prop);
689 if (idx < 0)
690 return val;
692 validated = (font_property_table[idx].validater) (prop, val);
693 if (EQ (validated, Qerror))
694 signal_error ("invalid font property", Fcons (prop, val));
695 return validated;
699 /* Store VAL as a value of extra font property PROP in FONT while
700 keeping the sorting order. Don't check the validity of VAL. */
702 Lisp_Object
703 font_put_extra (font, prop, val)
704 Lisp_Object font, prop, val;
706 Lisp_Object extra = AREF (font, FONT_EXTRA_INDEX);
707 Lisp_Object slot = (NILP (extra) ? Qnil : assq_no_quit (prop, extra));
709 if (NILP (slot))
711 Lisp_Object prev = Qnil;
713 while (CONSP (extra)
714 && NILP (Fstring_lessp (prop, XCAR (XCAR (extra)))))
715 prev = extra, extra = XCDR (extra);
716 if (NILP (prev))
717 ASET (font, FONT_EXTRA_INDEX, Fcons (Fcons (prop, val), extra));
718 else
719 XSETCDR (prev, Fcons (Fcons (prop, val), extra));
720 return val;
722 XSETCDR (slot, val);
723 return val;
727 /* Font name parser and unparser */
729 static int parse_matrix P_ ((char *));
730 static int font_expand_wildcards P_ ((Lisp_Object *, int));
731 static int font_parse_name P_ ((char *, Lisp_Object));
733 /* An enumerator for each field of an XLFD font name. */
734 enum xlfd_field_index
736 XLFD_FOUNDRY_INDEX,
737 XLFD_FAMILY_INDEX,
738 XLFD_WEIGHT_INDEX,
739 XLFD_SLANT_INDEX,
740 XLFD_SWIDTH_INDEX,
741 XLFD_ADSTYLE_INDEX,
742 XLFD_PIXEL_INDEX,
743 XLFD_POINT_INDEX,
744 XLFD_RESX_INDEX,
745 XLFD_RESY_INDEX,
746 XLFD_SPACING_INDEX,
747 XLFD_AVGWIDTH_INDEX,
748 XLFD_REGISTRY_INDEX,
749 XLFD_ENCODING_INDEX,
750 XLFD_LAST_INDEX
753 /* An enumerator for mask bit corresponding to each XLFD field. */
754 enum xlfd_field_mask
756 XLFD_FOUNDRY_MASK = 0x0001,
757 XLFD_FAMILY_MASK = 0x0002,
758 XLFD_WEIGHT_MASK = 0x0004,
759 XLFD_SLANT_MASK = 0x0008,
760 XLFD_SWIDTH_MASK = 0x0010,
761 XLFD_ADSTYLE_MASK = 0x0020,
762 XLFD_PIXEL_MASK = 0x0040,
763 XLFD_POINT_MASK = 0x0080,
764 XLFD_RESX_MASK = 0x0100,
765 XLFD_RESY_MASK = 0x0200,
766 XLFD_SPACING_MASK = 0x0400,
767 XLFD_AVGWIDTH_MASK = 0x0800,
768 XLFD_REGISTRY_MASK = 0x1000,
769 XLFD_ENCODING_MASK = 0x2000
773 /* Parse P pointing the pixel/point size field of the form
774 `[A B C D]' which specifies a transformation matrix:
776 A B 0
777 C D 0
778 0 0 1
780 by which all glyphs of the font are transformed. The spec says
781 that scalar value N for the pixel/point size is equivalent to:
782 A = N * resx/resy, B = C = 0, D = N.
784 Return the scalar value N if the form is valid. Otherwise return
785 -1. */
787 static int
788 parse_matrix (p)
789 char *p;
791 double matrix[4];
792 char *end;
793 int i;
795 for (i = 0, p++; i < 4 && *p && *p != ']'; i++)
797 if (*p == '~')
798 matrix[i] = - strtod (p + 1, &end);
799 else
800 matrix[i] = strtod (p, &end);
801 p = end;
803 return (i == 4 ? (int) matrix[3] : -1);
806 /* Expand a wildcard field in FIELD (the first N fields are filled) to
807 multiple fields to fill in all 14 XLFD fields while restring a
808 field position by its contents. */
810 static int
811 font_expand_wildcards (field, n)
812 Lisp_Object field[XLFD_LAST_INDEX];
813 int n;
815 /* Copy of FIELD. */
816 Lisp_Object tmp[XLFD_LAST_INDEX];
817 /* Array of information about where this element can go. Nth
818 element is for Nth element of FIELD. */
819 struct {
820 /* Minimum possible field. */
821 int from;
822 /* Maxinum possible field. */
823 int to;
824 /* Bit mask of possible field. Nth bit corresponds to Nth field. */
825 int mask;
826 } range[XLFD_LAST_INDEX];
827 int i, j;
828 int range_from, range_to;
829 unsigned range_mask;
831 #define XLFD_SYMBOL_MASK (XLFD_FOUNDRY_MASK | XLFD_FAMILY_MASK \
832 | XLFD_ADSTYLE_MASK | XLFD_REGISTRY_MASK)
833 #define XLFD_NULL_MASK (XLFD_FOUNDRY_MASK | XLFD_ADSTYLE_MASK)
834 #define XLFD_LARGENUM_MASK (XLFD_POINT_MASK | XLFD_RESX_MASK | XLFD_RESY_MASK \
835 | XLFD_AVGWIDTH_MASK)
836 #define XLFD_REGENC_MASK (XLFD_REGISTRY_MASK | XLFD_ENCODING_MASK)
838 /* Initialize RANGE_MASK for FIELD[0] which can be 0th to (14 - N)th
839 field. The value is shifted to left one bit by one in the
840 following loop. */
841 for (i = 0, range_mask = 0; i <= 14 - n; i++)
842 range_mask = (range_mask << 1) | 1;
844 /* The triplet RANGE_FROM, RANGE_TO, and RANGE_MASK is a
845 position-based retriction for FIELD[I]. */
846 for (i = 0, range_from = 0, range_to = 14 - n; i < n;
847 i++, range_from++, range_to++, range_mask <<= 1)
849 Lisp_Object val = field[i];
851 tmp[i] = val;
852 if (NILP (val))
854 /* Wildcard. */
855 range[i].from = range_from;
856 range[i].to = range_to;
857 range[i].mask = range_mask;
859 else
861 /* The triplet FROM, TO, and MASK is a value-based
862 retriction for FIELD[I]. */
863 int from, to;
864 unsigned mask;
866 if (INTEGERP (val))
868 int numeric = XINT (val);
870 if (i + 1 == n)
871 from = to = XLFD_ENCODING_INDEX,
872 mask = XLFD_ENCODING_MASK;
873 else if (numeric == 0)
874 from = XLFD_PIXEL_INDEX, to = XLFD_AVGWIDTH_INDEX,
875 mask = XLFD_PIXEL_MASK | XLFD_LARGENUM_MASK;
876 else if (numeric <= 48)
877 from = to = XLFD_PIXEL_INDEX,
878 mask = XLFD_PIXEL_MASK;
879 else
880 from = XLFD_POINT_INDEX, to = XLFD_AVGWIDTH_INDEX,
881 mask = XLFD_LARGENUM_MASK;
883 else if (SBYTES (SYMBOL_NAME (val)) == 0)
884 from = XLFD_FOUNDRY_INDEX, to = XLFD_ADSTYLE_INDEX,
885 mask = XLFD_NULL_MASK;
886 else if (i == 0)
887 from = to = XLFD_FOUNDRY_INDEX, mask = XLFD_FOUNDRY_MASK;
888 else if (i + 1 == n)
890 Lisp_Object name = SYMBOL_NAME (val);
892 if (SDATA (name)[SBYTES (name) - 1] == '*')
893 from = XLFD_REGISTRY_INDEX, to = XLFD_ENCODING_INDEX,
894 mask = XLFD_REGENC_MASK;
895 else
896 from = to = XLFD_ENCODING_INDEX,
897 mask = XLFD_ENCODING_MASK;
899 else if (range_from <= XLFD_WEIGHT_INDEX
900 && range_to >= XLFD_WEIGHT_INDEX
901 && FONT_WEIGHT_NAME_NUMERIC (val) >= 0)
902 from = to = XLFD_WEIGHT_INDEX, mask = XLFD_WEIGHT_MASK;
903 else if (range_from <= XLFD_SLANT_INDEX
904 && range_to >= XLFD_SLANT_INDEX
905 && FONT_SLANT_NAME_NUMERIC (val) >= 0)
906 from = to = XLFD_SLANT_INDEX, mask = XLFD_SLANT_MASK;
907 else if (range_from <= XLFD_SWIDTH_INDEX
908 && range_to >= XLFD_SWIDTH_INDEX
909 && FONT_WIDTH_NAME_NUMERIC (val) >= 0)
910 from = to = XLFD_SWIDTH_INDEX, mask = XLFD_SWIDTH_MASK;
911 else
913 if (EQ (val, Qc) || EQ (val, Qm) || EQ (val, Qp) || EQ (val, Qd))
914 from = to = XLFD_SPACING_INDEX, mask = XLFD_SPACING_MASK;
915 else
916 from = XLFD_FOUNDRY_INDEX, to = XLFD_ENCODING_INDEX,
917 mask = XLFD_SYMBOL_MASK;
920 /* Merge position-based and value-based restrictions. */
921 mask &= range_mask;
922 while (from < range_from)
923 mask &= ~(1 << from++);
924 while (from < 14 && ! (mask & (1 << from)))
925 from++;
926 while (to > range_to)
927 mask &= ~(1 << to--);
928 while (to >= 0 && ! (mask & (1 << to)))
929 to--;
930 if (from > to)
931 return -1;
932 range[i].from = from;
933 range[i].to = to;
934 range[i].mask = mask;
936 if (from > range_from || to < range_to)
938 /* The range is narrowed by value-based restrictions.
939 Reflect it to the other fields. */
941 /* Following fields should be after FROM. */
942 range_from = from;
943 /* Preceding fields should be before TO. */
944 for (j = i - 1, from--, to--; j >= 0; j--, from--, to--)
946 /* Check FROM for non-wildcard field. */
947 if (! NILP (tmp[j]) && range[j].from < from)
949 while (range[j].from < from)
950 range[j].mask &= ~(1 << range[j].from++);
951 while (from < 14 && ! (range[j].mask & (1 << from)))
952 from++;
953 range[j].from = from;
955 else
956 from = range[j].from;
957 if (range[j].to > to)
959 while (range[j].to > to)
960 range[j].mask &= ~(1 << range[j].to--);
961 while (to >= 0 && ! (range[j].mask & (1 << to)))
962 to--;
963 range[j].to = to;
965 else
966 to = range[j].to;
967 if (from > to)
968 return -1;
974 /* Decide all fileds from restrictions in RANGE. */
975 for (i = j = 0; i < n ; i++)
977 if (j < range[i].from)
979 if (i == 0 || ! NILP (tmp[i - 1]))
980 /* None of TMP[X] corresponds to Jth field. */
981 return -1;
982 for (; j < range[i].from; j++)
983 field[j] = Qnil;
985 field[j++] = tmp[i];
987 if (! NILP (tmp[n - 1]) && j < XLFD_REGISTRY_INDEX)
988 return -1;
989 for (; j < XLFD_LAST_INDEX; j++)
990 field[j] = Qnil;
991 if (INTEGERP (field[XLFD_ENCODING_INDEX]))
992 field[XLFD_ENCODING_INDEX]
993 = Fintern (Fnumber_to_string (field[XLFD_ENCODING_INDEX]), Qnil);
994 return 0;
998 #ifdef ENABLE_CHECKING
999 /* Match a 14-field XLFD pattern against a full XLFD font name. */
1000 static int
1001 font_match_xlfd (char *pattern, char *name)
1003 while (*pattern && *name)
1005 if (*pattern == *name)
1006 pattern++;
1007 else if (*pattern == '*')
1008 if (*name == pattern[1])
1009 pattern += 2;
1010 else
1012 else
1013 return 0;
1014 name++;
1016 return 1;
1019 /* Make sure the font object matches the XLFD font name. */
1020 static int
1021 font_check_xlfd_parse (Lisp_Object font, char *name)
1023 char name_check[256];
1024 font_unparse_xlfd (font, 0, name_check, 255);
1025 return font_match_xlfd (name_check, name);
1028 #endif
1031 /* Parse NAME (null terminated) as XLFD and store information in FONT
1032 (font-spec or font-entity). Size property of FONT is set as
1033 follows:
1034 specified XLFD fields FONT property
1035 --------------------- -------------
1036 PIXEL_SIZE PIXEL_SIZE (Lisp integer)
1037 POINT_SIZE and RESY calculated pixel size (Lisp integer)
1038 POINT_SIZE POINT_SIZE/10 (Lisp float)
1040 If NAME is successfully parsed, return 0. Otherwise return -1.
1042 FONT is usually a font-spec, but when this function is called from
1043 X font backend driver, it is a font-entity. In that case, NAME is
1044 a fully specified XLFD. */
1047 font_parse_xlfd (name, font)
1048 char *name;
1049 Lisp_Object font;
1051 int len = strlen (name);
1052 int i, j, n;
1053 char *f[XLFD_LAST_INDEX + 1];
1054 Lisp_Object val;
1055 char *p;
1057 if (len > 255)
1058 /* Maximum XLFD name length is 255. */
1059 return -1;
1060 /* Accept "*-.." as a fully specified XLFD. */
1061 if (name[0] == '*' && name[1] == '-')
1062 i = 1, f[XLFD_FOUNDRY_INDEX] = name;
1063 else
1064 i = 0;
1065 for (p = name + i; *p; p++)
1066 if (*p == '-')
1068 f[i++] = p + 1;
1069 if (i == XLFD_LAST_INDEX)
1070 break;
1072 f[i] = name + len;
1074 #define INTERN_FIELD(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 0)
1075 #define INTERN_FIELD_SYM(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 1)
1077 if (i == XLFD_LAST_INDEX)
1079 /* Fully specified XLFD. */
1080 int pixel_size;
1082 ASET (font, FONT_FOUNDRY_INDEX, INTERN_FIELD_SYM (XLFD_FOUNDRY_INDEX));
1083 ASET (font, FONT_FAMILY_INDEX, INTERN_FIELD_SYM (XLFD_FAMILY_INDEX));
1084 for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
1085 i <= XLFD_SWIDTH_INDEX; i++, j++)
1087 val = INTERN_FIELD_SYM (i);
1088 if (! NILP (val))
1090 if ((n = font_style_to_value (j, INTERN_FIELD_SYM (i), 0)) < 0)
1091 return -1;
1092 ASET (font, j, make_number (n));
1095 ASET (font, FONT_ADSTYLE_INDEX, INTERN_FIELD_SYM (XLFD_ADSTYLE_INDEX));
1096 if (strcmp (f[XLFD_REGISTRY_INDEX], "*-*") == 0)
1097 ASET (font, FONT_REGISTRY_INDEX, Qnil);
1098 else
1099 ASET (font, FONT_REGISTRY_INDEX,
1100 font_intern_prop (f[XLFD_REGISTRY_INDEX],
1101 f[XLFD_LAST_INDEX] - f[XLFD_REGISTRY_INDEX],
1102 1));
1103 p = f[XLFD_PIXEL_INDEX];
1104 if (*p == '[' && (pixel_size = parse_matrix (p)) >= 0)
1105 ASET (font, FONT_SIZE_INDEX, make_number (pixel_size));
1106 else
1108 val = INTERN_FIELD (XLFD_PIXEL_INDEX);
1109 if (INTEGERP (val))
1110 ASET (font, FONT_SIZE_INDEX, val);
1111 else
1113 double point_size = -1;
1115 font_assert (FONT_SPEC_P (font));
1116 p = f[XLFD_POINT_INDEX];
1117 if (*p == '[')
1118 point_size = parse_matrix (p);
1119 else if (isdigit (*p))
1120 point_size = atoi (p), point_size /= 10;
1121 if (point_size >= 0)
1122 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1126 ASET (font, FONT_DPI_INDEX, INTERN_FIELD (XLFD_RESY_INDEX));
1127 val = INTERN_FIELD (XLFD_SPACING_INDEX);
1128 if (! NILP (val))
1130 val = font_prop_validate_spacing (QCspacing, val);
1131 if (! INTEGERP (val))
1132 return -1;
1133 ASET (font, FONT_SPACING_INDEX, val);
1135 p = f[XLFD_AVGWIDTH_INDEX];
1136 if (*p == '~')
1137 p++;
1138 ASET (font, FONT_AVGWIDTH_INDEX,
1139 font_intern_prop (p, f[XLFD_REGISTRY_INDEX] - 1 - p, 0));
1141 else
1143 int wild_card_found = 0;
1144 Lisp_Object prop[XLFD_LAST_INDEX];
1146 if (FONT_ENTITY_P (font))
1147 return -1;
1148 for (j = 0; j < i; j++)
1150 if (*f[j] == '*')
1152 if (f[j][1] && f[j][1] != '-')
1153 return -1;
1154 prop[j] = Qnil;
1155 wild_card_found = 1;
1157 else if (j + 1 < i)
1158 prop[j] = INTERN_FIELD (j);
1159 else
1160 prop[j] = font_intern_prop (f[j], f[i] - f[j], 0);
1162 if (! wild_card_found)
1163 return -1;
1164 if (font_expand_wildcards (prop, i) < 0)
1165 return -1;
1167 ASET (font, FONT_FOUNDRY_INDEX, prop[XLFD_FOUNDRY_INDEX]);
1168 ASET (font, FONT_FAMILY_INDEX, prop[XLFD_FAMILY_INDEX]);
1169 for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
1170 i <= XLFD_SWIDTH_INDEX; i++, j++)
1171 if (! NILP (prop[i]))
1173 if ((n = font_style_to_value (j, prop[i], 1)) < 0)
1174 return -1;
1175 ASET (font, j, make_number (n));
1177 ASET (font, FONT_ADSTYLE_INDEX, prop[XLFD_ADSTYLE_INDEX]);
1178 val = prop[XLFD_REGISTRY_INDEX];
1179 if (NILP (val))
1181 val = prop[XLFD_ENCODING_INDEX];
1182 if (! NILP (val))
1183 val = concat2 (build_string ("*-"), SYMBOL_NAME (val));
1185 else if (NILP (prop[XLFD_ENCODING_INDEX]))
1186 val = concat2 (SYMBOL_NAME (val), build_string ("-*"));
1187 else
1188 val = concat3 (SYMBOL_NAME (val), build_string ("-"),
1189 SYMBOL_NAME (prop[XLFD_ENCODING_INDEX]));
1190 if (! NILP (val))
1191 ASET (font, FONT_REGISTRY_INDEX, Fintern (val, Qnil));
1193 if (INTEGERP (prop[XLFD_PIXEL_INDEX]))
1194 ASET (font, FONT_SIZE_INDEX, prop[XLFD_PIXEL_INDEX]);
1195 else if (INTEGERP (prop[XLFD_POINT_INDEX]))
1197 double point_size = XINT (prop[XLFD_POINT_INDEX]);
1199 ASET (font, FONT_SIZE_INDEX, make_float (point_size / 10));
1202 if (INTEGERP (prop[XLFD_RESX_INDEX]))
1203 ASET (font, FONT_DPI_INDEX, prop[XLFD_RESY_INDEX]);
1204 if (! NILP (prop[XLFD_SPACING_INDEX]))
1206 val = font_prop_validate_spacing (QCspacing,
1207 prop[XLFD_SPACING_INDEX]);
1208 if (! INTEGERP (val))
1209 return -1;
1210 ASET (font, FONT_SPACING_INDEX, val);
1212 if (INTEGERP (prop[XLFD_AVGWIDTH_INDEX]))
1213 ASET (font, FONT_AVGWIDTH_INDEX, prop[XLFD_AVGWIDTH_INDEX]);
1216 return 0;
1219 /* Store XLFD name of FONT (font-spec or font-entity) in NAME (NBYTES
1220 length), and return the name length. If FONT_SIZE_INDEX of FONT is
1221 0, use PIXEL_SIZE instead. */
1224 font_unparse_xlfd (font, pixel_size, name, nbytes)
1225 Lisp_Object font;
1226 int pixel_size;
1227 char *name;
1228 int nbytes;
1230 char *f[XLFD_REGISTRY_INDEX + 1];
1231 Lisp_Object val;
1232 int i, j, len = 0;
1234 font_assert (FONTP (font));
1236 for (i = FONT_FOUNDRY_INDEX, j = XLFD_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX;
1237 i++, j++)
1239 if (i == FONT_ADSTYLE_INDEX)
1240 j = XLFD_ADSTYLE_INDEX;
1241 else if (i == FONT_REGISTRY_INDEX)
1242 j = XLFD_REGISTRY_INDEX;
1243 val = AREF (font, i);
1244 if (NILP (val))
1246 if (j == XLFD_REGISTRY_INDEX)
1247 f[j] = "*-*", len += 4;
1248 else
1249 f[j] = "*", len += 2;
1251 else
1253 if (SYMBOLP (val))
1254 val = SYMBOL_NAME (val);
1255 if (j == XLFD_REGISTRY_INDEX
1256 && ! strchr ((char *) SDATA (val), '-'))
1258 /* Change "jisx0208*" and "jisx0208" to "jisx0208*-*". */
1259 if (SDATA (val)[SBYTES (val) - 1] == '*')
1261 f[j] = alloca (SBYTES (val) + 3);
1262 sprintf (f[j], "%s-*", SDATA (val));
1263 len += SBYTES (val) + 3;
1265 else
1267 f[j] = alloca (SBYTES (val) + 4);
1268 sprintf (f[j], "%s*-*", SDATA (val));
1269 len += SBYTES (val) + 4;
1272 else
1273 f[j] = (char *) SDATA (val), len += SBYTES (val) + 1;
1277 for (i = FONT_WEIGHT_INDEX, j = XLFD_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX;
1278 i++, j++)
1280 val = font_style_symbolic (font, i, 0);
1281 if (NILP (val))
1282 f[j] = "*", len += 2;
1283 else
1285 val = SYMBOL_NAME (val);
1286 f[j] = (char *) SDATA (val), len += SBYTES (val) + 1;
1290 val = AREF (font, FONT_SIZE_INDEX);
1291 font_assert (NUMBERP (val) || NILP (val));
1292 if (INTEGERP (val))
1294 i = XINT (val);
1295 if (i <= 0)
1296 i = pixel_size;
1297 if (i > 0)
1299 f[XLFD_PIXEL_INDEX] = alloca (22);
1300 len += sprintf (f[XLFD_PIXEL_INDEX], "%d-*", i) + 1;
1302 else
1303 f[XLFD_PIXEL_INDEX] = "*-*", len += 4;
1305 else if (FLOATP (val))
1307 i = XFLOAT_DATA (val) * 10;
1308 f[XLFD_PIXEL_INDEX] = alloca (12);
1309 len += sprintf (f[XLFD_PIXEL_INDEX], "*-%d", i) + 1;
1311 else
1312 f[XLFD_PIXEL_INDEX] = "*-*", len += 4;
1314 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1316 i = XINT (AREF (font, FONT_DPI_INDEX));
1317 f[XLFD_RESX_INDEX] = alloca (22);
1318 len += sprintf (f[XLFD_RESX_INDEX],
1319 "%d-%d", i, i) + 1;
1321 else
1322 f[XLFD_RESX_INDEX] = "*-*", len += 4;
1323 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1325 int spacing = XINT (AREF (font, FONT_SPACING_INDEX));
1327 f[XLFD_SPACING_INDEX] = (spacing <= FONT_SPACING_PROPORTIONAL ? "p"
1328 : spacing <= FONT_SPACING_DUAL ? "d"
1329 : spacing <= FONT_SPACING_MONO ? "m"
1330 : "c");
1331 len += 2;
1333 else
1334 f[XLFD_SPACING_INDEX] = "*", len += 2;
1335 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1337 f[XLFD_AVGWIDTH_INDEX] = alloca (11);
1338 len += sprintf (f[XLFD_AVGWIDTH_INDEX],
1339 "%d", XINT (AREF (font, FONT_AVGWIDTH_INDEX))) + 1;
1341 else
1342 f[XLFD_AVGWIDTH_INDEX] = "*", len += 2;
1343 len++; /* for terminating '\0'. */
1344 if (len >= nbytes)
1345 return -1;
1346 return sprintf (name, "-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s",
1347 f[XLFD_FOUNDRY_INDEX], f[XLFD_FAMILY_INDEX],
1348 f[XLFD_WEIGHT_INDEX], f[XLFD_SLANT_INDEX],
1349 f[XLFD_SWIDTH_INDEX], f[XLFD_ADSTYLE_INDEX],
1350 f[XLFD_PIXEL_INDEX], f[XLFD_RESX_INDEX],
1351 f[XLFD_SPACING_INDEX], f[XLFD_AVGWIDTH_INDEX],
1352 f[XLFD_REGISTRY_INDEX]);
1355 /* Parse NAME (null terminated) and store information in FONT
1356 (font-spec or font-entity). NAME is supplied in either the
1357 Fontconfig or GTK font name format. If NAME is successfully
1358 parsed, return 0. Otherwise return -1.
1360 The fontconfig format is
1362 FAMILY[-SIZE][:PROP1[=VAL1][:PROP2[=VAL2]...]]
1364 The GTK format is
1366 FAMILY [PROPS...] [SIZE]
1368 This function tries to guess which format it is. */
1371 font_parse_fcname (name, font)
1372 char *name;
1373 Lisp_Object font;
1375 char *p, *q;
1376 char *size_beg = NULL, *size_end = NULL;
1377 char *props_beg = NULL, *family_end = NULL;
1378 int len = strlen (name);
1380 if (len == 0)
1381 return -1;
1383 for (p = name; *p; p++)
1385 if (*p == '\\' && p[1])
1386 p++;
1387 else if (*p == ':')
1389 props_beg = family_end = p;
1390 break;
1392 else if (*p == '-')
1394 int decimal = 0, size_found = 1;
1395 for (q = p + 1; *q && *q != ':'; q++)
1396 if (! isdigit(*q))
1398 if (*q != '.' || decimal)
1400 size_found = 0;
1401 break;
1403 decimal = 1;
1405 if (size_found)
1407 family_end = p;
1408 size_beg = p + 1;
1409 size_end = q;
1410 break;
1415 if (family_end)
1417 /* A fontconfig name with size and/or property data. */
1418 if (family_end > name)
1420 Lisp_Object family;
1421 family = font_intern_prop (name, family_end - name, 1);
1422 ASET (font, FONT_FAMILY_INDEX, family);
1424 if (size_beg)
1426 double point_size = strtod (size_beg, &size_end);
1427 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1428 if (*size_end == ':' && size_end[1])
1429 props_beg = size_end;
1431 if (props_beg)
1433 /* Now parse ":KEY=VAL" patterns. */
1434 Lisp_Object val;
1436 for (p = props_beg; *p; p = q)
1438 for (q = p + 1; *q && *q != '=' && *q != ':'; q++);
1439 if (*q != '=')
1441 /* Must be an enumerated value. */
1442 int word_len;
1443 p = p + 1;
1444 word_len = q - p;
1445 val = font_intern_prop (p, q - p, 1);
1447 #define PROP_MATCH(STR,N) ((word_len == N) && memcmp (p, STR, N) == 0)
1449 if (PROP_MATCH ("light", 5)
1450 || PROP_MATCH ("medium", 6)
1451 || PROP_MATCH ("demibold", 8)
1452 || PROP_MATCH ("bold", 4)
1453 || PROP_MATCH ("black", 5))
1454 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, val);
1455 else if (PROP_MATCH ("roman", 5)
1456 || PROP_MATCH ("italic", 6)
1457 || PROP_MATCH ("oblique", 7))
1458 FONT_SET_STYLE (font, FONT_SLANT_INDEX, val);
1459 else if (PROP_MATCH ("charcell", 8))
1460 ASET (font, FONT_SPACING_INDEX,
1461 make_number (FONT_SPACING_CHARCELL));
1462 else if (PROP_MATCH ("mono", 4))
1463 ASET (font, FONT_SPACING_INDEX,
1464 make_number (FONT_SPACING_MONO));
1465 else if (PROP_MATCH ("proportional", 12))
1466 ASET (font, FONT_SPACING_INDEX,
1467 make_number (FONT_SPACING_PROPORTIONAL));
1468 #undef PROP_MATCH
1470 else
1472 /* KEY=VAL pairs */
1473 Lisp_Object key;
1474 int prop;
1476 if (q - p == 10 && memcmp (p + 1, "pixelsize", 9) == 0)
1477 prop = FONT_SIZE_INDEX;
1478 else
1480 key = font_intern_prop (p, q - p, 1);
1481 prop = get_font_prop_index (key);
1484 p = q + 1;
1485 for (q = p; *q && *q != ':'; q++);
1486 val = font_intern_prop (p, q - p, 0);
1488 if (prop >= FONT_FOUNDRY_INDEX
1489 && prop < FONT_EXTRA_INDEX)
1490 ASET (font, prop, font_prop_validate (prop, Qnil, val));
1491 else
1492 Ffont_put (font, key, val);
1494 p = q;
1498 else
1500 /* Either a fontconfig-style name with no size and property
1501 data, or a GTK-style name. */
1502 Lisp_Object prop;
1503 int word_len, prop_found = 0;
1505 for (p = name; *p; p = *q ? q + 1 : q)
1507 if (isdigit (*p))
1509 int size_found = 1;
1511 for (q = p + 1; *q && *q != ' '; q++)
1512 if (! isdigit (*q))
1514 size_found = 0;
1515 break;
1517 if (size_found)
1519 double point_size = strtod (p, &q);
1520 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1521 continue;
1525 for (q = p + 1; *q && *q != ' '; q++)
1526 if (*q == '\\' && q[1])
1527 q++;
1528 word_len = q - p;
1530 #define PROP_MATCH(STR,N) ((word_len == N) && memcmp (p, STR, N) == 0)
1532 if (PROP_MATCH ("Ultra-Light", 11))
1534 prop_found = 1;
1535 prop = font_intern_prop ("ultra-light", 11, 1);
1536 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
1538 else if (PROP_MATCH ("Light", 5))
1540 prop_found = 1;
1541 prop = font_intern_prop ("light", 5, 1);
1542 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
1544 else if (PROP_MATCH ("Semi-Bold", 9))
1546 prop_found = 1;
1547 prop = font_intern_prop ("semi-bold", 9, 1);
1548 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
1550 else if (PROP_MATCH ("Bold", 4))
1552 prop_found = 1;
1553 prop = font_intern_prop ("bold", 4, 1);
1554 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
1556 else if (PROP_MATCH ("Italic", 6))
1558 prop_found = 1;
1559 prop = font_intern_prop ("italic", 4, 1);
1560 FONT_SET_STYLE (font, FONT_SLANT_INDEX, prop);
1562 else if (PROP_MATCH ("Oblique", 7))
1564 prop_found = 1;
1565 prop = font_intern_prop ("oblique", 7, 1);
1566 FONT_SET_STYLE (font, FONT_SLANT_INDEX, prop);
1568 else {
1569 if (prop_found)
1570 return -1; /* Unknown property in GTK-style font name. */
1571 family_end = q;
1574 #undef PROP_MATCH
1576 if (family_end)
1578 Lisp_Object family;
1579 family = font_intern_prop (name, family_end - name, 1);
1580 ASET (font, FONT_FAMILY_INDEX, family);
1584 return 0;
1587 /* Store fontconfig's font name of FONT (font-spec or font-entity) in
1588 NAME (NBYTES length), and return the name length. If
1589 FONT_SIZE_INDEX of FONT is 0, use PIXEL_SIZE instead. */
1592 font_unparse_fcname (font, pixel_size, name, nbytes)
1593 Lisp_Object font;
1594 int pixel_size;
1595 char *name;
1596 int nbytes;
1598 Lisp_Object family, foundry;
1599 Lisp_Object tail, val;
1600 int point_size;
1601 int dpi;
1602 int i, len = 1;
1603 char *p;
1604 Lisp_Object styles[3];
1605 char *style_names[3] = { "weight", "slant", "width" };
1606 char work[256];
1608 family = AREF (font, FONT_FAMILY_INDEX);
1609 if (! NILP (family))
1611 if (SYMBOLP (family))
1613 family = SYMBOL_NAME (family);
1614 len += SBYTES (family);
1616 else
1617 family = Qnil;
1620 val = AREF (font, FONT_SIZE_INDEX);
1621 if (INTEGERP (val))
1623 if (XINT (val) != 0)
1624 pixel_size = XINT (val);
1625 point_size = -1;
1626 len += 21; /* for ":pixelsize=NUM" */
1628 else if (FLOATP (val))
1630 pixel_size = -1;
1631 point_size = (int) XFLOAT_DATA (val);
1632 len += 11; /* for "-NUM" */
1635 foundry = AREF (font, FONT_FOUNDRY_INDEX);
1636 if (! NILP (foundry))
1638 if (SYMBOLP (foundry))
1640 foundry = SYMBOL_NAME (foundry);
1641 len += 9 + SBYTES (foundry); /* ":foundry=NAME" */
1643 else
1644 foundry = Qnil;
1647 for (i = 0; i < 3; i++)
1649 styles[i] = font_style_symbolic (font, FONT_WEIGHT_INDEX + i, 0);
1650 if (! NILP (styles[i]))
1651 len += sprintf (work, ":%s=%s", style_names[i],
1652 SDATA (SYMBOL_NAME (styles[i])));
1655 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1656 len += sprintf (work, ":dpi=%d", dpi);
1657 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1658 len += strlen (":spacing=100");
1659 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1660 len += strlen (":scalable=false"); /* or ":scalable=true" */
1661 for (tail = AREF (font, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail))
1663 Lisp_Object key = XCAR (XCAR (tail)), val = XCDR (XCAR (tail));
1665 len += SBYTES (SYMBOL_NAME (key)) + 1; /* for :KEY= */
1666 if (STRINGP (val))
1667 len += SBYTES (val);
1668 else if (INTEGERP (val))
1669 len += sprintf (work, "%d", XINT (val));
1670 else if (SYMBOLP (val))
1671 len += (NILP (val) ? 5 : 4); /* for "false" or "true" */
1674 if (len > nbytes)
1675 return -1;
1676 p = name;
1677 if (! NILP (family))
1678 p += sprintf (p, "%s", SDATA (family));
1679 if (point_size > 0)
1681 if (p == name)
1682 p += sprintf (p, "%d", point_size);
1683 else
1684 p += sprintf (p, "-%d", point_size);
1686 else if (pixel_size > 0)
1687 p += sprintf (p, ":pixelsize=%d", pixel_size);
1688 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
1689 p += sprintf (p, ":foundry=%s",
1690 SDATA (SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX))));
1691 for (i = 0; i < 3; i++)
1692 if (! NILP (styles[i]))
1693 p += sprintf (p, ":%s=%s", style_names[i],
1694 SDATA (SYMBOL_NAME (styles[i])));
1695 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1696 p += sprintf (p, ":dpi=%d", XINT (AREF (font, FONT_DPI_INDEX)));
1697 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1698 p += sprintf (p, ":spacing=%d", XINT (AREF (font, FONT_SPACING_INDEX)));
1699 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1701 if (XINT (AREF (font, FONT_AVGWIDTH_INDEX)) == 0)
1702 p += sprintf (p, ":scalable=true");
1703 else
1704 p += sprintf (p, ":scalable=false");
1706 return (p - name);
1709 /* Store GTK-style font name of FONT (font-spec or font-entity) in
1710 NAME (NBYTES length), and return the name length. F is the frame
1711 on which the font is displayed; it is used to calculate the point
1712 size. */
1715 font_unparse_gtkname (font, f, name, nbytes)
1716 Lisp_Object font;
1717 struct frame *f;
1718 char *name;
1719 int nbytes;
1721 char *p;
1722 int len = 1;
1723 Lisp_Object family, weight, slant, size;
1724 int point_size = -1;
1726 family = AREF (font, FONT_FAMILY_INDEX);
1727 if (! NILP (family))
1729 if (! SYMBOLP (family))
1730 return -1;
1731 family = SYMBOL_NAME (family);
1732 len += SBYTES (family);
1735 weight = font_style_symbolic (font, FONT_WEIGHT_INDEX, 0);
1736 if (EQ (weight, Qnormal))
1737 weight = Qnil;
1738 else if (! NILP (weight))
1740 weight = SYMBOL_NAME (weight);
1741 len += SBYTES (weight);
1744 slant = font_style_symbolic (font, FONT_SLANT_INDEX, 0);
1745 if (EQ (slant, Qnormal))
1746 slant = Qnil;
1747 else if (! NILP (slant))
1749 slant = SYMBOL_NAME (slant);
1750 len += SBYTES (slant);
1753 size = AREF (font, FONT_SIZE_INDEX);
1754 /* Convert pixel size to point size. */
1755 if (INTEGERP (size))
1757 Lisp_Object font_dpi = AREF (font, FONT_DPI_INDEX);
1758 int dpi = 75;
1759 if (INTEGERP (font_dpi))
1760 dpi = XINT (font_dpi);
1761 else if (f)
1762 dpi = f->resy;
1763 point_size = PIXEL_TO_POINT (XINT (size), dpi);
1764 len += 11;
1766 else if (FLOATP (size))
1768 point_size = (int) XFLOAT_DATA (size);
1769 len += 11;
1772 if (len > nbytes)
1773 return -1;
1775 p = name + sprintf (name, "%s", SDATA (family));
1777 if (! NILP (weight))
1779 char *q = p;
1780 p += sprintf (p, " %s", SDATA (weight));
1781 q[1] = toupper (q[1]);
1784 if (! NILP (slant))
1786 char *q = p;
1787 p += sprintf (p, " %s", SDATA (slant));
1788 q[1] = toupper (q[1]);
1791 if (point_size > 0)
1792 p += sprintf (p, " %d", point_size);
1794 return (p - name);
1797 /* Parse NAME (null terminated) and store information in FONT
1798 (font-spec or font-entity). If NAME is successfully parsed, return
1799 0. Otherwise return -1. */
1801 static int
1802 font_parse_name (name, font)
1803 char *name;
1804 Lisp_Object font;
1806 if (name[0] == '-' || index (name, '*'))
1807 return font_parse_xlfd (name, font);
1808 return font_parse_fcname (name, font);
1812 /* Merge FAMILY and REGISTRY into FONT_SPEC. FAMILY may have the form
1813 "FAMILY-FOUNDRY". REGISTRY may not contain charset-encoding
1814 part. */
1816 void
1817 font_parse_family_registry (family, registry, font_spec)
1818 Lisp_Object family, registry, font_spec;
1820 int len;
1821 char *p0, *p1;
1823 if (! NILP (family)
1824 && NILP (AREF (font_spec, FONT_FAMILY_INDEX)))
1826 CHECK_STRING (family);
1827 len = SBYTES (family);
1828 p0 = (char *) SDATA (family);
1829 p1 = index (p0, '-');
1830 if (p1)
1832 if ((*p0 != '*' || p1 - p0 > 1)
1833 && NILP (AREF (font_spec, FONT_FOUNDRY_INDEX)))
1834 Ffont_put (font_spec, QCfoundry, font_intern_prop (p0, p1 - p0, 1));
1835 p1++;
1836 len -= p1 - p0;
1837 Ffont_put (font_spec, QCfamily, font_intern_prop (p1, len, 1));
1839 else
1840 ASET (font_spec, FONT_FAMILY_INDEX, Fintern (family, Qnil));
1842 if (! NILP (registry))
1844 /* Convert "XXX" and "XXX*" to "XXX*-*". */
1845 CHECK_STRING (registry);
1846 len = SBYTES (registry);
1847 p0 = (char *) SDATA (registry);
1848 p1 = index (p0, '-');
1849 if (! p1)
1851 if (SDATA (registry)[len - 1] == '*')
1852 registry = concat2 (registry, build_string ("-*"));
1853 else
1854 registry = concat2 (registry, build_string ("*-*"));
1856 registry = Fdowncase (registry);
1857 ASET (font_spec, FONT_REGISTRY_INDEX, Fintern (registry, Qnil));
1862 /* This part (through the next ^L) is still experimental and not
1863 tested much. We may drastically change codes. */
1865 /* OTF handler */
1867 #if 0
1869 #define LGSTRING_HEADER_SIZE 6
1870 #define LGSTRING_GLYPH_SIZE 8
1872 static int
1873 check_gstring (gstring)
1874 Lisp_Object gstring;
1876 Lisp_Object val;
1877 int i, j;
1879 CHECK_VECTOR (gstring);
1880 val = AREF (gstring, 0);
1881 CHECK_VECTOR (val);
1882 if (ASIZE (val) < LGSTRING_HEADER_SIZE)
1883 goto err;
1884 CHECK_FONT_OBJECT (LGSTRING_FONT (gstring));
1885 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_LBEARING)))
1886 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_LBEARING));
1887 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_RBEARING)))
1888 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_RBEARING));
1889 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_WIDTH)))
1890 CHECK_NATNUM (LGSTRING_SLOT (gstring, LGSTRING_IX_WIDTH));
1891 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT)))
1892 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT));
1893 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT)))
1894 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT));
1896 for (i = 0; i < LGSTRING_GLYPH_LEN (gstring); i++)
1898 val = LGSTRING_GLYPH (gstring, i);
1899 CHECK_VECTOR (val);
1900 if (ASIZE (val) < LGSTRING_GLYPH_SIZE)
1901 goto err;
1902 if (NILP (AREF (val, LGLYPH_IX_CHAR)))
1903 break;
1904 CHECK_NATNUM (AREF (val, LGLYPH_IX_FROM));
1905 CHECK_NATNUM (AREF (val, LGLYPH_IX_TO));
1906 CHECK_CHARACTER (AREF (val, LGLYPH_IX_CHAR));
1907 if (!NILP (AREF (val, LGLYPH_IX_CODE)))
1908 CHECK_NATNUM (AREF (val, LGLYPH_IX_CODE));
1909 if (!NILP (AREF (val, LGLYPH_IX_WIDTH)))
1910 CHECK_NATNUM (AREF (val, LGLYPH_IX_WIDTH));
1911 if (!NILP (AREF (val, LGLYPH_IX_ADJUSTMENT)))
1913 val = AREF (val, LGLYPH_IX_ADJUSTMENT);
1914 CHECK_VECTOR (val);
1915 if (ASIZE (val) < 3)
1916 goto err;
1917 for (j = 0; j < 3; j++)
1918 CHECK_NUMBER (AREF (val, j));
1921 return i;
1922 err:
1923 error ("Invalid glyph-string format");
1924 return -1;
1927 static void
1928 check_otf_features (otf_features)
1929 Lisp_Object otf_features;
1931 Lisp_Object val;
1933 CHECK_CONS (otf_features);
1934 CHECK_SYMBOL (XCAR (otf_features));
1935 otf_features = XCDR (otf_features);
1936 CHECK_CONS (otf_features);
1937 CHECK_SYMBOL (XCAR (otf_features));
1938 otf_features = XCDR (otf_features);
1939 for (val = Fcar (otf_features); ! NILP (val); val = Fcdr (val))
1941 CHECK_SYMBOL (Fcar (val));
1942 if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
1943 error ("Invalid OTF GSUB feature: %s", SYMBOL_NAME (XCAR (val)));
1945 otf_features = XCDR (otf_features);
1946 for (val = Fcar (otf_features); ! NILP (val); val = Fcdr (val))
1948 CHECK_SYMBOL (Fcar (val));
1949 if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
1950 error ("Invalid OTF GPOS feature: %s", SYMBOL_NAME (XCAR (val)));
1954 #ifdef HAVE_LIBOTF
1955 #include <otf.h>
1957 Lisp_Object otf_list;
1959 static Lisp_Object
1960 otf_tag_symbol (tag)
1961 OTF_Tag tag;
1963 char name[5];
1965 OTF_tag_name (tag, name);
1966 return Fintern (make_unibyte_string (name, 4), Qnil);
1969 static OTF *
1970 otf_open (file)
1971 Lisp_Object file;
1973 Lisp_Object val = Fassoc (file, otf_list);
1974 OTF *otf;
1976 if (! NILP (val))
1977 otf = XSAVE_VALUE (XCDR (val))->pointer;
1978 else
1980 otf = STRINGP (file) ? OTF_open ((char *) SDATA (file)) : NULL;
1981 val = make_save_value (otf, 0);
1982 otf_list = Fcons (Fcons (file, val), otf_list);
1984 return otf;
1988 /* Return a list describing which scripts/languages FONT supports by
1989 which GSUB/GPOS features of OpenType tables. See the comment of
1990 (struct font_driver).otf_capability. */
1992 Lisp_Object
1993 font_otf_capability (font)
1994 struct font *font;
1996 OTF *otf;
1997 Lisp_Object capability = Fcons (Qnil, Qnil);
1998 int i;
2000 otf = otf_open (font->props[FONT_FILE_INDEX]);
2001 if (! otf)
2002 return Qnil;
2003 for (i = 0; i < 2; i++)
2005 OTF_GSUB_GPOS *gsub_gpos;
2006 Lisp_Object script_list = Qnil;
2007 int j;
2009 if (OTF_get_features (otf, i == 0) < 0)
2010 continue;
2011 gsub_gpos = i == 0 ? otf->gsub : otf->gpos;
2012 for (j = gsub_gpos->ScriptList.ScriptCount - 1; j >= 0; j--)
2014 OTF_Script *script = gsub_gpos->ScriptList.Script + j;
2015 Lisp_Object langsys_list = Qnil;
2016 Lisp_Object script_tag = otf_tag_symbol (script->ScriptTag);
2017 int k;
2019 for (k = script->LangSysCount; k >= 0; k--)
2021 OTF_LangSys *langsys;
2022 Lisp_Object feature_list = Qnil;
2023 Lisp_Object langsys_tag;
2024 int l;
2026 if (k == script->LangSysCount)
2028 langsys = &script->DefaultLangSys;
2029 langsys_tag = Qnil;
2031 else
2033 langsys = script->LangSys + k;
2034 langsys_tag
2035 = otf_tag_symbol (script->LangSysRecord[k].LangSysTag);
2037 for (l = langsys->FeatureCount - 1; l >= 0; l--)
2039 OTF_Feature *feature
2040 = gsub_gpos->FeatureList.Feature + langsys->FeatureIndex[l];
2041 Lisp_Object feature_tag
2042 = otf_tag_symbol (feature->FeatureTag);
2044 feature_list = Fcons (feature_tag, feature_list);
2046 langsys_list = Fcons (Fcons (langsys_tag, feature_list),
2047 langsys_list);
2049 script_list = Fcons (Fcons (script_tag, langsys_list),
2050 script_list);
2053 if (i == 0)
2054 XSETCAR (capability, script_list);
2055 else
2056 XSETCDR (capability, script_list);
2059 return capability;
2062 /* Parse OTF features in SPEC and write a proper features spec string
2063 in FEATURES for the call of OTF_drive_gsub/gpos (of libotf). It is
2064 assured that the sufficient memory has already allocated for
2065 FEATURES. */
2067 static void
2068 generate_otf_features (spec, features)
2069 Lisp_Object spec;
2070 char *features;
2072 Lisp_Object val;
2073 char *p;
2074 int asterisk;
2076 p = features;
2077 *p = '\0';
2078 for (asterisk = 0; CONSP (spec); spec = XCDR (spec))
2080 val = XCAR (spec);
2081 CHECK_SYMBOL (val);
2082 if (p > features)
2083 *p++ = ',';
2084 if (SREF (SYMBOL_NAME (val), 0) == '*')
2086 asterisk = 1;
2087 *p++ = '*';
2089 else if (! asterisk)
2091 val = SYMBOL_NAME (val);
2092 p += sprintf (p, "%s", SDATA (val));
2094 else
2096 val = SYMBOL_NAME (val);
2097 p += sprintf (p, "~%s", SDATA (val));
2100 if (CONSP (spec))
2101 error ("OTF spec too long");
2104 Lisp_Object
2105 font_otf_DeviceTable (device_table)
2106 OTF_DeviceTable *device_table;
2108 int len = device_table->StartSize - device_table->EndSize + 1;
2110 return Fcons (make_number (len),
2111 make_unibyte_string (device_table->DeltaValue, len));
2114 Lisp_Object
2115 font_otf_ValueRecord (value_format, value_record)
2116 int value_format;
2117 OTF_ValueRecord *value_record;
2119 Lisp_Object val = Fmake_vector (make_number (8), Qnil);
2121 if (value_format & OTF_XPlacement)
2122 ASET (val, 0, make_number (value_record->XPlacement));
2123 if (value_format & OTF_YPlacement)
2124 ASET (val, 1, make_number (value_record->YPlacement));
2125 if (value_format & OTF_XAdvance)
2126 ASET (val, 2, make_number (value_record->XAdvance));
2127 if (value_format & OTF_YAdvance)
2128 ASET (val, 3, make_number (value_record->YAdvance));
2129 if (value_format & OTF_XPlaDevice)
2130 ASET (val, 4, font_otf_DeviceTable (&value_record->XPlaDevice));
2131 if (value_format & OTF_YPlaDevice)
2132 ASET (val, 4, font_otf_DeviceTable (&value_record->YPlaDevice));
2133 if (value_format & OTF_XAdvDevice)
2134 ASET (val, 4, font_otf_DeviceTable (&value_record->XAdvDevice));
2135 if (value_format & OTF_YAdvDevice)
2136 ASET (val, 4, font_otf_DeviceTable (&value_record->YAdvDevice));
2137 return val;
2140 Lisp_Object
2141 font_otf_Anchor (anchor)
2142 OTF_Anchor *anchor;
2144 Lisp_Object val;
2146 val = Fmake_vector (make_number (anchor->AnchorFormat + 1), Qnil);
2147 ASET (val, 0, make_number (anchor->XCoordinate));
2148 ASET (val, 1, make_number (anchor->YCoordinate));
2149 if (anchor->AnchorFormat == 2)
2150 ASET (val, 2, make_number (anchor->f.f1.AnchorPoint));
2151 else
2153 ASET (val, 3, font_otf_DeviceTable (&anchor->f.f2.XDeviceTable));
2154 ASET (val, 4, font_otf_DeviceTable (&anchor->f.f2.YDeviceTable));
2156 return val;
2158 #endif /* HAVE_LIBOTF */
2159 #endif /* 0 */
2162 /* Font sorting */
2164 static unsigned font_score P_ ((Lisp_Object, Lisp_Object *));
2165 static int font_compare P_ ((const void *, const void *));
2166 static Lisp_Object font_sort_entites P_ ((Lisp_Object, Lisp_Object,
2167 Lisp_Object, int));
2169 /* We sort fonts by scoring each of them against a specified
2170 font-spec. The score value is 32 bit (`unsigned'), and the smaller
2171 the value is, the closer the font is to the font-spec.
2173 The lowest 2 bits of the score is used for driver type. The font
2174 available by the most preferred font driver is 0.
2176 Each 7-bit in the higher 28 bits are used for numeric properties
2177 WEIGHT, SLANT, WIDTH, and SIZE. */
2179 /* How many bits to shift to store the difference value of each font
2180 property in a score. Note that flots for FONT_TYPE_INDEX and
2181 FONT_REGISTRY_INDEX are not used. */
2182 static int sort_shift_bits[FONT_SIZE_INDEX + 1];
2184 /* Score font-entity ENTITY against properties of font-spec SPEC_PROP.
2185 The return value indicates how different ENTITY is compared with
2186 SPEC_PROP. */
2188 static unsigned
2189 font_score (entity, spec_prop)
2190 Lisp_Object entity, *spec_prop;
2192 unsigned score = 0;
2193 int i;
2195 /* Score three style numeric fields. Maximum difference is 127. */
2196 for (i = FONT_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX; i++)
2197 if (! NILP (spec_prop[i]) && ! EQ (AREF (entity, i), spec_prop[i]))
2199 int diff = (XINT (AREF (entity, i)) >> 8) - (XINT (spec_prop[i]) >> 8);
2201 if (diff < 0)
2202 diff = - diff;
2203 if (diff > 0)
2204 score |= min (diff, 127) << sort_shift_bits[i];
2207 /* Score the size. Maximum difference is 127. */
2208 i = FONT_SIZE_INDEX;
2209 if (! NILP (spec_prop[i]) && XINT (AREF (entity, i)) > 0)
2211 /* We use the higher 6-bit for the actual size difference. The
2212 lowest bit is set if the DPI is different. */
2213 int diff = XINT (spec_prop[i]) - XINT (AREF (entity, i));
2215 if (diff < 0)
2216 diff = - diff;
2217 diff <<= 1;
2218 if (! NILP (spec_prop[FONT_DPI_INDEX])
2219 && ! EQ (spec_prop[FONT_DPI_INDEX], AREF (entity, FONT_DPI_INDEX)))
2220 diff |= 1;
2221 score |= min (diff, 127) << sort_shift_bits[FONT_SIZE_INDEX];
2224 return score;
2228 /* The comparison function for qsort. */
2230 static int
2231 font_compare (d1, d2)
2232 const void *d1, *d2;
2234 return (*(unsigned *) d1 - *(unsigned *) d2);
2238 /* The structure for elements being sorted by qsort. */
2239 struct font_sort_data
2241 unsigned score;
2242 Lisp_Object entity;
2246 /* Sort font-entities in vector VEC by closeness to font-spec PREFER.
2247 If PREFER specifies a point-size, calculate the corresponding
2248 pixel-size from QCdpi property of PREFER or from the Y-resolution
2249 of FRAME before sorting.
2251 If BEST-ONLY is nonzero, return the best matching entity. Otherwise,
2252 return the sorted VEC. */
2254 static Lisp_Object
2255 font_sort_entites (vec, prefer, frame, best_only)
2256 Lisp_Object vec, prefer, frame;
2257 int best_only;
2259 Lisp_Object prefer_prop[FONT_SPEC_MAX];
2260 int len, i;
2261 struct font_sort_data *data;
2262 unsigned best_score;
2263 Lisp_Object best_entity, driver_type;
2264 int driver_order;
2265 struct frame *f = XFRAME (frame);
2266 struct font_driver_list *list;
2267 USE_SAFE_ALLOCA;
2269 len = ASIZE (vec);
2270 if (len <= 1)
2271 return best_only ? AREF (vec, 0) : vec;
2273 for (i = FONT_WEIGHT_INDEX; i <= FONT_DPI_INDEX; i++)
2274 prefer_prop[i] = AREF (prefer, i);
2275 if (FLOATP (prefer_prop[FONT_SIZE_INDEX]))
2276 prefer_prop[FONT_SIZE_INDEX]
2277 = make_number (font_pixel_size (XFRAME (frame), prefer));
2279 /* Scoring and sorting. */
2280 SAFE_ALLOCA (data, struct font_sort_data *, (sizeof *data) * len);
2281 best_score = 0xFFFFFFFF;
2282 /* We are sure that the length of VEC > 1. */
2283 driver_type = AREF (AREF (vec, 0), FONT_TYPE_INDEX);
2284 for (driver_order = 0, list = f->font_driver_list; list;
2285 driver_order++, list = list->next)
2286 if (EQ (driver_type, list->driver->type))
2287 break;
2288 best_entity = data[0].entity = AREF (vec, 0);
2289 best_score = data[0].score
2290 = font_score (data[0].entity, prefer_prop) | driver_order;
2291 for (i = 0; i < len; i++)
2293 if (!EQ (driver_type, AREF (AREF (vec, i), FONT_TYPE_INDEX)))
2294 for (driver_order = 0, list = f->font_driver_list; list;
2295 driver_order++, list = list->next)
2296 if (EQ (driver_type, list->driver->type))
2297 break;
2298 data[i].entity = AREF (vec, i);
2299 data[i].score = font_score (data[i].entity, prefer_prop) | driver_order;
2300 if (best_only && best_score > data[i].score)
2302 best_score = data[i].score;
2303 best_entity = data[i].entity;
2304 if (best_score == 0)
2305 break;
2308 if (! best_only)
2310 qsort (data, len, sizeof *data, font_compare);
2311 for (i = 0; i < len; i++)
2312 ASET (vec, i, data[i].entity);
2314 else
2315 vec = best_entity;
2316 SAFE_FREE ();
2318 font_add_log ("sort-by", prefer, vec);
2319 return vec;
2323 /* API of Font Service Layer. */
2325 /* Reflect ORDER (see the variable font_sort_order in xfaces.c) to
2326 sort_shift_bits. Finternal_set_font_selection_order calls this
2327 function with font_sort_order after setting up it. */
2329 void
2330 font_update_sort_order (order)
2331 int *order;
2333 int i, shift_bits;
2335 for (i = 0, shift_bits = 23; i < 4; i++, shift_bits -= 7)
2337 int xlfd_idx = order[i];
2339 if (xlfd_idx == XLFD_WEIGHT_INDEX)
2340 sort_shift_bits[FONT_WEIGHT_INDEX] = shift_bits;
2341 else if (xlfd_idx == XLFD_SLANT_INDEX)
2342 sort_shift_bits[FONT_SLANT_INDEX] = shift_bits;
2343 else if (xlfd_idx == XLFD_SWIDTH_INDEX)
2344 sort_shift_bits[FONT_WIDTH_INDEX] = shift_bits;
2345 else
2346 sort_shift_bits[FONT_SIZE_INDEX] = shift_bits;
2350 static int
2351 font_check_otf_features (script, langsys, features, table)
2352 Lisp_Object script, langsys, features, table;
2354 Lisp_Object val;
2355 int negative;
2357 table = assq_no_quit (script, table);
2358 if (NILP (table))
2359 return 0;
2360 table = XCDR (table);
2361 if (! NILP (langsys))
2363 table = assq_no_quit (langsys, table);
2364 if (NILP (table))
2365 return 0;
2367 else
2369 val = assq_no_quit (Qnil, table);
2370 if (NILP (val))
2371 table = XCAR (table);
2372 else
2373 table = val;
2375 table = XCDR (table);
2376 for (negative = 0; CONSP (features); features = XCDR (features))
2378 if (NILP (XCAR (features)))
2379 negative = 1;
2380 if (NILP (Fmemq (XCAR (features), table)) != negative)
2381 return 0;
2383 return 1;
2386 /* Check if OTF_CAPABILITY satisfies SPEC (otf-spec). */
2388 static int
2389 font_check_otf (Lisp_Object spec, Lisp_Object otf_capability)
2391 Lisp_Object script, langsys = Qnil, gsub = Qnil, gpos = Qnil;
2393 script = XCAR (spec);
2394 spec = XCDR (spec);
2395 if (! NILP (spec))
2397 langsys = XCAR (spec);
2398 spec = XCDR (spec);
2399 if (! NILP (spec))
2401 gsub = XCAR (spec);
2402 spec = XCDR (spec);
2403 if (! NILP (spec))
2404 gpos = XCAR (spec);
2408 if (! NILP (gsub) && ! font_check_otf_features (script, langsys, gsub,
2409 XCAR (otf_capability)))
2410 return 0;
2411 if (! NILP (gpos) && ! font_check_otf_features (script, langsys, gpos,
2412 XCDR (otf_capability)))
2413 return 0;
2414 return 1;
2419 /* Check if FONT (font-entity or font-object) matches with the font
2420 specification SPEC. */
2423 font_match_p (spec, font)
2424 Lisp_Object spec, font;
2426 Lisp_Object prop[FONT_SPEC_MAX], *props;
2427 Lisp_Object extra, font_extra;
2428 int i;
2430 for (i = FONT_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX; i++)
2431 if (! NILP (AREF (spec, i))
2432 && ! NILP (AREF (font, i))
2433 && ! EQ (AREF (spec, i), AREF (font, i)))
2434 return 0;
2435 props = XFONT_SPEC (spec)->props;
2436 if (FLOATP (props[FONT_SIZE_INDEX]))
2438 for (i = FONT_FOUNDRY_INDEX; i < FONT_SIZE_INDEX; i++)
2439 prop[i] = AREF (spec, i);
2440 prop[FONT_SIZE_INDEX]
2441 = make_number (font_pixel_size (XFRAME (selected_frame), spec));
2442 props = prop;
2445 if (font_score (font, props) > 0)
2446 return 0;
2447 extra = AREF (spec, FONT_EXTRA_INDEX);
2448 font_extra = AREF (font, FONT_EXTRA_INDEX);
2449 for (; CONSP (extra); extra = XCDR (extra))
2451 Lisp_Object key = XCAR (XCAR (extra));
2452 Lisp_Object val = XCDR (XCAR (extra)), val2;
2454 if (EQ (key, QClang))
2456 val2 = assq_no_quit (key, font_extra);
2457 if (NILP (val2))
2458 return 0;
2459 val2 = XCDR (val2);
2460 if (CONSP (val))
2462 if (! CONSP (val2))
2463 return 0;
2464 while (CONSP (val))
2465 if (NILP (Fmemq (val, val2)))
2466 return 0;
2468 else
2469 if (CONSP (val2)
2470 ? NILP (Fmemq (val, XCDR (val2)))
2471 : ! EQ (val, val2))
2472 return 0;
2474 else if (EQ (key, QCscript))
2476 val2 = assq_no_quit (val, Vscript_representative_chars);
2477 if (CONSP (val2))
2479 val2 = XCDR (val2);
2480 if (CONSP (val2))
2482 /* All characters in the list must be supported. */
2483 for (; CONSP (val2); val2 = XCDR (val2))
2485 if (! NATNUMP (XCAR (val2)))
2486 continue;
2487 if (font_encode_char (font, XFASTINT (XCAR (val2)))
2488 == FONT_INVALID_CODE)
2489 return 0;
2492 else if (VECTORP (val2))
2494 /* At most one character in the vector must be supported. */
2495 for (i = 0; i < ASIZE (val2); i++)
2497 if (! NATNUMP (AREF (val2, i)))
2498 continue;
2499 if (font_encode_char (font, XFASTINT (AREF (val2, i)))
2500 != FONT_INVALID_CODE)
2501 break;
2503 if (i == ASIZE (val2))
2504 return 0;
2508 else if (EQ (key, QCotf))
2510 struct font *fontp;
2512 if (! FONT_OBJECT_P (font))
2513 return 0;
2514 fontp = XFONT_OBJECT (font);
2515 if (! fontp->driver->otf_capability)
2516 return 0;
2517 val2 = fontp->driver->otf_capability (fontp);
2518 if (NILP (val2) || ! font_check_otf (val, val2))
2519 return 0;
2523 return 1;
2527 /* Font cache
2529 Each font backend has the callback function get_cache, and it
2530 returns a cons cell of which cdr part can be freely used for
2531 caching fonts. The cons cell may be shared by multiple frames
2532 and/or multiple font drivers. So, we arrange the cdr part as this:
2534 ((DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) ...)
2536 where DRIVER-TYPE is a symbol such as `x', `xft', etc., NUM-FRAMES
2537 is a number frames sharing this cache, and FONT-CACHE-DATA is a
2538 cons (FONT-SPEC FONT-ENTITY ...). */
2540 static void font_prepare_cache P_ ((FRAME_PTR, struct font_driver *));
2541 static void font_finish_cache P_ ((FRAME_PTR, struct font_driver *));
2542 static Lisp_Object font_get_cache P_ ((FRAME_PTR, struct font_driver *));
2543 static void font_clear_cache P_ ((FRAME_PTR, Lisp_Object,
2544 struct font_driver *));
2546 static void
2547 font_prepare_cache (f, driver)
2548 FRAME_PTR f;
2549 struct font_driver *driver;
2551 Lisp_Object cache, val;
2553 cache = driver->get_cache (f);
2554 val = XCDR (cache);
2555 while (CONSP (val) && ! EQ (XCAR (XCAR (val)), driver->type))
2556 val = XCDR (val);
2557 if (NILP (val))
2559 val = Fcons (driver->type, Fcons (make_number (1), Qnil));
2560 XSETCDR (cache, Fcons (val, XCDR (cache)));
2562 else
2564 val = XCDR (XCAR (val));
2565 XSETCAR (val, make_number (XINT (XCAR (val)) + 1));
2570 static void
2571 font_finish_cache (f, driver)
2572 FRAME_PTR f;
2573 struct font_driver *driver;
2575 Lisp_Object cache, val, tmp;
2578 cache = driver->get_cache (f);
2579 val = XCDR (cache);
2580 while (CONSP (val) && ! EQ (XCAR (XCAR (val)), driver->type))
2581 cache = val, val = XCDR (val);
2582 font_assert (! NILP (val));
2583 tmp = XCDR (XCAR (val));
2584 XSETCAR (tmp, make_number (XINT (XCAR (tmp)) - 1));
2585 if (XINT (XCAR (tmp)) == 0)
2587 font_clear_cache (f, XCAR (val), driver);
2588 XSETCDR (cache, XCDR (val));
2593 static Lisp_Object
2594 font_get_cache (f, driver)
2595 FRAME_PTR f;
2596 struct font_driver *driver;
2598 Lisp_Object val = driver->get_cache (f);
2599 Lisp_Object type = driver->type;
2601 font_assert (CONSP (val));
2602 for (val = XCDR (val); ! EQ (XCAR (XCAR (val)), type); val = XCDR (val));
2603 font_assert (CONSP (val));
2604 /* VAL = ((DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) ...) */
2605 val = XCDR (XCAR (val));
2606 return val;
2609 static int num_fonts;
2611 static void
2612 font_clear_cache (f, cache, driver)
2613 FRAME_PTR f;
2614 Lisp_Object cache;
2615 struct font_driver *driver;
2617 Lisp_Object tail, elt;
2619 /* CACHE = (DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) */
2620 for (tail = XCDR (XCDR (cache)); CONSP (tail); tail = XCDR (tail))
2622 elt = XCAR (tail);
2623 if (CONSP (elt) && FONT_SPEC_P (XCAR (elt)) && VECTORP (XCDR (elt)))
2625 Lisp_Object vec = XCDR (elt);
2626 int i;
2628 for (i = 0; i < ASIZE (vec); i++)
2630 Lisp_Object entity = AREF (vec, i);
2632 if (EQ (driver->type, AREF (entity, FONT_TYPE_INDEX)))
2634 Lisp_Object objlist = AREF (entity, FONT_OBJLIST_INDEX);
2636 for (; CONSP (objlist); objlist = XCDR (objlist))
2638 Lisp_Object val = XCAR (objlist);
2639 struct font *font = XFONT_OBJECT (val);
2641 if (! NILP (AREF (val, FONT_TYPE_INDEX)))
2643 font_assert (font && driver == font->driver);
2644 driver->close (f, font);
2645 num_fonts--;
2648 if (driver->free_entity)
2649 driver->free_entity (entity);
2654 XSETCDR (cache, Qnil);
2658 static Lisp_Object scratch_font_spec, scratch_font_prefer;
2660 Lisp_Object
2661 font_delete_unmatched (list, spec, size)
2662 Lisp_Object list, spec;
2663 int size;
2665 Lisp_Object entity, val;
2666 enum font_property_index prop;
2668 for (val = Qnil; CONSP (list); list = XCDR (list))
2670 entity = XCAR (list);
2671 for (prop = FONT_WEIGHT_INDEX; prop < FONT_SIZE_INDEX; prop++)
2672 if (INTEGERP (AREF (spec, prop))
2673 && ((XINT (AREF (spec, prop)) >> 8)
2674 != (XINT (AREF (entity, prop)) >> 8)))
2675 prop = FONT_SPEC_MAX;
2676 if (prop < FONT_SPEC_MAX
2677 && size
2678 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
2680 int diff = XINT (AREF (entity, FONT_SIZE_INDEX)) - size;
2682 if (diff != 0
2683 && (diff < 0 ? -diff > FONT_PIXEL_SIZE_QUANTUM
2684 : diff > FONT_PIXEL_SIZE_QUANTUM))
2685 prop = FONT_SPEC_MAX;
2687 if (prop < FONT_SPEC_MAX
2688 && INTEGERP (AREF (spec, FONT_DPI_INDEX))
2689 && INTEGERP (AREF (entity, FONT_DPI_INDEX))
2690 && XINT (AREF (entity, FONT_DPI_INDEX)) != 0
2691 && ! EQ (AREF (spec, FONT_DPI_INDEX), AREF (entity, FONT_DPI_INDEX)))
2692 prop = FONT_SPEC_MAX;
2693 if (prop < FONT_SPEC_MAX
2694 && INTEGERP (AREF (spec, FONT_AVGWIDTH_INDEX))
2695 && INTEGERP (AREF (entity, FONT_AVGWIDTH_INDEX))
2696 && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) != 0
2697 && ! EQ (AREF (spec, FONT_AVGWIDTH_INDEX),
2698 AREF (entity, FONT_AVGWIDTH_INDEX)))
2699 prop = FONT_SPEC_MAX;
2700 if (prop < FONT_SPEC_MAX)
2701 val = Fcons (entity, val);
2703 return val;
2707 /* Return a vector of font-entities matching with SPEC on FRAME. */
2709 Lisp_Object
2710 font_list_entities (frame, spec)
2711 Lisp_Object frame, spec;
2713 FRAME_PTR f = XFRAME (frame);
2714 struct font_driver_list *driver_list = f->font_driver_list;
2715 Lisp_Object ftype, val;
2716 Lisp_Object *vec;
2717 int size;
2718 int need_filtering = 0;
2719 int i;
2721 font_assert (FONT_SPEC_P (spec));
2723 if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
2724 size = XINT (AREF (spec, FONT_SIZE_INDEX));
2725 else if (FLOATP (AREF (spec, FONT_SIZE_INDEX)))
2726 size = font_pixel_size (f, spec);
2727 else
2728 size = 0;
2730 ftype = AREF (spec, FONT_TYPE_INDEX);
2731 for (i = FONT_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX; i++)
2732 ASET (scratch_font_spec, i, AREF (spec, i));
2733 for (i = FONT_WEIGHT_INDEX; i < FONT_EXTRA_INDEX; i++)
2735 ASET (scratch_font_spec, i, Qnil);
2736 if (! NILP (AREF (spec, i)))
2737 need_filtering = 1;
2738 if (i == FONT_DPI_INDEX)
2739 /* Skip FONT_SPACING_INDEX */
2740 i++;
2742 ASET (scratch_font_spec, FONT_SPACING_INDEX, AREF (spec, FONT_SPACING_INDEX));
2743 ASET (scratch_font_spec, FONT_EXTRA_INDEX, AREF (spec, FONT_EXTRA_INDEX));
2745 vec = alloca (sizeof (Lisp_Object) * num_font_drivers);
2746 if (! vec)
2747 return null_vector;
2749 for (i = 0; driver_list; driver_list = driver_list->next)
2750 if (driver_list->on
2751 && (NILP (ftype) || EQ (driver_list->driver->type, ftype)))
2753 Lisp_Object cache = font_get_cache (f, driver_list->driver);
2755 ASET (scratch_font_spec, FONT_TYPE_INDEX, driver_list->driver->type);
2756 val = assoc_no_quit (scratch_font_spec, XCDR (cache));
2757 if (CONSP (val))
2758 val = XCDR (val);
2759 else
2761 Lisp_Object copy;
2763 val = driver_list->driver->list (frame, scratch_font_spec);
2764 copy = Fcopy_font_spec (scratch_font_spec);
2765 ASET (copy, FONT_TYPE_INDEX, driver_list->driver->type);
2766 XSETCDR (cache, Fcons (Fcons (copy, val), XCDR (cache)));
2768 if (! NILP (val) && need_filtering)
2769 val = font_delete_unmatched (val, spec, size);
2770 if (! NILP (val))
2771 vec[i++] = val;
2774 val = (i > 0 ? Fvconcat (i, vec) : null_vector);
2775 font_add_log ("list", spec, val);
2776 return (val);
2780 /* Return a font entity matching with SPEC on FRAME. ATTRS, if non
2781 nil, is an array of face's attributes, which specifies preferred
2782 font-related attributes. */
2784 static Lisp_Object
2785 font_matching_entity (f, attrs, spec)
2786 FRAME_PTR f;
2787 Lisp_Object *attrs, spec;
2789 struct font_driver_list *driver_list = f->font_driver_list;
2790 Lisp_Object ftype, size, entity;
2791 Lisp_Object frame;
2792 Lisp_Object work = Fcopy_font_spec (spec);
2794 XSETFRAME (frame, f);
2795 ftype = AREF (spec, FONT_TYPE_INDEX);
2796 size = AREF (spec, FONT_SIZE_INDEX);
2798 if (FLOATP (size))
2799 ASET (work, FONT_SIZE_INDEX, make_number (font_pixel_size (f, spec)));
2800 FONT_SET_STYLE (work, FONT_WEIGHT_INDEX, attrs[LFACE_WEIGHT_INDEX]);
2801 FONT_SET_STYLE (work, FONT_SLANT_INDEX, attrs[LFACE_SLANT_INDEX]);
2802 FONT_SET_STYLE (work, FONT_WIDTH_INDEX, attrs[LFACE_SWIDTH_INDEX]);
2804 entity = Qnil;
2805 for (; driver_list; driver_list = driver_list->next)
2806 if (driver_list->on
2807 && (NILP (ftype) || EQ (driver_list->driver->type, ftype)))
2809 Lisp_Object cache = font_get_cache (f, driver_list->driver);
2810 Lisp_Object copy;
2812 ASET (work, FONT_TYPE_INDEX, driver_list->driver->type);
2813 entity = assoc_no_quit (work, XCDR (cache));
2814 if (CONSP (entity))
2815 entity = XCDR (entity);
2816 else
2818 entity = driver_list->driver->match (frame, work);
2819 copy = Fcopy_font_spec (work);
2820 ASET (copy, FONT_TYPE_INDEX, driver_list->driver->type);
2821 XSETCDR (cache, Fcons (Fcons (copy, entity), XCDR (cache)));
2823 if (! NILP (entity))
2824 break;
2826 font_add_log ("match", work, entity);
2827 return entity;
2831 /* Open a font of ENTITY and PIXEL_SIZE on frame F, and return the
2832 opened font object. */
2834 static Lisp_Object
2835 font_open_entity (f, entity, pixel_size)
2836 FRAME_PTR f;
2837 Lisp_Object entity;
2838 int pixel_size;
2840 struct font_driver_list *driver_list;
2841 Lisp_Object objlist, size, val, font_object;
2842 struct font *font;
2843 int min_width, height;
2845 font_assert (FONT_ENTITY_P (entity));
2846 size = AREF (entity, FONT_SIZE_INDEX);
2847 if (XINT (size) != 0)
2848 pixel_size = XINT (size);
2850 for (objlist = AREF (entity, FONT_OBJLIST_INDEX); CONSP (objlist);
2851 objlist = XCDR (objlist))
2852 if (! NILP (AREF (XCAR (objlist), FONT_TYPE_INDEX))
2853 && XFONT_OBJECT (XCAR (objlist))->pixel_size == pixel_size)
2854 return XCAR (objlist);
2856 val = AREF (entity, FONT_TYPE_INDEX);
2857 for (driver_list = f->font_driver_list;
2858 driver_list && ! EQ (driver_list->driver->type, val);
2859 driver_list = driver_list->next);
2860 if (! driver_list)
2861 return Qnil;
2863 font_object = driver_list->driver->open (f, entity, pixel_size);
2864 font_add_log ("open", entity, font_object);
2865 if (NILP (font_object))
2866 return Qnil;
2867 ASET (entity, FONT_OBJLIST_INDEX,
2868 Fcons (font_object, AREF (entity, FONT_OBJLIST_INDEX)));
2869 ASET (font_object, FONT_OBJLIST_INDEX, Qnil);
2870 num_fonts++;
2872 font = XFONT_OBJECT (font_object);
2873 min_width = (font->min_width ? font->min_width
2874 : font->average_width ? font->average_width
2875 : font->space_width ? font->space_width
2876 : 1);
2877 height = (font->height ? font->height : 1);
2878 #ifdef HAVE_WINDOW_SYSTEM
2879 FRAME_X_DISPLAY_INFO (f)->n_fonts++;
2880 if (FRAME_X_DISPLAY_INFO (f)->n_fonts == 1)
2882 FRAME_SMALLEST_CHAR_WIDTH (f) = min_width;
2883 FRAME_SMALLEST_FONT_HEIGHT (f) = height;
2884 fonts_changed_p = 1;
2886 else
2888 if (FRAME_SMALLEST_CHAR_WIDTH (f) > min_width)
2889 FRAME_SMALLEST_CHAR_WIDTH (f) = min_width, fonts_changed_p = 1;
2890 if (FRAME_SMALLEST_FONT_HEIGHT (f) > height)
2891 FRAME_SMALLEST_FONT_HEIGHT (f) = height, fonts_changed_p = 1;
2893 #endif
2895 return font_object;
2899 /* Close FONT_OBJECT that is opened on frame F. */
2901 void
2902 font_close_object (f, font_object)
2903 FRAME_PTR f;
2904 Lisp_Object font_object;
2906 struct font *font = XFONT_OBJECT (font_object);
2908 if (NILP (AREF (font_object, FONT_TYPE_INDEX)))
2909 /* Already closed. */
2910 return;
2911 font_add_log ("close", font_object, Qnil);
2912 font->driver->close (f, font);
2913 #ifdef HAVE_WINDOW_SYSTEM
2914 font_assert (FRAME_X_DISPLAY_INFO (f)->n_fonts);
2915 FRAME_X_DISPLAY_INFO (f)->n_fonts--;
2916 #endif
2917 num_fonts--;
2921 /* Return 1 if FONT on F has a glyph for character C, 0 if not, -1 if
2922 FONT is a font-entity and it must be opened to check. */
2925 font_has_char (f, font, c)
2926 FRAME_PTR f;
2927 Lisp_Object font;
2928 int c;
2930 struct font *fontp;
2932 if (FONT_ENTITY_P (font))
2934 Lisp_Object type = AREF (font, FONT_TYPE_INDEX);
2935 struct font_driver_list *driver_list;
2937 for (driver_list = f->font_driver_list;
2938 driver_list && ! EQ (driver_list->driver->type, type);
2939 driver_list = driver_list->next);
2940 if (! driver_list)
2941 return 0;
2942 if (! driver_list->driver->has_char)
2943 return -1;
2944 return driver_list->driver->has_char (font, c);
2947 font_assert (FONT_OBJECT_P (font));
2948 fontp = XFONT_OBJECT (font);
2949 if (fontp->driver->has_char)
2951 int result = fontp->driver->has_char (font, c);
2953 if (result >= 0)
2954 return result;
2956 return (fontp->driver->encode_char (fontp, c) != FONT_INVALID_CODE);
2960 /* Return the glyph ID of FONT_OBJECT for character C. */
2962 unsigned
2963 font_encode_char (font_object, c)
2964 Lisp_Object font_object;
2965 int c;
2967 struct font *font;
2969 font_assert (FONT_OBJECT_P (font_object));
2970 font = XFONT_OBJECT (font_object);
2971 return font->driver->encode_char (font, c);
2975 /* Return the name of FONT_OBJECT. */
2977 Lisp_Object
2978 font_get_name (font_object)
2979 Lisp_Object font_object;
2981 font_assert (FONT_OBJECT_P (font_object));
2982 return AREF (font_object, FONT_NAME_INDEX);
2986 /* Return the specification of FONT_OBJECT. */
2988 Lisp_Object
2989 font_get_spec (font_object)
2990 Lisp_Object font_object;
2992 Lisp_Object spec = font_make_spec ();
2993 int i;
2995 for (i = 0; i < FONT_SIZE_INDEX; i++)
2996 ASET (spec, i, AREF (font_object, i));
2997 ASET (spec, FONT_SIZE_INDEX,
2998 make_number (XFONT_OBJECT (font_object)->pixel_size));
2999 return spec;
3002 Lisp_Object
3003 font_spec_from_name (font_name)
3004 Lisp_Object font_name;
3006 Lisp_Object args[2];
3008 args[0] = QCname;
3009 args[1] = font_name;
3010 return Ffont_spec (2, args);
3014 void
3015 font_clear_prop (attrs, prop)
3016 Lisp_Object *attrs;
3017 enum font_property_index prop;
3019 Lisp_Object font = attrs[LFACE_FONT_INDEX];
3021 if (! FONTP (font))
3022 return;
3023 if (NILP (AREF (font, prop))
3024 && prop != FONT_FAMILY_INDEX
3025 && prop != FONT_FOUNDRY_INDEX
3026 && prop != FONT_WIDTH_INDEX
3027 && prop != FONT_SIZE_INDEX)
3028 return;
3029 font = Fcopy_font_spec (font);
3030 ASET (font, prop, Qnil);
3031 if (prop == FONT_FAMILY_INDEX || prop == FONT_FOUNDRY_INDEX)
3033 if (prop == FONT_FAMILY_INDEX)
3034 ASET (font, FONT_FOUNDRY_INDEX, Qnil);
3035 ASET (font, FONT_ADSTYLE_INDEX, Qnil);
3036 ASET (font, FONT_REGISTRY_INDEX, Qnil);
3037 ASET (font, FONT_SIZE_INDEX, Qnil);
3038 ASET (font, FONT_DPI_INDEX, Qnil);
3039 ASET (font, FONT_SPACING_INDEX, Qnil);
3040 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3042 else if (prop == FONT_SIZE_INDEX)
3044 ASET (font, FONT_DPI_INDEX, Qnil);
3045 ASET (font, FONT_SPACING_INDEX, Qnil);
3046 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3048 else if (prop == FONT_WIDTH_INDEX)
3049 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3050 attrs[LFACE_FONT_INDEX] = font;
3053 void
3054 font_update_lface (f, attrs)
3055 FRAME_PTR f;
3056 Lisp_Object *attrs;
3058 Lisp_Object spec;
3060 spec = attrs[LFACE_FONT_INDEX];
3061 if (! FONT_SPEC_P (spec))
3062 return;
3064 if (! NILP (AREF (spec, FONT_FOUNDRY_INDEX)))
3065 attrs[LFACE_FOUNDRY_INDEX] = SYMBOL_NAME (AREF (spec, FONT_FOUNDRY_INDEX));
3066 if (! NILP (AREF (spec, FONT_FAMILY_INDEX)))
3067 attrs[LFACE_FAMILY_INDEX] = SYMBOL_NAME (AREF (spec, FONT_FAMILY_INDEX));
3068 if (! NILP (AREF (spec, FONT_WEIGHT_INDEX)))
3069 attrs[LFACE_WEIGHT_INDEX] = FONT_WEIGHT_FOR_FACE (spec);
3070 if (! NILP (AREF (spec, FONT_SLANT_INDEX)))
3071 attrs[LFACE_SLANT_INDEX] = FONT_SLANT_FOR_FACE (spec);;
3072 if (! NILP (AREF (spec, FONT_WIDTH_INDEX)))
3073 attrs[LFACE_SWIDTH_INDEX] = FONT_WIDTH_FOR_FACE (spec);
3074 if (! NILP (AREF (spec, FONT_SIZE_INDEX)))
3076 int point;
3078 if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
3080 Lisp_Object val;
3081 int dpi = f->resy;
3083 val = Ffont_get (spec, QCdpi);
3084 if (! NILP (val))
3085 dpi = XINT (val);
3086 point = PIXEL_TO_POINT (XINT (AREF (spec, FONT_SIZE_INDEX)) * 10,
3087 dpi);
3089 else if (FLOATP (AREF (spec, FONT_SIZE_INDEX)))
3090 point = XFLOAT_DATA (AREF (spec, FONT_SIZE_INDEX)) * 10;
3091 attrs[LFACE_HEIGHT_INDEX] = make_number (point);
3096 /* Return a font-entity satisfying SPEC and best matching with face's
3097 font related attributes in ATTRS. C, if not negative, is a
3098 character that the entity must support. */
3100 Lisp_Object
3101 font_find_for_lface (f, attrs, spec, c)
3102 FRAME_PTR f;
3103 Lisp_Object *attrs;
3104 Lisp_Object spec;
3105 int c;
3107 Lisp_Object work;
3108 Lisp_Object frame, entities, val, props[FONT_REGISTRY_INDEX + 1] ;
3109 Lisp_Object size, foundry[3], *family, registry[3], adstyle[3];
3110 int pixel_size;
3111 int i, j, k, l, result;
3113 registry[0] = AREF (spec, FONT_REGISTRY_INDEX);
3114 if (NILP (registry[0]))
3116 registry[0] = DEFAULT_ENCODING;
3117 registry[1] = Qascii_0;
3118 registry[2] = null_vector;
3120 else
3121 registry[1] = null_vector;
3123 if (c >= 0 && ! NILP (AREF (spec, FONT_REGISTRY_INDEX)))
3125 struct charset *encoding, *repertory;
3127 if (font_registry_charsets (AREF (spec, FONT_REGISTRY_INDEX),
3128 &encoding, &repertory) < 0)
3129 return Qnil;
3130 if (repertory)
3132 if (ENCODE_CHAR (repertory, c) == CHARSET_INVALID_CODE (repertory))
3133 return Qnil;
3134 /* Any font of this registry support C. So, let's
3135 suppress the further checking. */
3136 c = -1;
3138 else if (c > encoding->max_char)
3139 return Qnil;
3142 work = Fcopy_font_spec (spec);
3143 XSETFRAME (frame, f);
3144 size = AREF (spec, FONT_SIZE_INDEX);
3145 pixel_size = font_pixel_size (f, spec);
3146 if (pixel_size == 0)
3148 double pt = XINT (attrs[LFACE_HEIGHT_INDEX]);
3150 pixel_size = POINT_TO_PIXEL (pt / 10, f->resy);
3152 ASET (work, FONT_SIZE_INDEX, Qnil);
3153 foundry[0] = AREF (work, FONT_FOUNDRY_INDEX);
3154 if (! NILP (foundry[0]))
3155 foundry[1] = null_vector;
3156 else if (STRINGP (attrs[LFACE_FOUNDRY_INDEX]))
3158 val = attrs[LFACE_FOUNDRY_INDEX];
3159 foundry[0] = font_intern_prop ((char *) SDATA (val), SBYTES (val), 1);
3160 foundry[1] = Qnil;
3161 foundry[2] = null_vector;
3163 else
3164 foundry[0] = Qnil, foundry[1] = null_vector;
3166 adstyle[0] = AREF (work, FONT_ADSTYLE_INDEX);
3167 if (! NILP (adstyle[0]))
3168 adstyle[1] = null_vector;
3169 else if (FONTP (attrs[LFACE_FONT_INDEX]))
3171 Lisp_Object face_font = attrs[LFACE_FONT_INDEX];
3173 if (! NILP (AREF (face_font, FONT_ADSTYLE_INDEX)))
3175 adstyle[0] = AREF (face_font, FONT_ADSTYLE_INDEX);
3176 adstyle[1] = Qnil;
3177 adstyle[2] = null_vector;
3179 else
3180 adstyle[0] = Qnil, adstyle[1] = null_vector;
3182 else
3183 adstyle[0] = Qnil, adstyle[1] = null_vector;
3186 val = AREF (work, FONT_FAMILY_INDEX);
3187 if (NILP (val) && STRINGP (attrs[LFACE_FAMILY_INDEX]))
3189 val = attrs[LFACE_FAMILY_INDEX];
3190 val = font_intern_prop ((char *) SDATA (val), SBYTES (val), 1);
3192 if (NILP (val))
3194 family = alloca ((sizeof family[0]) * 2);
3195 family[0] = Qnil;
3196 family[1] = null_vector; /* terminator. */
3198 else
3200 Lisp_Object alters
3201 = Fassoc_string (val, Vface_alternative_font_family_alist,
3202 #ifndef HAVE_NS
3204 #else
3205 Qnil
3206 #endif
3209 if (! NILP (alters))
3211 family = alloca ((sizeof family[0]) * (XINT (Flength (alters)) + 2));
3212 for (i = 0; CONSP (alters); i++, alters = XCDR (alters))
3213 family[i] = XCAR (alters);
3214 if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
3215 family[i++] = Qnil;
3216 family[i] = null_vector;
3218 else
3220 family = alloca ((sizeof family[0]) * 3);
3221 i = 0;
3222 family[i++] = val;
3223 if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
3224 family[i++] = Qnil;
3225 family[i] = null_vector;
3229 for (i = 0; SYMBOLP (family[i]); i++)
3231 ASET (work, FONT_FAMILY_INDEX, family[i]);
3232 for (j = 0; SYMBOLP (foundry[j]); j++)
3234 ASET (work, FONT_FOUNDRY_INDEX, foundry[j]);
3235 for (k = 0; SYMBOLP (registry[k]); k++)
3237 ASET (work, FONT_REGISTRY_INDEX, registry[k]);
3238 for (l = 0; SYMBOLP (adstyle[l]); l++)
3240 ASET (work, FONT_ADSTYLE_INDEX, adstyle[l]);
3241 entities = font_list_entities (frame, work);
3242 if (ASIZE (entities) > 0)
3243 goto found;
3248 return Qnil;
3249 found:
3250 if (ASIZE (entities) == 1)
3252 if (c < 0)
3253 return AREF (entities, 0);
3255 else
3257 /* Sort fonts by properties specified in LFACE. */
3258 Lisp_Object prefer = scratch_font_prefer;
3260 for (i = 0; i < FONT_EXTRA_INDEX; i++)
3261 ASET (prefer, i, AREF (work, i));
3262 if (FONTP (attrs[LFACE_FONT_INDEX]))
3264 Lisp_Object face_font = attrs[LFACE_FONT_INDEX];
3266 for (i = 0; i < FONT_EXTRA_INDEX; i++)
3267 if (NILP (AREF (prefer, i)))
3268 ASET (prefer, i, AREF (face_font, i));
3270 if (NILP (AREF (prefer, FONT_WEIGHT_INDEX)))
3271 FONT_SET_STYLE (prefer, FONT_WEIGHT_INDEX, attrs[LFACE_WEIGHT_INDEX]);
3272 if (NILP (AREF (prefer, FONT_SLANT_INDEX)))
3273 FONT_SET_STYLE (prefer, FONT_SLANT_INDEX, attrs[LFACE_SLANT_INDEX]);
3274 if (NILP (AREF (prefer, FONT_WIDTH_INDEX)))
3275 FONT_SET_STYLE (prefer, FONT_WIDTH_INDEX, attrs[LFACE_SWIDTH_INDEX]);
3276 ASET (prefer, FONT_SIZE_INDEX, make_number (pixel_size));
3277 entities = font_sort_entites (entities, prefer, frame, c < 0);
3279 if (c < 0)
3280 return entities;
3282 for (i = 0; i < ASIZE (entities); i++)
3284 int j;
3286 val = AREF (entities, i);
3287 if (i > 0)
3289 for (j = FONT_FOUNDRY_INDEX; j <= FONT_REGISTRY_INDEX; j++)
3290 if (! EQ (AREF (val, j), props[j]))
3291 break;
3292 if (j > FONT_REGISTRY_INDEX)
3293 continue;
3295 for (j = FONT_FOUNDRY_INDEX; j <= FONT_REGISTRY_INDEX; j++)
3296 props[j] = AREF (val, j);
3297 result = font_has_char (f, val, c);
3298 if (result > 0)
3299 return val;
3300 if (result == 0)
3301 return Qnil;
3302 val = font_open_for_lface (f, val, attrs, spec);
3303 if (NILP (val))
3304 continue;
3305 result = font_has_char (f, val, c);
3306 font_close_object (f, val);
3307 if (result > 0)
3308 return AREF (entities, i);
3310 return Qnil;
3314 Lisp_Object
3315 font_open_for_lface (f, entity, attrs, spec)
3316 FRAME_PTR f;
3317 Lisp_Object entity;
3318 Lisp_Object *attrs;
3319 Lisp_Object spec;
3321 int size;
3323 if (INTEGERP (AREF (entity, FONT_SIZE_INDEX))
3324 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
3325 size = XINT (AREF (entity, FONT_SIZE_INDEX));
3326 else if (FONT_SPEC_P (spec) && ! NILP (AREF (spec, FONT_SIZE_INDEX)))
3327 size = font_pixel_size (f, spec);
3328 else
3330 double pt = XINT (attrs[LFACE_HEIGHT_INDEX]);
3332 pt /= 10;
3333 size = POINT_TO_PIXEL (pt, f->resy);
3334 #ifdef HAVE_NS
3335 if (size == 0)
3337 Lisp_Object ffsize = get_frame_param(f, Qfontsize);
3338 size = NUMBERP (ffsize) ? POINT_TO_PIXEL (XINT (ffsize), f->resy) : 0;
3340 #endif
3342 return font_open_entity (f, entity, size);
3346 /* Find a font satisfying SPEC and best matching with face's
3347 attributes in ATTRS on FRAME, and return the opened
3348 font-object. */
3350 Lisp_Object
3351 font_load_for_lface (f, attrs, spec)
3352 FRAME_PTR f;
3353 Lisp_Object *attrs, spec;
3355 Lisp_Object entity;
3357 entity = font_find_for_lface (f, attrs, spec, -1);
3358 if (NILP (entity))
3360 /* No font is listed for SPEC, but each font-backend may have
3361 the different criteria about "font matching". So, try
3362 it. */
3363 entity = font_matching_entity (f, attrs, spec);
3364 if (NILP (entity))
3365 return Qnil;
3367 return font_open_for_lface (f, entity, attrs, spec);
3371 /* Make FACE on frame F ready to use the font opened for FACE. */
3373 void
3374 font_prepare_for_face (f, face)
3375 FRAME_PTR f;
3376 struct face *face;
3378 if (face->font->driver->prepare_face)
3379 face->font->driver->prepare_face (f, face);
3383 /* Make FACE on frame F stop using the font opened for FACE. */
3385 void
3386 font_done_for_face (f, face)
3387 FRAME_PTR f;
3388 struct face *face;
3390 if (face->font->driver->done_face)
3391 face->font->driver->done_face (f, face);
3392 face->extra = NULL;
3396 /* Open a font best matching with NAME on frame F. If no proper font
3397 is found, return Qnil. */
3399 Lisp_Object
3400 font_open_by_name (f, name)
3401 FRAME_PTR f;
3402 char *name;
3404 Lisp_Object args[2];
3405 Lisp_Object spec, attrs[LFACE_VECTOR_SIZE];
3407 args[0] = QCname;
3408 args[1] = make_unibyte_string (name, strlen (name));
3409 spec = Ffont_spec (2, args);
3410 /* We set up the default font-related attributes of a face to prefer
3411 a moderate font. */
3412 attrs[LFACE_FAMILY_INDEX] = attrs[LFACE_FOUNDRY_INDEX] = Qnil;
3413 attrs[LFACE_SWIDTH_INDEX] = attrs[LFACE_WEIGHT_INDEX]
3414 = attrs[LFACE_SLANT_INDEX] = Qnormal;
3415 #ifndef HAVE_NS
3416 attrs[LFACE_HEIGHT_INDEX] = make_number (120);
3417 #else
3418 attrs[LFACE_HEIGHT_INDEX] = make_number (0);
3419 #endif
3420 attrs[LFACE_FONT_INDEX] = Qnil;
3422 return font_load_for_lface (f, attrs, spec);
3426 /* Register font-driver DRIVER. This function is used in two ways.
3428 The first is with frame F non-NULL. In this case, make DRIVER
3429 available (but not yet activated) on F. All frame creaters
3430 (e.g. Fx_create_frame) must call this function at least once with
3431 an available font-driver.
3433 The second is with frame F NULL. In this case, DRIVER is globally
3434 registered in the variable `font_driver_list'. All font-driver
3435 implementations must call this function in its syms_of_XXXX
3436 (e.g. syms_of_xfont). */
3438 void
3439 register_font_driver (driver, f)
3440 struct font_driver *driver;
3441 FRAME_PTR f;
3443 struct font_driver_list *root = f ? f->font_driver_list : font_driver_list;
3444 struct font_driver_list *prev, *list;
3446 if (f && ! driver->draw)
3447 error ("Unusable font driver for a frame: %s",
3448 SDATA (SYMBOL_NAME (driver->type)));
3450 for (prev = NULL, list = root; list; prev = list, list = list->next)
3451 if (EQ (list->driver->type, driver->type))
3452 error ("Duplicated font driver: %s", SDATA (SYMBOL_NAME (driver->type)));
3454 list = malloc (sizeof (struct font_driver_list));
3455 list->on = 0;
3456 list->driver = driver;
3457 list->next = NULL;
3458 if (prev)
3459 prev->next = list;
3460 else if (f)
3461 f->font_driver_list = list;
3462 else
3463 font_driver_list = list;
3464 if (! f)
3465 num_font_drivers++;
3469 /* Make the frame F use font backends listed in NEW_DRIVERS (list of
3470 symbols, e.g. xft, x). If NEW_DRIVERS is t, make F use all
3471 available font drivers. If NEW_DRIVERS is nil, finalize all drivers.
3473 A caller must free all realized faces if any in advance. The
3474 return value is a list of font backends actually made used on
3475 F. */
3477 Lisp_Object
3478 font_update_drivers (f, new_drivers)
3479 FRAME_PTR f;
3480 Lisp_Object new_drivers;
3482 Lisp_Object active_drivers = Qnil;
3483 struct font_driver *driver;
3484 struct font_driver_list *list;
3486 /* At first, turn off non-requested drivers, and turn on requested
3487 drivers. */
3488 for (list = f->font_driver_list; list; list = list->next)
3490 driver = list->driver;
3491 if ((EQ (new_drivers, Qt) || ! NILP (Fmemq (driver->type, new_drivers)))
3492 != list->on)
3494 if (list->on)
3496 if (driver->end_for_frame)
3497 driver->end_for_frame (f);
3498 font_finish_cache (f, driver);
3499 list->on = 0;
3501 else
3503 if (! driver->start_for_frame
3504 || driver->start_for_frame (f) == 0)
3506 font_prepare_cache (f, driver);
3507 list->on = 1;
3513 if (NILP (new_drivers))
3514 return Qnil;
3516 if (! EQ (new_drivers, Qt))
3518 /* Re-order the driver list according to new_drivers. */
3519 struct font_driver_list **list_table, **next;
3520 Lisp_Object tail;
3521 int i;
3523 list_table = alloca (sizeof list_table[0] * (num_font_drivers + 1));
3524 for (i = 0, tail = new_drivers; ! NILP (tail); tail = XCDR (tail))
3526 for (list = f->font_driver_list; list; list = list->next)
3527 if (list->on && EQ (list->driver->type, XCAR (tail)))
3528 break;
3529 if (list)
3530 list_table[i++] = list;
3532 for (list = f->font_driver_list; list; list = list->next)
3533 if (! list->on)
3534 list_table[i] = list;
3535 list_table[i] = NULL;
3537 next = &f->font_driver_list;
3538 for (i = 0; list_table[i]; i++)
3540 *next = list_table[i];
3541 next = &(*next)->next;
3543 *next = NULL;
3546 for (list = f->font_driver_list; list; list = list->next)
3547 if (list->on)
3548 active_drivers = nconc2 (active_drivers,
3549 Fcons (list->driver->type, Qnil));
3550 return active_drivers;
3554 font_put_frame_data (f, driver, data)
3555 FRAME_PTR f;
3556 struct font_driver *driver;
3557 void *data;
3559 struct font_data_list *list, *prev;
3561 for (prev = NULL, list = f->font_data_list; list;
3562 prev = list, list = list->next)
3563 if (list->driver == driver)
3564 break;
3565 if (! data)
3567 if (list)
3569 if (prev)
3570 prev->next = list->next;
3571 else
3572 f->font_data_list = list->next;
3573 free (list);
3575 return 0;
3578 if (! list)
3580 list = malloc (sizeof (struct font_data_list));
3581 if (! list)
3582 return -1;
3583 list->driver = driver;
3584 list->next = f->font_data_list;
3585 f->font_data_list = list;
3587 list->data = data;
3588 return 0;
3592 void *
3593 font_get_frame_data (f, driver)
3594 FRAME_PTR f;
3595 struct font_driver *driver;
3597 struct font_data_list *list;
3599 for (list = f->font_data_list; list; list = list->next)
3600 if (list->driver == driver)
3601 break;
3602 if (! list)
3603 return NULL;
3604 return list->data;
3608 /* Return the font used to draw character C by FACE at buffer position
3609 POS in window W. If STRING is non-nil, it is a string containing C
3610 at index POS. If C is negative, get C from the current buffer or
3611 STRING. */
3613 Lisp_Object
3614 font_at (c, pos, face, w, string)
3615 int c;
3616 EMACS_INT pos;
3617 struct face *face;
3618 struct window *w;
3619 Lisp_Object string;
3621 FRAME_PTR f;
3622 int multibyte;
3623 Lisp_Object font_object;
3625 if (c < 0)
3627 if (NILP (string))
3629 multibyte = ! NILP (current_buffer->enable_multibyte_characters);
3630 if (multibyte)
3632 EMACS_INT pos_byte = CHAR_TO_BYTE (pos);
3634 c = FETCH_CHAR (pos_byte);
3636 else
3637 c = FETCH_BYTE (pos);
3639 else
3641 unsigned char *str;
3643 multibyte = STRING_MULTIBYTE (string);
3644 if (multibyte)
3646 EMACS_INT pos_byte = string_char_to_byte (string, pos);
3648 str = SDATA (string) + pos_byte;
3649 c = STRING_CHAR (str, 0);
3651 else
3652 c = SDATA (string)[pos];
3656 f = XFRAME (w->frame);
3657 if (! FRAME_WINDOW_P (f))
3658 return Qnil;
3659 if (! face)
3661 int face_id;
3662 EMACS_INT endptr;
3664 if (STRINGP (string))
3665 face_id = face_at_string_position (w, string, pos, 0, -1, -1, &endptr,
3666 DEFAULT_FACE_ID, 0);
3667 else
3668 face_id = face_at_buffer_position (w, pos, -1, -1, &endptr,
3669 pos + 100, 0);
3670 face = FACE_FROM_ID (f, face_id);
3672 if (multibyte)
3674 int face_id = FACE_FOR_CHAR (f, face, c, pos, string);
3675 face = FACE_FROM_ID (f, face_id);
3677 if (! face->font)
3678 return Qnil;
3680 XSETFONT (font_object, face->font);
3681 return font_object;
3685 #ifdef HAVE_WINDOW_SYSTEM
3687 /* Check how many characters after POS (at most to *LIMIT) can be
3688 displayed by the same font on the window W. FACE, if non-NULL, is
3689 the face selected for the character at POS. If STRING is not nil,
3690 it is the string to check instead of the current buffer. In that
3691 case, FACE must be not NULL.
3693 The return value is the font-object for the character at POS.
3694 *LIMIT is set to the position where that font can't be used.
3696 It is assured that the current buffer (or STRING) is multibyte. */
3698 Lisp_Object
3699 font_range (pos, limit, w, face, string)
3700 EMACS_INT pos, *limit;
3701 struct window *w;
3702 struct face *face;
3703 Lisp_Object string;
3705 EMACS_INT pos_byte, ignore, start, start_byte;
3706 int c;
3707 Lisp_Object font_object = Qnil;
3709 if (NILP (string))
3711 pos_byte = CHAR_TO_BYTE (pos);
3712 if (! face)
3714 int face_id;
3716 face_id = face_at_buffer_position (w, pos, 0, 0, &ignore, *limit, 0);
3717 face = FACE_FROM_ID (XFRAME (w->frame), face_id);
3720 else
3722 font_assert (face);
3723 pos_byte = string_char_to_byte (string, pos);
3726 start = pos, start_byte = pos_byte;
3727 while (pos < *limit)
3729 Lisp_Object category;
3731 if (NILP (string))
3732 FETCH_CHAR_ADVANCE_NO_CHECK (c, pos, pos_byte);
3733 else
3734 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, string, pos, pos_byte);
3735 if (NILP (font_object))
3737 font_object = font_for_char (face, c, pos - 1, string);
3738 if (NILP (font_object))
3739 return Qnil;
3740 continue;
3743 category = CHAR_TABLE_REF (Vunicode_category_table, c);
3744 if (! EQ (category, QCf)
3745 && font_encode_char (font_object, c) == FONT_INVALID_CODE)
3747 Lisp_Object f = font_for_char (face, c, pos - 1, string);
3748 EMACS_INT i, i_byte;
3751 if (NILP (f))
3753 *limit = pos - 1;
3754 return font_object;
3756 i = start, i_byte = start_byte;
3757 while (i < pos - 1)
3760 if (NILP (string))
3761 FETCH_CHAR_ADVANCE_NO_CHECK (c, i, i_byte);
3762 else
3763 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, string, i, i_byte);
3764 category = CHAR_TABLE_REF (Vunicode_category_table, c);
3765 if (! EQ (category, QCf)
3766 && font_encode_char (f, c) == FONT_INVALID_CODE)
3768 *limit = pos - 1;
3769 return font_object;
3772 font_object = f;
3775 return font_object;
3777 #endif
3780 /* Lisp API */
3782 DEFUN ("fontp", Ffontp, Sfontp, 1, 2, 0,
3783 doc: /* Return t if OBJECT is a font-spec, font-entity, or font-object.
3784 Return nil otherwise.
3785 Optional 2nd argument EXTRA-TYPE, if non-nil, specifies to check
3786 which kind of font it is. It must be one of `font-spec', `font-entity',
3787 `font-object'. */)
3788 (object, extra_type)
3789 Lisp_Object object, extra_type;
3791 if (NILP (extra_type))
3792 return (FONTP (object) ? Qt : Qnil);
3793 if (EQ (extra_type, Qfont_spec))
3794 return (FONT_SPEC_P (object) ? Qt : Qnil);
3795 if (EQ (extra_type, Qfont_entity))
3796 return (FONT_ENTITY_P (object) ? Qt : Qnil);
3797 if (EQ (extra_type, Qfont_object))
3798 return (FONT_OBJECT_P (object) ? Qt : Qnil);
3799 wrong_type_argument (intern ("font-extra-type"), extra_type);
3802 DEFUN ("font-spec", Ffont_spec, Sfont_spec, 0, MANY, 0,
3803 doc: /* Return a newly created font-spec with arguments as properties.
3805 ARGS must come in pairs KEY VALUE of font properties. KEY must be a
3806 valid font property name listed below:
3808 `:family', `:weight', `:slant', `:width'
3810 They are the same as face attributes of the same name. See
3811 `set-face-attribute'.
3813 `:foundry'
3815 VALUE must be a string or a symbol specifying the font foundry, e.g. ``misc''.
3817 `:adstyle'
3819 VALUE must be a string or a symbol specifying the additional
3820 typographic style information of a font, e.g. ``sans''.
3822 `:registry'
3824 VALUE must be a string or a symbol specifying the charset registry and
3825 encoding of a font, e.g. ``iso8859-1''.
3827 `:size'
3829 VALUE must be a non-negative integer or a floating point number
3830 specifying the font size. It specifies the font size in pixels
3831 (if VALUE is an integer), or in points (if VALUE is a float).
3833 `:name'
3835 VALUE must be a string of XLFD-style or fontconfig-style font name.
3836 usage: (font-spec ARGS ...)
3838 `:script'
3840 VALUE must be a symbol representing a script that the font must
3841 support. */)
3842 (nargs, args)
3843 int nargs;
3844 Lisp_Object *args;
3846 Lisp_Object spec = font_make_spec ();
3847 int i;
3849 for (i = 0; i < nargs; i += 2)
3851 Lisp_Object key = args[i], val = args[i + 1];
3853 if (EQ (key, QCname))
3855 CHECK_STRING (val);
3856 font_parse_name ((char *) SDATA (val), spec);
3857 font_put_extra (spec, key, val);
3859 else
3861 int idx = get_font_prop_index (key);
3863 if (idx >= 0)
3865 val = font_prop_validate (idx, Qnil, val);
3866 if (idx < FONT_EXTRA_INDEX)
3867 ASET (spec, idx, val);
3868 else
3869 font_put_extra (spec, key, val);
3871 else
3872 font_put_extra (spec, key, font_prop_validate (0, key, val));
3875 return spec;
3878 DEFUN ("copy-font-spec", Fcopy_font_spec, Scopy_font_spec, 1, 1, 0,
3879 doc: /* Return a copy of FONT as a font-spec. */)
3880 (font)
3881 Lisp_Object font;
3883 Lisp_Object new_spec, tail, prev, extra;
3884 int i;
3886 CHECK_FONT (font);
3887 new_spec = font_make_spec ();
3888 for (i = 1; i < FONT_EXTRA_INDEX; i++)
3889 ASET (new_spec, i, AREF (font, i));
3890 extra = Fcopy_sequence (AREF (font, FONT_EXTRA_INDEX));
3891 /* We must remove :font-entity property. */
3892 for (prev = Qnil, tail = extra; CONSP (tail); prev = tail, tail = XCDR (tail))
3893 if (EQ (XCAR (XCAR (tail)), QCfont_entity))
3895 if (NILP (prev))
3896 extra = XCDR (extra);
3897 else
3898 XSETCDR (prev, XCDR (tail));
3899 break;
3901 ASET (new_spec, FONT_EXTRA_INDEX, extra);
3902 return new_spec;
3905 DEFUN ("merge-font-spec", Fmerge_font_spec, Smerge_font_spec, 2, 2, 0,
3906 doc: /* Merge font-specs FROM and TO, and return a new font-spec.
3907 Every specified properties in FROM override the corresponding
3908 properties in TO. */)
3909 (from, to)
3910 Lisp_Object from, to;
3912 Lisp_Object extra, tail;
3913 int i;
3915 CHECK_FONT (from);
3916 CHECK_FONT (to);
3917 to = Fcopy_font_spec (to);
3918 for (i = 0; i < FONT_EXTRA_INDEX; i++)
3919 ASET (to, i, AREF (from, i));
3920 extra = AREF (to, FONT_EXTRA_INDEX);
3921 for (tail = AREF (from, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail))
3922 if (! EQ (XCAR (XCAR (tail)), Qfont_entity))
3924 Lisp_Object slot = assq_no_quit (XCAR (XCAR (tail)), extra);
3926 if (! NILP (slot))
3927 XSETCDR (slot, XCDR (XCAR (tail)));
3928 else
3929 extra = Fcons (Fcons (XCAR (XCAR (tail)), XCDR (XCAR (tail))), extra);
3931 ASET (to, FONT_EXTRA_INDEX, extra);
3932 return to;
3935 DEFUN ("font-get", Ffont_get, Sfont_get, 2, 2, 0,
3936 doc: /* Return the value of FONT's property KEY.
3937 FONT is a font-spec, a font-entity, or a font-object.
3938 KEY must be one of these symbols:
3939 :family, :weight, :slant, :width, :foundry, :adstyle, :registry,
3940 :size, :name, :script
3941 See the documentation of `font-spec' for their meanings.
3942 If FONT is a font-entity or font-object, the value of :script may be
3943 a list of scripts that are supported by the font. */)
3944 (font, key)
3945 Lisp_Object font, key;
3947 int idx;
3949 CHECK_FONT (font);
3950 CHECK_SYMBOL (key);
3952 idx = get_font_prop_index (key);
3953 if (idx >= FONT_WEIGHT_INDEX && idx <= FONT_WIDTH_INDEX)
3954 return font_style_symbolic (font, idx, 0);
3955 if (idx >= 0 && idx < FONT_EXTRA_INDEX)
3956 return AREF (font, idx);
3957 return Fcdr (Fassq (key, AREF (font, FONT_EXTRA_INDEX)));
3960 #ifdef HAVE_WINDOW_SYSTEM
3962 DEFUN ("font-face-attributes", Ffont_face_attributes, Sfont_face_attributes, 1, 2, 0,
3963 doc: /* Return a plist of face attributes generated by FONT.
3964 FONT is a font name, a font-spec, a font-entity, or a font-object.
3965 The return value is a list of the form
3967 \(:family FAMILY :height HEIGHT :weight WEIGHT :slant SLANT :width WIDTH)
3969 where FAMILY, HEIGHT, WEIGHT, SLANT, and WIDTH are face attribute values
3970 compatible with `set-face-attribute'. Some of these key-attribute pairs
3971 may be omitted from the list if they are not specified by FONT.
3973 The optional argument FRAME specifies the frame that the face attributes
3974 are to be displayed on. If omitted, the selected frame is used. */)
3975 (font, frame)
3976 Lisp_Object font, frame;
3978 struct frame *f;
3979 Lisp_Object plist[10];
3980 Lisp_Object val;
3981 int n = 0;
3983 if (NILP (frame))
3984 frame = selected_frame;
3985 CHECK_LIVE_FRAME (frame);
3986 f = XFRAME (frame);
3988 if (STRINGP (font))
3990 int fontset = fs_query_fontset (font, 0);
3991 Lisp_Object name = font;
3992 if (fontset >= 0)
3993 font = fontset_ascii (fontset);
3994 font = font_spec_from_name (name);
3995 if (! FONTP (font))
3996 signal_error ("Invalid font name", name);
3998 else if (! FONTP (font))
3999 signal_error ("Invalid font object", font);
4001 val = AREF (font, FONT_FAMILY_INDEX);
4002 if (! NILP (val))
4004 plist[n++] = QCfamily;
4005 plist[n++] = SYMBOL_NAME (val);
4008 val = AREF (font, FONT_SIZE_INDEX);
4009 if (INTEGERP (val))
4011 Lisp_Object font_dpi = AREF (font, FONT_DPI_INDEX);
4012 int dpi = INTEGERP (font_dpi) ? XINT (font_dpi) : f->resy;
4013 plist[n++] = QCheight;
4014 plist[n++] = make_number (PIXEL_TO_POINT (XINT (val) * 10, dpi));
4016 else if (FLOATP (val))
4018 plist[n++] = QCheight;
4019 plist[n++] = make_number (10 * (int) XFLOAT_DATA (val));
4022 val = FONT_WEIGHT_FOR_FACE (font);
4023 if (! NILP (val))
4025 plist[n++] = QCweight;
4026 plist[n++] = val;
4029 val = FONT_SLANT_FOR_FACE (font);
4030 if (! NILP (val))
4032 plist[n++] = QCslant;
4033 plist[n++] = val;
4036 val = FONT_WIDTH_FOR_FACE (font);
4037 if (! NILP (val))
4039 plist[n++] = QCwidth;
4040 plist[n++] = val;
4043 return Flist (n, plist);
4046 #endif
4048 DEFUN ("font-put", Ffont_put, Sfont_put, 3, 3, 0,
4049 doc: /* Set one property of FONT-SPEC: give property PROP value VAL. */)
4050 (font_spec, prop, val)
4051 Lisp_Object font_spec, prop, val;
4053 int idx;
4055 CHECK_FONT_SPEC (font_spec);
4056 idx = get_font_prop_index (prop);
4057 if (idx >= 0 && idx < FONT_EXTRA_INDEX)
4058 ASET (font_spec, idx, font_prop_validate (idx, Qnil, val));
4059 else
4060 font_put_extra (font_spec, prop, font_prop_validate (0, prop, val));
4061 return val;
4064 DEFUN ("list-fonts", Flist_fonts, Slist_fonts, 1, 4, 0,
4065 doc: /* List available fonts matching FONT-SPEC on the current frame.
4066 Optional 2nd argument FRAME specifies the target frame.
4067 Optional 3rd argument NUM, if non-nil, limits the number of returned fonts.
4068 Optional 4th argument PREFER, if non-nil, is a font-spec to
4069 control the order of the returned list. Fonts are sorted by
4070 how close they are to PREFER. */)
4071 (font_spec, frame, num, prefer)
4072 Lisp_Object font_spec, frame, num, prefer;
4074 Lisp_Object vec, list, tail;
4075 int n = 0, i, len;
4077 if (NILP (frame))
4078 frame = selected_frame;
4079 CHECK_LIVE_FRAME (frame);
4080 CHECK_FONT_SPEC (font_spec);
4081 if (! NILP (num))
4083 CHECK_NUMBER (num);
4084 n = XINT (num);
4085 if (n <= 0)
4086 return Qnil;
4088 if (! NILP (prefer))
4089 CHECK_FONT_SPEC (prefer);
4091 vec = font_list_entities (frame, font_spec);
4092 len = ASIZE (vec);
4093 if (len == 0)
4094 return Qnil;
4095 if (len == 1)
4096 return Fcons (AREF (vec, 0), Qnil);
4098 if (! NILP (prefer))
4099 vec = font_sort_entites (vec, prefer, frame, 0);
4101 list = tail = Fcons (AREF (vec, 0), Qnil);
4102 if (n == 0 || n > len)
4103 n = len;
4104 for (i = 1; i < n; i++)
4106 Lisp_Object val = Fcons (AREF (vec, i), Qnil);
4108 XSETCDR (tail, val);
4109 tail = val;
4111 return list;
4114 DEFUN ("font-family-list", Ffont_family_list, Sfont_family_list, 0, 1, 0,
4115 doc: /* List available font families on the current frame.
4116 Optional argument FRAME, if non-nil, specifies the target frame. */)
4117 (frame)
4118 Lisp_Object frame;
4120 FRAME_PTR f;
4121 struct font_driver_list *driver_list;
4122 Lisp_Object list;
4124 if (NILP (frame))
4125 frame = selected_frame;
4126 CHECK_LIVE_FRAME (frame);
4127 f = XFRAME (frame);
4128 list = Qnil;
4129 for (driver_list = f->font_driver_list; driver_list;
4130 driver_list = driver_list->next)
4131 if (driver_list->driver->list_family)
4133 Lisp_Object val = driver_list->driver->list_family (frame);
4135 if (NILP (list))
4136 list = val;
4137 else
4139 Lisp_Object tail = list;
4141 for (; CONSP (val); val = XCDR (val))
4142 if (NILP (Fmemq (XCAR (val), tail)))
4143 list = Fcons (XCAR (val), list);
4146 return list;
4149 DEFUN ("find-font", Ffind_font, Sfind_font, 1, 2, 0,
4150 doc: /* Return a font-entity matching with FONT-SPEC on the current frame.
4151 Optional 2nd argument FRAME, if non-nil, specifies the target frame. */)
4152 (font_spec, frame)
4153 Lisp_Object font_spec, frame;
4155 Lisp_Object val = Flist_fonts (font_spec, frame, make_number (1), Qnil);
4157 if (CONSP (val))
4158 val = XCAR (val);
4159 return val;
4162 DEFUN ("font-xlfd-name", Ffont_xlfd_name, Sfont_xlfd_name, 1, 2, 0,
4163 doc: /* Return XLFD name of FONT.
4164 FONT is a font-spec, font-entity, or font-object.
4165 If the name is too long for XLFD (maximum 255 chars), return nil.
4166 If the 2nd optional arg FOLD-WILDCARDS is non-nil,
4167 the consecutive wildcards are folded to one. */)
4168 (font, fold_wildcards)
4169 Lisp_Object font, fold_wildcards;
4171 char name[256];
4172 int pixel_size = 0;
4174 CHECK_FONT (font);
4176 if (FONT_OBJECT_P (font))
4178 Lisp_Object font_name = AREF (font, FONT_NAME_INDEX);
4180 if (STRINGP (font_name)
4181 && SDATA (font_name)[0] == '-')
4183 if (NILP (fold_wildcards))
4184 return font_name;
4185 strcpy (name, (char *) SDATA (font_name));
4186 goto done;
4188 pixel_size = XFONT_OBJECT (font)->pixel_size;
4190 if (font_unparse_xlfd (font, pixel_size, name, 256) < 0)
4191 return Qnil;
4192 done:
4193 if (! NILP (fold_wildcards))
4195 char *p0 = name, *p1;
4197 while ((p1 = strstr (p0, "-*-*")))
4199 strcpy (p1, p1 + 2);
4200 p0 = p1;
4204 return build_string (name);
4207 DEFUN ("clear-font-cache", Fclear_font_cache, Sclear_font_cache, 0, 0, 0,
4208 doc: /* Clear font cache. */)
4211 Lisp_Object list, frame;
4213 FOR_EACH_FRAME (list, frame)
4215 FRAME_PTR f = XFRAME (frame);
4216 struct font_driver_list *driver_list = f->font_driver_list;
4218 for (; driver_list; driver_list = driver_list->next)
4219 if (driver_list->on)
4221 Lisp_Object cache = driver_list->driver->get_cache (f);
4222 Lisp_Object val;
4224 val = XCDR (cache);
4225 while (! NILP (val)
4226 && ! EQ (XCAR (XCAR (val)), driver_list->driver->type))
4227 val = XCDR (val);
4228 font_assert (! NILP (val));
4229 val = XCDR (XCAR (val));
4230 if (XINT (XCAR (val)) == 0)
4232 font_clear_cache (f, XCAR (val), driver_list->driver);
4233 XSETCDR (cache, XCDR (val));
4238 return Qnil;
4242 void
4243 font_fill_lglyph_metrics (glyph, font_object)
4244 Lisp_Object glyph, font_object;
4246 struct font *font = XFONT_OBJECT (font_object);
4247 unsigned code;
4248 /* ecode used in LGLYPH_SET_CODE to avoid compiler warnings. */
4249 EMACS_INT ecode = font->driver->encode_char (font, LGLYPH_CHAR (glyph));
4250 struct font_metrics metrics;
4252 LGLYPH_SET_CODE (glyph, ecode);
4253 code = ecode;
4254 font->driver->text_extents (font, &code, 1, &metrics);
4255 LGLYPH_SET_LBEARING (glyph, metrics.lbearing);
4256 LGLYPH_SET_RBEARING (glyph, metrics.rbearing);
4257 LGLYPH_SET_WIDTH (glyph, metrics.width);
4258 LGLYPH_SET_ASCENT (glyph, metrics.ascent);
4259 LGLYPH_SET_DESCENT (glyph, metrics.descent);
4263 DEFUN ("font-shape-gstring", Ffont_shape_gstring, Sfont_shape_gstring, 1, 1, 0,
4264 doc: /* Shape the glyph-string GSTRING.
4265 Shaping means substituting glyphs and/or adjusting positions of glyphs
4266 to get the correct visual image of character sequences set in the
4267 header of the glyph-string.
4269 If the shaping was successful, the value is GSTRING itself or a newly
4270 created glyph-string. Otherwise, the value is nil. */)
4271 (gstring)
4272 Lisp_Object gstring;
4274 struct font *font;
4275 Lisp_Object font_object, n, glyph;
4276 int i, j, from, to;
4278 if (! composition_gstring_p (gstring))
4279 signal_error ("Invalid glyph-string: ", gstring);
4280 if (! NILP (LGSTRING_ID (gstring)))
4281 return gstring;
4282 font_object = LGSTRING_FONT (gstring);
4283 CHECK_FONT_OBJECT (font_object);
4284 font = XFONT_OBJECT (font_object);
4285 if (! font->driver->shape)
4286 return Qnil;
4288 /* Try at most three times with larger gstring each time. */
4289 for (i = 0; i < 3; i++)
4291 n = font->driver->shape (gstring);
4292 if (INTEGERP (n))
4293 break;
4294 gstring = larger_vector (gstring,
4295 ASIZE (gstring) + LGSTRING_GLYPH_LEN (gstring),
4296 Qnil);
4298 if (i == 3 || XINT (n) == 0)
4299 return Qnil;
4301 glyph = LGSTRING_GLYPH (gstring, 0);
4302 from = LGLYPH_FROM (glyph);
4303 to = LGLYPH_TO (glyph);
4304 for (i = 1, j = 0; i < LGSTRING_GLYPH_LEN (gstring); i++)
4306 Lisp_Object this = LGSTRING_GLYPH (gstring, i);
4308 if (NILP (this))
4309 break;
4310 if (NILP (LGLYPH_ADJUSTMENT (this)))
4312 if (j < i - 1)
4313 for (; j < i; j++)
4315 glyph = LGSTRING_GLYPH (gstring, j);
4316 LGLYPH_SET_FROM (glyph, from);
4317 LGLYPH_SET_TO (glyph, to);
4319 from = LGLYPH_FROM (this);
4320 to = LGLYPH_TO (this);
4321 j = i;
4323 else
4325 if (from > LGLYPH_FROM (this))
4326 from = LGLYPH_FROM (this);
4327 if (to < LGLYPH_TO (this))
4328 to = LGLYPH_TO (this);
4331 if (j < i - 1)
4332 for (; j < i; j++)
4334 glyph = LGSTRING_GLYPH (gstring, j);
4335 LGLYPH_SET_FROM (glyph, from);
4336 LGLYPH_SET_TO (glyph, to);
4338 return composition_gstring_put_cache (gstring, XINT (n));
4341 #if 0
4343 DEFUN ("font-drive-otf", Ffont_drive_otf, Sfont_drive_otf, 6, 6, 0,
4344 doc: /* Apply OpenType features on glyph-string GSTRING-IN.
4345 OTF-FEATURES specifies which features to apply in this format:
4346 (SCRIPT LANGSYS GSUB GPOS)
4347 where
4348 SCRIPT is a symbol specifying a script tag of OpenType,
4349 LANGSYS is a symbol specifying a langsys tag of OpenType,
4350 GSUB and GPOS, if non-nil, are lists of symbols specifying feature tags.
4352 If LANGYS is nil, the default langsys is selected.
4354 The features are applied in the order they appear in the list. The
4355 symbol `*' means to apply all available features not present in this
4356 list, and the remaining features are ignored. For instance, (vatu
4357 pstf * haln) is to apply vatu and pstf in this order, then to apply
4358 all available features other than vatu, pstf, and haln.
4360 The features are applied to the glyphs in the range FROM and TO of
4361 the glyph-string GSTRING-IN.
4363 If some feature is actually applicable, the resulting glyphs are
4364 produced in the glyph-string GSTRING-OUT from the index INDEX. In
4365 this case, the value is the number of produced glyphs.
4367 If no feature is applicable, no glyph is produced in GSTRING-OUT, and
4368 the value is 0.
4370 If GSTRING-OUT is too short to hold produced glyphs, no glyphs are
4371 produced in GSTRING-OUT, and the value is nil.
4373 See the documentation of `font-make-gstring' for the format of
4374 glyph-string. */)
4375 (otf_features, gstring_in, from, to, gstring_out, index)
4376 Lisp_Object otf_features, gstring_in, from, to, gstring_out, index;
4378 Lisp_Object font_object = LGSTRING_FONT (gstring_in);
4379 Lisp_Object val;
4380 struct font *font;
4381 int len, num;
4383 check_otf_features (otf_features);
4384 CHECK_FONT_OBJECT (font_object);
4385 font = XFONT_OBJECT (font_object);
4386 if (! font->driver->otf_drive)
4387 error ("Font backend %s can't drive OpenType GSUB table",
4388 SDATA (SYMBOL_NAME (font->driver->type)));
4389 CHECK_CONS (otf_features);
4390 CHECK_SYMBOL (XCAR (otf_features));
4391 val = XCDR (otf_features);
4392 CHECK_SYMBOL (XCAR (val));
4393 val = XCDR (otf_features);
4394 if (! NILP (val))
4395 CHECK_CONS (val);
4396 len = check_gstring (gstring_in);
4397 CHECK_VECTOR (gstring_out);
4398 CHECK_NATNUM (from);
4399 CHECK_NATNUM (to);
4400 CHECK_NATNUM (index);
4402 if (XINT (from) >= XINT (to) || XINT (to) > len)
4403 args_out_of_range_3 (from, to, make_number (len));
4404 if (XINT (index) >= ASIZE (gstring_out))
4405 args_out_of_range (index, make_number (ASIZE (gstring_out)));
4406 num = font->driver->otf_drive (font, otf_features,
4407 gstring_in, XINT (from), XINT (to),
4408 gstring_out, XINT (index), 0);
4409 if (num < 0)
4410 return Qnil;
4411 return make_number (num);
4414 DEFUN ("font-otf-alternates", Ffont_otf_alternates, Sfont_otf_alternates,
4415 3, 3, 0,
4416 doc: /* Return a list of alternate glyphs of CHARACTER in FONT-OBJECT.
4417 OTF-FEATURES specifies which features of the font FONT-OBJECT to apply
4418 in this format:
4419 (SCRIPT LANGSYS FEATURE ...)
4420 See the documentation of `font-drive-otf' for more detail.
4422 The value is a list of cons cells of the format (GLYPH-ID . CHARACTER),
4423 where GLYPH-ID is a glyph index of the font, and CHARACTER is a
4424 character code corresponding to the glyph or nil if there's no
4425 corresponding character. */)
4426 (font_object, character, otf_features)
4427 Lisp_Object font_object, character, otf_features;
4429 struct font *font;
4430 Lisp_Object gstring_in, gstring_out, g;
4431 Lisp_Object alternates;
4432 int i, num;
4434 CHECK_FONT_GET_OBJECT (font_object, font);
4435 if (! font->driver->otf_drive)
4436 error ("Font backend %s can't drive OpenType GSUB table",
4437 SDATA (SYMBOL_NAME (font->driver->type)));
4438 CHECK_CHARACTER (character);
4439 CHECK_CONS (otf_features);
4441 gstring_in = Ffont_make_gstring (font_object, make_number (1));
4442 g = LGSTRING_GLYPH (gstring_in, 0);
4443 LGLYPH_SET_CHAR (g, XINT (character));
4444 gstring_out = Ffont_make_gstring (font_object, make_number (10));
4445 while ((num = font->driver->otf_drive (font, otf_features, gstring_in, 0, 1,
4446 gstring_out, 0, 1)) < 0)
4447 gstring_out = Ffont_make_gstring (font_object,
4448 make_number (ASIZE (gstring_out) * 2));
4449 alternates = Qnil;
4450 for (i = 0; i < num; i++)
4452 Lisp_Object g = LGSTRING_GLYPH (gstring_out, i);
4453 int c = LGLYPH_CHAR (g);
4454 unsigned code = LGLYPH_CODE (g);
4456 alternates = Fcons (Fcons (make_number (code),
4457 c > 0 ? make_number (c) : Qnil),
4458 alternates);
4460 return Fnreverse (alternates);
4462 #endif /* 0 */
4464 #ifdef FONT_DEBUG
4466 DEFUN ("open-font", Fopen_font, Sopen_font, 1, 3, 0,
4467 doc: /* Open FONT-ENTITY. */)
4468 (font_entity, size, frame)
4469 Lisp_Object font_entity;
4470 Lisp_Object size;
4471 Lisp_Object frame;
4473 int isize;
4475 CHECK_FONT_ENTITY (font_entity);
4476 if (NILP (frame))
4477 frame = selected_frame;
4478 CHECK_LIVE_FRAME (frame);
4480 if (NILP (size))
4481 isize = XINT (AREF (font_entity, FONT_SIZE_INDEX));
4482 else
4484 CHECK_NUMBER_OR_FLOAT (size);
4485 if (FLOATP (size))
4486 isize = POINT_TO_PIXEL (- isize, XFRAME (frame)->resy);
4487 else
4488 isize = XINT (size);
4489 if (isize == 0)
4490 isize = 120;
4492 return font_open_entity (XFRAME (frame), font_entity, isize);
4495 DEFUN ("close-font", Fclose_font, Sclose_font, 1, 2, 0,
4496 doc: /* Close FONT-OBJECT. */)
4497 (font_object, frame)
4498 Lisp_Object font_object, frame;
4500 CHECK_FONT_OBJECT (font_object);
4501 if (NILP (frame))
4502 frame = selected_frame;
4503 CHECK_LIVE_FRAME (frame);
4504 font_close_object (XFRAME (frame), font_object);
4505 return Qnil;
4508 DEFUN ("query-font", Fquery_font, Squery_font, 1, 1, 0,
4509 doc: /* Return information about FONT-OBJECT.
4510 The value is a vector:
4511 [ NAME FILENAME PIXEL-SIZE SIZE ASCENT DESCENT SPACE-WIDTH AVERAGE-WIDTH
4512 CAPABILITY ]
4514 NAME is a string of the font name (or nil if the font backend doesn't
4515 provide a name).
4517 FILENAME is a string of the font file (or nil if the font backend
4518 doesn't provide a file name).
4520 PIXEL-SIZE is a pixel size by which the font is opened.
4522 SIZE is a maximum advance width of the font in pixels.
4524 ASCENT, DESCENT, SPACE-WIDTH, AVERAGE-WIDTH are metrics of the font in
4525 pixels.
4527 CAPABILITY is a list whose first element is a symbol representing the
4528 font format \(x, opentype, truetype, type1, pcf, or bdf) and the
4529 remaining elements describe the details of the font capability.
4531 If the font is OpenType font, the form of the list is
4532 \(opentype GSUB GPOS)
4533 where GSUB shows which "GSUB" features the font supports, and GPOS
4534 shows which "GPOS" features the font supports. Both GSUB and GPOS are
4535 lists of the format:
4536 \((SCRIPT (LANGSYS FEATURE ...) ...) ...)
4538 If the font is not OpenType font, currently the length of the form is
4539 one.
4541 SCRIPT is a symbol representing OpenType script tag.
4543 LANGSYS is a symbol representing OpenType langsys tag, or nil
4544 representing the default langsys.
4546 FEATURE is a symbol representing OpenType feature tag.
4548 If the font is not OpenType font, CAPABILITY is nil. */)
4549 (font_object)
4550 Lisp_Object font_object;
4552 struct font *font;
4553 Lisp_Object val;
4555 CHECK_FONT_GET_OBJECT (font_object, font);
4557 val = Fmake_vector (make_number (9), Qnil);
4558 ASET (val, 0, AREF (font_object, FONT_NAME_INDEX));
4559 ASET (val, 1, AREF (font_object, FONT_FILE_INDEX));
4560 ASET (val, 2, make_number (font->pixel_size));
4561 ASET (val, 3, make_number (font->max_width));
4562 ASET (val, 4, make_number (font->ascent));
4563 ASET (val, 5, make_number (font->descent));
4564 ASET (val, 6, make_number (font->space_width));
4565 ASET (val, 7, make_number (font->average_width));
4566 if (font->driver->otf_capability)
4567 ASET (val, 8, Fcons (Qopentype, font->driver->otf_capability (font)));
4568 return val;
4571 DEFUN ("get-font-glyphs", Fget_font_glyphs, Sget_font_glyphs, 2, 2, 0,
4572 doc: /* Return a vector of glyphs of FONT-OBJECT for drawing STRING.
4573 Each element is a vector [GLYPH-CODE LBEARING RBEARING WIDTH ASCENT DESCENT]. */)
4574 (font_object, string)
4575 Lisp_Object font_object, string;
4577 struct font *font;
4578 int i, len;
4579 Lisp_Object vec;
4581 CHECK_FONT_GET_OBJECT (font_object, font);
4582 CHECK_STRING (string);
4583 len = SCHARS (string);
4584 vec = Fmake_vector (make_number (len), Qnil);
4585 for (i = 0; i < len; i++)
4587 Lisp_Object ch = Faref (string, make_number (i));
4588 Lisp_Object val;
4589 int c = XINT (ch);
4590 unsigned code;
4591 EMACS_INT cod;
4592 struct font_metrics metrics;
4594 cod = code = font->driver->encode_char (font, c);
4595 if (code == FONT_INVALID_CODE)
4596 continue;
4597 val = Fmake_vector (make_number (6), Qnil);
4598 if (cod <= MOST_POSITIVE_FIXNUM)
4599 ASET (val, 0, make_number (code));
4600 else
4601 ASET (val, 0, Fcons (make_number (code >> 16),
4602 make_number (code & 0xFFFF)));
4603 font->driver->text_extents (font, &code, 1, &metrics);
4604 ASET (val, 1, make_number (metrics.lbearing));
4605 ASET (val, 2, make_number (metrics.rbearing));
4606 ASET (val, 3, make_number (metrics.width));
4607 ASET (val, 4, make_number (metrics.ascent));
4608 ASET (val, 5, make_number (metrics.descent));
4609 ASET (vec, i, val);
4611 return vec;
4614 DEFUN ("font-match-p", Ffont_match_p, Sfont_match_p, 2, 2, 0,
4615 doc: /* Return t if and only if font-spec SPEC matches with FONT.
4616 FONT is a font-spec, font-entity, or font-object. */)
4617 (spec, font)
4618 Lisp_Object spec, font;
4620 CHECK_FONT_SPEC (spec);
4621 CHECK_FONT (font);
4623 return (font_match_p (spec, font) ? Qt : Qnil);
4626 DEFUN ("font-at", Ffont_at, Sfont_at, 1, 3, 0,
4627 doc: /* Return a font-object for displaying a character at POSITION.
4628 Optional second arg WINDOW, if non-nil, is a window displaying
4629 the current buffer. It defaults to the currently selected window. */)
4630 (position, window, string)
4631 Lisp_Object position, window, string;
4633 struct window *w;
4634 EMACS_INT pos;
4636 if (NILP (string))
4638 CHECK_NUMBER_COERCE_MARKER (position);
4639 pos = XINT (position);
4640 if (pos < BEGV || pos >= ZV)
4641 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
4643 else
4645 CHECK_NUMBER (position);
4646 CHECK_STRING (string);
4647 pos = XINT (position);
4648 if (pos < 0 || pos >= SCHARS (string))
4649 args_out_of_range (string, position);
4651 if (NILP (window))
4652 window = selected_window;
4653 CHECK_LIVE_WINDOW (window);
4654 w = XWINDOW (window);
4656 return font_at (-1, pos, NULL, w, string);
4659 #if 0
4660 DEFUN ("draw-string", Fdraw_string, Sdraw_string, 2, 2, 0,
4661 doc: /* Draw STRING by FONT-OBJECT on the top left corner of the current frame.
4662 The value is a number of glyphs drawn.
4663 Type C-l to recover what previously shown. */)
4664 (font_object, string)
4665 Lisp_Object font_object, string;
4667 Lisp_Object frame = selected_frame;
4668 FRAME_PTR f = XFRAME (frame);
4669 struct font *font;
4670 struct face *face;
4671 int i, len, width;
4672 unsigned *code;
4674 CHECK_FONT_GET_OBJECT (font_object, font);
4675 CHECK_STRING (string);
4676 len = SCHARS (string);
4677 code = alloca (sizeof (unsigned) * len);
4678 for (i = 0; i < len; i++)
4680 Lisp_Object ch = Faref (string, make_number (i));
4681 Lisp_Object val;
4682 int c = XINT (ch);
4684 code[i] = font->driver->encode_char (font, c);
4685 if (code[i] == FONT_INVALID_CODE)
4686 break;
4688 face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4689 face->fontp = font;
4690 if (font->driver->prepare_face)
4691 font->driver->prepare_face (f, face);
4692 width = font->driver->text_extents (font, code, i, NULL);
4693 len = font->driver->draw_text (f, face, 0, font->ascent, code, i, width);
4694 if (font->driver->done_face)
4695 font->driver->done_face (f, face);
4696 face->fontp = NULL;
4697 return make_number (len);
4699 #endif
4701 #endif /* FONT_DEBUG */
4703 #ifdef HAVE_WINDOW_SYSTEM
4705 DEFUN ("font-info", Ffont_info, Sfont_info, 1, 2, 0,
4706 doc: /* Return information about a font named NAME on frame FRAME.
4707 If FRAME is omitted or nil, use the selected frame.
4708 The returned value is a vector of OPENED-NAME, FULL-NAME, CHARSET, SIZE,
4709 HEIGHT, BASELINE-OFFSET, RELATIVE-COMPOSE, and DEFAULT-ASCENT,
4710 where
4711 OPENED-NAME is the name used for opening the font,
4712 FULL-NAME is the full name of the font,
4713 SIZE is the maximum bound width of the font,
4714 HEIGHT is the height of the font,
4715 BASELINE-OFFSET is the upward offset pixels from ASCII baseline,
4716 RELATIVE-COMPOSE and DEFAULT-ASCENT are the numbers controlling
4717 how to compose characters.
4718 If the named font is not yet loaded, return nil. */)
4719 (name, frame)
4720 Lisp_Object name, frame;
4722 FRAME_PTR f;
4723 struct font *font;
4724 Lisp_Object info;
4725 Lisp_Object font_object;
4727 (*check_window_system_func) ();
4729 if (! FONTP (name))
4730 CHECK_STRING (name);
4731 if (NILP (frame))
4732 frame = selected_frame;
4733 CHECK_LIVE_FRAME (frame);
4734 f = XFRAME (frame);
4736 if (STRINGP (name))
4738 int fontset = fs_query_fontset (name, 0);
4740 if (fontset >= 0)
4741 name = fontset_ascii (fontset);
4742 font_object = font_open_by_name (f, (char *) SDATA (name));
4744 else if (FONT_OBJECT_P (name))
4745 font_object = name;
4746 else if (FONT_ENTITY_P (name))
4747 font_object = font_open_entity (f, name, 0);
4748 else
4750 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4751 Lisp_Object entity = font_matching_entity (f, face->lface, name);
4753 font_object = ! NILP (entity) ? font_open_entity (f, entity, 0) : Qnil;
4755 if (NILP (font_object))
4756 return Qnil;
4757 font = XFONT_OBJECT (font_object);
4759 info = Fmake_vector (make_number (7), Qnil);
4760 XVECTOR (info)->contents[0] = AREF (font_object, FONT_NAME_INDEX);
4761 XVECTOR (info)->contents[1] = AREF (font_object, FONT_NAME_INDEX);
4762 XVECTOR (info)->contents[2] = make_number (font->pixel_size);
4763 XVECTOR (info)->contents[3] = make_number (font->height);
4764 XVECTOR (info)->contents[4] = make_number (font->baseline_offset);
4765 XVECTOR (info)->contents[5] = make_number (font->relative_compose);
4766 XVECTOR (info)->contents[6] = make_number (font->default_ascent);
4768 #if 0
4769 /* As font_object is still in FONT_OBJLIST of the entity, we can't
4770 close it now. Perhaps, we should manage font-objects
4771 by `reference-count'. */
4772 font_close_object (f, font_object);
4773 #endif
4774 return info;
4776 #endif
4779 #define BUILD_STYLE_TABLE(TBL) \
4780 build_style_table ((TBL), sizeof TBL / sizeof (struct table_entry))
4782 static Lisp_Object
4783 build_style_table (entry, nelement)
4784 struct table_entry *entry;
4785 int nelement;
4787 int i, j;
4788 Lisp_Object table, elt;
4790 table = Fmake_vector (make_number (nelement), Qnil);
4791 for (i = 0; i < nelement; i++)
4793 for (j = 0; entry[i].names[j]; j++);
4794 elt = Fmake_vector (make_number (j + 1), Qnil);
4795 ASET (elt, 0, make_number (entry[i].numeric));
4796 for (j = 0; entry[i].names[j]; j++)
4797 ASET (elt, j + 1, intern (entry[i].names[j]));
4798 ASET (table, i, elt);
4800 return table;
4803 static Lisp_Object Vfont_log;
4804 static int font_log_env_checked;
4806 /* The deferred font-log data of the form [ACTION ARG RESULT].
4807 If ACTION is not nil, that is added to the log when font_add_log is
4808 called next time. At that time, ACTION is set back to nil. */
4809 static Lisp_Object Vfont_log_deferred;
4811 /* Prepend the font-related logging data in Vfont_log if it is not
4812 `t'. ACTION describes a kind of font-related action (e.g. listing,
4813 opening), ARG is the argument for the action, and RESULT is the
4814 result of the action. */
4815 void
4816 font_add_log (action, arg, result)
4817 char *action;
4818 Lisp_Object arg, result;
4820 Lisp_Object tail, val;
4821 int i;
4823 if (! font_log_env_checked)
4825 Vfont_log = egetenv ("EMACS_FONT_LOG") ? Qnil : Qt;
4826 font_log_env_checked = 1;
4828 if (EQ (Vfont_log, Qt))
4829 return;
4830 if (STRINGP (AREF (Vfont_log_deferred, 0)))
4832 char *str = (char *) SDATA (AREF (Vfont_log_deferred, 0));
4834 ASET (Vfont_log_deferred, 0, Qnil);
4835 font_add_log (str, AREF (Vfont_log_deferred, 1),
4836 AREF (Vfont_log_deferred, 2));
4839 if (FONTP (arg))
4841 Lisp_Object tail, elt;
4842 Lisp_Object equalstr = build_string ("=");
4844 val = Ffont_xlfd_name (arg, Qt);
4845 for (tail = AREF (arg, FONT_EXTRA_INDEX); CONSP (tail);
4846 tail = XCDR (tail))
4848 elt = XCAR (tail);
4849 if (EQ (XCAR (elt), QCscript)
4850 && SYMBOLP (XCDR (elt)))
4851 val = concat3 (val, SYMBOL_NAME (QCscript),
4852 concat2 (equalstr, SYMBOL_NAME (XCDR (elt))));
4853 else if (EQ (XCAR (elt), QClang)
4854 && SYMBOLP (XCDR (elt)))
4855 val = concat3 (val, SYMBOL_NAME (QClang),
4856 concat2 (equalstr, SYMBOL_NAME (XCDR (elt))));
4857 else if (EQ (XCAR (elt), QCotf)
4858 && CONSP (XCDR (elt)) && SYMBOLP (XCAR (XCDR (elt))))
4859 val = concat3 (val, SYMBOL_NAME (QCotf),
4860 concat2 (equalstr,
4861 SYMBOL_NAME (XCAR (XCDR (elt)))));
4863 arg = val;
4865 if (FONTP (result))
4867 val = Ffont_xlfd_name (result, Qt);
4868 if (! FONT_SPEC_P (result))
4869 val = concat3 (SYMBOL_NAME (AREF (result, FONT_TYPE_INDEX)),
4870 build_string (":"), val);
4871 result = val;
4873 else if (CONSP (result))
4875 result = Fcopy_sequence (result);
4876 for (tail = result; CONSP (tail); tail = XCDR (tail))
4878 val = XCAR (tail);
4879 if (FONTP (val))
4880 val = Ffont_xlfd_name (val, Qt);
4881 XSETCAR (tail, val);
4884 else if (VECTORP (result))
4886 result = Fcopy_sequence (result);
4887 for (i = 0; i < ASIZE (result); i++)
4889 val = AREF (result, i);
4890 if (FONTP (val))
4891 val = Ffont_xlfd_name (val, Qt);
4892 ASET (result, i, val);
4895 Vfont_log = Fcons (list3 (intern (action), arg, result), Vfont_log);
4898 /* Record a font-related logging data to be added to Vfont_log when
4899 font_add_log is called next time. ACTION, ARG, RESULT are the same
4900 as font_add_log. */
4902 void
4903 font_deferred_log (action, arg, result)
4904 char *action;
4905 Lisp_Object arg, result;
4907 ASET (Vfont_log_deferred, 0, build_string (action));
4908 ASET (Vfont_log_deferred, 1, arg);
4909 ASET (Vfont_log_deferred, 2, result);
4912 extern void syms_of_ftfont P_ (());
4913 extern void syms_of_xfont P_ (());
4914 extern void syms_of_xftfont P_ (());
4915 extern void syms_of_ftxfont P_ (());
4916 extern void syms_of_bdffont P_ (());
4917 extern void syms_of_w32font P_ (());
4918 extern void syms_of_atmfont P_ (());
4919 extern void syms_of_nsfont P_ (());
4921 void
4922 syms_of_font ()
4924 sort_shift_bits[FONT_TYPE_INDEX] = 0;
4925 sort_shift_bits[FONT_SLANT_INDEX] = 2;
4926 sort_shift_bits[FONT_WEIGHT_INDEX] = 9;
4927 sort_shift_bits[FONT_SIZE_INDEX] = 16;
4928 sort_shift_bits[FONT_WIDTH_INDEX] = 23;
4929 /* Note that the other elements in sort_shift_bits are not used. */
4931 staticpro (&font_charset_alist);
4932 font_charset_alist = Qnil;
4934 DEFSYM (Qfont_spec, "font-spec");
4935 DEFSYM (Qfont_entity, "font-entity");
4936 DEFSYM (Qfont_object, "font-object");
4938 DEFSYM (Qopentype, "opentype");
4940 DEFSYM (Qascii_0, "ascii-0");
4941 DEFSYM (Qiso8859_1, "iso8859-1");
4942 DEFSYM (Qiso10646_1, "iso10646-1");
4943 DEFSYM (Qunicode_bmp, "unicode-bmp");
4944 DEFSYM (Qunicode_sip, "unicode-sip");
4946 DEFSYM (QCf, "Cf");
4948 DEFSYM (QCotf, ":otf");
4949 DEFSYM (QClang, ":lang");
4950 DEFSYM (QCscript, ":script");
4951 DEFSYM (QCantialias, ":antialias");
4953 DEFSYM (QCfoundry, ":foundry");
4954 DEFSYM (QCadstyle, ":adstyle");
4955 DEFSYM (QCregistry, ":registry");
4956 DEFSYM (QCspacing, ":spacing");
4957 DEFSYM (QCdpi, ":dpi");
4958 DEFSYM (QCscalable, ":scalable");
4959 DEFSYM (QCavgwidth, ":avgwidth");
4960 DEFSYM (QCfont_entity, ":font-entity");
4961 DEFSYM (QCfc_unknown_spec, ":fc-unknown-spec");
4963 DEFSYM (Qc, "c");
4964 DEFSYM (Qm, "m");
4965 DEFSYM (Qp, "p");
4966 DEFSYM (Qd, "d");
4968 staticpro (&null_vector);
4969 null_vector = Fmake_vector (make_number (0), Qnil);
4971 staticpro (&scratch_font_spec);
4972 scratch_font_spec = Ffont_spec (0, NULL);
4973 staticpro (&scratch_font_prefer);
4974 scratch_font_prefer = Ffont_spec (0, NULL);
4976 staticpro (&Vfont_log_deferred);
4977 Vfont_log_deferred = Fmake_vector (make_number (3), Qnil);
4979 #if 0
4980 #ifdef HAVE_LIBOTF
4981 staticpro (&otf_list);
4982 otf_list = Qnil;
4983 #endif /* HAVE_LIBOTF */
4984 #endif /* 0 */
4986 defsubr (&Sfontp);
4987 defsubr (&Sfont_spec);
4988 defsubr (&Sfont_get);
4989 #ifdef HAVE_WINDOW_SYSTEM
4990 defsubr (&Sfont_face_attributes);
4991 #endif
4992 defsubr (&Sfont_put);
4993 defsubr (&Slist_fonts);
4994 defsubr (&Sfont_family_list);
4995 defsubr (&Sfind_font);
4996 defsubr (&Sfont_xlfd_name);
4997 defsubr (&Sclear_font_cache);
4998 defsubr (&Sfont_shape_gstring);
4999 #if 0
5000 defsubr (&Sfont_drive_otf);
5001 defsubr (&Sfont_otf_alternates);
5002 #endif /* 0 */
5004 #ifdef FONT_DEBUG
5005 defsubr (&Sopen_font);
5006 defsubr (&Sclose_font);
5007 defsubr (&Squery_font);
5008 defsubr (&Sget_font_glyphs);
5009 defsubr (&Sfont_match_p);
5010 defsubr (&Sfont_at);
5011 #if 0
5012 defsubr (&Sdraw_string);
5013 #endif
5014 #endif /* FONT_DEBUG */
5015 #ifdef HAVE_WINDOW_SYSTEM
5016 defsubr (&Sfont_info);
5017 #endif
5019 DEFVAR_LISP ("font-encoding-alist", &Vfont_encoding_alist,
5020 doc: /*
5021 Alist of fontname patterns vs the corresponding encoding and repertory info.
5022 Each element looks like (REGEXP . (ENCODING . REPERTORY)),
5023 where ENCODING is a charset or a char-table,
5024 and REPERTORY is a charset, a char-table, or nil.
5026 If ENCODING and REPERTORY are the same, the element can have the form
5027 \(REGEXP . ENCODING).
5029 ENCODING is for converting a character to a glyph code of the font.
5030 If ENCODING is a charset, encoding a character by the charset gives
5031 the corresponding glyph code. If ENCODING is a char-table, looking up
5032 the table by a character gives the corresponding glyph code.
5034 REPERTORY specifies a repertory of characters supported by the font.
5035 If REPERTORY is a charset, all characters beloging to the charset are
5036 supported. If REPERTORY is a char-table, all characters who have a
5037 non-nil value in the table are supported. If REPERTORY is nil, Emacs
5038 gets the repertory information by an opened font and ENCODING. */);
5039 Vfont_encoding_alist = Qnil;
5041 DEFVAR_LISP_NOPRO ("font-weight-table", &Vfont_weight_table,
5042 doc: /* Vector of valid font weight values.
5043 Each element has the form:
5044 [NUMERIC-VALUE SYMBOLIC-NAME ALIAS-NAME ...]
5045 NUMERIC-VALUE is an integer, and SYMBOLIC-NAME and ALIAS-NAME are symbols. */);
5046 Vfont_weight_table = BUILD_STYLE_TABLE (weight_table);
5048 DEFVAR_LISP_NOPRO ("font-slant-table", &Vfont_slant_table,
5049 doc: /* Vector of font slant symbols vs the corresponding numeric values.
5050 See `font-weight-table' for the format of the vector. */);
5051 Vfont_slant_table = BUILD_STYLE_TABLE (slant_table);
5053 DEFVAR_LISP_NOPRO ("font-width-table", &Vfont_width_table,
5054 doc: /* Alist of font width symbols vs the corresponding numeric values.
5055 See `font-weight-table' for the format of the vector. */);
5056 Vfont_width_table = BUILD_STYLE_TABLE (width_table);
5058 staticpro (&font_style_table);
5059 font_style_table = Fmake_vector (make_number (3), Qnil);
5060 ASET (font_style_table, 0, Vfont_weight_table);
5061 ASET (font_style_table, 1, Vfont_slant_table);
5062 ASET (font_style_table, 2, Vfont_width_table);
5064 DEFVAR_LISP ("font-log", &Vfont_log, doc: /*
5065 *Logging list of font related actions and results.
5066 The value t means to suppress the logging.
5067 The initial value is set to nil if the environment variable
5068 EMACS_FONT_LOG is set. Otherwise, it is set to t. */);
5069 Vfont_log = Qnil;
5071 #ifdef HAVE_WINDOW_SYSTEM
5072 #ifdef HAVE_FREETYPE
5073 syms_of_ftfont ();
5074 #ifdef HAVE_X_WINDOWS
5075 syms_of_xfont ();
5076 syms_of_ftxfont ();
5077 #ifdef HAVE_XFT
5078 syms_of_xftfont ();
5079 #endif /* HAVE_XFT */
5080 #endif /* HAVE_X_WINDOWS */
5081 #else /* not HAVE_FREETYPE */
5082 #ifdef HAVE_X_WINDOWS
5083 syms_of_xfont ();
5084 #endif /* HAVE_X_WINDOWS */
5085 #endif /* not HAVE_FREETYPE */
5086 #ifdef HAVE_BDFFONT
5087 syms_of_bdffont ();
5088 #endif /* HAVE_BDFFONT */
5089 #ifdef WINDOWSNT
5090 syms_of_w32font ();
5091 #endif /* WINDOWSNT */
5092 #ifdef HAVE_NS
5093 syms_of_nsfont ();
5094 #endif /* HAVE_NS */
5095 #endif /* HAVE_WINDOW_SYSTEM */
5098 /* arch-tag: 74c9475d-5976-4c93-a327-942ae3072846
5099 (do not change this comment) */