The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

PenTool.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
Circle boundaries heads/master
1 2 /* 3 Copyright (C) 2012 2013 2014 2015 Johan Mattsson 4 5 This library is free software; you can redistribute it and/or modify 6 it under the terms of the GNU Lesser General Public License as 7 published by the Free Software Foundation; either version 3 of the 8 License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 */ 15 16 using Math; 17 using Cairo; 18 19 namespace BirdFont { 20 21 /** Create new points. */ 22 public class PenTool : Tool { 23 24 private static double contact_surface { 25 get { 26 return MainWindow.units * 20; 27 } 28 } 29 30 public static bool move_selected = false; 31 public static bool move_selected_handle = false; 32 public static bool move_point_on_path = false; 33 public static bool edit_active_corner = false; 34 public static bool move_point_independent_of_handle = false; 35 public static Gee.ArrayList<PointSelection> selected_points; 36 37 public static EditPointHandle active_handle; 38 public static EditPointHandle selected_handle; 39 public static PointSelection handle_selection; 40 41 public static EditPoint? active_edit_point; 42 public static Path active_path; 43 44 public static EditPoint selected_point; 45 public static Path selected_path; 46 47 public static double last_point_x = 0; 48 public static double last_point_y = 0; 49 50 public static bool show_selection_box = false; 51 private static double selection_box_x = 0; 52 private static double selection_box_y = 0; 53 private static double selection_box_last_x = 0; 54 private static double selection_box_last_y = 0; 55 56 private static bool point_selection_image = false; 57 58 public static double precision = 1; 59 60 // The pixel where the user pressed the mouse button 61 public static int begin_action_x = 0; 62 public static int begin_action_y = 0; 63 64 /* First move action must move the current point in to the grid. */ 65 bool first_move_action = false; 66 67 /** Move curve handle instead of control point. */ 68 private bool last_selected_is_handle = false; 69 70 static Gee.ArrayList<Path> clockwise; 71 static Gee.ArrayList<Path> counter_clockwise; 72 73 public static double path_stroke_width = 0; 74 public static double simplification_threshold = 0.5; 75 76 public static bool retain_angle = false; 77 78 /** Move handle along axis. */ 79 public static bool on_axis = false; 80 81 public PenTool (string name) { 82 base (name, t_("Add new points")); 83 84 selected_points = new Gee.ArrayList<PointSelection> (); 85 86 active_handle = new EditPointHandle.empty (); 87 selected_handle = new EditPointHandle.empty (); 88 handle_selection = new PointSelection.empty (); 89 90 active_edit_point = new EditPoint (); 91 active_path = new Path (); 92 selected_path = new Path (); 93 94 selected_point = new EditPoint (); 95 clockwise = new Gee.ArrayList<Path> (); 96 counter_clockwise = new Gee.ArrayList<Path> (); 97 98 select_action.connect ((self) => { 99 }); 100 101 deselect_action.connect ((self) => { 102 }); 103 104 press_action.connect ((self, b, x, y) => { 105 // retain path direction 106 clockwise = new Gee.ArrayList<Path> (); 107 counter_clockwise = new Gee.ArrayList<Path> (); 108 109 begin_action_x = x; 110 begin_action_y = y; 111 112 update_orientation (); 113 114 first_move_action = true; 115 116 last_point_x = Glyph.path_coordinate_x (x); 117 last_point_y = Glyph.path_coordinate_y (y); 118 119 move_action (this, x, y); 120 121 press (b, x, y, false); 122 123 if (BirdFont.android) { 124 point_selection_image = true; 125 } 126 127 selection_box_x = x; 128 selection_box_y = y; 129 130 last_point_x = Glyph.path_coordinate_x (x); 131 last_point_y = Glyph.path_coordinate_y (y); 132 133 BirdFont.get_current_font ().touch (); 134 135 // move new points on to grid 136 if (b == 1 && (GridTool.has_ttf_grid () || GridTool.is_visible ())) { 137 move (x, y); 138 } 139 140 reset_stroke (); 141 }); 142 143 double_click_action.connect ((self, b, x, y) => { 144 last_point_x = Glyph.path_coordinate_x (x); 145 last_point_y = Glyph.path_coordinate_y (y); 146 147 press (b, (int) x, (int) y, true); 148 }); 149 150 release_action.connect ((self, b, ix, iy) => { 151 double x, y; 152 Glyph g; 153 154 g = MainWindow.get_current_glyph (); 155 x = Glyph.path_coordinate_x (ix); 156 y = Glyph.path_coordinate_y (iy); 157 158 if (has_join_icon () && active_edit_point != null) { 159 join_paths ((!) active_edit_point); 160 } 161 162 active_handle = new EditPointHandle.empty (); 163 164 if (show_selection_box) { 165 select_points_in_box (); 166 } 167 168 move_selected = false; 169 move_selected_handle = false; 170 edit_active_corner = false; 171 show_selection_box = false; 172 173 set_orientation (); 174 175 MainWindow.set_cursor (NativeWindow.VISIBLE); 176 177 point_selection_image = false; 178 BirdFont.get_current_font ().touch (); 179 180 foreach (Path p in g.get_visible_paths ()) { 181 if (p.is_open () && p.points.size > 0) { 182 p.get_first_point ().set_tie_handle (false); 183 p.get_first_point ().set_reflective_handles (false); 184 p.get_last_point ().set_tie_handle (false); 185 p.get_last_point ().set_reflective_handles (false); 186 } 187 188 // cache good stroke 189 if (p.full_stroke == null) { 190 p.create_full_stroke (); 191 } 192 } 193 194 // select or deselect points 195 if (KeyBindings.has_shift ()) { 196 control_point_event (x, y, false); 197 if (active_edit_point != null) { 198 if (((!)active_edit_point).is_selected () && selected_points.size > 1) { 199 ((!)active_edit_point).set_selected (false); 200 } else { 201 ((!)active_edit_point).set_selected (true); 202 } 203 update_selection (); 204 } 205 } 206 207 update_boundaries_for_selected_paths (); 208 on_axis = false; 209 }); 210 211 move_action.connect ((self, x, y) => { 212 selection_box_last_x = x; 213 selection_box_last_y = y; 214 215 if (Path.distance (begin_action_x, x, begin_action_y, y) > 10 * MainWindow.units) { 216 point_selection_image = false; 217 } 218 219 move (x, y); 220 }); 221 222 key_press_action.connect ((self, keyval) => { 223 reset_stroke (); 224 225 if (keyval == Key.DEL || keyval == Key.BACK_SPACE) { 226 if (KeyBindings.has_shift ()) { 227 delete_selected_points (); 228 } else { 229 delete_simplify (); 230 } 231 } 232 233 if (is_arrow_key (keyval)) { 234 if (KeyBindings.modifier != CTRL) { 235 move_selected_points (keyval); 236 active_edit_point = selected_point; 237 } else { 238 move_select_next_point (keyval); 239 } 240 } 241 242 if (KeyBindings.has_shift ()) { 243 if (selected_point.tie_handles && KeyBindings.modifier == SHIFT) { 244 last_point_x = selected_point.x; 245 last_point_y = selected_point.y; 246 } 247 } 248 249 GlyphCanvas.redraw (); 250 BirdFont.get_current_font ().touch (); 251 }); 252 253 key_release_action.connect ((self, keyval) => { 254 double x, y; 255 if (is_arrow_key (keyval)) { 256 if (KeyBindings.modifier != CTRL && active_edit_point != null) { 257 x = Glyph.reverse_path_coordinate_x (selected_point.x); 258 y = Glyph.reverse_path_coordinate_y (selected_point.y); 259 join_paths ((!) active_edit_point); 260 reset_stroke (); 261 } 262 } 263 }); 264 265 draw_action.connect ((tool, cairo_context, glyph) => { 266 draw_on_canvas (cairo_context, glyph); 267 }); 268 } 269 270 public static void update_orientation () { 271 Glyph glyph = MainWindow.get_current_glyph (); 272 273 clockwise.clear (); 274 counter_clockwise.clear (); 275 foreach (Path p in glyph.get_visible_paths ()) { 276 p.direction_is_set = true; 277 278 if (p.is_clockwise ()) { 279 clockwise.add (p); 280 } else { 281 counter_clockwise.add (p); 282 } 283 } 284 } 285 286 public static void set_orientation () { 287 // update path direction if it has changed 288 foreach (Path p in clockwise) { 289 if (!p.is_open () && !p.is_clockwise ()) { 290 p.reverse (); 291 update_selection (); 292 } 293 } 294 295 foreach (Path p in counter_clockwise) { 296 if (!p.is_open () && p.is_clockwise ()) { 297 p.reverse (); 298 update_selection (); 299 } 300 } 301 } 302 303 public bool has_join_icon () { 304 if (active_edit_point != null) { 305 return can_join ((!) active_edit_point); 306 } 307 308 return false; 309 } 310 311 public static bool can_join (EditPoint ep) { 312 double mx, my; 313 get_tie_position (ep, out mx, out my); 314 return (mx > -10 * MainWindow.units && my > -10 * MainWindow.units); 315 } 316 317 public static void select_points_in_box () { 318 double x1, y1, x2, y2; 319 Glyph g; 320 321 g = MainWindow.get_current_glyph (); 322 323 x1 = Glyph.path_coordinate_x (fmin (selection_box_x, selection_box_last_x)); 324 y1 = Glyph.path_coordinate_y (fmin (selection_box_y, selection_box_last_y)); 325 x2 = Glyph.path_coordinate_x (fmax (selection_box_x, selection_box_last_x)); 326 y2 = Glyph.path_coordinate_y (fmax (selection_box_y, selection_box_last_y)); 327 328 if (!KeyBindings.has_shift ()) { 329 remove_all_selected_points (); 330 } 331 332 foreach (Path p in g.get_paths_in_current_layer ()) { 333 // TODO: Select path only of bounding box is in selection box 334 foreach (EditPoint ep in p.points) { 335 if (x1 <= ep.x <= x2 && y2 <= ep.y <= y1) { 336 add_selected_point (ep, p); 337 ep.set_selected (true); 338 } 339 } 340 } 341 } 342 343 public static void delete_selected_points () { 344 Glyph g = MainWindow.get_current_glyph (); 345 346 foreach (PointSelection p in selected_points) { 347 p.point.deleted = true; 348 } 349 350 process_deleted (); 351 352 foreach (Path p in g.get_all_paths ()) { 353 if (p.has_deleted_point ()) { 354 process_deleted (); 355 } 356 } 357 358 g.update_view (); 359 360 selected_points.clear (); 361 selected_handle.selected = false; 362 363 active_handle = new EditPointHandle.empty (); 364 selected_handle = new EditPointHandle.empty (); 365 366 active_edit_point = null; 367 selected_point = new EditPoint (); 368 } 369 370 static void get_closes_point_in_segment (EditPoint ep0, EditPoint ep1, EditPoint ep2, 371 double px, double py, 372 out double nx, out double ny) { 373 double npx0, npy0; 374 double npx1, npy1; 375 376 Path.find_closes_point_in_segment (ep0, ep1, px, py, out npx0, out npy0, 50); 377 Path.find_closes_point_in_segment (ep1, ep2, px, py, out npx1, out npy1, 50); 378 379 if (Path.distance (px, npx0, py, npy0) < Path.distance (px, npx1, py, npy1)) { 380 nx = npx0; 381 ny = npy0; 382 } else { 383 nx = npx1; 384 ny = npy1; 385 } 386 } 387 388 public static void get_path_distortion (EditPoint oe0, EditPoint oe1, EditPoint oe2, 389 EditPoint ep1, EditPoint ep2, 390 out double distortion_first, out double distortion_next) { 391 double nx, ny; 392 double df, dn; 393 int step; 394 395 df = 0; 396 dn = 0; 397 nx = 0; 398 ny = 0; 399 400 step = 4; 401 402 Path.all_of (ep1, ep2, (xa, ya, ta) => { 403 double f, n; 404 405 get_closes_point_in_segment (oe0, oe1, oe2, xa, ya, out nx, out ny); 406 407 if (ta < 0.5) { 408 f = Path.distance (nx, xa, ny, ya); 409 if (f > df) { 410 df += f; 411 } 412 } else { 413 n = Path.distance (nx, xa, ny, ya); 414 if (n > dn) { 415 dn += n; 416 } 417 } 418 419 return true; 420 }, step); 421 422 distortion_first = df; 423 distortion_next = dn; 424 } 425 426 public static void update_boundaries_for_selected_paths () { 427 Gee.ArrayList<Path> paths = new Gee.ArrayList<Path> (); 428 foreach (PointSelection p in selected_points) { 429 if (paths.index_of (p.path) == -1) { 430 paths.add (p.path); 431 } 432 } 433 434 foreach (Path p in paths) { 435 p.update_region_boundaries (); 436 } 437 } 438 439 public static void delete_simplify () { 440 Glyph g = MainWindow.get_current_glyph (); 441 442 foreach (PointSelection p in selected_points) { 443 remove_point_simplify (p); 444 p.path.reset_stroke (); 445 } 446 447 foreach (PointSelection p in selected_points) { 448 p.path.create_full_stroke (); 449 } 450 451 g.update_view (); 452 453 selected_points.clear (); 454 selected_handle.selected = false; 455 456 active_handle = new EditPointHandle.empty (); 457 selected_handle = new EditPointHandle.empty (); 458 459 active_edit_point = null; 460 selected_point = new EditPoint (); 461 } 462 463 /** @return path distortion. */ 464 public static double remove_point_simplify (PointSelection p, double tolerance = 0.6) { 465 double e; 466 467 e = remove_point_simplify_path (p, tolerance, Glyph.CANVAS_MAX); 468 p.path.update_region_boundaries (); 469 470 return e; 471 } 472 473 /** @return path distortion. */ 474 public static double remove_point_simplify_path (PointSelection p, 475 double tolerance = 0.6, double keep_tolerance = 10000) { 476 477 double start_length, stop_length; 478 double start_distortion, start_min_distortion, start_previous_length; 479 double stop_distortion, stop_min_distortion, stop_previous_length; 480 double distortion, min_distortion; 481 double prev_length_adjustment, next_length_adjustment; 482 double prev_length_adjustment_reverse, next_length_adjustment_reverse; 483 EditPoint ep1, ep2; 484 EditPoint next, prev; 485 double step, distance; 486 487 return_val_if_fail (p.path.points.size > 0, 0); 488 489 if (p.path.points.size <= 2) { 490 p.point.deleted = true; 491 p.path.remove_deleted_points (); 492 return 0; 493 } 494 495 p.point.deleted = true; 496 497 if (p.point.next != null) { 498 next = p.point.get_next (); 499 } else { 500 next = p.path.points.get (0); 501 } 502 503 if (p.point.prev != null) { 504 prev = p.point.get_prev (); 505 } else { 506 prev = p.path.points.get (p.path.points.size - 1); 507 } 508 509 prev.get_right_handle ().convert_to_curve (); 510 next.get_left_handle ().convert_to_curve (); 511 512 if (prev.get_right_handle ().type == PointType.QUADRATIC 513 && next.get_left_handle ().type != PointType.QUADRATIC) { 514 convert_point_type (prev, next.get_left_handle ().type); 515 } 516 517 if (prev.get_right_handle ().type != PointType.QUADRATIC 518 && next.get_left_handle ().type == PointType.QUADRATIC) { 519 convert_point_type (next, prev.get_right_handle ().type); 520 } 521 522 ep1 = prev.copy (); 523 ep2 = next.copy (); 524 525 start_length = ep1.get_right_handle ().length; 526 stop_length = ep2.get_left_handle ().length; 527 528 stop_previous_length = start_length; 529 start_previous_length = stop_length; 530 531 stop_min_distortion = double.MAX; 532 ep1.get_right_handle ().length = start_length; 533 534 start_min_distortion = double.MAX; 535 ep2.get_left_handle ().length = stop_length; 536 537 prev_length_adjustment = 0; 538 next_length_adjustment = 0; 539 prev_length_adjustment_reverse = 0; 540 next_length_adjustment_reverse = 0; 541 542 min_distortion = double.MAX; 543 distance = Path.distance (ep1.x, ep2.x, ep1.y, ep2.y); 544 545 for (double m = 50.0; m >= tolerance / 2.0; m /= 10.0) { 546 step = m / 10.0; 547 min_distortion = double.MAX; 548 549 double first = (m == 50.0) ? 0 : -m; 550 551 for (double a = first; a < m; a += step) { 552 for (double b = first; b < m; b += step) { 553 554 if (start_length + a + stop_length + b > distance) { 555 break; 556 } 557 558 ep1.get_right_handle ().length = start_length + a; 559 ep2.get_left_handle ().length = stop_length + b; 560 561 get_path_distortion (prev, p.point, next, 562 ep1, ep2, 563 out start_distortion, out stop_distortion); 564 565 distortion = Math.fmax (start_distortion, stop_distortion); 566 567 if (distortion < min_distortion 568 && start_length + a > 0 569 && stop_length + b > 0) { 570 min_distortion = distortion; 571 prev_length_adjustment_reverse = a; 572 next_length_adjustment = b; 573 } 574 } 575 } 576 577 start_length += prev_length_adjustment_reverse; 578 stop_length += next_length_adjustment; 579 } 580 581 582 if (min_distortion < keep_tolerance || keep_tolerance >= Glyph.CANVAS_MAX) { 583 prev.get_right_handle ().length = start_length; 584 585 if (prev.get_right_handle ().type != PointType.QUADRATIC) { 586 next.get_left_handle ().length = stop_length; 587 } else { 588 next.get_left_handle ().move_to_coordinate ( 589 prev.get_right_handle ().x, prev.get_right_handle ().y); 590 } 591 592 p.point.deleted = true; 593 p.path.remove_deleted_points (); 594 } 595 596 return min_distortion; 597 } 598 599 /** @return path distortion. */ 600 public static double remove_point_simplify_path_fast (PointSelection p, 601 double tolerance = 0.6, double keep_tolerance = 10000) { 602 603 double start_length, stop_length; 604 double start_distortion, start_min_distortion, start_previous_length; 605 double stop_distortion, stop_min_distortion, stop_previous_length; 606 double distortion, min_distortion; 607 double prev_length_adjustment, next_length_adjustment; 608 double prev_length_adjustment_reverse, next_length_adjustment_reverse; 609 EditPoint ep1, ep2; 610 EditPoint next, prev; 611 double step, distance; 612 613 return_val_if_fail (p.path.points.size > 0, 0); 614 615 if (p.path.points.size <= 2) { 616 p.point.deleted = true; 617 p.path.remove_deleted_points (); 618 return 0; 619 } 620 621 p.point.deleted = true; 622 623 if (p.point.next != null) { 624 next = p.point.get_next (); 625 } else { 626 next = p.path.points.get (0); 627 } 628 629 if (p.point.prev != null) { 630 prev = p.point.get_prev (); 631 } else { 632 prev = p.path.points.get (p.path.points.size - 1); 633 } 634 635 prev.get_right_handle ().convert_to_curve (); 636 next.get_left_handle ().convert_to_curve (); 637 638 if (prev.get_right_handle ().type == PointType.QUADRATIC 639 && next.get_left_handle ().type != PointType.QUADRATIC) { 640 convert_point_type (prev, next.get_left_handle ().type); 641 } 642 643 if (prev.get_right_handle ().type != PointType.QUADRATIC 644 && next.get_left_handle ().type == PointType.QUADRATIC) { 645 convert_point_type (next, prev.get_right_handle ().type); 646 } 647 648 ep1 = prev.copy (); 649 ep2 = next.copy (); 650 651 start_length = ep1.get_right_handle ().length; 652 stop_length = ep2.get_left_handle ().length; 653 654 stop_previous_length = start_length; 655 start_previous_length = stop_length; 656 657 stop_min_distortion = double.MAX; 658 ep1.get_right_handle ().length = start_length; 659 660 start_min_distortion = double.MAX; 661 ep2.get_left_handle ().length = stop_length; 662 663 prev_length_adjustment = 0; 664 next_length_adjustment = 0; 665 prev_length_adjustment_reverse = 0; 666 next_length_adjustment_reverse = 0; 667 668 min_distortion = double.MAX; 669 distance = Path.distance (ep1.x, ep2.x, ep1.y, ep2.y); 670 671 for (double m = 50.0; m >= tolerance / 2.0; m /= 10.0) { 672 step = m / 10.0; 673 min_distortion = double.MAX; 674 675 double first = (m == 50.0) ? 0 : -m; 676 677 for (double a = first; a < m; a += step) { 678 for (double b = first; b < m; b += step) { 679 680 if (start_length + a + stop_length + b > distance) { 681 break; 682 } 683 684 ep1.get_right_handle ().length = start_length + a; 685 ep2.get_left_handle ().length = stop_length + b; 686 687 get_path_distortion (prev, p.point, next, 688 ep1, ep2, 689 out start_distortion, out stop_distortion); 690 691 distortion = Math.fmax (start_distortion, stop_distortion); 692 693 if (distortion < min_distortion 694 && start_length + a > 0 695 && stop_length + b > 0) { 696 min_distortion = distortion; 697 prev_length_adjustment_reverse = a; 698 next_length_adjustment = b; 699 } 700 } 701 } 702 703 start_length += prev_length_adjustment_reverse; 704 stop_length += next_length_adjustment; 705 } 706 707 708 if (min_distortion < keep_tolerance || keep_tolerance >= Glyph.CANVAS_MAX) { 709 prev.get_right_handle ().length = start_length; 710 711 if (prev.get_right_handle ().type != PointType.QUADRATIC) { 712 next.get_left_handle ().length = stop_length; 713 } else { 714 next.get_left_handle ().move_to_coordinate ( 715 prev.get_right_handle ().x, prev.get_right_handle ().y); 716 } 717 718 p.point.deleted = true; 719 p.path.remove_deleted_points (); 720 } 721 722 return min_distortion; 723 } 724 725 /** Retain selected points even if path is copied after running reverse. */ 726 public static void update_selection () { 727 Glyph g = MainWindow.get_current_glyph (); 728 729 selected_points.clear (); 730 731 foreach (Path p in g.get_visible_paths ()) { 732 foreach (EditPoint e in p.points) { 733 if (e.is_selected ()) { 734 selected_points.add (new PointSelection (e, p)); 735 } 736 } 737 } 738 } 739 740 static void process_deleted () { 741 Glyph g = MainWindow.get_current_glyph (); 742 while (g.process_deleted ()); 743 } 744 745 public static void close_all_paths () { 746 Glyph g = MainWindow.get_current_glyph (); 747 foreach (Path p in g.get_visible_paths ()) { 748 if (p.stroke == 0) { 749 p.close (); 750 } 751 } 752 g.close_path (); 753 GlyphCanvas.redraw (); 754 } 755 756 public void set_precision (double p) { 757 precision = p; 758 SettingsDisplay.precision.set_value_round (p, false, false); 759 } 760 761 public static void reset_stroke () { 762 Glyph g = MainWindow.get_current_glyph (); 763 764 foreach (PointSelection p in selected_points) { 765 p.path.reset_stroke (); 766 } 767 768 foreach (Object path in g.active_paths) { 769 if (path is PathObject) { 770 PathObject p = (PathObject) path; 771 p.get_path ().reset_stroke (); 772 } 773 } 774 } 775 776 public void move (int x, int y) { 777 double coordinate_x, coordinate_y; 778 double delta_coordinate_x, delta_coordinate_y; 779 double angle = 0; 780 bool tied; 781 Glyph g; 782 783 g = MainWindow.get_current_glyph (); 784 785 if (move_selected || move_selected_handle) { 786 cancel_stroke_creator (); 787 } 788 789 control_point_event (x, y); 790 curve_active_corner_event (x, y); 791 set_default_handle_positions (); 792 793 if (move_selected_handle && move_selected) { 794 warning ("move_selected_handle && move_selected"); 795 move_selected = false; 796 move_selected_handle = false; 797 } 798 799 if (move_selected_handle || move_selected) { 800 MainWindow.set_cursor (NativeWindow.HIDDEN); 801 reset_stroke (); 802 } else { 803 MainWindow.set_cursor (NativeWindow.VISIBLE); 804 } 805 806 // move control point handles 807 if (move_selected_handle) { 808 set_type_for_moving_handle (); 809 810 // don't update angle if the user is holding down shift 811 if (KeyBindings.modifier == SHIFT || PenTool.retain_angle) { 812 angle = selected_handle.angle; 813 } 814 815 if (GridTool.is_visible ()) { 816 coordinate_x = Glyph.path_coordinate_x (x); 817 coordinate_y = Glyph.path_coordinate_y (y); 818 GridTool.tie_coordinate (ref coordinate_x, ref coordinate_y); 819 delta_coordinate_x = coordinate_x - last_point_x; 820 delta_coordinate_y = coordinate_y - last_point_y; 821 selected_handle.move_delta_coordinate (delta_coordinate_x, delta_coordinate_y); 822 } else if (GridTool.has_ttf_grid ()) { 823 coordinate_x = Glyph.path_coordinate_x (x); 824 coordinate_y = Glyph.path_coordinate_y (y); 825 GridTool.ttf_grid_coordinate (ref coordinate_x, ref coordinate_y); 826 delta_coordinate_x = coordinate_x - last_point_x; 827 delta_coordinate_y = coordinate_y - last_point_y; 828 selected_handle.move_delta_coordinate (delta_coordinate_x, delta_coordinate_y); 829 } else { 830 coordinate_x = Glyph.path_coordinate_x (x); 831 coordinate_y = Glyph.path_coordinate_y (y); 832 833 selected_handle.move_to_coordinate (coordinate_x, coordinate_y); 834 835 if (on_axis) { 836 double tied_x = 0; 837 double tied_y = 0; 838 839 PointTool.tie_angle (selected_handle.parent.x, selected_handle.parent.y, 840 coordinate_x, coordinate_y, out tied_x, out tied_y); 841 842 selected_handle.move_to_coordinate (tied_x, tied_y); 843 } 844 } 845 846 if (KeyBindings.modifier == SHIFT || PenTool.retain_angle) { 847 selected_handle.angle = angle; 848 selected_handle.process_connected_handle (); 849 850 if (selected_handle.parent.tie_handles) { 851 if (selected_handle.is_left_handle ()) { 852 selected_handle.parent.get_right_handle ().angle = angle - PI; 853 } else { 854 selected_handle.parent.get_left_handle ().angle = angle + PI; 855 } 856 } 857 } 858 859 handle_selection.path.update_region_boundaries (); 860 861 // FIXME: redraw line only 862 GlyphCanvas.redraw (); 863 864 if (GridTool.is_visible ()) { 865 last_point_x = selected_handle.x; 866 last_point_y = selected_handle.y; 867 } else if (GridTool.has_ttf_grid ()) { 868 last_point_x = selected_handle.x; 869 last_point_y = selected_handle.y; 870 } else { 871 last_point_x = Glyph.path_coordinate_x (x); 872 last_point_y = Glyph.path_coordinate_y (y); 873 } 874 } 875 876 // move edit point 877 if (move_selected && !show_selection_box) { 878 879 if (GridTool.is_visible ()) { 880 coordinate_x = Glyph.path_coordinate_x (x); 881 coordinate_y = Glyph.path_coordinate_y (y); 882 883 if (selected_point.tie_handles && KeyBindings.modifier == SHIFT) { 884 if (first_move_action) { 885 last_point_x = selected_point.x; 886 last_point_y = selected_point.y; 887 } 888 889 move_point_on_handles (coordinate_x, coordinate_y, out coordinate_x, out coordinate_y); 890 } else { 891 GridTool.tie_coordinate (ref coordinate_x, ref coordinate_y); 892 } 893 894 delta_coordinate_x = coordinate_x - last_point_x; 895 delta_coordinate_y = coordinate_y - last_point_y; 896 897 foreach (PointSelection selected in selected_points) { 898 if (move_point_independent_of_handle || KeyBindings.modifier == SHIFT) { 899 selected.point.set_independet_position (selected.point.x + delta_coordinate_x, 900 selected.point.y + delta_coordinate_y); 901 } else { 902 selected.point.set_position (selected.point.x + delta_coordinate_x, 903 selected.point.y + delta_coordinate_y); 904 } 905 906 selected.path.reset_stroke (); 907 selected.path.recalculate_linear_handles_for_point(selected.point); 908 selected.path.update_region_boundaries (); 909 } 910 } else if (GridTool.has_ttf_grid ()) { 911 coordinate_x = Glyph.path_coordinate_x (x); 912 coordinate_y = Glyph.path_coordinate_y (y); 913 914 GridTool.ttf_grid_coordinate (ref coordinate_x, ref coordinate_y); 915 916 if (selected_point.tie_handles && KeyBindings.modifier == SHIFT) { 917 if (first_move_action) { 918 last_point_x = selected_point.x; 919 last_point_y = selected_point.y; 920 } 921 922 move_point_on_handles (coordinate_x, coordinate_y, out coordinate_x, out coordinate_y); 923 } 924 925 delta_coordinate_x = coordinate_x - last_point_x; 926 delta_coordinate_y = coordinate_y - last_point_y; 927 928 foreach (PointSelection selected in selected_points) { 929 if (move_point_independent_of_handle || KeyBindings.modifier == SHIFT) { 930 tied = selected.point.tie_handles; 931 selected.point.tie_handles = false; 932 selected.point.set_independet_position (selected.point.x + delta_coordinate_x, 933 selected.point.y + delta_coordinate_y); 934 selected.point.tie_handles = tied; 935 } else { 936 selected.point.set_position (selected.point.x + delta_coordinate_x, 937 selected.point.y + delta_coordinate_y); 938 } 939 940 selected.path.reset_stroke (); 941 selected.path.recalculate_linear_handles_for_point(selected.point); 942 selected.path.update_region_boundaries (); 943 } 944 } else { 945 coordinate_x = Glyph.path_coordinate_x (x); 946 coordinate_y = Glyph.path_coordinate_y (y); 947 948 if (selected_point.tie_handles && KeyBindings.modifier == SHIFT) { 949 if (first_move_action) { 950 last_point_x = selected_point.x; 951 last_point_y = selected_point.y; 952 } 953 954 move_point_on_handles (coordinate_x, coordinate_y, out coordinate_x, out coordinate_y); 955 delta_coordinate_x = coordinate_x - last_point_x; 956 delta_coordinate_y = coordinate_y - last_point_y; 957 } else { 958 delta_coordinate_x = coordinate_x - last_point_x; 959 delta_coordinate_y = coordinate_y - last_point_y; 960 } 961 962 foreach (PointSelection selected in selected_points) { 963 964 if (move_point_independent_of_handle || KeyBindings.modifier == SHIFT) { 965 selected.point.set_independet_position (selected.point.x + delta_coordinate_x, 966 selected.point.y + delta_coordinate_y); 967 } else { 968 selected.point.set_position (selected.point.x + delta_coordinate_x, 969 selected.point.y + delta_coordinate_y); 970 } 971 972 selected.path.recalculate_linear_handles_for_point(selected.point); 973 selected.path.reset_stroke (); 974 selected.path.update_region_boundaries (); 975 } 976 } 977 978 if (selected_point.tie_handles && KeyBindings.modifier == SHIFT) { 979 last_point_x = selected_point.x; 980 last_point_y = selected_point.y; 981 } else if (GridTool.is_visible ()) { 982 last_point_x = selected_point.x; 983 last_point_y = selected_point.y; 984 } else if (GridTool.has_ttf_grid ()) { 985 last_point_x = selected_point.x; 986 last_point_y = selected_point.y; 987 } else { 988 last_point_x = Glyph.path_coordinate_x (x); 989 last_point_y = Glyph.path_coordinate_y (y); 990 } 991 992 GlyphCanvas.redraw (); 993 } 994 995 if (show_selection_box) { 996 GlyphCanvas.redraw (); 997 } 998 } 999 1000 static void cancel_stroke_creator () { 1001 foreach (PointSelection p in selected_points) { 1002 p.path.stop_stroke_creator (); 1003 } 1004 } 1005 1006 private void move_point_on_handles (double px, double py, out double cx, out double cy) { 1007 EditPoint ep; 1008 ep = selected_point.copy (); 1009 ep.tie_handles = false; 1010 ep.reflective_point = false; 1011 ep.get_right_handle ().angle += PI / 2; 1012 ep.x = px; 1013 ep.y = py; 1014 1015 Path.find_intersection_handle (ep.get_right_handle (), selected_point.get_right_handle (), out cx, out cy); 1016 } 1017 1018 public void press (int button, int x, int y, bool double_click) { 1019 Glyph? g = MainWindow.get_current_glyph (); 1020 Glyph glyph = (!) g; 1021 bool reflective; 1022 1023 return_if_fail (g != null); 1024 1025 if ((double_click && !BirdFont.android) 1026 || Toolbox.drawing_tools.insert_point_on_path_tool.is_selected ()) { 1027 glyph.insert_new_point_on_path (x, y); 1028 return; 1029 } 1030 1031 if (button == 1) { 1032 add_point_event (x, y); 1033 return; 1034 } 1035 1036 if (button == 2) { 1037 if (glyph.is_open ()) { 1038 force_direction (); 1039 glyph.close_path (); 1040 } else { 1041 glyph.open_path (); 1042 } 1043 1044 return; 1045 } 1046 1047 if (button == 3 && !KeyBindings.has_shift () ) { 1048 selected_path = active_path; 1049 move_point_event (x, y); 1050 1051 // alt+click on a handle ends the symmetrical editing 1052 if ((KeyBindings.has_alt () || KeyBindings.has_ctrl ()) 1053 && is_over_handle (x, y)) { 1054 1055 // don't use set point to reflective to on open ends 1056 reflective = true; 1057 foreach (SvgBird.Object path in MainWindow.get_current_glyph ().active_paths) { 1058 if (!path.is_empty () && path is PathObject) { 1059 Path p = ((PathObject) path).get_path (); 1060 1061 if (p.is_open ()) { 1062 if (selected_handle.parent == p.get_first_point () 1063 || selected_handle.parent == p.get_last_point ()) { 1064 reflective = false; 1065 } 1066 } 1067 } 1068 } 1069 1070 if (reflective) { 1071 selected_handle.parent.set_reflective_handles (false); 1072 selected_handle.parent.set_tie_handle (false); 1073 GlyphCanvas.redraw (); 1074 } 1075 } 1076 1077 return; 1078 } 1079 1080 if (button == 3) { 1081 selected_path = active_path; 1082 move_point_event (x, y); 1083 } 1084 1085 if (KeyBindings.has_shift () && !is_over_handle (x, y)) { 1086 show_selection_box = true; 1087 return; 1088 } 1089 } 1090 1091 public void add_point_event (int x, int y) { 1092 Glyph? g = MainWindow.get_current_glyph (); 1093 Glyph glyph = (!) g; 1094 PointSelection ps; 1095 1096 if (move_selected_handle) { 1097 warning ("moving handle"); 1098 return; 1099 } 1100 1101 return_if_fail (g != null); 1102 1103 remove_all_selected_points (); 1104 ps = new_point_action (x, y); 1105 active_path = ps.path; 1106 glyph.store_undo_state (); 1107 } 1108 1109 public void move_point_event (int x, int y) { 1110 Glyph? g = MainWindow.get_current_glyph (); 1111 Glyph glyph = (!) g; 1112 1113 return_if_fail (g != null); 1114 1115 control_point_event (x, y); 1116 curve_corner_event (x, y); 1117 1118 if (!move_selected_handle) { 1119 select_active_point (x, y); 1120 last_selected_is_handle = false; 1121 } 1122 1123 if (!KeyBindings.has_shift () 1124 && selected_points.size == 0 1125 && !active_handle.active) { 1126 show_selection_box = true; 1127 } 1128 1129 glyph.store_undo_state (); 1130 } 1131 1132 void set_type_for_moving_handle () { 1133 if (selected_handle.type == PointType.LINE_CUBIC) { 1134 selected_handle.set_point_type (PointType.CUBIC); 1135 } 1136 1137 if (selected_handle.type == PointType.LINE_QUADRATIC) { 1138 selected_handle.set_point_type (PointType.QUADRATIC); 1139 } 1140 1141 if (selected_handle.type == PointType.LINE_DOUBLE_CURVE) { 1142 selected_handle.set_point_type (PointType.DOUBLE_CURVE); 1143 } 1144 } 1145 1146 /** Set fill property to transparend for counter paths. */ 1147 public static void force_direction () { 1148 Glyph g = MainWindow.get_current_glyph (); 1149 1150 clear_directions (); 1151 1152 foreach (Path p in g.get_visible_paths ()) { 1153 if (!p.has_direction ()) { 1154 if (is_counter_path (p)) { 1155 p.force_direction (Direction.COUNTER_CLOCKWISE); 1156 } else { 1157 p.force_direction (Direction.CLOCKWISE); 1158 } 1159 } 1160 } 1161 1162 update_selected_points (); 1163 } 1164 1165 public static void clear_directions () { 1166 clockwise.clear (); 1167 counter_clockwise.clear (); 1168 } 1169 1170 public static bool is_counter_path (Path path) { 1171 Glyph g = MainWindow.get_current_glyph (); 1172 PathList pl = new PathList (); 1173 1174 foreach (Path p in g.get_visible_paths ()) { 1175 pl.add (p); 1176 } 1177 1178 return Path.is_counter (pl, path); 1179 } 1180 1181 public void remove_from_selected (EditPoint ep) 1182 requires (selected_points.size > 0) { 1183 1184 Gee.ArrayList<PointSelection> remove = new Gee.ArrayList<PointSelection> (); 1185 1186 foreach (PointSelection e in selected_points) { 1187 if (e.point.equals (e.point)) { 1188 remove.add (e); 1189 } 1190 } 1191 1192 foreach (PointSelection e in remove) { 1193 selected_points.remove (e); 1194 } 1195 } 1196 1197 public void select_active_point (double x, double y) { 1198 Glyph glyph = MainWindow.get_current_glyph (); 1199 bool reverse; 1200 1201 control_point_event (x, y); 1202 1203 // continue adding points from the other end of the selected path 1204 reverse = false; 1205 foreach (Path p in glyph.get_visible_paths ()) { 1206 if (p.is_open () && p.points.size >= 1 1207 && (active_edit_point == p.points.get (0) 1208 || active_edit_point == p.points.get (p.points.size - 1))) { 1209 update_selection (); 1210 reverse = true; 1211 control_point_event (x, y); 1212 break; 1213 } 1214 } 1215 1216 foreach (Path p in glyph.get_visible_paths ()) { 1217 if (p.is_open () && p.points.size > 1 && active_edit_point == p.points.get (0)) { 1218 p.reverse (); 1219 update_selection (); 1220 reverse = true; 1221 control_point_event (x, y); 1222 break; 1223 } 1224 } 1225 1226 if (active_edit_point == null && KeyBindings.modifier != SHIFT) { 1227 remove_all_selected_points (); 1228 } 1229 1230 move_selected = true; 1231 move_point_on_path = true; 1232 1233 if (active_edit_point != null) { 1234 glyph.clear_active_paths (); 1235 1236 PathObject path = new PathObject.for_path (active_path); 1237 glyph.add_active_object (path); 1238 1239 DrawingTools.update_stroke_settings (); 1240 1241 if (KeyBindings.modifier != SHIFT) { 1242 selected_point = (!)active_edit_point; 1243 1244 if (!((!)active_edit_point).is_selected ()) { 1245 remove_all_selected_points (); 1246 ((!)active_edit_point).set_selected (true); 1247 selected_point = (!)active_edit_point; 1248 add_selected_point (selected_point, active_path); // FIXME: double check active path 1249 last_selected_is_handle = false; 1250 } 1251 1252 // alt+click creates a point with symmetrical handles 1253 if (KeyBindings.has_alt () || KeyBindings.has_ctrl ()) { 1254 selected_point.set_reflective_handles (true); 1255 selected_point.get_right_handle ().process_symmetrical_handle (); 1256 GlyphCanvas.redraw (); 1257 } 1258 } 1259 } 1260 1261 if (reverse) { 1262 clockwise.clear (); 1263 counter_clockwise.clear (); 1264 } 1265 } 1266 1267 public static Path? find_path_to_join (EditPoint end_point) { 1268 Path? m = null; 1269 Glyph glyph = MainWindow.get_current_glyph (); 1270 EditPoint ep_last, ep_first; 1271 1272 foreach (Path path in glyph.get_visible_paths ()) { 1273 if (path.points.size == 0) { 1274 continue; 1275 } 1276 1277 ep_last = path.points.get (path.points.size - 1); 1278 ep_first = path.points.get (0); 1279 1280 if (end_point == ep_last) { 1281 m = path; 1282 break; 1283 } 1284 1285 if (end_point == ep_first) { 1286 m = path; 1287 break; 1288 } 1289 } 1290 1291 return m; 1292 } 1293 1294 public static Path merge_open_paths (Path path1, Path path2) { 1295 Path union, merge; 1296 EditPoint first_point; 1297 1298 union = path1.copy (); 1299 merge = path2.copy (); 1300 1301 return_val_if_fail (path1.points.size >= 1, merge); 1302 return_val_if_fail (path2.points.size >= 1, union); 1303 1304 merge.points.get (0).set_tie_handle (false); 1305 merge.points.get (0).set_reflective_handles (false); 1306 1307 merge.points.get (merge.points.size - 1).set_tie_handle (false); 1308 merge.points.get (merge.points.size - 1).set_reflective_handles (false); 1309 1310 union.points.get (union.points.size - 1).set_tie_handle (false); 1311 union.points.get (union.points.size - 1).set_reflective_handles (false); 1312 1313 union.points.get (0).set_tie_handle (false); 1314 union.points.get (0).set_reflective_handles (false); 1315 1316 first_point = merge.get_first_point (); 1317 1318 if (union.get_last_point ().get_left_handle ().is_curve ()) { 1319 first_point.get_left_handle ().convert_to_curve (); 1320 } else { 1321 if (first_point.type != PointType.QUADRATIC) { 1322 first_point.get_left_handle ().convert_to_line (); 1323 } 1324 } 1325 1326 if (first_point.type != PointType.QUADRATIC) { 1327 first_point.get_left_handle ().move_to_coordinate_internal ( 1328 union.get_last_point ().get_left_handle ().x, 1329 union.get_last_point ().get_left_handle ().y); 1330 } 1331 1332 union.delete_last_point (); 1333 1334 union.append_path (merge); 1335 1336 return union; 1337 } 1338 1339 public static void close_path (Path path) { 1340 bool last_segment_is_line; 1341 bool first_segment_is_line; 1342 1343 return_if_fail (path.points.size > 1); 1344 1345 last_segment_is_line = path.get_last_point ().get_left_handle ().is_line (); 1346 first_segment_is_line = path.get_first_point ().get_right_handle ().is_line (); 1347 1348 // TODO: set point type 1349 path.points.get (0).left_handle.move_to_coordinate ( 1350 path.points.get (path.points.size - 1).left_handle.x, 1351 path.points.get (path.points.size - 1).left_handle.y); 1352 1353 path.points.get (0).left_handle.type = 1354 path.points.get (path.points.size - 1).left_handle.type; 1355 1356 path.recalculate_linear_handles_for_point (path.points.get (0)); 1357 path.recalculate_linear_handles_for_point (path.points.get (path.points.size - 1)); 1358 1359 // force the connected handle to move 1360 path.points.get (0).set_position ( 1361 path.points.get (0).x, path.points.get (0).y); 1362 1363 path.points.remove_at (path.points.size - 1); 1364 1365 path.close (); 1366 1367 if (last_segment_is_line) { 1368 path.get_first_point ().get_left_handle ().convert_to_line (); 1369 path.recalculate_linear_handles_for_point (path.get_first_point ()); 1370 } 1371 1372 if (first_segment_is_line) { 1373 path.get_first_point ().get_right_handle ().convert_to_line (); 1374 path.recalculate_linear_handles_for_point (path.get_first_point ()); 1375 } 1376 } 1377 1378 /** @return the new path or null if no path could be merged with the end point. */ 1379 public static Path? join_paths (EditPoint end_point) { 1380 Glyph glyph = MainWindow.get_current_glyph (); 1381 Path? p; 1382 Path path; 1383 Path union; 1384 bool direction_changed = false; 1385 int px, py; 1386 1387 var paths = glyph.get_visible_paths (); 1388 1389 if (paths.size == 0) { 1390 warning ("No paths."); 1391 return null; 1392 } 1393 1394 p = find_path_to_join (end_point); 1395 if (p == null) { 1396 warning ("No path to join."); 1397 return null; 1398 } 1399 1400 path = (!) p; 1401 if (!path.is_open ()) { 1402 warning ("Path is closed."); 1403 return null; 1404 } 1405 1406 return_val_if_fail (path.points.size > 0, null); 1407 1408 px = Glyph.reverse_path_coordinate_x (end_point.x); 1409 py = Glyph.reverse_path_coordinate_y (end_point.y); 1410 1411 if (path.points.size == 1) { 1412 glyph.delete_path (path); 1413 glyph.clear_active_paths (); 1414 1415 foreach (Path merge in paths) { 1416 if (merge.points.size > 0) { 1417 PathObject merged_path = new PathObject.for_path (merge); 1418 1419 if (is_close_to_point (merge.points.get (merge.points.size - 1), px, py)) { 1420 glyph.add_active_object (merged_path); 1421 active_path = merge; 1422 merge.reopen (); 1423 glyph.open_path (); 1424 return merge; 1425 } 1426 1427 if (is_close_to_point (merge.points.get (0), px, py)) { 1428 glyph.add_active_object (merged_path); 1429 active_path = merge; 1430 clear_directions (); 1431 merge.reopen (); 1432 glyph.open_path (); 1433 merge.reverse (); 1434 return merge; 1435 } 1436 } 1437 } 1438 1439 warning ("No point to merge."); 1440 return null; 1441 } 1442 1443 if (active_edit_point == path.points.get (0)) { 1444 path.reverse (); 1445 update_selection (); 1446 path.recalculate_linear_handles (); 1447 direction_changed = true; 1448 active_edit_point = path.points.get (path.points.size - 1); 1449 active_path = path; 1450 } 1451 1452 if (path.points.get (0) == active_edit_point) { 1453 warning ("Wrong direction."); 1454 return null; 1455 } 1456 1457 // join path with it self 1458 if (is_close_to_point (path.points.get (0), px, py) && path.points.size > 2) { 1459 close_path (path); 1460 glyph.close_path (); 1461 force_direction (); 1462 1463 glyph.clear_active_paths (); 1464 1465 PathObject closed_path = new PathObject.for_path (path); 1466 glyph.add_active_object (closed_path); 1467 1468 if (direction_changed) { 1469 path.reverse (); 1470 update_selection (); 1471 } 1472 1473 remove_all_selected_points (); 1474 1475 return path; 1476 } 1477 1478 foreach (Path merge in paths) { 1479 // don't join path with itself here 1480 if (path == merge) { 1481 continue; 1482 } 1483 1484 // we need both start and end points 1485 if (merge.points.size <= 1 || path.points.size <= 1) { 1486 continue; 1487 } 1488 1489 if (is_close_to_point (merge.points.get (merge.points.size - 1), px, py)) { 1490 merge.reverse (); 1491 update_selection (); 1492 direction_changed = !direction_changed; 1493 } 1494 1495 return_val_if_fail (merge.points.size > 0, null); 1496 1497 if (is_close_to_point (merge.points.get (0), px, py)) { 1498 if (path.points.size == 1) { 1499 warning ("path.points.size == 1\n"); 1500 } else if (merge.points.size == 1) { 1501 warning ("merge.points.size == 1\n"); 1502 } else { 1503 union = merge_open_paths (path, merge); 1504 1505 PathObject union_path = new PathObject.for_path (union); 1506 glyph.add_path (union); 1507 glyph.delete_path (path); 1508 glyph.delete_path (merge); 1509 glyph.clear_active_paths (); 1510 glyph.add_active_object (union_path); 1511 1512 union.reopen (); 1513 union.create_list (); 1514 1515 force_direction (); 1516 1517 if (direction_changed) { 1518 path.reverse (); 1519 update_selection (); 1520 } 1521 1522 union.update_region_boundaries (); 1523 1524 return union; 1525 } 1526 } 1527 } 1528 1529 if (direction_changed) { 1530 path.reverse (); 1531 update_selection (); 1532 } 1533 1534 warning ("No paths merged."); 1535 return null; 1536 } 1537 1538 /** Merge paths if ends are close. */ 1539 public static bool is_close_to_point (EditPoint ep, double x, double y) { 1540 double px, py, distance; 1541 1542 px = Glyph.reverse_path_coordinate_x (ep.x); 1543 py = Glyph.reverse_path_coordinate_y (ep.y); 1544 1545 distance = sqrt (fabs (pow (px - x, 2)) + fabs (pow (py - y, 2))); 1546 1547 return (distance < 7 * MainWindow.units); 1548 } 1549 1550 /** Show the user that curves will be merged on release. */ 1551 public void draw_on_canvas (Context cr, Glyph glyph) { 1552 if (show_selection_box) { 1553 draw_selection_box (cr); 1554 } 1555 1556 if (point_selection_image) { 1557 draw_point_selection_circle (cr); 1558 } 1559 1560 draw_merge_icon (cr); 1561 } 1562 1563 /** Higlight the selected point on Android. */ 1564 void draw_point_selection_circle (Context cr) { 1565 PointSelection ps; 1566 1567 if (active_handle.active) { 1568 Path.draw_control_point (cr, Glyph.path_coordinate_x (begin_action_x), 1569 Glyph.path_coordinate_y (begin_action_y), Theme.get_color ("Active Handle")); 1570 } else if (selected_points.size > 0) { 1571 ps = selected_points.get (selected_points.size - 1); 1572 1573 if (ps.point.type == PointType.CUBIC) { 1574 Path.draw_control_point (cr, Glyph.path_coordinate_x (begin_action_x), 1575 Glyph.path_coordinate_y (begin_action_y), Theme.get_color ("Selected Cubic Control Point")); 1576 } else { 1577 Path.draw_control_point (cr, Glyph.path_coordinate_x (begin_action_x), 1578 Glyph.path_coordinate_y (begin_action_y), Theme.get_color ("Selected Quadratic Control Point")); 1579 } 1580 } 1581 } 1582 1583 void draw_selection_box (Context cr) { 1584 double x, y, w, h; 1585 1586 x = fmin (selection_box_x, selection_box_last_x); 1587 y = fmin (selection_box_y, selection_box_last_y); 1588 w = fmax (selection_box_x, selection_box_last_x) - x; 1589 h = fmax (selection_box_y, selection_box_last_y) - y; 1590 1591 cr.save (); 1592 Theme.color (cr, "Foreground 1"); 1593 cr.set_line_width (2); 1594 cr.rectangle (x, y, w, h); 1595 cr.stroke (); 1596 cr.restore (); 1597 } 1598 1599 public static void draw_join_icon (Context cr, double x, double y) { 1600 cr.save (); 1601 Theme.color (cr, "Merge"); 1602 cr.move_to (x, y); 1603 cr.arc (x, y, 15, 0, 2 * Math.PI); 1604 cr.close_path (); 1605 cr.fill (); 1606 cr.restore (); 1607 } 1608 1609 void draw_merge_icon (Context cr) { 1610 double x, y; 1611 if (active_edit_point != null) { 1612 get_tie_position ((!) active_edit_point, out x, out y); 1613 draw_join_icon (cr, x, y); 1614 } 1615 } 1616 1617 /** Obtain the position where to ends meet. */ 1618 static void get_tie_position (EditPoint current_point, out double x, out double y) { 1619 Glyph glyph; 1620 EditPoint active; 1621 double px, py; 1622 1623 x = -100; 1624 y = -100; 1625 1626 if (!is_endpoint (current_point)) { 1627 return; 1628 } 1629 1630 glyph = MainWindow.get_current_glyph (); 1631 active = current_point; 1632 1633 return_if_fail (!is_null (glyph)); 1634 1635 px = Glyph.reverse_path_coordinate_x (active.x); 1636 py = Glyph.reverse_path_coordinate_y (active.y); 1637 1638 foreach (Path path in glyph.get_visible_paths ()) { 1639 1640 if (!path.is_open ()) { 1641 continue; 1642 } 1643 1644 if (path.points.size == 0) { 1645 continue; 1646 } 1647 1648 foreach (EditPoint ep in path.points) { 1649 if (ep == active || !is_endpoint (ep)) { 1650 continue; 1651 } 1652 1653 if (is_close_to_point (ep, px, py)) { 1654 x = Glyph.reverse_path_coordinate_x (ep.x); 1655 y = Glyph.reverse_path_coordinate_y (ep.y); 1656 return; 1657 } 1658 } 1659 } 1660 } 1661 1662 public static bool is_endpoint (EditPoint ep) { 1663 EditPoint start; 1664 EditPoint end; 1665 Glyph glyph = MainWindow.get_current_glyph (); 1666 1667 foreach (Path path in glyph.get_visible_paths ()) { 1668 if (path.points.size < 1) { 1669 continue; 1670 } 1671 1672 start = path.points.get (0); 1673 end = path.points.get (path.points.size - 1); 1674 1675 if (ep == start || ep == end) { 1676 return true; 1677 } 1678 } 1679 1680 return false; 1681 } 1682 1683 public static void set_active_edit_point (EditPoint? e, Path path) { 1684 bool redraw; 1685 Glyph g = MainWindow.get_current_glyph (); 1686 1687 foreach (Path p in g.get_visible_paths ()) { 1688 foreach (var ep in p.points) { 1689 ep.set_active (false); 1690 } 1691 } 1692 1693 redraw = active_edit_point != e; 1694 active_edit_point = e; 1695 active_path = path; 1696 1697 if (e != null) { 1698 ((!)e).set_active (true); 1699 } 1700 1701 if (redraw) { 1702 GlyphCanvas.redraw (); 1703 } 1704 } 1705 1706 PointSelection? get_closest_point (double ex, double ey, out Path? path) { 1707 double x = Glyph.path_coordinate_x (ex); 1708 double y = Glyph.path_coordinate_y (ey); 1709 double d = double.MAX; 1710 double nd; 1711 PointSelection? ep = null; 1712 Glyph g = MainWindow.get_current_glyph (); 1713 1714 path = null; 1715 1716 foreach (Path current_path in g.get_paths_in_current_layer ()) { 1717 if (is_close_to_path (current_path, ex, ey)) { 1718 foreach (EditPoint e in current_path.points) { 1719 nd = e.get_distance (x, y); 1720 1721 if (nd < d) { 1722 d = nd; 1723 ep = new PointSelection (e, current_path); 1724 path = current_path; 1725 } 1726 } 1727 } 1728 } 1729 1730 return ep; 1731 } 1732 1733 public double get_distance_to_closest_edit_point (double event_x, double event_y) { 1734 Path? p; 1735 PointSelection e; 1736 PointSelection? ep = get_closest_point (event_x, event_y, out p); 1737 1738 double x = Glyph.path_coordinate_x (event_x); 1739 double y = Glyph.path_coordinate_y (event_y); 1740 1741 if (ep == null) { 1742 return double.MAX; 1743 } 1744 1745 e = (!) ep; 1746 1747 return e.point.get_distance (x, y); 1748 } 1749 1750 public void control_point_event (double event_x, double event_y, bool reset_active_point = true) { 1751 Path? p; 1752 PointSelection? ep = get_closest_point (event_x, event_y, out p); 1753 Glyph g = MainWindow.get_current_glyph (); 1754 double x = Glyph.path_coordinate_x (event_x); 1755 double y = Glyph.path_coordinate_y (event_y); 1756 double distance; 1757 PointSelection e; 1758 1759 if (reset_active_point) { 1760 set_active_edit_point (null, new Path ()); 1761 } 1762 1763 if (ep == null) { 1764 return; 1765 } 1766 1767 e = (!) ep; 1768 distance = e.point.get_distance (x, y) * g.view_zoom; 1769 1770 if (distance < contact_surface) { 1771 set_active_edit_point (e.point, e.path); 1772 } 1773 } 1774 1775 public PointSelection new_point_action (int x, int y) { 1776 Glyph glyph; 1777 PointSelection new_point; 1778 1779 glyph = MainWindow.get_current_glyph (); 1780 glyph.open_path (); 1781 1782 remove_all_selected_points (); 1783 1784 new_point = add_new_edit_point (x, y); 1785 new_point.point.set_selected (true); 1786 1787 selected_point = new_point.point; 1788 active_edit_point = new_point.point; 1789 1790 return_val_if_fail (glyph.active_paths.size > 0, new PointSelection.empty ()); 1791 SvgBird.Object object = glyph.active_paths.get (glyph.active_paths.size - 1); 1792 1793 if (object is PathObject) { 1794 Path path = ((PathObject) object).get_path (); 1795 1796 add_selected_point (selected_point, path); 1797 1798 active_path = new_point.path; 1799 glyph.clear_active_paths (); 1800 glyph.add_active_object (object); 1801 1802 move_selected = true; 1803 } 1804 1805 return new_point; 1806 } 1807 1808 public static PointSelection add_new_edit_point (int x, int y) { 1809 PointSelection new_point; 1810 1811 new_point = insert_edit_point (x, y); 1812 new_point.path.update_region_boundaries (); 1813 1814 if (new_point.path.is_open () && new_point.path.points.size > 0) { 1815 new_point.path.get_first_point ().set_reflective_handles (false); 1816 new_point.path.get_first_point ().set_tie_handle (false); 1817 new_point.path.get_last_point ().set_reflective_handles (false); 1818 new_point.path.get_last_point ().set_tie_handle (false); 1819 } 1820 1821 selected_point = new_point.point; 1822 active_edit_point = new_point.point; 1823 1824 set_point_type (selected_point); 1825 set_default_handle_positions (); 1826 1827 selected_points.clear (); 1828 add_selected_point (new_point.point, new_point.path); 1829 1830 return new_point; 1831 } 1832 1833 private static PointSelection insert_edit_point (double x, double y) { 1834 double xt, yt; 1835 Path np; 1836 EditPoint inserted; 1837 bool stroke = StrokeTool.add_stroke; 1838 Glyph g = MainWindow.get_current_glyph (); 1839 PathObject path; 1840 1841 if (!g.has_active_path_objects ()) { 1842 np = new Path (); 1843 g.add_path (np); 1844 np.stroke = stroke ? StrokeTool.stroke_width : 0; 1845 np.line_cap = StrokeTool.line_cap; 1846 1847 path = new PathObject.for_path (np); 1848 g.add_active_object (path); 1849 1850 active_path = np; 1851 selected_path = np; 1852 } 1853 1854 xt = Glyph.path_coordinate_x (x); 1855 yt = Glyph.path_coordinate_y (y); 1856 1857 if (selected_path.is_open ()) { 1858 np = PenTool.selected_path; 1859 np.add (xt, yt); 1860 } else { 1861 np = new Path (); 1862 np.stroke = stroke ? StrokeTool.stroke_width : 0; 1863 g.add_path (np); 1864 np.add (xt, yt); 1865 1866 if (DrawingTools.pen_tool.is_selected ()) { 1867 np.stroke = PenTool.path_stroke_width; 1868 } 1869 1870 path = new PathObject.for_path (np); 1871 g.add_active_object (path); 1872 1873 PenTool.active_path = np; 1874 PenTool.selected_path = np; 1875 } 1876 1877 g.clear_active_paths (); 1878 path = new PathObject.for_path (np); 1879 g.add_active_object (path); 1880 active_path = np; 1881 selected_path = np; 1882 1883 inserted = np.points.get (np.points.size - 1); 1884 1885 return new PointSelection (inserted, np); 1886 } 1887 1888 static void set_point_type (EditPoint p) { 1889 if (p.prev != null && p.get_prev ().right_handle.type == PointType.QUADRATIC) { 1890 p.left_handle.type = PointType.QUADRATIC; 1891 p.right_handle.type = PointType.LINE_QUADRATIC; 1892 p.type = PointType.QUADRATIC; 1893 } else if (DrawingTools.get_selected_point_type () == PointType.QUADRATIC) { 1894 p.left_handle.type = PointType.LINE_QUADRATIC; 1895 p.right_handle.type = PointType.LINE_QUADRATIC; 1896 p.type = PointType.LINE_QUADRATIC; 1897 } else if (DrawingTools.get_selected_point_type () == PointType.DOUBLE_CURVE) { 1898 p.left_handle.type = PointType.LINE_DOUBLE_CURVE; 1899 p.right_handle.type = PointType.LINE_DOUBLE_CURVE; 1900 p.type = PointType.DOUBLE_CURVE; 1901 } else { 1902 p.left_handle.type = PointType.LINE_CUBIC; 1903 p.right_handle.type = PointType.LINE_CUBIC; 1904 p.type = PointType.CUBIC; 1905 } 1906 } 1907 1908 public static void set_default_handle_positions () { 1909 Glyph g = MainWindow.get_current_glyph (); 1910 foreach (var p in g.get_visible_paths ()) { 1911 if (p.is_editable ()) { 1912 p.create_list (); 1913 set_default_handle_positions_on_path (p); 1914 } 1915 } 1916 } 1917 1918 static void set_default_handle_positions_on_path (Path path) { 1919 foreach (EditPoint e in path.points) { 1920 if (!e.tie_handles && !e.reflective_point) { 1921 path.recalculate_linear_handles_for_point (e); 1922 } 1923 } 1924 } 1925 1926 private bool is_over_handle (double event_x, double event_y) { 1927 Glyph g = MainWindow.get_current_glyph (); 1928 double distance_to_edit_point = g.view_zoom * get_distance_to_closest_edit_point (event_x, event_y); 1929 1930 if (!CanvasSettings.show_all_line_handles) { 1931 foreach (PointSelection selected_corner in selected_points) { 1932 if (is_close_to_handle (selected_corner.point, event_x, event_y, distance_to_edit_point)) { 1933 return true; 1934 } 1935 } 1936 } else { 1937 foreach (Path p in g.get_visible_paths ()) { 1938 foreach (EditPoint ep in p.points) { 1939 if (is_close_to_handle (ep, event_x, event_y, distance_to_edit_point)) { 1940 return true; 1941 } 1942 } 1943 } 1944 } 1945 1946 return false; 1947 } 1948 1949 bool is_close_to_path (Path p, double event_x, double event_y) { 1950 double c = contact_surface * Glyph.ivz (); 1951 double x = Glyph.path_coordinate_x (event_x); 1952 double y = Glyph.path_coordinate_y (event_y); 1953 1954 if (unlikely (!p.has_region_boundaries ())) { 1955 if (p.points.size > 0) { 1956 warning (@"No bounding box. $(p.points.size)"); 1957 p.update_region_boundaries (); 1958 } 1959 } 1960 1961 return p.xmin - c - 10 <= x <= p.xmax + c + 10 && p.ymin - c - 10 <= y <= p.ymax + c + 10 ; 1962 } 1963 1964 private bool is_close_to_handle (EditPoint selected_corner, double event_x, double event_y, double distance_to_edit_point) { 1965 double x = Glyph.path_coordinate_x (event_x); 1966 double y = Glyph.path_coordinate_y (event_y); 1967 Glyph g = MainWindow.get_current_glyph (); 1968 double d_point = distance_to_edit_point; 1969 double dl, dr; 1970 1971 dl = g.view_zoom * selected_corner.get_left_handle ().get_point ().get_distance (x, y); 1972 dr = g.view_zoom * selected_corner.get_right_handle ().get_point ().get_distance (x, y); 1973 1974 if (dl < contact_surface && dl < d_point) { 1975 return true; 1976 } 1977 1978 if (dr < contact_surface && dr < d_point) { 1979 return true; 1980 } 1981 1982 return false; 1983 } 1984 1985 PointSelection get_closest_handle (double event_x, double event_y) { 1986 EditPointHandle left, right; 1987 double x = Glyph.path_coordinate_x (event_x); 1988 double y = Glyph.path_coordinate_y (event_y); 1989 EditPointHandle eh = new EditPointHandle.empty(); 1990 Glyph g = MainWindow.get_current_glyph (); 1991 double d = double.MAX; 1992 double dn; 1993 Path path = new Path (); 1994 bool left_handle = false; 1995 EditPoint parent_point; 1996 EditPoint tied_point; 1997 1998 foreach (Path p in g.get_paths_in_current_layer ()) { 1999 foreach (EditPoint ep in p.points) { 2000 if (ep.is_selected () || CanvasSettings.show_all_line_handles) { 2001 left = ep.get_left_handle (); 2002 right = ep.get_right_handle (); 2003 2004 dn = left.get_point ().get_distance (x, y); 2005 2006 if (dn < d) { 2007 eh = left; 2008 d = dn; 2009 path = p; 2010 left_handle = true; 2011 } 2012 2013 dn = right.get_point ().get_distance (x, y); 2014 2015 if (dn < d) { 2016 eh = right; 2017 d = dn; 2018 path = p; 2019 left_handle = false; 2020 } 2021 } 2022 } 2023 } 2024 2025 // Make sure the selected handle belongs to the selected point if 2026 // the current segment is quadratic. 2027 if (eh.type == PointType.QUADRATIC) { 2028 parent_point = eh.get_parent (); 2029 2030 if (left_handle) { 2031 if (parent_point.prev != null) { 2032 tied_point = parent_point.get_prev (); 2033 if (tied_point.selected_point) { 2034 eh = tied_point.get_right_handle (); 2035 } 2036 } 2037 } else { 2038 if (parent_point.next != null) { 2039 tied_point = parent_point.get_next (); 2040 if (tied_point.selected_point) { 2041 eh = tied_point.get_left_handle (); 2042 } 2043 } 2044 } 2045 } 2046 2047 return new PointSelection.handle_selection (eh, path); 2048 } 2049 2050 private void curve_active_corner_event (double event_x, double event_y) { 2051 PointSelection eh; 2052 Glyph glyph; 2053 2054 glyph = MainWindow.get_current_glyph (); 2055 active_handle.active = false; 2056 2057 if (!is_over_handle (event_x, event_y)) { 2058 return; 2059 } 2060 2061 eh = get_closest_handle (event_x, event_y); 2062 eh.handle.active = true; 2063 2064 if (active_handle != eh.handle) { 2065 GlyphCanvas.redraw (); 2066 } 2067 2068 active_handle = eh.handle; 2069 } 2070 2071 private void curve_corner_event (double event_x, double event_y) { 2072 Glyph g = MainWindow.get_current_glyph (); 2073 g.open_path (); 2074 PointSelection p; 2075 2076 if (!is_over_handle (event_x, event_y)) { 2077 return; 2078 } 2079 2080 move_selected_handle = true; 2081 last_selected_is_handle = true; 2082 selected_handle.selected = false; 2083 p = get_closest_handle (event_x, event_y); 2084 selected_handle = p.handle; 2085 handle_selection = p; 2086 selected_handle.selected = true; 2087 2088 active_path = p.path; 2089 PathObject path = new PathObject.for_path (active_path); 2090 g.add_active_object (path); 2091 } 2092 2093 public static void add_selected_point (EditPoint p, Path path) { 2094 bool in_path = false; 2095 2096 foreach (EditPoint e in path.points) { 2097 if (e == p) { 2098 in_path = true; 2099 break; 2100 } 2101 } 2102 2103 if (!in_path) { 2104 warning ("Point is not in path."); 2105 } 2106 2107 foreach (PointSelection ep in selected_points) { 2108 if (p == ep.point) { 2109 return; 2110 } 2111 } 2112 2113 selected_points.add (new PointSelection (p, path)); 2114 } 2115 2116 public static void remove_all_selected_points () { 2117 Glyph g = MainWindow.get_current_glyph (); 2118 2119 foreach (PointSelection ep in selected_points) { 2120 ep.point.set_active (false); 2121 ep.point.set_selected (false); 2122 } 2123 2124 selected_points.clear (); 2125 2126 foreach (Path p in g.get_visible_paths ()) { 2127 foreach (EditPoint e in p.points) { 2128 e.set_active (false); 2129 e.set_selected (false); 2130 } 2131 } 2132 } 2133 2134 static void move_select_next_point (uint keyval) { 2135 PointSelection next = new PointSelection.empty (); 2136 2137 if (selected_points.size == 0) { 2138 return; 2139 } 2140 2141 cancel_stroke_creator (); 2142 2143 switch (keyval) { 2144 case Key.UP: 2145 next = get_next_point_up (); 2146 break; 2147 case Key.DOWN: 2148 next = get_next_point_down (); 2149 break; 2150 case Key.LEFT: 2151 next = get_next_point_left (); 2152 break; 2153 case Key.RIGHT: 2154 next = get_next_point_right (); 2155 break; 2156 default: 2157 break; 2158 } 2159 2160 set_selected_point (next.point, next.path); 2161 2162 GlyphCanvas.redraw (); 2163 } 2164 2165 private static PointSelection get_next_point (double angle) 2166 requires (selected_points.size != 0) { 2167 PointSelection e = selected_points.get (selected_points.size - 1); 2168 double right_angle = e.point.right_handle.angle; 2169 double left_angle = e.point.left_handle.angle; 2170 double min_right, min_left; 2171 double min; 2172 2173 return_val_if_fail (e.point.next != null, new EditPoint ()); 2174 return_val_if_fail (e.point.prev != null, new EditPoint ()); 2175 2176 // angle might be greater than 2 PI or less than 0 2177 min_right = double.MAX; 2178 min_left = double.MAX; 2179 for (double i = -2 * PI; i <= 2 * PI; i += 2 * PI) { 2180 min = fabs (right_angle - (angle + i)); 2181 if (min < min_right) { 2182 min_right = min; 2183 } 2184 2185 min = fabs (left_angle - (angle + i)); 2186 if (min < min_left) { 2187 min_left = min; 2188 } 2189 } 2190 2191 if (min_right < min_left) { 2192 return new PointSelection (e.point.get_next (), e.path); 2193 } 2194 2195 return new PointSelection (e.point.get_prev (), e.path); 2196 } 2197 2198 private static PointSelection get_next_point_up () { 2199 return get_next_point (PI / 2); 2200 } 2201 2202 private static PointSelection get_next_point_down () { 2203 return get_next_point (PI + PI / 2); 2204 } 2205 2206 private static PointSelection get_next_point_left () { 2207 return get_next_point (PI); 2208 } 2209 2210 private static PointSelection get_next_point_right () { 2211 return get_next_point (0); 2212 } 2213 2214 private static void set_selected_point (EditPoint ep, Path p) { 2215 remove_all_selected_points (); 2216 add_selected_point (ep, p); 2217 set_active_edit_point (ep, p); 2218 edit_active_corner = true; 2219 ep.set_selected (true); 2220 set_default_handle_positions (); 2221 } 2222 2223 public static void select_point_up () { 2224 move_select_next_point (Key.UP); 2225 } 2226 2227 public static void select_point_down () { 2228 move_select_next_point (Key.DOWN); 2229 } 2230 2231 public static void select_point_right () { 2232 move_select_next_point (Key.RIGHT); 2233 } 2234 2235 public static void select_point_left () { 2236 move_select_next_point (Key.LEFT); 2237 } 2238 2239 /** 2240 * Move the selected editpoint one pixel with keyboard irrespectivly of 2241 * current zoom. 2242 */ 2243 void move_selected_points (uint keyval) { 2244 Path? last_path = null; 2245 2246 cancel_stroke_creator (); 2247 update_selected_points (); 2248 2249 if (!last_selected_is_handle) { 2250 if (keyval == Key.UP) { 2251 foreach (PointSelection e in selected_points) { 2252 e.point.set_position (e.point.x, e.point.y + Glyph.ivz ()); 2253 e.path.recalculate_linear_handles_for_point (e.point); 2254 } 2255 } 2256 2257 if (keyval == Key.DOWN) { 2258 foreach (PointSelection e in selected_points) { 2259 e.point.set_position (e.point.x, e.point.y - Glyph.ivz ()); 2260 e.path.recalculate_linear_handles_for_point (e.point); 2261 } 2262 } 2263 2264 if (keyval == Key.LEFT) { 2265 foreach (PointSelection e in selected_points) { 2266 e.point.set_position (e.point.x - Glyph.ivz (), e.point.y); 2267 e.path.recalculate_linear_handles_for_point (e.point); 2268 } 2269 } 2270 2271 if (keyval == Key.RIGHT) { 2272 foreach (PointSelection e in selected_points) { 2273 e.point.set_position (e.point.x + Glyph.ivz (), e.point.y); 2274 e.path.recalculate_linear_handles_for_point (e.point); 2275 } 2276 } 2277 2278 last_path = null; 2279 foreach (PointSelection e in selected_points) { 2280 if (e.path != last_path) { 2281 e.path.update_region_boundaries (); 2282 last_path = e.path; 2283 } 2284 } 2285 2286 } else { 2287 set_type_for_moving_handle (); 2288 active_handle.active = false; 2289 active_handle = new EditPointHandle.empty (); 2290 2291 if (keyval == Key.UP) { 2292 selected_handle.move_delta_coordinate (0, 1 * Glyph.ivz ()); 2293 } 2294 2295 if (keyval == Key.DOWN) { 2296 selected_handle.move_delta_coordinate (0, -1 * Glyph.ivz ()); 2297 } 2298 2299 if (keyval == Key.LEFT) { 2300 selected_handle.move_delta_coordinate (-1 * Glyph.ivz (), 0); 2301 } 2302 2303 if (keyval == Key.RIGHT) { 2304 selected_handle.move_delta_coordinate (1 * Glyph.ivz (), 0); 2305 } 2306 } 2307 2308 reset_stroke (); 2309 2310 // TODO: redraw only the relevant parts 2311 GlyphCanvas.redraw (); 2312 } 2313 2314 public static void convert_point_to_line (EditPoint ep, bool both) { 2315 ep.set_tie_handle (false); 2316 ep.set_reflective_handles (false); 2317 2318 if (ep.next == null) { 2319 // FIXME: write a new method for this case 2320 // warning ("Next is null."); 2321 } 2322 2323 if (ep.prev == null) { 2324 warning ("Prev is null."); 2325 } 2326 2327 if (ep.type == PointType.CUBIC || ep.type == PointType.LINE_CUBIC) { 2328 ep.type = PointType.LINE_CUBIC; 2329 2330 if (both) { 2331 ep.get_left_handle ().type = PointType.LINE_CUBIC; 2332 ep.get_right_handle ().type = PointType.LINE_CUBIC; 2333 } 2334 2335 if (ep.next != null && ep.get_next ().is_selected ()) { 2336 ep.get_right_handle ().type = PointType.LINE_CUBIC; 2337 } 2338 2339 if (ep.prev != null && ep.get_prev ().is_selected ()) { 2340 ep.get_left_handle ().type = PointType.LINE_CUBIC; 2341 } 2342 2343 } 2344 2345 if (ep.type == PointType.DOUBLE_CURVE| ep.type == PointType.LINE_DOUBLE_CURVE) { 2346 ep.type = PointType.LINE_DOUBLE_CURVE; 2347 if (both) { 2348 ep.get_left_handle ().type = PointType.LINE_DOUBLE_CURVE; 2349 ep.get_right_handle ().type = PointType.LINE_DOUBLE_CURVE; 2350 } 2351 2352 if (ep.next != null && ep.get_next ().is_selected ()) { 2353 ep.get_right_handle ().type = PointType.LINE_DOUBLE_CURVE; 2354 } 2355 2356 if (ep.prev != null && ep.get_prev ().is_selected ()) { 2357 ep.get_left_handle ().type = PointType.LINE_DOUBLE_CURVE; 2358 } 2359 } 2360 2361 if (ep.type == PointType.QUADRATIC || ep.type == PointType.LINE_QUADRATIC) { 2362 ep.type = PointType.LINE_QUADRATIC; 2363 2364 if (both) { 2365 ep.get_left_handle ().type = PointType.LINE_QUADRATIC; 2366 ep.get_right_handle ().type = PointType.LINE_QUADRATIC; 2367 2368 if (ep.next != null) { 2369 ep.get_next ().get_left_handle ().type = PointType.LINE_QUADRATIC; 2370 } 2371 2372 if (ep.prev != null) { 2373 ep.get_prev ().get_right_handle ().type = PointType.LINE_QUADRATIC; 2374 } 2375 } 2376 2377 if (ep.next != null && ep.get_next ().is_selected ()) { 2378 ep.get_right_handle ().type = PointType.LINE_QUADRATIC; 2379 ep.get_next ().get_left_handle ().type = PointType.LINE_QUADRATIC; 2380 } 2381 2382 if (ep.prev != null && ep.get_prev ().is_selected ()) { 2383 ep.get_left_handle ().type = PointType.LINE_QUADRATIC; 2384 ep.get_prev ().get_right_handle ().type = PointType.LINE_QUADRATIC; 2385 } 2386 2387 } 2388 2389 //FIXME: ep.recalculate_linear_handles (); 2390 } 2391 2392 public static void convert_segment_to_line () { 2393 if (selected_points.size == 0) { 2394 return; 2395 } 2396 2397 if (selected_points.size == 1) { 2398 convert_point_to_line (selected_points.get (0).point, true); 2399 } else { 2400 foreach (PointSelection p in selected_points) { 2401 convert_point_to_line (p.point, false); 2402 } 2403 } 2404 2405 foreach (PointSelection p in selected_points) { 2406 p.path.recalculate_linear_handles_for_point (p.point); 2407 } 2408 } 2409 2410 public static bool is_line (PointType t) { 2411 return t == PointType.LINE_QUADRATIC 2412 || t == PointType.LINE_DOUBLE_CURVE 2413 || t == PointType.LINE_CUBIC; 2414 } 2415 2416 public static PointType to_line (PointType t) { 2417 switch (t) { 2418 case PointType.QUADRATIC: 2419 return PointType.LINE_QUADRATIC; 2420 case PointType.DOUBLE_CURVE: 2421 return PointType.LINE_DOUBLE_CURVE; 2422 case PointType.CUBIC: 2423 return PointType.LINE_CUBIC; 2424 default: 2425 break; 2426 } 2427 return t; 2428 } 2429 2430 public static PointType to_curve (PointType t) { 2431 switch (t) { 2432 case PointType.LINE_QUADRATIC: 2433 return PointType.QUADRATIC; 2434 case PointType.LINE_DOUBLE_CURVE: 2435 return PointType.DOUBLE_CURVE; 2436 case PointType.LINE_CUBIC: 2437 return PointType.CUBIC; 2438 default: 2439 break; 2440 } 2441 2442 if (unlikely (t == PointType.NONE)) { 2443 warning ("Type is not set."); 2444 } 2445 2446 return t; 2447 } 2448 2449 public static void set_converted_handle_length (EditPointHandle e, PointType pt) { 2450 2451 if (e.type == PointType.QUADRATIC && pt == PointType.DOUBLE_CURVE) { 2452 e.length *= 2; 2453 e.length /= 4; 2454 } 2455 2456 if (e.type == PointType.QUADRATIC && pt == PointType.CUBIC) { 2457 e.length *= 2; 2458 e.length /= 3; 2459 } 2460 2461 if (e.type == PointType.DOUBLE_CURVE && pt == PointType.QUADRATIC) { 2462 e.length *= 4; 2463 e.length /= 2; 2464 } 2465 2466 if (e.type == PointType.DOUBLE_CURVE && pt == PointType.CUBIC) { 2467 e.length *= 4; 2468 e.length /= 3; 2469 } 2470 2471 if (e.type == PointType.CUBIC && pt == PointType.QUADRATIC) { 2472 e.length *= 3; 2473 e.length /= 2; 2474 } 2475 2476 if (e.type == PointType.CUBIC && pt == PointType.DOUBLE_CURVE) { 2477 e.length *= 3; 2478 e.length /= 4; 2479 } 2480 } 2481 2482 public static void convert_point_segment_type (EditPoint first, EditPoint next, PointType point_type) { 2483 bool line; 2484 2485 set_converted_handle_length (first.get_right_handle (), point_type); 2486 set_converted_handle_length (next.get_left_handle (), point_type); 2487 2488 line = is_line (first.type) 2489 && is_line (first.get_right_handle ().type) 2490 && is_line (next.get_left_handle ().type); 2491 2492 if (!line) { 2493 first.type = point_type; 2494 } else { 2495 first.type = to_line (point_type); 2496 } 2497 2498 if (!line) { 2499 first.get_right_handle ().type = point_type; 2500 } else { 2501 first.get_right_handle ().type = to_line (point_type); 2502 } 2503 2504 if (!line) { 2505 next.get_left_handle ().type = point_type; 2506 } else { 2507 next.get_left_handle ().type = to_line (point_type); 2508 } 2509 2510 // process connected handle 2511 if (point_type == PointType.QUADRATIC) { 2512 first.set_position (first.x, first.y); 2513 // FIXME: linear handles 2514 } 2515 } 2516 2517 public static void convert_point_type (EditPoint first, PointType point_type) { 2518 convert_point_segment_type (first, first.get_next (), point_type); 2519 } 2520 2521 public static void convert_point_types () { 2522 Glyph glyph = MainWindow.get_current_glyph (); 2523 glyph.store_undo_state (); 2524 PointSelection selected = new PointSelection.empty (); 2525 bool reset_selected = false; 2526 EditPoint e; 2527 2528 if (selected_points.size == 1) { 2529 selected = selected_points.get (0); 2530 if (selected.point.next != null) { 2531 selected_points.add (new PointSelection (selected.point.get_next (), selected.path)); 2532 selected.point.get_next ().set_selected (true); 2533 } 2534 2535 if (selected.point.prev != null) { 2536 selected_points.add (new PointSelection (selected.point.get_prev (), selected.path)); 2537 selected.point.get_next ().set_selected (true); 2538 } 2539 2540 reset_selected = true; 2541 } 2542 2543 foreach (PointSelection ps in selected_points) { 2544 e = ps.point; 2545 2546 // convert segments not control points 2547 if (e.next == null || !e.get_next ().is_selected ()) { 2548 continue; 2549 } 2550 2551 convert_point_type (e, DrawingTools.point_type); 2552 ps.path.recalculate_linear_handles_for_point (ps.point); 2553 } 2554 2555 if (reset_selected) { 2556 remove_all_selected_points (); 2557 selected_points.add (selected); 2558 selected.point.set_selected (true); 2559 } 2560 2561 foreach (Path p in glyph.get_visible_paths ()) { 2562 p.update_region_boundaries (); 2563 } 2564 } 2565 2566 public static void update_selected_points () { 2567 Glyph g = MainWindow.get_current_glyph (); 2568 selected_points.clear (); 2569 2570 foreach (Path p in g.get_visible_paths ()) { 2571 foreach (EditPoint ep in p.points) { 2572 if (ep.is_selected ()) { 2573 selected_points.add (new PointSelection (ep, p)); 2574 } 2575 } 2576 } 2577 } 2578 2579 public void select_all_points () { 2580 Glyph g = MainWindow.get_current_glyph (); 2581 2582 foreach (Path p in g.get_visible_paths ()) { 2583 foreach (EditPoint ep in p.points) { 2584 ep.set_selected (true); 2585 add_selected_point (ep, p); 2586 } 2587 } 2588 } 2589 2590 public static Path simplify (Path path, bool selected_segments = false, double threshold = 0.3) { 2591 PointSelection ps; 2592 EditPoint ep; 2593 Path p1, new_path; 2594 double d, sumd; 2595 int i; 2596 2597 p1 = path.copy (); 2598 new_path = p1.copy (); 2599 i = 0; 2600 sumd = 0; 2601 while (i < new_path.points.size) { 2602 ep = new_path.points.get (i); 2603 ps = new PointSelection (ep, new_path); 2604 d = PenTool.remove_point_simplify (ps); 2605 sumd += d; 2606 2607 if (sumd < threshold) { 2608 p1 = new_path.copy (); 2609 } else { 2610 new_path = p1.copy (); 2611 sumd = 0; 2612 i++; 2613 } 2614 } 2615 2616 new_path.update_region_boundaries (); 2617 2618 return new_path; 2619 } 2620 2621 public void set_simplification_threshold (double t) { 2622 simplification_threshold = t; 2623 } 2624 2625 public static void move_handle_on_axis () { 2626 on_axis = true; 2627 } 2628 } 2629 2630 } 2631