--- rxvt-unicode/src/rxvtfont.C 2004/08/25 02:33:09 1.41 +++ rxvt-unicode/src/rxvtfont.C 2005/01/11 05:45:57 1.64 @@ -90,17 +90,22 @@ { CS_JIS0212_1990_0, "-*-*-*-r-*--*-*-*-*-c-*-jisx0212*-0" }, #endif -#if ENCODING_CN || ENCODING_CN_EXT +#if ENCODING_ZH || ENCODING_ZH_EXT # if XFT + { CS_GBK_0, "xft:AR PL KaitiM GB" }, + { CS_GBK_0, "xft:AR PL SungtiL GB" }, + { CS_GBK_0, "xft::spacing=100:lang=zh" }, { CS_BIG5_EXT, "xft:AR PL Mingti2L Big5" }, { CS_BIG5_EXT, "xft:AR PL KaitiM Big5" }, { CS_GB2312_1980_0, "xft:AR PL KaitiM GB" }, { CS_GB2312_1980_0, "xft:AR PL SungtiL GB" }, { CS_GB2312_1980_0, "xft::spacing=100:lang=zh" }, # endif + { CS_GBK_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gbk*-0" }, { CS_BIG5, "-*-*-*-*-*-*-*-*-*-*-c-*-big5-0" }, { CS_BIG5_PLUS, "-*-*-*-*-*-*-*-*-*-*-c-*-big5p-0" }, { CS_BIG5_EXT, "-*-*-*-*-*-*-*-*-*-*-c-*-big5.eten-0" }, + { CS_GB2312_1980_0, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" }, { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-gb2312*-0" }, { CS_CNS11643_1992_1, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-1" }, { CS_CNS11643_1992_2, "-*-*-*-*-*-*-*-*-*-*-c-*-cns11643*-2" }, @@ -127,20 +132,20 @@ { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-c-*-iso10646-1" }, { CS_UNICODE, "-*-*-*-r-*-*-*-*-*-*-m-*-iso10646-1" }, #if XFT - { CS_UNICODE, "xft:Bitstream Vera Sans Mono: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" }, { CS_UNICODE, "xft:Arial Unicode MS:antialias=false" }, - { CS_UNICODE, "xft:Courier New:antialias=false" }, // FreeMono is usually uglier than x fonts, so try last only. - { CS_UNICODE, "xft:FreeMono" }, + { CS_UNICODE, "xft:FreeMono:autohint=true" }, #endif { CS_UNKNOWN, 0 } }; // 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, // ÍÕĔŷﻙ @@ -332,12 +337,19 @@ XSetForeground (d.display->display, TGC, r->pix_colors[fg]); - while (len--) + while (len) { #if ENABLE_COMBINING compose_char *cc; #endif - text_t t = *text++; + const text_t *tp = text; + text_t t = *tp; + + while (++text, --len && *text == NOCHAR) + ; + + int width = text - tp; + int fwidth = r->TermWin.fwidth * width; if (0x2500 <= t && t <= 0x259f) { @@ -345,7 +357,7 @@ uint32_t *a = linedraw_command + (offs >> 4); uint32_t *b = a + (offs & 15); - int W = r->TermWin.fwidth; + int W = fwidth; int H = r->TermWin.fheight; int x_[16]; @@ -420,17 +432,25 @@ #if ENABLE_COMBINING else if (IS_COMPOSE (t) && (cc = rxvt_composite[t])) { + text_t chrs[2]; + width = min (2, width); + chrs [1] = NOCHAR; + + *chrs = cc->c1; rxvt_font *f1 = (*fs)[fs->find_font (cc->c1)]; - f1->draw (d, x, y, &(t = cc->c1), 1, fg, bg); + f1->draw (d, x, y, chrs, width, fg, bg); + if (cc->c2 != NOCHAR) { - // prefer font of first character, for no good reasons bool careful; - rxvt_font *f2 = f1->has_char (cc->c2, 0, careful) + + // prefer font of first character, for no good reasons + *chrs = cc->c2; + rxvt_font *f2 = (f1->has_char (cc->c2, 0, careful) && !careful) ? f1 : (*fs)[fs->find_font (cc->c2)]; - f2->draw (d, x, y, &(t = cc->c2), 1, fg, -1); + f2->draw (d, x, y, chrs, width, fg, -1); } } #endif @@ -443,20 +463,11 @@ break; default: - int w = 0; - while (len > 0 && *text == NOCHAR) - { - ++text; - --len; - w += r->TermWin.fwidth; - } - XDrawRectangle (d.display->display, d, TGC, x + 2, y + 2, - w + r->TermWin.fwidth - 4, r->TermWin.fheight - 4); - x += w; + fwidth - 4, r->TermWin.fheight - 4); } - x += r->TermWin.fwidth; + x += fwidth; } } @@ -522,8 +533,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)) @@ -661,26 +677,41 @@ } } + 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 (DISPLAY, 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; char fname[1024]; int diff = 0; - + if (replace_field (fname, list[i], 6, '0', field_str)) diff += 10; // slightly penalize scalable fonts @@ -696,20 +727,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; - f = XLoadQueryFont (DISPLAY, name); + if (!best->name + || !(f = XLoadQueryFont (DISPLAY, best->name))) + break; + + 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; @@ -748,10 +800,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 @@ -1044,7 +1092,8 @@ 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); if (prop.weight != rxvt_fontprop::unset @@ -1266,6 +1315,9 @@ void rxvt_fontset::clear () { + prop.width = prop.height = prop.weight = prop.slant + = rxvt_fontprop::unset; + for (rxvt_font **i = fonts.begin (); i != fonts.end (); i++) FONT_UNREF (*i); @@ -1395,7 +1447,7 @@ } bool -rxvt_fontset::populate (const char *desc, const rxvt_fontprop &prop) +rxvt_fontset::populate (const char *desc) { clear (); @@ -1404,8 +1456,6 @@ fonts.push_back (new_font (0, CS_UNICODE)); realize_font (0); - this->prop = prop; - add_fonts (desc); return true;