+ /* Intelligent cleaning up of the borders. */
+ if(x == 0) {
+ xclear(0, (y == 0)? 0 : winy, borderpx,
+ winy + xw.ch + ((y >= term.row-1)? xw.h : 0));
+ }
+ if(x + charlen >= term.col) {
+ xclear(winx + width, (y == 0)? 0 : winy, xw.w,
+ ((y >= term.row-1)? xw.h : (winy + xw.ch)));
+ }
+ if(y == 0)
+ xclear(winx, 0, winx + width, borderpx);
+ if(y == term.row-1)
+ xclear(winx, winy + xw.ch, winx + width, xw.h);
+
+ /* Clean up the region we want to draw to. */
+ XftDrawRect(xw.draw, bg, winx, winy, width, xw.ch);
+ r.x = 0;
+ r.y = 0;
+ r.height = xw.ch;
+ r.width = width;
+ XftDrawSetClipRectangles(xw.draw, winx, winy, &r, 1);
+
+ fcsets[0] = font->set;
+ for(xp = winx; bytelen > 0;) {
+ /*
+ * Search for the range in the to be printed string of glyphs
+ * that are in the main font. Then print that range. If
+ * some glyph is found that is not in the font, do the
+ * fallback dance.
+ */
+ u8fs = s;
+ u8fblen = 0;
+ u8fl = 0;
+ for(;;) {
+ u8c = s;
+ u8cblen = utf8decode(s, &u8char);
+ s += u8cblen;
+ bytelen -= u8cblen;
+
+ doesexist = XftCharIndex(xw.dpy, font->match, u8char);
+ if(!doesexist || bytelen <= 0) {
+ if(bytelen <= 0) {
+ if(doesexist) {
+ u8fl++;
+ u8fblen += u8cblen;
+ }
+ }
+
+ if(u8fl > 0) {
+ XftDrawStringUtf8(xw.draw, fg,
+ font->match, xp,
+ winy + font->ascent,
+ (FcChar8 *)u8fs,
+ u8fblen);
+ xp += font->width * u8fl;
+
+ }
+ break;
+ }
+
+ u8fl++;
+ u8fblen += u8cblen;
+ }
+ if(doesexist)
+ break;
+
+ frp = frccur;
+ /* Search the font cache. */
+ for(i = 0; i < frclen; i++, frp--) {
+ if(frp <= 0)
+ frp = LEN(frc) - 1;
+
+ if(frc[frp].c == u8char
+ && frc[frp].flags == frcflags) {
+ break;
+ }
+ }
+
+ /* Nothing was found. */
+ if(i >= frclen) {
+ /*
+ * Nothing was found in the cache. Now use
+ * some dozen of Fontconfig calls to get the
+ * font for one single character.
+ */
+ fcpattern = FcPatternDuplicate(font->pattern);
+ fccharset = FcCharSetCreate();
+
+ FcCharSetAddChar(fccharset, u8char);
+ FcPatternAddCharSet(fcpattern, FC_CHARSET,
+ fccharset);
+ FcPatternAddBool(fcpattern, FC_SCALABLE,
+ FcTrue);
+
+ FcConfigSubstitute(0, fcpattern,
+ FcMatchPattern);
+ FcDefaultSubstitute(fcpattern);
+
+ fontpattern = FcFontSetMatch(0, fcsets,
+ FcTrue, fcpattern, &fcres);
+
+ /*
+ * Overwrite or create the new cache entry.
+ */
+ frccur++;
+ frclen++;
+ if(frccur >= LEN(frc))
+ frccur = 0;
+ if(frclen > LEN(frc)) {
+ frclen = LEN(frc);
+ XftFontClose(xw.dpy, frc[frccur].font);
+ }
+
+ frc[frccur].font = XftFontOpenPattern(xw.dpy,
+ fontpattern);
+ frc[frccur].c = u8char;
+ frc[frccur].flags = frcflags;
+
+ FcPatternDestroy(fcpattern);
+ FcCharSetDestroy(fccharset);
+
+ frp = frccur;
+ }
+
+ XftDrawStringUtf8(xw.draw, fg, frc[frp].font,
+ xp, winy + frc[frp].font->ascent,
+ (FcChar8 *)u8c, u8cblen);
+
+ xp += font->width;
+ }
+
+ /*
+ XftDrawStringUtf8(xw.draw, fg, font->set, winx,
+ winy + font->ascent, (FcChar8 *)s, bytelen);
+ */
+
+ if(base.mode & ATTR_UNDERLINE) {
+ XftDrawRect(xw.draw, fg, winx, winy + font->ascent + 1,
+ width, 1);
+ }
+
+ /* Reset clip to none. */
+ XftDrawSetClip(xw.draw, 0);