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