summaryrefslogtreecommitdiffstats
path: root/ksvg/plugin/backends/libart/BezierPathLibart.cpp
blob: fb163952cf76613d350cfd5fbf73d9c191b580dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
    Copyright (C) 2003 Nikolas Zimmermann <wildfox@kde.org>
    This file is part of the KDE project

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    aint with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <math.h>
#include "Point.h"
#include "BezierPathLibart.h"

#include <kdebug.h>

#include <libart_lgpl/art_bpath.h>
#include <libart_lgpl/art_vpath.h>
#include <libart_lgpl/art_vpath_bpath.h>

using namespace T2P;

BezierPathLibart::BezierPathLibart() : BezierPath()
{
	m_array.resize(0);
	m_length = -1;
}

BezierPathLibart::BezierPathLibart(ArtBpath *other) : BezierPath()
{
	int i = 0;
	for(;other[i].code != ART_END; i++)
	{
		ensureSpace(m_array, i)
		m_array[i] = other[i];
	}
	ensureSpace(m_array, i)
	m_array[i].code = ART_END;
}

BezierPathLibart::~BezierPathLibart()
{
}

double BezierPathLibart::length(double t)
{
	if(m_length < 0.0)
	{
		double total = 0.0;
		// We cheat a bit...
		ArtVpath *vpath = art_bez_path_to_vec(m_array.data(), 0.25);
		double x = 0.0, y = 0.0;
		for(int i = 0; vpath[i].code != ART_END; i++)
		{
			if(vpath[i].code == ART_MOVETO)
			{
				x = vpath[i].x;
				y = vpath[i].y;
			}
			else if(vpath[i].code == ART_LINETO)
			{
				double dx = x, dy = y;
				x = vpath[i].x;
				y = vpath[i].y;
				dx = x - dx;
				dy = y - dy;
				total += sqrt(pow(dx, 2) + pow(dy, 2));
			}
		}
		art_free(vpath);
		return total * t;
	}
	else
		return m_length * t;
}

void BezierPathLibart::pointTangentNormalAt(double t, Point *p, Point *tn, Point *n)
{
	double totallen = length(t);
	// We cheat a bit...
	ArtVpath *vpath = art_bez_path_to_vec(m_array.data(), 0.25);
	double total = 0.0;
	double x = 0.0, y = 0.0;
	for(int i = 0; vpath[i].code != ART_END; i++)
	{
		if(vpath[i].code == ART_MOVETO)
		{
			x = vpath[i].x;
			y = vpath[i].y;
		}
		else if(vpath[i].code == ART_LINETO)
		{
			double dx = x, dy = y;
			x = vpath[i].x;
			y = vpath[i].y;
			dx = x - dx;
			dy = y - dy;
			double seg_len = sqrt(pow(dx, 2) + pow(dy, 2));
			total += seg_len;
			if(total >= totallen)
			{
				double fract = 1 - (totallen - (total - seg_len)) / seg_len;
				if(p)
				{
					p->setX(x - dx * fract);
					p->setY(y - dy * fract);
				}
				// Calculate tangent
				if(tn)
				{
					tn->setX(dx);
					tn->setY(dy);
				}
				// Calculate normal vector.
				if(n)
				{
					// Calculate vector product of "binormal" x tangent
					// (0,0,1) x (dx,dy,0), which is simply (dy,-dx,0).
					n->setX(dy);
					n->setY(-dx);
				}
				return;
			}
		}
	}
	art_free(vpath);
}	   

void BezierPathLibart::boundingBox(Point *topLeft, Point *bottomRight)
{
	if(m_array.count() > 0)
	{
		// We cheat a bit...
		ArtVpath *vpath = art_bez_path_to_vec(m_array.data(), 0.25);

		ArtDRect rect;
		art_vpath_bbox_drect(vpath, &rect);
		art_free(vpath);

		*topLeft = Point(rect.x0, rect.y0);
		*bottomRight = Point(rect.x1, rect.y1);
	}
	else
	{
		*topLeft = Point(0, 0);
		*bottomRight = Point(0, 0);
	}
}

// vim:ts=4:noet