2020-01-13 17:01:18 +01:00
|
|
|
/*
|
|
|
|
* Copyright © 2020 Ebrahim Byagowi
|
|
|
|
*
|
|
|
|
* This is part of HarfBuzz, a text shaping library.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, without written agreement and without
|
|
|
|
* license or royalty fees, to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose, provided that the
|
|
|
|
* above copyright notice and the following two paragraphs appear in
|
|
|
|
* all copies of this software.
|
|
|
|
*
|
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
|
|
|
|
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
|
|
|
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
|
|
|
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
|
|
|
|
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
*/
|
|
|
|
|
2020-01-28 10:04:47 +01:00
|
|
|
#ifndef HB_DRAW_HH
|
|
|
|
#define HB_DRAW_HH
|
2020-01-13 17:01:18 +01:00
|
|
|
|
|
|
|
#include "hb.hh"
|
|
|
|
|
2022-02-03 04:42:48 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* hb_draw_funcs_t
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define HB_DRAW_FUNCS_IMPLEMENT_CALLBACKS \
|
|
|
|
HB_DRAW_FUNC_IMPLEMENT (move_to) \
|
|
|
|
HB_DRAW_FUNC_IMPLEMENT (line_to) \
|
|
|
|
HB_DRAW_FUNC_IMPLEMENT (quadratic_to) \
|
|
|
|
HB_DRAW_FUNC_IMPLEMENT (cubic_to) \
|
|
|
|
HB_DRAW_FUNC_IMPLEMENT (close_path) \
|
|
|
|
/* ^--- Add new callbacks here */
|
|
|
|
|
2020-01-28 10:04:47 +01:00
|
|
|
struct hb_draw_funcs_t
|
2020-01-13 17:01:18 +01:00
|
|
|
{
|
|
|
|
hb_object_header_t header;
|
|
|
|
|
2022-02-03 04:42:48 +01:00
|
|
|
struct {
|
|
|
|
#define HB_DRAW_FUNC_IMPLEMENT(name) hb_draw_##name##_func_t name;
|
|
|
|
HB_DRAW_FUNCS_IMPLEMENT_CALLBACKS
|
|
|
|
#undef HB_DRAW_FUNC_IMPLEMENT
|
|
|
|
} func;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
#define HB_DRAW_FUNC_IMPLEMENT(name) void *name;
|
|
|
|
HB_DRAW_FUNCS_IMPLEMENT_CALLBACKS
|
|
|
|
#undef HB_DRAW_FUNC_IMPLEMENT
|
|
|
|
} user_data;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
#define HB_DRAW_FUNC_IMPLEMENT(name) hb_destroy_func_t name;
|
|
|
|
HB_DRAW_FUNCS_IMPLEMENT_CALLBACKS
|
|
|
|
#undef HB_DRAW_FUNC_IMPLEMENT
|
|
|
|
} destroy;
|
|
|
|
|
2022-02-03 21:10:40 +01:00
|
|
|
void emit_move_to (void *draw_data, hb_draw_state_t &st,
|
2022-02-03 20:00:39 +01:00
|
|
|
float to_x, float to_y)
|
2022-02-03 21:10:40 +01:00
|
|
|
{ func.move_to (this, draw_data, &st,
|
2022-02-03 04:42:48 +01:00
|
|
|
to_x, to_y,
|
|
|
|
user_data.move_to); }
|
2022-02-03 21:10:40 +01:00
|
|
|
void emit_line_to (void *draw_data, hb_draw_state_t &st,
|
2022-02-03 20:00:39 +01:00
|
|
|
float to_x, float to_y)
|
2022-02-03 21:10:40 +01:00
|
|
|
{ func.line_to (this, draw_data, &st,
|
2022-02-03 04:42:48 +01:00
|
|
|
to_x, to_y,
|
|
|
|
user_data.line_to); }
|
2022-02-03 21:10:40 +01:00
|
|
|
void emit_quadratic_to (void *draw_data, hb_draw_state_t &st,
|
2022-02-03 20:00:39 +01:00
|
|
|
float control_x, float control_y,
|
|
|
|
float to_x, float to_y)
|
2022-02-03 21:10:40 +01:00
|
|
|
{ func.quadratic_to (this, draw_data, &st,
|
2022-02-03 04:42:48 +01:00
|
|
|
control_x, control_y,
|
|
|
|
to_x, to_y,
|
|
|
|
user_data.quadratic_to); }
|
2022-02-03 21:10:40 +01:00
|
|
|
void emit_cubic_to (void *draw_data, hb_draw_state_t &st,
|
2022-02-03 20:00:39 +01:00
|
|
|
float control1_x, float control1_y,
|
|
|
|
float control2_x, float control2_y,
|
|
|
|
float to_x, float to_y)
|
2022-02-03 21:10:40 +01:00
|
|
|
{ func.cubic_to (this, draw_data, &st,
|
2022-02-03 04:42:48 +01:00
|
|
|
control1_x, control1_y,
|
|
|
|
control2_x, control2_y,
|
|
|
|
to_x, to_y,
|
|
|
|
user_data.cubic_to); }
|
2022-02-03 21:10:40 +01:00
|
|
|
void emit_close_path (void *draw_data, hb_draw_state_t &st)
|
|
|
|
{ func.close_path (this, draw_data, &st,
|
2022-02-03 04:42:48 +01:00
|
|
|
user_data.close_path); }
|
|
|
|
|
2020-01-13 17:01:18 +01:00
|
|
|
|
2022-02-03 20:58:36 +01:00
|
|
|
void move_to (void *draw_data, hb_draw_state_t &st,
|
|
|
|
float to_x, float to_y)
|
2020-02-26 12:44:41 +01:00
|
|
|
{
|
2022-02-03 20:58:36 +01:00
|
|
|
if (st.path_open) close_path (draw_data, st);
|
|
|
|
st.current_x = st.path_start_x = to_x;
|
|
|
|
st.current_y = st.path_start_y = to_y;
|
2020-02-26 12:44:41 +01:00
|
|
|
}
|
|
|
|
|
2022-02-03 20:58:36 +01:00
|
|
|
void line_to (void *draw_data, hb_draw_state_t &st,
|
|
|
|
float to_x, float to_y)
|
2020-02-26 12:44:41 +01:00
|
|
|
{
|
2022-02-03 20:58:36 +01:00
|
|
|
if (!st.path_open) start_path (draw_data, st);
|
2022-02-03 21:10:40 +01:00
|
|
|
emit_line_to (draw_data, st, to_x, to_y);
|
2022-02-03 20:58:36 +01:00
|
|
|
st.current_x = to_x;
|
|
|
|
st.current_y = to_y;
|
2020-02-26 13:10:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-02-03 20:58:36 +01:00
|
|
|
quadratic_to (void *draw_data, hb_draw_state_t &st,
|
|
|
|
float control_x, float control_y,
|
2022-02-02 23:08:40 +01:00
|
|
|
float to_x, float to_y)
|
2020-02-26 13:10:40 +01:00
|
|
|
{
|
2022-02-03 20:58:36 +01:00
|
|
|
if (!st.path_open) start_path (draw_data, st);
|
2022-02-03 21:42:56 +01:00
|
|
|
emit_quadratic_to (draw_data, st, control_x, control_y, to_x, to_y);
|
2022-02-03 19:43:25 +01:00
|
|
|
st.current_x = to_x;
|
|
|
|
st.current_y = to_y;
|
2020-02-26 12:44:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-02-03 20:58:36 +01:00
|
|
|
cubic_to (void *draw_data, hb_draw_state_t &st,
|
|
|
|
float control1_x, float control1_y,
|
2022-02-02 23:08:40 +01:00
|
|
|
float control2_x, float control2_y,
|
|
|
|
float to_x, float to_y)
|
2020-02-26 12:44:41 +01:00
|
|
|
{
|
2022-02-03 20:58:36 +01:00
|
|
|
if (!st.path_open) start_path (draw_data, st);
|
2022-02-03 21:10:40 +01:00
|
|
|
emit_cubic_to (draw_data, st, control1_x, control1_y, control2_x, control2_y, to_x, to_y);
|
2022-02-03 19:43:25 +01:00
|
|
|
st.current_x = to_x;
|
|
|
|
st.current_y = to_y;
|
2020-02-26 12:44:41 +01:00
|
|
|
}
|
|
|
|
|
2022-02-03 20:58:36 +01:00
|
|
|
void
|
|
|
|
close_path (void *draw_data, hb_draw_state_t &st)
|
2020-02-26 12:44:41 +01:00
|
|
|
{
|
2022-02-03 19:43:25 +01:00
|
|
|
if (st.path_open)
|
2020-02-26 12:44:41 +01:00
|
|
|
{
|
2022-02-03 19:43:25 +01:00
|
|
|
if ((st.path_start_x != st.current_x) || (st.path_start_y != st.current_y))
|
2022-02-03 21:10:40 +01:00
|
|
|
emit_line_to (draw_data, st, st.path_start_x, st.path_start_y);
|
|
|
|
emit_close_path (draw_data, st);
|
2020-02-26 12:44:41 +01:00
|
|
|
}
|
2022-02-03 19:43:25 +01:00
|
|
|
st.path_open = false;
|
|
|
|
st.path_start_x = st.current_x = st.path_start_y = st.current_y = 0;
|
2020-02-26 12:44:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-02-26 13:28:11 +01:00
|
|
|
|
2022-02-03 20:58:36 +01:00
|
|
|
void start_path (void *draw_data, hb_draw_state_t &st)
|
2020-02-26 12:44:41 +01:00
|
|
|
{
|
2022-02-03 19:54:32 +01:00
|
|
|
assert (!st.path_open);
|
2022-02-03 19:43:25 +01:00
|
|
|
st.path_open = true;
|
2022-02-03 21:10:40 +01:00
|
|
|
emit_move_to (draw_data, st, st.path_start_x, st.path_start_y);
|
2020-02-26 12:44:41 +01:00
|
|
|
}
|
2022-02-03 20:58:36 +01:00
|
|
|
};
|
|
|
|
DECLARE_NULL_INSTANCE (hb_draw_funcs_t);
|
|
|
|
|
2022-02-05 01:40:44 +01:00
|
|
|
struct hb_draw_session_t
|
2022-02-03 20:58:36 +01:00
|
|
|
{
|
2022-02-08 00:53:38 +01:00
|
|
|
hb_draw_session_t (hb_draw_funcs_t *funcs_, void *draw_data_, float slant_ = 0.f)
|
|
|
|
: slant {slant_}, not_slanted {slant == 0.f},
|
|
|
|
funcs {funcs_}, draw_data {draw_data_}, st HB_DRAW_STATE_DEFAULT
|
|
|
|
{}
|
2022-02-03 20:58:36 +01:00
|
|
|
|
2022-02-05 01:40:44 +01:00
|
|
|
~hb_draw_session_t () { close_path (); }
|
2022-02-03 20:58:36 +01:00
|
|
|
|
|
|
|
void move_to (float to_x, float to_y)
|
|
|
|
{
|
2022-02-08 00:53:38 +01:00
|
|
|
if (likely (not_slanted))
|
|
|
|
funcs->move_to (draw_data, st,
|
|
|
|
to_x, to_y);
|
|
|
|
else
|
|
|
|
funcs->move_to (draw_data, st,
|
|
|
|
to_x + to_y * slant, to_y);
|
2022-02-03 20:58:36 +01:00
|
|
|
}
|
|
|
|
void line_to (float to_x, float to_y)
|
|
|
|
{
|
2022-02-08 00:53:38 +01:00
|
|
|
if (likely (not_slanted))
|
|
|
|
funcs->line_to (draw_data, st,
|
|
|
|
to_x, to_y);
|
|
|
|
else
|
|
|
|
funcs->line_to (draw_data, st,
|
|
|
|
to_x + to_y * slant, to_y);
|
2022-02-03 20:58:36 +01:00
|
|
|
}
|
|
|
|
void
|
|
|
|
quadratic_to (float control_x, float control_y,
|
|
|
|
float to_x, float to_y)
|
|
|
|
{
|
2022-02-08 00:53:38 +01:00
|
|
|
if (likely (not_slanted))
|
|
|
|
funcs->quadratic_to (draw_data, st,
|
|
|
|
control_x, control_y,
|
|
|
|
to_x, to_y);
|
|
|
|
else
|
|
|
|
funcs->quadratic_to (draw_data, st,
|
|
|
|
control_x + control_y * slant, control_y,
|
|
|
|
to_x + to_y * slant, to_y);
|
2022-02-03 20:58:36 +01:00
|
|
|
}
|
|
|
|
void
|
|
|
|
cubic_to (float control1_x, float control1_y,
|
|
|
|
float control2_x, float control2_y,
|
|
|
|
float to_x, float to_y)
|
|
|
|
{
|
2022-02-08 00:53:38 +01:00
|
|
|
if (likely (not_slanted))
|
|
|
|
funcs->cubic_to (draw_data, st,
|
|
|
|
control1_x, control1_y,
|
|
|
|
control2_x, control2_y,
|
|
|
|
to_x, to_y);
|
|
|
|
else
|
|
|
|
funcs->cubic_to (draw_data, st,
|
|
|
|
control1_x + control1_y * slant, control1_y,
|
|
|
|
control2_x + control2_y * slant, control2_y,
|
|
|
|
to_x + to_y * slant, to_y);
|
2022-02-03 20:58:36 +01:00
|
|
|
}
|
|
|
|
void close_path ()
|
|
|
|
{
|
|
|
|
funcs->close_path (draw_data, st);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2022-02-08 00:53:38 +01:00
|
|
|
float slant;
|
|
|
|
bool not_slanted;
|
2022-02-03 04:42:48 +01:00
|
|
|
hb_draw_funcs_t *funcs;
|
|
|
|
void *draw_data;
|
2022-02-03 19:43:25 +01:00
|
|
|
hb_draw_state_t st;
|
2020-02-26 12:44:41 +01:00
|
|
|
};
|
2020-01-13 17:01:18 +01:00
|
|
|
|
2020-01-28 10:04:47 +01:00
|
|
|
#endif /* HB_DRAW_HH */
|