HTTPS
https://git.nageyqma.icu/nageyqma/st.git
SSH
git@git.nageyqma.icu:nageyqma/st.git
HTTPS 链接可直接 git clone;推送代码请使用 SSH。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
/* See LICENSE for license details. */
#include <stdlib.h>
#include <stdio.h>
#include <hb.h>
#include <hb-ft.h>
#include "st.h"
#include "render.h"
#include "hb.h"
void hbtransformsegment(FT_Face, const Glyph *, hb_codepoint_t *, int, int);
hb_font_t *hbfindfont(FT_Face);
typedef struct {
FT_Face face;
hb_font_t *font;
} HbFontMatch;
static int hbfontslen = 0;
static HbFontMatch *hbfontcache = NULL;
void
hbunloadfonts(void)
{
int i;
for (i = 0; i < hbfontslen; i++)
hb_font_destroy(hbfontcache[i].font);
free(hbfontcache);
hbfontcache = NULL;
hbfontslen = 0;
}
hb_font_t *
hbfindfont(FT_Face face)
{
int i;
hb_font_t *font;
for (i = 0; i < hbfontslen; i++)
if (hbfontcache[i].face == face)
return hbfontcache[i].font;
hbfontcache = realloc(hbfontcache,
sizeof(HbFontMatch) * (hbfontslen + 1));
if (!hbfontcache)
die("realloc: failed to grow HarfBuzz font cache\n");
font = hb_ft_font_create(face, NULL);
if (!font)
die("Failed to load Harfbuzz font.\n");
hbfontcache[hbfontslen].face = face;
hbfontcache[hbfontslen].font = font;
hbfontslen++;
return font;
}
void
hbtransform(GlyphSpec *specs, const Glyph *glyphs, size_t len, int x, int y)
{
int start = 0, length = 1, gstart = 0;
hb_codepoint_t *codepoints;
int idx, specidx;
codepoints = calloc(len, sizeof(hb_codepoint_t));
if (!codepoints)
die("calloc: failed to allocate HarfBuzz buffer\n");
for (idx = 1, specidx = 1; idx < (int)len; idx++) {
if (glyphs[idx].mode & ATTR_WDUMMY) {
length++;
continue;
}
if (specs[specidx].font != specs[start].font ||
ATTRCMP(glyphs[gstart], glyphs[idx]) ||
selected(x + idx, y) != selected(x + gstart, y)) {
hbtransformsegment((FT_Face)specs[start].font, glyphs,
codepoints, gstart, length);
length = 1;
start = specidx;
gstart = idx;
} else {
length++;
}
specidx++;
}
/* EOL. */
hbtransformsegment((FT_Face)specs[start].font, glyphs, codepoints,
gstart, length);
/* Apply the transformation to glyph specs. */
for (idx = 0, specidx = 0; idx < (int)len; idx++) {
if (glyphs[idx].mode & ATTR_WDUMMY)
continue;
if (glyphs[idx].mode & ATTR_BOXDRAW) {
specidx++;
continue;
}
if (codepoints[idx] != specs[specidx].glyph)
((Glyph *)glyphs)[idx].mode |= ATTR_LIGA;
specs[specidx++].glyph = codepoints[idx];
}
free(codepoints);
}
void
hbtransformsegment(FT_Face face, const Glyph *string,
hb_codepoint_t *codepoints, int start, int length)
{
hb_font_t *font = hbfindfont(face);
hb_buffer_t *buffer;
hb_glyph_info_t *info;
unsigned int count, i;
Rune rune;
ushort mode;
if (font == NULL)
return;
buffer = hb_buffer_create();
hb_buffer_set_direction(buffer, HB_DIRECTION_LTR);
/* Fill buffer with codepoints. */
for (i = 0; i < (unsigned)length; i++) {
rune = string[start + i].u;
mode = string[start + i].mode;
if (mode & ATTR_WDUMMY)
rune = 0x0020;
hb_buffer_add_codepoints(buffer, &rune, 1, 0, 1);
}
/* Shape the segment. */
hb_shape(font, buffer, NULL, 0);
/* Get new glyph info. */
info = hb_buffer_get_glyph_infos(buffer, &count);
/* Write new codepoints. A ligature can collapse several cells into
* one glyph; leave the remaining cells with glyph 0, which wl.c skips. */
for (i = 0; i < (unsigned)length; i++)
codepoints[start + i] = i < count ? info[i].codepoint : 0;
hb_buffer_destroy(buffer);
}