st-ligatures-20200405-28ad288.diff (7162B)
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 0cbb002..76c5c4f 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..bb0bea8 46 --- /dev/null 47 +++ b/hb.c 48 @@ -0,0 +1,127 @@ 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 +void hbtransformsegment(XftFont *xfont, const Glyph *string, hb_codepoint_t *codepoints, int start, int length); 59 +hb_font_t *hbfindfont(XftFont *match); 60 + 61 +typedef struct { 62 + XftFont *match; 63 + hb_font_t *font; 64 +} HbFontMatch; 65 + 66 +static int hbfontslen = 0; 67 +static HbFontMatch *hbfontcache = NULL; 68 + 69 +void hbunloadfonts() { 70 + for (int i = 0; i < hbfontslen; i++) 71 + hb_font_destroy(hbfontcache[i].font); 72 + 73 + if (hbfontcache != NULL) { 74 + free(hbfontcache); 75 + hbfontcache = NULL; 76 + } 77 + hbfontslen = 0; 78 +} 79 + 80 +hb_font_t *hbfindfont(XftFont *match) { 81 + for (int i = 0; i < hbfontslen; i++) { 82 + if (hbfontcache[i].match == match) 83 + return hbfontcache[i].font; 84 + } 85 + 86 + // Font not found in cache, caching it now. 87 + hbfontcache = realloc(hbfontcache, sizeof(HbFontMatch) * (hbfontslen + 1)); 88 + FT_Face face = XftLockFace(match); 89 + hb_font_t *font = hb_ft_font_create(face, NULL); 90 + if (font == NULL) 91 + die("Failed to load Harfbuzz font."); 92 + XftUnlockFace(match); 93 + 94 + hbfontcache[hbfontslen].match = match; 95 + hbfontcache[hbfontslen].font = font; 96 + hbfontslen += 1; 97 + 98 + return font; 99 +} 100 + 101 +void hbtransform(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x, int y) { 102 + int start = 0, length = 1, gstart = 0; 103 + hb_codepoint_t *codepoints = calloc(len, sizeof(hb_codepoint_t)); 104 + 105 + for (int idx = 1, specidx = 1; idx < len; idx++) { 106 + if (glyphs[idx].mode & ATTR_WDUMMY) { 107 + length += 1; 108 + continue; 109 + } 110 + 111 + if (specs[specidx].font != specs[start].font || ATTRCMP(glyphs[gstart], glyphs[idx]) || selected(x + idx, y) != selected(x + gstart, y)) { 112 + hbtransformsegment(specs[start].font, glyphs, codepoints, gstart, length); 113 + 114 + // Reset the sequence. 115 + length = 1; 116 + start = specidx; 117 + gstart = idx; 118 + } else { 119 + length += 1; 120 + } 121 + 122 + specidx++; 123 + } 124 + 125 + // EOL. 126 + hbtransformsegment(specs[start].font, glyphs, codepoints, gstart, length); 127 + 128 + // Apply the transformation to glyph specs. 129 + for (int i = 0, specidx = 0; i < len; i++) { 130 + if (glyphs[i].mode & ATTR_WDUMMY) 131 + continue; 132 + 133 + if (codepoints[i] != specs[specidx].glyph) 134 + ((Glyph *)glyphs)[i].mode |= ATTR_LIGA; 135 + 136 + specs[specidx++].glyph = codepoints[i]; 137 + } 138 + 139 + free(codepoints); 140 +} 141 + 142 +void hbtransformsegment(XftFont *xfont, const Glyph *string, hb_codepoint_t *codepoints, int start, int length) { 143 + hb_font_t *font = hbfindfont(xfont); 144 + if (font == NULL) 145 + return; 146 + 147 + Rune rune; 148 + ushort mode = USHRT_MAX; 149 + hb_buffer_t *buffer = hb_buffer_create(); 150 + hb_buffer_set_direction(buffer, HB_DIRECTION_LTR); 151 + 152 + // Fill buffer with codepoints. 153 + for (int i = start; i < (start+length); i++) { 154 + rune = string[i].u; 155 + mode = string[i].mode; 156 + if (mode & ATTR_WDUMMY) 157 + rune = 0x0020; 158 + hb_buffer_add_codepoints(buffer, &rune, 1, 0, 1); 159 + } 160 + 161 + // Shape the segment. 162 + hb_shape(font, buffer, NULL, 0); 163 + 164 + // Get new glyph info. 165 + hb_glyph_info_t *info = hb_buffer_get_glyph_infos(buffer, NULL); 166 + 167 + // Write new codepoints. 168 + for (int i = 0; i < length; i++) { 169 + hb_codepoint_t gid = info[i].codepoint; 170 + codepoints[start+i] = gid; 171 + } 172 + 173 + // Cleanup. 174 + hb_buffer_destroy(buffer); 175 +} 176 diff --git a/hb.h b/hb.h 177 new file mode 100644 178 index 0000000..a209238 179 --- /dev/null 180 +++ b/hb.h 181 @@ -0,0 +1,7 @@ 182 +#include <X11/Xft/Xft.h> 183 +#include <hb.h> 184 +#include <hb-ft.h> 185 + 186 +void hbunloadfonts(); 187 +void hbtransform(XftGlyphFontSpec *, const Glyph *, int, int, int); 188 + 189 diff --git a/st.c b/st.c 190 index 3e48410..073e4c7 100644 191 --- a/st.c 192 +++ b/st.c 193 @@ -2584,8 +2584,14 @@ draw(void) 194 cx--; 195 196 drawregion(0, 0, term.col, term.row); 197 + // Draw current line to format ligatures properly. 198 + xdrawline(term.line[term.c.y], 0, term.c.y, term.col); 199 + 200 xdrawcursor(cx, term.c.y, term.line[term.c.y][cx], 201 term.ocx, term.ocy, term.line[term.ocy][term.ocx]); 202 + // If cursor was on a transformed glyph, we need to redraw the previous line 203 + if (term.ocy != term.c.y && (term.line[term.ocy][term.ocx].mode & ATTR_LIGA)) 204 + xdrawline(term.line[term.ocy], 0, term.ocy, term.col); 205 term.ocx = cx, term.ocy = term.c.y; 206 xfinishdraw(); 207 xximspot(term.ocx, term.ocy); 208 diff --git a/st.h b/st.h 209 index a1928ca..07ba678 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) & (~ATTR_LIGA)) != ((b).mode & (~ATTR_WRAP) & (~ATTR_LIGA)) || \ 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 @@ -33,6 +34,7 @@ enum glyph_attribute { 223 ATTR_WRAP = 1 << 8, 224 ATTR_WIDE = 1 << 9, 225 ATTR_WDUMMY = 1 << 10, 226 + ATTR_LIGA = 1 << 11, 227 ATTR_BOLD_FAINT = ATTR_BOLD | ATTR_FAINT, 228 }; 229 230 diff --git a/x.c b/x.c 231 index 4cf6b21..447f475 100644 232 --- a/x.c 233 +++ b/x.c 234 @@ -19,6 +19,7 @@ static char *argv0; 235 #include "arg.h" 236 #include "st.h" 237 #include "win.h" 238 +#include "hb.h" 239 240 /* types used in config.h */ 241 typedef struct { 242 @@ -1031,6 +1032,9 @@ xunloadfont(Font *f) 243 void 244 xunloadfonts(void) 245 { 246 + /* Clear Harfbuzz font cache */ 247 + hbunloadfonts(); 248 + 249 /* Free the loaded fonts in the font cache. */ 250 while (frclen > 0) 251 XftFontClose(xw.dpy, frc[--frclen].font); 252 @@ -1229,7 +1233,7 @@ xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x 253 mode = glyphs[i].mode; 254 255 /* Skip dummy wide-character spacing. */ 256 - if (mode == ATTR_WDUMMY) 257 + if (mode & ATTR_WDUMMY) 258 continue; 259 260 /* Determine font for glyph if different from previous glyph. */ 261 @@ -1336,6 +1340,9 @@ xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x 262 numspecs++; 263 } 264 265 + // Harfbuzz transformation for ligatures. 266 + hbtransform(specs, glyphs, len, x, y); 267 + 268 return numspecs; 269 } 270