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.11 by pcg, Wed Dec 24 09:40:30 2003 UTC vs.
Revision 1.17 by pcg, Mon Feb 9 07:11:49 2004 UTC

23#include "rxvt.h" 23#include "rxvt.h"
24#include "defaultfont.h" 24#include "defaultfont.h"
25 25
26#include <cstdlib> 26#include <cstdlib>
27 27
28#define DISPLAY r->Xdisplay 28#define DISPLAY r->display->display
29#define DRAWABLE r->TermWin.vt 29#define DRAWABLE r->TermWin.vt
30#define GC r->TermWin.gc 30#define GC r->TermWin.gc
31 31
32const struct rxvt_fallback_font { 32const struct rxvt_fallback_font {
33 codeset cs; 33 codeset cs;
73 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, 73 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
74 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, 74 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
75 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, 75 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
76 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, 76 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
77# endif 77# endif
78 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
78 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-1" }, 79 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
79 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-2" }, 80 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
80 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-3" }, 81 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
81 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-4" }, 82 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
82 { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-5" }, 83 { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-5" },
83 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-6" }, 84 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
84 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-7" }, 85 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
85 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643.1992-f" }, 86 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
86#endif 87#endif
87 88
88#if XFT 89#if XFT
89 { CS_UNICODE, "xft:Andale Mono" }, 90 { CS_UNICODE, "xft:Andale Mono" },
90 { CS_UNICODE, "xft:Arial Unicode MS" }, 91 { CS_UNICODE, "xft:Arial Unicode MS" },
102 103
103static void *enc_buf; 104static void *enc_buf;
104static uint32_t enc_len; 105static uint32_t enc_len;
105 106
106static inline void * 107static inline void *
107get_enc_buf (int len) 108get_enc_buf (uint32_t len)
108{ 109{
109 if (len > enc_len) 110 if (len > enc_len)
110 { 111 {
111 free (enc_buf); 112 free (enc_buf);
112 enc_buf = malloc (len); 113 enc_buf = malloc (len);
114 115
115 return enc_buf; 116 return enc_buf;
116} 117}
117 118
118static const char * 119static const char *
119enc_char (const text_t *text, int len, codeset cs, bool &zero) 120enc_char (const text_t *text, uint32_t len, codeset cs, bool &zero)
120{ 121{
121 uint8_t *buf = (uint8_t *)get_enc_buf (len); 122 uint8_t *buf = (uint8_t *)get_enc_buf (len);
122 123
123 while (len--) 124 while (len--)
124 { 125 {
135 136
136 return (const char *)enc_buf; 137 return (const char *)enc_buf;
137} 138}
138 139
139static const XChar2b * 140static const XChar2b *
140enc_xchar2b (const text_t *text, int len, codeset cs, bool &zero) 141enc_xchar2b (const text_t *text, uint32_t len, codeset cs, bool &zero)
141{ 142{
142 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b)); 143 XChar2b *buf = (XChar2b *)get_enc_buf (len * sizeof (XChar2b));
143 144
144 while (len--) 145 while (len--)
145 { 146 {
337 rxvt_font_x11 () { f = 0; } 338 rxvt_font_x11 () { f = 0; }
338 339
339 void clear (); 340 void clear ();
340 341
341 rxvt_fontprop properties (); 342 rxvt_fontprop properties ();
342 rxvt_fontprop properties (XFontStruct *f);
343 343
344 bool load (const rxvt_fontprop &prop); 344 bool load (const rxvt_fontprop &prop);
345 345
346 bool has_codepoint (uint32_t unicode); 346 bool has_codepoint (uint32_t unicode);
347 347
352 XFontStruct *f; 352 XFontStruct *f;
353 codeset cs; 353 codeset cs;
354 bool enc2b, encm; 354 bool enc2b, encm;
355 355
356 const char *get_property (XFontStruct *f, 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);
357}; 360};
358 361
359const char * 362const char *
360rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const 363rxvt_font_x11::get_property (XFontStruct *f, const char *property, const char *repl) const
361{ 364{
368} 371}
369 372
370rxvt_fontprop 373rxvt_fontprop
371rxvt_font_x11::properties () 374rxvt_font_x11::properties ()
372{ 375{
373 return properties (f);
374}
375
376rxvt_fontprop
377rxvt_font_x11::properties (XFontStruct *f)
378{
379 rxvt_fontprop p; 376 rxvt_fontprop p;
377 set_properties (p, f);
378 return p;
379}
380 380
381bool
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{
381 const char *weight = get_property (f, "WEIGHT_NAME", "medium"); 395 const char *weight = get_property (f, "WEIGHT_NAME", "medium");
382 const char *slant = get_property (f, "SLANT", "r"); 396 const char *slant = get_property (f, "SLANT", "r");
383 397
398 unsigned long height;
399 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height))
400 return false;
401
384 unsigned long avgwidth; 402 unsigned long avgwidth;
385 p.width = XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth) 403 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth))
386 ? avgwidth / 10 404 avgwidth = 0;
387 : (height + 1) / 2;
388 p.height = height;
389 p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium;
390 p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic;
391 405
392 return p; 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 exists 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);
393} 470}
394 471
395bool 472bool
396rxvt_font_x11::load (const rxvt_fontprop &prop) 473rxvt_font_x11::load (const rxvt_fontprop &prop)
397{ 474{
398 clear (); 475 clear ();
399 476
400 char **list; 477 char **list;
401 int count; 478 int count;
402 XFontStruct *info;
403 list = XListFontsWithInfo (DISPLAY, name, 128, &count, &info); 479 list = XListFonts (DISPLAY, name, 512, &count);
480 set_name (0);
404 481
405 if (!list) 482 if (!list)
406 return false; 483 return false;
407 484
408 int bestdiff = 0x7fffffff; 485 int bestdiff = 0x7fffffff;
409 XFontStruct *best = 0;
410 for (int i = 0; i < count; i++) 486 for (int i = 0; i < count; i++)
411 { 487 {
412 XFontStruct *f = info + i; 488 rxvt_fontprop p;
489 char fname[1024];
490 fix_scalable (fname, list[i], prop);
413 491
492 if (!set_properties (p, fname))
493 continue;
494
414 if (f->ascent + f->descent <= prop.height) // weed out too large fonts 495 if (p.height > prop.height) // weed out too large fonts
415 { 496 continue;
416 rxvt_fontprop p = properties (f); 497
417 int diff = (prop.height - f->ascent + f->descent) * 32 498 int diff = (prop.height - p.height) * 32
418 + abs (prop.weight - p.weight) 499 + abs (prop.weight - p.weight)
419 + abs (prop.slant - p.slant ); 500 + abs (prop.slant - p.slant );
420 501
421 if (!best // compare against best found so far 502 if (!name // compare against best found so far
422 || diff < bestdiff) 503 || diff < bestdiff)
423 { 504 {
424 best = f; 505 set_name (strdup (fname));
425 bestdiff = diff; 506 bestdiff = diff;
426 } 507 }
427 }
428 } 508 }
429 509
510 XFreeFontNames (list);
511
430 if (!best) 512 if (!name)
431 return false; 513 return false;
432
433 set_name (strdup (list[best - info]));
434
435 XFreeFontInfo (list, info, count);
436 514
437 f = XLoadQueryFont (DISPLAY, name); 515 f = XLoadQueryFont (DISPLAY, name);
438 516
439 if (!f) 517 if (!f)
440 return false; 518 return false;
441
442 unsigned long value;
443 519
444 const char *registry = get_property (f, "CHARSET_REGISTRY", 0); 520 const char *registry = get_property (f, "CHARSET_REGISTRY", 0);
445 const char *encoding = get_property (f, "CHARSET_ENCODING", 0); 521 const char *encoding = get_property (f, "CHARSET_ENCODING", 0);
446 522
447 if (registry && encoding) 523 if (registry && encoding)
534 /* check wether the character exists in _this_ font. horrible. */ 610 /* check wether the character exists in _this_ font. horrible. */
535 XCharStruct *xcs; 611 XCharStruct *xcs;
536 612
537 if (encm) 613 if (encm)
538 { 614 {
539 int byte1 = ch >> 8; 615 unsigned char byte1 = ch >> 8;
540 int byte2 = ch & 255; 616 unsigned char byte2 = ch & 255;
541 617
542 if (byte1 < f->min_byte1 || byte1 > f->max_byte1 618 if (byte1 < f->min_byte1 || byte1 > f->max_byte1
543 || 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)
544 return false; 620 return false;
545 621
815rxvt_font_xft::draw (int x, int y, 891rxvt_font_xft::draw (int x, int y,
816 const text_t *text, int len, 892 const text_t *text, int len,
817 int fg, int bg) 893 int fg, int bg)
818{ 894{
819 if (!d) 895 if (!d)
820 { 896 d = XftDrawCreate (DISPLAY, DRAWABLE, r->display->visual, r->display->cmap);
821 dR;
822 d = XftDrawCreate (DISPLAY, DRAWABLE, XVISUAL, XCMAP);
823 }
824 897
825 if (bg >= 0 && bg != Color_bg) 898 if (bg >= 0 && bg != Color_bg)
826 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);
827 else 900 else
828 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);
829 902
830 if (!slow && width == r->TermWin.fwidth) 903 if (!slow && width == r->TermWin.fwidth && 0)
831 { 904 {
832 if (sizeof (text_t) == sizeof (FcChar16)) 905 if (sizeof (text_t) == sizeof (FcChar16))
833 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);
834 else 907 else
835 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);
838 { 911 {
839 while (len) 912 while (len)
840 { 913 {
841 if (*text != NOCHAR && *text != ' ') 914 if (*text != NOCHAR && *text != ' ')
842 { 915 {
916 int fwidth = r->TermWin.fwidth;
917 if (len >= 2 && text[1] == NOCHAR)
918 fwidth *= 2;
919
920 XGlyphInfo extents;
843 if (sizeof (text_t) == sizeof (FcChar16)) 921 if (sizeof (text_t) == sizeof (FcChar16))
844 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 }
845 else 927 else
846 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 }
847 } 934 }
848 935
849 x += r->TermWin.fwidth; 936 x += r->TermWin.fwidth;
850 text++; 937 text++;
851 len--; 938 len--;
855#endif 942#endif
856 943
857///////////////////////////////////////////////////////////////////////////// 944/////////////////////////////////////////////////////////////////////////////
858 945
859rxvt_fontset::rxvt_fontset (rxvt_t r) 946rxvt_fontset::rxvt_fontset (rxvt_t r)
860#ifdef EXPLICIT_CONTEXT
861: r(r) 947: r(r)
862#endif
863{ 948{
864 clear (); 949 clear ();
865} 950}
866 951
867rxvt_fontset::~rxvt_fontset () 952rxvt_fontset::~rxvt_fontset ()
934 1019
935 if (*desc == '[') 1020 if (*desc == '[')
936 { 1021 {
937 fprintf (stderr, "extra font parameters not yet supported, skipping.\n"); 1022 fprintf (stderr, "extra font parameters not yet supported, skipping.\n");
938 1023
939 const char *extra = desc++; 1024 //const char *extra = desc++; // not yet used
940 1025
941 desc = strchr (desc, ']'); 1026 desc = strchr (desc, ']');
942 1027
943 if (!desc) 1028 if (!desc)
944 { 1029 {
997 1082
998 if (!base_id) 1083 if (!base_id)
999 base_id = 1; 1084 base_id = 1;
1000 1085
1001 // we currently need a base-font, no matter what 1086 // we currently need a base-font, no matter what
1002 if (fonts.size () <= base_id || !realize_font (base_id)) 1087 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1003 { 1088 {
1089 puts ("unable to load specified font(s), falling back to 'fixed'\n");
1004 add_fonts ("fixed"); 1090 add_fonts ("fixed");
1005 base_id = fonts.size () - 1; 1091 base_id = fonts.size () - 1;
1006 } 1092 }
1007 1093
1008 if (fonts.size () <= base_id || !realize_font (base_id)) 1094 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1009 { 1095 {
1010 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");
1011 exit (1); 1097 exit (1);
1012 } 1098 }
1013 1099
1015} 1101}
1016 1102
1017int 1103int
1018rxvt_fontset::find_font (uint32_t unicode) 1104rxvt_fontset::find_font (uint32_t unicode)
1019{ 1105{
1020 for (int i = 0; i < fonts.size (); i++) 1106 for (unsigned int i = 0; i < fonts.size (); i++)
1021 { 1107 {
1022 rxvt_font *f = fonts[i]; 1108 rxvt_font *f = fonts[i];
1023 1109
1024 if (!f->loaded) 1110 if (!f->loaded)
1025 { 1111 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines