ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtfont.C
(Generate patch)

Comparing rxvt-unicode/src/rxvtfont.C (file contents):
Revision 1.1 by root, Sun Aug 15 00:37:04 2004 UTC vs.
Revision 1.207 by root, Thu Jun 17 19:42:55 2021 UTC

1/*--------------------------------*-C-*---------------------------------* 1/*----------------------------------------------------------------------*
2 * File: defaultfont.C 2 * File: rxvtfont.C
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> 4 * Copyright (c) 2003-2008 Marc Lehmann <schmorp@schmorp.de>
5 * - original version. 5 * - original version.
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version. 10 * (at your option) any later version.
11 * 11 *
12 * This program is distributed in the hope that it will be useful, 12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details. 15 * GNU General Public License for more details.
16 * 16 *
17 * You should have received a copy of the GNU General Public License 17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software 18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 *---------------------------------------------------------------------*/ 20 *---------------------------------------------------------------------*/
21 21
22#include "../config.h" 22#include "../config.h"
23#include "rxvt.h" 23#include "rxvt.h"
24#include "rxvtutil.h"
24#include "rxvtfont.h" 25#include "rxvtfont.h"
25 26
26#include <cstdlib> 27#include <stdlib.h>
27 28
28#define DISPLAY r->display->display 29#include <inttypes.h>
29#define TGC r->TermWin.gc
30 30
31#if XFT
32# include <fontconfig/fontconfig.h>
33#endif
34
35#define MAX_OVERLAP_ROMAN (8 + 2) // max. character width in 8ths of the base width
36#define MAX_OVERLAP_ITALIC (8 + 3) // max. overlap for italic fonts
37
38#define OVERLAP_OK(w,wcw,prop) ((w) <= ( \
39 (prop)->slant >= rxvt_fontprop::italic \
40 ? ((prop)->width * (wcw) * MAX_OVERLAP_ITALIC + 7) >> 3 \
41 : ((prop)->width * (wcw) * MAX_OVERLAP_ROMAN + 7) >> 3 \
42 ))
43
31const struct rxvt_fallback_font { 44static const struct rxvt_fallback_font {
32 codeset cs; 45 codeset cs;
33 const char *name; 46 const char *name;
34} fallback_fonts[] = { 47} fallback_fonts[] = {
35 { CS_ISO8859_1, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-1" }, 48 { CS_ISO8859_1, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-1" },
36 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-15" }, 49 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-15" },
37 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-fcd8859-15" }, 50 { CS_ISO8859_15, "-*-*-*-r-*--*-*-*-*-c-*-fcd8859-15" },
38 51
39#if ENCODING_EU 52#if ENCODING_EU
40 // cyrillic 53 // cyrillic
41 { CS_KOI8_R, "-*-*-*-r-*--*-*-*-*-c-*-koi8-r" }, 54 { CS_KOI8_R, "-*-*-*-r-*--*-*-*-*-c-*-koi8-r" },
42 { CS_KOI8_U, "-*-*-*-r-*--*-*-*-*-c-*-koi8-u" }, 55 { CS_KOI8_U, "-*-*-*-r-*--*-*-*-*-c-*-koi8-u" },
43 56
44 { CS_ISO8859_2, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-2" }, 57 { CS_ISO8859_2, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-2" },
45 { CS_ISO8859_3, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-3" }, 58 { CS_ISO8859_3, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-3" },
46 { CS_ISO8859_4, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-4" }, 59 { CS_ISO8859_4, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-4" },
47 { CS_ISO8859_5, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-5" }, 60 { CS_ISO8859_5, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-5" },
48 { CS_ISO8859_6, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-6" }, 61 { CS_ISO8859_6, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-6" },
52 { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" }, 65 { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" },
53 { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" }, 66 { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" },
54 { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" }, 67 { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" },
55 { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" }, 68 { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" },
56 { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" }, 69 { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" },
70
71# if XFT
72 { CS_KOI8_U, "xft::lang=ru" },
73
74 { CS_ISO8859_5, "xft::lang=ru" },
75 { CS_ISO8859_6, "xft::lang=ar" },
76 { CS_ISO8859_7, "xft::lang=el" },
77 { CS_ISO8859_8, "xft::lang=he" },
78 { CS_ISO8859_9, "xft::lang=tr" },
79 { CS_ISO8859_10, "xft::lang=se" },
80 { CS_ISO8859_11, "xft::lang=th" },
81# endif
57#endif 82#endif
58 83
59 // japanese 84 // japanese
60#if ENCODING_JP || ENCODING_JP_EXT 85#if ENCODING_JP || ENCODING_JP_EXT
61# if XFT 86# if XFT
62 // prefer xft for complex scripts 87 // prefer xft for complex scripts
88 { CS_JIS0208_1990_0, "xft:Sazanami Mincho:antialias=false" },
63 { CS_UNICODE, "xft:Kochi Gothic:antialias=false" }, 89 { CS_JIS0208_1990_0, "xft:Kochi Gothic:antialias=false" },
90 { CS_JIS0208_1990_0, "xft:Mincho:antialias=false" },
91 { CS_JIS0208_1990_0, "xft::lang=ja:antialias=false" },
64# endif 92# endif
65 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" }, 93 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
66 { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" }, 94 { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
67 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, 95 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
68 { CS_JIS0201_1976_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0201*-0" }, 96 { CS_JIS0201_1976_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
69 { CS_JIS0208_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0208*-0" }, 97 { CS_JIS0208_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
70 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, 98 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
71#endif 99#endif
72 100
73#if ENCODING_CN || ENCODING_CN_EXT 101#if ENCODING_ZH || ENCODING_ZH_EXT
74# if XFT 102# if XFT
103 { CS_GBK_0, "xft:AR PL KaitiM GB" },
104 { CS_GBK_0, "xft:AR PL SungtiL GB" },
105 { CS_GBK_0, "xft::lang=zh" },
75 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, 106 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
76 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, 107 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
77 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, 108 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
78 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, 109 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
110 { CS_GB2312_1980_0, "xft::lang=zh" },
79# endif 111# endif
112 { CS_GBK_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gbk*-0" },
80 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" }, 113 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
81 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" }, 114 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
82 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" }, 115 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
116 { CS_GB2312_1980_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
83 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" }, 117 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
84 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" }, 118 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
85 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" }, 119 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
86 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" }, 120 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
87 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" }, 121 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
89 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" }, 123 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
90 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" }, 124 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
91 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" }, 125 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
92#endif 126#endif
93 127
128#if ENCODING_KR
129 { CS_KSC5601_1987_0, "-baekmuk-gulim-*-*-*-*-*-*-*-*-c-*-ksc5601*" },
130 { CS_KSC5601_1987_0, "-*-*-*-*-*-*-*-*-*-*-c-*-ksc5601*" },
131# if XFT
132 { CS_KSC5601_1987_0, "xft:Baekmuk Gulim:antialias=false" },
133 { CS_KSC5601_1987_0, "xft::lang=ko:antialias=false" },
134# endif
135#endif
136
137 //{ CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, // this gem of a font has actual dotted circles within the combining character glyphs.
94#if XFT 138#if XFT
139 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true" },
140 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" },
141 { CS_UNICODE, "xft:Andale Mono:antialias=false:autohint=false" },
142 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" },
143
144 // FreeMono is usually uglier than x fonts, so try after the others
95 { CS_UNICODE, "xft:Andale Mono" }, 145 { CS_UNICODE, "xft:FreeMono:autohint=true" },
96 { CS_UNICODE, "xft:Arial Unicode MS" },
97#endif 146#endif
98 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" }, 147
99 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, 148 // generic font fallback, put this last, as many iso10646 fonts have extents
149 // specified for all glyphs in the range they cover, but most are simply empty
100 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" }, 150 //{ CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
101 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 151 //{ CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
102#if XFT
103 // FreeMono is usually uglier than x fonts, so try last only.
104 //{ CS_UNICODE, "xft:FreeMono" },
105#endif
106
107 { CS_UNKNOWN, 0 } 152 { CS_UNKNOWN, 0 }
108}; 153};
109 154
155// these characters are used to guess the font height and width
156// pango uses a similar algorithm and doesn't trust the font either.
157static uint16_t extent_test_chars[] = {
158 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_',
159 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ
160 0x304c, 0x672c, // が本
161};
162
163#define dTermDisplay Display *disp = term->dpy
164#define dTermGC GC gc = term->gc
165
110///////////////////////////////////////////////////////////////////////////// 166/////////////////////////////////////////////////////////////////////////////
111
112#if XFT
113rxvt_drawable::~rxvt_drawable ()
114{
115 if (xftdrawable)
116 XftDrawDestroy (xftdrawable);
117}
118
119rxvt_drawable::operator XftDraw *()
120{
121 if (!xftdrawable)
122 xftdrawable = XftDrawCreate (display->display, drawable, display->visual, display->cmap);
123
124 return xftdrawable;
125}
126#endif
127
128/////////////////////////////////////////////////////////////////////////////
129
130static void *enc_buf;
131static uint32_t enc_len;
132
133static inline void *
134get_enc_buf (uint32_t len)
135{
136 if (len > enc_len)
137 {
138 free (enc_buf);
139 enc_buf = malloc (len);
140 enc_len = len;
141 }
142
143 return enc_buf;
144}
145 167
146static const char * 168static const char *
147enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero) 169enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
148{ 170{
149 uint8_t *buf = (uint8_t *)get_enc_buf (len); 171 uint8_t *buf = rxvt_temp_buf<uint8_t> (len);
172 uint8_t *res = buf;
150 173
151 while (len--) 174 while (len--)
152 { 175 {
153 uint32_t c = FROM_UNICODE (cs, *text++); 176 uint32_t c = FROM_UNICODE (cs, *text++);
154 177
159 } 182 }
160 183
161 *buf++ = c; 184 *buf++ = c;
162 } 185 }
163 186
164 return (const char *)enc_buf; 187 return (const char *)res;
165} 188}
166 189
167static const XChar2b * 190static const XChar2b *
168enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero) 191enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
169{ 192{
170 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b)); 193 XChar2b *buf = rxvt_temp_buf<XChar2b> (len);
194 XChar2b *res = buf;
171 195
172 while (len--) 196 while (len--)
173 { 197 {
174 uint32_t c = FROM_UNICODE (cs, *text++); 198 uint32_t c = FROM_UNICODE (cs, *text++);
175 199
182 buf->byte1 = c >> 8; 206 buf->byte1 = c >> 8;
183 buf->byte2 = c; 207 buf->byte2 = c;
184 buf++; 208 buf++;
185 } 209 }
186 210
187 return (XChar2b *)enc_buf; 211 return res;
188} 212}
189 213
190///////////////////////////////////////////////////////////////////////////// 214/////////////////////////////////////////////////////////////////////////////
191 215
216rxvt_font::rxvt_font ()
217: name(0), width(rxvt_fontprop::unset), height(rxvt_fontprop::unset)
218{
219}
220
192void 221void
222rxvt_font::set_name (char *name_)
223{
224 if (name == name_)
225 return;
226
227 if (name) free (name); // let the compiler optimize
228 name = name_;
229}
230
231void
193rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color) 232rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color) const
194{ 233{
195 if (color == Color_bg) 234 dTermDisplay;
196 XClearArea (d.display->display, d, x, y, w, h, FALSE); 235 dTermGC;
236
237 if (color == Color_bg || color == Color_transparent)
238 XClearArea (disp, d, x, y, w, h, false);
197 else if (color >= 0) 239 else if (color >= 0)
198 { 240 {
199#if XFT 241#if XFT
242 Picture dst;
243
244# ifdef HAVE_IMG
245 if (term->bg_img
246 && !term->pix_colors[color].is_opaque ()
247 && ((dst = XftDrawPicture (d))))
248 {
249 XClearArea (disp, d, x, y, w, h, false);
250
251 Picture solid_color_pict = XftDrawSrcPicture (d, &term->pix_colors[color].c);
252 XRenderComposite (disp, PictOpOver, solid_color_pict, None, dst, 0, 0, 0, 0, x, y, w, h);
253 }
254 else
255# endif
200 XftDrawRect (d, &r->PixColors[color].c, x, y, w, h); 256 XftDrawRect (d, &term->pix_colors[color].c, x, y, w, h);
257
201#else 258#else
202 XSetForeground (d.display->display, TGC, r->PixColors[color]); 259 XSetForeground (disp, gc, term->pix_colors[color]);
203 XFillRectangle (d.display->display, d, TGC, x, y, w, h); 260 XFillRectangle (disp, d, gc, x, y, w, h);
204#endif 261#endif
205 } 262 }
206} 263}
207 264
208#include "table/linedraw.h" 265/////////////////////////////////////////////////////////////////////////////
209 266
210struct rxvt_font_default : rxvt_font { 267struct rxvt_font_default : rxvt_font
268{
269 struct rxvt_fontset *fs;
270
271 rxvt_font_default (rxvt_fontset *fs)
272 : rxvt_font ()
273 {
274 this->fs = fs;
275 }
211 276
212 rxvt_fontprop properties () 277 rxvt_fontprop properties ()
213 { 278 {
214 rxvt_fontprop p; 279 rxvt_fontprop p;
215 280
216 p.width = p.height = 1; 281 p.width = p.height = 1;
282 p.ascent = rxvt_fontprop::unset;
217 p.weight = rxvt_fontprop::medium; 283 p.weight = rxvt_fontprop::medium;
218 p.slant = rxvt_fontprop::roman; 284 p.slant = rxvt_fontprop::roman;
219 285
220 return p; 286 return p;
221 } 287 }
222 288
223 bool load (const rxvt_fontprop &prop) 289 bool load (const rxvt_fontprop &prop, bool force_prop)
224 { 290 {
225 width = 1; height = 1; 291 width = 1; height = 1;
226 ascent = 1; descent = 0; 292 ascent = 1; descent = 0;
227 293
228 set_name (strdup ("built-in pseudofont")); 294 set_name (strdup ("built-in support font"));
229 295
230 return true; 296 return true;
231 } 297 }
232 298
233 bool has_codepoint (unicode_t unicode) 299 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
234 { 300 {
301 careful = false;
302
235 if (unicode <= 0x001f) 303 if (unicode <= 0x001f)
236 return true; 304 return true;
237 305
238 if (unicode <= 0x007f) 306 if (unicode <= 0x007f)
239 return false; 307 return false;
240 308
241 if (unicode <= 0x009f) 309 if (unicode <= 0x009f)
242 return true; 310 return true;
243 311
312#ifdef BUILTIN_GLYPHS
244 if (unicode >= 0x2500 && unicode <= 0x259f) 313 if (unicode >= 0x2500 && unicode <= 0x259f &&
314 !term->option (Opt_skipBuiltinGlyphs))
245 return true; 315 return true;
316#endif
246 317
247 if (IS_COMPOSE (unicode)) 318 // we do not check for IS_COMPOSE here, as this would
248 return true; 319 // rob other fonts from taking over.
249 320
250 switch (unicode) 321 switch (unicode)
251 { 322 {
252 case ZERO_WIDTH_CHAR: 323 case ZERO_WIDTH_CHAR:
324 case NOCHAR:
253 return true; 325 return true;
254 } 326 }
255 327
256 return false; 328 return false;
257 } 329 }
264void 336void
265rxvt_font_default::draw (rxvt_drawable &d, int x, int y, 337rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
266 const text_t *text, int len, 338 const text_t *text, int len,
267 int fg, int bg) 339 int fg, int bg)
268{ 340{
341 dTermDisplay;
342 dTermGC;
343
269 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 344 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
270 345
271 XSetForeground (d.display->display, TGC, r->PixColors[fg]); 346 XSetForeground (disp, gc, term->pix_colors[fg]);
272 347
273 while (len--) 348 while (len)
274 { 349 {
350 const text_t *tp = text;
351 text_t t = *tp;
352
353 while (++text, --len && *text == NOCHAR)
354 ;
355
275#if ENABLE_COMBINING 356#if ENABLE_COMBINING
276 compose_char *cc; 357 compose_char *cc;
277#endif 358#endif
278 text_t t = *text++; 359 int width = text - tp;
360 int fwidth = term->fwidth * width;
279 361
362#ifdef BUILTIN_GLYPHS
280 if (0x2500 <= t && t <= 0x259f) 363 if (0x2500 <= t && t <= 0x259f)
281 { 364 {
365# include "table/linedraw.h"
282 uint16_t offs = linedraw_offs[t - 0x2500]; 366 uint16_t offs = linedraw_offs[t - 0x2500];
283 uint32_t *a = linedraw_command + (offs >> 4); 367 uint32_t *a = linedraw_command + (offs >> 4);
284 uint32_t *b = a + (offs & 15); 368 uint32_t *b = a + (offs & 15);
285 369
286 int W = r->TermWin.fwidth; 370 int W = fwidth;
287 int H = r->TermWin.fheight; 371 int H = term->fheight;
288 372
289 int x_[16]; 373 int x_[16];
290 int y_[16]; 374 int y_[16];
291 375
292 for (int i = 0; i <= 8; i++) 376 for (int i = 0; i <= 8; i++)
300 384
301 XGCValues gcv; 385 XGCValues gcv;
302 386
303 gcv.cap_style = CapButt; 387 gcv.cap_style = CapButt;
304 gcv.line_width = 0; 388 gcv.line_width = 0;
305 XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv); 389 XChangeGC (disp, gc, GCLineWidth | GCCapStyle, &gcv);
306 390
307 while (a < b) 391 while (a < b)
308 { 392 {
309 uint32_t command = *a++; 393 uint32_t command = *a++;
310 394
317 int y2 = y_[(command >> 0) & 15]; 401 int y2 = y_[(command >> 0) & 15];
318 402
319 switch (op) 403 switch (op)
320 { 404 {
321 case 0: // line 405 case 0: // line
322 XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2); 406 XDrawLine (disp, d, gc, x1, y1, x2, y2);
323 break; 407 break;
324 408
325 case 1: // rectangle, possibly stippled 409 case 1: // rectangle, possibly stippled
326 if (a) 410 if (a)
327 { 411 {
328 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 }; 412 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
329 413
330 gcv.fill_style = FillStippled; 414 gcv.fill_style = FillStippled;
331 gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2); 415 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
332 gcv.ts_x_origin = x; 416 gcv.ts_x_origin = x;
333 gcv.ts_y_origin = y; 417 gcv.ts_y_origin = y;
334 418
335 XChangeGC (d.display->display, TGC, 419 XChangeGC (disp, gc,
336 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, 420 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
337 &gcv); 421 &gcv);
338 } 422 }
339 423
340 XFillRectangle (d.display->display, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1); 424 XFillRectangle (disp, d, gc, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
341 425
342 if (a) 426 if (a)
343 { 427 {
344 XFreePixmap (d.display->display, gcv.stipple); 428 XFreePixmap (disp, gcv.stipple);
345 gcv.stipple = 0; 429 gcv.stipple = 0;
346 gcv.fill_style = FillSolid; 430 gcv.fill_style = FillSolid;
347 XChangeGC (d.display->display, TGC, GCFillStyle, &gcv); 431 XChangeGC (disp, gc, GCFillStyle, &gcv);
348 } 432 }
349 break; 433 break;
350 case 2: // arc 434 case 2: // arc
351 XDrawArc (d.display->display, d, TGC, 435 XDrawArc (disp, d, gc,
352 x1 - W/2, y1 - H/2, W-1, H-1, 436 x1 - W/2, y1 - H/2, W-1, H-1,
353 (a - 1) * 90*64, (b - 1) * 90*64); 437 (a - 1) * 90*64, (b - 1) * 90*64);
354 break; 438 break;
355 } 439 }
356 } 440 }
357 } 441 }
442#else
443 if (0)
444 ;
445#endif
358#if ENABLE_COMBINING 446#if ENABLE_COMBINING
359 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t])) 447 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
360 { 448 {
449 min_it (width, 2); // we only support wcwidth up to 2
450 text_t chrs[2];
451 chrs [1] = NOCHAR;
452
453 *chrs = cc->c1;
361 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)]; 454 rxvt_font *f1 = (*fs)[fs->find_font_idx (cc->c1)];
362 f1->draw (d, x, y, &(t = cc->c1), 1, fg, bg); 455 f1->draw (d, x, y, chrs, width, fg, bg);
456
363 if (cc->c2 != NOCHAR) 457 if (cc->c2 != NOCHAR)
364 { 458 {
459 bool careful;
460
365 // prefer font of first character, for no good reasons 461 // prefer font of first character, for no good reasons
366 rxvt_font *f2 = f1->has_codepoint (cc->c2) 462 *chrs = cc->c2;
463 rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful)
367 ? f1 464 ? f1
368 : (*fs)[fs->find_font (cc->c2)]; 465 : (*fs)[fs->find_font_idx (cc->c2)];
369 466
370 f2->draw (d, x, y, &(t = cc->c2), 1, fg, -1); 467 f2->draw (d, x, y, chrs, width, fg, Color_none);
371 } 468 }
372 } 469 }
373#endif 470#endif
374 else 471 else
375 switch (t) 472 switch (t)
376 { 473 {
474 case '\t':
377 case ZERO_WIDTH_CHAR: 475 case ZERO_WIDTH_CHAR:
476 case NOCHAR:
378 break; 477 break;
379 478
479 /*
480 * If the base font does not support variation selectors, treat them as ZWC.
481 * a point could be made to do this for all wcwidth == 0 characters, but I
482 * decided against that until more data is available.
483 */
484 case 0xfe00: case 0xfe01: case 0xfe02: case 0xfe03: case 0xfe04: case 0xfe05: case 0xfe06: case 0xfe07:
485 case 0xfe08: case 0xfe09: case 0xfe0a: case 0xfe0b: case 0xfe0c: case 0xfe0d: case 0xfe0e: case 0xfe0f:
486 break;
487
380 default: 488 default:
381 int w = 0;
382 while (len > 0 && *text == NOCHAR)
383 {
384 ++text;
385 --len;
386 w += r->TermWin.fwidth;
387 }
388
389 XDrawRectangle (d.display->display, d, TGC, x + 2, y + 2, 489 XDrawRectangle (disp, d, gc, x + 2, y + 2,
390 w + r->TermWin.fwidth - 4, r->TermWin.fheight - 4); 490 fwidth - 4, term->fheight - 4);
391 x += w;
392 } 491 }
393 492
394 x += r->TermWin.fwidth; 493 x += fwidth;
494 }
495}
496
497struct rxvt_font_overflow : rxvt_font
498{
499 struct rxvt_fontset *fs;
500
501 rxvt_font_overflow (rxvt_fontset *fs)
502 : rxvt_font ()
503 {
504 this->fs = fs;
505 }
506
507 rxvt_fontprop properties ()
508 {
509 rxvt_fontprop p;
510
511 p.width = p.height = 1;
512 p.ascent = rxvt_fontprop::unset;
513 p.weight = rxvt_fontprop::medium;
514 p.slant = rxvt_fontprop::roman;
515
516 return p;
517 }
518
519 bool load (const rxvt_fontprop &prop, bool force_prop)
520 {
521 width = 1; height = 1;
522 ascent = 1; descent = 0;
523
524 set_name (strdup ("built-in rendition overflow font"));
525
526 return true;
527 }
528
529 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
530 {
531 return false;
532 }
533
534 void draw (rxvt_drawable &d, int x, int y,
535 const text_t *text, int len,
536 int fg, int bg)
537 {
538 while (len)
539 {
540 int fid = fs->find_font_idx (*text);
541 int w = 1;
542 while (w < len && text[w] == NOCHAR)
543 w++;
544 (*fs)[fid]->draw (d, x, y, text, w, fg, bg);
545 text += w;
546 len -= w;
547 x += term->fwidth * w;
395 } 548 }
396} 549 }
550};
397 551
398///////////////////////////////////////////////////////////////////////////// 552/////////////////////////////////////////////////////////////////////////////
399 553
400struct rxvt_font_x11 : rxvt_font { 554struct rxvt_font_x11 : rxvt_font
555{
401 rxvt_font_x11 () { f = 0; } 556 rxvt_font_x11 () { f = 0; }
402 557
403 void clear (); 558 void clear ();
404 559
405 rxvt_fontprop properties (); 560 rxvt_fontprop properties ();
406 561
407 bool load (const rxvt_fontprop &prop); 562 bool load (const rxvt_fontprop &prop, bool force_prop);
408 563
409 bool has_codepoint (unicode_t unicode); 564 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const;
410 565
411 void draw (rxvt_drawable &d, int x, int y, 566 void draw (rxvt_drawable &d, int x, int y,
412 const text_t *text, int len, 567 const text_t *text, int len,
413 int fg, int bg); 568 int fg, int bg);
414 569
570 bool slow; // whether this is a proportional font or has other funny characteristics
415 XFontStruct *f; 571 XFontStruct *f;
416 codeset cs;
417 bool enc2b, encm; 572 bool enc2b, encm;
418 573
419 char *get_property (XFontStruct *f, const char *property, const char *repl) const; 574 char *get_property (XFontStruct *f, Atom property, const char *repl) const;
420 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth); 575 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
421 bool set_properties (rxvt_fontprop &p, XFontStruct *f); 576 bool set_properties (rxvt_fontprop &p, XFontStruct *f);
422 bool set_properties (rxvt_fontprop &p, const char *name); 577 bool set_properties (rxvt_fontprop &p, const char *name);
423}; 578};
424 579
425char * 580char *
426rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const 581rxvt_font_x11::get_property (XFontStruct *f, Atom property, const char *repl) const
427{ 582{
428 unsigned long value; 583 unsigned long value;
429 584
430 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value)) 585 if (XGetFontProperty (f, property, &value))
431 return XGetAtomName (DISPLAY, value); 586 return XGetAtomName (term->dpy, value);
432 else 587 else
433 return rxvt_strdup (repl); 588 return repl ? strdup (repl) : 0;
434} 589}
435 590
436rxvt_fontprop 591rxvt_fontprop
437rxvt_font_x11::properties () 592rxvt_font_x11::properties ()
438{ 593{
442} 597}
443 598
444bool 599bool
445rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth) 600rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
446{ 601{
447 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2; 602 p.width = width != rxvt_fontprop::unset ? width
603 : avgwidth ? (avgwidth + 1) / 10
604 : (height + 1) / 2;
448 p.height = height; 605 p.height = height;
606 p.ascent = rxvt_fontprop::unset;
449 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium; 607 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
450 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic; 608 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
451 609
452 return true; 610 return true;
453} 611}
454 612
455bool 613bool
456rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) 614rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
457{ 615{
458 unsigned long height; 616 unsigned long height;
617
618#if 0
459 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) 619 if (!XGetFontProperty (f, XInternAtom (term->dpy, "PIXEL_SIZE", 0), &height))
460 return false; 620 return false;
621#else
622 height = f->ascent + f->descent;
623#endif
461 624
462 unsigned long avgwidth; 625 unsigned long avgwidth;
463 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) 626 if (!XGetFontProperty (f, term->xa [XA_AVERAGE_WIDTH], &avgwidth))
464 avgwidth = 0; 627 avgwidth = 0;
465 628
466 char *weight = get_property (f, "WEIGHT_NAME", "medium"); 629 char *weight = get_property (f, term->xa [XA_WEIGHT_NAME], "medium");
467 char *slant = get_property (f, "SLANT", "r"); 630 char *slant = get_property (f, term->xa [XA_SLANT], "r");
468 631
469 set_properties (p, height, weight, slant, avgwidth); 632 set_properties (p, height, weight, slant, avgwidth);
470 633
471 free (weight); 634 free (weight);
472 free (slant); 635 free (slant);
473 636
637 p.ascent = f->ascent;
638
474 return true; 639 return true;
475} 640}
476 641
477bool 642bool
478rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name) 643rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
479{ 644{
645 dTermDisplay;
480 int slashes = 0; 646 int slashes = 0;
481 const char *comp[13]; 647 const char *comp[13];
482 648
483 for (const char *c = name; *c; c++) 649 for (const char *c = name; *c; c++)
484 if (*c == '-') 650 if (*c == '-')
492 if (slashes >= 13 658 if (slashes >= 13
493 && (*comp[ 6] >= '1' && *comp[ 6] <= '9') 659 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
494 && (*comp[11] >= '0' && *comp[11] <= '9')) 660 && (*comp[11] >= '0' && *comp[11] <= '9'))
495 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11])); 661 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
496 662
497 XFontStruct *f = XLoadQueryFont (DISPLAY, name); 663 XFontStruct *f = XLoadQueryFont (disp, name);
498 664
499 if (f) 665 if (f)
500 { 666 {
501 // the font should really exist now. if not, we have a problem 667 // the font should really exist now. if not, we have a problem
502 // (e.g. if the user did xset fp rehash just when we were searching fonts). 668 // (e.g. if the user did xset fp rehash just when we were searching fonts).
503 // in that case, just return garbage. 669 // in that case, just return garbage.
504 bool ret = set_properties (p, f); 670 bool ret = set_properties (p, f);
505 XFreeFont (DISPLAY, f); 671 XFreeFont (disp, f);
506 return ret; 672 return ret;
507 } 673 }
508 else 674 else
509 return false; 675 return false;
510} 676}
511 677
512// fix the size of scalable fonts 678// fix the size of scalable fonts
513static void 679static bool
514fix_scalable (char *buf, const char *name, const rxvt_fontprop &prop) 680replace_field (char **ptr, const char *name, int index, const char old, const char *replace)
515{ 681{
516 int slashes = 0; 682 int slashes = 0;
517 const char *size; 683 const char *field, *end;
518 684
519 for (const char *c = name; *c; c++) 685 for (const char *c = name; *c; c++)
520 if (*c == '-') 686 if (*c == '-')
521 { 687 {
522 if (slashes == 6) 688 if (slashes == index)
523 size = c + 1; 689 field = c + 1;
690
691 if (slashes == index + 1)
692 end = c;
524 693
525 if (++slashes >= 13) 694 if (++slashes >= 13)
526 break; 695 break;
527 } 696 }
528 697
529 if (slashes >= 13 && size[0] == '0') 698 if (slashes >= 13 && (!old || *field == old))
530 { 699 {
531 strncpy (buf, name, size - name); 700 size_t len = field - name;
532 buf += size - name; 701 *ptr = (char *)malloc (len + strlen (replace) + strlen (end) + 1);
533 buf += sprintf (buf, "%d", prop.height); 702 memcpy (*ptr, name, len);
534 strcpy (buf, size + 1); 703 strcpy (*ptr + len, replace);
704 strcat (*ptr, end);
705
706 return true;
535 } 707 }
536 else 708 else
537 strcpy (buf, name); 709 {
710 *ptr = strdup (name);
711
712 return false;
713 }
538} 714}
539 715
540bool 716bool
541rxvt_font_x11::load (const rxvt_fontprop &prop) 717rxvt_font_x11::load (const rxvt_fontprop &prop, bool force_prop)
542{ 718{
719 dTermDisplay;
720
543 clear (); 721 clear ();
722
723 char field_str[64]; // enough for 128 bits
724
725 // first morph the font if required
726 if (force_prop)
727 {
728 char *fname;
729
730 if (name[0] != '-')
731 {
732 f = XLoadQueryFont (disp, name);
733
734 if (!f)
735 return false;
736
737 char *new_name = get_property (f, XA_FONT, 0);
738
739 if (new_name)
740 set_name (new_name);
741 else
742 rxvt_warn ("font '%s' has no FONT property, continuing without.\n", name);
743
744 XFreeFont (disp, f);
745 f = 0;
746 }
747
748 if (prop.weight != rxvt_fontprop::unset)
749 {
750 replace_field (&fname, name, 2, 0,
751 prop.weight < rxvt_fontprop::bold
752 ? "medium" : "bold");
753 set_name (fname);
754 }
755
756 if (prop.slant != rxvt_fontprop::unset)
757 {
758 replace_field (&fname, name, 3, 0,
759 prop.slant < rxvt_fontprop::italic
760 ? "r" : "i"); // TODO: handle "o"blique, too
761 set_name (fname);
762 }
763 }
764
765 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
766 ? 0 : prop.height);
767
768 struct font_weight {
769 char *name;
770 int diff;
771
772 void clear ()
773 {
774 name = 0;
775 diff = 0x7fffffff;
776 }
777
778 font_weight () { clear (); }
779 ~font_weight () { free (name); }
780 };
544 781
545 char **list; 782 char **list;
546 int count; 783 int count;
547 list = XListFonts (DISPLAY, name, 1024, &count); 784 list = XListFonts (disp, name, 4000, &count);
785
548 set_name (0); 786 set_name (0);
549 787
550 if (!list) 788 if (!list)
551 return false; 789 return false;
552 790
553 int bestdiff = 0x7fffffff; 791 font_weight *fonts = new font_weight[count];
792
554 for (int i = 0; i < count; i++) 793 for (int i = 0; i < count; i++)
555 { 794 {
556 rxvt_fontprop p; 795 rxvt_fontprop p;
557 char fname[1024]; 796 char *fname;
558 fix_scalable (fname, list[i], prop);
559 797
798 int diff = 0;
799
800 if (replace_field (&fname, list[i], 6, '0', field_str))
801 diff += 10; // slightly penalize scalable fonts
802 else
803 {
804 free (fname);
805 if (replace_field (&fname, list[i], 11, '0', "0"))
806 diff += 300; // more heavily penalize what looks like scaled bitmap fonts
807 }
808
560 if (!set_properties (p, fname)) 809 if (!set_properties (p, fname)
810 // also weed out too large fonts
811 || (prop.height != rxvt_fontprop::unset
812 && p.height > prop.height))
813 {
814 free (fname);
561 continue; 815 continue;
562
563 if (p.height > prop.height) // weed out too large fonts
564 continue;
565
566 int diff = (prop.height - p.height) * 32
567 + abs (prop.weight - p.weight)
568 + abs (prop.slant - p.slant );
569
570 if (!name // compare against best found so far
571 || diff < bestdiff)
572 { 816 }
573 set_name (strdup (fname)); 817
574 bestdiff = diff; 818 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128;
575 } 819 if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight);
820 if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant);
821 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
822
823 fonts[i].name = fname;
824 fonts[i].diff = diff;
576 } 825 }
577 826
578 XFreeFontNames (list); 827 XFreeFontNames (list);
579 828
580 if (!name) 829 // this loop only iterates when the guessed font-size is too small
581 return false; 830 for (;;)
831 {
832 font_weight *best = fonts;
582 833
583 f = XLoadQueryFont (DISPLAY, name); 834 for (font_weight *w = fonts + 1; w < fonts + count; w++)
835 if (w->diff < best->diff)
836 best = w;
837
838 if (!best->name
839 || !(f = XLoadQueryFont (disp, best->name)))
840 break;
841
842 set_name (best->name);
843 best->clear ();
844
845 ascent = f->ascent;
846 descent = f->descent;
847 height = ascent + descent;
848
849 if (prop.height == rxvt_fontprop::unset
850 || height <= prop.height)
851 break; // font is ready for use
852
853 // PIXEL_SIZE small enough, but real height too large
854 clear ();
855 }
856
857 delete [] fonts;
584 858
585 if (!f) 859 if (!f)
586 return false; 860 return false;
587 861
588 char *registry = get_property (f, "CHARSET_REGISTRY", 0); 862 char *registry = get_property (f, term->xa [XA_CHARSET_REGISTRY], 0);
589 char *encoding = get_property (f, "CHARSET_ENCODING", 0); 863 char *encoding = get_property (f, term->xa [XA_CHARSET_ENCODING], 0);
864
865 cs = CS_UNKNOWN;
590 866
591 if (registry && encoding) 867 if (registry && encoding)
592 { 868 {
593 char charset[64]; 869 char charset[64];
594 snprintf (charset, 64, "%s-%s", registry, encoding); 870 snprintf (charset, 64, "%s-%s", registry, encoding);
595 871
596 cs = codeset_from_name (charset); 872 cs = codeset_from_name (charset);
597 } 873
598 else 874 if (cs == CS_UNKNOWN)
875 rxvt_warn ("%s: cannot deduce encoding from registry/encoding properties \"%s\", ignoring font.\n", name, charset);
599 { 876 }
877
878 free (registry);
879 free (encoding);
880
881 if (cs == CS_UNKNOWN)
882 {
600 const char *charset = get_property (f, "FONT", 0); 883 char *value = get_property (f, XA_FONT, 0);
884 const char *charset = value;
601 885
602 if (!charset) 886 if (!charset)
603 charset = name; 887 charset = name;
604 888
605 int count = 13; 889 int count = 13;
606 while (*charset) 890 while (*charset)
607 if (*charset++ == '-' && !--count) 891 if (*charset++ == '-' && !--count)
608 break; 892 break;
609 893
610 cs = codeset_from_name (charset); 894 cs = codeset_from_name (charset);
895 if (cs == CS_UNKNOWN)
896 rxvt_warn ("%s: cannot deduce encoding from font name property \"%s\", ignoring font.\n", name, charset);
897
898 free (value);
899 }
900
901 if (cs == CS_UNKNOWN)
611 } 902 {
612 903 clear ();
613 free (registry); 904 return false;
614 free (encoding); 905 }
615 906
616 if (cs == CS_UNICODE) 907 if (cs == CS_UNICODE)
617 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font 908 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
618 909
619 encm = f->min_byte1 != 0 || f->max_byte1 != 0; 910 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
620 enc2b = encm || f->max_char_or_byte2 > 255; 911 enc2b = encm || f->max_char_or_byte2 > 255;
621 912
622 ascent = f->ascent;
623 descent = f->descent;
624 height = ascent + descent;
625
626 slow = false; 913 slow = false;
627 914
915#if 1 // only used for slow detection, TODO optimize
628 if (f->min_bounds.width == f->max_bounds.width) 916 if (f->min_bounds.width == f->max_bounds.width || !f->per_char)
629 width = f->min_bounds.width;
630 else if (f->per_char == NULL)
631 width = f->max_bounds.width; 917 width = f->max_bounds.width;
632 else 918 else
633 { 919 {
634 slow = true; 920 slow = true;
635 921
636 int N = f->max_char_or_byte2 - f->min_char_or_byte2; 922 int N = f->max_char_or_byte2 - f->min_char_or_byte2;
637 923
638 if (encm) 924 if (encm)
639 N += (f->max_byte1 - f->min_byte1) 925 N += (f->max_byte1 - f->min_byte1)
640 * (f->max_char_or_byte2 - f->min_char_or_byte2 + 1); 926 * (f->max_char_or_byte2 - f->min_char_or_byte2 + 1);
641 927
642 while (N) 928 while (N)
643 { 929 {
644 if (f->per_char[N].width > width) 930 if (f->per_char[N].width > width)
645 width = f->per_char[N].width; 931 width = f->per_char[N].width;
646 932
647 --N; 933 --N;
648 } 934 }
649 } 935 }
936#endif
650 937
651 if (cs == CS_UNKNOWN) 938 width = 1;
939
940 for (uint16_t *t = extent_test_chars; t < extent_test_chars + ecb_array_length (extent_test_chars); t++)
941 {
942 if (FROM_UNICODE (cs, *t) == NOCHAR)
943 continue;
944
945 // ignore characters we wouldn't use anyways
946 bool careful;
947 if (!has_char (*t, &prop, careful))
948 continue;
949
950 // the casts are needed in C++11 (see 8.5.1)
951 XChar2b ch = { (unsigned char)(*t >> 8), (unsigned char)*t };
952
953 XCharStruct g;
954 int dir_ret, asc_ret, des_ret;
955 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
956
957 int wcw = WCWIDTH (*t);
958 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
959
960 if (width < g.width) width = g.width;
652 { 961 }
653 fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name);
654 962
963#if 0 // do it per-character
964 if (prop && width > prop->width)
965 {
655 clear (); 966 clear ();
656
657 return false; 967 return false;
658 } 968 }
969#endif
659 970
660 return true; 971 return true;
661} 972}
662 973
663void 974void
664rxvt_font_x11::clear () 975rxvt_font_x11::clear ()
665{ 976{
666 if (f) 977 if (f)
667 { 978 {
668 XFreeFont (DISPLAY, f); 979 XFreeFont (term->dpy, f);
669 f = 0; 980 f = 0;
670 } 981 }
671} 982}
672 983
673bool 984bool
674rxvt_font_x11::has_codepoint (unicode_t unicode) 985rxvt_font_x11::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
675{ 986{
987 careful = false;
988
676 uint32_t ch = FROM_UNICODE (cs, unicode); 989 uint32_t ch = FROM_UNICODE (cs, unicode);
677 990
678 if (ch == NOCHAR) 991 if (ch == NOCHAR)
679 return false; 992 return false;
680 993
681 /* check wether the character exists in _this_ font. horrible. */ 994 /* check whether the character exists in _this_ font. horrible. */
682 XCharStruct *xcs; 995 XCharStruct *xcs;
683 996
684 if (encm) 997 if (encm)
685 { 998 {
686 unsigned char byte1 = ch >> 8; 999 unsigned char byte1 = ch >> 8;
688 1001
689 if (byte1 < f->min_byte1 || byte1 > f->max_byte1 1002 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
690 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2) 1003 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
691 return false; 1004 return false;
692 1005
693 if (!f->per_char) 1006 if (f->per_char)
694 return true; 1007 {
695
696 int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1; 1008 int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1;
697 int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2; 1009 int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2;
698 1010
699 xcs = f->per_char + N; 1011 xcs = f->per_char + N;
1012 }
1013 else
1014 xcs = &f->max_bounds;
700 } 1015 }
701 else 1016 else
702 { 1017 {
703 if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2) 1018 if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2)
704 return false; 1019 return false;
705 1020
706 if (!f->per_char) 1021 if (f->per_char)
707 return true;
708
709 xcs = f->per_char + (ch - f->min_char_or_byte2); 1022 xcs = f->per_char + (ch - f->min_char_or_byte2);
1023 else
1024 xcs = &f->max_bounds;
710 } 1025 }
711 1026
712 if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0 1027 if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0
713 && xcs->ascent == 0 && xcs->descent == 0) 1028 && xcs->ascent == 0 && xcs->descent == 0)
1029 return false;
1030
1031 if (!prop || prop->width == rxvt_fontprop::unset)
1032 return true;
1033
1034 // check whether character overlaps previous/next character
1035 int w = xcs->rbearing - xcs->lbearing;
1036 int wcw = max (WCWIDTH (unicode), 1);
1037
1038 careful = xcs->lbearing < 0 || xcs->rbearing > prop->width * wcw;
1039
1040 if (careful && !OVERLAP_OK (w, wcw, prop))
714 return false; 1041 return false;
715 1042
716 return true; 1043 return true;
717} 1044}
718 1045
724 // this looks like a mess /. 1051 // this looks like a mess /.
725 // and it is a mess /. 1052 // and it is a mess /.
726 // yet we are trying to be perfect /. 1053 // yet we are trying to be perfect /.
727 // but the result still isn't perfect /. 1054 // but the result still isn't perfect /.
728 1055
1056 dTermDisplay;
1057 dTermGC;
1058
729 bool slow = this->slow 1059 bool slow = this->slow
730 || width != r->TermWin.fwidth 1060 || width != term->fwidth
731 || height != r->TermWin.fheight; 1061 || height != term->fheight
1062 || ascent != f->ascent;
732 1063
733 int base = r->TermWin.fbase; 1064 int base = ascent; // sorry, incorrect: term->fbase;
734 1065
735 XGCValues v; 1066 XGCValues v;
736 v.foreground = r->PixColors[fg]; 1067 v.foreground = term->pix_colors[fg];
737 v.font = f->fid; 1068 v.font = f->fid;
738 1069
739 if (enc2b) 1070 if (enc2b)
740 { 1071 {
741 const XChar2b *xc = enc_xchar2b (text, len, cs, slow); 1072 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
742 1073
743 if (bg == Color_bg && !slow) 1074 if (bg == Color_bg && !slow)
744 { 1075 {
745 v.background = r->PixColors[bg]; 1076 v.background = term->pix_colors[bg];
746 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 1077 XChangeGC (disp, gc, GCForeground | GCBackground | GCFont, &v);
747 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len); 1078 XDrawImageString16 (disp, d, gc, x, y + base, xc, len);
748 } 1079 }
749 else 1080 else
750 { 1081 {
751 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1082 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
752 1083
753 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 1084 XChangeGC (disp, gc, GCForeground | GCFont, &v);
754 1085
755 if (slow) 1086 if (slow)
756 { 1087 {
757 do 1088 do
758 { 1089 {
759 if (xc->byte1 || xc->byte2) 1090 if (xc->byte1 || xc->byte2)
760 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1); 1091 XDrawString16 (disp, d, gc, x, y + base, xc, 1);
761 1092
762 x += r->TermWin.fwidth; 1093 x += term->fwidth;
763 xc++; len--; 1094 xc++; len--;
764 } 1095 }
765 while (len); 1096 while (len);
766 } 1097 }
767 else 1098 else
768 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, len); 1099 XDrawString16 (disp, d, gc, x, y + base, xc, len);
769 } 1100 }
770 } 1101 }
771 else 1102 else
772 { 1103 {
773 const char *xc = enc_char (text, len, cs, slow); 1104 const char *xc = enc_char (text, len, cs, slow);
774 1105
775 if (bg == Color_bg && !slow) 1106 if (bg == Color_bg && !slow)
776 { 1107 {
777 v.background = r->PixColors[bg]; 1108 v.background = term->pix_colors[bg];
778 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 1109 XChangeGC (disp, gc, GCForeground | GCBackground | GCFont, &v);
779 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len); 1110 XDrawImageString (disp, d, gc, x, y + base, xc, len);
780 } 1111 }
781 else 1112 else
782 { 1113 {
783 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1114 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
784 1115
785 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 1116 XChangeGC (disp, gc, GCForeground | GCFont, &v);
786 1117
787 if (slow) 1118 if (slow)
788 { 1119 {
789 do 1120 do
790 { 1121 {
791 if (*xc) 1122 if (*xc)
792 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1); 1123 XDrawString (disp, d, gc, x, y + base, xc, 1);
793 1124
794 x += r->TermWin.fwidth; 1125 x += term->fwidth;
795 xc++; len--; 1126 xc++; len--;
796 } 1127 }
797 while (len); 1128 while (len);
798 } 1129 }
799 else 1130 else
800 XDrawString (d.display->display, d, TGC, x, y + base, xc, len); 1131 XDrawString (disp, d, gc, x, y + base, xc, len);
801 } 1132 }
802 } 1133 }
803} 1134}
804 1135
805///////////////////////////////////////////////////////////////////////////// 1136/////////////////////////////////////////////////////////////////////////////
806 1137
807#if XFT 1138#if XFT
808#if 0
809#define UNIBITS 21
810//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
811#define SWATHBITS 8
812#endif
813 1139
814struct rxvt_font_xft : rxvt_font { 1140struct rxvt_font_xft : rxvt_font
1141{
815 rxvt_font_xft () { f = 0; } 1142 rxvt_font_xft ()
1143 {
1144 f = 0;
1145 }
816 1146
817 void clear (); 1147 void clear ();
818 1148
819 rxvt_fontprop properties (); 1149 rxvt_fontprop properties ();
820 1150
821 bool load (const rxvt_fontprop &prop); 1151 bool load (const rxvt_fontprop &prop, bool force_prop);
822 1152
823 void draw (rxvt_drawable &d, int x, int y, 1153 void draw (rxvt_drawable &d, int x, int y,
824 const text_t *text, int len, 1154 const text_t *text, int len,
825 int fg, int bg); 1155 int fg, int bg);
826 1156
827 bool has_codepoint (unicode_t unicode); 1157 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const;
828 1158
829protected: 1159protected:
830 XftFont *f; 1160 XftFont *f;
831}; 1161};
832 1162
833void 1163void
834rxvt_font_xft::clear () 1164rxvt_font_xft::clear ()
835{ 1165{
836 if (f) 1166 if (f)
837 { 1167 {
838 XftFontClose (DISPLAY, f); 1168 XftFontClose (term->dpy, f);
839 f = 0; 1169 f = 0;
840 } 1170 }
841} 1171}
842 1172
843rxvt_fontprop 1173rxvt_fontprop
847 1177
848 FT_Face face = XftLockFace (f); 1178 FT_Face face = XftLockFace (f);
849 1179
850 p.width = width; 1180 p.width = width;
851 p.height = height; 1181 p.height = height;
1182 p.ascent = ascent;
852 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD 1183 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
853 ? rxvt_fontprop::bold : rxvt_fontprop::medium; 1184 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
854 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC 1185 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
855 ? rxvt_fontprop::italic : rxvt_fontprop::roman; 1186 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
856 1187
858 1189
859 return p; 1190 return p;
860} 1191}
861 1192
862bool 1193bool
863rxvt_font_xft::load (const rxvt_fontprop &prop) 1194rxvt_font_xft::load (const rxvt_fontprop &prop, bool force_prop)
864{ 1195{
865#if 0 1196 dTermDisplay;
866 for (int i = 0; i < SWATHCOUNT; i++)
867 cvr[i] = 0;
868#endif
869 1197
870 clear (); 1198 clear ();
871 1199
872 FcPattern *p = FcNameParse ((FcChar8 *) name); 1200 FcPattern *p = FcNameParse ((FcChar8 *) name);
873 1201
874 if (!p) 1202 if (!p)
875 return false; 1203 return false;
876 1204
877 FcValue v; 1205 FcValue v;
878 1206
1207 if (prop.height != rxvt_fontprop::unset
879 if (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch) 1208 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1209 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
880 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); 1210 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
881 1211
1212 if (prop.weight != rxvt_fontprop::unset
882 if (FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) 1213 && (force_prop || FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch))
883 FcPatternAddInteger (p, FC_WEIGHT, prop.weight); 1214 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
884 1215
1216 if (prop.slant != rxvt_fontprop::unset
885 if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch) 1217 && (force_prop || FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch))
886 FcPatternAddInteger (p, FC_SLANT, prop.slant); 1218 FcPatternAddInteger (p, FC_SLANT, prop.slant);
887
888 if (FcPatternGet (p, FC_MINSPACE, 0, &v) != FcResultMatch)
889 FcPatternAddBool (p, FC_MINSPACE, 1);
890 1219
891#if 0 // clipping unfortunately destroys our precious double-width-characters 1220#if 0 // clipping unfortunately destroys our precious double-width-characters
892 // clip width, we can't do better, or can we? 1221 // clip width, we can't do better, or can we?
893 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch) 1222 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
894 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width); 1223 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width);
895#endif 1224#endif
896 1225
1226 if (FcPatternGet (p, FC_MINSPACE, 0, &v) != FcResultMatch)
1227 FcPatternAddBool (p, FC_MINSPACE, 1);
1228
1229 // store generated name so iso14755 view gives better results
1230 set_name ((char *)FcNameUnparse (p));
1231
897 XftResult result; 1232 XftResult result;
898 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1233 FcPattern *match = XftFontMatch (disp, term->display->screen, p, &result);
899 1234
900 FcPatternDestroy (p); 1235 FcPatternDestroy (p);
901 1236
902 if (!match) 1237 if (!match)
903 return false; 1238 return false;
904 1239
905 f = XftFontOpenPattern (DISPLAY, match);
906
907 if (!f)
908 {
909 FcPatternDestroy (match);
910 return false;
911 }
912
913 FT_Face face = XftLockFace (f);
914
915 slow = !FT_IS_FIXED_WIDTH (face);
916
917 int ftheight = 0; 1240 int ftheight = 0;
1241 bool success = true;
918 1242
919 for (;;) 1243 for (;;)
920 { 1244 {
921 XGlyphInfo g1, g2; 1245 p = FcPatternDuplicate (match);
922 FcChar8 c; 1246 f = XftFontOpenPattern (disp, p);
923 1247
924 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1); 1248 if (!f)
925 c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2); 1249 {
1250 FcPatternDestroy (p);
1251 success = false;
1252 break;
1253 }
926 1254
927 if (g1.xOff != g2.xOff) // don't simply trust the font 1255 FT_Face face = XftLockFace (f);
928 slow = true;
929 1256
930 width = g2.xOff;
931 ascent = (face->size->metrics.ascender + 63) >> 6; 1257 ascent = (face->size->metrics.ascender + 63) >> 6;
932 descent = (-face->size->metrics.descender + 63) >> 6; 1258 descent = (-face->size->metrics.descender + 63) >> 6;
933 height = ascent + descent; 1259 height = max (ascent + descent, (face->size->metrics.height + 63) >> 6);
1260 width = 0;
934 1261
1262 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1263
1264 XftUnlockFace (f);
1265
1266 int glheight = height;
1267
1268 for (uint16_t *t = extent_test_chars; t < extent_test_chars + ecb_array_length (extent_test_chars); t++)
1269 {
1270 FcChar16 ch = *t;
1271
1272 if (cs != CS_UNICODE
1273 && ch > 0x100
1274 && FROM_UNICODE (cs, ch) == NOCHAR)
1275 continue;
1276
1277 // ignore characters we wouldn't use anyways
1278 bool careful;
1279 if (!has_char (*t, &prop, careful))
1280 continue;
1281
1282 XGlyphInfo g;
1283 XftTextExtents16 (disp, f, &ch, 1, &g);
1284
1285 g.width -= g.x;
1286
1287 int wcw = WCWIDTH (ch);
1288 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1289
1290 if (width < g.width ) width = g.width;
1291 if (height < g.height ) height = g.height;
1292 if (glheight < g.height - g.y) glheight = g.height - g.y;
1293 }
1294
1295 if (!width)
1296 {
1297 rxvt_warn ("unable to calculate font width for '%s', using max_advance_width.\n", name);
1298 width = f->max_advance_width;
1299 break;
1300 }
1301
1302 if (prop.height == rxvt_fontprop::unset
935 if (height <= prop.height || !prop.height) 1303 || (height <= prop.height && glheight <= prop.height)
1304 || height <= 2
1305 || !scalable)
936 break; 1306 break;
937 1307
938 if (ftheight) 1308 if (ftheight)
939 { 1309 {
940 // take smaller steps near the end 1310 // take smaller steps near the end
941 if (height > prop.height + 1) ftheight++; 1311 if (height > prop.height + 1) ftheight++;
942 if (height > prop.height + 2) ftheight++; 1312 if (height > prop.height + 2) ftheight++;
943 if (height > prop.height + 3) ftheight++; 1313 if (height > prop.height + 3) ftheight++;
944 1314
945 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - prop.height); 1315 ftheight -= height - prop.height;
946 } 1316 }
947 else 1317 else
948 FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height); 1318 ftheight = prop.height - 1;
1319
1320 XftFontClose (disp, f);
1321 FcPatternDel (match, FC_PIXEL_SIZE);
1322 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1323 }
1324
1325 FcPatternDestroy (match);
1326
1327#if 0 // do it per-character
1328 if (prop.width != rxvt_fontprop::unset && width > prop.width)
949 } 1329 {
1330 clear ();
1331 success = false;
1332 }
1333#endif
950 1334
951 XftUnlockFace (f); 1335 return success;
1336}
1337
1338bool
1339rxvt_font_xft::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
1340{
1341 careful = false;
1342
1343 rxvt_compose_expand_static<FcChar32> exp;
1344 FcChar32 *chrs = exp (unicode);
1345 int nchrs = exp.length (chrs);
1346
1347 // we handle the sequence if the first char is available
1348 if (!XftCharExists (term->dpy, f, chrs [0]))
1349 return false;
1350
1351 if (!prop || prop->width == rxvt_fontprop::unset)
1352 return true;
1353
1354 // but we check against the whole sequence bounding box
1355
1356 // check character against base font bounding box
1357 FcChar32 ch = unicode;
1358 XGlyphInfo g;
1359 XftTextExtents32 (term->dpy, f, chrs, nchrs, &g);
1360
1361 int w = g.width - g.x;
1362 int wcw = max (WCWIDTH (chrs [0]), 1);
1363
1364 careful = g.x > 0 || w > prop->width * wcw;
1365
1366 if (careful && !OVERLAP_OK (w, wcw, prop))
1367 return false;
1368
1369 // this weeds out _totally_ broken fonts, or glyphs
1370 if (!OVERLAP_OK (g.xOff, wcw, prop))
1371 return false;
952 1372
953 return true; 1373 return true;
954}
955
956bool
957rxvt_font_xft::has_codepoint (unicode_t unicode)
958{
959 return XftCharExists (DISPLAY, f, unicode);
960} 1374}
961 1375
962void 1376void
963rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1377rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
964 const text_t *text, int len, 1378 const text_t *text, int len,
965 int fg, int bg) 1379 int fg, int bg)
966{ 1380{
967 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1381 XGlyphInfo extents;
1382 //XftGlyphSpec *enc = rxvt_temp_buf<XftGlyphSpec> (len);//D
1383 //XftGlyphSpec *ep = enc;//D
1384 static vector<XftGlyphSpec> enc; enc.resize (0); // static to avoid malloc, still slow
968 1385
969 if (!slow && width == r->TermWin.fwidth && 0) 1386 dTermDisplay;
970 { 1387 dTermGC;
971 if (sizeof (text_t) == sizeof (FcChar16)) 1388
972 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len); 1389 int w = term->fwidth * len;
973 else 1390 int h = term->fheight;
974 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len); 1391
975 } 1392 bool buffered = bg >= Color_transparent
976 else 1393 && term->option (Opt_buffered);
977 { 1394
1395 // cut trailing spaces
1396 while (len && text [len - 1] == ' ')
1397 len--;
1398
1399 int x_ = buffered ? 0 : x;
1400 int y_ = buffered ? 0 : y;
1401
978 while (len) 1402 while (len)
1403 {
1404 int cwidth = term->fwidth;
1405
1406 rxvt_compose_expand_static<FcChar32> exp;
1407 FcChar32 *chrs = exp (*text++); len--;
1408 int nchrs = exp.length (chrs);
1409
1410 while (len && *text == NOCHAR)
1411 text++, len--, cwidth += term->fwidth;
1412
1413 if (chrs [0] != ' ') // skip spaces
979 { 1414 {
980 if (*text != NOCHAR && *text != ' ') 1415 FT_UInt glyphs [rxvt_compose_expand_static<FcChar32>::max_size];
1416
1417 for (int i = 0; i < nchrs; ++i)
1418 glyphs [i] = XftCharIndex (disp, f, chrs [i]);
1419
1420 XftGlyphExtents (disp, f, glyphs, nchrs, &extents);
1421
1422 XftGlyphSpec ep;
1423
1424 ep.x = x_ + (cwidth - extents.xOff >> 1);
1425 ep.y = y_ + ascent;
1426
1427 if (extents.xOff == 0)
1428 ep.x = x_ + cwidth;
1429
1430 for (int i = 0; i < nchrs; ++i)
981 { 1431 {
982 int fwidth = r->TermWin.fwidth; 1432 ep.glyph = glyphs [i];
983 if (len >= 2 && text[1] == NOCHAR) 1433 enc.push_back (ep);
984 fwidth *= 2; 1434 }
1435 }
985 1436
986 XGlyphInfo extents; 1437 x_ += cwidth;
987 if (sizeof (text_t) == sizeof (FcChar16)) 1438 }
1439
1440 if (buffered)
1441 {
1442 if (!enc.empty ())
1443 {
1444 rxvt_drawable &d2 = d.screen->scratch_drawable (w, h);
1445
1446#ifdef HAVE_IMG
1447 Picture dst = 0; // the only assignment is done conditionally in the following if condition
1448
1449 if (term->bg_img
1450 && (bg == Color_transparent || bg == Color_bg
1451 || (bg >= 0 && !term->pix_colors[bg].is_opaque () && ((dst = XftDrawPicture (d2))))))
1452 {
1453 int src_x = x, src_y = y;
1454
1455 if (term->bg_flags & rxvt_term::BG_IS_TRANSPARENT)
988 { 1456 {
989 XftTextExtents16 (d.display->display, f, (const FcChar16 *)text, 1, &extents); 1457 src_x += term->window_vt_x;
990 XftDrawString16 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2, 1458 src_y += term->window_vt_y;
991 y + r->TermWin.fbase, (const FcChar16 *)text, 1); 1459 }
1460
1461 if (term->bg_img->w >= src_x + w
1462 && term->bg_img->h >= src_y + h)
1463 {
1464 XCopyArea (disp, term->bg_img->pm, d2, gc,
1465 src_x, src_y, w, h, 0, 0);
992 } 1466 }
993 else 1467 else
994 { 1468 {
995 XGlyphInfo extents; 1469 XGCValues gcv;
996 XftTextExtents32 (d.display->display, f, (const FcChar32 *)text, 1, &extents); 1470
997 XftDrawString32 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2, 1471 gcv.fill_style = FillTiled;
998 y + r->TermWin.fbase, (const FcChar32 *)text, 1); 1472 gcv.tile = term->bg_img->pm;
1473 gcv.ts_x_origin = -src_x;
1474 gcv.ts_y_origin = -src_y;
1475
1476 XChangeGC (disp, gc,
1477 GCTile | GCTileStipXOrigin | GCTileStipYOrigin | GCFillStyle,
1478 &gcv);
1479
1480 XFillRectangle (disp, d2, gc, 0, 0, w, h);
1481
1482 gcv.fill_style = FillSolid;
1483 XChangeGC (disp, gc, GCFillStyle, &gcv);
1484 }
1485
1486 if (dst)
1487 {
1488 Picture solid_color_pict = XftDrawSrcPicture (d2, &term->pix_colors[bg].c);
1489
1490 // dst can only be set when bg >= 0
1491 XRenderComposite (disp, PictOpOver, solid_color_pict, None, dst, 0, 0, 0, 0, 0, 0, w, h);
999 } 1492 }
1000 } 1493 }
1001
1002 x += r->TermWin.fwidth;
1003 text++;
1004 len--; 1494 else
1495#endif
1496 XftDrawRect (d2, &term->pix_colors[bg >= 0 ? bg : Color_bg].c, 0, 0, w, h);
1497
1498 XftDrawGlyphSpec (d2, &term->pix_colors[fg].c, f, &enc[0], enc.size ());
1499 XCopyArea (disp, d2, d, gc, 0, 0, w, h, x, y);
1005 } 1500 }
1501 else
1502 clear_rect (d, x, y, w, h, bg);
1503 }
1504 else
1006 } 1505 {
1506 clear_rect (d, x, y, w, h, bg);
1507 XftDrawGlyphSpec (d, &term->pix_colors[fg].c, f, &enc[0], enc.size ());
1508 }
1007} 1509}
1510
1008#endif 1511#endif
1009 1512
1010///////////////////////////////////////////////////////////////////////////// 1513/////////////////////////////////////////////////////////////////////////////
1011 1514
1012rxvt_fontset::rxvt_fontset (rxvt_t r) 1515rxvt_fontset::rxvt_fontset (rxvt_term *term)
1013: r (r), fontdesc (0) 1516: fontdesc (0), term (term)
1014{ 1517{
1015 clear (); 1518 clear ();
1016} 1519}
1017 1520
1018rxvt_fontset::~rxvt_fontset () 1521rxvt_fontset::~rxvt_fontset ()
1021} 1524}
1022 1525
1023void 1526void
1024rxvt_fontset::clear () 1527rxvt_fontset::clear ()
1025{ 1528{
1529 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1530 = rxvt_fontprop::unset;
1531 force_prop = false;
1532
1026 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) 1533 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1027 FONT_UNREF (*i); 1534 (*i)->unref ();
1535
1536 for (pagemap **p = fmap.begin (); p != fmap.end (); p++)
1537 delete *p;
1028 1538
1029 free (fontdesc); fontdesc = 0; 1539 free (fontdesc); fontdesc = 0;
1030 1540
1031 fonts.clear (); 1541 fonts.clear ();
1032 base_id = 0;
1033 base_prop.height = 0x7fffffff;
1034 base_prop.weight = rxvt_fontprop::medium;
1035 base_prop.slant = rxvt_fontprop::roman;
1036 1542
1037 fallback = fallback_fonts; 1543 fallback = fallback_fonts;
1544}
1545
1546void
1547rxvt_fontset::prepare_font (rxvt_font *font, codeset cs)
1548{
1549 font->set_term (term);
1550
1551 font->cs = cs;
1552 font->loaded = false;
1038} 1553}
1039 1554
1040rxvt_font * 1555rxvt_font *
1041rxvt_fontset::new_font (const char *name, codeset cs) 1556rxvt_fontset::new_font (const char *name, codeset cs)
1042{ 1557{
1043 rxvt_font *f; 1558 rxvt_font *f;
1044 1559
1045 if (!name || !*name) 1560 if (!name || !*name)
1046 { 1561 {
1047 name = ""; 1562 name = "";
1048 f = new rxvt_font_default; 1563 f = new rxvt_font_default (this);
1049 } 1564 }
1050#if XFT 1565#if XFT
1051 else if (!strncmp (name, "xft:", 4)) 1566 else if (!strncmp (name, "xft:", 4))
1052 { 1567 {
1053 name += 4; 1568 name += 4;
1054 f = new rxvt_font_xft; 1569 f = new rxvt_font_xft ();
1055 } 1570 }
1056#endif 1571#endif
1057 else if (!strncmp (name, "x:", 2)) 1572 else if (!strncmp (name, "x:", 2))
1058 { 1573 {
1059 name += 2; 1574 name += 2;
1060 f = new rxvt_font_x11; 1575 f = new rxvt_font_x11;
1061 } 1576 }
1062 else 1577 else
1063 f = new rxvt_font_x11; 1578 f = new rxvt_font_x11;
1064 1579
1065 f->fs = this;
1066 f->set_term (r);
1067 f->set_name (strdup (name)); 1580 f->set_name (strdup (name));
1068 1581 prepare_font (f, cs);
1069 f->cs = cs;
1070 f->loaded = false;
1071 1582
1072 return f; 1583 return f;
1073} 1584}
1074 1585
1075///////////////////////////////////////////////////////////////////////////// 1586/////////////////////////////////////////////////////////////////////////////
1587
1588void
1589rxvt_fontset::push_font (rxvt_font *font)
1590{
1591 // the fontCount index is reserved for the overflow font, it is only
1592 // necessary when we get fontCount or more fonts, as they cannot be
1593 // represented in the rendition.
1594 if (fonts.size () == fontCount)
1595 {
1596 rxvt_font *f = new rxvt_font_overflow (this);
1597
1598 prepare_font (f, CS_UNICODE);
1599 fonts.push_back (f);
1600 }
1601
1602 fonts.push_back (font);
1603}
1076 1604
1077void 1605void
1078rxvt_fontset::add_fonts (const char *desc) 1606rxvt_fontset::add_fonts (const char *desc)
1079{ 1607{
1080 if (desc) 1608 if (desc)
1082 char buf[512]; 1610 char buf[512];
1083 const char *end; 1611 const char *end;
1084 1612
1085 do 1613 do
1086 { 1614 {
1087 while (*desc <= ' ') desc++; 1615 while (*desc && *desc <= ' ')
1616 desc++;
1617
1618 codeset cs = CS_UNICODE;
1088 1619
1089 if (*desc == '[') 1620 if (*desc == '[')
1090 { 1621 {
1091 fprintf (stderr, "extra font parameters not yet supported, skipping.\n"); 1622 char spec[256];
1092
1093 //const char *extra = desc++; // not yet used 1623 const char *extra = ++desc; // not yet used
1094 1624
1095 desc = strchr (desc, ']'); 1625 desc = strchr (desc, ']');
1096 1626
1097 if (!desc) 1627 if (!desc)
1098 { 1628 {
1099 fprintf (stderr, "ERROR: opening '[' without closing ']' in font specification.\n"); 1629 rxvt_warn ("ERROR: opening '[' without closing ']' in font specification, trying to continue.\n");
1100 break; 1630 break;
1101 } 1631 }
1102 1632
1633 memcpy (spec, extra, min (desc - extra, 255));
1634 spec[min (desc - extra, 255)] = 0;
1635
1636 if (!strncmp (extra, "codeset=", sizeof ("codeset=") - 1))
1637 cs = codeset_from_name (spec + sizeof ("codeset=") - 1);
1638 else
1639 rxvt_warn ("unknown parameter '%s' in font specification, skipping.\n", spec);
1640
1103 desc++; 1641 desc++;
1104 while (*desc <= ' ') desc++; 1642 while (*desc <= ' ' && *desc) desc++;
1105 } 1643 }
1106 1644
1107 end = strchr (desc, ','); 1645 end = strchr (desc, ',');
1108 if (!end) 1646 if (!end)
1109 end = desc + strlen (desc); 1647 end = desc + strlen (desc);
1110 1648
1111 if (end - desc < 511) 1649 if (end - desc < 511)
1112 { 1650 {
1113 strncpy (buf, desc, end - desc); 1651 memcpy (buf, desc, end - desc);
1114 buf[end - desc] = 0; 1652 buf[end - desc] = 0;
1115 1653
1116 fonts.push_back (new_font (buf, CS_UNICODE)); 1654 push_font (new_font (buf, cs));
1117 } 1655 }
1656 else
1657 rxvt_warn ("fontset element too long (>511 bytes), ignored.\n");
1118 1658
1119 desc = end + 1; 1659 desc = end + 1;
1120 } 1660 }
1121 while (*end); 1661 while (*end);
1122 } 1662 }
1123} 1663}
1124 1664
1125bool 1665bool
1126rxvt_fontset::realize_font (int i) 1666rxvt_fontset::realize_font (int i)
1127{ 1667{
1668 if (i < 0 || i >= fonts.size ())
1669 return false;
1670
1128 if (fonts[i]->loaded) 1671 if (fonts[i]->loaded)
1129 return true; 1672 return true;
1130 1673
1131 fonts[i]->loaded = true; 1674 fonts[i]->loaded = true;
1132 1675
1133 if (!fonts[i]->load (base_prop)) 1676 if (!fonts[i]->load (prop, force_prop))
1134 { 1677 {
1135 fonts[i]->cs = CS_UNKNOWN; 1678 fonts[i]->cs = CS_UNKNOWN;
1136 return false; 1679 return false;
1137 } 1680 }
1138 1681
1144{ 1687{
1145 clear (); 1688 clear ();
1146 1689
1147 fontdesc = strdup (desc); 1690 fontdesc = strdup (desc);
1148 1691
1149 fonts.push_back (new_font (0, CS_UNICODE)); 1692 push_font (new_font (0, CS_UNICODE));
1150 realize_font (0); 1693 realize_font (0);
1151 1694
1152 add_fonts (desc); 1695 add_fonts (desc);
1153
1154 if (!base_id)
1155 base_id = 1;
1156
1157 // we currently need a base-font, no matter what
1158 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1159 {
1160 puts ("unable to load specified font (s), falling back to 'fixed'\n");
1161 add_fonts ("fixed");
1162 base_id = fonts.size () - 1;
1163 }
1164
1165 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1166 return false;
1167
1168 base_prop = fonts[base_id]->properties ();
1169 1696
1170 return true; 1697 return true;
1171} 1698}
1172 1699
1173int 1700int
1179 1706
1180 return -1; 1707 return -1;
1181} 1708}
1182 1709
1183int 1710int
1184rxvt_fontset::find_font (unicode_t unicode, bool bold) 1711rxvt_fontset::find_font_idx (unicode_t unicode)
1185{ 1712{
1713 // this limits fmap size. it has to accomodate COMPOSE_HI when UNICODE_3
1714 if (unicode > 0x1fffff)
1715 return 0;
1186 1716
1187 for (unsigned int i = !!(0x20 <= unicode && unicode <= 0x7f); // skip pseudo-font for ascii 1717 unicode_t hi = unicode >> 8;
1188 i < fonts.size (); 1718
1189 i++) 1719 if (hi < fmap.size ())
1720 if (pagemap *pm = fmap[hi])
1721 if ((*pm)[unicode & 0xff] != 0xff)
1722 return (*pm)[unicode & 0xff];
1723
1724 unsigned int i;
1725
1726 for (i = 0; i < fonts.size (); i++)
1190 { 1727 {
1191 rxvt_font *f = fonts[i]; 1728 rxvt_font *f = fonts[i];
1192 1729
1193 if (!f->loaded) 1730 if (!f->loaded)
1194 { 1731 {
1195 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1732 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1196 goto next_font; 1733 goto next_font;
1197 1734
1198 if (!realize_font (i)) 1735 if (!realize_font (i))
1199 goto next_font; 1736 goto next_font;
1737
1738 if (prop.ascent != rxvt_fontprop::unset)
1739 max_it (f->ascent, prop.ascent);
1200 } 1740 }
1201 1741
1202 if (f->cs == CS_UNKNOWN) 1742 if (f->cs == CS_UNKNOWN)
1203 goto next_font; 1743 goto next_font;
1204 1744
1205 if (bold && f->properties ().weight < rxvt_fontprop::bold) 1745 bool careful;
1746 if (f->has_char (unicode, &prop, careful))
1747 {
1748 i = (i << 1) | careful;
1749
1206 goto next_font; 1750 goto found;
1207 1751 }
1208 if (f->has_codepoint (unicode))
1209 return i;
1210 1752
1211 next_font: 1753 next_font:
1212 if (i == fonts.size () - 1) 1754 if (i == fonts.size () - 1)
1213 { 1755 {
1756 // compose characters are handled by the default font, unless another font takes over
1757 // we do not go via the fallback list for speed reasons.
1758 if (IS_COMPOSE (unicode))
1759 return 0;
1760
1214 if (fallback->name) 1761 if (fallback->name)
1215 { 1762 {
1216 // search through the fallback list 1763 // search through the fallback list
1217 fonts.push_back (new_font (fallback->name, fallback->cs)); 1764 push_font (new_font (fallback->name, fallback->cs));
1218 fallback++; 1765 fallback++;
1219 } 1766 }
1220 else 1767 else
1221 { 1768 {
1222 // try to find a new font. 1769 // try to find a new font.
1223 // only xft currently supported, as there is no 1770 // only xft currently supported, as there is no
1224 // way to configure this and xft is easier to hack in, 1771 // way to configure this and xft is easier to hack in,
1225 // while x11 has more framework in place already. 1772 // while x11 has more framework in place already.
1226#if XFT 1773 // TODO: this is a real resource hog, xft takes ages(?)
1774#if XFT && USE_SLOW_LOOKUP
1227 // grab the first xft font that seems suitable 1775 // grab the first xft font that seems suitable
1776 // TOOD: should go first for cellchar (spacing 110) then mono, then else
1228 FcPattern *p = FcPatternCreate (); 1777 FcPattern *p = FcPatternCreate ();
1229 1778
1230 FcCharSet *s = FcCharSetCreate (); 1779 FcCharSet *s = FcCharSetCreate ();
1231 FcCharSetAddChar (s, unicode); 1780 FcCharSetAddChar (s, unicode);
1232 FcPatternAddCharSet (p, FC_CHARSET, s); 1781 FcPatternAddCharSet (p, FC_CHARSET, s);
1233 // charsets don't help that much, as xft might return 1782 // charsets don't help that much, as xft might return
1234 // a non-matching font even if a better font is available :/ 1783 // a non-matching font even if a better font is available :/
1235 1784
1785 x x x x TODO prop might have unset contents
1236 FcPatternAddInteger (p, FC_PIXEL_SIZE, base_prop.height); 1786 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1237 FcPatternAddInteger (p, FC_WEIGHT, base_prop.weight); 1787 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1238 FcPatternAddInteger (p, FC_SLANT, base_prop.slant); 1788 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1239 FcPatternAddBool (p, FC_MINSPACE, 1); 1789 FcPatternAddBool (p, FC_MINSPACE, 1);
1240 //FcPatternAddBool (p, FC_ANTIALIAS, 1); 1790 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1241 1791
1242 XftResult result; 1792 XftResult result;
1243 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1793 FcPattern *match = XftFontMatch (term->dpy, term->display->screen, p, &result);
1244 1794
1245 FcPatternDestroy (p); 1795 FcPatternDestroy (p);
1246 1796
1247 if (match) 1797 if (match)
1248 { 1798 {
1251 FcPatternDestroy (match); 1801 FcPatternDestroy (match);
1252 1802
1253 if (find_font (font) < 0) 1803 if (find_font (font) < 0)
1254 { 1804 {
1255 char fontname[4096]; 1805 char fontname[4096];
1256 sprintf (fontname, "xft:%-.4090s", font); 1806 snprintf (fontname, sizeof (fontname), "xft:%s", font);
1257 1807
1258 fonts.push_back (new_font (fontname, CS_UNICODE)); 1808 push_font (new_font (fontname, CS_UNICODE));
1259 } 1809 }
1260 1810
1261 free (font); 1811 free (font);
1262 } 1812 }
1263#endif 1813#endif
1264 } 1814 }
1265 } 1815 }
1266 } 1816 }
1267 1817
1268 // if no bold font found, use a regular one 1818 /* we must return SOME font */
1269 if (bold) 1819 i = 0;
1270 return find_font (unicode);
1271 1820
1272 return 0; /* we must return SOME font */ 1821found:
1273} 1822 // found a font, cache it
1823 if (i < 255)
1824 {
1825 while (hi >= fmap.size ())
1826 fmap.push_back (0);
1274 1827
1828 if (!fmap[hi])
1829 {
1830 fmap[hi] = new pagemap;
1831 memset (fmap[hi], 0xff, sizeof (pagemap));
1832 }
1275 1833
1834 (*fmap[hi])[unicode & 0xff] = i;
1835 }
1276 1836
1837 return i;
1838}
1839

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines