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.78 by root, Fri Jan 6 05:37:59 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-2006 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
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
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
144 { CS_UNKNOWN, 0 } 145 { CS_UNKNOWN, 0 }
145}; 146};
146 147
147// these characters are used to guess the font height and width 148// these characters are used to guess the font height and width
148// pango uses a similar algorithm and eosn't trust the font either. 149// pango uses a similar algorithm and doesn't trust the font either.
149static uint16_t extent_test_chars[] = { 150static uint16_t extent_test_chars[] = {
150 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_', 151 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_',
151 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ 152 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ
152 0x304c, 0x672c, // が本 153 0x304c, 0x672c, // が本
153}; 154};
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}
531 538
532bool 539bool
533rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) 540rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
534{ 541{
535 unsigned long height; 542 unsigned long height;
543
544#if 0
536 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) 545 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
537 return false; 546 return false;
547#else
548 height = f->ascent + f->descent;
549#endif
538 550
539 unsigned long avgwidth; 551 unsigned long avgwidth;
540 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) 552 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
541 avgwidth = 0; 553 avgwidth = 0;
542 554
545 557
546 set_properties (p, height, weight, slant, avgwidth); 558 set_properties (p, height, weight, slant, avgwidth);
547 559
548 free (weight); 560 free (weight);
549 free (slant); 561 free (slant);
562
563 p.ascent = f->ascent;
550 564
551 return true; 565 return true;
552} 566}
553 567
554bool 568bool
625} 639}
626 640
627bool 641bool
628rxvt_font_x11::load (const rxvt_fontprop &prop) 642rxvt_font_x11::load (const rxvt_fontprop &prop)
629{ 643{
644 Display *disp = DISPLAY;
645
630 clear (); 646 clear ();
631 647
632 char field_str[64]; // enough for 128 bits 648 char field_str[64]; // enough for 128 bits
633 649
634 // first morph the font if required 650 // first morph the font if required
637 { 653 {
638 char fname[1024]; 654 char fname[1024];
639 655
640 if (name[0] != '-') 656 if (name[0] != '-')
641 { 657 {
642 f = XLoadQueryFont (DISPLAY, name); 658 f = XLoadQueryFont (disp, name);
643 659
644 if (!f) 660 if (!f)
645 return false; 661 return false;
646 662
647 char *new_name = get_property (f, "FONT", name); 663 char *new_name = get_property (f, "FONT", name);
649 if (new_name) 665 if (new_name)
650 set_name (new_name); 666 set_name (new_name);
651 else 667 else
652 rxvt_warn ("font '%s' has no FONT property, continuing without.", name); 668 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
653 669
654 XFreeFont (DISPLAY, f); 670 XFreeFont (disp, f);
655 f = 0; 671 f = 0;
656 } 672 }
657 673
658 if (prop.weight != rxvt_fontprop::unset) 674 if (prop.weight != rxvt_fontprop::unset)
659 { 675 {
670 ? "r" : "i"); // TODO: handle "o"blique, too 686 ? "r" : "i"); // TODO: handle "o"blique, too
671 set_name (strdup (fname)); 687 set_name (strdup (fname));
672 } 688 }
673 } 689 }
674 690
691 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
692 ? 0 : prop.height);
693
694 struct font_weight {
695 char *name;
696 int diff;
697
698 void clear ()
699 {
700 name = 0;
701 diff = 0x7fffffff;
702 }
703
704 font_weight () { clear (); }
705 ~font_weight () { free (name); }
706 };
707
675 char **list; 708 char **list;
676 int count; 709 int count;
677 list = XListFonts (DISPLAY, name, 1024, &count); 710 list = XListFonts (disp, name, 4000, &count);
678 711
679 set_name (0); 712 set_name (0);
680 713
681 if (!list) 714 if (!list)
682 return false; 715 return false;
683 716
684 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset 717 font_weight *fonts = new font_weight[count];
685 ? 0 : prop.height);
686 718
687 int bestdiff = 0x7fffffff;
688 for (int i = 0; i < count; i++) 719 for (int i = 0; i < count; i++)
689 { 720 {
690 rxvt_fontprop p; 721 rxvt_fontprop p;
691 char fname[1024]; 722 char fname[1024];
692 723
693 int diff = 0; 724 int diff = 0;
694 725
695 if (replace_field (fname, list[i], 6, '0', field_str)) 726 if (replace_field (fname, list[i], 6, '0', field_str))
696 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
697 730
698 if (!set_properties (p, fname)) 731 if (!set_properties (p, fname))
699 continue; 732 continue;
700 733
701 if (prop.height != rxvt_fontprop::unset 734 if (prop.height != rxvt_fontprop::unset
705 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128; 738 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); 739 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); 740 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); 741 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
709 742
710 if (!name // compare against best found so far 743 fonts[i].name = strdup (fname);
711 || diff < bestdiff) 744 fonts[i].diff = diff;
712 {
713 set_name (strdup (fname));
714 bestdiff = diff;
715 }
716 } 745 }
717 746
718 XFreeFontNames (list); 747 XFreeFontNames (list);
719 748
720 if (!name) 749 // this loop only iterates when the guessed font-size is too small
721 return false; 750 for (;;)
751 {
752 font_weight *best = fonts + count - 1;
722 753
723 f = XLoadQueryFont (DISPLAY, name); 754 for (font_weight *w = best; w-- > fonts; )
755 if (w->diff <= best->diff)
756 best = w;
757
758 if (!best->name
759 || !(f = XLoadQueryFont (disp, best->name)))
760 break;
761
762 set_name (best->name);
763 best->clear ();
764
765 ascent = f->ascent;
766 descent = f->descent;
767 height = ascent + descent;
768
769 if (prop.height == rxvt_fontprop::unset
770 || height <= prop.height)
771 break; // font is ready for use
772
773 // PIXEL_SIZE small enough, but real height too large
774 clear ();
775 }
776
777 delete [] fonts;
724 778
725 if (!f) 779 if (!f)
726 return false; 780 return false;
727 781
728 char *registry = get_property (f, "CHARSET_REGISTRY", 0); 782 char *registry = get_property (f, "CHARSET_REGISTRY", 0);
756 if (cs == CS_UNICODE) 810 if (cs == CS_UNICODE)
757 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font 811 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
758 812
759 encm = f->min_byte1 != 0 || f->max_byte1 != 0; 813 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
760 enc2b = encm || f->max_char_or_byte2 > 255; 814 enc2b = encm || f->max_char_or_byte2 > 255;
761
762 ascent = f->ascent;
763 descent = f->descent;
764 height = ascent + descent;
765 815
766 slow = false; 816 slow = false;
767 817
768#if 1 // only used for slow detection, TODO optimize 818#if 1 // only used for slow detection, TODO optimize
769 if (f->min_bounds.width == f->max_bounds.width) 819 if (f->min_bounds.width == f->max_bounds.width)
808 858
809 XCharStruct g; 859 XCharStruct g;
810 int dir_ret, asc_ret, des_ret; 860 int dir_ret, asc_ret, des_ret;
811 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g); 861 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
812 862
813 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;
814 864
815 if (width < g.width) width = g.width; 865 if (width < g.width) width = g.width;
816 } 866 }
817 867
818 if (cs == CS_UNKNOWN) 868 if (cs == CS_UNKNOWN)
893 return true; 943 return true;
894 944
895 // check character against base font bounding box 945 // check character against base font bounding box
896 int w = xcs->width; 946 int w = xcs->width;
897 int wcw = wcwidth (unicode); 947 int wcw = wcwidth (unicode);
898 if (wcw > 0) w /= wcw; 948 if (wcw > 0) w = (w + wcw - 1) / wcw;
899 949
900 careful = w > prop->width; 950 careful = w > prop->width;
901 if (careful && w > prop->width * MAX_OVERLAP >> 2) 951 if (careful && w > prop->width * MAX_OVERLAP >> 2)
902 return false; 952 return false;
903 953
913 // and it is a mess /. 963 // and it is a mess /.
914 // yet we are trying to be perfect /. 964 // yet we are trying to be perfect /.
915 // but the result still isn't perfect /. 965 // but the result still isn't perfect /.
916 966
917 bool slow = this->slow 967 bool slow = this->slow
918 || width != r->TermWin.fwidth 968 || width != r->fwidth
919 || height != r->TermWin.fheight; 969 || height != r->fheight;
920 970
921 int base = ascent; // sorry, incorrect: r->TermWin.fbase; 971 int base = ascent; // sorry, incorrect: r->fbase;
922 972
923 XGCValues v; 973 XGCValues v;
924 v.foreground = r->pix_colors[fg]; 974 v.foreground = r->pix_colors[fg];
925 v.font = f->fid; 975 v.font = f->fid;
926 976
934 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 984 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
935 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len); 985 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
936 } 986 }
937 else 987 else
938 { 988 {
939 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);
940 990
941 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 991 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
942 992
943 if (slow) 993 if (slow)
944 { 994 {
945 do 995 do
946 { 996 {
947 if (xc->byte1 || xc->byte2) 997 if (xc->byte1 || xc->byte2)
948 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1); 998 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
949 999
950 x += r->TermWin.fwidth; 1000 x += r->fwidth;
951 xc++; len--; 1001 xc++; len--;
952 } 1002 }
953 while (len); 1003 while (len);
954 } 1004 }
955 else 1005 else
966 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 1016 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
967 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len); 1017 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
968 } 1018 }
969 else 1019 else
970 { 1020 {
971 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);
972 1022
973 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 1023 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
974 1024
975 if (slow) 1025 if (slow)
976 { 1026 {
977 do 1027 do
978 { 1028 {
979 if (*xc) 1029 if (*xc)
980 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1); 1030 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
981 1031
982 x += r->TermWin.fwidth; 1032 x += r->fwidth;
983 xc++; len--; 1033 xc++; len--;
984 } 1034 }
985 while (len); 1035 while (len);
986 } 1036 }
987 else 1037 else
1030 1080
1031 FT_Face face = XftLockFace (f); 1081 FT_Face face = XftLockFace (f);
1032 1082
1033 p.width = width; 1083 p.width = width;
1034 p.height = height; 1084 p.height = height;
1085 p.ascent = ascent;
1035 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD 1086 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
1036 ? rxvt_fontprop::bold : rxvt_fontprop::medium; 1087 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
1037 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC 1088 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
1038 ? rxvt_fontprop::italic : rxvt_fontprop::roman; 1089 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
1039 1090
1043} 1094}
1044 1095
1045bool 1096bool
1046rxvt_font_xft::load (const rxvt_fontprop &prop) 1097rxvt_font_xft::load (const rxvt_fontprop &prop)
1047{ 1098{
1099 Display *disp = DISPLAY;
1100
1048 clear (); 1101 clear ();
1049 1102
1050 FcPattern *p = FcNameParse ((FcChar8 *) name); 1103 FcPattern *p = FcNameParse ((FcChar8 *) name);
1051 1104
1052 if (!p) 1105 if (!p)
1053 return false; 1106 return false;
1054 1107
1055 FcValue v; 1108 FcValue v;
1056 1109
1057 if (prop.height != rxvt_fontprop::unset 1110 if (prop.height != rxvt_fontprop::unset
1058 || (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch 1111 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1059 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch)) 1112 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
1060 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); 1113 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1061 1114
1062 if (prop.weight != rxvt_fontprop::unset 1115 if (prop.weight != rxvt_fontprop::unset
1063 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) 1116 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
1078 1131
1079 // store generated name so iso14755 view gives better results 1132 // store generated name so iso14755 view gives better results
1080 set_name ((char *)FcNameUnparse (p)); 1133 set_name ((char *)FcNameUnparse (p));
1081 1134
1082 XftResult result; 1135 XftResult result;
1083 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1136 FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result);
1084 1137
1085 FcPatternDestroy (p); 1138 FcPatternDestroy (p);
1086 1139
1087 if (!match) 1140 if (!match)
1088 return false; 1141 return false;
1090 int ftheight = 0; 1143 int ftheight = 0;
1091 bool success = true; 1144 bool success = true;
1092 1145
1093 for (;;) 1146 for (;;)
1094 { 1147 {
1095 f = XftFontOpenPattern (DISPLAY, FcPatternDuplicate (match)); 1148 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
1096 1149
1097 if (!f) 1150 if (!f)
1098 { 1151 {
1099 success = false; 1152 success = false;
1100 break; 1153 break;
1109 1162
1110 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE; 1163 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1111 1164
1112 XftUnlockFace (f); 1165 XftUnlockFace (f);
1113 1166
1167 int glheight = height;
1168
1114 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; )
1115 { 1170 {
1116 FcChar16 ch = *t; 1171 FcChar16 ch = *t;
1117 1172
1118 if (cs != CS_UNICODE 1173 if (cs != CS_UNICODE
1124 bool careful; 1179 bool careful;
1125 if (!has_char (*t, &prop, careful)) 1180 if (!has_char (*t, &prop, careful))
1126 continue; 1181 continue;
1127 1182
1128 XGlyphInfo g; 1183 XGlyphInfo g;
1129 XftTextExtents16 (DISPLAY, f, &ch, 1, &g); 1184 XftTextExtents16 (disp, f, &ch, 1, &g);
1185
1186 g.width -= g.x;
1130 1187
1131 int wcw = wcwidth (ch); 1188 int wcw = wcwidth (ch);
1132 if (wcw > 0) g.width = g.width / wcw; 1189 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1133 1190
1134 if (width < g.width) width = g.width; 1191 if (width < g.width ) width = g.width;
1135 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;
1136 } 1205 }
1137 1206
1138 if (prop.height == rxvt_fontprop::unset 1207 if (prop.height == rxvt_fontprop::unset
1139 || height <= prop.height 1208 || (height <= prop.height && glheight <= prop.height)
1140 || height <= 2 1209 || height <= 2
1141 || !scalable) 1210 || !scalable)
1142 break; 1211 break;
1143 1212
1144 if (ftheight) 1213 if (ftheight)
1151 ftheight -= height - prop.height; 1220 ftheight -= height - prop.height;
1152 } 1221 }
1153 else 1222 else
1154 ftheight = prop.height - 1; 1223 ftheight = prop.height - 1;
1155 1224
1156 XftFontClose (DISPLAY, f); 1225 XftFontClose (disp, f);
1157 FcPatternDel (match, FC_PIXEL_SIZE); 1226 FcPatternDel (match, FC_PIXEL_SIZE);
1158 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); 1227 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1159 } 1228 }
1160 1229
1161 FcPatternDestroy (match); 1230 FcPatternDestroy (match);
1162 1231
1163#if 0 // do it per-character 1232#if 0 // do it per-character
1185 // check character against base font bounding box 1254 // check character against base font bounding box
1186 FcChar32 ch = unicode; 1255 FcChar32 ch = unicode;
1187 XGlyphInfo g; 1256 XGlyphInfo g;
1188 XftTextExtents32 (DISPLAY, f, &ch, 1, &g); 1257 XftTextExtents32 (DISPLAY, f, &ch, 1, &g);
1189 1258
1190 int w = g.width; 1259 int w = g.width - g.x;
1191 int wcw = wcwidth (unicode); 1260 int wcw = wcwidth (unicode);
1192 if (wcw > 0) w /= wcw; 1261 if (wcw > 0) w = (w + wcw - 1) / wcw;
1193 1262
1194 careful = w > prop->width; 1263 careful = w > prop->width;
1195 if (careful && w > prop->width * MAX_OVERLAP >> 2) 1264 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1196 return false; 1265 return false;
1197 1266
1201void 1270void
1202rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1271rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1203 const text_t *text, int len, 1272 const text_t *text, int len,
1204 int fg, int bg) 1273 int fg, int bg)
1205{ 1274{
1206 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);
1207 1276
1208 int base = ascent; // should be fbase, but that is incorrect 1277 int base = ascent; // should be fbase, but that is incorrect
1209 1278
1210 XGlyphInfo extents; 1279 XGlyphInfo extents;
1211 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32)); 1280 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32));
1212 FcChar32 *ep = enc; 1281 FcChar32 *ep = enc;
1213 int ewidth = 0; 1282 int ewidth = 0;
1214 int xoff = 0;
1215 1283
1216 while (len) 1284 while (len)
1217 { 1285 {
1218 int cwidth = r->TermWin.fwidth; 1286 int cwidth = r->fwidth;
1219 FcChar32 fc = *text++; len--; 1287 FcChar32 fc = *text++; len--;
1220 FT_UInt gl;
1221 1288
1222 while (len && *text == NOCHAR) 1289 while (len && *text == NOCHAR)
1223 text++, len--, cwidth += r->TermWin.fwidth; 1290 text++, len--, cwidth += r->fwidth;
1224 1291
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 1292 if (fc == ' ' && ep == enc) // skip leading spaces
1242 {
1243 x += cwidth; 1293 x += cwidth;
1244 continue;
1245 }
1246
1247 else 1294 else
1248 { 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 {
1249 *ep++ = gl; 1321 *ep++ = gl;
1250 ewidth += cwidth; 1322 ewidth += cwidth;
1251 xoff += extents.xOff; 1323 }
1252 } 1324 }
1253 } 1325 }
1254 1326
1255 if (ep != enc) 1327 if (ep != enc)
1256 {
1257 if (xoff > ewidth) xoff = ewidth;
1258 XftDrawGlyphs (d, &r->pix_colors[fg].c, f, 1328 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1259 x + (ewidth - xoff >> 1),
1260 y + base, enc, ep - enc); 1329 x, y + base, enc, ep - enc);
1261 }
1262} 1330}
1263#endif 1331#endif
1264 1332
1265///////////////////////////////////////////////////////////////////////////// 1333/////////////////////////////////////////////////////////////////////////////
1266 1334
1267rxvt_fontset::rxvt_fontset (rxvt_t r) 1335rxvt_fontset::rxvt_fontset (rxvt_t r)
1268: r (r), fontdesc (0) 1336: fontdesc (0), r (r)
1269{ 1337{
1270 clear (); 1338 clear ();
1271} 1339}
1272 1340
1273rxvt_fontset::~rxvt_fontset () 1341rxvt_fontset::~rxvt_fontset ()
1276} 1344}
1277 1345
1278void 1346void
1279rxvt_fontset::clear () 1347rxvt_fontset::clear ()
1280{ 1348{
1281 prop.width = prop.height = prop.weight = prop.slant 1349 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1282 = rxvt_fontprop::unset; 1350 = rxvt_fontprop::unset;
1283 1351
1284 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) 1352 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1285 FONT_UNREF (*i); 1353 FONT_UNREF (*i);
1286 1354
1338 char buf[512]; 1406 char buf[512];
1339 const char *end; 1407 const char *end;
1340 1408
1341 do 1409 do
1342 { 1410 {
1343 while (*desc <= ' ') desc++; 1411 while (*desc && *desc <= ' ')
1412 desc++;
1344 1413
1345 codeset cs = CS_UNICODE; 1414 codeset cs = CS_UNICODE;
1346 1415
1347 if (*desc == '[') 1416 if (*desc == '[')
1348 { 1417 {
1458 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1527 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1459 goto next_font; 1528 goto next_font;
1460 1529
1461 if (!realize_font (i)) 1530 if (!realize_font (i))
1462 goto next_font; 1531 goto next_font;
1532
1533 if (prop.ascent != rxvt_fontprop::unset)
1534 max_it (f->ascent, prop.ascent);
1463 } 1535 }
1464 1536
1465 if (f->cs == CS_UNKNOWN) 1537 if (f->cs == CS_UNKNOWN)
1466 goto next_font; 1538 goto next_font;
1467 1539
1506 FcPatternAddInteger (p, FC_SLANT, prop.slant); 1578 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1507 FcPatternAddBool (p, FC_MINSPACE, 1); 1579 FcPatternAddBool (p, FC_MINSPACE, 1);
1508 //FcPatternAddBool (p, FC_ANTIALIAS, 1); 1580 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1509 1581
1510 XftResult result; 1582 XftResult result;
1511 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1583 FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result);
1512 1584
1513 FcPatternDestroy (p); 1585 FcPatternDestroy (p);
1514 1586
1515 if (match) 1587 if (match)
1516 { 1588 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines