[draw] Move common CFF path building logic to draw_helper_t

This commit is contained in:
Ebrahim Byagowi 2020-02-26 15:14:41 +03:30
parent c400cb8863
commit 0ebf3a4e62
3 changed files with 93 additions and 104 deletions

View File

@ -39,5 +39,75 @@ struct hb_draw_funcs_t
hb_draw_close_path_func_t close_path;
};
struct draw_helper_t
{
void init (const hb_draw_funcs_t *funcs_, void *user_data_)
{
path_open = false;
funcs = funcs_;
user_data = user_data_;
path_start_x = 0;
path_start_y = 0;
path_last_x = 0;
path_last_y = 0;
}
void fini () { end_path (); }
void move_to (hb_position_t x, hb_position_t y)
{
if (path_open) end_path ();
path_last_x = path_start_x = x;
path_last_y = path_start_y = y;
}
void line_to (hb_position_t x, hb_position_t y)
{
if (!path_open) start_path ();
funcs->line_to (x, y, user_data);
path_last_x = x;
path_last_y = y;
}
void
cubic_to (hb_position_t x1, hb_position_t y1,
hb_position_t x2, hb_position_t y2,
hb_position_t x3, hb_position_t y3)
{
if (!path_open) start_path ();
funcs->cubic_to (x1, y1, x2, y2, x3, y3, user_data);
path_last_x = x3;
path_last_y = y3;
}
void end_path ()
{
if (path_open)
{
if ((path_start_x != path_last_x) || (path_start_y != path_last_y))
funcs->line_to (path_start_x, path_start_y, user_data);
funcs->close_path (user_data);
}
path_open = false;
}
protected:
void start_path ()
{
if (path_open) end_path ();
path_open = true;
funcs->move_to (path_start_x, path_start_y, user_data);
}
hb_position_t path_start_x;
hb_position_t path_start_y;
hb_position_t path_last_x;
hb_position_t path_last_y;
bool path_open;
const hb_draw_funcs_t *funcs;
void *user_data;
};
#endif /* HB_DRAW_HH */

View File

@ -349,50 +349,32 @@ struct cff1_path_param_t
const hb_draw_funcs_t *funcs_, void *user_data_,
point_t *delta_)
{
path_open = false;
cff = cff_;
font = font_;
draw_helper = draw_helper_t ();
draw_helper.init (funcs_, user_data_);
funcs = funcs_;
user_data = user_data_;
cff = cff_;
font = font_;
delta = delta_;
path_start_x = 0;
path_start_y = 0;
path_last_x = 0;
path_last_y = 0;
}
~cff1_path_param_t () { end_path (); }
void start_path ()
{
if (path_open) end_path ();
path_open = true;
funcs->move_to (font->em_scalef_x (path_start_x), font->em_scalef_y (path_start_y),
user_data);
}
~cff1_path_param_t () { draw_helper.fini (); }
void move_to (const point_t &p)
{
if (path_open) end_path ();
point_t point = p;
if (delta) point.move (*delta);
path_last_x = path_start_x = point.x.to_real ();
path_last_y = path_start_y = point.y.to_real ();
draw_helper.move_to (font->em_scalef_x (point.x.to_real ()), font->em_scalef_y (point.y.to_real ()));
}
void line_to (const point_t &p)
{
if (!path_open) start_path ();
point_t point = p;
if (delta) point.move (*delta);
funcs->line_to (font->em_scalef_x (point.x.to_real ()), font->em_scalef_y (point.y.to_real ()),
user_data);
path_last_x = point.x.to_real ();
path_last_y = point.y.to_real ();
draw_helper.line_to (font->em_scalef_x (point.x.to_real ()), font->em_scalef_y (point.y.to_real ()));
}
void cubic_to (const point_t &p1, const point_t &p2, const point_t &p3)
{
if (!path_open) start_path ();
point_t point1 = p1, point2 = p2, point3 = p3;
if (delta)
{
@ -400,35 +382,17 @@ struct cff1_path_param_t
point2.move (*delta);
point3.move (*delta);
}
funcs->cubic_to (font->em_scalef_x (point1.x.to_real ()), font->em_scalef_y (point1.y.to_real ()),
font->em_scalef_x (point2.x.to_real ()), font->em_scalef_y (point2.y.to_real ()),
font->em_scalef_x (point3.x.to_real ()), font->em_scalef_y (point3.y.to_real ()),
user_data);
path_last_x = point3.x.to_real ();
path_last_y = point3.y.to_real ();
draw_helper.cubic_to (font->em_scalef_x (point1.x.to_real ()), font->em_scalef_y (point1.y.to_real ()),
font->em_scalef_x (point2.x.to_real ()), font->em_scalef_y (point2.y.to_real ()),
font->em_scalef_x (point3.x.to_real ()), font->em_scalef_y (point3.y.to_real ()));
}
void end_path ()
{
if (path_open)
{
if ((path_start_x != path_last_x) || (path_start_y != path_last_y))
funcs->line_to (font->em_scalef_x (path_start_x), font->em_scalef_y (path_start_y), user_data);
funcs->close_path (user_data);
}
path_open = false;
}
void end_path () { draw_helper.end_path (); }
double path_start_x;
double path_start_y;
double path_last_x;
double path_last_y;
bool path_open;
hb_font_t *font;
const hb_draw_funcs_t *funcs;
void *user_data;
draw_helper_t draw_helper;
point_t *delta;
const OT::cff1::accelerator_t *cff;

View File

@ -145,75 +145,30 @@ bool OT::cff2::accelerator_t::get_extents (hb_font_t *font,
struct cff2_path_param_t
{
cff2_path_param_t (hb_font_t *font_, const hb_draw_funcs_t *funcs_, void *user_data_)
cff2_path_param_t (hb_font_t *font_, const hb_draw_funcs_t *funcs, void *user_data)
{
path_open = false;
draw_helper = draw_helper_t ();
draw_helper.init (funcs, user_data);
font = font_;
funcs = funcs_;
user_data = user_data_;
path_start_x = 0;
path_start_y = 0;
path_last_x = 0;
path_last_y = 0;
}
~cff2_path_param_t () { end_path (); }
void start_path ()
{
if (path_open) end_path ();
path_open = true;
funcs->move_to (font->em_scalef_x (path_start_x), font->em_scalef_y (path_start_y),
user_data);
}
~cff2_path_param_t () { draw_helper.fini (); }
void move_to (const point_t &p)
{
if (path_open) end_path ();
path_last_x = path_start_x = p.x.to_real ();
path_last_y = path_start_y = p.y.to_real ();
}
{ draw_helper.move_to (font->em_scalef_x (p.x.to_real ()), font->em_scalef_y (p.y.to_real ())); }
void line_to (const point_t &p)
{
if (!path_open) start_path ();
funcs->line_to (font->em_scalef_x (p.x.to_real ()), font->em_scalef_y (p.y.to_real ()),
user_data);
path_last_x = p.x.to_real ();
path_last_y = p.y.to_real ();
}
{ draw_helper.line_to (font->em_scalef_x (p.x.to_real ()), font->em_scalef_y (p.y.to_real ())); }
void cubic_to (const point_t &p1, const point_t &p2, const point_t &p3)
{
if (!path_open) start_path ();
funcs->cubic_to (font->em_scalef_x (p1.x.to_real ()), font->em_scalef_y (p1.y.to_real ()),
font->em_scalef_x (p2.x.to_real ()), font->em_scalef_y (p2.y.to_real ()),
font->em_scalef_x (p3.x.to_real ()), font->em_scalef_y (p3.y.to_real ()),
user_data);
path_last_x = p3.x.to_real ();
path_last_y = p3.y.to_real ();
draw_helper.cubic_to (font->em_scalef_x (p1.x.to_real ()), font->em_scalef_y (p1.y.to_real ()),
font->em_scalef_x (p2.x.to_real ()), font->em_scalef_y (p2.y.to_real ()),
font->em_scalef_x (p3.x.to_real ()), font->em_scalef_y (p3.y.to_real ()));
}
void end_path ()
{
if (path_open)
{
if ((path_start_x != path_last_x) || (path_start_y != path_last_y))
funcs->line_to (font->em_scalef_x (path_start_x), font->em_scalef_y (path_start_y), user_data);
funcs->close_path (user_data);
}
path_open = false;
}
double path_start_x;
double path_start_y;
double path_last_x;
double path_last_y;
bool path_open;
protected:
draw_helper_t draw_helper;
hb_font_t *font;
const hb_draw_funcs_t *funcs;
void *user_data;
};
struct cff2_path_procs_path_t : path_procs_t<cff2_path_procs_path_t, cff2_cs_interp_env_t, cff2_path_param_t>