sites

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

st-ligatures-alpha-scrollback-20230105-0.9.diff (17803B)


      1 diff --git a/Makefile b/Makefile
      2 index 470ac86..38240da 100644
      3 --- a/Makefile
      4 +++ b/Makefile
      5 @@ -4,7 +4,7 @@
      6  
      7  include config.mk
      8  
      9 -SRC = st.c x.c
     10 +SRC = st.c x.c hb.c
     11  OBJ = $(SRC:.c=.o)
     12  
     13  all: options st
     14 @@ -22,7 +22,8 @@ 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 +hb.o: st.h
     21  
     22  $(OBJ): config.h config.mk
     23  
     24 diff --git a/config.mk b/config.mk
     25 index 47c615e..d7439a3 100644
     26 --- a/config.mk
     27 +++ b/config.mk
     28 @@ -15,10 +15,12 @@ PKG_CONFIG = pkg-config
     29  # includes and libs
     30  INCS = -I$(X11INC) \
     31         `$(PKG_CONFIG) --cflags fontconfig` \
     32 -       `$(PKG_CONFIG) --cflags freetype2`
     33 -LIBS = -L$(X11LIB) -lm -lrt -lX11 -lutil -lXft -lXrender\
     34 +       `$(PKG_CONFIG) --cflags freetype2` \
     35 +       `$(PKG_CONFIG) --cflags harfbuzz`
     36 +LIBS = -L$(X11LIB) -lm -lrt -lX11 -lutil -lXft -lXrender \
     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 79ee9ba..454771d 100644
    196 --- a/st.c
    197 +++ b/st.c
    198 @@ -2711,7 +2711,9 @@ draw(void)
    199  	drawregion(0, 0, term.col, term.row);
    200  	if (term.scr == 0)
    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 +
    206  	term.ocx = cx;
    207  	term.ocy = term.c.y;
    208  	xfinishdraw();
    209 diff --git a/st.h b/st.h
    210 index 78762a2..01eea49 100644
    211 --- a/st.h
    212 +++ b/st.h
    213 @@ -11,7 +11,8 @@
    214  #define DIVCEIL(n, d)		(((n) + ((d) - 1)) / (d))
    215  #define DEFAULT(a, b)		(a) = (a) ? (a) : (b)
    216  #define LIMIT(x, a, b)		(x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
    217 -#define ATTRCMP(a, b)		((a).mode != (b).mode || (a).fg != (b).fg || \
    218 +#define ATTRCMP(a, b)		(((a).mode & (~ATTR_WRAP)) != ((b).mode & (~ATTR_WRAP)) || \
    219 +				(a).fg != (b).fg || \
    220  				(a).bg != (b).bg)
    221  #define TIMEDIFF(t1, t2)	((t1.tv_sec-t2.tv_sec)*1000 + \
    222  				(t1.tv_nsec-t2.tv_nsec)/1E6)
    223 diff --git a/win.h b/win.h
    224 index 6de960d..94679e4 100644
    225 --- a/win.h
    226 +++ b/win.h
    227 @@ -25,7 +25,7 @@ enum win_mode {
    228  
    229  void xbell(void);
    230  void xclipcopy(void);
    231 -void xdrawcursor(int, int, Glyph, int, int, Glyph);
    232 +void xdrawcursor(int, int, Glyph, int, int, Glyph, Line, int);
    233  void xdrawline(Line, int, int, int);
    234  void xfinishdraw(void);
    235  void xloadcols(void);
    236 diff --git a/x.c b/x.c
    237 index 27e81d1..5d19ed7 100644
    238 --- a/x.c
    239 +++ b/x.c
    240 @@ -19,6 +19,7 @@ char *argv0;
    241  #include "arg.h"
    242  #include "st.h"
    243  #include "win.h"
    244 +#include "hb.h"
    245  
    246  /* types used in config.h */
    247  typedef struct {
    248 @@ -142,6 +143,7 @@ typedef struct {
    249  } DC;
    250  
    251  static inline ushort sixd_to_16bit(int);
    252 +static void xresetfontsettings(ushort mode, Font **font, int *frcflags);
    253  static int xmakeglyphfontspecs(XftGlyphFontSpec *, const Glyph *, int, int, int);
    254  static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int);
    255  static void xdrawglyph(Glyph, int, int);
    256 @@ -759,7 +761,7 @@ xresize(int col, int row)
    257  	xclear(0, 0, win.w, win.h);
    258  
    259  	/* resize to new width */
    260 -	xw.specbuf = xrealloc(xw.specbuf, col * sizeof(GlyphFontSpec));
    261 +	xw.specbuf = xrealloc(xw.specbuf, col * sizeof(GlyphFontSpec) * 4);
    262  }
    263  
    264  ushort
    265 @@ -1071,6 +1073,9 @@ xunloadfont(Font *f)
    266  void
    267  xunloadfonts(void)
    268  {
    269 +	/* Clear Harfbuzz font cache. */
    270 +	hbunloadfonts();
    271 +
    272  	/* Free the loaded fonts in the font cache.  */
    273  	while (frclen > 0)
    274  		XftFontClose(xw.dpy, frc[--frclen].font);
    275 @@ -1202,7 +1207,7 @@ xinit(int cols, int rows)
    276  	XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h);
    277  
    278  	/* font spec buffer */
    279 -	xw.specbuf = xmalloc(cols * sizeof(GlyphFontSpec));
    280 +	xw.specbuf = xmalloc(cols * sizeof(GlyphFontSpec) * 4);
    281  
    282  	/* Xft rendering context */
    283  	xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
    284 @@ -1256,6 +1261,22 @@ xinit(int cols, int rows)
    285  		xsel.xtarget = XA_STRING;
    286  }
    287  
    288 +void
    289 +xresetfontsettings(ushort mode, Font **font, int *frcflags)
    290 +{
    291 +	*font = &dc.font;
    292 +	if ((mode & ATTR_ITALIC) && (mode & ATTR_BOLD)) {
    293 +		*font = &dc.ibfont;
    294 +		*frcflags = FRC_ITALICBOLD;
    295 +	} else if (mode & ATTR_ITALIC) {
    296 +		*font = &dc.ifont;
    297 +		*frcflags = FRC_ITALIC;
    298 +	} else if (mode & ATTR_BOLD) {
    299 +		*font = &dc.bfont;
    300 +		*frcflags = FRC_BOLD;
    301 +	}
    302 +}
    303 +
    304  int
    305  xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x, int y)
    306  {
    307 @@ -1270,119 +1291,148 @@ xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x
    308  	FcPattern *fcpattern, *fontpattern;
    309  	FcFontSet *fcsets[] = { NULL };
    310  	FcCharSet *fccharset;
    311 -	int i, f, numspecs = 0;
    312 +	int i, f, length = 0, start = 0, numspecs = 0;
    313 +  float cluster_xp = xp, cluster_yp = yp;
    314 +	HbTransformData shaped = { 0 };
    315 +
    316 +	/* Initial values. */
    317 +	mode = prevmode = glyphs[0].mode & ~ATTR_WRAP;
    318 +	xresetfontsettings(mode, &font, &frcflags);
    319  
    320  	for (i = 0, xp = winx, yp = winy + font->ascent; i < len; ++i) {
    321 -		/* Fetch rune and mode for current glyph. */
    322 -		rune = glyphs[i].u;
    323 -		mode = glyphs[i].mode;
    324 +		mode = glyphs[i].mode & ~ATTR_WRAP;
    325  
    326  		/* Skip dummy wide-character spacing. */
    327 -		if (mode == ATTR_WDUMMY)
    328 +		if (mode & ATTR_WDUMMY && i < (len - 1))
    329  			continue;
    330  
    331 -		/* Determine font for glyph if different from previous glyph. */
    332 -		if (prevmode != mode) {
    333 -			prevmode = mode;
    334 -			font = &dc.font;
    335 -			frcflags = FRC_NORMAL;
    336 -			runewidth = win.cw * ((mode & ATTR_WIDE) ? 2.0f : 1.0f);
    337 -			if ((mode & ATTR_ITALIC) && (mode & ATTR_BOLD)) {
    338 -				font = &dc.ibfont;
    339 -				frcflags = FRC_ITALICBOLD;
    340 -			} else if (mode & ATTR_ITALIC) {
    341 -				font = &dc.ifont;
    342 -				frcflags = FRC_ITALIC;
    343 -			} else if (mode & ATTR_BOLD) {
    344 -				font = &dc.bfont;
    345 -				frcflags = FRC_BOLD;
    346 +		if (
    347 +			prevmode != mode
    348 +			|| ATTRCMP(glyphs[start], glyphs[i])
    349 +			|| selected(x + i, y) != selected(x + start, y)
    350 +			|| i == (len - 1)
    351 +		) {
    352 +			/* Handle 1-character wide segments and end of line */
    353 +			length = i - start;
    354 +			if (i == start) {
    355 +				length = 1;
    356 +			} else if (i == (len - 1)) {
    357 +				length = (i - start + 1);
    358  			}
    359 -			yp = winy + font->ascent;
    360 -		}
    361 -
    362 -		/* Lookup character index with default font. */
    363 -		glyphidx = XftCharIndex(xw.dpy, font->match, rune);
    364 -		if (glyphidx) {
    365 -			specs[numspecs].font = font->match;
    366 -			specs[numspecs].glyph = glyphidx;
    367 -			specs[numspecs].x = (short)xp;
    368 -			specs[numspecs].y = (short)yp;
    369 -			xp += runewidth;
    370 -			numspecs++;
    371 -			continue;
    372 -		}
    373  
    374 -		/* Fallback on font cache, search the font cache for match. */
    375 -		for (f = 0; f < frclen; f++) {
    376 -			glyphidx = XftCharIndex(xw.dpy, frc[f].font, rune);
    377 -			/* Everything correct. */
    378 -			if (glyphidx && frc[f].flags == frcflags)
    379 -				break;
    380 -			/* We got a default font for a not found glyph. */
    381 -			if (!glyphidx && frc[f].flags == frcflags
    382 -					&& frc[f].unicodep == rune) {
    383 -				break;
    384 +			/* Shape the segment. */
    385 +			hbtransform(&shaped, font->match, glyphs, start, length);
    386 +			runewidth = win.cw * ((glyphs[start].mode & ATTR_WIDE) ? 2.0f : 1.0f);
    387 +			cluster_xp = xp; cluster_yp = yp;
    388 +			for (int code_idx = 0; code_idx < shaped.count; code_idx++) {
    389 +				int idx = shaped.glyphs[code_idx].cluster;
    390 +
    391 +				if (glyphs[start + idx].mode & ATTR_WDUMMY)
    392 +					continue;
    393 +
    394 +				/* Advance the drawing cursor if we've moved to a new cluster */
    395 +				if (code_idx > 0 && idx != shaped.glyphs[code_idx - 1].cluster) {
    396 +					xp += runewidth;
    397 +					cluster_xp = xp;
    398 +					cluster_yp = yp;
    399 +					runewidth = win.cw * ((glyphs[start + idx].mode & ATTR_WIDE) ? 2.0f : 1.0f);
    400 +				}
    401 +
    402 +				if (shaped.glyphs[code_idx].codepoint != 0) {
    403 +					/* If symbol is found, put it into the specs. */
    404 +					specs[numspecs].font = font->match;
    405 +					specs[numspecs].glyph = shaped.glyphs[code_idx].codepoint;
    406 +					specs[numspecs].x = cluster_xp + (short)(shaped.positions[code_idx].x_offset / 64.);
    407 +					specs[numspecs].y = cluster_yp - (short)(shaped.positions[code_idx].y_offset / 64.);
    408 +					cluster_xp += shaped.positions[code_idx].x_advance / 64.;
    409 +					cluster_yp += shaped.positions[code_idx].y_advance / 64.;
    410 +					numspecs++;
    411 +				} else {
    412 +					/* If it's not found, try to fetch it through the font cache. */
    413 +					rune = glyphs[start + idx].u;
    414 +					for (f = 0; f < frclen; f++) {
    415 +						glyphidx = XftCharIndex(xw.dpy, frc[f].font, rune);
    416 +						/* Everything correct. */
    417 +						if (glyphidx && frc[f].flags == frcflags)
    418 +							break;
    419 +						/* We got a default font for a not found glyph. */
    420 +						if (!glyphidx && frc[f].flags == frcflags
    421 +								&& frc[f].unicodep == rune) {
    422 +							break;
    423 +						}
    424 +					}
    425 +
    426 +					/* Nothing was found. Use fontconfig to find matching font. */
    427 +					if (f >= frclen) {
    428 +						if (!font->set)
    429 +							font->set = FcFontSort(0, font->pattern,
    430 +																		 1, 0, &fcres);
    431 +						fcsets[0] = font->set;
    432 +
    433 +						/*
    434 +						 * Nothing was found in the cache. Now use
    435 +						 * some dozen of Fontconfig calls to get the
    436 +						 * font for one single character.
    437 +						 *
    438 +						 * Xft and fontconfig are design failures.
    439 +						 */
    440 +						fcpattern = FcPatternDuplicate(font->pattern);
    441 +						fccharset = FcCharSetCreate();
    442 +
    443 +						FcCharSetAddChar(fccharset, rune);
    444 +						FcPatternAddCharSet(fcpattern, FC_CHARSET,
    445 +								fccharset);
    446 +						FcPatternAddBool(fcpattern, FC_SCALABLE, 1);
    447 +
    448 +						FcConfigSubstitute(0, fcpattern,
    449 +								FcMatchPattern);
    450 +						FcDefaultSubstitute(fcpattern);
    451 +
    452 +						fontpattern = FcFontSetMatch(0, fcsets, 1,
    453 +								fcpattern, &fcres);
    454 +
    455 +						/* Allocate memory for the new cache entry. */
    456 +						if (frclen >= frccap) {
    457 +							frccap += 16;
    458 +							frc = xrealloc(frc, frccap * sizeof(Fontcache));
    459 +						}
    460 +
    461 +						frc[frclen].font = XftFontOpenPattern(xw.dpy,
    462 +								fontpattern);
    463 +						if (!frc[frclen].font)
    464 +							die("XftFontOpenPattern failed seeking fallback font: %s\n",
    465 +								strerror(errno));
    466 +						frc[frclen].flags = frcflags;
    467 +						frc[frclen].unicodep = rune;
    468 +
    469 +						glyphidx = XftCharIndex(xw.dpy, frc[frclen].font, rune);
    470 +
    471 +						f = frclen;
    472 +						frclen++;
    473 +
    474 +						FcPatternDestroy(fcpattern);
    475 +						FcCharSetDestroy(fccharset);
    476 +					}
    477 +
    478 +					specs[numspecs].font = frc[f].font;
    479 +					specs[numspecs].glyph = glyphidx;
    480 +					specs[numspecs].x = (short)xp;
    481 +					specs[numspecs].y = (short)yp;
    482 +					numspecs++;
    483 +				}
    484  			}
    485 -		}
    486 -
    487 -		/* Nothing was found. Use fontconfig to find matching font. */
    488 -		if (f >= frclen) {
    489 -			if (!font->set)
    490 -				font->set = FcFontSort(0, font->pattern,
    491 -				                       1, 0, &fcres);
    492 -			fcsets[0] = font->set;
    493  
    494 -			/*
    495 -			 * Nothing was found in the cache. Now use
    496 -			 * some dozen of Fontconfig calls to get the
    497 -			 * font for one single character.
    498 -			 *
    499 -			 * Xft and fontconfig are design failures.
    500 -			 */
    501 -			fcpattern = FcPatternDuplicate(font->pattern);
    502 -			fccharset = FcCharSetCreate();
    503 -
    504 -			FcCharSetAddChar(fccharset, rune);
    505 -			FcPatternAddCharSet(fcpattern, FC_CHARSET,
    506 -					fccharset);
    507 -			FcPatternAddBool(fcpattern, FC_SCALABLE, 1);
    508 +			/* Cleanup and get ready for next segment. */
    509 +			hbcleanup(&shaped);
    510 +			start = i;
    511  
    512 -			FcConfigSubstitute(0, fcpattern,
    513 -					FcMatchPattern);
    514 -			FcDefaultSubstitute(fcpattern);
    515 -
    516 -			fontpattern = FcFontSetMatch(0, fcsets, 1,
    517 -					fcpattern, &fcres);
    518 -
    519 -			/* Allocate memory for the new cache entry. */
    520 -			if (frclen >= frccap) {
    521 -				frccap += 16;
    522 -				frc = xrealloc(frc, frccap * sizeof(Fontcache));
    523 +			/* Determine font for glyph if different from previous glyph. */
    524 +			if (prevmode != mode) {
    525 +				prevmode = mode;
    526 +				xresetfontsettings(mode, &font, &frcflags);
    527 +				yp = winy + font->ascent;
    528  			}
    529 -
    530 -			frc[frclen].font = XftFontOpenPattern(xw.dpy,
    531 -					fontpattern);
    532 -			if (!frc[frclen].font)
    533 -				die("XftFontOpenPattern failed seeking fallback font: %s\n",
    534 -					strerror(errno));
    535 -			frc[frclen].flags = frcflags;
    536 -			frc[frclen].unicodep = rune;
    537 -
    538 -			glyphidx = XftCharIndex(xw.dpy, frc[frclen].font, rune);
    539 -
    540 -			f = frclen;
    541 -			frclen++;
    542 -
    543 -			FcPatternDestroy(fcpattern);
    544 -			FcCharSetDestroy(fccharset);
    545  		}
    546 -
    547 -		specs[numspecs].font = frc[f].font;
    548 -		specs[numspecs].glyph = glyphidx;
    549 -		specs[numspecs].x = (short)xp;
    550 -		specs[numspecs].y = (short)yp;
    551 -		xp += runewidth;
    552 -		numspecs++;
    553  	}
    554  
    555  	return numspecs;
    556 @@ -1534,14 +1584,17 @@ xdrawglyph(Glyph g, int x, int y)
    557  }
    558  
    559  void
    560 -xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og)
    561 +xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og, Line line, int len)
    562  {
    563  	Color drawcol;
    564  
    565  	/* remove the old cursor */
    566  	if (selected(ox, oy))
    567  		og.mode ^= ATTR_REVERSE;
    568 -	xdrawglyph(og, ox, oy);
    569 +
    570 +	/* Redraw the line where cursor was previously.
    571 +	 * It will restore the ligatures broken by the cursor. */
    572 +	xdrawline(line, 0, oy, len);
    573  
    574  	if (IS_SET(MODE_HIDE))
    575  		return;
    576 @@ -1669,18 +1722,16 @@ xdrawline(Line line, int x1, int y1, int x2)
    577  	Glyph base, new;
    578  	XftGlyphFontSpec *specs = xw.specbuf;
    579  
    580 -	numspecs = xmakeglyphfontspecs(specs, &line[x1], x2 - x1, x1, y1);
    581  	i = ox = 0;
    582 -	for (x = x1; x < x2 && i < numspecs; x++) {
    583 +	for (x = x1; x < x2; x++) {
    584  		new = line[x];
    585  		if (new.mode == ATTR_WDUMMY)
    586  			continue;
    587  		if (selected(x, y1))
    588  			new.mode ^= ATTR_REVERSE;
    589 -		if (i > 0 && ATTRCMP(base, new)) {
    590 -			xdrawglyphfontspecs(specs, base, i, ox, y1);
    591 -			specs += i;
    592 -			numspecs -= i;
    593 +		if ((i > 0) && ATTRCMP(base, new)) {
    594 +			numspecs = xmakeglyphfontspecs(specs, &line[ox], x - ox, ox, y1);
    595 +			xdrawglyphfontspecs(specs, base, numspecs, ox, y1);
    596  			i = 0;
    597  		}
    598  		if (i == 0) {
    599 @@ -1689,8 +1740,10 @@ xdrawline(Line line, int x1, int y1, int x2)
    600  		}
    601  		i++;
    602  	}
    603 -	if (i > 0)
    604 -		xdrawglyphfontspecs(specs, base, i, ox, y1);
    605 +	if (i > 0) {
    606 +		numspecs = xmakeglyphfontspecs(specs, &line[ox], x2 - ox, ox, y1);
    607 +		xdrawglyphfontspecs(specs, base, numspecs, ox, y1);
    608 +	}
    609  }
    610  
    611  void