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.52 by root, Sat Dec 4 02:56:06 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;
90 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, 91 { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
91#endif 92#endif
92 93
93#if ENCODING_ZH || ENCODING_ZH_EXT 94#if ENCODING_ZH || ENCODING_ZH_EXT
94# if XFT 95# if XFT
96 { CS_GBK_0, "xft:AR PL KaitiM GB" },
97 { CS_GBK_0, "xft:AR PL SungtiL GB" },
98 { CS_GBK_0, "xft::spacing=100:lang=zh" },
95 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, 99 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
96 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, 100 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
97 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, 101 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
98 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, 102 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
99 { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" }, 103 { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" },
100# endif 104# endif
105 { CS_GBK_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gbk*-0" },
101 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" }, 106 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
102 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" }, 107 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
103 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" }, 108 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
109 { CS_GB2312_1980_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
104 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" }, 110 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
105 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" }, 111 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
106 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" }, 112 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
107 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" }, 113 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
108 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" }, 114 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
125 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" }, 131 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
126 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, 132 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
127 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" }, 133 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
128 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 134 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
129#if XFT 135#if XFT
130 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true"}, 136 { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true" },
131 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" }, 137 { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" },
132 { CS_UNICODE, "xft:Andale Mono:antialias=false" }, 138 { CS_UNICODE, "xft:Andale Mono:antialias=false:autohint=false" },
133 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false" }, 139 { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" },
134 140
135 // FreeMono is usually uglier than x fonts, so try last only. 141 // FreeMono is usually uglier than x fonts, so try last only.
136 { CS_UNICODE, "xft:FreeMono:autohint=true" }, 142 { CS_UNICODE, "xft:FreeMono:autohint=true" },
137#endif 143#endif
138 144
139 { CS_UNKNOWN, 0 } 145 { CS_UNKNOWN, 0 }
140}; 146};
141 147
142// these characters are used to guess the font height and width 148// these characters are used to guess the font height and width
143// 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.
144static uint16_t extent_test_chars[] = { 150static uint16_t extent_test_chars[] = {
145 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_', 151 '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_',
146 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ 152 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ
147 0x304c, 0x672c, // が本 153 0x304c, 0x672c, // が本
148}; 154};
271 rxvt_fontprop properties () 277 rxvt_fontprop properties ()
272 { 278 {
273 rxvt_fontprop p; 279 rxvt_fontprop p;
274 280
275 p.width = p.height = 1; 281 p.width = p.height = 1;
282 p.ascent = rxvt_fontprop::unset;
276 p.weight = rxvt_fontprop::medium; 283 p.weight = rxvt_fontprop::medium;
277 p.slant = rxvt_fontprop::roman; 284 p.slant = rxvt_fontprop::roman;
278 285
279 return p; 286 return p;
280 } 287 }
300 return false; 307 return false;
301 308
302 if (unicode <= 0x009f) 309 if (unicode <= 0x009f)
303 return true; 310 return true;
304 311
305 if (unicode >= 0x2500 && unicode <= 0x259f) 312 if (unicode >= 0x2500 && unicode <= 0x259f &&
313 !r->option (Opt_skipBuiltinGlyphs))
306 return true; 314 return true;
307 315
308 if (IS_COMPOSE (unicode)) 316 if (IS_COMPOSE (unicode))
309 return true; 317 return true;
310 318
326void 334void
327rxvt_font_default::draw (rxvt_drawable &d, int x, int y, 335rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
328 const text_t *text, int len, 336 const text_t *text, int len,
329 int fg, int bg) 337 int fg, int bg)
330{ 338{
339 Display *disp = d.display->display;
340
331 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);
332 342
333 XSetForeground (d.display->display, TGC, r->pix_colors[fg]); 343 XSetForeground (disp, TGC, r->pix_colors[fg]);
334 344
335 while (len--) 345 while (len)
336 { 346 {
337#if ENABLE_COMBINING 347#if ENABLE_COMBINING
338 compose_char *cc; 348 compose_char *cc;
339#endif 349#endif
350 const text_t *tp = text;
340 text_t t = *text++; 351 text_t t = *tp;
352
353 while (++text, --len && *text == NOCHAR)
354 ;
355
356 int width = text - tp;
357 int fwidth = r->fwidth * width;
341 358
342 if (0x2500 <= t && t <= 0x259f) 359 if (0x2500 <= t && t <= 0x259f)
343 { 360 {
344 uint16_t offs = linedraw_offs[t - 0x2500]; 361 uint16_t offs = linedraw_offs[t - 0x2500];
345 uint32_t *a = linedraw_command + (offs >> 4); 362 uint32_t *a = linedraw_command + (offs >> 4);
346 uint32_t *b = a + (offs & 15); 363 uint32_t *b = a + (offs & 15);
347 364
348 int W = r->TermWin.fwidth; 365 int W = fwidth;
349 int H = r->TermWin.fheight; 366 int H = r->fheight;
350
351 // support double-width (but not more) graphics chars
352 // TODO: do this outside the if/elsif.. statement for all cases
353 if (len && *text == NOCHAR)
354 W += r->TermWin.fwidth;
355 367
356 int x_[16]; 368 int x_[16];
357 int y_[16]; 369 int y_[16];
358 370
359 for (int i = 0; i <= 8; i++) 371 for (int i = 0; i <= 8; i++)
367 379
368 XGCValues gcv; 380 XGCValues gcv;
369 381
370 gcv.cap_style = CapButt; 382 gcv.cap_style = CapButt;
371 gcv.line_width = 0; 383 gcv.line_width = 0;
372 XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv); 384 XChangeGC (disp, TGC, GCLineWidth | GCCapStyle, &gcv);
373 385
374 while (a < b) 386 while (a < b)
375 { 387 {
376 uint32_t command = *a++; 388 uint32_t command = *a++;
377 389
384 int y2 = y_[(command >> 0) & 15]; 396 int y2 = y_[(command >> 0) & 15];
385 397
386 switch (op) 398 switch (op)
387 { 399 {
388 case 0: // line 400 case 0: // line
389 XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2); 401 XDrawLine (disp, d, TGC, x1, y1, x2, y2);
390 break; 402 break;
391 403
392 case 1: // rectangle, possibly stippled 404 case 1: // rectangle, possibly stippled
393 if (a) 405 if (a)
394 { 406 {
395 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 }; 407 static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 };
396 408
397 gcv.fill_style = FillStippled; 409 gcv.fill_style = FillStippled;
398 gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2); 410 gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2);
399 gcv.ts_x_origin = x; 411 gcv.ts_x_origin = x;
400 gcv.ts_y_origin = y; 412 gcv.ts_y_origin = y;
401 413
402 XChangeGC (d.display->display, TGC, 414 XChangeGC (disp, TGC,
403 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, 415 GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin,
404 &gcv); 416 &gcv);
405 } 417 }
406 418
407 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);
408 420
409 if (a) 421 if (a)
410 { 422 {
411 XFreePixmap (d.display->display, gcv.stipple); 423 XFreePixmap (disp, gcv.stipple);
412 gcv.stipple = 0; 424 gcv.stipple = 0;
413 gcv.fill_style = FillSolid; 425 gcv.fill_style = FillSolid;
414 XChangeGC (d.display->display, TGC, GCFillStyle, &gcv); 426 XChangeGC (disp, TGC, GCFillStyle, &gcv);
415 } 427 }
416 break; 428 break;
417 case 2: // arc 429 case 2: // arc
418 XDrawArc (d.display->display, d, TGC, 430 XDrawArc (disp, d, TGC,
419 x1 - W/2, y1 - H/2, W-1, H-1, 431 x1 - W/2, y1 - H/2, W-1, H-1,
420 (a - 1) * 90*64, (b - 1) * 90*64); 432 (a - 1) * 90*64, (b - 1) * 90*64);
421 break; 433 break;
422 } 434 }
423 } 435 }
424 } 436 }
425#if ENABLE_COMBINING 437#if ENABLE_COMBINING
426 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t])) 438 else if (IS_COMPOSE (t) && (cc = rxvt_composite[t]))
427 { 439 {
428 text_t c2[2]; 440 text_t chrs[2];
429 int len2 = 1; 441 width = min (2, width);
430 442 chrs [1] = NOCHAR;
431 // support double-width (but not more) combining characters
432 if (len && *text == NOCHAR)
433 {
434 c2[1] = *text; // not text++ to get correct x-width
435 len2++;
436 }
437 443
438 c2[0] = cc->c1; 444 *chrs = cc->c1;
439 rxvt_font *f1 = (*fs)[fs->find_font (c2[0])]; 445 rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)];
440 f1->draw (d, x, y, c2, len2, fg, bg); 446 f1->draw (d, x, y, chrs, width, fg, bg);
441 447
442 if (cc->c2 != NOCHAR) 448 if (cc->c2 != NOCHAR)
443 { 449 {
444 bool careful; 450 bool careful;
445 451
446 // prefer font of first character, for no good reasons 452 // prefer font of first character, for no good reasons
447 c2[0] = cc->c2; 453 *chrs = cc->c2;
448 rxvt_font *f2 = (f1->has_char (c2[0], 0, careful) && !careful) 454 rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful)
449 ? f1 455 ? f1
450 : (*fs)[fs->find_font (c2[0])]; 456 : (*fs)[fs->find_font (cc->c2)];
451 457
452 f2->draw (d, x, y, c2, len2, fg, -1); 458 f2->draw (d, x, y, chrs, width, fg, -1);
453 } 459 }
454 } 460 }
455#endif 461#endif
456 else 462 else
457 switch (t) 463 switch (t)
460 case ZERO_WIDTH_CHAR: 466 case ZERO_WIDTH_CHAR:
461 case NOCHAR: 467 case NOCHAR:
462 break; 468 break;
463 469
464 default: 470 default:
465 int w = 0;
466 while (len > 0 && *text == NOCHAR)
467 {
468 ++text;
469 --len;
470 w += r->TermWin.fwidth;
471 }
472
473 XDrawRectangle (d.display->display, d, TGC, x + 2, y + 2, 471 XDrawRectangle (disp, d, TGC, x + 2, y + 2,
474 w + r->TermWin.fwidth - 4, r->TermWin.fheight - 4); 472 fwidth - 4, r->fheight - 4);
475 x += w;
476 } 473 }
477 474
478 x += r->TermWin.fwidth; 475 x += fwidth;
479 } 476 }
480} 477}
481 478
482///////////////////////////////////////////////////////////////////////////// 479/////////////////////////////////////////////////////////////////////////////
483 480
529bool 526bool
530rxvt_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)
531{ 528{
532 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2; 529 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
533 p.height = height; 530 p.height = height;
531 p.ascent = rxvt_fontprop::unset;
534 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;
535 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;
536 534
537 return true; 535 return true;
538} 536}
539 537
540bool 538bool
541rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) 539rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
542{ 540{
543 unsigned long height; 541 unsigned long height;
542
543#if 0
544 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) 544 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
545 return false; 545 return false;
546#else
547 height = f->ascent + f->descent;
548#endif
546 549
547 unsigned long avgwidth; 550 unsigned long avgwidth;
548 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) 551 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
549 avgwidth = 0; 552 avgwidth = 0;
550 553
553 556
554 set_properties (p, height, weight, slant, avgwidth); 557 set_properties (p, height, weight, slant, avgwidth);
555 558
556 free (weight); 559 free (weight);
557 free (slant); 560 free (slant);
561
562 p.ascent = f->ascent;
558 563
559 return true; 564 return true;
560} 565}
561 566
562bool 567bool
633} 638}
634 639
635bool 640bool
636rxvt_font_x11::load (const rxvt_fontprop &prop) 641rxvt_font_x11::load (const rxvt_fontprop &prop)
637{ 642{
643 Display *disp = DISPLAY;
644
638 clear (); 645 clear ();
639 646
640 char field_str[64]; // enough for 128 bits 647 char field_str[64]; // enough for 128 bits
641 648
642 // first morph the font if required 649 // first morph the font if required
645 { 652 {
646 char fname[1024]; 653 char fname[1024];
647 654
648 if (name[0] != '-') 655 if (name[0] != '-')
649 { 656 {
650 f = XLoadQueryFont (DISPLAY, name); 657 f = XLoadQueryFont (disp, name);
651 658
652 if (!f) 659 if (!f)
653 return false; 660 return false;
654 661
655 char *new_name = get_property (f, "FONT", name); 662 char *new_name = get_property (f, "FONT", name);
657 if (new_name) 664 if (new_name)
658 set_name (new_name); 665 set_name (new_name);
659 else 666 else
660 rxvt_warn ("font '%s' has no FONT property, continuing without.", name); 667 rxvt_warn ("font '%s' has no FONT property, continuing without.", name);
661 668
662 XFreeFont (DISPLAY, f); 669 XFreeFont (disp, f);
663 f = 0; 670 f = 0;
664 } 671 }
665 672
666 if (prop.weight != rxvt_fontprop::unset) 673 if (prop.weight != rxvt_fontprop::unset)
667 { 674 {
678 ? "r" : "i"); // TODO: handle "o"blique, too 685 ? "r" : "i"); // TODO: handle "o"blique, too
679 set_name (strdup (fname)); 686 set_name (strdup (fname));
680 } 687 }
681 } 688 }
682 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
683 char **list; 707 char **list;
684 int count; 708 int count;
685 list = XListFonts (DISPLAY, name, 1024, &count); 709 list = XListFonts (disp, name, 4000, &count);
686 710
687 set_name (0); 711 set_name (0);
688 712
689 if (!list) 713 if (!list)
690 return false; 714 return false;
691 715
692 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset 716 font_weight *fonts = new font_weight[count];
693 ? 0 : prop.height);
694 717
695 int bestdiff = 0x7fffffff;
696 for (int i = 0; i < count; i++) 718 for (int i = 0; i < count; i++)
697 { 719 {
698 rxvt_fontprop p; 720 rxvt_fontprop p;
699 char fname[1024]; 721 char fname[1024];
700 722
701 int diff = 0; 723 int diff = 0;
702 724
703 if (replace_field (fname, list[i], 6, '0', field_str)) 725 if (replace_field (fname, list[i], 6, '0', field_str))
704 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
705 729
706 if (!set_properties (p, fname)) 730 if (!set_properties (p, fname))
707 continue; 731 continue;
708 732
709 if (prop.height != rxvt_fontprop::unset 733 if (prop.height != rxvt_fontprop::unset
713 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;
714 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);
715 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);
716 //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);
717 741
718 if (!name // compare against best found so far 742 fonts[i].name = strdup (fname);
719 || diff < bestdiff) 743 fonts[i].diff = diff;
720 {
721 set_name (strdup (fname));
722 bestdiff = diff;
723 }
724 } 744 }
725 745
726 XFreeFontNames (list); 746 XFreeFontNames (list);
727 747
728 if (!name) 748 // this loop only iterates when the guessed font-size is too small
729 return false; 749 for (;;)
750 {
751 font_weight *best = fonts + count - 1;
730 752
731 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;
732 777
733 if (!f) 778 if (!f)
734 return false; 779 return false;
735 780
736 char *registry = get_property (f, "CHARSET_REGISTRY", 0); 781 char *registry = get_property (f, "CHARSET_REGISTRY", 0);
764 if (cs == CS_UNICODE) 809 if (cs == CS_UNICODE)
765 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
766 811
767 encm = f->min_byte1 != 0 || f->max_byte1 != 0; 812 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
768 enc2b = encm || f->max_char_or_byte2 > 255; 813 enc2b = encm || f->max_char_or_byte2 > 255;
769
770 ascent = f->ascent;
771 descent = f->descent;
772 height = ascent + descent;
773 814
774 slow = false; 815 slow = false;
775 816
776#if 1 // only used for slow detection, TODO optimize 817#if 1 // only used for slow detection, TODO optimize
777 if (f->min_bounds.width == f->max_bounds.width) 818 if (f->min_bounds.width == f->max_bounds.width)
816 857
817 XCharStruct g; 858 XCharStruct g;
818 int dir_ret, asc_ret, des_ret; 859 int dir_ret, asc_ret, des_ret;
819 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g); 860 XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g);
820 861
821 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;
822 863
823 if (width < g.width) width = g.width; 864 if (width < g.width) width = g.width;
824 } 865 }
825 866
826 if (cs == CS_UNKNOWN) 867 if (cs == CS_UNKNOWN)
901 return true; 942 return true;
902 943
903 // check character against base font bounding box 944 // check character against base font bounding box
904 int w = xcs->width; 945 int w = xcs->width;
905 int wcw = wcwidth (unicode); 946 int wcw = wcwidth (unicode);
906 if (wcw > 0) w /= wcw; 947 if (wcw > 0) w = (w + wcw - 1) / wcw;
907 948
908 careful = w > prop->width; 949 careful = w > prop->width;
909 if (careful && w > prop->width * MAX_OVERLAP >> 2) 950 if (careful && w > prop->width * MAX_OVERLAP >> 2)
910 return false; 951 return false;
911 952
921 // and it is a mess /. 962 // and it is a mess /.
922 // yet we are trying to be perfect /. 963 // yet we are trying to be perfect /.
923 // but the result still isn't perfect /. 964 // but the result still isn't perfect /.
924 965
925 bool slow = this->slow 966 bool slow = this->slow
926 || width != r->TermWin.fwidth 967 || width != r->fwidth
927 || height != r->TermWin.fheight; 968 || height != r->fheight;
928 969
929 int base = ascent; // sorry, incorrect: r->TermWin.fbase; 970 int base = ascent; // sorry, incorrect: r->fbase;
930 971
931 XGCValues v; 972 XGCValues v;
932 v.foreground = r->pix_colors[fg]; 973 v.foreground = r->pix_colors[fg];
933 v.font = f->fid; 974 v.font = f->fid;
934 975
942 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 983 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
943 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len); 984 XDrawImageString16 (d.display->display, d, TGC, x, y + base, xc, len);
944 } 985 }
945 else 986 else
946 { 987 {
947 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);
948 989
949 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 990 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
950 991
951 if (slow) 992 if (slow)
952 { 993 {
953 do 994 do
954 { 995 {
955 if (xc->byte1 || xc->byte2) 996 if (xc->byte1 || xc->byte2)
956 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1); 997 XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1);
957 998
958 x += r->TermWin.fwidth; 999 x += r->fwidth;
959 xc++; len--; 1000 xc++; len--;
960 } 1001 }
961 while (len); 1002 while (len);
962 } 1003 }
963 else 1004 else
974 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v); 1015 XChangeGC (d.display->display, TGC, GCForeground | GCBackground | GCFont, &v);
975 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len); 1016 XDrawImageString (d.display->display, d, TGC, x, y + base, xc, len);
976 } 1017 }
977 else 1018 else
978 { 1019 {
979 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);
980 1021
981 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); 1022 XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v);
982 1023
983 if (slow) 1024 if (slow)
984 { 1025 {
985 do 1026 do
986 { 1027 {
987 if (*xc) 1028 if (*xc)
988 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1); 1029 XDrawString (d.display->display, d, TGC, x, y + base, xc, 1);
989 1030
990 x += r->TermWin.fwidth; 1031 x += r->fwidth;
991 xc++; len--; 1032 xc++; len--;
992 } 1033 }
993 while (len); 1034 while (len);
994 } 1035 }
995 else 1036 else
1038 1079
1039 FT_Face face = XftLockFace (f); 1080 FT_Face face = XftLockFace (f);
1040 1081
1041 p.width = width; 1082 p.width = width;
1042 p.height = height; 1083 p.height = height;
1084 p.ascent = ascent;
1043 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD 1085 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD
1044 ? rxvt_fontprop::bold : rxvt_fontprop::medium; 1086 ? rxvt_fontprop::bold : rxvt_fontprop::medium;
1045 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC 1087 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC
1046 ? rxvt_fontprop::italic : rxvt_fontprop::roman; 1088 ? rxvt_fontprop::italic : rxvt_fontprop::roman;
1047 1089
1051} 1093}
1052 1094
1053bool 1095bool
1054rxvt_font_xft::load (const rxvt_fontprop &prop) 1096rxvt_font_xft::load (const rxvt_fontprop &prop)
1055{ 1097{
1098 Display *disp = DISPLAY;
1099
1056 clear (); 1100 clear ();
1057 1101
1058 FcPattern *p = FcNameParse ((FcChar8 *) name); 1102 FcPattern *p = FcNameParse ((FcChar8 *) name);
1059 1103
1060 if (!p) 1104 if (!p)
1061 return false; 1105 return false;
1062 1106
1063 FcValue v; 1107 FcValue v;
1064 1108
1065 if (prop.height != rxvt_fontprop::unset 1109 if (prop.height != rxvt_fontprop::unset
1066 || (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch 1110 && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch
1067 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch)) 1111 && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch))
1068 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); 1112 FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height);
1069 1113
1070 if (prop.weight != rxvt_fontprop::unset 1114 if (prop.weight != rxvt_fontprop::unset
1071 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch) 1115 && FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
1086 1130
1087 // store generated name so iso14755 view gives better results 1131 // store generated name so iso14755 view gives better results
1088 set_name ((char *)FcNameUnparse (p)); 1132 set_name ((char *)FcNameUnparse (p));
1089 1133
1090 XftResult result; 1134 XftResult result;
1091 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1135 FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result);
1092 1136
1093 FcPatternDestroy (p); 1137 FcPatternDestroy (p);
1094 1138
1095 if (!match) 1139 if (!match)
1096 return false; 1140 return false;
1098 int ftheight = 0; 1142 int ftheight = 0;
1099 bool success = true; 1143 bool success = true;
1100 1144
1101 for (;;) 1145 for (;;)
1102 { 1146 {
1103 f = XftFontOpenPattern (DISPLAY, FcPatternDuplicate (match)); 1147 f = XftFontOpenPattern (disp, FcPatternDuplicate (match));
1104 1148
1105 if (!f) 1149 if (!f)
1106 { 1150 {
1107 success = false; 1151 success = false;
1108 break; 1152 break;
1117 1161
1118 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE; 1162 bool scalable = face->face_flags & FT_FACE_FLAG_SCALABLE;
1119 1163
1120 XftUnlockFace (f); 1164 XftUnlockFace (f);
1121 1165
1166 int glheight = height;
1167
1122 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; )
1123 { 1169 {
1124 FcChar16 ch = *t; 1170 FcChar16 ch = *t;
1125 1171
1126 if (cs != CS_UNICODE 1172 if (cs != CS_UNICODE
1132 bool careful; 1178 bool careful;
1133 if (!has_char (*t, &prop, careful)) 1179 if (!has_char (*t, &prop, careful))
1134 continue; 1180 continue;
1135 1181
1136 XGlyphInfo g; 1182 XGlyphInfo g;
1137 XftTextExtents16 (DISPLAY, f, &ch, 1, &g); 1183 XftTextExtents16 (disp, f, &ch, 1, &g);
1184
1185 g.width -= g.x;
1138 1186
1139 int wcw = wcwidth (ch); 1187 int wcw = wcwidth (ch);
1140 if (wcw > 0) g.width = g.width / wcw; 1188 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
1141 1189
1142 if (width < g.width) width = g.width; 1190 if (width < g.width ) width = g.width;
1143 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;
1144 } 1204 }
1145 1205
1146 if (prop.height == rxvt_fontprop::unset 1206 if (prop.height == rxvt_fontprop::unset
1147 || height <= prop.height 1207 || (height <= prop.height && glheight <= prop.height)
1148 || height <= 2 1208 || height <= 2
1149 || !scalable) 1209 || !scalable)
1150 break; 1210 break;
1151 1211
1152 if (ftheight) 1212 if (ftheight)
1159 ftheight -= height - prop.height; 1219 ftheight -= height - prop.height;
1160 } 1220 }
1161 else 1221 else
1162 ftheight = prop.height - 1; 1222 ftheight = prop.height - 1;
1163 1223
1164 XftFontClose (DISPLAY, f); 1224 XftFontClose (disp, f);
1165 FcPatternDel (match, FC_PIXEL_SIZE); 1225 FcPatternDel (match, FC_PIXEL_SIZE);
1166 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); 1226 FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight);
1167 } 1227 }
1168 1228
1169 FcPatternDestroy (match); 1229 FcPatternDestroy (match);
1170 1230
1171#if 0 // do it per-character 1231#if 0 // do it per-character
1193 // check character against base font bounding box 1253 // check character against base font bounding box
1194 FcChar32 ch = unicode; 1254 FcChar32 ch = unicode;
1195 XGlyphInfo g; 1255 XGlyphInfo g;
1196 XftTextExtents32 (DISPLAY, f, &ch, 1, &g); 1256 XftTextExtents32 (DISPLAY, f, &ch, 1, &g);
1197 1257
1198 int w = g.width; 1258 int w = g.width - g.x;
1199 int wcw = wcwidth (unicode); 1259 int wcw = wcwidth (unicode);
1200 if (wcw > 0) w /= wcw; 1260 if (wcw > 0) w = (w + wcw - 1) / wcw;
1201 1261
1202 careful = w > prop->width; 1262 careful = w > prop->width;
1203 if (careful && w > prop->width * MAX_OVERLAP >> 2) 1263 if (careful && w > prop->width * MAX_OVERLAP >> 2)
1204 return false; 1264 return false;
1205 1265
1209void 1269void
1210rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1270rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1211 const text_t *text, int len, 1271 const text_t *text, int len,
1212 int fg, int bg) 1272 int fg, int bg)
1213{ 1273{
1214 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);
1215
1216 int base = ascent; // should be fbase, but that is incorrect
1217 1275
1218 XGlyphInfo extents; 1276 XGlyphInfo extents;
1219 FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32)); 1277 XftGlyphSpec *enc = (XftGlyphSpec *) get_enc_buf (len * sizeof (XftGlyphSpec));
1220 FcChar32 *ep = enc; 1278 XftGlyphSpec *ep = enc;
1221 int ewidth = 0; 1279
1222 int xoff = 0; 1280 // cut trailing spaces
1281 while (len && text [len - 1] == ' ')
1282 len--;
1223 1283
1224 while (len) 1284 while (len)
1225 { 1285 {
1226 int cwidth = r->TermWin.fwidth; 1286 int cwidth = r->fwidth;
1227 FcChar32 fc = *text++; len--; 1287 FcChar32 fc = *text++; len--;
1228 FT_UInt gl;
1229 1288
1230 while (len && *text == NOCHAR) 1289 while (len && *text == NOCHAR)
1231 text++, len--, cwidth += r->TermWin.fwidth; 1290 text++, len--, cwidth += r->fwidth;
1232 1291
1292 if (fc != ' ') // skip spaces
1293 {
1233 gl = XftCharIndex (d.display->display, f, fc); 1294 FT_UInt gl = XftCharIndex (d.display->display, f, fc);
1234 XftGlyphExtents (d.display->display, f, &gl, 1, &extents); 1295 XftGlyphExtents (d.display->display, f, &gl, 1, &extents);
1235 1296
1236 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++;
1237 { 1301 }
1238 if (xoff > ewidth) xoff = ewidth;
1239 XftDrawGlyphs (d, &r->pix_colors[fg].c, f,
1240 x + (ewidth - xoff >> 1),
1241 y + base, enc, ep - enc);
1242 x += ewidth;
1243 1302
1244 ep = enc;
1245 ewidth = 0;
1246 xoff = 0;
1247 }
1248
1249 if (fc == ' ' && ep == enc) // skip leading spaces
1250 {
1251 x += cwidth; 1303 x += cwidth;
1252 continue;
1253 }
1254
1255 else
1256 {
1257 *ep++ = gl;
1258 ewidth += cwidth;
1259 xoff += extents.xOff;
1260 }
1261 } 1304 }
1262 1305
1263 if (ep != enc) 1306 if (ep != enc)
1264 {
1265 if (xoff > ewidth) xoff = ewidth;
1266 XftDrawGlyphs (d, &r->pix_colors[fg].c, f, 1307 XftDrawGlyphSpec (d, &r->pix_colors[fg].c, f, enc, ep - enc);
1267 x + (ewidth - xoff >> 1),
1268 y + base, enc, ep - enc);
1269 }
1270} 1308}
1271#endif 1309#endif
1272 1310
1273///////////////////////////////////////////////////////////////////////////// 1311/////////////////////////////////////////////////////////////////////////////
1274 1312
1275rxvt_fontset::rxvt_fontset (rxvt_t r) 1313rxvt_fontset::rxvt_fontset (rxvt_t r)
1276: r (r), fontdesc (0) 1314: fontdesc (0), r (r)
1277{ 1315{
1278 clear (); 1316 clear ();
1279} 1317}
1280 1318
1281rxvt_fontset::~rxvt_fontset () 1319rxvt_fontset::~rxvt_fontset ()
1284} 1322}
1285 1323
1286void 1324void
1287rxvt_fontset::clear () 1325rxvt_fontset::clear ()
1288{ 1326{
1289 prop.width = prop.height = prop.weight = prop.slant 1327 prop.width = prop.height = prop.ascent = prop.weight = prop.slant
1290 = rxvt_fontprop::unset; 1328 = rxvt_fontprop::unset;
1291 1329
1292 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) 1330 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
1293 FONT_UNREF (*i); 1331 FONT_UNREF (*i);
1294 1332
1346 char buf[512]; 1384 char buf[512];
1347 const char *end; 1385 const char *end;
1348 1386
1349 do 1387 do
1350 { 1388 {
1351 while (*desc <= ' ') desc++; 1389 while (*desc && *desc <= ' ')
1390 desc++;
1352 1391
1353 codeset cs = CS_UNICODE; 1392 codeset cs = CS_UNICODE;
1354 1393
1355 if (*desc == '[') 1394 if (*desc == '[')
1356 { 1395 {
1466 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1505 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
1467 goto next_font; 1506 goto next_font;
1468 1507
1469 if (!realize_font (i)) 1508 if (!realize_font (i))
1470 goto next_font; 1509 goto next_font;
1510
1511 if (prop.ascent != rxvt_fontprop::unset)
1512 max_it (f->ascent, prop.ascent);
1471 } 1513 }
1472 1514
1473 if (f->cs == CS_UNKNOWN) 1515 if (f->cs == CS_UNKNOWN)
1474 goto next_font; 1516 goto next_font;
1475 1517
1514 FcPatternAddInteger (p, FC_SLANT, prop.slant); 1556 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1515 FcPatternAddBool (p, FC_MINSPACE, 1); 1557 FcPatternAddBool (p, FC_MINSPACE, 1);
1516 //FcPatternAddBool (p, FC_ANTIALIAS, 1); 1558 //FcPatternAddBool (p, FC_ANTIALIAS, 1);
1517 1559
1518 XftResult result; 1560 XftResult result;
1519 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); 1561 FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result);
1520 1562
1521 FcPatternDestroy (p); 1563 FcPatternDestroy (p);
1522 1564
1523 if (match) 1565 if (match)
1524 { 1566 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines