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