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 int prev_index; 968 EditPoint previous_point; 969 970 if (points.size == 0) { 971 points.add (p); 972 p.prev = points.get (0).get_link_item (); 973 p.next = points.get (0).get_link_item (); 974 } else { 975 previous_point = points.get (points.size - 1); 976 points.add (p); 977 p.prev = (!) previous_point; 978 p.next = null; 979 points.add (p); 980 } 981 982 last_point = p; 983 984 return p; 985 } 986 987 /** Insert a new point after @param previous_point and return a reference 988 * to the new item in list. 989 */ 990 public EditPoint add_after (double x, double y, EditPoint? previous_point) { 991 EditPoint p = new EditPoint (x, y, PointType.NONE); 992 return add_point_after (p, previous_point); 993 } 994 995 /** @return a list item pointing to the new point */ 996 public EditPoint add_point_after (EditPoint p, EditPoint? previous_point) { 997 int prev_index; 998 999 if (unlikely (previous_point == null && points.size != 0)) { 1000 warning ("previous_point == null"); 1001 previous_point = points.get (points.size - 1).get_link_item (); 1002 } 1003 1004 if (points.size == 0) { 1005 points.add (p); 1006 p.prev = points.get (0).get_link_item (); 1007 p.next = points.get (0).get_link_item (); 1008 } else { 1009 p.prev = (!) previous_point; 1010 p.next = ((!) previous_point).next; 1011 1012 prev_index = points.index_of ((!) previous_point); 1013 1014 if (unlikely (!(0 <= prev_index < points.size))) { 1015 warning ("no previous point"); 1016 } 1017 1018 points.insert (prev_index + 1, p); 1019 } 1020 1021 last_point = p; 1022 1023 return p; 1024 } 1025 1026 public void recalculate_linear_handles () { 1027 foreach (EditPoint e in points) { 1028 e.recalculate_linear_handles (); 1029 } 1030 } 1031 1032 public void close () { 1033 open = false; 1034 edit = false; 1035 1036 create_list (); 1037 1038 if (points.size > 2) { 1039 points.get (0).recalculate_linear_handles (); 1040 points.get (points.size - 1).recalculate_linear_handles (); 1041 } 1042 } 1043 1044 public void reopen () { 1045 open = true; 1046 edit = true; 1047 } 1048 1049 /** Move path. */ 1050 public void move (double delta_x, double delta_y) { 1051 foreach (EditPoint ep in points) { 1052 ep.x += delta_x; 1053 ep.y += delta_y; 1054 } 1055 1056 if (gradient != null) { 1057 Gradient g = (!) gradient; 1058 g.x1 += delta_x; 1059 g.x2 += delta_x; 1060 g.y1 += delta_y; 1061 g.y2 += delta_y; 1062 } 1063 1064 update_region_boundaries (); 1065 } 1066 1067 private void update_region_boundaries_for_segment (EditPoint a, EditPoint b) { 1068 EditPointHandle left_handle; 1069 EditPointHandle right_handle; 1070 int steps = 10; 1071 1072 right_handle = a.get_right_handle (); 1073 left_handle = b.get_left_handle (); 1074 1075 if (a.x > xmax || b.x > xmax || left_handle.x > xmax || right_handle.x > xmax) { 1076 all_of (a, b, (cx, cy) => { 1077 if (cx > xmax) { 1078 this.xmax = cx; 1079 } 1080 return true; 1081 }, steps); 1082 } 1083 1084 if (a.x < xmin || b.x < xmin || left_handle.x < xmin || right_handle.x < xmin) { 1085 all_of (a, b, (cx, cy) => { 1086 if (cx < xmin) { 1087 this.xmin = cx; 1088 } 1089 return true; 1090 }, steps); 1091 } 1092 1093 if (a.y > ymax || b.y > ymax || left_handle.y > xmax || right_handle.y > xmax) { 1094 all_of (a, b, (cx, cy) => { 1095 if (cy > ymax) { 1096 this.ymax = cy; 1097 } 1098 return true; 1099 }, steps); 1100 } 1101 1102 if (a.y < ymin || b.y < ymin || left_handle.y < xmin || right_handle.y < xmin) { 1103 all_of (a, b, (cx, cy) => { 1104 if (cy < ymin) { 1105 this.ymin = cy; 1106 } 1107 return true; 1108 }, steps); 1109 } 1110 } 1111 1112 public void update_region_boundaries () { 1113 PathList s; 1114 1115 xmax = Glyph.CANVAS_MIN; 1116 xmin = Glyph.CANVAS_MAX; 1117 ymax = Glyph.CANVAS_MIN; 1118 ymin = Glyph.CANVAS_MAX; 1119 1120 if (points.size == 0) { 1121 xmax = 0; 1122 xmin = 0; 1123 ymax = 0; 1124 ymin = 0; 1125 } 1126 1127 if (stroke == 0) { 1128 all_segments ((a, b) => { 1129 update_region_boundaries_for_segment (a, b); 1130 return true; 1131 }); 1132 } else { 1133 s = get_stroke_fast (); 1134 foreach (Path p in s.paths) { 1135 p.all_segments ((a, b) => { 1136 update_region_boundaries_for_segment (a, b); 1137 return true; 1138 }); 1139 } 1140 } 1141 1142 if (points.size == 1) { 1143 EditPoint e = points.get (0); 1144 xmax = e.x; 1145 xmin = e.x; 1146 ymax = e.y; 1147 ymin = e.y; 1148 } 1149 } 1150 1151 /** Test if @param path is a valid outline for this object. */ 1152 public bool test_is_outline (Path path) { 1153 assert (false); 1154 return this.test_is_outline_of_path (path) && path.test_is_outline_of_path (this); 1155 } 1156 1157 private bool test_is_outline_of_path (Path outline) 1158 requires (outline.points.size >= 2 || points.size >= 2) 1159 { 1160 // rather slow use it for testing, only 1161 unowned EditPoint i = outline.points.get (0).get_link_item (); 1162 unowned EditPoint prev = outline.points.get (outline.points.size - 1).get_link_item (); 1163 1164 double tolerance = 1; 1165 bool g = false; 1166 1167 EditPoint ep = new EditPoint (0, 0); 1168 double min = double.MAX; 1169 1170 while (true) { 1171 min = 10000; 1172 1173 all_of (prev, i, (cx, cy) => { 1174 get_closest_point_on_path (ep, cx, cy); 1175 1176 double n = pow (ep.x - cx, 2) + pow (cy - ep.y, 2); 1177 1178 if (n < min) min = n; 1179 1180 if (n < tolerance) { 1181 g = true; 1182 return false; 1183 } 1184 1185 return true; 1186 }); 1187 1188 if (!g) { 1189 critical (@"this path does not seem to be the outline. (min $min)"); 1190 } 1191 1192 g = false; 1193 1194 if (i == outline.points.get (outline.points.size - 1)) { 1195 break; 1196 } 1197 1198 i = i.get_next (); 1199 } 1200 1201 return true; 1202 } 1203 1204 /** Add the extra point between line handles for double curve. */ 1205 public void add_hidden_double_points () requires (points.size > 1) { 1206 EditPoint hidden; 1207 EditPoint prev; 1208 EditPoint first; 1209 PointType left; 1210 PointType right; 1211 double x, y; 1212 Gee.ArrayList<EditPoint> middle_points = new Gee.ArrayList<EditPoint> (); 1213 Gee.ArrayList<EditPoint> first_points = new Gee.ArrayList<EditPoint> (); 1214 1215 first = is_open () ? points.get (0) : points.get (points.size - 1); 1216 1217 foreach (EditPoint next in points) { 1218 left = first.get_right_handle ().type; 1219 right = next.get_left_handle ().type; 1220 1221 if (next != first && (right == PointType.DOUBLE_CURVE || left == PointType.DOUBLE_CURVE)) { 1222 1223 first.get_right_handle ().type = PointType.QUADRATIC; 1224 1225 // half way between handles 1226 x = first.get_right_handle ().x + (next.get_left_handle ().x - first.get_right_handle ().x) / 2; 1227 y = first.get_right_handle ().y + (next.get_left_handle ().y - first.get_right_handle ().y) / 2; 1228 1229 hidden = new EditPoint (x, y, PointType.QUADRATIC); 1230 hidden.get_right_handle ().type = PointType.QUADRATIC; 1231 hidden.get_left_handle ().type = PointType.QUADRATIC; 1232 hidden.type = PointType.QUADRATIC; 1233 1234 hidden.right_handle.move_to_coordinate_internal (next.get_left_handle ().x, next.get_left_handle ().y); 1235 1236 first.get_right_handle ().type = PointType.QUADRATIC; 1237 first.type = PointType.QUADRATIC; 1238 1239 next.get_left_handle ().type = PointType.QUADRATIC; 1240 next.type = PointType.QUADRATIC; 1241 1242 middle_points.add (hidden); 1243 first_points.add (first); 1244 } 1245 first = next; 1246 } 1247 1248 for (int i = 0; i < middle_points.size; i++) { 1249 hidden = middle_points.get (i); 1250 add_point_after (middle_points.get (i), first_points.get (i)); 1251 } 1252 1253 create_list (); 1254 1255 prev = get_last_point (); 1256 foreach (EditPoint ep in points) { 1257 if (ep.type == PointType.QUADRATIC) { 1258 x = prev.get_right_handle ().x; 1259 y = prev.get_right_handle ().y; 1260 ep.get_left_handle ().move_to_coordinate (x, y); 1261 } 1262 1263 prev = ep; 1264 } 1265 } 1266 1267 /** Convert quadratic bezier points to cubic representation of the glyph 1268 * for ttf-export. 1269 */ 1270 public Path get_quadratic_points () { 1271 PointConverter converter; 1272 converter = new PointConverter (this); 1273 return converter.get_quadratic_path (); 1274 } 1275 1276 public void insert_new_point_on_path (EditPoint ep, double t = -1, bool move_point_to_path = false) { 1277 EditPoint start, stop; 1278 double x0, x1, y0, y1; 1279 double position, min; 1280 PointType left, right; 1281 double closest_x = 0; 1282 double closest_y = 0; 1283 1284 if (ep.next == null || ep.prev == null) { 1285 warning ("missing point"); 1286 return; 1287 } 1288 1289 start = ep.get_prev (); 1290 stop = ep.get_next (); 1291 1292 right = start.get_right_handle ().type; 1293 left = stop.get_left_handle ().type; 1294 1295 if (right == PointType.CUBIC || left == PointType.CUBIC) { 1296 start.get_right_handle ().type = PointType.CUBIC; 1297 stop.get_left_handle ().type = PointType.CUBIC; 1298 } 1299 1300 add_point_after (ep, ep.get_prev ()); 1301 1302 min = double.MAX; 1303 1304 position = 0.5; 1305 1306 if (t < 0) { 1307 all_of (start, stop, (cx, cy, t) => { 1308 double n = pow (ep.x - cx, 2) + pow (ep.y - cy, 2); 1309 1310 if (n < min) { 1311 min = n; 1312 position = t; 1313 closest_x = cx; 1314 closest_y = cy; 1315 } 1316 1317 return true; 1318 }); 1319 1320 if (move_point_to_path) { 1321 ep.x = closest_x; 1322 ep.y = closest_y; 1323 } 1324 } else { 1325 position = t; 1326 } 1327 1328 if (right == PointType.DOUBLE_CURVE || left == PointType.DOUBLE_CURVE) { 1329 double_bezier_vector (position, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x, out x0, out x1); 1330 double_bezier_vector (position, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y, out y0, out y1); 1331 1332 ep.get_left_handle ().set_point_type (PointType.DOUBLE_CURVE); 1333 ep.get_right_handle ().set_point_type (PointType.DOUBLE_CURVE); 1334 1335 ep.get_left_handle ().move_to_coordinate (x0, y0); 1336 ep.get_right_handle ().move_to_coordinate (x1, y1); 1337 1338 ep.type = PointType.DOUBLE_CURVE; 1339 } else if (right == PointType.QUADRATIC) { 1340 x0 = quadratic_bezier_vector (1 - position, stop.x, start.get_right_handle ().x, start.x); 1341 y0 = quadratic_bezier_vector (1 - position, stop.y, start.get_right_handle ().y, start.y); 1342 ep.get_right_handle ().move_to_coordinate (x0, y0); 1343 1344 ep.get_left_handle ().set_point_type (PointType.QUADRATIC); 1345 ep.get_right_handle ().set_point_type (PointType.QUADRATIC); 1346 1347 ep.get_left_handle ().move_to_coordinate_internal (0, 0); 1348 1349 ep.type = PointType.QUADRATIC; 1350 } else if (right == PointType.CUBIC || left == PointType.CUBIC) { 1351 bezier_vector (position, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x, out x0, out x1); 1352 bezier_vector (position, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y, out y0, out y1); 1353 1354 ep.get_left_handle ().set_point_type (PointType.CUBIC); 1355 ep.get_left_handle ().move_to_coordinate (x0, y0); 1356 1357 ep.get_right_handle ().set_point_type (PointType.CUBIC); 1358 ep.get_right_handle ().move_to_coordinate (x1, y1); 1359 1360 ep.type = PointType.LINE_CUBIC; 1361 } else if (right == PointType.LINE_QUADRATIC && left == PointType.LINE_QUADRATIC) { 1362 ep.get_right_handle ().set_point_type (PointType.LINE_QUADRATIC); 1363 ep.get_left_handle ().set_point_type (PointType.LINE_QUADRATIC); 1364 ep.type = PointType.QUADRATIC; 1365 } else if (right == PointType.LINE_CUBIC && left == PointType.LINE_CUBIC) { 1366 ep.get_right_handle ().set_point_type (PointType.LINE_CUBIC); 1367 ep.get_left_handle ().set_point_type (PointType.LINE_CUBIC); 1368 ep.type = PointType.LINE_CUBIC; 1369 } else if (right == PointType.LINE_DOUBLE_CURVE && left == PointType.LINE_DOUBLE_CURVE) { 1370 ep.get_right_handle ().set_point_type (PointType.LINE_DOUBLE_CURVE); 1371 ep.get_left_handle ().set_point_type (PointType.LINE_DOUBLE_CURVE); 1372 ep.type = PointType.DOUBLE_CURVE; 1373 } else { 1374 warning (@"Point types: $right and $left in insert_new_point_on_path"); 1375 } 1376 1377 ep.get_left_handle ().parent = ep; 1378 ep.get_right_handle ().parent = ep; 1379 1380 stop.get_left_handle ().length *= 1 - position; 1381 start.get_right_handle ().length *= position; 1382 1383 if (right == PointType.QUADRATIC) { // update connected handle 1384 if (ep.prev != null) { 1385 ep.get_left_handle ().move_to_coordinate_internal ( 1386 ep.get_prev ().right_handle.x, 1387 ep.get_prev ().right_handle.y); 1388 1389 } else { 1390 warning ("ep.prev is null for quadratic point"); 1391 } 1392 } 1393 1394 create_list (); 1395 foreach (EditPoint p in points) { 1396 p.recalculate_linear_handles (); 1397 } 1398 } 1399 1400 /** Get a point on the this path closest to x and y coordinates. 1401 * Don't look for a point in the segment skip_previous to skip_next. 1402 */ 1403 public void get_closest_point_on_path (EditPoint edit_point, double x, double y, 1404 EditPoint? skip_previous = null, EditPoint? skip_next = null, 1405 int steps = -1) { 1406 return_if_fail (points.size >= 1); 1407 1408 double min = double.MAX; 1409 double n = 0; 1410 bool g = false; 1411 1412 double ox = 0; 1413 double oy = 0; 1414 1415 EditPoint prev = points.get (points.size - 1); 1416 EditPoint i = points.get (0); 1417 1418 bool done = false; 1419 bool exit = false; 1420 bool first = true; 1421 1422 EditPoint? previous_point = null; 1423 EditPoint? next_point = null; 1424 1425 EditPoint previous; 1426 EditPoint next; 1427 double step = 0; 1428 1429 if (points.size == 0) { 1430 warning ("Empty path."); 1431 return; 1432 } 1433 1434 if (points.size == 1) { 1435 edit_point.x = i.x; 1436 edit_point.y = i.y; 1437 1438 edit_point.prev = i; 1439 edit_point.next = i; 1440 return; 1441 } 1442 1443 edit_point.x = i.x; 1444 edit_point.y = i.y; 1445 1446 create_list (); 1447 1448 while (!exit) { 1449 if (!first && i == points.get (points.size - 1)) { 1450 done = true; 1451 } 1452 1453 if (!done) { 1454 i = i.get_next (); 1455 prev = i.get_prev (); 1456 } else if (done && !is_open ()) { 1457 i = points.get (0); 1458 prev = points.get (points.size - 1); 1459 exit = true; 1460 } else { 1461 break; 1462 } 1463 1464 if (skip_previous == prev) { 1465 continue; 1466 } 1467 1468 if (prev.prev != null && skip_previous == prev.get_prev ()) { 1469 continue; 1470 } 1471 1472 if (skip_next == i) { 1473 continue; 1474 } 1475 1476 if (prev.next != null && skip_next == prev.get_next ()) { 1477 continue; 1478 } 1479 1480 all_of (prev, i, (cx, cy, t) => { 1481 n = pow (x - cx, 2) + pow (y - cy, 2); 1482 1483 if (n < min) { 1484 min = n; 1485 1486 ox = cx; 1487 oy = cy; 1488 1489 previous_point = i.prev; 1490 next_point = i; 1491 1492 step = t; 1493 1494 g = true; 1495 } 1496 1497 return true; 1498 }, steps); 1499 1500 first = false; 1501 } 1502 1503 if (previous_point == null && is_open ()) { 1504 previous_point = points.get (points.size - 1).get_link_item (); 1505 } 1506 1507 if (previous_point == null) { 1508 warning (@"previous_point == null, points.size: $(points.size)"); 1509 return; 1510 } 1511 1512 if (next_point == null) { 1513 warning ("next_point != null"); 1514 return; 1515 } 1516 1517 previous = (!) previous_point; 1518 next = (!) next_point; 1519 1520 edit_point.prev = previous_point; 1521 edit_point.next = next_point; 1522 1523 edit_point.set_position (ox, oy); 1524 1525 edit_point.type = previous.type; 1526 edit_point.get_left_handle ().type = previous.get_right_handle ().type; 1527 edit_point.get_right_handle ().type = next.get_left_handle ().type; 1528 } 1529 1530 public static bool all_of (EditPoint start, EditPoint stop, 1531 RasterIterator iter, int steps = -1, 1532 double min_t = 0, double max_t = 1) { 1533 1534 PointType right = PenTool.to_curve (start.get_right_handle ().type); 1535 PointType left = PenTool.to_curve (stop.get_left_handle ().type); 1536 1537 if (steps == -1) { 1538 steps = (int) (10 * get_length_from (start, stop)); 1539 } 1540 1541 if (right == PointType.DOUBLE_CURVE || left == PointType.DOUBLE_CURVE) { 1542 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); 1543 } else if (right == PointType.QUADRATIC && left == PointType.QUADRATIC) { 1544 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); 1545 } else if (right == PointType.CUBIC && left == PointType.CUBIC) { 1546 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); 1547 } 1548 1549 if (start.x == stop.x && start.y == stop.y) { 1550 warning ("Zero length."); 1551 return true; 1552 } 1553 1554 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))"); 1555 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); 1556 } 1557 1558 public static void get_point_for_step (EditPoint start, EditPoint stop, double step, 1559 out double x, out double y) { 1560 1561 PointType right = PenTool.to_curve (start.type); 1562 PointType left = PenTool.to_curve (stop.type); 1563 1564 if (right == PointType.DOUBLE_CURVE || left == PointType.DOUBLE_CURVE) { 1565 x = double_bezier_path (step, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x); 1566 y = double_bezier_path (step, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y); 1567 } else if (right == PointType.QUADRATIC && left == PointType.QUADRATIC) { 1568 x = quadratic_bezier_path (step, start.x, start.get_right_handle ().x, stop.x); 1569 y = quadratic_bezier_path (step, start.y, start.get_right_handle ().y, stop.y); 1570 } else if (right == PointType.CUBIC && left == PointType.CUBIC) { 1571 x = bezier_path (step, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x); 1572 y = bezier_path (step, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y); 1573 } else if (right == PointType.HIDDEN && left == PointType.HIDDEN) { 1574 x = bezier_path (step, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x); 1575 y = bezier_path (step, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y); 1576 } else { 1577 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))"); 1578 x = bezier_path (step, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x); 1579 y = bezier_path (step, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y); 1580 } 1581 } 1582 1583 private static bool all_of_double (double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, 1584 RasterIterator iter, double steps = 400, double min_t = 0, double max_t = 1) { 1585 1586 double px = x1; 1587 double py = y1; 1588 1589 double t; 1590 double middle_x, middle_y; 1591 double double_step; 1592 1593 middle_x = x1 + (x2 - x1) / 2; 1594 middle_y = y1 + (y2 - y1) / 2; 1595 1596 for (int i = 0; i < steps; i++) { 1597 t = i / steps + min_t; 1598 1599 px = quadratic_bezier_path (t, x0, x1, middle_x); 1600 py = quadratic_bezier_path (t, y0, y1, middle_y); 1601 1602 double_step = t / 2; 1603 1604 if (double_step > max_t) { 1605 return false; 1606 } 1607 1608 if (!iter (px, py, double_step)) { 1609 return false; 1610 } 1611 } 1612 1613 for (int i = 0; i < steps; i++) { 1614 t = i / steps + min_t; 1615 1616 px = quadratic_bezier_path (t, middle_x, x2, x3); 1617 py = quadratic_bezier_path (t, middle_y, y2, y3); 1618 1619 double_step = 0.5 + t / 2; 1620 1621 if (double_step > max_t) { 1622 return false; 1623 } 1624 1625 if (!iter (px, py, double_step)) { 1626 return false; 1627 } 1628 } 1629 1630 return true; 1631 } 1632 1633 private static bool all_of_quadratic_curve (double x0, double y0, double x1, double y1, double x2, double y2, 1634 RasterIterator iter, double steps = 400, double min_t = 0, double max_t = 1) { 1635 double px = x1; 1636 double py = y1; 1637 1638 double t; 1639 1640 for (int i = 0; i < steps; i++) { 1641 t = i / steps + min_t; 1642 1643 px = quadratic_bezier_path (t, x0, x1, x2); 1644 py = quadratic_bezier_path (t, y0, y1, y2); 1645 1646 if (t > max_t) { 1647 return false; 1648 } 1649 1650 if (!iter (px, py, t)) { 1651 return false; 1652 } 1653 } 1654 1655 return true; 1656 } 1657 1658 private static bool all_of_curve (double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, 1659 RasterIterator iter, double steps = 400, double min_t = 0, double max_t = 1) { 1660 double px = x1; 1661 double py = y1; 1662 1663 double t; 1664 1665 for (int i = 0; i < steps; i++) { 1666 t = i / steps + min_t; 1667 1668 px = bezier_path (t, x0, x1, x2, x3); 1669 py = bezier_path (t, y0, y1, y2, y3); 1670 1671 if (t > max_t) { 1672 return false; 1673 } 1674 1675 if (!iter (px, py, t)) { 1676 return false; 1677 } 1678 } 1679 1680 return true; 1681 } 1682 1683 public bool all_segments (SegmentIterator iter) { 1684 unowned EditPoint i, next; 1685 1686 if (points.size < 2) { 1687 return false; 1688 } 1689 1690 for (int j = 0; j < points.size - 1; j++) { 1691 i = points.get (j).get_link_item (); 1692 next = i.get_next (); 1693 if (!iter (i, next)) { 1694 return false; 1695 } 1696 } 1697 1698 if (!is_open ()) { 1699 return iter (points.get (points.size - 1), points.get (0)); 1700 } 1701 1702 return true; 1703 } 1704 1705 public void all_of_path (RasterIterator iter, int steps = -1) { 1706 all_segments ((start, stop) => { 1707 return all_of (start, stop, iter, steps); 1708 }); 1709 } 1710 1711 public static double bezier_path (double step, double p0, double p1, double p2, double p3) { 1712 double q0, q1, q2; 1713 double r0, r1; 1714 1715 q0 = step * (p1 - p0) + p0; 1716 q1 = step * (p2 - p1) + p1; 1717 q2 = step * (p3 - p2) + p2; 1718 1719 r0 = step * (q1 - q0) + q0; 1720 r1 = step * (q2 - q1) + q1; 1721 1722 return step * (r1 - r0) + r0; 1723 } 1724 1725 public static void bezier_vector (double step, double p0, double p1, double p2, double p3, out double a0, out double a1) { 1726 double q0, q1, q2; 1727 1728 q0 = step * (p1 - p0) + p0; 1729 q1 = step * (p2 - p1) + p1; 1730 q2 = step * (p3 - p2) + p2; 1731 1732 a0 = step * (q1 - q0) + q0; 1733 a1 = step * (q2 - q1) + q1; 1734 } 1735 1736 public static double quadratic_bezier_vector (double step, double p0, double p1, double p2) { 1737 return step * (p1 - p0) + p0; 1738 } 1739 1740 public static double quadratic_bezier_path (double step, double p0, double p1, double p2) { 1741 double q0 = step * (p1 - p0) + p0; 1742 double q1 = step * (p2 - p1) + p1; 1743 1744 return step * (q1 - q0) + q0; 1745 } 1746 1747 public static double double_bezier_path (double step, double p0, double p1, double p2, double p3) { 1748 double middle = p1 + (p2 - p1) / 2; 1749 1750 if (step == 0.5) { 1751 // FIXME: return the middle point 1752 warning ("Middle"); 1753 } 1754 1755 if (step < 0.5) { 1756 return quadratic_bezier_path (2 * step, p0, p1, middle); 1757 } 1758 1759 return quadratic_bezier_path (2 * (step - 0.5), middle, p2, p3); 1760 } 1761 1762 public static void double_bezier_vector (double step, double p0, double p1, double p2, double p3, out double a0, out double a1) { 1763 double b0, b1, c0, c1, d0, d1; 1764 1765 if (unlikely (step <= 0 || step >= 1)) { 1766 warning (@"Bad step: $step"); 1767 step += 0.00004; 1768 } 1769 1770 // set angle 1771 b0 = double_bezier_path (step + 0.00001, p0, p1, p2, p3); 1772 c0 = double_bezier_path (step + 0.00002, p0, p1, p2, p3); 1773 1774 b1 = double_bezier_path (step - 0.00001, p0, p1, p2, p3); 1775 c1 = double_bezier_path (step - 0.00002, p0, p1, p2, p3); 1776 1777 // adjust length 1778 d0 = b0 + (b0 - c0) * 25000 * (step); 1779 d1 = b1 + (b1 - c1) * 25000 * (1 - step); 1780 1781 a0 = d0; 1782 a1 = d1; 1783 } 1784 1785 public static void get_handles_for_step (EditPoint start, EditPoint stop, double step, 1786 out double x1, out double y1, out double x2, out double y2) { 1787 1788 PointType right = PenTool.to_curve (start.type); 1789 PointType left = PenTool.to_curve (stop.type); 1790 1791 if (right == PointType.DOUBLE_CURVE || left == PointType.DOUBLE_CURVE) { 1792 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? 1793 double_bezier_vector (step, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y, out y1, out y2); 1794 } else if (right == PointType.QUADRATIC && left == PointType.QUADRATIC) { 1795 x1 = quadratic_bezier_vector (step, start.x, start.get_right_handle ().x, stop.x); 1796 y1 = quadratic_bezier_vector (step, start.y, start.get_right_handle ().y, stop.y); 1797 x2 = x1; 1798 y2 = y1; 1799 } else if (right == PointType.CUBIC && left == PointType.CUBIC) { 1800 bezier_vector (step, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x, out x1, out x2); 1801 bezier_vector (step, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y, out y1, out y2); 1802 } else if (right == PointType.HIDDEN && left == PointType.HIDDEN) { 1803 bezier_vector (step, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x, out x1, out x2); 1804 bezier_vector (step, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y, out y1, out y2); 1805 } else { 1806 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))"); 1807 bezier_vector (step, start.x, start.get_right_handle ().x, stop.get_left_handle ().x, stop.x, out x1, out x2); 1808 bezier_vector (step, start.y, start.get_right_handle ().y, stop.get_left_handle ().y, stop.y, out y1, out y2); 1809 } 1810 } 1811 1812 public void plot (Context cr, WidgetAllocation allocation, double view_zoom) { 1813 double px = 0, py = 0; 1814 double xc = allocation.width / 2.0; 1815 double yc = allocation.height / 2.0; 1816 1817 cr.save (); 1818 1819 all_of_path ((x, y) => { 1820 cr.move_to (px + xc, -py + yc); 1821 cr.line_to (x + xc, -y + yc); 1822 1823 px = x; 1824 py = y; 1825 1826 return true; 1827 }); 1828 1829 cr.stroke (); 1830 cr.restore (); 1831 } 1832 1833 public void print_boundaries () { 1834 stderr.printf (@"xmax $xmax \n"); 1835 stderr.printf (@"xmin $xmin \n"); 1836 stderr.printf (@"ymax $ymax \n"); 1837 stderr.printf (@"ymin $ymin \n"); 1838 } 1839 1840 public bool has_region_boundaries () { 1841 return !(xmax == -10000 || xmin == 10000 || ymax == -10000 || ymin == 10000); 1842 } 1843 1844 public void create_list () { 1845 EditPoint ep; 1846 1847 if (points.size == 0) { 1848 return; 1849 } 1850 1851 if (points.size == 1) { 1852 ep = points.get (0); 1853 ep.next = null; 1854 ep.prev = null; 1855 return; 1856 } 1857 1858 ep = points.get (0); 1859 ep.next = points.get (1).get_link_item (); 1860 ep.prev = points.get (points.size - 1).get_link_item (); 1861 1862 for (int i = 1; i < points.size - 1; i++) { 1863 ep = points.get (i); 1864 ep.prev = points.get (i - 1).get_link_item (); 1865 ep.next = points.get (i + 1).get_link_item (); 1866 } 1867 1868 ep = points.get (points.size - 1); 1869 ep.next = points.get (0).get_link_item (); 1870 ep.prev = points.get (points.size - 2).get_link_item (); 1871 } 1872 1873 public bool has_point (EditPoint ep) { 1874 return points.contains (ep); 1875 } 1876 1877 public bool has_deleted_point () { 1878 foreach (EditPoint p in points) { 1879 if (p.deleted) { 1880 return true; 1881 } 1882 } 1883 return false; 1884 } 1885 1886 /** @return the remaining parts as a new path. */ 1887 public PathList process_deleted_points () 1888 requires (points.size > 0) 1889 { 1890 EditPoint p; 1891 EditPoint ep; 1892 Path current_path = new Path (); 1893 Path remaining_points = new Path (); 1894 PathList path_list = new PathList (); 1895 int i; 1896 int index = 0; 1897 1898 remaining_points.stroke = stroke; 1899 current_path.stroke = stroke; 1900 1901 if (!has_deleted_point ()) { 1902 return path_list; 1903 } 1904 1905 if (points.size == 1) { 1906 points.remove_at (0); 1907 return path_list; 1908 } 1909 1910 // set start position to a point that will be removed 1911 for (i = 0; i < points.size; i++) { 1912 p = points.get (i); 1913 1914 if (p.deleted) { 1915 index = i; 1916 i++; 1917 ep = p; 1918 break; 1919 } 1920 } 1921 1922 // don't tie end points on the open path 1923 if (points.size > 1) { 1924 p = points.get (1); 1925 p.convert_to_curve (); 1926 p.set_reflective_handles (false); 1927 p.set_tie_handle (false); 1928 } 1929 1930 if (points.size > 0) { 1931 p = points.get (points.size - 1); 1932 p.convert_to_curve (); 1933 p.set_reflective_handles (false); 1934 p.set_tie_handle (false); 1935 } 1936 1937 // copy points after the deleted point 1938 while (i < points.size) { 1939 p = points.get (i); 1940 current_path.add_point (p); 1941 i++; 1942 } 1943 1944 // copy points before the deleted point 1945 for (i = 0; i < index; i++) { 1946 p = points.get (i); 1947 remaining_points.add_point (p); 1948 } 1949 1950 // merge if we still only have one path 1951 if (!is_open ()) { 1952 foreach (EditPoint point in remaining_points.points) { 1953 current_path.add_point (point.copy ()); 1954 } 1955 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 path_list.add (current_path); 1963 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 } else { 1969 if (current_path.points.size > 0) { 1970 ep = current_path.points.get (0); 1971 ep.set_tie_handle (false); 1972 ep.set_reflective_handles (false); 1973 ep.get_left_handle ().type = PenTool.to_line (ep.type); 1974 ep.type = PenTool.to_curve (ep.type); 1975 set_new_start (current_path.points.get (0)); 1976 path_list.add (current_path); 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 if (remaining_points.points.size > 0) { 1983 remaining_points.points.get (0).set_tie_handle (false); 1984 remaining_points.points.get (0).set_reflective_handles (false); 1985 remaining_points.points.get (0).type = remaining_points.points.get (0).type; 1986 set_new_start (remaining_points.points.get (0)); 1987 path_list.add (remaining_points); 1988 1989 if (current_path.points.size > 0) { 1990 ep = current_path.points.get (current_path.points.size - 1); 1991 ep.get_right_handle ().type = PenTool.to_line (ep.type); 1992 ep.type = PenTool.to_curve (ep.get_right_handle ().type); 1993 } 1994 } 1995 } 1996 1997 foreach (Path path in path_list.paths) { 1998 path.update_region_boundaries (); 1999 } 2000 2001 return path_list; 2002 } 2003 2004 public void set_new_start (EditPoint ep) 2005 requires (points.size > 0) { 2006 Gee.ArrayList<EditPoint> list = new Gee.ArrayList<EditPoint> (); 2007 int start = 0; 2008 2009 for (int i = 0; i < points.size; i++) { 2010 if (ep == points.get (i)) { 2011 start = i; 2012 } 2013 } 2014 2015 for (int i = start; i < points.size; i++) { 2016 list.add (points.get (i)); 2017 } 2018 2019 for (int i = 0; i < start; i++) { 2020 list.add (points.get (i)); 2021 } 2022 2023 control_points = list; 2024 } 2025 2026 public void append_path (Path path) { 2027 if (points.size == 0 || path.points.size == 0) { 2028 warning ("No points"); 2029 return; 2030 } 2031 2032 // copy remaining points 2033 foreach (EditPoint p in path.points) { 2034 add_point (p.copy ()); 2035 } 2036 2037 path.points.clear (); 2038 } 2039 2040 /** Roatate around coordinate xc, xc. */ 2041 public void rotate (double theta, double xc, double yc) { 2042 double a, radius; 2043 2044 foreach (EditPoint ep in points) { 2045 radius = sqrt (pow (xc - ep.x, 2) + pow (yc + ep.y, 2)); 2046 2047 if (yc + ep.y < 0) { 2048 radius = -radius; 2049 } 2050 2051 a = acos ((ep.x - xc) / radius); 2052 2053 ep.x = xc + cos (a - theta) * radius; 2054 ep.y = yc + sin (a - theta) * radius; 2055 2056 ep.get_right_handle ().angle -= theta; 2057 ep.get_left_handle ().angle -= theta; 2058 2059 while (ep.get_right_handle ().angle < 0) { 2060 ep.get_right_handle ().angle += 2 * PI; 2061 } 2062 2063 while (ep.get_left_handle ().angle < 0) { 2064 ep.get_left_handle ().angle += 2 * PI; 2065 } 2066 } 2067 2068 rotation += theta; 2069 rotation %= 2 * PI; 2070 2071 update_region_boundaries (); 2072 } 2073 2074 public void flip_vertical () { 2075 EditPointHandle hl, hr; 2076 double lx, ly, rx, ry; 2077 2078 foreach (EditPoint e in points) { 2079 hl = e.get_left_handle (); 2080 hr = e.get_right_handle (); 2081 2082 lx = hl.x; 2083 ly = hl.y; 2084 rx = hr.x; 2085 ry = hr.y; 2086 2087 e.y *= -1; 2088 2089 hr.move_to_coordinate_internal (rx, -1 * ry); 2090 hl.move_to_coordinate_internal (lx, -1 * ly); 2091 } 2092 2093 update_region_boundaries (); 2094 } 2095 2096 public void flip_horizontal () { 2097 EditPointHandle hl, hr; 2098 double lx, ly, rx, ry; 2099 foreach (EditPoint e in points) { 2100 hl = e.get_left_handle (); 2101 hr = e.get_right_handle (); 2102 2103 lx = hl.x; 2104 ly = hl.y; 2105 rx = hr.x; 2106 ry = hr.y; 2107 2108 e.x *= -1; 2109 2110 hr.move_to_coordinate_internal (-1 * rx, ry); 2111 hl.move_to_coordinate_internal (-1 * lx, ly); 2112 } 2113 2114 update_region_boundaries (); 2115 } 2116 2117 public void init_point_type (PointType pt = PointType.NONE) { 2118 PointType type; 2119 2120 if (pt == PointType.NONE) { 2121 pt = DrawingTools.point_type; 2122 } 2123 2124 switch (pt) { 2125 case PointType.QUADRATIC: 2126 type = PointType.LINE_QUADRATIC; 2127 break; 2128 case PointType.DOUBLE_CURVE: 2129 type = PointType.LINE_DOUBLE_CURVE; 2130 break; 2131 case PointType.CUBIC: 2132 type = PointType.LINE_CUBIC; 2133 break; 2134 default: 2135 warning ("No type is set"); 2136 type = PointType.LINE_CUBIC; 2137 break; 2138 } 2139 2140 foreach (EditPoint ep in points) { 2141 ep.type = type; 2142 ep.get_right_handle ().type = type; 2143 ep.get_left_handle ().type = type; 2144 } 2145 } 2146 2147 public void convert_path_ending_to_line () { 2148 if (points.size < 2) { 2149 return; 2150 } 2151 2152 get_first_point ().get_left_handle ().convert_to_line (); 2153 get_last_point ().get_right_handle ().convert_to_line (); 2154 } 2155 2156 public void print_all_types () { 2157 print (@"Control points:\n"); 2158 foreach (EditPoint ep in points) { 2159 print (@"$(ep.type) L: $(ep.get_left_handle ().type) R: L: $(ep.get_right_handle ().type)\n"); 2160 } 2161 } 2162 2163 /** Find the point where two lines intersect. */ 2164 public static void find_intersection (double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, 2165 out double point_x, out double point_y) { 2166 point_x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)); 2167 point_y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)); 2168 } 2169 2170 public static void find_intersection_handle (EditPointHandle h1, EditPointHandle h2, out double point_x, out double point_y) { 2171 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); 2172 } 2173 2174 /** Finx intersection point for two straight lines. */ 2175 public static void find_intersection_point (EditPoint p1, EditPoint p2, EditPoint q1, EditPoint q2, out double point_x, out double point_y) { 2176 find_intersection (p1.x, p1.y, p2.x, p2.y, q1.x, q1.y, q2.x, q2.y, out point_x, out point_y); 2177 } 2178 2179 public void add_extrema () { 2180 double x0, y0, x1, y1, x2, y2, x3, y3; 2181 double minx, maxx, miny, maxy; 2182 2183 if (unlikely (points.size < 2)) { 2184 warning (@"Missing points, $(points.size) points in path."); 2185 return; 2186 } 2187 2188 minx = Glyph.CANVAS_MAX; 2189 miny = Glyph.CANVAS_MAX; 2190 maxx = Glyph.CANVAS_MIN; 2191 maxy = Glyph.CANVAS_MIN; 2192 2193 x0 = 0; 2194 y0 = 0; 2195 x1 = 0; 2196 y1 = 0; 2197 x2 = 0; 2198 y2 = 0; 2199 x3 = 0; 2200 y3 = 0; 2201 2202 all_of_path ((x, y) => { 2203 if (x < minx) { 2204 x0 = x; 2205 y0 = y; 2206 minx = x; 2207 } 2208 2209 if (x > maxx) { 2210 x1 = x; 2211 y1 = y; 2212 maxx = x; 2213 } 2214 2215 if (y < miny) { 2216 x2 = x; 2217 y2 = y; 2218 miny = y; 2219 } 2220 2221 if (y > maxy) { 2222 x3 = x; 2223 y3 = y; 2224 maxy = y; 2225 } 2226 2227 return true; 2228 }); 2229 2230 insert_new_point_on_path_at (x0 - 0.001, y0); 2231 insert_new_point_on_path_at (x1 + 0.001, y1); 2232 insert_new_point_on_path_at (x2, y2 - 0.001); 2233 insert_new_point_on_path_at (x3, y3 + 0.001); 2234 } 2235 2236 public EditPoint insert_new_point_on_path_at (double x, double y) { 2237 EditPoint ep = new EditPoint (); 2238 EditPoint prev, next; 2239 bool exists; 2240 2241 if (points.size < 2) { 2242 warning ("Can't add extrema to just one point."); 2243 return ep; 2244 } 2245 2246 get_closest_point_on_path (ep, x, y); 2247 2248 next = (ep.next == null) ? points.get (0) : ep.get_next (); 2249 prev = (ep.prev == null) ? points.get (points.size - 1) : ep.get_prev (); 2250 2251 exists = prev.x == ep.x && prev.y == ep.y; 2252 exists |= next.x == ep.x && next.y == ep.y; 2253 2254 if (!exists) { 2255 insert_new_point_on_path (ep); 2256 } 2257 2258 return ep; 2259 } 2260 2261 public static bool is_counter (PathList pl, Path path) { 2262 return counters (pl, path) % 2 != 0; 2263 } 2264 2265 public static int counters (PathList pl, Path path) { 2266 int inside_count = 0; 2267 bool inside; 2268 PathList lines = new PathList (); 2269 2270 lines = pl; 2271 2272 /** // FIXME: Check automatic orientation. 2273 foreach (Path p in pl.paths) { 2274 lines.add (SvgParser.get_lines (p)); 2275 } 2276 */ 2277 2278 foreach (Path p in lines.paths) { 2279 if (p.points.size > 1 && p != path 2280 && path.boundaries_intersecting (p)) { 2281 2282 inside = false; 2283 foreach (EditPoint ep in path.points) { 2284 if (SvgParser.is_inside (ep, p)) { 2285 inside = true; 2286 } 2287 } 2288 2289 if (inside) { 2290 inside_count++; 2291 } 2292 } 2293 } 2294 2295 return inside_count; 2296 } 2297 2298 public bool boundaries_intersecting (Path p) { 2299 return in_boundaries (p.xmin, p.xmax, p.ymin, p.ymax); 2300 } 2301 2302 public bool in_boundaries (double other_xmin, double other_xmax, double other_ymin, double other_ymax) { 2303 return ((xmin <= other_xmin <= xmax) || (xmin <= other_xmax <= xmax) 2304 || (other_xmin <= xmin <= other_xmax) || (other_xmin <= xmax <= other_xmax)) 2305 && ((ymin <= other_ymin <= ymax) || (ymin <= other_ymax <= ymax) 2306 || (other_ymin <= ymin <= other_ymax) || (other_ymin <= ymax <= other_ymax)); 2307 } 2308 2309 /** @param t smallest distance to other points. */ 2310 public void remove_points_on_points (double t = 0.00001) { 2311 Gee.ArrayList<EditPoint> remove = new Gee.ArrayList<EditPoint> (); 2312 EditPoint n; 2313 EditPointHandle hr, h; 2314 double t3 = t / 3; 2315 2316 if (points.size == 0) { 2317 return; 2318 } 2319 2320 for (int i = 0; i < points.size + 1; i++) { 2321 EditPoint ep = points.get (i % points.size); 2322 if (ep.get_right_handle ().length < t3 2323 && ep.get_left_handle ().length < t3 2324 && !is_endpoint (ep) 2325 && (ep.flags & EditPoint.CURVE_KEEP) == 0 2326 && (ep.flags & EditPoint.SEGMENT_END) == 0) { 2327 ep.deleted = true; 2328 } 2329 } 2330 2331 remove_deleted_points (); 2332 2333 for (int i = 0; i < points.size + 1; i++) { 2334 EditPoint ep = points.get (i % points.size); 2335 n = points.get ((i + 1) % points.size); 2336 2337 if (Path.distance_to_point (n, ep) < t) { 2338 remove.add (ep); 2339 } 2340 } 2341 2342 create_list (); 2343 2344 foreach (EditPoint r in remove) { 2345 if (points.size == 0) { 2346 return; 2347 } 2348 2349 if (r.next != null) { 2350 n = r.get_next (); 2351 } else { 2352 n = points.get (0); 2353 } 2354 2355 points.remove (r); 2356 h = n.get_left_handle (); 2357 hr = r.get_left_handle (); 2358 h.length = hr.length; 2359 h.angle = hr.angle; 2360 h.type = hr.type; 2361 2362 if (h.length < t) { 2363 h.length = t; 2364 h.angle = n.get_right_handle ().angle - PI; 2365 } 2366 2367 create_list (); 2368 } 2369 2370 recalculate_linear_handles (); 2371 } 2372 2373 public bool is_endpoint (EditPoint ep) { 2374 if (points.size == 0) { 2375 return false; 2376 } 2377 2378 return ep == points.get (0) || ep == points.get (points.size - 1); 2379 } 2380 2381 public void remove_deleted_points () { 2382 Gee.ArrayList<EditPoint> p = new Gee.ArrayList<EditPoint> (); 2383 2384 foreach (EditPoint ep in points) { 2385 if (ep.deleted) { 2386 p.add (ep); 2387 } 2388 } 2389 2390 foreach (EditPoint e in p) { 2391 points.remove (e); 2392 } 2393 2394 create_list (); 2395 } 2396 2397 public static void find_closes_point_in_segment (EditPoint ep0, EditPoint ep1, 2398 double px, double py, 2399 out double nx, out double ny, 2400 double max_step = 200) { 2401 2402 double min_distance = double.MAX; 2403 double npx, npy; 2404 double min_t, max_t; 2405 double rmin_t, rmax_t; 2406 bool found; 2407 int step; 2408 2409 npx = 0; 2410 npy = 0; 2411 2412 min_t = 0; 2413 max_t = 1; 2414 2415 rmin_t = 0; 2416 rmax_t = 1; 2417 2418 for (step = 3; step <= max_step; step *= 2) { 2419 found = false; 2420 min_distance = double.MAX; 2421 Path.all_of (ep0, ep1, (xa, ya, ta) => { 2422 double d = Path.distance (px, xa, py, ya); 2423 2424 if (d < min_distance) { 2425 min_distance = d; 2426 npx = xa; 2427 npy = ya; 2428 rmin_t = ta - 1.0 / step; 2429 rmax_t = ta + 1.0 / step; 2430 found = true; 2431 } 2432 2433 return true; 2434 }, step, min_t, max_t); 2435 2436 if (!found) { 2437 rmin_t = 1 - (1.0 / step); 2438 rmax_t = 1; 2439 } 2440 2441 min_t = (rmin_t > 0) ? rmin_t : 0; 2442 max_t = (rmax_t < 1) ? rmax_t : 1; 2443 } 2444 2445 nx = npx; 2446 ny = npy; 2447 } 2448 2449 public void reset_stroke () { 2450 full_stroke = null; 2451 fast_stroke = null; 2452 } 2453 2454 public void create_full_stroke () { 2455 if (stroke <= 0) { 2456 return; 2457 } 2458 2459 print(@"Create full stroke for $(points.size) points.\n"); 2460 StrokeTask task = new StrokeTask (this); 2461 MainWindow.native_window.run_non_blocking_background_thread (task); 2462 2463 stop_stroke_creator (); 2464 stroke_creator = task; 2465 } 2466 2467 public void stop_stroke_creator () { 2468 if (stroke_creator != null) { 2469 ((!) stroke_creator).cancel (); 2470 } 2471 } 2472 2473 public PathList get_stroke () { 2474 if (full_stroke == null) { 2475 StrokeTool s = new StrokeTool (); 2476 full_stroke = s.get_stroke (this, stroke); 2477 } 2478 2479 return (!) full_stroke; 2480 } 2481 2482 public PathList get_stroke_fast () { 2483 if (full_stroke != null) { 2484 return (!) full_stroke; 2485 } 2486 2487 if (fast_stroke != null) { 2488 return (!) fast_stroke; 2489 } 2490 2491 StrokeTool s = new StrokeTool (); 2492 Test t = new Test.time ("fast stroke"); 2493 fast_stroke = s.get_stroke_fast (this, stroke); 2494 t.print(); 2495 2496 return (!) fast_stroke; 2497 } 2498 2499 // Callback for path simplifier 2500 public void add_cubic_bezier_points (double x0, double y0, double x1, double y1, 2501 double x2, double y2, double x3, double y3) { 2502 2503 EditPoint start; 2504 EditPoint end; 2505 2506 if (points.size > 0) { 2507 start = get_last_point (); 2508 } else { 2509 start = add (x0, y0); 2510 } 2511 2512 end = add (x3, y3); 2513 2514 start.set_point_type (PointType.CUBIC); 2515 end.set_point_type (PointType.CUBIC); 2516 2517 start.convert_to_curve (); 2518 end.convert_to_curve (); 2519 2520 start.get_right_handle ().move_to_coordinate (x1, y1); 2521 end.get_left_handle ().move_to_coordinate (x2, y2); 2522 } 2523 } 2524 2525 } 2526