ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/defaultfont.C
(Generate patch)

Comparing rxvt-unicode/src/defaultfont.C (file contents):
Revision 1.4 by pcg, Sat Nov 29 18:42:07 2003 UTC vs.
Revision 1.22 by pcg, Sat Feb 21 22:14:00 2004 UTC

1/*--------------------------------*-C-*---------------------------------*; 1/*--------------------------------*-C-*---------------------------------*;
2 * File: defaultfont.C 2 * File: defaultfont.C
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * Copyright (c) 2003 Marc Lehmann rxvt@plan9.de> 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
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
21 21
22#include "../config.h" 22#include "../config.h"
23#include "rxvt.h" 23#include "rxvt.h"
24#include "defaultfont.h" 24#include "defaultfont.h"
25 25
26#include <cstdlib>
27
26#define DISPLAY r->Xdisplay 28#define DISPLAY r->display->display
27#define DRAWABLE r->TermWin.vt 29#define DRAWABLE r->TermWin.vt
28#define GC r->TermWin.gc 30#define GC r->TermWin.gc
29 31
30const struct rxvt_fallback_font { 32const struct rxvt_fallback_font {
31 codeset cs; 33 codeset cs;
57 59
58 // japanese 60 // japanese
59#if ENCODING_JP || ENCODING_JP_EXT 61#if ENCODING_JP || ENCODING_JP_EXT
60# if XFT 62# if XFT
61 // prefer xft for complex scripts 63 // prefer xft for complex scripts
62 { CS_UNICODE, "xft:Kochi Gothic" }, 64 { CS_UNICODE, "xft:Kochi Gothic:antialias=false" },
63# endif 65# endif
64 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" }, 66 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
65 { CS_JIS0208_1983_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" }, 67 { CS_JIS0208_1983_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
66 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, 68 { CS_JIS0212_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0212*-0" },
67#endif 69#endif
68 70
69#if ENCODING_CN || ENCODING_CN_EXT 71#if ENCODING_CN || ENCODING_CN_EXT
70# if XFT 72# if XFT
71 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, 73 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
72 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, 74 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
73 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, 75 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
74 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, 76 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
75# endif 77# endif
78 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
76 { CS_CNS11643_1992_1, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-1" }, 79 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
77 { CS_CNS11643_1992_2, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-2" }, 80 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
78 { CS_CNS11643_1992_3, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-3" }, 81 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
79 { CS_CNS11643_1992_4, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-4" }, 82 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
80 { CS_CNS11643_1992_5, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-5" }, 83 { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-5" },
81 { CS_CNS11643_1992_6, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-6" }, 84 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
82 { CS_CNS11643_1992_7, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-7" }, 85 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
83 { CS_CNS11643_1992_F, "-*-*-*-r-*-*-*-*-*-*-c-*-cns11643.1992-f" }, 86 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
84#endif 87#endif
85 88
86#if XFT 89#if XFT
87 { CS_UNICODE, "xft:Andale Mono" }, 90 { CS_UNICODE, "xft:Andale Mono" },
88 { CS_UNICODE, "xft:Arial Unicode MS" }, 91 { CS_UNICODE, "xft:Arial Unicode MS" },
89#endif 92#endif
90 { CS_UNICODE, "-*-lucidatypewriter-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 93 { CS_UNICODE, "-*-lucidatypewriter-*-*-*-*-*-*-*-*-m-*-iso10646-1" },
94 { CS_UNICODE, "xft:FreeMono" },
95 { CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" },
96 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" },
91 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, 97 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
92 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
93 { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" },
94 98
95 { CS_UNKNOWN, 0 } 99 { CS_UNKNOWN, 0 }
96}; 100};
97 101
98///////////////////////////////////////////////////////////////////////////// 102/////////////////////////////////////////////////////////////////////////////
99 103
100static void *enc_buf; 104static void *enc_buf;
101static uint32_t enc_len; 105static uint32_t enc_len;
102 106
103static inline void * 107static inline void *
104get_enc_buf (int len) 108get_enc_buf (uint32_t len)
105{ 109{
106 if (len > enc_len) 110 if (len > enc_len)
107 { 111 {
108 free (enc_buf); 112 free (enc_buf);
109 enc_buf = malloc (len); 113 enc_buf = malloc (len);
111 115
112 return enc_buf; 116 return enc_buf;
113} 117}
114 118
115static const char * 119static const char *
116enc_char (const text_t *text, int len, codeset cs, bool &zero) 120enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
117{ 121{
118 uint8_t *buf = (uint8_t *)get_enc_buf (len); 122 uint8_t *buf = (uint8_t *)get_enc_buf (len);
119 123
120 while (len--) 124 while (len--)
121 { 125 {
132 136
133 return (const char *)enc_buf; 137 return (const char *)enc_buf;
134} 138}
135 139
136static const XChar2b * 140static const XChar2b *
137enc_xchar2b (const text_t *text, int len, codeset cs, bool &zero) 141enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
138{ 142{
139 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b)); 143 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b));
140 144
141 while (len--) 145 while (len--)
142 { 146 {
220 224
221 // to be done 225 // to be done
222}; 226};
223 227
224struct rxvt_font_default : rxvt_font { 228struct rxvt_font_default : rxvt_font {
225 bool load (int maxheight) 229 rxvt_fontprop properties ()
230 {
231 rxvt_fontprop p;
232
233 p.width = p.height = 1;
234 p.weight = rxvt_fontprop::medium;
235 p.slant = rxvt_fontprop::roman;
236
237 return p;
238 }
239
240 bool load (const rxvt_fontprop &prop)
226 { 241 {
227 width = 1; height = 1; 242 width = 1; height = 1;
228 ascent = 1; descent = 0; 243 ascent = 1; descent = 0;
229 244
230 return true; 245 return true;
322struct rxvt_font_x11 : rxvt_font { 337struct rxvt_font_x11 : rxvt_font {
323 rxvt_font_x11 () { f = 0; } 338 rxvt_font_x11 () { f = 0; }
324 339
325 void clear (); 340 void clear ();
326 341
327 bool load (int maxheight); 342 rxvt_fontprop properties ();
343
344 bool load (const rxvt_fontprop &prop);
328 345
329 bool has_codepoint (uint32_t unicode); 346 bool has_codepoint (uint32_t unicode);
330 347
331 void draw (int x, int y, 348 void draw (int x, int y,
332 const text_t *text, int len, 349 const text_t *text, int len,
334 351
335 XFontStruct *f; 352 XFontStruct *f;
336 codeset cs; 353 codeset cs;
337 bool enc2b, encm; 354 bool enc2b, encm;
338 355
339 const char *get_property (const char *property, const char *repl) const; 356 const char *get_property (XFontStruct *f, const char *property, const char *repl) const;
357 bool set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth);
358 bool set_properties (rxvt_fontprop &p, XFontStruct *f);
359 bool set_properties (rxvt_fontprop &p, const char *name);
340}; 360};
341 361
342const char * 362const char *
343rxvt_font_x11::get_property (const char *property, const char *repl) const 363rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
344{ 364{
345 unsigned long value; 365 unsigned long value;
346 366
347 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value)) 367 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value))
348 return XGetAtomName (DISPLAY, value); 368 return XGetAtomName (DISPLAY, value);
349 else 369 else
350 return repl; 370 return repl;
351} 371}
352 372
373rxvt_fontprop
374rxvt_font_x11::properties ()
375{
376 rxvt_fontprop p;
377 set_properties (p, f);
378 return p;
379}
380
353bool 381bool
354rxvt_font_x11::load (int maxheight) 382rxvt_font_x11::set_properties (rxvt_fontprop &p, int height, const char *weight, const char *slant, int avgwidth)
383{
384 p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2;
385 p.height = height;
386 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
387 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
388
389 return true;
390}
391
392bool
393rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f)
394{
395 const char *weight = get_property (f, "WEIGHT_NAME", "medium");
396 const char *slant = get_property (f, "SLANT", "r");
397
398 unsigned long height;
399 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
400 return false;
401
402 unsigned long avgwidth;
403 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
404 avgwidth = 0;
405
406 return set_properties (p, height, weight, slant, avgwidth);
407}
408
409bool
410rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
411{
412 int slashes = 0;
413 const char *comp[12];
414
415 for (const char *c = name; *c; c++)
416 if (*c == '-')
417 {
418 comp[slashes++] = c + 1;
419 if (slashes >= 13)
420 break;
421 }
422
423 /* can we short-circuit the costly XLoadQueryFont? */
424 if (slashes >= 13
425 && (*comp[ 6] >= '1' && *comp[ 6] <= '9')
426 && (*comp[11] >= '0' && *comp[11] <= '9'))
427 return set_properties (p, atoi (comp[6]), comp[2], comp[3], atoi (comp[11]));
428
429 XFontStruct *f = XLoadQueryFont (DISPLAY, name);
430
431 if (f)
432 {
433 // the font should really exist now. if not, we have a problem
434 // (e.g. if the user did xset fp rehash just when we were searching fonts).
435 // in that case, just return garbage.
436 bool ret = set_properties (p, f);
437 XFreeFont (DISPLAY, f);
438 return ret;
439 }
440 else
441 return false;
442}
443
444// fix the size of scalable fonts
445static void
446fix_scalable (char *buf, const char *name, const rxvt_fontprop &prop)
447{
448 int slashes = 0;
449 const char *size;
450
451 for (const char *c = name; *c; c++)
452 if (*c == '-')
453 {
454 if (slashes == 6)
455 size = c + 1;
456
457 if (++slashes >= 13)
458 break;
459 }
460
461 if (slashes >= 13 && size[0] == '0')
462 {
463 strncpy (buf, name, size - name);
464 buf += size - name;
465 buf += sprintf (buf, "%d", prop.height);
466 strcpy (buf, size + 1);
467 }
468 else
469 strcpy (buf, name);
470}
471
472bool
473rxvt_font_x11::load (const rxvt_fontprop &prop)
355{ 474{
356 clear (); 475 clear ();
476
477 char **list;
478 int count;
479 list = XListFonts (DISPLAY, name, 512, &count);
480 set_name (0);
481
482 if (!list)
483 return false;
484
485 int bestdiff = 0x7fffffff;
486 for (int i = 0; i < count; i++)
487 {
488 rxvt_fontprop p;
489 char fname[1024];
490 fix_scalable (fname, list[i], prop);
491
492 if (!set_properties (p, fname))
493 continue;
494
495 if (p.height > prop.height) // weed out too large fonts
496 continue;
497
498 int diff = (prop.height - p.height) * 32
499 + abs (prop.weight - p.weight)
500 + abs (prop.slant - p.slant );
501
502 if (!name // compare against best found so far
503 || diff < bestdiff)
504 {
505 set_name (strdup (fname));
506 bestdiff = diff;
507 }
508 }
509
510 XFreeFontNames (list);
511
512 if (!name)
513 return false;
357 514
358 f = XLoadQueryFont (DISPLAY, name); 515 f = XLoadQueryFont (DISPLAY, name);
359 516
360 if (!f) 517 if (!f)
361 return false; 518 return false;
362 519
363 unsigned long value;
364
365 const char *registry = get_property ("CHARSET_REGISTRY", 0); 520 const char *registry = get_property (f, "CHARSET_REGISTRY", 0);
366 const char *encoding = get_property ("CHARSET_ENCODING", 0); 521 const char *encoding = get_property (f, "CHARSET_ENCODING", 0);
367 522
368 if (registry && encoding) 523 if (registry && encoding)
369 { 524 {
370 char charset[64]; 525 char charset[64];
371 snprintf (charset, 64, "%s-%s", registry, encoding); 526 snprintf (charset, 64, "%s-%s", registry, encoding);
372 527
373 cs = codeset_from_name (charset); 528 cs = codeset_from_name (charset);
374 } 529 }
375 else 530 else
376 { 531 {
377 const char *charset = get_property ("FONT", 0); 532 const char *charset = get_property (f, "FONT", 0);
378 533
379 if (!charset) 534 if (!charset)
380 charset = name; 535 charset = name;
381 536
382 int count = 13; 537 int count = 13;
395 550
396 ascent = f->ascent; 551 ascent = f->ascent;
397 descent = f->descent; 552 descent = f->descent;
398 height = ascent + descent; 553 height = ascent + descent;
399 554
400 prop = false; 555 slow = false;
401 556
402 if (f->min_bounds.width == f->max_bounds.width) 557 if (f->min_bounds.width == f->max_bounds.width)
403 width = f->min_bounds.width; 558 width = f->min_bounds.width;
404 else if (f->per_char == NULL) 559 else if (f->per_char == NULL)
405 width = f->max_bounds.width; 560 width = f->max_bounds.width;
406 else 561 else
407 { 562 {
408 prop = true; 563 slow = true;
409 564
410 int N = f->max_char_or_byte2 - f->min_char_or_byte2; 565 int N = f->max_char_or_byte2 - f->min_char_or_byte2;
411 566
412 if (encm) 567 if (encm)
413 N += (f->max_byte1 - f->min_byte1) 568 N += (f->max_byte1 - f->min_byte1)
455 /* check wether the character exists in _this_ font. horrible. */ 610 /* check wether the character exists in _this_ font. horrible. */
456 XCharStruct *xcs; 611 XCharStruct *xcs;
457 612
458 if (encm) 613 if (encm)
459 { 614 {
460 int byte1 = ch >> 8; 615 unsigned char byte1 = ch >> 8;
461 int byte2 = ch & 255; 616 unsigned char byte2 = ch & 255;
462 617
463 if (byte1 < f->min_byte1 || byte1 > f->max_byte1 618 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
464 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2) 619 || byte2 < f->min_char_or_byte2 || byte2 > f->max_char_or_byte2)
465 return false; 620 return false;
466 621
498 // this looks like a mess /. 653 // this looks like a mess /.
499 // and it is a mess /. 654 // and it is a mess /.
500 // yet we are trying to be perfect /. 655 // yet we are trying to be perfect /.
501 // but the result still isn't perfect /. 656 // but the result still isn't perfect /.
502 657
503 bool slow = prop 658 bool slow = this->slow
504 || width != r->TermWin.fwidth 659 || width != r->TermWin.fwidth
505 || height != r->TermWin.fheight; 660 || height != r->TermWin.fheight;
506 661
507 int base = r->TermWin.fbase; 662 int base = r->TermWin.fbase;
508 663
583//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables 738//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
584#define SWATHBITS 8 739#define SWATHBITS 8
585#endif 740#endif
586 741
587struct rxvt_font_xft : rxvt_font { 742struct rxvt_font_xft : rxvt_font {
588#if 0
589 enum {
590 SWATHCOUNT = 1 << (21 - UNIBITS),
591 SWATHSIZE = 1 << (SWATHBITS - 5)
592 };
593 typedef uint32_t swath[SWATHSIZE];
594
595 swath *cvr[SWATHCOUNT];
596#endif
597
598#if 0
599 void gen_coverage_swath (unsigned int page);
600
601 bool has_char (uint32_t ch)
602 {
603 unsigned int page = ch >> SWATHBITS;
604 unsigned int idx = ch & ((1 << SWATHBITS) - 1);
605
606 if (page >= SWATHCOUNT)
607 return false;
608
609 if (!cvr[page]) gen_coverage_swath (page);
610
611 return cvr[page][idx >> 5] & (1 << (idx & 31));
612 }
613#endif
614 rxvt_font_xft () { f = 0; d = 0; } 743 rxvt_font_xft () { f = 0; d = 0; }
615 744
616 void clear (); 745 void clear ();
617 746
618 bool load (int maxheight); 747 rxvt_fontprop properties ();
748
749 bool load (const rxvt_fontprop &prop);
619 750
620 void draw (int x, int y, 751 void draw (int x, int y,
621 const text_t *text, int len, 752 const text_t *text, int len,
622 int fg, int bg); 753 int fg, int bg);
623 754
624 bool has_codepoint (uint32_t unicode); 755 bool has_codepoint (uint32_t unicode);
625 756
626protected: 757protected:
627 XftFont *f; 758 XftFont *f;
628 XftDraw *d; 759 XftDraw *d;
629
630#if 0
631 virtual void populate_coverage_swath (uint32_t lo, uint32_t hi) = 0;
632 void set_swath (uint32_t ch)
633 {
634 cvr[ch >> SWATHBITS] |= 1 << (ch & ((1 << SWATHBITS) - 1));
635 }
636#endif
637}; 760};
638 761
639void 762void
640rxvt_font_xft::clear () 763rxvt_font_xft::clear ()
641{ 764{
765 if (d)
766 {
767 XftDrawDestroy (d);
768 d = 0;
769 }
770
642 if (f) 771 if (f)
643 { 772 {
644 XftFontClose (DISPLAY, f); 773 XftFontClose (DISPLAY, f);
645 f = 0; 774 f = 0;
646 } 775 }
776}
647 777
648 if (d) 778rxvt_fontprop
649 { 779rxvt_font_xft::properties ()
650 XftDrawDestroy (d); 780{
651 d = 0; 781 rxvt_fontprop p;
652 }
653 782
654#if 0 783 FT_Face face = XftLockFace (f);
655 for (int i = 0; i < SWATHCOUNT; i++) 784
656 delete cvr[i]; 785 p.width = width; p.height = height;
657#endif 786 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD ? rxvt_fontprop::bold : rxvt_fontprop::medium;
787 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC ? rxvt_fontprop::italic : rxvt_fontprop::roman;
788
789 XftUnlockFace (f);
790
791 return p;
658} 792}
659 793
660bool 794bool
661rxvt_font_xft::load (int maxheight) 795rxvt_font_xft::load (const rxvt_fontprop &prop)
662{ 796{
663#if 0 797#if 0
664 for (int i = 0; i < SWATHCOUNT; i++) 798 for (int i = 0; i < SWATHCOUNT; i++)
665 cvr[i] = 0; 799 cvr[i] = 0;
666#endif 800#endif
667 801
668 clear (); 802 clear ();
669 803
670 f = XftFontOpenName (DISPLAY, DefaultScreen (DISPLAY), name); 804 FcPattern *p = FcNameParse ((FcChar8 *) name);
805
806 if (!p)
807 return false;
808
809 FcValue v;
810
811 if (FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
812 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
813
814 if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
815 FcPatternAddInteger (p, FC_SLANT, prop.slant);
816
817#if 0 // clipping unfortunately destroys our precious double-width-characters
818 // clip width, we can't do better, or can we?
819 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
820 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width);
821#endif
822
823 //FcPatternAddBool (p, FC_MINSPACE, 1);
824
825 XftResult result;
826 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result);
827
828 FcPatternDestroy (p);
829
830 if (!match)
831 return false;
832
833 f = XftFontOpenPattern (DISPLAY, match);
671 834
672 if (!f) 835 if (!f)
836 {
837 FcPatternDestroy (match);
673 return false; 838 return false;
839 }
674 840
675 FT_Face face = XftLockFace (f); 841 FT_Face face = XftLockFace (f);
676 842
677 prop = !FT_IS_FIXED_WIDTH (face); 843 slow = !FT_IS_FIXED_WIDTH (face);
678 844
679 int ftheight = 0; 845 int ftheight = 0;
680 846
681 for (;;) 847 for (;;)
682 { 848 {
684 FcChar8 c; 850 FcChar8 c;
685 851
686 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1); 852 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1);
687 c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2); 853 c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2);
688 854
689 prop = prop || g1.xOff != g2.xOff; // don't simply trust the font 855 if (g1.xOff != g2.xOff) // don't simply trust the font
856 slow = true;
690 857
691 width = g2.xOff; 858 width = g2.xOff;
692 ascent = (face->size->metrics.ascender + 63) >> 6; 859 ascent = (face->size->metrics.ascender + 63) >> 6;
693 descent = (-face->size->metrics.descender + 63) >> 6; 860 descent = (-face->size->metrics.descender + 63) >> 6;
694 height = ascent + descent; 861 height = ascent + descent;
695 862
696 if (height <= maxheight || !maxheight) 863 if (height <= prop.height || !prop.height)
697 break; 864 break;
698 865
699 if (ftheight) 866 if (ftheight)
700 { 867 {
701 // take smaller steps near the end 868 // take smaller steps near the end
702 if (height > maxheight + 1) ftheight++; 869 if (height > prop.height + 1) ftheight++;
703 if (height > maxheight + 2) ftheight++; 870 if (height > prop.height + 2) ftheight++;
704 if (height > maxheight + 3) ftheight++; 871 if (height > prop.height + 3) ftheight++;
705 872
706 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - maxheight); 873 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - prop.height);
707 } 874 }
708 else 875 else
709 FT_Set_Pixel_Sizes (face, 0, ftheight = maxheight); 876 FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height);
710 } 877 }
711 878
712 XftUnlockFace (f); 879 XftUnlockFace (f);
713 880
714 return true; 881 return true;
715} 882}
716
717#if 0
718void rxvt_font::gen_coverage_swath (unsigned int page)
719{
720 cvr[page] = new swath;
721
722 for (int i = 0; i < SWATHSIZE; i++)
723 cvr[page][i] = 0;
724
725 populate_coverage_swath (cvr[page], page << SWATHBITS, ((page + 1) << SWATHBITS) - 1);
726}
727#endif
728 883
729bool 884bool
730rxvt_font_xft::has_codepoint (uint32_t unicode) 885rxvt_font_xft::has_codepoint (uint32_t unicode)
731{ 886{
732 return XftCharExists (DISPLAY, f, unicode); 887 return XftCharExists (DISPLAY, f, unicode);
735void 890void
736rxvt_font_xft::draw (int x, int y, 891rxvt_font_xft::draw (int x, int y,
737 const text_t *text, int len, 892 const text_t *text, int len,
738 int fg, int bg) 893 int fg, int bg)
739{ 894{
740 if (!d) 895 d = XftDrawCreate (DISPLAY, DRAWABLE, r->display->visual, r->display->cmap);
741 {
742 dR;
743 d = XftDrawCreate (DISPLAY, DRAWABLE, XVISUAL, XCMAP);
744 }
745 896
746 if (bg >= 0 && bg != Color_bg) 897 if (bg >= 0 && bg != Color_bg)
747 XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight); 898 XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight);
748 else 899 else
749 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 900 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
750 901
751 if (!prop && width == r->TermWin.fwidth) 902 if (!slow && width == r->TermWin.fwidth && 0)
752 { 903 {
753 if (sizeof (text_t) == sizeof (FcChar16)) 904 if (sizeof (text_t) == sizeof (FcChar16))
754 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len); 905 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len);
755 else 906 else
756 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len); 907 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len);
759 { 910 {
760 while (len) 911 while (len)
761 { 912 {
762 if (*text != NOCHAR && *text != ' ') 913 if (*text != NOCHAR && *text != ' ')
763 { 914 {
915 int fwidth = r->TermWin.fwidth;
916 if (len >= 2 && text[1] == NOCHAR)
917 fwidth *= 2;
918
919 XGlyphInfo extents;
764 if (sizeof (text_t) == sizeof (FcChar16)) 920 if (sizeof (text_t) == sizeof (FcChar16))
765 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, 1); 921 {
922 XftTextExtents16 (DISPLAY, f, (const FcChar16 *)text, 1, &extents);
923 XftDrawString16 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
924 y + r->TermWin.fbase, (const FcChar16 *)text, 1);
925 }
766 else 926 else
767 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, 1); 927 {
928 XGlyphInfo extents;
929 XftTextExtents32 (DISPLAY, f, (const FcChar32 *)text, 1, &extents);
930 XftDrawString32 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
931 y + r->TermWin.fbase, (const FcChar32 *)text, 1);
932 }
768 } 933 }
769 934
770 x += r->TermWin.fwidth; 935 x += r->TermWin.fwidth;
771 text++; 936 text++;
772 len--; 937 len--;
776#endif 941#endif
777 942
778///////////////////////////////////////////////////////////////////////////// 943/////////////////////////////////////////////////////////////////////////////
779 944
780rxvt_fontset::rxvt_fontset (rxvt_t r) 945rxvt_fontset::rxvt_fontset (rxvt_t r)
781#ifdef EXPLICIT_CONTEXT
782: r(r) 946: r (r)
783#endif
784{ 947{
785 clear (); 948 clear ();
786} 949}
787 950
788rxvt_fontset::~rxvt_fontset () 951rxvt_fontset::~rxvt_fontset ()
791} 954}
792 955
793void 956void
794rxvt_fontset::clear () 957rxvt_fontset::clear ()
795{ 958{
796 for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++) 959 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
797 FONT_UNREF (*i); 960 FONT_UNREF (*i);
798 961
799 fonts.clear (); 962 fonts.clear ();
800 base_id = 0; 963 base_id = 0;
801 height = 0x7fffffff; 964 base_prop.height = 0x7fffffff;
965 base_prop.weight = rxvt_fontprop::medium;
966 base_prop.slant = rxvt_fontprop::roman;
802 967
803 fallback = fallback_fonts; 968 fallback = fallback_fonts;
804} 969}
805 970
806rxvt_font * 971rxvt_font *
853 1018
854 if (*desc == '[') 1019 if (*desc == '[')
855 { 1020 {
856 fprintf (stderr, "extra font parameters not yet supported, skipping.\n"); 1021 fprintf (stderr, "extra font parameters not yet supported, skipping.\n");
857 1022
858 const char *extra = desc++; 1023 //const char *extra = desc++; // not yet used
859 1024
860 desc = strchr (desc, ']'); 1025 desc = strchr (desc, ']');
861 1026
862 if (!desc) 1027 if (!desc)
863 { 1028 {
891rxvt_fontset::realize_font (int i) 1056rxvt_fontset::realize_font (int i)
892{ 1057{
893 if (fonts[i]->loaded) 1058 if (fonts[i]->loaded)
894 return true; 1059 return true;
895 1060
896 if (fonts[i]->load (height))
897 return fonts[i]->loaded = true; 1061 fonts[i]->loaded = true;
898 1062
899 delete fonts[i]; 1063 if (!fonts[i]->load (base_prop))
900 fonts.erase (fonts.begin () + i); 1064 {
901 1065 fonts[i]->cs = CS_UNKNOWN;
902 return false; 1066 return false;
1067 }
1068
1069 return true;
903} 1070}
904 1071
905void 1072void
906rxvt_fontset::populate (const char *desc) 1073rxvt_fontset::populate (const char *desc)
907{ 1074{
914 1081
915 if (!base_id) 1082 if (!base_id)
916 base_id = 1; 1083 base_id = 1;
917 1084
918 // we currently need a base-font, no matter what 1085 // we currently need a base-font, no matter what
919 if (fonts.size () <= base_id) 1086 if ((int)fonts.size () <= base_id || !realize_font (base_id))
920 { 1087 {
1088 puts ("unable to load specified font (s), falling back to 'fixed'\n");
921 add_fonts ("fixed"); 1089 add_fonts ("fixed");
922 base_id = 1; 1090 base_id = fonts.size () - 1;
923 } 1091 }
924 1092
925 if (fonts.size () <= base_id || !realize_font (base_id)) 1093 if ((int)fonts.size () <= base_id || !realize_font (base_id))
926 { 1094 {
927 fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n"); 1095 fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n");
928 exit (1); 1096 exit (1);
929 } 1097 }
930 1098
931 height = fonts[base_id]->height; 1099 base_prop = fonts[base_id]->properties ();
932
933 /*add_fonts ("-efont-fixed-medium-r-normal-*-14-*-*-*-*-*-iso10646-1,"*/
934} 1100}
935 1101
936int 1102int
937rxvt_fontset::find_font (uint32_t unicode) 1103rxvt_fontset::find_font (uint32_t unicode)
938{ 1104{
939 for (int i = 0; i < fonts.size (); i++) 1105 for (unsigned int i = 0; i < fonts.size (); i++)
940 { 1106 {
941 rxvt_font *f = fonts[i]; 1107 rxvt_font *f = fonts[i];
942 1108
943 if (!f->loaded) 1109 if (!f->loaded)
944 { 1110 {
945 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1111 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
946 goto next_font; 1112 goto next_font;
947 1113
948 if (!realize_font (i)) 1114 if (!realize_font (i))
949 {
950 --i;
951 goto next_font; 1115 goto next_font;
952 } 1116 }
953 1117
954 //printf ("added font %s for %04lx\n", f->name, unicode);
955 }
956
957 if (f->has_codepoint (unicode)) 1118 if (f->cs != CS_UNKNOWN && f->has_codepoint (unicode))
958 return i; 1119 return i;
959 1120
960 next_font: 1121 next_font:
961 if (i == fonts.size () - 1 && fallback->name) 1122 if (i == fonts.size () - 1 && fallback->name)
962 { 1123 {
963 fonts.push_back (new_font (fallback->name, fallback->cs)); 1124 fonts.push_back (new_font (fallback->name, fallback->cs));
964 fallback++; 1125 fallback++;
1126 i = 0;
965 } 1127 }
966 } 1128 }
967 1129
968 return 0; /* we must return SOME font */ 1130 return 0; /* we must return SOME font */
969} 1131}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines