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.21 by pcg, Sat Feb 21 20:33:40 2004 UTC vs.
Revision 1.29 by pcg, Wed Mar 3 20:15:18 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
24#include "defaultfont.h" 24#include "defaultfont.h"
25 25
26#include <cstdlib> 26#include <cstdlib>
27 27
28#define DISPLAY r->display->display 28#define DISPLAY r->display->display
29#define DRAWABLE r->TermWin.vt
30#define GC r->TermWin.gc 29#define GC r->TermWin.gc
31 30
32const struct rxvt_fallback_font { 31const struct rxvt_fallback_font {
33 codeset cs; 32 codeset cs;
34 const char *name; 33 const char *name;
73 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, 72 { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" },
74 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, 73 { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" },
75 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, 74 { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" },
76 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, 75 { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" },
77# endif 76# endif
77 { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" },
78 { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" },
79 { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" },
78 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" }, 80 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" },
79 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" }, 81 { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" },
80 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" }, 82 { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" },
81 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" }, 83 { CS_CNS11643_1992_3, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-3" },
82 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" }, 84 { CS_CNS11643_1992_4, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-4" },
83 { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-5" }, 85 { CS_CNS11643_1992_5, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-5" },
84 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" }, 86 { CS_CNS11643_1992_6, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-6" },
85 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" }, 87 { CS_CNS11643_1992_7, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-7" },
86 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" }, 88 { CS_CNS11643_1992_F, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-f" },
87#endif 89#endif
88 90
89#if XFT 91#if XFT
90 { CS_UNICODE, "xft:Andale Mono" }, 92 { CS_UNICODE, "xft:Andale Mono" },
91 { CS_UNICODE, "xft:Arial Unicode MS" }, 93 { CS_UNICODE, "xft:Arial Unicode MS" },
99 { CS_UNKNOWN, 0 } 101 { CS_UNKNOWN, 0 }
100}; 102};
101 103
102///////////////////////////////////////////////////////////////////////////// 104/////////////////////////////////////////////////////////////////////////////
103 105
106#if XFT
107rxvt_drawable::~rxvt_drawable ()
108{
109 if (xftdrawable)
110 XftDrawDestroy (xftdrawable);
111}
112
113rxvt_drawable::operator XftDraw *()
114{
115 if (!xftdrawable)
116 xftdrawable = XftDrawCreate (display->display, drawable, display->visual, display->cmap);
117
118 return xftdrawable;
119}
120#endif
121
122/////////////////////////////////////////////////////////////////////////////
123
104static void *enc_buf; 124static void *enc_buf;
105static uint32_t enc_len; 125static uint32_t enc_len;
106 126
107static inline void * 127static inline void *
108get_enc_buf (uint32_t len) 128get_enc_buf (uint32_t len)
161} 181}
162 182
163///////////////////////////////////////////////////////////////////////////// 183/////////////////////////////////////////////////////////////////////////////
164 184
165void 185void
166rxvt_font::clear_rect (int x, int y, int w, int h, int color) 186rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color)
167{ 187{
168 if (color == Color_bg) 188 if (color == Color_bg)
169 XClearArea (DISPLAY, DRAWABLE, x, y, w, h, FALSE); 189 XClearArea (d.display->display, d, x, y, w, h, FALSE);
170 else if (color >= 0) 190 else if (color >= 0)
171 { 191 {
192#if XFT
193 XftDrawRect (d, &r->PixColors[color].c, x, y, w, h);
194#else
172 XSetForeground (DISPLAY, GC, r->PixColors[color]); 195 XSetForeground (d.display->display, GC, r->PixColors[color]);
173 XFillRectangle (DISPLAY, DRAWABLE, GC, x, y, w, h); 196 XFillRectangle (d.display->display, d, GC, x, y, w, h);
197#endif
174 } 198 }
175} 199}
176 200
177static const char *linedraw_cmds[128] = { 201static const char *linedraw_cmds[128] = {
178 "1hH", "2hH", "1vV", "2vV", 202 "1hH", "2hH", "1vV", "2vV",
263 } 287 }
264 288
265 return false; 289 return false;
266 } 290 }
267 291
268 void draw (int x, int y, 292 void draw (rxvt_drawable &d, int x, int y,
269 const text_t *text, int len, 293 const text_t *text, int len,
270 int fg, int bg); 294 int fg, int bg);
271}; 295};
272 296
273void 297void
274rxvt_font_default::draw (int x, int y, 298rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
275 const text_t *text, int len, 299 const text_t *text, int len,
276 int fg, int bg) 300 int fg, int bg)
277{ 301{
278 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 302 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
279 303
280 XSetForeground (DISPLAY, GC, r->PixColors[fg]); 304 XSetForeground (d.display->display, GC, r->PixColors[fg]);
281 305
282 while (len--) 306 while (len--)
283 { 307 {
284 text_t t = *text++; 308 text_t t = *text++;
285 309
310 // is it in our linedrawing table?
286 if (t >= 0x2500 & t <= 0x2580 && linedraw_cmds[t - 0x2500]) 311 if (t >= 0x2500 & t <= 0x2580 && linedraw_cmds[t - 0x2500])
287 { 312 {
288 const char *p = linedraw_cmds[t - 0x2500]; 313 const char *p = linedraw_cmds[t - 0x2500];
289 314
290 int x0 = x, x1 = x + r->TermWin.fwidth / 2, x2 = x + r->TermWin.fwidth ; 315 int x0 = x, x1 = x + r->TermWin.fwidth / 2, x2 = x + r->TermWin.fwidth - 1;
291 int y0 = y, y1 = y + r->TermWin.fheight / 2, y2 = y + r->TermWin.fheight; 316 int y0 = y, y1 = y + r->TermWin.fheight / 2, y2 = y + r->TermWin.fheight - 1;
292 317
293 XGCValues gcv; 318 XGCValues gcv;
294 319
295 while (*p) 320 while (*p)
296 { 321 {
297 switch (*p++) 322 switch (*p++)
298 { 323 {
299 case '1': 324 case '1':
300 gcv.line_width = 0; 325 gcv.line_width = 0;
301 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv); 326 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
302 break; 327 break;
303 328
304 case '2': 329 case '2':
305 gcv.line_width = 2; 330 gcv.line_width = 2;
306 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv); 331 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
307 break; 332 break;
308 333
309 case 'h': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y1, x1, y1); break; 334 case 'h': XDrawLine (d.display->display, d, GC, x0, y1, x1, y1); break;
310 case 'H': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y1, x2, y1); break; 335 case 'H': XDrawLine (d.display->display, d, GC, x1, y1, x2, y1); break;
311 case 'v': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y0, x1, y1); break; 336 case 'v': XDrawLine (d.display->display, d, GC, x1, y0, x1, y1); break;
312 case 'V': XDrawLine (DISPLAY, DRAWABLE, GC, x1, y1, x1, y2); break; 337 case 'V': XDrawLine (d.display->display, d, GC, x1, y1, x1, y2); break;
313 case 'a': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y2, x2, y0); break; 338 case 'a': XDrawLine (d.display->display, d, GC, x0, y2, x2, y0); break;
314 case 'b': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y0, x2, y2); break; 339 case 'b': XDrawLine (d.display->display, d, GC, x0, y0, x2, y2); break;
315 } 340 }
316 } 341 }
317 342
318 gcv.line_width = 0; 343 gcv.line_width = 0;
319 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv); 344 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
320 } 345 }
321 else 346 else
322 switch (*text++) 347 switch (t)
323 { 348 {
324 case NOCHAR:
325 case ZERO_WIDTH_CHAR: 349 case ZERO_WIDTH_CHAR:
326 break; 350 break;
327 default: 351 default:
328 XDrawRectangle (DISPLAY, DRAWABLE, GC, x + 2, y + 2, r->TermWin.fwidth - 5, r->TermWin.fheight - 5); 352 int w = 0;
353 while (len > 0 && *text == NOCHAR)
354 {
355 ++text;
356 --len;
357 w += r->TermWin.fwidth;
358 }
359
360 XDrawRectangle (d.display->display, d, GC, x + 2, y + 2,
361 w + r->TermWin.fwidth - 5, r->TermWin.fheight - 5);
362 x += w;
329 } 363 }
330 364
331 x += r->TermWin.fwidth; 365 x += r->TermWin.fwidth;
332 } 366 }
333} 367}
343 377
344 bool load (const rxvt_fontprop &prop); 378 bool load (const rxvt_fontprop &prop);
345 379
346 bool has_codepoint (uint32_t unicode); 380 bool has_codepoint (uint32_t unicode);
347 381
348 void draw (int x, int y, 382 void draw (rxvt_drawable &d, int x, int y,
349 const text_t *text, int len, 383 const text_t *text, int len,
350 int fg, int bg); 384 int fg, int bg);
351 385
352 XFontStruct *f; 386 XFontStruct *f;
353 codeset cs; 387 codeset cs;
408 442
409bool 443bool
410rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name) 444rxvt_font_x11::set_properties (rxvt_fontprop &p, const char *name)
411{ 445{
412 int slashes = 0; 446 int slashes = 0;
413 const char *comp[12]; 447 const char *comp[13];
414 448
415 for (const char *c = name; *c; c++) 449 for (const char *c = name; *c; c++)
416 if (*c == '-') 450 if (*c == '-')
417 { 451 {
418 comp[slashes++] = c + 1; 452 comp[slashes++] = c + 1;
644 678
645 return true; 679 return true;
646} 680}
647 681
648void 682void
649rxvt_font_x11::draw (int x, int y, 683rxvt_font_x11::draw (rxvt_drawable &d, int x, int y,
650 const text_t *text, int len, 684 const text_t *text, int len,
651 int fg, int bg) 685 int fg, int bg)
652{ 686{
653 // this looks like a mess /. 687 // this looks like a mess /.
654 // and it is a mess /. 688 // and it is a mess /.
670 { 704 {
671 const XChar2b *xc = enc_xchar2b (text, len, cs, slow); 705 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
672 706
673 if (bg == Color_bg && !slow) 707 if (bg == Color_bg && !slow)
674 { 708 {
675 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v); 709 XChangeGC (d.display->display, GC, GCForeground | GCBackground | GCFont, &v);
676 XDrawImageString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 710 XDrawImageString16 (d.display->display, d, GC, x, y + base, xc, len);
677 } 711 }
678 else 712 else
679 { 713 {
680 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 714 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
681 715
682 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v); 716 XChangeGC (d.display->display, GC, GCForeground | GCFont, &v);
683 717
684 if (slow) 718 if (slow)
685 { 719 {
686 do 720 do
687 { 721 {
688 if (xc->byte1 || xc->byte2) 722 if (xc->byte1 || xc->byte2)
689 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1); 723 XDrawString16 (d.display->display, d, GC, x, y + base, xc, 1);
690 724
691 x += r->TermWin.fwidth; 725 x += r->TermWin.fwidth;
692 xc++; len--; 726 xc++; len--;
693 } 727 }
694 while (len); 728 while (len);
695 } 729 }
696 else 730 else
697 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 731 XDrawString16 (d.display->display, d, GC, x, y + base, xc, len);
698 } 732 }
699 } 733 }
700 else 734 else
701 { 735 {
702 const char *xc = enc_char (text, len, cs, slow); 736 const char *xc = enc_char (text, len, cs, slow);
703 737
704 if (bg == Color_bg && !slow) 738 if (bg == Color_bg && !slow)
705 { 739 {
706 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v); 740 XChangeGC (d.display->display, GC, GCForeground | GCBackground | GCFont, &v);
707 XDrawImageString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 741 XDrawImageString (d.display->display, d, GC, x, y + base, xc, len);
708 } 742 }
709 else 743 else
710 { 744 {
711 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 745 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
712 746
713 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v); 747 XChangeGC (d.display->display, GC, GCForeground | GCFont, &v);
714 748
715 if (slow) 749 if (slow)
716 { 750 {
717 do 751 do
718 { 752 {
719 if (*xc) 753 if (*xc)
720 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1); 754 XDrawString (d.display->display, d, GC, x, y + base, xc, 1);
721 755
722 x += r->TermWin.fwidth; 756 x += r->TermWin.fwidth;
723 xc++; len--; 757 xc++; len--;
724 } 758 }
725 while (len); 759 while (len);
726 } 760 }
727 else 761 else
728 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 762 XDrawString (d.display->display, d, GC, x, y + base, xc, len);
729 } 763 }
730 } 764 }
731} 765}
732 766
733///////////////////////////////////////////////////////////////////////////// 767/////////////////////////////////////////////////////////////////////////////
738//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables 772//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
739#define SWATHBITS 8 773#define SWATHBITS 8
740#endif 774#endif
741 775
742struct rxvt_font_xft : rxvt_font { 776struct rxvt_font_xft : rxvt_font {
743 rxvt_font_xft () { f = 0; d = 0; } 777 rxvt_font_xft () { f = 0; }
744 778
745 void clear (); 779 void clear ();
746 780
747 rxvt_fontprop properties (); 781 rxvt_fontprop properties ();
748 782
749 bool load (const rxvt_fontprop &prop); 783 bool load (const rxvt_fontprop &prop);
750 784
751 void draw (int x, int y, 785 void draw (rxvt_drawable &d, int x, int y,
752 const text_t *text, int len, 786 const text_t *text, int len,
753 int fg, int bg); 787 int fg, int bg);
754 788
755 bool has_codepoint (uint32_t unicode); 789 bool has_codepoint (uint32_t unicode);
756 790
757protected: 791protected:
758 XftFont *f; 792 XftFont *f;
759 XftDraw *d;
760}; 793};
761 794
762void 795void
763rxvt_font_xft::clear () 796rxvt_font_xft::clear ()
764{ 797{
765 if (d)
766 {
767 XftDrawDestroy (d);
768 d = 0;
769 }
770
771 if (f) 798 if (f)
772 { 799 {
773 XftFontClose (DISPLAY, f); 800 XftFontClose (DISPLAY, f);
774 f = 0; 801 f = 0;
775 } 802 }
886{ 913{
887 return XftCharExists (DISPLAY, f, unicode); 914 return XftCharExists (DISPLAY, f, unicode);
888} 915}
889 916
890void 917void
891rxvt_font_xft::draw (int x, int y, 918rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
892 const text_t *text, int len, 919 const text_t *text, int len,
893 int fg, int bg) 920 int fg, int bg)
894{ 921{
895 d = XftDrawCreate (DISPLAY, DRAWABLE, r->display->visual, r->display->cmap);
896
897 if (bg >= 0 && bg != Color_bg)
898 XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight);
899 else
900 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 922 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
901 923
902 if (!slow && width == r->TermWin.fwidth && 0) 924 if (!slow && width == r->TermWin.fwidth && 0)
903 { 925 {
904 if (sizeof (text_t) == sizeof (FcChar16)) 926 if (sizeof (text_t) == sizeof (FcChar16))
905 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len); 927 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len);
917 fwidth *= 2; 939 fwidth *= 2;
918 940
919 XGlyphInfo extents; 941 XGlyphInfo extents;
920 if (sizeof (text_t) == sizeof (FcChar16)) 942 if (sizeof (text_t) == sizeof (FcChar16))
921 { 943 {
922 XftTextExtents16 (DISPLAY, f, (const FcChar16 *)text, 1, &extents); 944 XftTextExtents16 (d.display->display, f, (const FcChar16 *)text, 1, &extents);
923 XftDrawString16 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2, 945 XftDrawString16 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
924 y + r->TermWin.fbase, (const FcChar16 *)text, 1); 946 y + r->TermWin.fbase, (const FcChar16 *)text, 1);
925 } 947 }
926 else 948 else
927 { 949 {
928 XGlyphInfo extents; 950 XGlyphInfo extents;
929 XftTextExtents32 (DISPLAY, f, (const FcChar32 *)text, 1, &extents); 951 XftTextExtents32 (d.display->display, f, (const FcChar32 *)text, 1, &extents);
930 XftDrawString32 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2, 952 XftDrawString32 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
931 y + r->TermWin.fbase, (const FcChar32 *)text, 1); 953 y + r->TermWin.fbase, (const FcChar32 *)text, 1);
932 } 954 }
933 } 955 }
934 956
1067 } 1089 }
1068 1090
1069 return true; 1091 return true;
1070} 1092}
1071 1093
1072void 1094bool
1073rxvt_fontset::populate (const char *desc) 1095rxvt_fontset::populate (const char *desc)
1074{ 1096{
1075 clear (); 1097 clear ();
1076 1098
1077 fonts.push_back (new_font (0, CS_UNICODE)); 1099 fonts.push_back (new_font (0, CS_UNICODE));
1089 add_fonts ("fixed"); 1111 add_fonts ("fixed");
1090 base_id = fonts.size () - 1; 1112 base_id = fonts.size () - 1;
1091 } 1113 }
1092 1114
1093 if ((int)fonts.size () <= base_id || !realize_font (base_id)) 1115 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1094 { 1116 return false;
1095 fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n");
1096 exit (1);
1097 }
1098 1117
1099 base_prop = fonts[base_id]->properties (); 1118 base_prop = fonts[base_id]->properties ();
1119
1120 return true;
1100} 1121}
1101 1122
1102int 1123int
1103rxvt_fontset::find_font (uint32_t unicode) 1124rxvt_fontset::find_font (uint32_t unicode)
1104{ 1125{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines