st-ligatures-scrollback-20210824-0.8.4.diff (8215B)
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 c070a4a..3d236f0 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 + `$(PKG_CONFIG) --cflags freetype2` \ 34 + `$(PKG_CONFIG) --cflags harfbuzz` 35 LIBS = -L$(X11LIB) -lm -lrt -lX11 -lutil -lXft \ 36 `$(PKG_CONFIG) --libs fontconfig` \ 37 - `$(PKG_CONFIG) --libs freetype2` 38 + `$(PKG_CONFIG) --libs freetype2` \ 39 + `$(PKG_CONFIG) --libs harfbuzz` 40 41 # flags 42 STCPPFLAGS = -DVERSION=\"$(VERSION)\" -D_XOPEN_SOURCE=600 43 diff --git a/hb.c b/hb.c 44 new file mode 100644 45 index 0000000..f9c4f76 46 --- /dev/null 47 +++ b/hb.c 48 @@ -0,0 +1,144 @@ 49 +#include <stdlib.h> 50 +#include <stdio.h> 51 +#include <math.h> 52 +#include <X11/Xft/Xft.h> 53 +#include <hb.h> 54 +#include <hb-ft.h> 55 + 56 +#include "st.h" 57 + 58 +#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 } 59 + 60 +void hbtransformsegment(XftFont *xfont, const Glyph *string, hb_codepoint_t *codepoints, int start, int length); 61 +hb_font_t *hbfindfont(XftFont *match); 62 + 63 +typedef struct { 64 + XftFont *match; 65 + hb_font_t *font; 66 +} HbFontMatch; 67 + 68 +static int hbfontslen = 0; 69 +static HbFontMatch *hbfontcache = NULL; 70 + 71 +/* 72 + * Replace 0 with a list of font features, wrapped in FEATURE macro, e.g. 73 + * FEATURE('c', 'a', 'l', 't'), FEATURE('d', 'l', 'i', 'g') 74 + */ 75 +hb_feature_t features[] = { 0 }; 76 + 77 +void 78 +hbunloadfonts() 79 +{ 80 + for (int i = 0; i < hbfontslen; i++) { 81 + hb_font_destroy(hbfontcache[i].font); 82 + XftUnlockFace(hbfontcache[i].match); 83 + } 84 + 85 + if (hbfontcache != NULL) { 86 + free(hbfontcache); 87 + hbfontcache = NULL; 88 + } 89 + hbfontslen = 0; 90 +} 91 + 92 +hb_font_t * 93 +hbfindfont(XftFont *match) 94 +{ 95 + for (int i = 0; i < hbfontslen; i++) { 96 + if (hbfontcache[i].match == match) 97 + return hbfontcache[i].font; 98 + } 99 + 100 + /* Font not found in cache, caching it now. */ 101 + hbfontcache = realloc(hbfontcache, sizeof(HbFontMatch) * (hbfontslen + 1)); 102 + FT_Face face = XftLockFace(match); 103 + hb_font_t *font = hb_ft_font_create(face, NULL); 104 + if (font == NULL) 105 + die("Failed to load Harfbuzz font."); 106 + 107 + hbfontcache[hbfontslen].match = match; 108 + hbfontcache[hbfontslen].font = font; 109 + hbfontslen += 1; 110 + 111 + return font; 112 +} 113 + 114 +void 115 +hbtransform(XftGlyphFontSpec *specs, const Glyph *glyphs, size_t len, int x, int y) 116 +{ 117 + int start = 0, length = 1, gstart = 0; 118 + hb_codepoint_t *codepoints = calloc((unsigned int)len, sizeof(hb_codepoint_t)); 119 + 120 + for (int idx = 1, specidx = 1; idx < len; idx++) { 121 + if (glyphs[idx].mode & ATTR_WDUMMY) { 122 + length += 1; 123 + continue; 124 + } 125 + 126 + if (specs[specidx].font != specs[start].font || ATTRCMP(glyphs[gstart], glyphs[idx]) || selected(x + idx, y) != selected(x + gstart, y)) { 127 + hbtransformsegment(specs[start].font, glyphs, codepoints, gstart, length); 128 + 129 + /* Reset the sequence. */ 130 + length = 1; 131 + start = specidx; 132 + gstart = idx; 133 + } else { 134 + length += 1; 135 + } 136 + 137 + specidx++; 138 + } 139 + 140 + /* EOL. */ 141 + hbtransformsegment(specs[start].font, glyphs, codepoints, gstart, length); 142 + 143 + /* Apply the transformation to glyph specs. */ 144 + for (int i = 0, specidx = 0; i < len; i++) { 145 + if (glyphs[i].mode & ATTR_WDUMMY) 146 + continue; 147 + 148 + if (codepoints[i] != specs[specidx].glyph) 149 + ((Glyph *)glyphs)[i].mode |= ATTR_LIGA; 150 + 151 + specs[specidx++].glyph = codepoints[i]; 152 + } 153 + 154 + free(codepoints); 155 +} 156 + 157 +void 158 +hbtransformsegment(XftFont *xfont, const Glyph *string, hb_codepoint_t *codepoints, int start, int length) 159 +{ 160 + hb_font_t *font = hbfindfont(xfont); 161 + if (font == NULL) 162 + return; 163 + 164 + Rune rune; 165 + ushort mode = USHRT_MAX; 166 + hb_buffer_t *buffer = hb_buffer_create(); 167 + hb_buffer_set_direction(buffer, HB_DIRECTION_LTR); 168 + 169 + /* Fill buffer with codepoints. */ 170 + for (int i = start; i < (start+length); i++) { 171 + rune = string[i].u; 172 + mode = string[i].mode; 173 + if (mode & ATTR_WDUMMY) 174 + rune = 0x0020; 175 + hb_buffer_add_codepoints(buffer, &rune, 1, 0, 1); 176 + } 177 + 178 + /* Shape the segment. */ 179 + hb_shape(font, buffer, features, sizeof(features)); 180 + 181 + /* Get new glyph info. */ 182 + hb_glyph_info_t *info = hb_buffer_get_glyph_infos(buffer, NULL); 183 + 184 + /* Write new codepoints. */ 185 + for (int i = 0; i < length; i++) { 186 + hb_codepoint_t gid = info[i].codepoint; 187 + codepoints[start+i] = gid; 188 + } 189 + 190 + /* Cleanup. */ 191 + hb_buffer_destroy(buffer); 192 +} 193 diff --git a/hb.h b/hb.h 194 new file mode 100644 195 index 0000000..07888df 196 --- /dev/null 197 +++ b/hb.h 198 @@ -0,0 +1,6 @@ 199 +#include <X11/Xft/Xft.h> 200 +#include <hb.h> 201 +#include <hb-ft.h> 202 + 203 +void hbunloadfonts(); 204 +void hbtransform(XftGlyphFontSpec *, const Glyph *, size_t, int, int); 205 diff --git a/st.c b/st.c 206 index edec064..ea13c13 100644 207 --- a/st.c 208 +++ b/st.c 209 @@ -2652,7 +2652,8 @@ draw(void) 210 drawregion(0, 0, term.col, term.row); 211 if (term.scr == 0) 212 xdrawcursor(cx, term.c.y, term.line[term.c.y][cx], 213 - term.ocx, term.ocy, term.line[term.ocy][term.ocx]); 214 + term.ocx, term.ocy, term.line[term.ocy][term.ocx], 215 + term.line[term.ocy], term.col); 216 term.ocx = cx; 217 term.ocy = term.c.y; 218 xfinishdraw(); 219 diff --git a/st.h b/st.h 220 index f44e1d3..00c796c 100644 221 --- a/st.h 222 +++ b/st.h 223 @@ -11,7 +11,8 @@ 224 #define DIVCEIL(n, d) (((n) + ((d) - 1)) / (d)) 225 #define DEFAULT(a, b) (a) = (a) ? (a) : (b) 226 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x) 227 -#define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || \ 228 +#define ATTRCMP(a, b) (((a).mode & (~ATTR_WRAP) & (~ATTR_LIGA)) != ((b).mode & (~ATTR_WRAP) & (~ATTR_LIGA)) || \ 229 + (a).fg != (b).fg || \ 230 (a).bg != (b).bg) 231 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + \ 232 (t1.tv_nsec-t2.tv_nsec)/1E6) 233 @@ -33,6 +34,7 @@ enum glyph_attribute { 234 ATTR_WRAP = 1 << 8, 235 ATTR_WIDE = 1 << 9, 236 ATTR_WDUMMY = 1 << 10, 237 + ATTR_LIGA = 1 << 11, 238 ATTR_BOLD_FAINT = ATTR_BOLD | ATTR_FAINT, 239 }; 240 241 diff --git a/win.h b/win.h 242 index a6ef1b9..bc0d180 100644 243 --- a/win.h 244 +++ b/win.h 245 @@ -25,7 +25,7 @@ enum win_mode { 246 247 void xbell(void); 248 void xclipcopy(void); 249 -void xdrawcursor(int, int, Glyph, int, int, Glyph); 250 +void xdrawcursor(int, int, Glyph, int, int, Glyph, Line, int); 251 void xdrawline(Line, int, int, int); 252 void xfinishdraw(void); 253 void xloadcols(void); 254 diff --git a/x.c b/x.c 255 index 210f184..f6d67ef 100644 256 --- a/x.c 257 +++ b/x.c 258 @@ -19,6 +19,7 @@ char *argv0; 259 #include "arg.h" 260 #include "st.h" 261 #include "win.h" 262 +#include "hb.h" 263 264 /* types used in config.h */ 265 typedef struct { 266 @@ -1031,6 +1032,9 @@ xunloadfont(Font *f) 267 void 268 xunloadfonts(void) 269 { 270 + /* Clear Harfbuzz font cache. */ 271 + hbunloadfonts(); 272 + 273 /* Free the loaded fonts in the font cache. */ 274 while (frclen > 0) 275 XftFontClose(xw.dpy, frc[--frclen].font); 276 @@ -1229,7 +1233,7 @@ xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x 277 mode = glyphs[i].mode; 278 279 /* Skip dummy wide-character spacing. */ 280 - if (mode == ATTR_WDUMMY) 281 + if (mode & ATTR_WDUMMY) 282 continue; 283 284 /* Determine font for glyph if different from previous glyph. */ 285 @@ -1336,6 +1340,9 @@ xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x 286 numspecs++; 287 } 288 289 + /* Harfbuzz transformation for ligatures. */ 290 + hbtransform(specs, glyphs, len, x, y); 291 + 292 return numspecs; 293 } 294 295 @@ -1485,14 +1492,17 @@ xdrawglyph(Glyph g, int x, int y) 296 } 297 298 void 299 -xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og) 300 +xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og, Line line, int len) 301 { 302 Color drawcol; 303 304 /* remove the old cursor */ 305 if (selected(ox, oy)) 306 og.mode ^= ATTR_REVERSE; 307 - xdrawglyph(og, ox, oy); 308 + 309 + /* Redraw the line where cursor was previously. 310 + * It will restore the ligatures broken by the cursor. */ 311 + xdrawline(line, 0, oy, len); 312 313 if (IS_SET(MODE_HIDE)) 314 return;