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