2022-06-26 02:14:42 +02:00
|
|
|
#ifndef OT_GLYF_PATH_BUILDER_HH
|
|
|
|
#define OT_GLYF_PATH_BUILDER_HH
|
2022-06-26 02:07:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
#include "../../hb.hh"
|
|
|
|
|
|
|
|
|
|
|
|
namespace OT {
|
|
|
|
namespace glyf_impl {
|
|
|
|
|
|
|
|
|
|
|
|
struct path_builder_t
|
|
|
|
{
|
|
|
|
hb_font_t *font;
|
|
|
|
hb_draw_session_t *draw_session;
|
|
|
|
|
|
|
|
struct optional_point_t
|
|
|
|
{
|
2022-06-26 21:18:00 +02:00
|
|
|
optional_point_t () {}
|
|
|
|
optional_point_t (float x_, float y_) : has_data (true), x (x_), y (y_) {}
|
2022-06-27 00:12:50 +02:00
|
|
|
operator bool () const { return has_data; }
|
2022-06-26 02:07:49 +02:00
|
|
|
|
2022-06-26 21:18:00 +02:00
|
|
|
bool has_data = false;
|
|
|
|
float x = 0.;
|
|
|
|
float y = 0.;
|
2022-06-26 02:07:49 +02:00
|
|
|
|
|
|
|
optional_point_t lerp (optional_point_t p, float t)
|
|
|
|
{ return optional_point_t (x + t * (p.x - x), y + t * (p.y - y)); }
|
2023-02-21 23:25:59 +01:00
|
|
|
} first_oncurve, first_offcurve, first_offcurve2, last_offcurve, last_offcurve2;
|
2022-06-26 02:07:49 +02:00
|
|
|
|
|
|
|
path_builder_t (hb_font_t *font_, hb_draw_session_t &draw_session_)
|
|
|
|
{
|
|
|
|
font = font_;
|
|
|
|
draw_session = &draw_session_;
|
2023-02-21 23:25:59 +01:00
|
|
|
first_oncurve = first_offcurve = first_offcurve2 = last_offcurve = last_offcurve2 = optional_point_t ();
|
2022-06-26 02:07:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* based on https://github.com/RazrFalcon/ttf-parser/blob/4f32821/src/glyf.rs#L287
|
|
|
|
See also:
|
|
|
|
* https://developer.apple.com/fonts/TrueType-Reference-Manual/RM01/Chap1.html
|
2023-02-10 22:24:03 +01:00
|
|
|
* https://stackoverflow.com/a/20772557
|
|
|
|
*
|
2023-02-22 00:15:45 +01:00
|
|
|
* Cubic support added. */
|
2022-06-26 02:07:49 +02:00
|
|
|
void consume_point (const contour_point_t &point)
|
|
|
|
{
|
|
|
|
bool is_on_curve = point.flag & glyf_impl::SimpleGlyph::FLAG_ON_CURVE;
|
2023-02-10 22:15:16 +01:00
|
|
|
#ifdef HB_NO_CUBIC_GLYF
|
|
|
|
bool is_cubic = false;
|
|
|
|
#else
|
2023-02-08 06:19:45 +01:00
|
|
|
bool is_cubic = !is_on_curve && (point.flag & glyf_impl::SimpleGlyph::FLAG_CUBIC);
|
2023-02-10 22:15:16 +01:00
|
|
|
#endif
|
2022-06-27 00:26:17 +02:00
|
|
|
optional_point_t p (font->em_fscalef_x (point.x), font->em_fscalef_y (point.y));
|
2022-06-27 00:12:50 +02:00
|
|
|
if (!first_oncurve)
|
2022-06-26 02:07:49 +02:00
|
|
|
{
|
|
|
|
if (is_on_curve)
|
|
|
|
{
|
|
|
|
first_oncurve = p;
|
2022-06-27 00:26:17 +02:00
|
|
|
draw_session->move_to (p.x, p.y);
|
2022-06-26 02:07:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-02-21 23:25:59 +01:00
|
|
|
if (is_cubic && !first_offcurve2)
|
|
|
|
{
|
|
|
|
first_offcurve2 = first_offcurve;
|
|
|
|
first_offcurve = p;
|
|
|
|
}
|
|
|
|
else if (first_offcurve)
|
2022-06-26 02:07:49 +02:00
|
|
|
{
|
|
|
|
optional_point_t mid = first_offcurve.lerp (p, .5f);
|
|
|
|
first_oncurve = mid;
|
|
|
|
last_offcurve = p;
|
2022-06-27 00:26:17 +02:00
|
|
|
draw_session->move_to (mid.x, mid.y);
|
2022-06-26 02:07:49 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
first_offcurve = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-06-27 00:12:50 +02:00
|
|
|
if (last_offcurve)
|
2022-06-26 02:07:49 +02:00
|
|
|
{
|
|
|
|
if (is_on_curve)
|
|
|
|
{
|
2023-02-08 06:19:45 +01:00
|
|
|
if (last_offcurve2)
|
|
|
|
{
|
|
|
|
draw_session->cubic_to (last_offcurve2.x, last_offcurve2.y,
|
|
|
|
last_offcurve.x, last_offcurve.y,
|
|
|
|
p.x, p.y);
|
|
|
|
last_offcurve2 = optional_point_t ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
draw_session->quadratic_to (last_offcurve.x, last_offcurve.y,
|
|
|
|
p.x, p.y);
|
2022-06-26 02:07:49 +02:00
|
|
|
last_offcurve = optional_point_t ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-02-08 06:19:45 +01:00
|
|
|
if (is_cubic && !last_offcurve2)
|
|
|
|
{
|
|
|
|
last_offcurve2 = last_offcurve;
|
|
|
|
last_offcurve = p;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
optional_point_t mid = last_offcurve.lerp (p, .5f);
|
|
|
|
|
|
|
|
if (is_cubic)
|
|
|
|
{
|
|
|
|
draw_session->cubic_to (last_offcurve2.x, last_offcurve2.y,
|
|
|
|
last_offcurve.x, last_offcurve.y,
|
|
|
|
mid.x, mid.y);
|
|
|
|
last_offcurve2 = optional_point_t ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
draw_session->quadratic_to (last_offcurve.x, last_offcurve.y,
|
|
|
|
mid.x, mid.y);
|
|
|
|
last_offcurve = p;
|
|
|
|
}
|
2022-06-26 02:07:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (is_on_curve)
|
2022-06-27 00:26:17 +02:00
|
|
|
draw_session->line_to (p.x, p.y);
|
2022-06-26 02:07:49 +02:00
|
|
|
else
|
|
|
|
last_offcurve = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (point.is_end_point)
|
|
|
|
{
|
2022-06-27 00:12:50 +02:00
|
|
|
if (first_offcurve && last_offcurve)
|
2022-06-26 02:07:49 +02:00
|
|
|
{
|
2023-02-21 23:50:55 +01:00
|
|
|
optional_point_t mid = last_offcurve.lerp (first_offcurve2 ?
|
|
|
|
first_offcurve2 :
|
|
|
|
first_offcurve, .5f);
|
|
|
|
if (last_offcurve2)
|
|
|
|
draw_session->cubic_to (last_offcurve2.x, last_offcurve2.y,
|
|
|
|
last_offcurve.x, last_offcurve.y,
|
|
|
|
mid.x, mid.y);
|
|
|
|
else
|
|
|
|
draw_session->quadratic_to (last_offcurve.x, last_offcurve.y,
|
|
|
|
mid.x, mid.y);
|
2022-06-26 02:07:49 +02:00
|
|
|
last_offcurve = optional_point_t ();
|
|
|
|
}
|
2023-02-21 23:50:55 +01:00
|
|
|
/* now check the rest */
|
2022-06-26 02:07:49 +02:00
|
|
|
|
2022-06-27 00:12:50 +02:00
|
|
|
if (first_offcurve && first_oncurve)
|
2023-02-21 23:50:55 +01:00
|
|
|
{
|
|
|
|
if (first_offcurve2)
|
|
|
|
draw_session->cubic_to (first_offcurve2.x, first_offcurve2.y,
|
|
|
|
first_offcurve.x, first_offcurve.y,
|
|
|
|
first_oncurve.x, first_oncurve.y);
|
|
|
|
else
|
|
|
|
draw_session->quadratic_to (first_offcurve.x, first_offcurve.y,
|
|
|
|
first_oncurve.x, first_oncurve.y);
|
|
|
|
}
|
2022-06-27 00:12:50 +02:00
|
|
|
else if (last_offcurve && first_oncurve)
|
2023-02-08 06:19:45 +01:00
|
|
|
{
|
|
|
|
if (last_offcurve2)
|
|
|
|
draw_session->cubic_to (last_offcurve2.x, last_offcurve2.y,
|
|
|
|
last_offcurve.x, last_offcurve.y,
|
|
|
|
first_oncurve.x, first_oncurve.y);
|
|
|
|
else
|
|
|
|
draw_session->quadratic_to (last_offcurve.x, last_offcurve.y,
|
|
|
|
first_oncurve.x, first_oncurve.y);
|
|
|
|
}
|
2022-06-27 00:12:50 +02:00
|
|
|
else if (first_oncurve)
|
2022-06-27 00:26:17 +02:00
|
|
|
draw_session->line_to (first_oncurve.x, first_oncurve.y);
|
2022-06-27 00:12:50 +02:00
|
|
|
else if (first_offcurve)
|
2022-06-26 02:07:49 +02:00
|
|
|
{
|
2022-06-27 00:26:17 +02:00
|
|
|
float x = first_offcurve.x, y = first_offcurve.y;
|
2022-06-26 02:07:49 +02:00
|
|
|
draw_session->move_to (x, y);
|
|
|
|
draw_session->quadratic_to (x, y, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Getting ready for the next contour */
|
2023-02-08 06:19:45 +01:00
|
|
|
first_oncurve = first_offcurve = last_offcurve = last_offcurve2 = optional_point_t ();
|
2022-06-26 02:07:49 +02:00
|
|
|
draw_session->close_path ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void points_end () {}
|
|
|
|
|
|
|
|
bool is_consuming_contour_points () { return true; }
|
|
|
|
contour_point_t *get_phantoms_sink () { return nullptr; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} /* namespace glyf_impl */
|
|
|
|
} /* namespace OT */
|
|
|
|
|
|
|
|
|
2022-06-26 02:14:42 +02:00
|
|
|
#endif /* OT_GLYF_PATH_BUILDER_HH */
|