[draw] Fix draw signatures

This commit is contained in:
Behdad Esfahbod committed 2022-02-13 13:21:14 -06:00
1 parent 08e1096609
commit 2bed4f46fb
8 files changed
+448 -345

No files matched your search

+1 -1
View File
@@ -45,7 +45,7 @@
void
hb_font_draw_glyph (hb_font_t *font,
hb_codepoint_t glyph,
const hb_draw_funcs_t *funcs,
hb_draw_funcs_t *funcs,
void *user_data)
{
if (unlikely (funcs == &Null (hb_draw_funcs_t) ||
+79 -62
View File
@@ -28,6 +28,63 @@
#include "hb-draw.hh"
static void
hb_draw_move_to_nil (hb_draw_funcs_t *dfuncs HB_UNUSED, void *draw_data HB_UNUSED,
float to_x HB_UNUSED, float to_y HB_UNUSED,
void *user_data HB_UNUSED) {}
static void
hb_draw_line_to_nil (hb_draw_funcs_t *dfuncs HB_UNUSED, void *draw_data HB_UNUSED,
float to_x HB_UNUSED, float to_y HB_UNUSED,
void *user_data HB_UNUSED) {}
static void
hb_draw_quadratic_to_nil (hb_draw_funcs_t *dfuncs HB_UNUSED, void *draw_data HB_UNUSED,
float control_x HB_UNUSED, float control_y HB_UNUSED,
float to_x HB_UNUSED, float to_y HB_UNUSED,
void *user_data HB_UNUSED) {}
static void
hb_draw_cubic_to_nil (hb_draw_funcs_t *dfuncs HB_UNUSED, void *draw_data HB_UNUSED,
float control1_x HB_UNUSED, float control1_y HB_UNUSED,
float control2_x HB_UNUSED, float control2_y HB_UNUSED,
float to_x HB_UNUSED, float to_y HB_UNUSED,
void *user_data HB_UNUSED) {}
static void
hb_draw_close_path_nil (hb_draw_funcs_t *dfuncs HB_UNUSED, void *draw_data HB_UNUSED,
void *user_data HB_UNUSED) {}
#define HB_DRAW_FUNC_IMPLEMENT(name) \
\
void \
hb_draw_funcs_set_##name##_func (hb_draw_funcs_t *dfuncs, \
hb_draw_##name##_func_t func, \
void *user_data, \
hb_destroy_func_t destroy) \
{ \
if (hb_object_is_immutable (dfuncs)) \
return; \
\
if (dfuncs->destroy.name) \
dfuncs->destroy.name (dfuncs->user_data.name); \
\
if (func) { \
dfuncs->func.name = func; \
dfuncs->user_data.name = user_data; \
dfuncs->destroy.name = destroy; \
} else { \
dfuncs->func.name = hb_draw_##name##_nil; \
dfuncs->user_data.name = nullptr; \
dfuncs->destroy.name = nullptr; \
} \
}
HB_DRAW_FUNCS_IMPLEMENT_CALLBACKS
#undef HB_DRAW_FUNC_IMPLEMENT
/**
* hb_draw_funcs_set_move_to_func:
* @funcs: draw functions object
@@ -37,13 +94,6 @@
*
* Since: REPLACEME
**/
void
hb_draw_funcs_set_move_to_func (hb_draw_funcs_t *funcs,
hb_draw_move_to_func_t move_to)
{
if (unlikely (hb_object_is_immutable (funcs))) return;
funcs->move_to = move_to;
}
/**
* hb_draw_funcs_set_line_to_func:
@@ -54,13 +104,6 @@ hb_draw_funcs_set_move_to_func (hb_draw_funcs_t *funcs,
*
* Since: REPLACEME
**/
void
hb_draw_funcs_set_line_to_func (hb_draw_funcs_t *funcs,
hb_draw_line_to_func_t line_to)
{
if (unlikely (hb_object_is_immutable (funcs))) return;
funcs->line_to = line_to;
}
/**
* hb_draw_funcs_set_quadratic_to_func:
@@ -71,14 +114,6 @@ hb_draw_funcs_set_line_to_func (hb_draw_funcs_t *funcs,
*
* Since: REPLACEME
**/
void
hb_draw_funcs_set_quadratic_to_func (hb_draw_funcs_t *funcs,
hb_draw_quadratic_to_func_t quadratic_to)
{
if (unlikely (hb_object_is_immutable (funcs))) return;
funcs->quadratic_to = quadratic_to;
funcs->is_quadratic_to_set = true;
}
/**
* hb_draw_funcs_set_cubic_to_func:
@@ -89,13 +124,6 @@ hb_draw_funcs_set_quadratic_to_func (hb_draw_funcs_t *funcs,
*
* Since: REPLACEME
**/
void
hb_draw_funcs_set_cubic_to_func (hb_draw_funcs_t *funcs,
hb_draw_cubic_to_func_t cubic_to)
{
if (unlikely (hb_object_is_immutable (funcs))) return;
funcs->cubic_to = cubic_to;
}
/**
* hb_draw_funcs_set_close_path_func:
@@ -106,33 +134,6 @@ hb_draw_funcs_set_cubic_to_func (hb_draw_funcs_t *funcs,
*
* Since: REPLACEME
**/
void
hb_draw_funcs_set_close_path_func (hb_draw_funcs_t *funcs,
hb_draw_close_path_func_t close_path)
{
if (unlikely (hb_object_is_immutable (funcs))) return;
funcs->close_path = close_path;
}
static void
_move_to_nil (float to_x HB_UNUSED, float to_y HB_UNUSED, void *user_data HB_UNUSED) {}
static void
_line_to_nil (float to_x HB_UNUSED, float to_y HB_UNUSED, void *user_data HB_UNUSED) {}
static void
_quadratic_to_nil (float control_x HB_UNUSED, float control_y HB_UNUSED,
float to_x HB_UNUSED, float to_y HB_UNUSED,
void *user_data HB_UNUSED) {}
static void
_cubic_to_nil (float control1_x HB_UNUSED, float control1_y HB_UNUSED,
float control2_x HB_UNUSED, float control2_y HB_UNUSED,
float to_x HB_UNUSED, float to_y HB_UNUSED,
void *user_data HB_UNUSED) {}
static void
_close_path_nil (void *user_data HB_UNUSED) {}
/**
* hb_draw_funcs_create:
@@ -148,15 +149,31 @@ hb_draw_funcs_create ()
if (unlikely (!(funcs = hb_object_create<hb_draw_funcs_t> ())))
return const_cast<hb_draw_funcs_t *> (&Null (hb_draw_funcs_t));
funcs->move_to = (hb_draw_move_to_func_t) _move_to_nil;
funcs->line_to = (hb_draw_line_to_func_t) _line_to_nil;
funcs->quadratic_to = (hb_draw_quadratic_to_func_t) _quadratic_to_nil;
funcs->is_quadratic_to_set = false;
funcs->cubic_to = (hb_draw_cubic_to_func_t) _cubic_to_nil;
funcs->close_path = (hb_draw_close_path_func_t) _close_path_nil;
/* XXX Clean up. Use nil object. */
funcs->func.move_to = hb_draw_move_to_nil;
funcs->func.line_to = hb_draw_line_to_nil;
funcs->func.quadratic_to = hb_draw_quadratic_to_nil;
funcs->func.cubic_to = hb_draw_cubic_to_nil;
funcs->func.close_path = hb_draw_close_path_nil;
return funcs;
}
DEFINE_NULL_INSTANCE (hb_draw_funcs_t) =
{
HB_OBJECT_HEADER_STATIC,
{
#define HB_DRAW_FUNC_IMPLEMENT(name) hb_draw_##name##_nil,
HB_DRAW_FUNCS_IMPLEMENT_CALLBACKS
#undef HB_DRAW_FUNC_IMPLEMENT
}
};
/* XXX Remove. */
bool
hb_draw_funcs_t::quadratic_to_is_set () { return func.quadratic_to != hb_draw_quadratic_to_nil; }
/**
* hb_draw_funcs_reference:
* @funcs: draw functions
+29 -25
View File
@@ -33,26 +33,6 @@
HB_BEGIN_DECLS
typedef void (*hb_draw_move_to_func_t) (float to_x,
float to_y,
void *user_data);
typedef void (*hb_draw_line_to_func_t) (float to_x,
float to_y,
void *user_data);
typedef void (*hb_draw_quadratic_to_func_t) (float control_x,
float control_y,
float to_x,
float to_y,
void *user_data);
typedef void (*hb_draw_cubic_to_func_t) (float control1_x,
float control1_y,
float control2_x,
float control2_y,
float to_x,
float to_y,
void *user_data);
typedef void (*hb_draw_close_path_func_t) (void *user_data);
/**
* hb_draw_funcs_t:
*
@@ -63,27 +43,51 @@ typedef void (*hb_draw_close_path_func_t) (void *user_data);
*
* Since: REPLACEME
**/
typedef struct hb_draw_funcs_t hb_draw_funcs_t;
typedef void (*hb_draw_move_to_func_t) (hb_draw_funcs_t *dfuncs, void *draw_data,
float to_x, float to_y,
void *user_data);
typedef void (*hb_draw_line_to_func_t) (hb_draw_funcs_t *dfuncs, void *draw_data,
float to_x, float to_y,
void *user_data);
typedef void (*hb_draw_quadratic_to_func_t) (hb_draw_funcs_t *dfuncs, void *draw_data,
float control_x, float control_y,
float to_x, float to_y,
void *user_data);
typedef void (*hb_draw_cubic_to_func_t) (hb_draw_funcs_t *dfuncs, void *draw_data,
float control1_x, float control1_y,
float control2_x, float control2_y,
float to_x, float to_y,
void *user_data);
typedef void (*hb_draw_close_path_func_t) (hb_draw_funcs_t *dfuncs, void *draw_data,
void *user_data);
HB_EXTERN void
hb_draw_funcs_set_move_to_func (hb_draw_funcs_t *funcs,
hb_draw_move_to_func_t move_to);
hb_draw_move_to_func_t move_to,
void *user_data, hb_destroy_func_t destroy);
HB_EXTERN void
hb_draw_funcs_set_line_to_func (hb_draw_funcs_t *funcs,
hb_draw_line_to_func_t line_to);
hb_draw_line_to_func_t line_to,
void *user_data, hb_destroy_func_t destroy);
HB_EXTERN void
hb_draw_funcs_set_quadratic_to_func (hb_draw_funcs_t *funcs,
hb_draw_quadratic_to_func_t quadratic_to);
hb_draw_quadratic_to_func_t quadratic_to,
void *user_data, hb_destroy_func_t destroy);
HB_EXTERN void
hb_draw_funcs_set_cubic_to_func (hb_draw_funcs_t *funcs,
hb_draw_cubic_to_func_t cubic_to);
hb_draw_cubic_to_func_t cubic_to,
void *user_data, hb_destroy_func_t destroy);
HB_EXTERN void
hb_draw_funcs_set_close_path_func (hb_draw_funcs_t *funcs,
hb_draw_close_path_func_t close_path);
hb_draw_close_path_func_t close_path,
void *user_data, hb_destroy_func_t destroy);
HB_EXTERN hb_draw_funcs_t *
hb_draw_funcs_create (void);
+78 -19
View File
@@ -27,24 +27,82 @@
#include "hb.hh"
/*
* 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 */
struct hb_draw_funcs_t
{
hb_object_header_t header;
hb_draw_move_to_func_t move_to;
hb_draw_line_to_func_t line_to;
hb_draw_quadratic_to_func_t quadratic_to;
bool is_quadratic_to_set;
hb_draw_cubic_to_func_t cubic_to;
hb_draw_close_path_func_t close_path;
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;
void move_to (void *draw_data,
float to_x, float to_y)
{ func.move_to (this, draw_data,
to_x, to_y,
user_data.move_to); }
void line_to (void *draw_data,
float to_x, float to_y)
{ func.line_to (this, draw_data,
to_x, to_y,
user_data.line_to); }
void quadratic_to (void *draw_data,
float control_x, float control_y,
float to_x, float to_y)
{ func.quadratic_to (this, draw_data,
control_x, control_y,
to_x, to_y,
user_data.quadratic_to); }
void cubic_to (void *draw_data,
float control1_x, float control1_y,
float control2_x, float control2_y,
float to_x, float to_y)
{ func.cubic_to (this, draw_data,
control1_x, control1_y,
control2_x, control2_y,
to_x, to_y,
user_data.cubic_to); }
void close_path (void *draw_data)
{ func.close_path (this, draw_data,
user_data.close_path); }
/* XXX Remove */
HB_INTERNAL bool quadratic_to_is_set ();
};
DECLARE_NULL_INSTANCE (hb_draw_funcs_t);
struct draw_helper_t
{
draw_helper_t (const hb_draw_funcs_t *funcs_, void *user_data_)
draw_helper_t (hb_draw_funcs_t *funcs_, void *draw_data_)
{
funcs = funcs_;
user_data = user_data_;
draw_data = draw_data_;
path_open = false;
path_start_x = current_x = path_start_y = current_y = 0;
}
@@ -61,7 +119,7 @@ struct draw_helper_t
{
if (equal_to_current (x, y)) return;
if (!path_open) start_path ();
funcs->line_to (x, y, user_data);
funcs->line_to (draw_data, x, y);
current_x = x;
current_y = y;
}
@@ -73,14 +131,15 @@ 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 (funcs->is_quadratic_to_set)
funcs->quadratic_to (control_x, control_y, to_x, to_y, user_data);
if (funcs->quadratic_to_is_set ())
funcs->quadratic_to (draw_data, control_x, control_y, to_x, to_y);
else
funcs->cubic_to (roundf ((current_x + 2.f * control_x) / 3.f),
funcs->cubic_to (draw_data,
roundf ((current_x + 2.f * control_x) / 3.f),
roundf ((current_y + 2.f * control_y) / 3.f),
roundf ((to_x + 2.f * control_x) / 3.f),
roundf ((to_y + 2.f * control_y) / 3.f),
to_x, to_y, user_data);
to_x, to_y);
current_x = to_x;
current_y = to_y;
}
@@ -95,7 +154,7 @@ struct draw_helper_t
equal_to_current (to_x, to_y))
return;
if (!path_open) start_path ();
funcs->cubic_to (control1_x, control1_y, control2_x, control2_y, to_x, to_y, user_data);
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;
}
@@ -105,8 +164,8 @@ struct draw_helper_t
if (path_open)
{
if ((path_start_x != current_x) || (path_start_y != current_y))
funcs->line_to (path_start_x, path_start_y, user_data);
funcs->close_path (user_data);
funcs->line_to (draw_data, path_start_x, path_start_y);
funcs->close_path (draw_data);
}
path_open = false;
path_start_x = current_x = path_start_y = current_y = 0;
@@ -120,7 +179,7 @@ struct draw_helper_t
{
if (path_open) end_path ();
path_open = true;
funcs->move_to (path_start_x, path_start_y, user_data);
funcs->move_to (draw_data, path_start_x, path_start_y);
}
float path_start_x;
@@ -130,8 +189,8 @@ struct draw_helper_t
float current_y;
bool path_open;
const hb_draw_funcs_t *funcs;
void *user_data;
hb_draw_funcs_t *funcs;
void *draw_data;
};
#endif /* HB_DRAW_HH */
+1 -1
View File
@@ -1059,7 +1059,7 @@ hb_font_set_var_named_instance (hb_font_t *font,
HB_EXTERN void
hb_font_draw_glyph (hb_font_t *font,
hb_codepoint_t glyph,
const hb_draw_funcs_t *funcs,
hb_draw_funcs_t *funcs,
void *user_data);
HB_END_DECLS
+36 -29
View File
@@ -129,48 +129,55 @@ png_dump (hb_face_t *face, unsigned face_index)
hb_font_destroy (font);
}
struct user_data_t
struct draw_data_t
{
FILE *f;
hb_position_t ascender;
};
static void
move_to (float to_x, float to_y, user_data_t *user_data)
move_to (hb_draw_funcs_t *dfuncs, draw_data_t *draw_data,
float to_x, float to_y,
void *)
{
fprintf (user_data->f, "M%g,%g", to_x, user_data->ascender - to_y);
fprintf (draw_data->f, "M%g,%g", to_x, draw_data->ascender - to_y);
}
static void
line_to (float to_x, float to_y, user_data_t *user_data)
line_to (hb_draw_funcs_t *dfuncs, draw_data_t *draw_data,
float to_x, float to_y,
void *)
{
fprintf (user_data->f, "L%g,%g", to_x, user_data->ascender - to_y);
fprintf (draw_data->f, "L%g,%g", to_x, draw_data->ascender - to_y);
}
static void
quadratic_to (float control_x, float control_y,
quadratic_to (hb_draw_funcs_t *dfuncs, draw_data_t *draw_data,
float control_x, float control_y,
float to_x, float to_y,
user_data_t *user_data)
void *)
{
fprintf (user_data->f, "Q%g,%g %g,%g", control_x, user_data->ascender - control_y,
to_x, user_data->ascender - to_y);
fprintf (draw_data->f, "Q%g,%g %g,%g", control_x, draw_data->ascender - control_y,
to_x, draw_data->ascender - to_y);
}
static void
cubic_to (float control1_x, float control1_y,
cubic_to (hb_draw_funcs_t *dfuncs, draw_data_t *draw_data,
float control1_x, float control1_y,
float control2_x, float control2_y,
float to_x, float to_y,
user_data_t *user_data)
void *)
{
fprintf (user_data->f, "C%g,%g %g,%g %g,%g", control1_x, user_data->ascender - control1_y,
control2_x, user_data->ascender - control2_y,
to_x, user_data->ascender - to_y);
fprintf (draw_data->f, "C%g,%g %g,%g %g,%g", control1_x, draw_data->ascender - control1_y,
control2_x, draw_data->ascender - control2_y,
to_x, draw_data->ascender - to_y);
}
static void
close_path (user_data_t *user_data)
close_path (hb_draw_funcs_t *dfuncs, draw_data_t *draw_data,
void *)
{
fprintf (user_data->f, "Z");
fprintf (draw_data->f, "Z");
}
static void
@@ -218,9 +225,9 @@ layered_glyph_dump (hb_font_t *font, hb_draw_funcs_t *funcs, unsigned face_index
" viewBox=\"%d %d %d %d\">\n",
extents.x_bearing, 0,
extents.x_bearing + extents.width, -extents.height);
user_data_t user_data;
user_data.ascender = extents.y_bearing;
user_data.f = f;
draw_data_t draw_data;
draw_data.ascender = extents.y_bearing;
draw_data.f = f;
for (unsigned layer = 0; layer < num_layers; ++layer)
{
@@ -232,7 +239,7 @@ layered_glyph_dump (hb_font_t *font, hb_draw_funcs_t *funcs, unsigned face_index
if (hb_color_get_alpha (color) != 255)
fprintf (f, "fill-opacity=\"%.3f\"", (double) hb_color_get_alpha (color) / 255.);
fprintf (f, "d=\"");
hb_font_draw_glyph (font, layers[layer].glyph, funcs, &user_data);
hb_font_draw_glyph (font, layers[layer].glyph, funcs, &draw_data);
fprintf (f, "\"/>\n");
}
@@ -268,10 +275,10 @@ dump_glyphs (hb_font_t *font, hb_draw_funcs_t *funcs, unsigned face_index)
" viewBox=\"%d %d %d %d\"><path d=\"",
extents.x_bearing, 0,
extents.x_bearing + extents.width, font_extents.ascender - font_extents.descender);
user_data_t user_data;
user_data.ascender = font_extents.ascender;
user_data.f = f;
hb_font_draw_glyph (font, gid, funcs, &user_data);
draw_data_t draw_data;
draw_data.ascender = font_extents.ascender;
draw_data.f = f;
hb_font_draw_glyph (font, gid, funcs, &draw_data);
fprintf (f, "\"/></svg>");
fclose (f);
}
@@ -298,11 +305,11 @@ dump_glyphs (hb_blob_t *blob, const char *font_name)
fclose (font_name_file);
hb_draw_funcs_t *funcs = hb_draw_funcs_create ();
hb_draw_funcs_set_move_to_func (funcs, (hb_draw_move_to_func_t) move_to);
hb_draw_funcs_set_line_to_func (funcs, (hb_draw_line_to_func_t) line_to);
hb_draw_funcs_set_quadratic_to_func (funcs, (hb_draw_quadratic_to_func_t) quadratic_to);
hb_draw_funcs_set_cubic_to_func (funcs, (hb_draw_cubic_to_func_t) cubic_to);
hb_draw_funcs_set_close_path_func (funcs, (hb_draw_close_path_func_t) close_path);
hb_draw_funcs_set_move_to_func (funcs, (hb_draw_move_to_func_t) move_to, nullptr, nullptr);
hb_draw_funcs_set_line_to_func (funcs, (hb_draw_line_to_func_t) line_to, nullptr, nullptr);
hb_draw_funcs_set_quadratic_to_func (funcs, (hb_draw_quadratic_to_func_t) quadratic_to, nullptr, nullptr);
hb_draw_funcs_set_cubic_to_func (funcs, (hb_draw_cubic_to_func_t) cubic_to, nullptr, nullptr);
hb_draw_funcs_set_close_path_func (funcs, (hb_draw_close_path_func_t) close_path, nullptr, nullptr);
unsigned num_faces = hb_face_count (blob);
for (unsigned face_index = 0; face_index < num_faces; ++face_index)