[draw][glyf] Implement quadratic to cubic call translation

This commit is contained in:
Ebrahim Byagowi committed 2020-01-28 15:27:27 +03:30
1 parent 74fdcdcac8
commit d106900bfd
4 files changed
+81 -14

No files matched your search

+1 -6
View File
@@ -123,11 +123,6 @@ _move_to_nil (hb_position_t to_x HB_UNUSED, hb_position_t to_y HB_UNUSED, void *
static void
_line_to_nil (hb_position_t to_x HB_UNUSED, hb_position_t to_y HB_UNUSED, void *user_data HB_UNUSED) {}
static void
_quadratic_to_nil (hb_position_t control_x HB_UNUSED, hb_position_t control_y HB_UNUSED,
hb_position_t to_x HB_UNUSED, hb_position_t to_y HB_UNUSED,
void *user_data HB_UNUSED) {}
static void
_cubic_to_nil (hb_position_t control1_x HB_UNUSED, hb_position_t control1_y HB_UNUSED,
hb_position_t control2_x HB_UNUSED, hb_position_t control2_y HB_UNUSED,
@@ -153,7 +148,7 @@ hb_draw_funcs_create ()
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->quadratic_to = nullptr;
funcs->cubic_to = (hb_draw_cubic_to_func_t) _cubic_to_nil;
funcs->close_path = (hb_draw_close_path_func_t) _close_path_nil;
return funcs;
+3
View File
@@ -49,6 +49,9 @@ typedef void (*hb_draw_close_path_func_t) (void *user_data);
*
* Glyph decompose callbacks.
*
* _move_to, _line_to and _cubic_to are nessecary to be defined but we
* can translate _quadratic_to calls to _cubic_to in case isn't defined.
*
* Since: REPLACEME
**/
typedef struct hb_draw_funcs_t hb_draw_funcs_t;
+52 -6
View File
@@ -1043,6 +1043,36 @@ struct glyf
add_gid_and_children (item.glyphIndex, gids_to_retain, depth);
}
static void
_normal_quadratic_to_call (hb_font_t *font, const hb_draw_funcs_t *funcs,
float from_x HB_UNUSED, float from_y HB_UNUSED,
float control_x, float control_y,
float to_x, float to_y,
void *user_data)
{
funcs->quadratic_to (font->em_scalef_x (control_x), font->em_scalef_y (control_y),
font->em_scalef_x (to_x), font->em_scalef_y (to_y),
user_data);
}
static void
_translate_quadratic_to_cubic (hb_font_t *font, const hb_draw_funcs_t *funcs,
float from_x, float from_y,
float control_x, float control_y,
float to_x, float to_y,
void *user_data)
{
/* based on https://github.com/fonttools/fonttools/blob/a37dab3/Lib/fontTools/pens/basePen.py#L218 */
float mid1_x = from_x + 0.6666666667f * (control_x - from_x);
float mid1_y = from_y + 0.6666666667f * (control_y - from_y);
float mid2_x = to_x + 0.6666666667f * (control_x - to_x);
float mid2_y = to_y + 0.6666666667f * (control_y - to_y);
funcs->cubic_to (font->em_scalef_x (mid1_x), font->em_scalef_y (mid1_y),
font->em_scalef_x (mid2_x), font->em_scalef_y (mid2_y),
font->em_scalef_x (to_x), font->em_scalef_y (to_y),
user_data);
}
bool
get_path (hb_font_t *font, hb_codepoint_t gid,
const hb_draw_funcs_t *funcs, void *user_data) const
@@ -1055,10 +1085,15 @@ struct glyf
if (unlikely (!get_points (font, gid, all_points))) return false;
hb_array_t<contour_point_t> points = all_points.sub_array (0, all_points.length - 4);
void (*quad_to) (hb_font_t *, const hb_draw_funcs_t *,
float, float, float, float, float, float,
void *) = funcs->quadratic_to ? _normal_quadratic_to_call : _translate_quadratic_to_cubic;
unsigned contour_start = 0;
/* Learnt from https://github.com/opentypejs/opentype.js/blob/4e0bb99/src/tables/glyf.js#L222 */
while (contour_start < points.length)
{
float prev_x = 0; float prev_y = 0;
unsigned contour_length = 0;
for (unsigned i = contour_start; i < points.length; ++i)
{
@@ -1070,15 +1105,23 @@ struct glyf
contour_point_t *next = &points[contour_start];
if (curr->flag & Glyph::FLAG_ON_CURVE)
funcs->move_to (font->em_scalef_x (curr->x), font->em_scalef_y (curr->y), user_data);
{
prev_x = curr->x; prev_y = curr->y;
funcs->move_to (font->em_scalef_x (prev_x), font->em_scalef_y (prev_y), user_data);
}
else
{
if (next->flag & Glyph::FLAG_ON_CURVE)
funcs->move_to (font->em_scalef_x (next->x), font->em_scalef_y (next->y), user_data);
{
prev_x = next->x; prev_y = next->y;
funcs->move_to (font->em_scalef_x (prev_x), font->em_scalef_y (prev_y), user_data);
}
else
{
prev_x = (curr->x + next->x) / 2.f; prev_y = (curr->y + next->y) / 2.f;
/* If both first and last points are off-curve, start at their middle. */
funcs->move_to (font->em_scalef_x ((curr->x + next->x) / 2.f),
font->em_scalef_y ((curr->y + next->y) / 2.f), user_data);
funcs->move_to (font->em_scalef_x (prev_x), font->em_scalef_y (prev_y), user_data);
}
}
for (unsigned i = 0; i < contour_length; ++i)
@@ -1087,14 +1130,17 @@ struct glyf
next = &points[contour_start + ((i + 1) % contour_length)];
if (curr->flag & Glyph::FLAG_ON_CURVE)
{
prev_x = curr->x; prev_y = curr->y;
funcs->line_to (font->em_scalef_x (curr->x), font->em_scalef_y (curr->y), user_data);
}
else
{
float to_x, to_y;
if (next->flag & Glyph::FLAG_ON_CURVE) { to_x = next->x; to_y = next->y; }
else { to_x = (curr->x + next->x) / 2.f; to_y = (curr->y + next->y) / 2.f; }
funcs->quadratic_to (font->em_scalef_x (curr->x), font->em_scalef_y (curr->y),
font->em_scalef_x (to_x), font->em_scalef_y (to_y), user_data);
quad_to (font, funcs, prev_x, prev_y, curr->x, curr->y, to_x, to_y, user_data);
prev_x = to_x; prev_y = to_y;
}
}
contour_start += contour_length;