1 /* xfont.c -- X core font driver.
2 Copyright (C) 2006-2017 Free Software Foundation, Inc.
3 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H13PRO009
7 This file is part of GNU Emacs.
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 (at
12 your option) any later version.
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.
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/>. */
29 #include "blockinput.h"
30 #include "character.h"
35 /* X core font driver. */
42 unsigned x_display_id
;
45 /* Prototypes of support functions. */
47 static XCharStruct
*xfont_get_pcm (XFontStruct
*, XChar2b
*);
49 /* Get metrics of character CHAR2B in XFONT. Value is null if CHAR2B
50 is not contained in the font. */
53 xfont_get_pcm (XFontStruct
*xfont
, XChar2b
*char2b
)
55 /* The result metric information. */
56 XCharStruct
*pcm
= NULL
;
58 eassert (xfont
&& char2b
);
60 if (xfont
->per_char
!= NULL
)
62 if (xfont
->min_byte1
== 0 && xfont
->max_byte1
== 0)
64 /* min_char_or_byte2 specifies the linear character index
65 corresponding to the first element of the per_char array,
66 max_char_or_byte2 is the index of the last character. A
67 character with non-zero CHAR2B->byte1 is not in the font.
68 A character with byte2 less than min_char_or_byte2 or
69 greater max_char_or_byte2 is not in the font. */
70 if (char2b
->byte1
== 0
71 && char2b
->byte2
>= xfont
->min_char_or_byte2
72 && char2b
->byte2
<= xfont
->max_char_or_byte2
)
73 pcm
= xfont
->per_char
+ char2b
->byte2
- xfont
->min_char_or_byte2
;
77 /* If either min_byte1 or max_byte1 are nonzero, both
78 min_char_or_byte2 and max_char_or_byte2 are less than
79 256, and the 2-byte character index values corresponding
80 to the per_char array element N (counting from 0) are:
82 byte1 = N/D + min_byte1
83 byte2 = N\D + min_char_or_byte2
87 D = max_char_or_byte2 - min_char_or_byte2 + 1
89 \ = integer modulus */
90 if (char2b
->byte1
>= xfont
->min_byte1
91 && char2b
->byte1
<= xfont
->max_byte1
92 && char2b
->byte2
>= xfont
->min_char_or_byte2
93 && char2b
->byte2
<= xfont
->max_char_or_byte2
)
94 pcm
= (xfont
->per_char
95 + ((xfont
->max_char_or_byte2
- xfont
->min_char_or_byte2
+ 1)
96 * (char2b
->byte1
- xfont
->min_byte1
))
97 + (char2b
->byte2
- xfont
->min_char_or_byte2
));
102 /* If the per_char pointer is null, all glyphs between the first
103 and last character indexes inclusive have the same
104 information, as given by both min_bounds and max_bounds. */
105 if (char2b
->byte2
>= xfont
->min_char_or_byte2
106 && char2b
->byte2
<= xfont
->max_char_or_byte2
)
107 pcm
= &xfont
->max_bounds
;
111 || (pcm
->width
== 0 && (pcm
->rbearing
- pcm
->lbearing
) == 0))
115 static Lisp_Object
xfont_get_cache (struct frame
*);
116 static Lisp_Object
xfont_list (struct frame
*, Lisp_Object
);
117 static Lisp_Object
xfont_match (struct frame
*, Lisp_Object
);
118 static Lisp_Object
xfont_list_family (struct frame
*);
119 static Lisp_Object
xfont_open (struct frame
*, Lisp_Object
, int);
120 static void xfont_close (struct font
*);
121 static void xfont_prepare_face (struct frame
*, struct face
*);
122 static int xfont_has_char (Lisp_Object
, int);
123 static unsigned xfont_encode_char (struct font
*, int);
124 static void xfont_text_extents (struct font
*, unsigned *, int,
125 struct font_metrics
*);
126 static int xfont_draw (struct glyph_string
*, int, int, int, int, bool);
127 static int xfont_check (struct frame
*, struct font
*);
129 struct font_driver xfont_driver
=
131 LISP_INITIALLY_ZERO
, /* Qx */
132 false, /* case insensitive */
146 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
148 NULL
, /* get_variation_glyphs */
149 NULL
, /* filter_properties */
153 xfont_get_cache (struct frame
*f
)
155 Display_Info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
157 return (dpyinfo
->name_list_element
);
161 compare_font_names (const void *name1
, const void *name2
)
163 char *const *n1
= name1
;
164 char *const *n2
= name2
;
165 return xstrcasecmp (*n1
, *n2
);
168 /* Decode XLFD as iso-8859-1 into OUTPUT, and return the byte length
169 of the decoding result. LEN is the byte length of XLFD, or -1 if
170 XLFD is NULL terminated. The caller must assure that OUTPUT is at
171 least twice (plus 1) as large as XLFD. */
174 xfont_decode_coding_xlfd (char *xlfd
, int len
, char *output
)
176 char *p0
= xlfd
, *p1
= output
;
181 c
= *(unsigned char *) p0
++;
182 p1
+= CHAR_STRING (c
, (unsigned char *) p1
);
187 return (p1
- output
);
190 /* Encode XLFD from UTF-8 to iso-8859-1 destructively, and return the
191 resulting byte length. If XLFD contains unencodable character,
195 xfont_encode_coding_xlfd (char *xlfd
)
197 const unsigned char *p0
= (unsigned char *) xlfd
;
198 unsigned char *p1
= (unsigned char *) xlfd
;
203 int c
= STRING_CHAR_ADVANCE (p0
);
214 /* Check if CHARS (cons or vector) is supported by XFONT whose
215 encoding charset is ENCODING (XFONT is NULL) or by a font whose
216 registry corresponds to ENCODING and REPERTORY.
217 Return true if supported. */
220 xfont_chars_supported (Lisp_Object chars
, XFontStruct
*xfont
,
221 struct charset
*encoding
, struct charset
*repertory
)
223 struct charset
*charset
= repertory
? repertory
: encoding
;
227 for (; CONSP (chars
); chars
= XCDR (chars
))
229 int c
= XINT (XCAR (chars
));
230 unsigned code
= ENCODE_CHAR (charset
, c
);
233 if (code
== CHARSET_INVALID_CODE (charset
))
239 char2b
.byte1
= code
>> 8;
240 char2b
.byte2
= code
& 0xFF;
241 if (! xfont_get_pcm (xfont
, &char2b
))
244 return (NILP (chars
));
246 else if (VECTORP (chars
))
250 for (i
= ASIZE (chars
) - 1; i
>= 0; i
--)
252 int c
= XINT (AREF (chars
, i
));
253 unsigned code
= ENCODE_CHAR (charset
, c
);
256 if (code
== CHARSET_INVALID_CODE (charset
))
262 char2b
.byte1
= code
>> 8;
263 char2b
.byte2
= code
& 0xFF;
264 if (xfont_get_pcm (xfont
, &char2b
))
272 /* A hash table recoding which font supports which scripts. Each key
273 is a vector of characteristic font properties FOUNDRY to WIDTH and
274 ADDSTYLE, and each value is a list of script symbols.
276 We assume that fonts that have the same value in the above
277 properties supports the same set of characters on all displays. */
279 static Lisp_Object xfont_scripts_cache
;
281 /* Re-usable vector to store characteristic font properties. */
282 static Lisp_Object xfont_scratch_props
;
284 /* Return a list of scripts supported by the font of FONTNAME whose
285 characteristic properties are in PROPS and whose encoding charset
286 is ENCODING. A caller must call BLOCK_INPUT in advance. */
289 xfont_supported_scripts (Display
*display
, char *fontname
, Lisp_Object props
,
290 struct charset
*encoding
)
294 /* Two special cases to avoid opening rather big fonts. */
295 if (EQ (AREF (props
, 2), Qja
))
296 return list2 (intern ("kana"), intern ("han"));
297 if (EQ (AREF (props
, 2), Qko
))
298 return list1 (intern ("hangul"));
299 scripts
= Fgethash (props
, xfont_scripts_cache
, Qt
);
300 if (EQ (scripts
, Qt
))
306 xfont
= XLoadQueryFont (display
, fontname
);
311 for (val
= Vscript_representative_chars
; CONSP (val
);
313 if (CONSP (XCAR (val
)) && SYMBOLP (XCAR (XCAR (val
))))
315 Lisp_Object script
= XCAR (XCAR (val
));
316 Lisp_Object chars
= XCDR (XCAR (val
));
318 if (xfont_chars_supported (chars
, xfont
, encoding
, NULL
))
319 scripts
= Fcons (script
, scripts
);
322 XFreeFont (display
, xfont
);
324 if (EQ (AREF (props
, 3), Qiso10646_1
)
325 && NILP (Fmemq (Qlatin
, scripts
)))
326 scripts
= Fcons (Qlatin
, scripts
);
327 Fputhash (Fcopy_sequence (props
), scripts
, xfont_scripts_cache
);
333 xfont_list_pattern (Display
*display
, const char *pattern
,
334 Lisp_Object registry
, Lisp_Object script
)
336 Lisp_Object list
= Qnil
;
337 Lisp_Object chars
= Qnil
;
338 struct charset
*encoding
, *repertory
= NULL
;
339 int i
, limit
, num_fonts
;
341 /* Large enough to decode the longest XLFD (255 bytes). */
344 if (! NILP (registry
)
345 && font_registry_charsets (registry
, &encoding
, &repertory
) < 0)
346 /* Unknown REGISTRY, not supported. */
350 chars
= assq_no_quit (script
, Vscript_representative_chars
);
352 /* We can't tell whether or not a font supports SCRIPT. */
354 chars
= XCDR (chars
);
357 if (! xfont_chars_supported (chars
, NULL
, encoding
, repertory
))
364 x_catch_errors (display
);
366 for (limit
= 512; ; limit
*= 2)
368 names
= XListFonts (display
, pattern
, limit
, &num_fonts
);
369 if (x_had_errors_p (display
))
371 /* This error is perhaps due to insufficient memory on X
372 server. Let's just ignore it. */
373 x_clear_errors (display
);
377 if (num_fonts
< limit
)
379 XFreeFontNames (names
);
384 char **indices
= alloca (sizeof (char *) * num_fonts
);
385 Lisp_Object
*props
= XVECTOR (xfont_scratch_props
)->contents
;
386 Lisp_Object scripts
= Qnil
, entity
= Qnil
;
388 for (i
= 0; i
< ASIZE (xfont_scratch_props
); i
++)
389 ASET (xfont_scratch_props
, i
, Qnil
);
390 for (i
= 0; i
< num_fonts
; i
++)
391 indices
[i
] = names
[i
];
392 qsort (indices
, num_fonts
, sizeof (char *), compare_font_names
);
394 /* Take one or two passes over the font list. Do the second
395 pass only if we really need it, i.e., only if the first pass
396 found no fonts and skipped some scalable fonts. */
397 bool skipped_some_scalable_fonts
= false;
400 || (i_pass
== 1 && NILP (list
) && skipped_some_scalable_fonts
));
402 for (i
= 0; i
< num_fonts
; i
++)
406 if (i
> 0 && xstrcasecmp (indices
[i
- 1], indices
[i
]) == 0)
409 entity
= font_make_entity ();
410 len
= xfont_decode_coding_xlfd (indices
[i
], -1, buf
);
411 if (font_parse_xlfd (buf
, len
, entity
) < 0)
413 ASET (entity
, FONT_TYPE_INDEX
, Qx
);
414 /* Avoid auto-scaled fonts. */
415 if (INTEGERP (AREF (entity
, FONT_DPI_INDEX
))
416 && INTEGERP (AREF (entity
, FONT_AVGWIDTH_INDEX
))
417 && XINT (AREF (entity
, FONT_DPI_INDEX
)) != 0
418 && XINT (AREF (entity
, FONT_AVGWIDTH_INDEX
)) == 0)
420 /* Avoid not-allowed scalable fonts. */
421 if (NILP (Vscalable_fonts_allowed
))
425 if (INTEGERP (AREF (entity
, FONT_SIZE_INDEX
)))
426 size
= XINT (AREF (entity
, FONT_SIZE_INDEX
));
427 else if (FLOATP (AREF (entity
, FONT_SIZE_INDEX
)))
428 size
= XFLOAT_DATA (AREF (entity
, FONT_SIZE_INDEX
));
429 if (size
== 0 && i_pass
== 0)
431 skipped_some_scalable_fonts
= true;
435 else if (CONSP (Vscalable_fonts_allowed
))
439 for (tail
= Vscalable_fonts_allowed
; CONSP (tail
);
442 Lisp_Object elt
= XCAR (tail
);
444 && (fast_c_string_match_ignore_case (elt
, indices
[i
],
453 /* Avoid fonts of invalid registry. */
454 if (NILP (AREF (entity
, FONT_REGISTRY_INDEX
)))
457 /* Update encoding and repertory if necessary. */
458 if (! EQ (registry
, AREF (entity
, FONT_REGISTRY_INDEX
)))
460 registry
= AREF (entity
, FONT_REGISTRY_INDEX
);
461 if (font_registry_charsets (registry
, &encoding
, &repertory
) < 0)
465 /* Unknown REGISTRY, not supported. */
470 || xfont_chars_supported (chars
, NULL
, encoding
, repertory
))
471 list
= Fcons (entity
, list
), entity
= Qnil
;
474 if (memcmp (props
, aref_addr (entity
, FONT_FOUNDRY_INDEX
),
476 || ! EQ (AREF (entity
, FONT_SPACING_INDEX
), props
[7]))
478 vcopy (xfont_scratch_props
, 0,
479 aref_addr (entity
, FONT_FOUNDRY_INDEX
), 7);
480 ASET (xfont_scratch_props
, 7, AREF (entity
, FONT_SPACING_INDEX
));
481 scripts
= xfont_supported_scripts (display
, indices
[i
],
486 || ! NILP (Fmemq (script
, scripts
)))
487 list
= Fcons (entity
, list
), entity
= Qnil
;
489 XFreeFontNames (names
);
495 FONT_ADD_LOG ("xfont-list", build_string (pattern
), list
);
500 xfont_list (struct frame
*f
, Lisp_Object spec
)
502 Display
*display
= FRAME_DISPLAY_INFO (f
)->display
;
503 Lisp_Object registry
, list
, val
, extra
, script
;
505 /* Large enough to contain the longest XLFD (255 bytes) in UTF-8. */
508 extra
= AREF (spec
, FONT_EXTRA_INDEX
);
511 val
= assq_no_quit (QCotf
, extra
);
514 val
= assq_no_quit (QClang
, extra
);
519 registry
= AREF (spec
, FONT_REGISTRY_INDEX
);
520 len
= font_unparse_xlfd (spec
, 0, name
, 512);
521 if (len
< 0 || (len
= xfont_encode_coding_xlfd (name
)) < 0)
524 val
= assq_no_quit (QCscript
, extra
);
526 list
= xfont_list_pattern (display
, name
, registry
, script
);
527 if (NILP (list
) && NILP (registry
))
530 char *r
= name
+ len
- 9; /* 9 == strlen (iso8859-1) */
532 if (r
- name
+ 10 < 256) /* 10 == strlen (iso10646-1) */
534 strcpy (r
, "iso10646-1");
535 list
= xfont_list_pattern (display
, name
, Qiso10646_1
, script
);
538 if (NILP (list
) && ! NILP (registry
))
540 /* Try alternate registries. */
543 if ((alter
= Fassoc (SYMBOL_NAME (registry
),
544 Vface_alternative_font_registry_alist
),
547 /* Pointer to REGISTRY-ENCODING field. */
548 char *r
= name
+ len
- SBYTES (SYMBOL_NAME (registry
));
550 for (alter
= XCDR (alter
); CONSP (alter
); alter
= XCDR (alter
))
551 if (STRINGP (XCAR (alter
))
552 && ((r
- name
) + SBYTES (XCAR (alter
))) < 256)
554 lispstpcpy (r
, XCAR (alter
));
555 list
= xfont_list_pattern (display
, name
, registry
, script
);
564 val
= assq_no_quit (QCname
, AREF (spec
, FONT_EXTRA_INDEX
));
565 if (CONSP (val
) && STRINGP (XCDR (val
)) && SBYTES (XCDR (val
)) < 512)
567 memcpy (name
, SDATA (XCDR (val
)), SBYTES (XCDR (val
)) + 1);
568 if (xfont_encode_coding_xlfd (name
) < 0)
570 list
= xfont_list_pattern (display
, name
, registry
, script
);
578 xfont_match (struct frame
*f
, Lisp_Object spec
)
580 Display
*display
= FRAME_DISPLAY_INFO (f
)->display
;
581 Lisp_Object extra
, val
, entity
;
586 extra
= AREF (spec
, FONT_EXTRA_INDEX
);
587 val
= assq_no_quit (QCname
, extra
);
588 if (! CONSP (val
) || ! STRINGP (XCDR (val
)))
590 if (font_unparse_xlfd (spec
, 0, name
, 512) < 0)
593 else if (SBYTES (XCDR (val
)) < 512)
594 memcpy (name
, SDATA (XCDR (val
)), SBYTES (XCDR (val
)) + 1);
597 if (xfont_encode_coding_xlfd (name
) < 0)
602 xfont
= XLoadQueryFont (display
, name
);
605 if (XGetFontProperty (xfont
, XA_FONT
, &value
))
607 char *s
= XGetAtomName (display
, (Atom
) value
);
609 /* If DXPC (a Differential X Protocol Compressor)
610 Ver.3.7 is running, XGetAtomName will return null
611 string. We must avoid such a name. */
615 entity
= font_make_entity ();
616 ASET (entity
, FONT_TYPE_INDEX
, Qx
);
617 len
= xfont_decode_coding_xlfd (s
, -1, name
);
618 if (font_parse_xlfd (name
, len
, entity
) < 0)
623 XFreeFont (display
, xfont
);
627 FONT_ADD_LOG ("xfont-match", spec
, entity
);
632 xfont_list_family (struct frame
*f
)
634 Display_Info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
638 char *last_family
IF_LINT (= 0);
642 x_catch_errors (dpyinfo
->display
);
643 names
= XListFonts (dpyinfo
->display
, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*",
645 if (x_had_errors_p (dpyinfo
->display
))
647 /* This error is perhaps due to insufficient memory on X server.
648 Let's just ignore it. */
649 x_clear_errors (dpyinfo
->display
);
654 for (i
= 0, last_len
= 0; i
< num_fonts
; i
++)
656 char *p0
= names
[i
], *p1
, buf
[512];
660 p0
++; /* skip the leading '-' */
661 while (*p0
&& *p0
!= '-') p0
++; /* skip foundry */
665 while (*p1
&& *p1
!= '-') p1
++; /* find the end of family */
666 if (! *p1
|| p1
== p0
)
668 if (last_len
== p1
- p0
669 && memcmp (last_family
, p0
, last_len
) == 0)
674 decoded_len
= xfont_decode_coding_xlfd (p0
, last_len
, buf
);
675 family
= font_intern_prop (p0
, decoded_len
, 1);
676 if (NILP (assq_no_quit (family
, list
)))
677 list
= Fcons (family
, list
);
680 XFreeFontNames (names
);
688 xfont_open (struct frame
*f
, Lisp_Object entity
, int pixel_size
)
690 Display_Info
*dpyinfo
= FRAME_DISPLAY_INFO (f
);
691 Display
*display
= dpyinfo
->display
;
695 Lisp_Object registry
;
696 struct charset
*encoding
, *repertory
;
697 Lisp_Object font_object
, fullname
;
701 /* At first, check if we know how to encode characters for this
703 registry
= AREF (entity
, FONT_REGISTRY_INDEX
);
704 if (font_registry_charsets (registry
, &encoding
, &repertory
) < 0)
706 FONT_ADD_LOG (" x:unknown registry", registry
, Qnil
);
710 if (XINT (AREF (entity
, FONT_SIZE_INDEX
)) != 0)
711 pixel_size
= XINT (AREF (entity
, FONT_SIZE_INDEX
));
712 else if (pixel_size
== 0)
715 pixel_size
= FRAME_FONT (f
)->pixel_size
;
719 len
= font_unparse_xlfd (entity
, pixel_size
, name
, 512);
720 if (len
<= 0 || (len
= xfont_encode_coding_xlfd (name
)) < 0)
722 FONT_ADD_LOG (" x:unparse failed", entity
, Qnil
);
727 x_catch_errors (display
);
728 xfont
= XLoadQueryFont (display
, name
);
729 if (x_had_errors_p (display
))
731 /* This error is perhaps due to insufficient memory on X server.
732 Let's just ignore it. */
733 x_clear_errors (display
);
738 /* Some version of X lists:
739 -misc-fixed-medium-r-normal--20-*-75-75-c-100-iso8859-1
740 -misc-fixed-medium-r-normal--20-*-100-100-c-100-iso8859-1
742 -misc-fixed-medium-r-normal--20-*-100-100-c-100-iso8859-1
744 -misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1
745 So, we try again with wildcards in RESX and RESY. */
748 temp
= copy_font_spec (entity
);
749 ASET (temp
, FONT_DPI_INDEX
, Qnil
);
750 len
= font_unparse_xlfd (temp
, pixel_size
, name
, 512);
751 if (len
<= 0 || (len
= xfont_encode_coding_xlfd (name
)) < 0)
753 FONT_ADD_LOG (" x:unparse failed", temp
, Qnil
);
756 xfont
= XLoadQueryFont (display
, name
);
757 if (x_had_errors_p (display
))
759 /* This error is perhaps due to insufficient memory on X server.
760 Let's just ignore it. */
761 x_clear_errors (display
);
766 /* Try to get the full name of FONT. */
767 if (xfont
&& XGetFontProperty (xfont
, XA_FONT
, &value
))
772 p0
= p
= XGetAtomName (FRAME_X_DISPLAY (f
), (Atom
) value
);
773 /* Count the number of dashes in the "full name".
774 If it is too few, this isn't really the font's full name,
776 In X11R4, the fonts did not come with their canonical names
787 len
= xfont_decode_coding_xlfd (p0
, -1, name
);
788 fullname
= Fdowncase (make_string (name
, len
));
797 FONT_ADD_LOG (" x:open failed", build_string (name
), Qnil
);
801 font_object
= font_make_object (VECSIZE (struct xfont_info
),
803 ASET (font_object
, FONT_TYPE_INDEX
, Qx
);
804 if (STRINGP (fullname
))
806 font_parse_xlfd (SSDATA (fullname
), SBYTES (fullname
), font_object
);
807 ASET (font_object
, FONT_NAME_INDEX
, fullname
);
813 len
= xfont_decode_coding_xlfd (name
, -1, buf
);
814 ASET (font_object
, FONT_NAME_INDEX
, make_string (buf
, len
));
816 ASET (font_object
, FONT_FULLNAME_INDEX
, fullname
);
817 font
= XFONT_OBJECT (font_object
);
818 ((struct xfont_info
*) font
)->xfont
= xfont
;
819 ((struct xfont_info
*) font
)->display
= FRAME_X_DISPLAY (f
);
820 ((struct xfont_info
*) font
)->x_display_id
= FRAME_DISPLAY_INFO (f
)->x_id
;
821 font
->pixel_size
= pixel_size
;
822 font
->driver
= &xfont_driver
;
823 font
->encoding_charset
= encoding
->id
;
824 font
->repertory_charset
= repertory
? repertory
->id
: -1;
825 font
->ascent
= xfont
->ascent
;
826 font
->descent
= xfont
->descent
;
827 font
->height
= font
->ascent
+ font
->descent
;
828 font
->min_width
= xfont
->min_bounds
.width
;
829 font
->max_width
= xfont
->max_bounds
.width
;
830 if (xfont
->min_bounds
.width
== xfont
->max_bounds
.width
)
832 /* Fixed width font. */
833 font
->average_width
= font
->space_width
= xfont
->min_bounds
.width
;
841 char2b
.byte1
= 0x00, char2b
.byte2
= 0x20;
842 pcm
= xfont_get_pcm (xfont
, &char2b
);
844 font
->space_width
= pcm
->width
;
846 font
->space_width
= 0;
848 val
= Ffont_get (font_object
, QCavgwidth
);
850 font
->average_width
= XINT (val
) / 10;
851 if (font
->average_width
< 0)
852 font
->average_width
= - font
->average_width
;
855 if (font
->average_width
== 0
856 && encoding
->ascii_compatible_p
)
858 int width
= font
->space_width
, n
= pcm
!= NULL
;
860 for (char2b
.byte2
= 33; char2b
.byte2
<= 126; char2b
.byte2
++)
861 if ((pcm
= xfont_get_pcm (xfont
, &char2b
)) != NULL
)
862 width
+= pcm
->width
, n
++;
864 font
->average_width
= width
/ n
;
866 if (font
->average_width
== 0)
867 /* No easy way other than this to get a reasonable
870 = (xfont
->min_bounds
.width
+ xfont
->max_bounds
.width
) / 2;
875 font
->underline_thickness
876 = (XGetFontProperty (xfont
, XA_UNDERLINE_THICKNESS
, &value
)
878 font
->underline_position
879 = (XGetFontProperty (xfont
, XA_UNDERLINE_POSITION
, &value
)
880 ? (long) value
: -1);
881 font
->baseline_offset
882 = (XGetFontProperty (xfont
, dpyinfo
->Xatom_MULE_BASELINE_OFFSET
, &value
)
884 font
->relative_compose
885 = (XGetFontProperty (xfont
, dpyinfo
->Xatom_MULE_RELATIVE_COMPOSE
, &value
)
888 = (XGetFontProperty (xfont
, dpyinfo
->Xatom_MULE_DEFAULT_ASCENT
, &value
)
893 fullname
= AREF (font_object
, FONT_NAME_INDEX
);
894 font
->vertical_centering
895 = (STRINGP (Vvertical_centering_font_regexp
)
896 && (fast_string_match_ignore_case
897 (Vvertical_centering_font_regexp
, fullname
) >= 0));
903 xfont_close (struct font
*font
)
905 struct x_display_info
*xdi
;
906 struct xfont_info
*xfi
= (struct xfont_info
*) font
;
908 /* This function may be called from GC when X connection is gone
909 (Bug#16093), and an attempt to free font resources on invalid
910 display may lead to X protocol errors or segfaults. Moreover,
911 the memory referenced by 'Display *' pointer may be reused for
912 the logically different X connection after the previous display
913 connection was closed. That's why we also check whether font's
914 ID matches the one recorded in x_display_info for this display.
915 See http://debbugs.gnu.org/cgi/bugreport.cgi?bug=16069. */
917 && ((xdi
= x_display_info_for_display (xfi
->display
))
918 && xfi
->x_display_id
== xdi
->x_id
))
921 XFreeFont (xfi
->display
, xfi
->xfont
);
928 xfont_prepare_face (struct frame
*f
, struct face
*face
)
931 XSetFont (FRAME_X_DISPLAY (f
), face
->gc
,
932 ((struct xfont_info
*) face
->font
)->xfont
->fid
);
937 xfont_has_char (Lisp_Object font
, int c
)
939 Lisp_Object registry
= AREF (font
, FONT_REGISTRY_INDEX
);
940 struct charset
*encoding
;
941 struct charset
*repertory
= NULL
;
943 if (EQ (registry
, Qiso10646_1
))
945 encoding
= CHARSET_FROM_ID (charset_unicode
);
946 /* We use a font of `ja' and `ko' adstyle only for a character
947 in JISX0208 and KSC5601 charsets respectively. */
948 if (EQ (AREF (font
, FONT_ADSTYLE_INDEX
), Qja
)
949 && charset_jisx0208
>= 0)
950 repertory
= CHARSET_FROM_ID (charset_jisx0208
);
951 else if (EQ (AREF (font
, FONT_ADSTYLE_INDEX
), Qko
)
952 && charset_ksc5601
>= 0)
953 repertory
= CHARSET_FROM_ID (charset_ksc5601
);
955 else if (font_registry_charsets (registry
, &encoding
, &repertory
) < 0)
956 /* Unknown REGISTRY, not usable. */
958 if (ASCII_CHAR_P (c
) && encoding
->ascii_compatible_p
)
962 return (ENCODE_CHAR (repertory
, c
) != CHARSET_INVALID_CODE (repertory
));
966 xfont_encode_char (struct font
*font
, int c
)
968 XFontStruct
*xfont
= ((struct xfont_info
*) font
)->xfont
;
969 struct charset
*charset
;
973 charset
= CHARSET_FROM_ID (font
->encoding_charset
);
974 code
= ENCODE_CHAR (charset
, c
);
975 if (code
== CHARSET_INVALID_CODE (charset
))
976 return FONT_INVALID_CODE
;
977 if (font
->repertory_charset
>= 0)
979 charset
= CHARSET_FROM_ID (font
->repertory_charset
);
980 return (ENCODE_CHAR (charset
, c
) != CHARSET_INVALID_CODE (charset
)
981 ? code
: FONT_INVALID_CODE
);
983 char2b
.byte1
= code
>> 8;
984 char2b
.byte2
= code
& 0xFF;
985 return (xfont_get_pcm (xfont
, &char2b
) ? code
: FONT_INVALID_CODE
);
989 xfont_text_extents (struct font
*font
, unsigned int *code
,
990 int nglyphs
, struct font_metrics
*metrics
)
992 XFontStruct
*xfont
= ((struct xfont_info
*) font
)->xfont
;
996 for (i
= 0, first
= true; i
< nglyphs
; i
++)
999 static XCharStruct
*pcm
;
1001 if (code
[i
] >= 0x10000)
1003 char2b
.byte1
= code
[i
] >> 8, char2b
.byte2
= code
[i
] & 0xFF;
1004 pcm
= xfont_get_pcm (xfont
, &char2b
);
1009 metrics
->lbearing
= pcm
->lbearing
;
1010 metrics
->rbearing
= pcm
->rbearing
;
1011 metrics
->ascent
= pcm
->ascent
;
1012 metrics
->descent
= pcm
->descent
;
1017 if (metrics
->lbearing
> width
+ pcm
->lbearing
)
1018 metrics
->lbearing
= width
+ pcm
->lbearing
;
1019 if (metrics
->rbearing
< width
+ pcm
->rbearing
)
1020 metrics
->rbearing
= width
+ pcm
->rbearing
;
1021 if (metrics
->ascent
< pcm
->ascent
)
1022 metrics
->ascent
= pcm
->ascent
;
1023 if (metrics
->descent
< pcm
->descent
)
1024 metrics
->descent
= pcm
->descent
;
1026 width
+= pcm
->width
;
1029 metrics
->width
= width
;
1033 xfont_draw (struct glyph_string
*s
, int from
, int to
, int x
, int y
,
1034 bool with_background
)
1036 XFontStruct
*xfont
= ((struct xfont_info
*) s
->font
)->xfont
;
1037 int len
= to
- from
;
1041 if (s
->gc
!= s
->face
->gc
)
1044 XSetFont (s
->display
, gc
, xfont
->fid
);
1048 if (xfont
->min_byte1
== 0 && xfont
->max_byte1
== 0)
1051 char *str
= SAFE_ALLOCA (len
);
1052 for (i
= 0; i
< len
; i
++)
1053 str
[i
] = XCHAR2B_BYTE2 (s
->char2b
+ from
+ i
);
1055 if (with_background
)
1058 for (i
= 0; i
< len
; i
++)
1059 XDrawImageString (FRAME_X_DISPLAY (s
->f
), FRAME_X_WINDOW (s
->f
),
1060 gc
, x
+ i
, y
, str
+ i
, 1);
1062 XDrawImageString (FRAME_X_DISPLAY (s
->f
), FRAME_X_WINDOW (s
->f
),
1063 gc
, x
, y
, str
, len
);
1068 for (i
= 0; i
< len
; i
++)
1069 XDrawString (FRAME_X_DISPLAY (s
->f
), FRAME_X_WINDOW (s
->f
),
1070 gc
, x
+ i
, y
, str
+ i
, 1);
1072 XDrawString (FRAME_X_DISPLAY (s
->f
), FRAME_X_WINDOW (s
->f
),
1073 gc
, x
, y
, str
, len
);
1081 if (with_background
)
1084 for (i
= 0; i
< len
; i
++)
1085 XDrawImageString16 (FRAME_X_DISPLAY (s
->f
), FRAME_X_WINDOW (s
->f
),
1086 gc
, x
+ i
, y
, s
->char2b
+ from
+ i
, 1);
1088 XDrawImageString16 (FRAME_X_DISPLAY (s
->f
), FRAME_X_WINDOW (s
->f
),
1089 gc
, x
, y
, s
->char2b
+ from
, len
);
1094 for (i
= 0; i
< len
; i
++)
1095 XDrawString16 (FRAME_X_DISPLAY (s
->f
), FRAME_X_WINDOW (s
->f
),
1096 gc
, x
+ i
, y
, s
->char2b
+ from
+ i
, 1);
1098 XDrawString16 (FRAME_X_DISPLAY (s
->f
), FRAME_X_WINDOW (s
->f
),
1099 gc
, x
, y
, s
->char2b
+ from
, len
);
1107 xfont_check (struct frame
*f
, struct font
*font
)
1109 struct xfont_info
*xfont
= (struct xfont_info
*) font
;
1111 return (FRAME_X_DISPLAY (f
) == xfont
->display
? 0 : -1);
1116 syms_of_xfont (void)
1118 staticpro (&xfont_scripts_cache
);
1119 xfont_scripts_cache
= CALLN (Fmake_hash_table
, QCtest
, Qequal
);
1120 staticpro (&xfont_scratch_props
);
1121 xfont_scratch_props
= Fmake_vector (make_number (8), Qnil
);
1122 xfont_driver
.type
= Qx
;
1123 register_font_driver (&xfont_driver
, NULL
);