4 #include <X11/Xft/Xft.h>
10 void hbtransformsegment(XftFont *xfont, const Glyph *string, hb_codepoint_t *codepoints, int start, int length);
11 hb_font_t *hbfindfont(XftFont *match);
18 static int hbfontslen = 0;
19 static HbFontMatch *hbfontcache = NULL;
24 for (int i = 0; i < hbfontslen; i++) {
25 hb_font_destroy(hbfontcache[i].font);
26 XftUnlockFace(hbfontcache[i].match);
29 if (hbfontcache != NULL) {
37 hbfindfont(XftFont *match)
39 for (int i = 0; i < hbfontslen; i++) {
40 if (hbfontcache[i].match == match)
41 return hbfontcache[i].font;
44 /* Font not found in cache, caching it now. */
45 hbfontcache = realloc(hbfontcache, sizeof(HbFontMatch) * (hbfontslen + 1));
46 FT_Face face = XftLockFace(match);
47 hb_font_t *font = hb_ft_font_create(face, NULL);
49 die("Failed to load Harfbuzz font.");
51 hbfontcache[hbfontslen].match = match;
52 hbfontcache[hbfontslen].font = font;
59 hbtransform(XftGlyphFontSpec *specs, const Glyph *glyphs, size_t len, int x, int y)
61 int start = 0, length = 1, gstart = 0;
62 hb_codepoint_t *codepoints = calloc(len, sizeof(hb_codepoint_t));
64 for (int idx = 1, specidx = 1; idx < len; idx++) {
65 if (glyphs[idx].mode & ATTR_WDUMMY) {
70 if (specs[specidx].font != specs[start].font || ATTRCMP(glyphs[gstart], glyphs[idx]) || selected(x + idx, y) != selected(x + gstart, y)) {
71 hbtransformsegment(specs[start].font, glyphs, codepoints, gstart, length);
73 /* Reset the sequence. */
85 hbtransformsegment(specs[start].font, glyphs, codepoints, gstart, length);
87 /* Apply the transformation to glyph specs. */
88 for (int i = 0, specidx = 0; i < len; i++) {
89 if (glyphs[i].mode & ATTR_WDUMMY)
92 if (codepoints[i] != specs[specidx].glyph)
93 ((Glyph *)glyphs)[i].mode |= ATTR_LIGA;
95 specs[specidx++].glyph = codepoints[i];
102 hbtransformsegment(XftFont *xfont, const Glyph *string, hb_codepoint_t *codepoints, int start, int length)
104 hb_font_t *font = hbfindfont(xfont);
109 ushort mode = USHRT_MAX;
110 hb_buffer_t *buffer = hb_buffer_create();
111 hb_buffer_set_direction(buffer, HB_DIRECTION_LTR);
113 /* Fill buffer with codepoints. */
114 for (int i = start; i < (start+length); i++) {
116 mode = string[i].mode;
117 if (mode & ATTR_WDUMMY)
119 hb_buffer_add_codepoints(buffer, &rune, 1, 0, 1);
122 /* Shape the segment. */
123 hb_shape(font, buffer, NULL, 0);
125 /* Get new glyph info. */
126 hb_glyph_info_t *info = hb_buffer_get_glyph_infos(buffer, NULL);
128 /* Write new codepoints. */
129 for (int i = 0; i < length; i++) {
130 hb_codepoint_t gid = info[i].codepoint;
131 codepoints[start+i] = gid;
135 hb_buffer_destroy(buffer);