The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Glyph.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/Glyph.vala.
Ignore case in key bindings
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 Cairo; 16 using Math; 17 using Gee; 18 19 namespace BirdFont { 20 21 public class Glyph : FontDisplay { 22 23 // Background image 24 BackgroundImage? background_image = null; 25 bool background_image_visible = true; 26 27 // Glyph zoom level 28 public double view_zoom = 0.1; 29 30 public double view_offset_x = 0; 31 public double view_offset_y = 0; 32 Gee.ArrayList<ZoomView> zoom_list = new Gee.ArrayList<ZoomView> (); 33 int zoom_list_index = 0; 34 35 // The point where edit event begun 36 double pointer_begin_x = 0; 37 double pointer_begin_y = 0; 38 39 // Tap last tap event position in pixels 40 int last_tap0_y = 0; 41 int last_tap0_x = 0; 42 int last_tap1_y = 0; 43 int last_tap1_x = 0; 44 double zoom_distance = 0; 45 bool change_view; 46 47 bool ignore_input = false; 48 49 // Current pointer position 50 public double motion_x = 0; 51 public double motion_y = 0; 52 53 // Zoom area 54 public double zoom_x1 = 0; 55 public double zoom_y1 = 0; 56 public double zoom_x2 = 0; 57 public double zoom_y2 = 0; 58 public bool zoom_area_is_visible = false; 59 60 bool view_is_moving = false; 61 public double move_offset_x = 0; 62 public double move_offset_y = 0; 63 bool move_canvas = false; 64 65 public WidgetAllocation allocation = new WidgetAllocation (); 66 67 public unichar unichar_code = 0; // FIXME: name and unichar should be moved to to glyph collection 68 public string name; 69 70 public double left_limit { 71 get { 72 return _left_limit; 73 } 74 75 set { 76 _left_limit = value; 77 } 78 } 79 public double right_limit; 80 81 private double _left_limit = 0; 82 83 // x-height, lsb, etc. 84 public Gee.ArrayList<Line> vertical_help_lines = new Gee.ArrayList<Line> (); 85 public Gee.ArrayList<Line> horizontal_help_lines = new Gee.ArrayList<Line> (); 86 public bool show_help_lines = true; 87 bool xheight_lines_visible = false; 88 bool margin_boundaries_visible = false; 89 string new_guide_name = ""; 90 91 Gee.ArrayList<Glyph> undo_list = new Gee.ArrayList<Glyph> (); 92 Gee.ArrayList<Glyph> redo_list = new Gee.ArrayList<Glyph> (); 93 94 string glyph_sequence = ""; 95 bool open = true; 96 97 public static Glyph? background_glyph = null; 98 99 bool empty = false; 100 101 /** Id in the version list. */ 102 public int version_id = 0; 103 104 /** Cache quadratic form on export. */ 105 GlyfData? ttf_data = null; 106 107 Line left_line; 108 Line right_line; 109 110 /** Cache for Cairo rendering */ 111 HashMap<string, Surface> glyph_cache = new HashMap<string, Surface> (); 112 113 public const double CANVAS_MIN = -10000; 114 public const double CANVAS_MAX = 10000; 115 116 public static bool show_orientation_arrow = false; 117 public static double orientation_arrow_opacity = 1; 118 119 public Layer layers = new Layer (); 120 public int current_layer = 0; 121 public Gee.ArrayList<Path> active_paths = new Gee.ArrayList<Path> (); 122 public Gee.ArrayList<Layer> selected_groups = new Gee.ArrayList<Layer> (); 123 124 public Glyph (string name, unichar unichar_code = 0) { 125 this.name = name; 126 this.unichar_code = unichar_code; 127 128 add_help_lines (); 129 130 left_limit = -28; 131 right_limit = 28; 132 } 133 134 public Glyph.no_lines (string name, unichar unichar_code = 0) { 135 this.name = name; 136 this.unichar_code = unichar_code; 137 } 138 139 public Gee.ArrayList<Path> get_active_paths () { 140 return active_paths; 141 } 142 143 public Layer get_current_layer () { 144 return_val_if_fail (0 <= current_layer < layers.subgroups.size, new Layer ()); 145 return layers.subgroups.get (current_layer); 146 } 147 148 public void set_current_layer (Layer layer) { 149 int i = 0; 150 foreach (Layer l in layers.subgroups) { 151 if (likely (l == layer)) { 152 current_layer = i; 153 return; 154 } 155 i++; 156 } 157 158 warning ("Layer is not added to glyph."); 159 } 160 161 public PathList get_visible_path_list () { 162 return layers.get_visible_paths (); 163 } 164 165 public Gee.ArrayList<Path> get_visible_paths () { 166 return layers.get_visible_paths ().paths; 167 } 168 169 public Gee.ArrayList<Path> get_paths_in_current_layer () { 170 return get_current_layer ().get_all_paths ().paths; 171 } 172 173 public Gee.ArrayList<Path> get_all_paths () { 174 return layers.get_all_paths ().paths; 175 } 176 177 public void add_new_layer () { 178 layers.add_layer (new Layer ()); 179 current_layer = layers.subgroups.size - 1; 180 } 181 182 public GlyfData get_ttf_data () { 183 if (ttf_data == null) { 184 185 ttf_data = new GlyfData (this); 186 } 187 188 return (!) ttf_data; 189 } 190 191 public PathList get_quadratic_paths () { 192 PointConverter pc; 193 PathList pl; 194 PathList stroke; 195 196 pl = new PathList (); 197 198 foreach (Path p in get_visible_paths ()) { 199 if (p.stroke > 0) { 200 stroke = p.get_stroke (); 201 foreach (Path stroke_part in stroke.paths) { 202 pc = new PointConverter (stroke_part); 203 pl.add (pc.get_quadratic_path ()); 204 } 205 } else { 206 pc = new PointConverter (p); 207 pl.add (pc.get_quadratic_path ()); 208 } 209 } 210 211 return pl; 212 } 213 214 public override void close () { 215 undo_list.clear (); 216 redo_list.clear (); 217 } 218 219 public void set_empty_ttf (bool e) { 220 empty = e; 221 } 222 223 public bool is_empty_ttf () { 224 return empty; 225 } 226 227 public void clear_active_paths () { 228 selected_groups.clear (); 229 active_paths.clear (); 230 } 231 232 public void add_active_path (Layer? group, Path? p) { 233 Path path; 234 Layer g; 235 236 if (p != null) { 237 path = (!) p; 238 239 if (Toolbox.get_move_tool ().is_selected ()) { 240 if (path.stroke > 0) { 241 Toolbox.set_object_stroke (path.stroke); 242 } 243 } 244 245 if (!active_paths.contains (path)) { 246 active_paths.add (path); 247 } 248 PenTool.active_path = path; 249 } 250 251 if (group != null) { 252 g = (!) group; 253 if (!selected_groups.contains (g)) { 254 selected_groups.add (g); 255 } 256 } 257 } 258 259 public void delete_background () { 260 store_undo_state (); 261 background_image = null; 262 GlyphCanvas.redraw (); 263 } 264 265 public Path? get_active_path () { 266 return_val_if_fail (active_paths.size > 0, null); 267 return active_paths.get (active_paths.size - 1); 268 } 269 270 public bool boundaries (out double x1, out double y1, out double x2, out double y2) { 271 var paths = get_all_paths (); 272 273 if (paths.size == 0) { 274 x1 = 0; 275 y1 = 0; 276 x2 = 0; 277 y2 = 0; 278 return false; 279 } 280 281 x1 = CANVAS_MAX; 282 x2 = CANVAS_MIN; 283 y1 = CANVAS_MAX; 284 y2 = CANVAS_MIN; 285 286 foreach (Path p in paths) { 287 p.update_region_boundaries (); 288 289 if (p.points.size > 1) { 290 if (p.xmin < x1) { 291 x1 = p.xmin; 292 } 293 294 if (p.xmax > x2) { 295 x2 = p.xmax; 296 } 297 298 if (p.ymin < y1) { 299 y1 = p.ymin; 300 } 301 302 if (p.ymax > y2) { 303 y2 = p.ymax; 304 } 305 } 306 } 307 308 return x1 != double.MAX; 309 } 310 311 public void selection_boundaries (out double x, out double y, out double w, out double h) { 312 double px, py, px2, py2; 313 314 px = 10000; 315 py = 10000; 316 px2 = -10000; 317 py2 = -10000; 318 319 foreach (Path p in active_paths) { 320 if (p.xmin < px) { 321 px = p.xmin; 322 } 323 324 if (p.ymin < py) { 325 py = p.ymin; 326 } 327 328 if (p.xmax > px2) { 329 px2 = p.xmax; 330 } 331 332 if (p.ymax > py2) { 333 py2 = p.ymax; 334 } 335 } 336 337 if (px2 == -10000 || px == 10000) { 338 warning (@"No box for selected paths. ($(active_paths.size))"); 339 px = 0; 340 py = 0; 341 px2 = 0; 342 py2 = 0; 343 } 344 345 x = px; 346 y = py2; 347 w = px2 - px; 348 h = py2 - py; 349 } 350 351 /** @return centrum pixel for x coordinates. */ 352 public static double xc () { 353 double c = MainWindow.get_current_glyph ().allocation.width / 2.0; 354 return c; 355 } 356 357 /** @return centrum pixel for y coordinates. */ 358 public static double yc () { 359 double c = MainWindow.get_current_glyph ().allocation.height / 2.0; 360 return c; 361 } 362 363 /** @return 1/view_zoom */ 364 public static double ivz () { 365 return 1 / MainWindow.get_current_glyph ().view_zoom; 366 } 367 368 public void resized (WidgetAllocation alloc) { 369 double a, b, c, d; 370 371 a = Glyph.path_coordinate_x (0); 372 b = Glyph.path_coordinate_y (0); 373 374 this.allocation = alloc; 375 376 c = Glyph.path_coordinate_x (0); 377 d = Glyph.path_coordinate_y (0); 378 379 view_offset_x -= c - a; 380 view_offset_y -= b - d; 381 } 382 383 public void set_background_image (BackgroundImage? b) { 384 BackgroundImage bg; 385 386 if (b == null) { 387 background_image = null; 388 } else { 389 bg = (!) b; 390 background_image = bg; 391 } 392 393 BirdFont.get_current_font ().touch (); 394 } 395 396 public BackgroundImage? get_background_image () { 397 return background_image; 398 } 399 400 public override void scroll_wheel (double x, double y, 401 double pixeldelta_x, double pixeldelta_y) { 402 403 if (KeyBindings.has_alt () || KeyBindings.has_ctrl ()) { 404 if (pixeldelta_y > 0) { 405 zoom_in_at_point (x, y, pixeldelta_y); 406 } else { 407 zoom_out_at_point (x, y, pixeldelta_y); 408 } 409 } else { 410 if (!KeyBindings.has_shift ()) { 411 view_offset_x -= pixeldelta_x / view_zoom; 412 view_offset_y -= pixeldelta_y / view_zoom; 413 } else { 414 // move canvas a long x axis instead of y 415 view_offset_x -= pixeldelta_y / view_zoom; 416 view_offset_y -= pixeldelta_x / view_zoom; 417 } 418 } 419 420 redraw_area (0, 0, allocation.width, allocation.height); 421 } 422 423 public virtual void add_path (Path p) { 424 if (layers.subgroups.size == 0) { 425 layers.add_layer (new Layer ()); 426 } 427 428 get_current_layer ().add_path (p); 429 } 430 431 public override void selected_canvas () { 432 TimeoutSource input_delay; 433 434 ttf_data = null; // recreate quatradic path on export 435 436 ignore_input = true; // make sure that tripple clicks in overview are ignored 437 438 input_delay = new TimeoutSource (250); 439 input_delay.set_callback(() => { 440 ignore_input = false; 441 return false; 442 }); 443 input_delay.attach (null); 444 445 add_help_lines (); 446 KeyBindings.set_require_modifier (false); 447 glyph_sequence = Preferences.get ("glyph_sequence"); 448 449 GridTool.update_lines (); 450 451 if (!is_null (MainWindow.native_window)) { 452 MainWindow.native_window.set_scrollbar_size (0); 453 } 454 455 update_zoom_bar (); 456 DrawingTools.update_layers (); 457 } 458 459 void update_zoom_bar () { 460 if (!is_null (Toolbox.drawing_tools) 461 && !is_null (Toolbox.drawing_tools.zoom_bar)) { 462 Toolbox.drawing_tools.zoom_bar.set_zoom ((view_zoom - 1) / 20); 463 } 464 } 465 466 public void remove_lines () { 467 vertical_help_lines.clear (); 468 horizontal_help_lines.clear (); 469 } 470 471 public void add_help_lines () { 472 remove_lines (); 473 474 return_if_fail (!is_null (BirdFont.get_current_font ())); 475 476 double bgt = BirdFont.get_current_font ().top_limit; 477 Line top_margin_line = new Line ("top margin", bgt, false); 478 top_margin_line.set_color_theme ("Guide 2"); 479 top_margin_line.position_updated.connect ((pos) => { 480 BirdFont.get_current_font ().top_limit = pos; 481 }); 482 483 double thp = BirdFont.get_current_font ().top_position; 484 Line top_line = new Line ("top", thp, false); 485 top_line.position_updated.connect ((pos) => { 486 Font f = BirdFont.get_current_font (); 487 f.top_position = pos; 488 }); 489 490 double xhp = BirdFont.get_current_font ().xheight_position; 491 Line xheight_line = new Line ("x-height", xhp, false); 492 xheight_line.set_color_theme ("Guide 3"); 493 xheight_line.dashed = true; 494 xheight_line.position_updated.connect ((pos) => { 495 Font f = BirdFont.get_current_font (); 496 f.xheight_position = pos; 497 }); 498 499 double xbl = BirdFont.get_current_font ().base_line; 500 Line base_line = new Line ("baseline", xbl, false); 501 base_line.position_updated.connect ((pos) => { 502 Font f = BirdFont.get_current_font (); 503 f.base_line = pos; 504 }); 505 506 double bp = BirdFont.get_current_font ().bottom_position; 507 Line bottom_line = new Line ("bottom", bp, false); 508 bottom_line.position_updated.connect ((pos) => { 509 BirdFont.get_current_font ().bottom_position = pos; 510 }); 511 512 double bgb = BirdFont.get_current_font ().bottom_limit; 513 Line bottom_margin_line = new Line ("bottom margin", bgb, false); 514 bottom_margin_line.set_color_theme ("Guide 2"); 515 bottom_margin_line.position_updated.connect ((pos) => { 516 BirdFont.get_current_font ().bottom_limit = pos; 517 }); 518 519 left_line = new Line ("left", left_limit, true); 520 left_line.lsb = true; 521 left_line.position_updated.connect ((pos) => { 522 left_limit = pos; 523 update_other_spacing_classes (); 524 525 left_line.set_metrics (get_left_side_bearing ()); 526 }); 527 left_line.set_metrics (get_left_side_bearing ()); 528 529 right_line = new Line ("right", right_limit, true); 530 right_line.rsb = true; 531 right_line.position_updated.connect ((pos) => { 532 right_limit = pos; 533 update_other_spacing_classes (); 534 right_line.set_metrics (get_right_side_bearing ()); 535 }); 536 right_line.set_metrics (get_right_side_bearing ()); 537 538 // lists of lines are sorted and lines are added only if 539 // they are relevant for a particular glyph. 540 541 // left to right 542 add_line (left_line); 543 add_line (right_line); 544 545 bool glyph_has_top = has_top_line (); 546 547 // top to bottom 548 add_line (top_margin_line); 549 top_margin_line.set_visible (margin_boundaries_visible); 550 551 add_line (top_line); 552 top_line.set_visible (glyph_has_top || xheight_lines_visible); 553 554 add_line (xheight_line); 555 xheight_line.set_visible (!glyph_has_top || xheight_lines_visible); 556 557 add_line (base_line); 558 559 add_line (bottom_line); 560 bottom_line.set_visible (CharDatabase.has_descender (unichar_code) || xheight_lines_visible); 561 562 add_line (bottom_margin_line); 563 bottom_margin_line.set_visible (margin_boundaries_visible); 564 565 foreach (Line guide in BirdFont.get_current_font ().custom_guides) { 566 add_line (guide); 567 } 568 } 569 570 public double get_left_side_bearing () { 571 double x1, y1, x2, y2; 572 573 if (get_boundaries (out x1, out y1, out x2, out y2)) { 574 return x1 - left_limit; 575 } else { 576 return right_limit - left_limit; 577 } 578 } 579 580 public double get_right_side_bearing () { 581 double x1, y1, x2, y2; 582 583 if (get_boundaries (out x1, out y1, out x2, out y2)) { 584 return right_limit - x2; 585 } else { 586 return right_limit - left_limit; 587 } 588 } 589 590 public bool get_boundaries (out double x1, out double y1, 591 out double x2, out double y2) { 592 593 double max_x, min_x, max_y, min_y; 594 PathList pl; 595 var paths = get_all_paths (); 596 597 if (paths.size == 0) { 598 x1 = 0; 599 y1 = 0; 600 x2 = 0; 601 y2 = 0; 602 return false; 603 } 604 605 max_x = CANVAS_MIN; 606 min_x = CANVAS_MAX; 607 max_y = CANVAS_MIN; 608 min_y = CANVAS_MAX; 609 610 // FIXME: optimize 611 foreach (Path p in paths) { 612 613 if (p.stroke > 0) { 614 pl = p.get_stroke_fast (); 615 616 foreach (Path part in pl.paths) { 617 boundaries_for_path (part, ref min_x, ref max_x, ref min_y, ref max_y); 618 } 619 } else { 620 boundaries_for_path (p, ref min_x, ref max_x, ref min_y, ref max_y); 621 } 622 } 623 624 x1 = min_x; 625 y1 = max_y; 626 x2 = max_x; 627 y2 = min_y; 628 629 return max_x != CANVAS_MIN; 630 } 631 632 void boundaries_for_path (Path p, ref double min_x, ref double max_x, 633 ref double min_y, ref double max_y) { 634 635 double max_x2, max_y2, min_x2, min_y2; 636 637 max_x2 = max_x; 638 min_x2 = min_x; 639 max_y2 = max_y; 640 min_y2 = min_y; 641 642 p.all_of_path ((x, y, t) => { 643 if (x > max_x2) { 644 max_x2 = x; 645 } 646 647 if (y > max_y2) { 648 max_y2 = y; 649 } 650 651 if (x < min_x2) { 652 min_x2 = x; 653 } 654 655 if (y < min_y2) { 656 min_y2 = y; 657 } 658 659 return true; 660 }); 661 662 max_x = max_x2; 663 min_x = min_x2; 664 max_y = max_y2; 665 min_y = min_y2; 666 } 667 668 bool has_top_line () { 669 return !unichar_code.islower () || CharDatabase.has_ascender (unichar_code); 670 } 671 672 public bool get_show_help_lines () { 673 return show_help_lines; 674 } 675 676 /** Show both x-height and top lines. */ 677 public bool get_xheight_lines_visible () { 678 return xheight_lines_visible; 679 } 680 681 /** Show both x-height and top lines. */ 682 public void set_xheight_lines_visible (bool x) { 683 xheight_lines_visible = x; 684 add_help_lines (); 685 } 686 687 public void set_margin_lines_visible (bool m) { 688 margin_boundaries_visible = m; 689 add_help_lines (); 690 } 691 692 public bool get_margin_lines_visible () { 693 return margin_boundaries_visible; 694 } 695 696 public void remove_empty_paths () { 697 foreach (Path p in get_all_paths ()) { 698 if (p.points.size < 2) { 699 delete_path (p); 700 remove_empty_paths (); 701 return; 702 } 703 } 704 } 705 706 public void delete_path (Path p) { 707 layers.remove_path(p); 708 } 709 710 public string get_svg_data () { 711 return Svg.to_svg_glyph (this); 712 } 713 714 public int get_height () { 715 Font f = BirdFont.get_current_font (); 716 return (int) Math.fabs (f.top_limit - f.bottom_limit); 717 } 718 719 public double get_width () { 720 return Math.fabs (right_limit - left_limit); 721 } 722 723 public unichar get_unichar () { 724 return unichar_code; 725 } 726 727 public string get_unichar_string () { 728 string? s = (!)get_unichar ().to_string (); 729 730 if (unlikely (s == null)) { 731 warning ("Invalid character."); 732 return "".dup (); 733 } 734 735 return (!) s; 736 } 737 738 public void redraw_help_lines () { 739 redraw_area (0, 0, allocation.width, allocation.height); 740 } 741 742 public void set_show_help_lines (bool hl) { 743 show_help_lines = hl; 744 } 745 746 private void add_line (Line line) { 747 if (line.is_vertical ()) { 748 vertical_help_lines.add (line); 749 } else { 750 horizontal_help_lines.add (line); 751 } 752 753 sort_help_lines (); 754 755 line.queue_draw_area.connect ((x, y, w, h) => { 756 this.redraw_area (x, y, w, h); 757 }); 758 } 759 760 public void sort_help_lines () { 761 vertical_help_lines.sort ((a, b) => { 762 Line first, next; 763 first = (Line) a; 764 next = (Line) b; 765 return (int) (first.get_pos () - next.get_pos ()); 766 }); 767 768 horizontal_help_lines.sort ((a, b) => { 769 Line first, next; 770 first = (Line) a; 771 next = (Line) b; 772 return (int) (first.get_pos () - next.get_pos ()); 773 }); 774 } 775 776 public override string get_name () { 777 return name; 778 } 779 780 public override string get_label () { 781 return name; 782 } 783 784 private void help_line_event (int x, int y) { 785 bool m = false; 786 787 foreach (Line line in vertical_help_lines) { 788 if (!m && line.event_move_to (x, y, allocation)) { 789 m = true; 790 } 791 } 792 793 foreach (Line line in horizontal_help_lines) { 794 if (!m && line.event_move_to (x, y, allocation)) { 795 m = true; 796 } 797 } 798 } 799 800 public override void key_release (uint keyval) { 801 Tool t; 802 t = MainWindow.get_toolbox ().get_current_tool (); 803 t.key_release_action (t, keyval); 804 805 if (keyval == (uint)' ') { 806 move_canvas = false; 807 } 808 } 809 810 public override void key_press (uint keyval) { 811 Tool t = MainWindow.get_toolbox ().get_current_tool (); 812 t.key_press_action (t, keyval); 813 814 if (keyval == (uint)' ') { 815 move_canvas = true; 816 } 817 818 switch (keyval) { 819 case Key.NUM_PLUS: 820 zoom_in (); 821 break; 822 case Key.NUM_MINUS: 823 zoom_out (); 824 break; 825 } 826 } 827 828 /** Delete edit point from path. 829 * @return false if no points was deleted 830 */ 831 public bool process_deleted () { 832 Gee.ArrayList<Path> deleted_paths = new Gee.ArrayList<Path> (); 833 foreach (Path p in get_all_paths ()) { 834 if (p.points.size > 0) { 835 if (process_deleted_points_in_path (p)) { 836 return true; 837 } 838 } else { 839 deleted_paths.add (p); 840 } 841 } 842 843 foreach (Path p in deleted_paths) { 844 delete_path (p); 845 } 846 847 return false; 848 } 849 850 private bool process_deleted_points_in_path (Path p) { 851 PathList remaining_points; 852 remaining_points = p.process_deleted_points (); 853 foreach (Path path in remaining_points.paths) { 854 add_path (path); 855 path.reopen (); 856 path.create_list (); 857 858 add_active_path (null, path); 859 } 860 861 if (remaining_points.paths.size > 0) { 862 delete_path (p); 863 return true; 864 } 865 866 return false; 867 } 868 869 public override void motion_notify (double x, double y) { 870 Tool t = MainWindow.get_toolbox ().get_current_tool (); 871 872 if (view_is_moving) { 873 move_view_offset (x, y); 874 return; 875 } 876 877 help_line_event ((int) x, (int) y); 878 t.move_action (t, (int) x, (int) y); 879 880 motion_x = x * ivz () - xc () + view_offset_x; 881 motion_y = yc () - y * ivz () - view_offset_y; 882 } 883 884 public override void button_release (int button, double ex, double ey) { 885 bool line_moving = false; 886 view_is_moving = false; 887 888 foreach (Line line in get_all_help_lines ()) { 889 if (!line.set_move (false)) { 890 line_moving = true; 891 } 892 } 893 894 if (!line_moving) { 895 Tool t = MainWindow.get_toolbox ().get_current_tool (); 896 t.release_action (t, (int) button, (int) ex, (int) ey); 897 } 898 899 update_view (); 900 } 901 902 private Gee.ArrayList<Line> get_all_help_lines () { 903 Gee.ArrayList<Line> all_lines = new Gee.ArrayList<Line> (); 904 905 foreach (Line l in vertical_help_lines) { 906 all_lines.add (l); 907 } 908 909 foreach (Line l in horizontal_help_lines) { 910 all_lines.add (l); 911 } 912 913 if (GridTool.is_visible ()) { 914 foreach (Line l in GridTool.get_vertical_lines ()) { 915 all_lines.add (l); 916 } 917 918 foreach (Line l in GridTool.get_horizontal_lines ()) { 919 all_lines.add (l); 920 } 921 } 922 923 return all_lines; 924 } 925 926 public void update_view () { 927 GlyphCanvas.redraw (); 928 } 929 930 public override void double_click (uint button, double ex, double ey) { 931 Tool t = MainWindow.get_toolbox ().get_current_tool (); 932 t.double_click_action (t, (int) button, (int) ex, (int) ey); 933 } 934 935 public override void button_press (uint button, double ex, double ey) { 936 bool moving_lines = false; 937 938 pointer_begin_x = ex; 939 pointer_begin_y = ey; 940 941 foreach (Line line in horizontal_help_lines) { 942 if (!moving_lines && line.is_visible () && line.button_press (button)) { 943 moving_lines = true; 944 } 945 } 946 947 foreach (Line line in vertical_help_lines) { 948 if (!moving_lines && line.is_visible () && line.button_press (button)) { 949 moving_lines = true; 950 } 951 } 952 953 if (moving_lines) { 954 return; 955 } 956 957 if (move_canvas || DrawingTools.move_canvas.is_selected ()) { 958 view_is_moving = true; 959 move_offset_x = view_offset_x; 960 move_offset_y = view_offset_y; 961 } else { 962 Tool t = MainWindow.get_toolbox ().get_current_tool (); 963 t.press_action (t, (int) button, (int) ex, (int) ey); 964 } 965 } 966 967 /** Add new points to this path. */ 968 public void set_active_path (Path p) { 969 p.reopen (); 970 clear_active_paths (); 971 add_active_path (null, p); 972 } 973 974 /** Move view port centrum to this coordinate. */ 975 public void set_center (double x, double y) { 976 x -= allocation.width / 2.0; 977 y -= allocation.height / 2.0; 978 979 view_offset_x += x / view_zoom; 980 view_offset_y += y / view_zoom; 981 } 982 983 public void set_zoom_from_area () { 984 double x = Math.fmin (zoom_x1, zoom_x2); 985 double y = Math.fmin (zoom_y1, zoom_y2); 986 987 double w = Math.fabs (zoom_x1 - zoom_x2); 988 double h = Math.fabs (zoom_y1 - zoom_y2); 989 990 double view_zoom_x, view_zoom_y; 991 double ivz, off; 992 993 if (move_canvas) { 994 return; 995 } 996 997 if (Path.distance (x, x + w, y, y + h) < 7) { 998 zoom_in (); 999 } else { 1000 view_offset_x += x / view_zoom; 1001 view_offset_y += y / view_zoom; 1002 1003 if (unlikely (allocation.width == 0 || allocation.height == 0)) { 1004 return; 1005 } 1006 1007 view_zoom_x = allocation.width * view_zoom / w; 1008 view_zoom_y = allocation.height * view_zoom / h; 1009 1010 // TODO: there is a max zoom level 1011 1012 if (view_zoom_x * allocation.width < view_zoom_y * allocation.height) { 1013 view_zoom = view_zoom_x; 1014 ivz = 1 / view_zoom; 1015 1016 off = (view_zoom / view_zoom_y) * allocation.height / view_zoom; 1017 off = allocation.height/view_zoom - off; 1018 off /= 2; 1019 1020 view_offset_y -= off; 1021 1022 } else { 1023 view_zoom = view_zoom_y; 1024 ivz = 1 / view_zoom; 1025 1026 off = (view_zoom / view_zoom_x) * allocation.width / view_zoom; 1027 off = allocation.width / view_zoom - off; 1028 off /= 2; 1029 1030 view_offset_x -= off; 1031 } 1032 1033 zoom_area_is_visible = false; 1034 store_current_view (); 1035 } 1036 1037 update_zoom_bar (); 1038 } 1039 1040 public void show_zoom_area (int sx, int sy, int nx, int ny) { 1041 double x, y, w, h; 1042 1043 set_zoom_area (sx, sy, nx, ny); 1044 1045 zoom_area_is_visible = true; 1046 1047 x = Math.fmin (zoom_x1, zoom_x2) - 50; 1048 y = Math.fmin (zoom_y1, zoom_y2) - 50; 1049 1050 w = Math.fabs (zoom_x1 - zoom_x2) + 100; 1051 h = Math.fabs (zoom_y1 - zoom_y2) + 100; 1052 1053 redraw_area ((int)x, (int)y, (int)w, (int)h); 1054 } 1055 1056 public void set_zoom_area (int sx, int sy, int nx, int ny) { 1057 zoom_x1 = sx; 1058 zoom_y1 = sy; 1059 zoom_x2 = nx; 1060 zoom_y2 = ny; 1061 } 1062 1063 public static void validate_zoom () { 1064 Glyph g = MainWindow.get_current_glyph (); 1065 if (unlikely (g.view_zoom == 0)) { 1066 warning ("Zoom is zero."); 1067 g.reset_zoom (); 1068 1069 if (g.view_zoom == 0) { 1070 g.view_zoom = 1; 1071 g.view_offset_x = 0; 1072 g.view_offset_y = 0; 1073 } 1074 } 1075 } 1076 1077 public static double path_coordinate_x (double x) { 1078 Glyph g = MainWindow.get_current_glyph (); 1079 validate_zoom (); 1080 return x * ivz () - xc () + g.view_offset_x; 1081 } 1082 1083 public static int reverse_path_coordinate_x (double x) { 1084 Glyph g = MainWindow.get_current_glyph (); 1085 validate_zoom (); 1086 return (int) Math.rint ((x - g.view_offset_x + xc ()) * g.view_zoom); 1087 } 1088 1089 public static double precise_reverse_path_coordinate_x (double x) { 1090 Glyph g = MainWindow.get_current_glyph (); 1091 validate_zoom (); 1092 return (x - g.view_offset_x + xc ()) * g.view_zoom; 1093 } 1094 1095 public static double path_coordinate_y (double y) { 1096 Glyph g = MainWindow.get_current_glyph (); 1097 validate_zoom (); 1098 return yc () - y * ivz () - g.view_offset_y; 1099 } 1100 1101 public static int reverse_path_coordinate_y (double y) { 1102 Glyph g = MainWindow.get_current_glyph (); 1103 validate_zoom (); 1104 y = Math.rint ((y + g.view_offset_y - yc ()) * g.view_zoom); 1105 return (int) (-y); 1106 } 1107 1108 public static double precise_reverse_path_coordinate_y (double y) { 1109 Glyph g = MainWindow.get_current_glyph (); 1110 validate_zoom (); 1111 y = (y + g.view_offset_y - yc ()) * g.view_zoom; 1112 return -y; 1113 } 1114 1115 public Layer? get_path_at (double x, double y) { 1116 Layer? group = null; 1117 bool found = false; 1118 1119 foreach (Layer layer in get_current_layer ().subgroups) { 1120 foreach (Path pt in layer.paths.paths) { 1121 if (pt.is_over (x, y)) { 1122 found = true; 1123 group = layer; 1124 } 1125 } 1126 } 1127 1128 if (!found) { 1129 foreach (Path pt in get_paths_in_current_layer ()) { 1130 if (pt.is_over (x, y)) { 1131 Layer layer = new Layer (); 1132 layer.is_counter = true; 1133 layer.single_path = true; 1134 layer.add_path (pt); 1135 group = layer; 1136 } 1137 } 1138 } 1139 1140 return group; 1141 } 1142 1143 public bool select_path (double x, double y) { 1144 Path? p = null; 1145 bool found = false; 1146 1147 foreach (Path pt in get_paths_in_current_layer ()) { 1148 if (pt.is_over (x, y)) { 1149 p = pt; 1150 found = true; 1151 } 1152 } 1153 1154 if (!KeyBindings.has_shift ()) { 1155 clear_active_paths (); 1156 } 1157 1158 add_active_path (null, p); 1159 1160 return found; 1161 } 1162 1163 public bool is_over_selected_path (double x, double y) { 1164 foreach (Path pt in active_paths) { 1165 if (pt.is_over (x, y)) { 1166 return true; 1167 } 1168 } 1169 return false; 1170 } 1171 1172 public void queue_redraw_path (Path path) { 1173 redraw_path (path.xmin, path.ymin, path.xmax, path.ymax); 1174 } 1175 1176 private void redraw_path (double xmin, double ymin, double xmax, double ymax) { 1177 int yc = (int)(allocation.height / 2.0); 1178 1179 double yta = yc - ymin - view_offset_y; 1180 double ytb = yc - ymax - view_offset_y; 1181 1182 double xta = -view_offset_x - xmin; 1183 double xtb = -view_offset_x - xmax; 1184 1185 redraw_area ((int)xtb - 10, (int)yta - 10, (int)(xtb - xta) + 10, (int) (yta - ytb) + 10); 1186 } 1187 1188 public Path get_closeset_path (double x, double y) { 1189 double d; 1190 EditPoint ep = new EditPoint (); 1191 1192 Path min_point = new Path (); 1193 double min_distance = double.MAX; 1194 1195 double xt = path_coordinate_x (x); 1196 double yt = path_coordinate_y (y); 1197 var paths = get_visible_paths (); 1198 1199 foreach (Path p in paths) { 1200 if (p.is_over (xt, yt)) { 1201 return p; 1202 } 1203 } 1204 1205 foreach (Path p in paths) { 1206 if (p.points.size == 0) continue; 1207 1208 p.get_closest_point_on_path (ep, xt, yt); 1209 d = Math.pow (ep.x - xt, 2) + Math.pow (ep.y - yt, 2); 1210 1211 if (d < min_distance) { 1212 min_distance = d; 1213 min_point = p; 1214 } 1215 1216 } 1217 1218 // a path without any editpoints 1219 if (paths.size > 0) { 1220 return paths.get (0); 1221 } 1222 1223 if (unlikely (min_distance == double.MAX)) { 1224 warning (@"No path found in path_list."); 1225 } 1226 1227 return min_point; 1228 } 1229 1230 public void move_selected_edit_point_coordinates (EditPoint selected_point, double xt, double yt) { 1231 double x, y; 1232 1233 BirdFont.get_current_font ().touch (); 1234 1235 x = reverse_path_coordinate_x (xt); 1236 y = reverse_path_coordinate_y (yt); 1237 1238 // redraw control point 1239 redraw_area ((int)(x - 4*view_zoom), (int)(y - 4*view_zoom), (int)(x + 3*view_zoom), (int)(y + 3*view_zoom)); 1240 1241 // update position of selected point 1242 selected_point.set_position (xt, yt); 1243 1244 if (view_zoom >= 2) { 1245 redraw_area (0, 0, allocation.width, allocation.height); 1246 } else { 1247 redraw_last_stroke (x, y); 1248 } 1249 } 1250 1251 public void move_selected_edit_point (EditPoint selected_point, double x, double y) { 1252 double xt = path_coordinate_x (x); 1253 double yt = path_coordinate_y (y); 1254 move_selected_edit_point_coordinates (selected_point, xt, yt); 1255 } 1256 1257 public void redraw_segment (EditPoint a, EditPoint b) { 1258 double margin = 10; 1259 double x = Math.fmin (reverse_path_coordinate_x (a.x), reverse_path_coordinate_x (b.x)) - margin; 1260 double y = Math.fmin (reverse_path_coordinate_y (a.y), reverse_path_coordinate_y(b.y)) - margin; 1261 double w = Math.fabs (reverse_path_coordinate_x (a.x) - reverse_path_coordinate_x(b.x)) + 2 * margin; 1262 double h = Math.fabs (reverse_path_coordinate_y (a.y) - reverse_path_coordinate_y (b.y)) + 2 * margin; 1263 1264 redraw_area ((int)x, (int)y, (int)w, (int)h); 1265 } 1266 1267 private void redraw_last_stroke (double x, double y) { 1268 // redraw line, if we have more than one new point on path 1269 double px = 0; 1270 double py = 0; 1271 int tw = 0; 1272 int th = 0; 1273 1274 double xc = (allocation.width / 2.0); 1275 1276 if (active_paths.size == 0) { 1277 return; 1278 } 1279 1280 foreach (Path path in active_paths) { 1281 EditPoint p; 1282 EditPoint pl = path.get_last_point (); 1283 1284 if (pl.prev != null) { 1285 p = pl.get_prev (); 1286 1287 px = p.x + xc; 1288 py = p.y - xc; 1289 1290 tw = (px > x) ? (int) (px - x) : (int) (x - px); 1291 th = (py > y) ? (int) (py - y) : (int) (y - py); 1292 1293 if (px > x) px -= tw + 60; 1294 if (py > y) py -= th + 60; 1295 1296 } else { 1297 px = x - 60; 1298 py = y - 60; 1299 tw = 0; 1300 th = 0; 1301 } 1302 } 1303 1304 redraw_area ((int)px - 20, (int)py - 20, tw + 120, th + 120); 1305 } 1306 1307 public bool has_active_path () { 1308 return active_paths.size > 0; 1309 } 1310 1311 public bool is_open () { 1312 return open; 1313 } 1314 1315 /** Close all editable paths and return false if no path have been closed. */ 1316 public bool close_path () { 1317 bool r = false; 1318 1319 foreach (Path p in get_all_paths ()) { 1320 if (p.is_editable ()) { 1321 r = true; 1322 p.set_editable (false); 1323 } 1324 1325 p.create_full_stroke (); // cache stroke 1326 } 1327 1328 open = false; 1329 clear_active_paths (); 1330 GlyphCanvas.redraw (); 1331 1332 MainWindow.set_cursor (NativeWindow.VISIBLE); 1333 1334 return r; 1335 } 1336 1337 public void open_path () { 1338 foreach (Path p in get_visible_paths ()) { 1339 p.set_editable (true); 1340 p.recalculate_linear_handles (); 1341 1342 if (p.is_open () && p.points.size > 0) { 1343 p.get_first_point ().set_reflective_handles (false); 1344 p.get_first_point ().set_tie_handle (false); 1345 p.get_last_point ().set_reflective_handles (false); 1346 p.get_last_point ().set_tie_handle (false); 1347 } 1348 } 1349 1350 open = true; 1351 redraw_area (0, 0, allocation.width, allocation.height); 1352 } 1353 1354 public void redraw_path_region (Path p) { 1355 int x, y, w, h; 1356 1357 p.update_region_boundaries (); 1358 1359 x = reverse_path_coordinate_x (p.xmin); 1360 y = reverse_path_coordinate_x (p.xmin); 1361 w = reverse_path_coordinate_x (p.xmax) - x; 1362 h = reverse_path_coordinate_x (p.ymax) - y; // FIXME: redraw path 1363 1364 redraw_area (x, y, w, h); 1365 } 1366 1367 public Line get_line (string name) { 1368 foreach (Line line in vertical_help_lines) { 1369 if (likely (line.get_label () == name)) { 1370 return line; 1371 } 1372 } 1373 1374 foreach (Line line in horizontal_help_lines) { 1375 if (likely (line.get_label () == name)) { 1376 return line; 1377 } 1378 } 1379 1380 warning (@"No line with label $name found"); 1381 return new Line ("Err"); 1382 } 1383 1384 public override void zoom_in () { 1385 if (move_canvas) { 1386 return; 1387 } 1388 1389 set_zoom_area (10, 10, allocation.width - 10, allocation.height - 10); 1390 set_zoom_from_area (); 1391 update_view (); 1392 update_zoom_bar (); 1393 } 1394 1395 public override void zoom_out () { 1396 double w = allocation.width; 1397 int n = (int) (10 * ((w - 10) / allocation.width)); 1398 set_zoom_area (-n, -n, allocation.width + n, allocation.height + n); 1399 set_zoom_from_area (); 1400 update_view (); 1401 update_zoom_bar (); 1402 } 1403 1404 public override void zoom_max () { 1405 default_zoom (); 1406 update_zoom_bar (); 1407 } 1408 1409 public override void zoom_min () { 1410 double ax = 1000; 1411 double ay = 1000; 1412 double bx = -1000; 1413 double by = -1000; 1414 1415 int iax, iay, ibx, iby; 1416 1417 reset_zoom (); 1418 1419 foreach (var p in get_visible_paths ()) { 1420 p.update_region_boundaries (); 1421 1422 if (p.points.size > 2) { 1423 if (p.xmin < ax) ax = p.xmin; 1424 if (p.ymin < ay) ay = p.ymin; 1425 if (p.xmax > bx) bx = p.xmax; 1426 if (p.ymax > by) by = p.ymax; 1427 } 1428 } 1429 1430 if (ax == 1000) return; // empty page 1431 1432 iax = (int) ((ax + view_offset_x + allocation.width / 2.0) * view_zoom); 1433 iay = (int) ((-ay + view_offset_y + allocation.height / 2.0) * view_zoom); 1434 ibx = (int) ((bx + view_offset_x + allocation.width / 2.0) * view_zoom); 1435 iby = (int) ((-by + view_offset_y + allocation.height / 2.0) * view_zoom); 1436 1437 show_zoom_area (iax, iay, ibx, iby); // set this later on button release 1438 set_zoom_from_area (); 1439 zoom_out (); // add some margin 1440 1441 redraw_area (0, 0, allocation.width, allocation.height); 1442 update_zoom_bar (); 1443 } 1444 1445 public override void store_current_view () { 1446 ZoomView n; 1447 1448 if (zoom_list_index + 1 < zoom_list.size) { 1449 n = zoom_list.get (zoom_list_index); 1450 while (n != zoom_list.get (zoom_list.size - 1)) { 1451 zoom_list.remove_at (zoom_list.size - 1); 1452 } 1453 } 1454 1455 zoom_list.add (new ZoomView (view_offset_x, view_offset_y, view_zoom, allocation)); 1456 zoom_list_index = (int) zoom_list.size - 1; 1457 1458 if (zoom_list.size > 50) { 1459 zoom_list.remove_at (0); 1460 } 1461 } 1462 1463 public override void restore_last_view () { 1464 if (zoom_list.size == 0 || zoom_list_index - 1 < 0) { 1465 return; 1466 } 1467 1468 zoom_list_index--; 1469 1470 ZoomView z = zoom_list.get (zoom_list_index); 1471 1472 view_offset_x = z.x; 1473 view_offset_y = z.y; 1474 view_zoom = z.zoom; 1475 allocation = z.allocation; 1476 1477 update_zoom_bar (); 1478 } 1479 1480 public override void next_view () { 1481 ZoomView z; 1482 1483 if (zoom_list.size == 0 || zoom_list_index + 1 >= zoom_list.size) { 1484 return; 1485 } 1486 1487 zoom_list_index++; 1488 1489 z = zoom_list.get (zoom_list_index); 1490 1491 view_offset_x = z.x; 1492 view_offset_y = z.y; 1493 view_zoom = z.zoom; 1494 allocation = z.allocation; 1495 1496 update_zoom_bar (); 1497 } 1498 1499 public override void reset_zoom () { 1500 view_offset_x = 0; 1501 view_offset_y = 0; 1502 1503 set_zoom (1); 1504 1505 store_current_view (); 1506 update_zoom_bar (); 1507 } 1508 1509 /** Get x-height or top line. */ 1510 public Line get_upper_line () { 1511 if (has_top_line () || xheight_lines_visible) { 1512 return get_line ("top"); 1513 } 1514 1515 return get_line ("x-height"); 1516 } 1517 1518 /** Get base line. */ 1519 public Line get_lower_line () { 1520 return get_line ("baseline"); 1521 } 1522 1523 /** Set default zoom. See default_zoom. */ 1524 public void set_default_zoom () { 1525 int bottom = 0; 1526 int top = 0; 1527 int left = 0; 1528 int right = 0; 1529 1530 return_if_fail (vertical_help_lines.size != 0); 1531 return_if_fail (horizontal_help_lines.size != 0); 1532 1533 reset_zoom (); 1534 1535 bottom = get_lower_line ().get_position_pixel (); 1536 top = get_upper_line ().get_position_pixel (); 1537 1538 left = vertical_help_lines.get (vertical_help_lines.size - 1).get_position_pixel (); 1539 right = vertical_help_lines.get (0).get_position_pixel (); 1540 1541 set_zoom_area (left + 10, top - 10, right - 10, bottom + 10); 1542 set_zoom_from_area (); 1543 } 1544 1545 /** Set default zoom and redraw canvas. */ 1546 public void default_zoom () { 1547 set_default_zoom (); 1548 update_view (); 1549 } 1550 1551 public bool is_empty () { 1552 foreach (Path p in get_visible_paths ()) { 1553 if (p.points.size > 0) { 1554 return false; 1555 } 1556 } 1557 1558 return true; 1559 } 1560 1561 public void set_zoom (double z) 1562 requires (z > 0) 1563 { 1564 view_zoom = z; 1565 } 1566 1567 public void set_background_visible (bool visibility) { 1568 background_image_visible = visibility; 1569 } 1570 1571 public bool get_background_visible () { 1572 return background_image_visible; 1573 } 1574 1575 public void draw_coordinate (Context cr) { 1576 Theme.color (cr, "Table Border"); 1577 cr.set_font_size (12); 1578 cr.move_to (0, 10); 1579 cr.show_text (@"($motion_x, $motion_y)"); 1580 cr.stroke (); 1581 } 1582 1583 /** Draw filled paths. */ 1584 public void draw_paths (Context cr, Color? c = null) { 1585 PathList stroke; 1586 Color color; 1587 1588 cr.save (); 1589 cr.new_path (); 1590 foreach (Path p in get_visible_paths ()) { 1591 if (c != null) { 1592 color = (!) c; 1593 } else if (p.color != null) { 1594 color = (!) p.color; 1595 } else { 1596 color = Color.black (); 1597 } 1598 1599 if (p.stroke > 0) { 1600 stroke = p.get_stroke_fast (); 1601 draw_path_list (stroke, cr, color); 1602 } else { 1603 p.draw_path (cr, this, color); 1604 } 1605 } 1606 cr.fill (); 1607 cr.restore (); 1608 } 1609 1610 public void draw_path (Context cr) { 1611 PathList stroke; 1612 Color color; 1613 1614 cr.save (); 1615 cr.new_path (); 1616 foreach (Path p in get_visible_paths ()) { 1617 if (p.stroke > 0) { 1618 stroke = p.get_stroke_fast (); 1619 1620 if (p.is_editable ()) { 1621 color = Theme.get_color ("Filled Stroke"); 1622 color.a = 0.8; 1623 } else { 1624 color = Color.black (); 1625 } 1626 1627 draw_path_list (stroke, cr, color); 1628 } 1629 } 1630 cr.fill (); 1631 cr.restore (); 1632 1633 if (!(MainWindow.get_toolbox ().get_current_tool () is PenTool) 1634 && !(MainWindow.get_toolbox ().get_current_tool () is PointTool) 1635 && !(MainWindow.get_toolbox ().get_current_tool () is TrackTool) 1636 && !(MainWindow.get_toolbox ().get_current_tool () is BezierTool)) { 1637 cr.save (); 1638 cr.new_path (); 1639 foreach (Path p in active_paths) { 1640 if (p.stroke > 0) { 1641 stroke = p.get_stroke_fast (); 1642 color = Theme.get_color ("Selected Objects"); 1643 draw_path_list (stroke, cr, color); 1644 } 1645 } 1646 cr.fill (); 1647 cr.restore (); 1648 } 1649 1650 if (is_open () && Path.fill_open_path) { 1651 cr.save (); 1652 cr.new_path (); 1653 foreach (Path p in get_visible_paths ()) { 1654 if (p.stroke == 0) { 1655 color = p.color == null ? get_path_fill_color () : (!) p.color; 1656 p.draw_path (cr, this, color); 1657 } 1658 } 1659 cr.fill (); 1660 cr.restore (); 1661 } 1662 1663 if (is_open ()) { 1664 cr.save (); 1665 cr.new_path (); 1666 foreach (Path p in get_visible_paths ()) { 1667 p.draw_outline (cr); 1668 p.draw_edit_points (cr); 1669 } 1670 cr.restore (); 1671 } 1672 1673 if (!is_open ()) { 1674 // This was good for testing but it is way too slow: 1675 // Svg.draw_svg_path (cr, get_svg_data (), Glyph.xc () + left, Glyph.yc () - baseline); 1676 1677 cr.save (); 1678 cr.new_path (); 1679 foreach (Path p in get_visible_paths ()) { 1680 if (p.stroke == 0) { 1681 color = p.color == null ? Color.black () : (!) p.color; 1682 p.draw_path (cr, this, color); 1683 } 1684 } 1685 cr.close_path (); 1686 cr.fill (); 1687 cr.restore (); 1688 1689 foreach (Path p in active_paths) { 1690 cr.save (); 1691 cr.new_path (); 1692 if (p.stroke == 0) { 1693 p.draw_path (cr, this); 1694 } 1695 cr.close_path (); 1696 cr.fill (); 1697 cr.restore (); 1698 } 1699 } 1700 1701 if (show_orientation_arrow) { 1702 foreach (Path p in get_visible_paths ()) { 1703 if (p.stroke > 0) { 1704 stroke = p.get_stroke_fast (); 1705 foreach (Path ps in stroke.paths) { 1706 ps.draw_orientation_arrow (cr, orientation_arrow_opacity); 1707 } 1708 } else { 1709 p.draw_orientation_arrow (cr, orientation_arrow_opacity); 1710 } 1711 } 1712 } 1713 } 1714 1715 private Color get_path_fill_color () { 1716 return Theme.get_color ("Fill Color"); 1717 } 1718 1719 public void draw_path_list (PathList pl, Context cr, Color? c = null) { 1720 foreach (Path p in pl.paths) { 1721 p.draw_path (cr, this, c); 1722 } 1723 } 1724 1725 public void draw_background_color (Context cr, double opacity) { 1726 if (opacity > 0) { 1727 cr.save (); 1728 cr.rectangle (0, 0, allocation.width, allocation.height); 1729 Theme.color (cr, "Canvas Background"); 1730 cr.fill (); 1731 cr.restore (); 1732 } 1733 } 1734 1735 public void draw_help_lines (Context cr) { 1736 foreach (Line line in get_all_help_lines ()) { 1737 cr.save (); 1738 line.draw (cr, allocation); 1739 cr.restore (); 1740 } 1741 } 1742 1743 public void set_allocation (WidgetAllocation a) { 1744 allocation = a; 1745 } 1746 1747 public override void draw (WidgetAllocation allocation, Context cmp) { 1748 Tool tool; 1749 1750 this.allocation = allocation; 1751 1752 cmp.save (); 1753 draw_background_color (cmp, 1); 1754 cmp.restore (); 1755 1756 if (background_image != null && background_image_visible) { 1757 ((!)background_image).draw (cmp, allocation, view_offset_x, view_offset_y, view_zoom); 1758 } 1759 1760 if (unlikely (Preferences.draw_boundaries)) { 1761 foreach (Path p in get_visible_paths ()) { 1762 p.draw_boundaries (cmp); 1763 } 1764 } 1765 1766 draw_background_glyph (allocation, cmp); 1767 juxtapose (allocation, cmp); 1768 1769 if (BirdFont.show_coordinates) { 1770 draw_coordinate (cmp); 1771 } 1772 1773 if (show_help_lines) { 1774 cmp.save (); 1775 draw_help_lines (cmp); 1776 cmp.restore (); 1777 } 1778 1779 if (!is_empty ()) { 1780 cmp.save (); 1781 cmp.scale (view_zoom, view_zoom); 1782 cmp.translate (-view_offset_x, -view_offset_y); 1783 draw_path (cmp); 1784 cmp.restore (); 1785 } 1786 1787 cmp.save (); 1788 tool = MainWindow.get_toolbox ().get_current_tool (); 1789 tool.draw_action (tool, cmp, this); 1790 cmp.restore (); 1791 } 1792 1793 private void zoom_in_at_point (double x, double y, double amount = 15) { 1794 int n = (int) (-amount); 1795 zoom_at_point (x, y, n); 1796 } 1797 1798 private void zoom_out_at_point (double x, double y, double amount = 15) { 1799 double a = -amount; 1800 int n = (int) (a * ((allocation.width - a) / allocation.width)); 1801 zoom_at_point (x, y, n); 1802 } 1803 1804 public void zoom_tap (double distance) { 1805 int w = (int) (distance); 1806 if (distance != 0) { 1807 show_zoom_area (-w , -w, allocation.width + w, allocation.height + w); 1808 set_zoom_from_area (); 1809 } 1810 } 1811 1812 /** Zoom in @param n pixels. */ 1813 private void zoom_at_point (double x, double y, int n) { 1814 double w = allocation.width; 1815 double h = allocation.height; 1816 1817 double rx = Math.fabs (w / 2 - x) / (w / 2); 1818 double ry = Math.fabs (h / 2 - y) / (h / 2); 1819 1820 int xd = (x < w / 2) ? (int) (n * rx) : (int) (-n * rx); 1821 int yd = (y < h / 2) ? (int) (n * ry) : (int) (-n * ry); 1822 1823 show_zoom_area (-n + xd, -n + yd, allocation.width + n + xd, allocation.height + n + yd); 1824 set_zoom_from_area (); 1825 } 1826 1827 private void move_view_offset (double x, double y) { 1828 view_offset_x = move_offset_x + (pointer_begin_x - x) * (1/view_zoom); 1829 view_offset_y = move_offset_y + (pointer_begin_y - y) * (1/view_zoom); 1830 redraw_area (0, 0, allocation.width, allocation.height); 1831 } 1832 1833 public void store_undo_state (bool clear_redo = false) { 1834 Glyph g = copy (); 1835 undo_list.add (g); 1836 1837 if (clear_redo) { 1838 redo_list.clear (); 1839 } 1840 } 1841 1842 public void store_redo_state () { 1843 Glyph g = copy (); 1844 redo_list.add (g); 1845 } 1846 1847 public Glyph copy () { 1848 Glyph g = new Glyph.no_lines (name, unichar_code); 1849 1850 g.current_layer = current_layer; 1851 1852 g.left_limit = left_limit; 1853 g.right_limit = right_limit; 1854 1855 g.remove_lines (); 1856 1857 foreach (Line line in get_all_help_lines ()) { 1858 g.add_line (line.copy ()); 1859 } 1860 1861 g.layers = layers.copy (); 1862 1863 foreach (Path p in active_paths) { 1864 g.active_paths.add (p); 1865 } 1866 1867 if (background_image != null) { 1868 g.background_image = ((!) background_image).copy (); 1869 } 1870 1871 g.empty = empty; 1872 g.open = open; 1873 g.version_id = version_id; 1874 1875 return g; 1876 } 1877 1878 public void reload () { 1879 Font f = BirdFont.get_current_font (); 1880 1881 if (f.has_glyph (name)) { 1882 set_glyph_data ((!) f.get_glyph (name)); 1883 } 1884 } 1885 1886 public override void undo () { 1887 Glyph g; 1888 Tool tool; 1889 1890 if (undo_list.size == 0) { 1891 return; 1892 } 1893 1894 tool = MainWindow.get_toolbox ().get_current_tool (); 1895 tool.before_undo (); 1896 1897 g = undo_list.get (undo_list.size - 1); 1898 1899 store_redo_state (); 1900 set_glyph_data (g); 1901 1902 undo_list.remove_at (undo_list.size - 1); 1903 1904 DrawingTools.update_layers (); 1905 PenTool.update_selected_points (); 1906 1907 clear_active_paths (); 1908 1909 tool.after_undo (); 1910 } 1911 1912 public override void redo () { 1913 Glyph g; 1914 1915 if (redo_list.size == 0) { 1916 return; 1917 } 1918 1919 g = redo_list.get (redo_list.size - 1); 1920 1921 store_undo_state (false); 1922 set_glyph_data (g); 1923 1924 redo_list.remove_at (redo_list.size - 1); 1925 1926 DrawingTools.update_layers (); 1927 PenTool.update_selected_points (); 1928 1929 clear_active_paths (); 1930 } 1931 1932 void set_glyph_data (Glyph g) { 1933 current_layer = g.current_layer; 1934 layers = g.layers.copy (); 1935 1936 remove_lines (); 1937 foreach (Line line in g.get_all_help_lines ()) { 1938 add_line (line.copy ()); 1939 } 1940 1941 add_help_lines (); 1942 1943 if (g.background_image != null) { 1944 background_image = ((!) g.background_image).copy (); 1945 } 1946 1947 clear_active_paths (); 1948 foreach (Path p in g.active_paths) { 1949 add_active_path (null, p); 1950 } 1951 1952 redraw_area (0, 0, allocation.width, allocation.height); 1953 } 1954 1955 /** Split curve in two parts and add a new point in between. 1956 * @return the new point 1957 */ 1958 public void insert_new_point_on_path (double x, double y) { 1959 double min, distance; 1960 Path? p = null; 1961 Path path; 1962 EditPoint? np = null; 1963 EditPoint lep; 1964 1965 double xt; 1966 double yt; 1967 1968 xt = x * ivz () - xc () + view_offset_x; 1969 yt = yc () - y * ivz () - view_offset_y; 1970 1971 min = double.MAX; 1972 1973 foreach (Path pp in get_visible_paths ()) { 1974 lep = new EditPoint (); 1975 pp.get_closest_point_on_path (lep, xt, yt); 1976 distance = Math.sqrt (Math.pow (Math.fabs (xt - lep.x), 2) + Math.pow (Math.fabs (yt - lep.y), 2)); 1977 1978 if (distance < min) { 1979 min = distance; 1980 p = pp; 1981 np = lep; 1982 } 1983 } 1984 1985 if (p == null) { 1986 return; 1987 } 1988 1989 path = (!) p; 1990 1991 lep = new EditPoint (); 1992 path.get_closest_point_on_path (lep, xt, yt); 1993 path.insert_new_point_on_path (lep); 1994 } 1995 1996 static bool in_range (double offset_x, double coordinate_x1, double coordinate_x2) { 1997 return coordinate_x1 <= offset_x <= coordinate_x2; 1998 } 1999 2000 public void juxtapose (WidgetAllocation allocation, Context cr) { 2001 string glyph_sequence = Preferences.get ("glyph_sequence"); 2002 unichar c; 2003 Font font = BirdFont.get_current_font (); 2004 Glyph glyph = MainWindow.get_current_glyph (); 2005 Glyph juxtaposed; 2006 StringBuilder current = new StringBuilder (); 2007 int pos; 2008 string name; 2009 double x, kern; 2010 double left, baseline; 2011 string last_name; 2012 2013 double box_x1, box_x2, box_y1, box_y2; 2014 double marker_x, marker_y; 2015 2016 KerningClasses classes = font.get_kerning_classes (); 2017 2018 x = 0; 2019 2020 box_x1 = path_coordinate_x (0); 2021 box_y1 = path_coordinate_y (0); 2022 box_x2 = path_coordinate_x (allocation.width); 2023 box_y2 = path_coordinate_y (allocation.height); 2024 2025 current.append_unichar (glyph.unichar_code); 2026 pos = glyph_sequence.index_of (current.str); 2027 2028 baseline = font.base_line;; 2029 left = glyph.get_line ("left").pos; 2030 2031 x = glyph.get_width (); 2032 last_name = glyph.name; 2033 for (int i = pos + 1; i < glyph_sequence.char_count (); i++) { 2034 c = glyph_sequence.get_char (i); 2035 name = (!) c.to_string (); 2036 juxtaposed = (font.has_glyph (name)) ? (!) font.get_glyph (name) : font.get_space ().get_current (); 2037 2038 if (font.has_glyph (last_name) && font.has_glyph (name)) { 2039 kern = classes.get_kerning (last_name, name); 2040 } else { 2041 kern = 0; 2042 } 2043 2044 if (!juxtaposed.is_empty () 2045 && (in_range (left + x + kern, box_x1, box_x2) // the letter is visible 2046 || in_range (left + x + kern + juxtaposed.get_width (), box_x1, box_x2))) { 2047 2048 marker_x = Glyph.xc () + left + x + kern - glyph.view_offset_x; 2049 marker_y = Glyph.yc () - baseline - glyph.view_offset_y; 2050 2051 cr.save (); 2052 cr.scale (glyph.view_zoom, glyph.view_zoom); 2053 Theme.color (cr, "Foreground 1"); 2054 2055 Svg.draw_svg_path (cr, juxtaposed.get_svg_data (), marker_x, marker_y); 2056 cr.restore (); 2057 } 2058 2059 x += juxtaposed.get_width () + kern; 2060 2061 last_name = name; 2062 } 2063 2064 x = 0; 2065 last_name = glyph.name; 2066 for (int i = pos - 1; i >= 0; i--) { 2067 c = glyph_sequence.get_char (i); 2068 name = (!) c.to_string (); 2069 juxtaposed = (font.has_glyph (name)) ? (!) font.get_glyph (name) : font.get_space ().get_current (); 2070 2071 if (font.has_glyph (last_name) && font.has_glyph (name)) { 2072 kern = classes.get_kerning (name, last_name); 2073 } else { 2074 kern = 0; 2075 } 2076 2077 x -= juxtaposed.get_width (); 2078 x -= kern; 2079 2080 marker_x = Glyph.xc () + left + x; 2081 marker_y = Glyph.yc () - baseline; 2082 if (!juxtaposed.is_empty () 2083 &&(in_range (left + x, box_x1, box_x2) 2084 || in_range (left + x + juxtaposed.get_width (), box_x1, box_x2))) { 2085 cr.save (); 2086 cr.scale (glyph.view_zoom, glyph.view_zoom); 2087 cr.translate (-glyph.view_offset_x, -glyph.view_offset_y); 2088 Theme.color (cr, "Foreground 1"); 2089 Svg.draw_svg_path (cr, juxtaposed.get_svg_data (), marker_x, marker_y); 2090 cr.restore (); 2091 } 2092 2093 last_name = name; 2094 } 2095 } 2096 2097 /** @return left side bearing */ 2098 public double get_lsb () { 2099 return get_line ("left").pos; 2100 } 2101 2102 /** @return bottom line */ 2103 public double get_baseline () { 2104 Font font = BirdFont.get_current_font (); 2105 return font.base_line; 2106 } 2107 2108 void draw_background_glyph (WidgetAllocation allocation, Context cr) { 2109 double left, baseline, current_left; 2110 Glyph g; 2111 Font font = BirdFont.get_current_font (); 2112 2113 current_left = get_line ("left").pos; 2114 2115 if (background_glyph != null) { 2116 g = (!) background_glyph; 2117 baseline = font.base_line; 2118 left = g.get_line ("left").pos; 2119 cr.save (); 2120 cr.scale (view_zoom, view_zoom); 2121 cr.translate (-view_offset_x, -view_offset_y); 2122 Theme.color (cr, "Background Glyph"); 2123 2124 Svg.draw_svg_path (cr, g.get_svg_data (), 2125 Glyph.xc () + left - (left - current_left) , 2126 Glyph.yc () - baseline); 2127 cr.restore (); 2128 } 2129 2130 } 2131 2132 public string get_hex () { 2133 return Font.to_hex_code (unichar_code); 2134 } 2135 2136 public override void move_view (double x, double y) { 2137 view_offset_x += x / view_zoom; 2138 view_offset_y += y / view_zoom; 2139 GlyphCanvas.redraw (); 2140 } 2141 2142 /** Scroll or zoom from tap events. */ 2143 public void change_view_event (int finger, int x, int y) { 2144 double dx, dy; 2145 double last_distance, new_distance; 2146 2147 dx = 0; 2148 dy = 0; 2149 2150 new_distance = 0; 2151 2152 if (last_tap0_y == -1 || last_tap0_x == -1 || last_tap1_y == -1 || last_tap1_x == -1) { 2153 return; 2154 } 2155 2156 if (finger == 0) { 2157 dx = last_tap0_x - x; 2158 dy = last_tap0_y - y; 2159 new_distance = Path.distance (last_tap1_x, x, last_tap1_y, y); 2160 } 2161 2162 if (finger == 1) { 2163 dx = last_tap1_x - x; 2164 dy = last_tap1_y - y; 2165 new_distance = Path.distance (last_tap0_x, x, last_tap0_y, y); 2166 } 2167 2168 last_distance = Path.distance (last_tap0_x, last_tap1_x, last_tap0_y, last_tap1_y); 2169 2170 if (zoom_distance != 0) { 2171 zoom_tap (zoom_distance - new_distance); 2172 } 2173 2174 if (finger == 1) { 2175 warning (@"dx $dx dy $dy last_tap1_x $last_tap1_x last_tap1_y $last_tap1_y x $x y $y"); 2176 move_view (dx, dy); 2177 } 2178 2179 zoom_distance = new_distance; 2180 } 2181 2182 public override void tap_down (int finger, int x, int y) { 2183 TimeoutSource delay; 2184 2185 if (finger == 0) { 2186 delay = new TimeoutSource (400); // wait for second finger 2187 delay.set_callback(() => { 2188 if (!change_view && !ignore_input) { 2189 button_press (1, x, y); 2190 } 2191 return false; 2192 }); 2193 delay.attach (null); 2194 2195 last_tap0_x = x; 2196 last_tap0_y = y; 2197 } 2198 2199 if (finger == 1) { 2200 change_view = true; 2201 last_tap1_x = x; 2202 last_tap1_y = y; 2203 } 2204 } 2205 2206 public override void tap_up (int finger, int x, int y) { 2207 if (finger == 0) { 2208 button_release (1, x, y); 2209 2210 last_tap0_x = -1; 2211 last_tap0_y = -1; 2212 } 2213 2214 if (finger == 1) { 2215 last_tap1_x = -1; 2216 last_tap1_y = -1; 2217 2218 change_view = false; 2219 zoom_distance = 0; 2220 } 2221 } 2222 2223 public override void tap_move (int finger, int x, int y) { 2224 if (!change_view) { 2225 motion_notify (x, y); 2226 } else { 2227 change_view_event (finger, x, y); 2228 } 2229 2230 if (finger == 0) { 2231 last_tap0_x = x; 2232 last_tap0_y = y; 2233 } 2234 2235 if (finger == 1) { 2236 last_tap1_x = x; 2237 last_tap1_y = y; 2238 } 2239 } 2240 2241 public void update_spacing_class () { 2242 Font font = BirdFont.get_current_font (); 2243 GlyphCollection? g; 2244 GlyphCollection gc; 2245 Glyph glyph; 2246 Gee.ArrayList<string> s; 2247 SpacingData sd; 2248 2249 sd = font.get_spacing (); 2250 s = sd.get_all_connections ((!) unichar_code.to_string ()); 2251 foreach (string l in s) { 2252 if (l != (!) unichar_code.to_string ()) { 2253 g = font.get_glyph_collection (l); 2254 if (g != null) { 2255 gc = (!) g; 2256 glyph = gc.get_current (); 2257 2258 if (glyph.left_limit == glyph.right_limit) { 2259 warning ("Zero width glyph in kerning class."); 2260 } 2261 2262 left_limit = glyph.left_limit; 2263 right_limit = glyph.right_limit; 2264 2265 break; 2266 } 2267 } 2268 } 2269 2270 add_help_lines (); 2271 } 2272 2273 public void update_other_spacing_classes () { 2274 Font font = BirdFont.get_current_font (); 2275 GlyphCollection? g; 2276 GlyphCollection gc; 2277 Glyph glyph; 2278 Gee.ArrayList<string> s; 2279 SpacingData sd; 2280 2281 sd = font.get_spacing (); 2282 s = sd.get_all_connections ((!) unichar_code.to_string ()); 2283 2284 foreach (string l in s) { 2285 if (l != (!) unichar_code.to_string ()) { 2286 g = font.get_glyph_collection (l); 2287 if (g != null) { 2288 gc = (!) g; 2289 glyph = gc.get_current (); 2290 glyph.left_limit = left_limit; 2291 glyph.right_limit = right_limit; 2292 } 2293 } 2294 } 2295 } 2296 2297 public void set_cache (string key, Surface cache) { 2298 glyph_cache.set (key, cache); 2299 } 2300 2301 public bool has_cache (string key) { 2302 return glyph_cache.has_key (key); 2303 } 2304 2305 public Surface get_cache (string key) { 2306 if (unlikely (!has_cache (key))) { 2307 warning ("No cache for glyph."); 2308 return new ImageSurface (Cairo.Format.ARGB32, 1, 1); 2309 } 2310 2311 return glyph_cache.get (key); 2312 } 2313 2314 public void add_custom_guide () { 2315 TextListener listener; 2316 2317 listener = new TextListener (t_("Guide"), "", t_("Add")); 2318 2319 listener.signal_text_input.connect ((text) => { 2320 new_guide_name = text; 2321 }); 2322 2323 listener.signal_submit.connect (() => { 2324 Line guide; 2325 double position; 2326 2327 position = path_coordinate_y (allocation.height / 2.0); 2328 guide = new Line (new_guide_name, position); 2329 horizontal_help_lines.add (guide); 2330 2331 BirdFont.get_current_font ().custom_guides.add (guide); 2332 2333 TabContent.hide_text_input (); 2334 GlyphCanvas.redraw (); 2335 }); 2336 2337 TabContent.show_text_input (listener); 2338 } 2339 2340 public Path? get_last_path () { 2341 var paths = get_all_paths (); 2342 return_val_if_fail (paths.size > 0, null); 2343 return paths.get (paths.size - 1); 2344 } 2345 2346 public void move_layer_up () { 2347 Layer layer = get_current_layer (); 2348 2349 if (current_layer + 2 <= layers.subgroups.size) { 2350 return_if_fail (0 <= current_layer + 2 <= layers.subgroups.size); 2351 layers.subgroups.insert (current_layer + 2, layer); 2352 2353 return_if_fail (0 <= current_layer + 1 < layers.subgroups.size); 2354 layers.subgroups.remove_at (current_layer); 2355 2356 set_current_layer (layer); 2357 } 2358 } 2359 2360 public void move_layer_down () { 2361 Layer layer = get_current_layer (); 2362 2363 if (current_layer >= 1) { 2364 return_if_fail (0 <= current_layer - 1 < layers.subgroups.size); 2365 layers.subgroups.insert (current_layer - 1, layer); 2366 2367 return_if_fail (0 <= current_layer + 1 < layers.subgroups.size); 2368 layers.subgroups.remove_at (current_layer + 1); 2369 2370 set_current_layer (layer); 2371 } 2372 } 2373 } 2374 2375 } 2376