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.75 by root, Sun Dec 18 00:59:42 2005 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};
331void 331void
332rxvt_font_default::draw (rxvt_drawable &d, int x, int y, 332rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
333 const text_t *text, int len, 333 const text_t *text, int len,
334 int fg, int bg) 334 int fg, int bg)
335{ 335{
336 Display *disp = d.display->display;
337
336 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 338 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
337 339
338 XSetForeground (d.display->display, TGC, r->pix_colors[fg]); 340 XSetForeground (disp, TGC, r->pix_colors[fg]);
339 341
340 while (len) 342 while (len)
341 { 343 {
342#if ENABLE_COMBINING 344#if ENABLE_COMBINING
343 compose_char *cc; 345 compose_char *cc;
347 349
348 while (++text, --len && *text == NOCHAR) 350 while (++text, --len && *text == NOCHAR)
349 ; 351 ;
350 352
351 int width = text - tp; 353 int width = text - tp;
352 int fwidth = r->TermWin.fwidth * width; 354 int fwidth = r->fwidth * width;
353 355
354 if (0x2500 <= t && t <= 0x259f) 356 if (0x2500 <= t && t <= 0x259f)
355 { 357 {
356 uint16_t offs = linedraw_offs[t - 0x2500]; 358 uint16_t offs = linedraw_offs[t - 0x2500];
357 uint32_t *a = linedraw_command + (offs >> 4); 359 uint32_t *a = linedraw_command + (offs >> 4);
358 uint32_t *b = a + (offs & 15); 360 uint32_t *b = a + (offs & 15);
359 361
360 int W = fwidth; 362 int W = fwidth;
361 int H = r->TermWin.fheight; 363 int H = r->fheight;
362 364
363 int x_[16]; 365 int x_[16];
364 int y_[16]; 366 int y_[16];
365 367
366 for (int i = 0; i <= 8; i++) 368 for (int i = 0; i <= 8; i++)
374 376
375 XGCValues gcv; 377 XGCValues gcv;
376 378
377 gcv.cap_style = CapButt; 379 gcv.cap_style = CapButt;
378 gcv.line_width = 0; 380 gcv.line_width = 0;
379 XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv); 381 XChangeGC (disp, TGC, GCLineWidth | GCCapStyle, &gcv);
380 382
381 while (a < b) 383 while (a < b)
382 { 384 {
383 uint32_t command = *a++; 385 uint32_t command = *a++;
384 386
391 int y2 = y_[(command >> 0) & 15]; 393 int y2 = y_[(command >> 0) & 15];
392 394
393 switch (op) 395 switch (op)
394 { 396 {
395 case 0: // line 397 case 0: // line
396 XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2); 398 XDrawLine (disp, d, TGC, x1, y1, x2, y2);
397 break; 399 break;
398 400
399 case 1: // rectangle, possibly stippled 401 case 1: // rectangle, possibly stippled
400 if (a) 402 if (a)
401 { 403 {
402 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 }; 404 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
403 405
404 gcv.fill_style = FillStippled; 406 gcv.fill_style = FillStippled;
405 gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2); 407 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
406 gcv.ts_x_origin = x; 408 gcv.ts_x_origin = x;
407 gcv.ts_y_origin = y; 409 gcv.ts_y_origin = y;
408 410
409 XChangeGC (d.display->display, TGC, 411 XChangeGC (disp, TGC,
410 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, 412 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
411 &gcv); 413 &gcv);
412 } 414 }
413 415
414 XFillRectangle (d.display->display, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1); 416 XFillRectangle (disp, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
415 417
416 if (a) 418 if (a)
417 { 419 {
418 XFreePixmap (d.display->display, gcv.stipple); 420 XFreePixmap (disp, gcv.stipple);
419 gcv.stipple = 0; 421 gcv.stipple = 0;
420 gcv.fill_style = FillSolid; 422 gcv.fill_style = FillSolid;
421 XChangeGC (d.display->display, TGC, GCFillStyle, &gcv); 423 XChangeGC (disp, TGC, GCFillStyle, &gcv);
422 } 424 }
423 break; 425 break;
424 case 2: // arc 426 case 2: // arc
425 XDrawArc (d.display->display, d, TGC, 427 XDrawArc (disp, d, TGC,
426 x1 - W/2, y1 - H/2, W-1, H-1, 428 x1 - W/2, y1 - H/2, W-1, H-1,
427 (a - 1) * 90*64, (b - 1) * 90*64); 429 (a - 1) * 90*64, (b - 1) * 90*64);
428 break; 430 break;
429 } 431 }
430 } 432 }
461 case ZERO_WIDTH_CHAR: 463 case ZERO_WIDTH_CHAR:
462 case NOCHAR: 464 case NOCHAR:
463 break; 465 break;
464 466
465 default: 467 default:
466 XDrawRectangle (d.display->display, d, TGC, x + 2, y + 2, 468 XDrawRectangle (disp, d, TGC, x + 2, y + 2,
467 fwidth - 4, r->TermWin.fheight - 4); 469 fwidth - 4, r->fheight - 4);
468 } 470 }
469 471
470 x += fwidth; 472 x += fwidth;
471 } 473 }
472} 474}
531 533
532bool 534bool
533rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) 535rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
534{ 536{
535 unsigned long height; 537 unsigned long height;
538
539#if 0
536 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) 540 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
537 return false; 541 return false;
542#else
543 height = f->ascent + f->descent;
544#endif
538 545
539 unsigned long avgwidth; 546 unsigned long avgwidth;
540 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) 547 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
541 avgwidth = 0; 548 avgwidth = 0;
542 549
625} 632}
626 633
627bool 634bool
628rxvt_font_x11::load (const rxvt_fontprop &prop) 635rxvt_font_x11::load (const rxvt_fontprop &prop)
629{ 636{
637 Display *disp = DISPLAY;
638
630 clear (); 639 clear ();
631 640
632 char field_str[64]; // enough for 128 bits 641 char field_str[64]; // enough for 128 bits
633 642
634 // first morph the font if required 643 // first morph the font if required
637 { 646 {
638 char fname[1024]; 647 char fname[1024];
639 648
640 if (name[0] != '-') 649 if (name[0] != '-')
641 { 650 {
642 f = XLoadQueryFont (DISPLAY, name); 651 f = XLoadQueryFont (disp, name);
643 652
644 if (!f) 653 if (!f)
645 return false; 654 return false;
646 655
647 char *new_name = get_property (f, "FONT", name); 656 char *new_name = get_property (f, "FONT", name);
649 if (new_name) 658 if (new_name)
650 set_name (new_name); 659 set_name (new_name);
651 else 660 else
652 rxvt_warn ("font '%s' has no FONT property, continuing without.", name); 661 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
653 662
654 XFreeFont (DISPLAY, f); 663 XFreeFont (disp, f);
655 f = 0; 664 f = 0;
656 } 665 }
657 666
658 if (prop.weight != rxvt_fontprop::unset) 667 if (prop.weight != rxvt_fontprop::unset)
659 { 668 {
670 ? "r" : "i"); // TODO: handle "o"blique, too 679 ? "r" : "i"); // TODO: handle "o"blique, too
671 set_name (strdup (fname)); 680 set_name (strdup (fname));
672 } 681 }
673 } 682 }
674 683
684 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
685 ? 0 : prop.height);
686
687 struct font_weight {
688 char *name;
689 int diff;
690
691 void clear ()
692 {
693 name = 0;
694 diff = 0x7fffffff;
695 }
696
697 font_weight () { clear (); }
698 ~font_weight () { free (name); }
699 };
700
675 char **list; 701 char **list;
676 int count; 702 int count;
677 list = XListFonts (DISPLAY, name, 1024, &count); 703 list = XListFonts (disp, name, 4000, &count);
678 704
679 set_name (0); 705 set_name (0);
680 706
681 if (!list) 707 if (!list)
682 return false; 708 return false;
683 709
684 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset 710 font_weight *fonts = new font_weight[count];
685 ? 0 : prop.height);
686 711
687 int bestdiff = 0x7fffffff;
688 for (int i = 0; i < count; i++) 712 for (int i = 0; i < count; i++)
689 { 713 {
690 rxvt_fontprop p; 714 rxvt_fontprop p;
691 char fname[1024]; 715 char fname[1024];
692 716
693 int diff = 0; 717 int diff = 0;
694 718
695 if (replace_field (fname, list[i], 6, '0', field_str)) 719 if (replace_field (fname, list[i], 6, '0', field_str))
696 diff += 10; // slightly penalize scalable fonts 720 diff += 10; // slightly penalize scalable fonts
721 else if (replace_field (fname, list[i], 11, '0', "0"))
722 diff += 300; // more heavily penalize what looks like scaled bitmap fotns
697 723
698 if (!set_properties (p, fname)) 724 if (!set_properties (p, fname))
699 continue; 725 continue;
700 726
701 if (prop.height != rxvt_fontprop::unset 727 if (prop.height != rxvt_fontprop::unset
705 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128; 731 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); 732 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); 733 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); 734 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
709 735
710 if (!name // compare against best found so far 736 fonts[i].name = strdup (fname);
711 || diff < bestdiff) 737 fonts[i].diff = diff;
712 {
713 set_name (strdup (fname));
714 bestdiff = diff;
715 }
716 } 738 }
717 739
718 XFreeFontNames (list); 740 XFreeFontNames (list);
719 741
720 if (!name) 742 // this loop only iterates when the guessed font-size is too small
721 return false; 743 for (;;)
744 {
745 font_weight *best = fonts + count - 1;
722 746
723 f = XLoadQueryFont (DISPLAY, name); 747 for (font_weight *w = best; w-- > fonts; )
748 if (w->diff <= best->diff)
749 best = w;
750
751 if (!best->name
752 || !(f = XLoadQueryFont (disp, best->name)))
753 break;
754
755 set_name (best->name);
756 best->clear ();
757
758 ascent = f->ascent;
759 descent = f->descent;
760 height = ascent + descent;
761
762 if (prop.height == rxvt_fontprop::unset
763 || height <= prop.height)
764 break; // font is ready for use
765
766 // PIXEL_SIZE small enough, but real height too large
767 clear ();
768 }
769
770 delete [] fonts;
724 771
725 if (!f) 772 if (!f)
726 return false; 773 return false;
727 774
728 char *registry = get_property (f, "CHARSET_REGISTRY", 0); 775 char *registry = get_property (f, "CHARSET_REGISTRY", 0);
756 if (cs == CS_UNICODE) 803 if (cs == CS_UNICODE)
757 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font 804 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
758 805
759 encm = f->min_byte1 != 0 || f->max_byte1 != 0; 806 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
760 enc2b = encm || f->max_char_or_byte2 > 255; 807 enc2b = encm || f->max_char_or_byte2 > 255;
761
762 ascent = f->ascent;
763 descent = f->descent;
764 height = ascent + descent;
765 808
766 slow = false; 809 slow = false;
767 810
768#if 1 // only used for slow detection, TODO optimize 811#if 1 // only used for slow detection, TODO optimize
769 if (f->min_bounds.width == f->max_bounds.width) 812 if (f->min_bounds.width == f->max_bounds.width)
808 851
809 XCharStruct g; 852 XCharStruct g;
810 int dir_ret, asc_ret, des_ret; 853 int dir_ret, asc_ret, des_ret;
811 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g); 854 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
812 855
813 int wcw = wcwidth (*t); if (wcw > 0) g.width = g.width / wcw; 856 int wcw = wcwidth (*t); if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
814 857
815 if (width < g.width) width = g.width; 858 if (width < g.width) width = g.width;
816 } 859 }
817 860
818 if (cs == CS_UNKNOWN) 861 if (cs == CS_UNKNOWN)
893 return true; 936 return true;
894 937
895 // check character against base font bounding box 938 // check character against base font bounding box
896 int w = xcs->width; 939 int w = xcs->width;
897 int wcw = wcwidth (unicode); 940 int wcw = wcwidth (unicode);
898 if (wcw > 0) w /= wcw; 941 if (wcw > 0) w = (w + wcw - 1) / wcw;
899 942
900 careful = w > prop->width; 943 careful = w > prop->width;
901 if (careful && w > prop->width * MAX_OVERLAP >> 2) 944 if (careful && w > prop->width * MAX_OVERLAP >> 2)
902 return false; 945 return false;
903 946
913 // and it is a mess /. 956 // and it is a mess /.
914 // yet we are trying to be perfect /. 957 // yet we are trying to be perfect /.
915 // but the result still isn't perfect /. 958 // but the result still isn't perfect /.
916 959
917 bool slow = this->slow 960 bool slow = this->slow
918 || width != r->TermWin.fwidth 961 || width != r->fwidth
919 || height != r->TermWin.fheight; 962 || height != r->fheight;
920 963
921 int base = ascent; // sorry, incorrect: r->TermWin.fbase; 964 int base = ascent; // sorry, incorrect: r->fbase;
922 965
923 XGCValues v; 966 XGCValues v;
924 v.foreground = r->pix_colors[fg]; 967 v.foreground = r->pix_colors[fg];
925 v.font = f->fid; 968 v.font = f->fid;
926 969
934 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 977 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
935 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len); 978 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
936 } 979 }
937 else 980 else
938 { 981 {
939 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 982 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
940 983
941 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 984 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
942 985
943 if (slow) 986 if (slow)
944 { 987 {
945 do 988 do
946 { 989 {
947 if (xc->byte1 || xc->byte2) 990 if (xc->byte1 || xc->byte2)
948 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1); 991 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
949 992
950 x += r->TermWin.fwidth; 993 x += r->fwidth;
951 xc++; len--; 994 xc++; len--;
952 } 995 }
953 while (len); 996 while (len);
954 } 997 }
955 else 998 else
966 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 1009 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
967 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len); 1010 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
968 } 1011 }
969 else 1012 else
970 { 1013 {
971 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1014 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
972 1015
973 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 1016 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
974 1017
975 if (slow) 1018 if (slow)
976 { 1019 {
977 do 1020 do
978 { 1021 {
979 if (*xc) 1022 if (*xc)
980 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1); 1023 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
981 1024
982 x += r->TermWin.fwidth; 1025 x += r->fwidth;
983 xc++; len--; 1026 xc++; len--;
984 } 1027 }
985 while (len); 1028 while (len);
986 } 1029 }
987 else 1030 else
1043} 1086}
1044 1087
1045bool 1088bool
1046rxvt_font_xft::load (const rxvt_fontprop &prop) 1089rxvt_font_xft::load (const rxvt_fontprop &prop)
1047{ 1090{
1091 Display *disp = DISPLAY;
1092
1048 clear (); 1093 clear ();
1049 1094
1050 FcPattern *p = FcNameParse ((FcChar8 *) name); 1095 FcPattern *p = FcNameParse ((FcChar8 *) name);
1051 1096
1052 if (!p) 1097 if (!p)
1053 return false; 1098 return false;
1054 1099
1055 FcValue v; 1100 FcValue v;
1056 1101
1057 if (prop.height != rxvt_fontprop::unset 1102 if (prop.height != rxvt_fontprop::unset
1058 || (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch 1103 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1059 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch)) 1104 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
1060 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); 1105 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1061 1106
1062 if (prop.weight != rxvt_fontprop::unset 1107 if (prop.weight != rxvt_fontprop::unset
1063 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) 1108 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
1078 1123
1079 // store generated name so iso14755 view gives better results 1124 // store generated name so iso14755 view gives better results
1080 set_name ((char *)FcNameUnparse (p)); 1125 set_name ((char *)FcNameUnparse (p));
1081 1126
1082 XftResult result; 1127 XftResult result;
1083 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1128 FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result);
1084 1129
1085 FcPatternDestroy (p); 1130 FcPatternDestroy (p);
1086 1131
1087 if (!match) 1132 if (!match)
1088 return false; 1133 return false;
1090 int ftheight = 0; 1135 int ftheight = 0;
1091 bool success = true; 1136 bool success = true;
1092 1137
1093 for (;;) 1138 for (;;)
1094 { 1139 {
1095 f = XftFontOpenPattern (DISPLAY, FcPatternDuplicate (match)); 1140 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
1096 1141
1097 if (!f) 1142 if (!f)
1098 { 1143 {
1099 success = false; 1144 success = false;
1100 break; 1145 break;
1109 1154
1110 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE; 1155 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1111 1156
1112 XftUnlockFace (f); 1157 XftUnlockFace (f);
1113 1158
1159 int glheight = height;
1160
1114 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; ) 1161 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1115 { 1162 {
1116 FcChar16 ch = *t; 1163 FcChar16 ch = *t;
1117 1164
1118 if (cs != CS_UNICODE 1165 if (cs != CS_UNICODE
1124 bool careful; 1171 bool careful;
1125 if (!has_char (*t, &prop, careful)) 1172 if (!has_char (*t, &prop, careful))
1126 continue; 1173 continue;
1127 1174
1128 XGlyphInfo g; 1175 XGlyphInfo g;
1129 XftTextExtents16 (DISPLAY, f, &ch, 1, &g); 1176 XftTextExtents16 (disp, f, &ch, 1, &g);
1177
1178 g.width -= g.x;
1130 1179
1131 int wcw = wcwidth (ch); 1180 int wcw = wcwidth (ch);
1132 if (wcw > 0) g.width = g.width / wcw; 1181 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1133 1182
1134 if (width < g.width) width = g.width; 1183 if (width < g.width ) width = g.width;
1135 if (height < g.height) height = g.height; 1184 if (height < g.height ) height = g.height;
1185 if (glheight < g.height - g.y) glheight = g.height - g.y;
1186 }
1187
1188 if (!width)
1189 {
1190 rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name);
1191
1192 XftFontClose (disp, f);
1193 f = 0;
1194
1195 success = false;
1196 break;
1136 } 1197 }
1137 1198
1138 if (prop.height == rxvt_fontprop::unset 1199 if (prop.height == rxvt_fontprop::unset
1139 || height <= prop.height 1200 || (height <= prop.height && glheight <= prop.height)
1140 || height <= 2 1201 || height <= 2
1141 || !scalable) 1202 || !scalable)
1142 break; 1203 break;
1143 1204
1144 if (ftheight) 1205 if (ftheight)
1151 ftheight -= height - prop.height; 1212 ftheight -= height - prop.height;
1152 } 1213 }
1153 else 1214 else
1154 ftheight = prop.height - 1; 1215 ftheight = prop.height - 1;
1155 1216
1156 XftFontClose (DISPLAY, f); 1217 XftFontClose (disp, f);
1157 FcPatternDel (match, FC_PIXEL_SIZE); 1218 FcPatternDel (match, FC_PIXEL_SIZE);
1158 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); 1219 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1159 } 1220 }
1160 1221
1161 FcPatternDestroy (match); 1222 FcPatternDestroy (match);
1162 1223
1163#if 0 // do it per-character 1224#if 0 // do it per-character
1185 // check character against base font bounding box 1246 // check character against base font bounding box
1186 FcChar32 ch = unicode; 1247 FcChar32 ch = unicode;
1187 XGlyphInfo g; 1248 XGlyphInfo g;
1188 XftTextExtents32 (DISPLAY, f, &ch, 1, &g); 1249 XftTextExtents32 (DISPLAY, f, &ch, 1, &g);
1189 1250
1190 int w = g.width; 1251 int w = g.width - g.x;
1191 int wcw = wcwidth (unicode); 1252 int wcw = wcwidth (unicode);
1192 if (wcw > 0) w /= wcw; 1253 if (wcw > 0) w = (w + wcw - 1) / wcw;
1193 1254
1194 careful = w > prop->width; 1255 careful = w > prop->width;
1195 if (careful && w > prop->width * MAX_OVERLAP >> 2) 1256 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1196 return false; 1257 return false;
1197 1258
1201void 1262void
1202rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1263rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1203 const text_t *text, int len, 1264 const text_t *text, int len,
1204 int fg, int bg) 1265 int fg, int bg)
1205{ 1266{
1206 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1267 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
1207 1268
1208 int base = ascent; // should be fbase, but that is incorrect 1269 int base = ascent; // should be fbase, but that is incorrect
1209 1270
1210 XGlyphInfo extents; 1271 XGlyphInfo extents;
1211 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32)); 1272 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32));
1212 FcChar32 *ep = enc; 1273 FcChar32 *ep = enc;
1213 int ewidth = 0; 1274 int ewidth = 0;
1214 int xoff = 0;
1215 1275
1216 while (len) 1276 while (len)
1217 { 1277 {
1218 int cwidth = r->TermWin.fwidth; 1278 int cwidth = r->fwidth;
1219 FcChar32 fc = *text++; len--; 1279 FcChar32 fc = *text++; len--;
1220 FT_UInt gl;
1221 1280
1222 while (len && *text == NOCHAR) 1281 while (len && *text == NOCHAR)
1223 text++, len--, cwidth += r->TermWin.fwidth; 1282 text++, len--, cwidth += r->fwidth;
1224 1283
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 1284 if (fc == ' ' && ep == enc) // skip leading spaces
1242 {
1243 x += cwidth; 1285 x += cwidth;
1244 continue;
1245 }
1246
1247 else 1286 else
1248 { 1287 {
1288 FT_UInt gl = XftCharIndex (d.display->display, f, fc);
1289 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1290
1291 if (extents.xOff != cwidth)
1292 {
1293 if (ewidth)
1294 {
1295 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1296 x, y + base, enc, ep - enc);
1297 x += ewidth;
1298
1299 ep = enc;
1300 ewidth = 0;
1301 }
1302
1303 if (extents.xOff > cwidth)
1304 extents.xOff = cwidth;
1305
1306 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1307 x + (cwidth - extents.xOff >> 1),
1308 y + base, &gl, 1);
1309 x += cwidth;
1310 }
1311 else
1312 {
1249 *ep++ = gl; 1313 *ep++ = gl;
1250 ewidth += cwidth; 1314 ewidth += cwidth;
1251 xoff += extents.xOff; 1315 }
1252 } 1316 }
1253 } 1317 }
1254 1318
1255 if (ep != enc) 1319 if (ep != enc)
1256 {
1257 if (xoff > ewidth) xoff = ewidth;
1258 XftDrawGlyphs (d, &r->pix_colors[fg].c, f, 1320 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1259 x + (ewidth - xoff >> 1),
1260 y + base, enc, ep - enc); 1321 x, y + base, enc, ep - enc);
1261 }
1262} 1322}
1263#endif 1323#endif
1264 1324
1265///////////////////////////////////////////////////////////////////////////// 1325/////////////////////////////////////////////////////////////////////////////
1266 1326
1267rxvt_fontset::rxvt_fontset (rxvt_t r) 1327rxvt_fontset::rxvt_fontset (rxvt_t r)
1268: r (r), fontdesc (0) 1328: fontdesc (0), r (r)
1269{ 1329{
1270 clear (); 1330 clear ();
1271} 1331}
1272 1332
1273rxvt_fontset::~rxvt_fontset () 1333rxvt_fontset::~rxvt_fontset ()
1338 char buf[512]; 1398 char buf[512];
1339 const char *end; 1399 const char *end;
1340 1400
1341 do 1401 do
1342 { 1402 {
1343 while (*desc <= ' ') desc++; 1403 while (*desc && *desc <= ' ')
1404 desc++;
1344 1405
1345 codeset cs = CS_UNICODE; 1406 codeset cs = CS_UNICODE;
1346 1407
1347 if (*desc == '[') 1408 if (*desc == '[')
1348 { 1409 {
1506 FcPatternAddInteger (p, FC_SLANT, prop.slant); 1567 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1507 FcPatternAddBool (p, FC_MINSPACE, 1); 1568 FcPatternAddBool (p, FC_MINSPACE, 1);
1508 //FcPatternAddBool (p, FC_ANTIALIAS, 1); 1569 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1509 1570
1510 XftResult result; 1571 XftResult result;
1511 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1572 FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result);
1512 1573
1513 FcPatternDestroy (p); 1574 FcPatternDestroy (p);
1514 1575
1515 if (match) 1576 if (match)
1516 { 1577 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines