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

Revisions

View the latest version of libbirdfont/PenTool.vala.
Revert "Replace the list implementation"
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 (Path path in g.active_paths) { 769 path.reset_stroke (); 770 } 771 } 772 773 public void move (int x, int y) { 774 double coordinate_x, coordinate_y; 775 double delta_coordinate_x, delta_coordinate_y; 776 double angle = 0; 777 bool tied; 778 Glyph g; 779 780 g = MainWindow.get_current_glyph (); 781 782 if (move_selected || move_selected_handle) { 783 cancel_stroke_creator (); 784 } 785 786 control_point_event (x, y); 787 curve_active_corner_event (x, y); 788 set_default_handle_positions (); 789 790 if (move_selected_handle && move_selected) { 791 warning ("move_selected_handle && move_selected"); 792 move_selected = false; 793 move_selected_handle = false; 794 } 795 796 if (move_selected_handle || move_selected) { 797 MainWindow.set_cursor (NativeWindow.HIDDEN); 798 reset_stroke (); 799 } else { 800 MainWindow.set_cursor (NativeWindow.VISIBLE); 801 } 802 803 // move control point handles 804 if (move_selected_handle) { 805 set_type_for_moving_handle (); 806 807 // don't update angle if the user is holding down shift 808 if (KeyBindings.modifier == SHIFT || PenTool.retain_angle) { 809 angle = selected_handle.angle; 810 } 811 812 if (GridTool.is_visible ()) { 813 coordinate_x = Glyph.path_coordinate_x (x); 814 coordinate_y = Glyph.path_coordinate_y (y); 815 GridTool.tie_coordinate (ref coordinate_x, ref coordinate_y); 816 delta_coordinate_x = coordinate_x - last_point_x; 817 delta_coordinate_y = coordinate_y - last_point_y; 818 selected_handle.move_to_coordinate (selected_handle.x + delta_coordinate_x, selected_handle.y + delta_coordinate_y); 819 } else if (GridTool.has_ttf_grid ()) { 820 coordinate_x = Glyph.path_coordinate_x (x); 821 coordinate_y = Glyph.path_coordinate_y (y); 822 GridTool.ttf_grid_coordinate (ref coordinate_x, ref coordinate_y); 823 delta_coordinate_x = coordinate_x - last_point_x; 824 delta_coordinate_y = coordinate_y - last_point_y; 825 selected_handle.move_delta_coordinate (delta_coordinate_x, delta_coordinate_y); 826 } else { 827 coordinate_x = Glyph.path_coordinate_x (x); 828 coordinate_y = Glyph.path_coordinate_y (y); 829 delta_coordinate_x = coordinate_x - last_point_x; 830 delta_coordinate_y = coordinate_y - last_point_y; 831 selected_handle.move_delta_coordinate (delta_coordinate_x, delta_coordinate_y); 832 833 if (on_axis) { 834 double horizontal, vertical; 835 836 horizontal = Path.distance (selected_handle.parent.x, selected_handle.x, selected_handle.y, selected_handle.y); 837 vertical = Path.distance (selected_handle.x, selected_handle.x, selected_handle.parent.y, selected_handle.y); 838 839 if (horizontal < vertical) { 840 selected_handle.move_to_coordinate (selected_handle.parent.x, selected_handle.y); 841 } else { 842 selected_handle.move_to_coordinate (selected_handle.x, selected_handle.parent.y); 843 } 844 } 845 } 846 847 if (KeyBindings.modifier == SHIFT || PenTool.retain_angle) { 848 selected_handle.angle = angle; 849 selected_handle.process_connected_handle (); 850 851 if (selected_handle.parent.tie_handles) { 852 if (selected_handle.is_left_handle ()) { 853 selected_handle.parent.get_right_handle ().angle = angle - PI; 854 } else { 855 selected_handle.parent.get_left_handle ().angle = angle + PI; 856 } 857 } 858 } 859 860 handle_selection.path.update_region_boundaries (); 861 862 // FIXME: redraw line only 863 GlyphCanvas.redraw (); 864 865 if (GridTool.is_visible ()) { 866 last_point_x = selected_handle.x; 867 last_point_y = selected_handle.y; 868 } else if (GridTool.has_ttf_grid ()) { 869 last_point_x = selected_handle.x; 870 last_point_y = selected_handle.y; 871 } else { 872 last_point_x = Glyph.path_coordinate_x (x); 873 last_point_y = Glyph.path_coordinate_y (y); 874 } 875 } 876 877 // move edit point 878 if (move_selected && !show_selection_box) { 879 880 if (GridTool.is_visible ()) { 881 coordinate_x = Glyph.path_coordinate_x (x); 882 coordinate_y = Glyph.path_coordinate_y (y); 883 884 if (selected_point.tie_handles && KeyBindings.modifier == SHIFT) { 885 if (first_move_action) { 886 last_point_x = selected_point.x; 887 last_point_y = selected_point.y; 888 } 889 890 move_point_on_handles (coordinate_x, coordinate_y, out coordinate_x, out coordinate_y); 891 } else { 892 GridTool.tie_coordinate (ref coordinate_x, ref coordinate_y); 893 } 894 895 delta_coordinate_x = coordinate_x - last_point_x; 896 delta_coordinate_y = coordinate_y - last_point_y; 897 898 foreach (PointSelection selected in selected_points) { 899 if (move_point_independent_of_handle || KeyBindings.modifier == SHIFT) { 900 selected.point.set_independet_position (selected.point.x + delta_coordinate_x, 901 selected.point.y + delta_coordinate_y); 902 } else { 903 selected.point.set_position (selected.point.x + delta_coordinate_x, 904 selected.point.y + delta_coordinate_y); 905 } 906 907 selected.path.reset_stroke (); 908 selected.point.recalculate_linear_handles (); 909 selected.path.update_region_boundaries (); 910 } 911 } else if (GridTool.has_ttf_grid ()) { 912 coordinate_x = Glyph.path_coordinate_x (x); 913 coordinate_y = Glyph.path_coordinate_y (y); 914 915 GridTool.ttf_grid_coordinate (ref coordinate_x, ref coordinate_y); 916 917 if (selected_point.tie_handles && KeyBindings.modifier == SHIFT) { 918 if (first_move_action) { 919 last_point_x = selected_point.x; 920 last_point_y = selected_point.y; 921 } 922 923 move_point_on_handles (coordinate_x, coordinate_y, out coordinate_x, out coordinate_y); 924 } 925 926 delta_coordinate_x = coordinate_x - last_point_x; 927 delta_coordinate_y = coordinate_y - last_point_y; 928 929 foreach (PointSelection selected in selected_points) { 930 if (move_point_independent_of_handle || KeyBindings.modifier == SHIFT) { 931 tied = selected.point.tie_handles; 932 selected.point.tie_handles = false; 933 selected.point.set_independet_position (selected.point.x + delta_coordinate_x, 934 selected.point.y + delta_coordinate_y); 935 selected.point.tie_handles = tied; 936 } else { 937 selected.point.set_position (selected.point.x + delta_coordinate_x, 938 selected.point.y + delta_coordinate_y); 939 } 940 941 selected.path.reset_stroke (); 942 selected.point.recalculate_linear_handles (); 943 selected.path.update_region_boundaries (); 944 } 945 } else { 946 coordinate_x = Glyph.path_coordinate_x (x); 947 coordinate_y = Glyph.path_coordinate_y (y); 948 949 if (selected_point.tie_handles && KeyBindings.modifier == SHIFT) { 950 if (first_move_action) { 951 last_point_x = selected_point.x; 952 last_point_y = selected_point.y; 953 } 954 955 move_point_on_handles (coordinate_x, coordinate_y, out coordinate_x, out coordinate_y); 956 delta_coordinate_x = coordinate_x - last_point_x; 957 delta_coordinate_y = coordinate_y - last_point_y; 958 } else { 959 delta_coordinate_x = coordinate_x - last_point_x; 960 delta_coordinate_y = coordinate_y - last_point_y; 961 } 962 963 foreach (PointSelection selected in selected_points) { 964 965 if (move_point_independent_of_handle || KeyBindings.modifier == SHIFT) { 966 selected.point.set_independet_position (selected.point.x + delta_coordinate_x, 967 selected.point.y + delta_coordinate_y); 968 } else { 969 selected.point.set_position (selected.point.x + delta_coordinate_x, 970 selected.point.y + delta_coordinate_y); 971 } 972 973 selected.point.recalculate_linear_handles (); 974 selected.path.reset_stroke (); 975 selected.path.update_region_boundaries (); 976 } 977 } 978 979 if (selected_point.tie_handles && KeyBindings.modifier == SHIFT) { 980 last_point_x = selected_point.x; 981 last_point_y = selected_point.y; 982 } else if (GridTool.is_visible ()) { 983 last_point_x = selected_point.x; 984 last_point_y = selected_point.y; 985 } else if (GridTool.has_ttf_grid ()) { 986 last_point_x = selected_point.x; 987 last_point_y = selected_point.y; 988 } else { 989 last_point_x = Glyph.path_coordinate_x (x); 990 last_point_y = Glyph.path_coordinate_y (y); 991 } 992 993 GlyphCanvas.redraw (); 994 } 995 996 if (show_selection_box) { 997 GlyphCanvas.redraw (); 998 } 999 } 1000 1001 static void cancel_stroke_creator () { 1002 foreach (PointSelection p in selected_points) { 1003 p.path.stop_stroke_creator (); 1004 } 1005 } 1006 1007 private void move_point_on_handles (double px, double py, out double cx, out double cy) { 1008 EditPoint ep; 1009 ep = selected_point.copy (); 1010 ep.tie_handles = false; 1011 ep.reflective_point = false; 1012 ep.get_right_handle ().angle += PI / 2; 1013 ep.x = px; 1014 ep.y = py; 1015 1016 Path.find_intersection_handle (ep.get_right_handle (), selected_point.get_right_handle (), out cx, out cy); 1017 } 1018 1019 public void press (int button, int x, int y, bool double_click) { 1020 Glyph? g = MainWindow.get_current_glyph (); 1021 Glyph glyph = (!) g; 1022 bool reflective; 1023 1024 return_if_fail (g != null); 1025 1026 if ((double_click && !BirdFont.android) 1027 || Toolbox.drawing_tools.insert_point_on_path_tool.is_selected ()) { 1028 glyph.insert_new_point_on_path (x, y); 1029 return; 1030 } 1031 1032 if (button == 1) { 1033 add_point_event (x, y); 1034 return; 1035 } 1036 1037 if (button == 2) { 1038 if (glyph.is_open ()) { 1039 force_direction (); 1040 glyph.close_path (); 1041 } else { 1042 glyph.open_path (); 1043 } 1044 1045 return; 1046 } 1047 1048 if (button == 3 && !KeyBindings.has_shift () ) { 1049 selected_path = active_path; 1050 move_point_event (x, y); 1051 1052 // alt+click on a handle ends the symmetrical editing 1053 if ((KeyBindings.has_alt () || KeyBindings.has_ctrl ()) 1054 && is_over_handle (x, y)) { 1055 1056 // don't use set point to reflective to on open ends 1057 reflective = true; 1058 foreach (Path path in MainWindow.get_current_glyph ().active_paths) { 1059 if (path.is_open () && path.points.size > 0) { 1060 if (selected_handle.parent == path.get_first_point () 1061 || selected_handle.parent == path.get_last_point ()) { 1062 reflective = false; 1063 } 1064 } 1065 } 1066 1067 if (reflective) { 1068 selected_handle.parent.set_reflective_handles (false); 1069 selected_handle.parent.set_tie_handle (false); 1070 GlyphCanvas.redraw (); 1071 } 1072 } 1073 1074 return; 1075 } 1076 1077 if (button == 3) { 1078 selected_path = active_path; 1079 move_point_event (x, y); 1080 } 1081 1082 if (KeyBindings.has_shift () && !is_over_handle (x, y)) { 1083 show_selection_box = true; 1084 return; 1085 } 1086 } 1087 1088 public void add_point_event (int x, int y) { 1089 Glyph? g = MainWindow.get_current_glyph (); 1090 Glyph glyph = (!) g; 1091 PointSelection ps; 1092 1093 if (move_selected_handle) { 1094 warning ("moving handle"); 1095 return; 1096 } 1097 1098 return_if_fail (g != null); 1099 1100 remove_all_selected_points (); 1101 ps = new_point_action (x, y); 1102 active_path = ps.path; 1103 glyph.store_undo_state (); 1104 } 1105 1106 public void move_point_event (int x, int y) { 1107 Glyph? g = MainWindow.get_current_glyph (); 1108 Glyph glyph = (!) g; 1109 1110 return_if_fail (g != null); 1111 1112 control_point_event (x, y); 1113 curve_corner_event (x, y); 1114 1115 if (!move_selected_handle) { 1116 select_active_point (x, y); 1117 last_selected_is_handle = false; 1118 } 1119 1120 if (!KeyBindings.has_shift () 1121 && selected_points.size == 0 1122 && !active_handle.active) { 1123 show_selection_box = true; 1124 } 1125 1126 glyph.store_undo_state (); 1127 } 1128 1129 void set_type_for_moving_handle () { 1130 if (selected_handle.type == PointType.LINE_CUBIC) { 1131 selected_handle.set_point_type (PointType.CUBIC); 1132 } 1133 1134 if (selected_handle.type == PointType.LINE_QUADRATIC) { 1135 selected_handle.set_point_type (PointType.QUADRATIC); 1136 } 1137 1138 if (selected_handle.type == PointType.LINE_DOUBLE_CURVE) { 1139 selected_handle.set_point_type (PointType.DOUBLE_CURVE); 1140 } 1141 } 1142 1143 /** Set fill property to transparend for counter paths. */ 1144 public static void force_direction () { 1145 Glyph g = MainWindow.get_current_glyph (); 1146 1147 clear_directions (); 1148 1149 foreach (Path p in g.get_visible_paths ()) { 1150 if (!p.has_direction ()) { 1151 if (is_counter_path (p)) { 1152 p.force_direction (Direction.COUNTER_CLOCKWISE); 1153 } else { 1154 p.force_direction (Direction.CLOCKWISE); 1155 } 1156 } 1157 } 1158 1159 update_selected_points (); 1160 } 1161 1162 public static void clear_directions () { 1163 clockwise.clear (); 1164 counter_clockwise.clear (); 1165 } 1166 1167 public static bool is_counter_path (Path path) { 1168 Glyph g = MainWindow.get_current_glyph (); 1169 PathList pl = new PathList (); 1170 1171 foreach (Path p in g.get_visible_paths ()) { 1172 pl.add (p); 1173 } 1174 1175 return Path.is_counter (pl, path); 1176 } 1177 1178 public void remove_from_selected (EditPoint ep) 1179 requires (selected_points.size > 0) { 1180 1181 Gee.ArrayList<PointSelection> remove = new Gee.ArrayList<PointSelection> (); 1182 1183 foreach (PointSelection e in selected_points) { 1184 if (e.point.equals (e.point)) { 1185 remove.add (e); 1186 } 1187 } 1188 1189 foreach (PointSelection e in remove) { 1190 selected_points.remove (e); 1191 } 1192 } 1193 1194 public void select_active_point (double x, double y) { 1195 Glyph glyph = MainWindow.get_current_glyph (); 1196 bool reverse; 1197 1198 control_point_event (x, y); 1199 1200 // continue adding points from the other end of the selected path 1201 reverse = false; 1202 foreach (Path p in glyph.get_visible_paths ()) { 1203 if (p.is_open () && p.points.size >= 1 1204 && (active_edit_point == p.points.get (0) 1205 || active_edit_point == p.points.get (p.points.size - 1))) { 1206 update_selection (); 1207 reverse = true; 1208 control_point_event (x, y); 1209 break; 1210 } 1211 } 1212 1213 foreach (Path p in glyph.get_visible_paths ()) { 1214 if (p.is_open () && p.points.size > 1 && active_edit_point == p.points.get (0)) { 1215 p.reverse (); 1216 update_selection (); 1217 reverse = true; 1218 control_point_event (x, y); 1219 break; 1220 } 1221 } 1222 1223 if (active_edit_point == null && KeyBindings.modifier != SHIFT) { 1224 remove_all_selected_points (); 1225 } 1226 1227 move_selected = true; 1228 move_point_on_path = true; 1229 1230 if (active_edit_point != null) { 1231 glyph.clear_active_paths (); 1232 glyph.add_active_path (null, active_path); 1233 DrawingTools.update_stroke_settings (); 1234 1235 if (KeyBindings.modifier != SHIFT) { 1236 selected_point = (!)active_edit_point; 1237 1238 if (!((!)active_edit_point).is_selected ()) { 1239 remove_all_selected_points (); 1240 ((!)active_edit_point).set_selected (true); 1241 selected_point = (!)active_edit_point; 1242 add_selected_point (selected_point, active_path); // FIXME: double check active path 1243 last_selected_is_handle = false; 1244 } 1245 1246 // alt+click creates a point with symmetrical handles 1247 if (KeyBindings.has_alt () || KeyBindings.has_ctrl ()) { 1248 selected_point.set_reflective_handles (true); 1249 selected_point.process_symmetrical_handles (); 1250 GlyphCanvas.redraw (); 1251 } 1252 } 1253 } 1254 1255 if (reverse) { 1256 clockwise.clear (); 1257 counter_clockwise.clear (); 1258 } 1259 } 1260 1261 public static Path? find_path_to_join (EditPoint end_point) { 1262 Path? m = null; 1263 Glyph glyph = MainWindow.get_current_glyph (); 1264 EditPoint ep_last, ep_first; 1265 1266 foreach (Path path in glyph.get_visible_paths ()) { 1267 if (path.points.size == 0) { 1268 continue; 1269 } 1270 1271 ep_last = path.points.get (path.points.size - 1); 1272 ep_first = path.points.get (0); 1273 1274 if (end_point == ep_last) { 1275 m = path; 1276 break; 1277 } 1278 1279 if (end_point == ep_first) { 1280 m = path; 1281 break; 1282 } 1283 } 1284 1285 return m; 1286 } 1287 1288 public static Path merge_open_paths (Path path1, Path path2) { 1289 Path union, merge; 1290 EditPoint first_point; 1291 1292 union = path1.copy (); 1293 merge = path2.copy (); 1294 1295 return_val_if_fail (path1.points.size >= 1, merge); 1296 return_val_if_fail (path2.points.size >= 1, union); 1297 1298 merge.points.get (0).set_tie_handle (false); 1299 merge.points.get (0).set_reflective_handles (false); 1300 1301 merge.points.get (merge.points.size - 1).set_tie_handle (false); 1302 merge.points.get (merge.points.size - 1).set_reflective_handles (false); 1303 1304 union.points.get (union.points.size - 1).set_tie_handle (false); 1305 union.points.get (union.points.size - 1).set_reflective_handles (false); 1306 1307 union.points.get (0).set_tie_handle (false); 1308 union.points.get (0).set_reflective_handles (false); 1309 1310 first_point = merge.get_first_point (); 1311 1312 if (union.get_last_point ().get_left_handle ().is_curve ()) { 1313 first_point.get_left_handle ().convert_to_curve (); 1314 } else { 1315 if (first_point.type != PointType.QUADRATIC) { 1316 first_point.get_left_handle ().convert_to_line (); 1317 } 1318 } 1319 1320 if (first_point.type != PointType.QUADRATIC) { 1321 first_point.get_left_handle ().move_to_coordinate_internal ( 1322 union.get_last_point ().get_left_handle ().x, 1323 union.get_last_point ().get_left_handle ().y); 1324 } 1325 1326 union.delete_last_point (); 1327 1328 union.append_path (merge); 1329 1330 return union; 1331 } 1332 1333 public static void close_path (Path path) { 1334 bool last_segment_is_line; 1335 bool first_segment_is_line; 1336 1337 return_if_fail (path.points.size > 1); 1338 1339 last_segment_is_line = path.get_last_point ().get_left_handle ().is_line (); 1340 first_segment_is_line = path.get_first_point ().get_right_handle ().is_line (); 1341 1342 // TODO: set point type 1343 path.points.get (0).left_handle.move_to_coordinate ( 1344 path.points.get (path.points.size - 1).left_handle.x, 1345 path.points.get (path.points.size - 1).left_handle.y); 1346 1347 path.points.get (0).left_handle.type = 1348 path.points.get (path.points.size - 1).left_handle.type; 1349 1350 path.points.get (0).recalculate_linear_handles (); 1351 path.points.get (path.points.size - 1).recalculate_linear_handles (); 1352 1353 // force the connected handle to move 1354 path.points.get (0).set_position ( 1355 path.points.get (0).x, path.points.get (0).y); 1356 1357 path.points.remove_at (path.points.size - 1); 1358 1359 path.close (); 1360 1361 if (last_segment_is_line) { 1362 path.get_first_point ().get_left_handle ().convert_to_line (); 1363 path.get_first_point ().recalculate_linear_handles (); 1364 } 1365 1366 if (first_segment_is_line) { 1367 path.get_first_point ().get_right_handle ().convert_to_line (); 1368 path.get_first_point ().recalculate_linear_handles (); 1369 } 1370 } 1371 1372 /** @return the new path or null if no path could be merged with the end point. */ 1373 public static Path? join_paths (EditPoint end_point) { 1374 Glyph glyph = MainWindow.get_current_glyph (); 1375 Path? p; 1376 Path path; 1377 Path union; 1378 bool direction_changed = false; 1379 int px, py; 1380 1381 var paths = glyph.get_visible_paths (); 1382 1383 if (paths.size == 0) { 1384 warning ("No paths."); 1385 return null; 1386 } 1387 1388 p = find_path_to_join (end_point); 1389 if (p == null) { 1390 warning ("No path to join."); 1391 return null; 1392 } 1393 1394 path = (!) p; 1395 if (!path.is_open ()) { 1396 warning ("Path is closed."); 1397 return null; 1398 } 1399 1400 return_val_if_fail (path.points.size > 0, null); 1401 1402 px = Glyph.reverse_path_coordinate_x (end_point.x); 1403 py = Glyph.reverse_path_coordinate_y (end_point.y); 1404 1405 if (path.points.size == 1) { 1406 glyph.delete_path (path); 1407 glyph.clear_active_paths (); 1408 1409 foreach (Path merge in paths) { 1410 if (merge.points.size > 0) { 1411 if (is_close_to_point (merge.points.get (merge.points.size - 1), px, py)) { 1412 glyph.add_active_path (null, merge); 1413 active_path = merge; 1414 merge.reopen (); 1415 glyph.open_path (); 1416 return merge; 1417 } 1418 1419 if (is_close_to_point (merge.points.get (0), px, py)) { 1420 glyph.add_active_path (null, merge); 1421 active_path = merge; 1422 clear_directions (); 1423 merge.reopen (); 1424 glyph.open_path (); 1425 merge.reverse (); 1426 return merge; 1427 } 1428 } 1429 } 1430 1431 warning ("No point to merge."); 1432 return null; 1433 } 1434 1435 if (active_edit_point == path.points.get (0)) { 1436 path.reverse (); 1437 update_selection (); 1438 path.recalculate_linear_handles (); 1439 direction_changed = true; 1440 active_edit_point = path.points.get (path.points.size - 1); 1441 active_path = path; 1442 } 1443 1444 if (path.points.get (0) == active_edit_point) { 1445 warning ("Wrong direction."); 1446 return null; 1447 } 1448 1449 // join path with it self 1450 if (is_close_to_point (path.points.get (0), px, py)) { 1451 1452 close_path (path); 1453 glyph.close_path (); 1454 force_direction (); 1455 1456 glyph.clear_active_paths (); 1457 glyph.add_active_path (null, path); 1458 1459 if (direction_changed) { 1460 path.reverse (); 1461 update_selection (); 1462 } 1463 1464 remove_all_selected_points (); 1465 1466 return path; 1467 } 1468 1469 foreach (Path merge in paths) { 1470 // don't join path with itself here 1471 if (path == merge) { 1472 continue; 1473 } 1474 1475 // we need both start and end points 1476 if (merge.points.size <= 1 || path.points.size <= 1) { 1477 continue; 1478 } 1479 1480 if (is_close_to_point (merge.points.get (merge.points.size - 1), px, py)) { 1481 merge.reverse (); 1482 update_selection (); 1483 direction_changed = !direction_changed; 1484 } 1485 1486 return_val_if_fail (merge.points.size > 0, null); 1487 1488 if (is_close_to_point (merge.points.get (0), px, py)) { 1489 if (path.points.size == 1) { 1490 warning ("path.points.size == 1\n"); 1491 } else if (merge.points.size == 1) { 1492 warning ("merge.points.size == 1\n"); 1493 } else { 1494 union = merge_open_paths (path, merge); 1495 1496 glyph.add_path (union); 1497 glyph.delete_path (path); 1498 glyph.delete_path (merge); 1499 glyph.clear_active_paths (); 1500 glyph.add_active_path (null, union); 1501 1502 union.reopen (); 1503 union.create_list (); 1504 1505 force_direction (); 1506 1507 if (direction_changed) { 1508 path.reverse (); 1509 update_selection (); 1510 } 1511 1512 union.update_region_boundaries (); 1513 1514 return union; 1515 } 1516 } 1517 } 1518 1519 if (direction_changed) { 1520 path.reverse (); 1521 update_selection (); 1522 } 1523 1524 warning ("No paths merged."); 1525 return null; 1526 } 1527 1528 /** Merge paths if ends are close. */ 1529 public static bool is_close_to_point (EditPoint ep, double x, double y) { 1530 double px, py, distance; 1531 1532 px = Glyph.reverse_path_coordinate_x (ep.x); 1533 py = Glyph.reverse_path_coordinate_y (ep.y); 1534 1535 distance = sqrt (fabs (pow (px - x, 2)) + fabs (pow (py - y, 2))); 1536 1537 return (distance < 7 * MainWindow.units); 1538 } 1539 1540 /** Show the user that curves will be merged on release. */ 1541 public void draw_on_canvas (Context cr, Glyph glyph) { 1542 if (show_selection_box) { 1543 draw_selection_box (cr); 1544 } 1545 1546 if (point_selection_image) { 1547 draw_point_selection_circle (cr); 1548 } 1549 1550 draw_merge_icon (cr); 1551 } 1552 1553 /** Higlight the selected point on Android. */ 1554 void draw_point_selection_circle (Context cr) { 1555 PointSelection ps; 1556 1557 if (active_handle.active) { 1558 Path.draw_control_point (cr, Glyph.path_coordinate_x (begin_action_x), 1559 Glyph.path_coordinate_y (begin_action_y), Theme.get_color ("Active Handle")); 1560 } else if (selected_points.size > 0) { 1561 ps = selected_points.get (selected_points.size - 1); 1562 1563 if (ps.point.type == PointType.CUBIC) { 1564 Path.draw_control_point (cr, Glyph.path_coordinate_x (begin_action_x), 1565 Glyph.path_coordinate_y (begin_action_y), Theme.get_color ("Selected Cubic Control Point")); 1566 } else { 1567 Path.draw_control_point (cr, Glyph.path_coordinate_x (begin_action_x), 1568 Glyph.path_coordinate_y (begin_action_y), Theme.get_color ("Selected Quadratic Control Point")); 1569 } 1570 } 1571 } 1572 1573 void draw_selection_box (Context cr) { 1574 double x, y, w, h; 1575 1576 x = fmin (selection_box_x, selection_box_last_x); 1577 y = fmin (selection_box_y, selection_box_last_y); 1578 w = fmax (selection_box_x, selection_box_last_x) - x; 1579 h = fmax (selection_box_y, selection_box_last_y) - y; 1580 1581 cr.save (); 1582 Theme.color (cr, "Foreground 1"); 1583 cr.set_line_width (2); 1584 cr.rectangle (x, y, w, h); 1585 cr.stroke (); 1586 cr.restore (); 1587 } 1588 1589 public static void draw_join_icon (Context cr, double x, double y) { 1590 cr.save (); 1591 Theme.color (cr, "Merge"); 1592 cr.move_to (x, y); 1593 cr.arc (x, y, 15, 0, 2 * Math.PI); 1594 cr.close_path (); 1595 cr.fill (); 1596 cr.restore (); 1597 } 1598 1599 void draw_merge_icon (Context cr) { 1600 double x, y; 1601 if (active_edit_point != null) { 1602 get_tie_position ((!) active_edit_point, out x, out y); 1603 draw_join_icon (cr, x, y); 1604 } 1605 } 1606 1607 /** Obtain the position where to ends meet. */ 1608 static void get_tie_position (EditPoint current_point, out double x, out double y) { 1609 Glyph glyph; 1610 EditPoint active; 1611 double px, py; 1612 1613 x = -100; 1614 y = -100; 1615 1616 if (!is_endpoint (current_point)) { 1617 return; 1618 } 1619 1620 glyph = MainWindow.get_current_glyph (); 1621 active = current_point; 1622 1623 return_if_fail (!is_null (glyph)); 1624 1625 px = Glyph.reverse_path_coordinate_x (active.x); 1626 py = Glyph.reverse_path_coordinate_y (active.y); 1627 1628 foreach (Path path in glyph.get_visible_paths ()) { 1629 1630 if (!path.is_open ()) { 1631 continue; 1632 } 1633 1634 if (path.points.size == 0) { 1635 continue; 1636 } 1637 1638 foreach (EditPoint ep in path.points) { 1639 if (ep == active || !is_endpoint (ep)) { 1640 continue; 1641 } 1642 1643 if (is_close_to_point (ep, px, py)) { 1644 x = Glyph.reverse_path_coordinate_x (ep.x); 1645 y = Glyph.reverse_path_coordinate_y (ep.y); 1646 return; 1647 } 1648 } 1649 } 1650 } 1651 1652 public static bool is_endpoint (EditPoint ep) { 1653 EditPoint start; 1654 EditPoint end; 1655 Glyph glyph = MainWindow.get_current_glyph (); 1656 1657 foreach (Path path in glyph.get_visible_paths ()) { 1658 if (path.points.size < 1) { 1659 continue; 1660 } 1661 1662 start = path.points.get (0); 1663 end = path.points.get (path.points.size - 1); 1664 1665 if (ep == start || ep == end) { 1666 return true; 1667 } 1668 } 1669 1670 return false; 1671 } 1672 1673 public static void set_active_edit_point (EditPoint? e, Path path) { 1674 bool redraw; 1675 Glyph g = MainWindow.get_current_glyph (); 1676 1677 foreach (Path p in g.get_visible_paths ()) { 1678 foreach (var ep in p.points) { 1679 ep.set_active (false); 1680 } 1681 } 1682 1683 redraw = active_edit_point != e; 1684 active_edit_point = e; 1685 active_path = path; 1686 1687 if (e != null) { 1688 ((!)e).set_active (true); 1689 } 1690 1691 if (redraw) { 1692 GlyphCanvas.redraw (); 1693 } 1694 } 1695 1696 PointSelection? get_closest_point (double ex, double ey, out Path? path) { 1697 double x = Glyph.path_coordinate_x (ex); 1698 double y = Glyph.path_coordinate_y (ey); 1699 double d = double.MAX; 1700 double nd; 1701 PointSelection? ep = null; 1702 Glyph g = MainWindow.get_current_glyph (); 1703 1704 path = null; 1705 1706 foreach (Path current_path in g.get_paths_in_current_layer ()) { 1707 if (is_close_to_path (current_path, ex, ey)) { 1708 foreach (EditPoint e in current_path.points) { 1709 nd = e.get_distance (x, y); 1710 1711 if (nd < d) { 1712 d = nd; 1713 ep = new PointSelection (e, current_path); 1714 path = current_path; 1715 } 1716 } 1717 } 1718 } 1719 1720 return ep; 1721 } 1722 1723 public double get_distance_to_closest_edit_point (double event_x, double event_y) { 1724 Path? p; 1725 PointSelection e; 1726 PointSelection? ep = get_closest_point (event_x, event_y, out p); 1727 1728 double x = Glyph.path_coordinate_x (event_x); 1729 double y = Glyph.path_coordinate_y (event_y); 1730 1731 if (ep == null) { 1732 return double.MAX; 1733 } 1734 1735 e = (!) ep; 1736 1737 return e.point.get_distance (x, y); 1738 } 1739 1740 public void control_point_event (double event_x, double event_y, bool reset_active_point = true) { 1741 Path? p; 1742 PointSelection? ep = get_closest_point (event_x, event_y, out p); 1743 Glyph g = MainWindow.get_current_glyph (); 1744 double x = Glyph.path_coordinate_x (event_x); 1745 double y = Glyph.path_coordinate_y (event_y); 1746 double distance; 1747 PointSelection e; 1748 1749 if (reset_active_point) { 1750 set_active_edit_point (null, new Path ()); 1751 } 1752 1753 if (ep == null) { 1754 return; 1755 } 1756 1757 e = (!) ep; 1758 distance = e.point.get_distance (x, y) * g.view_zoom; 1759 1760 if (distance < contact_surface) { 1761 set_active_edit_point (e.point, e.path); 1762 } 1763 } 1764 1765 public PointSelection new_point_action (int x, int y) { 1766 Glyph glyph; 1767 PointSelection new_point; 1768 1769 glyph = MainWindow.get_current_glyph (); 1770 glyph.open_path (); 1771 1772 remove_all_selected_points (); 1773 1774 new_point = add_new_edit_point (x, y); 1775 new_point.point.set_selected (true); 1776 1777 selected_point = new_point.point; 1778 active_edit_point = new_point.point; 1779 1780 return_val_if_fail (glyph.active_paths.size > 0, new PointSelection.empty ()); 1781 add_selected_point (selected_point, glyph.active_paths.get (glyph.active_paths.size - 1)); 1782 1783 active_path = new_point.path; 1784 glyph.clear_active_paths (); 1785 glyph.add_active_path (null, new_point.path); 1786 1787 move_selected = true; 1788 1789 return new_point; 1790 } 1791 1792 public static PointSelection add_new_edit_point (int x, int y) { 1793 PointSelection new_point; 1794 1795 new_point = insert_edit_point (x, y); 1796 new_point.path.update_region_boundaries (); 1797 1798 if (new_point.path.is_open () && new_point.path.points.size > 0) { 1799 new_point.path.get_first_point ().set_reflective_handles (false); 1800 new_point.path.get_first_point ().set_tie_handle (false); 1801 new_point.path.get_last_point ().set_reflective_handles (false); 1802 new_point.path.get_last_point ().set_tie_handle (false); 1803 } 1804 1805 selected_point = new_point.point; 1806 active_edit_point = new_point.point; 1807 1808 set_point_type (selected_point); 1809 set_default_handle_positions (); 1810 1811 selected_points.clear (); 1812 add_selected_point (new_point.point, new_point.path); 1813 1814 return new_point; 1815 } 1816 1817 private static PointSelection insert_edit_point (double x, double y) { 1818 double xt, yt; 1819 Path np; 1820 EditPoint inserted; 1821 bool stroke = StrokeTool.add_stroke; 1822 Glyph g = MainWindow.get_current_glyph (); 1823 1824 if (g.active_paths.size == 0) { 1825 np = new Path (); 1826 g.add_path (np); 1827 np.stroke = stroke ? StrokeTool.stroke_width : 0; 1828 np.line_cap = StrokeTool.line_cap; 1829 1830 g.add_active_path (null, np); 1831 1832 active_path = np; 1833 selected_path = np; 1834 } 1835 1836 xt = Glyph.path_coordinate_x (x); 1837 yt = Glyph.path_coordinate_y (y); 1838 1839 if (selected_path.is_open ()) { 1840 np = PenTool.selected_path; 1841 np.add (xt, yt); 1842 } else { 1843 np = new Path (); 1844 np.stroke = stroke ? StrokeTool.stroke_width : 0; 1845 g.add_path (np); 1846 np.add (xt, yt); 1847 1848 if (DrawingTools.pen_tool.is_selected ()) { 1849 np.set_stroke (PenTool.path_stroke_width); 1850 } 1851 1852 PenTool.active_path = np; 1853 } 1854 1855 g.clear_active_paths (); 1856 g.add_active_path (null, np); 1857 active_path = np; 1858 selected_path = np; 1859 1860 inserted = np.points.get (np.points.size - 1); 1861 1862 return new PointSelection (inserted, np); 1863 } 1864 1865 static void set_point_type (EditPoint p) { 1866 if (p.prev != null && p.get_prev ().right_handle.type == PointType.QUADRATIC) { 1867 p.left_handle.type = PointType.QUADRATIC; 1868 p.right_handle.type = PointType.LINE_QUADRATIC; 1869 p.type = PointType.QUADRATIC; 1870 } else if (DrawingTools.get_selected_point_type () == PointType.QUADRATIC) { 1871 p.left_handle.type = PointType.LINE_QUADRATIC; 1872 p.right_handle.type = PointType.LINE_QUADRATIC; 1873 p.type = PointType.LINE_QUADRATIC; 1874 } else if (DrawingTools.get_selected_point_type () == PointType.DOUBLE_CURVE) { 1875 p.left_handle.type = PointType.LINE_DOUBLE_CURVE; 1876 p.right_handle.type = PointType.LINE_DOUBLE_CURVE; 1877 p.type = PointType.DOUBLE_CURVE; 1878 } else { 1879 p.left_handle.type = PointType.LINE_CUBIC; 1880 p.right_handle.type = PointType.LINE_CUBIC; 1881 p.type = PointType.CUBIC; 1882 } 1883 } 1884 1885 public static void set_default_handle_positions () { 1886 Glyph g = MainWindow.get_current_glyph (); 1887 foreach (var p in g.get_visible_paths ()) { 1888 if (p.is_editable ()) { 1889 p.create_list (); 1890 set_default_handle_positions_on_path (p); 1891 } 1892 } 1893 } 1894 1895 static void set_default_handle_positions_on_path (Path path) { 1896 foreach (EditPoint e in path.points) { 1897 if (!e.tie_handles && !e.reflective_point) { 1898 e.recalculate_linear_handles (); 1899 } 1900 } 1901 } 1902 1903 private bool is_over_handle (double event_x, double event_y) { 1904 Glyph g = MainWindow.get_current_glyph (); 1905 double distance_to_edit_point = g.view_zoom * get_distance_to_closest_edit_point (event_x, event_y); 1906 1907 if (!Path.show_all_line_handles) { 1908 foreach (PointSelection selected_corner in selected_points) { 1909 if (is_close_to_handle (selected_corner.point, event_x, event_y, distance_to_edit_point)) { 1910 return true; 1911 } 1912 } 1913 } else { 1914 foreach (Path p in g.get_visible_paths ()) { 1915 foreach (EditPoint ep in p.points) { 1916 if (is_close_to_handle (ep, event_x, event_y, distance_to_edit_point)) { 1917 return true; 1918 } 1919 } 1920 } 1921 } 1922 1923 return false; 1924 } 1925 1926 bool is_close_to_path (Path p, double event_x, double event_y) { 1927 double c = contact_surface * Glyph.ivz (); 1928 double x = Glyph.path_coordinate_x (event_x); 1929 double y = Glyph.path_coordinate_y (event_y); 1930 1931 if (unlikely (!p.has_region_boundaries ())) { 1932 if (p.points.size > 0) { 1933 warning (@"No bounding box. $(p.points.size)"); 1934 p.update_region_boundaries (); 1935 } 1936 } 1937 1938 return p.xmin - c - 10 <= x <= p.xmax + c + 10 && p.ymin - c - 10 <= y <= p.ymax + c + 10 ; 1939 } 1940 1941 private bool is_close_to_handle (EditPoint selected_corner, double event_x, double event_y, double distance_to_edit_point) { 1942 double x = Glyph.path_coordinate_x (event_x); 1943 double y = Glyph.path_coordinate_y (event_y); 1944 Glyph g = MainWindow.get_current_glyph (); 1945 double d_point = distance_to_edit_point; 1946 double dl, dr; 1947 1948 dl = g.view_zoom * selected_corner.get_left_handle ().get_point ().get_distance (x, y); 1949 dr = g.view_zoom * selected_corner.get_right_handle ().get_point ().get_distance (x, y); 1950 1951 if (dl < contact_surface && dl < d_point) { 1952 return true; 1953 } 1954 1955 if (dr < contact_surface && dr < d_point) { 1956 return true; 1957 } 1958 1959 return false; 1960 } 1961 1962 PointSelection get_closest_handle (double event_x, double event_y) { 1963 EditPointHandle left, right; 1964 double x = Glyph.path_coordinate_x (event_x); 1965 double y = Glyph.path_coordinate_y (event_y); 1966 EditPointHandle eh = new EditPointHandle.empty(); 1967 Glyph g = MainWindow.get_current_glyph (); 1968 double d = double.MAX; 1969 double dn; 1970 Path path = new Path (); 1971 bool left_handle = false; 1972 EditPoint parent_point; 1973 EditPoint tied_point; 1974 1975 foreach (Path p in g.get_paths_in_current_layer ()) { 1976 foreach (EditPoint ep in p.points) { 1977 if (ep.is_selected () || Path.show_all_line_handles) { 1978 left = ep.get_left_handle (); 1979 right = ep.get_right_handle (); 1980 1981 dn = left.get_point ().get_distance (x, y); 1982 1983 if (dn < d) { 1984 eh = left; 1985 d = dn; 1986 path = p; 1987 left_handle = true; 1988 } 1989 1990 dn = right.get_point ().get_distance (x, y); 1991 1992 if (dn < d) { 1993 eh = right; 1994 d = dn; 1995 path = p; 1996 left_handle = false; 1997 } 1998 } 1999 } 2000 } 2001 2002 // Make sure the selected handle belongs to the selected point if 2003 // the current segment is quadratic. 2004 if (eh.type == PointType.QUADRATIC) { 2005 parent_point = eh.get_parent (); 2006 2007 if (left_handle) { 2008 if (parent_point.prev != null) { 2009 tied_point = parent_point.get_prev (); 2010 if (tied_point.selected_point) { 2011 eh = tied_point.get_right_handle (); 2012 } 2013 } 2014 } else { 2015 if (parent_point.next != null) { 2016 tied_point = parent_point.get_next (); 2017 if (tied_point.selected_point) { 2018 eh = tied_point.get_left_handle (); 2019 } 2020 } 2021 } 2022 } 2023 2024 return new PointSelection.handle_selection (eh, path); 2025 } 2026 2027 private void curve_active_corner_event (double event_x, double event_y) { 2028 PointSelection eh; 2029 Glyph glyph; 2030 2031 glyph = MainWindow.get_current_glyph (); 2032 active_handle.active = false; 2033 2034 if (!is_over_handle (event_x, event_y)) { 2035 return; 2036 } 2037 2038 eh = get_closest_handle (event_x, event_y); 2039 eh.handle.active = true; 2040 2041 if (active_handle != eh.handle) { 2042 GlyphCanvas.redraw (); 2043 } 2044 2045 active_handle = eh.handle; 2046 } 2047 2048 private void curve_corner_event (double event_x, double event_y) { 2049 Glyph g = MainWindow.get_current_glyph (); 2050 g.open_path (); 2051 PointSelection p; 2052 2053 if (!is_over_handle (event_x, event_y)) { 2054 return; 2055 } 2056 2057 move_selected_handle = true; 2058 last_selected_is_handle = true; 2059 selected_handle.selected = false; 2060 p = get_closest_handle (event_x, event_y); 2061 selected_handle = p.handle; 2062 handle_selection = p; 2063 selected_handle.selected = true; 2064 2065 active_path = p.path; 2066 g.add_active_path (null, active_path); 2067 } 2068 2069 public static void add_selected_point (EditPoint p, Path path) { 2070 bool in_path = false; 2071 2072 foreach (EditPoint e in path.points) { 2073 if (e == p) { 2074 in_path = true; 2075 break; 2076 } 2077 } 2078 2079 if (!in_path) { 2080 warning ("Point is not in path."); 2081 } 2082 2083 foreach (PointSelection ep in selected_points) { 2084 if (p == ep.point) { 2085 return; 2086 } 2087 } 2088 2089 selected_points.add (new PointSelection (p, path)); 2090 } 2091 2092 public static void remove_all_selected_points () { 2093 Glyph g = MainWindow.get_current_glyph (); 2094 2095 foreach (PointSelection ep in selected_points) { 2096 ep.point.set_active (false); 2097 ep.point.set_selected (false); 2098 } 2099 2100 selected_points.clear (); 2101 2102 foreach (Path p in g.get_visible_paths ()) { 2103 foreach (EditPoint e in p.points) { 2104 e.set_active (false); 2105 e.set_selected (false); 2106 } 2107 } 2108 } 2109 2110 static void move_select_next_point (uint keyval) { 2111 PointSelection next = new PointSelection.empty (); 2112 2113 if (selected_points.size == 0) { 2114 return; 2115 } 2116 2117 cancel_stroke_creator (); 2118 2119 switch (keyval) { 2120 case Key.UP: 2121 next = get_next_point_up (); 2122 break; 2123 case Key.DOWN: 2124 next = get_next_point_down (); 2125 break; 2126 case Key.LEFT: 2127 next = get_next_point_left (); 2128 break; 2129 case Key.RIGHT: 2130 next = get_next_point_right (); 2131 break; 2132 default: 2133 break; 2134 } 2135 2136 set_selected_point (next.point, next.path); 2137 2138 GlyphCanvas.redraw (); 2139 } 2140 2141 private static PointSelection get_next_point (double angle) 2142 requires (selected_points.size != 0) { 2143 PointSelection e = selected_points.get (selected_points.size - 1); 2144 double right_angle = e.point.right_handle.angle; 2145 double left_angle = e.point.left_handle.angle; 2146 double min_right, min_left; 2147 double min; 2148 2149 return_val_if_fail (e.point.next != null, new EditPoint ()); 2150 return_val_if_fail (e.point.prev != null, new EditPoint ()); 2151 2152 // angle might be greater than 2 PI or less than 0 2153 min_right = double.MAX; 2154 min_left = double.MAX; 2155 for (double i = -2 * PI; i <= 2 * PI; i += 2 * PI) { 2156 min = fabs (right_angle - (angle + i)); 2157 if (min < min_right) { 2158 min_right = min; 2159 } 2160 2161 min = fabs (left_angle - (angle + i)); 2162 if (min < min_left) { 2163 min_left = min; 2164 } 2165 } 2166 2167 if (min_right < min_left) { 2168 return new PointSelection (e.point.get_next (), e.path); 2169 } 2170 2171 return new PointSelection (e.point.get_prev (), e.path); 2172 } 2173 2174 private static PointSelection get_next_point_up () { 2175 return get_next_point (PI / 2); 2176 } 2177 2178 private static PointSelection get_next_point_down () { 2179 return get_next_point (PI + PI / 2); 2180 } 2181 2182 private static PointSelection get_next_point_left () { 2183 return get_next_point (PI); 2184 } 2185 2186 private static PointSelection get_next_point_right () { 2187 return get_next_point (0); 2188 } 2189 2190 private static void set_selected_point (EditPoint ep, Path p) { 2191 remove_all_selected_points (); 2192 add_selected_point (ep, p); 2193 set_active_edit_point (ep, p); 2194 edit_active_corner = true; 2195 ep.set_selected (true); 2196 set_default_handle_positions (); 2197 } 2198 2199 public static void select_point_up () { 2200 move_select_next_point (Key.UP); 2201 } 2202 2203 public static void select_point_down () { 2204 move_select_next_point (Key.DOWN); 2205 } 2206 2207 public static void select_point_right () { 2208 move_select_next_point (Key.RIGHT); 2209 } 2210 2211 public static void select_point_left () { 2212 move_select_next_point (Key.LEFT); 2213 } 2214 2215 /** 2216 * Move the selected editpoint one pixel with keyboard irrespectivly of 2217 * current zoom. 2218 */ 2219 void move_selected_points (uint keyval) { 2220 Path? last_path = null; 2221 2222 cancel_stroke_creator (); 2223 update_selected_points (); 2224 2225 if (!last_selected_is_handle) { 2226 if (keyval == Key.UP) { 2227 foreach (PointSelection e in selected_points) { 2228 e.point.set_position (e.point.x, e.point.y + Glyph.ivz ()); 2229 e.point.recalculate_linear_handles (); 2230 } 2231 } 2232 2233 if (keyval == Key.DOWN) { 2234 foreach (PointSelection e in selected_points) { 2235 e.point.set_position (e.point.x, e.point.y - Glyph.ivz ()); 2236 e.point.recalculate_linear_handles (); 2237 } 2238 } 2239 2240 if (keyval == Key.LEFT) { 2241 foreach (PointSelection e in selected_points) { 2242 e.point.set_position (e.point.x - Glyph.ivz (), e.point.y); 2243 e.point.recalculate_linear_handles (); 2244 } 2245 } 2246 2247 if (keyval == Key.RIGHT) { 2248 foreach (PointSelection e in selected_points) { 2249 e.point.set_position (e.point.x + Glyph.ivz (), e.point.y); 2250 e.point.recalculate_linear_handles (); 2251 } 2252 } 2253 2254 last_path = null; 2255 foreach (PointSelection e in selected_points) { 2256 if (e.path != last_path) { 2257 e.path.update_region_boundaries (); 2258 last_path = e.path; 2259 } 2260 } 2261 2262 } else { 2263 set_type_for_moving_handle (); 2264 active_handle.active = false; 2265 active_handle = new EditPointHandle.empty (); 2266 2267 if (keyval == Key.UP) { 2268 selected_handle.move_delta_coordinate (0, 1 * Glyph.ivz ()); 2269 } 2270 2271 if (keyval == Key.DOWN) { 2272 selected_handle.move_delta_coordinate (0, -1 * Glyph.ivz ()); 2273 } 2274 2275 if (keyval == Key.LEFT) { 2276 selected_handle.move_delta_coordinate (-1 * Glyph.ivz (), 0); 2277 } 2278 2279 if (keyval == Key.RIGHT) { 2280 selected_handle.move_delta_coordinate (1 * Glyph.ivz (), 0); 2281 } 2282 } 2283 2284 reset_stroke (); 2285 2286 // TODO: redraw only the relevant parts 2287 GlyphCanvas.redraw (); 2288 } 2289 2290 public static void convert_point_to_line (EditPoint ep, bool both) { 2291 ep.set_tie_handle (false); 2292 ep.set_reflective_handles (false); 2293 2294 if (ep.next == null) { 2295 // FIXME: write a new function for this case 2296 // warning ("Next is null."); 2297 } 2298 2299 if (ep.prev == null) { 2300 warning ("Prev is null."); 2301 } 2302 2303 if (ep.type == PointType.CUBIC || ep.type == PointType.LINE_CUBIC) { 2304 ep.type = PointType.LINE_CUBIC; 2305 2306 if (both) { 2307 ep.get_left_handle ().type = PointType.LINE_CUBIC; 2308 ep.get_right_handle ().type = PointType.LINE_CUBIC; 2309 } 2310 2311 if (ep.next != null && ep.get_next ().is_selected ()) { 2312 ep.get_right_handle ().type = PointType.LINE_CUBIC; 2313 } 2314 2315 if (ep.prev != null && ep.get_prev ().is_selected ()) { 2316 ep.get_left_handle ().type = PointType.LINE_CUBIC; 2317 } 2318 2319 } 2320 2321 if (ep.type == PointType.DOUBLE_CURVE| ep.type == PointType.LINE_DOUBLE_CURVE) { 2322 ep.type = PointType.LINE_DOUBLE_CURVE; 2323 if (both) { 2324 ep.get_left_handle ().type = PointType.LINE_DOUBLE_CURVE; 2325 ep.get_right_handle ().type = PointType.LINE_DOUBLE_CURVE; 2326 } 2327 2328 if (ep.next != null && ep.get_next ().is_selected ()) { 2329 ep.get_right_handle ().type = PointType.LINE_DOUBLE_CURVE; 2330 } 2331 2332 if (ep.prev != null && ep.get_prev ().is_selected ()) { 2333 ep.get_left_handle ().type = PointType.LINE_DOUBLE_CURVE; 2334 } 2335 } 2336 2337 if (ep.type == PointType.QUADRATIC || ep.type == PointType.LINE_QUADRATIC) { 2338 ep.type = PointType.LINE_QUADRATIC; 2339 2340 if (both) { 2341 ep.get_left_handle ().type = PointType.LINE_QUADRATIC; 2342 ep.get_right_handle ().type = PointType.LINE_QUADRATIC; 2343 2344 if (ep.next != null) { 2345 ep.get_next ().get_left_handle ().type = PointType.LINE_QUADRATIC; 2346 } 2347 2348 if (ep.prev != null) { 2349 ep.get_prev ().get_right_handle ().type = PointType.LINE_QUADRATIC; 2350 } 2351 } 2352 2353 if (ep.next != null && ep.get_next ().is_selected ()) { 2354 ep.get_right_handle ().type = PointType.LINE_QUADRATIC; 2355 ep.get_next ().get_left_handle ().type = PointType.LINE_QUADRATIC; 2356 } 2357 2358 if (ep.prev != null && ep.get_prev ().is_selected ()) { 2359 ep.get_left_handle ().type = PointType.LINE_QUADRATIC; 2360 ep.get_prev ().get_right_handle ().type = PointType.LINE_QUADRATIC; 2361 } 2362 2363 } 2364 2365 ep.recalculate_linear_handles (); 2366 } 2367 2368 public static void convert_segment_to_line () { 2369 if (selected_points.size == 0) { 2370 return; 2371 } 2372 2373 if (selected_points.size == 1) { 2374 convert_point_to_line (selected_points.get (0).point, true); 2375 } else { 2376 foreach (PointSelection p in selected_points) { 2377 convert_point_to_line (p.point, false); 2378 } 2379 } 2380 } 2381 2382 public static bool is_line (PointType t) { 2383 return t == PointType.LINE_QUADRATIC 2384 || t == PointType.LINE_DOUBLE_CURVE 2385 || t == PointType.LINE_CUBIC; 2386 } 2387 2388 public static PointType to_line (PointType t) { 2389 switch (t) { 2390 case PointType.QUADRATIC: 2391 return PointType.LINE_QUADRATIC; 2392 case PointType.DOUBLE_CURVE: 2393 return PointType.LINE_DOUBLE_CURVE; 2394 case PointType.CUBIC: 2395 return PointType.LINE_CUBIC; 2396 default: 2397 break; 2398 } 2399 return t; 2400 } 2401 2402 public static PointType to_curve (PointType t) { 2403 switch (t) { 2404 case PointType.LINE_QUADRATIC: 2405 return PointType.QUADRATIC; 2406 case PointType.LINE_DOUBLE_CURVE: 2407 return PointType.DOUBLE_CURVE; 2408 case PointType.LINE_CUBIC: 2409 return PointType.CUBIC; 2410 default: 2411 break; 2412 } 2413 2414 if (unlikely (t == PointType.NONE)) { 2415 warning ("Type is not set."); 2416 } 2417 2418 return t; 2419 } 2420 2421 public static void set_converted_handle_length (EditPointHandle e, PointType pt) { 2422 2423 if (e.type == PointType.QUADRATIC && pt == PointType.DOUBLE_CURVE) { 2424 e.length *= 2; 2425 e.length /= 4; 2426 } 2427 2428 if (e.type == PointType.QUADRATIC && pt == PointType.CUBIC) { 2429 e.length *= 2; 2430 e.length /= 3; 2431 } 2432 2433 if (e.type == PointType.DOUBLE_CURVE && pt == PointType.QUADRATIC) { 2434 e.length *= 4; 2435 e.length /= 2; 2436 } 2437 2438 if (e.type == PointType.DOUBLE_CURVE && pt == PointType.CUBIC) { 2439 e.length *= 4; 2440 e.length /= 3; 2441 } 2442 2443 if (e.type == PointType.CUBIC && pt == PointType.QUADRATIC) { 2444 e.length *= 3; 2445 e.length /= 2; 2446 } 2447 2448 if (e.type == PointType.CUBIC && pt == PointType.DOUBLE_CURVE) { 2449 e.length *= 3; 2450 e.length /= 4; 2451 } 2452 } 2453 2454 public static void convert_point_segment_type (EditPoint first, EditPoint next, PointType point_type) { 2455 bool line; 2456 2457 set_converted_handle_length (first.get_right_handle (), point_type); 2458 set_converted_handle_length (next.get_left_handle (), point_type); 2459 2460 line = is_line (first.type) 2461 && is_line (first.get_right_handle ().type) 2462 && is_line (next.get_left_handle ().type); 2463 2464 if (!line) { 2465 first.type = point_type; 2466 } else { 2467 first.type = to_line (point_type); 2468 } 2469 2470 if (!line) { 2471 first.get_right_handle ().type = point_type; 2472 } else { 2473 first.get_right_handle ().type = to_line (point_type); 2474 } 2475 2476 if (!line) { 2477 next.get_left_handle ().type = point_type; 2478 } else { 2479 next.get_left_handle ().type = to_line (point_type); 2480 } 2481 2482 // process connected handle 2483 if (point_type == PointType.QUADRATIC) { 2484 first.set_position (first.x, first.y); 2485 first.recalculate_linear_handles (); 2486 } 2487 } 2488 2489 public static void convert_point_type (EditPoint first, PointType point_type) { 2490 convert_point_segment_type (first, first.get_next (), point_type); 2491 } 2492 2493 public static void convert_point_types () { 2494 Glyph glyph = MainWindow.get_current_glyph (); 2495 glyph.store_undo_state (); 2496 PointSelection selected = new PointSelection.empty (); 2497 bool reset_selected = false; 2498 EditPoint e; 2499 2500 if (selected_points.size == 1) { 2501 selected = selected_points.get (0); 2502 if (selected.point.next != null) { 2503 selected_points.add (new PointSelection (selected.point.get_next (), selected.path)); 2504 selected.point.get_next ().set_selected (true); 2505 } 2506 2507 if (selected.point.prev != null) { 2508 selected_points.add (new PointSelection (selected.point.get_prev (), selected.path)); 2509 selected.point.get_next ().set_selected (true); 2510 } 2511 2512 reset_selected = true; 2513 } 2514 2515 foreach (PointSelection ps in selected_points) { 2516 e = ps.point; 2517 2518 // convert segments not control points 2519 if (e.next == null || !e.get_next ().is_selected ()) { 2520 continue; 2521 } 2522 2523 convert_point_type (e, DrawingTools.point_type); 2524 } 2525 2526 if (reset_selected) { 2527 remove_all_selected_points (); 2528 selected_points.add (selected); 2529 selected.point.set_selected (true); 2530 } 2531 2532 foreach (Path p in glyph.get_visible_paths ()) { 2533 p.update_region_boundaries (); 2534 } 2535 } 2536 2537 public static void update_selected_points () { 2538 Glyph g = MainWindow.get_current_glyph (); 2539 selected_points.clear (); 2540 2541 foreach (Path p in g.get_visible_paths ()) { 2542 foreach (EditPoint ep in p.points) { 2543 if (ep.is_selected ()) { 2544 selected_points.add (new PointSelection (ep, p)); 2545 } 2546 } 2547 } 2548 } 2549 2550 public void select_all_points () { 2551 Glyph g = MainWindow.get_current_glyph (); 2552 2553 foreach (Path p in g.get_visible_paths ()) { 2554 foreach (EditPoint ep in p.points) { 2555 ep.set_selected (true); 2556 add_selected_point (ep, p); 2557 } 2558 } 2559 } 2560 2561 public static Path simplify (Path path, bool selected_segments = false, double threshold = 0.3) { 2562 PointSelection ps; 2563 EditPoint ep; 2564 Path p1, new_path; 2565 double d, sumd; 2566 int i; 2567 2568 p1 = path.copy (); 2569 new_path = p1.copy (); 2570 i = 0; 2571 sumd = 0; 2572 while (i < new_path.points.size) { 2573 ep = new_path.points.get (i); 2574 ps = new PointSelection (ep, new_path); 2575 d = PenTool.remove_point_simplify (ps); 2576 sumd += d; 2577 2578 if (sumd < threshold) { 2579 p1 = new_path.copy (); 2580 } else { 2581 new_path = p1.copy (); 2582 sumd = 0; 2583 i++; 2584 } 2585 } 2586 2587 new_path.update_region_boundaries (); 2588 2589 return new_path; 2590 } 2591 2592 public void set_simplification_threshold (double t) { 2593 simplification_threshold = t; 2594 } 2595 2596 public static void move_handle_on_axis () { 2597 on_axis = true; 2598 } 2599 } 2600 2601 } 2602