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