Skip to content

Commit

Permalink
Bind hb_font_funcs_set_variation_glyph_func()
Browse files Browse the repository at this point in the history
  • Loading branch information
khaledhosny committed Dec 30, 2023
1 parent df16a04 commit e8bfad7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/uharfbuzz/_harfbuzz.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,17 @@ cdef hb_bool_t _nominal_glyph_func(hb_font_t* font, void* font_data,
# If the glyph is .notdef, return false, else return true
return int(glyph[0] != 0)

cdef hb_bool_t _variation_glyph_func(hb_font_t* font, void* font_data,
hb_codepoint_t unicode,
hb_codepoint_t variation_selector,
hb_codepoint_t* glyph,
void* user_data) noexcept:
cdef Font py_font = <Font>font_data
glyph[0] = (<FontFuncs>py_font.funcs)._variation_glyph_func(
py_font, unicode, variation_selector, <object>user_data)
# If the glyph is .notdef, return false, else return true
return int(glyph[0] != 0)


cdef hb_bool_t _font_h_extents_func(hb_font_t* font, void* font_data,
hb_font_extents_t *extents,
Expand Down Expand Up @@ -1013,6 +1024,7 @@ cdef class FontFuncs:
cdef object _glyph_v_origin_func
cdef object _glyph_name_func
cdef object _nominal_glyph_func
cdef object _variation_glyph_func
cdef object _font_h_extents_func
cdef object _font_v_extents_func

Expand Down Expand Up @@ -1083,6 +1095,18 @@ cdef class FontFuncs:
self._hb_ffuncs, _nominal_glyph_func, <void*>user_data, NULL)
self._nominal_glyph_func = func

def set_variation_glyph_func(self,
func: Callable[[
Font,
int, # unicode
int, # variation_selector
object, # user_data
], int], # gid
user_data: object = None) -> None:
hb_font_funcs_set_variation_glyph_func(
self._hb_ffuncs, _variation_glyph_func, <void*>user_data, NULL)
self._variation_glyph_func = func

def set_font_h_extents_func(self,
func: Callable[[
Font,
Expand Down
10 changes: 10 additions & 0 deletions src/uharfbuzz/charfbuzz.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ cdef extern from "hb.h":
hb_codepoint_t unicode,
hb_codepoint_t* glyph,
void* user_data)
ctypedef hb_bool_t (*hb_font_get_variation_glyph_func_t) (
hb_font_t *font, void *font_data,
hb_codepoint_t unicode,
hb_codepoint_t variation_selector,
hb_codepoint_t *glyph,
void *user_data)
ctypedef hb_position_t (*hb_font_get_glyph_advance_func_t) (
hb_font_t* font, void* font_data,
hb_codepoint_t glyph,
Expand Down Expand Up @@ -313,6 +319,10 @@ cdef extern from "hb.h":
hb_font_funcs_t* ffuncs,
hb_font_get_nominal_glyph_func_t func,
void* user_data, hb_destroy_func_t destroy)
void hb_font_funcs_set_variation_glyph_func(
hb_font_funcs_t *ffuncs,
hb_font_get_variation_glyph_func_t func,
void *user_data, hb_destroy_func_t destroy)
void hb_font_funcs_set_font_h_extents_func(
hb_font_funcs_t *ffuncs,
hb_font_get_font_h_extents_func_t func,
Expand Down
18 changes: 18 additions & 0 deletions tests/test_uharfbuzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,24 @@ def nominal_glyph_func(font, code_point, data):
infos = [g.codepoint for g in buf.glyph_infos]
assert infos == expected

def test_variation_glyph_func(self, blankfont):
string = "a\uFE00"
expected = [ord("a") + 0xFE00]
buf = hb.Buffer()
buf.add_str(string)
buf.guess_segment_properties()

def variation_glyph_func(font, unicode, variation_selector, data):
return unicode + variation_selector

funcs = hb.FontFuncs.create()
funcs.set_variation_glyph_func(variation_glyph_func)
blankfont.funcs = funcs

hb.shape(blankfont, buf)
infos = [g.codepoint for g in buf.glyph_infos]
assert infos == expected

def test_glyph_h_advance_func(self, blankfont):
string = "abcde"
expected = [456, 456, 456, 456, 456]
Expand Down

0 comments on commit e8bfad7

Please sign in to comment.