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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines