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.77 by root, Wed Jan 4 20:43:38 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 "rxvtlib.h"
24#include "rxvtutil.h" 25#include "rxvtutil.h"
25#include "rxvtfont.h" 26#include "rxvtfont.h"
26 27
27#include <cstdlib> 28#include <cstdlib>
28#include <wchar.h> 29#include <wchar.h>
29#include <inttypes.h> 30#include <inttypes.h>
30 31
31#define DISPLAY r->display->display 32#define DISPLAY r->display->display
32#define TGC r->TermWin.gc 33#define TGC r->gc
33 34
34#define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width 35#define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width
35 36
36const struct rxvt_fallback_font { 37const struct rxvt_fallback_font {
37 codeset cs; 38 codeset cs;
130 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" }, 131 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
131 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, 132 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
132 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" }, 133 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
133 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 134 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
134#if XFT 135#if XFT
135 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true"}, 136 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true" },
136 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" }, 137 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" },
137 { CS_UNICODE, "xft:Andale Mono:antialias=false" }, 138 { CS_UNICODE, "xft:Andale Mono:antialias=false:autohint=false" },
138 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false" }, 139 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" },
139 140
140 // FreeMono is usually uglier than x fonts, so try last only. 141 // FreeMono is usually uglier than x fonts, so try last only.
141 { CS_UNICODE, "xft:FreeMono:autohint=true" }, 142 { CS_UNICODE, "xft:FreeMono:autohint=true" },
142#endif 143#endif
143 144
276 rxvt_fontprop properties () 277 rxvt_fontprop properties ()
277 { 278 {
278 rxvt_fontprop p; 279 rxvt_fontprop p;
279 280
280 p.width = p.height = 1; 281 p.width = p.height = 1;
282 p.ascent = rxvt_fontprop::unset;
281 p.weight = rxvt_fontprop::medium; 283 p.weight = rxvt_fontprop::medium;
282 p.slant = rxvt_fontprop::roman; 284 p.slant = rxvt_fontprop::roman;
283 285
284 return p; 286 return p;
285 } 287 }
305 return false; 307 return false;
306 308
307 if (unicode <= 0x009f) 309 if (unicode <= 0x009f)
308 return true; 310 return true;
309 311
310 if (unicode >= 0x2500 && unicode <= 0x259f) 312 if (unicode >= 0x2500 && unicode <= 0x259f &&
313 ! OPTION_R (Opt_skipBuiltinGlyphs))
311 return true; 314 return true;
312 315
313 if (IS_COMPOSE (unicode)) 316 if (IS_COMPOSE (unicode))
314 return true; 317 return true;
315 318
331void 334void
332rxvt_font_default::draw (rxvt_drawable &d, int x, int y, 335rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
333 const text_t *text, int len, 336 const text_t *text, int len,
334 int fg, int bg) 337 int fg, int bg)
335{ 338{
339 Display *disp = d.display->display;
340
336 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 341 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
337 342
338 XSetForeground (d.display->display, TGC, r->pix_colors[fg]); 343 XSetForeground (disp, TGC, r->pix_colors[fg]);
339 344
340 while (len) 345 while (len)
341 { 346 {
342#if ENABLE_COMBINING 347#if ENABLE_COMBINING
343 compose_char *cc; 348 compose_char *cc;
347 352
348 while (++text, --len && *text == NOCHAR) 353 while (++text, --len && *text == NOCHAR)
349 ; 354 ;
350 355
351 int width = text - tp; 356 int width = text - tp;
352 int fwidth = r->TermWin.fwidth * width; 357 int fwidth = r->fwidth * width;
353 358
354 if (0x2500 <= t && t <= 0x259f) 359 if (0x2500 <= t && t <= 0x259f &&
360 ! OPTION_R (Opt_skipBuiltinGlyphs))
355 { 361 {
356 uint16_t offs = linedraw_offs[t - 0x2500]; 362 uint16_t offs = linedraw_offs[t - 0x2500];
357 uint32_t *a = linedraw_command + (offs >> 4); 363 uint32_t *a = linedraw_command + (offs >> 4);
358 uint32_t *b = a + (offs & 15); 364 uint32_t *b = a + (offs & 15);
359 365
360 int W = fwidth; 366 int W = fwidth;
361 int H = r->TermWin.fheight; 367 int H = r->fheight;
362 368
363 int x_[16]; 369 int x_[16];
364 int y_[16]; 370 int y_[16];
365 371
366 for (int i = 0; i <= 8; i++) 372 for (int i = 0; i <= 8; i++)
374 380
375 XGCValues gcv; 381 XGCValues gcv;
376 382
377 gcv.cap_style = CapButt; 383 gcv.cap_style = CapButt;
378 gcv.line_width = 0; 384 gcv.line_width = 0;
379 XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv); 385 XChangeGC (disp, TGC, GCLineWidth | GCCapStyle, &gcv);
380 386
381 while (a < b) 387 while (a < b)
382 { 388 {
383 uint32_t command = *a++; 389 uint32_t command = *a++;
384 390
391 int y2 = y_[(command >> 0) & 15]; 397 int y2 = y_[(command >> 0) & 15];
392 398
393 switch (op) 399 switch (op)
394 { 400 {
395 case 0: // line 401 case 0: // line
396 XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2); 402 XDrawLine (disp, d, TGC, x1, y1, x2, y2);
397 break; 403 break;
398 404
399 case 1: // rectangle, possibly stippled 405 case 1: // rectangle, possibly stippled
400 if (a) 406 if (a)
401 { 407 {
402 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 }; 408 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
403 409
404 gcv.fill_style = FillStippled; 410 gcv.fill_style = FillStippled;
405 gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2); 411 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
406 gcv.ts_x_origin = x; 412 gcv.ts_x_origin = x;
407 gcv.ts_y_origin = y; 413 gcv.ts_y_origin = y;
408 414
409 XChangeGC (d.display->display, TGC, 415 XChangeGC (disp, TGC,
410 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, 416 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
411 &gcv); 417 &gcv);
412 } 418 }
413 419
414 XFillRectangle (d.display->display, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1); 420 XFillRectangle (disp, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
415 421
416 if (a) 422 if (a)
417 { 423 {
418 XFreePixmap (d.display->display, gcv.stipple); 424 XFreePixmap (disp, gcv.stipple);
419 gcv.stipple = 0; 425 gcv.stipple = 0;
420 gcv.fill_style = FillSolid; 426 gcv.fill_style = FillSolid;
421 XChangeGC (d.display->display, TGC, GCFillStyle, &gcv); 427 XChangeGC (disp, TGC, GCFillStyle, &gcv);
422 } 428 }
423 break; 429 break;
424 case 2: // arc 430 case 2: // arc
425 XDrawArc (d.display->display, d, TGC, 431 XDrawArc (disp, d, TGC,
426 x1 - W/2, y1 - H/2, W-1, H-1, 432 x1 - W/2, y1 - H/2, W-1, H-1,
427 (a - 1) * 90*64, (b - 1) * 90*64); 433 (a - 1) * 90*64, (b - 1) * 90*64);
428 break; 434 break;
429 } 435 }
430 } 436 }
461 case ZERO_WIDTH_CHAR: 467 case ZERO_WIDTH_CHAR:
462 case NOCHAR: 468 case NOCHAR:
463 break; 469 break;
464 470
465 default: 471 default:
466 XDrawRectangle (d.display->display, d, TGC, x + 2, y + 2, 472 XDrawRectangle (disp, d, TGC, x + 2, y + 2,
467 fwidth - 4, r->TermWin.fheight - 4); 473 fwidth - 4, r->fheight - 4);
468 } 474 }
469 475
470 x += fwidth; 476 x += fwidth;
471 } 477 }
472} 478}
521bool 527bool
522rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth) 528rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
523{ 529{
524 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2; 530 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
525 p.height = height; 531 p.height = height;
532 p.ascent = rxvt_fontprop::unset;
526 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium; 533 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; 534 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
528 535
529 return true; 536 return true;
530} 537}
550 557
551 set_properties (p, height, weight, slant, avgwidth); 558 set_properties (p, height, weight, slant, avgwidth);
552 559
553 free (weight); 560 free (weight);
554 free (slant); 561 free (slant);
562
563 p.ascent = f->ascent;
555 564
556 return true; 565 return true;
557} 566}
558 567
559bool 568bool
630} 639}
631 640
632bool 641bool
633rxvt_font_x11::load (const rxvt_fontprop &prop) 642rxvt_font_x11::load (const rxvt_fontprop &prop)
634{ 643{
644 Display *disp = DISPLAY;
645
635 clear (); 646 clear ();
636 647
637 char field_str[64]; // enough for 128 bits 648 char field_str[64]; // enough for 128 bits
638 649
639 // first morph the font if required 650 // first morph the font if required
642 { 653 {
643 char fname[1024]; 654 char fname[1024];
644 655
645 if (name[0] != '-') 656 if (name[0] != '-')
646 { 657 {
647 f = XLoadQueryFont (DISPLAY, name); 658 f = XLoadQueryFont (disp, name);
648 659
649 if (!f) 660 if (!f)
650 return false; 661 return false;
651 662
652 char *new_name = get_property (f, "FONT", name); 663 char *new_name = get_property (f, "FONT", name);
654 if (new_name) 665 if (new_name)
655 set_name (new_name); 666 set_name (new_name);
656 else 667 else
657 rxvt_warn ("font '%s' has no FONT property, continuing without.", name); 668 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
658 669
659 XFreeFont (DISPLAY, f); 670 XFreeFont (disp, f);
660 f = 0; 671 f = 0;
661 } 672 }
662 673
663 if (prop.weight != rxvt_fontprop::unset) 674 if (prop.weight != rxvt_fontprop::unset)
664 { 675 {
694 ~font_weight () { free (name); } 705 ~font_weight () { free (name); }
695 }; 706 };
696 707
697 char **list; 708 char **list;
698 int count; 709 int count;
699 list = XListFonts (DISPLAY, name, 4000, &count); 710 list = XListFonts (disp, name, 4000, &count);
700 711
701 set_name (0); 712 set_name (0);
702 713
703 if (!list) 714 if (!list)
704 return false; 715 return false;
712 723
713 int diff = 0; 724 int diff = 0;
714 725
715 if (replace_field (fname, list[i], 6, '0', field_str)) 726 if (replace_field (fname, list[i], 6, '0', field_str))
716 diff += 10; // slightly penalize scalable fonts 727 diff += 10; // slightly penalize scalable fonts
728 else if (replace_field (fname, list[i], 11, '0', "0"))
729 diff += 300; // more heavily penalize what looks like scaled bitmap fotns
717 730
718 if (!set_properties (p, fname)) 731 if (!set_properties (p, fname))
719 continue; 732 continue;
720 733
721 if (prop.height != rxvt_fontprop::unset 734 if (prop.height != rxvt_fontprop::unset
737 for (;;) 750 for (;;)
738 { 751 {
739 font_weight *best = fonts + count - 1; 752 font_weight *best = fonts + count - 1;
740 753
741 for (font_weight *w = best; w-- > fonts; ) 754 for (font_weight *w = best; w-- > fonts; )
742 if (w->diff < best->diff) 755 if (w->diff <= best->diff)
743 best = w; 756 best = w;
744 757
745 if (!best->name 758 if (!best->name
746 || !(f = XLoadQueryFont (DISPLAY, best->name))) 759 || !(f = XLoadQueryFont (disp, best->name)))
747 break; 760 break;
748 761
749 set_name (best->name); 762 set_name (best->name);
750 best->clear (); 763 best->clear ();
751 764
845 858
846 XCharStruct g; 859 XCharStruct g;
847 int dir_ret, asc_ret, des_ret; 860 int dir_ret, asc_ret, des_ret;
848 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g); 861 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
849 862
850 int wcw = wcwidth (*t); if (wcw > 0) g.width = g.width / wcw; 863 int wcw = wcwidth (*t); if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
851 864
852 if (width < g.width) width = g.width; 865 if (width < g.width) width = g.width;
853 } 866 }
854 867
855 if (cs == CS_UNKNOWN) 868 if (cs == CS_UNKNOWN)
930 return true; 943 return true;
931 944
932 // check character against base font bounding box 945 // check character against base font bounding box
933 int w = xcs->width; 946 int w = xcs->width;
934 int wcw = wcwidth (unicode); 947 int wcw = wcwidth (unicode);
935 if (wcw > 0) w /= wcw; 948 if (wcw > 0) w = (w + wcw - 1) / wcw;
936 949
937 careful = w > prop->width; 950 careful = w > prop->width;
938 if (careful && w > prop->width * MAX_OVERLAP >> 2) 951 if (careful && w > prop->width * MAX_OVERLAP >> 2)
939 return false; 952 return false;
940 953
950 // and it is a mess /. 963 // and it is a mess /.
951 // yet we are trying to be perfect /. 964 // yet we are trying to be perfect /.
952 // but the result still isn't perfect /. 965 // but the result still isn't perfect /.
953 966
954 bool slow = this->slow 967 bool slow = this->slow
955 || width != r->TermWin.fwidth 968 || width != r->fwidth
956 || height != r->TermWin.fheight; 969 || height != r->fheight;
957 970
958 int base = ascent; // sorry, incorrect: r->TermWin.fbase; 971 int base = ascent; // sorry, incorrect: r->fbase;
959 972
960 XGCValues v; 973 XGCValues v;
961 v.foreground = r->pix_colors[fg]; 974 v.foreground = r->pix_colors[fg];
962 v.font = f->fid; 975 v.font = f->fid;
963 976
971 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 984 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
972 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len); 985 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
973 } 986 }
974 else 987 else
975 { 988 {
976 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 989 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
977 990
978 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 991 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
979 992
980 if (slow) 993 if (slow)
981 { 994 {
982 do 995 do
983 { 996 {
984 if (xc->byte1 || xc->byte2) 997 if (xc->byte1 || xc->byte2)
985 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1); 998 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
986 999
987 x += r->TermWin.fwidth; 1000 x += r->fwidth;
988 xc++; len--; 1001 xc++; len--;
989 } 1002 }
990 while (len); 1003 while (len);
991 } 1004 }
992 else 1005 else
1003 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 1016 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
1004 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len); 1017 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
1005 } 1018 }
1006 else 1019 else
1007 { 1020 {
1008 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1021 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
1009 1022
1010 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 1023 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
1011 1024
1012 if (slow) 1025 if (slow)
1013 { 1026 {
1014 do 1027 do
1015 { 1028 {
1016 if (*xc) 1029 if (*xc)
1017 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1); 1030 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
1018 1031
1019 x += r->TermWin.fwidth; 1032 x += r->fwidth;
1020 xc++; len--; 1033 xc++; len--;
1021 } 1034 }
1022 while (len); 1035 while (len);
1023 } 1036 }
1024 else 1037 else
1067 1080
1068 FT_Face face = XftLockFace (f); 1081 FT_Face face = XftLockFace (f);
1069 1082
1070 p.width = width; 1083 p.width = width;
1071 p.height = height; 1084 p.height = height;
1085 p.ascent = ascent;
1072 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD 1086 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
1073 ? rxvt_fontprop::bold : rxvt_fontprop::medium; 1087 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
1074 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC 1088 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
1075 ? rxvt_fontprop::italic : rxvt_fontprop::roman; 1089 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
1076 1090
1080} 1094}
1081 1095
1082bool 1096bool
1083rxvt_font_xft::load (const rxvt_fontprop &prop) 1097rxvt_font_xft::load (const rxvt_fontprop &prop)
1084{ 1098{
1099 Display *disp = DISPLAY;
1100
1085 clear (); 1101 clear ();
1086 1102
1087 FcPattern *p = FcNameParse ((FcChar8 *) name); 1103 FcPattern *p = FcNameParse ((FcChar8 *) name);
1088 1104
1089 if (!p) 1105 if (!p)
1090 return false; 1106 return false;
1091 1107
1092 FcValue v; 1108 FcValue v;
1093 1109
1094 if (prop.height != rxvt_fontprop::unset 1110 if (prop.height != rxvt_fontprop::unset
1095 || (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch 1111 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1096 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch)) 1112 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
1097 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); 1113 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1098 1114
1099 if (prop.weight != rxvt_fontprop::unset 1115 if (prop.weight != rxvt_fontprop::unset
1100 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) 1116 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
1115 1131
1116 // store generated name so iso14755 view gives better results 1132 // store generated name so iso14755 view gives better results
1117 set_name ((char *)FcNameUnparse (p)); 1133 set_name ((char *)FcNameUnparse (p));
1118 1134
1119 XftResult result; 1135 XftResult result;
1120 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1136 FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result);
1121 1137
1122 FcPatternDestroy (p); 1138 FcPatternDestroy (p);
1123 1139
1124 if (!match) 1140 if (!match)
1125 return false; 1141 return false;
1127 int ftheight = 0; 1143 int ftheight = 0;
1128 bool success = true; 1144 bool success = true;
1129 1145
1130 for (;;) 1146 for (;;)
1131 { 1147 {
1132 f = XftFontOpenPattern (DISPLAY, FcPatternDuplicate (match)); 1148 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
1133 1149
1134 if (!f) 1150 if (!f)
1135 { 1151 {
1136 success = false; 1152 success = false;
1137 break; 1153 break;
1146 1162
1147 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE; 1163 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1148 1164
1149 XftUnlockFace (f); 1165 XftUnlockFace (f);
1150 1166
1167 int glheight = height;
1168
1151 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; ) 1169 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1152 { 1170 {
1153 FcChar16 ch = *t; 1171 FcChar16 ch = *t;
1154 1172
1155 if (cs != CS_UNICODE 1173 if (cs != CS_UNICODE
1161 bool careful; 1179 bool careful;
1162 if (!has_char (*t, &prop, careful)) 1180 if (!has_char (*t, &prop, careful))
1163 continue; 1181 continue;
1164 1182
1165 XGlyphInfo g; 1183 XGlyphInfo g;
1166 XftTextExtents16 (DISPLAY, f, &ch, 1, &g); 1184 XftTextExtents16 (disp, f, &ch, 1, &g);
1185
1186 g.width -= g.x;
1167 1187
1168 int wcw = wcwidth (ch); 1188 int wcw = wcwidth (ch);
1169 if (wcw > 0) g.width = g.width / wcw; 1189 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1170 1190
1171 if (width < g.width) width = g.width; 1191 if (width < g.width ) width = g.width;
1172 if (height < g.height) height = g.height; 1192 if (height < g.height ) height = g.height;
1193 if (glheight < g.height - g.y) glheight = g.height - g.y;
1194 }
1195
1196 if (!width)
1197 {
1198 rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name);
1199
1200 XftFontClose (disp, f);
1201 f = 0;
1202
1203 success = false;
1204 break;
1173 } 1205 }
1174 1206
1175 if (prop.height == rxvt_fontprop::unset 1207 if (prop.height == rxvt_fontprop::unset
1176 || height <= prop.height 1208 || (height <= prop.height && glheight <= prop.height)
1177 || height <= 2 1209 || height <= 2
1178 || !scalable) 1210 || !scalable)
1179 break; 1211 break;
1180 1212
1181 if (ftheight) 1213 if (ftheight)
1188 ftheight -= height - prop.height; 1220 ftheight -= height - prop.height;
1189 } 1221 }
1190 else 1222 else
1191 ftheight = prop.height - 1; 1223 ftheight = prop.height - 1;
1192 1224
1193 XftFontClose (DISPLAY, f); 1225 XftFontClose (disp, f);
1194 FcPatternDel (match, FC_PIXEL_SIZE); 1226 FcPatternDel (match, FC_PIXEL_SIZE);
1195 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); 1227 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1196 } 1228 }
1197 1229
1198 FcPatternDestroy (match); 1230 FcPatternDestroy (match);
1199 1231
1200#if 0 // do it per-character 1232#if 0 // do it per-character
1222 // check character against base font bounding box 1254 // check character against base font bounding box
1223 FcChar32 ch = unicode; 1255 FcChar32 ch = unicode;
1224 XGlyphInfo g; 1256 XGlyphInfo g;
1225 XftTextExtents32 (DISPLAY, f, &ch, 1, &g); 1257 XftTextExtents32 (DISPLAY, f, &ch, 1, &g);
1226 1258
1227 int w = g.width; 1259 int w = g.width - g.x;
1228 int wcw = wcwidth (unicode); 1260 int wcw = wcwidth (unicode);
1229 if (wcw > 0) w /= wcw; 1261 if (wcw > 0) w = (w + wcw - 1) / wcw;
1230 1262
1231 careful = w > prop->width; 1263 careful = w > prop->width;
1232 if (careful && w > prop->width * MAX_OVERLAP >> 2) 1264 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1233 return false; 1265 return false;
1234 1266
1238void 1270void
1239rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1271rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1240 const text_t *text, int len, 1272 const text_t *text, int len,
1241 int fg, int bg) 1273 int fg, int bg)
1242{ 1274{
1243 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1275 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
1244 1276
1245 int base = ascent; // should be fbase, but that is incorrect 1277 int base = ascent; // should be fbase, but that is incorrect
1246 1278
1247 XGlyphInfo extents; 1279 XGlyphInfo extents;
1248 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32)); 1280 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32));
1249 FcChar32 *ep = enc; 1281 FcChar32 *ep = enc;
1250 int ewidth = 0; 1282 int ewidth = 0;
1251 int xoff = 0;
1252 1283
1253 while (len) 1284 while (len)
1254 { 1285 {
1255 int cwidth = r->TermWin.fwidth; 1286 int cwidth = r->fwidth;
1256 FcChar32 fc = *text++; len--; 1287 FcChar32 fc = *text++; len--;
1257 FT_UInt gl;
1258 1288
1259 while (len && *text == NOCHAR) 1289 while (len && *text == NOCHAR)
1260 text++, len--, cwidth += r->TermWin.fwidth; 1290 text++, len--, cwidth += r->fwidth;
1261 1291
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 1292 if (fc == ' ' && ep == enc) // skip leading spaces
1279 {
1280 x += cwidth; 1293 x += cwidth;
1281 continue;
1282 }
1283
1284 else 1294 else
1285 { 1295 {
1296 FT_UInt gl = XftCharIndex (d.display->display, f, fc);
1297 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1298
1299 if (extents.xOff != cwidth)
1300 {
1301 if (ewidth)
1302 {
1303 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1304 x, y + base, enc, ep - enc);
1305 x += ewidth;
1306
1307 ep = enc;
1308 ewidth = 0;
1309 }
1310
1311 if (extents.xOff > cwidth)
1312 extents.xOff = cwidth;
1313
1314 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1315 x + (cwidth - extents.xOff >> 1),
1316 y + base, &gl, 1);
1317 x += cwidth;
1318 }
1319 else
1320 {
1286 *ep++ = gl; 1321 *ep++ = gl;
1287 ewidth += cwidth; 1322 ewidth += cwidth;
1288 xoff += extents.xOff; 1323 }
1289 } 1324 }
1290 } 1325 }
1291 1326
1292 if (ep != enc) 1327 if (ep != enc)
1293 {
1294 if (xoff > ewidth) xoff = ewidth;
1295 XftDrawGlyphs (d, &r->pix_colors[fg].c, f, 1328 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1296 x + (ewidth - xoff >> 1),
1297 y + base, enc, ep - enc); 1329 x, y + base, enc, ep - enc);
1298 }
1299} 1330}
1300#endif 1331#endif
1301 1332
1302///////////////////////////////////////////////////////////////////////////// 1333/////////////////////////////////////////////////////////////////////////////
1303 1334
1304rxvt_fontset::rxvt_fontset (rxvt_t r) 1335rxvt_fontset::rxvt_fontset (rxvt_t r)
1305: r (r), fontdesc (0) 1336: fontdesc (0), r (r)
1306{ 1337{
1307 clear (); 1338 clear ();
1308} 1339}
1309 1340
1310rxvt_fontset::~rxvt_fontset () 1341rxvt_fontset::~rxvt_fontset ()
1313} 1344}
1314 1345
1315void 1346void
1316rxvt_fontset::clear () 1347rxvt_fontset::clear ()
1317{ 1348{
1318 prop.width = prop.height = prop.weight = prop.slant 1349 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1319 = rxvt_fontprop::unset; 1350 = rxvt_fontprop::unset;
1320 1351
1321 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) 1352 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1322 FONT_UNREF (*i); 1353 FONT_UNREF (*i);
1323 1354
1375 char buf[512]; 1406 char buf[512];
1376 const char *end; 1407 const char *end;
1377 1408
1378 do 1409 do
1379 { 1410 {
1380 while (*desc <= ' ') desc++; 1411 while (*desc && *desc <= ' ')
1412 desc++;
1381 1413
1382 codeset cs = CS_UNICODE; 1414 codeset cs = CS_UNICODE;
1383 1415
1384 if (*desc == '[') 1416 if (*desc == '[')
1385 { 1417 {
1495 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1527 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1496 goto next_font; 1528 goto next_font;
1497 1529
1498 if (!realize_font (i)) 1530 if (!realize_font (i))
1499 goto next_font; 1531 goto next_font;
1532
1533 if (prop.ascent != rxvt_fontprop::unset)
1534 max_it (f->ascent, prop.ascent);
1500 } 1535 }
1501 1536
1502 if (f->cs == CS_UNKNOWN) 1537 if (f->cs == CS_UNKNOWN)
1503 goto next_font; 1538 goto next_font;
1504 1539
1543 FcPatternAddInteger (p, FC_SLANT, prop.slant); 1578 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1544 FcPatternAddBool (p, FC_MINSPACE, 1); 1579 FcPatternAddBool (p, FC_MINSPACE, 1);
1545 //FcPatternAddBool (p, FC_ANTIALIAS, 1); 1580 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1546 1581
1547 XftResult result; 1582 XftResult result;
1548 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1583 FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result);
1549 1584
1550 FcPatternDestroy (p); 1585 FcPatternDestroy (p);
1551 1586
1552 if (match) 1587 if (match)
1553 { 1588 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines