[draw-state] Add type and use in draw-helper

This commit is contained in:
Behdad Esfahbod 2022-02-03 12:43:25 -06:00
parent 5610fa1da0
commit fc78592e67
3 changed files with 55 additions and 36 deletions

View File

@ -130,6 +130,16 @@ typedef union _hb_var_int_t {
int8_t i8[4];
} hb_var_int_t;
typedef union _hb_var_num_t {
float f;
uint32_t u32;
int32_t i32;
uint16_t u16[2];
int16_t i16[2];
uint8_t u8[4];
int8_t i8[4];
} hb_var_num_t;
/* hb_tag_t */

View File

@ -33,6 +33,25 @@
HB_BEGIN_DECLS
typedef struct hb_draw_state_t {
hb_bool_t path_open;
float path_start_x;
float path_start_y;
float current_x;
float current_y;
/*< private >*/
hb_var_num_t reserved1;
hb_var_num_t reserved2;
hb_var_num_t reserved3;
} hb_draw_state_t;
#define HB_DRAW_STATE_DEFAULT {0, 0.f, 0.f, 0.f, 0.f, {0.}, {0.}, {0.}}
/**
* hb_draw_funcs_t:
*

View File

@ -100,28 +100,24 @@ DECLARE_NULL_INSTANCE (hb_draw_funcs_t);
struct draw_helper_t
{
draw_helper_t (hb_draw_funcs_t *funcs_, void *draw_data_)
{
funcs = funcs_;
draw_data = draw_data_;
path_open = false;
path_start_x = current_x = path_start_y = current_y = 0;
}
: funcs {funcs_}, draw_data {draw_data_}, st HB_DRAW_STATE_DEFAULT {}
~draw_helper_t () { end_path (); }
void move_to (float x, float y)
{
if (path_open) end_path ();
current_x = path_start_x = x;
current_y = path_start_y = y;
if (st.path_open) end_path ();
st.current_x = st.path_start_x = x;
st.current_y = st.path_start_y = y;
}
void line_to (float x, float y)
{
if (equal_to_current (x, y)) return;
if (!path_open) start_path ();
if (!st.path_open) start_path ();
funcs->line_to (draw_data, x, y);
current_x = x;
current_y = y;
st.current_x = x;
st.current_y = y;
}
void
@ -130,18 +126,18 @@ struct draw_helper_t
{
if (equal_to_current (control_x, control_y) && equal_to_current (to_x, to_y))
return;
if (!path_open) start_path ();
if (!st.path_open) start_path ();
if (funcs->quadratic_to_is_set ())
funcs->quadratic_to (draw_data, control_x, control_y, to_x, to_y);
else
funcs->cubic_to (draw_data,
(current_x + 2.f * control_x) / 3.f,
(current_y + 2.f * control_y) / 3.f,
(st.current_x + 2.f * control_x) / 3.f,
(st.current_y + 2.f * control_y) / 3.f,
(to_x + 2.f * control_x) / 3.f,
(to_y + 2.f * control_y) / 3.f,
to_x, to_y);
current_x = to_x;
current_y = to_y;
st.current_x = to_x;
st.current_y = to_y;
}
void
@ -153,44 +149,38 @@ struct draw_helper_t
equal_to_current (control2_x, control2_y) &&
equal_to_current (to_x, to_y))
return;
if (!path_open) start_path ();
if (!st.path_open) start_path ();
funcs->cubic_to (draw_data, control1_x, control1_y, control2_x, control2_y, to_x, to_y);
current_x = to_x;
current_y = to_y;
st.current_x = to_x;
st.current_y = to_y;
}
void end_path ()
{
if (path_open)
if (st.path_open)
{
if ((path_start_x != current_x) || (path_start_y != current_y))
funcs->line_to (draw_data, path_start_x, path_start_y);
if ((st.path_start_x != st.current_x) || (st.path_start_y != st.current_y))
funcs->line_to (draw_data, st.path_start_x, st.path_start_y);
funcs->close_path (draw_data);
}
path_open = false;
path_start_x = current_x = path_start_y = current_y = 0;
st.path_open = false;
st.path_start_x = st.current_x = st.path_start_y = st.current_y = 0;
}
protected:
bool equal_to_current (float x, float y)
{ return current_x == x && current_y == y; }
{ return st.current_x == x && st.current_y == y; }
void start_path ()
{
if (path_open) end_path ();
path_open = true;
funcs->move_to (draw_data, path_start_x, path_start_y);
if (st.path_open) end_path ();
st.path_open = true;
funcs->move_to (draw_data, st.path_start_x, st.path_start_y);
}
float path_start_x;
float path_start_y;
float current_x;
float current_y;
bool path_open;
hb_draw_funcs_t *funcs;
void *draw_data;
hb_draw_state_t st;
};
#endif /* HB_DRAW_HH */