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.46 by root, Wed Sep 8 05:45:30 2004 UTC vs.
Revision 1.84 by root, Tue Jan 17 05:47:42 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"
24#include "rxvtutil.h" 25#include "rxvtutil.h"
25#include "rxvtfont.h" 26#include "rxvtfont.h"
26 27
27#include <cstdlib> 28#include <cstdlib>
28#include <wchar.h> 29#include <wchar.h>
29#include <inttypes.h> 30#include <inttypes.h>
30
31#define DISPLAY r->display->display
32#define TGC r->TermWin.gc
33 31
34#define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width 32#define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width
35 33
36const struct rxvt_fallback_font { 34const struct rxvt_fallback_font {
37 codeset cs; 35 codeset cs;
90 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, 88 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
91#endif 89#endif
92 90
93#if ENCODING_ZH || ENCODING_ZH_EXT 91#if ENCODING_ZH || ENCODING_ZH_EXT
94# if XFT 92# if XFT
93 { CS_GBK_0, "xft:AR PL KaitiM GB" },
94 { CS_GBK_0, "xft:AR PL SungtiL GB" },
95 { CS_GBK_0, "xft::spacing=100:lang=zh" },
95 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, 96 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
96 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, 97 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
97 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, 98 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
98 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, 99 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
99 { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" }, 100 { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" },
100# endif 101# endif
102 { CS_GBK_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gbk*-0" },
101 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" }, 103 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
102 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" }, 104 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
103 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" }, 105 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
106 { CS_GB2312_1980_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
104 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" }, 107 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
105 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" }, 108 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
106 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" }, 109 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
107 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" }, 110 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
108 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" }, 111 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
125 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" }, 128 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
126 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, 129 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
127 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" }, 130 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
128 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 131 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
129#if XFT 132#if XFT
130 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true"}, 133 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true" },
131 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" }, 134 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" },
132 { CS_UNICODE, "xft:Andale Mono:antialias=false" }, 135 { CS_UNICODE, "xft:Andale Mono:antialias=false:autohint=false" },
133 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false" }, 136 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" },
134 137
135 // FreeMono is usually uglier than x fonts, so try last only. 138 // FreeMono is usually uglier than x fonts, so try last only.
136 { CS_UNICODE, "xft:FreeMono:autohint=true" }, 139 { CS_UNICODE, "xft:FreeMono:autohint=true" },
137#endif 140#endif
138 141
139 { CS_UNKNOWN, 0 } 142 { CS_UNKNOWN, 0 }
140}; 143};
141 144
142// these characters are used to guess the font height and width 145// these characters are used to guess the font height and width
143// pango uses a similar algorithm and eosn't trust the font either. 146// pango uses a similar algorithm and doesn't trust the font either.
144static uint16_t extent_test_chars[] = { 147static uint16_t extent_test_chars[] = {
145 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_', 148 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_',
146 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ 149 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ
147 0x304c, 0x672c, // が本 150 0x304c, 0x672c, // が本
148}; 151};
149 152
150#define NUM_EXTENT_TEST_CHARS (sizeof (extent_test_chars) / sizeof (extent_test_chars[0])) 153#define NUM_EXTENT_TEST_CHARS (sizeof (extent_test_chars) / sizeof (extent_test_chars[0]))
151 154
155#define dTermDisplay Display *disp = term->display->display
156#define dTermGC GC gc = term->gc
157
152///////////////////////////////////////////////////////////////////////////// 158/////////////////////////////////////////////////////////////////////////////
153 159
154#if XFT 160#if XFT
155rxvt_drawable::~rxvt_drawable () 161rxvt_drawable::~rxvt_drawable ()
156{ 162{
240 if (this->name) free (this->name); // let the compiler optimize 246 if (this->name) free (this->name); // let the compiler optimize
241 this->name = name; 247 this->name = name;
242} 248}
243 249
244void 250void
245rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color) 251rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color) const
246{ 252{
253 dTermDisplay;
254 dTermGC;
255
247 if (color == Color_bg) 256 if (color == Color_bg)
248 XClearArea (d.display->display, d, x, y, w, h, FALSE); 257 XClearArea (disp, d, x, y, w, h, FALSE);
249 else if (color >= 0) 258 else if (color >= 0)
250 { 259 {
251#if XFT 260#if XFT
252 XftDrawRect (d, &r->pix_colors[color].c, x, y, w, h); 261 XftDrawRect (d, &term->pix_colors[color].c, x, y, w, h);
253#else 262#else
254 XSetForeground (d.display->display, TGC, r->pix_colors[color]); 263 XSetForeground (disp, gc, term->pix_colors[color]);
255 XFillRectangle (d.display->display, d, TGC, x, y, w, h); 264 XFillRectangle (disp, d, gc, x, y, w, h);
256#endif 265#endif
257 } 266 }
258} 267}
259 268
260#include "table/linedraw.h" 269#include "table/linedraw.h"
271 rxvt_fontprop properties () 280 rxvt_fontprop properties ()
272 { 281 {
273 rxvt_fontprop p; 282 rxvt_fontprop p;
274 283
275 p.width = p.height = 1; 284 p.width = p.height = 1;
285 p.ascent = rxvt_fontprop::unset;
276 p.weight = rxvt_fontprop::medium; 286 p.weight = rxvt_fontprop::medium;
277 p.slant = rxvt_fontprop::roman; 287 p.slant = rxvt_fontprop::roman;
278 288
279 return p; 289 return p;
280 } 290 }
287 set_name (strdup ("built-in support font")); 297 set_name (strdup ("built-in support font"));
288 298
289 return true; 299 return true;
290 } 300 }
291 301
292 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) 302 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
293 { 303 {
294 careful = false; 304 careful = false;
295 305
296 if (unicode <= 0x001f) 306 if (unicode <= 0x001f)
297 return true; 307 return true;
300 return false; 310 return false;
301 311
302 if (unicode <= 0x009f) 312 if (unicode <= 0x009f)
303 return true; 313 return true;
304 314
305 if (unicode >= 0x2500 && unicode <= 0x259f) 315 if (unicode >= 0x2500 && unicode <= 0x259f &&
316 !term->option (Opt_skipBuiltinGlyphs))
306 return true; 317 return true;
307 318
308 if (IS_COMPOSE (unicode)) 319 if (IS_COMPOSE (unicode))
309 return true; 320 return true;
310 321
326void 337void
327rxvt_font_default::draw (rxvt_drawable &d, int x, int y, 338rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
328 const text_t *text, int len, 339 const text_t *text, int len,
329 int fg, int bg) 340 int fg, int bg)
330{ 341{
342 dTermDisplay;
343 dTermGC;
344
331 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 345 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
332 346
333 XSetForeground (d.display->display, TGC, r->pix_colors[fg]); 347 XSetForeground (disp, gc, term->pix_colors[fg]);
334 348
335 while (len--) 349 while (len)
336 { 350 {
337#if ENABLE_COMBINING 351#if ENABLE_COMBINING
338 compose_char *cc; 352 compose_char *cc;
339#endif 353#endif
354 const text_t *tp = text;
340 text_t t = *text++; 355 text_t t = *tp;
356
357 while (++text, --len && *text == NOCHAR)
358 ;
359
360 int width = text - tp;
361 int fwidth = term->fwidth * width;
341 362
342 if (0x2500 <= t && t <= 0x259f) 363 if (0x2500 <= t && t <= 0x259f)
343 { 364 {
344 uint16_t offs = linedraw_offs[t - 0x2500]; 365 uint16_t offs = linedraw_offs[t - 0x2500];
345 uint32_t *a = linedraw_command + (offs >> 4); 366 uint32_t *a = linedraw_command + (offs >> 4);
346 uint32_t *b = a + (offs & 15); 367 uint32_t *b = a + (offs & 15);
347 368
348 int W = r->TermWin.fwidth; 369 int W = fwidth;
349 int H = r->TermWin.fheight; 370 int H = term->fheight;
350 371
351 int x_[16]; 372 int x_[16];
352 int y_[16]; 373 int y_[16];
353 374
354 for (int i = 0; i <= 8; i++) 375 for (int i = 0; i <= 8; i++)
362 383
363 XGCValues gcv; 384 XGCValues gcv;
364 385
365 gcv.cap_style = CapButt; 386 gcv.cap_style = CapButt;
366 gcv.line_width = 0; 387 gcv.line_width = 0;
367 XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv); 388 XChangeGC (disp, gc, GCLineWidth | GCCapStyle, &gcv);
368 389
369 while (a < b) 390 while (a < b)
370 { 391 {
371 uint32_t command = *a++; 392 uint32_t command = *a++;
372 393
379 int y2 = y_[(command >> 0) & 15]; 400 int y2 = y_[(command >> 0) & 15];
380 401
381 switch (op) 402 switch (op)
382 { 403 {
383 case 0: // line 404 case 0: // line
384 XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2); 405 XDrawLine (disp, d, gc, x1, y1, x2, y2);
385 break; 406 break;
386 407
387 case 1: // rectangle, possibly stippled 408 case 1: // rectangle, possibly stippled
388 if (a) 409 if (a)
389 { 410 {
390 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 }; 411 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
391 412
392 gcv.fill_style = FillStippled; 413 gcv.fill_style = FillStippled;
393 gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2); 414 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
394 gcv.ts_x_origin = x; 415 gcv.ts_x_origin = x;
395 gcv.ts_y_origin = y; 416 gcv.ts_y_origin = y;
396 417
397 XChangeGC (d.display->display, TGC, 418 XChangeGC (disp, gc,
398 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, 419 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
399 &gcv); 420 &gcv);
400 } 421 }
401 422
402 XFillRectangle (d.display->display, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1); 423 XFillRectangle (disp, d, gc, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
403 424
404 if (a) 425 if (a)
405 { 426 {
406 XFreePixmap (d.display->display, gcv.stipple); 427 XFreePixmap (disp, gcv.stipple);
407 gcv.stipple = 0; 428 gcv.stipple = 0;
408 gcv.fill_style = FillSolid; 429 gcv.fill_style = FillSolid;
409 XChangeGC (d.display->display, TGC, GCFillStyle, &gcv); 430 XChangeGC (disp, gc, GCFillStyle, &gcv);
410 } 431 }
411 break; 432 break;
412 case 2: // arc 433 case 2: // arc
413 XDrawArc (d.display->display, d, TGC, 434 XDrawArc (disp, d, gc,
414 x1 - W/2, y1 - H/2, W-1, H-1, 435 x1 - W/2, y1 - H/2, W-1, H-1,
415 (a - 1) * 90*64, (b - 1) * 90*64); 436 (a - 1) * 90*64, (b - 1) * 90*64);
416 break; 437 break;
417 } 438 }
418 } 439 }
419 } 440 }
420#if ENABLE_COMBINING 441#if ENABLE_COMBINING
421 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t])) 442 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
422 { 443 {
423 text_t c2[2]; 444 text_t chrs[2];
424 int len2 = 1; 445 width = min (2, width);
425 446 chrs [1] = NOCHAR;
426 // support double-width (but not more) combining characters
427 if (len && *text == NOCHAR)
428 {
429 c2[1] = *text; // not text++ to get correct x-width
430 len2++;
431 }
432 447
433 c2[0] = cc->c1; 448 *chrs = cc->c1;
434 rxvt_font *f1 = (*fs)[fs->find_font (c2[0])]; 449 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
435 f1->draw (d, x, y, c2, len2, fg, bg); 450 f1->draw (d, x, y, chrs, width, fg, bg);
436 451
437 if (cc->c2 != NOCHAR) 452 if (cc->c2 != NOCHAR)
438 { 453 {
439 bool careful; 454 bool careful;
440 455
441 // prefer font of first character, for no good reasons 456 // prefer font of first character, for no good reasons
442 c2[0] = cc->c2; 457 *chrs = cc->c2;
443 rxvt_font *f2 = (f1->has_char (c2[0], 0, careful) && !careful) 458 rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful)
444 ? f1 459 ? f1
445 : (*fs)[fs->find_font (c2[0])]; 460 : (*fs)[fs->find_font (cc->c2)];
446 461
447 f2->draw (d, x, y, c2, len2, fg, -1); 462 f2->draw (d, x, y, chrs, width, fg, -1);
448 } 463 }
449 } 464 }
450#endif 465#endif
451 else 466 else
452 switch (t) 467 switch (t)
455 case ZERO_WIDTH_CHAR: 470 case ZERO_WIDTH_CHAR:
456 case NOCHAR: 471 case NOCHAR:
457 break; 472 break;
458 473
459 default: 474 default:
460 int w = 0;
461 while (len > 0 && *text == NOCHAR)
462 {
463 ++text;
464 --len;
465 w += r->TermWin.fwidth;
466 }
467
468 XDrawRectangle (d.display->display, d, TGC, x + 2, y + 2, 475 XDrawRectangle (disp, d, gc, x + 2, y + 2,
469 w + r->TermWin.fwidth - 4, r->TermWin.fheight - 4); 476 fwidth - 4, term->fheight - 4);
470 x += w;
471 } 477 }
472 478
473 x += r->TermWin.fwidth; 479 x += fwidth;
474 } 480 }
475} 481}
476 482
477///////////////////////////////////////////////////////////////////////////// 483/////////////////////////////////////////////////////////////////////////////
478 484
483 489
484 rxvt_fontprop properties (); 490 rxvt_fontprop properties ();
485 491
486 bool load (const rxvt_fontprop &prop); 492 bool load (const rxvt_fontprop &prop);
487 493
488 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful); 494 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const;
489 495
490 void draw (rxvt_drawable &d, int x, int y, 496 void draw (rxvt_drawable &d, int x, int y,
491 const text_t *text, int len, 497 const text_t *text, int len,
492 int fg, int bg); 498 int fg, int bg);
493 499
505char * 511char *
506rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const 512rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
507{ 513{
508 unsigned long value; 514 unsigned long value;
509 515
510 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value)) 516 if (XGetFontProperty (f, XInternAtom (term->display->display, property, 0), &value))
511 return XGetAtomName (DISPLAY, value); 517 return XGetAtomName (term->display->display, value);
512 else 518 else
513 return rxvt_strdup (repl); 519 return rxvt_strdup (repl);
514} 520}
515 521
516rxvt_fontprop 522rxvt_fontprop
524bool 530bool
525rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth) 531rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
526{ 532{
527 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2; 533 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
528 p.height = height; 534 p.height = height;
535 p.ascent = rxvt_fontprop::unset;
529 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium; 536 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
530 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic; 537 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
531 538
532 return true; 539 return true;
533} 540}
534 541
535bool 542bool
536rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) 543rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
537{ 544{
538 unsigned long height; 545 unsigned long height;
546
547#if 0
539 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) 548 if (!XGetFontProperty (f, XInternAtom (term->display->display, "PIXEL_SIZE", 0), &height))
540 return false; 549 return false;
550#else
551 height = f->ascent + f->descent;
552#endif
541 553
542 unsigned long avgwidth; 554 unsigned long avgwidth;
543 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) 555 if (!XGetFontProperty (f, XInternAtom (term->display->display, "AVERAGE_WIDTH", 0), &avgwidth))
544 avgwidth = 0; 556 avgwidth = 0;
545 557
546 char *weight = get_property (f, "WEIGHT_NAME", "medium"); 558 char *weight = get_property (f, "WEIGHT_NAME", "medium");
547 char *slant = get_property (f, "SLANT", "r"); 559 char *slant = get_property (f, "SLANT", "r");
548 560
549 set_properties (p, height, weight, slant, avgwidth); 561 set_properties (p, height, weight, slant, avgwidth);
550 562
551 free (weight); 563 free (weight);
552 free (slant); 564 free (slant);
553 565
566 p.ascent = f->ascent;
567
554 return true; 568 return true;
555} 569}
556 570
557bool 571bool
558rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name) 572rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
559{ 573{
574 dTermDisplay;
560 int slashes = 0; 575 int slashes = 0;
561 const char *comp[13]; 576 const char *comp[13];
562 577
563 for (const char *c = name; *c; c++) 578 for (const char *c = name; *c; c++)
564 if (*c == '-') 579 if (*c == '-')
572 if (slashes >= 13 587 if (slashes >= 13
573 && (*comp[ 6] >= '1' && *comp[ 6] <= '9') 588 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
574 && (*comp[11] >= '0' && *comp[11] <= '9')) 589 && (*comp[11] >= '0' && *comp[11] <= '9'))
575 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11])); 590 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
576 591
577 XFontStruct *f = XLoadQueryFont (DISPLAY, name); 592 XFontStruct *f = XLoadQueryFont (disp, name);
578 593
579 if (f) 594 if (f)
580 { 595 {
581 // the font should really exist now. if not, we have a problem 596 // the font should really exist now. if not, we have a problem
582 // (e.g. if the user did xset fp rehash just when we were searching fonts). 597 // (e.g. if the user did xset fp rehash just when we were searching fonts).
583 // in that case, just return garbage. 598 // in that case, just return garbage.
584 bool ret = set_properties (p, f); 599 bool ret = set_properties (p, f);
585 XFreeFont (DISPLAY, f); 600 XFreeFont (disp, f);
586 return ret; 601 return ret;
587 } 602 }
588 else 603 else
589 return false; 604 return false;
590} 605}
628} 643}
629 644
630bool 645bool
631rxvt_font_x11::load (const rxvt_fontprop &prop) 646rxvt_font_x11::load (const rxvt_fontprop &prop)
632{ 647{
648 dTermDisplay;
649
633 clear (); 650 clear ();
634 651
635 char field_str[64]; // enough for 128 bits 652 char field_str[64]; // enough for 128 bits
636 653
637 // first morph the font if required 654 // first morph the font if required
640 { 657 {
641 char fname[1024]; 658 char fname[1024];
642 659
643 if (name[0] != '-') 660 if (name[0] != '-')
644 { 661 {
645 f = XLoadQueryFont (DISPLAY, name); 662 f = XLoadQueryFont (disp, name);
646 663
647 if (!f) 664 if (!f)
648 return false; 665 return false;
649 666
650 char *new_name = get_property (f, "FONT", name); 667 char *new_name = get_property (f, "FONT", name);
652 if (new_name) 669 if (new_name)
653 set_name (new_name); 670 set_name (new_name);
654 else 671 else
655 rxvt_warn ("font '%s' has no FONT property, continuing without.", name); 672 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
656 673
657 XFreeFont (DISPLAY, f); 674 XFreeFont (disp, f);
658 f = 0; 675 f = 0;
659 } 676 }
660 677
661 if (prop.weight != rxvt_fontprop::unset) 678 if (prop.weight != rxvt_fontprop::unset)
662 { 679 {
673 ? "r" : "i"); // TODO: handle "o"blique, too 690 ? "r" : "i"); // TODO: handle "o"blique, too
674 set_name (strdup (fname)); 691 set_name (strdup (fname));
675 } 692 }
676 } 693 }
677 694
695 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
696 ? 0 : prop.height);
697
698 struct font_weight {
699 char *name;
700 int diff;
701
702 void clear ()
703 {
704 name = 0;
705 diff = 0x7fffffff;
706 }
707
708 font_weight () { clear (); }
709 ~font_weight () { free (name); }
710 };
711
678 char **list; 712 char **list;
679 int count; 713 int count;
680 list = XListFonts (DISPLAY, name, 1024, &count); 714 list = XListFonts (disp, name, 4000, &count);
681 715
682 set_name (0); 716 set_name (0);
683 717
684 if (!list) 718 if (!list)
685 return false; 719 return false;
686 720
687 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset 721 font_weight *fonts = new font_weight[count];
688 ? 0 : prop.height);
689 722
690 int bestdiff = 0x7fffffff;
691 for (int i = 0; i < count; i++) 723 for (int i = 0; i < count; i++)
692 { 724 {
693 rxvt_fontprop p; 725 rxvt_fontprop p;
694 char fname[1024]; 726 char fname[1024];
695 727
696 int diff = 0; 728 int diff = 0;
697 729
698 if (replace_field (fname, list[i], 6, '0', field_str)) 730 if (replace_field (fname, list[i], 6, '0', field_str))
699 diff += 10; // slightly penalize scalable fonts 731 diff += 10; // slightly penalize scalable fonts
732 else if (replace_field (fname, list[i], 11, '0', "0"))
733 diff += 300; // more heavily penalize what looks like scaled bitmap fonts
700 734
701 if (!set_properties (p, fname)) 735 if (!set_properties (p, fname))
702 continue; 736 continue;
703 737
704 if (prop.height != rxvt_fontprop::unset 738 if (prop.height != rxvt_fontprop::unset
708 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128; 742 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128;
709 if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight); 743 if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight);
710 if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant); 744 if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant);
711 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width); 745 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
712 746
713 if (!name // compare against best found so far 747 fonts[i].name = strdup (fname);
714 || diff < bestdiff) 748 fonts[i].diff = diff;
715 {
716 set_name (strdup (fname));
717 bestdiff = diff;
718 }
719 } 749 }
720 750
721 XFreeFontNames (list); 751 XFreeFontNames (list);
722 752
723 if (!name) 753 // this loop only iterates when the guessed font-size is too small
724 return false; 754 for (;;)
755 {
756 font_weight *best = fonts + count - 1;
725 757
726 f = XLoadQueryFont (DISPLAY, name); 758 for (font_weight *w = best; w-- > fonts; )
759 if (w->diff <= best->diff)
760 best = w;
761
762 if (!best->name
763 || !(f = XLoadQueryFont (disp, best->name)))
764 break;
765
766 set_name (best->name);
767 best->clear ();
768
769 ascent = f->ascent;
770 descent = f->descent;
771 height = ascent + descent;
772
773 if (prop.height == rxvt_fontprop::unset
774 || height <= prop.height)
775 break; // font is ready for use
776
777 // PIXEL_SIZE small enough, but real height too large
778 clear ();
779 }
780
781 delete [] fonts;
727 782
728 if (!f) 783 if (!f)
729 return false; 784 return false;
730 785
731 char *registry = get_property (f, "CHARSET_REGISTRY", 0); 786 char *registry = get_property (f, "CHARSET_REGISTRY", 0);
759 if (cs == CS_UNICODE) 814 if (cs == CS_UNICODE)
760 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font 815 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
761 816
762 encm = f->min_byte1 != 0 || f->max_byte1 != 0; 817 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
763 enc2b = encm || f->max_char_or_byte2 > 255; 818 enc2b = encm || f->max_char_or_byte2 > 255;
764
765 ascent = f->ascent;
766 descent = f->descent;
767 height = ascent + descent;
768 819
769 slow = false; 820 slow = false;
770 821
771#if 1 // only used for slow detection, TODO optimize 822#if 1 // only used for slow detection, TODO optimize
772 if (f->min_bounds.width == f->max_bounds.width) 823 if (f->min_bounds.width == f->max_bounds.width)
811 862
812 XCharStruct g; 863 XCharStruct g;
813 int dir_ret, asc_ret, des_ret; 864 int dir_ret, asc_ret, des_ret;
814 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g); 865 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
815 866
816 int wcw = wcwidth (*t); if (wcw > 0) g.width = g.width / wcw; 867 int wcw = wcwidth (*t); if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
817 868
818 if (width < g.width) width = g.width; 869 if (width < g.width) width = g.width;
819 } 870 }
820 871
821 if (cs == CS_UNKNOWN) 872 if (cs == CS_UNKNOWN)
840void 891void
841rxvt_font_x11::clear () 892rxvt_font_x11::clear ()
842{ 893{
843 if (f) 894 if (f)
844 { 895 {
845 XFreeFont (DISPLAY, f); 896 XFreeFont (term->display->display, f);
846 f = 0; 897 f = 0;
847 } 898 }
848} 899}
849 900
850bool 901bool
851rxvt_font_x11::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) 902rxvt_font_x11::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
852{ 903{
853 uint32_t ch = FROM_UNICODE (cs, unicode); 904 uint32_t ch = FROM_UNICODE (cs, unicode);
854 905
855 if (ch == NOCHAR) 906 if (ch == NOCHAR)
856 return false; 907 return false;
896 return true; 947 return true;
897 948
898 // check character against base font bounding box 949 // check character against base font bounding box
899 int w = xcs->width; 950 int w = xcs->width;
900 int wcw = wcwidth (unicode); 951 int wcw = wcwidth (unicode);
901 if (wcw > 0) w /= wcw; 952 if (wcw > 0) w = (w + wcw - 1) / wcw;
902 953
903 careful = w > prop->width; 954 careful = w > prop->width;
904 if (careful && w > prop->width * MAX_OVERLAP >> 2) 955 if (careful && w > prop->width * MAX_OVERLAP >> 2)
905 return false; 956 return false;
906 957
915 // this looks like a mess /. 966 // this looks like a mess /.
916 // and it is a mess /. 967 // and it is a mess /.
917 // yet we are trying to be perfect /. 968 // yet we are trying to be perfect /.
918 // but the result still isn't perfect /. 969 // but the result still isn't perfect /.
919 970
971 dTermDisplay;
972 dTermGC;
973
920 bool slow = this->slow 974 bool slow = this->slow
921 || width != r->TermWin.fwidth 975 || width != term->fwidth
922 || height != r->TermWin.fheight; 976 || height != term->fheight;
923 977
924 int base = ascent; // sorry, incorrect: r->TermWin.fbase; 978 int base = ascent; // sorry, incorrect: term->fbase;
925 979
926 XGCValues v; 980 XGCValues v;
927 v.foreground = r->pix_colors[fg]; 981 v.foreground = term->pix_colors[fg];
928 v.font = f->fid; 982 v.font = f->fid;
929 983
930 if (enc2b) 984 if (enc2b)
931 { 985 {
932 const XChar2b *xc = enc_xchar2b (text, len, cs, slow); 986 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
933 987
934 if (bg == Color_bg && !slow) 988 if (bg == Color_bg && !slow)
935 { 989 {
936 v.background = r->pix_colors[bg]; 990 v.background = term->pix_colors[bg];
937 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 991 XChangeGC (disp, gc, GCForeground | GCBackground | GCFont, &v);
938 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len); 992 XDrawImageString16 (disp, d, gc, x, y + base, xc, len);
939 } 993 }
940 else 994 else
941 { 995 {
942 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 996 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
943 997
944 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 998 XChangeGC (disp, gc, GCForeground | GCFont, &v);
945 999
946 if (slow) 1000 if (slow)
947 { 1001 {
948 do 1002 do
949 { 1003 {
950 if (xc->byte1 || xc->byte2) 1004 if (xc->byte1 || xc->byte2)
951 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1); 1005 XDrawString16 (disp, d, gc, x, y + base, xc, 1);
952 1006
953 x += r->TermWin.fwidth; 1007 x += term->fwidth;
954 xc++; len--; 1008 xc++; len--;
955 } 1009 }
956 while (len); 1010 while (len);
957 } 1011 }
958 else 1012 else
959 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, len); 1013 XDrawString16 (disp, d, gc, x, y + base, xc, len);
960 } 1014 }
961 } 1015 }
962 else 1016 else
963 { 1017 {
964 const char *xc = enc_char (text, len, cs, slow); 1018 const char *xc = enc_char (text, len, cs, slow);
965 1019
966 if (bg == Color_bg && !slow) 1020 if (bg == Color_bg && !slow)
967 { 1021 {
968 v.background = r->pix_colors[bg]; 1022 v.background = term->pix_colors[bg];
969 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 1023 XChangeGC (disp, gc, GCForeground | GCBackground | GCFont, &v);
970 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len); 1024 XDrawImageString (disp, d, gc, x, y + base, xc, len);
971 } 1025 }
972 else 1026 else
973 { 1027 {
974 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1028 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
975 1029
976 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 1030 XChangeGC (disp, gc, GCForeground | GCFont, &v);
977 1031
978 if (slow) 1032 if (slow)
979 { 1033 {
980 do 1034 do
981 { 1035 {
982 if (*xc) 1036 if (*xc)
983 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1); 1037 XDrawString (disp, d, gc, x, y + base, xc, 1);
984 1038
985 x += r->TermWin.fwidth; 1039 x += term->fwidth;
986 xc++; len--; 1040 xc++; len--;
987 } 1041 }
988 while (len); 1042 while (len);
989 } 1043 }
990 else 1044 else
991 XDrawString (d.display->display, d, TGC, x, y + base, xc, len); 1045 XDrawString (disp, d, gc, x, y + base, xc, len);
992 } 1046 }
993 } 1047 }
994} 1048}
995 1049
996///////////////////////////////////////////////////////////////////////////// 1050/////////////////////////////////////////////////////////////////////////////
1008 1062
1009 void draw (rxvt_drawable &d, int x, int y, 1063 void draw (rxvt_drawable &d, int x, int y,
1010 const text_t *text, int len, 1064 const text_t *text, int len,
1011 int fg, int bg); 1065 int fg, int bg);
1012 1066
1013 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &carefull); 1067 bool has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &carefull) const;
1014 1068
1015protected: 1069protected:
1016 XftFont *f; 1070 XftFont *f;
1017}; 1071};
1018 1072
1019void 1073void
1020rxvt_font_xft::clear () 1074rxvt_font_xft::clear ()
1021{ 1075{
1022 if (f) 1076 if (f)
1023 { 1077 {
1024 XftFontClose (DISPLAY, f); 1078 XftFontClose (term->display->display, f);
1025 f = 0; 1079 f = 0;
1026 } 1080 }
1027} 1081}
1028 1082
1029rxvt_fontprop 1083rxvt_fontprop
1033 1087
1034 FT_Face face = XftLockFace (f); 1088 FT_Face face = XftLockFace (f);
1035 1089
1036 p.width = width; 1090 p.width = width;
1037 p.height = height; 1091 p.height = height;
1092 p.ascent = ascent;
1038 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD 1093 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
1039 ? rxvt_fontprop::bold : rxvt_fontprop::medium; 1094 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
1040 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC 1095 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
1041 ? rxvt_fontprop::italic : rxvt_fontprop::roman; 1096 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
1042 1097
1046} 1101}
1047 1102
1048bool 1103bool
1049rxvt_font_xft::load (const rxvt_fontprop &prop) 1104rxvt_font_xft::load (const rxvt_fontprop &prop)
1050{ 1105{
1106 dTermDisplay;
1107
1051 clear (); 1108 clear ();
1052 1109
1053 FcPattern *p = FcNameParse ((FcChar8 *) name); 1110 FcPattern *p = FcNameParse ((FcChar8 *) name);
1054 1111
1055 if (!p) 1112 if (!p)
1056 return false; 1113 return false;
1057 1114
1058 FcValue v; 1115 FcValue v;
1059 1116
1060 if (prop.height != rxvt_fontprop::unset 1117 if (prop.height != rxvt_fontprop::unset
1061 && FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch) 1118 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1119 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
1062 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); 1120 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1063 1121
1064 if (prop.weight != rxvt_fontprop::unset 1122 if (prop.weight != rxvt_fontprop::unset
1065 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) 1123 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
1066 FcPatternAddInteger (p, FC_WEIGHT, prop.weight); 1124 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1080 1138
1081 // store generated name so iso14755 view gives better results 1139 // store generated name so iso14755 view gives better results
1082 set_name ((char *)FcNameUnparse (p)); 1140 set_name ((char *)FcNameUnparse (p));
1083 1141
1084 XftResult result; 1142 XftResult result;
1085 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1143 FcPattern *match = XftFontMatch (disp, term->display->screen, p, &result);
1086 1144
1087 FcPatternDestroy (p); 1145 FcPatternDestroy (p);
1088 1146
1089 if (!match) 1147 if (!match)
1090 return false; 1148 return false;
1092 int ftheight = 0; 1150 int ftheight = 0;
1093 bool success = true; 1151 bool success = true;
1094 1152
1095 for (;;) 1153 for (;;)
1096 { 1154 {
1097 f = XftFontOpenPattern (DISPLAY, FcPatternDuplicate (match)); 1155 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
1098 1156
1099 if (!f) 1157 if (!f)
1100 { 1158 {
1101 success = false; 1159 success = false;
1102 break; 1160 break;
1111 1169
1112 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE; 1170 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1113 1171
1114 XftUnlockFace (f); 1172 XftUnlockFace (f);
1115 1173
1174 int glheight = height;
1175
1116 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; ) 1176 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1117 { 1177 {
1118 FcChar16 ch = *t; 1178 FcChar16 ch = *t;
1119 1179
1120 if (cs != CS_UNICODE 1180 if (cs != CS_UNICODE
1126 bool careful; 1186 bool careful;
1127 if (!has_char (*t, &prop, careful)) 1187 if (!has_char (*t, &prop, careful))
1128 continue; 1188 continue;
1129 1189
1130 XGlyphInfo g; 1190 XGlyphInfo g;
1131 XftTextExtents16 (DISPLAY, f, &ch, 1, &g); 1191 XftTextExtents16 (disp, f, &ch, 1, &g);
1192
1193 g.width -= g.x;
1132 1194
1133 int wcw = wcwidth (ch); 1195 int wcw = wcwidth (ch);
1134 if (wcw > 0) g.width = g.width / wcw; 1196 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1135 1197
1136 if (width < g.width) width = g.width; 1198 if (width < g.width ) width = g.width;
1137 if (height < g.height) height = g.height; 1199 if (height < g.height ) height = g.height;
1200 if (glheight < g.height - g.y) glheight = g.height - g.y;
1201 }
1202
1203 if (!width)
1204 {
1205 rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name);
1206
1207 XftFontClose (disp, f);
1208 f = 0;
1209
1210 success = false;
1211 break;
1138 } 1212 }
1139 1213
1140 if (prop.height == rxvt_fontprop::unset 1214 if (prop.height == rxvt_fontprop::unset
1141 || height <= prop.height 1215 || (height <= prop.height && glheight <= prop.height)
1142 || height <= 2 1216 || height <= 2
1143 || !scalable) 1217 || !scalable)
1144 break; 1218 break;
1145 1219
1146 if (ftheight) 1220 if (ftheight)
1153 ftheight -= height - prop.height; 1227 ftheight -= height - prop.height;
1154 } 1228 }
1155 else 1229 else
1156 ftheight = prop.height - 1; 1230 ftheight = prop.height - 1;
1157 1231
1158 XftFontClose (DISPLAY, f); 1232 XftFontClose (disp, f);
1159 FcPatternDel (match, FC_PIXEL_SIZE); 1233 FcPatternDel (match, FC_PIXEL_SIZE);
1160 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); 1234 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1161 } 1235 }
1162 1236
1163 FcPatternDestroy (match); 1237 FcPatternDestroy (match);
1164 1238
1165#if 0 // do it per-character 1239#if 0 // do it per-character
1172 1246
1173 return success; 1247 return success;
1174} 1248}
1175 1249
1176bool 1250bool
1177rxvt_font_xft::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) 1251rxvt_font_xft::has_char (unicode_t unicode, const rxvt_fontprop *prop, bool &careful) const
1178{ 1252{
1179 careful = false; 1253 careful = false;
1180 1254
1181 if (!XftCharExists (DISPLAY, f, unicode)) 1255 if (!XftCharExists (term->display->display, f, unicode))
1182 return false; 1256 return false;
1183 1257
1184 if (!prop || prop->width == rxvt_fontprop::unset) 1258 if (!prop || prop->width == rxvt_fontprop::unset)
1185 return true; 1259 return true;
1186 1260
1187 // check character against base font bounding box 1261 // check character against base font bounding box
1188 FcChar32 ch = unicode; 1262 FcChar32 ch = unicode;
1189 XGlyphInfo g; 1263 XGlyphInfo g;
1190 XftTextExtents32 (DISPLAY, f, &ch, 1, &g); 1264 XftTextExtents32 (term->display->display, f, &ch, 1, &g);
1191 1265
1192 int w = g.width; 1266 int w = g.width - g.x;
1193 int wcw = wcwidth (unicode); 1267 int wcw = wcwidth (unicode);
1194 if (wcw > 0) w /= wcw; 1268 if (wcw > 0) w = (w + wcw - 1) / wcw;
1195 1269
1196 careful = w > prop->width; 1270 careful = w > prop->width;
1197 if (careful && w > prop->width * MAX_OVERLAP >> 2) 1271 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1198 return false; 1272 return false;
1199 1273
1203void 1277void
1204rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1278rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1205 const text_t *text, int len, 1279 const text_t *text, int len,
1206 int fg, int bg) 1280 int fg, int bg)
1207{ 1281{
1208 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1282 clear_rect (d, x, y, term->fwidth * len, term->fheight, bg);
1209
1210 int base = ascent; // should be fbase, but that is incorrect
1211 1283
1212 XGlyphInfo extents; 1284 XGlyphInfo extents;
1213 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32)); 1285 XftGlyphSpec *enc = (XftGlyphSpec *)get_enc_buf (len * sizeof (XftGlyphSpec));
1214 FcChar32 *ep = enc; 1286 XftGlyphSpec *ep = enc;
1215 int ewidth = 0; 1287
1216 int xoff = 0; 1288 dTermDisplay;
1289 dTermGC;
1290
1291 // cut trailing spaces
1292 while (len && text [len - 1] == ' ')
1293 len--;
1217 1294
1218 while (len) 1295 while (len)
1219 { 1296 {
1220 int cwidth = r->TermWin.fwidth; 1297 int cwidth = term->fwidth;
1221 FcChar32 fc = *text++; len--; 1298 FcChar32 fc = *text++; len--;
1222 FT_UInt gl;
1223 1299
1224 while (len && *text == NOCHAR) 1300 while (len && *text == NOCHAR)
1225 text++, len--, cwidth += r->TermWin.fwidth; 1301 text++, len--, cwidth += term->fwidth;
1226 1302
1227 gl = XftCharIndex (d.display->display, f, fc); 1303 if (fc != ' ') // skip spaces
1228 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1229
1230 if (extents.xOff != cwidth && ep != enc)
1231 {
1232 if (xoff > ewidth) xoff = ewidth;
1233 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1234 x + (ewidth - xoff >> 1),
1235 y + base, enc, ep - enc);
1236 x += ewidth;
1237
1238 ep = enc;
1239 ewidth = 0;
1240 xoff = 0;
1241 } 1304 {
1305 FT_UInt glyph = XftCharIndex (disp, f, fc);
1306 XftGlyphExtents (disp, f, &glyph, 1, &extents);
1242 1307
1243 if (fc == ' ' && ep == enc) // skip leading spaces 1308 ep->glyph = glyph;
1309 ep->x = x + (cwidth - extents.xOff >> 1);
1310 ep->y = y + ascent;
1311 ep++;
1244 { 1312 }
1313
1245 x += cwidth; 1314 x += cwidth;
1246 continue;
1247 }
1248
1249 else
1250 {
1251 *ep++ = gl;
1252 ewidth += cwidth;
1253 xoff += extents.xOff;
1254 }
1255 } 1315 }
1256 1316
1257 if (ep != enc) 1317 if (ep != enc)
1258 {
1259 if (xoff > ewidth) xoff = ewidth;
1260 XftDrawGlyphs (d, &r->pix_colors[fg].c, f, 1318 XftDrawGlyphSpec (d, &term->pix_colors[fg].c, f, enc, ep - enc);
1261 x + (ewidth - xoff >> 1),
1262 y + base, enc, ep - enc);
1263 }
1264} 1319}
1265#endif 1320#endif
1266 1321
1267///////////////////////////////////////////////////////////////////////////// 1322/////////////////////////////////////////////////////////////////////////////
1268 1323
1269rxvt_fontset::rxvt_fontset (rxvt_t r) 1324rxvt_fontset::rxvt_fontset (rxvt_term *term)
1270: r (r), fontdesc (0) 1325: fontdesc (0), term (term)
1271{ 1326{
1272 clear (); 1327 clear ();
1273} 1328}
1274 1329
1275rxvt_fontset::~rxvt_fontset () 1330rxvt_fontset::~rxvt_fontset ()
1278} 1333}
1279 1334
1280void 1335void
1281rxvt_fontset::clear () 1336rxvt_fontset::clear ()
1282{ 1337{
1283 prop.width = prop.height = prop.weight = prop.slant 1338 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1284 = rxvt_fontprop::unset; 1339 = rxvt_fontprop::unset;
1285 1340
1286 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) 1341 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1287 FONT_UNREF (*i); 1342 FONT_UNREF (*i);
1288 1343
1319 f = new rxvt_font_x11; 1374 f = new rxvt_font_x11;
1320 } 1375 }
1321 else 1376 else
1322 f = new rxvt_font_x11; 1377 f = new rxvt_font_x11;
1323 1378
1324 f->set_term (r); 1379 f->set_term (term);
1325 f->set_name (strdup (name)); 1380 f->set_name (strdup (name));
1326 1381
1327 f->cs = cs; 1382 f->cs = cs;
1328 f->loaded = false; 1383 f->loaded = false;
1329 1384
1340 char buf[512]; 1395 char buf[512];
1341 const char *end; 1396 const char *end;
1342 1397
1343 do 1398 do
1344 { 1399 {
1345 while (*desc <= ' ') desc++; 1400 while (*desc && *desc <= ' ')
1401 desc++;
1346 1402
1347 codeset cs = CS_UNICODE; 1403 codeset cs = CS_UNICODE;
1348 1404
1349 if (*desc == '[') 1405 if (*desc == '[')
1350 { 1406 {
1460 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1516 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1461 goto next_font; 1517 goto next_font;
1462 1518
1463 if (!realize_font (i)) 1519 if (!realize_font (i))
1464 goto next_font; 1520 goto next_font;
1521
1522 if (prop.ascent != rxvt_fontprop::unset)
1523 max_it (f->ascent, prop.ascent);
1465 } 1524 }
1466 1525
1467 if (f->cs == CS_UNKNOWN) 1526 if (f->cs == CS_UNKNOWN)
1468 goto next_font; 1527 goto next_font;
1469 1528
1508 FcPatternAddInteger (p, FC_SLANT, prop.slant); 1567 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1509 FcPatternAddBool (p, FC_MINSPACE, 1); 1568 FcPatternAddBool (p, FC_MINSPACE, 1);
1510 //FcPatternAddBool (p, FC_ANTIALIAS, 1); 1569 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1511 1570
1512 XftResult result; 1571 XftResult result;
1513 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1572 FcPattern *match = XftFontMatch (term->display->display, term->display->screen, p, &result);
1514 1573
1515 FcPatternDestroy (p); 1574 FcPatternDestroy (p);
1516 1575
1517 if (match) 1576 if (match)
1518 { 1577 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines