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