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