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