From 01bd00cac26d97016939ba4c0462a015685e0a3d Mon Sep 17 00:00:00 2001 From: Marc Foley Date: Tue, 13 Nov 2018 11:47:20 +0000 Subject: [PATCH] Add hb_font_set_variations func --- src/uharfbuzz/_harfbuzz.pyx | 20 ++++++++++++++++++++ src/uharfbuzz/charfbuzz.pxd | 7 +++++++ 2 files changed, 27 insertions(+) diff --git a/src/uharfbuzz/_harfbuzz.pyx b/src/uharfbuzz/_harfbuzz.pyx index c975e8a..528712b 100644 --- a/src/uharfbuzz/_harfbuzz.pyx +++ b/src/uharfbuzz/_harfbuzz.pyx @@ -287,6 +287,26 @@ cdef class Font: x, y = value hb_font_set_scale(self._hb_font, x, y) + def set_variations(self, variations: Dict[str, float]) -> None: + cdef unsigned int size + cdef hb_variation_t* hb_variations + cdef bytes packed + cdef hb_variation_t variation + size = len(variations) + hb_variations = malloc(size * sizeof(hb_variation_t)) + if not hb_variations: + raise MemoryError() + + try: + for i, (name, value) in enumerate(variations.items()): + packed = name.encode() + variation.tag = hb_tag_from_string(packed, -1) + variation.value = value + hb_variations[i] = variation + hb_font_set_variations(self._hb_font, hb_variations, size) + finally: + free(hb_variations) + cdef hb_position_t _glyph_h_advance_func(hb_font_t* font, void* font_data, hb_codepoint_t glyph, diff --git a/src/uharfbuzz/charfbuzz.pxd b/src/uharfbuzz/charfbuzz.pxd index d8997c8..956829a 100644 --- a/src/uharfbuzz/charfbuzz.pxd +++ b/src/uharfbuzz/charfbuzz.pxd @@ -141,6 +141,9 @@ cdef extern from "hb.h": hb_codepoint_t glyph, char *name, unsigned int size, void *user_data) + ctypedef struct hb_variation_t: + hb_tag_t tag + float value hb_font_funcs_t* hb_font_funcs_create() void hb_font_funcs_set_glyph_h_advance_func( @@ -164,6 +167,10 @@ cdef extern from "hb.h": void* font_data, hb_destroy_func_t destroy) void hb_font_get_scale(hb_font_t* font, int* x_scale, int* y_scale) void hb_font_set_scale(hb_font_t* font, int x_scale, int y_scale) + void hb_font_set_variations( + hb_font_t* font, + const hb_variation_t* variations, + unsigned int variations_length) void hb_font_destroy(hb_font_t* font) # hb-shape.h