--- rxvt-unicode/src/rxvtfont.C 2021/06/17 00:08:35 1.199 +++ rxvt-unicode/src/rxvtfont.C 2021/07/03 04:47:08 1.220 @@ -136,10 +136,10 @@ //{ CS_UNICODE, "-*-unifont-*-*-*-*-*-*-*-*-c-*-iso10646-1" }, // this gem of a font has actual dotted circles within the combining character glyphs. #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:autohint=false" }, - { CS_UNICODE, "xft:Arial Unicode MS:antialias=false:autohint=false" }, + { CS_UNICODE, "xft:DejaVu 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 after the others { CS_UNICODE, "xft:FreeMono:autohint=true" }, @@ -315,8 +315,8 @@ return true; #endif - if (IS_COMPOSE (unicode)) - return true; + // we do not check for IS_COMPOSE here, as this would + // rob other fonts from taking over. switch (unicode) { @@ -347,15 +347,15 @@ while (len) { -#if ENABLE_COMBINING - compose_char *cc; -#endif const text_t *tp = text; text_t t = *tp; while (++text, --len && *text == NOCHAR) ; +#if ENABLE_COMBINING + compose_char *cc; +#endif int width = text - tp; int fwidth = term->fwidth * width; @@ -447,7 +447,6 @@ else if (IS_COMPOSE (t) && (cc = rxvt_composite[t])) { min_it (width, 2); // we only support wcwidth up to 2 - text_t chrs[2]; chrs [1] = NOCHAR; @@ -1140,6 +1139,17 @@ struct rxvt_font_xft : rxvt_font { +#if XFT_CHAR_CACHE + // we cache the qascii range xoffsets in the name of speed, + // expecting terminals to deal mostly with these characters + // we also assume the xoff always fits into uint8_t, + // which is questionable, but let's see... + // also, it is uncomfortably big, due to the uints. + enum { char_cache_min = 0x20, char_cache_max = 0x7e }; + uint8_t xoff_cache [char_cache_max - char_cache_min + 1]; + FT_UInt glyph_cache [char_cache_max - char_cache_min + 1]; +#endif + rxvt_font_xft () { f = 0; @@ -1295,12 +1305,8 @@ if (!width) { - rxvt_warn ("unable to calculate font width for '%s', ignoring.\n", name); - - XftFontClose (disp, f); - f = 0; - - success = false; + rxvt_warn ("unable to calculate font width for '%s', using max_advance_width.\n", name); + width = f->max_advance_width; break; } @@ -1337,6 +1343,19 @@ } #endif +#if XFT_CHAR_CACHE + // populate char cache + for (FcChar16 ch = char_cache_min; ch <= char_cache_max; ++ch) + { + FT_UInt glyph = XftCharIndex (disp, f, ch); + glyph_cache [ch - char_cache_min] = glyph; + + XGlyphInfo g; + XftGlyphExtents (disp, f, &glyph, 1, &g); + xoff_cache [ch - char_cache_min] = g.xOff; + } +#endif + return success; } @@ -1345,21 +1364,40 @@ { careful = false; - if (!XftCharExists (term->dpy, f, unicode)) + // handle non-bmp chars when text_t is 16 bit +#if ENABLE_COMBINING && !UNICODE_3 + if (ecb_expect_false (IS_COMPOSE (unicode))) + if (compose_char *cc = rxvt_composite [unicode]) + if (cc->c2 == NOCHAR) + unicode = cc->c1; + else + return false; + else + return false; +#endif + + FcChar32 chr = unicode; + + if (!XftCharExists (term->dpy, f, chr)) return false; if (!prop || prop->width == rxvt_fontprop::unset) return true; - // check character against base font bounding box - FcChar32 ch = unicode; + int wcw = max (WCWIDTH (chr), 1); + XGlyphInfo g; - XftTextExtents32 (term->dpy, f, &ch, 1, &g); + XftTextExtents32 (term->dpy, f, &chr, 1, &g); + + int cwidth = prop->width * wcw; + + // use same adjustments as in ->draw, see there + g.x += g.xOff ? cwidth - g.xOff >> 1 : 0; + g.x += g.xOff ? 0 : cwidth; int w = g.width - g.x; - int wcw = max (WCWIDTH (unicode), 1); - careful = g.x > 0 || w > prop->width * wcw; + careful = g.x > 0 || w > cwidth; if (careful && !OVERLAP_OK (w, wcw, prop)) return false; @@ -1376,7 +1414,6 @@ const text_t *text, int len, int fg, int bg) { - XGlyphInfo extents; XftGlyphSpec *enc = rxvt_temp_buf (len); XftGlyphSpec *ep = enc; @@ -1399,24 +1436,71 @@ while (len) { int cwidth = term->fwidth; - FcChar32 fc = *text++; len--; + FcChar32 chr = *text++; len--; while (len && *text == NOCHAR) text++, len--, cwidth += term->fwidth; - if (fc != ' ') // skip spaces + if (chr != ' ') // skip spaces { - FT_UInt glyph = XftCharIndex (disp, f, fc); - XftGlyphExtents (disp, f, &glyph, 1, &extents); + // handle non-bmp chars when text_t is 16 bit + #if ENABLE_COMBINING && !UNICODE_3 + if (ecb_expect_false (IS_COMPOSE (chr))) + if (compose_char *cc = rxvt_composite [chr]) + if (cc->c2 == NOCHAR) + chr = cc->c1; + #endif + + #if 0 + FT_UInt glyphs [decltype (exp)::max_size]; + + for (int i = 0; i < nchrs; ++i) + glyphs [i] = XftCharIndex (disp, f, chrs [i]); + + for (int i = 0; i < nchrs; ++i) + { + XGlyphInfo ep; + XftGlyphExtents (disp, f, glyphs+i, 1, &ep); + printf ("gs %4x g %4x + %3d,%3d o %3d,%3d wh %3d,%3d\n", chrs[i],glyphs[i],ep.x,ep.y,ep.xOff,ep.yOff,ep.width,ep.height); + } + #endif + + FT_UInt glyph; + int xOff; + + #if XFT_CHAR_CACHE + if (ecb_expect_true (IN_RANGE_INC (chr, char_cache_min, char_cache_max))) + { + glyph = glyph_cache [chr - char_cache_min]; + xOff = xoff_cache [chr - char_cache_min]; + } + else + #endif + { + glyph = XftCharIndex (disp, f, chr); + XGlyphInfo extents; + XftGlyphExtents (disp, f, &glyph, 1, &extents); + xOff = extents.xOff; + } ep->glyph = glyph; - ep->x = x_ + (cwidth - extents.xOff >> 1); + ep->x = x_; ep->y = y_ + ascent; - if (extents.xOff == 0) - ep->x = x_ + cwidth; + // the xft font cell might differ from the terminal font cell, + // in which case we use the average between the two. + ep->x += xOff ? cwidth - xOff >> 1 : 0; + + // xft/freetype represent combining characters as characters with zero + // width rendered over the previous character with some fonts, while + // in other fonts, they are considered normal characters, while yet + // in other fonts, they are shifted all over the place. + // we handle the first two cases by keying off on xOff being 0 + // for zero-width chars. normally, we would add extents.xOff + // of the base character here, but we don't have that, so we use cwidth. + ep->x += xOff ? 0 : cwidth; - ep++; + ++ep; } x_ += cwidth; @@ -1695,15 +1779,16 @@ int rxvt_fontset::find_font_idx (unicode_t unicode) { - if (unicode >= 1<<20) + // this limits fmap size. it has to accommodate COMPOSE_HI when UNICODE_3 + if (unicode > 0x1fffff) return 0; unicode_t hi = unicode >> 8; - if (hi < fmap.size () - && fmap[hi] - && (*fmap[hi])[unicode & 0xff] != 0xff) - return (*fmap[hi])[unicode & 0xff]; + if (hi < fmap.size ()) + if (pagemap *pm = fmap[hi]) + if ((*pm)[unicode & 0xff] != 0xff) + return (*pm)[unicode & 0xff]; unsigned int i; @@ -1737,6 +1822,11 @@ next_font: if (i == fonts.size () - 1) { + // compose characters are handled by the default font, unless another font takes over + // we do not go via the fallback list for speed reasons. + if (IS_COMPOSE (unicode)) + return 0; + if (fallback->name) { // search through the fallback list