2020-06-05 11:43:14 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
2021-03-29 08:26:04 +00:00
|
|
|
#include <time.h>
|
2022-07-05 13:32:06 +00:00
|
|
|
#include <X11/Xft/Xft.h>
|
|
|
|
#include <X11/cursorfont.h>
|
2020-06-05 11:43:14 +00:00
|
|
|
#include <hb.h>
|
|
|
|
#include <hb-ft.h>
|
2022-07-05 13:32:06 +00:00
|
|
|
|
2020-06-05 11:43:14 +00:00
|
|
|
#include "st.h"
|
2022-12-19 09:09:06 +00:00
|
|
|
#include "hb.h"
|
2020-06-05 11:43:14 +00:00
|
|
|
|
2022-07-05 13:32:06 +00:00
|
|
|
#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 }
|
2024-03-07 14:34:21 +00:00
|
|
|
#define BUFFER_STEP 256
|
2022-07-05 13:32:06 +00:00
|
|
|
|
2020-06-05 11:43:14 +00:00
|
|
|
hb_font_t *hbfindfont(XftFont *match);
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
XftFont *match;
|
|
|
|
hb_font_t *font;
|
|
|
|
} HbFontMatch;
|
|
|
|
|
2024-03-07 14:34:21 +00:00
|
|
|
typedef struct {
|
|
|
|
size_t capacity;
|
|
|
|
HbFontMatch *fonts;
|
|
|
|
} HbFontCache;
|
|
|
|
|
|
|
|
static HbFontCache hbfontcache = { 0, NULL };
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
size_t capacity;
|
|
|
|
Rune *runes;
|
|
|
|
} RuneBuffer;
|
|
|
|
|
|
|
|
static RuneBuffer hbrunebuffer = { 0, NULL };
|
|
|
|
static hb_buffer_t *hbbuffer;
|
2020-06-05 11:43:14 +00:00
|
|
|
|
2022-07-05 13:32:06 +00:00
|
|
|
/*
|
|
|
|
* Poplulate the array with a list of font features, wrapped in FEATURE macro,
|
|
|
|
* e. g.
|
|
|
|
* FEATURE('c', 'a', 'l', 't'), FEATURE('d', 'l', 'i', 'g')
|
|
|
|
*/
|
2024-03-07 14:34:21 +00:00
|
|
|
hb_feature_t features[] = { };
|
2022-07-05 13:32:06 +00:00
|
|
|
|
2020-06-05 11:43:14 +00:00
|
|
|
void
|
2024-03-07 14:34:21 +00:00
|
|
|
hbcreatebuffer(void)
|
2020-06-05 11:43:14 +00:00
|
|
|
{
|
2024-03-07 14:34:21 +00:00
|
|
|
hbbuffer = hb_buffer_create();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
hbdestroybuffer(void)
|
|
|
|
{
|
|
|
|
hb_buffer_destroy(hbbuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
hbunloadfonts(void)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < hbfontcache.capacity; i++) {
|
|
|
|
hb_font_destroy(hbfontcache.fonts[i].font);
|
|
|
|
XftUnlockFace(hbfontcache.fonts[i].match);
|
2020-06-05 11:43:14 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 14:34:21 +00:00
|
|
|
if (hbfontcache.fonts != NULL) {
|
|
|
|
free(hbfontcache.fonts);
|
|
|
|
hbfontcache.fonts = NULL;
|
2020-06-05 11:43:14 +00:00
|
|
|
}
|
2024-03-07 14:34:21 +00:00
|
|
|
hbfontcache.capacity = 0;
|
2020-06-05 11:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hb_font_t *
|
|
|
|
hbfindfont(XftFont *match)
|
|
|
|
{
|
2024-03-07 14:34:21 +00:00
|
|
|
for (int i = 0; i < hbfontcache.capacity; i++) {
|
|
|
|
if (hbfontcache.fonts[i].match == match)
|
|
|
|
return hbfontcache.fonts[i].font;
|
2020-06-05 11:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Font not found in cache, caching it now. */
|
2024-03-07 14:34:21 +00:00
|
|
|
hbfontcache.fonts = realloc(hbfontcache.fonts, sizeof(HbFontMatch) * (hbfontcache.capacity + 1));
|
2020-06-05 11:43:14 +00:00
|
|
|
FT_Face face = XftLockFace(match);
|
|
|
|
hb_font_t *font = hb_ft_font_create(face, NULL);
|
|
|
|
if (font == NULL)
|
|
|
|
die("Failed to load Harfbuzz font.");
|
|
|
|
|
2024-03-07 14:34:21 +00:00
|
|
|
hbfontcache.fonts[hbfontcache.capacity].match = match;
|
|
|
|
hbfontcache.fonts[hbfontcache.capacity].font = font;
|
|
|
|
hbfontcache.capacity += 1;
|
2020-06-05 11:43:14 +00:00
|
|
|
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
2024-03-07 14:34:21 +00:00
|
|
|
void
|
|
|
|
hbtransform(HbTransformData *data, XftFont *xfont, const Glyph *glyphs, int start, int length)
|
|
|
|
{
|
|
|
|
uint32_t mode;
|
2022-12-19 09:09:06 +00:00
|
|
|
unsigned int glyph_count;
|
2024-03-07 14:34:21 +00:00
|
|
|
int rune_idx, glyph_idx, end = start + length;
|
|
|
|
hb_buffer_t *buffer = hbbuffer;
|
2020-06-05 11:43:14 +00:00
|
|
|
|
|
|
|
hb_font_t *font = hbfindfont(xfont);
|
2024-03-07 14:34:21 +00:00
|
|
|
if (font == NULL) {
|
|
|
|
data->count = 0;
|
2020-06-05 11:43:14 +00:00
|
|
|
return;
|
2024-03-07 14:34:21 +00:00
|
|
|
}
|
2020-06-05 11:43:14 +00:00
|
|
|
|
2024-03-07 14:34:21 +00:00
|
|
|
hb_buffer_reset(buffer);
|
2020-06-05 11:43:14 +00:00
|
|
|
hb_buffer_set_direction(buffer, HB_DIRECTION_LTR);
|
2024-03-07 14:34:21 +00:00
|
|
|
hb_buffer_set_cluster_level(buffer, HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS);
|
|
|
|
|
|
|
|
/* Resize the buffer if required length is larger. */
|
|
|
|
if (hbrunebuffer.capacity < length) {
|
|
|
|
hbrunebuffer.capacity = (length / BUFFER_STEP + 1) * BUFFER_STEP;
|
|
|
|
hbrunebuffer.runes = realloc(hbrunebuffer.runes, hbrunebuffer.capacity * sizeof(Rune));
|
|
|
|
}
|
2020-06-05 11:43:14 +00:00
|
|
|
|
|
|
|
/* Fill buffer with codepoints. */
|
2024-03-07 14:34:21 +00:00
|
|
|
for (rune_idx = 0, glyph_idx = start; glyph_idx < end; glyph_idx++, rune_idx++) {
|
|
|
|
hbrunebuffer.runes[rune_idx] = glyphs[glyph_idx].u;
|
|
|
|
mode = glyphs[glyph_idx].mode;
|
2020-06-05 11:43:14 +00:00
|
|
|
if (mode & ATTR_WDUMMY)
|
2024-03-07 14:34:21 +00:00
|
|
|
hbrunebuffer.runes[rune_idx] = 0x0020;
|
2020-06-05 11:43:14 +00:00
|
|
|
}
|
2024-03-07 14:34:21 +00:00
|
|
|
hb_buffer_add_codepoints(buffer, hbrunebuffer.runes, length, 0, length);
|
2020-06-05 11:43:14 +00:00
|
|
|
|
|
|
|
/* Shape the segment. */
|
2022-07-05 13:32:06 +00:00
|
|
|
hb_shape(font, buffer, features, sizeof(features)/sizeof(hb_feature_t));
|
2020-06-05 11:43:14 +00:00
|
|
|
|
|
|
|
/* Get new glyph info. */
|
2022-12-19 09:09:06 +00:00
|
|
|
hb_glyph_info_t *info = hb_buffer_get_glyph_infos(buffer, &glyph_count);
|
|
|
|
hb_glyph_position_t *pos = hb_buffer_get_glyph_positions(buffer, &glyph_count);
|
|
|
|
|
2024-03-07 14:34:21 +00:00
|
|
|
/* Fill the output. */
|
2022-12-19 09:09:06 +00:00
|
|
|
data->buffer = buffer;
|
|
|
|
data->glyphs = info;
|
|
|
|
data->positions = pos;
|
|
|
|
data->count = glyph_count;
|
|
|
|
}
|