st

my build of st
git clone git://giovanniamaral.com/st
Log | Files | Refs | README | LICENSE

hb.c (3366B)


      1 #include <stdlib.h>
      2 #include <stdio.h>
      3 #include <math.h>
      4 #include <X11/Xft/Xft.h>
      5 #include <X11/cursorfont.h>
      6 #include <hb.h>
      7 #include <hb-ft.h>
      8 
      9 #include "st.h"
     10 #include "hb.h"
     11 
     12 #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 }
     13 #define BUFFER_STEP 256
     14 
     15 hb_font_t *hbfindfont(XftFont *match);
     16 
     17 typedef struct {
     18 	XftFont *match;
     19 	hb_font_t *font;
     20 } HbFontMatch;
     21 
     22 typedef struct {
     23 	size_t capacity;
     24 	HbFontMatch *fonts;
     25 } HbFontCache;
     26 
     27 static HbFontCache hbfontcache = { 0, NULL };
     28 
     29 typedef struct {
     30 	size_t capacity;
     31 	Rune *runes;
     32 } RuneBuffer;
     33 
     34 static RuneBuffer hbrunebuffer = { 0, NULL };
     35 
     36 /*
     37  * Poplulate the array with a list of font features, wrapped in FEATURE macro,
     38  * e. g.
     39  * FEATURE('c', 'a', 'l', 't'), FEATURE('d', 'l', 'i', 'g')
     40  */
     41 hb_feature_t features[] = { };
     42 
     43 void
     44 hbunloadfonts()
     45 {
     46 	for (int i = 0; i < hbfontcache.capacity; i++) {
     47 		hb_font_destroy(hbfontcache.fonts[i].font);
     48 		XftUnlockFace(hbfontcache.fonts[i].match);
     49 	}
     50 
     51 	if (hbfontcache.fonts != NULL) {
     52 		free(hbfontcache.fonts);
     53 		hbfontcache.fonts = NULL;
     54 	}
     55 	hbfontcache.capacity = 0;
     56 }
     57 
     58 hb_font_t *
     59 hbfindfont(XftFont *match)
     60 {
     61 	for (int i = 0; i < hbfontcache.capacity; i++) {
     62 		if (hbfontcache.fonts[i].match == match)
     63 			return hbfontcache.fonts[i].font;
     64 	}
     65 
     66 	/* Font not found in cache, caching it now. */
     67 	hbfontcache.fonts = realloc(hbfontcache.fonts, sizeof(HbFontMatch) * (hbfontcache.capacity + 1));
     68 	FT_Face face = XftLockFace(match);
     69 	hb_font_t *font = hb_ft_font_create(face, NULL);
     70 	if (font == NULL)
     71 		die("Failed to load Harfbuzz font.");
     72 
     73 	hbfontcache.fonts[hbfontcache.capacity].match = match;
     74 	hbfontcache.fonts[hbfontcache.capacity].font = font;
     75 	hbfontcache.capacity += 1;
     76 
     77 	return font;
     78 }
     79 
     80 void hbtransform(HbTransformData *data, XftFont *xfont, const Glyph *glyphs, int start, int length) {
     81 	ushort mode = USHRT_MAX;
     82 	unsigned int glyph_count;
     83 	int rune_idx, glyph_idx, end = start + length;
     84 
     85 	hb_font_t *font = hbfindfont(xfont);
     86 	if (font == NULL)
     87 		return;
     88 
     89 	hb_buffer_t *buffer = hb_buffer_create();
     90 	hb_buffer_set_direction(buffer, HB_DIRECTION_LTR);
     91 	hb_buffer_set_cluster_level(buffer, HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS);
     92 
     93 	/* Resize the buffer if required length is larger. */
     94 	if (hbrunebuffer.capacity < length) {
     95 		hbrunebuffer.capacity = (length / BUFFER_STEP + 1) * BUFFER_STEP;
     96 		hbrunebuffer.runes = realloc(hbrunebuffer.runes, hbrunebuffer.capacity * sizeof(Rune));
     97 	}
     98 
     99 	/* Fill buffer with codepoints. */
    100 	for (rune_idx = 0, glyph_idx = start; glyph_idx < end; glyph_idx++, rune_idx++) {
    101 		hbrunebuffer.runes[rune_idx] = glyphs[glyph_idx].u;
    102 		mode = glyphs[glyph_idx].mode;
    103 		if (mode & ATTR_WDUMMY)
    104 			hbrunebuffer.runes[rune_idx] = 0x0020;
    105 	}
    106 	hb_buffer_add_codepoints(buffer, hbrunebuffer.runes, length, 0, length);
    107 
    108 	/* Shape the segment. */
    109 	hb_shape(font, buffer, features, sizeof(features)/sizeof(hb_feature_t));
    110 
    111 	/* Get new glyph info. */
    112 	hb_glyph_info_t *info = hb_buffer_get_glyph_infos(buffer, &glyph_count);
    113 	hb_glyph_position_t *pos = hb_buffer_get_glyph_positions(buffer, &glyph_count);
    114 
    115 	/* Fill the output. */
    116 	data->buffer = buffer;
    117 	data->glyphs = info;
    118 	data->positions = pos;
    119 	data->count = glyph_count;
    120 }
    121 
    122 void hbcleanup(HbTransformData *data) {
    123 	hb_buffer_destroy(data->buffer);
    124 	memset(data, 0, sizeof(HbTransformData));
    125 }