Debian Codegraph @

src/lib/CDRPath.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libcdr project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #include "CDRPath.h"
11
12 #include <math.h>
13 #include <map>
14
15 #include "CDRTransforms.h"
16 #include "libcdr_utils.h"
17
18 #ifndef DEBUG_SPLINES
19 #define DEBUG_SPLINES 0
20 #endif
21
22 namespace libcdr
23 {
24
25 namespace
26 {
27
28 static inline double getAngle(double bx, double by)
29 {
30 return fmod(2*M_PI + (by > 0.0 ? 1.0 : -1.0) * acos(bx / sqrt(bx * bx + by * by)), 2*M_PI);
31 }
32
33 static void getEllipticalArcBBox(double x0, double y0,
34 double rx, double ry, double phi, bool largeArc, bool sweep, double x, double y,
35 double &xmin, double &ymin, double &xmax, double &ymax)
36 {
37 phi *= M_PI/180;
38 if (rx < 0.0)
39 rx *= -1.0;
40 if (ry < 0.0)
41 ry *= -1.0;
42
43 if (CDR_ALMOST_ZERO(rx) || CDR_ALMOST_ZERO(ry))
44 {
45 xmin = (x0 < x ? x0 : x);
46 xmax = (x0 > x ? x0 : x);
47 ymin = (y0 < y ? y0 : y);
48 ymax = (y0 > y ? y0 : y);
49 return;
50 }
51
52 const double x1prime = cos(phi)*(x0 - x)/2 + sin(phi)*(y0 - y)/2;
53 const double y1prime = -sin(phi)*(x0 - x)/2 + cos(phi)*(y0 - y)/2;
54
55 double radicant = (rx*rx*ry*ry - rx*rx*y1prime*y1prime - ry*ry*x1prime*x1prime)/(rx*rx*y1prime*y1prime + ry*ry*x1prime*x1prime);
56 double cxprime = 0.0;
57 double cyprime = 0.0;
58 if (radicant < 0.0)
59 {
60 double ratio = rx/ry;
61 radicant = y1prime*y1prime + x1prime*x1prime/(ratio*ratio);
62 if (radicant < 0.0)
63 {
64 xmin = (x0 < x ? x0 : x);
65 xmax = (x0 > x ? x0 : x);
66 ymin = (y0 < y ? y0 : y);
67 ymax = (y0 > y ? y0 : y);
68 return;
69 }
70 ry=sqrt(radicant);
71 rx=ratio*ry;
72 }
73 else
74 {
75 double factor = (largeArc==sweep ? -1.0 : 1.0)*sqrt(radicant);
76
77 cxprime = factor*rx*y1prime/ry;
78 cyprime = -factor*ry*x1prime/rx;
79 }
80
81 double cx = cxprime*cos(phi) - cyprime*sin(phi) + (x0 + x)/2;
82 double cy = cxprime*sin(phi) + cyprime*cos(phi) + (y0 + y)/2;
83
84 double txmin, txmax, tymin, tymax;
85
86 if (CDR_ALMOST_ZERO(phi) || CDR_ALMOST_EQUAL(phi, M_PI))
87 {
88 xmin = cx - rx;
89 txmin = getAngle(-rx, 0);
90 xmax = cx + rx;
91 txmax = getAngle(rx, 0);
92 ymin = cy - ry;
93 tymin = getAngle(0, -ry);
94 ymax = cy + ry;
95 tymax = getAngle(0, ry);
96 }
97 else if (CDR_ALMOST_EQUAL(phi, M_PI / 2.0) || CDR_ALMOST_EQUAL(phi, 3.0*M_PI/2.0))
98 {
99 xmin = cx - ry;
100 txmin = getAngle(-ry, 0);
101 xmax = cx + ry;
102 txmax = getAngle(ry, 0);
103 ymin = cy - rx;
104 tymin = getAngle(0, -rx);
105 ymax = cy + rx;
106 tymax = getAngle(0, rx);
107 }
108 else
109 {
110 txmin = -atan(ry*tan(phi)/rx);
111 txmax = M_PI - atan(ry*tan(phi)/rx);
112 xmin = cx + rx*cos(txmin)*cos(phi) - ry*sin(txmin)*sin(phi);
113 xmax = cx + rx*cos(txmax)*cos(phi) - ry*sin(txmax)*sin(phi);
114 double tmpY = cy + rx*cos(txmin)*sin(phi) + ry*sin(txmin)*cos(phi);
115 txmin = getAngle(xmin - cx, tmpY - cy);
116 tmpY = cy + rx*cos(txmax)*sin(phi) + ry*sin(txmax)*cos(phi);
117 txmax = getAngle(xmax - cx, tmpY - cy);
118
119 tymin = atan(ry/(tan(phi)*rx));
120 tymax = atan(ry/(tan(phi)*rx))+M_PI;
121 ymin = cy + rx*cos(tymin)*sin(phi) + ry*sin(tymin)*cos(phi);
122 ymax = cy + rx*cos(tymax)*sin(phi) + ry*sin(tymax)*cos(phi);
123 double tmpX = cx + rx*cos(tymin)*cos(phi) - ry*sin(tymin)*sin(phi);
124 tymin = getAngle(tmpX - cx, ymin - cy);
125 tmpX = cx + rx*cos(tymax)*cos(phi) - ry*sin(tymax)*sin(phi);
126 tymax = getAngle(tmpX - cx, ymax - cy);
127 }
128 if (xmin > xmax)
129 {
130 std::swap(xmin,xmax);
131 std::swap(txmin,txmax);
132 }
133 if (ymin > ymax)
134 {
135 std::swap(ymin,ymax);
136 std::swap(tymin,tymax);
137 }
138 double angle1 = getAngle(x0 - cx, y0 - cy);
139 double angle2 = getAngle(x - cx, y - cy);
140
141 if (!sweep)
142 std::swap(angle1, angle2);
143
144 bool otherArc = false;
145 if (angle1 > angle2)
146 {
147 std::swap(angle1, angle2);
148 otherArc = true;
149 }
150
151 if ((!otherArc && (angle1 > txmin || angle2 < txmin)) || (otherArc && !(angle1 > txmin || angle2 < txmin)))
152 xmin = x0 < x ? x0 : x;
153 if ((!otherArc && (angle1 > txmax || angle2 < txmax)) || (otherArc && !(angle1 > txmax || angle2 < txmax)))
154 xmax = x0 > x ? x0 : x;
155 if ((!otherArc && (angle1 > tymin || angle2 < tymin)) || (otherArc && !(angle1 > tymin || angle2 < tymin)))
156 ymin = y0 < y ? y0 : y;
157 if ((!otherArc && (angle1 > tymax || angle2 < tymax)) || (otherArc && !(angle1 > tymax || angle2 < tymax)))
158 ymax = y0 > y ? y0 : y;
159 }
160
161 static inline double quadraticExtreme(double t, double a, double b, double c)
162 {
163 return (1.0-t)*(1.0-t)*a + 2.0*(1.0-t)*t*b + t*t*c;
164 }
165
166 static inline double quadraticDerivative(double a, double b, double c)
167 {
168 double denominator = a - 2.0*b + c;
169 if (!CDR_ALMOST_ZERO(denominator))
170 return (a - b)/denominator;
171 return -1.0;
172 }
173
174 static void getQuadraticBezierBBox(double x0, double y0, double x1, double y1, double x, double y,
175 double &xmin, double &ymin, double &xmax, double &ymax)
176 {
177 xmin = x0 < x ? x0 : x;
178 xmax = x0 > x ? x0 : x;
179 ymin = y0 < y ? y0 : y;
180 ymax = y0 > y ? y0 : y;
181
182 double t = quadraticDerivative(x0, x1, x);
183 if (t>=0 && t<=1)
184 {
185 double tmpx = quadraticExtreme(t, x0, x1, x);
186 xmin = tmpx < xmin ? tmpx : xmin;
187 xmax = tmpx > xmax ? tmpx : xmax;
188 }
189
190 t = quadraticDerivative(y0, y1, y);
191 if (t>=0 && t<=1)
192 {
193 double tmpy = quadraticExtreme(t, y0, y1, y);
194 ymin = tmpy < ymin ? tmpy : ymin;
195 ymax = tmpy > ymax ? tmpy : ymax;
196 }
197 }
198
199 static inline double cubicBase(double t, double a, double b, double c, double d)
200 {
201 return (1.0-t)*(1.0-t)*(1.0-t)*a + 3.0*(1.0-t)*(1.0-t)*t*b + 3.0*(1.0-t)*t*t*c + t*t*t*d;
202 }
203
204 static void getCubicBezierBBox(double x0, double y0, double x1, double y1, double x2, double y2, double x, double y,
205 double &xmin, double &ymin, double &xmax, double &ymax)
206 {
207 xmin = x0 < x ? x0 : x;
208 xmax = x0 > x ? x0 : x;
209 ymin = y0 < y ? y0 : y;
210 ymax = y0 > y ? y0 : y;
211
212 for (double t = 0.0; t <= 1.0; t+=0.01)
213 {
214 double tmpx = cubicBase(t, x0, x1, x2, x);
215 xmin = tmpx < xmin ? tmpx : xmin;
216 xmax = tmpx > xmax ? tmpx : xmax;
217 double tmpy = cubicBase(t, y0, y1, y2, y);
218 ymin = tmpy < ymin ? tmpy : ymin;
219 ymax = tmpy > ymax ? tmpy : ymax;
220 }
221 }
222
223 } // anonymous namespace
224
225 class CDRMoveToElement : public CDRPathElement
226 {
227 public:
228 CDRMoveToElement(double x, double y)
229 : m_x(x),
230 m_y(y) {}
231 ~CDRMoveToElement() override {}
232 void writeOut(librevenge::RVNGPropertyListVector &vec) const override;
233 void transform(const CDRTransforms &trafos) override;
234 void transform(const CDRTransform &trafo) override;
235 std::unique_ptr<CDRPathElement> clone() override;
236 private:
237 double m_x;
238 double m_y;
239 };
240
241 class CDRLineToElement : public CDRPathElement
242 {
243 public:
244 CDRLineToElement(double x, double y)
245 : m_x(x),
246 m_y(y) {}
247 ~CDRLineToElement() override {}
248 void writeOut(librevenge::RVNGPropertyListVector &vec) const override;
249 void transform(const CDRTransforms &trafos) override;
250 void transform(const CDRTransform &trafo) override;
251 std::unique_ptr<CDRPathElement> clone() override;
252 private:
253 double m_x;
254 double m_y;
255 };
256
257 class CDRCubicBezierToElement : public CDRPathElement
258 {
259 public:
260 CDRCubicBezierToElement(double x1, double y1, double x2, double y2, double x, double y)
261 : m_x1(x1),
262 m_y1(y1),
263 m_x2(x2),
264 m_y2(y2),
265 m_x(x),
266 m_y(y) {}
267 ~CDRCubicBezierToElement() override {}
268 void writeOut(librevenge::RVNGPropertyListVector &vec) const override;
269 void transform(const CDRTransforms &trafos) override;
270 void transform(const CDRTransform &trafo) override;
271 std::unique_ptr<CDRPathElement> clone() override;
272 private:
273 double m_x1;
274 double m_y1;
275 double m_x2;
276 double m_y2;
277 double m_x;
278 double m_y;
279 };
280
281 class CDRQuadraticBezierToElement : public CDRPathElement
282 {
283 public:
284 CDRQuadraticBezierToElement(double x1, double y1, double x, double y)
285 : m_x1(x1),
286 m_y1(y1),
287 m_x(x),
288 m_y(y) {}
289 ~CDRQuadraticBezierToElement() override {}
290 void writeOut(librevenge::RVNGPropertyListVector &vec) const override;
291 void transform(const CDRTransforms &trafos) override;
292 void transform(const CDRTransform &trafo) override;
293 std::unique_ptr<CDRPathElement> clone() override;
294 private:
295 double m_x1;
296 double m_y1;
297 double m_x;
298 double m_y;
299 };
300
301 class CDRSplineToElement : public CDRPathElement
302 {
303 public:
304 CDRSplineToElement(const std::vector<std::pair<double, double> > &points)
305 : m_points(points) {}
306 ~CDRSplineToElement() override {}
307 void writeOut(librevenge::RVNGPropertyListVector &vec) const override;
308 void transform(const CDRTransforms &trafos) override;
309 void transform(const CDRTransform &trafo) override;
310 std::unique_ptr<CDRPathElement> clone() override;
311 private:
312 std::vector<std::pair<double, double> > m_points;
313 unsigned knot(unsigned i) const;
314 };
315
316 class CDRArcToElement : public CDRPathElement
317 {
318 public:
319 CDRArcToElement(double rx, double ry, double rotation, bool largeArc, bool sweep, double x, double y)
320 : m_rx(rx),
321 m_ry(ry),
322 m_rotation(rotation),
323 m_largeArc(largeArc),
324 m_sweep(sweep),
325 m_x(x),
326 m_y(y) {}
327 ~CDRArcToElement() override {}
328 void writeOut(librevenge::RVNGPropertyListVector &vec) const override;
329 void transform(const CDRTransforms &trafos) override;
330 void transform(const CDRTransform &trafo) override;
331 std::unique_ptr<CDRPathElement> clone() override;
332 private:
333 double m_rx;
334 double m_ry;
335 double m_rotation;
336 bool m_largeArc;
337 bool m_sweep;
338 double m_x;
339 double m_y;
340 };
341
342 class CDRClosePathElement : public CDRPathElement
343 {
344 public:
345 CDRClosePathElement() {}
346 ~CDRClosePathElement() override {}
347 void writeOut(librevenge::RVNGPropertyListVector &vec) const override;
348 void transform(const CDRTransforms &trafos) override;
349 void transform(const CDRTransform &trafo) override;
350 std::unique_ptr<CDRPathElement> clone() override;
351 };
352
353 void CDRMoveToElement::writeOut(librevenge::RVNGPropertyListVector &vec) const
354 {
355 librevenge::RVNGPropertyList node;
356 node.insert("librevenge:path-action", "M");
357 node.insert("svg:x", m_x);
358 node.insert("svg:y", m_y);
359 vec.append(node);
360 }
361
362 void CDRMoveToElement::transform(const CDRTransforms &trafos)
363 {
364 trafos.applyToPoint(m_x,m_y);
365 }
366
367 void CDRMoveToElement::transform(const CDRTransform &trafo)
368 {
369 trafo.applyToPoint(m_x,m_y);
370 }
371
372 std::unique_ptr<CDRPathElement> CDRMoveToElement::clone()
373 {
374 return make_unique<CDRMoveToElement>(m_x, m_y);
375 }
376
377 void CDRLineToElement::writeOut(librevenge::RVNGPropertyListVector &vec) const
378 {
379 librevenge::RVNGPropertyList node;
380 node.insert("librevenge:path-action", "L");
381 node.insert("svg:x", m_x);
382 node.insert("svg:y", m_y);
383 vec.append(node);
384 }
385
386 void CDRLineToElement::transform(const CDRTransforms &trafos)
387 {
388 trafos.applyToPoint(m_x,m_y);
389 }
390
391 void CDRLineToElement::transform(const CDRTransform &trafo)
392 {
393 trafo.applyToPoint(m_x,m_y);
394 }
395
396 std::unique_ptr<CDRPathElement> CDRLineToElement::clone()
397 {
398 return make_unique<CDRLineToElement>(m_x, m_y);
399 }
400
401 void CDRCubicBezierToElement::writeOut(librevenge::RVNGPropertyListVector &vec) const
402 {
403 librevenge::RVNGPropertyList node;
404 node.insert("librevenge:path-action", "C");
405 node.insert("svg:x1", m_x1);
406 node.insert("svg:y1", m_y1);
407 node.insert("svg:x2", m_x2);
408 node.insert("svg:y2", m_y2);
409 node.insert("svg:x", m_x);
410 node.insert("svg:y", m_y);
411 vec.append(node);
412 }
413
414 void CDRCubicBezierToElement::transform(const CDRTransforms &trafos)
415 {
416 trafos.applyToPoint(m_x1,m_y1);
417 trafos.applyToPoint(m_x2,m_y2);
418 trafos.applyToPoint(m_x,m_y);
419 }
420
421 void CDRCubicBezierToElement::transform(const CDRTransform &trafo)
422 {
423 trafo.applyToPoint(m_x1,m_y1);
424 trafo.applyToPoint(m_x2,m_y2);
425 trafo.applyToPoint(m_x,m_y);
426 }
427
428 std::unique_ptr<CDRPathElement> CDRCubicBezierToElement::clone()
429 {
430 return make_unique<CDRCubicBezierToElement>(m_x1, m_y1, m_x2, m_y2, m_x, m_y);
431 }
432
433 void CDRQuadraticBezierToElement::writeOut(librevenge::RVNGPropertyListVector &vec) const
434 {
435 librevenge::RVNGPropertyList node;
436 node.insert("librevenge:path-action", "Q");
437 node.insert("svg:x1", m_x1);
438 node.insert("svg:y1", m_y1);
439 node.insert("svg:x", m_x);
440 node.insert("svg:y", m_y);
441 vec.append(node);
442 }
443
444 void CDRQuadraticBezierToElement::transform(const CDRTransforms &trafos)
445 {
446 trafos.applyToPoint(m_x1,m_y1);
447 trafos.applyToPoint(m_x,m_y);
448 }
449
450 void CDRQuadraticBezierToElement::transform(const CDRTransform &trafo)
451 {
452 trafo.applyToPoint(m_x1,m_y1);
453 trafo.applyToPoint(m_x,m_y);
454 }
455
456 std::unique_ptr<CDRPathElement> CDRQuadraticBezierToElement::clone()
457 {
458 return make_unique<CDRQuadraticBezierToElement>(m_x1, m_y1, m_x, m_y);
459 }
460
461 #define CDR_SPLINE_DEGREE 3
462
463 unsigned CDRSplineToElement::knot(unsigned i) const
464 {
465 /* Emulates knot vector of an uniform B-Spline of degree 3 */
466 if (i < CDR_SPLINE_DEGREE)
467 return 0;
468 if (i > m_points.size())
469 return (unsigned)(m_points.size() - CDR_SPLINE_DEGREE);
470 return i - CDR_SPLINE_DEGREE;
471 }
472
473 void CDRSplineToElement::writeOut(librevenge::RVNGPropertyListVector &vec) const
474 {
475 librevenge::RVNGPropertyList node;
476
477 #if DEBUG_SPLINES
478 /* Code for visual debugging of the spline decomposition */
479
480 node.insert("librevenge:path-action", "M");
481 node.insert("svg:x", m_points[0].first);
482 node.insert("svg:y", m_points[0].second);
483 vec.append(node);
484
485 for (unsigned j = 0; j < m_points.size(); ++j)
486 {
487 node.insert("librevenge:path-action", "L");
488 node.insert("svg:x", m_points[j].first);
489 node.insert("svg:y", m_points[j].second);
490 vec.append(node);
491 }
492 #endif
493
494 node.insert("librevenge:path-action", "M");
495 node.insert("svg:x", m_points[0].first);
496 node.insert("svg:y", m_points[0].second);
497 vec.append(node);
498
499 /* Decomposition of a spline of 3rd degree into Bezier segments
500 * adapted from the algorithm DecomposeCurve (Les Piegl, Wayne Tiller:
501 * The NURBS Book, 2nd Edition, 1997
502 */
503
504 unsigned long m = m_points.size() + CDR_SPLINE_DEGREE + 1;
505 unsigned a = CDR_SPLINE_DEGREE;
506 unsigned b = CDR_SPLINE_DEGREE + 1;
507 std::vector< std::pair<double, double> > Qw(CDR_SPLINE_DEGREE+1), NextQw(CDR_SPLINE_DEGREE+1);
508 unsigned i = 0;
509 for (; i <= CDR_SPLINE_DEGREE; i++)
510 Qw[i] = m_points[i];
511 while (b < m)
512 {
513 i = b;
514 while (b < m && knot(b+1) == knot(b))
515 b++;
516 unsigned mult = b - i + 1;
517 if (mult < CDR_SPLINE_DEGREE)
518 {
519 auto numer = (double)(knot(b) - knot(a));
520 unsigned j = CDR_SPLINE_DEGREE;
521 std::map<unsigned, double> alphas;
522 for (; j >mult; j--)
523 alphas[j-mult-1] = numer/double(knot(a+j)-knot(a));
524 unsigned r = CDR_SPLINE_DEGREE - mult;
525 for (j=1; j<=r; j++)
526 {
527 unsigned save = r - j;
528 unsigned s = mult+j;
529 for (unsigned k = CDR_SPLINE_DEGREE; k>=s; k--)
530 {
531 double alpha = alphas[k-s];
532 Qw[k].first = alpha*Qw[k].first + (1.0-alpha)*Qw[k-1].first;
533 Qw[k].second = alpha*Qw[k].second + (1.0-alpha)*Qw[k-1].second;
534 }
535 if (b < m)
536 {
537 NextQw[save].first = Qw[CDR_SPLINE_DEGREE].first;
538 NextQw[save].second = Qw[CDR_SPLINE_DEGREE].second;
539 }
540 }
541 }
542 // Pass the segment to the path
543
544 node.clear();
545 node.insert("librevenge:path-action", "C");
546 node.insert("svg:x1", Qw[1].first);
547 node.insert("svg:y1", Qw[1].second);
548 node.insert("svg:x2", Qw[2].first);
549 node.insert("svg:y2", Qw[2].second);
550 node.insert("svg:x", Qw[3].first);
551 node.insert("svg:y", Qw[3].second);
552
553 vec.append(node);
554
555 std::swap(Qw, NextQw);
556
557 if (b < m)
558 {
559 for (i=CDR_SPLINE_DEGREE-mult; i <= CDR_SPLINE_DEGREE; i++)
560 {
561 Qw[i].first = m_points[b-CDR_SPLINE_DEGREE+i].first;
562 Qw[i].second = m_points[b-CDR_SPLINE_DEGREE+i].second;
563 }
564 a = b;
565 b++;
566 }
567 }
568 }
569
570 void CDRSplineToElement::transform(const CDRTransforms &trafos)
571 {
572 for (auto &point : m_points)
573 trafos.applyToPoint(point.first, point.second);
574 }
575
576 void CDRSplineToElement::transform(const CDRTransform &trafo)
577 {
578 for (auto &point : m_points)
579 trafo.applyToPoint(point.first, point.second);
580 }
581
582 std::unique_ptr<CDRPathElement> CDRSplineToElement::clone()
583 {
584 return libcdr::make_unique<CDRSplineToElement>(m_points);
585 }
586
587 void CDRArcToElement::writeOut(librevenge::RVNGPropertyListVector &vec) const
588 {
589 librevenge::RVNGPropertyList node;
590 node.insert("librevenge:path-action", "A");
591 node.insert("svg:rx", m_rx);
592 node.insert("svg:ry", m_ry);
593 node.insert("librevenge:rotate", m_rotation * 180 / M_PI, librevenge::RVNG_GENERIC);
594 node.insert("librevenge:large-arc", m_largeArc);
595 node.insert("librevenge:sweep", m_sweep);
596 node.insert("svg:x", m_x);
597 node.insert("svg:y", m_y);
598 vec.append(node);
599 }
600
601 void CDRArcToElement::transform(const CDRTransforms &trafos)
602 {
603 trafos.applyToArc(m_rx, m_ry, m_rotation, m_sweep, m_x, m_y);
604 }
605
606 void CDRArcToElement::transform(const CDRTransform &trafo)
607 {
608 trafo.applyToArc(m_rx, m_ry, m_rotation, m_sweep, m_x, m_y);
609 }
610
611 std::unique_ptr<CDRPathElement> CDRArcToElement::clone()
612 {
613 return make_unique<CDRArcToElement>(m_rx, m_ry, m_rotation, m_largeArc, m_sweep, m_x, m_y);
614 }
615
616 void CDRClosePathElement::transform(const CDRTransforms &)
617 {
618 }
619
620 void CDRClosePathElement::transform(const CDRTransform &)
621 {
622 }
623
624 std::unique_ptr<CDRPathElement> CDRClosePathElement::clone()
625 {
626 return make_unique<CDRClosePathElement>();
627 }
628
629 void CDRClosePathElement::writeOut(librevenge::RVNGPropertyListVector &vec) const
630 {
631 librevenge::RVNGPropertyList node;
632 node.insert("librevenge:path-action", "Z");
633 vec.append(node);
634 }
635
636 void CDRPath::appendMoveTo(double x, double y)
637 {
638 m_elements.push_back(make_unique<CDRMoveToElement>(x, y));
639 }
640
641 void CDRPath::appendLineTo(double x, double y)
642 {
643 m_elements.push_back(make_unique<CDRLineToElement>(x, y));
644 }
645
646 void CDRPath::appendCubicBezierTo(double x1, double y1, double x2, double y2, double x, double y)
647 {
648 m_elements.push_back(make_unique<CDRCubicBezierToElement>(x1, y1, x2, y2, x, y));
649 }
650
651 void CDRPath::appendQuadraticBezierTo(double x1, double y1, double x, double y)
652 {
653 m_elements.push_back(make_unique<CDRQuadraticBezierToElement>(x1, y1, x, y));
654 }
655
656 void CDRPath::appendArcTo(double rx, double ry, double rotation, bool longAngle, bool sweep, double x, double y)
657 {
658 m_elements.push_back(make_unique<CDRArcToElement>(rx, ry, rotation, longAngle, sweep, x, y));
659 }
660
661 void CDRPath::appendSplineTo(const std::vector<std::pair<double, double> > &points)
662 {
663 m_elements.push_back(libcdr::make_unique<CDRSplineToElement>(points));
664 }
665
666 void CDRPath::appendClosePath()
667 {
668 m_elements.push_back(make_unique<CDRClosePathElement>());
669 m_isClosed = true;
670 }
671
672 CDRPath::CDRPath(const CDRPath &path) : m_elements(), m_isClosed(false)
673 {
674 appendPath(path);
675 m_isClosed = path.isClosed();
676 }
677
678 CDRPath &CDRPath::operator=(const CDRPath &path)
679 {
680 // Check for self-assignment
681 if (this == &path)
682 return *this;
683 clear();
684 appendPath(path);
685 m_isClosed = path.isClosed();
686 return *this;
687 }
688
689
690 CDRPath::~CDRPath()
691 {
692 }
693
694 void CDRPath::appendPath(const CDRPath &path)
695 {
696 for (const auto &element : path.m_elements)
697 m_elements.push_back(element->clone());
698 }
699
700 void CDRPath::writeOut(librevenge::RVNGPropertyListVector &vec) const
701 {
702 bool wasZ = true;
703 for (const auto &element : m_elements)
704 {
705 if (dynamic_cast<CDRClosePathElement *>(element.get()))
706 {
707 if (!wasZ)
708 {
709 element->writeOut(vec);
710 wasZ = true;
711 }
712 }
713 else
714 {
715 element->writeOut(vec);
716 wasZ = false;
717 }
718 }
719 }
720
721 void CDRPath::writeOut(librevenge::RVNGString &path, librevenge::RVNGString &viewBox, double &width) const
722 {
723 librevenge::RVNGPropertyListVector vec;
724 writeOut(vec);
725 if (vec.count() == 0)
726 return;
727 // This must be a mistake and we do not want to crash lower
728 if (vec[0]["librevenge:path-action"]->getStr() == "Z")
729 return;
730
731 // try to find the bounding box
732 bool isFirstPoint = true;
733
734 double px = 0.0, py = 0.0, qx = 0.0, qy = 0.0;
735 double lastX = 0.0;
736 double lastY = 0.0;
737
738 for (unsigned long k = 0; k < vec.count(); ++k)
739 {
740 if (!vec[k]["svg:x"] || !vec[k]["svg:y"])
741 continue;
742 if (isFirstPoint)
743 {
744 px = vec[k]["svg:x"]->getDouble();
745 py = vec[k]["svg:y"]->getDouble();
746 qx = px;
747 qy = py;
748 lastX = px;
749 lastY = py;
750 isFirstPoint = false;
751 }
752 px = (px > vec[k]["svg:x"]->getDouble()) ? vec[k]["svg:x"]->getDouble() : px;
753 py = (py > vec[k]["svg:y"]->getDouble()) ? vec[k]["svg:y"]->getDouble() : py;
754 qx = (qx < vec[k]["svg:x"]->getDouble()) ? vec[k]["svg:x"]->getDouble() : qx;
755 qy = (qy < vec[k]["svg:y"]->getDouble()) ? vec[k]["svg:y"]->getDouble() : qy;
756
757 double xmin, xmax, ymin, ymax;
758
759 if (vec[k]["librevenge:path-action"]->getStr() == "C")
760 {
761 getCubicBezierBBox(lastX, lastY, vec[k]["svg:x1"]->getDouble(), vec[k]["svg:y1"]->getDouble(),
762 vec[k]["svg:x2"]->getDouble(), vec[k]["svg:y2"]->getDouble(),
763 vec[k]["svg:x"]->getDouble(), vec[k]["svg:y"]->getDouble(), xmin, ymin, xmax, ymax);
764
765 px = (px > xmin ? xmin : px);
766 py = (py > ymin ? ymin : py);
767 qx = (qx < xmax ? xmax : qx);
768 qy = (qy < ymax ? ymax : qy);
769 }
770 if (vec[k]["librevenge:path-action"]->getStr() == "Q")
771 {
772 getQuadraticBezierBBox(lastX, lastY, vec[k]["svg:x1"]->getDouble(), vec[k]["svg:y1"]->getDouble(),
773 vec[k]["svg:x"]->getDouble(), vec[k]["svg:y"]->getDouble(), xmin, ymin, xmax, ymax);
774
775 px = (px > xmin ? xmin : px);
776 py = (py > ymin ? ymin : py);
777 qx = (qx < xmax ? xmax : qx);
778 qy = (qy < ymax ? ymax : qy);
779 }
780 if (vec[k]["librevenge:path-action"]->getStr() == "A")
781 {
782 getEllipticalArcBBox(lastX, lastY, vec[k]["svg:rx"]->getDouble(), vec[k]["svg:ry"]->getDouble(),
783 vec[k]["librevenge:rotate"] ? vec[k]["librevenge:rotate"]->getDouble() : 0.0,
784 vec[k]["librevenge:large-arc"] ? vec[k]["librevenge:large-arc"]->getInt() : 1,
785 vec[k]["librevenge:sweep"] ? vec[k]["librevenge:sweep"]->getInt() : 1,
786 vec[k]["svg:x"]->getDouble(), vec[k]["svg:y"]->getDouble(), xmin, ymin, xmax, ymax);
787
788 px = (px > xmin ? xmin : px);
789 py = (py > ymin ? ymin : py);
790 qx = (qx < xmax ? xmax : qx);
791 qy = (qy < ymax ? ymax : qy);
792 }
793 lastX = vec[k]["svg:x"]->getDouble();
794 lastY = vec[k]["svg:y"]->getDouble();
795 }
796
797
798 width = qy - py;
799 viewBox.sprintf("%i %i %i %i", 0, 0, (int)(2540*(qx - px)), (int)(2540*(qy - py)));
800
801 for (unsigned long i = 0; i < vec.count(); ++i)
802 {
803 librevenge::RVNGString sElement;
804 if (vec[i]["librevenge:path-action"]->getStr() == "M")
805 {
806 // 2540 is 2.54*1000, 2.54 in = 1 inch
807 sElement.sprintf("M%i %i", (int)((vec[i]["svg:x"]->getDouble()-px)*2540),
808 (int)((vec[i]["svg:y"]->getDouble()-py)*2540));
809 path.append(sElement);
810 }
811 else if (vec[i]["librevenge:path-action"]->getStr() == "L")
812 {
813 sElement.sprintf("L%i %i", (int)((vec[i]["svg:x"]->getDouble()-px)*2540),
814 (int)((vec[i]["svg:y"]->getDouble()-py)*2540));
815 path.append(sElement);
816 }
817 else if (vec[i]["librevenge:path-action"]->getStr() == "C")
818 {
819 sElement.sprintf("C%i %i %i %i %i %i", (int)((vec[i]["svg:x1"]->getDouble()-px)*2540),
820 (int)((vec[i]["svg:y1"]->getDouble()-py)*2540), (int)((vec[i]["svg:x2"]->getDouble()-px)*2540),
821 (int)((vec[i]["svg:y2"]->getDouble()-py)*2540), (int)((vec[i]["svg:x"]->getDouble()-px)*2540),
822 (int)((vec[i]["svg:y"]->getDouble()-py)*2540));
823 path.append(sElement);
824 }
825 else if (vec[i]["librevenge:path-action"]->getStr() == "Q")
826 {
827 sElement.sprintf("Q%i %i %i %i", (int)((vec[i]["svg:x1"]->getDouble()-px)*2540),
828 (int)((vec[i]["svg:y1"]->getDouble()-py)*2540), (int)((vec[i]["svg:x"]->getDouble()-px)*2540),
829 (int)((vec[i]["svg:y"]->getDouble()-py)*2540));
830 path.append(sElement);
831 }
832 else if (vec[i]["librevenge:path-action"]->getStr() == "A")
833 {
834 sElement.sprintf("A%i %i %i %i %i %i %i", (int)((vec[i]["svg:rx"]->getDouble())*2540),
835 (int)((vec[i]["svg:ry"]->getDouble())*2540), (vec[i]["librevenge:rotate"] ? vec[i]["librevenge:rotate"]->getInt() : 0),
836 (vec[i]["librevenge:large-arc"] ? vec[i]["librevenge:large-arc"]->getInt() : 1),
837 (vec[i]["librevenge:sweep"] ? vec[i]["librevenge:sweep"]->getInt() : 1),
838 (int)((vec[i]["svg:x"]->getDouble()-px)*2540), (int)((vec[i]["svg:y"]->getDouble()-py)*2540));
839 path.append(sElement);
840 }
841 else if (vec[i]["librevenge:path-action"]->getStr() == "Z")
842 {
843 path.append(" Z");
844 }
845 }
846 }
847
848 void CDRPath::transform(const CDRTransforms &trafos)
849 {
850 for (auto &element : m_elements)
851 element->transform(trafos);
852 }
853
854 void CDRPath::transform(const CDRTransform &trafo)
855 {
856 for (auto &element : m_elements)
857 element->transform(trafo);
858 }
859
860 std::unique_ptr<CDRPathElement> CDRPath::clone()
861 {
862 return make_unique<CDRPath>(*this);
863 }
864
865 void CDRPath::clear()
866 {
867 m_elements.clear();
868 m_isClosed = false;
869 }
870
871 bool CDRPath::empty() const
872 {
873 return m_elements.empty();
874 }
875
876 bool CDRPath::isClosed() const
877 {
878 return m_isClosed;
879 }
880
881 }
882
883 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */