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.6 by pcg, Wed Dec 17 22:29:03 2003 UTC vs.
Revision 1.7 by pcg, Thu Dec 18 04:14:30 2003 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->Xdisplay
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 {
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
220 222
221 // to be done 223 // to be done
222}; 224};
223 225
224struct rxvt_font_default : rxvt_font { 226struct rxvt_font_default : rxvt_font {
225 bool load (int maxheight) 227 rxvt_fontprop properties ()
228 {
229 rxvt_fontprop p;
230
231 p.height = 1;
232 p.weight = rxvt_fontprop::medium;
233 p.slant = rxvt_fontprop::roman;
234
235 return p;
236 }
237
238 bool load (const rxvt_fontprop &prop)
226 { 239 {
227 width = 1; height = 1; 240 width = 1; height = 1;
228 ascent = 1; descent = 0; 241 ascent = 1; descent = 0;
229 242
230 return true; 243 return true;
322struct rxvt_font_x11 : rxvt_font { 335struct rxvt_font_x11 : rxvt_font {
323 rxvt_font_x11 () { f = 0; } 336 rxvt_font_x11 () { f = 0; }
324 337
325 void clear (); 338 void clear ();
326 339
327 bool load (int maxheight); 340 rxvt_fontprop properties ();
341 rxvt_fontprop properties (XFontStruct *f);
342
343 bool load (const rxvt_fontprop &prop);
328 344
329 bool has_codepoint (uint32_t unicode); 345 bool has_codepoint (uint32_t unicode);
330 346
331 void draw (int x, int y, 347 void draw (int x, int y,
332 const text_t *text, int len, 348 const text_t *text, int len,
334 350
335 XFontStruct *f; 351 XFontStruct *f;
336 codeset cs; 352 codeset cs;
337 bool enc2b, encm; 353 bool enc2b, encm;
338 354
339 const char *get_property (const char *property, const char *repl) const; 355 const char *get_property (XFontStruct *f, const char *property, const char *repl) const;
340}; 356};
341 357
342const char * 358const char *
343rxvt_font_x11::get_property (const char *property, const char *repl) const 359rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
344{ 360{
345 unsigned long value; 361 unsigned long value;
346 362
347 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value)) 363 if (XGetFontProperty (f, XInternAtom (DISPLAY, property, 0), &value))
348 return XGetAtomName (DISPLAY, value); 364 return XGetAtomName (DISPLAY, value);
349 else 365 else
350 return repl; 366 return repl;
351} 367}
352 368
369rxvt_fontprop
370rxvt_font_x11::properties ()
371{
372 return properties (f);
373}
374
375rxvt_fontprop
376rxvt_font_x11::properties (XFontStruct *f)
377{
378 rxvt_fontprop p;
379
380 const char *weight = get_property (f, "WEIGHT_NAME", "medium");
381 const char *slant = get_property (f, "SLANT", "r");
382
383 p.height = height;
384 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
385 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
386
387 return p;
388}
389
353bool 390bool
354rxvt_font_x11::load (int maxheight) 391rxvt_font_x11::load (const rxvt_fontprop &prop)
355{ 392{
356 clear (); 393 clear ();
357 394
358 char **list; 395 char **list;
359 int count; 396 int count;
361 list = XListFontsWithInfo (DISPLAY, name, 128, &count, &info); 398 list = XListFontsWithInfo (DISPLAY, name, 128, &count, &info);
362 399
363 if (!list) 400 if (!list)
364 return false; 401 return false;
365 402
403 int bestdiff = 0x7fffffff;
366 XFontStruct *best = 0; 404 XFontStruct *best = 0;
367 for (int i = 0; i < count; i++) 405 for (int i = 0; i < count; i++)
368 { 406 {
369 XFontStruct *f = info + i; 407 XFontStruct *f = info + i;
408
370 if (f->ascent + f->descent <= maxheight) // weed out too large fonts 409 if (f->ascent + f->descent <= prop.height) // weed out too large fonts
410 {
411 rxvt_fontprop p = properties (f);
412 int diff = (prop.height - f->ascent + f->descent) * 32
413 + abs (prop.weight - p.weight)
414 + abs (prop.slant - p.slant );
415
371 if (!best // compare against best found so far 416 if (!best // compare against best found so far
372 || best->ascent + best->descent < f->ascent + f->descent) 417 || diff < bestdiff)
418 {
373 best = f; 419 best = f;
420 bestdiff = diff;
421 }
422 }
374 } 423 }
375 424
376 set_name (strdup (list[best - info])); 425 set_name (strdup (list[best - info]));
377 426
378 XFreeFontInfo (list, info, count); 427 XFreeFontInfo (list, info, count);
382 if (!f) 431 if (!f)
383 return false; 432 return false;
384 433
385 unsigned long value; 434 unsigned long value;
386 435
387 const char *registry = get_property ("CHARSET_REGISTRY", 0); 436 const char *registry = get_property (f, "CHARSET_REGISTRY", 0);
388 const char *encoding = get_property ("CHARSET_ENCODING", 0); 437 const char *encoding = get_property (f, "CHARSET_ENCODING", 0);
389 438
390 if (registry && encoding) 439 if (registry && encoding)
391 { 440 {
392 char charset[64]; 441 char charset[64];
393 snprintf (charset, 64, "%s-%s", registry, encoding); 442 snprintf (charset, 64, "%s-%s", registry, encoding);
394 443
395 cs = codeset_from_name (charset); 444 cs = codeset_from_name (charset);
396 } 445 }
397 else 446 else
398 { 447 {
399 const char *charset = get_property ("FONT", 0); 448 const char *charset = get_property (f, "FONT", 0);
400 449
401 if (!charset) 450 if (!charset)
402 charset = name; 451 charset = name;
403 452
404 int count = 13; 453 int count = 13;
417 466
418 ascent = f->ascent; 467 ascent = f->ascent;
419 descent = f->descent; 468 descent = f->descent;
420 height = ascent + descent; 469 height = ascent + descent;
421 470
422 prop = false; 471 slow = false;
423 472
424 if (f->min_bounds.width == f->max_bounds.width) 473 if (f->min_bounds.width == f->max_bounds.width)
425 width = f->min_bounds.width; 474 width = f->min_bounds.width;
426 else if (f->per_char == NULL) 475 else if (f->per_char == NULL)
427 width = f->max_bounds.width; 476 width = f->max_bounds.width;
428 else 477 else
429 { 478 {
430 prop = true; 479 slow = true;
431 480
432 int N = f->max_char_or_byte2 - f->min_char_or_byte2; 481 int N = f->max_char_or_byte2 - f->min_char_or_byte2;
433 482
434 if (encm) 483 if (encm)
435 N += (f->max_byte1 - f->min_byte1) 484 N += (f->max_byte1 - f->min_byte1)
520 // this looks like a mess /. 569 // this looks like a mess /.
521 // and it is a mess /. 570 // and it is a mess /.
522 // yet we are trying to be perfect /. 571 // yet we are trying to be perfect /.
523 // but the result still isn't perfect /. 572 // but the result still isn't perfect /.
524 573
525 bool slow = prop 574 bool slow = this->slow
526 || width != r->TermWin.fwidth 575 || width != r->TermWin.fwidth
527 || height != r->TermWin.fheight; 576 || height != r->TermWin.fheight;
528 577
529 int base = r->TermWin.fbase; 578 int base = r->TermWin.fbase;
530 579
635#endif 684#endif
636 rxvt_font_xft () { f = 0; d = 0; } 685 rxvt_font_xft () { f = 0; d = 0; }
637 686
638 void clear (); 687 void clear ();
639 688
640 bool load (int maxheight); 689 rxvt_fontprop properties ();
690
691 bool load (const rxvt_fontprop &prop);
641 692
642 void draw (int x, int y, 693 void draw (int x, int y,
643 const text_t *text, int len, 694 const text_t *text, int len,
644 int fg, int bg); 695 int fg, int bg);
645 696
677 for (int i = 0; i < SWATHCOUNT; i++) 728 for (int i = 0; i < SWATHCOUNT; i++)
678 delete cvr[i]; 729 delete cvr[i];
679#endif 730#endif
680} 731}
681 732
733rxvt_fontprop
734rxvt_font_xft::properties ()
735{
736 rxvt_fontprop p;
737
738 FT_Face face = XftLockFace (f);
739
740 p.height = height;
741 p.weight = face->style_flags & FT_STYLE_FLAG_BOLD ? rxvt_fontprop::bold : rxvt_fontprop::medium;
742 p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC ? rxvt_fontprop::italic : rxvt_fontprop::roman;
743
744 XftUnlockFace (f);
745
746 return p;
747}
748
682bool 749bool
683rxvt_font_xft::load (int maxheight) 750rxvt_font_xft::load (const rxvt_fontprop &prop)
684{ 751{
685#if 0 752#if 0
686 for (int i = 0; i < SWATHCOUNT; i++) 753 for (int i = 0; i < SWATHCOUNT; i++)
687 cvr[i] = 0; 754 cvr[i] = 0;
688#endif 755#endif
689 756
690 clear (); 757 clear ();
691 758
692 f = XftFontOpenName (DISPLAY, DefaultScreen (DISPLAY), name); 759 FcPattern *p = FcNameParse ((FcChar8 *) name);
760
761 if (!p)
762 return false;
763
764 FcValue v;
765
766 if (FcPatternGet (p, FC_WEIGHT, 0, &v) != FcResultMatch)
767 FcPatternAddInteger (p, FC_WEIGHT, prop.weight);
768
769 if (FcPatternGet (p, FC_SLANT, 0, &v) != FcResultMatch)
770 FcPatternAddInteger (p, FC_SLANT, prop.slant);
771
772 //FcPatternAddBool (p, FC_MINSPACE, 1);
773
774 XftResult result;
775 FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result);
776
777 FcPatternDestroy (p);
778
779 if (!match)
780 return false;
781
782 f = XftFontOpenPattern (DISPLAY, match);
783
784 FcPatternDestroy (match);
693 785
694 if (!f) 786 if (!f)
695 return false; 787 return false;
696 788
697 FT_Face face = XftLockFace (f); 789 FT_Face face = XftLockFace (f);
698 790
699 prop = !FT_IS_FIXED_WIDTH (face); 791 slow = !FT_IS_FIXED_WIDTH (face);
700 792
701 int ftheight = 0; 793 int ftheight = 0;
702 794
703 for (;;) 795 for (;;)
704 { 796 {
706 FcChar8 c; 798 FcChar8 c;
707 799
708 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1); 800 c = 'i'; XftTextExtents8 (DISPLAY, f, &c, 1, &g1);
709 c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2); 801 c = 'W'; XftTextExtents8 (DISPLAY, f, &c, 1, &g2);
710 802
711 prop = prop || g1.xOff != g2.xOff; // don't simply trust the font 803 if (g1.xOff != g2.xOff) // don't simply trust the font
804 slow = true;
712 805
713 width = g2.xOff; 806 width = g2.xOff;
714 ascent = (face->size->metrics.ascender + 63) >> 6; 807 ascent = (face->size->metrics.ascender + 63) >> 6;
715 descent = (-face->size->metrics.descender + 63) >> 6; 808 descent = (-face->size->metrics.descender + 63) >> 6;
716 height = ascent + descent; 809 height = ascent + descent;
717 810
718 if (height <= maxheight || !maxheight) 811 if (height <= prop.height || !prop.height)
719 break; 812 break;
720 813
721 if (ftheight) 814 if (ftheight)
722 { 815 {
723 // take smaller steps near the end 816 // take smaller steps near the end
724 if (height > maxheight + 1) ftheight++; 817 if (height > prop.height + 1) ftheight++;
725 if (height > maxheight + 2) ftheight++; 818 if (height > prop.height + 2) ftheight++;
726 if (height > maxheight + 3) ftheight++; 819 if (height > prop.height + 3) ftheight++;
727 820
728 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - maxheight); 821 FT_Set_Pixel_Sizes (face, 0, ftheight -= height - prop.height);
729 } 822 }
730 else 823 else
731 FT_Set_Pixel_Sizes (face, 0, ftheight = maxheight); 824 FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height);
732 } 825 }
733 826
734 XftUnlockFace (f); 827 XftUnlockFace (f);
735 828
736 return true; 829 return true;
768 if (bg >= 0 && bg != Color_bg) 861 if (bg >= 0 && bg != Color_bg)
769 XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight); 862 XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight);
770 else 863 else
771 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 864 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
772 865
773 if (!prop && width == r->TermWin.fwidth) 866 if (!slow && width == r->TermWin.fwidth)
774 { 867 {
775 if (sizeof (text_t) == sizeof (FcChar16)) 868 if (sizeof (text_t) == sizeof (FcChar16))
776 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len); 869 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len);
777 else 870 else
778 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len); 871 XftDrawString32 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar32 *)text, len);
818 for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++) 911 for (rxvt_font **i = fonts.begin (); i != fonts.end(); i++)
819 FONT_UNREF (*i); 912 FONT_UNREF (*i);
820 913
821 fonts.clear (); 914 fonts.clear ();
822 base_id = 0; 915 base_id = 0;
823 height = 0x7fffffff; 916 base_prop.height = 0x7fffffff;
917 base_prop.weight = rxvt_fontprop::medium;
918 base_prop.slant = rxvt_fontprop::roman;
824 919
825 fallback = fallback_fonts; 920 fallback = fallback_fonts;
826} 921}
827 922
828rxvt_font * 923rxvt_font *
915 if (fonts[i]->loaded) 1010 if (fonts[i]->loaded)
916 return true; 1011 return true;
917 1012
918 fonts[i]->loaded = true; 1013 fonts[i]->loaded = true;
919 1014
920 if (!fonts[i]->load (height)) 1015 if (!fonts[i]->load (base_prop))
921 { 1016 {
922 fonts[i]->cs = CS_UNKNOWN; 1017 fonts[i]->cs = CS_UNKNOWN;
923 return false; 1018 return false;
924 } 1019 }
925 1020
938 1033
939 if (!base_id) 1034 if (!base_id)
940 base_id = 1; 1035 base_id = 1;
941 1036
942 // we currently need a base-font, no matter what 1037 // we currently need a base-font, no matter what
943 if (fonts.size () <= base_id) 1038 if (fonts.size () <= base_id || !realize_font (base_id))
944 { 1039 {
945 add_fonts ("fixed"); 1040 add_fonts ("fixed");
946 base_id = 1; 1041 base_id = fonts.size () - 1;
947 } 1042 }
948 1043
949 if (fonts.size () <= base_id || !realize_font (base_id)) 1044 if (fonts.size () <= base_id || !realize_font (base_id))
950 { 1045 {
951 fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n"); 1046 fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n");
952 exit (1); 1047 exit (1);
953 } 1048 }
954 1049
955 height = fonts[base_id]->height; 1050 base_prop = fonts[base_id]->properties ();
956
957 /*add_fonts ("-efont-fixed-medium-r-normal-*-14-*-*-*-*-*-iso10646-1,"*/
958} 1051}
959 1052
960int 1053int
961rxvt_fontset::find_font (uint32_t unicode) 1054rxvt_fontset::find_font (uint32_t unicode)
962{ 1055{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines