Blob Blame Raw


#include "outlinearea.h"


namespace {
	struct DrawBezier {
	private:
		const Context &context;
		const Bezier &bezier;
		const Real width;
		const Real precision_sqr;
		const int max_level = 16;
		int level;
		Vector p0;
		Real l0;

		Vector func(const Real &l)
			{ return bezier(l) + bezier.t(l).perp().norm()*width; }

		void step(const Real &l1) {
			Vector p1 = bezier(l1) + bezier.t(l1).perp().norm()*width;
			if (++level < max_level)
				while (greater((p1 - p0).lensqr(), precision_sqr))
					step(l0 + 0.5*(l1 - l0));
			--level;
			context.line_to(p1);
			p0 = p1; l0 = l1;
		}

	public:
		DrawBezier(const Context &context, const Bezier &bezier, const Real &width = 0, bool jump = false):
			context(context), bezier(bezier), width(width), precision_sqr(context.pixelsize_sqr()), level(), l0()
		{
			p0 = func(0);
			if (jump) context.move_to(p0); else context.line_to(p0);
			step(1);
		}
	};
}


OutlineArea::OutlineArea():
	w(new ActivePoint(Vector(), Color::red()))
{
	set_size_request(500, 500);
	
	Vector center(250, 250);
	const Real radius = 100;
	const Real tangent_len = 50;
	const int count = 5;
	for(int i = 0; i < count; ++i) {
		Real a = 2*pi*i/count;
		Vector v = Vector::from_angle(a);
		curve.push_back(ActiveCurvePoint(center + v*radius, v*tangent_len));
		curve.back().add_to(*this);
	}
	//curve.loop = true;
	curve.set_tail_tangents_visible();
	add_point(w);
	set_width(50);
}

Real OutlineArea::get_width() const
	{ return fabs(dot(curve.front().get_t1().perp().norm(), w->position - curve.front().get_p())); }

void OutlineArea::set_width(const Real &width)
	{ w->position = curve.front().get_p() + curve.front().get_t1().perp().norm()*width; }

void OutlineArea::on_point_move(const ActivePoint::Handle &point, const Vector &oldposition, const Vector &newposition) {
	Real width = -1;
	if (point == w)
		width = get_width();
	if (!curve.empty())
		if (curve.front().p == point || curve.front().t1 == point)
			width = get_width();

	curve.on_point_move(point, oldposition, newposition);

	if (width >= 0)
		set_width(width);
}

void OutlineArea::put_bezier(const Context &context, const Bezier &bezier, const Real &width, bool jump) {
	if (equal(width, 0))
		context.bezier(bezier, jump);
	else
		DrawBezier(context, bezier, width, jump);
}

void OutlineArea::put_curve(const Context &context, const ActiveCurve &curve, const Real &width) {
	for(ActiveCurve::SegIter i(curve); i; i.next())
		put_bezier(context, i.bezier(), width);
	if (curve.loop) context->close_path();
	for(ActiveCurve::SegRIter i(curve); i; i.next())
		put_bezier(context, i.bezier(), width);
	context->close_path();
}

void OutlineArea::draw_curve(const Context &context, const ActiveCurve &curve, const Real &width) {
	context->save();
	
	curve.draw(context);
	
	context.set_line_width_px(1);
	context.set_source_rgba(Color::blue());
	put_curve(context, curve, width);
	context->stroke();
	
	context->restore();
}

void OutlineArea::on_draw_content(const Context &context) {
	draw_curve(context, curve, get_width());
}