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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
/*
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/
#include "scribushelper.h"
// Functions to convert from lib2geom to FPointArray and vice versa
void scribus_curve(FPointArray *cr, Geom::Curve const& c)
{
if(Geom::LineSegment const* line_segment = dynamic_cast<Geom::LineSegment const*>(&c))
{
cr->addPoint(currentPoint);
cr->addPoint(currentPoint);
cr->addPoint((*line_segment)[1][0], (*line_segment)[1][1]);
cr->addPoint((*line_segment)[1][0], (*line_segment)[1][1]);
currentPoint = FPoint((*line_segment)[1][0], (*line_segment)[1][1]);
}
else if(Geom::QuadraticBezier const *quadratic_bezier = dynamic_cast<Geom::QuadraticBezier const*>(&c))
{
std::vector<Geom::Point> points = quadratic_bezier->points();
Geom::Point b1 = points[0] + (2./3) * (points[1] - points[0]);
Geom::Point b2 = b1 + (1./3) * (points[2] - points[0]);
cr->addPoint(currentPoint);
cr->addPoint(b1[0], b1[1]);
cr->addPoint(points[2][0], points[2][1]);
cr->addPoint(b2[0], b2[1]);
currentPoint = FPoint(points[2][0], points[2][1]);
}
else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const*>(&c))
{
std::vector<Geom::Point> points = cubic_bezier->points();
cr->addPoint(currentPoint);
cr->addPoint(points[1][0], points[1][1]);
cr->addPoint(points[3][0], points[3][1]);
cr->addPoint(points[2][0], points[2][1]);
currentPoint = FPoint(points[3][0], points[3][1]);
}
else
{
//this case handles sbasis as well as all other curve types
Geom::Path sbasis_path = Geom::path_from_sbasis(c.toSBasis(), 0.1);
currentPoint = FPoint(sbasis_path.initialPoint()[0], sbasis_path.initialPoint()[1]);
//recurse to convert the new path resulting from the sbasis to svgd
for(Geom::Path::iterator iter = sbasis_path.begin(); iter != sbasis_path.end(); ++iter)
{
scribus_curve(cr, *iter);
}
}
}
void geomPath2FPointArray(FPointArray *p, Geom::Path &pp)
{
currentPoint = FPoint(pp.initialPoint()[0], pp.initialPoint()[1]);
for(Geom::Path::iterator iter(pp.begin()), end(pp.end()); iter != end; ++iter)
{
scribus_curve(p, *iter);
}
if (pp.closed())
p->setMarker();
}
void Piecewise2FPointArray(FPointArray *p, Geom::Piecewise<Geom::D2<Geom::SBasis> > &pp)
{
std::vector<Geom::Path> pa = path_from_piecewise( pp, 0.1);
std::vector<Geom::Path>::iterator it(pa.begin());
for(; it != pa.end(); it++)
{
geomPath2FPointArray(p, *it);
}
}
void D2sb2d2FPointArray(FPointArray* cr, Geom::D2<Geom::SBasis2d> const &sb2, int num, double width)
{
Geom::D2<Geom::SBasis> B;
for(int ui = 0; ui <= num; ui++)
{
double u = ui / static_cast<double>(num);
B[0] = extract_u(sb2[0], u);// + Linear(u);
B[1] = extract_u(sb2[1], u);
for(unsigned i = 0; i < 2; i ++)
{
B[i] = B[i]*(width/2) + Geom::Linear(width/4);
}
Geom::Path pp = path_from_sbasis(B, 0.1);
geomPath2FPointArray(cr, pp);
cr->setMarker();
}
for(int vi = 0; vi <= num; vi++)
{
double v = vi / static_cast<double>(num);
B[1] = extract_v(sb2[1], v);// + Linear(v);
B[0] = extract_v(sb2[0], v);
for(unsigned i = 0; i < 2; i ++)
{
B[i] = B[i]*(width/2) + Geom::Linear(width/4);
}
Geom::Path pp = path_from_sbasis(B, 0.1);
geomPath2FPointArray(cr, pp);
cr->setMarker();
}
}
std::vector<Geom::Path> FPointArray2geomPath(FPointArray &p, bool closed)
{
std::vector<Geom::Path> pa;
Geom::Path ret = Geom::Path();
Geom::Point cur;
FPoint np, np1, np2, np3;
bool nPath = true;
if (p.size() > 3)
{
for (uint poi=0; poi < p.size()-3; poi += 4)
{
if (p.point(poi).x() > 900000)
{
if (closed)
ret.close();
pa.push_back(ret);
ret.clear();
nPath = true;
continue;
}
if (nPath)
{
np = p.point(poi);
cur = Geom::Point(np.x(), np.y());
nPath = false;
}
np = p.point(poi);
np1 = p.point(poi+1);
np2 = p.point(poi+3);
np3 = p.point(poi+2);
if ((np == np1) && (np2 == np3))
{
// Geom::Point pe = Geom::Point(np3.x(), np3.y());
// ret.append(Geom::LineSegment(cur, pe));
Geom::Point pc1 = Geom::Point(np1.x()+0.001, np1.y()+0.001);
Geom::Point pc2 = Geom::Point(np2.x()+0.001, np2.y()+0.001);
Geom::Point pe = Geom::Point(np3.x(), np3.y());
ret.append(Geom::CubicBezier(cur, pc1, pc2, pe));
cur = pe;
}
else
{
Geom::Point pc1 = Geom::Point(np1.x(), np1.y());
Geom::Point pc2 = Geom::Point(np2.x(), np2.y());
Geom::Point pe = Geom::Point(np3.x(), np3.y());
ret.append(Geom::CubicBezier(cur, pc1, pc2, pe));
cur = pe;
}
}
}
if (closed)
ret.close();
pa.push_back(ret);
return pa;
}
Geom::Piecewise<Geom::D2<Geom::SBasis> > FPointArray2Piecewise(FPointArray &p, bool closed)
{
Geom::Piecewise<Geom::D2<Geom::SBasis> > patternpwd2;
std::vector<Geom::Path> originald = FPointArray2geomPath(p, closed);
for (unsigned int i=0; i < originald.size(); i++)
{
patternpwd2.concat( originald[i].toPwSb() );
}
return patternpwd2;
}
// Functions to convert from lib2geom to QPainterPath and vice versa
void arthur_curve(QPainterPath *cr, Geom::Curve const& c)
{
if(Geom::LineSegment const* line_segment = dynamic_cast<Geom::LineSegment const*>(&c))
{
cr->lineTo(QPointF((*line_segment)[1][0], (*line_segment)[1][1]));
}
else if(Geom::QuadraticBezier const *quadratic_bezier = dynamic_cast<Geom::QuadraticBezier const*>(&c))
{
std::vector<Geom::Point> points = quadratic_bezier->points();
Geom::Point b1 = points[0] + (2./3) * (points[1] - points[0]);
Geom::Point b2 = b1 + (1./3) * (points[2] - points[0]);
cr->cubicTo(b1[0], b1[1], b2[0], b2[1], points[2][0], points[2][1]);
}
else if(Geom::CubicBezier const *cubic_bezier = dynamic_cast<Geom::CubicBezier const*>(&c))
{
std::vector<Geom::Point> points = cubic_bezier->points();
cr->cubicTo(points[1][0], points[1][1], points[2][0], points[2][1], points[3][0], points[3][1]);
}
else
{
//this case handles sbasis as well as all other curve types
Geom::Path sbasis_path = Geom::path_from_sbasis(c.toSBasis(), 0.1);
cr->moveTo(sbasis_path.initialPoint()[0], sbasis_path.initialPoint()[1]);
//recurse to convert the new path resulting from the sbasis to svgd
for(Geom::Path::iterator iter = sbasis_path.begin(); iter != sbasis_path.end(); ++iter)
{
arthur_curve(cr, *iter);
}
}
}
void geomPath2QPainterPath(QPainterPath *p, Geom::Path &pp)
{
p->moveTo(pp.initialPoint()[0], pp.initialPoint()[1]);
for(Geom::Path::iterator iter(pp.begin()), end(pp.end()); iter != end; ++iter)
{
arthur_curve(p, *iter);
}
if (pp.closed())
p->closeSubpath();
}
void Piecewise2QPainterPath(QPainterPath *p, Geom::Piecewise<Geom::D2<Geom::SBasis> > &pp)
{
std::vector<Geom::Path> pa = path_from_piecewise( pp, 0.1);
std::vector<Geom::Path>::iterator it(pa.begin());
for(; it != pa.end(); it++)
{
geomPath2QPainterPath(p, *it);
}
}
void D2sb2d2QPainterPath(QPainterPath* cr, Geom::D2<Geom::SBasis2d> const &sb2, int num, double width)
{
Geom::D2<Geom::SBasis> B;
for(int ui = 0; ui <= num; ui++)
{
double u = ui / static_cast<double>(num);
B[0] = extract_u(sb2[0], u);// + Linear(u);
B[1] = extract_u(sb2[1], u);
for(unsigned i = 0; i < 2; i ++)
{
B[i] = B[i]*(width/2) + Geom::Linear(width/4);
}
Geom::Path pp = path_from_sbasis(B, 0.1);
geomPath2QPainterPath(cr, pp);
// cr->setMarker();
}
for(int vi = 0; vi <= num; vi++)
{
double v = vi / static_cast<double>(num);
B[1] = extract_v(sb2[1], v);// + Linear(v);
B[0] = extract_v(sb2[0], v);
for(unsigned i = 0; i < 2; i ++)
{
B[i] = B[i]*(width/2) + Geom::Linear(width/4);
}
Geom::Path pp = path_from_sbasis(B, 0.1);
geomPath2QPainterPath(cr, pp);
// cr->setMarker();
}
}
std::vector<Geom::Path> QPainterPath2geomPath(QPainterPath &p, bool closed)
{
std::vector<Geom::Path> pa;
Geom::Path ret = Geom::Path();
Geom::Point cur;
bool WasM = false;
for (int i = 0; i < p.elementCount(); ++i)
{
const QPainterPath::Element &elm = p.elementAt(i);
switch (elm.type)
{
case QPainterPath::MoveToElement:
if (WasM)
{
if (closed)
ret.close();
pa.push_back(ret);
ret.clear();
}
WasM = true;
cur = Geom::Point(elm.x, elm.y);
break;
case QPainterPath::LineToElement:
ret.append(Geom::LineSegment(cur, Geom::Point(elm.x, elm.y)));
cur = Geom::Point(elm.x, elm.y);
break;
case QPainterPath::CurveToElement:
{
Geom::Point pc1 = Geom::Point(elm.x, elm.y);
Geom::Point pc2 = Geom::Point(p.elementAt(i+1).x, p.elementAt(i+1).y);
Geom::Point pe = Geom::Point(p.elementAt(i+2).x, p.elementAt(i+2).y);
ret.append(Geom::CubicBezier(cur, pc1, pc2, pe));
cur = pe;
}
break;
default:
break;
}
}
if (closed)
ret.close();
pa.push_back(ret);
return pa;
}
Geom::Piecewise<Geom::D2<Geom::SBasis> > QPainterPath2Piecewise(QPainterPath &p, bool closed)
{
Geom::Piecewise<Geom::D2<Geom::SBasis> > patternpwd2;
std::vector<Geom::Path> originald = QPainterPath2geomPath(p, closed);
for (unsigned int i=0; i < originald.size(); i++)
{
patternpwd2.concat( originald[i].toPwSb() );
}
return patternpwd2;
}
|