Skip to content

Commit

Permalink
add callback support for vertical advance and vertical origin (#26)
Browse files Browse the repository at this point in the history
* add callback support for vertical advance and vertical origin

* added tests for callbacks

* whitespace: fixed alignment
  • Loading branch information
justvanrossum authored Feb 3, 2020
1 parent 4756fbc commit 5f9b224
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/uharfbuzz/_harfbuzz.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,29 @@ cdef hb_position_t _glyph_h_advance_func(hb_font_t* font, void* font_data,
py_font, glyph, <object>user_data)


cdef hb_position_t _glyph_v_advance_func(hb_font_t* font, void* font_data,
hb_codepoint_t glyph,
void* user_data):
cdef Font py_font = <Font>font_data
return (<FontFuncs>py_font.funcs)._glyph_v_advance_func(
py_font, glyph, <object>user_data)


cdef hb_bool_t _glyph_v_origin_func(hb_font_t* font, void* font_data,
hb_codepoint_t glyph,
hb_position_t* x, hb_position_t* y,
void* user_data):
cdef Font py_font = <Font>font_data
cdef hb_bool_t success
cdef hb_position_t px
cdef hb_position_t py
success, px, py = (<FontFuncs>py_font.funcs)._glyph_v_origin_func(
py_font, glyph, <object>user_data)
x[0] = px
y[0] = py
return success


cdef hb_bool_t _glyph_name_func(hb_font_t *font, void *font_data,
hb_codepoint_t glyph,
char *name, unsigned int size,
Expand All @@ -388,6 +411,8 @@ cdef hb_bool_t _nominal_glyph_func(hb_font_t* font, void* font_data,
cdef class FontFuncs:
cdef hb_font_funcs_t* _hb_ffuncs
cdef object _glyph_h_advance_func
cdef object _glyph_v_advance_func
cdef object _glyph_v_origin_func
cdef object _glyph_name_func
cdef object _nominal_glyph_func

Expand Down Expand Up @@ -415,6 +440,28 @@ cdef class FontFuncs:
self._hb_ffuncs, _glyph_h_advance_func, <void*>user_data, NULL)
self._glyph_h_advance_func = func

def set_glyph_v_advance_func(self,
func: Callable[[
Font,
int, # gid
object, # user_data
], int], # v_advance
user_data: object) -> None:
hb_font_funcs_set_glyph_v_advance_func(
self._hb_ffuncs, _glyph_v_advance_func, <void*>user_data, NULL)
self._glyph_v_advance_func = func

def set_glyph_v_origin_func(self,
func: Callable[[
Font,
int, # gid
object, # user_data
], (int, int, int)], # success, v_origin_x, v_origin_y
user_data: object) -> None:
hb_font_funcs_set_glyph_v_origin_func(
self._hb_ffuncs, _glyph_v_origin_func, <void*>user_data, NULL)
self._glyph_v_origin_func = func

def set_glyph_name_func(self,
func: Callable[[
Font,
Expand Down
15 changes: 15 additions & 0 deletions src/uharfbuzz/charfbuzz.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ cdef extern from "hb.h":
hb_codepoint_t glyph,
void* user_data)
ctypedef hb_font_get_glyph_advance_func_t hb_font_get_glyph_h_advance_func_t;
ctypedef hb_font_get_glyph_advance_func_t hb_font_get_glyph_v_advance_func_t;
ctypedef hb_bool_t (*hb_font_get_glyph_origin_func_t) (
hb_font_t* font, void* font_data,
hb_codepoint_t glyph,
hb_position_t* x, hb_position_t* y,
void* user_data)
ctypedef hb_font_get_glyph_origin_func_t hb_font_get_glyph_v_origin_func_t;
ctypedef hb_bool_t (*hb_font_get_glyph_name_func_t) (
hb_font_t *font, void *font_data,
hb_codepoint_t glyph,
Expand All @@ -164,6 +171,14 @@ cdef extern from "hb.h":
hb_font_funcs_t* ffuncs,
hb_font_get_glyph_h_advance_func_t func,
void* user_data, hb_destroy_func_t destroy)
void hb_font_funcs_set_glyph_v_advance_func(
hb_font_funcs_t* ffuncs,
hb_font_get_glyph_v_advance_func_t func,
void* user_data, hb_destroy_func_t destroy)
void hb_font_funcs_set_glyph_v_origin_func(
hb_font_funcs_t* ffuncs,
hb_font_get_glyph_v_origin_func_t func,
void* user_data, hb_destroy_func_t destroy)
void hb_font_funcs_set_glyph_name_func (
hb_font_funcs_t* ffuncs,
hb_font_get_glyph_name_func_t func,
Expand Down
61 changes: 61 additions & 0 deletions tests/test_uharfbuzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,64 @@ def test_gid_and_cluster_no_features(self, blankfont, string, expected):
hb.shape(blankfont, buf)
infos = [(g.codepoint, g.cluster) for g in buf.glyph_infos]
assert infos == expected


class TestCallbacks:
def test_nominal_glyph_func(self, blankfont):
string = "abcde"
expected = [97, 98, 99, 100, 101]
buf = hb.Buffer()
buf.add_str(string)
buf.guess_segment_properties()

def nominal_glyph_func(font, code_point, data):
return code_point

funcs = hb.FontFuncs.create()
funcs.set_nominal_glyph_func(nominal_glyph_func, None)
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]
buf = hb.Buffer()
buf.add_str(string)
buf.guess_segment_properties()

def h_advance_func(font, gid, data):
return 456

funcs = hb.FontFuncs.create()
funcs.set_glyph_h_advance_func(h_advance_func, None)
blankfont.funcs = funcs

hb.shape(blankfont, buf)
infos = [pos.x_advance for pos in buf.glyph_positions]
assert infos == expected

def test_glyph_v_metrics_funcs(self, blankfont):
string = "abcde"
expected = [(456, -345, -567)] * 5
buf = hb.Buffer()
buf.add_str(string)
buf.guess_segment_properties()
buf.direction = "TTB"

def v_advance_func(font, gid, data):
return 456

def v_origin_func(font, gid, data):
return (True, 345, 567)

funcs = hb.FontFuncs.create()
funcs.set_glyph_v_advance_func(v_advance_func, None)
funcs.set_glyph_v_origin_func(v_origin_func, None)
blankfont.funcs = funcs

hb.shape(blankfont, buf)
infos = [(pos.y_advance, pos.x_offset, pos.y_offset) for pos in buf.glyph_positions]
assert infos == expected

0 comments on commit 5f9b224

Please sign in to comment.