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