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