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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines