ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtfont.C
(Generate patch)

Comparing rxvt-unicode/src/rxvtfont.C (file contents):
Revision 1.124 by ayin, Sat Nov 24 11:41:11 2007 UTC vs.
Revision 1.154 by sf-exg, Sun Mar 28 22:02:52 2010 UTC

1/*----------------------------------------------------------------------* 1/*----------------------------------------------------------------------*
2 * File: rxvtfont.C 2 * File: rxvtfont.C
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * Copyright (c) 2003-2006 Marc Lehmann <pcg@goof.com> 4 * Copyright (c) 2003-2008 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
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 *---------------------------------------------------------------------*/ 20 *---------------------------------------------------------------------*/
21 21
22#include "../config.h" 22#include "../config.h"
23#include "rxvt.h" 23#include "rxvt.h"
24#include "rxvtlib.h"
25#include "rxvtutil.h" 24#include "rxvtutil.h"
26#include "rxvtfont.h" 25#include "rxvtfont.h"
27 26
28#include <cstdlib> 27#include <cstdlib>
29 28
80 79
81 // japanese 80 // japanese
82#if ENCODING_JP || ENCODING_JP_EXT 81#if ENCODING_JP || ENCODING_JP_EXT
83# if XFT 82# if XFT
84 // prefer xft for complex scripts 83 // prefer xft for complex scripts
84 { CS_JIS0208_1990_0, "xft:Sazanami Mincho:antialias=false" },
85 { CS_JIS0208_1990_0, "xft:Kochi Gothic:antialias=false" }, 85 { CS_JIS0208_1990_0, "xft:Kochi Gothic:antialias=false" },
86 { CS_JIS0208_1990_0, "xft:Sazanami Mincho:antialias=false" },
87 { CS_JIS0208_1990_0, "xft:Mincho:antialias=false" }, 86 { CS_JIS0208_1990_0, "xft:Mincho:antialias=false" },
88 { CS_JIS0208_1990_0, "xft::lang=ja:antialias=false" }, 87 { CS_JIS0208_1990_0, "xft::lang=ja:antialias=false" },
89# endif 88# endif
90 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" }, 89 { CS_JIS0201_1976_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0201*-0" },
91 { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" }, 90 { CS_JIS0208_1990_0, "-*-mincho-*-r-*--*-*-*-*-c-*-jisx0208*-0" },
217: name(0), width(rxvt_fontprop::unset), height(rxvt_fontprop::unset) 216: name(0), width(rxvt_fontprop::unset), height(rxvt_fontprop::unset)
218{ 217{
219} 218}
220 219
221void 220void
222rxvt_font::set_name (char *name) 221rxvt_font::set_name (char *name_)
223{ 222{
224 if (this->name == name) 223 if (name == name_)
225 return; 224 return;
226 225
227 if (this->name) free (this->name); // let the compiler optimize 226 if (name) free (name); // let the compiler optimize
228 this->name = name; 227 name = name_;
229} 228}
230 229
231void 230void
232rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color) const 231rxvt_font::clear_rect (rxvt_drawable &d, int x, int y, int w, int h, int color) const
233{ 232{
237 if (color == Color_bg || color == Color_transparent) 236 if (color == Color_bg || color == Color_transparent)
238 XClearArea (disp, d, x, y, w, h, false); 237 XClearArea (disp, d, x, y, w, h, false);
239 else if (color >= 0) 238 else if (color >= 0)
240 { 239 {
241#if XFT 240#if XFT
242 bool done = false; 241 Picture dst;
242
243#ifdef HAVE_BG_PIXMAP 243# ifdef HAVE_BG_PIXMAP
244 if (term->bgPixmap.pixmap && color >= 0 && term->pix_colors[color].c.color.alpha < 0x0ff00) 244 if (term->bgPixmap.pixmap
245 { 245 && !term->pix_colors[color].is_opaque ()
246 Picture dst = XftDrawPicture (d); 246 && ((dst = XftDrawPicture (d))))
247 if (dst != 0)
248 { 247 {
249 XClearArea (disp, d, x, y, w, h, false); 248 XClearArea (disp, d, x, y, w, h, false);
249
250 Picture solid_color_pict = XftDrawSrcPicture (d, &term->pix_colors[color].c); 250 Picture solid_color_pict = XftDrawSrcPicture (d, &term->pix_colors[color].c);
251 XRenderComposite (disp, PictOpOver, solid_color_pict, None, dst, 0, 0, 0, 0, x, y, w, h); 251 XRenderComposite (disp, PictOpOver, solid_color_pict, None, dst, 0, 0, 0, 0, x, y, w, h);
252 done = true;
253 } 252 }
254 } 253 else
255#endif 254# endif
256 if (!done)
257 XftDrawRect (d, &term->pix_colors[color].c, x, y, w, h); 255 XftDrawRect (d, &term->pix_colors[color].c, x, y, w, h);
256
258#else 257#else
259 XSetForeground (disp, gc, term->pix_colors[color]); 258 XSetForeground (disp, gc, term->pix_colors[color]);
260 XFillRectangle (disp, d, gc, x, y, w, h); 259 XFillRectangle (disp, d, gc, x, y, w, h);
261#endif 260#endif
262 } 261 }
610 return false; 609 return false;
611} 610}
612 611
613// fix the size of scalable fonts 612// fix the size of scalable fonts
614static bool 613static bool
615replace_field (char *buf, const char *name, int index, const char old, const char *replace) 614replace_field (char **ptr, const char *name, int index, const char old, const char *replace)
616{ 615{
617 int slashes = 0; 616 int slashes = 0;
618 const char *field, *end; 617 const char *field, *end;
619 618
620 for (const char *c = name; *c; c++) 619 for (const char *c = name; *c; c++)
630 break; 629 break;
631 } 630 }
632 631
633 if (slashes >= 13 && (!old || *field == old)) 632 if (slashes >= 13 && (!old || *field == old))
634 { 633 {
635 // TODO: check for overflow in font-name 634 size_t len = field - name;
636 strncpy (buf, name, field - name); 635 *ptr = (char *)malloc (len + strlen (replace) + strlen (end) + 1);
637 buf += field - name; 636 memcpy (*ptr, name, len);
638 strcpy (buf, replace); 637 strcpy (*ptr + len, replace);
639 strcat (buf, end); 638 strcat (*ptr, end);
640 639
641 return true; 640 return true;
642 } 641 }
643 else 642 else
644 { 643 {
645 strcpy (buf, name); 644 *ptr = strdup (name);
646 645
647 return false; 646 return false;
648 } 647 }
649} 648}
650 649
658 char field_str[64]; // enough for 128 bits 657 char field_str[64]; // enough for 128 bits
659 658
660 // first morph the font if required 659 // first morph the font if required
661 if (force_prop) 660 if (force_prop)
662 { 661 {
663 char fname[1024]; 662 char *fname;
664 663
665 if (name[0] != '-') 664 if (name[0] != '-')
666 { 665 {
667 f = XLoadQueryFont (disp, name); 666 f = XLoadQueryFont (disp, name);
668 667
680 f = 0; 679 f = 0;
681 } 680 }
682 681
683 if (prop.weight != rxvt_fontprop::unset) 682 if (prop.weight != rxvt_fontprop::unset)
684 { 683 {
685 replace_field (fname, name, 2, 0, 684 replace_field (&fname, name, 2, 0,
686 prop.weight < rxvt_fontprop::bold 685 prop.weight < rxvt_fontprop::bold
687 ? "medium" : "bold"); 686 ? "medium" : "bold");
688 set_name (strdup (fname)); 687 set_name (fname);
689 } 688 }
690 689
691 if (prop.slant != rxvt_fontprop::unset) 690 if (prop.slant != rxvt_fontprop::unset)
692 { 691 {
693 replace_field (fname, name, 3, 0, 692 replace_field (&fname, name, 3, 0,
694 prop.slant < rxvt_fontprop::italic 693 prop.slant < rxvt_fontprop::italic
695 ? "r" : "i"); // TODO: handle "o"blique, too 694 ? "r" : "i"); // TODO: handle "o"blique, too
696 set_name (strdup (fname)); 695 set_name (fname);
697 } 696 }
698 } 697 }
699 698
700 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset 699 sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset
701 ? 0 : prop.height); 700 ? 0 : prop.height);
726 font_weight *fonts = new font_weight[count]; 725 font_weight *fonts = new font_weight[count];
727 726
728 for (int i = 0; i < count; i++) 727 for (int i = 0; i < count; i++)
729 { 728 {
730 rxvt_fontprop p; 729 rxvt_fontprop p;
731 char fname[1024]; 730 char *fname;
732 731
733 int diff = 0; 732 int diff = 0;
734 733
735 if (replace_field (fname, list[i], 6, '0', field_str)) 734 if (replace_field (&fname, list[i], 6, '0', field_str))
736 diff += 10; // slightly penalize scalable fonts 735 diff += 10; // slightly penalize scalable fonts
736 else
737 {
738 free (fname);
737 else if (replace_field (fname, list[i], 11, '0', "0")) 739 if (replace_field (&fname, list[i], 11, '0', "0"))
738 diff += 300; // more heavily penalize what looks like scaled bitmap fonts 740 diff += 300; // more heavily penalize what looks like scaled bitmap fonts
741 }
739 742
740 if (!set_properties (p, fname)) 743 if (!set_properties (p, fname))
741 continue; 744 continue;
742 745
743 if (prop.height != rxvt_fontprop::unset 746 if (prop.height != rxvt_fontprop::unset
747 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128; 750 if (prop.height != rxvt_fontprop::unset) diff += (prop.height - p.height) * 128;
748 if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight); 751 if (prop.weight != rxvt_fontprop::unset) diff += abs (prop.weight - p.weight);
749 if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant); 752 if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant);
750 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width); 753 //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width);
751 754
752 fonts[i].name = strdup (fname); 755 fonts[i].name = fname;
753 fonts[i].diff = diff; 756 fonts[i].diff = diff;
754 } 757 }
755 758
756 XFreeFontNames (list); 759 XFreeFontNames (list);
757 760
789 return false; 792 return false;
790 793
791 char *registry = get_property (f, term->xa [XA_CHARSET_REGISTRY], 0); 794 char *registry = get_property (f, term->xa [XA_CHARSET_REGISTRY], 0);
792 char *encoding = get_property (f, term->xa [XA_CHARSET_ENCODING], 0); 795 char *encoding = get_property (f, term->xa [XA_CHARSET_ENCODING], 0);
793 796
797 cs = CS_UNKNOWN;
798
794 if (registry && encoding) 799 if (registry && encoding)
795 { 800 {
796 char charset[64]; 801 char charset[64];
797 snprintf (charset, 64, "%s-%s", registry, encoding); 802 snprintf (charset, 64, "%s-%s", registry, encoding);
798 803
799 cs = codeset_from_name (charset); 804 cs = codeset_from_name (charset);
805
806 if (cs == CS_UNKNOWN)
807 rxvt_warn ("%s: cannot deduce encoding from registry/encoding properties \"%s\", ignoring font.\n", name, charset);
800 } 808 }
801 else 809
810 free (registry);
811 free (encoding);
812
813 if (cs == CS_UNKNOWN)
802 { 814 {
803 const char *charset = get_property (f, XA_FONT, 0); 815 const char *charset = get_property (f, XA_FONT, 0);
804 816
805 if (!charset) 817 if (!charset)
806 charset = name; 818 charset = name;
809 while (*charset) 821 while (*charset)
810 if (*charset++ == '-' && !--count) 822 if (*charset++ == '-' && !--count)
811 break; 823 break;
812 824
813 cs = codeset_from_name (charset); 825 cs = codeset_from_name (charset);
826 if (cs == CS_UNKNOWN)
827 rxvt_warn ("%s: cannot deduce encoding from font name property \"%s\", ignoring font.\n", name, charset);
828 }
829
830 if (cs == CS_UNKNOWN)
814 } 831 {
815 832 clear ();
816 free (registry); 833 return false;
817 free (encoding); 834 }
818 835
819 if (cs == CS_UNICODE) 836 if (cs == CS_UNICODE)
820 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font 837 cs = CS_UNICODE_16; // X11 can have a max. of 65536 chars per font
821 838
822 encm = f->min_byte1 != 0 || f->max_byte1 != 0; 839 encm = f->min_byte1 != 0 || f->max_byte1 != 0;
867 884
868 int wcw = WCWIDTH (*t); 885 int wcw = WCWIDTH (*t);
869 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw; 886 if (wcw > 0) g.width = (g.width + wcw - 1) / wcw;
870 887
871 if (width < g.width) width = g.width; 888 if (width < g.width) width = g.width;
872 }
873
874 if (cs == CS_UNKNOWN)
875 {
876 fprintf (stderr, "unable to deduce codeset, ignoring font '%s'\n", name);
877
878 clear ();
879 return false;
880 } 889 }
881 890
882#if 0 // do it per-character 891#if 0 // do it per-character
883 if (prop && width > prop->width) 892 if (prop && width > prop->width)
884 { 893 {
1130 FcPatternAddInteger (p, FC_SLANT, prop.slant); 1139 FcPatternAddInteger (p, FC_SLANT, prop.slant);
1131 1140
1132#if 0 // clipping unfortunately destroys our precious double-width-characters 1141#if 0 // clipping unfortunately destroys our precious double-width-characters
1133 // clip width, we can't do better, or can we? 1142 // clip width, we can't do better, or can we?
1134 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch) 1143 if (FcPatternGet (p, FC_CHAR_WIDTH, 0, &v) != FcResultMatch)
1135 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop->width); 1144 FcPatternAddInteger (p, FC_CHAR_WIDTH, prop.width);
1136#endif 1145#endif
1137 1146
1138 if (FcPatternGet (p, FC_MINSPACE, 0, &v) != FcResultMatch) 1147 if (FcPatternGet (p, FC_MINSPACE, 0, &v) != FcResultMatch)
1139 FcPatternAddBool (p, FC_MINSPACE, 1); 1148 FcPatternAddBool (p, FC_MINSPACE, 1);
1140 1149
1284rxvt_font_xft::draw (rxvt_drawable &d, int x, int y, 1293rxvt_font_xft::draw (rxvt_drawable &d, int x, int y,
1285 const text_t *text, int len, 1294 const text_t *text, int len,
1286 int fg, int bg) 1295 int fg, int bg)
1287{ 1296{
1288 XGlyphInfo extents; 1297 XGlyphInfo extents;
1289 XftGlyphSpec *enc = (XftGlyphSpec *)rxvt_temp_buf (len * sizeof (XftGlyphSpec)); 1298 XftGlyphSpec *enc = rxvt_temp_buf<XftGlyphSpec> (len);
1290 XftGlyphSpec *ep = enc; 1299 XftGlyphSpec *ep = enc;
1291 1300
1292 dTermDisplay; 1301 dTermDisplay;
1293 dTermGC; 1302 dTermGC;
1294 1303
1295 int w = term->fwidth * len; 1304 int w = term->fwidth * len;
1296 int h = term->fheight; 1305 int h = term->fheight;
1297 1306
1298 /* TODO: this logic needs some more thinking, since we no longer do pseudo-transparency. 1307 bool buffered = bg >= Color_transparent
1299 * Maybe make buffering into a resource flag? Compile time option doesn't seems like a 1308 && term->option (Opt_buffered);
1300 * good idea from the perspective of packaging for wide variety of user configs. 1309
1301 */
1302 bool buffered = true
1303#ifdef FORCE_UNBUFFERED_XFT
1304 && bg >= 0
1305#endif
1306 ;
1307 // cut trailing spaces 1310 // cut trailing spaces
1308 while (len && text [len - 1] == ' ') 1311 while (len && text [len - 1] == ' ')
1309 len--; 1312 len--;
1310 1313
1311 int x_ = buffered ? 0 : x; 1314 int x_ = buffered ? 0 : x;
1337 x_ += cwidth; 1340 x_ += cwidth;
1338 } 1341 }
1339 1342
1340 if (buffered) 1343 if (buffered)
1341 { 1344 {
1342 bool back_rendered = false;
1343 if (ep != enc) 1345 if (ep != enc)
1344 { 1346 {
1345 rxvt_drawable &d2 = d.screen->scratch_drawable (w, h); 1347 rxvt_drawable &d2 = d.screen->scratch_drawable (w, h);
1346 1348
1347#ifdef HAVE_BG_PIXMAP 1349#ifdef HAVE_BG_PIXMAP
1350 Picture dst = 0; // the only assignment is done conditionally in the following if condition
1351
1348 if (term->bgPixmap.pixmap) 1352 if (term->bgPixmap.pixmap
1353 && (bg == Color_transparent || bg == Color_bg
1354 || (bg >= 0 && !term->pix_colors[bg].is_opaque () && ((dst = XftDrawPicture (d2))))))
1349 { 1355 {
1350 Picture dst = 0; 1356 int src_x = x, src_y = y;
1351 1357
1352 if (bg >= 0 && term->pix_colors[bg].c.color.alpha < 0x0ff00) 1358 if (term->bgPixmap.is_parentOrigin ())
1353 dst = XftDrawPicture (d2);
1354
1355 if (bg < 0 || dst != 0)
1356 { 1359 {
1357 int src_x = x, src_y = y ;
1358
1359 if (term->bgPixmap.is_parentOrigin ())
1360 {
1361 src_x += term->window_vt_x; 1360 src_x += term->window_vt_x;
1362 src_y += term->window_vt_y; 1361 src_y += term->window_vt_y;
1363 } 1362 }
1364 1363
1365 if (term->bgPixmap.pmap_width >= src_x+w 1364 if (term->bgPixmap.pmap_width >= src_x + w
1366 && term->bgPixmap.pmap_height >= src_y+h) 1365 && term->bgPixmap.pmap_height >= src_y + h)
1367 { 1366 {
1368 XCopyArea (disp, term->bgPixmap.pixmap, d2, gc, 1367 XCopyArea (disp, term->bgPixmap.pixmap, d2, gc,
1369 src_x, src_y, w, h, 0, 0); 1368 src_x, src_y, w, h, 0, 0);
1370 } 1369 }
1371 else 1370 else
1372 { 1371 {
1373 XGCValues gcv; 1372 XGCValues gcv;
1374 1373
1375 gcv.fill_style = FillTiled; 1374 gcv.fill_style = FillTiled;
1376 gcv.tile = term->bgPixmap.pixmap; 1375 gcv.tile = term->bgPixmap.pixmap;
1377 gcv.ts_x_origin = -src_x; 1376 gcv.ts_x_origin = -src_x;
1378 gcv.ts_y_origin = -src_y; 1377 gcv.ts_y_origin = -src_y;
1379 1378
1380 XChangeGC (disp, gc, 1379 XChangeGC (disp, gc,
1381 GCTile | GCTileStipXOrigin | GCTileStipYOrigin | GCFillStyle, 1380 GCTile | GCTileStipXOrigin | GCTileStipYOrigin | GCFillStyle,
1382 &gcv); 1381 &gcv);
1383 1382
1384 XFillRectangle (disp, d2, gc, 0, 0, w, h); 1383 XFillRectangle (disp, d2, gc, 0, 0, w, h);
1385 1384
1386 gcv.fill_style = FillSolid; 1385 gcv.fill_style = FillSolid;
1387 XChangeGC (disp, gc, GCFillStyle, &gcv); 1386 XChangeGC (disp, gc, GCFillStyle, &gcv);
1388 } 1387 }
1389 1388
1390 if (bg >= 0) 1389 if (dst)
1391 { 1390 {
1392 Picture solid_color_pict = XftDrawSrcPicture (d2, &term->pix_colors[bg].c); 1391 Picture solid_color_pict = XftDrawSrcPicture (d2, &term->pix_colors[bg].c);
1392
1393 // dst can only be set when bg >= 0
1393 XRenderComposite (disp, PictOpOver, solid_color_pict, None, dst, 0, 0, 0, 0, 0, 0, w, h); 1394 XRenderComposite (disp, PictOpOver, solid_color_pict, None, dst, 0, 0, 0, 0, 0, 0, w, h);
1394 }
1395
1396 back_rendered = true;
1397 } 1395 }
1398 } 1396 }
1397 else
1399#endif 1398#endif
1400
1401 if (bg >= 0 && !back_rendered)
1402 XftDrawRect (d2, &term->pix_colors[bg].c, 0, 0, w, h); 1399 XftDrawRect (d2, &term->pix_colors[bg >= 0 ? bg : Color_bg].c, 0, 0, w, h);
1403 1400
1404 XftDrawGlyphSpec (d2, &term->pix_colors[fg].c, f, enc, ep - enc); 1401 XftDrawGlyphSpec (d2, &term->pix_colors[fg].c, f, enc, ep - enc);
1405 XCopyArea (disp, d2, d, gc, 0, 0, w, h, x, y); 1402 XCopyArea (disp, d2, d, gc, 0, 0, w, h, x, y);
1406 } 1403 }
1407 else 1404 else
1529 if (!end) 1526 if (!end)
1530 end = desc + strlen (desc); 1527 end = desc + strlen (desc);
1531 1528
1532 if (end - desc < 511) 1529 if (end - desc < 511)
1533 { 1530 {
1534 strncpy (buf, desc, end - desc); 1531 memcpy (buf, desc, end - desc);
1535 buf[end - desc] = 0; 1532 buf[end - desc] = 0;
1536 1533
1537 fonts.push_back (new_font (buf, cs)); 1534 fonts.push_back (new_font (buf, cs));
1538 } 1535 }
1539 else 1536 else
1678 FcPatternDestroy (match); 1675 FcPatternDestroy (match);
1679 1676
1680 if (find_font (font) < 0) 1677 if (find_font (font) < 0)
1681 { 1678 {
1682 char fontname[4096]; 1679 char fontname[4096];
1683 sprintf (fontname, "xft:%-.4090s", font); 1680 snprintf (fontname, sizeof (fontname), "xft:%s", font);
1684 1681
1685 fonts.push_back (new_font (fontname, CS_UNICODE)); 1682 fonts.push_back (new_font (fontname, CS_UNICODE));
1686 } 1683 }
1687 1684
1688 free (font); 1685 free (font);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines