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.76 by root, Tue Jan 3 23:40:58 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-2004 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
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details. 15 * GNU General Public License for more details.
16 * 16 *
17 * You should have received a copy of the GNU General Public License 17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software 18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 *---------------------------------------------------------------------*/ 20 *---------------------------------------------------------------------*/
21 21
22#include "../config.h" 22#include "../config.h"
23#include "rxvt.h" 23#include "rxvt.h"
24#include "rxvtutil.h"
24#include "rxvtfont.h" 25#include "rxvtfont.h"
25 26
26#include <cstdlib> 27#include <cstdlib>
28#include <wchar.h>
29#include <inttypes.h>
27 30
28#define DISPLAY r->display->display 31#define DISPLAY r->display->display
29#define TGC r->TermWin.gc 32#define TGC r->gc
33
34#define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width
30 35
31const struct rxvt_fallback_font { 36const struct rxvt_fallback_font {
32 codeset cs; 37 codeset cs;
33 const char *name; 38 const char *name;
34} fallback_fonts[] = { 39} fallback_fonts[] = {
52 { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" }, 57 { CS_ISO8859_10, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-10" },
53 { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" }, 58 { CS_ISO8859_11, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-11" },
54 { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" }, 59 { CS_ISO8859_13, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-13" },
55 { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" }, 60 { CS_ISO8859_14, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-14" },
56 { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" }, 61 { CS_ISO8859_16, "-*-*-*-r-*--*-*-*-*-c-*-iso8859-16" },
62
63# if XFT
64 { CS_KOI8_U, "xft::spacing=100:lang=ru:antialias=false" },
65
66 { CS_ISO8859_5, "xft::spacing=100:lang=ru:antialias=false" },
67 { CS_ISO8859_6, "xft::spacing=100:lang=ar:antialias=false" },
68 { CS_ISO8859_7, "xft::spacing=100:lang=el:antialias=false" },
69 { CS_ISO8859_8, "xft::spacing=100:lang=he:antialias=false" },
70 { CS_ISO8859_9, "xft::spacing=100:lang=tr:antialias=false" },
71 { CS_ISO8859_10, "xft::spacing=100:lang=se:antialias=false" },
72 { CS_ISO8859_11, "xft::spacing=100:lang=th:antialias=false" },
73# endif
57#endif 74#endif
58 75
59 // japanese 76 // japanese
60#if ENCODING_JP || ENCODING_JP_EXT 77#if ENCODING_JP || ENCODING_JP_EXT
61# if XFT 78# if XFT
62 // prefer xft for complex scripts 79 // prefer xft for complex scripts
63 { CS_UNICODE, "xft:Kochi Gothic:antialias=false" }, 80 { CS_JIS0208_1990_0, "xft:Kochi Gothic:antialias=false" },
81 { CS_JIS0208_1990_0, "xft:Sazanami Mincho:antialias=false" },
82 { CS_JIS0208_1990_0, "xft:Mincho:antialias=false" },
83 { CS_JIS0208_1990_0, "xft::lang=ja:spacing=100:antialias=false" },
64# endif 84# endif
65 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" }, 85 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
66 { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" }, 86 { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
67 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, 87 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
68 { CS_JIS0201_1976_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0201*-0" }, 88 { CS_JIS0201_1976_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
69 { CS_JIS0208_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0208*-0" }, 89 { CS_JIS0208_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
70 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, 90 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
71#endif 91#endif
72 92
73#if ENCODING_CN || ENCODING_CN_EXT 93#if ENCODING_ZH || ENCODING_ZH_EXT
74# if XFT 94# if XFT
95 { CS_GBK_0, "xft:AR PL KaitiM GB" },
96 { CS_GBK_0, "xft:AR PL SungtiL GB" },
97 { CS_GBK_0, "xft::spacing=100:lang=zh" },
75 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, 98 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
76 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, 99 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
77 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, 100 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
78 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, 101 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
102 { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" },
79# endif 103# endif
104 { CS_GBK_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gbk*-0" },
80 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" }, 105 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
81 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" }, 106 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
82 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" }, 107 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
108 { CS_GB2312_1980_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
83 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" }, 109 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
84 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" }, 110 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
85 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" }, 111 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
86 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" }, 112 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
87 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" }, 113 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
89 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" }, 115 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
90 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" }, 116 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
91 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" }, 117 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
92#endif 118#endif
93 119
120#if ENCODING_KR
121 { CS_KSC5601_1987_0, "-baekmuk-gulim-*-*-*-*-*-*-*-*-c-*-ksc5601*" },
122 { CS_KSC5601_1987_0, "-*-*-*-*-*-*-*-*-*-*-c-*-ksc5601*" },
94#if XFT 123# if XFT
95 { CS_UNICODE, "xft:Andale Mono" }, 124 { CS_KSC5601_1987_0, "xft:Baekmuk Gulim:antialias=false" },
96 { CS_UNICODE, "xft:Arial Unicode MS" }, 125 { CS_KSC5601_1987_0, "xft::spacing=100:lang=ko:antialias=false" },
126# endif
97#endif 127#endif
128
129 // generic font fallback
98 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" }, 130 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
99 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, 131 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
100 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" }, 132 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
101 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 133 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
102#if XFT 134#if XFT
135 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true" },
136 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" },
137 { CS_UNICODE, "xft:Andale Mono:antialias=false:autohint=false" },
138 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" },
139
103 // FreeMono is usually uglier than x fonts, so try last only. 140 // FreeMono is usually uglier than x fonts, so try last only.
104 //{ CS_UNICODE, "xft:FreeMono" }, 141 { CS_UNICODE, "xft:FreeMono:autohint=true" },
105#endif 142#endif
106 143
107 { CS_UNKNOWN, 0 } 144 { CS_UNKNOWN, 0 }
108}; 145};
146
147// these characters are used to guess the font height and width
148// pango uses a similar algorithm and doesn't trust the font either.
149static uint16_t extent_test_chars[] = {
150 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_',
151 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ
152 0x304c, 0x672c, // が本
153};
154
155#define NUM_EXTENT_TEST_CHARS (sizeof (extent_test_chars) / sizeof (extent_test_chars[0]))
109 156
110///////////////////////////////////////////////////////////////////////////// 157/////////////////////////////////////////////////////////////////////////////
111 158
112#if XFT 159#if XFT
113rxvt_drawable::~rxvt_drawable () 160rxvt_drawable::~rxvt_drawable ()
188} 235}
189 236
190///////////////////////////////////////////////////////////////////////////// 237/////////////////////////////////////////////////////////////////////////////
191 238
192void 239void
240rxvt_font::set_name (char *name)
241{
242 if (this->name == name)
243 return;
244
245 if (this->name) free (this->name); // let the compiler optimize
246 this->name = name;
247}
248
249void
193rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color) 250rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color)
194{ 251{
195 if (color == Color_bg) 252 if (color == Color_bg)
196 XClearArea (d.display->display, d, x, y, w, h, FALSE); 253 XClearArea (d.display->display, d, x, y, w, h, FALSE);
197 else if (color >= 0) 254 else if (color >= 0)
198 { 255 {
199#if XFT 256#if XFT
200 XftDrawRect (d, &r->PixColors[color].c, x, y, w, h); 257 XftDrawRect (d, &r->pix_colors[color].c, x, y, w, h);
201#else 258#else
202 XSetForeground (d.display->display, TGC, r->PixColors[color]); 259 XSetForeground (d.display->display, TGC, r->pix_colors[color]);
203 XFillRectangle (d.display->display, d, TGC, x, y, w, h); 260 XFillRectangle (d.display->display, d, TGC, x, y, w, h);
204#endif 261#endif
205 } 262 }
206} 263}
207 264
208#include "table/linedraw.h" 265#include "table/linedraw.h"
209 266
210struct rxvt_font_default : rxvt_font { 267struct rxvt_font_default : rxvt_font {
268 struct rxvt_fontset *fs;
269
270 rxvt_font_default (rxvt_fontset *fs)
271 : rxvt_font ()
272 {
273 this->fs = fs;
274 }
211 275
212 rxvt_fontprop properties () 276 rxvt_fontprop properties ()
213 { 277 {
214 rxvt_fontprop p; 278 rxvt_fontprop p;
215 279
216 p.width = p.height = 1; 280 p.width = p.height = 1;
281 p.ascent = rxvt_fontprop::unset;
217 p.weight = rxvt_fontprop::medium; 282 p.weight = rxvt_fontprop::medium;
218 p.slant = rxvt_fontprop::roman; 283 p.slant = rxvt_fontprop::roman;
219 284
220 return p; 285 return p;
221 } 286 }
223 bool load (const rxvt_fontprop &prop) 288 bool load (const rxvt_fontprop &prop)
224 { 289 {
225 width = 1; height = 1; 290 width = 1; height = 1;
226 ascent = 1; descent = 0; 291 ascent = 1; descent = 0;
227 292
228 set_name (strdup ("built-in pseudofont")); 293 set_name (strdup ("built-in support font"));
229 294
230 return true; 295 return true;
231 } 296 }
232 297
233 bool has_codepoint (unicode_t unicode) 298 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
234 { 299 {
300 careful = false;
301
235 if (unicode <= 0x001f) 302 if (unicode <= 0x001f)
236 return true; 303 return true;
237 304
238 if (unicode <= 0x007f) 305 if (unicode <= 0x007f)
239 return false; 306 return false;
248 return true; 315 return true;
249 316
250 switch (unicode) 317 switch (unicode)
251 { 318 {
252 case ZERO_WIDTH_CHAR: 319 case ZERO_WIDTH_CHAR:
320 case NOCHAR:
253 return true; 321 return true;
254 } 322 }
255 323
256 return false; 324 return false;
257 } 325 }
264void 332void
265rxvt_font_default::draw (rxvt_drawable &d, int x, int y, 333rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
266 const text_t *text, int len, 334 const text_t *text, int len,
267 int fg, int bg) 335 int fg, int bg)
268{ 336{
337 Display *disp = d.display->display;
338
269 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 339 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
270 340
271 XSetForeground (d.display->display, TGC, r->PixColors[fg]); 341 XSetForeground (disp, TGC, r->pix_colors[fg]);
272 342
273 while (len--) 343 while (len)
274 { 344 {
275#if ENABLE_COMBINING 345#if ENABLE_COMBINING
276 compose_char *cc; 346 compose_char *cc;
277#endif 347#endif
348 const text_t *tp = text;
278 text_t t = *text++; 349 text_t t = *tp;
350
351 while (++text, --len && *text == NOCHAR)
352 ;
353
354 int width = text - tp;
355 int fwidth = r->fwidth * width;
279 356
280 if (0x2500 <= t && t <= 0x259f) 357 if (0x2500 <= t && t <= 0x259f)
281 { 358 {
282 uint16_t offs = linedraw_offs[t - 0x2500]; 359 uint16_t offs = linedraw_offs[t - 0x2500];
283 uint32_t *a = linedraw_command + (offs >> 4); 360 uint32_t *a = linedraw_command + (offs >> 4);
284 uint32_t *b = a + (offs & 15); 361 uint32_t *b = a + (offs & 15);
285 362
286 int W = r->TermWin.fwidth; 363 int W = fwidth;
287 int H = r->TermWin.fheight; 364 int H = r->fheight;
288 365
289 int x_[16]; 366 int x_[16];
290 int y_[16]; 367 int y_[16];
291 368
292 for (int i = 0; i <= 8; i++) 369 for (int i = 0; i <= 8; i++)
300 377
301 XGCValues gcv; 378 XGCValues gcv;
302 379
303 gcv.cap_style = CapButt; 380 gcv.cap_style = CapButt;
304 gcv.line_width = 0; 381 gcv.line_width = 0;
305 XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv); 382 XChangeGC (disp, TGC, GCLineWidth | GCCapStyle, &gcv);
306 383
307 while (a < b) 384 while (a < b)
308 { 385 {
309 uint32_t command = *a++; 386 uint32_t command = *a++;
310 387
317 int y2 = y_[(command >> 0) & 15]; 394 int y2 = y_[(command >> 0) & 15];
318 395
319 switch (op) 396 switch (op)
320 { 397 {
321 case 0: // line 398 case 0: // line
322 XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2); 399 XDrawLine (disp, d, TGC, x1, y1, x2, y2);
323 break; 400 break;
324 401
325 case 1: // rectangle, possibly stippled 402 case 1: // rectangle, possibly stippled
326 if (a) 403 if (a)
327 { 404 {
328 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 }; 405 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
329 406
330 gcv.fill_style = FillStippled; 407 gcv.fill_style = FillStippled;
331 gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2); 408 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
332 gcv.ts_x_origin = x; 409 gcv.ts_x_origin = x;
333 gcv.ts_y_origin = y; 410 gcv.ts_y_origin = y;
334 411
335 XChangeGC (d.display->display, TGC, 412 XChangeGC (disp, TGC,
336 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, 413 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
337 &gcv); 414 &gcv);
338 } 415 }
339 416
340 XFillRectangle (d.display->display, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1); 417 XFillRectangle (disp, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
341 418
342 if (a) 419 if (a)
343 { 420 {
344 XFreePixmap (d.display->display, gcv.stipple); 421 XFreePixmap (disp, gcv.stipple);
345 gcv.stipple = 0; 422 gcv.stipple = 0;
346 gcv.fill_style = FillSolid; 423 gcv.fill_style = FillSolid;
347 XChangeGC (d.display->display, TGC, GCFillStyle, &gcv); 424 XChangeGC (disp, TGC, GCFillStyle, &gcv);
348 } 425 }
349 break; 426 break;
350 case 2: // arc 427 case 2: // arc
351 XDrawArc (d.display->display, d, TGC, 428 XDrawArc (disp, d, TGC,
352 x1 - W/2, y1 - H/2, W-1, H-1, 429 x1 - W/2, y1 - H/2, W-1, H-1,
353 (a - 1) * 90*64, (b - 1) * 90*64); 430 (a - 1) * 90*64, (b - 1) * 90*64);
354 break; 431 break;
355 } 432 }
356 } 433 }
357 } 434 }
358#if ENABLE_COMBINING 435#if ENABLE_COMBINING
359 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t])) 436 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
360 { 437 {
438 text_t chrs[2];
439 width = min (2, width);
440 chrs [1] = NOCHAR;
441
442 *chrs = cc->c1;
361 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)]; 443 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
362 f1->draw (d, x, y, &(t = cc->c1), 1, fg, bg); 444 f1->draw (d, x, y, chrs, width, fg, bg);
445
363 if (cc->c2 != NOCHAR) 446 if (cc->c2 != NOCHAR)
364 { 447 {
448 bool careful;
449
365 // prefer font of first character, for no good reasons 450 // prefer font of first character, for no good reasons
366 rxvt_font *f2 = f1->has_codepoint (cc->c2) 451 *chrs = cc->c2;
452 rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful)
367 ? f1 453 ? f1
368 : (*fs)[fs->find_font (cc->c2)]; 454 : (*fs)[fs->find_font (cc->c2)];
369 455
370 f2->draw (d, x, y, &(t = cc->c2), 1, fg, -1); 456 f2->draw (d, x, y, chrs, width, fg, -1);
371 } 457 }
372 } 458 }
373#endif 459#endif
374 else 460 else
375 switch (t) 461 switch (t)
376 { 462 {
463 case '\t':
377 case ZERO_WIDTH_CHAR: 464 case ZERO_WIDTH_CHAR:
465 case NOCHAR:
378 break; 466 break;
379 467
380 default: 468 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, 469 XDrawRectangle (disp, d, TGC, x + 2, y + 2,
390 w + r->TermWin.fwidth - 4, r->TermWin.fheight - 4); 470 fwidth - 4, r->fheight - 4);
391 x += w;
392 } 471 }
393 472
394 x += r->TermWin.fwidth; 473 x += fwidth;
395 } 474 }
396} 475}
397 476
398///////////////////////////////////////////////////////////////////////////// 477/////////////////////////////////////////////////////////////////////////////
399 478
404 483
405 rxvt_fontprop properties (); 484 rxvt_fontprop properties ();
406 485
407 bool load (const rxvt_fontprop &prop); 486 bool load (const rxvt_fontprop &prop);
408 487
409 bool has_codepoint (unicode_t unicode); 488 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful);
410 489
411 void draw (rxvt_drawable &d, int x, int y, 490 void draw (rxvt_drawable &d, int x, int y,
412 const text_t *text, int len, 491 const text_t *text, int len,
413 int fg, int bg); 492 int fg, int bg);
414 493
494 bool slow; // wether this is a proportional font or has other funny characteristics
415 XFontStruct *f; 495 XFontStruct *f;
416 codeset cs; 496 codeset cs;
417 bool enc2b, encm; 497 bool enc2b, encm;
418 498
419 char *get_property (XFontStruct *f, const char *property, const char *repl) const; 499 char *get_property (XFontStruct *f, const char *property, const char *repl) const;
442} 522}
443 523
444bool 524bool
445rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth) 525rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
446{ 526{
447 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2; 527 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
448 p.height = height; 528 p.height = height;
529 p.ascent = rxvt_fontprop::unset;
449 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium; 530 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; 531 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
451 532
452 return true; 533 return true;
453} 534}
454 535
455bool 536bool
456rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) 537rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
457{ 538{
458 unsigned long height; 539 unsigned long height;
540
541#if 0
459 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) 542 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
460 return false; 543 return false;
544#else
545 height = f->ascent + f->descent;
546#endif
461 547
462 unsigned long avgwidth; 548 unsigned long avgwidth;
463 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) 549 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
464 avgwidth = 0; 550 avgwidth = 0;
465 551
468 554
469 set_properties (p, height, weight, slant, avgwidth); 555 set_properties (p, height, weight, slant, avgwidth);
470 556
471 free (weight); 557 free (weight);
472 free (slant); 558 free (slant);
559
560 p.ascent = f->ascent;
473 561
474 return true; 562 return true;
475} 563}
476 564
477bool 565bool
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 Display *disp = DISPLAY;
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 fotns
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
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)
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->width;
944 int wcw = wcwidth (unicode);
945 if (wcw > 0) w = (w + wcw - 1) / wcw;
946
947 careful = w > prop->width;
948 if (careful && w > prop->width * MAX_OVERLAP >> 2)
714 return false; 949 return false;
715 950
716 return true; 951 return true;
717} 952}
718 953
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
729 bool slow = this->slow 964 bool slow = this->slow
730 || width != r->TermWin.fwidth 965 || width != r->fwidth
731 || height != r->TermWin.fheight; 966 || height != r->fheight;
732 967
733 int base = r->TermWin.fbase; 968 int base = ascent; // sorry, incorrect: r->fbase;
734 969
735 XGCValues v; 970 XGCValues v;
736 v.foreground = r->PixColors[fg]; 971 v.foreground = r->pix_colors[fg];
737 v.font = f->fid; 972 v.font = f->fid;
738 973
739 if (enc2b) 974 if (enc2b)
740 { 975 {
741 const XChar2b *xc = enc_xchar2b (text, len, cs, slow); 976 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
742 977
743 if (bg == Color_bg && !slow) 978 if (bg == Color_bg && !slow)
744 { 979 {
745 v.background = r->PixColors[bg]; 980 v.background = r->pix_colors[bg];
746 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 981 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
747 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len); 982 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
748 } 983 }
749 else 984 else
750 { 985 {
751 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 986 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
752 987
753 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 988 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
754 989
755 if (slow) 990 if (slow)
756 { 991 {
757 do 992 do
758 { 993 {
759 if (xc->byte1 || xc->byte2) 994 if (xc->byte1 || xc->byte2)
760 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1); 995 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
761 996
762 x += r->TermWin.fwidth; 997 x += r->fwidth;
763 xc++; len--; 998 xc++; len--;
764 } 999 }
765 while (len); 1000 while (len);
766 } 1001 }
767 else 1002 else
772 { 1007 {
773 const char *xc = enc_char (text, len, cs, slow); 1008 const char *xc = enc_char (text, len, cs, slow);
774 1009
775 if (bg == Color_bg && !slow) 1010 if (bg == Color_bg && !slow)
776 { 1011 {
777 v.background = r->PixColors[bg]; 1012 v.background = r->pix_colors[bg];
778 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 1013 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
779 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len); 1014 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
780 } 1015 }
781 else 1016 else
782 { 1017 {
783 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1018 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
784 1019
785 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 1020 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
786 1021
787 if (slow) 1022 if (slow)
788 { 1023 {
789 do 1024 do
790 { 1025 {
791 if (*xc) 1026 if (*xc)
792 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1); 1027 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
793 1028
794 x += r->TermWin.fwidth; 1029 x += r->fwidth;
795 xc++; len--; 1030 xc++; len--;
796 } 1031 }
797 while (len); 1032 while (len);
798 } 1033 }
799 else 1034 else
803} 1038}
804 1039
805///////////////////////////////////////////////////////////////////////////// 1040/////////////////////////////////////////////////////////////////////////////
806 1041
807#if XFT 1042#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 1043
814struct rxvt_font_xft : rxvt_font { 1044struct rxvt_font_xft : rxvt_font {
815 rxvt_font_xft () { f = 0; } 1045 rxvt_font_xft () { f = 0; }
816 1046
817 void clear (); 1047 void clear ();
822 1052
823 void draw (rxvt_drawable &d, int x, int y, 1053 void draw (rxvt_drawable &d, int x, int y,
824 const text_t *text, int len, 1054 const text_t *text, int len,
825 int fg, int bg); 1055 int fg, int bg);
826 1056
827 bool has_codepoint (unicode_t unicode); 1057 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &carefull);
828 1058
829protected: 1059protected:
830 XftFont *f; 1060 XftFont *f;
831}; 1061};
832 1062
847 1077
848 FT_Face face = XftLockFace (f); 1078 FT_Face face = XftLockFace (f);
849 1079
850 p.width = width; 1080 p.width = width;
851 p.height = height; 1081 p.height = height;
1082 p.ascent = ascent;
852 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD 1083 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
853 ? rxvt_fontprop::bold : rxvt_fontprop::medium; 1084 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
854 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC 1085 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
855 ? rxvt_fontprop::italic : rxvt_fontprop::roman; 1086 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
856 1087
860} 1091}
861 1092
862bool 1093bool
863rxvt_font_xft::load (const rxvt_fontprop &prop) 1094rxvt_font_xft::load (const rxvt_fontprop &prop)
864{ 1095{
865#if 0 1096 Display *disp = DISPLAY;
866 for (int i = 0; i < SWATHCOUNT; i++)
867 cvr[i] = 0;
868#endif
869 1097
870 clear (); 1098 clear ();
871 1099
872 FcPattern *p = FcNameParse ((FcChar8 *) name); 1100 FcPattern *p = FcNameParse ((FcChar8 *) name);
873 1101
874 if (!p) 1102 if (!p)
875 return false; 1103 return false;
876 1104
877 FcValue v; 1105 FcValue v;
878 1106
1107 if (prop.height != rxvt_fontprop::unset
879 if (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch) 1108 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1109 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
880 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); 1110 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
881 1111
1112 if (prop.weight != rxvt_fontprop::unset
882 if (FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) 1113 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
883 FcPatternAddInteger (p, FC_WEIGHT, prop.weight); 1114 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
884 1115
1116 if (prop.slant != rxvt_fontprop::unset
885 if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch) 1117 && FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
886 FcPatternAddInteger (p, FC_SLANT, prop.slant); 1118 FcPatternAddInteger (p, FC_SLANT, prop.slant);
887
888 if (FcPatternGet (p, FC_MINSPACE, 0, &v) != FcResultMatch)
889 FcPatternAddBool (p, FC_MINSPACE, 1);
890 1119
891#if 0 // clipping unfortunately destroys our precious double-width-characters 1120#if 0 // clipping unfortunately destroys our precious double-width-characters
892 // clip width, we can't do better, or can we? 1121 // clip width, we can't do better, or can we?
893 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch) 1122 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
894 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width); 1123 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop->width);
895#endif 1124#endif
896 1125
1126 if (FcPatternGet (p, FC_MINSPACE, 0, &v) != FcResultMatch)
1127 FcPatternAddBool (p, FC_MINSPACE, 1);
1128
1129 // store generated name so iso14755 view gives better results
1130 set_name ((char *)FcNameUnparse (p));
1131
897 XftResult result; 1132 XftResult result;
898 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1133 FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result);
899 1134
900 FcPatternDestroy (p); 1135 FcPatternDestroy (p);
901 1136
902 if (!match) 1137 if (!match)
903 return false; 1138 return false;
904 1139
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; 1140 int ftheight = 0;
1141 bool success = true;
918 1142
919 for (;;) 1143 for (;;)
920 { 1144 {
921 XGlyphInfo g1, g2; 1145 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
922 FcChar8 c;
923 1146
924 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1); 1147 if (!f)
925 c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2); 1148 {
1149 success = false;
1150 break;
1151 }
926 1152
927 if (g1.xOff != g2.xOff) // don't simply trust the font 1153 FT_Face face = XftLockFace (f);
928 slow = true;
929 1154
930 width = g2.xOff;
931 ascent = (face->size->metrics.ascender + 63) >> 6; 1155 ascent = (face->size->metrics.ascender + 63) >> 6;
932 descent = (-face->size->metrics.descender + 63) >> 6; 1156 descent = (-face->size->metrics.descender + 63) >> 6;
933 height = ascent + descent; 1157 height = max (ascent + descent, (face->size->metrics.height + 63) >> 6);
1158 width = 0;
934 1159
1160 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1161
1162 XftUnlockFace (f);
1163
1164 int glheight = height;
1165
1166 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1167 {
1168 FcChar16 ch = *t;
1169
1170 if (cs != CS_UNICODE
1171 && ch > 0x100
1172 && FROM_UNICODE (cs, ch) == NOCHAR)
1173 continue;
1174
1175 // ignore characters we wouldn't use anyways
1176 bool careful;
1177 if (!has_char (*t, &prop, careful))
1178 continue;
1179
1180 XGlyphInfo g;
1181 XftTextExtents16 (disp, f, &ch, 1, &g);
1182
1183 g.width -= g.x;
1184
1185 int wcw = wcwidth (ch);
1186 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1187
1188 if (width < g.width ) width = g.width;
1189 if (height < g.height ) height = g.height;
1190 if (glheight < g.height - g.y) glheight = g.height - g.y;
1191 }
1192
1193 if (!width)
1194 {
1195 rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name);
1196
1197 XftFontClose (disp, f);
1198 f = 0;
1199
1200 success = false;
1201 break;
1202 }
1203
1204 if (prop.height == rxvt_fontprop::unset
935 if (height <= prop.height || !prop.height) 1205 || (height <= prop.height && glheight <= prop.height)
1206 || height <= 2
1207 || !scalable)
936 break; 1208 break;
937 1209
938 if (ftheight) 1210 if (ftheight)
939 { 1211 {
940 // take smaller steps near the end 1212 // take smaller steps near the end
941 if (height > prop.height + 1) ftheight++; 1213 if (height > prop.height + 1) ftheight++;
942 if (height > prop.height + 2) ftheight++; 1214 if (height > prop.height + 2) ftheight++;
943 if (height > prop.height + 3) ftheight++; 1215 if (height > prop.height + 3) ftheight++;
944 1216
945 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - prop.height); 1217 ftheight -= height - prop.height;
946 } 1218 }
947 else 1219 else
948 FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height); 1220 ftheight = prop.height - 1;
1221
1222 XftFontClose (disp, f);
1223 FcPatternDel (match, FC_PIXEL_SIZE);
1224 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1225 }
1226
1227 FcPatternDestroy (match);
1228
1229#if 0 // do it per-character
1230 if (prop.width != rxvt_fontprop::unset && width > prop.width)
949 } 1231 {
1232 clear ();
1233 success = false;
1234 }
1235#endif
950 1236
951 XftUnlockFace (f); 1237 return success;
1238}
1239
1240bool
1241rxvt_font_xft::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful)
1242{
1243 careful = false;
1244
1245 if (!XftCharExists (DISPLAY, f, unicode))
1246 return false;
1247
1248 if (!prop || prop->width == rxvt_fontprop::unset)
1249 return true;
1250
1251 // check character against base font bounding box
1252 FcChar32 ch = unicode;
1253 XGlyphInfo g;
1254 XftTextExtents32 (DISPLAY, f, &ch, 1, &g);
1255
1256 int w = g.width - g.x;
1257 int wcw = wcwidth (unicode);
1258 if (wcw > 0) w = (w + wcw - 1) / wcw;
1259
1260 careful = w > prop->width;
1261 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1262 return false;
952 1263
953 return true; 1264 return true;
954}
955
956bool
957rxvt_font_xft::has_codepoint (unicode_t unicode)
958{
959 return XftCharExists (DISPLAY, f, unicode);
960} 1265}
961 1266
962void 1267void
963rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1268rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
964 const text_t *text, int len, 1269 const text_t *text, int len,
965 int fg, int bg) 1270 int fg, int bg)
966{ 1271{
967 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1272 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
968 1273
969 if (!slow && width == r->TermWin.fwidth && 0) 1274 int base = ascent; // should be fbase, but that is incorrect
1275
1276 XGlyphInfo extents;
1277 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32));
1278 FcChar32 *ep = enc;
1279 int ewidth = 0;
1280
1281 while (len)
970 { 1282 {
971 if (sizeof (text_t) == sizeof (FcChar16)) 1283 int cwidth = r->fwidth;
972 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len); 1284 FcChar32 fc = *text++; len--;
1285
1286 while (len && *text == NOCHAR)
1287 text++, len--, cwidth += r->fwidth;
1288
1289 if (fc == ' ' && ep == enc) // skip leading spaces
1290 x += cwidth;
973 else 1291 else
974 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len);
975 }
976 else
977 {
978 while (len)
979 { 1292 {
980 if (*text != NOCHAR && *text != ' ') 1293 FT_UInt gl = XftCharIndex (d.display->display, f, fc);
1294 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1295
1296 if (extents.xOff != cwidth)
981 { 1297 {
982 int fwidth = r->TermWin.fwidth;
983 if (len >= 2 && text[1] == NOCHAR)
984 fwidth *= 2; 1298 if (ewidth)
985
986 XGlyphInfo extents;
987 if (sizeof (text_t) == sizeof (FcChar16))
988 { 1299 {
989 XftTextExtents16 (d.display->display, f, (const FcChar16 *)text, 1, &extents); 1300 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
990 XftDrawString16 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2, 1301 x, y + base, enc, ep - enc);
991 y + r->TermWin.fbase, (const FcChar16 *)text, 1); 1302 x += ewidth;
1303
1304 ep = enc;
1305 ewidth = 0;
992 } 1306 }
993 else 1307
994 { 1308 if (extents.xOff > cwidth)
995 XGlyphInfo extents; 1309 extents.xOff = cwidth;
996 XftTextExtents32 (d.display->display, f, (const FcChar32 *)text, 1, &extents); 1310
997 XftDrawString32 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2, 1311 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
998 y + r->TermWin.fbase, (const FcChar32 *)text, 1); 1312 x + (cwidth - extents.xOff >> 1),
999 } 1313 y + base, &gl, 1);
1314 x += cwidth;
1000 } 1315 }
1001
1002 x += r->TermWin.fwidth;
1003 text++;
1004 len--; 1316 else
1317 {
1318 *ep++ = gl;
1319 ewidth += cwidth;
1005 } 1320 }
1321 }
1006 } 1322 }
1323
1324 if (ep != enc)
1325 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1326 x, y + base, enc, ep - enc);
1007} 1327}
1008#endif 1328#endif
1009 1329
1010///////////////////////////////////////////////////////////////////////////// 1330/////////////////////////////////////////////////////////////////////////////
1011 1331
1012rxvt_fontset::rxvt_fontset (rxvt_t r) 1332rxvt_fontset::rxvt_fontset (rxvt_t r)
1013: r (r), fontdesc (0) 1333: fontdesc (0), r (r)
1014{ 1334{
1015 clear (); 1335 clear ();
1016} 1336}
1017 1337
1018rxvt_fontset::~rxvt_fontset () 1338rxvt_fontset::~rxvt_fontset ()
1021} 1341}
1022 1342
1023void 1343void
1024rxvt_fontset::clear () 1344rxvt_fontset::clear ()
1025{ 1345{
1346 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1347 = rxvt_fontprop::unset;
1348
1026 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) 1349 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1027 FONT_UNREF (*i); 1350 FONT_UNREF (*i);
1028 1351
1352 for (pagemap **p = fmap.begin (); p != fmap.end (); p++)
1353 delete *p;
1354
1029 free (fontdesc); fontdesc = 0; 1355 free (fontdesc); fontdesc = 0;
1030 1356
1031 fonts.clear (); 1357 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 1358
1037 fallback = fallback_fonts; 1359 fallback = fallback_fonts;
1038} 1360}
1039 1361
1040rxvt_font * 1362rxvt_font *
1043 rxvt_font *f; 1365 rxvt_font *f;
1044 1366
1045 if (!name || !*name) 1367 if (!name || !*name)
1046 { 1368 {
1047 name = ""; 1369 name = "";
1048 f = new rxvt_font_default; 1370 f = new rxvt_font_default (this);
1049 } 1371 }
1050#if XFT 1372#if XFT
1051 else if (!strncmp (name, "xft:", 4)) 1373 else if (!strncmp (name, "xft:", 4))
1052 { 1374 {
1053 name += 4; 1375 name += 4;
1054 f = new rxvt_font_xft; 1376 f = new rxvt_font_xft ();
1055 } 1377 }
1056#endif 1378#endif
1057 else if (!strncmp (name, "x:", 2)) 1379 else if (!strncmp (name, "x:", 2))
1058 { 1380 {
1059 name += 2; 1381 name += 2;
1060 f = new rxvt_font_x11; 1382 f = new rxvt_font_x11;
1061 } 1383 }
1062 else 1384 else
1063 f = new rxvt_font_x11; 1385 f = new rxvt_font_x11;
1064 1386
1065 f->fs = this;
1066 f->set_term (r); 1387 f->set_term (r);
1067 f->set_name (strdup (name)); 1388 f->set_name (strdup (name));
1068 1389
1069 f->cs = cs; 1390 f->cs = cs;
1070 f->loaded = false; 1391 f->loaded = false;
1082 char buf[512]; 1403 char buf[512];
1083 const char *end; 1404 const char *end;
1084 1405
1085 do 1406 do
1086 { 1407 {
1087 while (*desc <= ' ') desc++; 1408 while (*desc && *desc <= ' ')
1409 desc++;
1410
1411 codeset cs = CS_UNICODE;
1088 1412
1089 if (*desc == '[') 1413 if (*desc == '[')
1090 { 1414 {
1091 fprintf (stderr, "extra font parameters not yet supported, skipping.\n"); 1415 char spec[256];
1092
1093 //const char *extra = desc++; // not yet used 1416 const char *extra = ++desc; // not yet used
1094 1417
1095 desc = strchr (desc, ']'); 1418 desc = strchr (desc, ']');
1096 1419
1097 if (!desc) 1420 if (!desc)
1098 { 1421 {
1099 fprintf (stderr, "ERROR: opening '[' without closing ']' in font specification.\n"); 1422 rxvt_warn ("ERROR: opening '[' without closing ']' in font specification, trying to continue.\n");
1100 break; 1423 break;
1101 } 1424 }
1425
1426 memcpy (spec, extra, min (desc - extra, 255));
1427 spec[min (desc - extra, 255)] = 0;
1428
1429 if (!strncmp (extra, "codeset=", sizeof ("codeset=") - 1))
1430 cs = codeset_from_name (spec + sizeof ("codeset=") - 1);
1431 else
1432 rxvt_warn ("unknown parameter '%s' in font specification, skipping.\n", spec);
1102 1433
1103 desc++; 1434 desc++;
1104 while (*desc <= ' ') desc++; 1435 while (*desc <= ' ') desc++;
1105 } 1436 }
1106 1437
1111 if (end - desc < 511) 1442 if (end - desc < 511)
1112 { 1443 {
1113 strncpy (buf, desc, end - desc); 1444 strncpy (buf, desc, end - desc);
1114 buf[end - desc] = 0; 1445 buf[end - desc] = 0;
1115 1446
1116 fonts.push_back (new_font (buf, CS_UNICODE)); 1447 fonts.push_back (new_font (buf, cs));
1117 } 1448 }
1449 else
1450 rxvt_warn ("fontset element too long (>511 bytes), ignored.");
1118 1451
1119 desc = end + 1; 1452 desc = end + 1;
1120 } 1453 }
1121 while (*end); 1454 while (*end);
1122 } 1455 }
1123} 1456}
1124 1457
1125bool 1458bool
1126rxvt_fontset::realize_font (int i) 1459rxvt_fontset::realize_font (int i)
1127{ 1460{
1461 if (i < 0 || i >= fonts.size ())
1462 return false;
1463
1128 if (fonts[i]->loaded) 1464 if (fonts[i]->loaded)
1129 return true; 1465 return true;
1130 1466
1131 fonts[i]->loaded = true; 1467 fonts[i]->loaded = true;
1132 1468
1133 if (!fonts[i]->load (base_prop)) 1469 if (!fonts[i]->load (prop))
1134 { 1470 {
1135 fonts[i]->cs = CS_UNKNOWN; 1471 fonts[i]->cs = CS_UNKNOWN;
1136 return false; 1472 return false;
1137 } 1473 }
1138 1474
1148 1484
1149 fonts.push_back (new_font (0, CS_UNICODE)); 1485 fonts.push_back (new_font (0, CS_UNICODE));
1150 realize_font (0); 1486 realize_font (0);
1151 1487
1152 add_fonts (desc); 1488 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 1489
1170 return true; 1490 return true;
1171} 1491}
1172 1492
1173int 1493int
1179 1499
1180 return -1; 1500 return -1;
1181} 1501}
1182 1502
1183int 1503int
1184rxvt_fontset::find_font (unicode_t unicode, bool bold) 1504rxvt_fontset::find_font (unicode_t unicode)
1185{ 1505{
1506 if (unicode >= 1<<20)
1507 return 0;
1186 1508
1187 for (unsigned int i = !!(0x20 <= unicode && unicode <= 0x7f); // skip pseudo-font for ascii 1509 unicode_t hi = unicode >> 8;
1188 i < fonts.size (); 1510
1189 i++) 1511 if (hi < fmap.size ()
1512 && fmap[hi]
1513 && (*fmap[hi])[unicode & 0xff] != 0xff)
1514 return (*fmap[hi])[unicode & 0xff];
1515
1516 unsigned int i;
1517
1518 for (i = 0; i < fonts.size (); i++)
1190 { 1519 {
1191 rxvt_font *f = fonts[i]; 1520 rxvt_font *f = fonts[i];
1192 1521
1193 if (!f->loaded) 1522 if (!f->loaded)
1194 { 1523 {
1195 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1524 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1196 goto next_font; 1525 goto next_font;
1197 1526
1198 if (!realize_font (i)) 1527 if (!realize_font (i))
1199 goto next_font; 1528 goto next_font;
1529
1530 if (prop.ascent != rxvt_fontprop::unset)
1531 max_it (f->ascent, prop.ascent);
1200 } 1532 }
1201 1533
1202 if (f->cs == CS_UNKNOWN) 1534 if (f->cs == CS_UNKNOWN)
1203 goto next_font; 1535 goto next_font;
1204 1536
1205 if (bold && f->properties ().weight < rxvt_fontprop::bold) 1537 bool careful;
1538 if (f->has_char (unicode, &prop, careful))
1539 {
1540 if (careful)
1541 i |= 128;
1542
1206 goto next_font; 1543 goto found;
1207 1544 }
1208 if (f->has_codepoint (unicode))
1209 return i;
1210 1545
1211 next_font: 1546 next_font:
1212 if (i == fonts.size () - 1) 1547 if (i == fonts.size () - 1)
1213 { 1548 {
1214 if (fallback->name) 1549 if (fallback->name)
1221 { 1556 {
1222 // try to find a new font. 1557 // try to find a new font.
1223 // only xft currently supported, as there is no 1558 // only xft currently supported, as there is no
1224 // way to configure this and xft is easier to hack in, 1559 // way to configure this and xft is easier to hack in,
1225 // while x11 has more framework in place already. 1560 // while x11 has more framework in place already.
1226#if XFT 1561 // TODO: this is a real resource hog, xft takes ages(?)
1562#if XFT && USE_SLOW_LOOKUP
1227 // grab the first xft font that seems suitable 1563 // grab the first xft font that seems suitable
1228 FcPattern *p = FcPatternCreate (); 1564 FcPattern *p = FcPatternCreate ();
1229 1565
1230 FcCharSet *s = FcCharSetCreate (); 1566 FcCharSet *s = FcCharSetCreate ();
1231 FcCharSetAddChar (s, unicode); 1567 FcCharSetAddChar (s, unicode);
1232 FcPatternAddCharSet (p, FC_CHARSET, s); 1568 FcPatternAddCharSet (p, FC_CHARSET, s);
1233 // charsets don't help that much, as xft might return 1569 // charsets don't help that much, as xft might return
1234 // a non-matching font even if a better font is available :/ 1570 // a non-matching font even if a better font is available :/
1235 1571
1572 x x x x TODO prop might have unset contents
1236 FcPatternAddInteger (p, FC_PIXEL_SIZE, base_prop.height); 1573 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1237 FcPatternAddInteger (p, FC_WEIGHT, base_prop.weight); 1574 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1238 FcPatternAddInteger (p, FC_SLANT, base_prop.slant); 1575 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1239 FcPatternAddBool (p, FC_MINSPACE, 1); 1576 FcPatternAddBool (p, FC_MINSPACE, 1);
1240 //FcPatternAddBool (p, FC_ANTIALIAS, 1); 1577 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1241 1578
1242 XftResult result; 1579 XftResult result;
1243 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1580 FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result);
1244 1581
1245 FcPatternDestroy (p); 1582 FcPatternDestroy (p);
1246 1583
1247 if (match) 1584 if (match)
1248 { 1585 {
1263#endif 1600#endif
1264 } 1601 }
1265 } 1602 }
1266 } 1603 }
1267 1604
1268 // if no bold font found, use a regular one 1605 /* we must return SOME font */
1269 if (bold) 1606 i = 0;
1270 return find_font (unicode);
1271 1607
1272 return 0; /* we must return SOME font */ 1608found:
1273} 1609 // found a font, cache it
1610 if (i < 255)
1611 {
1612 while (hi >= fmap.size ())
1613 fmap.push_back (0);
1274 1614
1615 if (!fmap[hi])
1616 {
1617 fmap[hi] = (pagemap *)new pagemap;
1618 memset (fmap[hi], 0xff, sizeof (pagemap));
1619 }
1275 1620
1621 (*fmap[hi])[unicode & 0xff] = i;
1622 }
1276 1623
1624 return i;
1625}
1626
1627
1628

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines