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 Glyph glyph; 1736 PointSelection new_point; 1737 1738 new_point = insert_edit_point (x, y); 1739 new_point.path.update_region_boundaries (); 1740 1741 if (new_point.path.is_open () && new_point.path.points.size > 0) { 1742 new_point.path.get_first_point ().set_reflective_handles (false); 1743 new_point.path.get_first_point ().set_tie_handle (false); 1744 new_point.path.get_last_point ().set_reflective_handles (false); 1745 new_point.path.get_last_point ().set_tie_handle (false); 1746 } 1747 1748 selected_point = new_point.point; 1749 active_edit_point = new_point.point; 1750 1751 set_point_type (selected_point); 1752 set_default_handle_positions (); 1753 1754 selected_points.clear (); 1755 add_selected_point (new_point.point, new_point.path); 1756 1757 return new_point; 1758 } 1759 1760 private static PointSelection insert_edit_point (double x, double y) { 1761 double xt, yt; 1762 Path np; 1763 EditPoint inserted; 1764 bool stroke = StrokeTool.add_stroke; 1765 Glyph g = MainWindow.get_current_glyph (); 1766 1767 if (g.active_paths.size == 0) { 1768 np = new Path (); 1769 g.add_path (np); 1770 np.stroke = stroke ? StrokeTool.stroke_width : 0; 1771 g.add_active_path (np); 1772 1773 active_path = np; 1774 selected_path = np; 1775 } 1776 1777 xt = Glyph.path_coordinate_x (x); 1778 yt = Glyph.path_coordinate_y (y); 1779 1780 if (selected_path.is_open ()) { 1781 np = PenTool.selected_path; 1782 np.add (xt, yt); 1783 } else { 1784 np = new Path (); 1785 np.stroke = stroke ? StrokeTool.stroke_width : 0; 1786 g.add_path (np); 1787 np.add (xt, yt); 1788 1789 if (DrawingTools.pen_tool.is_selected ()) { 1790 np.set_stroke (PenTool.path_stroke_width); 1791 } 1792 1793 PenTool.active_path = np; 1794 } 1795 1796 g.clear_active_paths (); 1797 g.add_active_path (np); 1798 active_path = np; 1799 selected_path = np; 1800 1801 inserted = np.points.get (np.points.size - 1); 1802 1803 return new PointSelection (inserted, np); 1804 } 1805 1806 static void set_point_type (EditPoint p) { 1807 if (p.prev != null && p.get_prev ().right_handle.type == PointType.QUADRATIC) { 1808 p.left_handle.type = PointType.QUADRATIC; 1809 p.right_handle.type = PointType.LINE_QUADRATIC; 1810 p.type = PointType.QUADRATIC; 1811 } else if (DrawingTools.get_selected_point_type () == PointType.QUADRATIC) { 1812 p.left_handle.type = PointType.LINE_QUADRATIC; 1813 p.right_handle.type = PointType.LINE_QUADRATIC; 1814 p.type = PointType.LINE_QUADRATIC; 1815 } else if (DrawingTools.get_selected_point_type () == PointType.DOUBLE_CURVE) { 1816 p.left_handle.type = PointType.LINE_DOUBLE_CURVE; 1817 p.right_handle.type = PointType.LINE_DOUBLE_CURVE; 1818 p.type = PointType.DOUBLE_CURVE; 1819 } else { 1820 p.left_handle.type = PointType.LINE_CUBIC; 1821 p.right_handle.type = PointType.LINE_CUBIC; 1822 p.type = PointType.CUBIC; 1823 } 1824 } 1825 1826 public static void set_default_handle_positions () { 1827 Glyph g = MainWindow.get_current_glyph (); 1828 foreach (var p in g.path_list) { 1829 if (p.is_editable ()) { 1830 p.create_list (); 1831 set_default_handle_positions_on_path (p); 1832 } 1833 } 1834 } 1835 1836 static void set_default_handle_positions_on_path (Path path) { 1837 foreach (EditPoint e in path.points) { 1838 if (!e.tie_handles && !e.reflective_point) { 1839 e.recalculate_linear_handles (); 1840 } 1841 } 1842 } 1843 1844 private bool is_over_handle (double event_x, double event_y) { 1845 Glyph g = MainWindow.get_current_glyph (); 1846 double distance_to_edit_point = g.view_zoom * get_distance_to_closest_edit_point (event_x, event_y); 1847 1848 if (!Path.show_all_line_handles) { 1849 foreach (PointSelection selected_corner in selected_points) { 1850 if (is_close_to_handle (selected_corner.point, event_x, event_y, distance_to_edit_point)) { 1851 return true; 1852 } 1853 } 1854 } else { 1855 foreach (Path p in g.path_list) { 1856 if (is_close_to_path (p, event_x, event_y)) { 1857 foreach (EditPoint ep in p.points) { 1858 if (is_close_to_handle (ep, event_x, event_y, distance_to_edit_point)) { 1859 return true; 1860 } 1861 } 1862 } 1863 } 1864 } 1865 1866 return false; 1867 } 1868 1869 bool is_close_to_path (Path p, double event_x, double event_y) { 1870 double c = contact_surface * Glyph.ivz (); 1871 double x = Glyph.path_coordinate_x (event_x); 1872 double y = Glyph.path_coordinate_y (event_y); 1873 1874 if (unlikely (!p.has_region_boundaries ())) { 1875 if (p.points.size > 0) { 1876 warning (@"No bounding box. $(p.points.size)"); 1877 p.update_region_boundaries (); 1878 } 1879 } 1880 1881 return p.xmin - c <= x <= p.xmax + c && p.ymin - c <= y <= p.ymax + c; 1882 } 1883 1884 private bool is_close_to_handle (EditPoint selected_corner, double event_x, double event_y, double distance_to_edit_point) { 1885 double x = Glyph.path_coordinate_x (event_x); 1886 double y = Glyph.path_coordinate_y (event_y); 1887 Glyph g = MainWindow.get_current_glyph (); 1888 double d_point = distance_to_edit_point; 1889 double dl, dr; 1890 1891 dl = g.view_zoom * selected_corner.get_left_handle ().get_point ().get_distance (x, y); 1892 dr = g.view_zoom * selected_corner.get_right_handle ().get_point ().get_distance (x, y); 1893 1894 if (dl < contact_surface && dl < d_point) { 1895 return true; 1896 } 1897 1898 if (dr < contact_surface && dr < d_point) { 1899 return true; 1900 } 1901 1902 return false; 1903 } 1904 1905 PointSelection get_closest_handle (double event_x, double event_y) { 1906 EditPointHandle left, right; 1907 double x = Glyph.path_coordinate_x (event_x); 1908 double y = Glyph.path_coordinate_y (event_y); 1909 EditPointHandle eh = new EditPointHandle.empty(); 1910 Glyph g = MainWindow.get_current_glyph (); 1911 double d = double.MAX; 1912 double dn; 1913 Path path = new Path (); 1914 bool left_handle = false; 1915 EditPoint parent_point; 1916 EditPoint tied_point; 1917 1918 foreach (Path p in g.path_list) { 1919 if (is_close_to_path (p, event_x, event_y) || p == active_path) { 1920 foreach (EditPoint ep in p.points) { 1921 if (ep.is_selected () || Path.show_all_line_handles) { 1922 left = ep.get_left_handle (); 1923 right = ep.get_right_handle (); 1924 1925 dn = left.get_point ().get_distance (x, y); 1926 1927 if (dn < d) { 1928 eh = left; 1929 d = dn; 1930 path = p; 1931 left_handle = true; 1932 } 1933 1934 dn = right.get_point ().get_distance (x, y); 1935 1936 if (dn < d) { 1937 eh = right; 1938 d = dn; 1939 path = p; 1940 left_handle = false; 1941 } 1942 } 1943 } 1944 } 1945 } 1946 1947 // Make sure the selected handle belongs to the selected point if 1948 // the current segment is quadratic. 1949 if (eh.type == PointType.QUADRATIC) { 1950 parent_point = eh.get_parent (); 1951 1952 if (left_handle) { 1953 if (parent_point.prev != null) { 1954 tied_point = parent_point.get_prev (); 1955 if (tied_point.selected_point) { 1956 eh = tied_point.get_right_handle (); 1957 } 1958 } 1959 } else { 1960 if (parent_point.next != null) { 1961 tied_point = parent_point.get_next (); 1962 if (tied_point.selected_point) { 1963 eh = tied_point.get_left_handle (); 1964 } 1965 } 1966 } 1967 } 1968 1969 return new PointSelection.handle_selection (eh, path); 1970 } 1971 1972 private void curve_active_corner_event (double event_x, double event_y) { 1973 PointSelection eh; 1974 Glyph glyph; 1975 1976 glyph = MainWindow.get_current_glyph (); 1977 active_handle.active = false; 1978 1979 if (!is_over_handle (event_x, event_y)) { 1980 return; 1981 } 1982 1983 eh = get_closest_handle (event_x, event_y); 1984 eh.handle.active = true; 1985 active_handle = eh.handle; 1986 } 1987 1988 private void curve_corner_event (double event_x, double event_y) { 1989 Glyph g = MainWindow.get_current_glyph (); 1990 g.open_path (); 1991 PointSelection p; 1992 1993 if (!is_over_handle (event_x, event_y)) { 1994 return; 1995 } 1996 1997 move_selected_handle = true; 1998 last_selected_is_handle = true; 1999 selected_handle.selected = false; 2000 p = get_closest_handle (event_x, event_y); 2001 selected_handle = p.handle; 2002 handle_selection = p; 2003 selected_handle.selected = true; 2004 2005 active_path = p.path; 2006 g.add_active_path (active_path); 2007 } 2008 2009 public static void add_selected_point (EditPoint p, Path path) { 2010 bool in_path = false; 2011 2012 foreach (EditPoint e in path.points) { 2013 if (e == p) { 2014 in_path = true; 2015 break; 2016 } 2017 } 2018 2019 if (!in_path) { 2020 warning ("Point is not in path."); 2021 } 2022 2023 foreach (PointSelection ep in selected_points) { 2024 if (p == ep.point) { 2025 return; 2026 } 2027 } 2028 2029 selected_points.add (new PointSelection (p, path)); 2030 } 2031 2032 public static void remove_all_selected_points () { 2033 Glyph g = MainWindow.get_current_glyph (); 2034 2035 foreach (PointSelection ep in selected_points) { 2036 ep.point.set_active (false); 2037 ep.point.set_selected (false); 2038 } 2039 2040 selected_points.clear (); 2041 2042 foreach (Path p in g.path_list) { 2043 foreach (EditPoint e in p.points) { 2044 e.set_active (false); 2045 e.set_selected (false); 2046 } 2047 } 2048 } 2049 2050 static void move_select_next_point (uint keyval) { 2051 PointSelection next = new PointSelection.empty (); 2052 2053 if (selected_points.size == 0) { 2054 return; 2055 } 2056 2057 switch (keyval) { 2058 case Key.UP: 2059 next = get_next_point_up (); 2060 break; 2061 case Key.DOWN: 2062 next = get_next_point_down (); 2063 break; 2064 case Key.LEFT: 2065 next = get_next_point_left (); 2066 break; 2067 case Key.RIGHT: 2068 next = get_next_point_right (); 2069 break; 2070 default: 2071 break; 2072 } 2073 2074 set_selected_point (next.point, next.path); 2075 GlyphCanvas.redraw (); 2076 } 2077 2078 private static PointSelection get_next_point (double angle) 2079 requires (selected_points.size != 0) { 2080 PointSelection e = selected_points.get (selected_points.size - 1); 2081 double right_angle = e.point.right_handle.angle; 2082 double left_angle = e.point.left_handle.angle; 2083 double min_right, min_left; 2084 double min; 2085 2086 return_val_if_fail (e.point.next != null, new EditPoint ()); 2087 return_val_if_fail (e.point.prev != null, new EditPoint ()); 2088 2089 // angle might be greater than 2 PI or less than 0 2090 min_right = double.MAX; 2091 min_left = double.MAX; 2092 for (double i = -2 * PI; i <= 2 * PI; i += 2 * PI) { 2093 min = fabs (right_angle - (angle + i)); 2094 if (min < min_right) { 2095 min_right = min; 2096 } 2097 2098 min = fabs (left_angle - (angle + i)); 2099 if (min < min_left) { 2100 min_left = min; 2101 } 2102 } 2103 2104 if (min_right < min_left) { 2105 return new PointSelection (e.point.get_next (), e.path); 2106 } 2107 2108 return new PointSelection (e.point.get_prev (), e.path); 2109 } 2110 2111 private static PointSelection get_next_point_up () { 2112 return get_next_point (PI / 2); 2113 } 2114 2115 private static PointSelection get_next_point_down () { 2116 return get_next_point (PI + PI / 2); 2117 } 2118 2119 private static PointSelection get_next_point_left () { 2120 return get_next_point (PI); 2121 } 2122 2123 private static PointSelection get_next_point_right () { 2124 return get_next_point (0); 2125 } 2126 2127 private static void set_selected_point (EditPoint ep, Path p) { 2128 remove_all_selected_points (); 2129 add_selected_point (ep, p); 2130 set_active_edit_point (ep, p); 2131 edit_active_corner = true; 2132 ep.set_selected (true); 2133 set_default_handle_positions (); 2134 } 2135 2136 public static void select_point_up () { 2137 move_select_next_point (Key.UP); 2138 } 2139 2140 public static void select_point_down () { 2141 move_select_next_point (Key.DOWN); 2142 } 2143 2144 public static void select_point_right () { 2145 move_select_next_point (Key.RIGHT); 2146 } 2147 2148 public static void select_point_left () { 2149 move_select_next_point (Key.LEFT); 2150 } 2151 2152 /** 2153 * Move the selected editpoint one pixel with keyboard irrespectivly of 2154 * current zoom. 2155 */ 2156 void move_selected_points (uint keyval) { 2157 Path? last_path = null; 2158 2159 if (!last_selected_is_handle) { 2160 if (keyval == Key.UP) { 2161 foreach (PointSelection e in selected_points) { 2162 e.point.set_position (e.point.x, e.point.y + Glyph.ivz ()); 2163 e.point.recalculate_linear_handles (); 2164 } 2165 } 2166 2167 if (keyval == Key.DOWN) { 2168 foreach (PointSelection e in selected_points) { 2169 e.point.set_position (e.point.x, e.point.y - Glyph.ivz ()); 2170 e.point.recalculate_linear_handles (); 2171 } 2172 } 2173 2174 if (keyval == Key.LEFT) { 2175 foreach (PointSelection e in selected_points) { 2176 e.point.set_position (e.point.x - Glyph.ivz (), e.point.y); 2177 e.point.recalculate_linear_handles (); 2178 } 2179 } 2180 2181 if (keyval == Key.RIGHT) { 2182 foreach (PointSelection e in selected_points) { 2183 e.point.set_position (e.point.x + Glyph.ivz (), e.point.y); 2184 e.point.recalculate_linear_handles (); 2185 } 2186 } 2187 2188 last_path = null; 2189 foreach (PointSelection e in selected_points) { 2190 if (e.path != last_path) { 2191 e.path.update_region_boundaries (); 2192 last_path = e.path; 2193 } 2194 } 2195 2196 } else { 2197 set_type_for_moving_handle (); 2198 active_handle.active = false; 2199 active_handle = new EditPointHandle.empty (); 2200 2201 if (keyval == Key.UP) { 2202 selected_handle.move_delta_coordinate (0, 1 * Glyph.ivz ()); 2203 } 2204 2205 if (keyval == Key.DOWN) { 2206 selected_handle.move_delta_coordinate (0, -1 * Glyph.ivz ()); 2207 } 2208 2209 if (keyval == Key.LEFT) { 2210 selected_handle.move_delta_coordinate (-1 * Glyph.ivz (), 0); 2211 } 2212 2213 if (keyval == Key.RIGHT) { 2214 selected_handle.move_delta_coordinate (1 * Glyph.ivz (), 0); 2215 } 2216 } 2217 2218 reset_stroke (); 2219 2220 // TODO: redraw only the relevant parts 2221 GlyphCanvas.redraw (); 2222 } 2223 2224 public static void convert_point_to_line (EditPoint ep, bool both) { 2225 ep.set_tie_handle (false); 2226 ep.set_reflective_handles (false); 2227 2228 if (ep.next == null) { 2229 // FIXME: write a new function for this case 2230 // warning ("Next is null."); 2231 } 2232 2233 if (ep.prev == null) { 2234 warning ("Prev is null."); 2235 } 2236 2237 if (ep.type == PointType.CUBIC || ep.type == PointType.LINE_CUBIC) { 2238 ep.type = PointType.LINE_CUBIC; 2239 2240 if (both) { 2241 ep.get_left_handle ().type = PointType.LINE_CUBIC; 2242 ep.get_right_handle ().type = PointType.LINE_CUBIC; 2243 } 2244 2245 if (ep.next != null && ep.get_next ().is_selected ()) { 2246 ep.get_right_handle ().type = PointType.LINE_CUBIC; 2247 } 2248 2249 if (ep.prev != null && ep.get_prev ().is_selected ()) { 2250 ep.get_left_handle ().type = PointType.LINE_CUBIC; 2251 } 2252 2253 } 2254 2255 if (ep.type == PointType.DOUBLE_CURVE| ep.type == PointType.LINE_DOUBLE_CURVE) { 2256 ep.type = PointType.LINE_DOUBLE_CURVE; 2257 if (both) { 2258 ep.get_left_handle ().type = PointType.LINE_DOUBLE_CURVE; 2259 ep.get_right_handle ().type = PointType.LINE_DOUBLE_CURVE; 2260 } 2261 2262 if (ep.next != null && ep.get_next ().is_selected ()) { 2263 ep.get_right_handle ().type = PointType.LINE_DOUBLE_CURVE; 2264 } 2265 2266 if (ep.prev != null && ep.get_prev ().is_selected ()) { 2267 ep.get_left_handle ().type = PointType.LINE_DOUBLE_CURVE; 2268 } 2269 } 2270 2271 if (ep.type == PointType.QUADRATIC || ep.type == PointType.LINE_QUADRATIC) { 2272 ep.type = PointType.LINE_QUADRATIC; 2273 2274 if (both) { 2275 ep.get_left_handle ().type = PointType.LINE_QUADRATIC; 2276 ep.get_right_handle ().type = PointType.LINE_QUADRATIC; 2277 2278 if (ep.next != null) { 2279 ep.get_next ().get_left_handle ().type = PointType.LINE_QUADRATIC; 2280 } 2281 2282 if (ep.prev != null) { 2283 ep.get_prev ().get_right_handle ().type = PointType.LINE_QUADRATIC; 2284 } 2285 } 2286 2287 if (ep.next != null && ep.get_next ().is_selected ()) { 2288 ep.get_right_handle ().type = PointType.LINE_QUADRATIC; 2289 ep.get_next ().get_left_handle ().type = PointType.LINE_QUADRATIC; 2290 } 2291 2292 if (ep.prev != null && ep.get_prev ().is_selected ()) { 2293 ep.get_left_handle ().type = PointType.LINE_QUADRATIC; 2294 ep.get_prev ().get_right_handle ().type = PointType.LINE_QUADRATIC; 2295 } 2296 2297 } 2298 2299 ep.recalculate_linear_handles (); 2300 } 2301 2302 public static void convert_segment_to_line () { 2303 if (selected_points.size == 0) { 2304 return; 2305 } 2306 2307 if (selected_points.size == 1) { 2308 convert_point_to_line (selected_points.get (0).point, true); 2309 } else { 2310 foreach (PointSelection p in selected_points) { 2311 convert_point_to_line (p.point, false); 2312 } 2313 } 2314 } 2315 2316 public static bool is_line (PointType t) { 2317 return t == PointType.LINE_QUADRATIC 2318 || t == PointType.LINE_DOUBLE_CURVE 2319 || t == PointType.LINE_CUBIC; 2320 } 2321 2322 public static PointType to_line (PointType t) { 2323 switch (t) { 2324 case PointType.QUADRATIC: 2325 return PointType.LINE_QUADRATIC; 2326 case PointType.DOUBLE_CURVE: 2327 return PointType.LINE_DOUBLE_CURVE; 2328 case PointType.CUBIC: 2329 return PointType.LINE_CUBIC; 2330 default: 2331 break; 2332 } 2333 return t; 2334 } 2335 2336 public static PointType to_curve (PointType t) { 2337 switch (t) { 2338 case PointType.LINE_QUADRATIC: 2339 return PointType.QUADRATIC; 2340 case PointType.LINE_DOUBLE_CURVE: 2341 return PointType.DOUBLE_CURVE; 2342 case PointType.LINE_CUBIC: 2343 return PointType.CUBIC; 2344 default: 2345 break; 2346 } 2347 2348 if (unlikely (t == PointType.NONE)) { 2349 warning ("Type is not set."); 2350 } 2351 2352 return t; 2353 } 2354 2355 public static void set_converted_handle_length (EditPointHandle e, PointType pt) { 2356 2357 if (e.type == PointType.QUADRATIC && pt == PointType.DOUBLE_CURVE) { 2358 e.length *= 2; 2359 e.length /= 4; 2360 } 2361 2362 if (e.type == PointType.QUADRATIC && pt == PointType.CUBIC) { 2363 e.length *= 2; 2364 e.length /= 3; 2365 } 2366 2367 if (e.type == PointType.DOUBLE_CURVE && pt == PointType.QUADRATIC) { 2368 e.length *= 4; 2369 e.length /= 2; 2370 } 2371 2372 if (e.type == PointType.DOUBLE_CURVE && pt == PointType.CUBIC) { 2373 e.length *= 4; 2374 e.length /= 3; 2375 } 2376 2377 if (e.type == PointType.CUBIC && pt == PointType.QUADRATIC) { 2378 e.length *= 3; 2379 e.length /= 2; 2380 } 2381 2382 if (e.type == PointType.CUBIC && pt == PointType.DOUBLE_CURVE) { 2383 e.length *= 3; 2384 e.length /= 4; 2385 } 2386 } 2387 2388 public static void convert_point_segment_type (EditPoint first, EditPoint next, PointType point_type) { 2389 bool line; 2390 2391 set_converted_handle_length (first.get_right_handle (), point_type); 2392 set_converted_handle_length (next.get_left_handle (), point_type); 2393 2394 line = is_line (first.type) 2395 && is_line (first.get_right_handle ().type) 2396 && is_line (next.get_left_handle ().type); 2397 2398 if (!line) { 2399 first.type = point_type; 2400 } else { 2401 first.type = to_line (point_type); 2402 } 2403 2404 if (!line) { 2405 first.get_right_handle ().type = point_type; 2406 } else { 2407 first.get_right_handle ().type = to_line (point_type); 2408 } 2409 2410 if (!line) { 2411 next.get_left_handle ().type = point_type; 2412 } else { 2413 next.get_left_handle ().type = to_line (point_type); 2414 } 2415 2416 // process connected handle 2417 if (point_type == PointType.QUADRATIC) { 2418 first.set_position (first.x, first.y); 2419 first.recalculate_linear_handles (); 2420 } 2421 } 2422 2423 public static void convert_point_type (EditPoint first, PointType point_type) { 2424 convert_point_segment_type (first, first.get_next (), point_type); 2425 } 2426 2427 public static void convert_point_types () { 2428 Glyph glyph = MainWindow.get_current_glyph (); 2429 glyph.store_undo_state (); 2430 PointSelection selected = new PointSelection.empty (); 2431 bool reset_selected = false; 2432 EditPoint e; 2433 2434 if (selected_points.size == 1) { 2435 selected = selected_points.get (0); 2436 if (selected.point.next != null) { 2437 selected_points.add (new PointSelection (selected.point.get_next (), selected.path)); 2438 selected.point.get_next ().set_selected (true); 2439 } 2440 2441 if (selected.point.prev != null) { 2442 selected_points.add (new PointSelection (selected.point.get_prev (), selected.path)); 2443 selected.point.get_next ().set_selected (true); 2444 } 2445 2446 reset_selected = true; 2447 } 2448 2449 foreach (PointSelection ps in selected_points) { 2450 e = ps.point; 2451 2452 // convert segments not control points 2453 if (e.next == null || !e.get_next ().is_selected ()) { 2454 continue; 2455 } 2456 2457 convert_point_type (e, DrawingTools.point_type); 2458 } 2459 2460 if (reset_selected) { 2461 remove_all_selected_points (); 2462 selected_points.add (selected); 2463 selected.point.set_selected (true); 2464 } 2465 2466 foreach (Path p in glyph.path_list) { 2467 p.update_region_boundaries (); 2468 } 2469 } 2470 2471 public static void update_selected_points () { 2472 Glyph g = MainWindow.get_current_glyph (); 2473 selected_points.clear (); 2474 2475 foreach (Path p in g.path_list) { 2476 foreach (EditPoint ep in p.points) { 2477 if (ep.is_selected ()) { 2478 selected_points.add (new PointSelection (ep, p)); 2479 } 2480 } 2481 } 2482 } 2483 2484 public void select_all_points () { 2485 Glyph g = MainWindow.get_current_glyph (); 2486 2487 foreach (Path p in g.path_list) { 2488 foreach (EditPoint ep in p.points) { 2489 ep.set_selected (true); 2490 add_selected_point (ep, p); 2491 } 2492 } 2493 } 2494 2495 public static Path simplify (Path path, bool selected_segments = false, double threshold = 0.3) { 2496 PointSelection ps; 2497 EditPoint ep; 2498 Path p1, new_path; 2499 double d, sumd; 2500 int i; 2501 2502 p1 = path.copy (); 2503 new_path = p1.copy (); 2504 i = 0; 2505 sumd = 0; 2506 while (i < new_path.points.size) { 2507 ep = new_path.points.get (i); 2508 ps = new PointSelection (ep, new_path); 2509 d = PenTool.remove_point_simplify (ps); 2510 sumd += d; 2511 2512 if (sumd < threshold) { 2513 p1 = new_path.copy (); 2514 } else { 2515 new_path = p1.copy (); 2516 sumd = 0; 2517 i++; 2518 } 2519 } 2520 2521 new_path.update_region_boundaries (); 2522 2523 return new_path; 2524 } 2525 2526 public void set_simplification_threshold (double t) { 2527 simplification_threshold = t; 2528 } 2529 } 2530 2531 } 2532