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