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.64 by root, Tue Jan 11 05:45:57 2005 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
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}
550 554
551 set_properties (p, height, weight, slant, avgwidth); 555 set_properties (p, height, weight, slant, avgwidth);
552 556
553 free (weight); 557 free (weight);
554 free (slant); 558 free (slant);
559
560 p.ascent = f->ascent;
555 561
556 return true; 562 return true;
557} 563}
558 564
559bool 565bool
630} 636}
631 637
632bool 638bool
633rxvt_font_x11::load (const rxvt_fontprop &prop) 639rxvt_font_x11::load (const rxvt_fontprop &prop)
634{ 640{
641 Display *disp = DISPLAY;
642
635 clear (); 643 clear ();
636 644
637 char field_str[64]; // enough for 128 bits 645 char field_str[64]; // enough for 128 bits
638 646
639 // first morph the font if required 647 // first morph the font if required
642 { 650 {
643 char fname[1024]; 651 char fname[1024];
644 652
645 if (name[0] != '-') 653 if (name[0] != '-')
646 { 654 {
647 f = XLoadQueryFont (DISPLAY, name); 655 f = XLoadQueryFont (disp, name);
648 656
649 if (!f) 657 if (!f)
650 return false; 658 return false;
651 659
652 char *new_name = get_property (f, "FONT", name); 660 char *new_name = get_property (f, "FONT", name);
654 if (new_name) 662 if (new_name)
655 set_name (new_name); 663 set_name (new_name);
656 else 664 else
657 rxvt_warn ("font '%s' has no FONT property, continuing without.", name); 665 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
658 666
659 XFreeFont (DISPLAY, f); 667 XFreeFont (disp, f);
660 f = 0; 668 f = 0;
661 } 669 }
662 670
663 if (prop.weight != rxvt_fontprop::unset) 671 if (prop.weight != rxvt_fontprop::unset)
664 { 672 {
694 ~font_weight () { free (name); } 702 ~font_weight () { free (name); }
695 }; 703 };
696 704
697 char **list; 705 char **list;
698 int count; 706 int count;
699 list = XListFonts (DISPLAY, name, 4000, &count); 707 list = XListFonts (disp, name, 4000, &count);
700 708
701 set_name (0); 709 set_name (0);
702 710
703 if (!list) 711 if (!list)
704 return false; 712 return false;
712 720
713 int diff = 0; 721 int diff = 0;
714 722
715 if (replace_field (fname, list[i], 6, '0', field_str)) 723 if (replace_field (fname, list[i], 6, '0', field_str))
716 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
717 727
718 if (!set_properties (p, fname)) 728 if (!set_properties (p, fname))
719 continue; 729 continue;
720 730
721 if (prop.height != rxvt_fontprop::unset 731 if (prop.height != rxvt_fontprop::unset
737 for (;;) 747 for (;;)
738 { 748 {
739 font_weight *best = fonts + count - 1; 749 font_weight *best = fonts + count - 1;
740 750
741 for (font_weight *w = best; w-- > fonts; ) 751 for (font_weight *w = best; w-- > fonts; )
742 if (w->diff < best->diff) 752 if (w->diff <= best->diff)
743 best = w; 753 best = w;
744 754
745 if (!best->name 755 if (!best->name
746 || !(f = XLoadQueryFont (DISPLAY, best->name))) 756 || !(f = XLoadQueryFont (disp, best->name)))
747 break; 757 break;
748 758
749 set_name (best->name); 759 set_name (best->name);
750 best->clear (); 760 best->clear ();
751 761
845 855
846 XCharStruct g; 856 XCharStruct g;
847 int dir_ret, asc_ret, des_ret; 857 int dir_ret, asc_ret, des_ret;
848 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g); 858 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
849 859
850 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;
851 861
852 if (width < g.width) width = g.width; 862 if (width < g.width) width = g.width;
853 } 863 }
854 864
855 if (cs == CS_UNKNOWN) 865 if (cs == CS_UNKNOWN)
930 return true; 940 return true;
931 941
932 // check character against base font bounding box 942 // check character against base font bounding box
933 int w = xcs->width; 943 int w = xcs->width;
934 int wcw = wcwidth (unicode); 944 int wcw = wcwidth (unicode);
935 if (wcw > 0) w /= wcw; 945 if (wcw > 0) w = (w + wcw - 1) / wcw;
936 946
937 careful = w > prop->width; 947 careful = w > prop->width;
938 if (careful && w > prop->width * MAX_OVERLAP >> 2) 948 if (careful && w > prop->width * MAX_OVERLAP >> 2)
939 return false; 949 return false;
940 950
950 // and it is a mess /. 960 // and it is a mess /.
951 // yet we are trying to be perfect /. 961 // yet we are trying to be perfect /.
952 // but the result still isn't perfect /. 962 // but the result still isn't perfect /.
953 963
954 bool slow = this->slow 964 bool slow = this->slow
955 || width != r->TermWin.fwidth 965 || width != r->fwidth
956 || height != r->TermWin.fheight; 966 || height != r->fheight;
957 967
958 int base = ascent; // sorry, incorrect: r->TermWin.fbase; 968 int base = ascent; // sorry, incorrect: r->fbase;
959 969
960 XGCValues v; 970 XGCValues v;
961 v.foreground = r->pix_colors[fg]; 971 v.foreground = r->pix_colors[fg];
962 v.font = f->fid; 972 v.font = f->fid;
963 973
971 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 981 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
972 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len); 982 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
973 } 983 }
974 else 984 else
975 { 985 {
976 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);
977 987
978 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 988 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
979 989
980 if (slow) 990 if (slow)
981 { 991 {
982 do 992 do
983 { 993 {
984 if (xc->byte1 || xc->byte2) 994 if (xc->byte1 || xc->byte2)
985 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1); 995 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
986 996
987 x += r->TermWin.fwidth; 997 x += r->fwidth;
988 xc++; len--; 998 xc++; len--;
989 } 999 }
990 while (len); 1000 while (len);
991 } 1001 }
992 else 1002 else
1003 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 1013 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
1004 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len); 1014 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
1005 } 1015 }
1006 else 1016 else
1007 { 1017 {
1008 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);
1009 1019
1010 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 1020 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
1011 1021
1012 if (slow) 1022 if (slow)
1013 { 1023 {
1014 do 1024 do
1015 { 1025 {
1016 if (*xc) 1026 if (*xc)
1017 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1); 1027 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
1018 1028
1019 x += r->TermWin.fwidth; 1029 x += r->fwidth;
1020 xc++; len--; 1030 xc++; len--;
1021 } 1031 }
1022 while (len); 1032 while (len);
1023 } 1033 }
1024 else 1034 else
1067 1077
1068 FT_Face face = XftLockFace (f); 1078 FT_Face face = XftLockFace (f);
1069 1079
1070 p.width = width; 1080 p.width = width;
1071 p.height = height; 1081 p.height = height;
1082 p.ascent = ascent;
1072 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD 1083 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
1073 ? rxvt_fontprop::bold : rxvt_fontprop::medium; 1084 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
1074 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC 1085 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
1075 ? rxvt_fontprop::italic : rxvt_fontprop::roman; 1086 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
1076 1087
1080} 1091}
1081 1092
1082bool 1093bool
1083rxvt_font_xft::load (const rxvt_fontprop &prop) 1094rxvt_font_xft::load (const rxvt_fontprop &prop)
1084{ 1095{
1096 Display *disp = DISPLAY;
1097
1085 clear (); 1098 clear ();
1086 1099
1087 FcPattern *p = FcNameParse ((FcChar8 *) name); 1100 FcPattern *p = FcNameParse ((FcChar8 *) name);
1088 1101
1089 if (!p) 1102 if (!p)
1090 return false; 1103 return false;
1091 1104
1092 FcValue v; 1105 FcValue v;
1093 1106
1094 if (prop.height != rxvt_fontprop::unset 1107 if (prop.height != rxvt_fontprop::unset
1095 || (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch 1108 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1096 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch)) 1109 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
1097 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); 1110 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1098 1111
1099 if (prop.weight != rxvt_fontprop::unset 1112 if (prop.weight != rxvt_fontprop::unset
1100 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) 1113 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
1115 1128
1116 // store generated name so iso14755 view gives better results 1129 // store generated name so iso14755 view gives better results
1117 set_name ((char *)FcNameUnparse (p)); 1130 set_name ((char *)FcNameUnparse (p));
1118 1131
1119 XftResult result; 1132 XftResult result;
1120 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1133 FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result);
1121 1134
1122 FcPatternDestroy (p); 1135 FcPatternDestroy (p);
1123 1136
1124 if (!match) 1137 if (!match)
1125 return false; 1138 return false;
1127 int ftheight = 0; 1140 int ftheight = 0;
1128 bool success = true; 1141 bool success = true;
1129 1142
1130 for (;;) 1143 for (;;)
1131 { 1144 {
1132 f = XftFontOpenPattern (DISPLAY, FcPatternDuplicate (match)); 1145 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
1133 1146
1134 if (!f) 1147 if (!f)
1135 { 1148 {
1136 success = false; 1149 success = false;
1137 break; 1150 break;
1146 1159
1147 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE; 1160 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1148 1161
1149 XftUnlockFace (f); 1162 XftUnlockFace (f);
1150 1163
1164 int glheight = height;
1165
1151 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; )
1152 { 1167 {
1153 FcChar16 ch = *t; 1168 FcChar16 ch = *t;
1154 1169
1155 if (cs != CS_UNICODE 1170 if (cs != CS_UNICODE
1161 bool careful; 1176 bool careful;
1162 if (!has_char (*t, &prop, careful)) 1177 if (!has_char (*t, &prop, careful))
1163 continue; 1178 continue;
1164 1179
1165 XGlyphInfo g; 1180 XGlyphInfo g;
1166 XftTextExtents16 (DISPLAY, f, &ch, 1, &g); 1181 XftTextExtents16 (disp, f, &ch, 1, &g);
1182
1183 g.width -= g.x;
1167 1184
1168 int wcw = wcwidth (ch); 1185 int wcw = wcwidth (ch);
1169 if (wcw > 0) g.width = g.width / wcw; 1186 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1170 1187
1171 if (width < g.width) width = g.width; 1188 if (width < g.width ) width = g.width;
1172 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;
1173 } 1202 }
1174 1203
1175 if (prop.height == rxvt_fontprop::unset 1204 if (prop.height == rxvt_fontprop::unset
1176 || height <= prop.height 1205 || (height <= prop.height && glheight <= prop.height)
1177 || height <= 2 1206 || height <= 2
1178 || !scalable) 1207 || !scalable)
1179 break; 1208 break;
1180 1209
1181 if (ftheight) 1210 if (ftheight)
1188 ftheight -= height - prop.height; 1217 ftheight -= height - prop.height;
1189 } 1218 }
1190 else 1219 else
1191 ftheight = prop.height - 1; 1220 ftheight = prop.height - 1;
1192 1221
1193 XftFontClose (DISPLAY, f); 1222 XftFontClose (disp, f);
1194 FcPatternDel (match, FC_PIXEL_SIZE); 1223 FcPatternDel (match, FC_PIXEL_SIZE);
1195 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); 1224 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1196 } 1225 }
1197 1226
1198 FcPatternDestroy (match); 1227 FcPatternDestroy (match);
1199 1228
1200#if 0 // do it per-character 1229#if 0 // do it per-character
1222 // check character against base font bounding box 1251 // check character against base font bounding box
1223 FcChar32 ch = unicode; 1252 FcChar32 ch = unicode;
1224 XGlyphInfo g; 1253 XGlyphInfo g;
1225 XftTextExtents32 (DISPLAY, f, &ch, 1, &g); 1254 XftTextExtents32 (DISPLAY, f, &ch, 1, &g);
1226 1255
1227 int w = g.width; 1256 int w = g.width - g.x;
1228 int wcw = wcwidth (unicode); 1257 int wcw = wcwidth (unicode);
1229 if (wcw > 0) w /= wcw; 1258 if (wcw > 0) w = (w + wcw - 1) / wcw;
1230 1259
1231 careful = w > prop->width; 1260 careful = w > prop->width;
1232 if (careful && w > prop->width * MAX_OVERLAP >> 2) 1261 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1233 return false; 1262 return false;
1234 1263
1238void 1267void
1239rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1268rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1240 const text_t *text, int len, 1269 const text_t *text, int len,
1241 int fg, int bg) 1270 int fg, int bg)
1242{ 1271{
1243 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);
1244 1273
1245 int base = ascent; // should be fbase, but that is incorrect 1274 int base = ascent; // should be fbase, but that is incorrect
1246 1275
1247 XGlyphInfo extents; 1276 XGlyphInfo extents;
1248 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32)); 1277 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32));
1249 FcChar32 *ep = enc; 1278 FcChar32 *ep = enc;
1250 int ewidth = 0; 1279 int ewidth = 0;
1251 int xoff = 0;
1252 1280
1253 while (len) 1281 while (len)
1254 { 1282 {
1255 int cwidth = r->TermWin.fwidth; 1283 int cwidth = r->fwidth;
1256 FcChar32 fc = *text++; len--; 1284 FcChar32 fc = *text++; len--;
1257 FT_UInt gl;
1258 1285
1259 while (len && *text == NOCHAR) 1286 while (len && *text == NOCHAR)
1260 text++, len--, cwidth += r->TermWin.fwidth; 1287 text++, len--, cwidth += r->fwidth;
1261 1288
1262 gl = XftCharIndex (d.display->display, f, fc);
1263 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1264
1265 if (extents.xOff != cwidth && ep != enc)
1266 {
1267 if (xoff > ewidth) xoff = ewidth;
1268 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1269 x + (ewidth - xoff >> 1),
1270 y + base, enc, ep - enc);
1271 x += ewidth;
1272
1273 ep = enc;
1274 ewidth = 0;
1275 xoff = 0;
1276 }
1277
1278 if (fc == ' ' && ep == enc) // skip leading spaces 1289 if (fc == ' ' && ep == enc) // skip leading spaces
1279 {
1280 x += cwidth; 1290 x += cwidth;
1281 continue;
1282 }
1283
1284 else 1291 else
1285 { 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 {
1286 *ep++ = gl; 1318 *ep++ = gl;
1287 ewidth += cwidth; 1319 ewidth += cwidth;
1288 xoff += extents.xOff; 1320 }
1289 } 1321 }
1290 } 1322 }
1291 1323
1292 if (ep != enc) 1324 if (ep != enc)
1293 {
1294 if (xoff > ewidth) xoff = ewidth;
1295 XftDrawGlyphs (d, &r->pix_colors[fg].c, f, 1325 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1296 x + (ewidth - xoff >> 1),
1297 y + base, enc, ep - enc); 1326 x, y + base, enc, ep - enc);
1298 }
1299} 1327}
1300#endif 1328#endif
1301 1329
1302///////////////////////////////////////////////////////////////////////////// 1330/////////////////////////////////////////////////////////////////////////////
1303 1331
1304rxvt_fontset::rxvt_fontset (rxvt_t r) 1332rxvt_fontset::rxvt_fontset (rxvt_t r)
1305: r (r), fontdesc (0) 1333: fontdesc (0), r (r)
1306{ 1334{
1307 clear (); 1335 clear ();
1308} 1336}
1309 1337
1310rxvt_fontset::~rxvt_fontset () 1338rxvt_fontset::~rxvt_fontset ()
1313} 1341}
1314 1342
1315void 1343void
1316rxvt_fontset::clear () 1344rxvt_fontset::clear ()
1317{ 1345{
1318 prop.width = prop.height = prop.weight = prop.slant 1346 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1319 = rxvt_fontprop::unset; 1347 = rxvt_fontprop::unset;
1320 1348
1321 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) 1349 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1322 FONT_UNREF (*i); 1350 FONT_UNREF (*i);
1323 1351
1375 char buf[512]; 1403 char buf[512];
1376 const char *end; 1404 const char *end;
1377 1405
1378 do 1406 do
1379 { 1407 {
1380 while (*desc <= ' ') desc++; 1408 while (*desc && *desc <= ' ')
1409 desc++;
1381 1410
1382 codeset cs = CS_UNICODE; 1411 codeset cs = CS_UNICODE;
1383 1412
1384 if (*desc == '[') 1413 if (*desc == '[')
1385 { 1414 {
1495 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1524 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1496 goto next_font; 1525 goto next_font;
1497 1526
1498 if (!realize_font (i)) 1527 if (!realize_font (i))
1499 goto next_font; 1528 goto next_font;
1529
1530 if (prop.ascent != rxvt_fontprop::unset)
1531 max_it (f->ascent, prop.ascent);
1500 } 1532 }
1501 1533
1502 if (f->cs == CS_UNKNOWN) 1534 if (f->cs == CS_UNKNOWN)
1503 goto next_font; 1535 goto next_font;
1504 1536
1543 FcPatternAddInteger (p, FC_SLANT, prop.slant); 1575 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1544 FcPatternAddBool (p, FC_MINSPACE, 1); 1576 FcPatternAddBool (p, FC_MINSPACE, 1);
1545 //FcPatternAddBool (p, FC_ANTIALIAS, 1); 1577 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1546 1578
1547 XftResult result; 1579 XftResult result;
1548 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1580 FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result);
1549 1581
1550 FcPatternDestroy (p); 1582 FcPatternDestroy (p);
1551 1583
1552 if (match) 1584 if (match)
1553 { 1585 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines