Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing capsules for draw callbacks #130

Merged
merged 2 commits into from
Jul 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 71 additions & 10 deletions src/uharfbuzz/_harfbuzz.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ from .charfbuzz cimport *
from libc.stdlib cimport free, malloc, calloc
from libc.string cimport const_char
from collections import namedtuple
from cpython.pycapsule cimport PyCapsule_GetPointer, PyCapsule_IsValid
from typing import Callable, Dict, List, Sequence, Tuple, Union
from pathlib import Path

Expand Down Expand Up @@ -1033,9 +1034,21 @@ cdef class DrawFuncs:
object, # user_data
], None],
user_data: object = None) -> None:
self._move_to_func = func
cdef hb_draw_move_to_func_t func_p
cdef void *user_data_p
if PyCapsule_IsValid(func, NULL):
self._move_to_func = None
func_p = <hb_draw_move_to_func_t>PyCapsule_GetPointer(func, NULL)
justvanrossum marked this conversation as resolved.
Show resolved Hide resolved
if PyCapsule_IsValid(user_data, NULL):
user_data_p = <void*>PyCapsule_GetPointer(user_data, NULL)
else:
user_data_p = <void*>user_data
else:
self._move_to_func = func
func_p = _move_to_func
user_data_p = <void*>user_data
hb_draw_funcs_set_move_to_func(
self._hb_drawfuncs, _move_to_func, <void*>user_data, NULL)
self._hb_drawfuncs, func_p, user_data_p, NULL)

def set_line_to_func(self,
func: Callable[[
Expand All @@ -1044,9 +1057,21 @@ cdef class DrawFuncs:
object, # user_data
], None],
user_data: object = None) -> None:
self._line_to_func = func
cdef hb_draw_line_to_func_t func_p
cdef void *user_data_p
if PyCapsule_IsValid(func, NULL):
self._line_to_func = None
func_p = <hb_draw_line_to_func_t>PyCapsule_GetPointer(func, NULL)
if PyCapsule_IsValid(user_data, NULL):
user_data_p = <void*>PyCapsule_GetPointer(user_data, NULL)
else:
user_data_p = <void*>user_data
else:
self._line_to_func = func
func_p = _line_to_func
user_data_p = <void*>user_data
hb_draw_funcs_set_line_to_func(
self._hb_drawfuncs, _line_to_func, <void*>user_data, NULL)
self._hb_drawfuncs, func_p, user_data_p, NULL)

def set_cubic_to_func(self,
func: Callable[[
Expand All @@ -1059,9 +1084,21 @@ cdef class DrawFuncs:
object, # user_data
], None],
user_data: object = None) -> None:
self._cubic_to_func = func
cdef hb_draw_cubic_to_func_t func_p
cdef void *user_data_p
if PyCapsule_IsValid(func, NULL):
self._cubic_to_func = None
func_p = <hb_draw_cubic_to_func_t>PyCapsule_GetPointer(func, NULL)
if PyCapsule_IsValid(user_data, NULL):
user_data_p = <void*>PyCapsule_GetPointer(user_data, NULL)
else:
user_data_p = <void*>user_data
else:
self._cubic_to_func = func
func_p = _cubic_to_func
user_data_p = <void*>user_data
hb_draw_funcs_set_cubic_to_func(
self._hb_drawfuncs, _cubic_to_func, <void*>user_data, NULL)
self._hb_drawfuncs, func_p, user_data_p, NULL)

def set_quadratic_to_func(self,
func: Callable[[
Expand All @@ -1072,18 +1109,42 @@ cdef class DrawFuncs:
object, # user_data
], None],
user_data: object = None) -> None:
self._quadratic_to_func = func
cdef hb_draw_quadratic_to_func_t func_p
cdef void *user_data_p
if PyCapsule_IsValid(func, NULL):
self._quadratic_to_func = None
func_p = <hb_draw_quadratic_to_func_t>PyCapsule_GetPointer(func, NULL)
if PyCapsule_IsValid(user_data, NULL):
user_data_p = <void*>PyCapsule_GetPointer(user_data, NULL)
else:
user_data_p = <void*>user_data
else:
self._quadratic_to_func = func
func_p = _quadratic_to_func
user_data_p = <void*>user_data
hb_draw_funcs_set_quadratic_to_func(
self._hb_drawfuncs, _quadratic_to_func, <void*>user_data, NULL)
self._hb_drawfuncs, func_p, user_data_p, NULL)

def set_close_path_func(self,
func: Callable[[
object
], None],
user_data: object = None) -> None:
self._close_path_func = func
cdef hb_draw_close_path_func_t func_p
cdef void *user_data_p
if PyCapsule_IsValid(func, NULL):
self._close_path_func = None
func_p = <hb_draw_close_path_func_t>PyCapsule_GetPointer(func, NULL)
if PyCapsule_IsValid(user_data, NULL):
user_data_p = <void*>PyCapsule_GetPointer(user_data, NULL)
else:
user_data_p = <void*>user_data
else:
self._close_path_func = func
func_p = _close_path_func
user_data_p = <void*>user_data
hb_draw_funcs_set_close_path_func(
self._hb_drawfuncs, _close_path_func, <void*>user_data, NULL)
self._hb_drawfuncs, func_p, user_data_p, NULL)

cdef class HBObject:
cdef hb_object_t* _hb_obj_list
Expand Down