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.5 by root, Tue Aug 17 01:29:12 2004 UTC vs.
Revision 1.85 by root, Wed Jan 25 00:42:21 2006 UTC

1/*--------------------------------*-C-*---------------------------------* 1/*--------------------------------*-C-*---------------------------------*
2 * File: defaultfont.C 2 * File: rxvtfont.C
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> 4 * Copyright (c) 2003-2006 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 "rxvtlib.h"
25#include "rxvtutil.h"
24#include "rxvtfont.h" 26#include "rxvtfont.h"
25 27
26#include <cstdlib> 28#include <cstdlib>
29#include <wchar.h>
30#include <inttypes.h>
27 31
28#define DISPLAY r->display->display 32#define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width
29#define TGC r->TermWin.gc
30 33
31const struct rxvt_fallback_font { 34const struct rxvt_fallback_font {
32 codeset cs; 35 codeset cs;
33 const char *name; 36 const char *name;
34} fallback_fonts[] = { 37} fallback_fonts[] = {
52 { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" }, 55 { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" },
53 { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" }, 56 { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" },
54 { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" }, 57 { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" },
55 { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" }, 58 { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" },
56 { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" }, 59 { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" },
60
61# if XFT
62 { CS_KOI8_U, "xft::spacing=100:lang=ru:antialias=false" },
63
64 { CS_ISO8859_5, "xft::spacing=100:lang=ru:antialias=false" },
65 { CS_ISO8859_6, "xft::spacing=100:lang=ar:antialias=false" },
66 { CS_ISO8859_7, "xft::spacing=100:lang=el:antialias=false" },
67 { CS_ISO8859_8, "xft::spacing=100:lang=he:antialias=false" },
68 { CS_ISO8859_9, "xft::spacing=100:lang=tr:antialias=false" },
69 { CS_ISO8859_10, "xft::spacing=100:lang=se:antialias=false" },
70 { CS_ISO8859_11, "xft::spacing=100:lang=th:antialias=false" },
71# endif
57#endif 72#endif
58 73
59 // japanese 74 // japanese
60#if ENCODING_JP || ENCODING_JP_EXT 75#if ENCODING_JP || ENCODING_JP_EXT
61# if XFT 76# if XFT
62 // prefer xft for complex scripts 77 // prefer xft for complex scripts
63 { CS_UNICODE, "xft:Kochi Gothic:antialias=false" }, 78 { CS_JIS0208_1990_0, "xft:Kochi Gothic:antialias=false" },
79 { CS_JIS0208_1990_0, "xft:Sazanami Mincho:antialias=false" },
80 { CS_JIS0208_1990_0, "xft:Mincho:antialias=false" },
81 { CS_JIS0208_1990_0, "xft::lang=ja:spacing=100:antialias=false" },
64# endif 82# endif
65 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" }, 83 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
66 { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" }, 84 { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
67 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, 85 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
68 { CS_JIS0201_1976_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0201*-0" }, 86 { CS_JIS0201_1976_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
69 { CS_JIS0208_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0208*-0" }, 87 { CS_JIS0208_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
70 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, 88 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
71#endif 89#endif
72 90
73#if ENCODING_CN || ENCODING_CN_EXT 91#if ENCODING_ZH || ENCODING_ZH_EXT
74# if XFT 92# if XFT
93 { CS_GBK_0, "xft:AR PL KaitiM GB" },
94 { CS_GBK_0, "xft:AR PL SungtiL GB" },
95 { CS_GBK_0, "xft::spacing=100:lang=zh" },
75 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, 96 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
76 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, 97 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
77 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, 98 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
78 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, 99 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
100 { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" },
79# endif 101# endif
102 { CS_GBK_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gbk*-0" },
80 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" }, 103 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
81 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" }, 104 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
82 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" }, 105 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
106 { CS_GB2312_1980_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
83 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" }, 107 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
84 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" }, 108 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
85 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" }, 109 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
86 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" }, 110 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
87 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" }, 111 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
89 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" }, 113 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
90 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" }, 114 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
91 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" }, 115 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
92#endif 116#endif
93 117
118#if ENCODING_KR
119 { CS_KSC5601_1987_0, "-baekmuk-gulim-*-*-*-*-*-*-*-*-c-*-ksc5601*" },
120 { CS_KSC5601_1987_0, "-*-*-*-*-*-*-*-*-*-*-c-*-ksc5601*" },
94#if XFT 121# if XFT
95 { CS_UNICODE, "xft:Andale Mono" }, 122 { CS_KSC5601_1987_0, "xft:Baekmuk Gulim:antialias=false" },
96 { CS_UNICODE, "xft:Arial Unicode MS" }, 123 { CS_KSC5601_1987_0, "xft::spacing=100:lang=ko:antialias=false" },
124# endif
97#endif 125#endif
126
127 // generic font fallback
98 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" }, 128 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
99 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, 129 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
100 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" }, 130 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
101 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 131 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
102#if XFT 132#if XFT
133 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true" },
134 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" },
135 { CS_UNICODE, "xft:Andale Mono:antialias=false:autohint=false" },
136 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" },
137
103 // FreeMono is usually uglier than x fonts, so try last only. 138 // FreeMono is usually uglier than x fonts, so try last only.
104 //{ CS_UNICODE, "xft:FreeMono" }, 139 { CS_UNICODE, "xft:FreeMono:autohint=true" },
105#endif 140#endif
106 141
107 { CS_UNKNOWN, 0 } 142 { CS_UNKNOWN, 0 }
108}; 143};
109 144
145// these characters are used to guess the font height and width
146// pango uses a similar algorithm and doesn't trust the font either.
147static uint16_t extent_test_chars[] = {
148 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_',
149 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ
150 0x304c, 0x672c, // が本
151};
152
153#define NUM_EXTENT_TEST_CHARS (sizeof (extent_test_chars) / sizeof (extent_test_chars[0]))
154
155#define dTermDisplay Display *disp = term->display->display
156#define dTermGC GC gc = term->gc
157
110///////////////////////////////////////////////////////////////////////////// 158/////////////////////////////////////////////////////////////////////////////
111 159
112#if XFT 160#if XFT
113rxvt_drawable::~rxvt_drawable () 161rxvt_drawable::~rxvt_drawable ()
114{ 162{
125} 173}
126#endif 174#endif
127 175
128///////////////////////////////////////////////////////////////////////////// 176/////////////////////////////////////////////////////////////////////////////
129 177
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
146static const char * 178static const char *
147enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero) 179enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
148{ 180{
149 uint8_t *buf = (uint8_t *)get_enc_buf (len); 181 uint8_t *buf = rxvt_temp_buf<uint8_t> (len);
182 uint8_t *res = buf;
150 183
151 while (len--) 184 while (len--)
152 { 185 {
153 uint32_t c = FROM_UNICODE (cs, *text++); 186 uint32_t c = FROM_UNICODE (cs, *text++);
154 187
159 } 192 }
160 193
161 *buf++ = c; 194 *buf++ = c;
162 } 195 }
163 196
164 return (const char *)enc_buf; 197 return (const char *)res;
165} 198}
166 199
167static const XChar2b * 200static const XChar2b *
168enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero) 201enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
169{ 202{
170 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b)); 203 XChar2b *buf = rxvt_temp_buf<XChar2b> (len);
204 XChar2b *res = buf;
171 205
172 while (len--) 206 while (len--)
173 { 207 {
174 uint32_t c = FROM_UNICODE (cs, *text++); 208 uint32_t c = FROM_UNICODE (cs, *text++);
175 209
182 buf->byte1 = c >> 8; 216 buf->byte1 = c >> 8;
183 buf->byte2 = c; 217 buf->byte2 = c;
184 buf++; 218 buf++;
185 } 219 }
186 220
187 return (XChar2b *)enc_buf; 221 return res;
188} 222}
189 223
190///////////////////////////////////////////////////////////////////////////// 224/////////////////////////////////////////////////////////////////////////////
191 225
192void 226void
227rxvt_font::set_name (char *name)
228{
229 if (this->name == name)
230 return;
231
232 if (this->name) free (this->name); // let the compiler optimize
233 this->name = name;
234}
235
236void
193rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color) 237rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color) const
194{ 238{
239 dTermDisplay;
240 dTermGC;
241
195 if (color == Color_bg) 242 if (color == Color_bg)
196 XClearArea (d.display->display, d, x, y, w, h, FALSE); 243 XClearArea (disp, d, x, y, w, h, FALSE);
197 else if (color >= 0) 244 else if (color >= 0)
198 { 245 {
199#if XFT 246#if XFT
200 XftDrawRect (d, &r->pix_colors[color].c, x, y, w, h); 247 XftDrawRect (d, &term->pix_colors[color].c, x, y, w, h);
201#else 248#else
202 XSetForeground (d.display->display, TGC, r->pix_colors[color]); 249 XSetForeground (disp, gc, term->pix_colors[color]);
203 XFillRectangle (d.display->display, d, TGC, x, y, w, h); 250 XFillRectangle (disp, d, gc, x, y, w, h);
204#endif 251#endif
205 } 252 }
206} 253}
207 254
208#include "table/linedraw.h" 255#include "table/linedraw.h"
209 256
210struct rxvt_font_default : rxvt_font { 257struct rxvt_font_default : rxvt_font {
258 struct rxvt_fontset *fs;
259
260 rxvt_font_default (rxvt_fontset *fs)
261 : rxvt_font ()
262 {
263 this->fs = fs;
264 }
211 265
212 rxvt_fontprop properties () 266 rxvt_fontprop properties ()
213 { 267 {
214 rxvt_fontprop p; 268 rxvt_fontprop p;
215 269
216 p.width = p.height = 1; 270 p.width = p.height = 1;
271 p.ascent = rxvt_fontprop::unset;
217 p.weight = rxvt_fontprop::medium; 272 p.weight = rxvt_fontprop::medium;
218 p.slant = rxvt_fontprop::roman; 273 p.slant = rxvt_fontprop::roman;
219 274
220 return p; 275 return p;
221 } 276 }
223 bool load (const rxvt_fontprop &prop) 278 bool load (const rxvt_fontprop &prop)
224 { 279 {
225 width = 1; height = 1; 280 width = 1; height = 1;
226 ascent = 1; descent = 0; 281 ascent = 1; descent = 0;
227 282
228 set_name (strdup ("built-in pseudofont")); 283 set_name (strdup ("built-in support font"));
229 284
230 return true; 285 return true;
231 } 286 }
232 287
233 bool has_codepoint (unicode_t unicode) 288 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
234 { 289 {
290 careful = false;
291
235 if (unicode <= 0x001f) 292 if (unicode <= 0x001f)
236 return true; 293 return true;
237 294
238 if (unicode <= 0x007f) 295 if (unicode <= 0x007f)
239 return false; 296 return false;
240 297
241 if (unicode <= 0x009f) 298 if (unicode <= 0x009f)
242 return true; 299 return true;
243 300
244 if (unicode >= 0x2500 && unicode <= 0x259f) 301 if (unicode >= 0x2500 && unicode <= 0x259f &&
302 !term->option (Opt_skipBuiltinGlyphs))
245 return true; 303 return true;
246 304
247 if (IS_COMPOSE (unicode)) 305 if (IS_COMPOSE (unicode))
248 return true; 306 return true;
249 307
250 switch (unicode) 308 switch (unicode)
251 { 309 {
252 case ZERO_WIDTH_CHAR: 310 case ZERO_WIDTH_CHAR:
311 case NOCHAR:
253 return true; 312 return true;
254 } 313 }
255 314
256 return false; 315 return false;
257 } 316 }
264void 323void
265rxvt_font_default::draw (rxvt_drawable &d, int x, int y, 324rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
266 const text_t *text, int len, 325 const text_t *text, int len,
267 int fg, int bg) 326 int fg, int bg)
268{ 327{
328 dTermDisplay;
329 dTermGC;
330
269 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 331 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
270 332
271 XSetForeground (d.display->display, TGC, r->pix_colors[fg]); 333 XSetForeground (disp, gc, term->pix_colors[fg]);
272 334
273 while (len--) 335 while (len)
274 { 336 {
275#if ENABLE_COMBINING 337#if ENABLE_COMBINING
276 compose_char *cc; 338 compose_char *cc;
277#endif 339#endif
340 const text_t *tp = text;
278 text_t t = *text++; 341 text_t t = *tp;
342
343 while (++text, --len && *text == NOCHAR)
344 ;
345
346 int width = text - tp;
347 int fwidth = term->fwidth * width;
279 348
280 if (0x2500 <= t && t <= 0x259f) 349 if (0x2500 <= t && t <= 0x259f)
281 { 350 {
282 uint16_t offs = linedraw_offs[t - 0x2500]; 351 uint16_t offs = linedraw_offs[t - 0x2500];
283 uint32_t *a = linedraw_command + (offs >> 4); 352 uint32_t *a = linedraw_command + (offs >> 4);
284 uint32_t *b = a + (offs & 15); 353 uint32_t *b = a + (offs & 15);
285 354
286 int W = r->TermWin.fwidth; 355 int W = fwidth;
287 int H = r->TermWin.fheight; 356 int H = term->fheight;
288 357
289 int x_[16]; 358 int x_[16];
290 int y_[16]; 359 int y_[16];
291 360
292 for (int i = 0; i <= 8; i++) 361 for (int i = 0; i <= 8; i++)
300 369
301 XGCValues gcv; 370 XGCValues gcv;
302 371
303 gcv.cap_style = CapButt; 372 gcv.cap_style = CapButt;
304 gcv.line_width = 0; 373 gcv.line_width = 0;
305 XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv); 374 XChangeGC (disp, gc, GCLineWidth | GCCapStyle, &gcv);
306 375
307 while (a < b) 376 while (a < b)
308 { 377 {
309 uint32_t command = *a++; 378 uint32_t command = *a++;
310 379
317 int y2 = y_[(command >> 0) & 15]; 386 int y2 = y_[(command >> 0) & 15];
318 387
319 switch (op) 388 switch (op)
320 { 389 {
321 case 0: // line 390 case 0: // line
322 XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2); 391 XDrawLine (disp, d, gc, x1, y1, x2, y2);
323 break; 392 break;
324 393
325 case 1: // rectangle, possibly stippled 394 case 1: // rectangle, possibly stippled
326 if (a) 395 if (a)
327 { 396 {
328 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 }; 397 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
329 398
330 gcv.fill_style = FillStippled; 399 gcv.fill_style = FillStippled;
331 gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2); 400 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
332 gcv.ts_x_origin = x; 401 gcv.ts_x_origin = x;
333 gcv.ts_y_origin = y; 402 gcv.ts_y_origin = y;
334 403
335 XChangeGC (d.display->display, TGC, 404 XChangeGC (disp, gc,
336 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, 405 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
337 &gcv); 406 &gcv);
338 } 407 }
339 408
340 XFillRectangle (d.display->display, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1); 409 XFillRectangle (disp, d, gc, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
341 410
342 if (a) 411 if (a)
343 { 412 {
344 XFreePixmap (d.display->display, gcv.stipple); 413 XFreePixmap (disp, gcv.stipple);
345 gcv.stipple = 0; 414 gcv.stipple = 0;
346 gcv.fill_style = FillSolid; 415 gcv.fill_style = FillSolid;
347 XChangeGC (d.display->display, TGC, GCFillStyle, &gcv); 416 XChangeGC (disp, gc, GCFillStyle, &gcv);
348 } 417 }
349 break; 418 break;
350 case 2: // arc 419 case 2: // arc
351 XDrawArc (d.display->display, d, TGC, 420 XDrawArc (disp, d, gc,
352 x1 - W/2, y1 - H/2, W-1, H-1, 421 x1 - W/2, y1 - H/2, W-1, H-1,
353 (a - 1) * 90*64, (b - 1) * 90*64); 422 (a - 1) * 90*64, (b - 1) * 90*64);
354 break; 423 break;
355 } 424 }
356 } 425 }
357 } 426 }
358#if ENABLE_COMBINING 427#if ENABLE_COMBINING
359 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t])) 428 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
360 { 429 {
430 text_t chrs[2];
431 width = min (2, width);
432 chrs [1] = NOCHAR;
433
434 *chrs = cc->c1;
361 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)]; 435 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
362 f1->draw (d, x, y, &(t = cc->c1), 1, fg, bg); 436 f1->draw (d, x, y, chrs, width, fg, bg);
437
363 if (cc->c2 != NOCHAR) 438 if (cc->c2 != NOCHAR)
364 { 439 {
440 bool careful;
441
365 // prefer font of first character, for no good reasons 442 // prefer font of first character, for no good reasons
366 rxvt_font *f2 = f1->has_codepoint (cc->c2) 443 *chrs = cc->c2;
444 rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful)
367 ? f1 445 ? f1
368 : (*fs)[fs->find_font (cc->c2)]; 446 : (*fs)[fs->find_font (cc->c2)];
369 447
370 f2->draw (d, x, y, &(t = cc->c2), 1, fg, -1); 448 f2->draw (d, x, y, chrs, width, fg, -1);
371 } 449 }
372 } 450 }
373#endif 451#endif
374 else 452 else
375 switch (t) 453 switch (t)
376 { 454 {
455 case '\t':
377 case ZERO_WIDTH_CHAR: 456 case ZERO_WIDTH_CHAR:
457 case NOCHAR:
378 break; 458 break;
379 459
380 default: 460 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, 461 XDrawRectangle (disp, d, gc, x + 2, y + 2,
390 w + r->TermWin.fwidth - 4, r->TermWin.fheight - 4); 462 fwidth - 4, term->fheight - 4);
391 x += w;
392 } 463 }
393 464
394 x += r->TermWin.fwidth; 465 x += fwidth;
395 } 466 }
396} 467}
397 468
398///////////////////////////////////////////////////////////////////////////// 469/////////////////////////////////////////////////////////////////////////////
399 470
404 475
405 rxvt_fontprop properties (); 476 rxvt_fontprop properties ();
406 477
407 bool load (const rxvt_fontprop &prop); 478 bool load (const rxvt_fontprop &prop);
408 479
409 bool has_codepoint (unicode_t unicode); 480 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const;
410 481
411 void draw (rxvt_drawable &d, int x, int y, 482 void draw (rxvt_drawable &d, int x, int y,
412 const text_t *text, int len, 483 const text_t *text, int len,
413 int fg, int bg); 484 int fg, int bg);
414 485
486 bool slow; // wether this is a proportional font or has other funny characteristics
415 XFontStruct *f; 487 XFontStruct *f;
416 codeset cs; 488 codeset cs;
417 bool enc2b, encm; 489 bool enc2b, encm;
418 490
419 char *get_property (XFontStruct *f, const char *property, const char *repl) const; 491 char *get_property (XFontStruct *f, const char *property, const char *repl) const;
425char * 497char *
426rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const 498rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
427{ 499{
428 unsigned long value; 500 unsigned long value;
429 501
430 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value)) 502 if (XGetFontProperty (f, XInternAtom (term->display->display, property, 0), &value))
431 return XGetAtomName (DISPLAY, value); 503 return XGetAtomName (term->display->display, value);
432 else 504 else
433 return rxvt_strdup (repl); 505 return rxvt_strdup (repl);
434} 506}
435 507
436rxvt_fontprop 508rxvt_fontprop
442} 514}
443 515
444bool 516bool
445rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth) 517rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
446{ 518{
447 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2; 519 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
448 p.height = height; 520 p.height = height;
521 p.ascent = rxvt_fontprop::unset;
449 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium; 522 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; 523 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
451 524
452 return true; 525 return true;
453} 526}
454 527
455bool 528bool
456rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) 529rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
457{ 530{
458 unsigned long height; 531 unsigned long height;
532
533#if 0
459 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) 534 if (!XGetFontProperty (f, XInternAtom (term->display->display, "PIXEL_SIZE", 0), &height))
460 return false; 535 return false;
536#else
537 height = f->ascent + f->descent;
538#endif
461 539
462 unsigned long avgwidth; 540 unsigned long avgwidth;
463 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) 541 if (!XGetFontProperty (f, XInternAtom (term->display->display, "AVERAGE_WIDTH", 0), &avgwidth))
464 avgwidth = 0; 542 avgwidth = 0;
465 543
466 char *weight = get_property (f, "WEIGHT_NAME", "medium"); 544 char *weight = get_property (f, "WEIGHT_NAME", "medium");
467 char *slant = get_property (f, "SLANT", "r"); 545 char *slant = get_property (f, "SLANT", "r");
468 546
469 set_properties (p, height, weight, slant, avgwidth); 547 set_properties (p, height, weight, slant, avgwidth);
470 548
471 free (weight); 549 free (weight);
472 free (slant); 550 free (slant);
473 551
552 p.ascent = f->ascent;
553
474 return true; 554 return true;
475} 555}
476 556
477bool 557bool
478rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name) 558rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
479{ 559{
560 dTermDisplay;
480 int slashes = 0; 561 int slashes = 0;
481 const char *comp[13]; 562 const char *comp[13];
482 563
483 for (const char *c = name; *c; c++) 564 for (const char *c = name; *c; c++)
484 if (*c == '-') 565 if (*c == '-')
492 if (slashes >= 13 573 if (slashes >= 13
493 && (*comp[ 6] >= '1' && *comp[ 6] <= '9') 574 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
494 && (*comp[11] >= '0' && *comp[11] <= '9')) 575 && (*comp[11] >= '0' && *comp[11] <= '9'))
495 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11])); 576 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
496 577
497 XFontStruct *f = XLoadQueryFont (DISPLAY, name); 578 XFontStruct *f = XLoadQueryFont (disp, name);
498 579
499 if (f) 580 if (f)
500 { 581 {
501 // the font should really exist now. if not, we have a problem 582 // 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). 583 // (e.g. if the user did xset fp rehash just when we were searching fonts).
503 // in that case, just return garbage. 584 // in that case, just return garbage.
504 bool ret = set_properties (p, f); 585 bool ret = set_properties (p, f);
505 XFreeFont (DISPLAY, f); 586 XFreeFont (disp, f);
506 return ret; 587 return ret;
507 } 588 }
508 else 589 else
509 return false; 590 return false;
510} 591}
511 592
512// fix the size of scalable fonts 593// fix the size of scalable fonts
513static void 594static bool
514fix_scalable (char *buf, const char *name, const rxvt_fontprop &prop) 595replace_field (char *buf, const char *name, int index, const char old, const char *replace)
515{ 596{
516 int slashes = 0; 597 int slashes = 0;
517 const char *size; 598 const char *field, *end;
518 599
519 for (const char *c = name; *c; c++) 600 for (const char *c = name; *c; c++)
520 if (*c == '-') 601 if (*c == '-')
521 { 602 {
522 if (slashes == 6) 603 if (slashes == index)
523 size = c + 1; 604 field = c + 1;
605
606 if (slashes == index + 1)
607 end = c;
524 608
525 if (++slashes >= 13) 609 if (++slashes >= 13)
526 break; 610 break;
527 } 611 }
528 612
529 if (slashes >= 13 && size[0] == '0') 613 if (slashes >= 13 && (!old || *field == old))
530 { 614 {
615 // TODO: check for overflow in font-name
531 strncpy (buf, name, size - name); 616 strncpy (buf, name, field - name);
532 buf += size - name; 617 buf += field - name;
533 buf += sprintf (buf, "%d", prop.height);
534 strcpy (buf, size + 1); 618 strcpy (buf, replace);
619 strcat (buf, end);
620
621 return true;
535 } 622 }
536 else 623 else
624 {
537 strcpy (buf, name); 625 strcpy (buf, name);
626
627 return false;
628 }
538} 629}
539 630
540bool 631bool
541rxvt_font_x11::load (const rxvt_fontprop &prop) 632rxvt_font_x11::load (const rxvt_fontprop &prop)
542{ 633{
634 dTermDisplay;
635
543 clear (); 636 clear ();
637
638 char field_str[64]; // enough for 128 bits
639
640 // first morph the font if required
641 if (prop.weight != rxvt_fontprop::unset
642 || prop.slant != rxvt_fontprop::unset)
643 {
644 char fname[1024];
645
646 if (name[0] != '-')
647 {
648 f = XLoadQueryFont (disp, name);
649
650 if (!f)
651 return false;
652
653 char *new_name = get_property (f, "FONT", name);
654
655 if (new_name)
656 set_name (new_name);
657 else
658 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
659
660 XFreeFont (disp, f);
661 f = 0;
662 }
663
664 if (prop.weight != rxvt_fontprop::unset)
665 {
666 replace_field (fname, name, 2, 0,
667 prop.weight < rxvt_fontprop::bold
668 ? "medium" : "bold");
669 set_name (strdup (fname));
670 }
671
672 if (prop.slant != rxvt_fontprop::unset)
673 {
674 replace_field (fname, name, 3, 0,
675 prop.slant < rxvt_fontprop::italic
676 ? "r" : "i"); // TODO: handle "o"blique, too
677 set_name (strdup (fname));
678 }
679 }
680
681 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
682 ? 0 : prop.height);
683
684 struct font_weight {
685 char *name;
686 int diff;
687
688 void clear ()
689 {
690 name = 0;
691 diff = 0x7fffffff;
692 }
693
694 font_weight () { clear (); }
695 ~font_weight () { free (name); }
696 };
544 697
545 char **list; 698 char **list;
546 int count; 699 int count;
547 list = XListFonts (DISPLAY, name, 1024, &count); 700 list = XListFonts (disp, name, 4000, &count);
701
548 set_name (0); 702 set_name (0);
549 703
550 if (!list) 704 if (!list)
551 return false; 705 return false;
552 706
553 int bestdiff = 0x7fffffff; 707 font_weight *fonts = new font_weight[count];
708
554 for (int i = 0; i < count; i++) 709 for (int i = 0; i < count; i++)
555 { 710 {
556 rxvt_fontprop p; 711 rxvt_fontprop p;
557 char fname[1024]; 712 char fname[1024];
558 fix_scalable (fname, list[i], prop); 713
714 int diff = 0;
715
716 if (replace_field (fname, list[i], 6, '0', field_str))
717 diff += 10; // slightly penalize scalable fonts
718 else if (replace_field (fname, list[i], 11, '0', "0"))
719 diff += 300; // more heavily penalize what looks like scaled bitmap fonts
559 720
560 if (!set_properties (p, fname)) 721 if (!set_properties (p, fname))
561 continue; 722 continue;
562 723
724 if (prop.height != rxvt_fontprop::unset
563 if (p.height > prop.height) // weed out too large fonts 725 && p.height > prop.height) // weed out too large fonts
564 continue; 726 continue;
565 727
566 int diff = (prop.height - p.height) * 32 728 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128;
567 + abs (prop.weight - p.weight) 729 if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight);
568 + abs (prop.slant - p.slant ); 730 if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant);
731 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
569 732
570 if (!name // compare against best found so far 733 fonts[i].name = strdup (fname);
571 || diff < bestdiff) 734 fonts[i].diff = diff;
572 {
573 set_name (strdup (fname));
574 bestdiff = diff;
575 }
576 } 735 }
577 736
578 XFreeFontNames (list); 737 XFreeFontNames (list);
579 738
580 if (!name) 739 // this loop only iterates when the guessed font-size is too small
581 return false; 740 for (;;)
741 {
742 font_weight *best = fonts + count - 1;
582 743
583 f = XLoadQueryFont (DISPLAY, name); 744 for (font_weight *w = best; w-- > fonts; )
745 if (w->diff <= best->diff)
746 best = w;
747
748 if (!best->name
749 || !(f = XLoadQueryFont (disp, best->name)))
750 break;
751
752 set_name (best->name);
753 best->clear ();
754
755 ascent = f->ascent;
756 descent = f->descent;
757 height = ascent + descent;
758
759 if (prop.height == rxvt_fontprop::unset
760 || height <= prop.height)
761 break; // font is ready for use
762
763 // PIXEL_SIZE small enough, but real height too large
764 clear ();
765 }
766
767 delete [] fonts;
584 768
585 if (!f) 769 if (!f)
586 return false; 770 return false;
587 771
588 char *registry = get_property (f, "CHARSET_REGISTRY", 0); 772 char *registry = get_property (f, "CHARSET_REGISTRY", 0);
617 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font 801 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
618 802
619 encm = f->min_byte1 != 0 || f->max_byte1 != 0; 803 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
620 enc2b = encm || f->max_char_or_byte2 > 255; 804 enc2b = encm || f->max_char_or_byte2 > 255;
621 805
622 ascent = f->ascent;
623 descent = f->descent;
624 height = ascent + descent;
625
626 slow = false; 806 slow = false;
627 807
808#if 1 // only used for slow detection, TODO optimize
628 if (f->min_bounds.width == f->max_bounds.width) 809 if (f->min_bounds.width == f->max_bounds.width)
629 width = f->min_bounds.width; 810 width = f->min_bounds.width;
630 else if (f->per_char == NULL) 811 else if (f->per_char == NULL)
631 width = f->max_bounds.width; 812 width = f->max_bounds.width;
632 else 813 else
645 width = f->per_char[N].width; 826 width = f->per_char[N].width;
646 827
647 --N; 828 --N;
648 } 829 }
649 } 830 }
831#endif
832
833 width = 1;
834
835 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
836 {
837 if (cs != CS_UNICODE
838 && *t > 0x100
839 && FROM_UNICODE (cs, *t) == NOCHAR)
840 continue;
841
842 // ignore characters we wouldn't use anyways
843 bool careful;
844 if (!has_char (*t, &prop, careful))
845 continue;
846
847 XChar2b ch = { *t >> 8, *t };
848
849 XCharStruct g;
850 int dir_ret, asc_ret, des_ret;
851 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
852
853 int wcw = wcwidth (*t); if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
854
855 if (width < g.width) width = g.width;
856 }
650 857
651 if (cs == CS_UNKNOWN) 858 if (cs == CS_UNKNOWN)
652 { 859 {
653 fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name); 860 fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name);
654 861
655 clear (); 862 clear ();
656
657 return false; 863 return false;
658 } 864 }
865
866#if 0 // do it per-character
867 if (prop && width > prop->width)
868 {
869 clear ();
870 return false;
871 }
872#endif
659 873
660 return true; 874 return true;
661} 875}
662 876
663void 877void
664rxvt_font_x11::clear () 878rxvt_font_x11::clear ()
665{ 879{
666 if (f) 880 if (f)
667 { 881 {
668 XFreeFont (DISPLAY, f); 882 XFreeFont (term->display->display, f);
669 f = 0; 883 f = 0;
670 } 884 }
671} 885}
672 886
673bool 887bool
674rxvt_font_x11::has_codepoint (unicode_t unicode) 888rxvt_font_x11::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
675{ 889{
676 uint32_t ch = FROM_UNICODE (cs, unicode); 890 uint32_t ch = FROM_UNICODE (cs, unicode);
677 891
678 if (ch == NOCHAR) 892 if (ch == NOCHAR)
679 return false; 893 return false;
688 902
689 if (byte1 < f->min_byte1 || byte1 > f->max_byte1 903 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
690 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2) 904 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
691 return false; 905 return false;
692 906
693 if (!f->per_char) 907 if (f->per_char)
694 return true; 908 {
695
696 int D = f->max_char_or_byte2 - f->min_char_or_byte2 + 1; 909 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; 910 int N = (byte1 - f->min_byte1) * D + byte2 - f->min_char_or_byte2;
698 911
699 xcs = f->per_char + N; 912 xcs = f->per_char + N;
913 }
914 else
915 xcs = &f->max_bounds;
700 } 916 }
701 else 917 else
702 { 918 {
703 if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2) 919 if (ch < f->min_char_or_byte2 || ch > f->max_char_or_byte2)
704 return false; 920 return false;
705 921
706 if (!f->per_char) 922 if (f->per_char)
707 return true;
708
709 xcs = f->per_char + (ch - f->min_char_or_byte2); 923 xcs = f->per_char + (ch - f->min_char_or_byte2);
924 else
925 xcs = &f->max_bounds;
710 } 926 }
711 927
712 if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0 928 if (xcs->lbearing == 0 && xcs->rbearing == 0 && xcs->width == 0
713 && xcs->ascent == 0 && xcs->descent == 0) 929 && xcs->ascent == 0 && xcs->descent == 0)
930 return false;
931
932 if (!prop || prop->width == rxvt_fontprop::unset)
933 return true;
934
935 // check character against base font bounding box
936 int w = xcs->width;
937 int wcw = wcwidth (unicode);
938 if (wcw > 0) w = (w + wcw - 1) / wcw;
939
940 careful = w > prop->width;
941 if (careful && w > prop->width * MAX_OVERLAP >> 2)
714 return false; 942 return false;
715 943
716 return true; 944 return true;
717} 945}
718 946
724 // this looks like a mess /. 952 // this looks like a mess /.
725 // and it is a mess /. 953 // and it is a mess /.
726 // yet we are trying to be perfect /. 954 // yet we are trying to be perfect /.
727 // but the result still isn't perfect /. 955 // but the result still isn't perfect /.
728 956
957 dTermDisplay;
958 dTermGC;
959
729 bool slow = this->slow 960 bool slow = this->slow
730 || width != r->TermWin.fwidth 961 || width != term->fwidth
731 || height != r->TermWin.fheight; 962 || height != term->fheight;
732 963
733 int base = r->TermWin.fbase; 964 int base = ascent; // sorry, incorrect: term->fbase;
734 965
735 XGCValues v; 966 XGCValues v;
736 v.foreground = r->pix_colors[fg]; 967 v.foreground = term->pix_colors[fg];
737 v.font = f->fid; 968 v.font = f->fid;
738 969
739 if (enc2b) 970 if (enc2b)
740 { 971 {
741 const XChar2b *xc = enc_xchar2b (text, len, cs, slow); 972 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
742 973
743 if (bg == Color_bg && !slow) 974 if (bg == Color_bg && !slow)
744 { 975 {
745 v.background = r->pix_colors[bg]; 976 v.background = term->pix_colors[bg];
746 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 977 XChangeGC (disp, gc, GCForeground | GCBackground | GCFont, &v);
747 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len); 978 XDrawImageString16 (disp, d, gc, x, y + base, xc, len);
748 } 979 }
749 else 980 else
750 { 981 {
751 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 982 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
752 983
753 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 984 XChangeGC (disp, gc, GCForeground | GCFont, &v);
754 985
755 if (slow) 986 if (slow)
756 { 987 {
757 do 988 do
758 { 989 {
759 if (xc->byte1 || xc->byte2) 990 if (xc->byte1 || xc->byte2)
760 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1); 991 XDrawString16 (disp, d, gc, x, y + base, xc, 1);
761 992
762 x += r->TermWin.fwidth; 993 x += term->fwidth;
763 xc++; len--; 994 xc++; len--;
764 } 995 }
765 while (len); 996 while (len);
766 } 997 }
767 else 998 else
768 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, len); 999 XDrawString16 (disp, d, gc, x, y + base, xc, len);
769 } 1000 }
770 } 1001 }
771 else 1002 else
772 { 1003 {
773 const char *xc = enc_char (text, len, cs, slow); 1004 const char *xc = enc_char (text, len, cs, slow);
774 1005
775 if (bg == Color_bg && !slow) 1006 if (bg == Color_bg && !slow)
776 { 1007 {
777 v.background = r->pix_colors[bg]; 1008 v.background = term->pix_colors[bg];
778 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 1009 XChangeGC (disp, gc, GCForeground | GCBackground | GCFont, &v);
779 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len); 1010 XDrawImageString (disp, d, gc, x, y + base, xc, len);
780 } 1011 }
781 else 1012 else
782 { 1013 {
783 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1014 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
784 1015
785 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 1016 XChangeGC (disp, gc, GCForeground | GCFont, &v);
786 1017
787 if (slow) 1018 if (slow)
788 { 1019 {
789 do 1020 do
790 { 1021 {
791 if (*xc) 1022 if (*xc)
792 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1); 1023 XDrawString (disp, d, gc, x, y + base, xc, 1);
793 1024
794 x += r->TermWin.fwidth; 1025 x += term->fwidth;
795 xc++; len--; 1026 xc++; len--;
796 } 1027 }
797 while (len); 1028 while (len);
798 } 1029 }
799 else 1030 else
800 XDrawString (d.display->display, d, TGC, x, y + base, xc, len); 1031 XDrawString (disp, d, gc, x, y + base, xc, len);
801 } 1032 }
802 } 1033 }
803} 1034}
804 1035
805///////////////////////////////////////////////////////////////////////////// 1036/////////////////////////////////////////////////////////////////////////////
806 1037
807#if XFT 1038#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 1039
814struct rxvt_font_xft : rxvt_font { 1040struct rxvt_font_xft : rxvt_font {
815 rxvt_font_xft () { f = 0; } 1041 rxvt_font_xft () { f = 0; }
816 1042
817 void clear (); 1043 void clear ();
822 1048
823 void draw (rxvt_drawable &d, int x, int y, 1049 void draw (rxvt_drawable &d, int x, int y,
824 const text_t *text, int len, 1050 const text_t *text, int len,
825 int fg, int bg); 1051 int fg, int bg);
826 1052
827 bool has_codepoint (unicode_t unicode); 1053 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &carefull) const;
828 1054
829protected: 1055protected:
830 XftFont *f; 1056 XftFont *f;
831}; 1057};
832 1058
833void 1059void
834rxvt_font_xft::clear () 1060rxvt_font_xft::clear ()
835{ 1061{
836 if (f) 1062 if (f)
837 { 1063 {
838 XftFontClose (DISPLAY, f); 1064 XftFontClose (term->display->display, f);
839 f = 0; 1065 f = 0;
840 } 1066 }
841} 1067}
842 1068
843rxvt_fontprop 1069rxvt_fontprop
847 1073
848 FT_Face face = XftLockFace (f); 1074 FT_Face face = XftLockFace (f);
849 1075
850 p.width = width; 1076 p.width = width;
851 p.height = height; 1077 p.height = height;
1078 p.ascent = ascent;
852 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD 1079 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
853 ? rxvt_fontprop::bold : rxvt_fontprop::medium; 1080 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
854 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC 1081 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
855 ? rxvt_fontprop::italic : rxvt_fontprop::roman; 1082 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
856 1083
860} 1087}
861 1088
862bool 1089bool
863rxvt_font_xft::load (const rxvt_fontprop &prop) 1090rxvt_font_xft::load (const rxvt_fontprop &prop)
864{ 1091{
865#if 0 1092 dTermDisplay;
866 for (int i = 0; i < SWATHCOUNT; i++)
867 cvr[i] = 0;
868#endif
869 1093
870 clear (); 1094 clear ();
871 1095
872 FcPattern *p = FcNameParse ((FcChar8 *) name); 1096 FcPattern *p = FcNameParse ((FcChar8 *) name);
873 1097
874 if (!p) 1098 if (!p)
875 return false; 1099 return false;
876 1100
877 FcValue v; 1101 FcValue v;
878 1102
1103 if (prop.height != rxvt_fontprop::unset
879 if (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch) 1104 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1105 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
880 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); 1106 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
881 1107
1108 if (prop.weight != rxvt_fontprop::unset
882 if (FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) 1109 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
883 FcPatternAddInteger (p, FC_WEIGHT, prop.weight); 1110 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
884 1111
1112 if (prop.slant != rxvt_fontprop::unset
885 if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch) 1113 && FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
886 FcPatternAddInteger (p, FC_SLANT, prop.slant); 1114 FcPatternAddInteger (p, FC_SLANT, prop.slant);
887
888 if (FcPatternGet (p, FC_MINSPACE, 0, &v) != FcResultMatch)
889 FcPatternAddBool (p, FC_MINSPACE, 1);
890 1115
891#if 0 // clipping unfortunately destroys our precious double-width-characters 1116#if 0 // clipping unfortunately destroys our precious double-width-characters
892 // clip width, we can't do better, or can we? 1117 // clip width, we can't do better, or can we?
893 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch) 1118 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
894 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width); 1119 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop->width);
895#endif 1120#endif
896 1121
1122 if (FcPatternGet (p, FC_MINSPACE, 0, &v) != FcResultMatch)
1123 FcPatternAddBool (p, FC_MINSPACE, 1);
1124
1125 // store generated name so iso14755 view gives better results
1126 set_name ((char *)FcNameUnparse (p));
1127
897 XftResult result; 1128 XftResult result;
898 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1129 FcPattern *match = XftFontMatch (disp, term->display->screen, p, &result);
899 1130
900 FcPatternDestroy (p); 1131 FcPatternDestroy (p);
901 1132
902 if (!match) 1133 if (!match)
903 return false; 1134 return false;
904 1135
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; 1136 int ftheight = 0;
1137 bool success = true;
918 1138
919 for (;;) 1139 for (;;)
920 { 1140 {
921 XGlyphInfo g1, g2; 1141 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
922 FcChar8 c;
923 1142
924 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1); 1143 if (!f)
925 c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2); 1144 {
1145 success = false;
1146 break;
1147 }
926 1148
927 if (g1.xOff != g2.xOff) // don't simply trust the font 1149 FT_Face face = XftLockFace (f);
928 slow = true;
929 1150
930 width = g2.xOff;
931 ascent = (face->size->metrics.ascender + 63) >> 6; 1151 ascent = (face->size->metrics.ascender + 63) >> 6;
932 descent = (-face->size->metrics.descender + 63) >> 6; 1152 descent = (-face->size->metrics.descender + 63) >> 6;
933 height = ascent + descent; 1153 height = max (ascent + descent, (face->size->metrics.height + 63) >> 6);
1154 width = 0;
934 1155
1156 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1157
1158 XftUnlockFace (f);
1159
1160 int glheight = height;
1161
1162 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1163 {
1164 FcChar16 ch = *t;
1165
1166 if (cs != CS_UNICODE
1167 && ch > 0x100
1168 && FROM_UNICODE (cs, ch) == NOCHAR)
1169 continue;
1170
1171 // ignore characters we wouldn't use anyways
1172 bool careful;
1173 if (!has_char (*t, &prop, careful))
1174 continue;
1175
1176 XGlyphInfo g;
1177 XftTextExtents16 (disp, f, &ch, 1, &g);
1178
1179 g.width -= g.x;
1180
1181 int wcw = wcwidth (ch);
1182 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1183
1184 if (width < g.width ) width = g.width;
1185 if (height < g.height ) height = g.height;
1186 if (glheight < g.height - g.y) glheight = g.height - g.y;
1187 }
1188
1189 if (!width)
1190 {
1191 rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name);
1192
1193 XftFontClose (disp, f);
1194 f = 0;
1195
1196 success = false;
1197 break;
1198 }
1199
1200 if (prop.height == rxvt_fontprop::unset
935 if (height <= prop.height || !prop.height) 1201 || (height <= prop.height && glheight <= prop.height)
1202 || height <= 2
1203 || !scalable)
936 break; 1204 break;
937 1205
938 if (ftheight) 1206 if (ftheight)
939 { 1207 {
940 // take smaller steps near the end 1208 // take smaller steps near the end
941 if (height > prop.height + 1) ftheight++; 1209 if (height > prop.height + 1) ftheight++;
942 if (height > prop.height + 2) ftheight++; 1210 if (height > prop.height + 2) ftheight++;
943 if (height > prop.height + 3) ftheight++; 1211 if (height > prop.height + 3) ftheight++;
944 1212
945 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - prop.height); 1213 ftheight -= height - prop.height;
946 } 1214 }
947 else 1215 else
948 FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height); 1216 ftheight = prop.height - 1;
1217
1218 XftFontClose (disp, f);
1219 FcPatternDel (match, FC_PIXEL_SIZE);
1220 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1221 }
1222
1223 FcPatternDestroy (match);
1224
1225#if 0 // do it per-character
1226 if (prop.width != rxvt_fontprop::unset && width > prop.width)
949 } 1227 {
1228 clear ();
1229 success = false;
1230 }
1231#endif
950 1232
951 XftUnlockFace (f); 1233 return success;
1234}
1235
1236bool
1237rxvt_font_xft::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
1238{
1239 careful = false;
1240
1241 if (!XftCharExists (term->display->display, f, unicode))
1242 return false;
1243
1244 if (!prop || prop->width == rxvt_fontprop::unset)
1245 return true;
1246
1247 // check character against base font bounding box
1248 FcChar32 ch = unicode;
1249 XGlyphInfo g;
1250 XftTextExtents32 (term->display->display, f, &ch, 1, &g);
1251
1252 int w = g.width - g.x;
1253 int wcw = wcwidth (unicode);
1254 if (wcw > 0) w = (w + wcw - 1) / wcw;
1255
1256 careful = w > prop->width;
1257 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1258 return false;
952 1259
953 return true; 1260 return true;
954}
955
956bool
957rxvt_font_xft::has_codepoint (unicode_t unicode)
958{
959 return XftCharExists (DISPLAY, f, unicode);
960} 1261}
961 1262
962void 1263void
963rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1264rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
964 const text_t *text, int len, 1265 const text_t *text, int len,
965 int fg, int bg) 1266 int fg, int bg)
966{ 1267{
967 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1268 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
968 1269
969 if (0 && !slow && width == r->TermWin.fwidth) 1270 XGlyphInfo extents;
970 { 1271 XftGlyphSpec *enc = (XftGlyphSpec *)rxvt_temp_buf (len * sizeof (XftGlyphSpec));
971 if (sizeof (text_t) == sizeof (FcChar16)) 1272 XftGlyphSpec *ep = enc;
972 XftDrawString16 (d, &r->pix_colors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len); 1273
973 else 1274 dTermDisplay;
974 XftDrawString32 (d, &r->pix_colors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len); 1275 dTermGC;
975 } 1276
976 else 1277 // cut trailing spaces
977 { 1278 while (len && text [len - 1] == ' ')
1279 len--;
1280
978 while (len) 1281 while (len)
1282 {
1283 int cwidth = term->fwidth;
1284 FcChar32 fc = *text++; len--;
1285
1286 while (len && *text == NOCHAR)
1287 text++, len--, cwidth += term->fwidth;
979 { 1288
980 if (*text != NOCHAR && *text != ' ') 1289 if (fc != ' ') // skip spaces
981 {
982 int fwidth = r->TermWin.fwidth;
983 if (len >= 2 && text[1] == NOCHAR)
984 fwidth *= 2;
985
986 XGlyphInfo extents;
987 if (sizeof (text_t) == sizeof (FcChar16))
988 {
989 XftTextExtents16 (d.display->display, f, (const FcChar16 *)text, 1, &extents);
990 XftDrawString16 (d, &r->pix_colors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
991 y + r->TermWin.fbase, (const FcChar16 *)text, 1);
992 }
993 else
994 {
995 XGlyphInfo extents;
996 XftTextExtents32 (d.display->display, f, (const FcChar32 *)text, 1, &extents);
997 XftDrawString32 (d, &r->pix_colors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
998 y + r->TermWin.fbase, (const FcChar32 *)text, 1);
999 }
1000 }
1001
1002 x += r->TermWin.fwidth;
1003 text++;
1004 len--;
1005 } 1290 {
1291 FT_UInt glyph = XftCharIndex (disp, f, fc);
1292 XftGlyphExtents (disp, f, &glyph, 1, &extents);
1293
1294 ep->glyph = glyph;
1295 ep->x = x + (cwidth - extents.xOff >> 1);
1296 ep->y = y + ascent;
1297 ep++;
1298 }
1299
1300 x += cwidth;
1006 } 1301 }
1302
1303 if (ep != enc)
1304 XftDrawGlyphSpec (d, &term->pix_colors[fg].c, f, enc, ep - enc);
1007} 1305}
1008#endif 1306#endif
1009 1307
1010///////////////////////////////////////////////////////////////////////////// 1308/////////////////////////////////////////////////////////////////////////////
1011 1309
1012rxvt_fontset::rxvt_fontset (rxvt_t r) 1310rxvt_fontset::rxvt_fontset (rxvt_term *term)
1013: r (r), fontdesc (0) 1311: fontdesc (0), term (term)
1014{ 1312{
1015 clear (); 1313 clear ();
1016} 1314}
1017 1315
1018rxvt_fontset::~rxvt_fontset () 1316rxvt_fontset::~rxvt_fontset ()
1021} 1319}
1022 1320
1023void 1321void
1024rxvt_fontset::clear () 1322rxvt_fontset::clear ()
1025{ 1323{
1324 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1325 = rxvt_fontprop::unset;
1326
1026 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) 1327 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1027 FONT_UNREF (*i); 1328 FONT_UNREF (*i);
1028 1329
1330 for (pagemap **p = fmap.begin (); p != fmap.end (); p++)
1331 delete *p;
1332
1029 free (fontdesc); fontdesc = 0; 1333 free (fontdesc); fontdesc = 0;
1030 1334
1031 fonts.clear (); 1335 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 1336
1037 fallback = fallback_fonts; 1337 fallback = fallback_fonts;
1038} 1338}
1039 1339
1040rxvt_font * 1340rxvt_font *
1043 rxvt_font *f; 1343 rxvt_font *f;
1044 1344
1045 if (!name || !*name) 1345 if (!name || !*name)
1046 { 1346 {
1047 name = ""; 1347 name = "";
1048 f = new rxvt_font_default; 1348 f = new rxvt_font_default (this);
1049 } 1349 }
1050#if XFT 1350#if XFT
1051 else if (!strncmp (name, "xft:", 4)) 1351 else if (!strncmp (name, "xft:", 4))
1052 { 1352 {
1053 name += 4; 1353 name += 4;
1054 f = new rxvt_font_xft; 1354 f = new rxvt_font_xft ();
1055 } 1355 }
1056#endif 1356#endif
1057 else if (!strncmp (name, "x:", 2)) 1357 else if (!strncmp (name, "x:", 2))
1058 { 1358 {
1059 name += 2; 1359 name += 2;
1060 f = new rxvt_font_x11; 1360 f = new rxvt_font_x11;
1061 } 1361 }
1062 else 1362 else
1063 f = new rxvt_font_x11; 1363 f = new rxvt_font_x11;
1064 1364
1065 f->fs = this;
1066 f->set_term (r); 1365 f->set_term (term);
1067 f->set_name (strdup (name)); 1366 f->set_name (strdup (name));
1068 1367
1069 f->cs = cs; 1368 f->cs = cs;
1070 f->loaded = false; 1369 f->loaded = false;
1071 1370
1082 char buf[512]; 1381 char buf[512];
1083 const char *end; 1382 const char *end;
1084 1383
1085 do 1384 do
1086 { 1385 {
1087 while (*desc <= ' ') desc++; 1386 while (*desc && *desc <= ' ')
1387 desc++;
1388
1389 codeset cs = CS_UNICODE;
1088 1390
1089 if (*desc == '[') 1391 if (*desc == '[')
1090 { 1392 {
1091 fprintf (stderr, "extra font parameters not yet supported, skipping.\n"); 1393 char spec[256];
1092
1093 //const char *extra = desc++; // not yet used 1394 const char *extra = ++desc; // not yet used
1094 1395
1095 desc = strchr (desc, ']'); 1396 desc = strchr (desc, ']');
1096 1397
1097 if (!desc) 1398 if (!desc)
1098 { 1399 {
1099 fprintf (stderr, "ERROR: opening '[' without closing ']' in font specification.\n"); 1400 rxvt_warn ("ERROR: opening '[' without closing ']' in font specification, trying to continue.\n");
1100 break; 1401 break;
1101 } 1402 }
1403
1404 memcpy (spec, extra, min (desc - extra, 255));
1405 spec[min (desc - extra, 255)] = 0;
1406
1407 if (!strncmp (extra, "codeset=", sizeof ("codeset=") - 1))
1408 cs = codeset_from_name (spec + sizeof ("codeset=") - 1);
1409 else
1410 rxvt_warn ("unknown parameter '%s' in font specification, skipping.\n", spec);
1102 1411
1103 desc++; 1412 desc++;
1104 while (*desc <= ' ') desc++; 1413 while (*desc <= ' ') desc++;
1105 } 1414 }
1106 1415
1111 if (end - desc < 511) 1420 if (end - desc < 511)
1112 { 1421 {
1113 strncpy (buf, desc, end - desc); 1422 strncpy (buf, desc, end - desc);
1114 buf[end - desc] = 0; 1423 buf[end - desc] = 0;
1115 1424
1116 fonts.push_back (new_font (buf, CS_UNICODE)); 1425 fonts.push_back (new_font (buf, cs));
1117 } 1426 }
1427 else
1428 rxvt_warn ("fontset element too long (>511 bytes), ignored.");
1118 1429
1119 desc = end + 1; 1430 desc = end + 1;
1120 } 1431 }
1121 while (*end); 1432 while (*end);
1122 } 1433 }
1123} 1434}
1124 1435
1125bool 1436bool
1126rxvt_fontset::realize_font (int i) 1437rxvt_fontset::realize_font (int i)
1127{ 1438{
1439 if (i < 0 || i >= fonts.size ())
1440 return false;
1441
1128 if (fonts[i]->loaded) 1442 if (fonts[i]->loaded)
1129 return true; 1443 return true;
1130 1444
1131 fonts[i]->loaded = true; 1445 fonts[i]->loaded = true;
1132 1446
1133 if (!fonts[i]->load (base_prop)) 1447 if (!fonts[i]->load (prop))
1134 { 1448 {
1135 fonts[i]->cs = CS_UNKNOWN; 1449 fonts[i]->cs = CS_UNKNOWN;
1136 return false; 1450 return false;
1137 } 1451 }
1138 1452
1148 1462
1149 fonts.push_back (new_font (0, CS_UNICODE)); 1463 fonts.push_back (new_font (0, CS_UNICODE));
1150 realize_font (0); 1464 realize_font (0);
1151 1465
1152 add_fonts (desc); 1466 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 1467
1170 return true; 1468 return true;
1171} 1469}
1172 1470
1173int 1471int
1179 1477
1180 return -1; 1478 return -1;
1181} 1479}
1182 1480
1183int 1481int
1184rxvt_fontset::find_font (unicode_t unicode, bool bold) 1482rxvt_fontset::find_font (unicode_t unicode)
1185{ 1483{
1484 if (unicode >= 1<<20)
1485 return 0;
1186 1486
1187 for (unsigned int i = !!(0x20 <= unicode && unicode <= 0x7f); // skip pseudo-font for ascii 1487 unicode_t hi = unicode >> 8;
1188 i < fonts.size (); 1488
1189 i++) 1489 if (hi < fmap.size ()
1490 && fmap[hi]
1491 && (*fmap[hi])[unicode & 0xff] != 0xff)
1492 return (*fmap[hi])[unicode & 0xff];
1493
1494 unsigned int i;
1495
1496 for (i = 0; i < fonts.size (); i++)
1190 { 1497 {
1191 rxvt_font *f = fonts[i]; 1498 rxvt_font *f = fonts[i];
1192 1499
1193 if (!f->loaded) 1500 if (!f->loaded)
1194 { 1501 {
1195 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1502 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1196 goto next_font; 1503 goto next_font;
1197 1504
1198 if (!realize_font (i)) 1505 if (!realize_font (i))
1199 goto next_font; 1506 goto next_font;
1507
1508 if (prop.ascent != rxvt_fontprop::unset)
1509 max_it (f->ascent, prop.ascent);
1200 } 1510 }
1201 1511
1202 if (f->cs == CS_UNKNOWN) 1512 if (f->cs == CS_UNKNOWN)
1203 goto next_font; 1513 goto next_font;
1204 1514
1205 if (bold && f->properties ().weight < rxvt_fontprop::bold) 1515 bool careful;
1516 if (f->has_char (unicode, &prop, careful))
1517 {
1518 if (careful)
1519 i |= 128;
1520
1206 goto next_font; 1521 goto found;
1207 1522 }
1208 if (f->has_codepoint (unicode))
1209 return i;
1210 1523
1211 next_font: 1524 next_font:
1212 if (i == fonts.size () - 1) 1525 if (i == fonts.size () - 1)
1213 { 1526 {
1214 if (fallback->name) 1527 if (fallback->name)
1215 { 1528 {
1216 // search through the fallback list 1529 // search through the fallback list
1217 fonts.push_back (new_font (fallback->name, fallback->cs)); 1530 fonts.push_back (new_font (fallback->name, fallback->cs));
1218 fallback++; 1531 fallback++;
1219 } 1532 }
1220 else if (!bold) 1533 else
1221 { 1534 {
1222 // try to find a new font. 1535 // try to find a new font.
1223 // only xft currently supported, as there is no 1536 // only xft currently supported, as there is no
1224 // way to configure this and xft is easier to hack in, 1537 // way to configure this and xft is easier to hack in,
1225 // while x11 has more framework in place already. 1538 // while x11 has more framework in place already.
1226#if XFT 1539 // TODO: this is a real resource hog, xft takes ages(?)
1540#if XFT && USE_SLOW_LOOKUP
1227 // grab the first xft font that seems suitable 1541 // grab the first xft font that seems suitable
1228 FcPattern *p = FcPatternCreate (); 1542 FcPattern *p = FcPatternCreate ();
1229 1543
1230 FcCharSet *s = FcCharSetCreate (); 1544 FcCharSet *s = FcCharSetCreate ();
1231 FcCharSetAddChar (s, unicode); 1545 FcCharSetAddChar (s, unicode);
1232 FcPatternAddCharSet (p, FC_CHARSET, s); 1546 FcPatternAddCharSet (p, FC_CHARSET, s);
1233 // charsets don't help that much, as xft might return 1547 // charsets don't help that much, as xft might return
1234 // a non-matching font even if a better font is available :/ 1548 // a non-matching font even if a better font is available :/
1235 1549
1550 x x x x TODO prop might have unset contents
1236 FcPatternAddInteger (p, FC_PIXEL_SIZE, base_prop.height); 1551 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1237 FcPatternAddInteger (p, FC_WEIGHT, base_prop.weight); 1552 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1238 FcPatternAddInteger (p, FC_SLANT, base_prop.slant); 1553 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1239 FcPatternAddBool (p, FC_MINSPACE, 1); 1554 FcPatternAddBool (p, FC_MINSPACE, 1);
1240 //FcPatternAddBool (p, FC_ANTIALIAS, 1); 1555 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1241 1556
1242 XftResult result; 1557 XftResult result;
1243 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1558 FcPattern *match = XftFontMatch (term->display->display, term->display->screen, p, &result);
1244 1559
1245 FcPatternDestroy (p); 1560 FcPatternDestroy (p);
1246 1561
1247 if (match) 1562 if (match)
1248 { 1563 {
1263#endif 1578#endif
1264 } 1579 }
1265 } 1580 }
1266 } 1581 }
1267 1582
1268 // if no bold font found, use a regular one 1583 /* we must return SOME font */
1269 if (bold) 1584 i = 0;
1270 return find_font (unicode);
1271 1585
1272 return 0; /* we must return SOME font */ 1586found:
1273} 1587 // found a font, cache it
1588 if (i < 255)
1589 {
1590 while (hi >= fmap.size ())
1591 fmap.push_back (0);
1274 1592
1593 if (!fmap[hi])
1594 {
1595 fmap[hi] = (pagemap *)new pagemap;
1596 memset (fmap[hi], 0xff, sizeof (pagemap));
1597 }
1275 1598
1599 (*fmap[hi])[unicode & 0xff] = i;
1600 }
1276 1601
1602 return i;
1603}
1604
1605
1606

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines