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.81 by root, Mon Jan 9 07:19:18 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 !r->option (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)
355 { 360 {
356 uint16_t offs = linedraw_offs[t - 0x2500]; 361 uint16_t offs = linedraw_offs[t - 0x2500];
357 uint32_t *a = linedraw_command + (offs >> 4); 362 uint32_t *a = linedraw_command + (offs >> 4);
358 uint32_t *b = a + (offs & 15); 363 uint32_t *b = a + (offs & 15);
359 364
360 int W = fwidth; 365 int W = fwidth;
361 int H = r->TermWin.fheight; 366 int H = r->fheight;
362 367
363 int x_[16]; 368 int x_[16];
364 int y_[16]; 369 int y_[16];
365 370
366 for (int i = 0; i <= 8; i++) 371 for (int i = 0; i <= 8; i++)
374 379
375 XGCValues gcv; 380 XGCValues gcv;
376 381
377 gcv.cap_style = CapButt; 382 gcv.cap_style = CapButt;
378 gcv.line_width = 0; 383 gcv.line_width = 0;
379 XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv); 384 XChangeGC (disp, TGC, GCLineWidth | GCCapStyle, &gcv);
380 385
381 while (a < b) 386 while (a < b)
382 { 387 {
383 uint32_t command = *a++; 388 uint32_t command = *a++;
384 389
391 int y2 = y_[(command >> 0) & 15]; 396 int y2 = y_[(command >> 0) & 15];
392 397
393 switch (op) 398 switch (op)
394 { 399 {
395 case 0: // line 400 case 0: // line
396 XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2); 401 XDrawLine (disp, d, TGC, x1, y1, x2, y2);
397 break; 402 break;
398 403
399 case 1: // rectangle, possibly stippled 404 case 1: // rectangle, possibly stippled
400 if (a) 405 if (a)
401 { 406 {
402 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 }; 407 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
403 408
404 gcv.fill_style = FillStippled; 409 gcv.fill_style = FillStippled;
405 gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2); 410 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
406 gcv.ts_x_origin = x; 411 gcv.ts_x_origin = x;
407 gcv.ts_y_origin = y; 412 gcv.ts_y_origin = y;
408 413
409 XChangeGC (d.display->display, TGC, 414 XChangeGC (disp, TGC,
410 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, 415 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
411 &gcv); 416 &gcv);
412 } 417 }
413 418
414 XFillRectangle (d.display->display, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1); 419 XFillRectangle (disp, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
415 420
416 if (a) 421 if (a)
417 { 422 {
418 XFreePixmap (d.display->display, gcv.stipple); 423 XFreePixmap (disp, gcv.stipple);
419 gcv.stipple = 0; 424 gcv.stipple = 0;
420 gcv.fill_style = FillSolid; 425 gcv.fill_style = FillSolid;
421 XChangeGC (d.display->display, TGC, GCFillStyle, &gcv); 426 XChangeGC (disp, TGC, GCFillStyle, &gcv);
422 } 427 }
423 break; 428 break;
424 case 2: // arc 429 case 2: // arc
425 XDrawArc (d.display->display, d, TGC, 430 XDrawArc (disp, d, TGC,
426 x1 - W/2, y1 - H/2, W-1, H-1, 431 x1 - W/2, y1 - H/2, W-1, H-1,
427 (a - 1) * 90*64, (b - 1) * 90*64); 432 (a - 1) * 90*64, (b - 1) * 90*64);
428 break; 433 break;
429 } 434 }
430 } 435 }
461 case ZERO_WIDTH_CHAR: 466 case ZERO_WIDTH_CHAR:
462 case NOCHAR: 467 case NOCHAR:
463 break; 468 break;
464 469
465 default: 470 default:
466 XDrawRectangle (d.display->display, d, TGC, x + 2, y + 2, 471 XDrawRectangle (disp, d, TGC, x + 2, y + 2,
467 fwidth - 4, r->TermWin.fheight - 4); 472 fwidth - 4, r->fheight - 4);
468 } 473 }
469 474
470 x += fwidth; 475 x += fwidth;
471 } 476 }
472} 477}
521bool 526bool
522rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth) 527rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
523{ 528{
524 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2; 529 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
525 p.height = height; 530 p.height = height;
531 p.ascent = rxvt_fontprop::unset;
526 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium; 532 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; 533 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
528 534
529 return true; 535 return true;
530} 536}
531 537
532bool 538bool
533rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) 539rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
534{ 540{
535 unsigned long height; 541 unsigned long height;
542
543#if 0
536 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) 544 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
537 return false; 545 return false;
546#else
547 height = f->ascent + f->descent;
548#endif
538 549
539 unsigned long avgwidth; 550 unsigned long avgwidth;
540 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) 551 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
541 avgwidth = 0; 552 avgwidth = 0;
542 553
545 556
546 set_properties (p, height, weight, slant, avgwidth); 557 set_properties (p, height, weight, slant, avgwidth);
547 558
548 free (weight); 559 free (weight);
549 free (slant); 560 free (slant);
561
562 p.ascent = f->ascent;
550 563
551 return true; 564 return true;
552} 565}
553 566
554bool 567bool
625} 638}
626 639
627bool 640bool
628rxvt_font_x11::load (const rxvt_fontprop &prop) 641rxvt_font_x11::load (const rxvt_fontprop &prop)
629{ 642{
643 Display *disp = DISPLAY;
644
630 clear (); 645 clear ();
631 646
632 char field_str[64]; // enough for 128 bits 647 char field_str[64]; // enough for 128 bits
633 648
634 // first morph the font if required 649 // first morph the font if required
637 { 652 {
638 char fname[1024]; 653 char fname[1024];
639 654
640 if (name[0] != '-') 655 if (name[0] != '-')
641 { 656 {
642 f = XLoadQueryFont (DISPLAY, name); 657 f = XLoadQueryFont (disp, name);
643 658
644 if (!f) 659 if (!f)
645 return false; 660 return false;
646 661
647 char *new_name = get_property (f, "FONT", name); 662 char *new_name = get_property (f, "FONT", name);
649 if (new_name) 664 if (new_name)
650 set_name (new_name); 665 set_name (new_name);
651 else 666 else
652 rxvt_warn ("font '%s' has no FONT property, continuing without.", name); 667 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
653 668
654 XFreeFont (DISPLAY, f); 669 XFreeFont (disp, f);
655 f = 0; 670 f = 0;
656 } 671 }
657 672
658 if (prop.weight != rxvt_fontprop::unset) 673 if (prop.weight != rxvt_fontprop::unset)
659 { 674 {
670 ? "r" : "i"); // TODO: handle "o"blique, too 685 ? "r" : "i"); // TODO: handle "o"blique, too
671 set_name (strdup (fname)); 686 set_name (strdup (fname));
672 } 687 }
673 } 688 }
674 689
690 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
691 ? 0 : prop.height);
692
693 struct font_weight {
694 char *name;
695 int diff;
696
697 void clear ()
698 {
699 name = 0;
700 diff = 0x7fffffff;
701 }
702
703 font_weight () { clear (); }
704 ~font_weight () { free (name); }
705 };
706
675 char **list; 707 char **list;
676 int count; 708 int count;
677 list = XListFonts (DISPLAY, name, 1024, &count); 709 list = XListFonts (disp, name, 4000, &count);
678 710
679 set_name (0); 711 set_name (0);
680 712
681 if (!list) 713 if (!list)
682 return false; 714 return false;
683 715
684 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset 716 font_weight *fonts = new font_weight[count];
685 ? 0 : prop.height);
686 717
687 int bestdiff = 0x7fffffff;
688 for (int i = 0; i < count; i++) 718 for (int i = 0; i < count; i++)
689 { 719 {
690 rxvt_fontprop p; 720 rxvt_fontprop p;
691 char fname[1024]; 721 char fname[1024];
692 722
693 int diff = 0; 723 int diff = 0;
694 724
695 if (replace_field (fname, list[i], 6, '0', field_str)) 725 if (replace_field (fname, list[i], 6, '0', field_str))
696 diff += 10; // slightly penalize scalable fonts 726 diff += 10; // slightly penalize scalable fonts
727 else if (replace_field (fname, list[i], 11, '0', "0"))
728 diff += 300; // more heavily penalize what looks like scaled bitmap fotns
697 729
698 if (!set_properties (p, fname)) 730 if (!set_properties (p, fname))
699 continue; 731 continue;
700 732
701 if (prop.height != rxvt_fontprop::unset 733 if (prop.height != rxvt_fontprop::unset
705 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128; 737 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); 738 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); 739 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); 740 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
709 741
710 if (!name // compare against best found so far 742 fonts[i].name = strdup (fname);
711 || diff < bestdiff) 743 fonts[i].diff = diff;
712 {
713 set_name (strdup (fname));
714 bestdiff = diff;
715 }
716 } 744 }
717 745
718 XFreeFontNames (list); 746 XFreeFontNames (list);
719 747
720 if (!name) 748 // this loop only iterates when the guessed font-size is too small
721 return false; 749 for (;;)
750 {
751 font_weight *best = fonts + count - 1;
722 752
723 f = XLoadQueryFont (DISPLAY, name); 753 for (font_weight *w = best; w-- > fonts; )
754 if (w->diff <= best->diff)
755 best = w;
756
757 if (!best->name
758 || !(f = XLoadQueryFont (disp, best->name)))
759 break;
760
761 set_name (best->name);
762 best->clear ();
763
764 ascent = f->ascent;
765 descent = f->descent;
766 height = ascent + descent;
767
768 if (prop.height == rxvt_fontprop::unset
769 || height <= prop.height)
770 break; // font is ready for use
771
772 // PIXEL_SIZE small enough, but real height too large
773 clear ();
774 }
775
776 delete [] fonts;
724 777
725 if (!f) 778 if (!f)
726 return false; 779 return false;
727 780
728 char *registry = get_property (f, "CHARSET_REGISTRY", 0); 781 char *registry = get_property (f, "CHARSET_REGISTRY", 0);
756 if (cs == CS_UNICODE) 809 if (cs == CS_UNICODE)
757 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font 810 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
758 811
759 encm = f->min_byte1 != 0 || f->max_byte1 != 0; 812 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
760 enc2b = encm || f->max_char_or_byte2 > 255; 813 enc2b = encm || f->max_char_or_byte2 > 255;
761
762 ascent = f->ascent;
763 descent = f->descent;
764 height = ascent + descent;
765 814
766 slow = false; 815 slow = false;
767 816
768#if 1 // only used for slow detection, TODO optimize 817#if 1 // only used for slow detection, TODO optimize
769 if (f->min_bounds.width == f->max_bounds.width) 818 if (f->min_bounds.width == f->max_bounds.width)
808 857
809 XCharStruct g; 858 XCharStruct g;
810 int dir_ret, asc_ret, des_ret; 859 int dir_ret, asc_ret, des_ret;
811 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g); 860 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
812 861
813 int wcw = wcwidth (*t); if (wcw > 0) g.width = g.width / wcw; 862 int wcw = wcwidth (*t); if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
814 863
815 if (width < g.width) width = g.width; 864 if (width < g.width) width = g.width;
816 } 865 }
817 866
818 if (cs == CS_UNKNOWN) 867 if (cs == CS_UNKNOWN)
893 return true; 942 return true;
894 943
895 // check character against base font bounding box 944 // check character against base font bounding box
896 int w = xcs->width; 945 int w = xcs->width;
897 int wcw = wcwidth (unicode); 946 int wcw = wcwidth (unicode);
898 if (wcw > 0) w /= wcw; 947 if (wcw > 0) w = (w + wcw - 1) / wcw;
899 948
900 careful = w > prop->width; 949 careful = w > prop->width;
901 if (careful && w > prop->width * MAX_OVERLAP >> 2) 950 if (careful && w > prop->width * MAX_OVERLAP >> 2)
902 return false; 951 return false;
903 952
913 // and it is a mess /. 962 // and it is a mess /.
914 // yet we are trying to be perfect /. 963 // yet we are trying to be perfect /.
915 // but the result still isn't perfect /. 964 // but the result still isn't perfect /.
916 965
917 bool slow = this->slow 966 bool slow = this->slow
918 || width != r->TermWin.fwidth 967 || width != r->fwidth
919 || height != r->TermWin.fheight; 968 || height != r->fheight;
920 969
921 int base = ascent; // sorry, incorrect: r->TermWin.fbase; 970 int base = ascent; // sorry, incorrect: r->fbase;
922 971
923 XGCValues v; 972 XGCValues v;
924 v.foreground = r->pix_colors[fg]; 973 v.foreground = r->pix_colors[fg];
925 v.font = f->fid; 974 v.font = f->fid;
926 975
934 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 983 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
935 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len); 984 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
936 } 985 }
937 else 986 else
938 { 987 {
939 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 988 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
940 989
941 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 990 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
942 991
943 if (slow) 992 if (slow)
944 { 993 {
945 do 994 do
946 { 995 {
947 if (xc->byte1 || xc->byte2) 996 if (xc->byte1 || xc->byte2)
948 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1); 997 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
949 998
950 x += r->TermWin.fwidth; 999 x += r->fwidth;
951 xc++; len--; 1000 xc++; len--;
952 } 1001 }
953 while (len); 1002 while (len);
954 } 1003 }
955 else 1004 else
966 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 1015 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
967 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len); 1016 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
968 } 1017 }
969 else 1018 else
970 { 1019 {
971 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1020 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
972 1021
973 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 1022 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
974 1023
975 if (slow) 1024 if (slow)
976 { 1025 {
977 do 1026 do
978 { 1027 {
979 if (*xc) 1028 if (*xc)
980 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1); 1029 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
981 1030
982 x += r->TermWin.fwidth; 1031 x += r->fwidth;
983 xc++; len--; 1032 xc++; len--;
984 } 1033 }
985 while (len); 1034 while (len);
986 } 1035 }
987 else 1036 else
1030 1079
1031 FT_Face face = XftLockFace (f); 1080 FT_Face face = XftLockFace (f);
1032 1081
1033 p.width = width; 1082 p.width = width;
1034 p.height = height; 1083 p.height = height;
1084 p.ascent = ascent;
1035 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD 1085 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
1036 ? rxvt_fontprop::bold : rxvt_fontprop::medium; 1086 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
1037 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC 1087 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
1038 ? rxvt_fontprop::italic : rxvt_fontprop::roman; 1088 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
1039 1089
1043} 1093}
1044 1094
1045bool 1095bool
1046rxvt_font_xft::load (const rxvt_fontprop &prop) 1096rxvt_font_xft::load (const rxvt_fontprop &prop)
1047{ 1097{
1098 Display *disp = DISPLAY;
1099
1048 clear (); 1100 clear ();
1049 1101
1050 FcPattern *p = FcNameParse ((FcChar8 *) name); 1102 FcPattern *p = FcNameParse ((FcChar8 *) name);
1051 1103
1052 if (!p) 1104 if (!p)
1053 return false; 1105 return false;
1054 1106
1055 FcValue v; 1107 FcValue v;
1056 1108
1057 if (prop.height != rxvt_fontprop::unset 1109 if (prop.height != rxvt_fontprop::unset
1058 || (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch 1110 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1059 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch)) 1111 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
1060 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); 1112 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1061 1113
1062 if (prop.weight != rxvt_fontprop::unset 1114 if (prop.weight != rxvt_fontprop::unset
1063 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) 1115 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
1078 1130
1079 // store generated name so iso14755 view gives better results 1131 // store generated name so iso14755 view gives better results
1080 set_name ((char *)FcNameUnparse (p)); 1132 set_name ((char *)FcNameUnparse (p));
1081 1133
1082 XftResult result; 1134 XftResult result;
1083 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1135 FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result);
1084 1136
1085 FcPatternDestroy (p); 1137 FcPatternDestroy (p);
1086 1138
1087 if (!match) 1139 if (!match)
1088 return false; 1140 return false;
1090 int ftheight = 0; 1142 int ftheight = 0;
1091 bool success = true; 1143 bool success = true;
1092 1144
1093 for (;;) 1145 for (;;)
1094 { 1146 {
1095 f = XftFontOpenPattern (DISPLAY, FcPatternDuplicate (match)); 1147 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
1096 1148
1097 if (!f) 1149 if (!f)
1098 { 1150 {
1099 success = false; 1151 success = false;
1100 break; 1152 break;
1109 1161
1110 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE; 1162 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1111 1163
1112 XftUnlockFace (f); 1164 XftUnlockFace (f);
1113 1165
1166 int glheight = height;
1167
1114 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; ) 1168 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1115 { 1169 {
1116 FcChar16 ch = *t; 1170 FcChar16 ch = *t;
1117 1171
1118 if (cs != CS_UNICODE 1172 if (cs != CS_UNICODE
1124 bool careful; 1178 bool careful;
1125 if (!has_char (*t, &prop, careful)) 1179 if (!has_char (*t, &prop, careful))
1126 continue; 1180 continue;
1127 1181
1128 XGlyphInfo g; 1182 XGlyphInfo g;
1129 XftTextExtents16 (DISPLAY, f, &ch, 1, &g); 1183 XftTextExtents16 (disp, f, &ch, 1, &g);
1184
1185 g.width -= g.x;
1130 1186
1131 int wcw = wcwidth (ch); 1187 int wcw = wcwidth (ch);
1132 if (wcw > 0) g.width = g.width / wcw; 1188 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1133 1189
1134 if (width < g.width) width = g.width; 1190 if (width < g.width ) width = g.width;
1135 if (height < g.height) height = g.height; 1191 if (height < g.height ) height = g.height;
1192 if (glheight < g.height - g.y) glheight = g.height - g.y;
1193 }
1194
1195 if (!width)
1196 {
1197 rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name);
1198
1199 XftFontClose (disp, f);
1200 f = 0;
1201
1202 success = false;
1203 break;
1136 } 1204 }
1137 1205
1138 if (prop.height == rxvt_fontprop::unset 1206 if (prop.height == rxvt_fontprop::unset
1139 || height <= prop.height 1207 || (height <= prop.height && glheight <= prop.height)
1140 || height <= 2 1208 || height <= 2
1141 || !scalable) 1209 || !scalable)
1142 break; 1210 break;
1143 1211
1144 if (ftheight) 1212 if (ftheight)
1151 ftheight -= height - prop.height; 1219 ftheight -= height - prop.height;
1152 } 1220 }
1153 else 1221 else
1154 ftheight = prop.height - 1; 1222 ftheight = prop.height - 1;
1155 1223
1156 XftFontClose (DISPLAY, f); 1224 XftFontClose (disp, f);
1157 FcPatternDel (match, FC_PIXEL_SIZE); 1225 FcPatternDel (match, FC_PIXEL_SIZE);
1158 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); 1226 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1159 } 1227 }
1160 1228
1161 FcPatternDestroy (match); 1229 FcPatternDestroy (match);
1162 1230
1163#if 0 // do it per-character 1231#if 0 // do it per-character
1185 // check character against base font bounding box 1253 // check character against base font bounding box
1186 FcChar32 ch = unicode; 1254 FcChar32 ch = unicode;
1187 XGlyphInfo g; 1255 XGlyphInfo g;
1188 XftTextExtents32 (DISPLAY, f, &ch, 1, &g); 1256 XftTextExtents32 (DISPLAY, f, &ch, 1, &g);
1189 1257
1190 int w = g.width; 1258 int w = g.width - g.x;
1191 int wcw = wcwidth (unicode); 1259 int wcw = wcwidth (unicode);
1192 if (wcw > 0) w /= wcw; 1260 if (wcw > 0) w = (w + wcw - 1) / wcw;
1193 1261
1194 careful = w > prop->width; 1262 careful = w > prop->width;
1195 if (careful && w > prop->width * MAX_OVERLAP >> 2) 1263 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1196 return false; 1264 return false;
1197 1265
1201void 1269void
1202rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1270rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1203 const text_t *text, int len, 1271 const text_t *text, int len,
1204 int fg, int bg) 1272 int fg, int bg)
1205{ 1273{
1206 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1274 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
1207
1208 int base = ascent; // should be fbase, but that is incorrect
1209 1275
1210 XGlyphInfo extents; 1276 XGlyphInfo extents;
1211 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32)); 1277 XftGlyphSpec *enc = (XftGlyphSpec *) get_enc_buf (len * sizeof (XftGlyphSpec));
1212 FcChar32 *ep = enc; 1278 XftGlyphSpec *ep = enc;
1213 int ewidth = 0; 1279
1214 int xoff = 0; 1280 // cut trailing spaces
1281 while (len && text [len - 1] == ' ')
1282 len--;
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
1292 if (fc != ' ') // skip spaces
1293 {
1225 gl = XftCharIndex (d.display->display, f, fc); 1294 FT_UInt gl = XftCharIndex (d.display->display, f, fc);
1226 XftGlyphExtents (d.display->display, f, &gl, 1, &extents); 1295 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1227 1296
1228 if (extents.xOff != cwidth && ep != enc) 1297 ep->glyph = gl;
1298 ep->x = x + (cwidth - extents.xOff >> 1);
1299 ep->y = y + ascent;
1300 ep++;
1229 { 1301 }
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 1302
1236 ep = enc;
1237 ewidth = 0;
1238 xoff = 0;
1239 }
1240
1241 if (fc == ' ' && ep == enc) // skip leading spaces
1242 {
1243 x += cwidth; 1303 x += cwidth;
1244 continue;
1245 }
1246
1247 else
1248 {
1249 *ep++ = gl;
1250 ewidth += cwidth;
1251 xoff += extents.xOff;
1252 }
1253 } 1304 }
1254 1305
1255 if (ep != enc) 1306 if (ep != enc)
1256 {
1257 if (xoff > ewidth) xoff = ewidth;
1258 XftDrawGlyphs (d, &r->pix_colors[fg].c, f, 1307 XftDrawGlyphSpec (d, &r->pix_colors[fg].c, f, enc, ep - enc);
1259 x + (ewidth - xoff >> 1),
1260 y + base, enc, ep - enc);
1261 }
1262} 1308}
1263#endif 1309#endif
1264 1310
1265///////////////////////////////////////////////////////////////////////////// 1311/////////////////////////////////////////////////////////////////////////////
1266 1312
1267rxvt_fontset::rxvt_fontset (rxvt_t r) 1313rxvt_fontset::rxvt_fontset (rxvt_t r)
1268: r (r), fontdesc (0) 1314: fontdesc (0), r (r)
1269{ 1315{
1270 clear (); 1316 clear ();
1271} 1317}
1272 1318
1273rxvt_fontset::~rxvt_fontset () 1319rxvt_fontset::~rxvt_fontset ()
1276} 1322}
1277 1323
1278void 1324void
1279rxvt_fontset::clear () 1325rxvt_fontset::clear ()
1280{ 1326{
1281 prop.width = prop.height = prop.weight = prop.slant 1327 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1282 = rxvt_fontprop::unset; 1328 = rxvt_fontprop::unset;
1283 1329
1284 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) 1330 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1285 FONT_UNREF (*i); 1331 FONT_UNREF (*i);
1286 1332
1338 char buf[512]; 1384 char buf[512];
1339 const char *end; 1385 const char *end;
1340 1386
1341 do 1387 do
1342 { 1388 {
1343 while (*desc <= ' ') desc++; 1389 while (*desc && *desc <= ' ')
1390 desc++;
1344 1391
1345 codeset cs = CS_UNICODE; 1392 codeset cs = CS_UNICODE;
1346 1393
1347 if (*desc == '[') 1394 if (*desc == '[')
1348 { 1395 {
1458 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1505 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1459 goto next_font; 1506 goto next_font;
1460 1507
1461 if (!realize_font (i)) 1508 if (!realize_font (i))
1462 goto next_font; 1509 goto next_font;
1510
1511 if (prop.ascent != rxvt_fontprop::unset)
1512 max_it (f->ascent, prop.ascent);
1463 } 1513 }
1464 1514
1465 if (f->cs == CS_UNKNOWN) 1515 if (f->cs == CS_UNKNOWN)
1466 goto next_font; 1516 goto next_font;
1467 1517
1506 FcPatternAddInteger (p, FC_SLANT, prop.slant); 1556 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1507 FcPatternAddBool (p, FC_MINSPACE, 1); 1557 FcPatternAddBool (p, FC_MINSPACE, 1);
1508 //FcPatternAddBool (p, FC_ANTIALIAS, 1); 1558 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1509 1559
1510 XftResult result; 1560 XftResult result;
1511 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1561 FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result);
1512 1562
1513 FcPatternDestroy (p); 1563 FcPatternDestroy (p);
1514 1564
1515 if (match) 1565 if (match)
1516 { 1566 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines