sites

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

st-ligatures-alpha-scrollback-20200406-28ad288.diff (7423B)


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