[draw] Optimize set_func functions

This commit is contained in:
Behdad Esfahbod 2022-11-19 15:06:23 -07:00
parent 114167a933
commit 976bb26cc1
2 changed files with 63 additions and 27 deletions

View File

@ -80,6 +80,56 @@ hb_draw_close_path_nil (hb_draw_funcs_t *dfuncs HB_UNUSED, void *draw_data HB_UN
void *user_data HB_UNUSED) {}
static bool
_hb_draw_funcs_set_preamble (hb_draw_funcs_t *dfuncs,
bool func_is_null,
void **user_data,
hb_destroy_func_t *destroy)
{
if (hb_object_is_immutable (dfuncs))
{
if (*destroy)
(*destroy) (*user_data);
return false;
}
if (func_is_null)
{
if (*destroy)
(*destroy) (*user_data);
*destroy = nullptr;
*user_data = nullptr;
}
return true;
}
static bool
_hb_draw_funcs_set_middle (hb_draw_funcs_t *dfuncs,
void *user_data,
hb_destroy_func_t destroy)
{
if (user_data && !dfuncs->user_data)
{
dfuncs->user_data = (decltype (dfuncs->user_data)) hb_calloc (1, sizeof (*dfuncs->user_data));
if (unlikely (!dfuncs->user_data))
goto fail;
}
if (destroy && !dfuncs->destroy)
{
dfuncs->destroy = (decltype (dfuncs->destroy)) hb_calloc (1, sizeof (*dfuncs->destroy));
if (unlikely (!dfuncs->destroy))
goto fail;
}
return true;
fail:
if (destroy)
(destroy) (user_data);
return false;
}
#define HB_DRAW_FUNC_IMPLEMENT(name) \
\
void \
@ -88,24 +138,14 @@ hb_draw_funcs_set_##name##_func (hb_draw_funcs_t *dfuncs, \
void *user_data, \
hb_destroy_func_t destroy) \
{ \
if (hb_object_is_immutable (dfuncs)) \
return; \
if (!_hb_draw_funcs_set_preamble (dfuncs, !func, &user_data, &destroy))\
return; \
\
if (dfuncs->destroy && dfuncs->destroy->name) \
dfuncs->destroy->name (!dfuncs->user_data ? nullptr : dfuncs->user_data->name); \
\
if (user_data && !dfuncs->user_data) \
{ \
dfuncs->user_data = (decltype (dfuncs->user_data)) hb_calloc (1, sizeof (*dfuncs->user_data)); \
if (unlikely (!dfuncs->user_data)) \
goto fail; \
} \
if (destroy && !dfuncs->destroy) \
{ \
dfuncs->destroy = (decltype (dfuncs->destroy)) hb_calloc (1, sizeof (*dfuncs->destroy)); \
if (unlikely (!dfuncs->destroy)) \
goto fail; \
} \
if (!_hb_draw_funcs_set_middle (dfuncs, user_data, destroy)) \
return; \
\
if (func) { \
dfuncs->func.name = func; \
@ -121,10 +161,6 @@ hb_draw_funcs_set_##name##_func (hb_draw_funcs_t *dfuncs, \
dfuncs->destroy->name = nullptr; \
} \
return; \
\
fail: \
if (destroy) \
destroy (user_data); \
}
HB_DRAW_FUNCS_IMPLEMENT_CALLBACKS

View File

@ -823,10 +823,10 @@ hb_font_funcs_is_immutable (hb_font_funcs_t *ffuncs)
static bool
set_func_preamble (hb_font_funcs_t *ffuncs,
bool func_is_null,
void **user_data,
hb_destroy_func_t *destroy)
_hb_font_funcs_set_preamble (hb_font_funcs_t *ffuncs,
bool func_is_null,
void **user_data,
hb_destroy_func_t *destroy)
{
if (hb_object_is_immutable (ffuncs))
{
@ -847,9 +847,9 @@ set_func_preamble (hb_font_funcs_t *ffuncs,
}
static bool
set_func_middle (hb_font_funcs_t *ffuncs,
void *user_data,
hb_destroy_func_t destroy)
_hb_font_funcs_set_middle (hb_font_funcs_t *ffuncs,
void *user_data,
hb_destroy_func_t destroy)
{
if (user_data && !ffuncs->user_data)
{
@ -880,13 +880,13 @@ hb_font_funcs_set_##name##_func (hb_font_funcs_t *ffuncs, \
void *user_data, \
hb_destroy_func_t destroy) \
{ \
if (!set_func_preamble (ffuncs, !func, &user_data, &destroy)) \
if (!_hb_font_funcs_set_preamble (ffuncs, !func, &user_data, &destroy))\
return; \
\
if (ffuncs->destroy && ffuncs->destroy->name) \
ffuncs->destroy->name (!ffuncs->user_data ? nullptr : ffuncs->user_data->name); \
\
if (!set_func_middle (ffuncs, user_data, destroy)) \
if (!_hb_font_funcs_set_middle (ffuncs, user_data, destroy)) \
return; \
\
if (func) { \