1 #define __CURVE_C__
3 /** \file
4 * Routines for SPCurve and for NArtBpath arrays in general.
5 */
7 /*
8 * Author:
9 * Lauris Kaplinski <lauris@kaplinski.com>
10 *
11 * Copyright (C) 2000 Lauris Kaplinski
12 * Copyright (C) 2000-2001 Ximian, Inc.
13 * Copyright (C) 2002 Lauris Kaplinski
14 * Copyright (C) 2008 Johan Engelen
15 *
16 * Released under GNU GPL
17 */
19 #include "display/curve.h"
21 #include <string.h>
22 #include <glib/gmem.h>
23 #include "libnr/nr-point.h"
24 #include "libnr/nr-rect.h"
25 #include <libnr/n-art-bpath.h>
26 #include <libnr/nr-point-matrix-ops.h>
27 #include <libnr/nr-translate-ops.h>
28 #include <cstring>
29 #include <string>
31 static unsigned sp_bpath_length(NArtBpath const bpath[]);
32 static bool sp_bpath_closed(NArtBpath const bpath[]);
34 /* Constructors */
36 /**
37 * The returned curve's state is as if SPCurve::reset has just been called on it.
38 * \param length Initial number of NArtBpath elements allocated for bpath (including NR_END
39 * element).
40 */
41 SPCurve::SPCurve(guint length)
42 : _end(0),
43 _length(length),
44 _substart(0),
45 _hascpt(false),
46 _posSet(false),
47 _moving(false),
48 _closed(false),
49 _refcount(1),
50 _bpath(NULL)
51 {
52 if (length <= 0) {
53 g_error("SPCurve::SPCurve called with invalid length parameter");
54 throw;
55 }
57 _bpath = g_new(NArtBpath, length);
58 _bpath->code = NR_END;
59 }
61 SPCurve *
62 SPCurve::new_from_foreign_bpath(NArtBpath const *bpath)
63 {
64 g_return_val_if_fail(bpath != NULL, NULL);
66 NArtBpath *new_bpath;
67 unsigned const len = sp_bpath_length(bpath);
68 new_bpath = g_new(NArtBpath, len);
69 memcpy(new_bpath, bpath, len * sizeof(NArtBpath));
71 SPCurve *curve = new SPCurve();
73 curve->_bpath = new_bpath;
74 curve->_length = len;
75 curve->_end = curve->_length - 1;
76 gint i = curve->_end;
77 for (; i > 0; i--)
78 if ((curve->_bpath[i].code == NR_MOVETO) ||
79 (curve->_bpath[i].code == NR_MOVETO_OPEN))
80 break;
81 curve->_substart = i;
82 curve->_closed = sp_bpath_closed(new_bpath);
84 return curve;
85 }
87 /**
88 * Convert NArtBpath object to SPCurve object.
89 *
90 * \return new SPCurve, or NULL if the curve was not created for some reason.
91 */
92 SPCurve *
93 SPCurve::new_from_bpath(NArtBpath *bpath)
94 {
95 g_return_val_if_fail(bpath != NULL, NULL);
97 SPCurve *curve = SPCurve::new_from_foreign_bpath(bpath);
98 g_free(bpath);
99 return curve;
100 }
102 SPCurve *
103 SPCurve::new_from_rect(NR::Maybe<NR::Rect> const &rect)
104 {
105 g_return_val_if_fail(rect, NULL);
107 SPCurve *c = new SPCurve();
109 NR::Point p = rect->corner(0);
110 c->moveto(p);
112 for (int i=3; i>=0; i--) {
113 c->lineto(rect->corner(i));
114 }
115 c->closepath_current();
117 return c;
118 }
120 SPCurve::~SPCurve()
121 {
122 if (_bpath) {
123 g_free(_bpath);
124 _bpath = NULL;
125 }
126 }
128 /* Methods */
130 /**
131 * Frees old path and sets new path
132 * This does not copy the bpath, so the new_bpath should not be deleted by caller
133 */
134 void
135 SPCurve::set_bpath(NArtBpath * new_bpath)
136 {
137 if (new_bpath && new_bpath != _bpath) { // FIXME, add function to SPCurve to change bpath? or a copy function?
138 if (_bpath) {
139 g_free(_bpath); //delete old bpath
140 }
141 _bpath = new_bpath;
142 }
143 }
145 /**
146 * Get pointer to bpath data. Don't keep this reference too long, because the path might change by another function.
147 */
148 NArtBpath const *
149 SPCurve::get_bpath() const
150 {
151 return _bpath;
152 };
153 /*
154 NArtBpath *
155 SPCurve::get_bpath()
156 {
157 return _bpath;
158 };
159 */
161 /**
162 * Increase _refcount of curve.
163 *
164 * \todo should this be shared with other refcounting code?
165 */
166 SPCurve *
167 SPCurve::ref()
168 {
169 g_return_val_if_fail(this != NULL, NULL);
171 _refcount += 1;
173 return this;
174 }
176 /**
177 * Decrease refcount of curve, with possible destruction.
178 *
179 * \todo should this be shared with other refcounting code?
180 */
181 SPCurve *
182 SPCurve::unref()
183 {
184 g_return_val_if_fail(this != NULL, NULL);
186 _refcount -= 1;
188 if (_refcount < 1) {
189 if (_bpath) {
190 g_free(_bpath);
191 _bpath = NULL;
192 }
193 delete this;
194 }
196 return NULL;
197 }
199 /**
200 * Add space for more paths in curve.
201 */
202 void
203 SPCurve::ensure_space(guint space)
204 {
205 g_return_if_fail(this != NULL);
206 g_return_if_fail(space > 0);
208 if (_end + space < _length)
209 return;
211 if (space < SP_CURVE_LENSTEP)
212 space = SP_CURVE_LENSTEP;
214 _bpath = g_renew(NArtBpath, _bpath, _length + space);
216 _length += space;
217 }
219 /**
220 * Create new curve from its own bpath array.
221 */
222 SPCurve *
223 SPCurve::copy() const
224 {
225 g_return_val_if_fail(this != NULL, NULL);
227 return SPCurve::new_from_foreign_bpath(_bpath);
228 }
230 /**
231 * Return new curve that is the concatenation of all curves in list.
232 */
233 SPCurve *
234 SPCurve::concat(GSList const *list)
235 {
236 g_return_val_if_fail(list != NULL, NULL);
238 gint length = 0;
240 for (GSList const *l = list; l != NULL; l = l->next) {
241 SPCurve *c = (SPCurve *) l->data;
242 length += c->_end;
243 }
245 SPCurve *new_curve = new SPCurve(length + 1);
247 NArtBpath *bp = new_curve->_bpath;
249 for (GSList const *l = list; l != NULL; l = l->next) {
250 SPCurve *c = (SPCurve *) l->data;
251 memcpy(bp, c->_bpath, c->_end * sizeof(NArtBpath));
252 bp += c->_end;
253 }
255 bp->code = NR_END;
257 new_curve->_end = length;
258 gint i;
259 for (i = new_curve->_end; i > 0; i--) {
260 if ((new_curve->_bpath[i].code == NR_MOVETO) ||
261 (new_curve->_bpath[i].code == NR_MOVETO_OPEN) )
262 break;
263 }
265 new_curve->_substart = i;
267 return new_curve;
268 }
270 /**
271 * Returns a list of new curves corresponding to the subpaths in \a curve.
272 */
273 GSList *
274 SPCurve::split() const
275 {
276 g_return_val_if_fail(this != NULL, NULL);
278 guint p = 0;
279 GSList *l = NULL;
281 while (p < _end) {
282 gint i = 1;
283 while ((_bpath[p + i].code == NR_LINETO) ||
284 (_bpath[p + i].code == NR_CURVETO))
285 i++;
286 SPCurve *new_curve = new SPCurve(i + 1);
287 memcpy(new_curve->_bpath, _bpath + p, i * sizeof(NArtBpath));
288 new_curve->_end = i;
289 new_curve->_bpath[i].code = NR_END;
290 new_curve->_substart = 0;
291 new_curve->_closed = (new_curve->_bpath->code == NR_MOVETO);
292 new_curve->_hascpt = (new_curve->_bpath->code == NR_MOVETO_OPEN);
293 l = g_slist_prepend(l, new_curve);
294 p += i;
295 }
297 return l;
298 }
300 /**
301 * Transform all paths in curve, template helper.
302 */
303 template<class M>
304 static void
305 tmpl_curve_transform(SPCurve *const curve, M const &m)
306 {
307 g_return_if_fail(curve != NULL);
309 for (guint i = 0; i < curve->_end; i++) {
310 NArtBpath *p = curve->_bpath + i;
311 switch (p->code) {
312 case NR_MOVETO:
313 case NR_MOVETO_OPEN:
314 case NR_LINETO: {
315 p->setC(3, p->c(3) * m);
316 break;
317 }
318 case NR_CURVETO:
319 for (unsigned i = 1; i <= 3; ++i) {
320 p->setC(i, p->c(i) * m);
321 }
322 break;
323 default:
324 g_warning("Illegal pathcode %d", p->code);
325 break;
326 }
327 }
328 }
330 /**
331 * Transform all paths in curve using matrix.
332 */
333 void
334 SPCurve::transform(NR::Matrix const &m)
335 {
336 tmpl_curve_transform<NR::Matrix>(this, m);
337 }
339 /**
340 * Transform all paths in curve using NR::translate.
341 */
342 void
343 SPCurve::transform(NR::translate const &m)
344 {
345 tmpl_curve_transform<NR::translate>(this, m);
346 }
348 /**
349 * Set curve to empty curve.
350 */
351 void
352 SPCurve::reset()
353 {
354 g_return_if_fail(this != NULL);
356 _bpath->code = NR_END;
357 _end = 0;
358 _substart = 0;
359 _hascpt = false;
360 _posSet = false;
361 _moving = false;
362 _closed = false;
363 }
365 /* Several consecutive movetos are ALLOWED */
367 /**
368 * Calls SPCurve::moveto() with point made of given coordinates.
369 */
370 void
371 SPCurve::moveto(gdouble x, gdouble y)
372 {
373 moveto(NR::Point(x, y));
374 }
376 /**
377 * Perform a moveto to a point, thus starting a new subpath.
378 */
379 void
380 SPCurve::moveto(NR::Point const &p)
381 {
382 g_return_if_fail(this != NULL);
383 g_return_if_fail(!_moving);
385 _substart = _end;
386 _hascpt = true;
387 _posSet = true;
388 _movePos = p;
389 }
391 /**
392 * Calls SPCurve::lineto() with a point's coordinates.
393 */
394 void
395 SPCurve::lineto(NR::Point const &p)
396 {
397 lineto(p[NR::X], p[NR::Y]);
398 }
400 /**
401 * Adds a line to the current subpath.
402 */
403 void
404 SPCurve::lineto(gdouble x, gdouble y)
405 {
406 g_return_if_fail(this != NULL);
407 g_return_if_fail(_hascpt);
409 if (_moving) {
410 /* fix endpoint */
411 g_return_if_fail(!_posSet);
412 g_return_if_fail(_end > 1);
413 NArtBpath *bp = _bpath + _end - 1;
414 g_return_if_fail(bp->code == NR_LINETO);
415 bp->x3 = x;
416 bp->y3 = y;
417 _moving = false;
418 return;
419 }
421 if (_posSet) {
422 /* start a new segment */
423 ensure_space(2);
424 NArtBpath *bp = _bpath + _end;
425 bp->code = NR_MOVETO_OPEN;
426 bp->setC(3, _movePos);
427 bp++;
428 bp->code = NR_LINETO;
429 bp->x3 = x;
430 bp->y3 = y;
431 bp++;
432 bp->code = NR_END;
433 _end += 2;
434 _posSet = false;
435 _closed = false;
436 return;
437 }
439 /* add line */
441 g_return_if_fail(_end > 1);
442 ensure_space(1);
443 NArtBpath *bp = _bpath + _end;
444 bp->code = NR_LINETO;
445 bp->x3 = x;
446 bp->y3 = y;
447 bp++;
448 bp->code = NR_END;
449 _end++;
450 }
452 /// Unused
453 void
454 SPCurve::lineto_moving(gdouble x, gdouble y)
455 {
456 g_return_if_fail(this != NULL);
457 g_return_if_fail(_hascpt);
459 if (_moving) {
460 /* change endpoint */
461 g_return_if_fail(!_posSet);
462 g_return_if_fail(_end > 1);
463 NArtBpath *bp = _bpath + _end - 1;
464 g_return_if_fail(bp->code == NR_LINETO);
465 bp->x3 = x;
466 bp->y3 = y;
467 return;
468 }
470 if (_posSet) {
471 /* start a new segment */
472 ensure_space(2);
473 NArtBpath *bp = _bpath + _end;
474 bp->code = NR_MOVETO_OPEN;
475 bp->setC(3, _movePos);
476 bp++;
477 bp->code = NR_LINETO;
478 bp->x3 = x;
479 bp->y3 = y;
480 bp++;
481 bp->code = NR_END;
482 _end += 2;
483 _posSet = false;
484 _moving = true;
485 _closed = false;
486 return;
487 }
489 /* add line */
491 g_return_if_fail(_end > 1);
492 ensure_space(1);
493 NArtBpath *bp = _bpath + _end;
494 bp->code = NR_LINETO;
495 bp->x3 = x;
496 bp->y3 = y;
497 bp++;
498 bp->code = NR_END;
499 _end++;
500 _moving = true;
501 }
503 /**
504 * Calls SPCurve::curveto() with coordinates of three points.
505 */
506 void
507 SPCurve::curveto(NR::Point const &p0, NR::Point const &p1, NR::Point const &p2)
508 {
509 using NR::X;
510 using NR::Y;
511 curveto( p0[X], p0[Y],
512 p1[X], p1[Y],
513 p2[X], p2[Y] );
514 }
516 /**
517 * Adds a bezier segment to the current subpath.
518 */
519 void
520 SPCurve::curveto(gdouble x0, gdouble y0, gdouble x1, gdouble y1, gdouble x2, gdouble y2)
521 {
522 g_return_if_fail(this != NULL);
523 g_return_if_fail(_hascpt);
524 g_return_if_fail(!_moving);
526 if (_posSet) {
527 /* start a new segment */
528 ensure_space(2);
529 NArtBpath *bp = _bpath + _end;
530 bp->code = NR_MOVETO_OPEN;
531 bp->setC(3, _movePos);
532 bp++;
533 bp->code = NR_CURVETO;
534 bp->x1 = x0;
535 bp->y1 = y0;
536 bp->x2 = x1;
537 bp->y2 = y1;
538 bp->x3 = x2;
539 bp->y3 = y2;
540 bp++;
541 bp->code = NR_END;
542 _end += 2;
543 _posSet = false;
544 _closed = false;
545 return;
546 }
548 /* add curve */
550 g_return_if_fail(_end > 1);
551 ensure_space(1);
552 NArtBpath *bp = _bpath + _end;
553 bp->code = NR_CURVETO;
554 bp->x1 = x0;
555 bp->y1 = y0;
556 bp->x2 = x1;
557 bp->y2 = y1;
558 bp->x3 = x2;
559 bp->y3 = y2;
560 bp++;
561 bp->code = NR_END;
562 _end++;
563 }
565 /**
566 * Close current subpath by possibly adding a line between start and end.
567 */
568 void
569 SPCurve::closepath()
570 {
571 g_return_if_fail(this != NULL);
572 g_return_if_fail(_hascpt);
573 g_return_if_fail(!_posSet);
574 g_return_if_fail(!_moving);
575 g_return_if_fail(!_closed);
576 /* We need at least moveto, curveto, end. */
577 g_return_if_fail(_end - _substart > 1);
579 {
580 NArtBpath *bs = _bpath + _substart;
581 NArtBpath *be = _bpath + _end - 1;
583 if (bs->c(3) != be->c(3)) {
584 lineto(bs->c(3));
585 bs = _bpath + _substart;
586 }
588 bs->code = NR_MOVETO;
589 }
590 _closed = true;
592 for (NArtBpath const *bp = _bpath; bp->code != NR_END; bp++) {
593 /** \todo
594 * effic: Maintain a count of NR_MOVETO_OPEN's (e.g. instead of
595 * the closed boolean).
596 */
597 if (bp->code == NR_MOVETO_OPEN) {
598 _closed = false;
599 break;
600 }
601 }
603 _hascpt = false;
604 }
606 /** Like SPCurve::closepath() but sets the end point of the current
607 command to the subpath start point instead of adding a new lineto.
609 Used for freehand drawing when the user draws back to the start point.
610 **/
611 void
612 SPCurve::closepath_current()
613 {
614 g_return_if_fail(this != NULL);
615 g_return_if_fail(_hascpt);
616 g_return_if_fail(!_posSet);
617 g_return_if_fail(!_closed);
618 /* We need at least moveto, curveto, end. */
619 g_return_if_fail(_end - _substart > 1);
621 {
622 NArtBpath *bs = _bpath + _substart;
623 NArtBpath *be = _bpath + _end - 1;
625 be->x3 = bs->x3;
626 be->y3 = bs->y3;
628 bs->code = NR_MOVETO;
629 }
630 _closed = true;
632 for (NArtBpath const *bp = _bpath; bp->code != NR_END; bp++) {
633 /** \todo
634 * effic: Maintain a count of NR_MOVETO_OPEN's (e.g. instead of
635 * the closed boolean).
636 */
637 if (bp->code == NR_MOVETO_OPEN) {
638 _closed = false;
639 break;
640 }
641 }
643 _hascpt = false;
644 _moving = false;
645 }
647 /**
648 * True if no paths are in curve.
649 */
650 bool
651 SPCurve::is_empty() const
652 {
653 g_return_val_if_fail(this != NULL, TRUE);
655 return (_bpath->code == NR_END);
656 }
658 /**
659 * True iff all subpaths are closed.
660 */
661 bool
662 SPCurve::is_closed() const
663 {
664 return _closed;
665 }
667 /**
668 * Return last subpath or NULL.
669 */
670 NArtBpath *
671 SPCurve::last_bpath() const
672 {
673 g_return_val_if_fail(this != NULL, NULL);
675 if (_end == 0) {
676 return NULL;
677 }
679 return _bpath + _end - 1;
680 }
682 /**
683 * Return first subpath or NULL.
684 */
685 NArtBpath *
686 SPCurve::first_bpath() const
687 {
688 g_return_val_if_fail(this != NULL, NULL);
690 if (_end == 0) {
691 return NULL;
692 }
694 return _bpath;
695 }
697 /**
698 * Return first point of first subpath or (0,0).
699 */
700 NR::Point
701 SPCurve::first_point() const
702 {
703 NArtBpath *const bpath = first_bpath();
704 g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
705 return bpath->c(3);
706 }
708 /**
709 * Return the second point of first subpath or _movePos if curve too short.
710 */
711 NR::Point
712 SPCurve::second_point() const
713 {
714 g_return_val_if_fail(this != NULL, NR::Point(0, 0));
716 if (_end < 1) {
717 return _movePos;
718 }
720 NArtBpath *bpath = NULL;
721 if (_end < 2) {
722 bpath = _bpath;
723 } else {
724 bpath = _bpath + 1;
725 }
726 g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
727 return bpath->c(3);
728 }
730 /**
731 * Return the second-last point of last subpath or _movePos if curve too short.
732 */
733 NR::Point
734 SPCurve::penultimate_point() const
735 {
736 g_return_val_if_fail(this != NULL, NR::Point(0, 0));
738 if (_end < 2) {
739 return _movePos;
740 }
742 NArtBpath *const bpath = _bpath + _end - 2;
743 g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
744 return bpath->c(3);
745 }
747 /**
748 * Return last point of last subpath or (0,0).
749 */
750 NR::Point
751 SPCurve::last_point() const
752 {
753 NArtBpath *const bpath = last_bpath();
754 g_return_val_if_fail(bpath != NULL, NR::Point(0, 0));
755 return bpath->c(3);
756 }
758 inline static bool
759 is_moveto(NRPathcode const c)
760 {
761 return c == NR_MOVETO || c == NR_MOVETO_OPEN;
762 }
764 /**
765 * Returns a *new* \a curve but drawn in the opposite direction.
766 * Should result in the same shape, but
767 * with all its markers drawn facing the other direction.
768 **/
769 SPCurve *
770 SPCurve::create_reverse() const
771 {
772 /* We need at least moveto, curveto, end. */
773 g_return_val_if_fail(_end - _substart > 1, NULL);
775 NArtBpath const *be = _bpath + _end - 1;
777 g_assert(is_moveto(_bpath[_substart].code));
778 g_assert(is_moveto(_bpath[0].code));
779 g_assert((be+1)->code == NR_END);
781 SPCurve *new_curve = new SPCurve(_length);
782 new_curve->moveto(be->c(3));
784 for (NArtBpath const *bp = be; ; --bp) {
785 switch (bp->code) {
786 case NR_MOVETO:
787 g_assert(new_curve->_bpath[new_curve->_substart].code == NR_MOVETO_OPEN);
788 new_curve->_bpath[new_curve->_substart].code = NR_MOVETO;
789 /* FALL-THROUGH */
790 case NR_MOVETO_OPEN:
791 if (bp == _bpath) {
792 return new_curve;
793 }
794 new_curve->moveto((bp-1)->c(3));
795 break;
797 case NR_LINETO:
798 new_curve->lineto((bp-1)->c(3));
799 break;
801 case NR_CURVETO:
802 new_curve->curveto(bp->c(2), bp->c(1), (bp-1)->c(3));
803 break;
805 default:
806 g_assert_not_reached();
807 }
808 }
809 }
811 /**
812 * Append \a curve2 to \a curve.
813 */
814 void
815 SPCurve::append(SPCurve const *curve2,
816 bool use_lineto)
817 {
818 g_return_if_fail(this != NULL);
819 g_return_if_fail(curve2 != NULL);
821 if (curve2->_end < 1)
822 return;
824 NArtBpath const *bs = curve2->_bpath;
826 bool closed = this->_closed;
828 for (NArtBpath const *bp = bs; bp->code != NR_END; bp++) {
829 switch (bp->code) {
830 case NR_MOVETO_OPEN:
831 if (use_lineto && _hascpt) {
832 lineto(bp->x3, bp->y3);
833 use_lineto = FALSE;
834 } else {
835 if (closed) closepath();
836 moveto(bp->x3, bp->y3);
837 }
838 closed = false;
839 break;
841 case NR_MOVETO:
842 if (use_lineto && _hascpt) {
843 lineto(bp->x3, bp->y3);
844 use_lineto = FALSE;
845 } else {
846 if (closed) closepath();
847 moveto(bp->x3, bp->y3);
848 }
849 closed = true;
850 break;
852 case NR_LINETO:
853 lineto(bp->x3, bp->y3);
854 break;
856 case NR_CURVETO:
857 curveto(bp->x1, bp->y1, bp->x2, bp->y2, bp->x3, bp->y3);
858 break;
860 case NR_END:
861 g_assert_not_reached();
862 }
863 }
865 if (closed) {
866 closepath();
867 }
868 }
870 /**
871 * Append \a c1 to \a this with possible fusing of close endpoints.
872 */
873 SPCurve *
874 SPCurve::append_continuous(SPCurve const *c1, gdouble tolerance)
875 {
876 g_return_val_if_fail(this != NULL, NULL);
877 g_return_val_if_fail(c1 != NULL, NULL);
878 g_return_val_if_fail(!_closed, NULL);
879 g_return_val_if_fail(!c1->_closed, NULL);
881 if (c1->_end < 1) {
882 return this;
883 }
885 NArtBpath *be = last_bpath();
886 if (be) {
887 NArtBpath const *bs = c1->first_bpath();
888 if ( bs
889 && ( fabs( bs->x3 - be->x3 ) <= tolerance )
890 && ( fabs( bs->y3 - be->y3 ) <= tolerance ) )
891 {
892 /** \todo
893 * fixme: Strictly we mess in case of multisegment mixed
894 * open/close curves
895 */
896 bool closed = false;
897 for (bs = bs + 1; bs->code != NR_END; bs++) {
898 switch (bs->code) {
899 case NR_MOVETO_OPEN:
900 if (closed) closepath();
901 moveto(bs->x3, bs->y3);
902 closed = false;
903 break;
904 case NR_MOVETO:
905 if (closed) closepath();
906 moveto(bs->x3, bs->y3);
907 closed = true;
908 break;
909 case NR_LINETO:
910 lineto(bs->x3, bs->y3);
911 break;
912 case NR_CURVETO:
913 curveto(bs->x1, bs->y1, bs->x2, bs->y2, bs->x3, bs->y3);
914 break;
915 case NR_END:
916 g_assert_not_reached();
917 }
918 }
919 } else {
920 append(c1, TRUE);
921 }
922 } else {
923 append(c1, TRUE);
924 }
926 return this;
927 }
929 /**
930 * Remove last segment of curve.
931 */
932 void
933 SPCurve::backspace()
934 {
935 g_return_if_fail(this != NULL);
937 if (_end > 0) {
938 _end -= 1;
939 if (_end > 0) {
940 NArtBpath *bp = _bpath + _end - 1;
941 if ((bp->code == NR_MOVETO) ||
942 (bp->code == NR_MOVETO_OPEN) )
943 {
944 _hascpt = true;
945 _posSet = true;
946 _closed = false;
947 _movePos = bp->c(3);
948 _end -= 1;
949 }
950 }
951 _bpath[_end].code = NR_END;
952 }
953 }
955 /* Private methods */
957 /**
958 * Returns index of first NR_END bpath in array.
959 */
960 static unsigned sp_bpath_length(NArtBpath const bpath[])
961 {
962 g_return_val_if_fail(bpath != NULL, FALSE);
964 unsigned ret = 0;
965 while ( bpath[ret].code != NR_END ) {
966 ++ret;
967 }
968 ++ret;
970 return ret;
971 }
973 /**
974 * \brief
975 *
976 * \todo
977 * fixme: this is bogus -- it doesn't check for nr_moveto, which will indicate
978 * a closing of the subpath it's nonsense to talk about a path as a whole
979 * being closed, although maybe someone would want that for some other reason?
980 * Oh, also, if the bpath just ends, then it's *open*. I hope nobody is using
981 * this code for anything.
982 */
983 static bool sp_bpath_closed(NArtBpath const bpath[])
984 {
985 g_return_val_if_fail(bpath != NULL, FALSE);
987 for (NArtBpath const *bp = bpath; bp->code != NR_END; bp++) {
988 if (bp->code == NR_MOVETO_OPEN) {
989 return false;
990 }
991 }
993 return true;
994 }
996 /**
997 * Returns length of bezier segment.
998 */
999 static double
1000 bezier_len(NR::Point const &c0,
1001 NR::Point const &c1,
1002 NR::Point const &c2,
1003 NR::Point const &c3,
1004 double const threshold)
1005 {
1006 /** \todo
1007 * The SVG spec claims that a closed form exists, but for the moment I'll
1008 * use a stupid algorithm.
1009 */
1010 double const lbound = L2( c3 - c0 );
1011 double const ubound = L2( c1 - c0 ) + L2( c2 - c1 ) + L2( c3 - c2 );
1012 double ret;
1013 if ( ubound - lbound <= threshold ) {
1014 ret = .5 * ( lbound + ubound );
1015 } else {
1016 NR::Point const a1( .5 * ( c0 + c1 ) );
1017 NR::Point const b2( .5 * ( c2 + c3 ) );
1018 NR::Point const c12( .5 * ( c1 + c2 ) );
1019 NR::Point const a2( .5 * ( a1 + c12 ) );
1020 NR::Point const b1( .5 * ( c12 + b2 ) );
1021 NR::Point const midpoint( .5 * ( a2 + b1 ) );
1022 double const rec_threshold = .625 * threshold;
1023 ret = bezier_len(c0, a1, a2, midpoint, rec_threshold) + bezier_len(midpoint, b1, b2, c3, rec_threshold);
1024 if (!(lbound - 1e-2 <= ret && ret <= ubound + 1e-2)) {
1025 using NR::X; using NR::Y;
1026 g_warning("ret=%f outside of expected bounds [%f, %f] for {(%.0f %.0f) (%.0f %.0f) (%.0f %.0f) (%.0f %.0f)}",
1027 ret, lbound, ubound, c0[X], c0[Y], c1[X], c1[Y], c2[X], c2[Y], c3[X], c3[Y]);
1028 }
1029 }
1030 return ret;
1031 }
1033 /**
1034 * Returns total length of curve, excluding length of closepath segments.
1035 */
1036 double
1037 sp_curve_distance_including_space(SPCurve const *const curve, double seg2len[])
1038 {
1039 g_return_val_if_fail(curve != NULL, 0.);
1041 double ret = 0.0;
1043 if ( curve->_bpath->code == NR_END ) {
1044 return ret;
1045 }
1047 NR::Point prev(curve->_bpath->c(3));
1048 for (guint i = 1; i < curve->_end; ++i) {
1049 NArtBpath &p = curve->_bpath[i];
1050 double seg_len = 0;
1051 switch (p.code) {
1052 case NR_MOVETO_OPEN:
1053 case NR_MOVETO:
1054 case NR_LINETO:
1055 seg_len = L2(p.c(3) - prev);
1056 break;
1058 case NR_CURVETO:
1059 seg_len = bezier_len(prev, p.c(1), p.c(2), p.c(3), 1.);
1060 break;
1062 case NR_END:
1063 return ret;
1064 }
1065 seg2len[i - 1] = seg_len;
1066 ret += seg_len;
1067 prev = p.c(3);
1068 }
1069 g_assert(!(ret < 0));
1070 return ret;
1071 }
1073 /**
1074 * Like sp_curve_distance_including_space(), but ensures that the
1075 * result >= 1e-18: uses 1 per segment if necessary.
1076 */
1077 double
1078 sp_curve_nonzero_distance_including_space(SPCurve const *const curve, double seg2len[])
1079 {
1080 double const real_dist(sp_curve_distance_including_space(curve, seg2len));
1081 if (real_dist >= 1e-18) {
1082 return real_dist;
1083 } else {
1084 unsigned const nSegs = SP_CURVE_LENGTH(curve) - 1;
1085 for (unsigned i = 0; i < nSegs; ++i) {
1086 seg2len[i] = 1.;
1087 }
1088 return (double) nSegs;
1089 }
1090 }
1092 void
1093 SPCurve::stretch_endpoints(NR::Point const &new_p0, NR::Point const &new_p1)
1094 {
1095 if (is_empty()) {
1096 return;
1097 }
1098 g_assert(unsigned(SP_CURVE_LENGTH(this)) + 1 == sp_bpath_length(_bpath));
1099 unsigned const nSegs = SP_CURVE_LENGTH(this) - 1;
1100 g_assert(nSegs != 0);
1101 double *const seg2len = new double[nSegs];
1102 double const tot_len = sp_curve_nonzero_distance_including_space(this, seg2len);
1103 NR::Point const offset0( new_p0 - first_point() );
1104 NR::Point const offset1( new_p1 - last_point() );
1105 _bpath->setC(3, new_p0);
1106 double begin_dist = 0.;
1107 for (unsigned si = 0; si < nSegs; ++si) {
1108 double const end_dist = begin_dist + seg2len[si];
1109 NArtBpath &p = _bpath[1 + si];
1110 switch (p.code) {
1111 case NR_LINETO:
1112 case NR_MOVETO:
1113 case NR_MOVETO_OPEN:
1114 p.setC(3, p.c(3) + NR::Lerp(end_dist / tot_len, offset0, offset1));
1115 break;
1117 case NR_CURVETO:
1118 for (unsigned ci = 1; ci <= 3; ++ci) {
1119 p.setC(ci, p.c(ci) + Lerp((begin_dist + ci * seg2len[si] / 3.) / tot_len, offset0, offset1));
1120 }
1121 break;
1123 default:
1124 g_assert_not_reached();
1125 }
1127 begin_dist = end_dist;
1128 }
1129 g_assert(L1(_bpath[nSegs].c(3) - new_p1) < 1.);
1130 /* Explicit set for better numerical properties. */
1131 _bpath[nSegs].setC(3, new_p1);
1132 delete [] seg2len;
1133 }
1135 void
1136 SPCurve::move_endpoints(NR::Point const &new_p0, NR::Point const &new_p1)
1137 {
1138 if (is_empty()) {
1139 return;
1140 }
1141 unsigned const nSegs = SP_CURVE_LENGTH(this) - 1;
1142 g_assert(nSegs != 0);
1144 _bpath->setC(3, new_p0);
1145 _bpath[nSegs].setC(3, new_p1);
1146 }
1149 /*
1150 Local Variables:
1151 mode:c++
1152 c-file-style:"stroustrup"
1153 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1154 indent-tabs-mode:nil
1155 fill-column:99
1156 End:
1157 */
1158 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :