sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

st-ligatures-boxdraw-20230105-0.9.diff (18273B)


      1 diff --git a/Makefile b/Makefile
      2 index 6dfa212..adfa07a 100644
      3 --- a/Makefile
      4 +++ b/Makefile
      5 @@ -4,7 +4,7 @@
      6  
      7  include config.mk
      8  
      9 -SRC = st.c x.c boxdraw.c
     10 +SRC = st.c x.c boxdraw.c hb.c
     11  OBJ = $(SRC:.c=.o)
     12  
     13  all: options st
     14 @@ -22,8 +22,9 @@ config.h:
     15  	$(CC) $(STCFLAGS) -c $<
     16  
     17  st.o: config.h st.h win.h
     18 -x.o: arg.h config.h st.h win.h
     19 +x.o: arg.h config.h st.h win.h hb.h
     20  boxdraw.o: config.h st.h boxdraw_data.h
     21 +hb.o: st.h
     22  
     23  $(OBJ): config.h config.mk
     24  
     25 diff --git a/config.mk b/config.mk
     26 index 1e306f8..3e13e53 100644
     27 --- a/config.mk
     28 +++ b/config.mk
     29 @@ -15,10 +15,12 @@ PKG_CONFIG = pkg-config
     30  # includes and libs
     31  INCS = -I$(X11INC) \
     32         `$(PKG_CONFIG) --cflags fontconfig` \
     33 -       `$(PKG_CONFIG) --cflags freetype2`
     34 +       `$(PKG_CONFIG) --cflags freetype2` \
     35 +       `$(PKG_CONFIG) --cflags harfbuzz`
     36  LIBS = -L$(X11LIB) -lm -lrt -lX11 -lutil -lXft \
     37         `$(PKG_CONFIG) --libs fontconfig` \
     38 -       `$(PKG_CONFIG) --libs freetype2`
     39 +       `$(PKG_CONFIG) --libs freetype2` \
     40 +       `$(PKG_CONFIG) --libs harfbuzz`
     41  
     42  # flags
     43  STCPPFLAGS = -DVERSION=\"$(VERSION)\" -D_XOPEN_SOURCE=600
     44 diff --git a/hb.c b/hb.c
     45 new file mode 100644
     46 index 0000000..528c040
     47 --- /dev/null
     48 +++ b/hb.c
     49 @@ -0,0 +1,124 @@
     50 +#include <stdlib.h>
     51 +#include <stdio.h>
     52 +#include <math.h>
     53 +#include <X11/Xft/Xft.h>
     54 +#include <X11/cursorfont.h>
     55 +#include <hb.h>
     56 +#include <hb-ft.h>
     57 +
     58 +#include "st.h"
     59 +#include "hb.h"
     60 +
     61 +#define FEATURE(c1,c2,c3,c4) { .tag = HB_TAG(c1,c2,c3,c4), .value = 1, .start = HB_FEATURE_GLOBAL_START, .end = HB_FEATURE_GLOBAL_END }
     62 +#define BUFFER_STEP 256
     63 +
     64 +hb_font_t *hbfindfont(XftFont *match);
     65 +
     66 +typedef struct {
     67 +	XftFont *match;
     68 +	hb_font_t *font;
     69 +} HbFontMatch;
     70 +
     71 +typedef struct {
     72 +	size_t capacity;
     73 +	HbFontMatch *fonts;
     74 +} HbFontCache;
     75 +
     76 +static HbFontCache hbfontcache = { 0, NULL };
     77 +
     78 +typedef struct {
     79 +	size_t capacity;
     80 +	Rune *runes;
     81 +} RuneBuffer;
     82 +
     83 +static RuneBuffer hbrunebuffer = { 0, NULL };
     84 +
     85 +/*
     86 + * Poplulate the array with a list of font features, wrapped in FEATURE macro,
     87 + * e. g.
     88 + * FEATURE('c', 'a', 'l', 't'), FEATURE('d', 'l', 'i', 'g')
     89 + */
     90 +hb_feature_t features[] = { };
     91 +
     92 +void
     93 +hbunloadfonts()
     94 +{
     95 +	for (int i = 0; i < hbfontcache.capacity; i++) {
     96 +		hb_font_destroy(hbfontcache.fonts[i].font);
     97 +		XftUnlockFace(hbfontcache.fonts[i].match);
     98 +	}
     99 +
    100 +	if (hbfontcache.fonts != NULL) {
    101 +		free(hbfontcache.fonts);
    102 +		hbfontcache.fonts = NULL;
    103 +	}
    104 +	hbfontcache.capacity = 0;
    105 +}
    106 +
    107 +hb_font_t *
    108 +hbfindfont(XftFont *match)
    109 +{
    110 +	for (int i = 0; i < hbfontcache.capacity; i++) {
    111 +		if (hbfontcache.fonts[i].match == match)
    112 +			return hbfontcache.fonts[i].font;
    113 +	}
    114 +
    115 +	/* Font not found in cache, caching it now. */
    116 +	hbfontcache.fonts = realloc(hbfontcache.fonts, sizeof(HbFontMatch) * (hbfontcache.capacity + 1));
    117 +	FT_Face face = XftLockFace(match);
    118 +	hb_font_t *font = hb_ft_font_create(face, NULL);
    119 +	if (font == NULL)
    120 +		die("Failed to load Harfbuzz font.");
    121 +
    122 +	hbfontcache.fonts[hbfontcache.capacity].match = match;
    123 +	hbfontcache.fonts[hbfontcache.capacity].font = font;
    124 +	hbfontcache.capacity += 1;
    125 +
    126 +	return font;
    127 +}
    128 +
    129 +void hbtransform(HbTransformData *data, XftFont *xfont, const Glyph *glyphs, int start, int length) {
    130 +	ushort mode = USHRT_MAX;
    131 +	unsigned int glyph_count;
    132 +	int rune_idx, glyph_idx, end = start + length;
    133 +
    134 +	hb_font_t *font = hbfindfont(xfont);
    135 +	if (font == NULL)
    136 +		return;
    137 +
    138 +	hb_buffer_t *buffer = hb_buffer_create();
    139 +	hb_buffer_set_direction(buffer, HB_DIRECTION_LTR);
    140 +
    141 +	/* Resize the buffer if required length is larger. */
    142 +	if (hbrunebuffer.capacity < length) {
    143 +		hbrunebuffer.capacity = (length / BUFFER_STEP + 1) * BUFFER_STEP;
    144 +		hbrunebuffer.runes = realloc(hbrunebuffer.runes, hbrunebuffer.capacity * sizeof(Rune));
    145 +	}
    146 +
    147 +	/* Fill buffer with codepoints. */
    148 +	for (rune_idx = 0, glyph_idx = start; glyph_idx < end; glyph_idx++, rune_idx++) {
    149 +		hbrunebuffer.runes[rune_idx] = glyphs[glyph_idx].u;
    150 +		mode = glyphs[glyph_idx].mode;
    151 +		if (mode & ATTR_WDUMMY)
    152 +			hbrunebuffer.runes[rune_idx] = 0x0020;
    153 +	}
    154 +	hb_buffer_add_codepoints(buffer, hbrunebuffer.runes, length, 0, length);
    155 +
    156 +	/* Shape the segment. */
    157 +	hb_shape(font, buffer, features, sizeof(features)/sizeof(hb_feature_t));
    158 +
    159 +	/* Get new glyph info. */
    160 +	hb_glyph_info_t *info = hb_buffer_get_glyph_infos(buffer, &glyph_count);
    161 +	hb_glyph_position_t *pos = hb_buffer_get_glyph_positions(buffer, &glyph_count);
    162 +
    163 +	/* Fill the output. */
    164 +	data->buffer = buffer;
    165 +	data->glyphs = info;
    166 +	data->positions = pos;
    167 +	data->count = glyph_count;
    168 +}
    169 +
    170 +void hbcleanup(HbTransformData *data) {
    171 +	hb_buffer_destroy(data->buffer);
    172 +	memset(data, 0, sizeof(HbTransformData));
    173 +}
    174 diff --git a/hb.h b/hb.h
    175 new file mode 100644
    176 index 0000000..88de9bd
    177 --- /dev/null
    178 +++ b/hb.h
    179 @@ -0,0 +1,14 @@
    180 +#include <X11/Xft/Xft.h>
    181 +#include <hb.h>
    182 +#include <hb-ft.h>
    183 +
    184 +typedef struct {
    185 +  hb_buffer_t *buffer;
    186 +  hb_glyph_info_t *glyphs;
    187 +  hb_glyph_position_t *positions;
    188 +  unsigned int count;
    189 +} HbTransformData;
    190 +
    191 +void hbunloadfonts();
    192 +void hbtransform(HbTransformData *, XftFont *, const Glyph *, int, int);
    193 +void hbcleanup(HbTransformData *);
    194 diff --git a/st.c b/st.c
    195 index 41d5ace..1c2edd6 100644
    196 --- a/st.c
    197 +++ b/st.c
    198 @@ -2643,7 +2643,8 @@ draw(void)
    199  
    200  	drawregion(0, 0, term.col, term.row);
    201  	xdrawcursor(cx, term.c.y, term.line[term.c.y][cx],
    202 -			term.ocx, term.ocy, term.line[term.ocy][term.ocx]);
    203 +			term.ocx, term.ocy, term.line[term.ocy][term.ocx],
    204 +			term.line[term.ocy], term.col);
    205  	term.ocx = cx;
    206  	term.ocy = term.c.y;
    207  	xfinishdraw();
    208 diff --git a/st.h b/st.h
    209 index 808f5f7..ae41368 100644
    210 --- a/st.h
    211 +++ b/st.h
    212 @@ -11,7 +11,8 @@
    213  #define DIVCEIL(n, d)		(((n) + ((d) - 1)) / (d))
    214  #define DEFAULT(a, b)		(a) = (a) ? (a) : (b)
    215  #define LIMIT(x, a, b)		(x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
    216 -#define ATTRCMP(a, b)		((a).mode != (b).mode || (a).fg != (b).fg || \
    217 +#define ATTRCMP(a, b)		(((a).mode & (~ATTR_WRAP)) != ((b).mode & (~ATTR_WRAP)) || \
    218 +				(a).fg != (b).fg || \
    219  				(a).bg != (b).bg)
    220  #define TIMEDIFF(t1, t2)	((t1.tv_sec-t2.tv_sec)*1000 + \
    221  				(t1.tv_nsec-t2.tv_nsec)/1E6)
    222 diff --git a/win.h b/win.h
    223 index 6de960d..94679e4 100644
    224 --- a/win.h
    225 +++ b/win.h
    226 @@ -25,7 +25,7 @@ enum win_mode {
    227  
    228  void xbell(void);
    229  void xclipcopy(void);
    230 -void xdrawcursor(int, int, Glyph, int, int, Glyph);
    231 +void xdrawcursor(int, int, Glyph, int, int, Glyph, Line, int);
    232  void xdrawline(Line, int, int, int);
    233  void xfinishdraw(void);
    234  void xloadcols(void);
    235 diff --git a/x.c b/x.c
    236 index bf6bbf9..929a59a 100644
    237 --- a/x.c
    238 +++ b/x.c
    239 @@ -19,6 +19,7 @@ char *argv0;
    240  #include "arg.h"
    241  #include "st.h"
    242  #include "win.h"
    243 +#include "hb.h"
    244  
    245  /* types used in config.h */
    246  typedef struct {
    247 @@ -141,6 +142,7 @@ typedef struct {
    248  } DC;
    249  
    250  static inline ushort sixd_to_16bit(int);
    251 +static void xresetfontsettings(ushort mode, Font **font, int *frcflags);
    252  static int xmakeglyphfontspecs(XftGlyphFontSpec *, const Glyph *, int, int, int);
    253  static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int);
    254  static void xdrawglyph(Glyph, int, int);
    255 @@ -757,7 +759,7 @@ xresize(int col, int row)
    256  	xclear(0, 0, win.w, win.h);
    257  
    258  	/* resize to new width */
    259 -	xw.specbuf = xrealloc(xw.specbuf, col * sizeof(GlyphFontSpec));
    260 +	xw.specbuf = xrealloc(xw.specbuf, col * sizeof(GlyphFontSpec) * 4);
    261  }
    262  
    263  ushort
    264 @@ -1062,6 +1064,9 @@ xunloadfont(Font *f)
    265  void
    266  xunloadfonts(void)
    267  {
    268 +	/* Clear Harfbuzz font cache. */
    269 +	hbunloadfonts();
    270 +
    271  	/* Free the loaded fonts in the font cache.  */
    272  	while (frclen > 0)
    273  		XftFontClose(xw.dpy, frc[--frclen].font);
    274 @@ -1185,7 +1190,7 @@ xinit(int cols, int rows)
    275  	XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h);
    276  
    277  	/* font spec buffer */
    278 -	xw.specbuf = xmalloc(cols * sizeof(GlyphFontSpec));
    279 +	xw.specbuf = xmalloc(cols * sizeof(GlyphFontSpec) * 4);
    280  
    281  	/* Xft rendering context */
    282  	xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
    283 @@ -1241,6 +1246,22 @@ xinit(int cols, int rows)
    284  	boxdraw_xinit(xw.dpy, xw.cmap, xw.draw, xw.vis);
    285  }
    286  
    287 +void
    288 +xresetfontsettings(ushort mode, Font **font, int *frcflags)
    289 +{
    290 +	*font = &dc.font;
    291 +	if ((mode & ATTR_ITALIC) && (mode & ATTR_BOLD)) {
    292 +		*font = &dc.ibfont;
    293 +		*frcflags = FRC_ITALICBOLD;
    294 +	} else if (mode & ATTR_ITALIC) {
    295 +		*font = &dc.ifont;
    296 +		*frcflags = FRC_ITALIC;
    297 +	} else if (mode & ATTR_BOLD) {
    298 +		*font = &dc.bfont;
    299 +		*frcflags = FRC_BOLD;
    300 +	}
    301 +}
    302 +
    303  int
    304  xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x, int y)
    305  {
    306 @@ -1255,126 +1276,158 @@ xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x
    307  	FcPattern *fcpattern, *fontpattern;
    308  	FcFontSet *fcsets[] = { NULL };
    309  	FcCharSet *fccharset;
    310 -	int i, f, numspecs = 0;
    311 +	int i, f, length = 0, start = 0, numspecs = 0;
    312 +	float cluster_xp = xp, cluster_yp = yp;
    313 +	HbTransformData shaped = { 0 };
    314 +
    315 +	/* Initial values. */
    316 +	mode = prevmode = glyphs[0].mode & ~ATTR_WRAP;
    317 +	xresetfontsettings(mode, &font, &frcflags);
    318  
    319  	for (i = 0, xp = winx, yp = winy + font->ascent; i < len; ++i) {
    320 -		/* Fetch rune and mode for current glyph. */
    321 -		rune = glyphs[i].u;
    322 -		mode = glyphs[i].mode;
    323 +		mode = glyphs[i].mode & ~ATTR_WRAP;
    324  
    325  		/* Skip dummy wide-character spacing. */
    326 -		if (mode == ATTR_WDUMMY)
    327 +		if (mode & ATTR_WDUMMY && i < (len - 1))
    328  			continue;
    329  
    330 -		/* Determine font for glyph if different from previous glyph. */
    331 -		if (prevmode != mode) {
    332 -			prevmode = mode;
    333 -			font = &dc.font;
    334 -			frcflags = FRC_NORMAL;
    335 -			runewidth = win.cw * ((mode & ATTR_WIDE) ? 2.0f : 1.0f);
    336 -			if ((mode & ATTR_ITALIC) && (mode & ATTR_BOLD)) {
    337 -				font = &dc.ibfont;
    338 -				frcflags = FRC_ITALICBOLD;
    339 -			} else if (mode & ATTR_ITALIC) {
    340 -				font = &dc.ifont;
    341 -				frcflags = FRC_ITALIC;
    342 -			} else if (mode & ATTR_BOLD) {
    343 -				font = &dc.bfont;
    344 -				frcflags = FRC_BOLD;
    345 +		if (
    346 +			prevmode != mode
    347 +			|| ATTRCMP(glyphs[start], glyphs[i])
    348 +			|| selected(x + i, y) != selected(x + start, y)
    349 +			|| i == (len - 1)
    350 +		) {
    351 +			/* Handle 1-character wide segments and end of line */
    352 +			length = i - start;
    353 +			if (i == start) {
    354 +				length = 1;
    355 +			} else if (i == (len - 1)) {
    356 +				length = (i - start + 1);
    357  			}
    358 -			yp = winy + font->ascent;
    359 -		}
    360  
    361 -		if (mode & ATTR_BOXDRAW) {
    362 -			/* minor shoehorning: boxdraw uses only this ushort */
    363 -			glyphidx = boxdrawindex(&glyphs[i]);
    364 -		} else {
    365 -			/* Lookup character index with default font. */
    366 -			glyphidx = XftCharIndex(xw.dpy, font->match, rune);
    367 -		}
    368 -		if (glyphidx) {
    369 -			specs[numspecs].font = font->match;
    370 -			specs[numspecs].glyph = glyphidx;
    371 -			specs[numspecs].x = (short)xp;
    372 -			specs[numspecs].y = (short)yp;
    373 -			xp += runewidth;
    374 -			numspecs++;
    375 -			continue;
    376 -		}
    377 -
    378 -		/* Fallback on font cache, search the font cache for match. */
    379 -		for (f = 0; f < frclen; f++) {
    380 -			glyphidx = XftCharIndex(xw.dpy, frc[f].font, rune);
    381 -			/* Everything correct. */
    382 -			if (glyphidx && frc[f].flags == frcflags)
    383 -				break;
    384 -			/* We got a default font for a not found glyph. */
    385 -			if (!glyphidx && frc[f].flags == frcflags
    386 -					&& frc[f].unicodep == rune) {
    387 -				break;
    388 +			/* Shape the segment. */
    389 +			hbtransform(&shaped, font->match, glyphs, start, length);
    390 +			runewidth = win.cw * ((glyphs[start].mode & ATTR_WIDE) ? 2.0f : 1.0f);
    391 +			cluster_xp = xp; cluster_yp = yp;
    392 +			for (int code_idx = 0; code_idx < shaped.count; code_idx++) {
    393 +				int idx = shaped.glyphs[code_idx].cluster;
    394 +
    395 +				if (glyphs[start + idx].mode & ATTR_WDUMMY)
    396 +					continue;
    397 +
    398 +				/* Advance the drawing cursor if we've moved to a new cluster */
    399 +				if (code_idx > 0 && idx != shaped.glyphs[code_idx - 1].cluster) {
    400 +					xp += runewidth;
    401 +					cluster_xp = xp;
    402 +					cluster_yp = yp;
    403 +					runewidth = win.cw * ((glyphs[start + idx].mode & ATTR_WIDE) ? 2.0f : 1.0f);
    404 +				}
    405 +
    406 +				if (glyphs[start + idx].mode & ATTR_BOXDRAW) {
    407 +					/* minor shoehorning: boxdraw uses only this ushort */
    408 +					specs[numspecs].font = font->match;
    409 +					specs[numspecs].glyph = boxdrawindex(&glyphs[start + idx]);
    410 +					specs[numspecs].x = xp;
    411 +					specs[numspecs].y = yp;
    412 +					numspecs++;
    413 +				} else if (shaped.glyphs[code_idx].codepoint != 0) {
    414 +					/* If symbol is found, put it into the specs. */
    415 +					specs[numspecs].font = font->match;
    416 +					specs[numspecs].glyph = shaped.glyphs[code_idx].codepoint;
    417 +					specs[numspecs].x = cluster_xp + (short)(shaped.positions[code_idx].x_offset / 64.);
    418 +					specs[numspecs].y = cluster_yp - (short)(shaped.positions[code_idx].y_offset / 64.);
    419 +					cluster_xp += shaped.positions[code_idx].x_advance / 64.;
    420 +					cluster_yp += shaped.positions[code_idx].y_advance / 64.;
    421 +					numspecs++;
    422 +				} else {
    423 +					/* If it's not found, try to fetch it through the font cache. */
    424 +					rune = glyphs[start + idx].u;
    425 +					for (f = 0; f < frclen; f++) {
    426 +						glyphidx = XftCharIndex(xw.dpy, frc[f].font, rune);
    427 +						/* Everything correct. */
    428 +						if (glyphidx && frc[f].flags == frcflags)
    429 +							break;
    430 +						/* We got a default font for a not found glyph. */
    431 +						if (!glyphidx && frc[f].flags == frcflags
    432 +								&& frc[f].unicodep == rune) {
    433 +							break;
    434 +						}
    435 +					}
    436 +
    437 +					/* Nothing was found. Use fontconfig to find matching font. */
    438 +					if (f >= frclen) {
    439 +						if (!font->set)
    440 +							font->set = FcFontSort(0, font->pattern,
    441 +																		 1, 0, &fcres);
    442 +						fcsets[0] = font->set;
    443 +
    444 +						/*
    445 +						 * Nothing was found in the cache. Now use
    446 +						 * some dozen of Fontconfig calls to get the
    447 +						 * font for one single character.
    448 +						 *
    449 +						 * Xft and fontconfig are design failures.
    450 +						 */
    451 +						fcpattern = FcPatternDuplicate(font->pattern);
    452 +						fccharset = FcCharSetCreate();
    453 +
    454 +						FcCharSetAddChar(fccharset, rune);
    455 +						FcPatternAddCharSet(fcpattern, FC_CHARSET,
    456 +								fccharset);
    457 +						FcPatternAddBool(fcpattern, FC_SCALABLE, 1);
    458 +
    459 +						FcConfigSubstitute(0, fcpattern,
    460 +								FcMatchPattern);
    461 +						FcDefaultSubstitute(fcpattern);
    462 +
    463 +						fontpattern = FcFontSetMatch(0, fcsets, 1,
    464 +								fcpattern, &fcres);
    465 +
    466 +						/* Allocate memory for the new cache entry. */
    467 +						if (frclen >= frccap) {
    468 +							frccap += 16;
    469 +							frc = xrealloc(frc, frccap * sizeof(Fontcache));
    470 +						}
    471 +
    472 +						frc[frclen].font = XftFontOpenPattern(xw.dpy,
    473 +								fontpattern);
    474 +						if (!frc[frclen].font)
    475 +							die("XftFontOpenPattern failed seeking fallback font: %s\n",
    476 +								strerror(errno));
    477 +						frc[frclen].flags = frcflags;
    478 +						frc[frclen].unicodep = rune;
    479 +
    480 +						glyphidx = XftCharIndex(xw.dpy, frc[frclen].font, rune);
    481 +
    482 +						f = frclen;
    483 +						frclen++;
    484 +
    485 +						FcPatternDestroy(fcpattern);
    486 +						FcCharSetDestroy(fccharset);
    487 +					}
    488 +
    489 +					specs[numspecs].font = frc[f].font;
    490 +					specs[numspecs].glyph = glyphidx;
    491 +					specs[numspecs].x = (short)xp;
    492 +					specs[numspecs].y = (short)yp;
    493 +					numspecs++;
    494 +				}
    495  			}
    496 -		}
    497  
    498 -		/* Nothing was found. Use fontconfig to find matching font. */
    499 -		if (f >= frclen) {
    500 -			if (!font->set)
    501 -				font->set = FcFontSort(0, font->pattern,
    502 -				                       1, 0, &fcres);
    503 -			fcsets[0] = font->set;
    504 +			/* Cleanup and get ready for next segment. */
    505 +			hbcleanup(&shaped);
    506 +			start = i;
    507  
    508 -			/*
    509 -			 * Nothing was found in the cache. Now use
    510 -			 * some dozen of Fontconfig calls to get the
    511 -			 * font for one single character.
    512 -			 *
    513 -			 * Xft and fontconfig are design failures.
    514 -			 */
    515 -			fcpattern = FcPatternDuplicate(font->pattern);
    516 -			fccharset = FcCharSetCreate();
    517 -
    518 -			FcCharSetAddChar(fccharset, rune);
    519 -			FcPatternAddCharSet(fcpattern, FC_CHARSET,
    520 -					fccharset);
    521 -			FcPatternAddBool(fcpattern, FC_SCALABLE, 1);
    522 -
    523 -			FcConfigSubstitute(0, fcpattern,
    524 -					FcMatchPattern);
    525 -			FcDefaultSubstitute(fcpattern);
    526 -
    527 -			fontpattern = FcFontSetMatch(0, fcsets, 1,
    528 -					fcpattern, &fcres);
    529 -
    530 -			/* Allocate memory for the new cache entry. */
    531 -			if (frclen >= frccap) {
    532 -				frccap += 16;
    533 -				frc = xrealloc(frc, frccap * sizeof(Fontcache));
    534 +			/* Determine font for glyph if different from previous glyph. */
    535 +			if (prevmode != mode) {
    536 +				prevmode = mode;
    537 +				xresetfontsettings(mode, &font, &frcflags);
    538 +				yp = winy + font->ascent;
    539  			}
    540 -
    541 -			frc[frclen].font = XftFontOpenPattern(xw.dpy,
    542 -					fontpattern);
    543 -			if (!frc[frclen].font)
    544 -				die("XftFontOpenPattern failed seeking fallback font: %s\n",
    545 -					strerror(errno));
    546 -			frc[frclen].flags = frcflags;
    547 -			frc[frclen].unicodep = rune;
    548 -
    549 -			glyphidx = XftCharIndex(xw.dpy, frc[frclen].font, rune);
    550 -
    551 -			f = frclen;
    552 -			frclen++;
    553 -
    554 -			FcPatternDestroy(fcpattern);
    555 -			FcCharSetDestroy(fccharset);
    556  		}
    557 -
    558 -		specs[numspecs].font = frc[f].font;
    559 -		specs[numspecs].glyph = glyphidx;
    560 -		specs[numspecs].x = (short)xp;
    561 -		specs[numspecs].y = (short)yp;
    562 -		xp += runewidth;
    563 -		numspecs++;
    564  	}
    565  
    566 +	hbcleanup(&shaped);
    567  	return numspecs;
    568  }
    569  
    570 @@ -1528,14 +1581,17 @@ xdrawglyph(Glyph g, int x, int y)
    571  }
    572  
    573  void
    574 -xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og)
    575 +xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og, Line line, int len)
    576  {
    577  	Color drawcol;
    578  
    579  	/* remove the old cursor */
    580  	if (selected(ox, oy))
    581  		og.mode ^= ATTR_REVERSE;
    582 -	xdrawglyph(og, ox, oy);
    583 +
    584 +	/* Redraw the line where cursor was previously.
    585 +	 * It will restore the ligatures broken by the cursor. */
    586 +	xdrawline(line, 0, oy, len);
    587  
    588  	if (IS_SET(MODE_HIDE))
    589  		return;
    590 @@ -1663,18 +1719,16 @@ xdrawline(Line line, int x1, int y1, int x2)
    591  	Glyph base, new;
    592  	XftGlyphFontSpec *specs = xw.specbuf;
    593  
    594 -	numspecs = xmakeglyphfontspecs(specs, &line[x1], x2 - x1, x1, y1);
    595  	i = ox = 0;
    596 -	for (x = x1; x < x2 && i < numspecs; x++) {
    597 +	for (x = x1; x < x2; x++) {
    598  		new = line[x];
    599  		if (new.mode == ATTR_WDUMMY)
    600  			continue;
    601  		if (selected(x, y1))
    602  			new.mode ^= ATTR_REVERSE;
    603 -		if (i > 0 && ATTRCMP(base, new)) {
    604 -			xdrawglyphfontspecs(specs, base, i, ox, y1);
    605 -			specs += i;
    606 -			numspecs -= i;
    607 +		if ((i > 0) && ATTRCMP(base, new)) {
    608 +			numspecs = xmakeglyphfontspecs(specs, &line[ox], x - ox, ox, y1);
    609 +			xdrawglyphfontspecs(specs, base, numspecs, ox, y1);
    610  			i = 0;
    611  		}
    612  		if (i == 0) {
    613 @@ -1683,8 +1737,10 @@ xdrawline(Line line, int x1, int y1, int x2)
    614  		}
    615  		i++;
    616  	}
    617 -	if (i > 0)
    618 -		xdrawglyphfontspecs(specs, base, i, ox, y1);
    619 +	if (i > 0) {
    620 +		numspecs = xmakeglyphfontspecs(specs, &line[ox], x2 - ox, ox, y1);
    621 +		xdrawglyphfontspecs(specs, base, numspecs, ox, y1);
    622 +	}
    623  }
    624  
    625  void