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.76 by root, Tue Jan 3 23:40:58 2006 UTC

1/*--------------------------------*-C-*---------------------------------* 1/*--------------------------------*-C-*---------------------------------*
2 * File: defaultfont.C 2 * File: rxvtfont.C
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com> 4 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
5 * - original version. 5 * - original version.
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details. 15 * GNU General Public License for more details.
16 * 16 *
17 * You should have received a copy of the GNU General Public License 17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software 18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 *---------------------------------------------------------------------*/ 20 *---------------------------------------------------------------------*/
21 21
22#include "../config.h" 22#include "../config.h"
23#include "rxvt.h" 23#include "rxvt.h"
24#include "rxvtutil.h" 24#include "rxvtutil.h"
27#include <cstdlib> 27#include <cstdlib>
28#include <wchar.h> 28#include <wchar.h>
29#include <inttypes.h> 29#include <inttypes.h>
30 30
31#define DISPLAY r->display->display 31#define DISPLAY r->display->display
32#define TGC r->TermWin.gc 32#define TGC r->gc
33 33
34#define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width 34#define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width
35 35
36const struct rxvt_fallback_font { 36const struct rxvt_fallback_font {
37 codeset cs; 37 codeset cs;
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};
271 rxvt_fontprop properties () 276 rxvt_fontprop properties ()
272 { 277 {
273 rxvt_fontprop p; 278 rxvt_fontprop p;
274 279
275 p.width = p.height = 1; 280 p.width = p.height = 1;
281 p.ascent = rxvt_fontprop::unset;
276 p.weight = rxvt_fontprop::medium; 282 p.weight = rxvt_fontprop::medium;
277 p.slant = rxvt_fontprop::roman; 283 p.slant = rxvt_fontprop::roman;
278 284
279 return p; 285 return p;
280 } 286 }
326void 332void
327rxvt_font_default::draw (rxvt_drawable &d, int x, int y, 333rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
328 const text_t *text, int len, 334 const text_t *text, int len,
329 int fg, int bg) 335 int fg, int bg)
330{ 336{
337 Display *disp = d.display->display;
338
331 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 339 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
332 340
333 XSetForeground (d.display->display, TGC, r->pix_colors[fg]); 341 XSetForeground (disp, TGC, r->pix_colors[fg]);
334 342
335 while (len--) 343 while (len)
336 { 344 {
337#if ENABLE_COMBINING 345#if ENABLE_COMBINING
338 compose_char *cc; 346 compose_char *cc;
339#endif 347#endif
348 const text_t *tp = text;
340 text_t t = *text++; 349 text_t t = *tp;
350
351 while (++text, --len && *text == NOCHAR)
352 ;
353
354 int width = text - tp;
355 int fwidth = r->fwidth * width;
341 356
342 if (0x2500 <= t && t <= 0x259f) 357 if (0x2500 <= t && t <= 0x259f)
343 { 358 {
344 uint16_t offs = linedraw_offs[t - 0x2500]; 359 uint16_t offs = linedraw_offs[t - 0x2500];
345 uint32_t *a = linedraw_command + (offs >> 4); 360 uint32_t *a = linedraw_command + (offs >> 4);
346 uint32_t *b = a + (offs & 15); 361 uint32_t *b = a + (offs & 15);
347 362
348 int W = r->TermWin.fwidth; 363 int W = fwidth;
349 int H = r->TermWin.fheight; 364 int H = r->fheight;
350 365
351 int x_[16]; 366 int x_[16];
352 int y_[16]; 367 int y_[16];
353 368
354 for (int i = 0; i <= 8; i++) 369 for (int i = 0; i <= 8; i++)
362 377
363 XGCValues gcv; 378 XGCValues gcv;
364 379
365 gcv.cap_style = CapButt; 380 gcv.cap_style = CapButt;
366 gcv.line_width = 0; 381 gcv.line_width = 0;
367 XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv); 382 XChangeGC (disp, TGC, GCLineWidth | GCCapStyle, &gcv);
368 383
369 while (a < b) 384 while (a < b)
370 { 385 {
371 uint32_t command = *a++; 386 uint32_t command = *a++;
372 387
379 int y2 = y_[(command >> 0) & 15]; 394 int y2 = y_[(command >> 0) & 15];
380 395
381 switch (op) 396 switch (op)
382 { 397 {
383 case 0: // line 398 case 0: // line
384 XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2); 399 XDrawLine (disp, d, TGC, x1, y1, x2, y2);
385 break; 400 break;
386 401
387 case 1: // rectangle, possibly stippled 402 case 1: // rectangle, possibly stippled
388 if (a) 403 if (a)
389 { 404 {
390 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 }; 405 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
391 406
392 gcv.fill_style = FillStippled; 407 gcv.fill_style = FillStippled;
393 gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2); 408 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
394 gcv.ts_x_origin = x; 409 gcv.ts_x_origin = x;
395 gcv.ts_y_origin = y; 410 gcv.ts_y_origin = y;
396 411
397 XChangeGC (d.display->display, TGC, 412 XChangeGC (disp, TGC,
398 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, 413 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
399 &gcv); 414 &gcv);
400 } 415 }
401 416
402 XFillRectangle (d.display->display, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1); 417 XFillRectangle (disp, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
403 418
404 if (a) 419 if (a)
405 { 420 {
406 XFreePixmap (d.display->display, gcv.stipple); 421 XFreePixmap (disp, gcv.stipple);
407 gcv.stipple = 0; 422 gcv.stipple = 0;
408 gcv.fill_style = FillSolid; 423 gcv.fill_style = FillSolid;
409 XChangeGC (d.display->display, TGC, GCFillStyle, &gcv); 424 XChangeGC (disp, TGC, GCFillStyle, &gcv);
410 } 425 }
411 break; 426 break;
412 case 2: // arc 427 case 2: // arc
413 XDrawArc (d.display->display, d, TGC, 428 XDrawArc (disp, d, TGC,
414 x1 - W/2, y1 - H/2, W-1, H-1, 429 x1 - W/2, y1 - H/2, W-1, H-1,
415 (a - 1) * 90*64, (b - 1) * 90*64); 430 (a - 1) * 90*64, (b - 1) * 90*64);
416 break; 431 break;
417 } 432 }
418 } 433 }
419 } 434 }
420#if ENABLE_COMBINING 435#if ENABLE_COMBINING
421 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t])) 436 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
422 { 437 {
438 text_t chrs[2];
439 width = min (2, width);
440 chrs [1] = NOCHAR;
441
442 *chrs = cc->c1;
423 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)]; 443 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
424 f1->draw (d, x, y, &(t = cc->c1), 1, fg, bg); 444 f1->draw (d, x, y, chrs, width, fg, bg);
445
425 if (cc->c2 != NOCHAR) 446 if (cc->c2 != NOCHAR)
426 { 447 {
448 bool careful;
449
427 // prefer font of first character, for no good reasons 450 // prefer font of first character, for no good reasons
428 bool careful; 451 *chrs = cc->c2;
429 rxvt_font *f2 = f1->has_char (cc->c2, 0, careful) 452 rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful)
430 ? f1 453 ? f1
431 : (*fs)[fs->find_font (cc->c2)]; 454 : (*fs)[fs->find_font (cc->c2)];
432 455
433 f2->draw (d, x, y, &(t = cc->c2), 1, fg, -1); 456 f2->draw (d, x, y, chrs, width, fg, -1);
434 } 457 }
435 } 458 }
436#endif 459#endif
437 else 460 else
438 switch (t) 461 switch (t)
441 case ZERO_WIDTH_CHAR: 464 case ZERO_WIDTH_CHAR:
442 case NOCHAR: 465 case NOCHAR:
443 break; 466 break;
444 467
445 default: 468 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, 469 XDrawRectangle (disp, d, TGC, x + 2, y + 2,
455 w + r->TermWin.fwidth - 4, r->TermWin.fheight - 4); 470 fwidth - 4, r->fheight - 4);
456 x += w;
457 } 471 }
458 472
459 x += r->TermWin.fwidth; 473 x += fwidth;
460 } 474 }
461} 475}
462 476
463///////////////////////////////////////////////////////////////////////////// 477/////////////////////////////////////////////////////////////////////////////
464 478
510bool 524bool
511rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth) 525rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
512{ 526{
513 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2; 527 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
514 p.height = height; 528 p.height = height;
529 p.ascent = rxvt_fontprop::unset;
515 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium; 530 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
516 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic; 531 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
517 532
518 return true; 533 return true;
519} 534}
520 535
521bool 536bool
522rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) 537rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
523{ 538{
524 unsigned long height; 539 unsigned long height;
540
541#if 0
525 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) 542 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
526 return false; 543 return false;
544#else
545 height = f->ascent + f->descent;
546#endif
527 547
528 unsigned long avgwidth; 548 unsigned long avgwidth;
529 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) 549 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
530 avgwidth = 0; 550 avgwidth = 0;
531 551
534 554
535 set_properties (p, height, weight, slant, avgwidth); 555 set_properties (p, height, weight, slant, avgwidth);
536 556
537 free (weight); 557 free (weight);
538 free (slant); 558 free (slant);
559
560 p.ascent = f->ascent;
539 561
540 return true; 562 return true;
541} 563}
542 564
543bool 565bool
614} 636}
615 637
616bool 638bool
617rxvt_font_x11::load (const rxvt_fontprop &prop) 639rxvt_font_x11::load (const rxvt_fontprop &prop)
618{ 640{
641 Display *disp = DISPLAY;
642
619 clear (); 643 clear ();
620 644
621 char field_str[64]; // enough for 128 bits 645 char field_str[64]; // enough for 128 bits
622 646
623 // first morph the font if required 647 // first morph the font if required
626 { 650 {
627 char fname[1024]; 651 char fname[1024];
628 652
629 if (name[0] != '-') 653 if (name[0] != '-')
630 { 654 {
631 f = XLoadQueryFont (DISPLAY, name); 655 f = XLoadQueryFont (disp, name);
632 656
633 if (!f) 657 if (!f)
634 return false; 658 return false;
635 659
636 char *new_name = get_property (f, "FONT", name); 660 char *new_name = get_property (f, "FONT", name);
638 if (new_name) 662 if (new_name)
639 set_name (new_name); 663 set_name (new_name);
640 else 664 else
641 rxvt_warn ("font '%s' has no FONT property, continuing without.", name); 665 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
642 666
643 XFreeFont (DISPLAY, f); 667 XFreeFont (disp, f);
644 f = 0; 668 f = 0;
645 } 669 }
646 670
647 if (prop.weight != rxvt_fontprop::unset) 671 if (prop.weight != rxvt_fontprop::unset)
648 { 672 {
659 ? "r" : "i"); // TODO: handle "o"blique, too 683 ? "r" : "i"); // TODO: handle "o"blique, too
660 set_name (strdup (fname)); 684 set_name (strdup (fname));
661 } 685 }
662 } 686 }
663 687
688 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
689 ? 0 : prop.height);
690
691 struct font_weight {
692 char *name;
693 int diff;
694
695 void clear ()
696 {
697 name = 0;
698 diff = 0x7fffffff;
699 }
700
701 font_weight () { clear (); }
702 ~font_weight () { free (name); }
703 };
704
664 char **list; 705 char **list;
665 int count; 706 int count;
666 list = XListFonts (DISPLAY, name, 1024, &count); 707 list = XListFonts (disp, name, 4000, &count);
667 708
668 set_name (0); 709 set_name (0);
669 710
670 if (!list) 711 if (!list)
671 return false; 712 return false;
672 713
673 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset 714 font_weight *fonts = new font_weight[count];
674 ? 0 : prop.height);
675 715
676 int bestdiff = 0x7fffffff;
677 for (int i = 0; i < count; i++) 716 for (int i = 0; i < count; i++)
678 { 717 {
679 rxvt_fontprop p; 718 rxvt_fontprop p;
680 char fname[1024]; 719 char fname[1024];
681 720
682 int diff = 0; 721 int diff = 0;
683 722
684 if (replace_field (fname, list[i], 6, '0', field_str)) 723 if (replace_field (fname, list[i], 6, '0', field_str))
685 diff += 10; // slightly penalize scalable fonts 724 diff += 10; // slightly penalize scalable fonts
725 else if (replace_field (fname, list[i], 11, '0', "0"))
726 diff += 300; // more heavily penalize what looks like scaled bitmap fotns
686 727
687 if (!set_properties (p, fname)) 728 if (!set_properties (p, fname))
688 continue; 729 continue;
689 730
690 if (prop.height != rxvt_fontprop::unset 731 if (prop.height != rxvt_fontprop::unset
694 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128; 735 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); 736 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); 737 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); 738 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
698 739
699 if (!name // compare against best found so far 740 fonts[i].name = strdup (fname);
700 || diff < bestdiff) 741 fonts[i].diff = diff;
701 {
702 set_name (strdup (fname));
703 bestdiff = diff;
704 }
705 } 742 }
706 743
707 XFreeFontNames (list); 744 XFreeFontNames (list);
708 745
709 if (!name) 746 // this loop only iterates when the guessed font-size is too small
710 return false; 747 for (;;)
748 {
749 font_weight *best = fonts + count - 1;
711 750
712 f = XLoadQueryFont (DISPLAY, name); 751 for (font_weight *w = best; w-- > fonts; )
752 if (w->diff <= best->diff)
753 best = w;
754
755 if (!best->name
756 || !(f = XLoadQueryFont (disp, best->name)))
757 break;
758
759 set_name (best->name);
760 best->clear ();
761
762 ascent = f->ascent;
763 descent = f->descent;
764 height = ascent + descent;
765
766 if (prop.height == rxvt_fontprop::unset
767 || height <= prop.height)
768 break; // font is ready for use
769
770 // PIXEL_SIZE small enough, but real height too large
771 clear ();
772 }
773
774 delete [] fonts;
713 775
714 if (!f) 776 if (!f)
715 return false; 777 return false;
716 778
717 char *registry = get_property (f, "CHARSET_REGISTRY", 0); 779 char *registry = get_property (f, "CHARSET_REGISTRY", 0);
745 if (cs == CS_UNICODE) 807 if (cs == CS_UNICODE)
746 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font 808 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
747 809
748 encm = f->min_byte1 != 0 || f->max_byte1 != 0; 810 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
749 enc2b = encm || f->max_char_or_byte2 > 255; 811 enc2b = encm || f->max_char_or_byte2 > 255;
750
751 ascent = f->ascent;
752 descent = f->descent;
753 height = ascent + descent;
754 812
755 slow = false; 813 slow = false;
756 814
757#if 1 // only used for slow detection, TODO optimize 815#if 1 // only used for slow detection, TODO optimize
758 if (f->min_bounds.width == f->max_bounds.width) 816 if (f->min_bounds.width == f->max_bounds.width)
797 855
798 XCharStruct g; 856 XCharStruct g;
799 int dir_ret, asc_ret, des_ret; 857 int dir_ret, asc_ret, des_ret;
800 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g); 858 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
801 859
802 int wcw = wcwidth (*t); if (wcw > 0) g.width = g.width / wcw; 860 int wcw = wcwidth (*t); if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
803 861
804 if (width < g.width) width = g.width; 862 if (width < g.width) width = g.width;
805 } 863 }
806 864
807 if (cs == CS_UNKNOWN) 865 if (cs == CS_UNKNOWN)
882 return true; 940 return true;
883 941
884 // check character against base font bounding box 942 // check character against base font bounding box
885 int w = xcs->width; 943 int w = xcs->width;
886 int wcw = wcwidth (unicode); 944 int wcw = wcwidth (unicode);
887 if (wcw > 0) w /= wcw; 945 if (wcw > 0) w = (w + wcw - 1) / wcw;
888 946
889 careful = w > prop->width; 947 careful = w > prop->width;
890 if (careful && w > prop->width * MAX_OVERLAP >> 2) 948 if (careful && w > prop->width * MAX_OVERLAP >> 2)
891 return false; 949 return false;
892 950
902 // and it is a mess /. 960 // and it is a mess /.
903 // yet we are trying to be perfect /. 961 // yet we are trying to be perfect /.
904 // but the result still isn't perfect /. 962 // but the result still isn't perfect /.
905 963
906 bool slow = this->slow 964 bool slow = this->slow
907 || width != r->TermWin.fwidth 965 || width != r->fwidth
908 || height != r->TermWin.fheight; 966 || height != r->fheight;
909 967
910 int base = ascent; // sorry, incorrect: r->TermWin.fbase; 968 int base = ascent; // sorry, incorrect: r->fbase;
911 969
912 XGCValues v; 970 XGCValues v;
913 v.foreground = r->pix_colors[fg]; 971 v.foreground = r->pix_colors[fg];
914 v.font = f->fid; 972 v.font = f->fid;
915 973
923 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 981 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
924 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len); 982 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
925 } 983 }
926 else 984 else
927 { 985 {
928 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 986 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
929 987
930 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 988 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
931 989
932 if (slow) 990 if (slow)
933 { 991 {
934 do 992 do
935 { 993 {
936 if (xc->byte1 || xc->byte2) 994 if (xc->byte1 || xc->byte2)
937 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1); 995 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
938 996
939 x += r->TermWin.fwidth; 997 x += r->fwidth;
940 xc++; len--; 998 xc++; len--;
941 } 999 }
942 while (len); 1000 while (len);
943 } 1001 }
944 else 1002 else
955 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 1013 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
956 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len); 1014 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
957 } 1015 }
958 else 1016 else
959 { 1017 {
960 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1018 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
961 1019
962 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 1020 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
963 1021
964 if (slow) 1022 if (slow)
965 { 1023 {
966 do 1024 do
967 { 1025 {
968 if (*xc) 1026 if (*xc)
969 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1); 1027 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
970 1028
971 x += r->TermWin.fwidth; 1029 x += r->fwidth;
972 xc++; len--; 1030 xc++; len--;
973 } 1031 }
974 while (len); 1032 while (len);
975 } 1033 }
976 else 1034 else
1019 1077
1020 FT_Face face = XftLockFace (f); 1078 FT_Face face = XftLockFace (f);
1021 1079
1022 p.width = width; 1080 p.width = width;
1023 p.height = height; 1081 p.height = height;
1082 p.ascent = ascent;
1024 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD 1083 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
1025 ? rxvt_fontprop::bold : rxvt_fontprop::medium; 1084 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
1026 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC 1085 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
1027 ? rxvt_fontprop::italic : rxvt_fontprop::roman; 1086 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
1028 1087
1032} 1091}
1033 1092
1034bool 1093bool
1035rxvt_font_xft::load (const rxvt_fontprop &prop) 1094rxvt_font_xft::load (const rxvt_fontprop &prop)
1036{ 1095{
1096 Display *disp = DISPLAY;
1097
1037 clear (); 1098 clear ();
1038 1099
1039 FcPattern *p = FcNameParse ((FcChar8 *) name); 1100 FcPattern *p = FcNameParse ((FcChar8 *) name);
1040 1101
1041 if (!p) 1102 if (!p)
1042 return false; 1103 return false;
1043 1104
1044 FcValue v; 1105 FcValue v;
1045 1106
1046 if (prop.height != rxvt_fontprop::unset 1107 if (prop.height != rxvt_fontprop::unset
1047 && FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch) 1108 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1109 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
1048 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); 1110 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1049 1111
1050 if (prop.weight != rxvt_fontprop::unset 1112 if (prop.weight != rxvt_fontprop::unset
1051 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) 1113 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
1052 FcPatternAddInteger (p, FC_WEIGHT, prop.weight); 1114 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
1066 1128
1067 // store generated name so iso14755 view gives better results 1129 // store generated name so iso14755 view gives better results
1068 set_name ((char *)FcNameUnparse (p)); 1130 set_name ((char *)FcNameUnparse (p));
1069 1131
1070 XftResult result; 1132 XftResult result;
1071 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1133 FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result);
1072 1134
1073 FcPatternDestroy (p); 1135 FcPatternDestroy (p);
1074 1136
1075 if (!match) 1137 if (!match)
1076 return false; 1138 return false;
1078 int ftheight = 0; 1140 int ftheight = 0;
1079 bool success = true; 1141 bool success = true;
1080 1142
1081 for (;;) 1143 for (;;)
1082 { 1144 {
1083 f = XftFontOpenPattern (DISPLAY, FcPatternDuplicate (match)); 1145 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
1084 1146
1085 if (!f) 1147 if (!f)
1086 { 1148 {
1087 success = false; 1149 success = false;
1088 break; 1150 break;
1097 1159
1098 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE; 1160 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1099 1161
1100 XftUnlockFace (f); 1162 XftUnlockFace (f);
1101 1163
1164 int glheight = height;
1165
1102 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; ) 1166 for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; )
1103 { 1167 {
1104 FcChar16 ch = *t; 1168 FcChar16 ch = *t;
1105 1169
1106 if (cs != CS_UNICODE 1170 if (cs != CS_UNICODE
1112 bool careful; 1176 bool careful;
1113 if (!has_char (*t, &prop, careful)) 1177 if (!has_char (*t, &prop, careful))
1114 continue; 1178 continue;
1115 1179
1116 XGlyphInfo g; 1180 XGlyphInfo g;
1117 XftTextExtents16 (DISPLAY, f, &ch, 1, &g); 1181 XftTextExtents16 (disp, f, &ch, 1, &g);
1182
1183 g.width -= g.x;
1118 1184
1119 int wcw = wcwidth (ch); 1185 int wcw = wcwidth (ch);
1120 if (wcw > 0) g.width = g.width / wcw; 1186 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1121 1187
1122 if (width < g.width) width = g.width; 1188 if (width < g.width ) width = g.width;
1123 if (height < g.height) height = g.height; 1189 if (height < g.height ) height = g.height;
1190 if (glheight < g.height - g.y) glheight = g.height - g.y;
1191 }
1192
1193 if (!width)
1194 {
1195 rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name);
1196
1197 XftFontClose (disp, f);
1198 f = 0;
1199
1200 success = false;
1201 break;
1124 } 1202 }
1125 1203
1126 if (prop.height == rxvt_fontprop::unset 1204 if (prop.height == rxvt_fontprop::unset
1127 || height <= prop.height 1205 || (height <= prop.height && glheight <= prop.height)
1128 || height <= 2 1206 || height <= 2
1129 || !scalable) 1207 || !scalable)
1130 break; 1208 break;
1131 1209
1132 if (ftheight) 1210 if (ftheight)
1139 ftheight -= height - prop.height; 1217 ftheight -= height - prop.height;
1140 } 1218 }
1141 else 1219 else
1142 ftheight = prop.height - 1; 1220 ftheight = prop.height - 1;
1143 1221
1144 XftFontClose (DISPLAY, f); 1222 XftFontClose (disp, f);
1145 FcPatternDel (match, FC_PIXEL_SIZE); 1223 FcPatternDel (match, FC_PIXEL_SIZE);
1146 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); 1224 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1147 } 1225 }
1148 1226
1149 FcPatternDestroy (match); 1227 FcPatternDestroy (match);
1150 1228
1151#if 0 // do it per-character 1229#if 0 // do it per-character
1173 // check character against base font bounding box 1251 // check character against base font bounding box
1174 FcChar32 ch = unicode; 1252 FcChar32 ch = unicode;
1175 XGlyphInfo g; 1253 XGlyphInfo g;
1176 XftTextExtents32 (DISPLAY, f, &ch, 1, &g); 1254 XftTextExtents32 (DISPLAY, f, &ch, 1, &g);
1177 1255
1178 int w = g.width; 1256 int w = g.width - g.x;
1179 int wcw = wcwidth (unicode); 1257 int wcw = wcwidth (unicode);
1180 if (wcw > 0) w /= wcw; 1258 if (wcw > 0) w = (w + wcw - 1) / wcw;
1181 1259
1182 careful = w > prop->width; 1260 careful = w > prop->width;
1183 if (careful && w > prop->width * MAX_OVERLAP >> 2) 1261 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1184 return false; 1262 return false;
1185 1263
1189void 1267void
1190rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1268rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1191 const text_t *text, int len, 1269 const text_t *text, int len,
1192 int fg, int bg) 1270 int fg, int bg)
1193{ 1271{
1194 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 1272 clear_rect (d, x, y, r->fwidth * len, r->fheight, bg);
1195 1273
1196 int base = ascent; // should be fbase, but that is incorrect 1274 int base = ascent; // should be fbase, but that is incorrect
1197 1275
1198 XGlyphInfo extents; 1276 XGlyphInfo extents;
1199 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32)); 1277 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32));
1200 FcChar32 *ep = enc; 1278 FcChar32 *ep = enc;
1201 int ewidth = 0; 1279 int ewidth = 0;
1202 int xoff = 0;
1203 1280
1204 while (len) 1281 while (len)
1205 { 1282 {
1206 int cwidth = r->TermWin.fwidth; 1283 int cwidth = r->fwidth;
1207 FcChar32 fc = *text++; len--; 1284 FcChar32 fc = *text++; len--;
1208 FT_UInt gl;
1209 1285
1210 while (len && *text == NOCHAR) 1286 while (len && *text == NOCHAR)
1211 text++, len--, cwidth += r->TermWin.fwidth; 1287 text++, len--, cwidth += r->fwidth;
1212 1288
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 1289 if (fc == ' ' && ep == enc) // skip leading spaces
1230 {
1231 x += cwidth; 1290 x += cwidth;
1232 continue;
1233 }
1234
1235 else 1291 else
1236 { 1292 {
1293 FT_UInt gl = XftCharIndex (d.display->display, f, fc);
1294 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1295
1296 if (extents.xOff != cwidth)
1297 {
1298 if (ewidth)
1299 {
1300 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1301 x, y + base, enc, ep - enc);
1302 x += ewidth;
1303
1304 ep = enc;
1305 ewidth = 0;
1306 }
1307
1308 if (extents.xOff > cwidth)
1309 extents.xOff = cwidth;
1310
1311 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1312 x + (cwidth - extents.xOff >> 1),
1313 y + base, &gl, 1);
1314 x += cwidth;
1315 }
1316 else
1317 {
1237 *ep++ = gl; 1318 *ep++ = gl;
1238 ewidth += cwidth; 1319 ewidth += cwidth;
1239 xoff += extents.xOff; 1320 }
1240 } 1321 }
1241 } 1322 }
1242 1323
1243 if (ep != enc) 1324 if (ep != enc)
1244 {
1245 if (xoff > ewidth) xoff = ewidth;
1246 XftDrawGlyphs (d, &r->pix_colors[fg].c, f, 1325 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1247 x + (ewidth - xoff >> 1),
1248 y + base, enc, ep - enc); 1326 x, y + base, enc, ep - enc);
1249 }
1250} 1327}
1251#endif 1328#endif
1252 1329
1253///////////////////////////////////////////////////////////////////////////// 1330/////////////////////////////////////////////////////////////////////////////
1254 1331
1255rxvt_fontset::rxvt_fontset (rxvt_t r) 1332rxvt_fontset::rxvt_fontset (rxvt_t r)
1256: r (r), fontdesc (0) 1333: fontdesc (0), r (r)
1257{ 1334{
1258 clear (); 1335 clear ();
1259} 1336}
1260 1337
1261rxvt_fontset::~rxvt_fontset () 1338rxvt_fontset::~rxvt_fontset ()
1264} 1341}
1265 1342
1266void 1343void
1267rxvt_fontset::clear () 1344rxvt_fontset::clear ()
1268{ 1345{
1346 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1347 = rxvt_fontprop::unset;
1348
1269 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) 1349 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1270 FONT_UNREF (*i); 1350 FONT_UNREF (*i);
1271 1351
1272 for (pagemap **p = fmap.begin (); p != fmap.end (); p++) 1352 for (pagemap **p = fmap.begin (); p != fmap.end (); p++)
1273 delete *p; 1353 delete *p;
1323 char buf[512]; 1403 char buf[512];
1324 const char *end; 1404 const char *end;
1325 1405
1326 do 1406 do
1327 { 1407 {
1328 while (*desc <= ' ') desc++; 1408 while (*desc && *desc <= ' ')
1409 desc++;
1329 1410
1330 codeset cs = CS_UNICODE; 1411 codeset cs = CS_UNICODE;
1331 1412
1332 if (*desc == '[') 1413 if (*desc == '[')
1333 { 1414 {
1393 1474
1394 return true; 1475 return true;
1395} 1476}
1396 1477
1397bool 1478bool
1398rxvt_fontset::populate (const char *desc, const rxvt_fontprop &prop) 1479rxvt_fontset::populate (const char *desc)
1399{ 1480{
1400 clear (); 1481 clear ();
1401 1482
1402 fontdesc = strdup (desc); 1483 fontdesc = strdup (desc);
1403 1484
1404 fonts.push_back (new_font (0, CS_UNICODE)); 1485 fonts.push_back (new_font (0, CS_UNICODE));
1405 realize_font (0); 1486 realize_font (0);
1406
1407 this->prop = prop;
1408 1487
1409 add_fonts (desc); 1488 add_fonts (desc);
1410 1489
1411 return true; 1490 return true;
1412} 1491}
1445 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1524 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1446 goto next_font; 1525 goto next_font;
1447 1526
1448 if (!realize_font (i)) 1527 if (!realize_font (i))
1449 goto next_font; 1528 goto next_font;
1529
1530 if (prop.ascent != rxvt_fontprop::unset)
1531 max_it (f->ascent, prop.ascent);
1450 } 1532 }
1451 1533
1452 if (f->cs == CS_UNKNOWN) 1534 if (f->cs == CS_UNKNOWN)
1453 goto next_font; 1535 goto next_font;
1454 1536
1493 FcPatternAddInteger (p, FC_SLANT, prop.slant); 1575 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1494 FcPatternAddBool (p, FC_MINSPACE, 1); 1576 FcPatternAddBool (p, FC_MINSPACE, 1);
1495 //FcPatternAddBool (p, FC_ANTIALIAS, 1); 1577 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1496 1578
1497 XftResult result; 1579 XftResult result;
1498 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1580 FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result);
1499 1581
1500 FcPatternDestroy (p); 1582 FcPatternDestroy (p);
1501 1583
1502 if (match) 1584 if (match)
1503 { 1585 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines