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.3 by pcg, Thu Nov 27 20:09:00 2003 UTC vs.
Revision 1.19 by pcg, Fri Feb 13 13:28:17 2004 UTC

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;
270 285
271 if (t >= 0x2500 & t <= 0x2580 && linedraw_cmds[t - 0x2500]) 286 if (t >= 0x2500 & t <= 0x2580 && linedraw_cmds[t - 0x2500])
272 { 287 {
273 const char *p = linedraw_cmds[t - 0x2500]; 288 const char *p = linedraw_cmds[t - 0x2500];
274 289
275 int x0 = x, x1 = x + (r->TermWin.fwidth - 1) / 2, x2 = x + r->TermWin.fwidth - 1; 290 int x0 = x, x1 = x + r->TermWin.fwidth / 2, x2 = x + r->TermWin.fwidth ;
276 int y0 = y, y1 = y + (r->TermWin.fheight - 1) / 2, y2 = y + r->TermWin.fheight - 1; 291 int y0 = y, y1 = y + r->TermWin.fheight / 2, y2 = y + r->TermWin.fheight;
277 292
278 XGCValues gcv; 293 XGCValues gcv;
279
280 gcv.cap_style = CapButt;
281 XChangeGC (DISPLAY, GC, GCCapStyle, &gcv);
282 294
283 while (*p) 295 while (*p)
284 { 296 {
285 switch (*p++) 297 switch (*p++)
286 { 298 {
325struct rxvt_font_x11 : rxvt_font { 337struct rxvt_font_x11 : rxvt_font {
326 rxvt_font_x11 () { f = 0; } 338 rxvt_font_x11 () { f = 0; }
327 339
328 void clear (); 340 void clear ();
329 341
330 bool load (int maxheight); 342 rxvt_fontprop properties ();
343
344 bool load (const rxvt_fontprop &prop);
331 345
332 bool has_codepoint (uint32_t unicode); 346 bool has_codepoint (uint32_t unicode);
333 347
334 void draw (int x, int y, 348 void draw (int x, int y,
335 const text_t *text, int len, 349 const text_t *text, int len,
337 351
338 XFontStruct *f; 352 XFontStruct *f;
339 codeset cs; 353 codeset cs;
340 bool enc2b, encm; 354 bool enc2b, encm;
341 355
342 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);
343}; 360};
344 361
345const char * 362const char *
346rxvt_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
347{ 364{
348 unsigned long value; 365 unsigned long value;
349 366
350 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value)) 367 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value))
351 return XGetAtomName (DISPLAY, value); 368 return XGetAtomName (DISPLAY, value);
352 else 369 else
353 return repl; 370 return repl;
354} 371}
355 372
373rxvt_fontprop
374rxvt_font_x11::properties ()
375{
376 rxvt_fontprop p;
377 set_properties (p, f);
378 return p;
379}
380
356bool 381bool
357rxvt_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)
358{ 474{
359 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;
360 514
361 f = XLoadQueryFont (DISPLAY, name); 515 f = XLoadQueryFont (DISPLAY, name);
362 516
363 if (!f) 517 if (!f)
364 return false; 518 return false;
365 519
366 unsigned long value;
367
368 const char *registry = get_property ("CHARSET_REGISTRY", 0); 520 const char *registry = get_property (f, "CHARSET_REGISTRY", 0);
369 const char *encoding = get_property ("CHARSET_ENCODING", 0); 521 const char *encoding = get_property (f, "CHARSET_ENCODING", 0);
370 522
371 if (registry && encoding) 523 if (registry && encoding)
372 { 524 {
373 char charset[64]; 525 char charset[64];
374 snprintf (charset, 64, "%s-%s", registry, encoding); 526 snprintf (charset, 64, "%s-%s", registry, encoding);
375 527
376 cs = codeset_from_name (charset); 528 cs = codeset_from_name (charset);
377 } 529 }
378 else 530 else
379 { 531 {
380 const char *charset = get_property ("FONT", 0); 532 const char *charset = get_property (f, "FONT", 0);
381 533
382 if (!charset) 534 if (!charset)
383 charset = name; 535 charset = name;
384 536
385 int count = 13; 537 int count = 13;
398 550
399 ascent = f->ascent; 551 ascent = f->ascent;
400 descent = f->descent; 552 descent = f->descent;
401 height = ascent + descent; 553 height = ascent + descent;
402 554
403 prop = false; 555 slow = false;
404 556
405 if (f->min_bounds.width == f->max_bounds.width) 557 if (f->min_bounds.width == f->max_bounds.width)
406 width = f->min_bounds.width; 558 width = f->min_bounds.width;
407 else if (f->per_char == NULL) 559 else if (f->per_char == NULL)
408 width = f->max_bounds.width; 560 width = f->max_bounds.width;
409 else 561 else
410 { 562 {
411 prop = true; 563 slow = true;
412 564
413 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;
414 566
415 if (encm) 567 if (encm)
416 N += (f->max_byte1 - f->min_byte1) 568 N += (f->max_byte1 - f->min_byte1)
458 /* check wether the character exists in _this_ font. horrible. */ 610 /* check wether the character exists in _this_ font. horrible. */
459 XCharStruct *xcs; 611 XCharStruct *xcs;
460 612
461 if (encm) 613 if (encm)
462 { 614 {
463 int byte1 = ch >> 8; 615 unsigned char byte1 = ch >> 8;
464 int byte2 = ch & 255; 616 unsigned char byte2 = ch & 255;
465 617
466 if (byte1 < f->min_byte1 || byte1 > f->max_byte1 618 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
467 || 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)
468 return false; 620 return false;
469 621
501 // this looks like a mess /. 653 // this looks like a mess /.
502 // and it is a mess /. 654 // and it is a mess /.
503 // yet we are trying to be perfect /. 655 // yet we are trying to be perfect /.
504 // but the result still isn't perfect /. 656 // but the result still isn't perfect /.
505 657
506 bool slow = prop 658 bool slow = this->slow
507 || width != r->TermWin.fwidth 659 || width != r->TermWin.fwidth
508 || height != r->TermWin.fheight; 660 || height != r->TermWin.fheight;
509 661
510 int base = r->TermWin.fbase; 662 int base = r->TermWin.fbase;
511 663
586//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables 738//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
587#define SWATHBITS 8 739#define SWATHBITS 8
588#endif 740#endif
589 741
590struct rxvt_font_xft : rxvt_font { 742struct rxvt_font_xft : rxvt_font {
591#if 0
592 enum {
593 SWATHCOUNT = 1 << (21 - UNIBITS),
594 SWATHSIZE = 1 << (SWATHBITS - 5)
595 };
596 typedef uint32_t swath[SWATHSIZE];
597
598 swath *cvr[SWATHCOUNT];
599#endif
600
601#if 0
602 void gen_coverage_swath (unsigned int page);
603
604 bool has_char (uint32_t ch)
605 {
606 unsigned int page = ch >> SWATHBITS;
607 unsigned int idx = ch & ((1 << SWATHBITS) - 1);
608
609 if (page >= SWATHCOUNT)
610 return false;
611
612 if (!cvr[page]) gen_coverage_swath (page);
613
614 return cvr[page][idx >> 5] & (1 << (idx & 31));
615 }
616#endif
617 rxvt_font_xft () { f = 0; d = 0; } 743 rxvt_font_xft () { f = 0; d = 0; }
618 744
619 void clear (); 745 void clear ();
620 746
621 bool load (int maxheight); 747 rxvt_fontprop properties ();
748
749 bool load (const rxvt_fontprop &prop);
622 750
623 void draw (int x, int y, 751 void draw (int x, int y,
624 const text_t *text, int len, 752 const text_t *text, int len,
625 int fg, int bg); 753 int fg, int bg);
626 754
627 bool has_codepoint (uint32_t unicode); 755 bool has_codepoint (uint32_t unicode);
628 756
629protected: 757protected:
630 XftFont *f; 758 XftFont *f;
631 XftDraw *d; 759 XftDraw *d;
632
633#if 0
634 virtual void populate_coverage_swath (uint32_t lo, uint32_t hi) = 0;
635 void set_swath (uint32_t ch)
636 {
637 cvr[ch >> SWATHBITS] |= 1 << (ch & ((1 << SWATHBITS) - 1));
638 }
639#endif
640}; 760};
641 761
642void 762void
643rxvt_font_xft::clear () 763rxvt_font_xft::clear ()
644{ 764{
651 if (d) 771 if (d)
652 { 772 {
653 XftDrawDestroy (d); 773 XftDrawDestroy (d);
654 d = 0; 774 d = 0;
655 } 775 }
776}
656 777
657#if 0 778rxvt_fontprop
658 for (int i = 0; i < SWATHCOUNT; i++) 779rxvt_font_xft::properties ()
659 delete cvr[i]; 780{
660#endif 781 rxvt_fontprop p;
782
783 FT_Face face = XftLockFace (f);
784
785 p.width = width; p.height = height;
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;
661} 792}
662 793
663bool 794bool
664rxvt_font_xft::load (int maxheight) 795rxvt_font_xft::load (const rxvt_fontprop &prop)
665{ 796{
666#if 0 797#if 0
667 for (int i = 0; i < SWATHCOUNT; i++) 798 for (int i = 0; i < SWATHCOUNT; i++)
668 cvr[i] = 0; 799 cvr[i] = 0;
669#endif 800#endif
670 801
671 clear (); 802 clear ();
672 803
673 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);
674 834
675 if (!f) 835 if (!f)
836 {
837 FcPatternDestroy (match);
676 return false; 838 return false;
839 }
677 840
678 FT_Face face = XftLockFace (f); 841 FT_Face face = XftLockFace (f);
679 842
680 prop = !FT_IS_FIXED_WIDTH (face); 843 slow = !FT_IS_FIXED_WIDTH (face);
681 844
682 int ftheight = 0; 845 int ftheight = 0;
683 846
684 for (;;) 847 for (;;)
685 { 848 {
687 FcChar8 c; 850 FcChar8 c;
688 851
689 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1); 852 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1);
690 c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2); 853 c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2);
691 854
692 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;
693 857
694 width = g2.xOff; 858 width = g2.xOff;
695 ascent = (face->size->metrics.ascender + 63) >> 6; 859 ascent = (face->size->metrics.ascender + 63) >> 6;
696 descent = (-face->size->metrics.descender + 63) >> 6; 860 descent = (-face->size->metrics.descender + 63) >> 6;
697 height = ascent + descent; 861 height = ascent + descent;
698 862
699 if (height <= maxheight || !maxheight) 863 if (height <= prop.height || !prop.height)
700 break; 864 break;
701 865
702 if (ftheight) 866 if (ftheight)
703 { 867 {
704 // take smaller steps near the end 868 // take smaller steps near the end
705 if (height > maxheight + 1) ftheight++; 869 if (height > prop.height + 1) ftheight++;
706 if (height > maxheight + 2) ftheight++; 870 if (height > prop.height + 2) ftheight++;
707 if (height > maxheight + 3) ftheight++; 871 if (height > prop.height + 3) ftheight++;
708 872
709 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - maxheight); 873 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - prop.height);
710 } 874 }
711 else 875 else
712 FT_Set_Pixel_Sizes (face, 0, ftheight = maxheight); 876 FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height);
713 } 877 }
714 878
715 XftUnlockFace (f); 879 XftUnlockFace (f);
716 880
717 return true; 881 return true;
718} 882}
719
720#if 0
721void rxvt_font::gen_coverage_swath (unsigned int page)
722{
723 cvr[page] = new swath;
724
725 for (int i = 0; i < SWATHSIZE; i++)
726 cvr[page][i] = 0;
727
728 populate_coverage_swath (cvr[page], page << SWATHBITS, ((page + 1) << SWATHBITS) - 1);
729}
730#endif
731 883
732bool 884bool
733rxvt_font_xft::has_codepoint (uint32_t unicode) 885rxvt_font_xft::has_codepoint (uint32_t unicode)
734{ 886{
735 return XftCharExists (DISPLAY, f, unicode); 887 return XftCharExists (DISPLAY, f, unicode);
739rxvt_font_xft::draw (int x, int y, 891rxvt_font_xft::draw (int x, int y,
740 const text_t *text, int len, 892 const text_t *text, int len,
741 int fg, int bg) 893 int fg, int bg)
742{ 894{
743 if (!d) 895 if (!d)
744 { 896 d = XftDrawCreate (DISPLAY, DRAWABLE, r->display->visual, r->display->cmap);
745 dR;
746 d = XftDrawCreate (DISPLAY, DRAWABLE, XVISUAL, XCMAP);
747 }
748 897
749 if (bg >= 0 && bg != Color_bg) 898 if (bg >= 0 && bg != Color_bg)
750 XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight); 899 XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight);
751 else 900 else
752 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 901 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
753 902
754 if (!prop && width == r->TermWin.fwidth) 903 if (!slow && width == r->TermWin.fwidth && 0)
755 { 904 {
756 if (sizeof (text_t) == sizeof (FcChar16)) 905 if (sizeof (text_t) == sizeof (FcChar16))
757 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len); 906 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len);
758 else 907 else
759 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len); 908 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len);
762 { 911 {
763 while (len) 912 while (len)
764 { 913 {
765 if (*text != NOCHAR && *text != ' ') 914 if (*text != NOCHAR && *text != ' ')
766 { 915 {
916 int fwidth = r->TermWin.fwidth;
917 if (len >= 2 && text[1] == NOCHAR)
918 fwidth *= 2;
919
920 XGlyphInfo extents;
767 if (sizeof (text_t) == sizeof (FcChar16)) 921 if (sizeof (text_t) == sizeof (FcChar16))
768 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, 1); 922 {
923 XftTextExtents16 (DISPLAY, f, (const FcChar16 *)text, 1, &extents);
924 XftDrawString16 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
925 y + r->TermWin.fbase, (const FcChar16 *)text, 1);
926 }
769 else 927 else
770 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, 1); 928 {
929 XGlyphInfo extents;
930 XftTextExtents32 (DISPLAY, f, (const FcChar32 *)text, 1, &extents);
931 XftDrawString32 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
932 y + r->TermWin.fbase, (const FcChar32 *)text, 1);
933 }
771 } 934 }
772 935
773 x += r->TermWin.fwidth; 936 x += r->TermWin.fwidth;
774 text++; 937 text++;
775 len--; 938 len--;
779#endif 942#endif
780 943
781///////////////////////////////////////////////////////////////////////////// 944/////////////////////////////////////////////////////////////////////////////
782 945
783rxvt_fontset::rxvt_fontset (rxvt_t r) 946rxvt_fontset::rxvt_fontset (rxvt_t r)
784#ifdef EXPLICIT_CONTEXT
785: r(r) 947: r (r)
786#endif
787{ 948{
788 clear (); 949 clear ();
789} 950}
790 951
791rxvt_fontset::~rxvt_fontset () 952rxvt_fontset::~rxvt_fontset ()
794} 955}
795 956
796void 957void
797rxvt_fontset::clear () 958rxvt_fontset::clear ()
798{ 959{
799 for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++) 960 for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++)
800 FONT_UNREF (*i); 961 FONT_UNREF (*i);
801 962
802 fonts.clear (); 963 fonts.clear ();
803 base_id = 0; 964 base_id = 0;
804 height = 0x7fffffff; 965 base_prop.height = 0x7fffffff;
966 base_prop.weight = rxvt_fontprop::medium;
967 base_prop.slant = rxvt_fontprop::roman;
805 968
806 fallback = fallback_fonts; 969 fallback = fallback_fonts;
807} 970}
808 971
809rxvt_font * 972rxvt_font *
856 1019
857 if (*desc == '[') 1020 if (*desc == '[')
858 { 1021 {
859 fprintf (stderr, "extra font parameters not yet supported, skipping.\n"); 1022 fprintf (stderr, "extra font parameters not yet supported, skipping.\n");
860 1023
861 const char *extra = desc++; 1024 //const char *extra = desc++; // not yet used
862 1025
863 desc = strchr (desc, ']'); 1026 desc = strchr (desc, ']');
864 1027
865 if (!desc) 1028 if (!desc)
866 { 1029 {
894rxvt_fontset::realize_font (int i) 1057rxvt_fontset::realize_font (int i)
895{ 1058{
896 if (fonts[i]->loaded) 1059 if (fonts[i]->loaded)
897 return true; 1060 return true;
898 1061
899 if (fonts[i]->load (height))
900 return fonts[i]->loaded = true; 1062 fonts[i]->loaded = true;
901 1063
902 delete fonts[i]; 1064 if (!fonts[i]->load (base_prop))
903 fonts.erase (fonts.begin () + i); 1065 {
904 1066 fonts[i]->cs = CS_UNKNOWN;
905 return false; 1067 return false;
1068 }
1069
1070 return true;
906} 1071}
907 1072
908void 1073void
909rxvt_fontset::populate (const char *desc) 1074rxvt_fontset::populate (const char *desc)
910{ 1075{
917 1082
918 if (!base_id) 1083 if (!base_id)
919 base_id = 1; 1084 base_id = 1;
920 1085
921 // we currently need a base-font, no matter what 1086 // we currently need a base-font, no matter what
922 if (fonts.size () <= base_id) 1087 if ((int)fonts.size () <= base_id || !realize_font (base_id))
923 { 1088 {
1089 puts ("unable to load specified font (s), falling back to 'fixed'\n");
924 add_fonts ("fixed"); 1090 add_fonts ("fixed");
925 base_id = 1; 1091 base_id = fonts.size () - 1;
926 } 1092 }
927 1093
928 if (fonts.size () <= base_id || !realize_font (base_id)) 1094 if ((int)fonts.size () <= base_id || !realize_font (base_id))
929 { 1095 {
930 fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n"); 1096 fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n");
931 exit (1); 1097 exit (1);
932 } 1098 }
933 1099
934 height = fonts[base_id]->height; 1100 base_prop = fonts[base_id]->properties ();
935
936 /*add_fonts ("-efont-fixed-medium-r-normal-*-14-*-*-*-*-*-iso10646-1,"*/
937} 1101}
938 1102
939int 1103int
940rxvt_fontset::find_font (uint32_t unicode) 1104rxvt_fontset::find_font (uint32_t unicode)
941{ 1105{
942 for (int i = 0; i < fonts.size (); i++) 1106 for (unsigned int i = 0; i < fonts.size (); i++)
943 { 1107 {
944 rxvt_font *f = fonts[i]; 1108 rxvt_font *f = fonts[i];
945 1109
946 if (!f->loaded) 1110 if (!f->loaded)
947 { 1111 {
948 if (FROM_UNICODE (f->cs, unicode) == NOCHAR) 1112 if (FROM_UNICODE (f->cs, unicode) == NOCHAR)
949 goto next_font; 1113 goto next_font;
950 1114
951 if (!realize_font (i)) 1115 if (!realize_font (i))
952 {
953 --i;
954 goto next_font; 1116 goto next_font;
955 } 1117 }
956 1118
957 //printf ("added font %s for %04lx\n", f->name, unicode);
958 }
959
960 if (f->has_codepoint (unicode)) 1119 if (f->cs != CS_UNKNOWN && f->has_codepoint (unicode))
961 return i; 1120 return i;
962 1121
963 next_font: 1122 next_font:
964 if (i == fonts.size () - 1 && fallback->name) 1123 if (i == fonts.size () - 1 && fallback->name)
965 { 1124 {
966 fonts.push_back (new_font (fallback->name, fallback->cs)); 1125 fonts.push_back (new_font (fallback->name, fallback->cs));
967 fallback++; 1126 fallback++;
1127 i = 0;
968 } 1128 }
969 } 1129 }
970 1130
971 return 0; /* we must return SOME font */ 1131 return 0; /* we must return SOME font */
972} 1132}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines