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