--- rxvt-unicode/src/rxvtfont.C 2021/06/17 10:49:01 1.202 +++ rxvt-unicode/src/rxvtfont.C 2023/08/19 17:44:35 1.223 @@ -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" }, @@ -214,7 +214,7 @@ ///////////////////////////////////////////////////////////////////////////// rxvt_font::rxvt_font () -: name(0), width(rxvt_fontprop::unset), height(rxvt_fontprop::unset), can_compose(false) +: name(0), width(rxvt_fontprop::unset), height(rxvt_fontprop::unset) { } @@ -447,29 +447,6 @@ else if (IS_COMPOSE (t) && (cc = rxvt_composite[t])) { min_it (width, 2); // we only support wcwidth up to 2 - - #if NOT_YET - vector chrs; - chrs.reserve (rxvt_composite.expand (t)); - rxvt_composite.expand (t, &chrs[0]); - - while (!chrs.empty ()) - { - rxvt_font *f1 = fs->find_font_idx (chrs[0]) - - int i = 0; - while (i < chrs.size () - 1 - && f1->can_combine - && f1->has-char (chrs[i + 1], careful) - && !careful - ++i; - - (*fs)[f1]->draw (d, x, y, &chrs[0], width, fg, bg); - chrs.erase (&chrs[0], &chrs[i]); - } - #endif - - #if 1 text_t chrs[2]; chrs [1] = NOCHAR; @@ -489,7 +466,6 @@ f2->draw (d, x, y, chrs, width, fg, Color_none); } - #endif } #endif else @@ -543,7 +519,8 @@ bool load (const rxvt_fontprop &prop, bool force_prop) { width = 1; height = 1; - ascent = 1; descent = 0; + ascent = 1; + descent = (*fs)[2]->descent; set_name (strdup ("built-in rendition overflow font")); @@ -1163,9 +1140,19 @@ 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 () { - can_compose = true; f = 0; } @@ -1279,6 +1266,16 @@ FT_Face face = XftLockFace (f); + // fuck me plenty: XftLockFace can actually return 0. try not to crash. + // we also assume blindly that if the first lock succeeds, then subsequent + // locks will also succeed. + if (!face) + { + XftFontClose (disp, f); + success = false; + break; + } + ascent = (face->size->metrics.ascender + 63) >> 6; descent = (-face->size->metrics.descender + 63) >> 6; height = max (ascent + descent, (face->size->metrics.height + 63) >> 6); @@ -1319,12 +1316,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; } @@ -1361,6 +1354,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; } @@ -1369,28 +1375,40 @@ { careful = false; + // 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 (compose_char *cc = rxvt_composite [unicode]) if (cc->c2 == NOCHAR) unicode = cc->c1; + else + return false; + else + return false; #endif - if (!XftCharExists (term->dpy, f, unicode)) + 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; @@ -1407,7 +1425,6 @@ const text_t *text, int len, int fg, int bg) { - XGlyphInfo extents; XftGlyphSpec *enc = rxvt_temp_buf (len); XftGlyphSpec *ep = enc; @@ -1430,30 +1447,71 @@ while (len) { int cwidth = term->fwidth; - FcChar32 fc = *text++; len--; - -#if ENABLE_COMBINING && !UNICODE_3 - if (ecb_expect_false (IS_COMPOSE (fc))) - if (compose_char *cc = rxvt_composite[fc]) // should always be true, but better be safe than sorry - fc = cc->c1; // c2 must be NOCHAR, as has_char handles it that way -#endif + 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; @@ -1732,7 +1790,7 @@ int rxvt_fontset::find_font_idx (unicode_t unicode) { - // this limits fmap size. it has to accomodate COMPOSE_HI when UNICODE_3 + // this limits fmap size. it has to accommodate COMPOSE_HI when UNICODE_3 if (unicode > 0x1fffff) return 0;