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