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