Use gnulib's mktime module.
[bpt/emacs.git] / src / font.c
1 /* font.c -- "Font" primitives.
2
3 Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 Copyright (C) 2006, 2007, 2008, 2009, 2010
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H13PRO009
7
8 This file is part of GNU Emacs.
9
10 GNU Emacs is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 GNU Emacs is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22
23 #include <config.h>
24 #include <stdio.h>
25 #include <ctype.h>
26 #include <setjmp.h>
27
28 #include "lisp.h"
29 #include "buffer.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "dispextern.h"
33 #include "charset.h"
34 #include "character.h"
35 #include "composite.h"
36 #include "fontset.h"
37 #include "font.h"
38
39 #ifdef HAVE_X_WINDOWS
40 #include "xterm.h"
41 #endif /* HAVE_X_WINDOWS */
42
43 #ifdef HAVE_NTGUI
44 #include "w32term.h"
45 #endif /* HAVE_NTGUI */
46
47 #ifdef HAVE_NS
48 #include "nsterm.h"
49 #endif /* HAVE_NS */
50
51 Lisp_Object Qopentype;
52
53 /* Important character set strings. */
54 Lisp_Object Qascii_0, Qiso8859_1, Qiso10646_1, Qunicode_bmp, Qunicode_sip;
55
56 #define DEFAULT_ENCODING Qiso8859_1
57
58 /* Unicode category `Cf'. */
59 static Lisp_Object QCf;
60
61 /* Special vector of zero length. This is repeatedly used by (struct
62 font_driver *)->list when a specified font is not found. */
63 static Lisp_Object null_vector;
64
65 static Lisp_Object Vfont_weight_table, Vfont_slant_table, Vfont_width_table;
66
67 /* Vector of Vfont_weight_table, Vfont_slant_table, and Vfont_width_table. */
68 static Lisp_Object font_style_table;
69
70 /* Structure used for tables mapping weight, slant, and width numeric
71 values and their names. */
72
73 struct table_entry
74 {
75 int numeric;
76 /* The first one is a valid name as a face attribute.
77 The second one (if any) is a typical name in XLFD field. */
78 const char *names[5];
79 };
80
81 /* Table of weight numeric values and their names. This table must be
82 sorted by numeric values in ascending order. */
83
84 static const struct table_entry weight_table[] =
85 {
86 { 0, { "thin" }},
87 { 20, { "ultra-light", "ultralight" }},
88 { 40, { "extra-light", "extralight" }},
89 { 50, { "light" }},
90 { 75, { "semi-light", "semilight", "demilight", "book" }},
91 { 100, { "normal", "medium", "regular", "unspecified" }},
92 { 180, { "semi-bold", "semibold", "demibold", "demi" }},
93 { 200, { "bold" }},
94 { 205, { "extra-bold", "extrabold" }},
95 { 210, { "ultra-bold", "ultrabold", "black" }}
96 };
97
98 /* Table of slant numeric values and their names. This table must be
99 sorted by numeric values in ascending order. */
100
101 static const struct table_entry slant_table[] =
102 {
103 { 0, { "reverse-oblique", "ro" }},
104 { 10, { "reverse-italic", "ri" }},
105 { 100, { "normal", "r", "unspecified" }},
106 { 200, { "italic" ,"i", "ot" }},
107 { 210, { "oblique", "o" }}
108 };
109
110 /* Table of width numeric values and their names. This table must be
111 sorted by numeric values in ascending order. */
112
113 static const struct table_entry width_table[] =
114 {
115 { 50, { "ultra-condensed", "ultracondensed" }},
116 { 63, { "extra-condensed", "extracondensed" }},
117 { 75, { "condensed", "compressed", "narrow" }},
118 { 87, { "semi-condensed", "semicondensed", "demicondensed" }},
119 { 100, { "normal", "medium", "regular", "unspecified" }},
120 { 113, { "semi-expanded", "semiexpanded", "demiexpanded" }},
121 { 125, { "expanded" }},
122 { 150, { "extra-expanded", "extraexpanded" }},
123 { 200, { "ultra-expanded", "ultraexpanded", "wide" }}
124 };
125
126 Lisp_Object QCfoundry;
127 static Lisp_Object QCadstyle, QCregistry;
128 /* Symbols representing keys of font extra info. */
129 Lisp_Object QCspacing, QCdpi, QCscalable, QCotf, QClang, QCscript, QCavgwidth;
130 Lisp_Object QCantialias, QCfont_entity, QCfc_unknown_spec;
131 /* Symbols representing values of font spacing property. */
132 Lisp_Object Qc, Qm, Qp, Qd;
133 /* Special ADSTYLE properties to avoid fonts used for Latin
134 characters; used in xfont.c and ftfont.c. */
135 Lisp_Object Qja, Qko;
136
137 Lisp_Object QCuser_spec;
138
139 Lisp_Object Vfont_encoding_alist;
140
141 /* Alist of font registry symbol and the corresponding charsets
142 information. The information is retrieved from
143 Vfont_encoding_alist on demand.
144
145 Eash element has the form:
146 (REGISTRY . (ENCODING-CHARSET-ID . REPERTORY-CHARSET-ID))
147 or
148 (REGISTRY . nil)
149
150 In the former form, ENCODING-CHARSET-ID is an ID of a charset that
151 encodes a character code to a glyph code of a font, and
152 REPERTORY-CHARSET-ID is an ID of a charset that tells if a
153 character is supported by a font.
154
155 The latter form means that the information for REGISTRY couldn't be
156 retrieved. */
157 static Lisp_Object font_charset_alist;
158
159 /* List of all font drivers. Each font-backend (XXXfont.c) calls
160 register_font_driver in syms_of_XXXfont to register its font-driver
161 here. */
162 static struct font_driver_list *font_driver_list;
163
164 \f
165
166 /* Creaters of font-related Lisp object. */
167
168 static Lisp_Object
169 font_make_spec (void)
170 {
171 Lisp_Object font_spec;
172 struct font_spec *spec
173 = ((struct font_spec *)
174 allocate_pseudovector (VECSIZE (struct font_spec),
175 FONT_SPEC_MAX, PVEC_FONT));
176 XSETFONT (font_spec, spec);
177 return font_spec;
178 }
179
180 Lisp_Object
181 font_make_entity (void)
182 {
183 Lisp_Object font_entity;
184 struct font_entity *entity
185 = ((struct font_entity *)
186 allocate_pseudovector (VECSIZE (struct font_entity),
187 FONT_ENTITY_MAX, PVEC_FONT));
188 XSETFONT (font_entity, entity);
189 return font_entity;
190 }
191
192 /* Create a font-object whose structure size is SIZE. If ENTITY is
193 not nil, copy properties from ENTITY to the font-object. If
194 PIXELSIZE is positive, set the `size' property to PIXELSIZE. */
195 Lisp_Object
196 font_make_object (int size, Lisp_Object entity, int pixelsize)
197 {
198 Lisp_Object font_object;
199 struct font *font
200 = (struct font *) allocate_pseudovector (size, FONT_OBJECT_MAX, PVEC_FONT);
201 int i;
202
203 XSETFONT (font_object, font);
204
205 if (! NILP (entity))
206 {
207 for (i = 1; i < FONT_SPEC_MAX; i++)
208 font->props[i] = AREF (entity, i);
209 if (! NILP (AREF (entity, FONT_EXTRA_INDEX)))
210 font->props[FONT_EXTRA_INDEX]
211 = Fcopy_alist (AREF (entity, FONT_EXTRA_INDEX));
212 }
213 if (size > 0)
214 font->props[FONT_SIZE_INDEX] = make_number (pixelsize);
215 return font_object;
216 }
217
218 \f
219
220 static int font_pixel_size (FRAME_PTR f, Lisp_Object);
221 static Lisp_Object font_open_entity (FRAME_PTR, Lisp_Object, int);
222 static Lisp_Object font_matching_entity (FRAME_PTR, Lisp_Object *,
223 Lisp_Object);
224 static unsigned font_encode_char (Lisp_Object, int);
225
226 /* Number of registered font drivers. */
227 static int num_font_drivers;
228
229
230 /* Return a Lispy value of a font property value at STR and LEN bytes.
231 If STR is "*", it returns nil.
232 If FORCE_SYMBOL is zero and all characters in STR are digits, it
233 returns an integer. Otherwise, it returns a symbol interned from
234 STR. */
235
236 Lisp_Object
237 font_intern_prop (const char *str, int len, int force_symbol)
238 {
239 int i;
240 Lisp_Object tem;
241 Lisp_Object obarray;
242 EMACS_INT nbytes, nchars;
243
244 if (len == 1 && *str == '*')
245 return Qnil;
246 if (!force_symbol && len >=1 && isdigit (*str))
247 {
248 for (i = 1; i < len; i++)
249 if (! isdigit (str[i]))
250 break;
251 if (i == len)
252 return make_number (atoi (str));
253 }
254
255 /* The following code is copied from the function intern (in
256 lread.c), and modified to suite our purpose. */
257 obarray = Vobarray;
258 if (!VECTORP (obarray) || XVECTOR (obarray)->size == 0)
259 obarray = check_obarray (obarray);
260 parse_str_as_multibyte ((unsigned char *) str, len, &nchars, &nbytes);
261 if (len == nchars || len != nbytes)
262 /* CONTENTS contains no multibyte sequences or contains an invalid
263 multibyte sequence. We'll make a unibyte string. */
264 tem = oblookup (obarray, str, len, len);
265 else
266 tem = oblookup (obarray, str, nchars, len);
267 if (SYMBOLP (tem))
268 return tem;
269 if (len == nchars || len != nbytes)
270 tem = make_unibyte_string (str, len);
271 else
272 tem = make_multibyte_string (str, nchars, len);
273 return Fintern (tem, obarray);
274 }
275
276 /* Return a pixel size of font-spec SPEC on frame F. */
277
278 static int
279 font_pixel_size (FRAME_PTR f, Lisp_Object spec)
280 {
281 #ifdef HAVE_WINDOW_SYSTEM
282 Lisp_Object size = AREF (spec, FONT_SIZE_INDEX);
283 double point_size;
284 int dpi, pixel_size;
285 Lisp_Object val;
286
287 if (INTEGERP (size))
288 return XINT (size);
289 if (NILP (size))
290 return 0;
291 font_assert (FLOATP (size));
292 point_size = XFLOAT_DATA (size);
293 val = AREF (spec, FONT_DPI_INDEX);
294 if (INTEGERP (val))
295 dpi = XINT (val);
296 else
297 dpi = f->resy;
298 pixel_size = POINT_TO_PIXEL (point_size, dpi);
299 return pixel_size;
300 #else
301 return 1;
302 #endif
303 }
304
305
306 /* Return a value of PROP's VAL (symbol or integer) to be stored in a
307 font vector. If VAL is not valid (i.e. not registered in
308 font_style_table), return -1 if NOERROR is zero, and return a
309 proper index if NOERROR is nonzero. In that case, register VAL in
310 font_style_table if VAL is a symbol, and return a closest index if
311 VAL is an integer. */
312
313 int
314 font_style_to_value (enum font_property_index prop, Lisp_Object val, int noerror)
315 {
316 Lisp_Object table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
317 int len = ASIZE (table);
318 int i, j;
319
320 if (SYMBOLP (val))
321 {
322 unsigned char *s;
323 Lisp_Object args[2], elt;
324
325 /* At first try exact match. */
326 for (i = 0; i < len; i++)
327 for (j = 1; j < ASIZE (AREF (table, i)); j++)
328 if (EQ (val, AREF (AREF (table, i), j)))
329 return ((XINT (AREF (AREF (table, i), 0)) << 8)
330 | (i << 4) | (j - 1));
331 /* Try also with case-folding match. */
332 s = SDATA (SYMBOL_NAME (val));
333 for (i = 0; i < len; i++)
334 for (j = 1; j < ASIZE (AREF (table, i)); j++)
335 {
336 elt = AREF (AREF (table, i), j);
337 if (xstrcasecmp (s, SDATA (SYMBOL_NAME (elt))) == 0)
338 return ((XINT (AREF (AREF (table, i), 0)) << 8)
339 | (i << 4) | (j - 1));
340 }
341 if (! noerror)
342 return -1;
343 if (len == 255)
344 abort ();
345 elt = Fmake_vector (make_number (2), make_number (100));
346 ASET (elt, 1, val);
347 args[0] = table;
348 args[1] = Fmake_vector (make_number (1), elt);
349 ASET (font_style_table, prop - FONT_WEIGHT_INDEX, Fvconcat (2, args));
350 return (100 << 8) | (i << 4);
351 }
352 else
353 {
354 int i, last_n;
355 int numeric = XINT (val);
356
357 for (i = 0, last_n = -1; i < len; i++)
358 {
359 int n = XINT (AREF (AREF (table, i), 0));
360
361 if (numeric == n)
362 return (n << 8) | (i << 4);
363 if (numeric < n)
364 {
365 if (! noerror)
366 return -1;
367 return ((i == 0 || n - numeric < numeric - last_n)
368 ? (n << 8) | (i << 4): (last_n << 8 | ((i - 1) << 4)));
369 }
370 last_n = n;
371 }
372 if (! noerror)
373 return -1;
374 return ((last_n << 8) | ((i - 1) << 4));
375 }
376 }
377
378 Lisp_Object
379 font_style_symbolic (Lisp_Object font, enum font_property_index prop, int for_face)
380 {
381 Lisp_Object val = AREF (font, prop);
382 Lisp_Object table, elt;
383 int i;
384
385 if (NILP (val))
386 return Qnil;
387 table = AREF (font_style_table, prop - FONT_WEIGHT_INDEX);
388 i = XINT (val) & 0xFF;
389 font_assert (((i >> 4) & 0xF) < ASIZE (table));
390 elt = AREF (table, ((i >> 4) & 0xF));
391 font_assert ((i & 0xF) + 1 < ASIZE (elt));
392 return (for_face ? AREF (elt, 1) : AREF (elt, (i & 0xF) + 1));
393 }
394
395 /* Return ENCODING or a cons of ENCODING and REPERTORY of the font
396 FONTNAME. ENCODING is a charset symbol that specifies the encoding
397 of the font. REPERTORY is a charset symbol or nil. */
398
399 Lisp_Object
400 find_font_encoding (Lisp_Object fontname)
401 {
402 Lisp_Object tail, elt;
403
404 for (tail = Vfont_encoding_alist; CONSP (tail); tail = XCDR (tail))
405 {
406 elt = XCAR (tail);
407 if (CONSP (elt)
408 && STRINGP (XCAR (elt))
409 && fast_string_match_ignore_case (XCAR (elt), fontname) >= 0
410 && (SYMBOLP (XCDR (elt))
411 ? CHARSETP (XCDR (elt))
412 : CONSP (XCDR (elt)) && CHARSETP (XCAR (XCDR (elt)))))
413 return (XCDR (elt));
414 }
415 return Qnil;
416 }
417
418 /* Return encoding charset and repertory charset for REGISTRY in
419 ENCODING and REPERTORY correspondingly. If correct information for
420 REGISTRY is available, return 0. Otherwise return -1. */
421
422 int
423 font_registry_charsets (Lisp_Object registry, struct charset **encoding, struct charset **repertory)
424 {
425 Lisp_Object val;
426 int encoding_id, repertory_id;
427
428 val = Fassoc_string (registry, font_charset_alist, Qt);
429 if (! NILP (val))
430 {
431 val = XCDR (val);
432 if (NILP (val))
433 return -1;
434 encoding_id = XINT (XCAR (val));
435 repertory_id = XINT (XCDR (val));
436 }
437 else
438 {
439 val = find_font_encoding (SYMBOL_NAME (registry));
440 if (SYMBOLP (val) && CHARSETP (val))
441 {
442 encoding_id = repertory_id = XINT (CHARSET_SYMBOL_ID (val));
443 }
444 else if (CONSP (val))
445 {
446 if (! CHARSETP (XCAR (val)))
447 goto invalid_entry;
448 encoding_id = XINT (CHARSET_SYMBOL_ID (XCAR (val)));
449 if (NILP (XCDR (val)))
450 repertory_id = -1;
451 else
452 {
453 if (! CHARSETP (XCDR (val)))
454 goto invalid_entry;
455 repertory_id = XINT (CHARSET_SYMBOL_ID (XCDR (val)));
456 }
457 }
458 else
459 goto invalid_entry;
460 val = Fcons (make_number (encoding_id), make_number (repertory_id));
461 font_charset_alist
462 = nconc2 (font_charset_alist, Fcons (Fcons (registry, val), Qnil));
463 }
464
465 if (encoding)
466 *encoding = CHARSET_FROM_ID (encoding_id);
467 if (repertory)
468 *repertory = repertory_id >= 0 ? CHARSET_FROM_ID (repertory_id) : NULL;
469 return 0;
470
471 invalid_entry:
472 font_charset_alist
473 = nconc2 (font_charset_alist, Fcons (Fcons (registry, Qnil), Qnil));
474 return -1;
475 }
476
477 \f
478 /* Font property value validaters. See the comment of
479 font_property_table for the meaning of the arguments. */
480
481 static Lisp_Object font_prop_validate (int, Lisp_Object, Lisp_Object);
482 static Lisp_Object font_prop_validate_symbol (Lisp_Object, Lisp_Object);
483 static Lisp_Object font_prop_validate_style (Lisp_Object, Lisp_Object);
484 static Lisp_Object font_prop_validate_non_neg (Lisp_Object, Lisp_Object);
485 static Lisp_Object font_prop_validate_spacing (Lisp_Object, Lisp_Object);
486 static int get_font_prop_index (Lisp_Object);
487
488 static Lisp_Object
489 font_prop_validate_symbol (Lisp_Object prop, Lisp_Object val)
490 {
491 if (STRINGP (val))
492 val = Fintern (val, Qnil);
493 if (! SYMBOLP (val))
494 val = Qerror;
495 else if (EQ (prop, QCregistry))
496 val = Fintern (Fdowncase (SYMBOL_NAME (val)), Qnil);
497 return val;
498 }
499
500
501 static Lisp_Object
502 font_prop_validate_style (Lisp_Object style, Lisp_Object val)
503 {
504 enum font_property_index prop = (EQ (style, QCweight) ? FONT_WEIGHT_INDEX
505 : EQ (style, QCslant) ? FONT_SLANT_INDEX
506 : FONT_WIDTH_INDEX);
507 int n;
508 if (INTEGERP (val))
509 {
510 n = XINT (val);
511 if (((n >> 4) & 0xF)
512 >= ASIZE (AREF (font_style_table, prop - FONT_WEIGHT_INDEX)))
513 val = Qerror;
514 else
515 {
516 Lisp_Object elt = AREF (AREF (font_style_table, prop - FONT_WEIGHT_INDEX), (n >> 4) & 0xF);
517
518 if ((n & 0xF) + 1 >= ASIZE (elt))
519 val = Qerror;
520 else if (XINT (AREF (elt, 0)) != (n >> 8))
521 val = Qerror;
522 }
523 }
524 else if (SYMBOLP (val))
525 {
526 int n = font_style_to_value (prop, val, 0);
527
528 val = n >= 0 ? make_number (n) : Qerror;
529 }
530 else
531 val = Qerror;
532 return val;
533 }
534
535 static Lisp_Object
536 font_prop_validate_non_neg (Lisp_Object prop, Lisp_Object val)
537 {
538 return (NATNUMP (val) || (FLOATP (val) && XFLOAT_DATA (val) >= 0)
539 ? val : Qerror);
540 }
541
542 static Lisp_Object
543 font_prop_validate_spacing (Lisp_Object prop, Lisp_Object val)
544 {
545 if (NILP (val) || (NATNUMP (val) && XINT (val) <= FONT_SPACING_CHARCELL))
546 return val;
547 if (SYMBOLP (val) && SBYTES (SYMBOL_NAME (val)) == 1)
548 {
549 char spacing = SDATA (SYMBOL_NAME (val))[0];
550
551 if (spacing == 'c' || spacing == 'C')
552 return make_number (FONT_SPACING_CHARCELL);
553 if (spacing == 'm' || spacing == 'M')
554 return make_number (FONT_SPACING_MONO);
555 if (spacing == 'p' || spacing == 'P')
556 return make_number (FONT_SPACING_PROPORTIONAL);
557 if (spacing == 'd' || spacing == 'D')
558 return make_number (FONT_SPACING_DUAL);
559 }
560 return Qerror;
561 }
562
563 static Lisp_Object
564 font_prop_validate_otf (Lisp_Object prop, Lisp_Object val)
565 {
566 Lisp_Object tail, tmp;
567 int i;
568
569 /* VAL = (SCRIPT [ LANGSYS [ GSUB-FEATURES [ GPOS-FEATURES ]]])
570 GSUB-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil
571 GPOS-FEATURES = (FEATURE ... [ nil FEATURE ... ]) | nil */
572 if (! CONSP (val))
573 return Qerror;
574 if (! SYMBOLP (XCAR (val)))
575 return Qerror;
576 tail = XCDR (val);
577 if (NILP (tail))
578 return val;
579 if (! CONSP (tail) || ! SYMBOLP (XCAR (val)))
580 return Qerror;
581 for (i = 0; i < 2; i++)
582 {
583 tail = XCDR (tail);
584 if (NILP (tail))
585 return val;
586 if (! CONSP (tail))
587 return Qerror;
588 for (tmp = XCAR (tail); CONSP (tmp); tmp = XCDR (tmp))
589 if (! SYMBOLP (XCAR (tmp)))
590 return Qerror;
591 if (! NILP (tmp))
592 return Qerror;
593 }
594 return val;
595 }
596
597 /* Structure of known font property keys and validater of the
598 values. */
599 static const struct
600 {
601 /* Pointer to the key symbol. */
602 Lisp_Object *key;
603 /* Function to validate PROP's value VAL, or NULL if any value is
604 ok. The value is VAL or its regularized value if VAL is valid,
605 and Qerror if not. */
606 Lisp_Object (*validater) (Lisp_Object prop, Lisp_Object val);
607 } font_property_table[] =
608 { { &QCtype, font_prop_validate_symbol },
609 { &QCfoundry, font_prop_validate_symbol },
610 { &QCfamily, font_prop_validate_symbol },
611 { &QCadstyle, font_prop_validate_symbol },
612 { &QCregistry, font_prop_validate_symbol },
613 { &QCweight, font_prop_validate_style },
614 { &QCslant, font_prop_validate_style },
615 { &QCwidth, font_prop_validate_style },
616 { &QCsize, font_prop_validate_non_neg },
617 { &QCdpi, font_prop_validate_non_neg },
618 { &QCspacing, font_prop_validate_spacing },
619 { &QCavgwidth, font_prop_validate_non_neg },
620 /* The order of the above entries must match with enum
621 font_property_index. */
622 { &QClang, font_prop_validate_symbol },
623 { &QCscript, font_prop_validate_symbol },
624 { &QCotf, font_prop_validate_otf }
625 };
626
627 /* Size (number of elements) of the above table. */
628 #define FONT_PROPERTY_TABLE_SIZE \
629 ((sizeof font_property_table) / (sizeof *font_property_table))
630
631 /* Return an index number of font property KEY or -1 if KEY is not an
632 already known property. */
633
634 static int
635 get_font_prop_index (Lisp_Object key)
636 {
637 int i;
638
639 for (i = 0; i < FONT_PROPERTY_TABLE_SIZE; i++)
640 if (EQ (key, *font_property_table[i].key))
641 return i;
642 return -1;
643 }
644
645 /* Validate the font property. The property key is specified by the
646 symbol PROP, or the index IDX (if PROP is nil). If VAL is invalid,
647 signal an error. The value is VAL or the regularized one. */
648
649 static Lisp_Object
650 font_prop_validate (int idx, Lisp_Object prop, Lisp_Object val)
651 {
652 Lisp_Object validated;
653
654 if (NILP (val))
655 return val;
656 if (NILP (prop))
657 prop = *font_property_table[idx].key;
658 else
659 {
660 idx = get_font_prop_index (prop);
661 if (idx < 0)
662 return val;
663 }
664 validated = (font_property_table[idx].validater) (prop, val);
665 if (EQ (validated, Qerror))
666 signal_error ("invalid font property", Fcons (prop, val));
667 return validated;
668 }
669
670
671 /* Store VAL as a value of extra font property PROP in FONT while
672 keeping the sorting order. Don't check the validity of VAL. */
673
674 Lisp_Object
675 font_put_extra (Lisp_Object font, Lisp_Object prop, Lisp_Object val)
676 {
677 Lisp_Object extra = AREF (font, FONT_EXTRA_INDEX);
678 Lisp_Object slot = (NILP (extra) ? Qnil : assq_no_quit (prop, extra));
679
680 if (NILP (slot))
681 {
682 Lisp_Object prev = Qnil;
683
684 while (CONSP (extra)
685 && NILP (Fstring_lessp (prop, XCAR (XCAR (extra)))))
686 prev = extra, extra = XCDR (extra);
687
688 if (NILP (prev))
689 ASET (font, FONT_EXTRA_INDEX, Fcons (Fcons (prop, val), extra));
690 else
691 XSETCDR (prev, Fcons (Fcons (prop, val), extra));
692
693 return val;
694 }
695 XSETCDR (slot, val);
696 if (NILP (val))
697 ASET (font, FONT_EXTRA_INDEX, Fdelq (slot, extra));
698 return val;
699 }
700
701 \f
702 /* Font name parser and unparser */
703
704 static int parse_matrix (const char *);
705 static int font_expand_wildcards (Lisp_Object *, int);
706 static int font_parse_name (char *, Lisp_Object);
707
708 /* An enumerator for each field of an XLFD font name. */
709 enum xlfd_field_index
710 {
711 XLFD_FOUNDRY_INDEX,
712 XLFD_FAMILY_INDEX,
713 XLFD_WEIGHT_INDEX,
714 XLFD_SLANT_INDEX,
715 XLFD_SWIDTH_INDEX,
716 XLFD_ADSTYLE_INDEX,
717 XLFD_PIXEL_INDEX,
718 XLFD_POINT_INDEX,
719 XLFD_RESX_INDEX,
720 XLFD_RESY_INDEX,
721 XLFD_SPACING_INDEX,
722 XLFD_AVGWIDTH_INDEX,
723 XLFD_REGISTRY_INDEX,
724 XLFD_ENCODING_INDEX,
725 XLFD_LAST_INDEX
726 };
727
728 /* An enumerator for mask bit corresponding to each XLFD field. */
729 enum xlfd_field_mask
730 {
731 XLFD_FOUNDRY_MASK = 0x0001,
732 XLFD_FAMILY_MASK = 0x0002,
733 XLFD_WEIGHT_MASK = 0x0004,
734 XLFD_SLANT_MASK = 0x0008,
735 XLFD_SWIDTH_MASK = 0x0010,
736 XLFD_ADSTYLE_MASK = 0x0020,
737 XLFD_PIXEL_MASK = 0x0040,
738 XLFD_POINT_MASK = 0x0080,
739 XLFD_RESX_MASK = 0x0100,
740 XLFD_RESY_MASK = 0x0200,
741 XLFD_SPACING_MASK = 0x0400,
742 XLFD_AVGWIDTH_MASK = 0x0800,
743 XLFD_REGISTRY_MASK = 0x1000,
744 XLFD_ENCODING_MASK = 0x2000
745 };
746
747
748 /* Parse P pointing the pixel/point size field of the form
749 `[A B C D]' which specifies a transformation matrix:
750
751 A B 0
752 C D 0
753 0 0 1
754
755 by which all glyphs of the font are transformed. The spec says
756 that scalar value N for the pixel/point size is equivalent to:
757 A = N * resx/resy, B = C = 0, D = N.
758
759 Return the scalar value N if the form is valid. Otherwise return
760 -1. */
761
762 static int
763 parse_matrix (const char *p)
764 {
765 double matrix[4];
766 char *end;
767 int i;
768
769 for (i = 0, p++; i < 4 && *p && *p != ']'; i++)
770 {
771 if (*p == '~')
772 matrix[i] = - strtod (p + 1, &end);
773 else
774 matrix[i] = strtod (p, &end);
775 p = end;
776 }
777 return (i == 4 ? (int) matrix[3] : -1);
778 }
779
780 /* Expand a wildcard field in FIELD (the first N fields are filled) to
781 multiple fields to fill in all 14 XLFD fields while restring a
782 field position by its contents. */
783
784 static int
785 font_expand_wildcards (Lisp_Object *field, int n)
786 {
787 /* Copy of FIELD. */
788 Lisp_Object tmp[XLFD_LAST_INDEX];
789 /* Array of information about where this element can go. Nth
790 element is for Nth element of FIELD. */
791 struct {
792 /* Minimum possible field. */
793 int from;
794 /* Maxinum possible field. */
795 int to;
796 /* Bit mask of possible field. Nth bit corresponds to Nth field. */
797 int mask;
798 } range[XLFD_LAST_INDEX];
799 int i, j;
800 int range_from, range_to;
801 unsigned range_mask;
802
803 #define XLFD_SYMBOL_MASK (XLFD_FOUNDRY_MASK | XLFD_FAMILY_MASK \
804 | XLFD_ADSTYLE_MASK | XLFD_REGISTRY_MASK)
805 #define XLFD_NULL_MASK (XLFD_FOUNDRY_MASK | XLFD_ADSTYLE_MASK)
806 #define XLFD_LARGENUM_MASK (XLFD_POINT_MASK | XLFD_RESX_MASK | XLFD_RESY_MASK \
807 | XLFD_AVGWIDTH_MASK)
808 #define XLFD_REGENC_MASK (XLFD_REGISTRY_MASK | XLFD_ENCODING_MASK)
809
810 /* Initialize RANGE_MASK for FIELD[0] which can be 0th to (14 - N)th
811 field. The value is shifted to left one bit by one in the
812 following loop. */
813 for (i = 0, range_mask = 0; i <= 14 - n; i++)
814 range_mask = (range_mask << 1) | 1;
815
816 /* The triplet RANGE_FROM, RANGE_TO, and RANGE_MASK is a
817 position-based retriction for FIELD[I]. */
818 for (i = 0, range_from = 0, range_to = 14 - n; i < n;
819 i++, range_from++, range_to++, range_mask <<= 1)
820 {
821 Lisp_Object val = field[i];
822
823 tmp[i] = val;
824 if (NILP (val))
825 {
826 /* Wildcard. */
827 range[i].from = range_from;
828 range[i].to = range_to;
829 range[i].mask = range_mask;
830 }
831 else
832 {
833 /* The triplet FROM, TO, and MASK is a value-based
834 retriction for FIELD[I]. */
835 int from, to;
836 unsigned mask;
837
838 if (INTEGERP (val))
839 {
840 int numeric = XINT (val);
841
842 if (i + 1 == n)
843 from = to = XLFD_ENCODING_INDEX,
844 mask = XLFD_ENCODING_MASK;
845 else if (numeric == 0)
846 from = XLFD_PIXEL_INDEX, to = XLFD_AVGWIDTH_INDEX,
847 mask = XLFD_PIXEL_MASK | XLFD_LARGENUM_MASK;
848 else if (numeric <= 48)
849 from = to = XLFD_PIXEL_INDEX,
850 mask = XLFD_PIXEL_MASK;
851 else
852 from = XLFD_POINT_INDEX, to = XLFD_AVGWIDTH_INDEX,
853 mask = XLFD_LARGENUM_MASK;
854 }
855 else if (SBYTES (SYMBOL_NAME (val)) == 0)
856 from = XLFD_FOUNDRY_INDEX, to = XLFD_ADSTYLE_INDEX,
857 mask = XLFD_NULL_MASK;
858 else if (i == 0)
859 from = to = XLFD_FOUNDRY_INDEX, mask = XLFD_FOUNDRY_MASK;
860 else if (i + 1 == n)
861 {
862 Lisp_Object name = SYMBOL_NAME (val);
863
864 if (SDATA (name)[SBYTES (name) - 1] == '*')
865 from = XLFD_REGISTRY_INDEX, to = XLFD_ENCODING_INDEX,
866 mask = XLFD_REGENC_MASK;
867 else
868 from = to = XLFD_ENCODING_INDEX,
869 mask = XLFD_ENCODING_MASK;
870 }
871 else if (range_from <= XLFD_WEIGHT_INDEX
872 && range_to >= XLFD_WEIGHT_INDEX
873 && FONT_WEIGHT_NAME_NUMERIC (val) >= 0)
874 from = to = XLFD_WEIGHT_INDEX, mask = XLFD_WEIGHT_MASK;
875 else if (range_from <= XLFD_SLANT_INDEX
876 && range_to >= XLFD_SLANT_INDEX
877 && FONT_SLANT_NAME_NUMERIC (val) >= 0)
878 from = to = XLFD_SLANT_INDEX, mask = XLFD_SLANT_MASK;
879 else if (range_from <= XLFD_SWIDTH_INDEX
880 && range_to >= XLFD_SWIDTH_INDEX
881 && FONT_WIDTH_NAME_NUMERIC (val) >= 0)
882 from = to = XLFD_SWIDTH_INDEX, mask = XLFD_SWIDTH_MASK;
883 else
884 {
885 if (EQ (val, Qc) || EQ (val, Qm) || EQ (val, Qp) || EQ (val, Qd))
886 from = to = XLFD_SPACING_INDEX, mask = XLFD_SPACING_MASK;
887 else
888 from = XLFD_FOUNDRY_INDEX, to = XLFD_ENCODING_INDEX,
889 mask = XLFD_SYMBOL_MASK;
890 }
891
892 /* Merge position-based and value-based restrictions. */
893 mask &= range_mask;
894 while (from < range_from)
895 mask &= ~(1 << from++);
896 while (from < 14 && ! (mask & (1 << from)))
897 from++;
898 while (to > range_to)
899 mask &= ~(1 << to--);
900 while (to >= 0 && ! (mask & (1 << to)))
901 to--;
902 if (from > to)
903 return -1;
904 range[i].from = from;
905 range[i].to = to;
906 range[i].mask = mask;
907
908 if (from > range_from || to < range_to)
909 {
910 /* The range is narrowed by value-based restrictions.
911 Reflect it to the other fields. */
912
913 /* Following fields should be after FROM. */
914 range_from = from;
915 /* Preceding fields should be before TO. */
916 for (j = i - 1, from--, to--; j >= 0; j--, from--, to--)
917 {
918 /* Check FROM for non-wildcard field. */
919 if (! NILP (tmp[j]) && range[j].from < from)
920 {
921 while (range[j].from < from)
922 range[j].mask &= ~(1 << range[j].from++);
923 while (from < 14 && ! (range[j].mask & (1 << from)))
924 from++;
925 range[j].from = from;
926 }
927 else
928 from = range[j].from;
929 if (range[j].to > to)
930 {
931 while (range[j].to > to)
932 range[j].mask &= ~(1 << range[j].to--);
933 while (to >= 0 && ! (range[j].mask & (1 << to)))
934 to--;
935 range[j].to = to;
936 }
937 else
938 to = range[j].to;
939 if (from > to)
940 return -1;
941 }
942 }
943 }
944 }
945
946 /* Decide all fileds from restrictions in RANGE. */
947 for (i = j = 0; i < n ; i++)
948 {
949 if (j < range[i].from)
950 {
951 if (i == 0 || ! NILP (tmp[i - 1]))
952 /* None of TMP[X] corresponds to Jth field. */
953 return -1;
954 for (; j < range[i].from; j++)
955 field[j] = Qnil;
956 }
957 field[j++] = tmp[i];
958 }
959 if (! NILP (tmp[n - 1]) && j < XLFD_REGISTRY_INDEX)
960 return -1;
961 for (; j < XLFD_LAST_INDEX; j++)
962 field[j] = Qnil;
963 if (INTEGERP (field[XLFD_ENCODING_INDEX]))
964 field[XLFD_ENCODING_INDEX]
965 = Fintern (Fnumber_to_string (field[XLFD_ENCODING_INDEX]), Qnil);
966 return 0;
967 }
968
969
970 /* Parse NAME (null terminated) as XLFD and store information in FONT
971 (font-spec or font-entity). Size property of FONT is set as
972 follows:
973 specified XLFD fields FONT property
974 --------------------- -------------
975 PIXEL_SIZE PIXEL_SIZE (Lisp integer)
976 POINT_SIZE and RESY calculated pixel size (Lisp integer)
977 POINT_SIZE POINT_SIZE/10 (Lisp float)
978
979 If NAME is successfully parsed, return 0. Otherwise return -1.
980
981 FONT is usually a font-spec, but when this function is called from
982 X font backend driver, it is a font-entity. In that case, NAME is
983 a fully specified XLFD. */
984
985 int
986 font_parse_xlfd (char *name, Lisp_Object font)
987 {
988 int len = strlen (name);
989 int i, j, n;
990 char *f[XLFD_LAST_INDEX + 1];
991 Lisp_Object val;
992 char *p;
993
994 if (len > 255 || !len)
995 /* Maximum XLFD name length is 255. */
996 return -1;
997 /* Accept "*-.." as a fully specified XLFD. */
998 if (name[0] == '*' && (len == 1 || name[1] == '-'))
999 i = 1, f[XLFD_FOUNDRY_INDEX] = name;
1000 else
1001 i = 0;
1002 for (p = name + i; *p; p++)
1003 if (*p == '-')
1004 {
1005 f[i++] = p + 1;
1006 if (i == XLFD_LAST_INDEX)
1007 break;
1008 }
1009 f[i] = name + len;
1010
1011 #define INTERN_FIELD(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 0)
1012 #define INTERN_FIELD_SYM(N) font_intern_prop (f[N], f[(N) + 1] - 1 - f[N], 1)
1013
1014 if (i == XLFD_LAST_INDEX)
1015 {
1016 /* Fully specified XLFD. */
1017 int pixel_size;
1018
1019 ASET (font, FONT_FOUNDRY_INDEX, INTERN_FIELD_SYM (XLFD_FOUNDRY_INDEX));
1020 ASET (font, FONT_FAMILY_INDEX, INTERN_FIELD_SYM (XLFD_FAMILY_INDEX));
1021 for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
1022 i <= XLFD_SWIDTH_INDEX; i++, j++)
1023 {
1024 val = INTERN_FIELD_SYM (i);
1025 if (! NILP (val))
1026 {
1027 if ((n = font_style_to_value (j, INTERN_FIELD_SYM (i), 0)) < 0)
1028 return -1;
1029 ASET (font, j, make_number (n));
1030 }
1031 }
1032 ASET (font, FONT_ADSTYLE_INDEX, INTERN_FIELD_SYM (XLFD_ADSTYLE_INDEX));
1033 if (strcmp (f[XLFD_REGISTRY_INDEX], "*-*") == 0)
1034 ASET (font, FONT_REGISTRY_INDEX, Qnil);
1035 else
1036 ASET (font, FONT_REGISTRY_INDEX,
1037 font_intern_prop (f[XLFD_REGISTRY_INDEX],
1038 f[XLFD_LAST_INDEX] - f[XLFD_REGISTRY_INDEX],
1039 1));
1040 p = f[XLFD_PIXEL_INDEX];
1041 if (*p == '[' && (pixel_size = parse_matrix (p)) >= 0)
1042 ASET (font, FONT_SIZE_INDEX, make_number (pixel_size));
1043 else
1044 {
1045 val = INTERN_FIELD (XLFD_PIXEL_INDEX);
1046 if (INTEGERP (val))
1047 ASET (font, FONT_SIZE_INDEX, val);
1048 else if (FONT_ENTITY_P (font))
1049 return -1;
1050 else
1051 {
1052 double point_size = -1;
1053
1054 font_assert (FONT_SPEC_P (font));
1055 p = f[XLFD_POINT_INDEX];
1056 if (*p == '[')
1057 point_size = parse_matrix (p);
1058 else if (isdigit (*p))
1059 point_size = atoi (p), point_size /= 10;
1060 if (point_size >= 0)
1061 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1062 }
1063 }
1064
1065 val = INTERN_FIELD (XLFD_RESY_INDEX);
1066 if (! NILP (val) && ! INTEGERP (val))
1067 return -1;
1068 ASET (font, FONT_DPI_INDEX, val);
1069 val = INTERN_FIELD (XLFD_SPACING_INDEX);
1070 if (! NILP (val))
1071 {
1072 val = font_prop_validate_spacing (QCspacing, val);
1073 if (! INTEGERP (val))
1074 return -1;
1075 ASET (font, FONT_SPACING_INDEX, val);
1076 }
1077 p = f[XLFD_AVGWIDTH_INDEX];
1078 if (*p == '~')
1079 p++;
1080 val = font_intern_prop (p, f[XLFD_REGISTRY_INDEX] - 1 - p, 0);
1081 if (! NILP (val) && ! INTEGERP (val))
1082 return -1;
1083 ASET (font, FONT_AVGWIDTH_INDEX, val);
1084 }
1085 else
1086 {
1087 int wild_card_found = 0;
1088 Lisp_Object prop[XLFD_LAST_INDEX];
1089
1090 if (FONT_ENTITY_P (font))
1091 return -1;
1092 for (j = 0; j < i; j++)
1093 {
1094 if (*f[j] == '*')
1095 {
1096 if (f[j][1] && f[j][1] != '-')
1097 return -1;
1098 prop[j] = Qnil;
1099 wild_card_found = 1;
1100 }
1101 else if (j + 1 < i)
1102 prop[j] = INTERN_FIELD (j);
1103 else
1104 prop[j] = font_intern_prop (f[j], f[i] - f[j], 0);
1105 }
1106 if (! wild_card_found)
1107 return -1;
1108 if (font_expand_wildcards (prop, i) < 0)
1109 return -1;
1110
1111 ASET (font, FONT_FOUNDRY_INDEX, prop[XLFD_FOUNDRY_INDEX]);
1112 ASET (font, FONT_FAMILY_INDEX, prop[XLFD_FAMILY_INDEX]);
1113 for (i = XLFD_WEIGHT_INDEX, j = FONT_WEIGHT_INDEX;
1114 i <= XLFD_SWIDTH_INDEX; i++, j++)
1115 if (! NILP (prop[i]))
1116 {
1117 if ((n = font_style_to_value (j, prop[i], 1)) < 0)
1118 return -1;
1119 ASET (font, j, make_number (n));
1120 }
1121 ASET (font, FONT_ADSTYLE_INDEX, prop[XLFD_ADSTYLE_INDEX]);
1122 val = prop[XLFD_REGISTRY_INDEX];
1123 if (NILP (val))
1124 {
1125 val = prop[XLFD_ENCODING_INDEX];
1126 if (! NILP (val))
1127 val = concat2 (build_string ("*-"), SYMBOL_NAME (val));
1128 }
1129 else if (NILP (prop[XLFD_ENCODING_INDEX]))
1130 val = concat2 (SYMBOL_NAME (val), build_string ("-*"));
1131 else
1132 val = concat3 (SYMBOL_NAME (val), build_string ("-"),
1133 SYMBOL_NAME (prop[XLFD_ENCODING_INDEX]));
1134 if (! NILP (val))
1135 ASET (font, FONT_REGISTRY_INDEX, Fintern (val, Qnil));
1136
1137 if (INTEGERP (prop[XLFD_PIXEL_INDEX]))
1138 ASET (font, FONT_SIZE_INDEX, prop[XLFD_PIXEL_INDEX]);
1139 else if (INTEGERP (prop[XLFD_POINT_INDEX]))
1140 {
1141 double point_size = XINT (prop[XLFD_POINT_INDEX]);
1142
1143 ASET (font, FONT_SIZE_INDEX, make_float (point_size / 10));
1144 }
1145
1146 if (INTEGERP (prop[XLFD_RESX_INDEX]))
1147 ASET (font, FONT_DPI_INDEX, prop[XLFD_RESY_INDEX]);
1148 if (! NILP (prop[XLFD_SPACING_INDEX]))
1149 {
1150 val = font_prop_validate_spacing (QCspacing,
1151 prop[XLFD_SPACING_INDEX]);
1152 if (! INTEGERP (val))
1153 return -1;
1154 ASET (font, FONT_SPACING_INDEX, val);
1155 }
1156 if (INTEGERP (prop[XLFD_AVGWIDTH_INDEX]))
1157 ASET (font, FONT_AVGWIDTH_INDEX, prop[XLFD_AVGWIDTH_INDEX]);
1158 }
1159
1160 return 0;
1161 }
1162
1163 /* Store XLFD name of FONT (font-spec or font-entity) in NAME (NBYTES
1164 length), and return the name length. If FONT_SIZE_INDEX of FONT is
1165 0, use PIXEL_SIZE instead. */
1166
1167 int
1168 font_unparse_xlfd (Lisp_Object font, int pixel_size, char *name, int nbytes)
1169 {
1170 char *f[XLFD_REGISTRY_INDEX + 1];
1171 Lisp_Object val;
1172 int i, j, len = 0;
1173
1174 font_assert (FONTP (font));
1175
1176 for (i = FONT_FOUNDRY_INDEX, j = XLFD_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX;
1177 i++, j++)
1178 {
1179 if (i == FONT_ADSTYLE_INDEX)
1180 j = XLFD_ADSTYLE_INDEX;
1181 else if (i == FONT_REGISTRY_INDEX)
1182 j = XLFD_REGISTRY_INDEX;
1183 val = AREF (font, i);
1184 if (NILP (val))
1185 {
1186 if (j == XLFD_REGISTRY_INDEX)
1187 f[j] = "*-*", len += 4;
1188 else
1189 f[j] = "*", len += 2;
1190 }
1191 else
1192 {
1193 if (SYMBOLP (val))
1194 val = SYMBOL_NAME (val);
1195 if (j == XLFD_REGISTRY_INDEX
1196 && ! strchr ((char *) SDATA (val), '-'))
1197 {
1198 /* Change "jisx0208*" and "jisx0208" to "jisx0208*-*". */
1199 if (SDATA (val)[SBYTES (val) - 1] == '*')
1200 {
1201 f[j] = alloca (SBYTES (val) + 3);
1202 sprintf (f[j], "%s-*", SDATA (val));
1203 len += SBYTES (val) + 3;
1204 }
1205 else
1206 {
1207 f[j] = alloca (SBYTES (val) + 4);
1208 sprintf (f[j], "%s*-*", SDATA (val));
1209 len += SBYTES (val) + 4;
1210 }
1211 }
1212 else
1213 f[j] = (char *) SDATA (val), len += SBYTES (val) + 1;
1214 }
1215 }
1216
1217 for (i = FONT_WEIGHT_INDEX, j = XLFD_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX;
1218 i++, j++)
1219 {
1220 val = font_style_symbolic (font, i, 0);
1221 if (NILP (val))
1222 f[j] = "*", len += 2;
1223 else
1224 {
1225 val = SYMBOL_NAME (val);
1226 f[j] = (char *) SDATA (val), len += SBYTES (val) + 1;
1227 }
1228 }
1229
1230 val = AREF (font, FONT_SIZE_INDEX);
1231 font_assert (NUMBERP (val) || NILP (val));
1232 if (INTEGERP (val))
1233 {
1234 i = XINT (val);
1235 if (i <= 0)
1236 i = pixel_size;
1237 if (i > 0)
1238 {
1239 f[XLFD_PIXEL_INDEX] = alloca (22);
1240 len += sprintf (f[XLFD_PIXEL_INDEX], "%d-*", i) + 1;
1241 }
1242 else
1243 f[XLFD_PIXEL_INDEX] = "*-*", len += 4;
1244 }
1245 else if (FLOATP (val))
1246 {
1247 i = XFLOAT_DATA (val) * 10;
1248 f[XLFD_PIXEL_INDEX] = alloca (12);
1249 len += sprintf (f[XLFD_PIXEL_INDEX], "*-%d", i) + 1;
1250 }
1251 else
1252 f[XLFD_PIXEL_INDEX] = "*-*", len += 4;
1253
1254 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1255 {
1256 i = XINT (AREF (font, FONT_DPI_INDEX));
1257 f[XLFD_RESX_INDEX] = alloca (22);
1258 len += sprintf (f[XLFD_RESX_INDEX],
1259 "%d-%d", i, i) + 1;
1260 }
1261 else
1262 f[XLFD_RESX_INDEX] = "*-*", len += 4;
1263 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1264 {
1265 int spacing = XINT (AREF (font, FONT_SPACING_INDEX));
1266
1267 f[XLFD_SPACING_INDEX] = (spacing <= FONT_SPACING_PROPORTIONAL ? "p"
1268 : spacing <= FONT_SPACING_DUAL ? "d"
1269 : spacing <= FONT_SPACING_MONO ? "m"
1270 : "c");
1271 len += 2;
1272 }
1273 else
1274 f[XLFD_SPACING_INDEX] = "*", len += 2;
1275 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1276 {
1277 f[XLFD_AVGWIDTH_INDEX] = alloca (11);
1278 len += sprintf (f[XLFD_AVGWIDTH_INDEX], "%ld",
1279 (long) XINT (AREF (font, FONT_AVGWIDTH_INDEX))) + 1;
1280 }
1281 else
1282 f[XLFD_AVGWIDTH_INDEX] = "*", len += 2;
1283 len++; /* for terminating '\0'. */
1284 if (len >= nbytes)
1285 return -1;
1286 return sprintf (name, "-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s-%s",
1287 f[XLFD_FOUNDRY_INDEX], f[XLFD_FAMILY_INDEX],
1288 f[XLFD_WEIGHT_INDEX], f[XLFD_SLANT_INDEX],
1289 f[XLFD_SWIDTH_INDEX], f[XLFD_ADSTYLE_INDEX],
1290 f[XLFD_PIXEL_INDEX], f[XLFD_RESX_INDEX],
1291 f[XLFD_SPACING_INDEX], f[XLFD_AVGWIDTH_INDEX],
1292 f[XLFD_REGISTRY_INDEX]);
1293 }
1294
1295 /* Parse NAME (null terminated) and store information in FONT
1296 (font-spec or font-entity). NAME is supplied in either the
1297 Fontconfig or GTK font name format. If NAME is successfully
1298 parsed, return 0. Otherwise return -1.
1299
1300 The fontconfig format is
1301
1302 FAMILY[-SIZE][:PROP1[=VAL1][:PROP2[=VAL2]...]]
1303
1304 The GTK format is
1305
1306 FAMILY [PROPS...] [SIZE]
1307
1308 This function tries to guess which format it is. */
1309
1310 static int
1311 font_parse_fcname (char *name, Lisp_Object font)
1312 {
1313 char *p, *q;
1314 char *size_beg = NULL, *size_end = NULL;
1315 char *props_beg = NULL, *family_end = NULL;
1316 int len = strlen (name);
1317
1318 if (len == 0)
1319 return -1;
1320
1321 for (p = name; *p; p++)
1322 {
1323 if (*p == '\\' && p[1])
1324 p++;
1325 else if (*p == ':')
1326 {
1327 props_beg = family_end = p;
1328 break;
1329 }
1330 else if (*p == '-')
1331 {
1332 int decimal = 0, size_found = 1;
1333 for (q = p + 1; *q && *q != ':'; q++)
1334 if (! isdigit(*q))
1335 {
1336 if (*q != '.' || decimal)
1337 {
1338 size_found = 0;
1339 break;
1340 }
1341 decimal = 1;
1342 }
1343 if (size_found)
1344 {
1345 family_end = p;
1346 size_beg = p + 1;
1347 size_end = q;
1348 break;
1349 }
1350 }
1351 }
1352
1353 if (family_end)
1354 {
1355 Lisp_Object extra_props = Qnil;
1356
1357 /* A fontconfig name with size and/or property data. */
1358 if (family_end > name)
1359 {
1360 Lisp_Object family;
1361 family = font_intern_prop (name, family_end - name, 1);
1362 ASET (font, FONT_FAMILY_INDEX, family);
1363 }
1364 if (size_beg)
1365 {
1366 double point_size = strtod (size_beg, &size_end);
1367 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1368 if (*size_end == ':' && size_end[1])
1369 props_beg = size_end;
1370 }
1371 if (props_beg)
1372 {
1373 /* Now parse ":KEY=VAL" patterns. */
1374 Lisp_Object val;
1375
1376 for (p = props_beg; *p; p = q)
1377 {
1378 for (q = p + 1; *q && *q != '=' && *q != ':'; q++);
1379 if (*q != '=')
1380 {
1381 /* Must be an enumerated value. */
1382 int word_len;
1383 p = p + 1;
1384 word_len = q - p;
1385 val = font_intern_prop (p, q - p, 1);
1386
1387 #define PROP_MATCH(STR,N) ((word_len == N) && memcmp (p, STR, N) == 0)
1388
1389 if (PROP_MATCH ("light", 5)
1390 || PROP_MATCH ("medium", 6)
1391 || PROP_MATCH ("demibold", 8)
1392 || PROP_MATCH ("bold", 4)
1393 || PROP_MATCH ("black", 5))
1394 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, val);
1395 else if (PROP_MATCH ("roman", 5)
1396 || PROP_MATCH ("italic", 6)
1397 || PROP_MATCH ("oblique", 7))
1398 FONT_SET_STYLE (font, FONT_SLANT_INDEX, val);
1399 else if (PROP_MATCH ("charcell", 8))
1400 ASET (font, FONT_SPACING_INDEX,
1401 make_number (FONT_SPACING_CHARCELL));
1402 else if (PROP_MATCH ("mono", 4))
1403 ASET (font, FONT_SPACING_INDEX,
1404 make_number (FONT_SPACING_MONO));
1405 else if (PROP_MATCH ("proportional", 12))
1406 ASET (font, FONT_SPACING_INDEX,
1407 make_number (FONT_SPACING_PROPORTIONAL));
1408 #undef PROP_MATCH
1409 }
1410 else
1411 {
1412 /* KEY=VAL pairs */
1413 Lisp_Object key;
1414 int prop;
1415
1416 if (q - p == 10 && memcmp (p + 1, "pixelsize", 9) == 0)
1417 prop = FONT_SIZE_INDEX;
1418 else
1419 {
1420 key = font_intern_prop (p, q - p, 1);
1421 prop = get_font_prop_index (key);
1422 }
1423
1424 p = q + 1;
1425 for (q = p; *q && *q != ':'; q++);
1426 val = font_intern_prop (p, q - p, 0);
1427
1428 if (prop >= FONT_FOUNDRY_INDEX
1429 && prop < FONT_EXTRA_INDEX)
1430 ASET (font, prop, font_prop_validate (prop, Qnil, val));
1431 else
1432 {
1433 extra_props = nconc2 (extra_props,
1434 Fcons (Fcons (key, val), Qnil));
1435 }
1436 }
1437 p = q;
1438 }
1439 }
1440
1441 if (! NILP (extra_props))
1442 {
1443 struct font_driver_list *driver_list = font_driver_list;
1444 for ( ; driver_list; driver_list = driver_list->next)
1445 if (driver_list->driver->filter_properties)
1446 (*driver_list->driver->filter_properties) (font, extra_props);
1447 }
1448
1449 }
1450 else
1451 {
1452 /* Either a fontconfig-style name with no size and property
1453 data, or a GTK-style name. */
1454 Lisp_Object prop;
1455 int word_len, prop_found = 0;
1456
1457 for (p = name; *p; p = *q ? q + 1 : q)
1458 {
1459 if (isdigit (*p))
1460 {
1461 int size_found = 1;
1462
1463 for (q = p + 1; *q && *q != ' '; q++)
1464 if (! isdigit (*q) && *q != '.')
1465 {
1466 size_found = 0;
1467 break;
1468 }
1469 if (size_found)
1470 {
1471 double point_size = strtod (p, &q);
1472 ASET (font, FONT_SIZE_INDEX, make_float (point_size));
1473 continue;
1474 }
1475 }
1476
1477 for (q = p + 1; *q && *q != ' '; q++)
1478 if (*q == '\\' && q[1])
1479 q++;
1480 word_len = q - p;
1481
1482 #define PROP_MATCH(STR,N) ((word_len == N) && memcmp (p, STR, N) == 0)
1483
1484 if (PROP_MATCH ("Ultra-Light", 11))
1485 {
1486 prop_found = 1;
1487 prop = font_intern_prop ("ultra-light", 11, 1);
1488 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
1489 }
1490 else if (PROP_MATCH ("Light", 5))
1491 {
1492 prop_found = 1;
1493 prop = font_intern_prop ("light", 5, 1);
1494 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
1495 }
1496 else if (PROP_MATCH ("Book", 4))
1497 {
1498 prop_found = 1;
1499 prop = font_intern_prop ("book", 4, 1);
1500 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
1501 }
1502 else if (PROP_MATCH ("Medium", 6))
1503 {
1504 prop_found = 1;
1505 prop = font_intern_prop ("medium", 6, 1);
1506 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
1507 }
1508 else if (PROP_MATCH ("Semi-Bold", 9))
1509 {
1510 prop_found = 1;
1511 prop = font_intern_prop ("semi-bold", 9, 1);
1512 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
1513 }
1514 else if (PROP_MATCH ("Bold", 4))
1515 {
1516 prop_found = 1;
1517 prop = font_intern_prop ("bold", 4, 1);
1518 FONT_SET_STYLE (font, FONT_WEIGHT_INDEX, prop);
1519 }
1520 else if (PROP_MATCH ("Italic", 6))
1521 {
1522 prop_found = 1;
1523 prop = font_intern_prop ("italic", 4, 1);
1524 FONT_SET_STYLE (font, FONT_SLANT_INDEX, prop);
1525 }
1526 else if (PROP_MATCH ("Oblique", 7))
1527 {
1528 prop_found = 1;
1529 prop = font_intern_prop ("oblique", 7, 1);
1530 FONT_SET_STYLE (font, FONT_SLANT_INDEX, prop);
1531 }
1532 else if (PROP_MATCH ("Semi-Condensed", 14))
1533 {
1534 prop_found = 1;
1535 prop = font_intern_prop ("semi-condensed", 14, 1);
1536 FONT_SET_STYLE (font, FONT_WIDTH_INDEX, prop);
1537 }
1538 else if (PROP_MATCH ("Condensed", 9))
1539 {
1540 prop_found = 1;
1541 prop = font_intern_prop ("condensed", 9, 1);
1542 FONT_SET_STYLE (font, FONT_WIDTH_INDEX, prop);
1543 }
1544 else {
1545 if (prop_found)
1546 return -1; /* Unknown property in GTK-style font name. */
1547 family_end = q;
1548 }
1549 }
1550 #undef PROP_MATCH
1551
1552 if (family_end)
1553 {
1554 Lisp_Object family;
1555 family = font_intern_prop (name, family_end - name, 1);
1556 ASET (font, FONT_FAMILY_INDEX, family);
1557 }
1558 }
1559
1560 return 0;
1561 }
1562
1563 /* Store fontconfig's font name of FONT (font-spec or font-entity) in
1564 NAME (NBYTES length), and return the name length. If
1565 FONT_SIZE_INDEX of FONT is 0, use PIXEL_SIZE instead. */
1566
1567 int
1568 font_unparse_fcname (Lisp_Object font, int pixel_size, char *name, int nbytes)
1569 {
1570 Lisp_Object family, foundry;
1571 Lisp_Object tail, val;
1572 int point_size;
1573 int i, len = 1;
1574 char *p;
1575 Lisp_Object styles[3];
1576 const char *style_names[3] = { "weight", "slant", "width" };
1577 char work[256];
1578
1579 family = AREF (font, FONT_FAMILY_INDEX);
1580 if (! NILP (family))
1581 {
1582 if (SYMBOLP (family))
1583 {
1584 family = SYMBOL_NAME (family);
1585 len += SBYTES (family);
1586 }
1587 else
1588 family = Qnil;
1589 }
1590
1591 val = AREF (font, FONT_SIZE_INDEX);
1592 if (INTEGERP (val))
1593 {
1594 if (XINT (val) != 0)
1595 pixel_size = XINT (val);
1596 point_size = -1;
1597 len += 21; /* for ":pixelsize=NUM" */
1598 }
1599 else if (FLOATP (val))
1600 {
1601 pixel_size = -1;
1602 point_size = (int) XFLOAT_DATA (val);
1603 len += 11; /* for "-NUM" */
1604 }
1605
1606 foundry = AREF (font, FONT_FOUNDRY_INDEX);
1607 if (! NILP (foundry))
1608 {
1609 if (SYMBOLP (foundry))
1610 {
1611 foundry = SYMBOL_NAME (foundry);
1612 len += 9 + SBYTES (foundry); /* ":foundry=NAME" */
1613 }
1614 else
1615 foundry = Qnil;
1616 }
1617
1618 for (i = 0; i < 3; i++)
1619 {
1620 styles[i] = font_style_symbolic (font, FONT_WEIGHT_INDEX + i, 0);
1621 if (! NILP (styles[i]))
1622 len += sprintf (work, ":%s=%s", style_names[i],
1623 SDATA (SYMBOL_NAME (styles[i])));
1624 }
1625
1626 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1627 len += sprintf (work, ":dpi=%ld", (long)XINT (AREF (font, FONT_DPI_INDEX)));
1628 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1629 len += strlen (":spacing=100");
1630 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1631 len += strlen (":scalable=false"); /* or ":scalable=true" */
1632 for (tail = AREF (font, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail))
1633 {
1634 Lisp_Object key = XCAR (XCAR (tail)), val = XCDR (XCAR (tail));
1635
1636 len += SBYTES (SYMBOL_NAME (key)) + 1; /* for :KEY= */
1637 if (STRINGP (val))
1638 len += SBYTES (val);
1639 else if (INTEGERP (val))
1640 len += sprintf (work, "%ld", (long) XINT (val));
1641 else if (SYMBOLP (val))
1642 len += (NILP (val) ? 5 : 4); /* for "false" or "true" */
1643 }
1644
1645 if (len > nbytes)
1646 return -1;
1647 p = name;
1648 if (! NILP (family))
1649 p += sprintf (p, "%s", SDATA (family));
1650 if (point_size > 0)
1651 {
1652 if (p == name)
1653 p += sprintf (p, "%d", point_size);
1654 else
1655 p += sprintf (p, "-%d", point_size);
1656 }
1657 else if (pixel_size > 0)
1658 p += sprintf (p, ":pixelsize=%d", pixel_size);
1659 if (! NILP (AREF (font, FONT_FOUNDRY_INDEX)))
1660 p += sprintf (p, ":foundry=%s",
1661 SDATA (SYMBOL_NAME (AREF (font, FONT_FOUNDRY_INDEX))));
1662 for (i = 0; i < 3; i++)
1663 if (! NILP (styles[i]))
1664 p += sprintf (p, ":%s=%s", style_names[i],
1665 SDATA (SYMBOL_NAME (styles[i])));
1666 if (INTEGERP (AREF (font, FONT_DPI_INDEX)))
1667 p += sprintf (p, ":dpi=%ld", (long) XINT (AREF (font, FONT_DPI_INDEX)));
1668 if (INTEGERP (AREF (font, FONT_SPACING_INDEX)))
1669 p += sprintf (p, ":spacing=%ld",
1670 (long) XINT (AREF (font, FONT_SPACING_INDEX)));
1671 if (INTEGERP (AREF (font, FONT_AVGWIDTH_INDEX)))
1672 {
1673 if (XINT (AREF (font, FONT_AVGWIDTH_INDEX)) == 0)
1674 p += sprintf (p, ":scalable=true");
1675 else
1676 p += sprintf (p, ":scalable=false");
1677 }
1678 return (p - name);
1679 }
1680
1681 /* Parse NAME (null terminated) and store information in FONT
1682 (font-spec or font-entity). If NAME is successfully parsed, return
1683 0. Otherwise return -1. */
1684
1685 static int
1686 font_parse_name (char *name, Lisp_Object font)
1687 {
1688 if (name[0] == '-' || strchr (name, '*') || strchr (name, '?'))
1689 return font_parse_xlfd (name, font);
1690 return font_parse_fcname (name, font);
1691 }
1692
1693
1694 /* Merge FAMILY and REGISTRY into FONT_SPEC. FAMILY may have the form
1695 "FAMILY-FOUNDRY". REGISTRY may not contain charset-encoding
1696 part. */
1697
1698 void
1699 font_parse_family_registry (Lisp_Object family, Lisp_Object registry, Lisp_Object font_spec)
1700 {
1701 int len;
1702 char *p0, *p1;
1703
1704 if (! NILP (family)
1705 && NILP (AREF (font_spec, FONT_FAMILY_INDEX)))
1706 {
1707 CHECK_STRING (family);
1708 len = SBYTES (family);
1709 p0 = (char *) SDATA (family);
1710 p1 = strchr (p0, '-');
1711 if (p1)
1712 {
1713 if ((*p0 != '*' && p1 - p0 > 0)
1714 && NILP (AREF (font_spec, FONT_FOUNDRY_INDEX)))
1715 Ffont_put (font_spec, QCfoundry, font_intern_prop (p0, p1 - p0, 1));
1716 p1++;
1717 len -= p1 - p0;
1718 Ffont_put (font_spec, QCfamily, font_intern_prop (p1, len, 1));
1719 }
1720 else
1721 ASET (font_spec, FONT_FAMILY_INDEX, Fintern (family, Qnil));
1722 }
1723 if (! NILP (registry))
1724 {
1725 /* Convert "XXX" and "XXX*" to "XXX*-*". */
1726 CHECK_STRING (registry);
1727 len = SBYTES (registry);
1728 p0 = (char *) SDATA (registry);
1729 p1 = strchr (p0, '-');
1730 if (! p1)
1731 {
1732 if (SDATA (registry)[len - 1] == '*')
1733 registry = concat2 (registry, build_string ("-*"));
1734 else
1735 registry = concat2 (registry, build_string ("*-*"));
1736 }
1737 registry = Fdowncase (registry);
1738 ASET (font_spec, FONT_REGISTRY_INDEX, Fintern (registry, Qnil));
1739 }
1740 }
1741
1742 \f
1743 /* This part (through the next ^L) is still experimental and not
1744 tested much. We may drastically change codes. */
1745
1746 /* OTF handler */
1747
1748 #if 0
1749
1750 #define LGSTRING_HEADER_SIZE 6
1751 #define LGSTRING_GLYPH_SIZE 8
1752
1753 static int
1754 check_gstring (gstring)
1755 Lisp_Object gstring;
1756 {
1757 Lisp_Object val;
1758 int i, j;
1759
1760 CHECK_VECTOR (gstring);
1761 val = AREF (gstring, 0);
1762 CHECK_VECTOR (val);
1763 if (ASIZE (val) < LGSTRING_HEADER_SIZE)
1764 goto err;
1765 CHECK_FONT_OBJECT (LGSTRING_FONT (gstring));
1766 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_LBEARING)))
1767 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_LBEARING));
1768 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_RBEARING)))
1769 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_RBEARING));
1770 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_WIDTH)))
1771 CHECK_NATNUM (LGSTRING_SLOT (gstring, LGSTRING_IX_WIDTH));
1772 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT)))
1773 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT));
1774 if (!NILP (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT)))
1775 CHECK_NUMBER (LGSTRING_SLOT (gstring, LGSTRING_IX_ASCENT));
1776
1777 for (i = 0; i < LGSTRING_GLYPH_LEN (gstring); i++)
1778 {
1779 val = LGSTRING_GLYPH (gstring, i);
1780 CHECK_VECTOR (val);
1781 if (ASIZE (val) < LGSTRING_GLYPH_SIZE)
1782 goto err;
1783 if (NILP (AREF (val, LGLYPH_IX_CHAR)))
1784 break;
1785 CHECK_NATNUM (AREF (val, LGLYPH_IX_FROM));
1786 CHECK_NATNUM (AREF (val, LGLYPH_IX_TO));
1787 CHECK_CHARACTER (AREF (val, LGLYPH_IX_CHAR));
1788 if (!NILP (AREF (val, LGLYPH_IX_CODE)))
1789 CHECK_NATNUM (AREF (val, LGLYPH_IX_CODE));
1790 if (!NILP (AREF (val, LGLYPH_IX_WIDTH)))
1791 CHECK_NATNUM (AREF (val, LGLYPH_IX_WIDTH));
1792 if (!NILP (AREF (val, LGLYPH_IX_ADJUSTMENT)))
1793 {
1794 val = AREF (val, LGLYPH_IX_ADJUSTMENT);
1795 CHECK_VECTOR (val);
1796 if (ASIZE (val) < 3)
1797 goto err;
1798 for (j = 0; j < 3; j++)
1799 CHECK_NUMBER (AREF (val, j));
1800 }
1801 }
1802 return i;
1803 err:
1804 error ("Invalid glyph-string format");
1805 return -1;
1806 }
1807
1808 static void
1809 check_otf_features (otf_features)
1810 Lisp_Object otf_features;
1811 {
1812 Lisp_Object val;
1813
1814 CHECK_CONS (otf_features);
1815 CHECK_SYMBOL (XCAR (otf_features));
1816 otf_features = XCDR (otf_features);
1817 CHECK_CONS (otf_features);
1818 CHECK_SYMBOL (XCAR (otf_features));
1819 otf_features = XCDR (otf_features);
1820 for (val = Fcar (otf_features); ! NILP (val); val = Fcdr (val))
1821 {
1822 CHECK_SYMBOL (Fcar (val));
1823 if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
1824 error ("Invalid OTF GSUB feature: %s", SYMBOL_NAME (XCAR (val)));
1825 }
1826 otf_features = XCDR (otf_features);
1827 for (val = Fcar (otf_features); ! NILP (val); val = Fcdr (val))
1828 {
1829 CHECK_SYMBOL (Fcar (val));
1830 if (SBYTES (SYMBOL_NAME (XCAR (val))) > 4)
1831 error ("Invalid OTF GPOS feature: %s", SYMBOL_NAME (XCAR (val)));
1832 }
1833 }
1834
1835 #ifdef HAVE_LIBOTF
1836 #include <otf.h>
1837
1838 Lisp_Object otf_list;
1839
1840 static Lisp_Object
1841 otf_tag_symbol (tag)
1842 OTF_Tag tag;
1843 {
1844 char name[5];
1845
1846 OTF_tag_name (tag, name);
1847 return Fintern (make_unibyte_string (name, 4), Qnil);
1848 }
1849
1850 static OTF *
1851 otf_open (file)
1852 Lisp_Object file;
1853 {
1854 Lisp_Object val = Fassoc (file, otf_list);
1855 OTF *otf;
1856
1857 if (! NILP (val))
1858 otf = XSAVE_VALUE (XCDR (val))->pointer;
1859 else
1860 {
1861 otf = STRINGP (file) ? OTF_open ((char *) SDATA (file)) : NULL;
1862 val = make_save_value (otf, 0);
1863 otf_list = Fcons (Fcons (file, val), otf_list);
1864 }
1865 return otf;
1866 }
1867
1868
1869 /* Return a list describing which scripts/languages FONT supports by
1870 which GSUB/GPOS features of OpenType tables. See the comment of
1871 (struct font_driver).otf_capability. */
1872
1873 Lisp_Object
1874 font_otf_capability (font)
1875 struct font *font;
1876 {
1877 OTF *otf;
1878 Lisp_Object capability = Fcons (Qnil, Qnil);
1879 int i;
1880
1881 otf = otf_open (font->props[FONT_FILE_INDEX]);
1882 if (! otf)
1883 return Qnil;
1884 for (i = 0; i < 2; i++)
1885 {
1886 OTF_GSUB_GPOS *gsub_gpos;
1887 Lisp_Object script_list = Qnil;
1888 int j;
1889
1890 if (OTF_get_features (otf, i == 0) < 0)
1891 continue;
1892 gsub_gpos = i == 0 ? otf->gsub : otf->gpos;
1893 for (j = gsub_gpos->ScriptList.ScriptCount - 1; j >= 0; j--)
1894 {
1895 OTF_Script *script = gsub_gpos->ScriptList.Script + j;
1896 Lisp_Object langsys_list = Qnil;
1897 Lisp_Object script_tag = otf_tag_symbol (script->ScriptTag);
1898 int k;
1899
1900 for (k = script->LangSysCount; k >= 0; k--)
1901 {
1902 OTF_LangSys *langsys;
1903 Lisp_Object feature_list = Qnil;
1904 Lisp_Object langsys_tag;
1905 int l;
1906
1907 if (k == script->LangSysCount)
1908 {
1909 langsys = &script->DefaultLangSys;
1910 langsys_tag = Qnil;
1911 }
1912 else
1913 {
1914 langsys = script->LangSys + k;
1915 langsys_tag
1916 = otf_tag_symbol (script->LangSysRecord[k].LangSysTag);
1917 }
1918 for (l = langsys->FeatureCount - 1; l >= 0; l--)
1919 {
1920 OTF_Feature *feature
1921 = gsub_gpos->FeatureList.Feature + langsys->FeatureIndex[l];
1922 Lisp_Object feature_tag
1923 = otf_tag_symbol (feature->FeatureTag);
1924
1925 feature_list = Fcons (feature_tag, feature_list);
1926 }
1927 langsys_list = Fcons (Fcons (langsys_tag, feature_list),
1928 langsys_list);
1929 }
1930 script_list = Fcons (Fcons (script_tag, langsys_list),
1931 script_list);
1932 }
1933
1934 if (i == 0)
1935 XSETCAR (capability, script_list);
1936 else
1937 XSETCDR (capability, script_list);
1938 }
1939
1940 return capability;
1941 }
1942
1943 /* Parse OTF features in SPEC and write a proper features spec string
1944 in FEATURES for the call of OTF_drive_gsub/gpos (of libotf). It is
1945 assured that the sufficient memory has already allocated for
1946 FEATURES. */
1947
1948 static void
1949 generate_otf_features (spec, features)
1950 Lisp_Object spec;
1951 char *features;
1952 {
1953 Lisp_Object val;
1954 char *p;
1955 int asterisk;
1956
1957 p = features;
1958 *p = '\0';
1959 for (asterisk = 0; CONSP (spec); spec = XCDR (spec))
1960 {
1961 val = XCAR (spec);
1962 CHECK_SYMBOL (val);
1963 if (p > features)
1964 *p++ = ',';
1965 if (SREF (SYMBOL_NAME (val), 0) == '*')
1966 {
1967 asterisk = 1;
1968 *p++ = '*';
1969 }
1970 else if (! asterisk)
1971 {
1972 val = SYMBOL_NAME (val);
1973 p += sprintf (p, "%s", SDATA (val));
1974 }
1975 else
1976 {
1977 val = SYMBOL_NAME (val);
1978 p += sprintf (p, "~%s", SDATA (val));
1979 }
1980 }
1981 if (CONSP (spec))
1982 error ("OTF spec too long");
1983 }
1984
1985 Lisp_Object
1986 font_otf_DeviceTable (device_table)
1987 OTF_DeviceTable *device_table;
1988 {
1989 int len = device_table->StartSize - device_table->EndSize + 1;
1990
1991 return Fcons (make_number (len),
1992 make_unibyte_string (device_table->DeltaValue, len));
1993 }
1994
1995 Lisp_Object
1996 font_otf_ValueRecord (value_format, value_record)
1997 int value_format;
1998 OTF_ValueRecord *value_record;
1999 {
2000 Lisp_Object val = Fmake_vector (make_number (8), Qnil);
2001
2002 if (value_format & OTF_XPlacement)
2003 ASET (val, 0, make_number (value_record->XPlacement));
2004 if (value_format & OTF_YPlacement)
2005 ASET (val, 1, make_number (value_record->YPlacement));
2006 if (value_format & OTF_XAdvance)
2007 ASET (val, 2, make_number (value_record->XAdvance));
2008 if (value_format & OTF_YAdvance)
2009 ASET (val, 3, make_number (value_record->YAdvance));
2010 if (value_format & OTF_XPlaDevice)
2011 ASET (val, 4, font_otf_DeviceTable (&value_record->XPlaDevice));
2012 if (value_format & OTF_YPlaDevice)
2013 ASET (val, 4, font_otf_DeviceTable (&value_record->YPlaDevice));
2014 if (value_format & OTF_XAdvDevice)
2015 ASET (val, 4, font_otf_DeviceTable (&value_record->XAdvDevice));
2016 if (value_format & OTF_YAdvDevice)
2017 ASET (val, 4, font_otf_DeviceTable (&value_record->YAdvDevice));
2018 return val;
2019 }
2020
2021 Lisp_Object
2022 font_otf_Anchor (anchor)
2023 OTF_Anchor *anchor;
2024 {
2025 Lisp_Object val;
2026
2027 val = Fmake_vector (make_number (anchor->AnchorFormat + 1), Qnil);
2028 ASET (val, 0, make_number (anchor->XCoordinate));
2029 ASET (val, 1, make_number (anchor->YCoordinate));
2030 if (anchor->AnchorFormat == 2)
2031 ASET (val, 2, make_number (anchor->f.f1.AnchorPoint));
2032 else
2033 {
2034 ASET (val, 3, font_otf_DeviceTable (&anchor->f.f2.XDeviceTable));
2035 ASET (val, 4, font_otf_DeviceTable (&anchor->f.f2.YDeviceTable));
2036 }
2037 return val;
2038 }
2039 #endif /* HAVE_LIBOTF */
2040 #endif /* 0 */
2041
2042 \f
2043 /* Font sorting */
2044
2045 static unsigned font_score (Lisp_Object, Lisp_Object *);
2046 static int font_compare (const void *, const void *);
2047 static Lisp_Object font_sort_entities (Lisp_Object, Lisp_Object,
2048 Lisp_Object, int);
2049
2050 static double
2051 font_rescale_ratio (Lisp_Object font_entity)
2052 {
2053 Lisp_Object tail, elt;
2054 Lisp_Object name = Qnil;
2055
2056 for (tail = Vface_font_rescale_alist; CONSP (tail); tail = XCDR (tail))
2057 {
2058 elt = XCAR (tail);
2059 if (FLOATP (XCDR (elt)))
2060 {
2061 if (STRINGP (XCAR (elt)))
2062 {
2063 if (NILP (name))
2064 name = Ffont_xlfd_name (font_entity, Qnil);
2065 if (fast_string_match_ignore_case (XCAR (elt), name) >= 0)
2066 return XFLOAT_DATA (XCDR (elt));
2067 }
2068 else if (FONT_SPEC_P (XCAR (elt)))
2069 {
2070 if (font_match_p (XCAR (elt), font_entity))
2071 return XFLOAT_DATA (XCDR (elt));
2072 }
2073 }
2074 }
2075 return 1.0;
2076 }
2077
2078 /* We sort fonts by scoring each of them against a specified
2079 font-spec. The score value is 32 bit (`unsigned'), and the smaller
2080 the value is, the closer the font is to the font-spec.
2081
2082 The lowest 2 bits of the score is used for driver type. The font
2083 available by the most preferred font driver is 0.
2084
2085 Each 7-bit in the higher 28 bits are used for numeric properties
2086 WEIGHT, SLANT, WIDTH, and SIZE. */
2087
2088 /* How many bits to shift to store the difference value of each font
2089 property in a score. Note that flots for FONT_TYPE_INDEX and
2090 FONT_REGISTRY_INDEX are not used. */
2091 static int sort_shift_bits[FONT_SIZE_INDEX + 1];
2092
2093 /* Score font-entity ENTITY against properties of font-spec SPEC_PROP.
2094 The return value indicates how different ENTITY is compared with
2095 SPEC_PROP. */
2096
2097 static unsigned
2098 font_score (Lisp_Object entity, Lisp_Object *spec_prop)
2099 {
2100 unsigned score = 0;
2101 int i;
2102
2103 /* Score three style numeric fields. Maximum difference is 127. */
2104 for (i = FONT_WEIGHT_INDEX; i <= FONT_WIDTH_INDEX; i++)
2105 if (! NILP (spec_prop[i]) && ! EQ (AREF (entity, i), spec_prop[i]))
2106 {
2107 int diff = (XINT (AREF (entity, i)) >> 8) - (XINT (spec_prop[i]) >> 8);
2108
2109 if (diff < 0)
2110 diff = - diff;
2111 if (diff > 0)
2112 score |= min (diff, 127) << sort_shift_bits[i];
2113 }
2114
2115 /* Score the size. Maximum difference is 127. */
2116 i = FONT_SIZE_INDEX;
2117 if (! NILP (spec_prop[FONT_SIZE_INDEX])
2118 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
2119 {
2120 /* We use the higher 6-bit for the actual size difference. The
2121 lowest bit is set if the DPI is different. */
2122 int diff;
2123 int pixel_size = XINT (spec_prop[FONT_SIZE_INDEX]);
2124
2125 if (CONSP (Vface_font_rescale_alist))
2126 pixel_size *= font_rescale_ratio (entity);
2127 diff = pixel_size - XINT (AREF (entity, FONT_SIZE_INDEX));
2128 if (diff < 0)
2129 diff = - diff;
2130 diff <<= 1;
2131 if (! NILP (spec_prop[FONT_DPI_INDEX])
2132 && ! EQ (spec_prop[FONT_DPI_INDEX], AREF (entity, FONT_DPI_INDEX)))
2133 diff |= 1;
2134 if (! NILP (spec_prop[FONT_AVGWIDTH_INDEX])
2135 && ! EQ (spec_prop[FONT_AVGWIDTH_INDEX], AREF (entity, FONT_AVGWIDTH_INDEX)))
2136 diff |= 1;
2137 score |= min (diff, 127) << sort_shift_bits[FONT_SIZE_INDEX];
2138 }
2139
2140 return score;
2141 }
2142
2143
2144 /* Concatenate all elements of LIST into one vector. LIST is a list
2145 of font-entity vectors. */
2146
2147 static Lisp_Object
2148 font_vconcat_entity_vectors (Lisp_Object list)
2149 {
2150 int nargs = XINT (Flength (list));
2151 Lisp_Object *args = alloca (sizeof (Lisp_Object) * nargs);
2152 int i;
2153
2154 for (i = 0; i < nargs; i++, list = XCDR (list))
2155 args[i] = XCAR (list);
2156 return Fvconcat (nargs, args);
2157 }
2158
2159
2160 /* The structure for elements being sorted by qsort. */
2161 struct font_sort_data
2162 {
2163 unsigned score;
2164 int font_driver_preference;
2165 Lisp_Object entity;
2166 };
2167
2168
2169 /* The comparison function for qsort. */
2170
2171 static int
2172 font_compare (const void *d1, const void *d2)
2173 {
2174 const struct font_sort_data *data1 = d1;
2175 const struct font_sort_data *data2 = d2;
2176
2177 if (data1->score < data2->score)
2178 return -1;
2179 else if (data1->score > data2->score)
2180 return 1;
2181 return (data1->font_driver_preference - data2->font_driver_preference);
2182 }
2183
2184
2185 /* Sort each font-entity vector in LIST by closeness to font-spec PREFER.
2186 If PREFER specifies a point-size, calculate the corresponding
2187 pixel-size from QCdpi property of PREFER or from the Y-resolution
2188 of FRAME before sorting.
2189
2190 If BEST-ONLY is nonzero, return the best matching entity (that
2191 supports the character BEST-ONLY if BEST-ONLY is positive, or any
2192 if BEST-ONLY is negative). Otherwise, return the sorted result as
2193 a single vector of font-entities.
2194
2195 This function does no optimization for the case that the total
2196 number of elements is 1. The caller should avoid calling this in
2197 such a case. */
2198
2199 static Lisp_Object
2200 font_sort_entities (Lisp_Object list, Lisp_Object prefer, Lisp_Object frame, int best_only)
2201 {
2202 Lisp_Object prefer_prop[FONT_SPEC_MAX];
2203 int len, maxlen, i;
2204 struct font_sort_data *data;
2205 unsigned best_score;
2206 Lisp_Object best_entity;
2207 struct frame *f = XFRAME (frame);
2208 Lisp_Object tail, vec;
2209 USE_SAFE_ALLOCA;
2210
2211 for (i = FONT_WEIGHT_INDEX; i <= FONT_AVGWIDTH_INDEX; i++)
2212 prefer_prop[i] = AREF (prefer, i);
2213 if (FLOATP (prefer_prop[FONT_SIZE_INDEX]))
2214 prefer_prop[FONT_SIZE_INDEX]
2215 = make_number (font_pixel_size (XFRAME (frame), prefer));
2216
2217 if (NILP (XCDR (list)))
2218 {
2219 /* What we have to take care of is this single vector. */
2220 vec = XCAR (list);
2221 maxlen = ASIZE (vec);
2222 }
2223 else if (best_only)
2224 {
2225 /* We don't have to perform sort, so there's no need of creating
2226 a single vector. But, we must find the length of the longest
2227 vector. */
2228 maxlen = 0;
2229 for (tail = list; CONSP (tail); tail = XCDR (tail))
2230 if (maxlen < ASIZE (XCAR (tail)))
2231 maxlen = ASIZE (XCAR (tail));
2232 }
2233 else
2234 {
2235 /* We have to create a single vector to sort it. */
2236 vec = font_vconcat_entity_vectors (list);
2237 maxlen = ASIZE (vec);
2238 }
2239
2240 SAFE_ALLOCA (data, struct font_sort_data *, (sizeof *data) * maxlen);
2241 best_score = 0xFFFFFFFF;
2242 best_entity = Qnil;
2243
2244 for (tail = list; CONSP (tail); tail = XCDR (tail))
2245 {
2246 int font_driver_preference = 0;
2247 Lisp_Object current_font_driver;
2248
2249 if (best_only)
2250 vec = XCAR (tail);
2251 len = ASIZE (vec);
2252
2253 /* We are sure that the length of VEC > 0. */
2254 current_font_driver = AREF (AREF (vec, 0), FONT_TYPE_INDEX);
2255 /* Score the elements. */
2256 for (i = 0; i < len; i++)
2257 {
2258 data[i].entity = AREF (vec, i);
2259 data[i].score
2260 = ((best_only <= 0 || font_has_char (f, data[i].entity, best_only)
2261 > 0)
2262 ? font_score (data[i].entity, prefer_prop)
2263 : 0xFFFFFFFF);
2264 if (best_only && best_score > data[i].score)
2265 {
2266 best_score = data[i].score;
2267 best_entity = data[i].entity;
2268 if (best_score == 0)
2269 break;
2270 }
2271 if (! EQ (current_font_driver, AREF (AREF (vec, i), FONT_TYPE_INDEX)))
2272 {
2273 current_font_driver = AREF (AREF (vec, i), FONT_TYPE_INDEX);
2274 font_driver_preference++;
2275 }
2276 data[i].font_driver_preference = font_driver_preference;
2277 }
2278
2279 /* Sort if necessary. */
2280 if (! best_only)
2281 {
2282 qsort (data, len, sizeof *data, font_compare);
2283 for (i = 0; i < len; i++)
2284 ASET (vec, i, data[i].entity);
2285 break;
2286 }
2287 else
2288 vec = best_entity;
2289 }
2290
2291 SAFE_FREE ();
2292
2293 FONT_ADD_LOG ("sort-by", prefer, vec);
2294 return vec;
2295 }
2296
2297 \f
2298 /* API of Font Service Layer. */
2299
2300 /* Reflect ORDER (see the variable font_sort_order in xfaces.c) to
2301 sort_shift_bits. Finternal_set_font_selection_order calls this
2302 function with font_sort_order after setting up it. */
2303
2304 void
2305 font_update_sort_order (int *order)
2306 {
2307 int i, shift_bits;
2308
2309 for (i = 0, shift_bits = 23; i < 4; i++, shift_bits -= 7)
2310 {
2311 int xlfd_idx = order[i];
2312
2313 if (xlfd_idx == XLFD_WEIGHT_INDEX)
2314 sort_shift_bits[FONT_WEIGHT_INDEX] = shift_bits;
2315 else if (xlfd_idx == XLFD_SLANT_INDEX)
2316 sort_shift_bits[FONT_SLANT_INDEX] = shift_bits;
2317 else if (xlfd_idx == XLFD_SWIDTH_INDEX)
2318 sort_shift_bits[FONT_WIDTH_INDEX] = shift_bits;
2319 else
2320 sort_shift_bits[FONT_SIZE_INDEX] = shift_bits;
2321 }
2322 }
2323
2324 static int
2325 font_check_otf_features (Lisp_Object script, Lisp_Object langsys, Lisp_Object features, Lisp_Object table)
2326 {
2327 Lisp_Object val;
2328 int negative;
2329
2330 table = assq_no_quit (script, table);
2331 if (NILP (table))
2332 return 0;
2333 table = XCDR (table);
2334 if (! NILP (langsys))
2335 {
2336 table = assq_no_quit (langsys, table);
2337 if (NILP (table))
2338 return 0;
2339 }
2340 else
2341 {
2342 val = assq_no_quit (Qnil, table);
2343 if (NILP (val))
2344 table = XCAR (table);
2345 else
2346 table = val;
2347 }
2348 table = XCDR (table);
2349 for (negative = 0; CONSP (features); features = XCDR (features))
2350 {
2351 if (NILP (XCAR (features)))
2352 {
2353 negative = 1;
2354 continue;
2355 }
2356 if (NILP (Fmemq (XCAR (features), table)) != negative)
2357 return 0;
2358 }
2359 return 1;
2360 }
2361
2362 /* Check if OTF_CAPABILITY satisfies SPEC (otf-spec). */
2363
2364 static int
2365 font_check_otf (Lisp_Object spec, Lisp_Object otf_capability)
2366 {
2367 Lisp_Object script, langsys = Qnil, gsub = Qnil, gpos = Qnil;
2368
2369 script = XCAR (spec);
2370 spec = XCDR (spec);
2371 if (! NILP (spec))
2372 {
2373 langsys = XCAR (spec);
2374 spec = XCDR (spec);
2375 if (! NILP (spec))
2376 {
2377 gsub = XCAR (spec);
2378 spec = XCDR (spec);
2379 if (! NILP (spec))
2380 gpos = XCAR (spec);
2381 }
2382 }
2383
2384 if (! NILP (gsub) && ! font_check_otf_features (script, langsys, gsub,
2385 XCAR (otf_capability)))
2386 return 0;
2387 if (! NILP (gpos) && ! font_check_otf_features (script, langsys, gpos,
2388 XCDR (otf_capability)))
2389 return 0;
2390 return 1;
2391 }
2392
2393
2394
2395 /* Check if FONT (font-entity or font-object) matches with the font
2396 specification SPEC. */
2397
2398 int
2399 font_match_p (Lisp_Object spec, Lisp_Object font)
2400 {
2401 Lisp_Object prop[FONT_SPEC_MAX], *props;
2402 Lisp_Object extra, font_extra;
2403 int i;
2404
2405 for (i = FONT_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX; i++)
2406 if (! NILP (AREF (spec, i))
2407 && ! NILP (AREF (font, i))
2408 && ! EQ (AREF (spec, i), AREF (font, i)))
2409 return 0;
2410 props = XFONT_SPEC (spec)->props;
2411 if (FLOATP (props[FONT_SIZE_INDEX]))
2412 {
2413 for (i = FONT_FOUNDRY_INDEX; i < FONT_SIZE_INDEX; i++)
2414 prop[i] = AREF (spec, i);
2415 prop[FONT_SIZE_INDEX]
2416 = make_number (font_pixel_size (XFRAME (selected_frame), spec));
2417 props = prop;
2418 }
2419
2420 if (font_score (font, props) > 0)
2421 return 0;
2422 extra = AREF (spec, FONT_EXTRA_INDEX);
2423 font_extra = AREF (font, FONT_EXTRA_INDEX);
2424 for (; CONSP (extra); extra = XCDR (extra))
2425 {
2426 Lisp_Object key = XCAR (XCAR (extra));
2427 Lisp_Object val = XCDR (XCAR (extra)), val2;
2428
2429 if (EQ (key, QClang))
2430 {
2431 val2 = assq_no_quit (key, font_extra);
2432 if (NILP (val2))
2433 return 0;
2434 val2 = XCDR (val2);
2435 if (CONSP (val))
2436 {
2437 if (! CONSP (val2))
2438 return 0;
2439 while (CONSP (val))
2440 if (NILP (Fmemq (val, val2)))
2441 return 0;
2442 }
2443 else
2444 if (CONSP (val2)
2445 ? NILP (Fmemq (val, XCDR (val2)))
2446 : ! EQ (val, val2))
2447 return 0;
2448 }
2449 else if (EQ (key, QCscript))
2450 {
2451 val2 = assq_no_quit (val, Vscript_representative_chars);
2452 if (CONSP (val2))
2453 {
2454 val2 = XCDR (val2);
2455 if (CONSP (val2))
2456 {
2457 /* All characters in the list must be supported. */
2458 for (; CONSP (val2); val2 = XCDR (val2))
2459 {
2460 if (! NATNUMP (XCAR (val2)))
2461 continue;
2462 if (font_encode_char (font, XFASTINT (XCAR (val2)))
2463 == FONT_INVALID_CODE)
2464 return 0;
2465 }
2466 }
2467 else if (VECTORP (val2))
2468 {
2469 /* At most one character in the vector must be supported. */
2470 for (i = 0; i < ASIZE (val2); i++)
2471 {
2472 if (! NATNUMP (AREF (val2, i)))
2473 continue;
2474 if (font_encode_char (font, XFASTINT (AREF (val2, i)))
2475 != FONT_INVALID_CODE)
2476 break;
2477 }
2478 if (i == ASIZE (val2))
2479 return 0;
2480 }
2481 }
2482 }
2483 else if (EQ (key, QCotf))
2484 {
2485 struct font *fontp;
2486
2487 if (! FONT_OBJECT_P (font))
2488 return 0;
2489 fontp = XFONT_OBJECT (font);
2490 if (! fontp->driver->otf_capability)
2491 return 0;
2492 val2 = fontp->driver->otf_capability (fontp);
2493 if (NILP (val2) || ! font_check_otf (val, val2))
2494 return 0;
2495 }
2496 }
2497
2498 return 1;
2499 }
2500 \f
2501
2502 /* Font cache
2503
2504 Each font backend has the callback function get_cache, and it
2505 returns a cons cell of which cdr part can be freely used for
2506 caching fonts. The cons cell may be shared by multiple frames
2507 and/or multiple font drivers. So, we arrange the cdr part as this:
2508
2509 ((DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) ...)
2510
2511 where DRIVER-TYPE is a symbol such as `x', `xft', etc., NUM-FRAMES
2512 is a number frames sharing this cache, and FONT-CACHE-DATA is a
2513 cons (FONT-SPEC FONT-ENTITY ...). */
2514
2515 static void font_prepare_cache (FRAME_PTR, struct font_driver *);
2516 static void font_finish_cache (FRAME_PTR, struct font_driver *);
2517 static Lisp_Object font_get_cache (FRAME_PTR, struct font_driver *);
2518 static void font_clear_cache (FRAME_PTR, Lisp_Object,
2519 struct font_driver *);
2520
2521 static void
2522 font_prepare_cache (FRAME_PTR f, struct font_driver *driver)
2523 {
2524 Lisp_Object cache, val;
2525
2526 cache = driver->get_cache (f);
2527 val = XCDR (cache);
2528 while (CONSP (val) && ! EQ (XCAR (XCAR (val)), driver->type))
2529 val = XCDR (val);
2530 if (NILP (val))
2531 {
2532 val = Fcons (driver->type, Fcons (make_number (1), Qnil));
2533 XSETCDR (cache, Fcons (val, XCDR (cache)));
2534 }
2535 else
2536 {
2537 val = XCDR (XCAR (val));
2538 XSETCAR (val, make_number (XINT (XCAR (val)) + 1));
2539 }
2540 }
2541
2542
2543 static void
2544 font_finish_cache (FRAME_PTR f, struct font_driver *driver)
2545 {
2546 Lisp_Object cache, val, tmp;
2547
2548
2549 cache = driver->get_cache (f);
2550 val = XCDR (cache);
2551 while (CONSP (val) && ! EQ (XCAR (XCAR (val)), driver->type))
2552 cache = val, val = XCDR (val);
2553 font_assert (! NILP (val));
2554 tmp = XCDR (XCAR (val));
2555 XSETCAR (tmp, make_number (XINT (XCAR (tmp)) - 1));
2556 if (XINT (XCAR (tmp)) == 0)
2557 {
2558 font_clear_cache (f, XCAR (val), driver);
2559 XSETCDR (cache, XCDR (val));
2560 }
2561 }
2562
2563
2564 static Lisp_Object
2565 font_get_cache (FRAME_PTR f, struct font_driver *driver)
2566 {
2567 Lisp_Object val = driver->get_cache (f);
2568 Lisp_Object type = driver->type;
2569
2570 font_assert (CONSP (val));
2571 for (val = XCDR (val); ! EQ (XCAR (XCAR (val)), type); val = XCDR (val));
2572 font_assert (CONSP (val));
2573 /* VAL = ((DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) ...) */
2574 val = XCDR (XCAR (val));
2575 return val;
2576 }
2577
2578 static int num_fonts;
2579
2580 static void
2581 font_clear_cache (FRAME_PTR f, Lisp_Object cache, struct font_driver *driver)
2582 {
2583 Lisp_Object tail, elt;
2584 Lisp_Object tail2, entity;
2585
2586 /* CACHE = (DRIVER-TYPE NUM-FRAMES FONT-CACHE-DATA ...) */
2587 for (tail = XCDR (XCDR (cache)); CONSP (tail); tail = XCDR (tail))
2588 {
2589 elt = XCAR (tail);
2590 /* elt should have the form (FONT-SPEC FONT-ENTITY ...) */
2591 if (CONSP (elt) && FONT_SPEC_P (XCAR (elt)))
2592 {
2593 for (tail2 = XCDR (elt); CONSP (tail2); tail2 = XCDR (tail2))
2594 {
2595 entity = XCAR (tail2);
2596
2597 if (FONT_ENTITY_P (entity)
2598 && EQ (driver->type, AREF (entity, FONT_TYPE_INDEX)))
2599 {
2600 Lisp_Object objlist = AREF (entity, FONT_OBJLIST_INDEX);
2601
2602 for (; CONSP (objlist); objlist = XCDR (objlist))
2603 {
2604 Lisp_Object val = XCAR (objlist);
2605 struct font *font = XFONT_OBJECT (val);
2606
2607 if (! NILP (AREF (val, FONT_TYPE_INDEX)))
2608 {
2609 font_assert (font && driver == font->driver);
2610 driver->close (f, font);
2611 num_fonts--;
2612 }
2613 }
2614 if (driver->free_entity)
2615 driver->free_entity (entity);
2616 }
2617 }
2618 }
2619 }
2620 XSETCDR (cache, Qnil);
2621 }
2622 \f
2623
2624 static Lisp_Object scratch_font_spec, scratch_font_prefer;
2625
2626 /* Check each font-entity in VEC, and return a list of font-entities
2627 that satisfy this condition:
2628 (1) matches with SPEC and SIZE if SPEC is not nil, and
2629 (2) doesn't match with any regexps in Vface_ignored_fonts (if non-nil).
2630 */
2631
2632 Lisp_Object
2633 font_delete_unmatched (Lisp_Object vec, Lisp_Object spec, int size)
2634 {
2635 Lisp_Object entity, val;
2636 enum font_property_index prop;
2637 int i;
2638
2639 for (val = Qnil, i = ASIZE (vec) - 1; i >= 0; i--)
2640 {
2641 entity = AREF (vec, i);
2642 if (! NILP (Vface_ignored_fonts))
2643 {
2644 char name[256];
2645 Lisp_Object tail, regexp;
2646
2647 if (font_unparse_xlfd (entity, 0, name, 256) >= 0)
2648 {
2649 for (tail = Vface_ignored_fonts; CONSP (tail); tail = XCDR (tail))
2650 {
2651 regexp = XCAR (tail);
2652 if (STRINGP (regexp)
2653 && fast_c_string_match_ignore_case (regexp, name) >= 0)
2654 break;
2655 }
2656 if (CONSP (tail))
2657 continue;
2658 }
2659 }
2660 if (NILP (spec))
2661 {
2662 val = Fcons (entity, val);
2663 continue;
2664 }
2665 for (prop = FONT_WEIGHT_INDEX; prop < FONT_SIZE_INDEX; prop++)
2666 if (INTEGERP (AREF (spec, prop))
2667 && ((XINT (AREF (spec, prop)) >> 8)
2668 != (XINT (AREF (entity, prop)) >> 8)))
2669 prop = FONT_SPEC_MAX;
2670 if (prop < FONT_SPEC_MAX
2671 && size
2672 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
2673 {
2674 int diff = XINT (AREF (entity, FONT_SIZE_INDEX)) - size;
2675
2676 if (diff != 0
2677 && (diff < 0 ? -diff > FONT_PIXEL_SIZE_QUANTUM
2678 : diff > FONT_PIXEL_SIZE_QUANTUM))
2679 prop = FONT_SPEC_MAX;
2680 }
2681 if (prop < FONT_SPEC_MAX
2682 && INTEGERP (AREF (spec, FONT_DPI_INDEX))
2683 && INTEGERP (AREF (entity, FONT_DPI_INDEX))
2684 && XINT (AREF (entity, FONT_DPI_INDEX)) != 0
2685 && ! EQ (AREF (spec, FONT_DPI_INDEX), AREF (entity, FONT_DPI_INDEX)))
2686 prop = FONT_SPEC_MAX;
2687 if (prop < FONT_SPEC_MAX
2688 && INTEGERP (AREF (spec, FONT_AVGWIDTH_INDEX))
2689 && INTEGERP (AREF (entity, FONT_AVGWIDTH_INDEX))
2690 && XINT (AREF (entity, FONT_AVGWIDTH_INDEX)) != 0
2691 && ! EQ (AREF (spec, FONT_AVGWIDTH_INDEX),
2692 AREF (entity, FONT_AVGWIDTH_INDEX)))
2693 prop = FONT_SPEC_MAX;
2694 if (prop < FONT_SPEC_MAX)
2695 val = Fcons (entity, val);
2696 }
2697 return (Fvconcat (1, &val));
2698 }
2699
2700
2701 /* Return a list of vectors of font-entities matching with SPEC on
2702 FRAME. Each elements in the list is a vector of entities from the
2703 same font-driver. */
2704
2705 Lisp_Object
2706 font_list_entities (Lisp_Object frame, Lisp_Object spec)
2707 {
2708 FRAME_PTR f = XFRAME (frame);
2709 struct font_driver_list *driver_list = f->font_driver_list;
2710 Lisp_Object ftype, val;
2711 Lisp_Object list = Qnil;
2712 int size;
2713 int need_filtering = 0;
2714 int i;
2715
2716 font_assert (FONT_SPEC_P (spec));
2717
2718 if (INTEGERP (AREF (spec, FONT_SIZE_INDEX)))
2719 size = XINT (AREF (spec, FONT_SIZE_INDEX));
2720 else if (FLOATP (AREF (spec, FONT_SIZE_INDEX)))
2721 size = font_pixel_size (f, spec);
2722 else
2723 size = 0;
2724
2725 ftype = AREF (spec, FONT_TYPE_INDEX);
2726 for (i = FONT_FOUNDRY_INDEX; i <= FONT_REGISTRY_INDEX; i++)
2727 ASET (scratch_font_spec, i, AREF (spec, i));
2728 for (i = FONT_WEIGHT_INDEX; i < FONT_EXTRA_INDEX; i++)
2729 {
2730 ASET (scratch_font_spec, i, Qnil);
2731 if (! NILP (AREF (spec, i)))
2732 need_filtering = 1;
2733 if (i == FONT_DPI_INDEX)
2734 /* Skip FONT_SPACING_INDEX */
2735 i++;
2736 }
2737 ASET (scratch_font_spec, FONT_SPACING_INDEX, AREF (spec, FONT_SPACING_INDEX));
2738 ASET (scratch_font_spec, FONT_EXTRA_INDEX, AREF (spec, FONT_EXTRA_INDEX));
2739
2740 for (i = 0; driver_list; driver_list = driver_list->next)
2741 if (driver_list->on
2742 && (NILP (ftype) || EQ (driver_list->driver->type, ftype)))
2743 {
2744 Lisp_Object cache = font_get_cache (f, driver_list->driver);
2745
2746 ASET (scratch_font_spec, FONT_TYPE_INDEX, driver_list->driver->type);
2747 val = assoc_no_quit (scratch_font_spec, XCDR (cache));
2748 if (CONSP (val))
2749 val = XCDR (val);
2750 else
2751 {
2752 Lisp_Object copy;
2753
2754 val = driver_list->driver->list (frame, scratch_font_spec);
2755 if (NILP (val))
2756 val = null_vector;
2757 else
2758 val = Fvconcat (1, &val);
2759 copy = Fcopy_font_spec (scratch_font_spec);
2760 ASET (copy, FONT_TYPE_INDEX, driver_list->driver->type);
2761 XSETCDR (cache, Fcons (Fcons (copy, val), XCDR (cache)));
2762 }
2763 if (ASIZE (val) > 0
2764 && (need_filtering
2765 || ! NILP (Vface_ignored_fonts)))
2766 val = font_delete_unmatched (val, need_filtering ? spec : Qnil, size);
2767 if (ASIZE (val) > 0)
2768 list = Fcons (val, list);
2769 }
2770
2771 list = Fnreverse (list);
2772 FONT_ADD_LOG ("list", spec, list);
2773 return list;
2774 }
2775
2776
2777 /* Return a font entity matching with SPEC on FRAME. ATTRS, if non
2778 nil, is an array of face's attributes, which specifies preferred
2779 font-related attributes. */
2780
2781 static Lisp_Object
2782 font_matching_entity (FRAME_PTR f, Lisp_Object *attrs, Lisp_Object spec)
2783 {
2784 struct font_driver_list *driver_list = f->font_driver_list;
2785 Lisp_Object ftype, size, entity;
2786 Lisp_Object frame;
2787 Lisp_Object work = Fcopy_font_spec (spec);
2788
2789 XSETFRAME (frame, f);
2790 ftype = AREF (spec, FONT_TYPE_INDEX);
2791 size = AREF (spec, FONT_SIZE_INDEX);
2792
2793 if (FLOATP (size))
2794 ASET (work, FONT_SIZE_INDEX, make_number (font_pixel_size (f, spec)));
2795 FONT_SET_STYLE (work, FONT_WEIGHT_INDEX, attrs[LFACE_WEIGHT_INDEX]);
2796 FONT_SET_STYLE (work, FONT_SLANT_INDEX, attrs[LFACE_SLANT_INDEX]);
2797 FONT_SET_STYLE (work, FONT_WIDTH_INDEX, attrs[LFACE_SWIDTH_INDEX]);
2798
2799 entity = Qnil;
2800 for (; driver_list; driver_list = driver_list->next)
2801 if (driver_list->on
2802 && (NILP (ftype) || EQ (driver_list->driver->type, ftype)))
2803 {
2804 Lisp_Object cache = font_get_cache (f, driver_list->driver);
2805 Lisp_Object copy;
2806
2807 ASET (work, FONT_TYPE_INDEX, driver_list->driver->type);
2808 entity = assoc_no_quit (work, XCDR (cache));
2809 if (CONSP (entity))
2810 entity = XCDR (entity);
2811 else
2812 {
2813 entity = driver_list->driver->match (frame, work);
2814 copy = Fcopy_font_spec (work);
2815 ASET (copy, FONT_TYPE_INDEX, driver_list->driver->type);
2816 XSETCDR (cache, Fcons (Fcons (copy, entity), XCDR (cache)));
2817 }
2818 if (! NILP (entity))
2819 break;
2820 }
2821 FONT_ADD_LOG ("match", work, entity);
2822 return entity;
2823 }
2824
2825
2826 /* Open a font of ENTITY and PIXEL_SIZE on frame F, and return the
2827 opened font object. */
2828
2829 static Lisp_Object
2830 font_open_entity (FRAME_PTR f, Lisp_Object entity, int pixel_size)
2831 {
2832 struct font_driver_list *driver_list;
2833 Lisp_Object objlist, size, val, font_object;
2834 struct font *font;
2835 int min_width, height;
2836 int scaled_pixel_size;
2837
2838 font_assert (FONT_ENTITY_P (entity));
2839 size = AREF (entity, FONT_SIZE_INDEX);
2840 if (XINT (size) != 0)
2841 scaled_pixel_size = pixel_size = XINT (size);
2842 else if (CONSP (Vface_font_rescale_alist))
2843 scaled_pixel_size = pixel_size * font_rescale_ratio (entity);
2844
2845 val = AREF (entity, FONT_TYPE_INDEX);
2846 for (driver_list = f->font_driver_list;
2847 driver_list && ! EQ (driver_list->driver->type, val);
2848 driver_list = driver_list->next);
2849 if (! driver_list)
2850 return Qnil;
2851
2852 for (objlist = AREF (entity, FONT_OBJLIST_INDEX); CONSP (objlist);
2853 objlist = XCDR (objlist))
2854 {
2855 Lisp_Object fn = XCAR (objlist);
2856 if (! NILP (AREF (fn, FONT_TYPE_INDEX))
2857 && XFONT_OBJECT (fn)->pixel_size == pixel_size)
2858 {
2859 if (driver_list->driver->cached_font_ok == NULL
2860 || driver_list->driver->cached_font_ok (f, fn, entity))
2861 return fn;
2862 }
2863 }
2864
2865 font_object = driver_list->driver->open (f, entity, scaled_pixel_size);
2866 if (!NILP (font_object))
2867 ASET (font_object, FONT_SIZE_INDEX, make_number (pixel_size));
2868 FONT_ADD_LOG ("open", entity, font_object);
2869 if (NILP (font_object))
2870 return Qnil;
2871 ASET (entity, FONT_OBJLIST_INDEX,
2872 Fcons (font_object, AREF (entity, FONT_OBJLIST_INDEX)));
2873 num_fonts++;
2874
2875 font = XFONT_OBJECT (font_object);
2876 min_width = (font->min_width ? font->min_width
2877 : font->average_width ? font->average_width
2878 : font->space_width ? font->space_width
2879 : 1);
2880 height = (font->height ? font->height : 1);
2881 #ifdef HAVE_WINDOW_SYSTEM
2882 FRAME_X_DISPLAY_INFO (f)->n_fonts++;
2883 if (FRAME_X_DISPLAY_INFO (f)->n_fonts == 1)
2884 {
2885 FRAME_SMALLEST_CHAR_WIDTH (f) = min_width;
2886 FRAME_SMALLEST_FONT_HEIGHT (f) = height;
2887 fonts_changed_p = 1;
2888 }
2889 else
2890 {
2891 if (FRAME_SMALLEST_CHAR_WIDTH (f) > min_width)
2892 FRAME_SMALLEST_CHAR_WIDTH (f) = min_width, fonts_changed_p = 1;
2893 if (FRAME_SMALLEST_FONT_HEIGHT (f) > height)
2894 FRAME_SMALLEST_FONT_HEIGHT (f) = height, fonts_changed_p = 1;
2895 }
2896 #endif
2897
2898 return font_object;
2899 }
2900
2901
2902 /* Close FONT_OBJECT that is opened on frame F. */
2903
2904 void
2905 font_close_object (FRAME_PTR f, Lisp_Object font_object)
2906 {
2907 struct font *font = XFONT_OBJECT (font_object);
2908
2909 if (NILP (AREF (font_object, FONT_TYPE_INDEX)))
2910 /* Already closed. */
2911 return;
2912 FONT_ADD_LOG ("close", font_object, Qnil);
2913 font->driver->close (f, font);
2914 #ifdef HAVE_WINDOW_SYSTEM
2915 font_assert (FRAME_X_DISPLAY_INFO (f)->n_fonts);
2916 FRAME_X_DISPLAY_INFO (f)->n_fonts--;
2917 #endif
2918 num_fonts--;
2919 }
2920
2921
2922 /* Return 1 if FONT on F has a glyph for character C, 0 if not, -1 if
2923 FONT is a font-entity and it must be opened to check. */
2924
2925 int
2926 font_has_char (FRAME_PTR f, Lisp_Object font, int c)
2927 {
2928 struct font *fontp;
2929
2930 if (FONT_ENTITY_P (font))
2931 {
2932 Lisp_Object type = AREF (font, FONT_TYPE_INDEX);
2933 struct font_driver_list *driver_list;
2934
2935 for (driver_list = f->font_driver_list;
2936 driver_list && ! EQ (driver_list->driver->type, type);
2937 driver_list = driver_list->next);
2938 if (! driver_list)
2939 return 0;
2940 if (! driver_list->driver->has_char)
2941 return -1;
2942 return driver_list->driver->has_char (font, c);
2943 }
2944
2945 font_assert (FONT_OBJECT_P (font));
2946 fontp = XFONT_OBJECT (font);
2947 if (fontp->driver->has_char)
2948 {
2949 int result = fontp->driver->has_char (font, c);
2950
2951 if (result >= 0)
2952 return result;
2953 }
2954 return (fontp->driver->encode_char (fontp, c) != FONT_INVALID_CODE);
2955 }
2956
2957
2958 /* Return the glyph ID of FONT_OBJECT for character C. */
2959
2960 static unsigned
2961 font_encode_char (Lisp_Object font_object, int c)
2962 {
2963 struct font *font;
2964
2965 font_assert (FONT_OBJECT_P (font_object));
2966 font = XFONT_OBJECT (font_object);
2967 return font->driver->encode_char (font, c);
2968 }
2969
2970
2971 /* Return the name of FONT_OBJECT. */
2972
2973 Lisp_Object
2974 font_get_name (Lisp_Object font_object)
2975 {
2976 font_assert (FONT_OBJECT_P (font_object));
2977 return AREF (font_object, FONT_NAME_INDEX);
2978 }
2979
2980
2981 /* Return the specification of FONT_OBJECT. */
2982
2983 Lisp_Object
2984 font_get_spec (Lisp_Object font_object)
2985 {
2986 Lisp_Object spec = font_make_spec ();
2987 int i;
2988
2989 for (i = 0; i < FONT_SIZE_INDEX; i++)
2990 ASET (spec, i, AREF (font_object, i));
2991 ASET (spec, FONT_SIZE_INDEX,
2992 make_number (XFONT_OBJECT (font_object)->pixel_size));
2993 return spec;
2994 }
2995
2996
2997 /* Create a new font spec from FONT_NAME, and return it. If FONT_NAME
2998 could not be parsed by font_parse_name, return Qnil. */
2999
3000 Lisp_Object
3001 font_spec_from_name (Lisp_Object font_name)
3002 {
3003 Lisp_Object spec = Ffont_spec (0, NULL);
3004
3005 CHECK_STRING (font_name);
3006 if (font_parse_name ((char *) SDATA (font_name), spec) == -1)
3007 return Qnil;
3008 font_put_extra (spec, QCname, font_name);
3009 font_put_extra (spec, QCuser_spec, font_name);
3010 return spec;
3011 }
3012
3013
3014 void
3015 font_clear_prop (Lisp_Object *attrs, enum font_property_index prop)
3016 {
3017 Lisp_Object font = attrs[LFACE_FONT_INDEX];
3018
3019 if (! FONTP (font))
3020 return;
3021
3022 if (! NILP (Ffont_get (font, QCname)))
3023 {
3024 font = Fcopy_font_spec (font);
3025 font_put_extra (font, QCname, Qnil);
3026 }
3027
3028 if (NILP (AREF (font, prop))
3029 && prop != FONT_FAMILY_INDEX
3030 && prop != FONT_FOUNDRY_INDEX
3031 && prop != FONT_WIDTH_INDEX
3032 && prop != FONT_SIZE_INDEX)
3033 return;
3034 if (EQ (font, attrs[LFACE_FONT_INDEX]))
3035 font = Fcopy_font_spec (font);
3036 ASET (font, prop, Qnil);
3037 if (prop == FONT_FAMILY_INDEX || prop == FONT_FOUNDRY_INDEX)
3038 {
3039 if (prop == FONT_FAMILY_INDEX)
3040 {
3041 ASET (font, FONT_FOUNDRY_INDEX, Qnil);
3042 /* If we are setting the font family, we must also clear
3043 FONT_WIDTH_INDEX to avoid rejecting families that lack
3044 support for some widths. */
3045 ASET (font, FONT_WIDTH_INDEX, Qnil);
3046 }
3047 ASET (font, FONT_ADSTYLE_INDEX, Qnil);
3048 ASET (font, FONT_REGISTRY_INDEX, Qnil);
3049 ASET (font, FONT_SIZE_INDEX, Qnil);
3050 ASET (font, FONT_DPI_INDEX, Qnil);
3051 ASET (font, FONT_SPACING_INDEX, Qnil);
3052 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3053 }
3054 else if (prop == FONT_SIZE_INDEX)
3055 {
3056 ASET (font, FONT_DPI_INDEX, Qnil);
3057 ASET (font, FONT_SPACING_INDEX, Qnil);
3058 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3059 }
3060 else if (prop == FONT_WIDTH_INDEX)
3061 ASET (font, FONT_AVGWIDTH_INDEX, Qnil);
3062 attrs[LFACE_FONT_INDEX] = font;
3063 }
3064
3065 /* Selecte a font from ENTITIES (list of font-entity vectors) that
3066 supports C and matches best with ATTRS and PIXEL_SIZE. */
3067
3068 static Lisp_Object
3069 font_select_entity (Lisp_Object frame, Lisp_Object entities, Lisp_Object *attrs, int pixel_size, int c)
3070 {
3071 Lisp_Object font_entity;
3072 Lisp_Object prefer;
3073 int result, i;
3074 FRAME_PTR f = XFRAME (frame);
3075
3076 if (NILP (XCDR (entities))
3077 && ASIZE (XCAR (entities)) == 1)
3078 {
3079 font_entity = AREF (XCAR (entities), 0);
3080 if (c < 0
3081 || (result = font_has_char (f, font_entity, c)) > 0)
3082 return font_entity;
3083 return Qnil;
3084 }
3085
3086 /* Sort fonts by properties specified in ATTRS. */
3087 prefer = scratch_font_prefer;
3088
3089 for (i = FONT_WEIGHT_INDEX; i <= FONT_SIZE_INDEX; i++)
3090 ASET (prefer, i, Qnil);
3091 if (FONTP (attrs[LFACE_FONT_INDEX]))
3092 {
3093 Lisp_Object face_font = attrs[LFACE_FONT_INDEX];
3094
3095 for (i = FONT_WEIGHT_INDEX; i <= FONT_SIZE_INDEX; i++)
3096 ASET (prefer, i, AREF (face_font, i));
3097 }
3098 if (NILP (AREF (prefer, FONT_WEIGHT_INDEX)))
3099 FONT_SET_STYLE (prefer, FONT_WEIGHT_INDEX, attrs[LFACE_WEIGHT_INDEX]);
3100 if (NILP (AREF (prefer, FONT_SLANT_INDEX)))
3101 FONT_SET_STYLE (prefer, FONT_SLANT_INDEX, attrs[LFACE_SLANT_INDEX]);
3102 if (NILP (AREF (prefer, FONT_WIDTH_INDEX)))
3103 FONT_SET_STYLE (prefer, FONT_WIDTH_INDEX, attrs[LFACE_SWIDTH_INDEX]);
3104 ASET (prefer, FONT_SIZE_INDEX, make_number (pixel_size));
3105
3106 return font_sort_entities (entities, prefer, frame, c);
3107 }
3108
3109 /* Return a font-entity satisfying SPEC and best matching with face's
3110 font related attributes in ATTRS. C, if not negative, is a
3111 character that the entity must support. */
3112
3113 Lisp_Object
3114 font_find_for_lface (FRAME_PTR f, Lisp_Object *attrs, Lisp_Object spec, int c)
3115 {
3116 Lisp_Object work;
3117 Lisp_Object frame, entities, val;
3118 Lisp_Object size, foundry[3], *family, registry[3], adstyle[3];
3119 int pixel_size;
3120 int i, j, k, l;
3121
3122 registry[0] = AREF (spec, FONT_REGISTRY_INDEX);
3123 if (NILP (registry[0]))
3124 {
3125 registry[0] = DEFAULT_ENCODING;
3126 registry[1] = Qascii_0;
3127 registry[2] = null_vector;
3128 }
3129 else
3130 registry[1] = null_vector;
3131
3132 if (c >= 0 && ! NILP (AREF (spec, FONT_REGISTRY_INDEX)))
3133 {
3134 struct charset *encoding, *repertory;
3135
3136 if (font_registry_charsets (AREF (spec, FONT_REGISTRY_INDEX),
3137 &encoding, &repertory) < 0)
3138 return Qnil;
3139 if (repertory
3140 && ENCODE_CHAR (repertory, c) == CHARSET_INVALID_CODE (repertory))
3141 return Qnil;
3142 else if (c > encoding->max_char)
3143 return Qnil;
3144 }
3145
3146 work = Fcopy_font_spec (spec);
3147 ASET (work, FONT_TYPE_INDEX, AREF (spec, FONT_TYPE_INDEX));
3148 XSETFRAME (frame, f);
3149 size = AREF (spec, FONT_SIZE_INDEX);
3150 pixel_size = font_pixel_size (f, spec);
3151 if (pixel_size == 0)
3152 {
3153 double pt = XINT (attrs[LFACE_HEIGHT_INDEX]);
3154
3155 pixel_size = POINT_TO_PIXEL (pt / 10, f->resy);
3156 }
3157 ASET (work, FONT_SIZE_INDEX, Qnil);
3158 foundry[0] = AREF (work, FONT_FOUNDRY_INDEX);
3159 if (! NILP (foundry[0]))
3160 foundry[1] = null_vector;
3161 else if (STRINGP (attrs[LFACE_FOUNDRY_INDEX]))
3162 {
3163 val = attrs[LFACE_FOUNDRY_INDEX];
3164 foundry[0] = font_intern_prop ((char *) SDATA (val), SBYTES (val), 1);
3165 foundry[1] = Qnil;
3166 foundry[2] = null_vector;
3167 }
3168 else
3169 foundry[0] = Qnil, foundry[1] = null_vector;
3170
3171 adstyle[0] = AREF (work, FONT_ADSTYLE_INDEX);
3172 if (! NILP (adstyle[0]))
3173 adstyle[1] = null_vector;
3174 else if (FONTP (attrs[LFACE_FONT_INDEX]))
3175 {
3176 Lisp_Object face_font = attrs[LFACE_FONT_INDEX];
3177
3178 if (! NILP (AREF (face_font, FONT_ADSTYLE_INDEX)))
3179 {
3180 adstyle[0] = AREF (face_font, FONT_ADSTYLE_INDEX);
3181 adstyle[1] = Qnil;
3182 adstyle[2] = null_vector;
3183 }
3184 else
3185 adstyle[0] = Qnil, adstyle[1] = null_vector;
3186 }
3187 else
3188 adstyle[0] = Qnil, adstyle[1] = null_vector;
3189
3190
3191 val = AREF (work, FONT_FAMILY_INDEX);
3192 if (NILP (val) && STRINGP (attrs[LFACE_FAMILY_INDEX]))
3193 {
3194 val = attrs[LFACE_FAMILY_INDEX];
3195 val = font_intern_prop ((char *) SDATA (val), SBYTES (val), 1);
3196 }
3197 if (NILP (val))
3198 {
3199 family = alloca ((sizeof family[0]) * 2);
3200 family[0] = Qnil;
3201 family[1] = null_vector; /* terminator. */
3202 }
3203 else
3204 {
3205 Lisp_Object alters
3206 = Fassoc_string (val, Vface_alternative_font_family_alist,
3207 /* Font family names are case-sensitive under NS. */
3208 #ifndef HAVE_NS
3209 Qt
3210 #else
3211 Qnil
3212 #endif
3213 );
3214
3215 if (! NILP (alters))
3216 {
3217 family = alloca ((sizeof family[0]) * (XINT (Flength (alters)) + 2));
3218 for (i = 0; CONSP (alters); i++, alters = XCDR (alters))
3219 family[i] = XCAR (alters);
3220 if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
3221 family[i++] = Qnil;
3222 family[i] = null_vector;
3223 }
3224 else
3225 {
3226 family = alloca ((sizeof family[0]) * 3);
3227 i = 0;
3228 family[i++] = val;
3229 if (NILP (AREF (spec, FONT_FAMILY_INDEX)))
3230 family[i++] = Qnil;
3231 family[i] = null_vector;
3232 }
3233 }
3234
3235 for (i = 0; SYMBOLP (family[i]); i++)
3236 {
3237 ASET (work, FONT_FAMILY_INDEX, family[i]);
3238 for (j = 0; SYMBOLP (foundry[j]); j++)
3239 {
3240 ASET (work, FONT_FOUNDRY_INDEX, foundry[j]);
3241 for (k = 0; SYMBOLP (registry[k]); k++)
3242 {
3243 ASET (work, FONT_REGISTRY_INDEX, registry[k]);
3244 for (l = 0; SYMBOLP (adstyle[l]); l++)
3245 {
3246 ASET (work, FONT_ADSTYLE_INDEX, adstyle[l]);
3247 entities = font_list_entities (frame, work);
3248 if (! NILP (entities))
3249 {
3250 val = font_select_entity (frame, entities,
3251 attrs, pixel_size, c);
3252 if (! NILP (val))
3253 return val;
3254 }
3255 }
3256 }
3257 }
3258 }
3259 return Qnil;
3260 }
3261
3262
3263 Lisp_Object
3264 font_open_for_lface (FRAME_PTR f, Lisp_Object entity, Lisp_Object *attrs, Lisp_Object spec)
3265 {
3266 int size;
3267
3268 if (INTEGERP (AREF (entity, FONT_SIZE_INDEX))
3269 && XINT (AREF (entity, FONT_SIZE_INDEX)) > 0)
3270 size = XINT (AREF (entity, FONT_SIZE_INDEX));
3271 else if (FONT_SPEC_P (spec) && ! NILP (AREF (spec, FONT_SIZE_INDEX)))
3272 size = font_pixel_size (f, spec);
3273 else
3274 {
3275 double pt;
3276 if (INTEGERP (attrs[LFACE_HEIGHT_INDEX]))
3277 pt = XINT (attrs[LFACE_HEIGHT_INDEX]);
3278 else
3279 {
3280 struct face *def = FACE_FROM_ID (f, DEFAULT_FACE_ID);
3281 Lisp_Object height = def->lface[LFACE_HEIGHT_INDEX];
3282 if (INTEGERP (height))
3283 pt = XINT (height);
3284 else
3285 abort(); /* We should never end up here. */
3286 }
3287
3288 pt /= 10;
3289 size = POINT_TO_PIXEL (pt, f->resy);
3290 #ifdef HAVE_NS
3291 if (size == 0)
3292 {
3293 Lisp_Object ffsize = get_frame_param(f, Qfontsize);
3294 size = NUMBERP (ffsize) ? POINT_TO_PIXEL (XINT (ffsize), f->resy) : 0;
3295 }
3296 #endif
3297 }
3298 return font_open_entity (f, entity, size);
3299 }
3300
3301
3302 /* Find a font satisfying SPEC and best matching with face's
3303 attributes in ATTRS on FRAME, and return the opened
3304 font-object. */
3305
3306 Lisp_Object
3307 font_load_for_lface (FRAME_PTR f, Lisp_Object *attrs, Lisp_Object spec)
3308 {
3309 Lisp_Object entity, name;
3310
3311 entity = font_find_for_lface (f, attrs, spec, -1);
3312 if (NILP (entity))
3313 {
3314 /* No font is listed for SPEC, but each font-backend may have
3315 the different criteria about "font matching". So, try
3316 it. */
3317 entity = font_matching_entity (f, attrs, spec);
3318 if (NILP (entity))
3319 return Qnil;
3320 }
3321 /* Don't lose the original name that was put in initially. We need
3322 it to re-apply the font when font parameters (like hinting or dpi) have
3323 changed. */
3324 entity = font_open_for_lface (f, entity, attrs, spec);
3325 if (!NILP (entity))
3326 {
3327 name = Ffont_get (spec, QCuser_spec);
3328 if (STRINGP (name)) font_put_extra (entity, QCuser_spec, name);
3329 }
3330 return entity;
3331 }
3332
3333
3334 /* Make FACE on frame F ready to use the font opened for FACE. */
3335
3336 void
3337 font_prepare_for_face (FRAME_PTR f, struct face *face)
3338 {
3339 if (face->font->driver->prepare_face)
3340 face->font->driver->prepare_face (f, face);
3341 }
3342
3343
3344 /* Make FACE on frame F stop using the font opened for FACE. */
3345
3346 void
3347 font_done_for_face (FRAME_PTR f, struct face *face)
3348 {
3349 if (face->font->driver->done_face)
3350 face->font->driver->done_face (f, face);
3351 face->extra = NULL;
3352 }
3353
3354
3355 /* Open a font matching with font-spec SPEC on frame F. If no proper
3356 font is found, return Qnil. */
3357
3358 Lisp_Object
3359 font_open_by_spec (FRAME_PTR f, Lisp_Object spec)
3360 {
3361 Lisp_Object attrs[LFACE_VECTOR_SIZE];
3362
3363 /* We set up the default font-related attributes of a face to prefer
3364 a moderate font. */
3365 attrs[LFACE_FAMILY_INDEX] = attrs[LFACE_FOUNDRY_INDEX] = Qnil;
3366 attrs[LFACE_SWIDTH_INDEX] = attrs[LFACE_WEIGHT_INDEX]
3367 = attrs[LFACE_SLANT_INDEX] = Qnormal;
3368 #ifndef HAVE_NS
3369 attrs[LFACE_HEIGHT_INDEX] = make_number (120);
3370 #else
3371 attrs[LFACE_HEIGHT_INDEX] = make_number (0);
3372 #endif
3373 attrs[LFACE_FONT_INDEX] = Qnil;
3374
3375 return font_load_for_lface (f, attrs, spec);
3376 }
3377
3378
3379 /* Open a font matching with NAME on frame F. If no proper font is
3380 found, return Qnil. */
3381
3382 Lisp_Object
3383 font_open_by_name (FRAME_PTR f, const char *name)
3384 {
3385 Lisp_Object args[2];
3386 Lisp_Object spec, ret;
3387
3388 args[0] = QCname;
3389 args[1] = make_unibyte_string (name, strlen (name));
3390 spec = Ffont_spec (2, args);
3391 ret = font_open_by_spec (f, spec);
3392 /* Do not lose name originally put in. */
3393 if (!NILP (ret))
3394 font_put_extra (ret, QCuser_spec, args[1]);
3395
3396 return ret;
3397 }
3398
3399
3400 /* Register font-driver DRIVER. This function is used in two ways.
3401
3402 The first is with frame F non-NULL. In this case, make DRIVER
3403 available (but not yet activated) on F. All frame creaters
3404 (e.g. Fx_create_frame) must call this function at least once with
3405 an available font-driver.
3406
3407 The second is with frame F NULL. In this case, DRIVER is globally
3408 registered in the variable `font_driver_list'. All font-driver
3409 implementations must call this function in its syms_of_XXXX
3410 (e.g. syms_of_xfont). */
3411
3412 void
3413 register_font_driver (struct font_driver *driver, FRAME_PTR f)
3414 {
3415 struct font_driver_list *root = f ? f->font_driver_list : font_driver_list;
3416 struct font_driver_list *prev, *list;
3417
3418 if (f && ! driver->draw)
3419 error ("Unusable font driver for a frame: %s",
3420 SDATA (SYMBOL_NAME (driver->type)));
3421
3422 for (prev = NULL, list = root; list; prev = list, list = list->next)
3423 if (EQ (list->driver->type, driver->type))
3424 error ("Duplicated font driver: %s", SDATA (SYMBOL_NAME (driver->type)));
3425
3426 list = xmalloc (sizeof (struct font_driver_list));
3427 list->on = 0;
3428 list->driver = driver;
3429 list->next = NULL;
3430 if (prev)
3431 prev->next = list;
3432 else if (f)
3433 f->font_driver_list = list;
3434 else
3435 font_driver_list = list;
3436 if (! f)
3437 num_font_drivers++;
3438 }
3439
3440 void
3441 free_font_driver_list (FRAME_PTR f)
3442 {
3443 struct font_driver_list *list, *next;
3444
3445 for (list = f->font_driver_list; list; list = next)
3446 {
3447 next = list->next;
3448 xfree (list);
3449 }
3450 f->font_driver_list = NULL;
3451 }
3452
3453
3454 /* Make the frame F use font backends listed in NEW_DRIVERS (list of
3455 symbols, e.g. xft, x). If NEW_DRIVERS is t, make F use all
3456 available font drivers. If NEW_DRIVERS is nil, finalize all drivers.
3457
3458 A caller must free all realized faces if any in advance. The
3459 return value is a list of font backends actually made used on
3460 F. */
3461
3462 Lisp_Object
3463 font_update_drivers (FRAME_PTR f, Lisp_Object new_drivers)
3464 {
3465 Lisp_Object active_drivers = Qnil;
3466 struct font_driver *driver;
3467 struct font_driver_list *list;
3468
3469 /* At first, turn off non-requested drivers, and turn on requested
3470 drivers. */
3471 for (list = f->font_driver_list; list; list = list->next)
3472 {
3473 driver = list->driver;
3474 if ((EQ (new_drivers, Qt) || ! NILP (Fmemq (driver->type, new_drivers)))
3475 != list->on)
3476 {
3477 if (list->on)
3478 {
3479 if (driver->end_for_frame)
3480 driver->end_for_frame (f);
3481 font_finish_cache (f, driver);
3482 list->on = 0;
3483 }
3484 else
3485 {
3486 if (! driver->start_for_frame
3487 || driver->start_for_frame (f) == 0)
3488 {
3489 font_prepare_cache (f, driver);
3490 list->on = 1;
3491 }
3492 }
3493 }
3494 }
3495
3496 if (NILP (new_drivers))
3497 return Qnil;
3498
3499 if (! EQ (new_drivers, Qt))
3500 {
3501 /* Re-order the driver list according to new_drivers. */
3502 struct font_driver_list **list_table, **next;
3503 Lisp_Object tail;
3504 int i;
3505
3506 list_table = alloca (sizeof list_table[0] * (num_font_drivers + 1));
3507 for (i = 0, tail = new_drivers; ! NILP (tail); tail = XCDR (tail))
3508 {
3509 for (list = f->font_driver_list; list; list = list->next)
3510 if (list->on && EQ (list->driver->type, XCAR (tail)))
3511 break;
3512 if (list)
3513 list_table[i++] = list;
3514 }
3515 for (list = f->font_driver_list; list; list = list->next)
3516 if (! list->on)
3517 list_table[i++] = list;
3518 list_table[i] = NULL;
3519
3520 next = &f->font_driver_list;
3521 for (i = 0; list_table[i]; i++)
3522 {
3523 *next = list_table[i];
3524 next = &(*next)->next;
3525 }
3526 *next = NULL;
3527
3528 if (! f->font_driver_list->on)
3529 { /* None of the drivers is enabled: enable them all.
3530 Happens if you set the list of drivers to (xft x) in your .emacs
3531 and then use it under w32 or ns. */
3532 for (list = f->font_driver_list; list; list = list->next)
3533 {
3534 struct font_driver *driver = list->driver;
3535 eassert (! list->on);
3536 if (! driver->start_for_frame
3537 || driver->start_for_frame (f) == 0)
3538 {
3539 font_prepare_cache (f, driver);
3540 list->on = 1;
3541 }
3542 }
3543 }
3544 }
3545
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;
3551 }
3552
3553 int
3554 font_put_frame_data (FRAME_PTR f, struct font_driver *driver, void *data)
3555 {
3556 struct font_data_list *list, *prev;
3557
3558 for (prev = NULL, list = f->font_data_list; list;
3559 prev = list, list = list->next)
3560 if (list->driver == driver)
3561 break;
3562 if (! data)
3563 {
3564 if (list)
3565 {
3566 if (prev)
3567 prev->next = list->next;
3568 else
3569 f->font_data_list = list->next;
3570 xfree (list);
3571 }
3572 return 0;
3573 }
3574
3575 if (! list)
3576 {
3577 list = xmalloc (sizeof (struct font_data_list));
3578 list->driver = driver;
3579 list->next = f->font_data_list;
3580 f->font_data_list = list;
3581 }
3582 list->data = data;
3583 return 0;
3584 }
3585
3586
3587 void *
3588 font_get_frame_data (FRAME_PTR f, struct font_driver *driver)
3589 {
3590 struct font_data_list *list;
3591
3592 for (list = f->font_data_list; list; list = list->next)
3593 if (list->driver == driver)
3594 break;
3595 if (! list)
3596 return NULL;
3597 return list->data;
3598 }
3599
3600
3601 /* Sets attributes on a font. Any properties that appear in ALIST and
3602 BOOLEAN_PROPERTIES or NON_BOOLEAN_PROPERTIES are set on the font.
3603 BOOLEAN_PROPERTIES and NON_BOOLEAN_PROPERTIES are NULL-terminated
3604 arrays of strings. This function is intended for use by the font
3605 drivers to implement their specific font_filter_properties. */
3606 void
3607 font_filter_properties (Lisp_Object font,
3608 Lisp_Object alist,
3609 const char *const boolean_properties[],
3610 const char *const non_boolean_properties[])
3611 {
3612 Lisp_Object it;
3613 int i;
3614
3615 /* Set boolean values to Qt or Qnil */
3616 for (i = 0; boolean_properties[i] != NULL; ++i)
3617 for (it = alist; ! NILP (it); it = XCDR (it))
3618 {
3619 Lisp_Object key = XCAR (XCAR (it));
3620 Lisp_Object val = XCDR (XCAR (it));
3621 char *keystr = SDATA (SYMBOL_NAME (key));
3622
3623 if (strcmp (boolean_properties[i], keystr) == 0)
3624 {
3625 const char *str = INTEGERP (val) ? (XINT (val) ? "true" : "false")
3626 : SYMBOLP (val) ? (const char *) SDATA (SYMBOL_NAME (val))
3627 : "true";
3628
3629 if (strcmp ("false", str) == 0 || strcmp ("False", str) == 0
3630 || strcmp ("FALSE", str) == 0 || strcmp ("FcFalse", str) == 0
3631 || strcmp ("off", str) == 0 || strcmp ("OFF", str) == 0
3632 || strcmp ("Off", str) == 0)
3633 val = Qnil;
3634 else
3635 val = Qt;
3636
3637 Ffont_put (font, key, val);
3638 }
3639 }
3640
3641 for (i = 0; non_boolean_properties[i] != NULL; ++i)
3642 for (it = alist; ! NILP (it); it = XCDR (it))
3643 {
3644 Lisp_Object key = XCAR (XCAR (it));
3645 Lisp_Object val = XCDR (XCAR (it));
3646 char *keystr = SDATA (SYMBOL_NAME (key));
3647 if (strcmp (non_boolean_properties[i], keystr) == 0)
3648 Ffont_put (font, key, val);
3649 }
3650 }
3651
3652
3653 /* Return the font used to draw character C by FACE at buffer position
3654 POS in window W. If STRING is non-nil, it is a string containing C
3655 at index POS. If C is negative, get C from the current buffer or
3656 STRING. */
3657
3658 static Lisp_Object
3659 font_at (int c, EMACS_INT pos, struct face *face, struct window *w,
3660 Lisp_Object string)
3661 {
3662 FRAME_PTR f;
3663 int multibyte;
3664 Lisp_Object font_object;
3665
3666 multibyte = (NILP (string)
3667 ? ! NILP (current_buffer->enable_multibyte_characters)
3668 : STRING_MULTIBYTE (string));
3669 if (c < 0)
3670 {
3671 if (NILP (string))
3672 {
3673 if (multibyte)
3674 {
3675 EMACS_INT pos_byte = CHAR_TO_BYTE (pos);
3676
3677 c = FETCH_CHAR (pos_byte);
3678 }
3679 else
3680 c = FETCH_BYTE (pos);
3681 }
3682 else
3683 {
3684 unsigned char *str;
3685
3686 multibyte = STRING_MULTIBYTE (string);
3687 if (multibyte)
3688 {
3689 EMACS_INT pos_byte = string_char_to_byte (string, pos);
3690
3691 str = SDATA (string) + pos_byte;
3692 c = STRING_CHAR (str);
3693 }
3694 else
3695 c = SDATA (string)[pos];
3696 }
3697 }
3698
3699 f = XFRAME (w->frame);
3700 if (! FRAME_WINDOW_P (f))
3701 return Qnil;
3702 if (! face)
3703 {
3704 int face_id;
3705 EMACS_INT endptr;
3706
3707 if (STRINGP (string))
3708 face_id = face_at_string_position (w, string, pos, 0, -1, -1, &endptr,
3709 DEFAULT_FACE_ID, 0);
3710 else
3711 face_id = face_at_buffer_position (w, pos, -1, -1, &endptr,
3712 pos + 100, 0, -1);
3713 face = FACE_FROM_ID (f, face_id);
3714 }
3715 if (multibyte)
3716 {
3717 int face_id = FACE_FOR_CHAR (f, face, c, pos, string);
3718 face = FACE_FROM_ID (f, face_id);
3719 }
3720 if (! face->font)
3721 return Qnil;
3722
3723 XSETFONT (font_object, face->font);
3724 return font_object;
3725 }
3726
3727
3728 #ifdef HAVE_WINDOW_SYSTEM
3729
3730 /* Check how many characters after POS (at most to *LIMIT) can be
3731 displayed by the same font on the window W. FACE, if non-NULL, is
3732 the face selected for the character at POS. If STRING is not nil,
3733 it is the string to check instead of the current buffer. In that
3734 case, FACE must be not NULL.
3735
3736 The return value is the font-object for the character at POS.
3737 *LIMIT is set to the position where that font can't be used.
3738
3739 It is assured that the current buffer (or STRING) is multibyte. */
3740
3741 Lisp_Object
3742 font_range (EMACS_INT pos, EMACS_INT *limit, struct window *w, struct face *face, Lisp_Object string)
3743 {
3744 EMACS_INT pos_byte, ignore;
3745 int c;
3746 Lisp_Object font_object = Qnil;
3747
3748 if (NILP (string))
3749 {
3750 pos_byte = CHAR_TO_BYTE (pos);
3751 if (! face)
3752 {
3753 int face_id;
3754
3755 face_id = face_at_buffer_position (w, pos, 0, 0, &ignore,
3756 *limit, 0, -1);
3757 face = FACE_FROM_ID (XFRAME (w->frame), face_id);
3758 }
3759 }
3760 else
3761 {
3762 font_assert (face);
3763 pos_byte = string_char_to_byte (string, pos);
3764 }
3765
3766 while (pos < *limit)
3767 {
3768 Lisp_Object category;
3769
3770 if (NILP (string))
3771 FETCH_CHAR_ADVANCE_NO_CHECK (c, pos, pos_byte);
3772 else
3773 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, string, pos, pos_byte);
3774 category = CHAR_TABLE_REF (Vunicode_category_table, c);
3775 if (EQ (category, QCf)
3776 || CHAR_VARIATION_SELECTOR_P (c))
3777 continue;
3778 if (NILP (font_object))
3779 {
3780 font_object = font_for_char (face, c, pos - 1, string);
3781 if (NILP (font_object))
3782 return Qnil;
3783 continue;
3784 }
3785 if (font_encode_char (font_object, c) == FONT_INVALID_CODE)
3786 *limit = pos - 1;
3787 }
3788 return font_object;
3789 }
3790 #endif
3791
3792 \f
3793 /* Lisp API */
3794
3795 DEFUN ("fontp", Ffontp, Sfontp, 1, 2, 0,
3796 doc: /* Return t if OBJECT is a font-spec, font-entity, or font-object.
3797 Return nil otherwise.
3798 Optional 2nd argument EXTRA-TYPE, if non-nil, specifies to check
3799 which kind of font it is. It must be one of `font-spec', `font-entity',
3800 `font-object'. */)
3801 (Lisp_Object object, Lisp_Object extra_type)
3802 {
3803 if (NILP (extra_type))
3804 return (FONTP (object) ? Qt : Qnil);
3805 if (EQ (extra_type, Qfont_spec))
3806 return (FONT_SPEC_P (object) ? Qt : Qnil);
3807 if (EQ (extra_type, Qfont_entity))
3808 return (FONT_ENTITY_P (object) ? Qt : Qnil);
3809 if (EQ (extra_type, Qfont_object))
3810 return (FONT_OBJECT_P (object) ? Qt : Qnil);
3811 wrong_type_argument (intern ("font-extra-type"), extra_type);
3812 }
3813
3814 DEFUN ("font-spec", Ffont_spec, Sfont_spec, 0, MANY, 0,
3815 doc: /* Return a newly created font-spec with arguments as properties.
3816
3817 ARGS must come in pairs KEY VALUE of font properties. KEY must be a
3818 valid font property name listed below:
3819
3820 `:family', `:weight', `:slant', `:width'
3821
3822 They are the same as face attributes of the same name. See
3823 `set-face-attribute'.
3824
3825 `:foundry'
3826
3827 VALUE must be a string or a symbol specifying the font foundry, e.g. ``misc''.
3828
3829 `:adstyle'
3830
3831 VALUE must be a string or a symbol specifying the additional
3832 typographic style information of a font, e.g. ``sans''.
3833
3834 `:registry'
3835
3836 VALUE must be a string or a symbol specifying the charset registry and
3837 encoding of a font, e.g. ``iso8859-1''.
3838
3839 `:size'
3840
3841 VALUE must be a non-negative integer or a floating point number
3842 specifying the font size. It specifies the font size in pixels (if
3843 VALUE is an integer), or in points (if VALUE is a float).
3844
3845 `:name'
3846
3847 VALUE must be a string of XLFD-style or fontconfig-style font name.
3848
3849 `:script'
3850
3851 VALUE must be a symbol representing a script that the font must
3852 support. It may be a symbol representing a subgroup of a script
3853 listed in the variable `script-representative-chars'.
3854
3855 `:lang'
3856
3857 VALUE must be a symbol of two-letter ISO-639 language names,
3858 e.g. `ja'.
3859
3860 `:otf'
3861
3862 VALUE must be a list (SCRIPT-TAG LANGSYS-TAG GSUB [ GPOS ]) to specify
3863 required OpenType features.
3864
3865 SCRIPT-TAG: OpenType script tag symbol (e.g. `deva').
3866 LANGSYS-TAG: OpenType language system tag symbol,
3867 or nil for the default language system.
3868 GSUB: List of OpenType GSUB feature tag symbols, or nil if none required.
3869 GPOS: List of OpenType GPOS feature tag symbols, or nil if none required.
3870
3871 GSUB and GPOS may contain `nil' element. In such a case, the font
3872 must not have any of the remaining elements.
3873
3874 For instance, if the VALUE is `(thai nil nil (mark))', the font must
3875 be an OpenType font, and whose GPOS table of `thai' script's default
3876 language system must contain `mark' feature.
3877
3878 usage: (font-spec ARGS...) */)
3879 (int nargs, Lisp_Object *args)
3880 {
3881 Lisp_Object spec = font_make_spec ();
3882 int i;
3883
3884 for (i = 0; i < nargs; i += 2)
3885 {
3886 Lisp_Object key = args[i], val;
3887
3888 CHECK_SYMBOL (key);
3889 if (i + 1 >= nargs)
3890 error ("No value for key `%s'", SDATA (SYMBOL_NAME (key)));
3891 val = args[i + 1];
3892
3893 if (EQ (key, QCname))
3894 {
3895 CHECK_STRING (val);
3896 font_parse_name ((char *) SDATA (val), spec);
3897 font_put_extra (spec, key, val);
3898 }
3899 else
3900 {
3901 int idx = get_font_prop_index (key);
3902
3903 if (idx >= 0)
3904 {
3905 val = font_prop_validate (idx, Qnil, val);
3906 if (idx < FONT_EXTRA_INDEX)
3907 ASET (spec, idx, val);
3908 else
3909 font_put_extra (spec, key, val);
3910 }
3911 else
3912 font_put_extra (spec, key, font_prop_validate (0, key, val));
3913 }
3914 }
3915 return spec;
3916 }
3917
3918 DEFUN ("copy-font-spec", Fcopy_font_spec, Scopy_font_spec, 1, 1, 0,
3919 doc: /* Return a copy of FONT as a font-spec. */)
3920 (Lisp_Object font)
3921 {
3922 Lisp_Object new_spec, tail, prev, extra;
3923 int i;
3924
3925 CHECK_FONT (font);
3926 new_spec = font_make_spec ();
3927 for (i = 1; i < FONT_EXTRA_INDEX; i++)
3928 ASET (new_spec, i, AREF (font, i));
3929 extra = Fcopy_alist (AREF (font, FONT_EXTRA_INDEX));
3930 /* We must remove :font-entity property. */
3931 for (prev = Qnil, tail = extra; CONSP (tail); prev = tail, tail = XCDR (tail))
3932 if (EQ (XCAR (XCAR (tail)), QCfont_entity))
3933 {
3934 if (NILP (prev))
3935 extra = XCDR (extra);
3936 else
3937 XSETCDR (prev, XCDR (tail));
3938 break;
3939 }
3940 ASET (new_spec, FONT_EXTRA_INDEX, extra);
3941 return new_spec;
3942 }
3943
3944 DEFUN ("merge-font-spec", Fmerge_font_spec, Smerge_font_spec, 2, 2, 0,
3945 doc: /* Merge font-specs FROM and TO, and return a new font-spec.
3946 Every specified properties in FROM override the corresponding
3947 properties in TO. */)
3948 (Lisp_Object from, Lisp_Object to)
3949 {
3950 Lisp_Object extra, tail;
3951 int i;
3952
3953 CHECK_FONT (from);
3954 CHECK_FONT (to);
3955 to = Fcopy_font_spec (to);
3956 for (i = 0; i < FONT_EXTRA_INDEX; i++)
3957 ASET (to, i, AREF (from, i));
3958 extra = AREF (to, FONT_EXTRA_INDEX);
3959 for (tail = AREF (from, FONT_EXTRA_INDEX); CONSP (tail); tail = XCDR (tail))
3960 if (! EQ (XCAR (XCAR (tail)), Qfont_entity))
3961 {
3962 Lisp_Object slot = assq_no_quit (XCAR (XCAR (tail)), extra);
3963
3964 if (! NILP (slot))
3965 XSETCDR (slot, XCDR (XCAR (tail)));
3966 else
3967 extra = Fcons (Fcons (XCAR (XCAR (tail)), XCDR (XCAR (tail))), extra);
3968 }
3969 ASET (to, FONT_EXTRA_INDEX, extra);
3970 return to;
3971 }
3972
3973 DEFUN ("font-get", Ffont_get, Sfont_get, 2, 2, 0,
3974 doc: /* Return the value of FONT's property KEY.
3975 FONT is a font-spec, a font-entity, or a font-object.
3976 KEY is any symbol, but these are reserved for specific meanings:
3977 :family, :weight, :slant, :width, :foundry, :adstyle, :registry,
3978 :size, :name, :script, :otf
3979 See the documentation of `font-spec' for their meanings.
3980 In addition, if FONT is a font-entity or a font-object, values of
3981 :script and :otf are different from those of a font-spec as below:
3982
3983 The value of :script may be a list of scripts that are supported by the font.
3984
3985 The value of :otf is a cons (GSUB . GPOS) where GSUB and GPOS are lists
3986 representing the OpenType features supported by the font by this form:
3987 ((SCRIPT (LANGSYS FEATURE ...) ...) ...)
3988 SCRIPT, LANGSYS, and FEATURE are all symbols representing OpenType
3989 Layout tags. */)
3990 (Lisp_Object font, Lisp_Object key)
3991 {
3992 int idx;
3993 Lisp_Object val;
3994
3995 CHECK_FONT (font);
3996 CHECK_SYMBOL (key);
3997
3998 idx = get_font_prop_index (key);
3999 if (idx >= FONT_WEIGHT_INDEX && idx <= FONT_WIDTH_INDEX)
4000 return font_style_symbolic (font, idx, 0);
4001 if (idx >= 0 && idx < FONT_EXTRA_INDEX)
4002 return AREF (font, idx);
4003 val = Fassq (key, AREF (font, FONT_EXTRA_INDEX));
4004 if (NILP (val) && EQ (key, QCotf) && FONT_OBJECT_P (font))
4005 {
4006 struct font *fontp = XFONT_OBJECT (font);
4007
4008 if (fontp->driver->otf_capability)
4009 val = fontp->driver->otf_capability (fontp);
4010 else
4011 val = Fcons (Qnil, Qnil);
4012 font_put_extra (font, QCotf, val);
4013 }
4014 else
4015 val = Fcdr (val);
4016 return val;
4017 }
4018
4019 #ifdef HAVE_WINDOW_SYSTEM
4020
4021 DEFUN ("font-face-attributes", Ffont_face_attributes, Sfont_face_attributes, 1, 2, 0,
4022 doc: /* Return a plist of face attributes generated by FONT.
4023 FONT is a font name, a font-spec, a font-entity, or a font-object.
4024 The return value is a list of the form
4025
4026 \(:family FAMILY :height HEIGHT :weight WEIGHT :slant SLANT :width WIDTH)
4027
4028 where FAMILY, HEIGHT, WEIGHT, SLANT, and WIDTH are face attribute values
4029 compatible with `set-face-attribute'. Some of these key-attribute pairs
4030 may be omitted from the list if they are not specified by FONT.
4031
4032 The optional argument FRAME specifies the frame that the face attributes
4033 are to be displayed on. If omitted, the selected frame is used. */)
4034 (Lisp_Object font, Lisp_Object frame)
4035 {
4036 struct frame *f;
4037 Lisp_Object plist[10];
4038 Lisp_Object val;
4039 int n = 0;
4040
4041 if (NILP (frame))
4042 frame = selected_frame;
4043 CHECK_LIVE_FRAME (frame);
4044 f = XFRAME (frame);
4045
4046 if (STRINGP (font))
4047 {
4048 int fontset = fs_query_fontset (font, 0);
4049 Lisp_Object name = font;
4050 if (fontset >= 0)
4051 font = fontset_ascii (fontset);
4052 font = font_spec_from_name (name);
4053 if (! FONTP (font))
4054 signal_error ("Invalid font name", name);
4055 }
4056 else if (! FONTP (font))
4057 signal_error ("Invalid font object", font);
4058
4059 val = AREF (font, FONT_FAMILY_INDEX);
4060 if (! NILP (val))
4061 {
4062 plist[n++] = QCfamily;
4063 plist[n++] = SYMBOL_NAME (val);
4064 }
4065
4066 val = AREF (font, FONT_SIZE_INDEX);
4067 if (INTEGERP (val))
4068 {
4069 Lisp_Object font_dpi = AREF (font, FONT_DPI_INDEX);
4070 int dpi = INTEGERP (font_dpi) ? XINT (font_dpi) : f->resy;
4071 plist[n++] = QCheight;
4072 plist[n++] = make_number (PIXEL_TO_POINT (XINT (val) * 10, dpi));
4073 }
4074 else if (FLOATP (val))
4075 {
4076 plist[n++] = QCheight;
4077 plist[n++] = make_number (10 * (int) XFLOAT_DATA (val));
4078 }
4079
4080 val = FONT_WEIGHT_FOR_FACE (font);
4081 if (! NILP (val))
4082 {
4083 plist[n++] = QCweight;
4084 plist[n++] = val;
4085 }
4086
4087 val = FONT_SLANT_FOR_FACE (font);
4088 if (! NILP (val))
4089 {
4090 plist[n++] = QCslant;
4091 plist[n++] = val;
4092 }
4093
4094 val = FONT_WIDTH_FOR_FACE (font);
4095 if (! NILP (val))
4096 {
4097 plist[n++] = QCwidth;
4098 plist[n++] = val;
4099 }
4100
4101 return Flist (n, plist);
4102 }
4103
4104 #endif
4105
4106 DEFUN ("font-put", Ffont_put, Sfont_put, 3, 3, 0,
4107 doc: /* Set one property of FONT: give property KEY value VAL.
4108 FONT is a font-spec, a font-entity, or a font-object.
4109
4110 If FONT is a font-spec, KEY can be any symbol. But if KEY is the one
4111 accepted by the function `font-spec' (which see), VAL must be what
4112 allowed in `font-spec'.
4113
4114 If FONT is a font-entity or a font-object, KEY must not be the one
4115 accepted by `font-spec'. */)
4116 (Lisp_Object font, Lisp_Object prop, Lisp_Object val)
4117 {
4118 int idx;
4119
4120 idx = get_font_prop_index (prop);
4121 if (idx >= 0 && idx < FONT_EXTRA_INDEX)
4122 {
4123 CHECK_FONT_SPEC (font);
4124 ASET (font, idx, font_prop_validate (idx, Qnil, val));
4125 }
4126 else
4127 {
4128 if (EQ (prop, QCname)
4129 || EQ (prop, QCscript)
4130 || EQ (prop, QClang)
4131 || EQ (prop, QCotf))
4132 CHECK_FONT_SPEC (font);
4133 else
4134 CHECK_FONT (font);
4135 font_put_extra (font, prop, font_prop_validate (0, prop, val));
4136 }
4137 return val;
4138 }
4139
4140 DEFUN ("list-fonts", Flist_fonts, Slist_fonts, 1, 4, 0,
4141 doc: /* List available fonts matching FONT-SPEC on the current frame.
4142 Optional 2nd argument FRAME specifies the target frame.
4143 Optional 3rd argument NUM, if non-nil, limits the number of returned fonts.
4144 Optional 4th argument PREFER, if non-nil, is a font-spec to
4145 control the order of the returned list. Fonts are sorted by
4146 how close they are to PREFER. */)
4147 (Lisp_Object font_spec, Lisp_Object frame, Lisp_Object num, Lisp_Object prefer)
4148 {
4149 Lisp_Object vec, list;
4150 int n = 0;
4151
4152 if (NILP (frame))
4153 frame = selected_frame;
4154 CHECK_LIVE_FRAME (frame);
4155 CHECK_FONT_SPEC (font_spec);
4156 if (! NILP (num))
4157 {
4158 CHECK_NUMBER (num);
4159 n = XINT (num);
4160 if (n <= 0)
4161 return Qnil;
4162 }
4163 if (! NILP (prefer))
4164 CHECK_FONT_SPEC (prefer);
4165
4166 list = font_list_entities (frame, font_spec);
4167 if (NILP (list))
4168 return Qnil;
4169 if (NILP (XCDR (list))
4170 && ASIZE (XCAR (list)) == 1)
4171 return Fcons (AREF (XCAR (list), 0), Qnil);
4172
4173 if (! NILP (prefer))
4174 vec = font_sort_entities (list, prefer, frame, 0);
4175 else
4176 vec = font_vconcat_entity_vectors (list);
4177 if (n == 0 || n >= ASIZE (vec))
4178 {
4179 Lisp_Object args[2];
4180
4181 args[0] = vec;
4182 args[1] = Qnil;
4183 list = Fappend (2, args);
4184 }
4185 else
4186 {
4187 for (list = Qnil, n--; n >= 0; n--)
4188 list = Fcons (AREF (vec, n), list);
4189 }
4190 return list;
4191 }
4192
4193 DEFUN ("font-family-list", Ffont_family_list, Sfont_family_list, 0, 1, 0,
4194 doc: /* List available font families on the current frame.
4195 Optional argument FRAME, if non-nil, specifies the target frame. */)
4196 (Lisp_Object frame)
4197 {
4198 FRAME_PTR f;
4199 struct font_driver_list *driver_list;
4200 Lisp_Object list;
4201
4202 if (NILP (frame))
4203 frame = selected_frame;
4204 CHECK_LIVE_FRAME (frame);
4205 f = XFRAME (frame);
4206 list = Qnil;
4207 for (driver_list = f->font_driver_list; driver_list;
4208 driver_list = driver_list->next)
4209 if (driver_list->driver->list_family)
4210 {
4211 Lisp_Object val = driver_list->driver->list_family (frame);
4212 Lisp_Object tail = list;
4213
4214 for (; CONSP (val); val = XCDR (val))
4215 if (NILP (Fmemq (XCAR (val), tail))
4216 && SYMBOLP (XCAR (val)))
4217 list = Fcons (SYMBOL_NAME (XCAR (val)), list);
4218 }
4219 return list;
4220 }
4221
4222 DEFUN ("find-font", Ffind_font, Sfind_font, 1, 2, 0,
4223 doc: /* Return a font-entity matching with FONT-SPEC on the current frame.
4224 Optional 2nd argument FRAME, if non-nil, specifies the target frame. */)
4225 (Lisp_Object font_spec, Lisp_Object frame)
4226 {
4227 Lisp_Object val = Flist_fonts (font_spec, frame, make_number (1), Qnil);
4228
4229 if (CONSP (val))
4230 val = XCAR (val);
4231 return val;
4232 }
4233
4234 DEFUN ("font-xlfd-name", Ffont_xlfd_name, Sfont_xlfd_name, 1, 2, 0,
4235 doc: /* Return XLFD name of FONT.
4236 FONT is a font-spec, font-entity, or font-object.
4237 If the name is too long for XLFD (maximum 255 chars), return nil.
4238 If the 2nd optional arg FOLD-WILDCARDS is non-nil,
4239 the consecutive wildcards are folded to one. */)
4240 (Lisp_Object font, Lisp_Object fold_wildcards)
4241 {
4242 char name[256];
4243 int pixel_size = 0;
4244
4245 CHECK_FONT (font);
4246
4247 if (FONT_OBJECT_P (font))
4248 {
4249 Lisp_Object font_name = AREF (font, FONT_NAME_INDEX);
4250
4251 if (STRINGP (font_name)
4252 && SDATA (font_name)[0] == '-')
4253 {
4254 if (NILP (fold_wildcards))
4255 return font_name;
4256 strcpy (name, (char *) SDATA (font_name));
4257 goto done;
4258 }
4259 pixel_size = XFONT_OBJECT (font)->pixel_size;
4260 }
4261 if (font_unparse_xlfd (font, pixel_size, name, 256) < 0)
4262 return Qnil;
4263 done:
4264 if (! NILP (fold_wildcards))
4265 {
4266 char *p0 = name, *p1;
4267
4268 while ((p1 = strstr (p0, "-*-*")))
4269 {
4270 strcpy (p1, p1 + 2);
4271 p0 = p1;
4272 }
4273 }
4274
4275 return build_string (name);
4276 }
4277
4278 DEFUN ("clear-font-cache", Fclear_font_cache, Sclear_font_cache, 0, 0, 0,
4279 doc: /* Clear font cache. */)
4280 (void)
4281 {
4282 Lisp_Object list, frame;
4283
4284 FOR_EACH_FRAME (list, frame)
4285 {
4286 FRAME_PTR f = XFRAME (frame);
4287 struct font_driver_list *driver_list = f->font_driver_list;
4288
4289 for (; driver_list; driver_list = driver_list->next)
4290 if (driver_list->on)
4291 {
4292 Lisp_Object cache = driver_list->driver->get_cache (f);
4293 Lisp_Object val, tmp;
4294
4295 val = XCDR (cache);
4296 while (! NILP (val)
4297 && ! EQ (XCAR (XCAR (val)), driver_list->driver->type))
4298 val = XCDR (val);
4299 font_assert (! NILP (val));
4300 tmp = XCDR (XCAR (val));
4301 if (XINT (XCAR (tmp)) == 0)
4302 {
4303 font_clear_cache (f, XCAR (val), driver_list->driver);
4304 XSETCDR (cache, XCDR (val));
4305 }
4306 }
4307 }
4308
4309 return Qnil;
4310 }
4311
4312 \f
4313 void
4314 font_fill_lglyph_metrics (Lisp_Object glyph, Lisp_Object font_object)
4315 {
4316 struct font *font = XFONT_OBJECT (font_object);
4317 unsigned code;
4318 /* ecode used in LGLYPH_SET_CODE to avoid compiler warnings. */
4319 EMACS_INT ecode = font->driver->encode_char (font, LGLYPH_CHAR (glyph));
4320 struct font_metrics metrics;
4321
4322 LGLYPH_SET_CODE (glyph, ecode);
4323 code = ecode;
4324 font->driver->text_extents (font, &code, 1, &metrics);
4325 LGLYPH_SET_LBEARING (glyph, metrics.lbearing);
4326 LGLYPH_SET_RBEARING (glyph, metrics.rbearing);
4327 LGLYPH_SET_WIDTH (glyph, metrics.width);
4328 LGLYPH_SET_ASCENT (glyph, metrics.ascent);
4329 LGLYPH_SET_DESCENT (glyph, metrics.descent);
4330 }
4331
4332
4333 DEFUN ("font-shape-gstring", Ffont_shape_gstring, Sfont_shape_gstring, 1, 1, 0,
4334 doc: /* Shape the glyph-string GSTRING.
4335 Shaping means substituting glyphs and/or adjusting positions of glyphs
4336 to get the correct visual image of character sequences set in the
4337 header of the glyph-string.
4338
4339 If the shaping was successful, the value is GSTRING itself or a newly
4340 created glyph-string. Otherwise, the value is nil. */)
4341 (Lisp_Object gstring)
4342 {
4343 struct font *font;
4344 Lisp_Object font_object, n, glyph;
4345 int i, j, from, to;
4346
4347 if (! composition_gstring_p (gstring))
4348 signal_error ("Invalid glyph-string: ", gstring);
4349 if (! NILP (LGSTRING_ID (gstring)))
4350 return gstring;
4351 font_object = LGSTRING_FONT (gstring);
4352 CHECK_FONT_OBJECT (font_object);
4353 font = XFONT_OBJECT (font_object);
4354 if (! font->driver->shape)
4355 return Qnil;
4356
4357 /* Try at most three times with larger gstring each time. */
4358 for (i = 0; i < 3; i++)
4359 {
4360 n = font->driver->shape (gstring);
4361 if (INTEGERP (n))
4362 break;
4363 gstring = larger_vector (gstring,
4364 ASIZE (gstring) + LGSTRING_GLYPH_LEN (gstring),
4365 Qnil);
4366 }
4367 if (i == 3 || XINT (n) == 0)
4368 return Qnil;
4369 if (XINT (n) < LGSTRING_GLYPH_LEN (gstring))
4370 LGSTRING_SET_GLYPH (gstring, XINT (n), Qnil);
4371
4372 glyph = LGSTRING_GLYPH (gstring, 0);
4373 from = LGLYPH_FROM (glyph);
4374 to = LGLYPH_TO (glyph);
4375 for (i = 1, j = 0; i < LGSTRING_GLYPH_LEN (gstring); i++)
4376 {
4377 Lisp_Object this = LGSTRING_GLYPH (gstring, i);
4378
4379 if (NILP (this))
4380 break;
4381 if (NILP (LGLYPH_ADJUSTMENT (this)))
4382 {
4383 if (j < i - 1)
4384 for (; j < i; j++)
4385 {
4386 glyph = LGSTRING_GLYPH (gstring, j);
4387 LGLYPH_SET_FROM (glyph, from);
4388 LGLYPH_SET_TO (glyph, to);
4389 }
4390 from = LGLYPH_FROM (this);
4391 to = LGLYPH_TO (this);
4392 j = i;
4393 }
4394 else
4395 {
4396 if (from > LGLYPH_FROM (this))
4397 from = LGLYPH_FROM (this);
4398 if (to < LGLYPH_TO (this))
4399 to = LGLYPH_TO (this);
4400 }
4401 }
4402 if (j < i - 1)
4403 for (; j < i; j++)
4404 {
4405 glyph = LGSTRING_GLYPH (gstring, j);
4406 LGLYPH_SET_FROM (glyph, from);
4407 LGLYPH_SET_TO (glyph, to);
4408 }
4409 return composition_gstring_put_cache (gstring, XINT (n));
4410 }
4411
4412 DEFUN ("font-variation-glyphs", Ffont_variation_glyphs, Sfont_variation_glyphs,
4413 2, 2, 0,
4414 doc: /* Return a list of variation glyphs for CHAR in FONT-OBJECT.
4415 Each element of the value is a cons (VARIATION-SELECTOR . GLYPH-ID),
4416 where
4417 VARIATION-SELECTOR is a character code of variation selection
4418 (#xFE00..#xFE0F or #xE0100..#xE01EF)
4419 GLYPH-ID is a glyph code of the corresponding variation glyph. */)
4420 (Lisp_Object font_object, Lisp_Object character)
4421 {
4422 unsigned variations[256];
4423 struct font *font;
4424 int i, n;
4425 Lisp_Object val;
4426
4427 CHECK_FONT_OBJECT (font_object);
4428 CHECK_CHARACTER (character);
4429 font = XFONT_OBJECT (font_object);
4430 if (! font->driver->get_variation_glyphs)
4431 return Qnil;
4432 n = font->driver->get_variation_glyphs (font, XINT (character), variations);
4433 if (! n)
4434 return Qnil;
4435 val = Qnil;
4436 for (i = 0; i < 255; i++)
4437 if (variations[i])
4438 {
4439 Lisp_Object code;
4440 int vs = (i < 16 ? 0xFE00 + i : 0xE0100 + (i - 16));
4441 /* Stops GCC whining about limited range of data type. */
4442 EMACS_INT var = variations[i];
4443
4444 if (var > MOST_POSITIVE_FIXNUM)
4445 code = Fcons (make_number ((variations[i]) >> 16),
4446 make_number ((variations[i]) & 0xFFFF));
4447 else
4448 code = make_number (variations[i]);
4449 val = Fcons (Fcons (make_number (vs), code), val);
4450 }
4451 return val;
4452 }
4453
4454 #if 0
4455
4456 DEFUN ("font-drive-otf", Ffont_drive_otf, Sfont_drive_otf, 6, 6, 0,
4457 doc: /* Apply OpenType features on glyph-string GSTRING-IN.
4458 OTF-FEATURES specifies which features to apply in this format:
4459 (SCRIPT LANGSYS GSUB GPOS)
4460 where
4461 SCRIPT is a symbol specifying a script tag of OpenType,
4462 LANGSYS is a symbol specifying a langsys tag of OpenType,
4463 GSUB and GPOS, if non-nil, are lists of symbols specifying feature tags.
4464
4465 If LANGYS is nil, the default langsys is selected.
4466
4467 The features are applied in the order they appear in the list. The
4468 symbol `*' means to apply all available features not present in this
4469 list, and the remaining features are ignored. For instance, (vatu
4470 pstf * haln) is to apply vatu and pstf in this order, then to apply
4471 all available features other than vatu, pstf, and haln.
4472
4473 The features are applied to the glyphs in the range FROM and TO of
4474 the glyph-string GSTRING-IN.
4475
4476 If some feature is actually applicable, the resulting glyphs are
4477 produced in the glyph-string GSTRING-OUT from the index INDEX. In
4478 this case, the value is the number of produced glyphs.
4479
4480 If no feature is applicable, no glyph is produced in GSTRING-OUT, and
4481 the value is 0.
4482
4483 If GSTRING-OUT is too short to hold produced glyphs, no glyphs are
4484 produced in GSTRING-OUT, and the value is nil.
4485
4486 See the documentation of `font-make-gstring' for the format of
4487 glyph-string. */)
4488 (Lisp_Object otf_features, Lisp_Object gstring_in, Lisp_Object from, Lisp_Object to, Lisp_Object gstring_out, Lisp_Object index)
4489 {
4490 Lisp_Object font_object = LGSTRING_FONT (gstring_in);
4491 Lisp_Object val;
4492 struct font *font;
4493 int len, num;
4494
4495 check_otf_features (otf_features);
4496 CHECK_FONT_OBJECT (font_object);
4497 font = XFONT_OBJECT (font_object);
4498 if (! font->driver->otf_drive)
4499 error ("Font backend %s can't drive OpenType GSUB table",
4500 SDATA (SYMBOL_NAME (font->driver->type)));
4501 CHECK_CONS (otf_features);
4502 CHECK_SYMBOL (XCAR (otf_features));
4503 val = XCDR (otf_features);
4504 CHECK_SYMBOL (XCAR (val));
4505 val = XCDR (otf_features);
4506 if (! NILP (val))
4507 CHECK_CONS (val);
4508 len = check_gstring (gstring_in);
4509 CHECK_VECTOR (gstring_out);
4510 CHECK_NATNUM (from);
4511 CHECK_NATNUM (to);
4512 CHECK_NATNUM (index);
4513
4514 if (XINT (from) >= XINT (to) || XINT (to) > len)
4515 args_out_of_range_3 (from, to, make_number (len));
4516 if (XINT (index) >= ASIZE (gstring_out))
4517 args_out_of_range (index, make_number (ASIZE (gstring_out)));
4518 num = font->driver->otf_drive (font, otf_features,
4519 gstring_in, XINT (from), XINT (to),
4520 gstring_out, XINT (index), 0);
4521 if (num < 0)
4522 return Qnil;
4523 return make_number (num);
4524 }
4525
4526 DEFUN ("font-otf-alternates", Ffont_otf_alternates, Sfont_otf_alternates,
4527 3, 3, 0,
4528 doc: /* Return a list of alternate glyphs of CHARACTER in FONT-OBJECT.
4529 OTF-FEATURES specifies which features of the font FONT-OBJECT to apply
4530 in this format:
4531 (SCRIPT LANGSYS FEATURE ...)
4532 See the documentation of `font-drive-otf' for more detail.
4533
4534 The value is a list of cons cells of the format (GLYPH-ID . CHARACTER),
4535 where GLYPH-ID is a glyph index of the font, and CHARACTER is a
4536 character code corresponding to the glyph or nil if there's no
4537 corresponding character. */)
4538 (Lisp_Object font_object, Lisp_Object character, Lisp_Object otf_features)
4539 {
4540 struct font *font;
4541 Lisp_Object gstring_in, gstring_out, g;
4542 Lisp_Object alternates;
4543 int i, num;
4544
4545 CHECK_FONT_GET_OBJECT (font_object, font);
4546 if (! font->driver->otf_drive)
4547 error ("Font backend %s can't drive OpenType GSUB table",
4548 SDATA (SYMBOL_NAME (font->driver->type)));
4549 CHECK_CHARACTER (character);
4550 CHECK_CONS (otf_features);
4551
4552 gstring_in = Ffont_make_gstring (font_object, make_number (1));
4553 g = LGSTRING_GLYPH (gstring_in, 0);
4554 LGLYPH_SET_CHAR (g, XINT (character));
4555 gstring_out = Ffont_make_gstring (font_object, make_number (10));
4556 while ((num = font->driver->otf_drive (font, otf_features, gstring_in, 0, 1,
4557 gstring_out, 0, 1)) < 0)
4558 gstring_out = Ffont_make_gstring (font_object,
4559 make_number (ASIZE (gstring_out) * 2));
4560 alternates = Qnil;
4561 for (i = 0; i < num; i++)
4562 {
4563 Lisp_Object g = LGSTRING_GLYPH (gstring_out, i);
4564 int c = LGLYPH_CHAR (g);
4565 unsigned code = LGLYPH_CODE (g);
4566
4567 alternates = Fcons (Fcons (make_number (code),
4568 c > 0 ? make_number (c) : Qnil),
4569 alternates);
4570 }
4571 return Fnreverse (alternates);
4572 }
4573 #endif /* 0 */
4574
4575 #ifdef FONT_DEBUG
4576
4577 DEFUN ("open-font", Fopen_font, Sopen_font, 1, 3, 0,
4578 doc: /* Open FONT-ENTITY. */)
4579 (Lisp_Object font_entity, Lisp_Object size, Lisp_Object frame)
4580 {
4581 int isize;
4582
4583 CHECK_FONT_ENTITY (font_entity);
4584 if (NILP (frame))
4585 frame = selected_frame;
4586 CHECK_LIVE_FRAME (frame);
4587
4588 if (NILP (size))
4589 isize = XINT (AREF (font_entity, FONT_SIZE_INDEX));
4590 else
4591 {
4592 CHECK_NUMBER_OR_FLOAT (size);
4593 if (FLOATP (size))
4594 isize = POINT_TO_PIXEL (XFLOAT_DATA (size), XFRAME (frame)->resy);
4595 else
4596 isize = XINT (size);
4597 if (isize == 0)
4598 isize = 120;
4599 }
4600 return font_open_entity (XFRAME (frame), font_entity, isize);
4601 }
4602
4603 DEFUN ("close-font", Fclose_font, Sclose_font, 1, 2, 0,
4604 doc: /* Close FONT-OBJECT. */)
4605 (Lisp_Object font_object, Lisp_Object frame)
4606 {
4607 CHECK_FONT_OBJECT (font_object);
4608 if (NILP (frame))
4609 frame = selected_frame;
4610 CHECK_LIVE_FRAME (frame);
4611 font_close_object (XFRAME (frame), font_object);
4612 return Qnil;
4613 }
4614
4615 DEFUN ("query-font", Fquery_font, Squery_font, 1, 1, 0,
4616 doc: /* Return information about FONT-OBJECT.
4617 The value is a vector:
4618 [ NAME FILENAME PIXEL-SIZE SIZE ASCENT DESCENT SPACE-WIDTH AVERAGE-WIDTH
4619 CAPABILITY ]
4620
4621 NAME is a string of the font name (or nil if the font backend doesn't
4622 provide a name).
4623
4624 FILENAME is a string of the font file (or nil if the font backend
4625 doesn't provide a file name).
4626
4627 PIXEL-SIZE is a pixel size by which the font is opened.
4628
4629 SIZE is a maximum advance width of the font in pixels.
4630
4631 ASCENT, DESCENT, SPACE-WIDTH, AVERAGE-WIDTH are metrics of the font in
4632 pixels.
4633
4634 CAPABILITY is a list whose first element is a symbol representing the
4635 font format \(x, opentype, truetype, type1, pcf, or bdf) and the
4636 remaining elements describe the details of the font capability.
4637
4638 If the font is OpenType font, the form of the list is
4639 \(opentype GSUB GPOS)
4640 where GSUB shows which "GSUB" features the font supports, and GPOS
4641 shows which "GPOS" features the font supports. Both GSUB and GPOS are
4642 lists of the format:
4643 \((SCRIPT (LANGSYS FEATURE ...) ...) ...)
4644
4645 If the font is not OpenType font, currently the length of the form is
4646 one.
4647
4648 SCRIPT is a symbol representing OpenType script tag.
4649
4650 LANGSYS is a symbol representing OpenType langsys tag, or nil
4651 representing the default langsys.
4652
4653 FEATURE is a symbol representing OpenType feature tag.
4654
4655 If the font is not OpenType font, CAPABILITY is nil. */)
4656 (Lisp_Object font_object)
4657 {
4658 struct font *font;
4659 Lisp_Object val;
4660
4661 CHECK_FONT_GET_OBJECT (font_object, font);
4662
4663 val = Fmake_vector (make_number (9), Qnil);
4664 ASET (val, 0, AREF (font_object, FONT_NAME_INDEX));
4665 ASET (val, 1, AREF (font_object, FONT_FILE_INDEX));
4666 ASET (val, 2, make_number (font->pixel_size));
4667 ASET (val, 3, make_number (font->max_width));
4668 ASET (val, 4, make_number (font->ascent));
4669 ASET (val, 5, make_number (font->descent));
4670 ASET (val, 6, make_number (font->space_width));
4671 ASET (val, 7, make_number (font->average_width));
4672 if (font->driver->otf_capability)
4673 ASET (val, 8, Fcons (Qopentype, font->driver->otf_capability (font)));
4674 return val;
4675 }
4676
4677 DEFUN ("font-get-glyphs", Ffont_get_glyphs, Sfont_get_glyphs, 3, 4, 0,
4678 doc:
4679 /* Return a vector of FONT-OBJECT's glyphs for the specified characters.
4680 FROM and TO are positions (integers or markers) specifying a region
4681 of the current buffer.
4682 If the optional fourth arg OBJECT is not nil, it is a string or a
4683 vector containing the target characters.
4684
4685 Each element is a vector containing information of a glyph in this format:
4686 [FROM-IDX TO-IDX C CODE WIDTH LBEARING RBEARING ASCENT DESCENT ADJUSTMENT]
4687 where
4688 FROM is an index numbers of a character the glyph corresponds to.
4689 TO is the same as FROM.
4690 C is the character of the glyph.
4691 CODE is the glyph-code of C in FONT-OBJECT.
4692 WIDTH thru DESCENT are the metrics (in pixels) of the glyph.
4693 ADJUSTMENT is always nil.
4694 If FONT-OBJECT doesn't have a glyph for a character,
4695 the corresponding element is nil. */)
4696 (Lisp_Object font_object, Lisp_Object from, Lisp_Object to,
4697 Lisp_Object object)
4698 {
4699 struct font *font;
4700 int i, len, c;
4701 Lisp_Object *chars, vec;
4702 USE_SAFE_ALLOCA;
4703
4704 CHECK_FONT_GET_OBJECT (font_object, font);
4705 if (NILP (object))
4706 {
4707 EMACS_INT charpos, bytepos;
4708
4709 validate_region (&from, &to);
4710 if (EQ (from, to))
4711 return Qnil;
4712 len = XFASTINT (to) - XFASTINT (from);
4713 SAFE_ALLOCA_LISP (chars, len);
4714 charpos = XFASTINT (from);
4715 bytepos = CHAR_TO_BYTE (charpos);
4716 for (i = 0; charpos < XFASTINT (to); i++)
4717 {
4718 FETCH_CHAR_ADVANCE (c, charpos, bytepos);
4719 chars[i] = make_number (c);
4720 }
4721 }
4722 else if (STRINGP (object))
4723 {
4724 const unsigned char *p;
4725
4726 CHECK_NUMBER (from);
4727 CHECK_NUMBER (to);
4728 if (XINT (from) < 0 || XINT (from) > XINT (to)
4729 || XINT (to) > SCHARS (object))
4730 args_out_of_range_3 (object, from, to);
4731 if (EQ (from, to))
4732 return Qnil;
4733 len = XFASTINT (to) - XFASTINT (from);
4734 SAFE_ALLOCA_LISP (chars, len);
4735 p = SDATA (object);
4736 if (STRING_MULTIBYTE (object))
4737 for (i = 0; i < len; i++)
4738 {
4739 c = STRING_CHAR_ADVANCE (p);
4740 chars[i] = make_number (c);
4741 }
4742 else
4743 for (i = 0; i < len; i++)
4744 chars[i] = make_number (p[i]);
4745 }
4746 else
4747 {
4748 CHECK_VECTOR (object);
4749 CHECK_NUMBER (from);
4750 CHECK_NUMBER (to);
4751 if (XINT (from) < 0 || XINT (from) > XINT (to)
4752 || XINT (to) > ASIZE (object))
4753 args_out_of_range_3 (object, from, to);
4754 if (EQ (from, to))
4755 return Qnil;
4756 len = XFASTINT (to) - XFASTINT (from);
4757 for (i = 0; i < len; i++)
4758 {
4759 Lisp_Object elt = AREF (object, XFASTINT (from) + i);
4760 CHECK_CHARACTER (elt);
4761 }
4762 chars = &(AREF (object, XFASTINT (from)));
4763 }
4764
4765 vec = Fmake_vector (make_number (len), Qnil);
4766 for (i = 0; i < len; i++)
4767 {
4768 Lisp_Object g;
4769 int c = XFASTINT (chars[i]);
4770 unsigned code;
4771 EMACS_INT cod;
4772 struct font_metrics metrics;
4773
4774 cod = code = font->driver->encode_char (font, c);
4775 if (code == FONT_INVALID_CODE)
4776 continue;
4777 g = Fmake_vector (make_number (LGLYPH_SIZE), Qnil);
4778 LGLYPH_SET_FROM (g, i);
4779 LGLYPH_SET_TO (g, i);
4780 LGLYPH_SET_CHAR (g, c);
4781 LGLYPH_SET_CODE (g, code);
4782 font->driver->text_extents (font, &code, 1, &metrics);
4783 LGLYPH_SET_WIDTH (g, metrics.width);
4784 LGLYPH_SET_LBEARING (g, metrics.lbearing);
4785 LGLYPH_SET_RBEARING (g, metrics.rbearing);
4786 LGLYPH_SET_ASCENT (g, metrics.ascent);
4787 LGLYPH_SET_DESCENT (g, metrics.descent);
4788 ASET (vec, i, g);
4789 }
4790 if (! VECTORP (object))
4791 SAFE_FREE ();
4792 return vec;
4793 }
4794
4795 DEFUN ("font-match-p", Ffont_match_p, Sfont_match_p, 2, 2, 0,
4796 doc: /* Return t if and only if font-spec SPEC matches with FONT.
4797 FONT is a font-spec, font-entity, or font-object. */)
4798 (Lisp_Object spec, Lisp_Object font)
4799 {
4800 CHECK_FONT_SPEC (spec);
4801 CHECK_FONT (font);
4802
4803 return (font_match_p (spec, font) ? Qt : Qnil);
4804 }
4805
4806 DEFUN ("font-at", Ffont_at, Sfont_at, 1, 3, 0,
4807 doc: /* Return a font-object for displaying a character at POSITION.
4808 Optional second arg WINDOW, if non-nil, is a window displaying
4809 the current buffer. It defaults to the currently selected window. */)
4810 (Lisp_Object position, Lisp_Object window, Lisp_Object string)
4811 {
4812 struct window *w;
4813 EMACS_INT pos;
4814
4815 if (NILP (string))
4816 {
4817 CHECK_NUMBER_COERCE_MARKER (position);
4818 pos = XINT (position);
4819 if (pos < BEGV || pos >= ZV)
4820 args_out_of_range_3 (position, make_number (BEGV), make_number (ZV));
4821 }
4822 else
4823 {
4824 CHECK_NUMBER (position);
4825 CHECK_STRING (string);
4826 pos = XINT (position);
4827 if (pos < 0 || pos >= SCHARS (string))
4828 args_out_of_range (string, position);
4829 }
4830 if (NILP (window))
4831 window = selected_window;
4832 CHECK_LIVE_WINDOW (window);
4833 w = XWINDOW (window);
4834
4835 return font_at (-1, pos, NULL, w, string);
4836 }
4837
4838 #if 0
4839 DEFUN ("draw-string", Fdraw_string, Sdraw_string, 2, 2, 0,
4840 doc: /* Draw STRING by FONT-OBJECT on the top left corner of the current frame.
4841 The value is a number of glyphs drawn.
4842 Type C-l to recover what previously shown. */)
4843 (Lisp_Object font_object, Lisp_Object string)
4844 {
4845 Lisp_Object frame = selected_frame;
4846 FRAME_PTR f = XFRAME (frame);
4847 struct font *font;
4848 struct face *face;
4849 int i, len, width;
4850 unsigned *code;
4851
4852 CHECK_FONT_GET_OBJECT (font_object, font);
4853 CHECK_STRING (string);
4854 len = SCHARS (string);
4855 code = alloca (sizeof (unsigned) * len);
4856 for (i = 0; i < len; i++)
4857 {
4858 Lisp_Object ch = Faref (string, make_number (i));
4859 Lisp_Object val;
4860 int c = XINT (ch);
4861
4862 code[i] = font->driver->encode_char (font, c);
4863 if (code[i] == FONT_INVALID_CODE)
4864 break;
4865 }
4866 face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4867 face->fontp = font;
4868 if (font->driver->prepare_face)
4869 font->driver->prepare_face (f, face);
4870 width = font->driver->text_extents (font, code, i, NULL);
4871 len = font->driver->draw_text (f, face, 0, font->ascent, code, i, width);
4872 if (font->driver->done_face)
4873 font->driver->done_face (f, face);
4874 face->fontp = NULL;
4875 return make_number (len);
4876 }
4877 #endif
4878
4879 #endif /* FONT_DEBUG */
4880
4881 #ifdef HAVE_WINDOW_SYSTEM
4882
4883 DEFUN ("font-info", Ffont_info, Sfont_info, 1, 2, 0,
4884 doc: /* Return information about a font named NAME on frame FRAME.
4885 If FRAME is omitted or nil, use the selected frame.
4886 The returned value is a vector of OPENED-NAME, FULL-NAME, SIZE,
4887 HEIGHT, BASELINE-OFFSET, RELATIVE-COMPOSE, and DEFAULT-ASCENT,
4888 where
4889 OPENED-NAME is the name used for opening the font,
4890 FULL-NAME is the full name of the font,
4891 SIZE is the pixelsize of the font,
4892 HEIGHT is the pixel-height of the font (i.e ascent + descent),
4893 BASELINE-OFFSET is the upward offset pixels from ASCII baseline,
4894 RELATIVE-COMPOSE and DEFAULT-ASCENT are the numbers controlling
4895 how to compose characters.
4896 If the named font is not yet loaded, return nil. */)
4897 (Lisp_Object name, Lisp_Object frame)
4898 {
4899 FRAME_PTR f;
4900 struct font *font;
4901 Lisp_Object info;
4902 Lisp_Object font_object;
4903
4904 (*check_window_system_func) ();
4905
4906 if (! FONTP (name))
4907 CHECK_STRING (name);
4908 if (NILP (frame))
4909 frame = selected_frame;
4910 CHECK_LIVE_FRAME (frame);
4911 f = XFRAME (frame);
4912
4913 if (STRINGP (name))
4914 {
4915 int fontset = fs_query_fontset (name, 0);
4916
4917 if (fontset >= 0)
4918 name = fontset_ascii (fontset);
4919 font_object = font_open_by_name (f, (char *) SDATA (name));
4920 }
4921 else if (FONT_OBJECT_P (name))
4922 font_object = name;
4923 else if (FONT_ENTITY_P (name))
4924 font_object = font_open_entity (f, name, 0);
4925 else
4926 {
4927 struct face *face = FACE_FROM_ID (f, DEFAULT_FACE_ID);
4928 Lisp_Object entity = font_matching_entity (f, face->lface, name);
4929
4930 font_object = ! NILP (entity) ? font_open_entity (f, entity, 0) : Qnil;
4931 }
4932 if (NILP (font_object))
4933 return Qnil;
4934 font = XFONT_OBJECT (font_object);
4935
4936 info = Fmake_vector (make_number (7), Qnil);
4937 XVECTOR (info)->contents[0] = AREF (font_object, FONT_NAME_INDEX);
4938 XVECTOR (info)->contents[1] = AREF (font_object, FONT_FULLNAME_INDEX);
4939 XVECTOR (info)->contents[2] = make_number (font->pixel_size);
4940 XVECTOR (info)->contents[3] = make_number (font->height);
4941 XVECTOR (info)->contents[4] = make_number (font->baseline_offset);
4942 XVECTOR (info)->contents[5] = make_number (font->relative_compose);
4943 XVECTOR (info)->contents[6] = make_number (font->default_ascent);
4944
4945 #if 0
4946 /* As font_object is still in FONT_OBJLIST of the entity, we can't
4947 close it now. Perhaps, we should manage font-objects
4948 by `reference-count'. */
4949 font_close_object (f, font_object);
4950 #endif
4951 return info;
4952 }
4953 #endif
4954
4955 \f
4956 #define BUILD_STYLE_TABLE(TBL) \
4957 build_style_table ((TBL), sizeof TBL / sizeof (struct table_entry))
4958
4959 static Lisp_Object
4960 build_style_table (const struct table_entry *entry, int nelement)
4961 {
4962 int i, j;
4963 Lisp_Object table, elt;
4964
4965 table = Fmake_vector (make_number (nelement), Qnil);
4966 for (i = 0; i < nelement; i++)
4967 {
4968 for (j = 0; entry[i].names[j]; j++);
4969 elt = Fmake_vector (make_number (j + 1), Qnil);
4970 ASET (elt, 0, make_number (entry[i].numeric));
4971 for (j = 0; entry[i].names[j]; j++)
4972 ASET (elt, j + 1, intern_c_string (entry[i].names[j]));
4973 ASET (table, i, elt);
4974 }
4975 return table;
4976 }
4977
4978 Lisp_Object Vfont_log;
4979
4980 /* The deferred font-log data of the form [ACTION ARG RESULT].
4981 If ACTION is not nil, that is added to the log when font_add_log is
4982 called next time. At that time, ACTION is set back to nil. */
4983 static Lisp_Object Vfont_log_deferred;
4984
4985 /* Prepend the font-related logging data in Vfont_log if it is not
4986 `t'. ACTION describes a kind of font-related action (e.g. listing,
4987 opening), ARG is the argument for the action, and RESULT is the
4988 result of the action. */
4989 void
4990 font_add_log (const char *action, Lisp_Object arg, Lisp_Object result)
4991 {
4992 Lisp_Object tail, val;
4993 int i;
4994
4995 if (EQ (Vfont_log, Qt))
4996 return;
4997 if (STRINGP (AREF (Vfont_log_deferred, 0)))
4998 {
4999 char *str = (char *) SDATA (AREF (Vfont_log_deferred, 0));
5000
5001 ASET (Vfont_log_deferred, 0, Qnil);
5002 font_add_log (str, AREF (Vfont_log_deferred, 1),
5003 AREF (Vfont_log_deferred, 2));
5004 }
5005
5006 if (FONTP (arg))
5007 {
5008 Lisp_Object tail, elt;
5009 Lisp_Object equalstr = build_string ("=");
5010
5011 val = Ffont_xlfd_name (arg, Qt);
5012 for (tail = AREF (arg, FONT_EXTRA_INDEX); CONSP (tail);
5013 tail = XCDR (tail))
5014 {
5015 elt = XCAR (tail);
5016 if (EQ (XCAR (elt), QCscript)
5017 && SYMBOLP (XCDR (elt)))
5018 val = concat3 (val, SYMBOL_NAME (QCscript),
5019 concat2 (equalstr, SYMBOL_NAME (XCDR (elt))));
5020 else if (EQ (XCAR (elt), QClang)
5021 && SYMBOLP (XCDR (elt)))
5022 val = concat3 (val, SYMBOL_NAME (QClang),
5023 concat2 (equalstr, SYMBOL_NAME (XCDR (elt))));
5024 else if (EQ (XCAR (elt), QCotf)
5025 && CONSP (XCDR (elt)) && SYMBOLP (XCAR (XCDR (elt))))
5026 val = concat3 (val, SYMBOL_NAME (QCotf),
5027 concat2 (equalstr,
5028 SYMBOL_NAME (XCAR (XCDR (elt)))));
5029 }
5030 arg = val;
5031 }
5032
5033 if (CONSP (result)
5034 && VECTORP (XCAR (result))
5035 && ASIZE (XCAR (result)) > 0
5036 && FONTP (AREF (XCAR (result), 0)))
5037 result = font_vconcat_entity_vectors (result);
5038 if (FONTP (result))
5039 {
5040 val = Ffont_xlfd_name (result, Qt);
5041 if (! FONT_SPEC_P (result))
5042 val = concat3 (SYMBOL_NAME (AREF (result, FONT_TYPE_INDEX)),
5043 build_string (":"), val);
5044 result = val;
5045 }
5046 else if (CONSP (result))
5047 {
5048 result = Fcopy_sequence (result);
5049 for (tail = result; CONSP (tail); tail = XCDR (tail))
5050 {
5051 val = XCAR (tail);
5052 if (FONTP (val))
5053 val = Ffont_xlfd_name (val, Qt);
5054 XSETCAR (tail, val);
5055 }
5056 }
5057 else if (VECTORP (result))
5058 {
5059 result = Fcopy_sequence (result);
5060 for (i = 0; i < ASIZE (result); i++)
5061 {
5062 val = AREF (result, i);
5063 if (FONTP (val))
5064 val = Ffont_xlfd_name (val, Qt);
5065 ASET (result, i, val);
5066 }
5067 }
5068 Vfont_log = Fcons (list3 (intern (action), arg, result), Vfont_log);
5069 }
5070
5071 /* Record a font-related logging data to be added to Vfont_log when
5072 font_add_log is called next time. ACTION, ARG, RESULT are the same
5073 as font_add_log. */
5074
5075 void
5076 font_deferred_log (const char *action, Lisp_Object arg, Lisp_Object result)
5077 {
5078 if (EQ (Vfont_log, Qt))
5079 return;
5080 ASET (Vfont_log_deferred, 0, build_string (action));
5081 ASET (Vfont_log_deferred, 1, arg);
5082 ASET (Vfont_log_deferred, 2, result);
5083 }
5084
5085 void
5086 syms_of_font (void)
5087 {
5088 sort_shift_bits[FONT_TYPE_INDEX] = 0;
5089 sort_shift_bits[FONT_SLANT_INDEX] = 2;
5090 sort_shift_bits[FONT_WEIGHT_INDEX] = 9;
5091 sort_shift_bits[FONT_SIZE_INDEX] = 16;
5092 sort_shift_bits[FONT_WIDTH_INDEX] = 23;
5093 /* Note that the other elements in sort_shift_bits are not used. */
5094
5095 staticpro (&font_charset_alist);
5096 font_charset_alist = Qnil;
5097
5098 DEFSYM (Qopentype, "opentype");
5099
5100 DEFSYM (Qascii_0, "ascii-0");
5101 DEFSYM (Qiso8859_1, "iso8859-1");
5102 DEFSYM (Qiso10646_1, "iso10646-1");
5103 DEFSYM (Qunicode_bmp, "unicode-bmp");
5104 DEFSYM (Qunicode_sip, "unicode-sip");
5105
5106 DEFSYM (QCf, "Cf");
5107
5108 DEFSYM (QCotf, ":otf");
5109 DEFSYM (QClang, ":lang");
5110 DEFSYM (QCscript, ":script");
5111 DEFSYM (QCantialias, ":antialias");
5112
5113 DEFSYM (QCfoundry, ":foundry");
5114 DEFSYM (QCadstyle, ":adstyle");
5115 DEFSYM (QCregistry, ":registry");
5116 DEFSYM (QCspacing, ":spacing");
5117 DEFSYM (QCdpi, ":dpi");
5118 DEFSYM (QCscalable, ":scalable");
5119 DEFSYM (QCavgwidth, ":avgwidth");
5120 DEFSYM (QCfont_entity, ":font-entity");
5121 DEFSYM (QCfc_unknown_spec, ":fc-unknown-spec");
5122
5123 DEFSYM (Qc, "c");
5124 DEFSYM (Qm, "m");
5125 DEFSYM (Qp, "p");
5126 DEFSYM (Qd, "d");
5127
5128 DEFSYM (Qja, "ja");
5129 DEFSYM (Qko, "ko");
5130
5131 DEFSYM (QCuser_spec, "user-spec");
5132
5133 staticpro (&null_vector);
5134 null_vector = Fmake_vector (make_number (0), Qnil);
5135
5136 staticpro (&scratch_font_spec);
5137 scratch_font_spec = Ffont_spec (0, NULL);
5138 staticpro (&scratch_font_prefer);
5139 scratch_font_prefer = Ffont_spec (0, NULL);
5140
5141 staticpro (&Vfont_log_deferred);
5142 Vfont_log_deferred = Fmake_vector (make_number (3), Qnil);
5143
5144 #if 0
5145 #ifdef HAVE_LIBOTF
5146 staticpro (&otf_list);
5147 otf_list = Qnil;
5148 #endif /* HAVE_LIBOTF */
5149 #endif /* 0 */
5150
5151 defsubr (&Sfontp);
5152 defsubr (&Sfont_spec);
5153 defsubr (&Sfont_get);
5154 #ifdef HAVE_WINDOW_SYSTEM
5155 defsubr (&Sfont_face_attributes);
5156 #endif
5157 defsubr (&Sfont_put);
5158 defsubr (&Slist_fonts);
5159 defsubr (&Sfont_family_list);
5160 defsubr (&Sfind_font);
5161 defsubr (&Sfont_xlfd_name);
5162 defsubr (&Sclear_font_cache);
5163 defsubr (&Sfont_shape_gstring);
5164 defsubr (&Sfont_variation_glyphs);
5165 #if 0
5166 defsubr (&Sfont_drive_otf);
5167 defsubr (&Sfont_otf_alternates);
5168 #endif /* 0 */
5169
5170 #ifdef FONT_DEBUG
5171 defsubr (&Sopen_font);
5172 defsubr (&Sclose_font);
5173 defsubr (&Squery_font);
5174 defsubr (&Sfont_get_glyphs);
5175 defsubr (&Sfont_match_p);
5176 defsubr (&Sfont_at);
5177 #if 0
5178 defsubr (&Sdraw_string);
5179 #endif
5180 #endif /* FONT_DEBUG */
5181 #ifdef HAVE_WINDOW_SYSTEM
5182 defsubr (&Sfont_info);
5183 #endif
5184
5185 DEFVAR_LISP ("font-encoding-alist", &Vfont_encoding_alist,
5186 doc: /*
5187 Alist of fontname patterns vs the corresponding encoding and repertory info.
5188 Each element looks like (REGEXP . (ENCODING . REPERTORY)),
5189 where ENCODING is a charset or a char-table,
5190 and REPERTORY is a charset, a char-table, or nil.
5191
5192 If ENCODING and REPERTORY are the same, the element can have the form
5193 \(REGEXP . ENCODING).
5194
5195 ENCODING is for converting a character to a glyph code of the font.
5196 If ENCODING is a charset, encoding a character by the charset gives
5197 the corresponding glyph code. If ENCODING is a char-table, looking up
5198 the table by a character gives the corresponding glyph code.
5199
5200 REPERTORY specifies a repertory of characters supported by the font.
5201 If REPERTORY is a charset, all characters beloging to the charset are
5202 supported. If REPERTORY is a char-table, all characters who have a
5203 non-nil value in the table are supported. If REPERTORY is nil, Emacs
5204 gets the repertory information by an opened font and ENCODING. */);
5205 Vfont_encoding_alist = Qnil;
5206
5207 /* FIXME: These 3 vars are not quite what they appear: setq on them
5208 won't have any effect other than disconnect them from the style
5209 table used by the font display code. So we make them read-only,
5210 to avoid this confusing situation. */
5211
5212 DEFVAR_LISP_NOPRO ("font-weight-table", &Vfont_weight_table,
5213 doc: /* Vector of valid font weight values.
5214 Each element has the form:
5215 [NUMERIC-VALUE SYMBOLIC-NAME ALIAS-NAME ...]
5216 NUMERIC-VALUE is an integer, and SYMBOLIC-NAME and ALIAS-NAME are symbols. */);
5217 Vfont_weight_table = BUILD_STYLE_TABLE (weight_table);
5218 XSYMBOL (intern_c_string ("font-weight-table"))->constant = 1;
5219
5220 DEFVAR_LISP_NOPRO ("font-slant-table", &Vfont_slant_table,
5221 doc: /* Vector of font slant symbols vs the corresponding numeric values.
5222 See `font-weight-table' for the format of the vector. */);
5223 Vfont_slant_table = BUILD_STYLE_TABLE (slant_table);
5224 XSYMBOL (intern_c_string ("font-slant-table"))->constant = 1;
5225
5226 DEFVAR_LISP_NOPRO ("font-width-table", &Vfont_width_table,
5227 doc: /* Alist of font width symbols vs the corresponding numeric values.
5228 See `font-weight-table' for the format of the vector. */);
5229 Vfont_width_table = BUILD_STYLE_TABLE (width_table);
5230 XSYMBOL (intern_c_string ("font-width-table"))->constant = 1;
5231
5232 staticpro (&font_style_table);
5233 font_style_table = Fmake_vector (make_number (3), Qnil);
5234 ASET (font_style_table, 0, Vfont_weight_table);
5235 ASET (font_style_table, 1, Vfont_slant_table);
5236 ASET (font_style_table, 2, Vfont_width_table);
5237
5238 DEFVAR_LISP ("font-log", &Vfont_log, doc: /*
5239 *Logging list of font related actions and results.
5240 The value t means to suppress the logging.
5241 The initial value is set to nil if the environment variable
5242 EMACS_FONT_LOG is set. Otherwise, it is set to t. */);
5243 Vfont_log = Qnil;
5244
5245 #ifdef HAVE_WINDOW_SYSTEM
5246 #ifdef HAVE_FREETYPE
5247 syms_of_ftfont ();
5248 #ifdef HAVE_X_WINDOWS
5249 syms_of_xfont ();
5250 syms_of_ftxfont ();
5251 #ifdef HAVE_XFT
5252 syms_of_xftfont ();
5253 #endif /* HAVE_XFT */
5254 #endif /* HAVE_X_WINDOWS */
5255 #else /* not HAVE_FREETYPE */
5256 #ifdef HAVE_X_WINDOWS
5257 syms_of_xfont ();
5258 #endif /* HAVE_X_WINDOWS */
5259 #endif /* not HAVE_FREETYPE */
5260 #ifdef HAVE_BDFFONT
5261 syms_of_bdffont ();
5262 #endif /* HAVE_BDFFONT */
5263 #ifdef WINDOWSNT
5264 syms_of_w32font ();
5265 #endif /* WINDOWSNT */
5266 #ifdef HAVE_NS
5267 syms_of_nsfont ();
5268 #endif /* HAVE_NS */
5269 #endif /* HAVE_WINDOW_SYSTEM */
5270 }
5271
5272 void
5273 init_font (void)
5274 {
5275 Vfont_log = egetenv ("EMACS_FONT_LOG") ? Qnil : Qt;
5276 }
5277