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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines