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.50 by root, Wed Dec 1 02:29:14 2004 UTC vs.
Revision 1.80 by root, Mon Jan 9 07:17:24 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
31#define DISPLAY r->display->display 32#define DISPLAY r->display->display
32#define TGC r->TermWin.gc 33#define TGC r->gc
33 34
34#define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width 35#define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width
35 36
36const struct rxvt_fallback_font { 37const struct rxvt_fallback_font {
37 codeset cs; 38 codeset cs;
90 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, 91 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
91#endif 92#endif
92 93
93#if ENCODING_ZH || ENCODING_ZH_EXT 94#if ENCODING_ZH || ENCODING_ZH_EXT
94# if XFT 95# if XFT
96 { CS_GBK_0, "xft:AR PL KaitiM GB" },
97 { CS_GBK_0, "xft:AR PL SungtiL GB" },
98 { CS_GBK_0, "xft::spacing=100:lang=zh" },
95 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, 99 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
96 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, 100 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
97 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, 101 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
98 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, 102 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
99 { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" }, 103 { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" },
100# endif 104# endif
105 { CS_GBK_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gbk*-0" },
101 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" }, 106 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
102 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" }, 107 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
103 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" }, 108 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
109 { CS_GB2312_1980_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
104 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" }, 110 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
105 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" }, 111 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
106 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" }, 112 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
107 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" }, 113 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
108 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" }, 114 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
125 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" }, 131 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
126 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, 132 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
127 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" }, 133 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
128 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 134 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
129#if XFT 135#if XFT
130 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true"}, 136 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true" },
131 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" }, 137 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" },
132 { CS_UNICODE, "xft:Andale Mono:antialias=false" }, 138 { CS_UNICODE, "xft:Andale Mono:antialias=false:autohint=false" },
133 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false" }, 139 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" },
134 140
135 // FreeMono is usually uglier than x fonts, so try last only. 141 // FreeMono is usually uglier than x fonts, so try last only.
136 { CS_UNICODE, "xft:FreeMono:autohint=true" }, 142 { CS_UNICODE, "xft:FreeMono:autohint=true" },
137#endif 143#endif
138 144
139 { CS_UNKNOWN, 0 } 145 { CS_UNKNOWN, 0 }
140}; 146};
141 147
142// these characters are used to guess the font height and width 148// these characters are used to guess the font height and width
143// pango uses a similar algorithm and eosn't trust the font either. 149// pango uses a similar algorithm and doesn't trust the font either.
144static uint16_t extent_test_chars[] = { 150static uint16_t extent_test_chars[] = {
145 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_', 151 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_',
146 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ 152 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ
147 0x304c, 0x672c, // が本 153 0x304c, 0x672c, // が本
148}; 154};
271 rxvt_fontprop properties () 277 rxvt_fontprop properties ()
272 { 278 {
273 rxvt_fontprop p; 279 rxvt_fontprop p;
274 280
275 p.width = p.height = 1; 281 p.width = p.height = 1;
282 p.ascent = rxvt_fontprop::unset;
276 p.weight = rxvt_fontprop::medium; 283 p.weight = rxvt_fontprop::medium;
277 p.slant = rxvt_fontprop::roman; 284 p.slant = rxvt_fontprop::roman;
278 285
279 return p; 286 return p;
280 } 287 }
300 return false; 307 return false;
301 308
302 if (unicode <= 0x009f) 309 if (unicode <= 0x009f)
303 return true; 310 return true;
304 311
305 if (unicode >= 0x2500 && unicode <= 0x259f) 312 if (unicode >= 0x2500 && unicode <= 0x259f &&
313 !r->option (Opt_skipBuiltinGlyphs))
306 return true; 314 return true;
307 315
308 if (IS_COMPOSE (unicode)) 316 if (IS_COMPOSE (unicode))
309 return true; 317 return true;
310 318
326void 334void
327rxvt_font_default::draw (rxvt_drawable &d, int x, int y, 335rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
328 const text_t *text, int len, 336 const text_t *text, int len,
329 int fg, int bg) 337 int fg, int bg)
330{ 338{
339 Display *disp = d.display->display;
340
331 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 341 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
332 342
333 XSetForeground (d.display->display, TGC, r->pix_colors[fg]); 343 XSetForeground (disp, TGC, r->pix_colors[fg]);
334 344
335 while (len--) 345 while (len)
336 { 346 {
337#if ENABLE_COMBINING 347#if ENABLE_COMBINING
338 compose_char *cc; 348 compose_char *cc;
339#endif 349#endif
350 const text_t *tp = text;
340 text_t t = *text++; 351 text_t t = *tp;
352
353 while (++text, --len && *text == NOCHAR)
354 ;
355
356 int width = text - tp;
357 int fwidth = r->fwidth * width;
341 358
342 if (0x2500 <= t && t <= 0x259f) 359 if (0x2500 <= t && t <= 0x259f)
343 { 360 {
344 uint16_t offs = linedraw_offs[t - 0x2500]; 361 uint16_t offs = linedraw_offs[t - 0x2500];
345 uint32_t *a = linedraw_command + (offs >> 4); 362 uint32_t *a = linedraw_command + (offs >> 4);
346 uint32_t *b = a + (offs & 15); 363 uint32_t *b = a + (offs & 15);
347 364
348 int W = r->TermWin.fwidth * wcwidth (t); 365 int W = fwidth;
349 int H = r->TermWin.fheight; 366 int H = r->fheight;
350 367
351 int x_[16]; 368 int x_[16];
352 int y_[16]; 369 int y_[16];
353 370
354 for (int i = 0; i <= 8; i++) 371 for (int i = 0; i <= 8; i++)
362 379
363 XGCValues gcv; 380 XGCValues gcv;
364 381
365 gcv.cap_style = CapButt; 382 gcv.cap_style = CapButt;
366 gcv.line_width = 0; 383 gcv.line_width = 0;
367 XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv); 384 XChangeGC (disp, TGC, GCLineWidth | GCCapStyle, &gcv);
368 385
369 while (a < b) 386 while (a < b)
370 { 387 {
371 uint32_t command = *a++; 388 uint32_t command = *a++;
372 389
379 int y2 = y_[(command >> 0) & 15]; 396 int y2 = y_[(command >> 0) & 15];
380 397
381 switch (op) 398 switch (op)
382 { 399 {
383 case 0: // line 400 case 0: // line
384 XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2); 401 XDrawLine (disp, d, TGC, x1, y1, x2, y2);
385 break; 402 break;
386 403
387 case 1: // rectangle, possibly stippled 404 case 1: // rectangle, possibly stippled
388 if (a) 405 if (a)
389 { 406 {
390 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 }; 407 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
391 408
392 gcv.fill_style = FillStippled; 409 gcv.fill_style = FillStippled;
393 gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2); 410 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
394 gcv.ts_x_origin = x; 411 gcv.ts_x_origin = x;
395 gcv.ts_y_origin = y; 412 gcv.ts_y_origin = y;
396 413
397 XChangeGC (d.display->display, TGC, 414 XChangeGC (disp, TGC,
398 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, 415 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
399 &gcv); 416 &gcv);
400 } 417 }
401 418
402 XFillRectangle (d.display->display, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1); 419 XFillRectangle (disp, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
403 420
404 if (a) 421 if (a)
405 { 422 {
406 XFreePixmap (d.display->display, gcv.stipple); 423 XFreePixmap (disp, gcv.stipple);
407 gcv.stipple = 0; 424 gcv.stipple = 0;
408 gcv.fill_style = FillSolid; 425 gcv.fill_style = FillSolid;
409 XChangeGC (d.display->display, TGC, GCFillStyle, &gcv); 426 XChangeGC (disp, TGC, GCFillStyle, &gcv);
410 } 427 }
411 break; 428 break;
412 case 2: // arc 429 case 2: // arc
413 XDrawArc (d.display->display, d, TGC, 430 XDrawArc (disp, d, TGC,
414 x1 - W/2, y1 - H/2, W-1, H-1, 431 x1 - W/2, y1 - H/2, W-1, H-1,
415 (a - 1) * 90*64, (b - 1) * 90*64); 432 (a - 1) * 90*64, (b - 1) * 90*64);
416 break; 433 break;
417 } 434 }
418 } 435 }
419 } 436 }
420#if ENABLE_COMBINING 437#if ENABLE_COMBINING
421 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t])) 438 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
422 { 439 {
423 text_t c2[2]; 440 text_t chrs[2];
424 int len2 = 1; 441 width = min (2, width);
425 442 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 443
433 c2[0] = cc->c1; 444 *chrs = cc->c1;
434 rxvt_font *f1 = (*fs)[fs->find_font (c2[0])]; 445 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
435 f1->draw (d, x, y, c2, len2, fg, bg); 446 f1->draw (d, x, y, chrs, width, fg, bg);
436 447
437 if (cc->c2 != NOCHAR) 448 if (cc->c2 != NOCHAR)
438 { 449 {
439 bool careful; 450 bool careful;
440 451
441 // prefer font of first character, for no good reasons 452 // prefer font of first character, for no good reasons
442 c2[0] = cc->c2; 453 *chrs = cc->c2;
443 rxvt_font *f2 = (f1->has_char (c2[0], 0, careful) && !careful) 454 rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful)
444 ? f1 455 ? f1
445 : (*fs)[fs->find_font (c2[0])]; 456 : (*fs)[fs->find_font (cc->c2)];
446 457
447 f2->draw (d, x, y, c2, len2, fg, -1); 458 f2->draw (d, x, y, chrs, width, fg, -1);
448 } 459 }
449 } 460 }
450#endif 461#endif
451 else 462 else
452 switch (t) 463 switch (t)
455 case ZERO_WIDTH_CHAR: 466 case ZERO_WIDTH_CHAR:
456 case NOCHAR: 467 case NOCHAR:
457 break; 468 break;
458 469
459 default: 470 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, 471 XDrawRectangle (disp, d, TGC, x + 2, y + 2,
469 w + r->TermWin.fwidth - 4, r->TermWin.fheight - 4); 472 fwidth - 4, r->fheight - 4);
470 x += w;
471 } 473 }
472 474
473 x += r->TermWin.fwidth; 475 x += fwidth;
474 } 476 }
475} 477}
476 478
477///////////////////////////////////////////////////////////////////////////// 479/////////////////////////////////////////////////////////////////////////////
478 480
524bool 526bool
525rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth) 527rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
526{ 528{
527 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2; 529 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
528 p.height = height; 530 p.height = height;
531 p.ascent = rxvt_fontprop::unset;
529 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium; 532 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; 533 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
531 534
532 return true; 535 return true;
533} 536}
534 537
535bool 538bool
536rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) 539rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
537{ 540{
538 unsigned long height; 541 unsigned long height;
542
543#if 0
539 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) 544 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
540 return false; 545 return false;
546#else
547 height = f->ascent + f->descent;
548#endif
541 549
542 unsigned long avgwidth; 550 unsigned long avgwidth;
543 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) 551 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
544 avgwidth = 0; 552 avgwidth = 0;
545 553
548 556
549 set_properties (p, height, weight, slant, avgwidth); 557 set_properties (p, height, weight, slant, avgwidth);
550 558
551 free (weight); 559 free (weight);
552 free (slant); 560 free (slant);
561
562 p.ascent = f->ascent;
553 563
554 return true; 564 return true;
555} 565}
556 566
557bool 567bool
628} 638}
629 639
630bool 640bool
631rxvt_font_x11::load (const rxvt_fontprop &prop) 641rxvt_font_x11::load (const rxvt_fontprop &prop)
632{ 642{
643 Display *disp = DISPLAY;
644
633 clear (); 645 clear ();
634 646
635 char field_str[64]; // enough for 128 bits 647 char field_str[64]; // enough for 128 bits
636 648
637 // first morph the font if required 649 // first morph the font if required
640 { 652 {
641 char fname[1024]; 653 char fname[1024];
642 654
643 if (name[0] != '-') 655 if (name[0] != '-')
644 { 656 {
645 f = XLoadQueryFont (DISPLAY, name); 657 f = XLoadQueryFont (disp, name);
646 658
647 if (!f) 659 if (!f)
648 return false; 660 return false;
649 661
650 char *new_name = get_property (f, "FONT", name); 662 char *new_name = get_property (f, "FONT", name);
652 if (new_name) 664 if (new_name)
653 set_name (new_name); 665 set_name (new_name);
654 else 666 else
655 rxvt_warn ("font '%s' has no FONT property, continuing without.", name); 667 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
656 668
657 XFreeFont (DISPLAY, f); 669 XFreeFont (disp, f);
658 f = 0; 670 f = 0;
659 } 671 }
660 672
661 if (prop.weight != rxvt_fontprop::unset) 673 if (prop.weight != rxvt_fontprop::unset)
662 { 674 {
673 ? "r" : "i"); // TODO: handle "o"blique, too 685 ? "r" : "i"); // TODO: handle "o"blique, too
674 set_name (strdup (fname)); 686 set_name (strdup (fname));
675 } 687 }
676 } 688 }
677 689
690 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
691 ? 0 : prop.height);
692
693 struct font_weight {
694 char *name;
695 int diff;
696
697 void clear ()
698 {
699 name = 0;
700 diff = 0x7fffffff;
701 }
702
703 font_weight () { clear (); }
704 ~font_weight () { free (name); }
705 };
706
678 char **list; 707 char **list;
679 int count; 708 int count;
680 list = XListFonts (DISPLAY, name, 1024, &count); 709 list = XListFonts (disp, name, 4000, &count);
681 710
682 set_name (0); 711 set_name (0);
683 712
684 if (!list) 713 if (!list)
685 return false; 714 return false;
686 715
687 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset 716 font_weight *fonts = new font_weight[count];
688 ? 0 : prop.height);
689 717
690 int bestdiff = 0x7fffffff;
691 for (int i = 0; i < count; i++) 718 for (int i = 0; i < count; i++)
692 { 719 {
693 rxvt_fontprop p; 720 rxvt_fontprop p;
694 char fname[1024]; 721 char fname[1024];
695 722
696 int diff = 0; 723 int diff = 0;
697 724
698 if (replace_field (fname, list[i], 6, '0', field_str)) 725 if (replace_field (fname, list[i], 6, '0', field_str))
699 diff += 10; // slightly penalize scalable fonts 726 diff += 10; // slightly penalize scalable fonts
727 else if (replace_field (fname, list[i], 11, '0', "0"))
728 diff += 300; // more heavily penalize what looks like scaled bitmap fotns
700 729
701 if (!set_properties (p, fname)) 730 if (!set_properties (p, fname))
702 continue; 731 continue;
703 732
704 if (prop.height != rxvt_fontprop::unset 733 if (prop.height != rxvt_fontprop::unset
708 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128; 737 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); 738 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); 739 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); 740 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
712 741
713 if (!name // compare against best found so far 742 fonts[i].name = strdup (fname);
714 || diff < bestdiff) 743 fonts[i].diff = diff;
715 {
716 set_name (strdup (fname));
717 bestdiff = diff;
718 }
719 } 744 }
720 745
721 XFreeFontNames (list); 746 XFreeFontNames (list);
722 747
723 if (!name) 748 // this loop only iterates when the guessed font-size is too small
724 return false; 749 for (;;)
750 {
751 font_weight *best = fonts + count - 1;
725 752
726 f = XLoadQueryFont (DISPLAY, name); 753 for (font_weight *w = best; w-- > fonts; )
754 if (w->diff <= best->diff)
755 best = w;
756
757 if (!best->name
758 || !(f = XLoadQueryFont (disp, best->name)))
759 break;
760
761 set_name (best->name);
762 best->clear ();
763
764 ascent = f->ascent;
765 descent = f->descent;
766 height = ascent + descent;
767
768 if (prop.height == rxvt_fontprop::unset
769 || height <= prop.height)
770 break; // font is ready for use
771
772 // PIXEL_SIZE small enough, but real height too large
773 clear ();
774 }
775
776 delete [] fonts;
727 777
728 if (!f) 778 if (!f)
729 return false; 779 return false;
730 780
731 char *registry = get_property (f, "CHARSET_REGISTRY", 0); 781 char *registry = get_property (f, "CHARSET_REGISTRY", 0);
759 if (cs == CS_UNICODE) 809 if (cs == CS_UNICODE)
760 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font 810 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
761 811
762 encm = f->min_byte1 != 0 || f->max_byte1 != 0; 812 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
763 enc2b = encm || f->max_char_or_byte2 > 255; 813 enc2b = encm || f->max_char_or_byte2 > 255;
764
765 ascent = f->ascent;
766 descent = f->descent;
767 height = ascent + descent;
768 814
769 slow = false; 815 slow = false;
770 816
771#if 1 // only used for slow detection, TODO optimize 817#if 1 // only used for slow detection, TODO optimize
772 if (f->min_bounds.width == f->max_bounds.width) 818 if (f->min_bounds.width == f->max_bounds.width)
811 857
812 XCharStruct g; 858 XCharStruct g;
813 int dir_ret, asc_ret, des_ret; 859 int dir_ret, asc_ret, des_ret;
814 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g); 860 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
815 861
816 int wcw = wcwidth (*t); if (wcw > 0) g.width = g.width / wcw; 862 int wcw = wcwidth (*t); if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
817 863
818 if (width < g.width) width = g.width; 864 if (width < g.width) width = g.width;
819 } 865 }
820 866
821 if (cs == CS_UNKNOWN) 867 if (cs == CS_UNKNOWN)
896 return true; 942 return true;
897 943
898 // check character against base font bounding box 944 // check character against base font bounding box
899 int w = xcs->width; 945 int w = xcs->width;
900 int wcw = wcwidth (unicode); 946 int wcw = wcwidth (unicode);
901 if (wcw > 0) w /= wcw; 947 if (wcw > 0) w = (w + wcw - 1) / wcw;
902 948
903 careful = w > prop->width; 949 careful = w > prop->width;
904 if (careful && w > prop->width * MAX_OVERLAP >> 2) 950 if (careful && w > prop->width * MAX_OVERLAP >> 2)
905 return false; 951 return false;
906 952
916 // and it is a mess /. 962 // and it is a mess /.
917 // yet we are trying to be perfect /. 963 // yet we are trying to be perfect /.
918 // but the result still isn't perfect /. 964 // but the result still isn't perfect /.
919 965
920 bool slow = this->slow 966 bool slow = this->slow
921 || width != r->TermWin.fwidth 967 || width != r->fwidth
922 || height != r->TermWin.fheight; 968 || height != r->fheight;
923 969
924 int base = ascent; // sorry, incorrect: r->TermWin.fbase; 970 int base = ascent; // sorry, incorrect: r->fbase;
925 971
926 XGCValues v; 972 XGCValues v;
927 v.foreground = r->pix_colors[fg]; 973 v.foreground = r->pix_colors[fg];
928 v.font = f->fid; 974 v.font = f->fid;
929 975
937 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 983 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
938 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len); 984 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
939 } 985 }
940 else 986 else
941 { 987 {
942 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 988 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
943 989
944 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 990 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
945 991
946 if (slow) 992 if (slow)
947 { 993 {
948 do 994 do
949 { 995 {
950 if (xc->byte1 || xc->byte2) 996 if (xc->byte1 || xc->byte2)
951 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1); 997 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
952 998
953 x += r->TermWin.fwidth; 999 x += r->fwidth;
954 xc++; len--; 1000 xc++; len--;
955 } 1001 }
956 while (len); 1002 while (len);
957 } 1003 }
958 else 1004 else
969 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 1015 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
970 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len); 1016 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
971 } 1017 }
972 else 1018 else
973 { 1019 {
974 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1020 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
975 1021
976 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 1022 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
977 1023
978 if (slow) 1024 if (slow)
979 { 1025 {
980 do 1026 do
981 { 1027 {
982 if (*xc) 1028 if (*xc)
983 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1); 1029 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
984 1030
985 x += r->TermWin.fwidth; 1031 x += r->fwidth;
986 xc++; len--; 1032 xc++; len--;
987 } 1033 }
988 while (len); 1034 while (len);
989 } 1035 }
990 else 1036 else
1033 1079
1034 FT_Face face = XftLockFace (f); 1080 FT_Face face = XftLockFace (f);
1035 1081
1036 p.width = width; 1082 p.width = width;
1037 p.height = height; 1083 p.height = height;
1084 p.ascent = ascent;
1038 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD 1085 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
1039 ? rxvt_fontprop::bold : rxvt_fontprop::medium; 1086 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
1040 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC 1087 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
1041 ? rxvt_fontprop::italic : rxvt_fontprop::roman; 1088 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
1042 1089
1046} 1093}
1047 1094
1048bool 1095bool
1049rxvt_font_xft::load (const rxvt_fontprop &prop) 1096rxvt_font_xft::load (const rxvt_fontprop &prop)
1050{ 1097{
1098 Display *disp = DISPLAY;
1099
1051 clear (); 1100 clear ();
1052 1101
1053 FcPattern *p = FcNameParse ((FcChar8 *) name); 1102 FcPattern *p = FcNameParse ((FcChar8 *) name);
1054 1103
1055 if (!p) 1104 if (!p)
1056 return false; 1105 return false;
1057 1106
1058 FcValue v; 1107 FcValue v;
1059 1108
1060 if (prop.height != rxvt_fontprop::unset 1109 if (prop.height != rxvt_fontprop::unset
1061 || (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch 1110 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1062 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch)) 1111 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
1063 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); 1112 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1064 1113
1065 if (prop.weight != rxvt_fontprop::unset 1114 if (prop.weight != rxvt_fontprop::unset
1066 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) 1115 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
1081 1130
1082 // store generated name so iso14755 view gives better results 1131 // store generated name so iso14755 view gives better results
1083 set_name ((char *)FcNameUnparse (p)); 1132 set_name ((char *)FcNameUnparse (p));
1084 1133
1085 XftResult result; 1134 XftResult result;
1086 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1135 FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result);
1087 1136
1088 FcPatternDestroy (p); 1137 FcPatternDestroy (p);
1089 1138
1090 if (!match) 1139 if (!match)
1091 return false; 1140 return false;
1093 int ftheight = 0; 1142 int ftheight = 0;
1094 bool success = true; 1143 bool success = true;
1095 1144
1096 for (;;) 1145 for (;;)
1097 { 1146 {
1098 f = XftFontOpenPattern (DISPLAY, FcPatternDuplicate (match)); 1147 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
1099 1148
1100 if (!f) 1149 if (!f)
1101 { 1150 {
1102 success = false; 1151 success = false;
1103 break; 1152 break;
1112 1161
1113 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE; 1162 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1114 1163
1115 XftUnlockFace (f); 1164 XftUnlockFace (f);
1116 1165
1166 int glheight = height;
1167
1117 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; ) 1168 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1118 { 1169 {
1119 FcChar16 ch = *t; 1170 FcChar16 ch = *t;
1120 1171
1121 if (cs != CS_UNICODE 1172 if (cs != CS_UNICODE
1127 bool careful; 1178 bool careful;
1128 if (!has_char (*t, &prop, careful)) 1179 if (!has_char (*t, &prop, careful))
1129 continue; 1180 continue;
1130 1181
1131 XGlyphInfo g; 1182 XGlyphInfo g;
1132 XftTextExtents16 (DISPLAY, f, &ch, 1, &g); 1183 XftTextExtents16 (disp, f, &ch, 1, &g);
1184
1185 g.width -= g.x;
1133 1186
1134 int wcw = wcwidth (ch); 1187 int wcw = wcwidth (ch);
1135 if (wcw > 0) g.width = g.width / wcw; 1188 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1136 1189
1137 if (width < g.width) width = g.width; 1190 if (width < g.width ) width = g.width;
1138 if (height < g.height) height = g.height; 1191 if (height < g.height ) height = g.height;
1192 if (glheight < g.height - g.y) glheight = g.height - g.y;
1193 }
1194
1195 if (!width)
1196 {
1197 rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name);
1198
1199 XftFontClose (disp, f);
1200 f = 0;
1201
1202 success = false;
1203 break;
1139 } 1204 }
1140 1205
1141 if (prop.height == rxvt_fontprop::unset 1206 if (prop.height == rxvt_fontprop::unset
1142 || height <= prop.height 1207 || (height <= prop.height && glheight <= prop.height)
1143 || height <= 2 1208 || height <= 2
1144 || !scalable) 1209 || !scalable)
1145 break; 1210 break;
1146 1211
1147 if (ftheight) 1212 if (ftheight)
1154 ftheight -= height - prop.height; 1219 ftheight -= height - prop.height;
1155 } 1220 }
1156 else 1221 else
1157 ftheight = prop.height - 1; 1222 ftheight = prop.height - 1;
1158 1223
1159 XftFontClose (DISPLAY, f); 1224 XftFontClose (disp, f);
1160 FcPatternDel (match, FC_PIXEL_SIZE); 1225 FcPatternDel (match, FC_PIXEL_SIZE);
1161 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); 1226 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1162 } 1227 }
1163 1228
1164 FcPatternDestroy (match); 1229 FcPatternDestroy (match);
1165 1230
1166#if 0 // do it per-character 1231#if 0 // do it per-character
1188 // check character against base font bounding box 1253 // check character against base font bounding box
1189 FcChar32 ch = unicode; 1254 FcChar32 ch = unicode;
1190 XGlyphInfo g; 1255 XGlyphInfo g;
1191 XftTextExtents32 (DISPLAY, f, &ch, 1, &g); 1256 XftTextExtents32 (DISPLAY, f, &ch, 1, &g);
1192 1257
1193 int w = g.width; 1258 int w = g.width - g.x;
1194 int wcw = wcwidth (unicode); 1259 int wcw = wcwidth (unicode);
1195 if (wcw > 0) w /= wcw; 1260 if (wcw > 0) w = (w + wcw - 1) / wcw;
1196 1261
1197 careful = w > prop->width; 1262 careful = w > prop->width;
1198 if (careful && w > prop->width * MAX_OVERLAP >> 2) 1263 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1199 return false; 1264 return false;
1200 1265
1204void 1269void
1205rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1270rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1206 const text_t *text, int len, 1271 const text_t *text, int len,
1207 int fg, int bg) 1272 int fg, int bg)
1208{ 1273{
1209 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1274 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
1210 1275
1211 int base = ascent; // should be fbase, but that is incorrect 1276 int base = ascent; // should be fbase, but that is incorrect
1212 1277
1213 XGlyphInfo extents; 1278 XGlyphInfo extents;
1214 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32)); 1279 XftGlyphSpec *enc = (XftGlyphSpec *) get_enc_buf (len * sizeof (XftGlyphSpec));
1215 FcChar32 *ep = enc; 1280 XftGlyphSpec *ep = enc;
1216 int ewidth = 0; 1281
1217 int xoff = 0; 1282 // skip trailing spaces
1283 while (len && text [len - 1] == ' ')
1284 len--;
1218 1285
1219 while (len) 1286 while (len)
1220 { 1287 {
1221 int cwidth = r->TermWin.fwidth; 1288 int cwidth = r->fwidth;
1222 FcChar32 fc = *text++; len--; 1289 FcChar32 fc = *text++; len--;
1223 FT_UInt gl;
1224 1290
1225 while (len && *text == NOCHAR) 1291 while (len && *text == NOCHAR)
1226 text++, len--, cwidth += r->TermWin.fwidth; 1292 text++, len--, cwidth += r->fwidth;
1227 1293
1294 if (fc != ' ') // skip spaces
1295 {
1228 gl = XftCharIndex (d.display->display, f, fc); 1296 FT_UInt gl = XftCharIndex (d.display->display, f, fc);
1229 XftGlyphExtents (d.display->display, f, &gl, 1, &extents); 1297 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1230 1298
1231 if (extents.xOff != cwidth && ep != enc) 1299 ep->glyph = gl;
1300 ep->x = x + (cwidth - extents.xOff >> 1);
1301 ep->y = y + base;
1302 ep++;
1232 { 1303 }
1233 if (xoff > ewidth) xoff = ewidth;
1234 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1235 x + (ewidth - xoff >> 1),
1236 y + base, enc, ep - enc);
1237 x += ewidth;
1238 1304
1239 ep = enc;
1240 ewidth = 0;
1241 xoff = 0;
1242 }
1243
1244 if (fc == ' ' && ep == enc) // skip leading spaces
1245 {
1246 x += cwidth; 1305 x += cwidth;
1247 continue;
1248 }
1249
1250 else
1251 {
1252 *ep++ = gl;
1253 ewidth += cwidth;
1254 xoff += extents.xOff;
1255 }
1256 } 1306 }
1257 1307
1258 if (ep != enc) 1308 if (ep != enc)
1259 {
1260 if (xoff > ewidth) xoff = ewidth;
1261 XftDrawGlyphs (d, &r->pix_colors[fg].c, f, 1309 XftDrawGlyphSpec (d, &r->pix_colors[fg].c, f, enc, ep - enc);
1262 x + (ewidth - xoff >> 1),
1263 y + base, enc, ep - enc);
1264 }
1265} 1310}
1266#endif 1311#endif
1267 1312
1268///////////////////////////////////////////////////////////////////////////// 1313/////////////////////////////////////////////////////////////////////////////
1269 1314
1270rxvt_fontset::rxvt_fontset (rxvt_t r) 1315rxvt_fontset::rxvt_fontset (rxvt_t r)
1271: r (r), fontdesc (0) 1316: fontdesc (0), r (r)
1272{ 1317{
1273 clear (); 1318 clear ();
1274} 1319}
1275 1320
1276rxvt_fontset::~rxvt_fontset () 1321rxvt_fontset::~rxvt_fontset ()
1279} 1324}
1280 1325
1281void 1326void
1282rxvt_fontset::clear () 1327rxvt_fontset::clear ()
1283{ 1328{
1284 prop.width = prop.height = prop.weight = prop.slant 1329 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1285 = rxvt_fontprop::unset; 1330 = rxvt_fontprop::unset;
1286 1331
1287 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) 1332 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1288 FONT_UNREF (*i); 1333 FONT_UNREF (*i);
1289 1334
1341 char buf[512]; 1386 char buf[512];
1342 const char *end; 1387 const char *end;
1343 1388
1344 do 1389 do
1345 { 1390 {
1346 while (*desc <= ' ') desc++; 1391 while (*desc && *desc <= ' ')
1392 desc++;
1347 1393
1348 codeset cs = CS_UNICODE; 1394 codeset cs = CS_UNICODE;
1349 1395
1350 if (*desc == '[') 1396 if (*desc == '[')
1351 { 1397 {
1461 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1507 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1462 goto next_font; 1508 goto next_font;
1463 1509
1464 if (!realize_font (i)) 1510 if (!realize_font (i))
1465 goto next_font; 1511 goto next_font;
1512
1513 if (prop.ascent != rxvt_fontprop::unset)
1514 max_it (f->ascent, prop.ascent);
1466 } 1515 }
1467 1516
1468 if (f->cs == CS_UNKNOWN) 1517 if (f->cs == CS_UNKNOWN)
1469 goto next_font; 1518 goto next_font;
1470 1519
1509 FcPatternAddInteger (p, FC_SLANT, prop.slant); 1558 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1510 FcPatternAddBool (p, FC_MINSPACE, 1); 1559 FcPatternAddBool (p, FC_MINSPACE, 1);
1511 //FcPatternAddBool (p, FC_ANTIALIAS, 1); 1560 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1512 1561
1513 XftResult result; 1562 XftResult result;
1514 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1563 FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result);
1515 1564
1516 FcPatternDestroy (p); 1565 FcPatternDestroy (p);
1517 1566
1518 if (match) 1567 if (match)
1519 { 1568 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines