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