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