The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

StrokeTool.vala in libbirdfont

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libbirdfont/StrokeTool.vala.
Fix stroke code and remove points on points
1 /* 2 Copyright (C) 2014 2015 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 using Cairo; 16 using Math; 17 18 namespace BirdFont { 19 20 public enum LineCap { 21 BUTT, 22 SQUARE, 23 ROUND 24 } 25 26 public class StrokeTool : GLib.Object { 27 28 public static double stroke_width = 0; 29 public static bool add_stroke = false; 30 31 public static bool show_stroke_tools = false; 32 public static bool convert_stroke = false; 33 34 public static LineCap line_cap = LineCap.BUTT; 35 36 StrokeTask task; 37 38 public StrokeTool () { 39 task = new StrokeTask.none (); 40 } 41 42 public StrokeTool.with_task (StrokeTask cancelable_task) { 43 task = cancelable_task; 44 } 45 46 /** Create strokes for the selected outlines. */ 47 public void stroke_selected_paths () { 48 Glyph g = MainWindow.get_current_glyph (); 49 PathList paths = new PathList (); 50 51 convert_stroke = true; 52 g.store_undo_state (); 53 54 foreach (Path p in g.active_paths) { 55 if (p.stroke > 0) { 56 paths.append (p.get_stroke ()); 57 } 58 } 59 60 if (paths.paths.size > 0) { 61 foreach (Path p in g.active_paths) { 62 g.layers.remove_path (p); 63 } 64 65 g.active_paths.clear (); 66 67 foreach (Path np in paths.paths) { 68 g.add_path (np); 69 g.active_paths.add (np); 70 } 71 72 PenTool.update_orientation (); 73 74 GlyphCanvas.redraw (); 75 } 76 77 PenTool.update_orientation (); 78 convert_stroke = false; 79 } 80 81 public PathList get_stroke_fast (Path path, double thickness) { 82 PathList o; 83 Path stroke; 84 StrokeTool s = new StrokeTool (); 85 86 stroke = path.copy (); 87 stroke.remove_points_on_points (0.3); 88 o = s.create_stroke (stroke, thickness, false); // set to true for faster stroke 89 90 return o; 91 } 92 93 public PathList get_stroke (Path path, double thickness) { 94 PathList o, m; 95 Path stroke; 96 97 stroke = path.copy (); 98 stroke.remove_points_on_points (0.3); 99 o = create_stroke (stroke, thickness, false); 100 o = get_all_parts (o); 101 o = remove_intersection_paths (o); 102 o = merge (o); 103 104 m = new PathList (); 105 foreach (Path p in o.paths) { 106 m.add (simplify_stroke (p)); 107 } 108 109 return m; 110 } 111 112 void reset_flags (PathList o) { 113 foreach (Path p in o.paths) { 114 foreach (EditPoint ep in p.points) { 115 ep.flags &= uint.MAX ^ 116 (EditPoint.INTERSECTION 117 | EditPoint.COPIED 118 | EditPoint.NEW_CORNER 119 | EditPoint.SELF_INTERSECTION); 120 } 121 p.update_region_boundaries (); 122 } 123 } 124 125 public void merge_selected_paths () { 126 Glyph g = MainWindow.get_current_glyph (); 127 PathList o = new PathList (); 128 PathList r; 129 PathList new_paths = new PathList (); 130 bool error = false; 131 132 g.store_undo_state (); 133 134 foreach (Path p in g.active_paths) { 135 if (p.stroke == 0) { 136 o.add (p); 137 } else { 138 o.append (p.get_stroke ()); 139 } 140 } 141 142 foreach (Path p in o.paths) { 143 p.close (); 144 remove_single_point_intersections (p); 145 } 146 147 o = remove_overlap (o, out error); 148 149 if (error) { 150 warning ("merge_selected_paths failed."); 151 return; 152 } 153 154 reset_flags (o); 155 new_paths.append (o); 156 157 for (int merge = 0; merge < 2; merge++) { 158 for (int i = 0; i < o.paths.size; i++) { 159 for (int j = 0; j < o.paths.size; j++) { 160 Path p1, p2; 161 162 if (task.is_cancelled ()) { 163 return; 164 } 165 166 p1 = o.paths.get (i); 167 p2 = o.paths.get (j); 168 169 if (merge == 0) { 170 if (p1.is_clockwise () == p2.is_clockwise ()) { 171 continue; 172 } 173 } 174 175 if (merge == 1) { 176 if (p1.is_clockwise () != p2.is_clockwise ()) { 177 continue; 178 } 179 } 180 181 if (i == j) { 182 continue; 183 } 184 185 r = merge_selected (p1, p2, false, out error); 186 187 if (error) { 188 warning ("Can't merge selected paths."); 189 return; 190 } 191 192 remove_merged_curve_parts (r); 193 194 if (r.paths.size > 0) { 195 reset_flags (r); 196 new_paths.append (r); 197 198 new_paths.remove (p1); 199 new_paths.remove (p2); 200 201 o.remove (p1); 202 o.remove (p2); 203 204 o.append (r); 205 206 i = 0; 207 j = 0; 208 } 209 } 210 } 211 } 212 213 foreach (Path p in g.active_paths) { 214 g.delete_path (p); 215 } 216 217 g.clear_active_paths (); 218 219 remove_merged_curve_parts (new_paths); 220 221 foreach (Path p in new_paths.paths) { 222 g.add_path (p); 223 g.add_active_path (null, p); 224 } 225 226 PenTool.update_orientation (); 227 GlyphCanvas.redraw (); 228 } 229 230 void remove_single_point_intersections (Path p) { 231 PointSelection ps; 232 233 p.remove_points_on_points (); 234 235 for (int i = 0; i < p.points.size; i++) { 236 EditPoint ep = p.points.get (i); 237 EditPoint next = p.points.get ((i + 1) % p.points.size); 238 if (fabs (ep.get_right_handle ().angle - ep.get_left_handle ().angle) % (2 * PI) < 0.01) { 239 ps = new PointSelection (ep, p); 240 PenTool.remove_point_simplify (ps); 241 } else if (Path.distance_to_point (ep, next) < 0.01) { 242 ps = new PointSelection (ep, p); 243 PenTool.remove_point_simplify (ps); 244 } 245 } 246 } 247 248 PathList remove_overlap (PathList pl, out bool error) { 249 PathList r = new PathList (); 250 251 error = false; 252 253 foreach (Path p in pl.paths) { 254 PathList m = merge_selected (p, new Path (), true, out error); 255 256 if (error) { 257 warning ("Can't remove overlap."); 258 return pl; 259 } 260 261 if (m.paths.size > 0) { 262 r.append (m); 263 } else { 264 r.add (p); 265 } 266 } 267 268 return r; 269 } 270 271 void remove_merged_curve_parts (PathList r) { 272 Gee.ArrayList<Path> remove = new Gee.ArrayList<Path> (); 273 PathList flat = new PathList (); 274 275 foreach (Path p in r.paths) { 276 p.update_region_boundaries (); 277 flat.add (p.flatten ()); 278 } 279 280 foreach (Path p in r.paths) { 281 PathList pl = get_insides (flat, p); 282 283 int counters = 0; 284 int clockwise = 0; 285 286 foreach (Path i in pl.paths) { 287 if (i.is_clockwise ()) { 288 clockwise++; 289 } else { 290 counters++; 291 } 292 } 293 294 if (p.is_clockwise ()) { 295 if (clockwise - 1 > counters) { 296 remove.add (p); 297 break; 298 } 299 } else { 300 if (clockwise < counters - 1) { 301 remove.add (p); 302 break; 303 } 304 } 305 } 306 307 foreach (Path p in remove) { 308 r.paths.remove (p); 309 remove_merged_curve_parts (r); 310 return; 311 } 312 } 313 314 public PathList merge_selected (Path path1, Path path2, 315 bool self_intersection, out bool error) { 316 317 PathList flat = new PathList (); 318 PathList o = new PathList (); 319 PathList pl = new PathList (); 320 PathList r = new PathList (); 321 322 pl.add (path1); 323 pl.add (path2); 324 325 error = false; 326 327 if (!self_intersection) { 328 if (!path1.boundaries_intersecting (path2)) { 329 return r; 330 } 331 } 332 333 foreach (Path p in pl.paths) { 334 if (p.stroke == 0) { 335 o.add (p); 336 flat.add (p.copy ().flatten (50)); 337 } 338 } 339 340 flat = merge (flat); 341 342 foreach (Path pp in o.paths) { 343 pp.remove_points_on_points (0.1); 344 } 345 346 bool has_split_point = false; 347 foreach (Path p in flat.paths) { 348 foreach (EditPoint ep in p.points) { 349 if ((ep.flags & EditPoint.SPLIT_POINT) > 0) { 350 foreach (Path pp in o.paths) { 351 EditPoint lep = new EditPoint (); 352 353 if (pp.points.size > 1) { 354 pp.get_closest_point_on_path (lep, ep.x, ep.y, null, null); 355 356 if (Path.distance_to_point (ep, lep) < 0.1) { 357 EditPoint lep2 = new EditPoint (); 358 pp.get_closest_point_on_path (lep2, ep.x, ep.y, lep.prev, lep.next); 359 360 if (lep.prev != null) { 361 lep.get_left_handle ().type = lep.get_prev ().get_right_handle ().type; 362 } else { 363 lep.get_left_handle ().type = pp.get_last_point ().get_right_handle ().type; 364 } 365 366 if (lep.next != null) { 367 lep.get_right_handle ().type = lep.get_next ().get_left_handle ().type; 368 } else { 369 lep.get_left_handle ().type = pp.get_first_point ().get_right_handle ().type; 370 } 371 372 if (lep2.prev != null) { 373 lep2.get_left_handle ().type = lep2.get_prev ().get_right_handle ().type; 374 } else { 375 lep2.get_left_handle ().type = pp.get_first_point ().get_right_handle ().type; 376 } 377 378 if (lep2.next != null) { 379 lep2.get_right_handle ().type = lep2.get_next ().get_left_handle ().type; 380 } else { 381 lep2.get_left_handle ().type = pp.get_last_point ().get_right_handle ().type; 382 } 383 384 // self intersection 385 if (Path.distance_to_point (ep, lep2) < 0.1 386 && Path.distance_to_point (ep, lep) < 0.1) { 387 388 if (Path.distance_to_point (lep, (!) lep.prev) < 0.001) { 389 // FIXME: DELETE ep.flags &= uint.MAX ^ EditPoint.SPLIT_POINT; 390 continue; 391 } 392 393 if (Path.distance_to_point (lep, (!) lep.next) < 0.001) { 394 // FIXME: DELETE ep.flags &= uint.MAX ^ EditPoint.SPLIT_POINT; 395 continue; 396 } 397 398 add_double_point_at_intersection (pp, lep, ep); 399 add_double_point_at_intersection (pp, lep2, ep); 400 401 pp.insert_new_point_on_path (lep); 402 pp.insert_new_point_on_path (lep2); 403 404 lep.flags |= EditPoint.SELF_INTERSECTION; 405 lep2.flags |= EditPoint.SELF_INTERSECTION; 406 407 lep.tie_handles = false; 408 lep.reflective_point = false; 409 lep2.tie_handles = false; 410 lep2.reflective_point = false; 411 } else { 412 if (lep.prev != null && Path.distance_to_point (lep, (!) lep.prev) < 0.00000001) { 413 lep.get_prev ().flags |= EditPoint.INTERSECTION; 414 lep.get_prev ().tie_handles = false; 415 lep.get_prev ().reflective_point = false; 416 continue; 417 } 418 419 if (lep.next != null && Path.distance_to_point (lep, (!) lep.next) < 0.00000001) { 420 lep.get_next ().flags |= EditPoint.INTERSECTION; 421 lep.get_next ().tie_handles = false; 422 lep.get_next ().reflective_point = false; 423 continue; 424 } 425 426 add_double_point_at_intersection (pp, lep, ep); 427 pp.insert_new_point_on_path (lep); 428 lep.flags |= EditPoint.INTERSECTION; 429 lep.tie_handles = false; 430 lep.reflective_point = false; 431 } 432 433 has_split_point = true; 434 } 435 } 436 } 437 } 438 } 439 } 440 441 if (!has_split_point) { 442 return r; 443 } 444 445 // remove double intersection points 446 EditPoint prev = new EditPoint (); 447 foreach (Path pp in o.paths) { 448 foreach (EditPoint ep in pp.points) { 449 if (((prev.flags & EditPoint.SELF_INTERSECTION) > 0 || (prev.flags & EditPoint.INTERSECTION) > 0) 450 && ((ep.flags & EditPoint.SELF_INTERSECTION) > 0 || (ep.flags & EditPoint.INTERSECTION) > 0) 451 && fabs (ep.x - prev.x) < 0.2 452 && fabs (ep.y - prev.y) < 0.2) { 453 prev.deleted = true; 454 } 455 456 prev = ep; 457 } 458 } 459 460 foreach (Path pp in o.paths) { 461 pp.remove_deleted_points (); 462 } 463 464 foreach (Path p in o.paths) { 465 foreach (EditPoint ep in p.points) { 466 ep.flags &= uint.MAX ^ EditPoint.COPIED; 467 } 468 } 469 470 return_val_if_fail (o.paths.size == 2, r); 471 472 Path p1, p2; 473 474 p1 = o.paths.get (0); 475 p2 = o.paths.get (1); 476 PathList parts = new PathList (); 477 478 if (self_intersection) { 479 // remove overlap 480 PathList self_parts; 481 482 self_parts = remove_self_intersections (p1, out error); 483 484 if (error) { 485 warning ("remove_self_intersections failed"); 486 return new PathList (); 487 } 488 489 parts.append (self_parts); 490 } else { 491 // merge two path 492 PathList merged_paths = merge_paths_with_curves (p1, p2); 493 494 if (merged_paths.paths.size > 0) { 495 parts.append (merged_paths); 496 } else { 497 parts.add (p1); 498 parts.add (p2); 499 } 500 } 501 502 foreach (Path p in parts.paths) { 503 reset_intersections (p); 504 } 505 506 reset_intersections (path1); 507 reset_intersections (path2); 508 509 return parts; 510 } 511 512 /** Add hidden double points to make sure that the path does not 513 * change when new points are added to a 2x2 path. 514 */ 515 void add_double_point_at_intersection (Path pp, EditPoint lep, EditPoint ep) { 516 EditPoint before; 517 EditPoint after; 518 EditPoint hidden; 519 double px, py; 520 521 if (lep.get_right_handle ().type == PointType.DOUBLE_CURVE 522 || lep.get_right_handle ().type == PointType.LINE_DOUBLE_CURVE) { 523 524 return_if_fail (lep.prev != null); 525 return_if_fail (lep.next != null); 526 527 before = lep.get_prev (); 528 after = lep.get_next (); 529 hidden = new EditPoint (0, 0, PointType.QUADRATIC); 530 531 px = before.get_right_handle ().x 532 + (after.get_left_handle ().x - before.get_right_handle ().x) / 2.0; 533 py = before.get_right_handle ().y 534 + (after.get_left_handle ().y - before.get_right_handle ().y) / 2.0; 535 hidden.independent_x = px; 536 hidden.independent_y = py; 537 538 hidden.get_right_handle ().x = after.get_left_handle ().x; 539 hidden.get_right_handle ().y = after.get_left_handle ().y; 540 hidden.get_left_handle ().x = before.get_right_handle ().x; 541 hidden.get_left_handle ().y = before.get_right_handle ().y; 542 543 pp.add_point_after (hidden, before); 544 545 hidden.get_right_handle ().type = PointType.QUADRATIC; 546 hidden.get_left_handle ().type = PointType.QUADRATIC; 547 548 before.get_right_handle ().type = PointType.QUADRATIC; 549 after.get_left_handle ().type = PointType.QUADRATIC; 550 before.type = PointType.QUADRATIC; 551 after.type = PointType.QUADRATIC; 552 553 pp.get_closest_point_on_path (lep, ep.x, ep.y, null, null); 554 } 555 } 556 557 PathList remove_self_intersections (Path original, out bool error) { 558 Path merged = new Path (); 559 IntersectionList intersections = new IntersectionList (); 560 EditPoint ep1, ep2, found; 561 double d; 562 double min_d; 563 Path current; 564 bool found_intersection; 565 PathList parts; 566 int i = 0; 567 Path path = original.copy (); 568 569 error = false; 570 571 path.remove_points_on_points (); 572 parts = new PathList (); 573 574 if (path.points.size <= 1) { 575 return parts; 576 } 577 578 // reset copied points 579 foreach (EditPoint n in path.points) { 580 n.flags &= uint.MAX ^ EditPoint.COPIED; 581 } 582 583 // build list of intersection points 584 for (i = 0; i < path.points.size; i++) { 585 ep1 = path.points.get (i); 586 587 if ((ep1.flags & EditPoint.SELF_INTERSECTION) > 0 588 && (ep1.flags & EditPoint.COPIED) == 0) { 589 ep1.flags |= EditPoint.COPIED; 590 591 found = new EditPoint (); 592 min_d = double.MAX; 593 found_intersection = false; 594 595 for (int j = 0; j < path.points.size; j++) { 596 ep2 = path.points.get (j); 597 d = Path.distance_to_point (ep1, ep2); 598 if ((ep2.flags & EditPoint.COPIED) == 0 599 && (ep2.flags & EditPoint.SELF_INTERSECTION) > 0) { 600 if (d < min_d) { 601 min_d = d; 602 found_intersection = true; 603 found = ep2; 604 } 605 } 606 } 607 608 if (!found_intersection) { 609 warning (@"No self intersection:\n$(ep1)"); 610 return parts; 611 } 612 613 ep1.tie_handles = false; 614 ep1.reflective_point = false; 615 found.tie_handles = false; 616 found.reflective_point = false; 617 618 found.flags |= EditPoint.COPIED; 619 Intersection intersection = new Intersection (ep1, path, found, path); 620 intersection.self_intersection = true; 621 intersections.points.add (intersection); 622 } 623 } 624 625 // reset copy flag 626 foreach (EditPoint n in path.points) { 627 n.flags &= uint.MAX ^ EditPoint.COPIED; 628 } 629 630 if (intersections.points.size == 0) { 631 warning ("No intersection points."); 632 error = true; 633 return parts; 634 } 635 636 current = path; 637 current.reverse (); 638 639 while (true) { 640 EditPoint modified; 641 i = 0; 642 Intersection new_start = new Intersection.empty (); 643 ep1 = current.points.get (i); 644 current = path; 645 646 modified = ep1.copy (); 647 648 for (i = 0; i < current.points.size; i++) { 649 ep1 = current.points.get (i); 650 modified = ep1.copy (); 651 if ((ep1.flags & EditPoint.COPIED) == 0 652 && (ep1.flags & EditPoint.SELF_INTERSECTION) == 0) { 653 break; 654 } 655 } 656 657 if (i >= current.points.size || (ep1.flags & EditPoint.COPIED) > 0) { 658 // all points have been copied 659 break; 660 } 661 662 while (true) { 663 664 if ((ep1.flags & EditPoint.SELF_INTERSECTION) > 0) { 665 bool other; 666 EditPointHandle handle; 667 668 handle = ep1.get_left_handle (); 669 new_start = intersections.get_point (ep1, out other); 670 671 i = index_of (current, other ? new_start.point : new_start.other_point); 672 673 if (!(0 <= i < current.points.size)) { 674 warning (@"Index out of bounds. ($i)"); 675 return parts; 676 } 677 678 ep1 = current.points.get (i); 679 modified = ep1.copy (); 680 modified.left_handle.move_to_coordinate (handle.x, handle.y); 681 } 682 683 if ((ep1.flags & EditPoint.COPIED) > 0) { 684 merged.close (); 685 686 merged.close (); 687 merged.create_list (); 688 parts.add (merged); 689 690 foreach (EditPoint n in merged.points) { 691 n.flags &= uint.MAX ^ EditPoint.SELF_INTERSECTION; 692 } 693 694 merged.reverse (); 695 696 merged = new Path (); 697 698 break; 699 } 700 701 // adjust the other handle 702 if ((ep1.flags & EditPoint.INTERSECTION) > 0) { 703 ep1.left_handle.convert_to_curve (); 704 ep1.right_handle.convert_to_curve (); 705 ep1.tie_handles = false; 706 ep1.reflective_point = false; 707 } 708 709 // add point to path 710 ep1.flags |= EditPoint.COPIED; 711 merged.add_point (modified.copy ()); 712 713 i++; 714 ep1 = current.points.get (i % current.points.size); 715 modified = ep1.copy (); 716 } 717 718 ep1.flags |= EditPoint.COPIED; 719 720 } 721 722 return parts; 723 } 724 725 PathList merge_paths_with_curves (Path path1, Path path2) { 726 PathList r = new PathList (); 727 IntersectionList intersections = new IntersectionList (); 728 EditPoint ep1, ep2, found; 729 double d; 730 double min_d; 731 Path current; 732 bool found_intersection; 733 Path flat1, flat2; 734 735 if (path1.points.size <= 1 || path2.points.size <= 1) { 736 return r; 737 } 738 739 flat1 = path1.flatten (); 740 flat2 = path2.flatten (); 741 742 // reset copied points 743 foreach (EditPoint n in path2.points) { 744 n.flags &= uint.MAX ^ EditPoint.COPIED; 745 } 746 747 // build list of intersection points 748 for (int i = 0; i < path1.points.size; i++) { 749 ep1 = path1.points.get (i); 750 751 if ((ep1.flags & EditPoint.INTERSECTION) > 0) { 752 found = new EditPoint (); 753 min_d = double.MAX; 754 found_intersection = false; 755 for (int j = 0; j < path2.points.size; j++) { 756 ep2 = path2.points.get (j); 757 d = Path.distance_to_point (ep1, ep2); 758 if ((ep2.flags & EditPoint.COPIED) == 0 759 && (ep2.flags & EditPoint.INTERSECTION) > 0) { 760 if (d < min_d && d < 0.1) { 761 min_d = d; 762 found_intersection = true; 763 found = ep2; 764 } 765 } 766 } 767 768 if (!found_intersection) { 769 warning (@"No intersection for:\n $(ep1)"); 770 continue; 771 } 772 773 found.flags |= EditPoint.COPIED; 774 775 ep1.tie_handles = false; 776 ep1.reflective_point = false; 777 found.tie_handles = false; 778 found.reflective_point = false; 779 Intersection intersection = new Intersection (ep1, path1, found, path2); 780 intersections.points.add (intersection); 781 } 782 } 783 784 // reset copy flag 785 foreach (EditPoint n in path1.points) { 786 n.flags &= uint.MAX ^ EditPoint.COPIED; 787 } 788 789 foreach (EditPoint n in path2.points) { 790 n.flags &= uint.MAX ^ EditPoint.COPIED; 791 } 792 793 if (intersections.points.size == 0) { 794 warning ("No intersection points."); 795 return r; 796 } 797 798 Path new_path = new Path (); 799 current = path1; 800 while (true) { 801 // find a beginning of a new part 802 bool find_parts = false; 803 Intersection new_start = new Intersection.empty (); 804 foreach (Intersection inter in intersections.points) { 805 if (!inter.done && !find_parts) { 806 find_parts = true; 807 new_start = inter; 808 current = new_start.path; 809 } 810 } 811 812 if (new_path.points.size > 0) { 813 new_path.close (); 814 new_path.recalculate_linear_handles (); 815 new_path.update_region_boundaries (); 816 r.add (new_path); 817 } 818 819 if (!find_parts) { // no more parts 820 break; 821 } 822 823 if ((new_start.get_point (current).flags & EditPoint.COPIED) > 0) { 824 current = new_start.get_other_path (current); 825 } 826 827 int i = index_of (current, new_start.get_point (current)); 828 829 if (i < 0) { 830 warning ("i < 0"); 831 return r; 832 } 833 834 EditPoint previous = new EditPoint (); 835 new_path = new Path (); 836 ep1 = current.points.get (i); 837 current = new_start.get_other_path (current); // swap at first iteration 838 bool first = true; 839 while (true) { 840 if ((ep1.flags & EditPoint.INTERSECTION) > 0) { 841 bool other; 842 843 previous = ep1; 844 845 if (likely (intersections.has_point (ep1))) { 846 new_start = intersections.get_point (ep1, out other); 847 current = new_start.get_other_path (current); 848 i = index_of (current, new_start.get_point (current)); 849 850 if (!(0 <= i < current.points.size)) { 851 warning (@"Index out of bounds. ($i)"); 852 return r; 853 } 854 855 ep1 = current.points.get (i); 856 ep2 = current.points.get ((i + 1) % current.points.size); 857 858 double px, py; 859 860 Path.get_point_for_step (ep1, ep2, 0.5, out px, out py); 861 bool inside = (current == path1 && flat2.is_over_coordinate (px, py)) 862 || (current == path2 && flat1.is_over_coordinate (px, py)); 863 864 bool other_inside = (current != path1 && flat2.is_over_coordinate (px, py)) 865 || (current != path2 && flat1.is_over_coordinate (px, py)); 866 867 if (inside && !other_inside) { 868 current = new_start.get_other_path (current); 869 i = index_of (current, new_start.get_point (current)); 870 871 if (!(0 <= i < current.points.size)) { 872 warning (@"Index out of bounds. ($i >= $(current.points.size)) "); 873 return r; 874 } 875 876 new_start.done = true; 877 ep1 = current.points.get (i); 878 } 879 880 inside = (current == path1 && flat2.is_over_coordinate (px, py)) 881 || (current == path2 && flat1.is_over_coordinate (px, py)); 882 883 if (first) { 884 Path c = new_start.get_other_path (current); 885 if (c.points.size >= 1) { 886 previous = c.get_first_point (); 887 } 888 889 first = false; 890 } 891 } 892 } 893 894 if ((ep1.flags & EditPoint.COPIED) > 0) { 895 new_path.close (); 896 897 if (new_path.points.size >= 1) { 898 EditPoint first_point = new_path.get_first_point (); 899 EditPointHandle h; 900 if ((ep1.flags & EditPoint.INTERSECTION) > 0) { 901 first_point.left_handle.move_to_coordinate (previous.left_handle.x, previous.left_handle.y); 902 903 if (first_point.next != null) { 904 h = first_point.get_next ().get_left_handle (); 905 h.process_connected_handle (); 906 } 907 } 908 } 909 910 break; 911 } 912 913 // adjust the other handle 914 if ((ep1.flags & EditPoint.INTERSECTION) > 0) { 915 ep1.left_handle.convert_to_curve (); 916 ep1.right_handle.convert_to_curve (); 917 } 918 919 // add point to path 920 ep1.flags |= EditPoint.COPIED; 921 new_path.add_point (ep1.copy ()); 922 923 if ((ep1.flags & EditPoint.INTERSECTION) > 0) { 924 new_path.get_last_point ().left_handle.move_to_coordinate (previous.left_handle.x, previous.left_handle.y); 925 } 926 927 i++; 928 ep1 = current.points.get (i % current.points.size); 929 } 930 931 ep1.flags |= EditPoint.COPIED; 932 933 if (!new_start.done) { 934 new_start.done = (new_start.get_other_point (current).flags & EditPoint.COPIED) > 0; 935 } 936 } 937 938 return r; 939 } 940 941 Path simplify_stroke (Path p) { 942 Path simplified = new Path (); 943 Path segment, added_segment; 944 EditPoint ep, ep_start, last, first, segment_last; 945 int start, stop; 946 int j; 947 EditPointHandle last_handle; 948 949 last_handle = new EditPointHandle.empty (); 950 951 segment_last = new EditPoint (); 952 last = new EditPoint (); 953 954 p.remove_points_on_points (); 955 956 foreach (EditPoint e in p.points) { 957 PenTool.convert_point_type (e, PointType.CUBIC); 958 } 959 960 foreach (EditPoint e in p.points) { 961 if ((e.flags & EditPoint.CURVE) == 0) { 962 p.set_new_start (e); 963 break; 964 } 965 } 966 967 for (int i = 0; i < p.points.size; i++) { 968 ep = p.points.get (i); 969 970 if ((ep.flags & EditPoint.CURVE) > 0) { 971 start = i; 972 for (j = start + 1; j < p.points.size; j++) { 973 ep = p.points.get (j); 974 if ((ep.flags & EditPoint.CURVE) == 0) { 975 break; 976 } 977 } 978 979 if (task.is_cancelled ()) { 980 return new Path (); 981 } 982 983 984 stop = j; 985 start -= 1; 986 987 if (start < 0) { 988 warning ("start < 0"); 989 start = 0; 990 } 991 992 if (stop >= p.points.size) { 993 warning ("stop >= p.points.size"); 994 stop = p.points.size - 1; 995 } 996 997 ep_start = p.points.get (start); 998 ep = p.points.get (stop); 999 1000 double l = Path.distance_to_point (ep_start, ep); 1001 segment = fit_bezier_path (p, start, stop, 0.00001 * l * l); 1002 1003 added_segment = segment.copy (); 1004 1005 if (simplified.points.size > 0) { 1006 last = simplified.get_last_point (); 1007 } 1008 1009 if (added_segment.points.size > 0) { 1010 segment_last = added_segment.get_last_point (); 1011 first = added_segment.get_first_point (); 1012 segment_last.right_handle = ep_start.get_right_handle ().copy (); 1013 1014 if (simplified.points.size > 1) { 1015 last = simplified.delete_last_point (); 1016 } 1017 1018 first.set_tie_handle (false); 1019 last.set_tie_handle (false); 1020 1021 last.get_right_handle ().x = first.get_right_handle ().x; 1022 last.get_right_handle ().y = first.get_right_handle ().y; 1023 1024 first.get_left_handle ().convert_to_curve (); 1025 first.get_left_handle ().x = last.get_left_handle ().x; 1026 first.get_left_handle ().y = last.get_left_handle ().y; 1027 1028 last = added_segment.get_last_point (); 1029 last.right_handle = ep.get_right_handle ().copy (); 1030 last.recalculate_linear_handles (); 1031 1032 simplified.append_path (added_segment); 1033 1034 segment_last.right_handle = ep.get_right_handle ().copy (); 1035 1036 if (added_segment.points.size > 0) { 1037 if (ep_start.get_right_handle ().is_line ()) { 1038 first = added_segment.get_first_point (); 1039 first.recalculate_linear_handles (); 1040 } 1041 } 1042 1043 last_handle = last.get_left_handle (); 1044 } else { 1045 warning ("No points in segment."); 1046 } 1047 1048 i = stop; 1049 } else { 1050 simplified.add_point (ep.copy ()); 1051 } 1052 } 1053 1054 simplified.recalculate_linear_handles (); 1055 simplified.close (); 1056 remove_single_point_intersections (simplified); 1057 1058 simplified.remove_points_on_points (); 1059 1060 return simplified; 1061 } 1062 1063 Path fit_bezier_path (Path p, int start, int stop, double error) { 1064 int index, size; 1065 Path simplified; 1066 double[] lines; 1067 double[] result; 1068 EditPoint ep; 1069 1070 simplified = new Path (); 1071 1072 return_val_if_fail (0 <= start < p.points.size, simplified); 1073 return_val_if_fail (0 <= stop < p.points.size, simplified); 1074 1075 size = stop - start + 1; 1076 lines = new double[2 * size]; 1077 1078 index = 0; 1079 1080 for (int i = start; i <= stop; i++) { 1081 ep = p.points.get (i); 1082 lines[index] = ep.x; 1083 index++; 1084 1085 lines[index] = ep.y; 1086 index++; 1087 } 1088 1089 return_val_if_fail (2 * size == index, new Path ()); 1090 1091 Gems.fit_bezier_curve_to_line (lines, error, out result); 1092 1093 return_val_if_fail (!is_null (result), simplified); 1094 1095 for (int i = 0; i + 7 < result.length; i += 8) { 1096 simplified.add_cubic_bezier_points ( 1097 result[i], result[i + 1], 1098 result[i + 2], result[i + 3], 1099 result[i + 4], result[i + 5], 1100 result[i + 6], result[i + 7]); 1101 } 1102 1103 return simplified; 1104 } 1105 1106 PathList remove_intersection_paths (PathList pl) { 1107 PathList r = new PathList (); 1108 1109 foreach (Path p in pl.paths) { 1110 if (p.points.size > 7) { 1111 r.add (p); 1112 } else if (!has_new_corner (p)) { 1113 r.add (p); 1114 } else if (counters (pl, p) == 0) { 1115 r.add (p); 1116 } 1117 } 1118 1119 return r; 1120 } 1121 1122 bool has_new_corner (Path p) { 1123 foreach (EditPoint ep in p.points) { 1124 if ((ep.flags & EditPoint.NEW_CORNER) > 0) { 1125 return true; 1126 } 1127 } 1128 1129 return false; 1130 } 1131 1132 void add_line_cap (Path path, Path stroke1, Path stroke2, bool last_cap) { 1133 if (path.line_cap == LineCap.SQUARE) { 1134 add_square_cap (path, stroke1, stroke2, last_cap); 1135 } else if (path.line_cap == LineCap.ROUND) { 1136 add_round_cap (path, stroke1, stroke2, last_cap); 1137 } 1138 } 1139 1140 void add_round_cap (Path path, Path stroke1, Path stroke2, bool last_cap) { 1141 double px, py; 1142 double step, start_angle, stop_angle; 1143 double radius; 1144 EditPoint n, nstart, nend; 1145 Path cap = new Path (); 1146 1147 EditPoint start, end; 1148 EditPointHandle last_handle; 1149 EditPoint first, last; 1150 1151 stroke1.remove_points_on_points (); 1152 stroke2.remove_points_on_points (); 1153 1154 last_handle = path.get_first_point ().get_right_handle (); 1155 start = stroke1.get_last_point (); 1156 end = stroke2.get_first_point (); 1157 1158 start_angle = last_handle.angle + PI / 2; 1159 stop_angle = start_angle + PI; 1160 1161 nstart = cap.add (start.x, start.y); 1162 radius = Path.distance_to_point (start, end) / 2; 1163 step = PI / 5; 1164 1165 for (int j = 0; j < 5; j++) { 1166 double angle = start_angle + step * j; 1167 px = radius * cos (angle) + last_handle.parent.x; 1168 py = radius * sin (angle) + last_handle.parent.y; 1169 n = cap.add (px, py); 1170 1171 n.type = PointType.LINE_CUBIC; 1172 n.get_right_handle ().type = PointType.LINE_CUBIC; 1173 n.get_left_handle ().type = PointType.LINE_CUBIC; 1174 } 1175 1176 nend = cap.add (end.x, end.y); 1177 1178 for (int i = 0; i < cap.points.size; i++) { 1179 cap.points.get (i).recalculate_linear_handles (); 1180 } 1181 1182 int size = cap.points.size; 1183 1184 for (int i = 1; i < size; i++) { 1185 n = cap.points.get (i); 1186 n.convert_to_curve (); 1187 n.set_tie_handle (true); 1188 n.process_tied_handle (); 1189 } 1190 1191 int f = stroke1.points.size - 1; 1192 1193 for (int i = 2; i < cap.points.size - 1; i++) { 1194 n = cap.points.get (i).copy (); 1195 stroke1.add_point (n); 1196 } 1197 1198 cap.remove_points_on_points (); 1199 return_if_fail (0 < f < stroke1.points.size); 1200 1201 first = stroke1.points.get (f); 1202 1203 last = stroke1.get_last_point (); 1204 last.convert_to_curve (); 1205 1206 last = stroke1.add_point (stroke2.get_first_point ()); 1207 stroke2.delete_first_point (); 1208 1209 last.convert_to_line (); 1210 last.recalculate_linear_handles (); 1211 1212 last.next = stroke1.add_point (stroke2.get_first_point ()).get_link_item (); 1213 stroke2.delete_first_point (); 1214 1215 last.get_left_handle ().convert_to_curve (); 1216 last.get_left_handle ().angle = last.get_right_handle ().angle + PI; 1217 last.flags = EditPoint.CURVE_KEEP; 1218 1219 double a; 1220 double l; 1221 1222 return_if_fail (cap.points.size > 1); 1223 1224 a = (first.get_left_handle ().angle + PI) % (2 * PI); 1225 l = cap.points.get (1).get_right_handle ().length; 1226 1227 first.get_right_handle ().convert_to_curve (); 1228 first.get_right_handle ().angle = a; 1229 first.get_right_handle ().length = l; 1230 1231 a = (first.get_left_handle ().angle + PI) % (2 * PI); 1232 1233 last.get_left_handle ().convert_to_curve (); 1234 last.get_left_handle ().angle = a; 1235 last.get_left_handle ().length = l; 1236 } 1237 1238 void add_square_cap (Path path, Path stroke1, Path stroke2, bool last_cap) { 1239 EditPointHandle last_handle; 1240 EditPoint start; 1241 EditPoint end; 1242 EditPoint n; 1243 double x, y; 1244 double stroke_width = path.stroke / 2; 1245 1246 last_handle = path.get_first_point ().get_right_handle (); 1247 start = stroke1.get_last_point (); 1248 end = stroke2.get_first_point (); 1249 1250 y = sin (last_handle.angle - PI) * stroke_width; 1251 x = cos (last_handle.angle - PI) * stroke_width; 1252 1253 n = stroke1.add (start.x + x, start.y + y); 1254 n.type = PointType.CUBIC; 1255 n.get_right_handle ().type = PointType.CUBIC; 1256 n.get_left_handle ().type = PointType.CUBIC; 1257 n.convert_to_line (); 1258 1259 n = stroke1.add (end.x + x, end.y + y); 1260 n.type = PointType.CUBIC; 1261 n.get_right_handle ().type = PointType.CUBIC; 1262 n.get_left_handle ().type = PointType.CUBIC; 1263 n.convert_to_line (); 1264 } 1265 1266 /** Create one stroke from the outline and counter stroke and close the 1267 * open endings. 1268 * 1269 * @param path the path to create stroke for 1270 * @param stroke for the outline of path 1271 * @param stroke for the counter path 1272 */ 1273 Path merge_strokes (Path path, Path stroke, Path counter) { 1274 1275 Path merged; 1276 EditPoint last_counter, first; 1277 1278 merged = stroke.copy (); 1279 merged.reverse (); 1280 1281 last_counter = new EditPoint (); 1282 first = new EditPoint (); 1283 1284 add_line_cap (path, merged, counter, true); 1285 path.reverse (); 1286 1287 add_line_cap (path, counter, merged, true); 1288 path.reverse (); 1289 1290 merged.append_path (counter); 1291 1292 merged.close (); 1293 merged.create_list (); 1294 merged.recalculate_linear_handles (); 1295 1296 return merged; 1297 } 1298 1299 void move_segment (EditPoint stroke_start, EditPoint stroke_stop, double thickness) { 1300 EditPointHandle r, l; 1301 double m, n; 1302 double qx, qy; 1303 1304 stroke_start.set_tie_handle (false); 1305 stroke_stop.set_tie_handle (false); 1306 1307 r = stroke_start.get_right_handle (); 1308 l = stroke_stop.get_left_handle (); 1309 1310 m = cos (r.angle + PI / 2) * thickness; 1311 n = sin (r.angle + PI / 2) * thickness; 1312 1313 stroke_start.get_right_handle ().move_to_coordinate_delta (m, n); 1314 stroke_start.get_left_handle ().move_to_coordinate_delta (m, n); 1315 1316 stroke_start.independent_x += m; 1317 stroke_start.independent_y += n; 1318 1319 qx = cos (l.angle - PI / 2) * thickness; 1320 qy = sin (l.angle - PI / 2) * thickness; 1321 1322 stroke_stop.get_right_handle ().move_to_coordinate_delta (qx, qy); 1323 stroke_stop.get_left_handle ().move_to_coordinate_delta (qx, qy); 1324 1325 stroke_stop.independent_x += qx; 1326 stroke_stop.independent_y += qy; 1327 } 1328 1329 void add_corner (Path stroked, EditPoint previous, EditPoint next, 1330 EditPoint original, double stroke_width) { 1331 1332 double ratio; 1333 double distance; 1334 EditPoint corner; 1335 double corner_x, corner_y; 1336 EditPointHandle previous_handle; 1337 EditPointHandle next_handle; 1338 EditPoint cutoff1, cutoff2; 1339 double adjusted_stroke = (stroke_width * 0.999999) / 2.0; 1340 bool d1, d2; 1341 1342 previous_handle = previous.get_left_handle (); 1343 next_handle = next.get_right_handle (); 1344 1345 previous_handle.convert_to_line (); 1346 next_handle.convert_to_line (); 1347 1348 previous_handle.angle += PI; 1349 next_handle.angle += PI; 1350 1351 Path.find_intersection_handle (previous_handle, next_handle, out corner_x, out corner_y); 1352 corner = new EditPoint (corner_x, corner_y, PointType.CUBIC); 1353 corner.convert_to_line (); 1354 1355 previous_handle.angle -= PI; 1356 next_handle.angle -= PI; 1357 1358 distance = Path.distance_to_point (corner, original); 1359 ratio = 1.5 * fabs (adjusted_stroke) / distance; 1360 1361 double or = original.get_right_handle ().angle; 1362 double ol = original.get_left_handle ().angle; 1363 1364 if (previous.prev == null) { // FIXME: first point 1365 warning ("Point before corner."); 1366 d1 = false; 1367 d2 = false; 1368 } else { 1369 d1 = corner.x - previous.x >= 0 == previous.x - previous.get_prev ().x >= 0; 1370 d2 = corner.y - previous.y >= 0 == previous.y - previous.get_prev ().y >= 0; 1371 } 1372 1373 if (ratio > 1) { 1374 if (!d1 && !d2) { 1375 return; 1376 } else { 1377 stroked.add_point (corner); 1378 } 1379 } else { 1380 1381 cutoff1 = new EditPoint (); 1382 cutoff1.set_point_type (previous.type); 1383 cutoff1.convert_to_line (); 1384 1385 cutoff2 = new EditPoint (); 1386 cutoff2.set_point_type (previous.type); 1387 cutoff2.convert_to_line (); 1388 1389 if (fabs (or - ol) < 0.001) { 1390 cutoff1.x = previous.x + 1.5 * fabs (adjusted_stroke) * -cos (or); 1391 cutoff1.y = previous.y + 1.5 * fabs (adjusted_stroke) * -sin (or); 1392 1393 cutoff2.x = next.x + 1.5 * fabs (adjusted_stroke) * -cos (or); 1394 cutoff2.y = next.y + 1.5 * fabs (adjusted_stroke) * -sin (or); 1395 } else { 1396 cutoff1.x = previous.x + (corner.x - previous.x) * ratio; 1397 cutoff1.y = previous.y + (corner.y - previous.y) * ratio; 1398 1399 cutoff2.x = next.x + (corner.x - next.x) * ratio; 1400 cutoff2.y = next.y + (corner.y - next.y) * ratio; 1401 } 1402 1403 if (!cutoff1.is_valid () || cutoff2.is_valid ()) { 1404 cutoff1 = stroked.add_point (cutoff1); 1405 cutoff2 = stroked.add_point (cutoff2); 1406 } 1407 1408 cutoff1.recalculate_linear_handles (); 1409 cutoff2.recalculate_linear_handles (); 1410 1411 // self intersection 1412 if (!d1 && !d2) { 1413 cutoff1.deleted = true; 1414 cutoff2.deleted = true; 1415 1416 stroked.remove_deleted_points (); 1417 return; 1418 } 1419 1420 if (distance > 4 * stroke_width) { 1421 previous.flags = EditPoint.NONE; 1422 next.flags = EditPoint.NONE; 1423 } else { 1424 previous.flags |= EditPoint.NEW_CORNER; 1425 next.flags |= EditPoint.NEW_CORNER; 1426 } 1427 } 1428 } 1429 1430 PathList get_parts (Path path) { 1431 PathList intersections; 1432 PathList r; 1433 1434 r = get_parts_self (path); 1435 intersections = new PathList (); 1436 1437 foreach (Path p in r.paths) { 1438 intersections.add (p); 1439 } 1440 1441 return intersections; 1442 } 1443 1444 bool split_corner (PathList pl) { 1445 EditPoint p1, p2; 1446 EditPoint a1, a2; 1447 PathList result; 1448 bool split; 1449 1450 foreach (Path p in pl.paths) { 1451 if (p.points.size == 0) { 1452 continue; 1453 } 1454 1455 for (int index = 1; index < p.points.size + 2; index++) { 1456 p1 = p.points.get ((index - 1) % p.points.size); 1457 p2 = p.points.get (index % p.points.size); 1458 a1 = p.points.get ((index + 3) % p.points.size); // two points ahead 1459 a2 = p.points.get ((index + 4) % p.points.size); 1460 1461 if ((p1.flags & EditPoint.STROKE_OFFSET) > 0 1462 || (p2.flags & EditPoint.STROKE_OFFSET) > 0 1463 || (a1.flags & EditPoint.STROKE_OFFSET) > 0 1464 || (a2.flags & EditPoint.STROKE_OFFSET) > 0) { 1465 1466 split = split_segment (p, a1, a2, p1, p2, out result); 1467 1468 if (split) { 1469 pl.append (result); 1470 pl.paths.remove (p); 1471 split_corner (pl); 1472 return true; 1473 } else { 1474 p1 = p.points.get ((index - 1) % p.points.size); 1475 p2 = p.points.get (index % p.points.size); 1476 a1 = p.points.get ((index + 2) % p.points.size); // one point ahead 1477 a2 = p.points.get ((index + 3) % p.points.size); 1478 1479 split = split_segment (p, a1, a2, p1, p2, out result); 1480 1481 if (split) { 1482 pl.append (result); 1483 pl.paths.remove (p); 1484 split_corner (pl); 1485 return true; 1486 } else { 1487 p1 = p.points.get ((index - 1) % p.points.size); 1488 p2 = p.points.get (index % p.points.size); 1489 a1 = p.points.get ((index + 3) % p.points.size); // two points ahead 1490 a2 = p.points.get ((index + 4) % p.points.size); 1491 1492 if ((p1.flags & EditPoint.STROKE_OFFSET) > 0 1493 && (a1.flags & EditPoint.STROKE_OFFSET) > 0) { 1494 p1.flags = EditPoint.COUNTER_TO_OUTLINE; 1495 a1.flags = EditPoint.COUNTER_TO_OUTLINE; 1496 } 1497 } 1498 } 1499 } 1500 } 1501 } 1502 1503 return false; 1504 } 1505 1506 bool split_segment (Path p, EditPoint first, EditPoint next, EditPoint p1, EditPoint p2, out PathList result) { 1507 double ix, iy; 1508 bool intersection; 1509 int i; 1510 1511 result = new PathList (); 1512 intersection = segments_intersects (first, next, p1, p2, out ix, out iy, true); 1513 1514 if (intersection) { 1515 add_intersection (p, first, next, ix, iy); 1516 add_intersection (p, p1, p2, ix, iy); 1517 1518 i = mark_intersection_as_deleted (p); 1519 return_val_if_fail (i == 2, false); 1520 1521 result.append (get_remaining_points (p.copy ())); 1522 } 1523 1524 return intersection; 1525 } 1526 1527 PathList get_parts_self (Path path, PathList? paths = null) { 1528 PathList pl; 1529 PathList r; 1530 1531 r = paths == null ? new PathList () : (!) paths; 1532 pl = split (path); 1533 1534 foreach (Path part in pl.paths) { 1535 if (!has_self_intersection (part)) { 1536 r.add (part); 1537 } else { 1538 get_parts_self (part, r); 1539 } 1540 } 1541 1542 if (r.paths.size == 0) { 1543 warning ("No parts in path"); 1544 } 1545 1546 return r; 1547 } 1548 1549 1550 bool has_intersection_points (Path path) { 1551 foreach (EditPoint p in path.points) { 1552 if ((p.flags & EditPoint.INTERSECTION) > 0) { 1553 return true; 1554 } 1555 } 1556 return false; 1557 } 1558 1559 /** Split one path at intersection points in two parts. */ 1560 PathList split (Path path) { 1561 Path new_path; 1562 PathList pl; 1563 int i; 1564 1565 if (!has_intersection_points (path)) { 1566 add_self_intersection_points (path); 1567 } else { 1568 warning ("points already created."); 1569 } 1570 1571 foreach (EditPoint p in path.points) { 1572 if (insides (p, path) == 1) { 1573 path.set_new_start (p); 1574 path.close (); 1575 break; 1576 } 1577 } 1578 1579 i = mark_intersection_as_deleted (path); 1580 1581 if (!(i == 0 || i == 2)) { 1582 warning (@"Split should only create two parts, $i points will be deleted."); 1583 } 1584 1585 new_path = path.copy (); 1586 pl = get_remaining_points (new_path); 1587 1588 return pl; 1589 } 1590 1591 PathList process_deleted_control_points (Path path) { 1592 PathList paths, nl, pl, rl, result; 1593 1594 paths = new PathList (); 1595 rl = new PathList (); 1596 pl = new PathList (); 1597 nl = new PathList (); 1598 1599 if (!path.has_deleted_point ()) { 1600 return pl; 1601 } 1602 1603 pl.add (path); 1604 1605 foreach (Path p in pl.paths) { 1606 nl = p.process_deleted_points (); 1607 1608 if (nl.paths.size > 0) { 1609 rl.append (nl); 1610 rl.paths.remove (p); 1611 } 1612 } 1613 1614 result = new PathList (); 1615 foreach (Path p in rl.paths) { 1616 pl = process_deleted_control_points (p); 1617 1618 if (pl.paths.size > 0) { 1619 result.append (pl); 1620 } else { 1621 result.add (p); 1622 } 1623 } 1624 1625 for (int i = 1; i < result.paths.size; i++) { 1626 result.paths.get (i).reverse (); 1627 } 1628 1629 paths.append (result); 1630 1631 return paths; 1632 } 1633 1634 PathList get_remaining_points (Path old_path) { 1635 PathList pl; 1636 1637 old_path.close (); 1638 pl = process_deleted_control_points (old_path); 1639 1640 if (pl.paths.size == 0) { 1641 pl.add (old_path); 1642 } 1643 1644 foreach (Path pn in pl.paths) { 1645 pn.close (); 1646 } 1647 1648 return pl; 1649 } 1650 1651 bool has_self_intersection (Path path) { 1652 bool intersection = false; 1653 1654 path.all_segments ((ep1, ep2) => { 1655 double ix, iy; 1656 EditPoint p1, p2; 1657 1658 if (segment_intersects (path, ep1, ep2, out ix, out iy, out p1, out p2, true)) { 1659 intersection = true; 1660 return false; 1661 } 1662 1663 return true; 1664 }); 1665 1666 return intersection; 1667 } 1668 1669 bool add_self_intersection_points (Path path, bool only_offsets = false) { 1670 bool intersection = false; 1671 1672 path.all_segments ((ep1, ep2) => { 1673 double ix, iy; 1674 EditPoint p1, p2; 1675 1676 if (segment_intersects (path, ep1, ep2, out ix, out iy, out p1, out p2, true, only_offsets)) { 1677 add_intersection (path, ep1, ep2, ix, iy); 1678 add_intersection (path, p1, p2, ix, iy); 1679 1680 intersection = true; 1681 return false; 1682 } 1683 1684 return true; 1685 }); 1686 1687 return intersection; 1688 } 1689 1690 EditPoint add_intersection (Path path, EditPoint prev, EditPoint next, double px, double py, Color? c = null) { 1691 Gee.ArrayList<EditPoint> n = new Gee.ArrayList<EditPoint> (); 1692 EditPoint ep1 = new EditPoint (); 1693 EditPoint ep2 = new EditPoint (); 1694 EditPoint ep3 = new EditPoint (); 1695 double d; 1696 1697 if (next == path.get_first_point ()) { 1698 ep1.prev = null; 1699 } else { 1700 ep1.prev = prev; 1701 } 1702 1703 ep1.prev = prev; 1704 ep1.next = ep2; 1705 ep1.flags |= EditPoint.NEW_CORNER | EditPoint.SPLIT_POINT; 1706 ep1.type = prev.type; 1707 ep1.x = px; 1708 ep1.y = py; 1709 ep1.color = c; 1710 n.add (ep1); 1711 1712 ep2.prev = ep1; 1713 ep2.next = ep3; 1714 ep2.flags |= EditPoint.INTERSECTION | EditPoint.SPLIT_POINT; 1715 ep2.type = prev.type; 1716 ep2.x = px; 1717 ep2.y = py; 1718 ep2.color = c; 1719 n.add (ep2); 1720 1721 ep3.prev = ep2; 1722 ep3.next = next; 1723 ep3.flags |= EditPoint.NEW_CORNER | EditPoint.SPLIT_POINT; 1724 ep3.type = prev.type; 1725 ep3.x = px; 1726 ep3.y = py; 1727 ep3.color = c; 1728 n.add (ep3); 1729 1730 next.get_left_handle ().convert_to_line (); 1731 1732 foreach (EditPoint np in n) { 1733 np = path.add_point_after (np, np.prev); 1734 path.create_list (); 1735 } 1736 1737 PenTool.convert_point_to_line (ep1, true); 1738 PenTool.convert_point_to_line (ep2, true); 1739 PenTool.convert_point_to_line (ep3, true); 1740 1741 ep1.recalculate_linear_handles (); 1742 ep2.recalculate_linear_handles (); 1743 ep3.recalculate_linear_handles (); 1744 1745 d = Path.distance_to_point (prev, next); 1746 prev.get_right_handle ().length *= Path.distance_to_point (prev, ep1) / d; 1747 next.get_left_handle ().length *= Path.distance_to_point (ep3, next) / d; 1748 1749 next.recalculate_linear_handles (); 1750 1751 return ep2; 1752 } 1753 1754 bool segments_intersects (EditPoint p1, EditPoint p2, EditPoint ep, EditPoint next, 1755 out double ix, out double iy, 1756 bool skip_points_on_points = false) { 1757 double cross_x, cross_y; 1758 1759 ix = 0; 1760 iy = 0; 1761 1762 if (is_line (ep.x, ep.y, p1.x, p1.y, next.x, next.y)) { 1763 ix = p1.x; 1764 iy = p1.y; 1765 return true; 1766 } 1767 1768 if (is_line (ep.x, ep.y, p2.x, p2.y, next.x, next.y)) { 1769 ix = p2.x; 1770 iy = p2.y; 1771 return true; 1772 } 1773 1774 if (is_line (p1.x, p1.y, ep.x, ep.y, p2.x, p2.y)) { 1775 ix = ep.x; 1776 iy = ep.y; 1777 return true; 1778 } 1779 1780 if (is_line (p1.x, p1.y, next.x, next.y, p2.x, p2.y)) { 1781 ix = next.x; 1782 iy = next.y; 1783 return true; 1784 } 1785 1786 Path.find_intersection_point (ep, next, p1, p2, out cross_x, out cross_y); 1787 1788 if (fmin (ep.x, next.x) - 0.00001 <= cross_x <= fmax (ep.x, next.x) + 0.00001 1789 && fmin (ep.y, next.y) - 0.00001 <= cross_y <= fmax (ep.y, next.y) + 0.00001) { 1790 // iterate to find intersection. 1791 if (is_line (ep.x, ep.y, cross_x, cross_y, next.x, next.y) 1792 && is_line (p1.x, p1.y, cross_x, cross_y, p2.x, p2.y)) { 1793 1794 ix = cross_x; 1795 iy = cross_y; 1796 1797 return true; 1798 } 1799 } 1800 1801 return false; 1802 } 1803 1804 bool segment_intersects (Path path, EditPoint ep, EditPoint next, 1805 out double ix, out double iy, 1806 out EditPoint ia, out EditPoint ib, 1807 bool skip_points_on_points = false, 1808 bool only_offsets = false) { 1809 1810 EditPoint p1, p2; 1811 bool intersection, inter; 1812 double iix, iiy; 1813 1814 double distance, min_distance; 1815 1816 intersection = false; 1817 1818 ix = 0; 1819 iy = 0; 1820 1821 iix = 0; 1822 iiy = 0; 1823 1824 ia = new EditPoint (); 1825 ib = new EditPoint (); 1826 1827 if (path.points.size == 0) { 1828 return false; 1829 } 1830 1831 min_distance = double.MAX; 1832 p1 = path.points.get (path.points.size - 1); 1833 for (int i = 0; i < path.points.size; i++) { 1834 p2 = path.points.get (i); 1835 1836 bool is_offset = ((p1.flags & EditPoint.STROKE_OFFSET) > 0) 1837 && ((p2.flags & EditPoint.STROKE_OFFSET) > 0) 1838 && ((ep.flags & EditPoint.STROKE_OFFSET) > 0) 1839 && ((next.flags & EditPoint.STROKE_OFFSET) > 0); 1840 1841 if (!only_offsets || is_offset) { 1842 if (p1 != ep && p2 != next) { 1843 inter = segments_intersects (p1, p2, ep, next, out iix, out iiy, 1844 skip_points_on_points); 1845 1846 if (inter) { 1847 distance = Path.distance (ep.x, iix, ep.y, iiy); 1848 if (distance < min_distance) { 1849 ia = p1; 1850 ib = p2; 1851 ix = iix; 1852 iy = iiy; 1853 intersection = true; 1854 min_distance = distance; 1855 } 1856 } 1857 } 1858 } 1859 1860 p1 = p2; 1861 } 1862 1863 return intersection; 1864 } 1865 1866 /** @return true if p2 is on the line p1 to p3 */ 1867 bool is_line (double x1, double y1, double x2, double y2, double x3, double y3, double tolerance = 0.01) { 1868 return fmin (x1, x3) - 0.00001 <= x2 && x2 <= fmax (x1, x3) + 0.00001 1869 && fmin (y1, y3) - 0.00001 <= y2 && y2 <= fmax (y1, y3) + 0.00001 1870 && is_flat (x1, y1, x2, y2, x3, y3, tolerance); 1871 } 1872 1873 public static bool is_flat (double x1, double y1, double x2, double y2, double x3, double y3, double tolerance = 0.001) { 1874 double ds = Path.distance (x1, x3, y1, y3); 1875 double d1 = Path.distance (x1, x2, y1, y2); 1876 double d2 = Path.distance (x2, x3, y2, y3); 1877 double p = d1 / ds; 1878 double x = fabs ((x3 - x1) * p - (x2 - x1)) / ds; 1879 double y = fabs ((y3 - y1) * p - (y2 - y1)) / ds; 1880 double d = fabs (ds - (d1 + d2)) / ds; 1881 1882 return ds > 0.001 && d1 > 0.001 && d2 > 0.001 1883 && d < tolerance && x < tolerance && y < tolerance; 1884 } 1885 1886 // indside becomes outside in some paths 1887 void remove_points_in_stroke (PathList pl) { 1888 PathList r; 1889 1890 foreach (Path p in pl.paths) { 1891 if (remove_points_in_stroke_for_path (p, pl, out r)) { 1892 pl.append (r); 1893 remove_points_in_stroke (pl); 1894 return; 1895 } 1896 } 1897 } 1898 1899 bool remove_points_in_stroke_for_path (Path p, PathList pl, out PathList result) { 1900 EditPoint start_ep; 1901 EditPoint start_next; 1902 EditPoint start_prev; 1903 EditPoint end_ep = new EditPoint (); 1904 EditPoint end_next; 1905 EditPoint end_prev; 1906 1907 result = new PathList (); 1908 1909 for (int i = 1; i < p.points.size + 1; i++) { 1910 start_prev = p.points.get ((i - 1) % p.points.size); 1911 start_ep = p.points.get (i % p.points.size); 1912 start_next = p.points.get ((i + 1) % p.points.size); 1913 1914 if ((start_ep.flags & EditPoint.COUNTER_TO_OUTLINE) > 0) { 1915 for (int j = i; j < p.points.size + i; j++) { 1916 end_prev = p.points.get ((j - 1) % p.points.size); 1917 end_ep = p.points.get (j % p.points.size); 1918 end_next = p.points.get ((j + 1) % p.points.size); 1919 1920 1921 if ((end_ep.flags & EditPoint.COUNTER_TO_OUTLINE) > 0) { 1922 start_ep.flags = EditPoint.NONE; 1923 end_ep.flags = EditPoint.NONE; 1924 1925 if (merge_segments (pl, p, start_prev, start_ep, end_ep, end_next, out result)) { 1926 return true; 1927 } 1928 } 1929 } 1930 } 1931 1932 start_ep.flags = EditPoint.NONE; 1933 end_ep.flags = EditPoint.NONE; 1934 } 1935 1936 return false; 1937 } 1938 1939 bool merge_segments (PathList pl, 1940 Path path1, EditPoint start1, EditPoint stop1, 1941 EditPoint start2, EditPoint stop2, 1942 out PathList result) { 1943 1944 result = new PathList (); 1945 1946 PathList r1; 1947 PathList r2; 1948 1949 foreach (Path path2 in pl.paths) { 1950 if (path2 != path1) { 1951 reset_intersections (path1); 1952 reset_intersections (path2); 1953 1954 if (add_merge_intersection_point (path1, path2, start1, stop1)) { 1955 if (add_merge_intersection_point (path1, path2, start2, stop2)) { 1956 1957 r1 = get_remaining_points (path1.copy ()); 1958 r2 = get_remaining_points (path2.copy ()); 1959 1960 if (r1.paths.size != 2) { 1961 warning (@"Expecting two paths in r1 found $(r1.paths.size)\n"); 1962 reset_intersections (path1); 1963 reset_intersections (path2); 1964 return true; 1965 } 1966 1967 if (r2.paths.size != 2) { 1968 warning (@"Expecting two paths in r2 found $(r2.paths.size)\n"); 1969 reset_intersections (path1); 1970 reset_intersections (path2); 1971 return true; 1972 } 1973 1974 pl.paths.remove (path1); 1975 pl.paths.remove (path2); 1976 1977 double d1 = Path.point_distance (r1.paths.get (0).get_first_point (), 1978 r2.paths.get (0).get_first_point ()); 1979 1980 double d2 = Path.point_distance (r1.paths.get (0).get_first_point (), 1981 r2.paths.get (1).get_first_point ()); 1982 1983 Path m1, m2; 1984 1985 if (d1 > d2) { 1986 m1 = PenTool.merge_open_paths (r1.paths.get (0), r2.paths.get (0)); 1987 m2 = PenTool.merge_open_paths (r1.paths.get (1), r2.paths.get (1)); 1988 } else { 1989 m1 = PenTool.merge_open_paths (r1.paths.get (1), r2.paths.get (0)); 1990 m2 = PenTool.merge_open_paths (r1.paths.get (0), r2.paths.get (1)); 1991 } 1992 1993 result.add (m1); 1994 result.add (m2); 1995 1996 return true; 1997 } else { 1998 reset_intersections (path1); 1999 reset_intersections (path2); 2000 } 2001 } else { 2002 reset_intersections (path1); 2003 reset_intersections (path2); 2004 } 2005 } 2006 } 2007 2008 return false; 2009 } 2010 2011 void reset_intersections (Path p) { 2012 foreach (EditPoint ep in p.points) { 2013 ep.flags &= uint.MAX ^ EditPoint.INTERSECTION; 2014 ep.flags &= uint.MAX ^ EditPoint.COPIED; 2015 ep.flags &= uint.MAX ^ EditPoint.SELF_INTERSECTION; 2016 ep.deleted = false; 2017 } 2018 p.remove_points_on_points (); 2019 } 2020 2021 bool add_merge_intersection_point (Path path1, Path path2, EditPoint first, EditPoint next) { 2022 double ix, iy; 2023 bool intersection; 2024 2025 intersection = false; 2026 ix = 0; 2027 iy = 0; 2028 path2.all_segments ((p1, p2) => { 2029 int i; 2030 2031 intersection = segments_intersects (first, next, p1, p2, out ix, out iy); 2032 2033 if (intersection) { 2034 add_intersection (path1, first, next, ix, iy); 2035 add_intersection (path2, p1, p2, ix, iy); 2036 2037 i = mark_intersection_as_deleted (path1); 2038 i = mark_intersection_as_deleted (path2); 2039 } 2040 2041 return !intersection; 2042 }); 2043 2044 return intersection; 2045 } 2046 2047 bool is_inside_of_path (PointSelection ps, PathList pl, out Path outline) { 2048 outline = new Path (); 2049 foreach (Path p in pl.paths) { 2050 if (p != ps.path) { 2051 if (is_inside (ps.point, p)) { 2052 outline = p; 2053 return true; 2054 } 2055 } 2056 } 2057 2058 return false; 2059 } 2060 2061 PathList get_all_parts (PathList pl) { 2062 PathList m; 2063 bool intersects = false; 2064 PathList r = new PathList (); 2065 2066 foreach (Path p in pl.paths) { 2067 if (has_self_intersection (p)) { 2068 m = get_parts (p); 2069 r.append (m); 2070 intersects = true; 2071 } else { 2072 r.add (p); 2073 } 2074 } 2075 2076 foreach (Path p in r.paths) { 2077 p.close (); 2078 p.update_region_boundaries (); 2079 } 2080 2081 if (intersects) { 2082 return get_all_parts (r); 2083 } 2084 2085 return r; 2086 } 2087 2088 void remove_single_points (PathList pl) { 2089 PathList r = new PathList (); 2090 2091 foreach (Path p in pl.paths) { 2092 p.update_region_boundaries (); 2093 if (p.points.size < 10 2094 || p.xmax - p.xmin < 0.01 2095 || p.ymax - p.ymin < 0.01) { 2096 2097 r.add (p); 2098 } 2099 } 2100 2101 foreach (Path p in r.paths) { 2102 pl.remove (p); 2103 } 2104 } 2105 2106 public PathList merge (PathList pl) { 2107 bool error = false; 2108 PathList m; 2109 PathList r = pl; 2110 Path p1, p2; 2111 2112 r = get_all_parts (r); 2113 remove_single_points (r); 2114 2115 while (paths_has_intersection (r, out p1, out p2)) { 2116 if (task.is_cancelled ()) { 2117 return new PathList (); 2118 } 2119 2120 if (merge_path (p1, p2, out m, out error)) { 2121 r.paths.remove (p1); 2122 r.paths.remove (p2); 2123 2124 foreach (Path np in m.paths) { 2125 np.remove_points_on_points (); 2126 r.add (np); 2127 } 2128 2129 r = get_all_parts (r); 2130 remove_single_points (r); 2131 2132 if (paths_has_intersection (m, out p1, out p2)) { 2133 warning ("Paths are not merged."); 2134 error = true; 2135 } 2136 } else { 2137 warning ("Not merged."); 2138 error = true; 2139 } 2140 2141 if (error) { 2142 warning ("Merge error"); 2143 break; 2144 } 2145 } 2146 2147 if (!error) { 2148 remove_merged_parts (r); 2149 } 2150 2151 foreach (Path p in r.paths) { 2152 p.close (); 2153 p.recalculate_linear_handles (); 2154 } 2155 2156 return r; 2157 } 2158 2159 void remove_merged_parts (PathList r) { 2160 Gee.ArrayList<Path> remove = new Gee.ArrayList<Path> (); 2161 int c; 2162 2163 foreach (Path p in r.paths) { 2164 p.update_region_boundaries (); 2165 } 2166 2167 foreach (Path p in r.paths) { 2168 c = counters (r, p); 2169 2170 if (c % 2 == 0) { 2171 if (!p.is_clockwise ()) { 2172 remove.add (p); 2173 } 2174 } else { 2175 if (p.is_clockwise ()) { 2176 remove.add (p); 2177 } 2178 } 2179 } 2180 2181 foreach (Path p in remove) { 2182 r.paths.remove (p); 2183 } 2184 } 2185 2186 public PathList get_insides (PathList pl, Path path) { 2187 bool inside = false; 2188 PathList insides = new PathList (); 2189 2190 foreach (Path p in pl.paths) { 2191 if (p.points.size > 1 2192 && p != path 2193 && path.boundaries_intersecting (p)) { 2194 2195 inside = true; 2196 foreach (EditPoint ep in path.points) { 2197 if (!is_inside (ep, p)) { 2198 inside = false; 2199 break; 2200 } 2201 } 2202 2203 if (inside) { 2204 insides.add (p); // add the flat inside to the list 2205 } 2206 } 2207 } 2208 2209 return insides; 2210 } 2211 2212 public int counters (PathList pl, Path path) { 2213 int inside_count = 0; 2214 bool inside; 2215 2216 foreach (Path p in pl.paths) { 2217 inside = true; 2218 2219 if (p.points.size > 1 2220 && p != path 2221 && path.boundaries_intersecting (p)) { 2222 2223 foreach (EditPoint ep in path.points) { 2224 if (!is_inside (ep, p)) { 2225 inside = false; 2226 break; 2227 } 2228 } 2229 2230 if (inside) { 2231 inside_count++; 2232 } 2233 } 2234 } 2235 2236 return inside_count; 2237 } 2238 2239 public static bool is_inside (EditPoint point, Path path) { 2240 EditPoint prev; 2241 bool inside = false; 2242 2243 if (path.points.size <= 1) { 2244 return false; 2245 } 2246 2247 prev = path.points.get (path.points.size - 1); 2248 2249 foreach (EditPoint p in path.points) { 2250 if ((fabs (p.x - point.x) < 0.1 && fabs (p.y - point.y) < 0.1) 2251 || (fabs (prev.x - point.x) < 0.1 && fabs (prev.y - point.y) < 0.1)) { 2252 return true; 2253 } else if ((p.y > point.y) != (prev.y > point.y) 2254 && point.x < (prev.x - p.x) * (point.y - p.y) / (prev.y - p.y) + p.x) { 2255 inside = !inside; 2256 } 2257 2258 prev = p; 2259 } 2260 2261 return inside; 2262 } 2263 2264 public int insides (EditPoint point, Path path) { 2265 EditPoint prev; 2266 int inside = 0; 2267 2268 if (path.points.size <= 1) { 2269 return 0; 2270 } 2271 2272 prev = path.get_last_point (); 2273 2274 foreach (EditPoint start in path.points) { 2275 if (start.x == point.x && point.y == start.y) { 2276 inside++; 2277 } else if ((start.y > point.y) != (prev.y > point.y) 2278 && point.x < (prev.x - start.x) * (point.y - start.y) / (prev.y - start.y) + start.x) { 2279 inside++; 2280 } 2281 2282 prev = start; 2283 } 2284 2285 return inside; 2286 } 2287 2288 bool merge_path (Path path1, Path path2, out PathList merged_paths, out bool error) { 2289 IntersectionList intersections; 2290 EditPoint ep1, next, p1, p2, pp1, pp2; 2291 Path path, other, merged; 2292 PathList r, other_paths, result; 2293 bool intersects; 2294 int s, i; 2295 double ix, iy, iix, iiy; 2296 bool merge = false; 2297 EditPoint intersection_point, other_intersection_point; 2298 bool path1_direction, path2_direction; 2299 2300 error = false; 2301 merged_paths = new PathList (); 2302 intersections = new IntersectionList (); 2303 2304 if (path1.points.size == 0) { 2305 return false; 2306 } 2307 2308 if (path2.points.size == 0) { 2309 return false; 2310 } 2311 2312 reset_intersections (path1); 2313 reset_intersections (path2); 2314 2315 iix = 0; 2316 iiy = 0; 2317 2318 result = new PathList (); 2319 2320 if (path1.points.size == 0 || path2.points.size == 0) { 2321 warning ("No points in path."); 2322 error = true; 2323 return false; 2324 } 2325 2326 if (!path1.boundaries_intersecting (path2)) { 2327 return false; 2328 } 2329 2330 Path original_path1 = path1.copy (); 2331 Path original_path2 = path2.copy (); 2332 2333 s = 0; 2334 foreach (EditPoint e in original_path1.points) { 2335 if (!is_inside (e, original_path2) 2336 && insides (e, original_path1) == 1) { // FIXME: later as well 2337 break; 2338 } 2339 s++; 2340 } 2341 2342 if (s >= path1.points.size) { 2343 Path t; 2344 t = original_path1; 2345 original_path1 = original_path2; 2346 original_path2 = t; 2347 s = 0; 2348 foreach (EditPoint e in original_path1.points) { 2349 if (!is_inside (e, original_path2)) { 2350 break; 2351 } 2352 s++; 2353 } 2354 } 2355 2356 if (s >= original_path1.points.size) { 2357 warning ("No start point found."); 2358 error = true; 2359 return false; 2360 } 2361 2362 path = original_path1; 2363 other = original_path2; 2364 2365 other_paths = new PathList (); 2366 r = new PathList (); 2367 other_paths.add (path); 2368 other_paths.add (other); 2369 intersects = false; 2370 p1 = new EditPoint (); 2371 p2 = new EditPoint (); 2372 pp1 = new EditPoint (); 2373 pp2 = new EditPoint (); 2374 2375 ix = 0; 2376 iy = 0; 2377 i = s; 2378 merged = new Path (); 2379 2380 path1_direction = is_clockwise (original_path1); 2381 path2_direction = is_clockwise (original_path1); 2382 2383 while (true) { 2384 ep1 = path.points.get (i % path.points.size); 2385 next = path.points.get ((i + 1) % path.points.size); 2386 2387 if ((ep1.flags & EditPoint.COPIED) > 0) { 2388 if (merge) { 2389 merged.close (); 2390 result.add (merged.copy ()); 2391 } 2392 2393 merged = new Path (); 2394 2395 bool find_parts = false; 2396 Intersection new_start = new Intersection.empty (); 2397 foreach (Intersection inter in intersections.points) { 2398 if (!inter.done && !find_parts) { 2399 find_parts = true; 2400 new_start = inter; 2401 break; 2402 } 2403 } 2404 2405 if (task.is_cancelled ()) { 2406 return false; 2407 } 2408 2409 if (!find_parts) { 2410 break; // done, no more parts to merge 2411 } else { 2412 // set start point for next part 2413 path = new_start.other_path; 2414 2415 if (path.points.size == 0) { 2416 warning ("No points."); 2417 error = true; 2418 return false; 2419 } 2420 2421 i = index_of (path, new_start.get_point (path)); 2422 2423 if (i < 0) { 2424 warning ("Start point not found."); 2425 error = true; 2426 return false; 2427 } 2428 2429 ep1 = path.points.get (i % path.points.size); 2430 next = path.points.get ((i + 1) % path.points.size); 2431 2432 if ((ep1.flags & EditPoint.INTERSECTION) == 0) { 2433 warning ("Not starting on an intersection point."); 2434 error = true; 2435 return false; 2436 } 2437 2438 new_start.done = true; 2439 } 2440 } 2441 2442 intersects = false; 2443 2444 double dm; 2445 double d; 2446 2447 if ((ep1.flags & EditPoint.INTERSECTION) > 0) { 2448 Intersection current_intersection; 2449 bool continue_on_other_path; 2450 current_intersection = intersections.get_point (ep1, out continue_on_other_path); 2451 current_intersection.done = true; 2452 2453 // take the other part of an intersection 2454 if ((next.flags & EditPoint.COPIED) != 0) { 2455 bool next_is_intersection = false; 2456 bool next_continue_on_other_path; 2457 2458 next_is_intersection = ((next.flags & EditPoint.INTERSECTION) > 0); 2459 2460 if (next_is_intersection) { 2461 Intersection next_intersection = intersections.get_point (next, out next_continue_on_other_path); 2462 2463 ep1.flags |= EditPoint.COPIED; 2464 merged.add_point (ep1.copy ()); 2465 2466 if (!next_intersection.done) { 2467 EditPoint new_start_point; 2468 2469 path = next_continue_on_other_path 2470 ? next_intersection.other_path : next_intersection.path; 2471 2472 new_start_point = next_continue_on_other_path 2473 ? next_intersection.other_point : next_intersection.point; 2474 2475 i = index_of (path, new_start_point); 2476 2477 if (i < 0) { 2478 warning ("Point not found in path.\n"); 2479 error = true; 2480 return false; 2481 } 2482 2483 ep1 = path.points.get (i % path.points.size); 2484 next = path.points.get ((i + 1) % path.points.size); 2485 } else { 2486 warning ("Part is already created.\n"); 2487 error = true; 2488 return false; 2489 } 2490 } else { 2491 ep1.flags |= EditPoint.COPIED; 2492 merged.add_point (ep1.copy ()); 2493 2494 EditPoint new_start_point; 2495 2496 if ((next.flags & EditPoint.COPIED) > 0) { 2497 path = current_intersection.get_other_path (path); 2498 new_start_point = current_intersection.get_point (path); 2499 i = index_of (path, new_start_point); 2500 2501 if (i < 0) { 2502 warning ("Point not found in path."); 2503 error = true; 2504 return false; 2505 } 2506 2507 ep1 = path.points.get (i % path.points.size); 2508 next = path.points.get ((i + 1) % path.points.size); 2509 2510 if ((next.flags & EditPoint.INTERSECTION) > 0) { 2511 warning ("Wrong type."); 2512 error = true; 2513 return false; 2514 } 2515 2516 if ((next.flags & EditPoint.COPIED) > 0) { 2517 merged.add_point (ep1.copy ()); 2518 continue; 2519 } 2520 } else { 2521 ep1.flags |= EditPoint.COPIED; 2522 merged.add_point (ep1.copy ()); 2523 } 2524 } 2525 } else { 2526 ep1.flags |= EditPoint.COPIED; 2527 2528 if (path1_direction == path2_direction) { 2529 if (!is_inside (ep1, original_path1)) { 2530 merged.add_point (ep1.copy ()); 2531 } 2532 } else { 2533 merged.add_point (ep1.copy ()); 2534 } 2535 } 2536 2537 current_intersection.done = true; 2538 } else { 2539 // find new intersection 2540 dm = double.MAX; 2541 foreach (Path o in other_paths.paths) { 2542 bool inter = segment_intersects (o, ep1, next, out iix, out iiy, 2543 out pp1, out pp2, false, false); 2544 d = Path.distance (ep1.x, iix, ep1.y, iiy); 2545 if (d < dm && inter) { 2546 other = o; 2547 dm = d; 2548 intersects = true; 2549 p1 = pp1; 2550 p2 = pp2; 2551 ix = iix; 2552 iy = iiy; 2553 } 2554 2555 if (d < 0.0001) { 2556 intersects = false; 2557 } 2558 } 2559 2560 if (intersects) { 2561 merged.add_point (ep1.copy ()); 2562 ep1.flags |= EditPoint.COPIED; 2563 2564 intersection_point = add_intersection (path, ep1, next, ix, iy); 2565 other_intersection_point = add_intersection (other, p1, p2, ix, iy); 2566 2567 bool g = false; 2568 foreach (Intersection old_intersection in intersections.points) { 2569 if (old_intersection.point == intersection_point 2570 || old_intersection.other_point == other_intersection_point) { 2571 old_intersection.done = true; 2572 g = true; 2573 } 2574 } 2575 2576 if (!g) { 2577 Intersection ip = new Intersection (intersection_point, path, other_intersection_point, other); 2578 intersections.points.add (ip); 2579 } 2580 2581 merged.add_point (intersection_point.copy ()); 2582 intersection_point.flags |= EditPoint.COPIED; 2583 2584 i = index_of (other, other_intersection_point); 2585 2586 if (i < 0) { 2587 warning (@"Point not found ($i)."); 2588 break; 2589 } 2590 2591 path = other; 2592 merge = true; 2593 } else { 2594 ep1.flags |= EditPoint.COPIED; 2595 merged.add_point (ep1.copy ()); 2596 2597 PointSelection ps; 2598 Path outline; 2599 2600 // remove points inside of path 2601 if (is_clockwise (original_path2)) { 2602 ps = new PointSelection (ep1, merged); 2603 if (is_inside_of_path (ps, result, out outline)) { 2604 ep1.deleted = true; 2605 } 2606 } 2607 } 2608 } 2609 2610 i++; 2611 } 2612 2613 if (merge) { 2614 original_path1.remove_points_on_points (); 2615 original_path2.remove_points_on_points (); 2616 original_path1.remove_deleted_points (); 2617 original_path2.remove_deleted_points (); 2618 2619 foreach (Path np in result.paths) { 2620 Path p = np.copy (); 2621 bool has_direction = true; 2622 2623 p.remove_points_on_points (); 2624 2625 if (has_direction) { 2626 p.close (); 2627 reset_intersections (p); 2628 merged_paths.append (get_parts (p)); 2629 p.update_region_boundaries (); 2630 p.recalculate_linear_handles (); 2631 } 2632 } 2633 } 2634 2635 return merge && !error; 2636 } 2637 2638 int index_of (Path p, EditPoint ep) { 2639 int i = 0; 2640 foreach (EditPoint e in p.points) { 2641 if (e == ep) { 2642 return i; 2643 } 2644 i++; 2645 } 2646 2647 return -1; 2648 } 2649 2650 public int counters_in_point_in_path (Path p, EditPoint ep) { 2651 int inside_count = 0; 2652 bool inside; 2653 2654 if (p.points.size > 1) { 2655 inside = true; 2656 2657 if (!is_inside (ep, p)) { 2658 inside = false; 2659 } 2660 2661 if (inside) { 2662 inside_count++; 2663 } 2664 } 2665 2666 return inside_count; 2667 } 2668 2669 int mark_intersection_as_deleted (Path path) { 2670 int i = 0; 2671 2672 foreach (EditPoint p in path.points) { 2673 if ((p.flags & EditPoint.INTERSECTION) > 0) { 2674 p.deleted = true; 2675 i++; 2676 } 2677 } 2678 2679 return i; 2680 } 2681 2682 /** @param n number of interrsections to find per path. */ 2683 bool has_intersection (Path path1, Path path2) { 2684 bool intersection = false; 2685 2686 if (!path1.boundaries_intersecting (path2)) { 2687 return false; 2688 } 2689 2690 path1.all_segments ((ep1, ep2) => { 2691 double ix, iy; 2692 EditPoint p1, p2; 2693 bool i; 2694 2695 i = segment_intersects (path2, ep1, ep2, out ix, out iy, 2696 out p1, out p2, true); 2697 2698 if (i) { 2699 intersection = true; 2700 } 2701 2702 return !intersection; 2703 }); 2704 2705 return intersection; 2706 } 2707 2708 bool paths_has_intersection (PathList r, out Path path1, out Path path2) { 2709 int i, j; 2710 path1 = new Path (); 2711 path2 = new Path (); 2712 2713 i = 0; 2714 foreach (Path p1 in r.paths) { 2715 2716 j = 0; 2717 foreach (Path p2 in r.paths) { 2718 if (p1 != p2) { 2719 if (has_intersection (p1, p2)) { 2720 path1 = p1; 2721 path2 = p2; 2722 return true; 2723 } 2724 } 2725 j++; 2726 } 2727 i++; 2728 } 2729 return false; 2730 } 2731 2732 public bool has_points_outside (PathList pl, Path p) { 2733 if (pl.paths.size != 2) { 2734 warning ("Stroke should only create two parts."); 2735 return false; 2736 } 2737 2738 foreach (Path path in pl.paths) { 2739 if (path != p) { 2740 foreach (EditPoint ep in p.points) { 2741 if (!is_inside (ep, path)) { 2742 return true; 2743 } 2744 } 2745 } 2746 } 2747 2748 return false; 2749 } 2750 2751 bool is_clockwise (Path p) { 2752 double sum = 0; 2753 EditPoint p1, p2; 2754 2755 EditPointHandle l, r; 2756 2757 p.recalculate_linear_handles (); 2758 2759 if (p.points.size < 3) { 2760 return true; 2761 } 2762 2763 for (int i = 0; i < p.points.size; i++) { 2764 p1 = p.points.get (i); 2765 p2 = p.points.get ((i + 1) % p.points.size); 2766 2767 l = p1.get_left_handle (); 2768 r = p1.get_right_handle (); 2769 if (!(fabs (l.angle - r.angle) < 0.0001 && l.length > 0.01 && r.length > 0.01)) { 2770 sum += (p2.x - p1.x) * (p2.y + p1.y); 2771 } 2772 } 2773 2774 return sum > 0; 2775 } 2776 2777 public PathList create_stroke (Path original_path, 2778 double thickness, bool fast) { 2779 2780 PathList pl; 2781 EditPoint p1, p2, p3; 2782 EditPoint previous, previous_inside, start, start_inside; 2783 2784 Path side1, side2; 2785 2786 double x, y, x2, y2, x3, y3; 2787 int size; 2788 bool flat, f_next, f_bigger; 2789 int i; 2790 2791 double tolerance; 2792 double step_increment; 2793 double step_size; 2794 EditPoint corner1, corner1_inside; 2795 double step; 2796 double min_increment; 2797 2798 EditPointHandle l, r; 2799 2800 Path path = original_path.copy (); 2801 2802 int keep; 2803 bool on_curve; 2804 2805 pl = new PathList (); 2806 size = path.is_open () ? path.points.size - 1 : path.points.size; 2807 2808 side1 = new Path (); 2809 side2 = new Path (); 2810 2811 foreach (EditPoint ph in path.points) { 2812 if (ph.type == PointType.HIDDEN) { 2813 ph.type = PointType.CUBIC; 2814 } 2815 } 2816 path.remove_deleted_points (); 2817 2818 if (path.points.size < 2) { 2819 return pl; 2820 } 2821 2822 previous = new EditPoint (); 2823 previous_inside = new EditPoint (); 2824 corner1 = new EditPoint (); 2825 corner1_inside = new EditPoint (); 2826 2827 if (path.is_open () || fast) { 2828 p1 = path.points.get (0); 2829 p2 = path.points.get (1 % path.points.size); 2830 2831 get_segment (thickness, 0, 0.00001, p1, p2, out start); 2832 get_segment (-thickness, 0, 0.00001, p1, p2, out start_inside); 2833 2834 previous = start.copy (); 2835 previous_inside = start_inside.copy (); 2836 2837 previous.flags |= EditPoint.CURVE_KEEP; 2838 previous_inside.flags |= EditPoint.CURVE_KEEP; 2839 2840 side1.add_point (previous); 2841 side2.add_point (previous_inside); 2842 } 2843 2844 min_increment = 0.013; 2845 2846 for (i = 0; i < size; i++) { 2847 p1 = path.points.get (i % path.points.size); 2848 p2 = path.points.get ((i + 1) % path.points.size); 2849 p3 = path.points.get ((i + 2) % path.points.size); 2850 2851 if (task.is_cancelled ()) { 2852 return new PathList (); 2853 } 2854 2855 tolerance = 0.01; // 0.13 / sqrt (stroke_width) 2856 step_increment = 1.05; 2857 step_size = 0.039; // / stroke_width; 2858 2859 corner1 = new EditPoint (); 2860 2861 if (p1.type == PointType.HIDDEN 2862 || p2.type == PointType.HIDDEN) { 2863 continue; 2864 } 2865 2866 get_segment (thickness, 0, 0.00001, p1, p2, out start); 2867 get_segment (-thickness, 0, 0.00001, p1, p2, out start_inside); 2868 2869 previous = start.copy (); 2870 previous_inside = start_inside.copy (); 2871 2872 previous.flags |= EditPoint.CURVE | EditPoint.SEGMENT_END; 2873 previous_inside.flags |= EditPoint.CURVE | EditPoint.SEGMENT_END; 2874 2875 side1.add_point (previous); 2876 side2.add_point (previous_inside); 2877 2878 step = step_size; 2879 keep = 0; 2880 step_size = 0.01; 2881 2882 while (step < 1 - 2 * step_size) { 2883 Path.get_point_for_step (p1, p2, step, out x, out y); 2884 Path.get_point_for_step (p1, p2, step + step_size, out x2, out y2); 2885 Path.get_point_for_step (p1, p2, step + 2 * step_size, out x3, out y3); 2886 2887 flat = is_flat (x, y, x2, y2, x3, y3, tolerance); 2888 2889 Path.get_point_for_step (p1, p2, step, out x, out y); 2890 Path.get_point_for_step (p1, p2, step + step_size / step_increment, out x2, out y2); 2891 Path.get_point_for_step (p1, p2, step + 2 * step_size / step_increment, out x3, out y3); 2892 2893 f_next = is_flat (x, y, x2, y2, x3, y3, tolerance); 2894 2895 Path.get_point_for_step (p1, p2, step, out x, out y); 2896 Path.get_point_for_step (p1, p2, step + step_size * step_increment, out x2, out y2); 2897 Path.get_point_for_step (p1, p2, step + 2 * step_size * step_increment, out x3, out y3); 2898 2899 f_bigger = is_flat (x, y, x2, y2, x3, y3, tolerance); 2900 2901 if (!flat && !f_next && step_size > min_increment) { 2902 step_size /= step_increment; 2903 continue; 2904 } 2905 2906 if (flat && f_bigger && step_size < 0.1) { 2907 step_size *= step_increment; 2908 continue; 2909 } 2910 2911 get_segment (thickness, step, step_size, p1, p2, out corner1); 2912 get_segment (-thickness, step, step_size, p1, p2, out corner1_inside); 2913 2914 previous.get_right_handle ().length *= step_size; 2915 corner1.get_left_handle ().length *= step_size; 2916 previous_inside.get_right_handle ().length *= step_size; 2917 corner1_inside.get_left_handle ().length *= step_size; 2918 2919 previous = corner1.copy (); 2920 previous_inside = corner1_inside.copy (); 2921 2922 if (keep == 0 && step > 0.3) { // keep two points per segment 2923 on_curve = true; 2924 keep++; 2925 } else if (keep == 1 && step > 0.6) { 2926 on_curve = true; 2927 keep++; 2928 } else { 2929 on_curve = false; 2930 } 2931 2932 if (!on_curve) { 2933 previous.flags |= EditPoint.CURVE; 2934 previous_inside.flags |= EditPoint.CURVE; 2935 } else { 2936 previous.flags |= EditPoint.CURVE_KEEP; 2937 previous_inside.flags |= EditPoint.CURVE_KEEP; 2938 } 2939 2940 side1.add_point (previous); 2941 side2.add_point (previous_inside); 2942 2943 step += step_size; 2944 } 2945 2946 previous.get_right_handle ().length *= step_size; 2947 corner1.get_left_handle ().length *= step_size; 2948 previous_inside.get_right_handle ().length *= step_size; 2949 corner1_inside.get_left_handle ().length *= step_size; 2950 2951 get_segment (thickness, 1 - 0.00001, 0.00001, p1, p2, out corner1); 2952 get_segment (-thickness, 1 - 0.00001, 0.00001, p1, p2, out corner1_inside); 2953 2954 previous = corner1.copy (); 2955 previous_inside = corner1_inside.copy (); 2956 2957 previous.get_right_handle ().length *= step_size; 2958 previous.get_left_handle ().length *= step_size; 2959 previous_inside.get_right_handle ().length *= step_size; 2960 previous_inside.get_left_handle ().length *= step_size; 2961 2962 previous.flags |= EditPoint.CURVE | EditPoint.SEGMENT_END; 2963 previous_inside.flags |= EditPoint.CURVE | EditPoint.SEGMENT_END; 2964 2965 side1.add_point (previous); 2966 side2.add_point (previous_inside); 2967 2968 l = p2.get_left_handle (); 2969 r = p2.get_right_handle (); 2970 2971 if (fabs ((l.angle + r.angle + PI) % (2 * PI) - PI) > 0.005) { 2972 if (!path.is_open () || i < size - 1) { 2973 get_segment (thickness, 0, 0.00001, p2, p3, out start); 2974 add_corner (side1, previous, start, p2.copy (), thickness); 2975 2976 get_segment (-thickness, 0, 0.00001, p2, p3, out start); 2977 add_corner (side2, previous_inside, start, p2.copy (), thickness); 2978 } 2979 } 2980 2981 if (fast) { 2982 EditPoint s1, s2; 2983 bool open; 2984 2985 convert_to_curve (side1); 2986 convert_to_curve (side2); 2987 2988 side2.reverse (); 2989 s1 = side1.get_last_point ().copy (); 2990 s2 = side2.get_first_point ().copy (); 2991 2992 s1.flags &= EditPoint.CURVE ^ EditPoint.ALL; 2993 s2.flags &= EditPoint.CURVE ^ EditPoint.ALL; 2994 2995 s1.convert_to_line (); 2996 s2.convert_to_line (); 2997 2998 open = path.is_open (); 2999 3000 if (!open) { 3001 path.reopen (); 3002 } 3003 3004 pl.append (merge_stroke_parts (path, side1, side2)); 3005 3006 if (!open) { 3007 path.close (); 3008 } 3009 3010 side1 = new Path (); 3011 side2 = new Path (); 3012 3013 get_segment (thickness, 0, 0.00001, p2, p3, out start); 3014 get_segment (-thickness, 0, 0.00001, p2, p3, out start_inside); 3015 3016 previous = start.copy (); 3017 previous_inside = start_inside.copy (); 3018 3019 previous.flags |= EditPoint.CURVE; 3020 previous_inside.flags |= EditPoint.CURVE; 3021 3022 side1.add_point (previous); 3023 side2.add_point (previous_inside); 3024 } 3025 } 3026 3027 if (!fast) { 3028 side1.remove_points_on_points (); 3029 side2.remove_points_on_points (); 3030 3031 convert_to_curve (side1); 3032 convert_to_curve (side2); 3033 3034 side2.reverse (); 3035 3036 pl = merge_stroke_parts (path, side1, side2); 3037 } 3038 3039 if (fast) { 3040 foreach (Path p in pl.paths) { 3041 p.close (); 3042 convert_to_curve (p); 3043 } 3044 } 3045 3046 return pl; 3047 } 3048 3049 void convert_to_curve (Path path) { 3050 if (path.is_open ()) { 3051 path.get_first_point ().flags &= EditPoint.ALL ^ EditPoint.CURVE; 3052 path.get_last_point ().flags &= EditPoint.ALL ^ EditPoint.CURVE; 3053 } 3054 3055 path.recalculate_linear_handles (); 3056 path.remove_points_on_points (); 3057 3058 foreach (EditPoint ep in path.points) { 3059 if ((ep.flags & EditPoint.SEGMENT_END) == 0) { 3060 if ((ep.flags & EditPoint.CURVE) > 0 || (ep.flags & EditPoint.CURVE_KEEP) > 0) { 3061 ep.convert_to_curve (); 3062 } 3063 } 3064 } 3065 3066 if (task.is_cancelled ()) { 3067 return; 3068 } 3069 3070 foreach (EditPoint ep in path.points) { 3071 if ((ep.flags & EditPoint.SEGMENT_END) == 0) { 3072 if ((ep.flags & EditPoint.CURVE) > 0 || (ep.flags & EditPoint.CURVE_KEEP) > 0) { 3073 ep.set_tie_handle (true); 3074 } 3075 } 3076 } 3077 3078 if (task.is_cancelled ()) { 3079 return; 3080 } 3081 3082 foreach (EditPoint ep in path.points) { 3083 if ((ep.flags & EditPoint.SEGMENT_END) == 0) { 3084 if ((ep.flags & EditPoint.CURVE) > 0 || (ep.flags & EditPoint.CURVE_KEEP) > 0) { 3085 ep.process_tied_handle (); 3086 } 3087 } 3088 } 3089 } 3090 3091 public void get_segment (double stroke_thickness, double step, double step_size, 3092 EditPoint p1, EditPoint p2, out EditPoint ep1) { 3093 3094 double thickness = stroke_thickness / 2; 3095 Path overlay; 3096 double x, y, x2, y2, x3, y3; 3097 EditPoint corner1, corner2, corner3; 3098 PointType type; 3099 3100 Path.get_point_for_step (p1, p2, step, out x, out y); 3101 Path.get_point_for_step (p1, p2, step + step_size, out x2, out y2); 3102 Path.get_point_for_step (p1, p2, step + 2 * step_size, out x3, out y3); 3103 3104 overlay = new Path (); 3105 3106 type = p1.get_right_handle ().type; 3107 corner1 = new EditPoint (x, y, type); 3108 corner2 = new EditPoint (x2, y2, type); 3109 corner3 = new EditPoint (x3, y3, type); 3110 3111 corner2.convert_to_line (); 3112 3113 overlay.add_point (corner1); 3114 overlay.add_point (corner2); 3115 overlay.add_point (corner3); 3116 3117 overlay.close (); 3118 overlay.recalculate_linear_handles (); 3119 3120 move_segment (corner1, corner2, thickness); 3121 3122 ep1 = corner2; 3123 } 3124 3125 public PathList merge_stroke_parts (Path p, Path side1, Path side2) { 3126 Path merged = new Path (); 3127 PathList paths = new PathList (); 3128 3129 if (!p.is_open ()) { 3130 side1.update_region_boundaries (); 3131 paths.add (side1); 3132 side2.update_region_boundaries (); 3133 paths.add (side2); 3134 side1.close (); 3135 side2.close (); 3136 } else if (p.is_open ()) { 3137 side2.reverse (); 3138 merged = merge_strokes (p, side1, side2); 3139 merged.close (); 3140 merged.update_region_boundaries (); 3141 paths.add (merged); 3142 merged.reverse (); 3143 } else { 3144 warning ("Can not create stroke."); 3145 paths.add (p); 3146 } 3147 3148 return paths; 3149 } 3150 } 3151 3152 } 3153