--- rxvt-unicode/src/rxvtfont.C 2004/12/10 19:55:55 1.56 +++ rxvt-unicode/src/rxvtfont.C 2006/01/08 01:02:15 1.79 @@ -1,7 +1,7 @@ /*--------------------------------*-C-*---------------------------------* - * File: defaultfont.C + * File: rxvtfont.C *----------------------------------------------------------------------* - * Copyright (c) 2003-2004 Marc Lehmann + * Copyright (c) 2003-2006 Marc Lehmann * - original version. * * This program is free software; you can redistribute it and/or modify @@ -16,11 +16,12 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *---------------------------------------------------------------------*/ #include "../config.h" #include "rxvt.h" +#include "rxvtlib.h" #include "rxvtutil.h" #include "rxvtfont.h" @@ -29,7 +30,7 @@ #include #define DISPLAY r->display->display -#define TGC r->TermWin.gc +#define TGC r->gc #define MAX_OVERLAP (4 + 1) // max. character width in 4ths of the base width @@ -132,10 +133,10 @@ { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" }, { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, #if XFT - { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true"}, - { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" }, - { CS_UNICODE, "xft:Andale Mono:antialias=false" }, - { CS_UNICODE, "xft:Arial Unicode MS:antialias=false" }, + { CS_UNICODE, "xft:Bitstream Vera Sans Mono:antialias=false:autohint=true" }, + { CS_UNICODE, "xft:Courier New:antialias=false:autohint=true" }, + { CS_UNICODE, "xft:Andale Mono:antialias=false:autohint=false" }, + { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" }, // FreeMono is usually uglier than x fonts, so try last only. { CS_UNICODE, "xft:FreeMono:autohint=true" }, @@ -145,7 +146,7 @@ }; // these characters are used to guess the font height and width -// pango uses a similar algorithm and eosn't trust the font either. +// pango uses a similar algorithm and doesn't trust the font either. static uint16_t extent_test_chars[] = { '0', '1', '8', 'a', 'd', 'x', 'm', 'y', 'g', 'W', 'X', '\'', '_', 0x00cd, 0x00d5, 0x0114, 0x0177, 0x0643, // ÍÕĔŷﻙ @@ -278,6 +279,7 @@ rxvt_fontprop p; p.width = p.height = 1; + p.ascent = rxvt_fontprop::unset; p.weight = rxvt_fontprop::medium; p.slant = rxvt_fontprop::roman; @@ -307,7 +309,8 @@ if (unicode <= 0x009f) return true; - if (unicode >= 0x2500 && unicode <= 0x259f) + if (unicode >= 0x2500 && unicode <= 0x259f && + !r->option (Opt_skipBuiltinGlyphs)) return true; if (IS_COMPOSE (unicode)) @@ -333,9 +336,11 @@ const text_t *text, int len, int fg, int bg) { - clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); + Display *disp = d.display->display; - XSetForeground (d.display->display, TGC, r->pix_colors[fg]); + clear_rect (d, x, y, r->fwidth * len, r->fheight, bg); + + XSetForeground (disp, TGC, r->pix_colors[fg]); while (len) { @@ -349,7 +354,7 @@ ; int width = text - tp; - int fwidth = r->TermWin.fwidth * width; + int fwidth = r->fwidth * width; if (0x2500 <= t && t <= 0x259f) { @@ -358,7 +363,7 @@ uint32_t *b = a + (offs & 15); int W = fwidth; - int H = r->TermWin.fheight; + int H = r->fheight; int x_[16]; int y_[16]; @@ -376,7 +381,7 @@ gcv.cap_style = CapButt; gcv.line_width = 0; - XChangeGC (d.display->display, TGC, GCLineWidth | GCCapStyle, &gcv); + XChangeGC (disp, TGC, GCLineWidth | GCCapStyle, &gcv); while (a < b) { @@ -393,7 +398,7 @@ switch (op) { case 0: // line - XDrawLine (d.display->display, d, TGC, x1, y1, x2, y2); + XDrawLine (disp, d, TGC, x1, y1, x2, y2); break; case 1: // rectangle, possibly stippled @@ -402,27 +407,27 @@ static char bm[] = { 0,0 , 3,1 , 1,2 , 1,0 }; gcv.fill_style = FillStippled; - gcv.stipple = XCreateBitmapFromData (d.display->display, d, bm + a * 2, 2, 2); + gcv.stipple = XCreateBitmapFromData (disp, d, bm + a * 2, 2, 2); gcv.ts_x_origin = x; gcv.ts_y_origin = y; - XChangeGC (d.display->display, TGC, + XChangeGC (disp, TGC, GCFillStyle | GCStipple | GCTileStipXOrigin | GCTileStipYOrigin, &gcv); } - XFillRectangle (d.display->display, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1); + XFillRectangle (disp, d, TGC, x1, y1, x2 - x1 + 1, y2 - y1 + 1); if (a) { - XFreePixmap (d.display->display, gcv.stipple); + XFreePixmap (disp, gcv.stipple); gcv.stipple = 0; gcv.fill_style = FillSolid; - XChangeGC (d.display->display, TGC, GCFillStyle, &gcv); + XChangeGC (disp, TGC, GCFillStyle, &gcv); } break; case 2: // arc - XDrawArc (d.display->display, d, TGC, + XDrawArc (disp, d, TGC, x1 - W/2, y1 - H/2, W-1, H-1, (a - 1) * 90*64, (b - 1) * 90*64); break; @@ -463,8 +468,8 @@ break; default: - XDrawRectangle (d.display->display, d, TGC, x + 2, y + 2, - fwidth - 4, r->TermWin.fheight - 4); + XDrawRectangle (disp, d, TGC, x + 2, y + 2, + fwidth - 4, r->fheight - 4); } x += fwidth; @@ -523,6 +528,7 @@ { p.width = avgwidth ? (avgwidth + 1) / 10 : (height + 1) / 2; p.height = height; + p.ascent = rxvt_fontprop::unset; p.weight = *weight == 'B' || *weight == 'b' ? rxvt_fontprop::bold : rxvt_fontprop::medium; p.slant = *slant == 'r' || *slant == 'R' ? rxvt_fontprop::roman : rxvt_fontprop::italic; @@ -533,8 +539,13 @@ rxvt_font_x11::set_properties (rxvt_fontprop &p, XFontStruct *f) { unsigned long height; + +#if 0 if (!XGetFontProperty (f, XInternAtom (DISPLAY, "PIXEL_SIZE", 0), &height)) return false; +#else + height = f->ascent + f->descent; +#endif unsigned long avgwidth; if (!XGetFontProperty (f, XInternAtom (DISPLAY, "AVERAGE_WIDTH", 0), &avgwidth)) @@ -548,6 +559,8 @@ free (weight); free (slant); + p.ascent = f->ascent; + return true; } @@ -627,6 +640,8 @@ bool rxvt_font_x11::load (const rxvt_fontprop &prop) { + Display *disp = DISPLAY; + clear (); char field_str[64]; // enough for 128 bits @@ -639,7 +654,7 @@ if (name[0] != '-') { - f = XLoadQueryFont (DISPLAY, name); + f = XLoadQueryFont (disp, name); if (!f) return false; @@ -651,7 +666,7 @@ else rxvt_warn ("font '%s' has no FONT property, continuing without.", name); - XFreeFont (DISPLAY, f); + XFreeFont (disp, f); f = 0; } @@ -672,19 +687,34 @@ } } + sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset + ? 0 : prop.height); + + struct font_weight { + char *name; + int diff; + + void clear () + { + name = 0; + diff = 0x7fffffff; + } + + font_weight () { clear (); } + ~font_weight () { free (name); } + }; + char **list; int count; - list = XListFonts (DISPLAY, name, 1024, &count); + list = XListFonts (disp, name, 4000, &count); set_name (0); if (!list) return false; - sprintf (field_str, "%d", prop.height == rxvt_fontprop::unset - ? 0 : prop.height); + font_weight *fonts = new font_weight[count]; - int bestdiff = 0x7fffffff; for (int i = 0; i < count; i++) { rxvt_fontprop p; @@ -694,6 +724,8 @@ if (replace_field (fname, list[i], 6, '0', field_str)) diff += 10; // slightly penalize scalable fonts + else if (replace_field (fname, list[i], 11, '0', "0")) + diff += 300; // more heavily penalize what looks like scaled bitmap fotns if (!set_properties (p, fname)) continue; @@ -707,20 +739,41 @@ if (prop.slant != rxvt_fontprop::unset) diff += abs (prop.slant - p.slant); //if (prop.width != rxvt_fontprop::unset) diff += abs (prop.width - p.width); - if (!name // compare against best found so far - || diff < bestdiff) - { - set_name (strdup (fname)); - bestdiff = diff; - } + fonts[i].name = strdup (fname); + fonts[i].diff = diff; } XFreeFontNames (list); - if (!name) - return false; + // this loop only iterates when the guessed font-size is too small + for (;;) + { + font_weight *best = fonts + count - 1; + + for (font_weight *w = best; w-- > fonts; ) + if (w->diff <= best->diff) + best = w; + + if (!best->name + || !(f = XLoadQueryFont (disp, best->name))) + break; - f = XLoadQueryFont (DISPLAY, name); + set_name (best->name); + best->clear (); + + ascent = f->ascent; + descent = f->descent; + height = ascent + descent; + + if (prop.height == rxvt_fontprop::unset + || height <= prop.height) + break; // font is ready for use + + // PIXEL_SIZE small enough, but real height too large + clear (); + } + + delete [] fonts; if (!f) return false; @@ -759,10 +812,6 @@ encm = f->min_byte1 != 0 || f->max_byte1 != 0; enc2b = encm || f->max_char_or_byte2 > 255; - ascent = f->ascent; - descent = f->descent; - height = ascent + descent; - slow = false; #if 1 // only used for slow detection, TODO optimize @@ -810,7 +859,7 @@ int dir_ret, asc_ret, des_ret; XTextExtents16 (f, &ch, 1, &dir_ret, &asc_ret, &des_ret, &g); - int wcw = wcwidth (*t); if (wcw > 0) g.width = g.width / wcw; + int wcw = wcwidth (*t); if (wcw > 0) g.width = (g.width + wcw - 1) / wcw; if (width < g.width) width = g.width; } @@ -895,7 +944,7 @@ // check character against base font bounding box int w = xcs->width; int wcw = wcwidth (unicode); - if (wcw > 0) w /= wcw; + if (wcw > 0) w = (w + wcw - 1) / wcw; careful = w > prop->width; if (careful && w > prop->width * MAX_OVERLAP >> 2) @@ -915,10 +964,10 @@ // but the result still isn't perfect /. bool slow = this->slow - || width != r->TermWin.fwidth - || height != r->TermWin.fheight; + || width != r->fwidth + || height != r->fheight; - int base = ascent; // sorry, incorrect: r->TermWin.fbase; + int base = ascent; // sorry, incorrect: r->fbase; XGCValues v; v.foreground = r->pix_colors[fg]; @@ -936,7 +985,7 @@ } else { - clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); + clear_rect (d, x, y, r->fwidth * len, r->fheight, bg); XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); @@ -947,7 +996,7 @@ if (xc->byte1 || xc->byte2) XDrawString16 (d.display->display, d, TGC, x, y + base, xc, 1); - x += r->TermWin.fwidth; + x += r->fwidth; xc++; len--; } while (len); @@ -968,7 +1017,7 @@ } else { - clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); + clear_rect (d, x, y, r->fwidth * len, r->fheight, bg); XChangeGC (d.display->display, TGC, GCForeground | GCFont, &v); @@ -979,7 +1028,7 @@ if (*xc) XDrawString (d.display->display, d, TGC, x, y + base, xc, 1); - x += r->TermWin.fwidth; + x += r->fwidth; xc++; len--; } while (len); @@ -1032,6 +1081,7 @@ p.width = width; p.height = height; + p.ascent = ascent; p.weight = face->style_flags & FT_STYLE_FLAG_BOLD ? rxvt_fontprop::bold : rxvt_fontprop::medium; p.slant = face->style_flags & FT_STYLE_FLAG_ITALIC @@ -1045,6 +1095,8 @@ bool rxvt_font_xft::load (const rxvt_fontprop &prop) { + Display *disp = DISPLAY; + clear (); FcPattern *p = FcNameParse ((FcChar8 *) name); @@ -1055,7 +1107,7 @@ FcValue v; if (prop.height != rxvt_fontprop::unset - || (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch + && (FcPatternGet (p, FC_PIXEL_SIZE, 0, &v) != FcResultMatch && FcPatternGet (p, FC_SIZE, 0, &v) != FcResultMatch)) FcPatternAddInteger (p, FC_PIXEL_SIZE, prop.height); @@ -1080,7 +1132,7 @@ set_name ((char *)FcNameUnparse (p)); XftResult result; - FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); + FcPattern *match = XftFontMatch (disp, r->display->screen, p, &result); FcPatternDestroy (p); @@ -1092,7 +1144,7 @@ for (;;) { - f = XftFontOpenPattern (DISPLAY, FcPatternDuplicate (match)); + f = XftFontOpenPattern (disp, FcPatternDuplicate (match)); if (!f) { @@ -1111,6 +1163,8 @@ XftUnlockFace (f); + int glheight = height; + for (uint16_t *t = extent_test_chars + NUM_EXTENT_TEST_CHARS; t-- > extent_test_chars; ) { FcChar16 ch = *t; @@ -1126,17 +1180,31 @@ continue; XGlyphInfo g; - XftTextExtents16 (DISPLAY, f, &ch, 1, &g); + XftTextExtents16 (disp, f, &ch, 1, &g); + + g.width -= g.x; int wcw = wcwidth (ch); - if (wcw > 0) g.width = g.width / wcw; + if (wcw > 0) g.width = (g.width + wcw - 1) / wcw; + + if (width < g.width ) width = g.width; + if (height < g.height ) height = g.height; + if (glheight < g.height - g.y) glheight = g.height - g.y; + } + + if (!width) + { + rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name); + + XftFontClose (disp, f); + f = 0; - if (width < g.width) width = g.width; - if (height < g.height) height = g.height; + success = false; + break; } if (prop.height == rxvt_fontprop::unset - || height <= prop.height + || (height <= prop.height && glheight <= prop.height) || height <= 2 || !scalable) break; @@ -1153,9 +1221,9 @@ else ftheight = prop.height - 1; - XftFontClose (DISPLAY, f); - FcPatternDel (match, FC_PIXEL_SIZE); - FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); + XftFontClose (disp, f); + FcPatternDel (match, FC_PIXEL_SIZE); + FcPatternAddInteger (match, FC_PIXEL_SIZE, ftheight); } FcPatternDestroy (match); @@ -1187,9 +1255,9 @@ XGlyphInfo g; XftTextExtents32 (DISPLAY, f, &ch, 1, &g); - int w = g.width; + int w = g.width - g.x; int wcw = wcwidth (unicode); - if (wcw > 0) w /= wcw; + if (wcw > 0) w = (w + wcw - 1) / wcw; careful = w > prop->width; if (careful && w > prop->width * MAX_OVERLAP >> 2) @@ -1203,7 +1271,7 @@ const text_t *text, int len, int fg, int bg) { - clear_rect (d, x, y, r->TermWin.fwidth * len, r->TermWin.fheight, bg); + clear_rect (d, x, y, r->fwidth * len, r->fheight, bg); int base = ascent; // should be fbase, but that is incorrect @@ -1211,61 +1279,60 @@ FcChar32 *enc = (FcChar32 *) get_enc_buf (len * sizeof (FcChar32)); FcChar32 *ep = enc; int ewidth = 0; - int xoff = 0; while (len) { - int cwidth = r->TermWin.fwidth; + int cwidth = r->fwidth; FcChar32 fc = *text++; len--; - FT_UInt gl; while (len && *text == NOCHAR) - text++, len--, cwidth += r->TermWin.fwidth; + text++, len--, cwidth += r->fwidth; - gl = XftCharIndex (d.display->display, f, fc); - XftGlyphExtents (d.display->display, f, &gl, 1, &extents); - - if (extents.xOff != cwidth && ep != enc) - { - if (xoff > ewidth) xoff = ewidth; - XftDrawGlyphs (d, &r->pix_colors[fg].c, f, - x + (ewidth - xoff >> 1), - y + base, enc, ep - enc); - x += ewidth; - - ep = enc; - ewidth = 0; - xoff = 0; - } - if (fc == ' ' && ep == enc) // skip leading spaces - { - x += cwidth; - continue; - } - + x += cwidth; else { - *ep++ = gl; - ewidth += cwidth; - xoff += extents.xOff; + FT_UInt gl = XftCharIndex (d.display->display, f, fc); + XftGlyphExtents (d.display->display, f, &gl, 1, &extents); + + if (extents.xOff != cwidth) + { + if (ewidth) + { + XftDrawGlyphs (d, &r->pix_colors[fg].c, f, + x, y + base, enc, ep - enc); + x += ewidth; + + ep = enc; + ewidth = 0; + } + + if (extents.xOff > cwidth) + extents.xOff = cwidth; + + XftDrawGlyphs (d, &r->pix_colors[fg].c, f, + x + (cwidth - extents.xOff >> 1), + y + base, &gl, 1); + x += cwidth; + } + else + { + *ep++ = gl; + ewidth += cwidth; + } } } if (ep != enc) - { - if (xoff > ewidth) xoff = ewidth; - XftDrawGlyphs (d, &r->pix_colors[fg].c, f, - x + (ewidth - xoff >> 1), - y + base, enc, ep - enc); - } + XftDrawGlyphs (d, &r->pix_colors[fg].c, f, + x, y + base, enc, ep - enc); } #endif ///////////////////////////////////////////////////////////////////////////// rxvt_fontset::rxvt_fontset (rxvt_t r) -: r (r), fontdesc (0) +: fontdesc (0), r (r) { clear (); } @@ -1278,7 +1345,7 @@ void rxvt_fontset::clear () { - prop.width = prop.height = prop.weight = prop.slant + prop.width = prop.height = prop.ascent = prop.weight = prop.slant = rxvt_fontprop::unset; for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) @@ -1340,7 +1407,8 @@ do { - while (*desc <= ' ') desc++; + while (*desc && *desc <= ' ') + desc++; codeset cs = CS_UNICODE; @@ -1460,6 +1528,9 @@ if (!realize_font (i)) goto next_font; + + if (prop.ascent != rxvt_fontprop::unset) + max_it (f->ascent, prop.ascent); } if (f->cs == CS_UNKNOWN) @@ -1508,7 +1579,7 @@ //FcPatternAddBool (p, FC_ANTIALIAS, 1); XftResult result; - FcPattern *match = XftFontMatch (DISPLAY, DefaultScreen (DISPLAY), p, &result); + FcPattern *match = XftFontMatch (DISPLAY, r->display->screen, p, &result); FcPatternDestroy (p);