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.41 by root, Wed Aug 25 02:33:09 2004 UTC vs.
Revision 1.73 by root, Mon Nov 28 19:35:04 2005 UTC

1/*--------------------------------*-C-*---------------------------------* 1/*--------------------------------*-C-*---------------------------------*
2 * File: defaultfont.C 2 * File: rxvtfont.C
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> 4 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
5 * - original version. 5 * - original version.
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details. 15 * GNU General Public License for more details.
16 * 16 *
17 * You should have received a copy of the GNU General Public License 17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software 18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 *---------------------------------------------------------------------*/ 20 *---------------------------------------------------------------------*/
21 21
22#include "../config.h" 22#include "../config.h"
23#include "rxvt.h" 23#include "rxvt.h"
24#include "rxvtutil.h" 24#include "rxvtutil.h"
88 { CS_JIS0201_1976_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0201*-0" }, 88 { CS_JIS0201_1976_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
89 { CS_JIS0208_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0208*-0" }, 89 { CS_JIS0208_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
90 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, 90 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
91#endif 91#endif
92 92
93#if ENCODING_CN || ENCODING_CN_EXT 93#if ENCODING_ZH || ENCODING_ZH_EXT
94# if XFT 94# if XFT
95 { CS_GBK_0, "xft:AR PL KaitiM GB" },
96 { CS_GBK_0, "xft:AR PL SungtiL GB" },
97 { CS_GBK_0, "xft::spacing=100:lang=zh" },
95 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, 98 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
96 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, 99 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
97 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, 100 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
98 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, 101 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
99 { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" }, 102 { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" },
100# endif 103# endif
104 { CS_GBK_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gbk*-0" },
101 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" }, 105 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
102 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" }, 106 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
103 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" }, 107 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
108 { CS_GB2312_1980_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
104 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" }, 109 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
105 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" }, 110 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
106 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" }, 111 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
107 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" }, 112 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
108 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" }, 113 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
125 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" }, 130 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
126 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, 131 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
127 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" }, 132 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
128 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 133 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
129#if XFT 134#if XFT
130 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false"}, 135 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true" },
131 { CS_UNICODE, "xft:Andale Mono:antialias=false" },
132 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false" },
133 { CS_UNICODE, "xft:Courier New:antialias=false" }, 136 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" },
137 { CS_UNICODE, "xft:Andale Mono:antialias=false:autohint=false" },
138 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" },
134 139
135 // FreeMono is usually uglier than x fonts, so try last only. 140 // FreeMono is usually uglier than x fonts, so try last only.
136 { CS_UNICODE, "xft:FreeMono" }, 141 { CS_UNICODE, "xft:FreeMono:autohint=true" },
137#endif 142#endif
138 143
139 { CS_UNKNOWN, 0 } 144 { CS_UNKNOWN, 0 }
140}; 145};
141 146
142// these characters are used to guess the font height and width 147// these characters are used to guess the font height and width
143// pango uses a similar algorithm and eosn't trust the font either. 148// pango uses a similar algorithm and doesn't trust the font either.
144static uint16_t extent_test_chars[] = { 149static uint16_t extent_test_chars[] = {
145 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_', 150 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_',
146 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ 151 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ
147 0x304c, 0x672c, // が本 152 0x304c, 0x672c, // が本
148}; 153};
326void 331void
327rxvt_font_default::draw (rxvt_drawable &d, int x, int y, 332rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
328 const text_t *text, int len, 333 const text_t *text, int len,
329 int fg, int bg) 334 int fg, int bg)
330{ 335{
336 Display *disp = d.display->display;
337
331 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 338 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
332 339
333 XSetForeground (d.display->display, TGC, r->pix_colors[fg]); 340 XSetForeground (disp, TGC, r->pix_colors[fg]);
334 341
335 while (len--) 342 while (len)
336 { 343 {
337#if ENABLE_COMBINING 344#if ENABLE_COMBINING
338 compose_char *cc; 345 compose_char *cc;
339#endif 346#endif
347 const text_t *tp = text;
340 text_t t = *text++; 348 text_t t = *tp;
349
350 while (++text, --len && *text == NOCHAR)
351 ;
352
353 int width = text - tp;
354 int fwidth = r->TermWin.fwidth * width;
341 355
342 if (0x2500 <= t && t <= 0x259f) 356 if (0x2500 <= t && t <= 0x259f)
343 { 357 {
344 uint16_t offs = linedraw_offs[t - 0x2500]; 358 uint16_t offs = linedraw_offs[t - 0x2500];
345 uint32_t *a = linedraw_command + (offs >> 4); 359 uint32_t *a = linedraw_command + (offs >> 4);
346 uint32_t *b = a + (offs & 15); 360 uint32_t *b = a + (offs & 15);
347 361
348 int W = r->TermWin.fwidth; 362 int W = fwidth;
349 int H = r->TermWin.fheight; 363 int H = r->TermWin.fheight;
350 364
351 int x_[16]; 365 int x_[16];
352 int y_[16]; 366 int y_[16];
353 367
362 376
363 XGCValues gcv; 377 XGCValues gcv;
364 378
365 gcv.cap_style = CapButt; 379 gcv.cap_style = CapButt;
366 gcv.line_width = 0; 380 gcv.line_width = 0;
367 XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv); 381 XChangeGC (disp, TGC, GCLineWidth | GCCapStyle, &gcv);
368 382
369 while (a < b) 383 while (a < b)
370 { 384 {
371 uint32_t command = *a++; 385 uint32_t command = *a++;
372 386
379 int y2 = y_[(command >> 0) & 15]; 393 int y2 = y_[(command >> 0) & 15];
380 394
381 switch (op) 395 switch (op)
382 { 396 {
383 case 0: // line 397 case 0: // line
384 XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2); 398 XDrawLine (disp, d, TGC, x1, y1, x2, y2);
385 break; 399 break;
386 400
387 case 1: // rectangle, possibly stippled 401 case 1: // rectangle, possibly stippled
388 if (a) 402 if (a)
389 { 403 {
390 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 }; 404 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
391 405
392 gcv.fill_style = FillStippled; 406 gcv.fill_style = FillStippled;
393 gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2); 407 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
394 gcv.ts_x_origin = x; 408 gcv.ts_x_origin = x;
395 gcv.ts_y_origin = y; 409 gcv.ts_y_origin = y;
396 410
397 XChangeGC (d.display->display, TGC, 411 XChangeGC (disp, TGC,
398 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, 412 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
399 &gcv); 413 &gcv);
400 } 414 }
401 415
402 XFillRectangle (d.display->display, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1); 416 XFillRectangle (disp, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
403 417
404 if (a) 418 if (a)
405 { 419 {
406 XFreePixmap (d.display->display, gcv.stipple); 420 XFreePixmap (disp, gcv.stipple);
407 gcv.stipple = 0; 421 gcv.stipple = 0;
408 gcv.fill_style = FillSolid; 422 gcv.fill_style = FillSolid;
409 XChangeGC (d.display->display, TGC, GCFillStyle, &gcv); 423 XChangeGC (disp, TGC, GCFillStyle, &gcv);
410 } 424 }
411 break; 425 break;
412 case 2: // arc 426 case 2: // arc
413 XDrawArc (d.display->display, d, TGC, 427 XDrawArc (disp, d, TGC,
414 x1 - W/2, y1 - H/2, W-1, H-1, 428 x1 - W/2, y1 - H/2, W-1, H-1,
415 (a - 1) * 90*64, (b - 1) * 90*64); 429 (a - 1) * 90*64, (b - 1) * 90*64);
416 break; 430 break;
417 } 431 }
418 } 432 }
419 } 433 }
420#if ENABLE_COMBINING 434#if ENABLE_COMBINING
421 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t])) 435 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
422 { 436 {
437 text_t chrs[2];
438 width = min (2, width);
439 chrs [1] = NOCHAR;
440
441 *chrs = cc->c1;
423 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)]; 442 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
424 f1->draw (d, x, y, &(t = cc->c1), 1, fg, bg); 443 f1->draw (d, x, y, chrs, width, fg, bg);
444
425 if (cc->c2 != NOCHAR) 445 if (cc->c2 != NOCHAR)
426 { 446 {
447 bool careful;
448
427 // prefer font of first character, for no good reasons 449 // prefer font of first character, for no good reasons
428 bool careful; 450 *chrs = cc->c2;
429 rxvt_font *f2 = f1->has_char (cc->c2, 0, careful) 451 rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful)
430 ? f1 452 ? f1
431 : (*fs)[fs->find_font (cc->c2)]; 453 : (*fs)[fs->find_font (cc->c2)];
432 454
433 f2->draw (d, x, y, &(t = cc->c2), 1, fg, -1); 455 f2->draw (d, x, y, chrs, width, fg, -1);
434 } 456 }
435 } 457 }
436#endif 458#endif
437 else 459 else
438 switch (t) 460 switch (t)
441 case ZERO_WIDTH_CHAR: 463 case ZERO_WIDTH_CHAR:
442 case NOCHAR: 464 case NOCHAR:
443 break; 465 break;
444 466
445 default: 467 default:
446 int w = 0;
447 while (len > 0 && *text == NOCHAR)
448 {
449 ++text;
450 --len;
451 w += r->TermWin.fwidth;
452 }
453
454 XDrawRectangle (d.display->display, d, TGC, x + 2, y + 2, 468 XDrawRectangle (disp, d, TGC, x + 2, y + 2,
455 w + r->TermWin.fwidth - 4, r->TermWin.fheight - 4); 469 fwidth - 4, r->TermWin.fheight - 4);
456 x += w;
457 } 470 }
458 471
459 x += r->TermWin.fwidth; 472 x += fwidth;
460 } 473 }
461} 474}
462 475
463///////////////////////////////////////////////////////////////////////////// 476/////////////////////////////////////////////////////////////////////////////
464 477
520 533
521bool 534bool
522rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) 535rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
523{ 536{
524 unsigned long height; 537 unsigned long height;
538
539#if 0
525 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) 540 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
526 return false; 541 return false;
542#else
543 height = f->ascent + f->descent;
544#endif
527 545
528 unsigned long avgwidth; 546 unsigned long avgwidth;
529 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) 547 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
530 avgwidth = 0; 548 avgwidth = 0;
531 549
614} 632}
615 633
616bool 634bool
617rxvt_font_x11::load (const rxvt_fontprop &prop) 635rxvt_font_x11::load (const rxvt_fontprop &prop)
618{ 636{
637 Display *disp = DISPLAY;
638
619 clear (); 639 clear ();
620 640
621 char field_str[64]; // enough for 128 bits 641 char field_str[64]; // enough for 128 bits
622 642
623 // first morph the font if required 643 // first morph the font if required
626 { 646 {
627 char fname[1024]; 647 char fname[1024];
628 648
629 if (name[0] != '-') 649 if (name[0] != '-')
630 { 650 {
631 f = XLoadQueryFont (DISPLAY, name); 651 f = XLoadQueryFont (disp, name);
632 652
633 if (!f) 653 if (!f)
634 return false; 654 return false;
635 655
636 char *new_name = get_property (f, "FONT", name); 656 char *new_name = get_property (f, "FONT", name);
638 if (new_name) 658 if (new_name)
639 set_name (new_name); 659 set_name (new_name);
640 else 660 else
641 rxvt_warn ("font '%s' has no FONT property, continuing without.", name); 661 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
642 662
643 XFreeFont (DISPLAY, f); 663 XFreeFont (disp, f);
644 f = 0; 664 f = 0;
645 } 665 }
646 666
647 if (prop.weight != rxvt_fontprop::unset) 667 if (prop.weight != rxvt_fontprop::unset)
648 { 668 {
659 ? "r" : "i"); // TODO: handle "o"blique, too 679 ? "r" : "i"); // TODO: handle "o"blique, too
660 set_name (strdup (fname)); 680 set_name (strdup (fname));
661 } 681 }
662 } 682 }
663 683
684 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
685 ? 0 : prop.height);
686
687 struct font_weight {
688 char *name;
689 int diff;
690
691 void clear ()
692 {
693 name = 0;
694 diff = 0x7fffffff;
695 }
696
697 font_weight () { clear (); }
698 ~font_weight () { free (name); }
699 };
700
664 char **list; 701 char **list;
665 int count; 702 int count;
666 list = XListFonts (DISPLAY, name, 1024, &count); 703 list = XListFonts (disp, name, 4000, &count);
667 704
668 set_name (0); 705 set_name (0);
669 706
670 if (!list) 707 if (!list)
671 return false; 708 return false;
672 709
673 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset 710 font_weight *fonts = new font_weight[count];
674 ? 0 : prop.height);
675 711
676 int bestdiff = 0x7fffffff;
677 for (int i = 0; i < count; i++) 712 for (int i = 0; i < count; i++)
678 { 713 {
679 rxvt_fontprop p; 714 rxvt_fontprop p;
680 char fname[1024]; 715 char fname[1024];
681 716
682 int diff = 0; 717 int diff = 0;
683 718
684 if (replace_field (fname, list[i], 6, '0', field_str)) 719 if (replace_field (fname, list[i], 6, '0', field_str))
685 diff += 10; // slightly penalize scalable fonts 720 diff += 10; // slightly penalize scalable fonts
721 else if (replace_field (fname, list[i], 11, '0', "0"))
722 diff += 300; // more heavily penalize what looks like scaled bitmap fotns
686 723
687 if (!set_properties (p, fname)) 724 if (!set_properties (p, fname))
688 continue; 725 continue;
689 726
690 if (prop.height != rxvt_fontprop::unset 727 if (prop.height != rxvt_fontprop::unset
694 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128; 731 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128;
695 if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight); 732 if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight);
696 if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant); 733 if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant);
697 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width); 734 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
698 735
699 if (!name // compare against best found so far 736 fonts[i].name = strdup (fname);
700 || diff < bestdiff) 737 fonts[i].diff = diff;
701 {
702 set_name (strdup (fname));
703 bestdiff = diff;
704 }
705 } 738 }
706 739
707 XFreeFontNames (list); 740 XFreeFontNames (list);
708 741
709 if (!name) 742 // this loop only iterates when the guessed font-size is too small
710 return false; 743 for (;;)
744 {
745 font_weight *best = fonts + count - 1;
711 746
712 f = XLoadQueryFont (DISPLAY, name); 747 for (font_weight *w = best; w-- > fonts; )
748 if (w->diff <= best->diff)
749 best = w;
750
751 if (!best->name
752 || !(f = XLoadQueryFont (disp, best->name)))
753 break;
754
755 set_name (best->name);
756 best->clear ();
757
758 ascent = f->ascent;
759 descent = f->descent;
760 height = ascent + descent;
761
762 if (prop.height == rxvt_fontprop::unset
763 || height <= prop.height)
764 break; // font is ready for use
765
766 // PIXEL_SIZE small enough, but real height too large
767 clear ();
768 }
769
770 delete [] fonts;
713 771
714 if (!f) 772 if (!f)
715 return false; 773 return false;
716 774
717 char *registry = get_property (f, "CHARSET_REGISTRY", 0); 775 char *registry = get_property (f, "CHARSET_REGISTRY", 0);
745 if (cs == CS_UNICODE) 803 if (cs == CS_UNICODE)
746 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font 804 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
747 805
748 encm = f->min_byte1 != 0 || f->max_byte1 != 0; 806 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
749 enc2b = encm || f->max_char_or_byte2 > 255; 807 enc2b = encm || f->max_char_or_byte2 > 255;
750
751 ascent = f->ascent;
752 descent = f->descent;
753 height = ascent + descent;
754 808
755 slow = false; 809 slow = false;
756 810
757#if 1 // only used for slow detection, TODO optimize 811#if 1 // only used for slow detection, TODO optimize
758 if (f->min_bounds.width == f->max_bounds.width) 812 if (f->min_bounds.width == f->max_bounds.width)
797 851
798 XCharStruct g; 852 XCharStruct g;
799 int dir_ret, asc_ret, des_ret; 853 int dir_ret, asc_ret, des_ret;
800 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g); 854 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
801 855
802 int wcw = wcwidth (*t); if (wcw > 0) g.width = g.width / wcw; 856 int wcw = wcwidth (*t); if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
803 857
804 if (width < g.width) width = g.width; 858 if (width < g.width) width = g.width;
805 } 859 }
806 860
807 if (cs == CS_UNKNOWN) 861 if (cs == CS_UNKNOWN)
882 return true; 936 return true;
883 937
884 // check character against base font bounding box 938 // check character against base font bounding box
885 int w = xcs->width; 939 int w = xcs->width;
886 int wcw = wcwidth (unicode); 940 int wcw = wcwidth (unicode);
887 if (wcw > 0) w /= wcw; 941 if (wcw > 0) w = (w + wcw - 1) / wcw;
888 942
889 careful = w > prop->width; 943 careful = w > prop->width;
890 if (careful && w > prop->width * MAX_OVERLAP >> 2) 944 if (careful && w > prop->width * MAX_OVERLAP >> 2)
891 return false; 945 return false;
892 946
1032} 1086}
1033 1087
1034bool 1088bool
1035rxvt_font_xft::load (const rxvt_fontprop &prop) 1089rxvt_font_xft::load (const rxvt_fontprop &prop)
1036{ 1090{
1091 Display *disp = DISPLAY;
1092
1037 clear (); 1093 clear ();
1038 1094
1039 FcPattern *p = FcNameParse ((FcChar8 *) name); 1095 FcPattern *p = FcNameParse ((FcChar8 *) name);
1040 1096
1041 if (!p) 1097 if (!p)
1042 return false; 1098 return false;
1043 1099
1044 FcValue v; 1100 FcValue v;
1045 1101
1046 if (prop.height != rxvt_fontprop::unset 1102 if (prop.height != rxvt_fontprop::unset
1047 && FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch) 1103 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1104 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
1048 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); 1105 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1049 1106
1050 if (prop.weight != rxvt_fontprop::unset 1107 if (prop.weight != rxvt_fontprop::unset
1051 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) 1108 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
1052 FcPatternAddInteger (p, FC_WEIGHT, prop.weight); 1109 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1066 1123
1067 // store generated name so iso14755 view gives better results 1124 // store generated name so iso14755 view gives better results
1068 set_name ((char *)FcNameUnparse (p)); 1125 set_name ((char *)FcNameUnparse (p));
1069 1126
1070 XftResult result; 1127 XftResult result;
1071 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1128 FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result);
1072 1129
1073 FcPatternDestroy (p); 1130 FcPatternDestroy (p);
1074 1131
1075 if (!match) 1132 if (!match)
1076 return false; 1133 return false;
1078 int ftheight = 0; 1135 int ftheight = 0;
1079 bool success = true; 1136 bool success = true;
1080 1137
1081 for (;;) 1138 for (;;)
1082 { 1139 {
1083 f = XftFontOpenPattern (DISPLAY, FcPatternDuplicate (match)); 1140 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
1084 1141
1085 if (!f) 1142 if (!f)
1086 { 1143 {
1087 success = false; 1144 success = false;
1088 break; 1145 break;
1097 1154
1098 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE; 1155 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1099 1156
1100 XftUnlockFace (f); 1157 XftUnlockFace (f);
1101 1158
1159 int glheight = height;
1160
1102 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; ) 1161 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1103 { 1162 {
1104 FcChar16 ch = *t; 1163 FcChar16 ch = *t;
1105 1164
1106 if (cs != CS_UNICODE 1165 if (cs != CS_UNICODE
1112 bool careful; 1171 bool careful;
1113 if (!has_char (*t, &prop, careful)) 1172 if (!has_char (*t, &prop, careful))
1114 continue; 1173 continue;
1115 1174
1116 XGlyphInfo g; 1175 XGlyphInfo g;
1117 XftTextExtents16 (DISPLAY, f, &ch, 1, &g); 1176 XftTextExtents16 (disp, f, &ch, 1, &g);
1177
1178 g.width -= g.x;
1118 1179
1119 int wcw = wcwidth (ch); 1180 int wcw = wcwidth (ch);
1120 if (wcw > 0) g.width = g.width / wcw; 1181 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1121 1182
1122 if (width < g.width) width = g.width; 1183 if (width < g.width ) width = g.width;
1123 if (height < g.height) height = g.height; 1184 if (height < g.height ) height = g.height;
1185 if (glheight < g.height - g.y) glheight = g.height - g.y;
1124 } 1186 }
1125 1187
1126 if (prop.height == rxvt_fontprop::unset 1188 if (prop.height == rxvt_fontprop::unset
1127 || height <= prop.height 1189 || (height <= prop.height && glheight <= prop.height)
1128 || height <= 2 1190 || height <= 2
1129 || !scalable) 1191 || !scalable)
1130 break; 1192 break;
1131 1193
1132 if (ftheight) 1194 if (ftheight)
1139 ftheight -= height - prop.height; 1201 ftheight -= height - prop.height;
1140 } 1202 }
1141 else 1203 else
1142 ftheight = prop.height - 1; 1204 ftheight = prop.height - 1;
1143 1205
1144 XftFontClose (DISPLAY, f); 1206 XftFontClose (disp, f);
1145 FcPatternDel (match, FC_PIXEL_SIZE); 1207 FcPatternDel (match, FC_PIXEL_SIZE);
1146 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); 1208 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1147 } 1209 }
1148 1210
1149 FcPatternDestroy (match); 1211 FcPatternDestroy (match);
1173 // check character against base font bounding box 1235 // check character against base font bounding box
1174 FcChar32 ch = unicode; 1236 FcChar32 ch = unicode;
1175 XGlyphInfo g; 1237 XGlyphInfo g;
1176 XftTextExtents32 (DISPLAY, f, &ch, 1, &g); 1238 XftTextExtents32 (DISPLAY, f, &ch, 1, &g);
1177 1239
1178 int w = g.width; 1240 int w = g.width - g.x;
1179 int wcw = wcwidth (unicode); 1241 int wcw = wcwidth (unicode);
1180 if (wcw > 0) w /= wcw; 1242 if (wcw > 0) w = (w + wcw - 1) / wcw;
1181 1243
1182 careful = w > prop->width; 1244 careful = w > prop->width;
1183 if (careful && w > prop->width * MAX_OVERLAP >> 2) 1245 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1184 return false; 1246 return false;
1185 1247
1197 1259
1198 XGlyphInfo extents; 1260 XGlyphInfo extents;
1199 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32)); 1261 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32));
1200 FcChar32 *ep = enc; 1262 FcChar32 *ep = enc;
1201 int ewidth = 0; 1263 int ewidth = 0;
1202 int xoff = 0;
1203 1264
1204 while (len) 1265 while (len)
1205 { 1266 {
1206 int cwidth = r->TermWin.fwidth; 1267 int cwidth = r->TermWin.fwidth;
1207 FcChar32 fc = *text++; len--; 1268 FcChar32 fc = *text++; len--;
1208 FT_UInt gl;
1209 1269
1210 while (len && *text == NOCHAR) 1270 while (len && *text == NOCHAR)
1211 text++, len--, cwidth += r->TermWin.fwidth; 1271 text++, len--, cwidth += r->TermWin.fwidth;
1212 1272
1213 gl = XftCharIndex (d.display->display, f, fc);
1214 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1215
1216 if (extents.xOff != cwidth && ep != enc)
1217 {
1218 if (xoff > ewidth) xoff = ewidth;
1219 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1220 x + (ewidth - xoff >> 1),
1221 y + base, enc, ep - enc);
1222 x += ewidth;
1223
1224 ep = enc;
1225 ewidth = 0;
1226 xoff = 0;
1227 }
1228
1229 if (fc == ' ' && ep == enc) // skip leading spaces 1273 if (fc == ' ' && ep == enc) // skip leading spaces
1230 {
1231 x += cwidth; 1274 x += cwidth;
1232 continue;
1233 }
1234
1235 else 1275 else
1236 { 1276 {
1277 FT_UInt gl = XftCharIndex (d.display->display, f, fc);
1278 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1279
1280 if (extents.xOff != cwidth)
1281 {
1282 if (ewidth)
1283 {
1284 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1285 x, y + base, enc, ep - enc);
1286 x += ewidth;
1287
1288 ep = enc;
1289 ewidth = 0;
1290 }
1291
1292 if (extents.xOff > cwidth)
1293 extents.xOff = cwidth;
1294
1295 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1296 x + (cwidth - extents.xOff >> 1),
1297 y + base, &gl, 1);
1298 x += cwidth;
1299 }
1300 else
1301 {
1237 *ep++ = gl; 1302 *ep++ = gl;
1238 ewidth += cwidth; 1303 ewidth += cwidth;
1239 xoff += extents.xOff; 1304 }
1240 } 1305 }
1241 } 1306 }
1242 1307
1243 if (ep != enc) 1308 if (ep != enc)
1244 {
1245 if (xoff > ewidth) xoff = ewidth;
1246 XftDrawGlyphs (d, &r->pix_colors[fg].c, f, 1309 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1247 x + (ewidth - xoff >> 1),
1248 y + base, enc, ep - enc); 1310 x, y + base, enc, ep - enc);
1249 }
1250} 1311}
1251#endif 1312#endif
1252 1313
1253///////////////////////////////////////////////////////////////////////////// 1314/////////////////////////////////////////////////////////////////////////////
1254 1315
1255rxvt_fontset::rxvt_fontset (rxvt_t r) 1316rxvt_fontset::rxvt_fontset (rxvt_t r)
1256: r (r), fontdesc (0) 1317: fontdesc (0), r (r)
1257{ 1318{
1258 clear (); 1319 clear ();
1259} 1320}
1260 1321
1261rxvt_fontset::~rxvt_fontset () 1322rxvt_fontset::~rxvt_fontset ()
1264} 1325}
1265 1326
1266void 1327void
1267rxvt_fontset::clear () 1328rxvt_fontset::clear ()
1268{ 1329{
1330 prop.width = prop.height = prop.weight = prop.slant
1331 = rxvt_fontprop::unset;
1332
1269 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) 1333 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1270 FONT_UNREF (*i); 1334 FONT_UNREF (*i);
1271 1335
1272 for (pagemap **p = fmap.begin (); p != fmap.end (); p++) 1336 for (pagemap **p = fmap.begin (); p != fmap.end (); p++)
1273 delete *p; 1337 delete *p;
1323 char buf[512]; 1387 char buf[512];
1324 const char *end; 1388 const char *end;
1325 1389
1326 do 1390 do
1327 { 1391 {
1328 while (*desc <= ' ') desc++; 1392 while (*desc && *desc <= ' ')
1393 desc++;
1329 1394
1330 codeset cs = CS_UNICODE; 1395 codeset cs = CS_UNICODE;
1331 1396
1332 if (*desc == '[') 1397 if (*desc == '[')
1333 { 1398 {
1393 1458
1394 return true; 1459 return true;
1395} 1460}
1396 1461
1397bool 1462bool
1398rxvt_fontset::populate (const char *desc, const rxvt_fontprop &prop) 1463rxvt_fontset::populate (const char *desc)
1399{ 1464{
1400 clear (); 1465 clear ();
1401 1466
1402 fontdesc = strdup (desc); 1467 fontdesc = strdup (desc);
1403 1468
1404 fonts.push_back (new_font (0, CS_UNICODE)); 1469 fonts.push_back (new_font (0, CS_UNICODE));
1405 realize_font (0); 1470 realize_font (0);
1406
1407 this->prop = prop;
1408 1471
1409 add_fonts (desc); 1472 add_fonts (desc);
1410 1473
1411 return true; 1474 return true;
1412} 1475}
1493 FcPatternAddInteger (p, FC_SLANT, prop.slant); 1556 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1494 FcPatternAddBool (p, FC_MINSPACE, 1); 1557 FcPatternAddBool (p, FC_MINSPACE, 1);
1495 //FcPatternAddBool (p, FC_ANTIALIAS, 1); 1558 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1496 1559
1497 XftResult result; 1560 XftResult result;
1498 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1561 FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result);
1499 1562
1500 FcPatternDestroy (p); 1563 FcPatternDestroy (p);
1501 1564
1502 if (match) 1565 if (match)
1503 { 1566 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines