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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines