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