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.20 by pcg, Fri Feb 20 22:27:23 2004 UTC vs.
Revision 1.25 by pcg, Tue Feb 24 23:58:12 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;
99 { CS_UNKNOWN, 0 } 98 { CS_UNKNOWN, 0 }
100}; 99};
101 100
102///////////////////////////////////////////////////////////////////////////// 101/////////////////////////////////////////////////////////////////////////////
103 102
103rxvt_drawable::~rxvt_drawable ()
104{
105 if (xftdrawable)
106 XftDrawDestroy (xftdrawable);
107}
108
109rxvt_drawable::operator XftDraw *()
110{
111 if (!xftdrawable)
112 xftdrawable = XftDrawCreate (display->display, drawable, display->visual, display->cmap);
113
114 return xftdrawable;
115}
116
117/////////////////////////////////////////////////////////////////////////////
118
104static void *enc_buf; 119static void *enc_buf;
105static uint32_t enc_len; 120static uint32_t enc_len;
106 121
107static inline void * 122static inline void *
108get_enc_buf (uint32_t len) 123get_enc_buf (uint32_t len)
161} 176}
162 177
163///////////////////////////////////////////////////////////////////////////// 178/////////////////////////////////////////////////////////////////////////////
164 179
165void 180void
166rxvt_font::clear_rect (int x, int y, int w, int h, int color) 181rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color)
167{ 182{
168 if (color == Color_bg) 183 if (color == Color_bg)
169 XClearArea (DISPLAY, DRAWABLE, x, y, w, h, FALSE); 184 XClearArea (d.display->display, d, x, y, w, h, FALSE);
170 else if (color >= 0) 185 else if (color >= 0)
171 { 186 {
187#if XFT
188 XftDrawRect (d, &r->PixColors[color].c, x, y, w, h);
189#else
172 XSetForeground (DISPLAY, GC, r->PixColors[color]); 190 XSetForeground (d.display->display, GC, r->PixColors[color]);
173 XFillRectangle (DISPLAY, DRAWABLE, GC, x, y, w, h); 191 XFillRectangle (d.display->display, d, GC, x, y, w, h);
192#endif
174 } 193 }
175} 194}
176 195
177static const char *linedraw_cmds[128] = { 196static const char *linedraw_cmds[128] = {
178 "1hH", "2hH", "1vV", "2vV", 197 "1hH", "2hH", "1vV", "2vV",
263 } 282 }
264 283
265 return false; 284 return false;
266 } 285 }
267 286
268 void draw (int x, int y, 287 void draw (rxvt_drawable &d, int x, int y,
269 const text_t *text, int len, 288 const text_t *text, int len,
270 int fg, int bg); 289 int fg, int bg);
271}; 290};
272 291
273void 292void
274rxvt_font_default::draw (int x, int y, 293rxvt_font_default::draw (rxvt_drawable &d, int x, int y,
275 const text_t *text, int len, 294 const text_t *text, int len,
276 int fg, int bg) 295 int fg, int bg)
277{ 296{
278 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 297 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
279 298
280 XSetForeground (DISPLAY, GC, r->PixColors[fg]); 299 XSetForeground (d.display->display, GC, r->PixColors[fg]);
281 300
282 while (len--) 301 while (len--)
283 { 302 {
284 text_t t = *text++; 303 text_t t = *text++;
285 304
286 if (t >= 0x2500 & t <= 0x2580 && linedraw_cmds[t - 0x2500]) 305 if (t >= 0x2500 & t <= 0x2580 && linedraw_cmds[t - 0x2500])
287 { 306 {
288 const char *p = linedraw_cmds[t - 0x2500]; 307 const char *p = linedraw_cmds[t - 0x2500];
289 308
290 int x0 = x, x1 = x + r->TermWin.fwidth / 2, x2 = x + r->TermWin.fwidth ; 309 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; 310 int y0 = y, y1 = y + r->TermWin.fheight / 2, y2 = y + r->TermWin.fheight - 1;
292 311
293 XGCValues gcv; 312 XGCValues gcv;
294 313
295 while (*p) 314 while (*p)
296 { 315 {
297 switch (*p++) 316 switch (*p++)
298 { 317 {
299 case '1': 318 case '1':
300 gcv.line_width = 0; 319 gcv.line_width = 0;
301 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv); 320 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
302 break; 321 break;
303 322
304 case '2': 323 case '2':
305 gcv.line_width = 2; 324 gcv.line_width = 2;
306 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv); 325 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
307 break; 326 break;
308 327
309 case 'h': XDrawLine (DISPLAY, DRAWABLE, GC, x0, y1, x1, y1); break; 328 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; 329 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; 330 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; 331 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; 332 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; 333 case 'b': XDrawLine (d.display->display, d, GC, x0, y0, x2, y2); break;
315 } 334 }
316 } 335 }
317 336
318 gcv.line_width = 0; 337 gcv.line_width = 0;
319 XChangeGC (DISPLAY, GC, GCLineWidth, &gcv); 338 XChangeGC (d.display->display, GC, GCLineWidth, &gcv);
320 } 339 }
321 else 340 else
322 switch (*text++) 341 switch (*text++)
323 { 342 {
324 case NOCHAR: 343 case NOCHAR:
325 case ZERO_WIDTH_CHAR: 344 case ZERO_WIDTH_CHAR:
326 break; 345 break;
327 default: 346 default:
328 XDrawRectangle (DISPLAY, DRAWABLE, GC, x + 2, y + 2, r->TermWin.fwidth - 5, r->TermWin.fheight - 5); 347 XDrawRectangle (d.display->display, d, GC, x + 2, y + 2, r->TermWin.fwidth - 5, r->TermWin.fheight - 5);
329 } 348 }
330 349
331 x += r->TermWin.fwidth; 350 x += r->TermWin.fwidth;
332 } 351 }
333} 352}
343 362
344 bool load (const rxvt_fontprop &prop); 363 bool load (const rxvt_fontprop &prop);
345 364
346 bool has_codepoint (uint32_t unicode); 365 bool has_codepoint (uint32_t unicode);
347 366
348 void draw (int x, int y, 367 void draw (rxvt_drawable &d, int x, int y,
349 const text_t *text, int len, 368 const text_t *text, int len,
350 int fg, int bg); 369 int fg, int bg);
351 370
352 XFontStruct *f; 371 XFontStruct *f;
353 codeset cs; 372 codeset cs;
644 663
645 return true; 664 return true;
646} 665}
647 666
648void 667void
649rxvt_font_x11::draw (int x, int y, 668rxvt_font_x11::draw (rxvt_drawable &d, int x, int y,
650 const text_t *text, int len, 669 const text_t *text, int len,
651 int fg, int bg) 670 int fg, int bg)
652{ 671{
653 // this looks like a mess /. 672 // this looks like a mess /.
654 // and it is a mess /. 673 // and it is a mess /.
670 { 689 {
671 const XChar2b *xc = enc_xchar2b (text, len, cs, slow); 690 const XChar2b *xc = enc_xchar2b (text, len, cs, slow);
672 691
673 if (bg == Color_bg && !slow) 692 if (bg == Color_bg && !slow)
674 { 693 {
675 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v); 694 XChangeGC (d.display->display, GC, GCForeground | GCBackground | GCFont, &v);
676 XDrawImageString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 695 XDrawImageString16 (d.display->display, d, GC, x, y + base, xc, len);
677 } 696 }
678 else 697 else
679 { 698 {
680 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 699 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
681 700
682 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v); 701 XChangeGC (d.display->display, GC, GCForeground | GCFont, &v);
683 702
684 if (slow) 703 if (slow)
685 { 704 {
686 do 705 do
687 { 706 {
688 if (xc->byte1 || xc->byte2) 707 if (xc->byte1 || xc->byte2)
689 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1); 708 XDrawString16 (d.display->display, d, GC, x, y + base, xc, 1);
690 709
691 x += r->TermWin.fwidth; 710 x += r->TermWin.fwidth;
692 xc++; len--; 711 xc++; len--;
693 } 712 }
694 while (len); 713 while (len);
695 } 714 }
696 else 715 else
697 XDrawString16 (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 716 XDrawString16 (d.display->display, d, GC, x, y + base, xc, len);
698 } 717 }
699 } 718 }
700 else 719 else
701 { 720 {
702 const char *xc = enc_char (text, len, cs, slow); 721 const char *xc = enc_char (text, len, cs, slow);
703 722
704 if (bg == Color_bg && !slow) 723 if (bg == Color_bg && !slow)
705 { 724 {
706 XChangeGC (DISPLAY, GC, GCForeground | GCBackground | GCFont, &v); 725 XChangeGC (d.display->display, GC, GCForeground | GCBackground | GCFont, &v);
707 XDrawImageString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 726 XDrawImageString (d.display->display, d, GC, x, y + base, xc, len);
708 } 727 }
709 else 728 else
710 { 729 {
711 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 730 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
712 731
713 XChangeGC (DISPLAY, GC, GCForeground | GCFont, &v); 732 XChangeGC (d.display->display, GC, GCForeground | GCFont, &v);
714 733
715 if (slow) 734 if (slow)
716 { 735 {
717 do 736 do
718 { 737 {
719 if (*xc) 738 if (*xc)
720 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, 1); 739 XDrawString (d.display->display, d, GC, x, y + base, xc, 1);
721 740
722 x += r->TermWin.fwidth; 741 x += r->TermWin.fwidth;
723 xc++; len--; 742 xc++; len--;
724 } 743 }
725 while (len); 744 while (len);
726 } 745 }
727 else 746 else
728 XDrawString (DISPLAY, DRAWABLE, GC, x, y + base, xc, len); 747 XDrawString (d.display->display, d, GC, x, y + base, xc, len);
729 } 748 }
730 } 749 }
731} 750}
732 751
733///////////////////////////////////////////////////////////////////////////// 752/////////////////////////////////////////////////////////////////////////////
738//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables 757//#define SWATHBITS (UNIBITS / 2 + 3) // minimum size for "full" tables
739#define SWATHBITS 8 758#define SWATHBITS 8
740#endif 759#endif
741 760
742struct rxvt_font_xft : rxvt_font { 761struct rxvt_font_xft : rxvt_font {
743 rxvt_font_xft () { f = 0; d = 0; } 762 rxvt_font_xft () { f = 0; }
744 763
745 void clear (); 764 void clear ();
746 765
747 rxvt_fontprop properties (); 766 rxvt_fontprop properties ();
748 767
749 bool load (const rxvt_fontprop &prop); 768 bool load (const rxvt_fontprop &prop);
750 769
751 void draw (int x, int y, 770 void draw (rxvt_drawable &d, int x, int y,
752 const text_t *text, int len, 771 const text_t *text, int len,
753 int fg, int bg); 772 int fg, int bg);
754 773
755 bool has_codepoint (uint32_t unicode); 774 bool has_codepoint (uint32_t unicode);
756 775
757protected: 776protected:
758 XftFont *f; 777 XftFont *f;
759 XftDraw *d;
760}; 778};
761 779
762void 780void
763rxvt_font_xft::clear () 781rxvt_font_xft::clear ()
764{ 782{
765 if (d)
766 {
767 XftDrawDestroy (d);
768 d = 0;
769 }
770
771 if (f) 783 if (f)
772 { 784 {
773 XftFontClose (DISPLAY, f); 785 XftFontClose (DISPLAY, f);
774 f = 0; 786 f = 0;
775 } 787 }
876 FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height); 888 FT_Set_Pixel_Sizes (face, 0, ftheight = prop.height);
877 } 889 }
878 890
879 XftUnlockFace (f); 891 XftUnlockFace (f);
880 892
881 d = XftDrawCreate (DISPLAY, DRAWABLE, r->display->visual, r->display->cmap);
882 if (!d)
883 return false;
884
885 return true; 893 return true;
886} 894}
887 895
888bool 896bool
889rxvt_font_xft::has_codepoint (uint32_t unicode) 897rxvt_font_xft::has_codepoint (uint32_t unicode)
890{ 898{
891 return XftCharExists (DISPLAY, f, unicode); 899 return XftCharExists (DISPLAY, f, unicode);
892} 900}
893 901
894void 902void
895rxvt_font_xft::draw (int x, int y, 903rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
896 const text_t *text, int len, 904 const text_t *text, int len,
897 int fg, int bg) 905 int fg, int bg)
898{ 906{
899 if (bg >= 0 && bg != Color_bg)
900 XftDrawRect (d, &r->PixColors[bg].c, x, y, r->TermWin.fwidth * len, r->TermWin.fheight);
901 else
902 clear_rect (x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); 907 clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg);
903 908
904 if (!slow && width == r->TermWin.fwidth && 0) 909 if (!slow && width == r->TermWin.fwidth && 0)
905 { 910 {
906 if (sizeof (text_t) == sizeof (FcChar16)) 911 if (sizeof (text_t) == sizeof (FcChar16))
907 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len); 912 XftDrawString16 (d, &r->PixColors[fg].c, f, x, y + r->TermWin.fbase, (const FcChar16 *)text, len);
919 fwidth *= 2; 924 fwidth *= 2;
920 925
921 XGlyphInfo extents; 926 XGlyphInfo extents;
922 if (sizeof (text_t) == sizeof (FcChar16)) 927 if (sizeof (text_t) == sizeof (FcChar16))
923 { 928 {
924 XftTextExtents16 (DISPLAY, f, (const FcChar16 *)text, 1, &extents); 929 XftTextExtents16 (d.display->display, f, (const FcChar16 *)text, 1, &extents);
925 XftDrawString16 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2, 930 XftDrawString16 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
926 y + r->TermWin.fbase, (const FcChar16 *)text, 1); 931 y + r->TermWin.fbase, (const FcChar16 *)text, 1);
927 } 932 }
928 else 933 else
929 { 934 {
930 XGlyphInfo extents; 935 XGlyphInfo extents;
931 XftTextExtents32 (DISPLAY, f, (const FcChar32 *)text, 1, &extents); 936 XftTextExtents32 (d.display->display, f, (const FcChar32 *)text, 1, &extents);
932 XftDrawString32 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2, 937 XftDrawString32 (d, &r->PixColors[fg].c, f, x + extents.x + (fwidth - extents.width) / 2,
933 y + r->TermWin.fbase, (const FcChar32 *)text, 1); 938 y + r->TermWin.fbase, (const FcChar32 *)text, 1);
934 } 939 }
935 } 940 }
936 941
1069 } 1074 }
1070 1075
1071 return true; 1076 return true;
1072} 1077}
1073 1078
1074void 1079bool
1075rxvt_fontset::populate (const char *desc) 1080rxvt_fontset::populate (const char *desc)
1076{ 1081{
1077 clear (); 1082 clear ();
1078 1083
1079 fonts.push_back (new_font (0, CS_UNICODE)); 1084 fonts.push_back (new_font (0, CS_UNICODE));
1091 add_fonts ("fixed"); 1096 add_fonts ("fixed");
1092 base_id = fonts.size () - 1; 1097 base_id = fonts.size () - 1;
1093 } 1098 }
1094 1099
1095 if ((int)fonts.size () <= base_id || !realize_font (base_id)) 1100 if ((int)fonts.size () <= base_id || !realize_font (base_id))
1096 { 1101 return false;
1097 fprintf (stderr, "unable to load a base font, please provide one using -fn fontname\n");
1098 exit (1);
1099 }
1100 1102
1101 base_prop = fonts[base_id]->properties (); 1103 base_prop = fonts[base_id]->properties ();
1104
1105 return true;
1102} 1106}
1103 1107
1104int 1108int
1105rxvt_fontset::find_font (uint32_t unicode) 1109rxvt_fontset::find_font (uint32_t unicode)
1106{ 1110{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines