The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Path.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/Path.vala.
Speed optimizations and benchmarks
1 /* 2 Copyright (C) 2012, 2013, 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 Direction { 21 CLOCKWISE, 22 COUNTER_CLOCKWISE 23 } 24 25 public class Path : GLib.Object { 26 27 public Gee.ArrayList<EditPoint> points { 28 get { 29 if (control_points == null) { 30 control_points = new Gee.ArrayList<EditPoint> (); 31 BirdFontFile.parse_path_data (point_data, this); 32 point_data = ""; 33 } 34 35 return (!) control_points; 36 } 37 38 set { 39 control_points = value; 40 } 41 } 42 43 public Gee.ArrayList<EditPoint>? control_points = null; 44 45 EditPoint? last_point = null; 46 47 /** Path boundaries */ 48 public double xmax = Glyph.CANVAS_MIN; 49 public double xmin = Glyph.CANVAS_MAX; 50 public double ymax = Glyph.CANVAS_MIN; 51 public double ymin = Glyph.CANVAS_MAX; 52 53 /** Stroke width */ 54 public double stroke = 0; 55 public LineCap line_cap = LineCap.BUTT; 56 public PathList? full_stroke = null; 57 PathList? fast_stroke = null; 58 StrokeTask? stroke_creator; 59 60 /** Fill property for closed paths with stroke. */ 61 public bool fill = false; 62 63 bool edit = true; 64 bool open = true; 65 66 public bool direction_is_set = false; 67 bool no_derived_direction = false; 68 bool clockwise_direction = true; 69 70 // Iterate over each pixel in a path 71 public delegate bool RasterIterator (double x, double y, double step); 72 73 public delegate bool SegmentIterator (EditPoint start, EditPoint stop); 74 75 /** The stroke of an outline when the path is not filled. */ 76 public static double stroke_width = 0; 77 public static bool show_all_line_handles = true; 78 public static bool fill_open_path {get; set;} 79 80 public double rotation = 0; 81 public double skew = 0; 82 83 public bool hide_end_handle = true; 84 public bool highlight_last_segment = false; 85 86 public string point_data = ""; 87 88 public Color? color = null; 89 public Color? stroke_color = null; 90 91 public Gradient? gradient = null; 92 93 public Path () { 94 string width; 95 96 if (unlikely (stroke_width < 1)) { 97 width = Preferences.get ("stroke_width"); 98 if (width != "") { 99 stroke_width = double.parse (width); 100 } 101 } 102 103 if (stroke_width < 1) { 104 stroke_width = 1; 105 } 106 } 107 108 public bool is_filled () { 109 return fill; 110 } 111 112 public void set_fill (bool f) { 113 fill = f; 114 } 115 116 public EditPoint get_first_point () { 117 if (unlikely (points.size == 0)) { 118 warning ("No point"); 119 return new EditPoint (); 120 } 121 122 return points.get (0); 123 } 124 125 public EditPoint get_last_visible_point () { 126 EditPoint e; 127 128 if (unlikely (points.size == 0)) { 129 warning ("No point"); 130 return new EditPoint (); 131 } 132 133 for (int i = points.size - 1; i >= 0; i--) { 134 e = points.get (i); 135 if (e.type != PointType.HIDDEN) { 136 return e; 137 } 138 } 139 140 warning ("Only hidden points"); 141 return new EditPoint (); 142 } 143 144 public EditPoint get_last_point () { 145 if (unlikely (points.size == 0)) { 146 warning ("No point"); 147 return new EditPoint (); 148 } 149 150 return points.get (points.size - 1); 151 } 152 153 public bool has_direction () { 154 return direction_is_set; 155 } 156 157 public bool empty () { 158 return points.size == 0; 159 } 160 161 public void set_stroke (double width) { 162 stroke = width; 163 } 164 165 public void draw_boundaries (Context cr) { 166 double x = Glyph.reverse_path_coordinate_x (xmin); 167 double y = Glyph.reverse_path_coordinate_y (ymin); 168 double x2 = Glyph.reverse_path_coordinate_x (xmax); 169 double y2 = Glyph.reverse_path_coordinate_y (ymax); 170 171 cr.save (); 172 173 Theme.color (cr, "Default Background"); 174 cr.set_line_width (2); 175 cr.rectangle (x, y, x2 - x, y2 - y); 176 cr.stroke (); 177 178 cr.restore (); 179 } 180 181 public void draw_outline (Context cr) { 182 unowned EditPoint? n = null; 183 unowned EditPoint en; 184 unowned EditPoint em; 185 int i; 186 187 if (points.size < 2) { 188 return; 189 } 190 191 cr.new_path (); 192 193 // draw lines 194 i = 0; 195 foreach (EditPoint e in points) { 196 if (n != null) { 197 en = (!) n; 198 if (!highlight_last_segment || i != points.size - 1) { 199 draw_next (en, e, cr); 200 } 201 } 202 203 n = e; 204 i++; 205 } 206 207 // close path 208 if (!is_open () && n != null) { 209 if (highlight_last_segment) { 210 cr.stroke (); 211 en = points.get (points.size - 1).get_link_item (); 212 em = points.get (0).get_link_item (); 213 draw_next (en, em, cr); 214 cr.stroke (); 215 } else { 216 en = (!) n; 217 em = points.get (0).get_link_item (); 218 draw_next (en, em, cr); 219 cr.stroke (); 220 } 221 } else { 222 cr.stroke (); 223 } 224 225 // draw highlighted segment 226 if (highlight_last_segment && points.size >= 2) { 227 draw_next (points.get (points.size - 2), points.get (points.size - 1), cr, true); 228 cr.stroke (); 229 } 230 } 231 232 public void draw_edit_points (Context cr) { 233 if (is_editable ()) { 234 // control points for curvature 235 foreach (EditPoint e in points) { 236 if (show_all_line_handles || e.selected_point || e.selected_handle > 0) { 237 draw_edit_point_handles (e, cr); 238 } 239 } 240 241 // control points 242 foreach (EditPoint e in points) { 243 draw_edit_point (e, cr); 244 } 245 } 246 } 247 248 /** Add all control points for a path to the cairo context. 249 * Call Context.new_path (); before this method and Context.fill () 250 * to show the path. 251 */ 252 public void draw_path (Context cr, Glyph glyph, Color? color = null) { 253 unowned EditPoint? n = null; 254 unowned EditPoint en; 255 unowned EditPoint em; 256 Color c; 257 double center_x, center_y; 258 double ex, ey; 259 260 if (points.size == 0){ 261 return; 262 } 263 264 center_x = glyph.allocation.width / 2.0; 265 center_y = glyph.allocation.height / 2.0; 266 267 ex = center_x + points.get (0).x; 268 ey = center_y - points.get (0).y; 269 270 cr.move_to (ex, ey); 271 272 // draw lines 273 foreach (EditPoint e in points) { 274 if (n != null) { 275 en = (!) n; 276 draw_next (en, e, cr); 277 } 278 279 n = e; 280 } 281 282 // close path 283 if (!is_open () && points.size >= 2 && n != null) { 284 en = (!) n; 285 em = points.get (0).get_link_item (); 286 draw_next (en, em, cr); 287 } 288 289 // fill path 290 cr.close_path (); 291 292 if (this.color != null) { 293 c = (!) this.color; 294 cr.set_source_rgba (c.r, c.g, c.b, c.a); 295 } else if (color != null) { 296 c = (!) color; 297 cr.set_source_rgba (c.r, c.g, c.b, c.a); 298 } else { 299 if (is_clockwise ()) { 300 Theme.color_opacity (cr, "Selected Objects", 0.4); 301 } else { 302 Theme.color_opacity (cr, "Selected Objects", 0.8); 303 } 304 } 305 } 306 307 public void draw_orientation_arrow (Context cr, double opacity) { 308 EditPoint top = new EditPoint (); 309 double max = Glyph.CANVAS_MIN; 310 Text arrow; 311 double x, y, angle; 312 double size = 50 * Glyph.ivz (); 313 314 foreach (EditPoint e in points) { 315 if (e.y > max) { 316 max = e.y; 317 top = e; 318 } 319 } 320 321 arrow = new Text ("orientation_arrow", size); 322 arrow.load_font ("icons.bf"); 323 arrow.use_cache (false); 324 325 Theme.text_color_opacity (arrow, "Highlighted 1", opacity); 326 327 angle = top.get_right_handle ().angle; 328 x = Glyph.xc () + top.x + cos (angle + PI / 2) * 10 * Glyph.ivz (); 329 y = Glyph.yc () - top.y - sin (angle + PI / 2) * 10 * Glyph.ivz (); 330 331 if (points.size > 0) { 332 cr.save (); 333 cr.translate (x, y); 334 cr.rotate (-angle); 335 cr.translate (-x, -y); 336 337 arrow.draw_at_baseline (cr, x, y); 338 339 cr.restore (); 340 } 341 } 342 343 private void draw_next (EditPoint e, EditPoint en, Context cr, bool highlighted = false) { 344 PointType r = e.get_right_handle ().type; 345 PointType l = en.get_left_handle ().type; 346 347 if (r == PointType.DOUBLE_CURVE || l == PointType.DOUBLE_CURVE) { 348 draw_double_curve (e, en, cr, highlighted); 349 } else { 350 draw_curve (e, en, cr, highlighted); 351 } 352 } 353 354 private static void draw_double_curve (EditPoint e, EditPoint en, Context cr, bool highlighted) { 355 EditPoint middle; 356 double x, y; 357 358 x = e.get_right_handle ().x + (en.get_left_handle ().x - e.get_right_handle ().x) / 2; 359 y = e.get_right_handle ().y + (en.get_left_handle ().y - e.get_right_handle ().y) / 2; 360 361 middle = new EditPoint (x, y, PointType.DOUBLE_CURVE); 362 middle.right_handle = en.get_left_handle ().copy (); 363 364 middle.right_handle.type = PointType.DOUBLE_CURVE; 365 middle.left_handle.type = PointType.DOUBLE_CURVE; 366 367 draw_curve (e, middle, cr, highlighted); 368 draw_curve (middle, en, cr, highlighted); 369 } 370 371 private static void draw_curve (EditPoint e, EditPoint en, Context cr, bool highlighted = false, double alpha = 1) { 372 Glyph g = MainWindow.get_current_glyph (); 373 double xa, ya, xb, yb, xc, yc, xd, yd; 374 PointType t = e.get_right_handle ().type; 375 PointType u = en.get_left_handle ().type; 376 377 get_bezier_points (e, en, out xa, out ya, out xb, out yb, out xc, out yc, out xd, out yd); 378 379 if (!highlighted) { 380 Theme.color (cr, "Stroke Color"); 381 } else { 382 Theme.color (cr, "Highlighted Guide"); 383 } 384 385 cr.set_line_width (stroke_width / g.view_zoom); 386 387 cr.line_to (xa, ya); // this point makes sense only if it is in the first or last position 388 389 if (t == PointType.QUADRATIC || t == PointType.LINE_QUADRATIC || t == PointType.DOUBLE_CURVE || u == PointType.QUADRATIC || u == PointType.LINE_QUADRATIC || u == PointType.DOUBLE_CURVE) { 390 cr.curve_to ((xa + 2 * xb) / 3, (ya + 2 * yb) / 3, (xd + 2 * xb) / 3, (yd + 2 * yb) / 3, xd, yd); 391 } else { 392 cr.curve_to (xb, yb, xc, yc, xd, yd); 393 } 394 } 395 396 /** Curve relative to window center. */ 397 public static void get_bezier_points (EditPoint e, EditPoint en, out double xa, out double ya, out double xb, out double yb, out double xc, out double yc, out double xd, out double yd) { 398 Glyph g = MainWindow.get_current_glyph (); 399 400 double center_x, center_y; 401 402 center_x = g.allocation.width / 2.0; 403 center_y = g.allocation.height / 2.0; 404 405 xa = center_x + e.x; 406 ya = center_y - e.y; 407 408 xb = center_x + e.get_right_handle ().x; 409 yb = center_y - e.get_right_handle ().y; 410 411 xc = center_x + en.get_left_handle ().x; 412 yc = center_y - en.get_left_handle ().y; 413 414 xd = center_x + en.x; 415 yd = center_y - en.y; 416 } 417 418 /** Curve absolute glyph data. */ 419 public static void get_abs_bezier_points (EditPoint e, EditPoint en, out double xa, out double ya, out double xb, out double yb, out double xc, out double yc, out double xd, out double yd) { 420 xa = + e.x; 421 ya = - e.y; 422 423 xb = + e.get_right_handle ().x; 424 yb = - e.get_right_handle ().y; 425 426 xc = + en.get_left_handle ().x; 427 yc = - en.get_left_handle ().y; 428 429 xd = + en.x; 430 yd = - en.y; 431 } 432 433 /** Line points relative to centrum. */ 434 public static void get_line_points (EditPoint e, EditPoint en, out double xa, out double ya, out double xb, out double yb) { 435 double xc = Glyph.xc (); 436 double yc = Glyph.yc (); 437 438 xa = xc + e.x; 439 ya = yc - e.y; 440 441 xb = xc + en.x; 442 yb = yc - en.y; 443 } 444 445 public void draw_line (EditPoint e, EditPoint en, Context cr, double alpha = 1) { 446 Glyph g = MainWindow.get_current_glyph (); 447 double ax, ay, bx, by; 448 449 get_line_points (e, en, out ax, out ay, out bx, out by); 450 451 Theme.color (cr, "Handle Color"); 452 cr.set_line_width (1.7 * (stroke_width / g.view_zoom)); 453 454 cr.line_to (ax, ay); 455 cr.line_to (bx, by); 456 457 cr.stroke (); 458 } 459 460 public void draw_edit_point (EditPoint e, Context cr) { 461 draw_edit_point_center (e, cr); 462 } 463 464 public void draw_edit_point_handles (EditPoint e, Context cr) { 465 Color color_left = Theme.get_color ("Control Point Handle"); 466 Color color_right = Theme.get_color ("Control Point Handle"); 467 EditPoint handle_right = e.get_right_handle ().get_point (); 468 EditPoint handle_left = e.get_left_handle ().get_point (); 469 470 cr.stroke (); 471 472 if (e.type != PointType.HIDDEN) { 473 if (e.get_right_handle ().selected) { 474 color_right = Theme.get_color ("Selected Control Point Handle"); 475 } else if (e.get_right_handle ().active) { 476 color_right = Theme.get_color ("Active Handle"); 477 } else { 478 color_right = Theme.get_color ("Control Point Handle"); 479 } 480 481 if (e.get_left_handle ().selected) { 482 color_left = Theme.get_color ("Selected Control Point Handle"); 483 } else if (e.get_left_handle ().active) { 484 color_left = Theme.get_color ("Active Handle"); 485 } else { 486 color_left = Theme.get_color ("Control Point Handle"); 487 } 488 489 if (e.get_right_handle ().selected) { 490 color_right = Theme.get_color ("Selected Control Point Handle"); 491 } else if (e.get_right_handle ().active) { 492 color_right = Theme.get_color ("Active Handle"); 493 } else { 494 color_right = Theme.get_color ("Control Point Handle"); 495 } 496 497 if (!hide_end_handle || !(is_open () && e == points.get (points.size - 1))) { 498 draw_line (handle_right, e, cr, 0.15); 499 draw_control_point (cr, e.get_right_handle ().x, e.get_right_handle ().y, color_right); 500 } 501 502 if (!(is_open () && e == points.get (0))) { 503 draw_line (handle_left, e, cr, 0.15); 504 draw_control_point (cr, e.get_left_handle ().x, e.get_left_handle ().y, color_left); 505 } 506 } 507 } 508 509 public static void draw_edit_point_center (EditPoint e, Context cr) { 510 Color c; 511 512 if (e.type != PointType.HIDDEN) { 513 if (e.type == PointType.CUBIC || e.type == PointType.LINE_CUBIC) { 514 if (e.is_selected ()) { 515 if (e.active_point) { 516 if (e.color != null) { 517 c = (!) e.color; 518 } else { 519 c = Theme.get_color ("Selected Active Cubic Control Point"); 520 } 521 } else { 522 if (e.color != null) { 523 c = (!) e.color; 524 } else { 525 c = Theme.get_color ("Selected Cubic Control Point"); 526 } 527 } 528 } else { 529 if (e.active_point) { 530 if (e.color != null) { 531 c = (!) e.color; 532 } else { 533 c = Theme.get_color ("Active Cubic Control Point"); 534 } 535 } else { 536 if (e.color != null) { 537 c = (!) e.color; 538 } else { 539 c = Theme.get_color ("Cubic Control Point"); 540 } 541 } 542 } 543 } else { 544 if (e.is_selected ()) { 545 if (e.active_point) { 546 if (e.color != null) { 547 c = (!) e.color; 548 } else { 549 c = Theme.get_color ("Selected Active Quadratic Control Point"); 550 } 551 } else { 552 if (e.color != null) { 553 c = (!) e.color; 554 } else { 555 c = Theme.get_color ("Selected Quadratic Control Point"); 556 } 557 } 558 } else { 559 if (e.active_point) { 560 if (e.color != null) { 561 c = (!) e.color; 562 } else { 563 c = Theme.get_color ("Active Quadratic Control Point"); 564 } 565 } else { 566 if (e.color != null) { 567 c = (!) e.color; 568 } else { 569 c = Theme.get_color ("Quadratic Control Point"); 570 } 571 } 572 } 573 } 574 575 draw_control_point (cr, e.x, e.y, c); 576 } 577 } 578 579 public static void draw_control_point (Context cr, double x, double y, Color color, double size = 3.5) { 580 Glyph g = MainWindow.get_current_glyph (); 581 double ivz = 1 / g.view_zoom; 582 double width = size * Math.sqrt (stroke_width) * ivz; 583 double xc = g.allocation.width / 2.0; 584 double yc = g.allocation.height / 2.0; 585 586 cr.save (); 587 588 x = xc + x; 589 y = yc - y; 590 591 cr.set_source_rgba (color.r, color.g, color.b, color.a); 592 593 cr.move_to (x, y); 594 cr.arc (x, y, width, 0, 2 * Math.PI); 595 cr.close_path (); 596 cr.fill (); 597 598 cr.restore (); 599 } 600 601 /** Set direction for this path to clockwise for outline and 602 * counter clockwise for inline paths. 603 */ 604 public bool force_direction (Direction direction) { 605 bool c = (direction == Direction.CLOCKWISE); 606 bool d = is_clockwise (); 607 direction_is_set = true; 608 609 if (c != d) { 610 this.reverse (); 611 } 612 613 d = is_clockwise (); 614 if (unlikely (d != c)) { 615 warning ("Failed to set direction for path in force_direction."); 616 return true; 617 } 618 619 return false; 620 } 621 622 /** Switch direction from clockwise path to counter clockwise path or vise versa. */ 623 public bool reverse () { 624 bool direction = is_clockwise (); 625 626 if (no_derived_direction) { 627 clockwise_direction = !clockwise_direction; 628 } 629 630 reverse_points (); 631 632 if (unlikely (direction == is_clockwise ())) { 633 return false; 634 } 635 636 return true; 637 } 638 639 private void reverse_points () requires (points.size > 0) { 640 EditPointHandle t; 641 EditPoint e; 642 Gee.ArrayList<EditPoint> new_points; 643 644 new_points = new Gee.ArrayList<EditPoint> (); 645 646 for (int i = points.size - 1; i >= 0 ; i--) { 647 e = points.get (i); 648 649 t = e.right_handle; 650 e.right_handle = e.left_handle; 651 e.left_handle = t; 652 653 new_points.add (e); 654 } 655 656 points = new_points; 657 create_list (); 658 } 659 660 public void print_all_points () { 661 int i = 0; 662 foreach (EditPoint p in points) { 663 ++i; 664 string t = (p.type == PointType.END) ? " endpoint" : ""; 665 stdout.printf (@"Point $i at ($(p.x), $(p.y)) $t \n"); 666 } 667 } 668 669 private double clockwise_sum () { 670 double sum = 0; 671 672 return_val_if_fail (points.size >= 3, 0); 673 674 foreach (EditPoint e in points) { 675 sum += e.get_direction (); 676 } 677 678 return sum; 679 } 680 681 public bool is_clockwise () { 682 double s; 683 Path p; 684 685 if (unlikely (points.size <= 2)) { 686 no_derived_direction = true; 687 return clockwise_direction; 688 } 689 690 if (unlikely (points.size == 2)) { 691 p = copy (); 692 all_segments ((a, b) => { 693 double px, py; 694 double step; 695 EditPoint new_point; 696 697 step = 0.3; 698 699 Path.get_point_for_step (a, b, step, out px, out py); 700 701 new_point = new EditPoint (px, py); 702 new_point.prev = a; 703 new_point.next = b; 704 705 p.insert_new_point_on_path (new_point, step); 706 707 return true; 708 }); 709 710 return p.is_clockwise (); 711 } 712 713 s = clockwise_sum (); 714 715 if (s == 0) { 716 no_derived_direction = true; 717 return clockwise_direction; 718 } 719 720 return s > 0; 721 } 722 723 public bool is_editable () { 724 return edit; 725 } 726 727 /** Show control points on outline path. */ 728 public void set_editable (bool e) { 729 edit = e; 730 } 731 732 public bool is_open () { 733 return open; 734 } 735 736 /** Resize path relative to bottom left coordinates. */ 737 public void resize (double ratio) { 738 foreach (EditPoint p in points) { 739 p.x *= ratio; 740 p.y *= ratio; 741 p.right_handle.length *= ratio; 742 p.left_handle.length *= ratio; 743 } 744 745 xmin *= ratio; 746 xmax *= ratio; 747 ymin *= ratio; 748 ymax *= ratio; 749 } 750 751 public void scale (double scale_x, double scale_y) { 752 foreach (EditPoint p in points) { 753 p.right_handle.length *= scale_x * scale_y; 754 p.left_handle.length *= scale_x * scale_y; 755 } 756 757 foreach (EditPoint p in points) { 758 p.x *= scale_x; 759 p.y *= scale_y; 760 } 761 762 xmin *= scale_x; 763 xmax *= scale_x; 764 ymin *= scale_y; 765 ymax *= scale_y; 766 } 767 768 public Path copy () { 769 Path new_path = new Path (); 770 EditPoint p; 771 772 foreach (EditPoint ep in points) { 773 p = ep.copy (); 774 new_path.add_point (p); 775 } 776 777 if (gradient != null) { 778 new_path.gradient = ((!) gradient).copy (); 779 } 780 781 if (color != null) { 782 new_path.color = ((!) color).copy (); 783 } 784 785 if (stroke_color != null) { 786 new_path.stroke_color = ((!) stroke_color).copy (); 787 } 788 789 new_path.fill = fill; 790 new_path.edit = edit; 791 new_path.open = open; 792 new_path.stroke = stroke; 793 new_path.line_cap = line_cap; 794 new_path.skew = skew; 795 new_path.fill = fill; 796 new_path.direction_is_set = direction_is_set; 797 new_path.create_list (); 798 799 new_path.hide_end_handle = hide_end_handle; 800 new_path.highlight_last_segment = highlight_last_segment; 801 802 return new_path; 803 } 804 805 public bool is_over (double x, double y) { 806 Glyph g = MainWindow.get_current_glyph (); 807 808 x = x * Glyph.ivz () + g.view_offset_x - Glyph.xc (); 809 y = y * Glyph.ivz () + g.view_offset_y - Glyph.yc (); 810 811 y *= -1; 812 813 return is_over_coordinate (x, y); 814 } 815 816 public bool is_over_coordinate (double x, double y) { 817 return is_over_coordinate_var (x, y); 818 } 819 820 public static double point_distance (EditPoint p1, EditPoint p2) { 821 return distance (p1.x, p2.x, p1.y, p2.y); 822 } 823 824 public static double distance (double ax, double bx, double ay, double by) { 825 return Math.fabs (Math.sqrt (Math.pow (ax - bx, 2) + Math.pow (ay - by, 2))); 826 } 827 828 public static double distance_to_point (EditPoint a, EditPoint b) { 829 return distance (a.x, b.x, a.y, b.y); 830 } 831 832 public static double distance_pixels (double x1, double y1, double x2, double y2) { 833 return distance (Glyph.path_coordinate_x (x1), 834 Glyph.path_coordinate_x (x2), 835 Glyph.path_coordinate_x (y1), 836 Glyph.path_coordinate_x (y2)); 837 } 838 839 public static double get_length_from (EditPoint a, EditPoint b) { 840 double x, y; 841 842 x = Math.fabs (a.x - a.get_right_handle ().x); 843 x += Math.fabs (a.get_right_handle ().x - b.get_left_handle ().x ); 844 x += Math.fabs (b.get_left_handle ().x - b.x); 845 846 y = Math.fabs (a.y - a.get_right_handle ().y); 847 y += Math.fabs (a.get_right_handle ().y - b.get_left_handle ().y); 848 y += Math.fabs (b.get_left_handle ().y - b.y); 849 850 return Math.fabs (Math.sqrt (x * x + y * y)); 851 } 852 853 public Path flatten (int steps = 10) { 854 Path flat = new Path (); 855 856 all_of_path ((x, y, t) => { 857 EditPoint ep = flat.add (x, y); 858 ep.type = PointType.LINE_QUADRATIC; 859 ep.get_right_handle ().type = PointType.LINE_QUADRATIC; 860 ep.get_left_handle ().type = PointType.LINE_QUADRATIC; 861 return true; 862 }, steps); // FIXME: g.view_zoom 863 864 if (!is_open ()) { 865 flat.close (); 866 } 867 868 flat.update_region_boundaries (); 869 870 return flat; 871 } 872 873 /** Variable precision */ 874 public bool is_over_coordinate_var (double x, double y) { 875 int insides = 0; 876 Path path; 877 878 if (stroke > 0) { 879 foreach (Path p in get_stroke_fast ().paths) { 880 path = p.flatten (); 881 if (StrokeTool.is_inside (new EditPoint (x, y), path)) { 882 insides++; 883 } 884 } 885 886 if (insides > 0 && is_filled ()) { 887 return true; 888 } 889 890 if (insides % 2 == 1) { 891 return true; 892 } 893 } else if (is_over_boundry (x, y)) { 894 path = flatten (); 895 return StrokeTool.is_inside (new EditPoint (x, y), path); 896 } 897 898 return false; 899 } 900 901 public bool is_over_boundry (double x, double y) { 902 if (unlikely (ymin == double.MAX || ymin == 10000)) { 903 warning ("bounding box is not calculated, run update_region_boundaries first."); 904 update_region_boundaries (); 905 } 906 907 return (ymin <= y <= ymax) && (xmin <= x <= xmax); 908 } 909 910 public bool has_overlapping_boundry (Path p) { 911 return !(xmax <= p.xmin || ymax <= p.ymin) || (xmin >= p.xmax || ymin >= p.ymax); 912 } 913 914 public EditPoint delete_first_point () { 915 EditPoint r; 916 int size; 917 918 size = points.size; 919 if (unlikely (size == 0)) { 920 warning ("No points in path."); 921 return new EditPoint (); 922 } 923 924 r = points.get (0); 925 points.remove_at (0); 926 927 if (size > 1) { 928 r.get_next ().prev = null; 929 } 930 931 return r; 932 } 933 934 public EditPoint delete_last_point () { 935 EditPoint r; 936 int size; 937 938 size = points.size; 939 if (unlikely (size == 0)) { 940 warning ("No points in path."); 941 return new EditPoint (); 942 } 943 944 r = points.get (size - 1); 945 points.remove_at (size - 1); 946 947 if (size > 1) { 948 r.get_prev ().next = null; 949 950 if (r.next != null) { 951 r.get_next ().prev = null; 952 } 953 } 954 955 return r; 956 } 957 958 public EditPoint add (double x, double y) { 959 if (points.size > 0) { 960 return add_after (x, y, points.get (points.size - 1)); 961 } 962 963 return add_after (x, y, null); 964 } 965 966 public EditPoint add_point (EditPoint p) { 967 if (points.size > 0) { 968 return add_point_after (p, points.get (points.size - 1)); 969 } 970 971 return add_point_after (p, null); 972 } 973 974 /** Insert a new point after @param previous_point and return a reference 975 * to the new item in list. 976 */ 977 public EditPoint add_after (double x, double y, EditPoint? previous_point) { 978 EditPoint p = new EditPoint (x, y, PointType.NONE); 979 return add_point_after (p, previous_point); 980 } 981 982 /** @return a list item pointing to the new point */ 983 public EditPoint add_point_after (EditPoint p, EditPoint? previous_point) { 984 int prev_index; 985 986 if (unlikely (previous_point == null && points.size != 0)) { 987 warning ("previous_point == null"); 988 previous_point = points.get (points.size - 1).get_link_item (); 989 } 990 991 if (points.size == 0) { 992 points.add (p); 993 p.prev = points.get (0).get_link_item (); 994 p.next = points.get (0).get_link_item (); 995 } else { 996 p.prev = (!) previous_point; 997 p.next = ((!) previous_point).next; 998 999 prev_index = points.index_of ((!) previous_point); 1000 1001 if (unlikely (!(0 <= prev_index < points.size))) { 1002 warning ("no previous point"); 1003 } 1004 1005 points.insert (prev_index + 1, p); 1006 } 1007 1008 last_point = p; 1009 1010 return p; 1011 } 1012 1013 public void recalculate_linear_handles () { 1014 foreach (EditPoint e in points) { 1015 e.recalculate_linear_handles (); 1016 } 1017 } 1018 1019 public void close () { 1020 open = false; 1021 edit = false; 1022 1023 create_list (); 1024 1025 if (points.size > 2) { 1026 points.get (0).recalculate_linear_handles (); 1027 points.get (points.size - 1).recalculate_linear_handles (); 1028 } 1029 } 1030 1031 public void reopen () { 1032 open = true; 1033 edit = true; 1034 } 1035 1036 /** Move path. */ 1037 public void move (double delta_x, double delta_y) { 1038 foreach (EditPoint ep in points) { 1039 ep.x += delta_x; 1040 ep.y += delta_y; 1041 } 1042 1043 if (gradient != null) { 1044 Gradient g = (!) gradient; 1045 g.x1 += delta_x; 1046 g.x2 += delta_x; 1047 g.y1 += delta_y; 1048 g.y2 += delta_y; 1049 } 1050 1051 update_region_boundaries (); 1052 } 1053 1054 private void update_region_boundaries_for_segment (EditPoint a, EditPoint b) { 1055 EditPointHandle left_handle; 1056 EditPointHandle right_handle; 1057 int steps = 10; 1058 1059 right_handle = a.get_right_handle (); 1060 left_handle = b.get_left_handle (); 1061 1062 if (a.x > xmax || b.x > xmax || left_handle.x > xmax || right_handle.x > xmax) { 1063 all_of (a, b, (cx, cy) => { 1064 if (cx > xmax) { 1065 this.xmax = cx; 1066 } 1067 return true; 1068 }, steps); 1069 } 1070 1071 if (a.x < xmin || b.x < xmin || left_handle.x < xmin || right_handle.x < xmin) { 1072 all_of (a, b, (cx, cy) => { 1073 if (cx < xmin) { 1074 this.xmin = cx; 1075 } 1076 return true; 1077 }, steps); 1078 } 1079 1080 if (a.y > ymax || b.y > ymax || left_handle.y > xmax || right_handle.y > xmax) { 1081 all_of (a, b, (cx, cy) => { 1082 if (cy > ymax) { 1083 this.ymax = cy; 1084 } 1085 return true; 1086 }, steps); 1087 } 1088 1089 if (a.y < ymin || b.y < ymin || left_handle.y < xmin || right_handle.y < xmin) { 1090 all_of (a, b, (cx, cy) => { 1091 if (cy < ymin) { 1092 this.ymin = cy; 1093 } 1094 return true; 1095 }, steps); 1096 } 1097 } 1098 1099 public void update_region_boundaries () { 1100 PathList s; 1101 1102 xmax = Glyph.CANVAS_MIN; 1103 xmin = Glyph.CANVAS_MAX; 1104 ymax = Glyph.CANVAS_MIN; 1105 ymin = Glyph.CANVAS_MAX; 1106 1107 if (points.size == 0) { 1108 xmax = 0; 1109 xmin = 0; 1110 ymax = 0; 1111 ymin = 0; 1112 } 1113 1114 if (stroke == 0) { 1115 all_segments ((a, b) => { 1116 update_region_boundaries_for_segment (a, b); 1117 return true; 1118 }); 1119 } else { 1120 s = get_stroke_fast (); 1121 foreach (Path p in s.paths) { 1122 p.all_segments ((a, b) => { 1123 update_region_boundaries_for_segment (a, b); 1124 return true; 1125 }); 1126 } 1127 } 1128 1129 if (points.size == 1) { 1130 EditPoint e = points.get (0); 1131 xmax = e.x; 1132 xmin = e.x; 1133 ymax = e.y; 1134 ymin = e.y; 1135 } 1136 } 1137 1138 /** Test if @param path is a valid outline for this object. */ 1139 public bool test_is_outline (Path path) { 1140 assert (false); 1141 return this.test_is_outline_of_path (path) && path.test_is_outline_of_path (this); 1142 } 1143 1144 private bool test_is_outline_of_path (Path outline) 1145 requires (outline.points.size >= 2 || points.size >= 2) 1146 { 1147 // rather slow use it for testing, only 1148 unowned EditPoint i = outline.points.get (0).get_link_item (); 1149 unowned EditPoint prev = outline.points.get (outline.points.size - 1).get_link_item (); 1150 1151 double tolerance = 1; 1152 bool g = false; 1153 1154 EditPoint ep = new EditPoint (0, 0); 1155 double min = double.MAX; 1156 1157 while (true) { 1158 min = 10000; 1159 1160 all_of (prev, i, (cx, cy) => { 1161 get_closest_point_on_path (ep, cx, cy); 1162 1163 double n = pow (ep.x - cx, 2) + pow (cy - ep.y, 2); 1164 1165 if (n < min) min = n; 1166 1167 if (n < tolerance) { 1168 g = true; 1169 return false; 1170 } 1171 1172 return true; 1173 }); 1174 1175 if (!g) { 1176 critical (@"this path does not seem to be the outline. (min $min)"); 1177 } 1178 1179 g = false; 1180 1181 if (i == outline.points.get (outline.points.size - 1)) { 1182 break; 1183 } 1184 1185 i = i.get_next (); 1186 } 1187 1188 return true; 1189 } 1190 1191 /** Add the extra point between line handles for double curve. */ 1192 public void add_hidden_double_points () requires (points.size > 1) { 1193 EditPoint hidden; 1194 EditPoint prev; 1195 EditPoint first; 1196 PointType left; 1197 PointType right; 1198 double x, y; 1199 Gee.ArrayList<EditPoint> middle_points = new Gee.ArrayList<EditPoint> (); 1200 Gee.ArrayList<EditPoint> first_points = new Gee.ArrayList<EditPoint> (); 1201 1202 first = is_open () ? points.get (0) : points.get (points.size - 1); 1203 1204 foreach (EditPoint next in points) { 1205 left = first.get_right_handle ().type; 1206 right = next.get_left_handle ().type; 1207 1208 if (next != first && (right == PointType.DOUBLE_CURVE || left == PointType.DOUBLE_CURVE)) { 1209 1210 first.get_right_handle ().type = PointType.QUADRATIC; 1211 1212 // half way between handles 1213 x = first.get_right_handle ().x + (next.get_left_handle ().x - first.get_right_handle ().x) / 2; 1214 y = first.get_right_handle ().y + (next.get_left_handle ().y - first.get_right_handle ().y) / 2; 1215 1216 hidden = new EditPoint (x, y, PointType.QUADRATIC); 1217 hidden.get_right_handle ().type = PointType.QUADRATIC; 1218 hidden.get_left_handle ().type = PointType.QUADRATIC; 1219 hidden.type = PointType.QUADRATIC; 1220 1221 hidden.right_handle.move_to_coordinate_internal (next.get_left_handle ().x, next.get_left_handle ().y); 1222 1223 first.get_right_handle ().type = PointType.QUADRATIC; 1224 first.type = PointType.QUADRATIC; 1225 1226 next.get_left_handle ().type = PointType.QUADRATIC; 1227 next.type = PointType.QUADRATIC; 1228 1229 middle_points.add (hidden); 1230 first_points.add (first); 1231 } 1232 first = next; 1233 } 1234 1235 for (int i = 0; i < middle_points.size; i++) { 1236 hidden = middle_points.get (i); 1237 add_point_after (middle_points.get (i), first_points.get (i)); 1238 } 1239 1240 create_list (); 1241 1242 prev = get_last_point (); 1243 foreach (EditPoint ep in points) { 1244 if (ep.type == PointType.QUADRATIC) { 1245 x = prev.get_right_handle ().x; 1246 y = prev.get_right_handle ().y; 1247 ep.get_left_handle ().move_to_coordinate (x, y); 1248 } 1249 1250 prev = ep; 1251 } 1252 } 1253 1254 /** Convert quadratic bezier points to cubic representation of the glyph 1255 * for ttf-export. 1256 */ 1257 public Path get_quadratic_points () { 1258 PointConverter converter; 1259 converter = new PointConverter (this); 1260 return converter.get_quadratic_path (); 1261 } 1262 1263 public void insert_new_point_on_path (EditPoint ep, double t = -1, bool move_point_to_path = false) { 1264 EditPoint start, stop; 1265 double x0, x1, y0, y1; 1266 double position, min; 1267 PointType left, right; 1268 double closest_x = 0; 1269 double closest_y = 0; 1270 1271 if (ep.next == null || ep.prev == null) { 1272 warning ("missing point"); 1273 return; 1274 } 1275 1276 start = ep.get_prev (); 1277 stop = ep.get_next (); 1278 1279 right = start.get_right_handle ().type; 1280 left = stop.get_left_handle ().type; 1281 1282 if (right == PointType.CUBIC || left == PointType.CUBIC) { 1283 start.get_right_handle ().type = PointType.CUBIC; 1284 stop.get_left_handle ().type = PointType.CUBIC; 1285 } 1286 1287 add_point_after (ep, ep.get_prev ()); 1288 1289 min = double.MAX; 1290 1291 position = 0.5; 1292 1293 if (t < 0) { 1294 all_of (start, stop, (cx, cy, t) => { 1295 double n = pow (ep.x - cx, 2) + pow (ep.y - cy, 2); 1296 1297 if (n < min) { 1298 min = n; 1299 position = t; 1300 closest_x = cx; 1301 closest_y = cy; 1302 } 1303 1304 return true; 1305 }); 1306 1307 if (move_point_to_path) { 1308 ep.x = closest_x; 1309 ep.y = closest_y; 1310 } 1311 } else { 1312 position = t; 1313 } 1314 1315 if (right == PointType.DOUBLE_CURVE || left == PointType.DOUBLE_CURVE) { 1316 double_bezier_vector (position, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x, out x0, out x1); 1317 double_bezier_vector (position, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y, out y0, out y1); 1318 1319 ep.get_left_handle ().set_point_type (PointType.DOUBLE_CURVE); 1320 ep.get_right_handle ().set_point_type (PointType.DOUBLE_CURVE); 1321 1322 ep.get_left_handle ().move_to_coordinate (x0, y0); 1323 ep.get_right_handle ().move_to_coordinate (x1, y1); 1324 1325 ep.type = PointType.DOUBLE_CURVE; 1326 } else if (right == PointType.QUADRATIC) { 1327 x0 = quadratic_bezier_vector (1 - position, stop.x, start.get_right_handle ().x, start.x); 1328 y0 = quadratic_bezier_vector (1 - position, stop.y, start.get_right_handle ().y, start.y); 1329 ep.get_right_handle ().move_to_coordinate (x0, y0); 1330 1331 ep.get_left_handle ().set_point_type (PointType.QUADRATIC); 1332 ep.get_right_handle ().set_point_type (PointType.QUADRATIC); 1333 1334 ep.get_left_handle ().move_to_coordinate_internal (0, 0); 1335 1336 ep.type = PointType.QUADRATIC; 1337 } else if (right == PointType.CUBIC || left == PointType.CUBIC) { 1338 bezier_vector (position, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x, out x0, out x1); 1339 bezier_vector (position, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y, out y0, out y1); 1340 1341 ep.get_left_handle ().set_point_type (PointType.CUBIC); 1342 ep.get_left_handle ().move_to_coordinate (x0, y0); 1343 1344 ep.get_right_handle ().set_point_type (PointType.CUBIC); 1345 ep.get_right_handle ().move_to_coordinate (x1, y1); 1346 1347 ep.type = PointType.LINE_CUBIC; 1348 } else if (right == PointType.LINE_QUADRATIC && left == PointType.LINE_QUADRATIC) { 1349 ep.get_right_handle ().set_point_type (PointType.LINE_QUADRATIC); 1350 ep.get_left_handle ().set_point_type (PointType.LINE_QUADRATIC); 1351 ep.type = PointType.QUADRATIC; 1352 } else if (right == PointType.LINE_CUBIC && left == PointType.LINE_CUBIC) { 1353 ep.get_right_handle ().set_point_type (PointType.LINE_CUBIC); 1354 ep.get_left_handle ().set_point_type (PointType.LINE_CUBIC); 1355 ep.type = PointType.LINE_CUBIC; 1356 } else if (right == PointType.LINE_DOUBLE_CURVE && left == PointType.LINE_DOUBLE_CURVE) { 1357 ep.get_right_handle ().set_point_type (PointType.LINE_DOUBLE_CURVE); 1358 ep.get_left_handle ().set_point_type (PointType.LINE_DOUBLE_CURVE); 1359 ep.type = PointType.DOUBLE_CURVE; 1360 } else { 1361 warning (@"Point types: $right and $left in insert_new_point_on_path"); 1362 } 1363 1364 ep.get_left_handle ().parent = ep; 1365 ep.get_right_handle ().parent = ep; 1366 1367 stop.get_left_handle ().length *= 1 - position; 1368 start.get_right_handle ().length *= position; 1369 1370 if (right == PointType.QUADRATIC) { // update connected handle 1371 if (ep.prev != null) { 1372 ep.get_left_handle ().move_to_coordinate_internal ( 1373 ep.get_prev ().right_handle.x, 1374 ep.get_prev ().right_handle.y); 1375 1376 } else { 1377 warning ("ep.prev is null for quadratic point"); 1378 } 1379 } 1380 1381 create_list (); 1382 foreach (EditPoint p in points) { 1383 p.recalculate_linear_handles (); 1384 } 1385 } 1386 1387 /** Get a point on the this path closest to x and y coordinates. 1388 * Don't look for a point in the segment skip_previous to skip_next. 1389 */ 1390 public void get_closest_point_on_path (EditPoint edit_point, double x, double y, 1391 EditPoint? skip_previous = null, EditPoint? skip_next = null, 1392 int steps = -1) { 1393 return_if_fail (points.size >= 1); 1394 1395 double min = double.MAX; 1396 double n = 0; 1397 bool g = false; 1398 1399 double ox = 0; 1400 double oy = 0; 1401 1402 EditPoint prev = points.get (points.size - 1); 1403 EditPoint i = points.get (0); 1404 1405 bool done = false; 1406 bool exit = false; 1407 bool first = true; 1408 1409 EditPoint? previous_point = null; 1410 EditPoint? next_point = null; 1411 1412 EditPoint previous; 1413 EditPoint next; 1414 double step = 0; 1415 1416 if (points.size == 0) { 1417 warning ("Empty path."); 1418 return; 1419 } 1420 1421 if (points.size == 1) { 1422 edit_point.x = i.x; 1423 edit_point.y = i.y; 1424 1425 edit_point.prev = i; 1426 edit_point.next = i; 1427 return; 1428 } 1429 1430 edit_point.x = i.x; 1431 edit_point.y = i.y; 1432 1433 create_list (); 1434 1435 while (!exit) { 1436 if (!first && i == points.get (points.size - 1)) { 1437 done = true; 1438 } 1439 1440 if (!done) { 1441 i = i.get_next (); 1442 prev = i.get_prev (); 1443 } else if (done && !is_open ()) { 1444 i = points.get (0); 1445 prev = points.get (points.size - 1); 1446 exit = true; 1447 } else { 1448 break; 1449 } 1450 1451 if (skip_previous == prev) { 1452 continue; 1453 } 1454 1455 if (prev.prev != null && skip_previous == prev.get_prev ()) { 1456 continue; 1457 } 1458 1459 if (skip_next == i) { 1460 continue; 1461 } 1462 1463 if (prev.next != null && skip_next == prev.get_next ()) { 1464 continue; 1465 } 1466 1467 all_of (prev, i, (cx, cy, t) => { 1468 n = pow (x - cx, 2) + pow (y - cy, 2); 1469 1470 if (n < min) { 1471 min = n; 1472 1473 ox = cx; 1474 oy = cy; 1475 1476 previous_point = i.prev; 1477 next_point = i; 1478 1479 step = t; 1480 1481 g = true; 1482 } 1483 1484 return true; 1485 }, steps); 1486 1487 first = false; 1488 } 1489 1490 if (previous_point == null && is_open ()) { 1491 previous_point = points.get (points.size - 1).get_link_item (); 1492 } 1493 1494 if (previous_point == null) { 1495 warning (@"previous_point == null, points.size: $(points.size)"); 1496 return; 1497 } 1498 1499 if (next_point == null) { 1500 warning ("next_point != null"); 1501 return; 1502 } 1503 1504 previous = (!) previous_point; 1505 next = (!) next_point; 1506 1507 edit_point.prev = previous_point; 1508 edit_point.next = next_point; 1509 1510 edit_point.set_position (ox, oy); 1511 1512 edit_point.type = previous.type; 1513 edit_point.get_left_handle ().type = previous.get_right_handle ().type; 1514 edit_point.get_right_handle ().type = next.get_left_handle ().type; 1515 } 1516 1517 public static bool all_of (EditPoint start, EditPoint stop, 1518 RasterIterator iter, int steps = -1, 1519 double min_t = 0, double max_t = 1) { 1520 1521 PointType right = PenTool.to_curve (start.get_right_handle ().type); 1522 PointType left = PenTool.to_curve (stop.get_left_handle ().type); 1523 1524 if (steps == -1) { 1525 steps = (int) (10 * get_length_from (start, stop)); 1526 } 1527 1528 if (right == PointType.DOUBLE_CURVE || left == PointType.DOUBLE_CURVE) { 1529 return all_of_double (start.x, start.y, start.get_right_handle ().x, start.get_right_handle ().y, stop.get_left_handle ().x, stop.get_left_handle ().y, stop.x, stop.y, iter, steps, min_t, max_t); 1530 } else if (right == PointType.QUADRATIC && left == PointType.QUADRATIC) { 1531 return all_of_quadratic_curve (start.x, start.y, start.get_right_handle ().x, start.get_right_handle ().y, stop.x, stop.y, iter, steps, min_t, max_t); 1532 } else if (right == PointType.CUBIC && left == PointType.CUBIC) { 1533 return all_of_curve (start.x, start.y, start.get_right_handle ().x, start.get_right_handle ().y, stop.get_left_handle ().x, stop.get_left_handle ().y, stop.x, stop.y, iter, steps, min_t, max_t); 1534 } 1535 1536 if (start.x == stop.x && start.y == stop.y) { 1537 warning ("Zero length."); 1538 return true; 1539 } 1540 1541 warning (@"Mixed point types in segment $(start.x),$(start.y) to $(stop.x),$(stop.y) right: $(right), left: $(left) (start: $(start.type), stop: $(stop.type))"); 1542 return all_of_quadratic_curve (start.x, start.y, start.get_right_handle ().x, start.get_right_handle ().x, stop.x, stop.y, iter, steps); 1543 } 1544 1545 public static void get_point_for_step (EditPoint start, EditPoint stop, double step, 1546 out double x, out double y) { 1547 1548 PointType right = PenTool.to_curve (start.type); 1549 PointType left = PenTool.to_curve (stop.type); 1550 1551 if (right == PointType.DOUBLE_CURVE || left == PointType.DOUBLE_CURVE) { 1552 x = double_bezier_path (step, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x); 1553 y = double_bezier_path (step, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y); 1554 } else if (right == PointType.QUADRATIC && left == PointType.QUADRATIC) { 1555 x = quadratic_bezier_path (step, start.x, start.get_right_handle ().x, stop.x); 1556 y = quadratic_bezier_path (step, start.y, start.get_right_handle ().y, stop.y); 1557 } else if (right == PointType.CUBIC && left == PointType.CUBIC) { 1558 x = bezier_path (step, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x); 1559 y = bezier_path (step, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y); 1560 } else if (right == PointType.HIDDEN && left == PointType.HIDDEN) { 1561 x = bezier_path (step, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x); 1562 y = bezier_path (step, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y); 1563 } else { 1564 warning (@"Mixed point types in segment $(start.x),$(start.y) to $(stop.x),$(stop.y) right: $(right), left: $(left) (start: $(start.type), stop: $(stop.type))"); 1565 x = bezier_path (step, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x); 1566 y = bezier_path (step, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y); 1567 } 1568 } 1569 1570 private static bool all_of_double (double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, 1571 RasterIterator iter, double steps = 400, double min_t = 0, double max_t = 1) { 1572 1573 double px = x1; 1574 double py = y1; 1575 1576 double t; 1577 double middle_x, middle_y; 1578 double double_step; 1579 1580 middle_x = x1 + (x2 - x1) / 2; 1581 middle_y = y1 + (y2 - y1) / 2; 1582 1583 for (int i = 0; i < steps; i++) { 1584 t = i / steps + min_t; 1585 1586 px = quadratic_bezier_path (t, x0, x1, middle_x); 1587 py = quadratic_bezier_path (t, y0, y1, middle_y); 1588 1589 double_step = t / 2; 1590 1591 if (double_step > max_t) { 1592 return false; 1593 } 1594 1595 if (!iter (px, py, double_step)) { 1596 return false; 1597 } 1598 } 1599 1600 for (int i = 0; i < steps; i++) { 1601 t = i / steps + min_t; 1602 1603 px = quadratic_bezier_path (t, middle_x, x2, x3); 1604 py = quadratic_bezier_path (t, middle_y, y2, y3); 1605 1606 double_step = 0.5 + t / 2; 1607 1608 if (double_step > max_t) { 1609 return false; 1610 } 1611 1612 if (!iter (px, py, double_step)) { 1613 return false; 1614 } 1615 } 1616 1617 return true; 1618 } 1619 1620 private static bool all_of_quadratic_curve (double x0, double y0, double x1, double y1, double x2, double y2, 1621 RasterIterator iter, double steps = 400, double min_t = 0, double max_t = 1) { 1622 double px = x1; 1623 double py = y1; 1624 1625 double t; 1626 1627 for (int i = 0; i < steps; i++) { 1628 t = i / steps + min_t; 1629 1630 px = quadratic_bezier_path (t, x0, x1, x2); 1631 py = quadratic_bezier_path (t, y0, y1, y2); 1632 1633 if (t > max_t) { 1634 return false; 1635 } 1636 1637 if (!iter (px, py, t)) { 1638 return false; 1639 } 1640 } 1641 1642 return true; 1643 } 1644 1645 private static bool all_of_curve (double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, 1646 RasterIterator iter, double steps = 400, double min_t = 0, double max_t = 1) { 1647 double px = x1; 1648 double py = y1; 1649 1650 double t; 1651 1652 for (int i = 0; i < steps; i++) { 1653 t = i / steps + min_t; 1654 1655 px = bezier_path (t, x0, x1, x2, x3); 1656 py = bezier_path (t, y0, y1, y2, y3); 1657 1658 if (t > max_t) { 1659 return false; 1660 } 1661 1662 if (!iter (px, py, t)) { 1663 return false; 1664 } 1665 } 1666 1667 return true; 1668 } 1669 1670 public bool all_segments (SegmentIterator iter) { 1671 unowned EditPoint i, next; 1672 1673 if (points.size < 2) { 1674 return false; 1675 } 1676 1677 for (int j = 0; j < points.size - 1; j++) { 1678 i = points.get (j).get_link_item (); 1679 next = i.get_next (); 1680 if (!iter (i, next)) { 1681 return false; 1682 } 1683 } 1684 1685 if (!is_open ()) { 1686 return iter (points.get (points.size - 1), points.get (0)); 1687 } 1688 1689 return true; 1690 } 1691 1692 public void all_of_path (RasterIterator iter, int steps = -1) { 1693 all_segments ((start, stop) => { 1694 return all_of (start, stop, iter, steps); 1695 }); 1696 } 1697 1698 public static double bezier_path (double step, double p0, double p1, double p2, double p3) { 1699 double q0, q1, q2; 1700 double r0, r1; 1701 1702 q0 = step * (p1 - p0) + p0; 1703 q1 = step * (p2 - p1) + p1; 1704 q2 = step * (p3 - p2) + p2; 1705 1706 r0 = step * (q1 - q0) + q0; 1707 r1 = step * (q2 - q1) + q1; 1708 1709 return step * (r1 - r0) + r0; 1710 } 1711 1712 public static void bezier_vector (double step, double p0, double p1, double p2, double p3, out double a0, out double a1) { 1713 double q0, q1, q2; 1714 1715 q0 = step * (p1 - p0) + p0; 1716 q1 = step * (p2 - p1) + p1; 1717 q2 = step * (p3 - p2) + p2; 1718 1719 a0 = step * (q1 - q0) + q0; 1720 a1 = step * (q2 - q1) + q1; 1721 } 1722 1723 public static double quadratic_bezier_vector (double step, double p0, double p1, double p2) { 1724 return step * (p1 - p0) + p0; 1725 } 1726 1727 public static double quadratic_bezier_path (double step, double p0, double p1, double p2) { 1728 double q0 = step * (p1 - p0) + p0; 1729 double q1 = step * (p2 - p1) + p1; 1730 1731 return step * (q1 - q0) + q0; 1732 } 1733 1734 public static double double_bezier_path (double step, double p0, double p1, double p2, double p3) { 1735 double middle = p1 + (p2 - p1) / 2; 1736 1737 if (step == 0.5) { 1738 // FIXME: return the middle point 1739 warning ("Middle"); 1740 } 1741 1742 if (step < 0.5) { 1743 return quadratic_bezier_path (2 * step, p0, p1, middle); 1744 } 1745 1746 return quadratic_bezier_path (2 * (step - 0.5), middle, p2, p3); 1747 } 1748 1749 public static void double_bezier_vector (double step, double p0, double p1, double p2, double p3, out double a0, out double a1) { 1750 double b0, b1, c0, c1, d0, d1; 1751 1752 if (unlikely (step <= 0 || step >= 1)) { 1753 warning (@"Bad step: $step"); 1754 step += 0.00004; 1755 } 1756 1757 // set angle 1758 b0 = double_bezier_path (step + 0.00001, p0, p1, p2, p3); 1759 c0 = double_bezier_path (step + 0.00002, p0, p1, p2, p3); 1760 1761 b1 = double_bezier_path (step - 0.00001, p0, p1, p2, p3); 1762 c1 = double_bezier_path (step - 0.00002, p0, p1, p2, p3); 1763 1764 // adjust length 1765 d0 = b0 + (b0 - c0) * 25000 * (step); 1766 d1 = b1 + (b1 - c1) * 25000 * (1 - step); 1767 1768 a0 = d0; 1769 a1 = d1; 1770 } 1771 1772 public static void get_handles_for_step (EditPoint start, EditPoint stop, double step, 1773 out double x1, out double y1, out double x2, out double y2) { 1774 1775 PointType right = PenTool.to_curve (start.type); 1776 PointType left = PenTool.to_curve (stop.type); 1777 1778 if (right == PointType.DOUBLE_CURVE || left == PointType.DOUBLE_CURVE) { 1779 double_bezier_vector (step, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x, out x1, out x2); // FIXME: swap parameter? 1780 double_bezier_vector (step, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y, out y1, out y2); 1781 } else if (right == PointType.QUADRATIC && left == PointType.QUADRATIC) { 1782 x1 = quadratic_bezier_vector (step, start.x, start.get_right_handle ().x, stop.x); 1783 y1 = quadratic_bezier_vector (step, start.y, start.get_right_handle ().y, stop.y); 1784 x2 = x1; 1785 y2 = y1; 1786 } else if (right == PointType.CUBIC && left == PointType.CUBIC) { 1787 bezier_vector (step, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x, out x1, out x2); 1788 bezier_vector (step, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y, out y1, out y2); 1789 } else if (right == PointType.HIDDEN && left == PointType.HIDDEN) { 1790 bezier_vector (step, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x, out x1, out x2); 1791 bezier_vector (step, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y, out y1, out y2); 1792 } else { 1793 warning (@"Mixed point types in segment $(start.x),$(start.y) to $(stop.x),$(stop.y) right: $(right), left: $(left) (start: $(start.type), stop: $(stop.type))"); 1794 bezier_vector (step, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x, out x1, out x2); 1795 bezier_vector (step, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y, out y1, out y2); 1796 } 1797 } 1798 1799 public void plot (Context cr, WidgetAllocation allocation, double view_zoom) { 1800 double px = 0, py = 0; 1801 double xc = allocation.width / 2.0; 1802 double yc = allocation.height / 2.0; 1803 1804 cr.save (); 1805 1806 all_of_path ((x, y) => { 1807 cr.move_to (px + xc, -py + yc); 1808 cr.line_to (x + xc, -y + yc); 1809 1810 px = x; 1811 py = y; 1812 1813 return true; 1814 }); 1815 1816 cr.stroke (); 1817 cr.restore (); 1818 } 1819 1820 public void print_boundaries () { 1821 stderr.printf (@"xmax $xmax \n"); 1822 stderr.printf (@"xmin $xmin \n"); 1823 stderr.printf (@"ymax $ymax \n"); 1824 stderr.printf (@"ymin $ymin \n"); 1825 } 1826 1827 public bool has_region_boundaries () { 1828 return !(xmax == -10000 || xmin == 10000 || ymax == -10000 || ymin == 10000); 1829 } 1830 1831 public void create_list () { 1832 EditPoint ep; 1833 1834 if (points.size == 0) { 1835 return; 1836 } 1837 1838 if (points.size == 1) { 1839 ep = points.get (0); 1840 ep.next = null; 1841 ep.prev = null; 1842 return; 1843 } 1844 1845 ep = points.get (0); 1846 ep.next = points.get (1).get_link_item (); 1847 ep.prev = points.get (points.size - 1).get_link_item (); 1848 1849 for (int i = 1; i < points.size - 1; i++) { 1850 ep = points.get (i); 1851 ep.prev = points.get (i - 1).get_link_item (); 1852 ep.next = points.get (i + 1).get_link_item (); 1853 } 1854 1855 ep = points.get (points.size - 1); 1856 ep.next = points.get (0).get_link_item (); 1857 ep.prev = points.get (points.size - 2).get_link_item (); 1858 } 1859 1860 public bool has_point (EditPoint ep) { 1861 return points.contains (ep); 1862 } 1863 1864 public bool has_deleted_point () { 1865 foreach (EditPoint p in points) { 1866 if (p.deleted) { 1867 return true; 1868 } 1869 } 1870 return false; 1871 } 1872 1873 /** @return the remaining parts as a new path. */ 1874 public PathList process_deleted_points () 1875 requires (points.size > 0) 1876 { 1877 EditPoint p; 1878 EditPoint ep; 1879 Path current_path = new Path (); 1880 Path remaining_points = new Path (); 1881 PathList path_list = new PathList (); 1882 int i; 1883 int index = 0; 1884 1885 remaining_points.stroke = stroke; 1886 current_path.stroke = stroke; 1887 1888 if (!has_deleted_point ()) { 1889 return path_list; 1890 } 1891 1892 if (points.size == 1) { 1893 points.remove_at (0); 1894 return path_list; 1895 } 1896 1897 // set start position to a point that will be removed 1898 for (i = 0; i < points.size; i++) { 1899 p = points.get (i); 1900 1901 if (p.deleted) { 1902 index = i; 1903 i++; 1904 ep = p; 1905 break; 1906 } 1907 } 1908 1909 // don't tie end points on the open path 1910 if (points.size > 1) { 1911 p = points.get (1); 1912 p.convert_to_curve (); 1913 p.set_reflective_handles (false); 1914 p.set_tie_handle (false); 1915 } 1916 1917 if (points.size > 0) { 1918 p = points.get (points.size - 1); 1919 p.convert_to_curve (); 1920 p.set_reflective_handles (false); 1921 p.set_tie_handle (false); 1922 } 1923 1924 // copy points after the deleted point 1925 while (i < points.size) { 1926 p = points.get (i); 1927 current_path.add_point (p); 1928 i++; 1929 } 1930 1931 // copy points before the deleted point 1932 for (i = 0; i < index; i++) { 1933 p = points.get (i); 1934 remaining_points.add_point (p); 1935 } 1936 1937 // merge if we still only have one path 1938 if (!is_open ()) { 1939 foreach (EditPoint point in remaining_points.points) { 1940 current_path.add_point (point.copy ()); 1941 } 1942 1943 if (current_path.points.size > 0) { 1944 ep = current_path.points.get (0); 1945 ep.set_tie_handle (false); 1946 ep.set_reflective_handles (false); 1947 ep.get_left_handle ().type = PenTool.to_line (ep.type); 1948 ep.type = PenTool.to_curve (ep.type); 1949 path_list.add (current_path); 1950 1951 ep = current_path.points.get (current_path.points.size - 1); 1952 ep.get_right_handle ().type = PenTool.to_line (ep.type); 1953 ep.type = PenTool.to_curve (ep.get_right_handle ().type); 1954 } 1955 } else { 1956 if (current_path.points.size > 0) { 1957 ep = current_path.points.get (0); 1958 ep.set_tie_handle (false); 1959 ep.set_reflective_handles (false); 1960 ep.get_left_handle ().type = PenTool.to_line (ep.type); 1961 ep.type = PenTool.to_curve (ep.type); 1962 set_new_start (current_path.points.get (0)); 1963 path_list.add (current_path); 1964 ep = current_path.points.get (current_path.points.size - 1); 1965 ep.get_right_handle ().type = PenTool.to_line (ep.type); 1966 ep.type = PenTool.to_curve (ep.get_right_handle ().type); 1967 } 1968 1969 if (remaining_points.points.size > 0) { 1970 remaining_points.points.get (0).set_tie_handle (false); 1971 remaining_points.points.get (0).set_reflective_handles (false); 1972 remaining_points.points.get (0).type = remaining_points.points.get (0).type; 1973 set_new_start (remaining_points.points.get (0)); 1974 path_list.add (remaining_points); 1975 1976 if (current_path.points.size > 0) { 1977 ep = current_path.points.get (current_path.points.size - 1); 1978 ep.get_right_handle ().type = PenTool.to_line (ep.type); 1979 ep.type = PenTool.to_curve (ep.get_right_handle ().type); 1980 } 1981 } 1982 } 1983 1984 foreach (Path path in path_list.paths) { 1985 path.update_region_boundaries (); 1986 } 1987 1988 return path_list; 1989 } 1990 1991 public void set_new_start (EditPoint ep) 1992 requires (points.size > 0) { 1993 Gee.ArrayList<EditPoint> list = new Gee.ArrayList<EditPoint> (); 1994 int start = 0; 1995 1996 for (int i = 0; i < points.size; i++) { 1997 if (ep == points.get (i)) { 1998 start = i; 1999 } 2000 } 2001 2002 for (int i = start; i < points.size; i++) { 2003 list.add (points.get (i)); 2004 } 2005 2006 for (int i = 0; i < start; i++) { 2007 list.add (points.get (i)); 2008 } 2009 2010 control_points = list; 2011 } 2012 2013 public void append_path (Path path) { 2014 if (points.size == 0 || path.points.size == 0) { 2015 warning ("No points"); 2016 return; 2017 } 2018 2019 // copy remaining points 2020 foreach (EditPoint p in path.points) { 2021 add_point (p.copy ()); 2022 } 2023 2024 path.points.clear (); 2025 } 2026 2027 /** Roatate around coordinate xc, xc. */ 2028 public void rotate (double theta, double xc, double yc) { 2029 double a, radius; 2030 2031 foreach (EditPoint ep in points) { 2032 radius = sqrt (pow (xc - ep.x, 2) + pow (yc + ep.y, 2)); 2033 2034 if (yc + ep.y < 0) { 2035 radius = -radius; 2036 } 2037 2038 a = acos ((ep.x - xc) / radius); 2039 2040 ep.x = xc + cos (a - theta) * radius; 2041 ep.y = yc + sin (a - theta) * radius; 2042 2043 ep.get_right_handle ().angle -= theta; 2044 ep.get_left_handle ().angle -= theta; 2045 2046 while (ep.get_right_handle ().angle < 0) { 2047 ep.get_right_handle ().angle += 2 * PI; 2048 } 2049 2050 while (ep.get_left_handle ().angle < 0) { 2051 ep.get_left_handle ().angle += 2 * PI; 2052 } 2053 } 2054 2055 rotation += theta; 2056 rotation %= 2 * PI; 2057 2058 update_region_boundaries (); 2059 } 2060 2061 public void flip_vertical () { 2062 EditPointHandle hl, hr; 2063 double lx, ly, rx, ry; 2064 2065 foreach (EditPoint e in points) { 2066 hl = e.get_left_handle (); 2067 hr = e.get_right_handle (); 2068 2069 lx = hl.x; 2070 ly = hl.y; 2071 rx = hr.x; 2072 ry = hr.y; 2073 2074 e.y *= -1; 2075 2076 hr.move_to_coordinate_internal (rx, -1 * ry); 2077 hl.move_to_coordinate_internal (lx, -1 * ly); 2078 } 2079 2080 update_region_boundaries (); 2081 } 2082 2083 public void flip_horizontal () { 2084 EditPointHandle hl, hr; 2085 double lx, ly, rx, ry; 2086 foreach (EditPoint e in points) { 2087 hl = e.get_left_handle (); 2088 hr = e.get_right_handle (); 2089 2090 lx = hl.x; 2091 ly = hl.y; 2092 rx = hr.x; 2093 ry = hr.y; 2094 2095 e.x *= -1; 2096 2097 hr.move_to_coordinate_internal (-1 * rx, ry); 2098 hl.move_to_coordinate_internal (-1 * lx, ly); 2099 } 2100 2101 update_region_boundaries (); 2102 } 2103 2104 public void init_point_type (PointType pt = PointType.NONE) { 2105 PointType type; 2106 2107 if (pt == PointType.NONE) { 2108 pt = DrawingTools.point_type; 2109 } 2110 2111 switch (pt) { 2112 case PointType.QUADRATIC: 2113 type = PointType.LINE_QUADRATIC; 2114 break; 2115 case PointType.DOUBLE_CURVE: 2116 type = PointType.LINE_DOUBLE_CURVE; 2117 break; 2118 case PointType.CUBIC: 2119 type = PointType.LINE_CUBIC; 2120 break; 2121 default: 2122 warning ("No type is set"); 2123 type = PointType.LINE_CUBIC; 2124 break; 2125 } 2126 2127 foreach (EditPoint ep in points) { 2128 ep.type = type; 2129 ep.get_right_handle ().type = type; 2130 ep.get_left_handle ().type = type; 2131 } 2132 } 2133 2134 public void convert_path_ending_to_line () { 2135 if (points.size < 2) { 2136 return; 2137 } 2138 2139 get_first_point ().get_left_handle ().convert_to_line (); 2140 get_last_point ().get_right_handle ().convert_to_line (); 2141 } 2142 2143 public void print_all_types () { 2144 print (@"Control points:\n"); 2145 foreach (EditPoint ep in points) { 2146 print (@"$(ep.type) L: $(ep.get_left_handle ().type) R: L: $(ep.get_right_handle ().type)\n"); 2147 } 2148 } 2149 2150 /** Find the point where two lines intersect. */ 2151 public static void find_intersection (double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, 2152 out double point_x, out double point_y) { 2153 point_x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)); 2154 point_y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)); 2155 } 2156 2157 public static void find_intersection_handle (EditPointHandle h1, EditPointHandle h2, out double point_x, out double point_y) { 2158 find_intersection (h1.parent.x, h1.parent.y, h1.x, h1.y, h2.parent.x, h2.parent.y, h2.x, h2.y, out point_x, out point_y); 2159 } 2160 2161 /** Finx intersection point for two straight lines. */ 2162 public static void find_intersection_point (EditPoint p1, EditPoint p2, EditPoint q1, EditPoint q2, out double point_x, out double point_y) { 2163 find_intersection (p1.x, p1.y, p2.x, p2.y, q1.x, q1.y, q2.x, q2.y, out point_x, out point_y); 2164 } 2165 2166 public void add_extrema () { 2167 double x0, y0, x1, y1, x2, y2, x3, y3; 2168 double minx, maxx, miny, maxy; 2169 2170 if (unlikely (points.size < 2)) { 2171 warning (@"Missing points, $(points.size) points in path."); 2172 return; 2173 } 2174 2175 minx = Glyph.CANVAS_MAX; 2176 miny = Glyph.CANVAS_MAX; 2177 maxx = Glyph.CANVAS_MIN; 2178 maxy = Glyph.CANVAS_MIN; 2179 2180 x0 = 0; 2181 y0 = 0; 2182 x1 = 0; 2183 y1 = 0; 2184 x2 = 0; 2185 y2 = 0; 2186 x3 = 0; 2187 y3 = 0; 2188 2189 all_of_path ((x, y) => { 2190 if (x < minx) { 2191 x0 = x; 2192 y0 = y; 2193 minx = x; 2194 } 2195 2196 if (x > maxx) { 2197 x1 = x; 2198 y1 = y; 2199 maxx = x; 2200 } 2201 2202 if (y < miny) { 2203 x2 = x; 2204 y2 = y; 2205 miny = y; 2206 } 2207 2208 if (y > maxy) { 2209 x3 = x; 2210 y3 = y; 2211 maxy = y; 2212 } 2213 2214 return true; 2215 }); 2216 2217 insert_new_point_on_path_at (x0 - 0.001, y0); 2218 insert_new_point_on_path_at (x1 + 0.001, y1); 2219 insert_new_point_on_path_at (x2, y2 - 0.001); 2220 insert_new_point_on_path_at (x3, y3 + 0.001); 2221 } 2222 2223 public EditPoint insert_new_point_on_path_at (double x, double y) { 2224 EditPoint ep = new EditPoint (); 2225 EditPoint prev, next; 2226 bool exists; 2227 2228 if (points.size < 2) { 2229 warning ("Can't add extrema to just one point."); 2230 return ep; 2231 } 2232 2233 get_closest_point_on_path (ep, x, y); 2234 2235 next = (ep.next == null) ? points.get (0) : ep.get_next (); 2236 prev = (ep.prev == null) ? points.get (points.size - 1) : ep.get_prev (); 2237 2238 exists = prev.x == ep.x && prev.y == ep.y; 2239 exists |= next.x == ep.x && next.y == ep.y; 2240 2241 if (!exists) { 2242 insert_new_point_on_path (ep); 2243 } 2244 2245 return ep; 2246 } 2247 2248 public static bool is_counter (PathList pl, Path path) { 2249 return counters (pl, path) % 2 != 0; 2250 } 2251 2252 public static int counters (PathList pl, Path path) { 2253 int inside_count = 0; 2254 bool inside; 2255 PathList lines = new PathList (); 2256 2257 lines = pl; 2258 2259 /** // FIXME: Check automatic orientation. 2260 foreach (Path p in pl.paths) { 2261 lines.add (SvgParser.get_lines (p)); 2262 } 2263 */ 2264 2265 foreach (Path p in lines.paths) { 2266 if (p.points.size > 1 && p != path 2267 && path.boundaries_intersecting (p)) { 2268 2269 inside = false; 2270 foreach (EditPoint ep in path.points) { 2271 if (SvgParser.is_inside (ep, p)) { 2272 inside = true; 2273 } 2274 } 2275 2276 if (inside) { 2277 inside_count++; 2278 } 2279 } 2280 } 2281 2282 return inside_count; 2283 } 2284 2285 public bool boundaries_intersecting (Path p) { 2286 return in_boundaries (p.xmin, p.xmax, p.ymin, p.ymax); 2287 } 2288 2289 public bool in_boundaries (double other_xmin, double other_xmax, double other_ymin, double other_ymax) { 2290 return ((xmin <= other_xmin <= xmax) || (xmin <= other_xmax <= xmax) 2291 || (other_xmin <= xmin <= other_xmax) || (other_xmin <= xmax <= other_xmax)) 2292 && ((ymin <= other_ymin <= ymax) || (ymin <= other_ymax <= ymax) 2293 || (other_ymin <= ymin <= other_ymax) || (other_ymin <= ymax <= other_ymax)); 2294 } 2295 2296 /** @param t smallest distance to other points. */ 2297 public void remove_points_on_points (double t = 0.00001) { 2298 Gee.ArrayList<EditPoint> remove = new Gee.ArrayList<EditPoint> (); 2299 EditPoint n; 2300 EditPointHandle hr, h; 2301 double t3 = t / 3; 2302 2303 if (points.size == 0) { 2304 return; 2305 } 2306 2307 for (int i = 0; i < points.size + 1; i++) { 2308 EditPoint ep = points.get (i % points.size); 2309 if (ep.get_right_handle ().length < t3 2310 && ep.get_left_handle ().length < t3 2311 && !is_endpoint (ep) 2312 && (ep.flags & EditPoint.CURVE_KEEP) == 0 2313 && (ep.flags & EditPoint.SEGMENT_END) == 0) { 2314 ep.deleted = true; 2315 } 2316 } 2317 2318 remove_deleted_points (); 2319 2320 for (int i = 0; i < points.size + 1; i++) { 2321 EditPoint ep = points.get (i % points.size); 2322 n = points.get ((i + 1) % points.size); 2323 2324 if (Path.distance_to_point (n, ep) < t) { 2325 remove.add (ep); 2326 } 2327 } 2328 2329 create_list (); 2330 2331 foreach (EditPoint r in remove) { 2332 if (points.size == 0) { 2333 return; 2334 } 2335 2336 if (r.next != null) { 2337 n = r.get_next (); 2338 } else { 2339 n = points.get (0); 2340 } 2341 2342 points.remove (r); 2343 h = n.get_left_handle (); 2344 hr = r.get_left_handle (); 2345 h.length = hr.length; 2346 h.angle = hr.angle; 2347 h.type = hr.type; 2348 2349 if (h.length < t) { 2350 h.length = t; 2351 h.angle = n.get_right_handle ().angle - PI; 2352 } 2353 2354 create_list (); 2355 } 2356 2357 recalculate_linear_handles (); 2358 } 2359 2360 public bool is_endpoint (EditPoint ep) { 2361 if (points.size == 0) { 2362 return false; 2363 } 2364 2365 return ep == points.get (0) || ep == points.get (points.size - 1); 2366 } 2367 2368 public void remove_deleted_points () { 2369 Gee.ArrayList<EditPoint> p = new Gee.ArrayList<EditPoint> (); 2370 2371 foreach (EditPoint ep in points) { 2372 if (ep.deleted) { 2373 p.add (ep); 2374 } 2375 } 2376 2377 foreach (EditPoint e in p) { 2378 points.remove (e); 2379 } 2380 2381 create_list (); 2382 } 2383 2384 public static void find_closes_point_in_segment (EditPoint ep0, EditPoint ep1, 2385 double px, double py, 2386 out double nx, out double ny, 2387 double max_step = 200) { 2388 2389 double min_distance = double.MAX; 2390 double npx, npy; 2391 double min_t, max_t; 2392 double rmin_t, rmax_t; 2393 bool found; 2394 int step; 2395 2396 npx = 0; 2397 npy = 0; 2398 2399 min_t = 0; 2400 max_t = 1; 2401 2402 rmin_t = 0; 2403 rmax_t = 1; 2404 2405 for (step = 3; step <= max_step; step *= 2) { 2406 found = false; 2407 min_distance = double.MAX; 2408 Path.all_of (ep0, ep1, (xa, ya, ta) => { 2409 double d = Path.distance (px, xa, py, ya); 2410 2411 if (d < min_distance) { 2412 min_distance = d; 2413 npx = xa; 2414 npy = ya; 2415 rmin_t = ta - 1.0 / step; 2416 rmax_t = ta + 1.0 / step; 2417 found = true; 2418 } 2419 2420 return true; 2421 }, step, min_t, max_t); 2422 2423 if (!found) { 2424 rmin_t = 1 - (1.0 / step); 2425 rmax_t = 1; 2426 } 2427 2428 min_t = (rmin_t > 0) ? rmin_t : 0; 2429 max_t = (rmax_t < 1) ? rmax_t : 1; 2430 } 2431 2432 nx = npx; 2433 ny = npy; 2434 } 2435 2436 public void reset_stroke () { 2437 full_stroke = null; 2438 fast_stroke = null; 2439 } 2440 2441 public void create_full_stroke () { 2442 if (stroke <= 0) { 2443 return; 2444 } 2445 2446 print(@"Create full stroke for $(points.size) points.\n"); 2447 StrokeTask task = new StrokeTask (this); 2448 MainWindow.native_window.run_non_blocking_background_thread (task); 2449 2450 stop_stroke_creator (); 2451 stroke_creator = task; 2452 } 2453 2454 public void stop_stroke_creator () { 2455 if (stroke_creator != null) { 2456 ((!) stroke_creator).cancel (); 2457 } 2458 } 2459 2460 public PathList get_stroke () { 2461 if (full_stroke == null) { 2462 StrokeTool s = new StrokeTool (); 2463 full_stroke = s.get_stroke (this, stroke); 2464 } 2465 2466 return (!) full_stroke; 2467 } 2468 2469 public PathList get_stroke_fast () { 2470 if (full_stroke != null) { 2471 return (!) full_stroke; 2472 } 2473 2474 if (fast_stroke != null) { 2475 return (!) fast_stroke; 2476 } 2477 2478 StrokeTool s = new StrokeTool (); 2479 Test t = new Test.time ("fast stroke"); 2480 fast_stroke = s.get_stroke_fast (this, stroke); 2481 t.print(); 2482 2483 return (!) fast_stroke; 2484 } 2485 2486 // Callback for path simplifier 2487 public void add_cubic_bezier_points (double x0, double y0, double x1, double y1, 2488 double x2, double y2, double x3, double y3) { 2489 2490 EditPoint start; 2491 EditPoint end; 2492 2493 if (points.size > 0) { 2494 start = get_last_point (); 2495 } else { 2496 start = add (x0, y0); 2497 } 2498 2499 end = add (x3, y3); 2500 2501 start.set_point_type (PointType.CUBIC); 2502 end.set_point_type (PointType.CUBIC); 2503 2504 start.convert_to_curve (); 2505 end.convert_to_curve (); 2506 2507 start.get_right_handle ().move_to_coordinate (x1, y1); 2508 end.get_left_handle ().move_to_coordinate (x2, y2); 2509 } 2510 } 2511 2512 } 2513