The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

OverView.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/OverView.vala.
Merge branch 'master' of github.com:johanmattssonm/birdfont
1 /* 2 Copyright (C) 2012 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 17 namespace BirdFont { 18 19 /** A display with all glyphs present in this font. */ 20 public class OverView : FontDisplay { 21 public WidgetAllocation allocation = new WidgetAllocation (); 22 23 public OverViewItem selected_item = new OverViewItem (null, '\0', 0, 0); 24 25 public Gee.ArrayList<GlyphCollection> copied_glyphs = new Gee.ArrayList<GlyphCollection> (); 26 public Gee.ArrayList<GlyphCollection> selected_items = new Gee.ArrayList<GlyphCollection> (); 27 28 int selected = 0; 29 int first_visible = 0; 30 int rows = 0; 31 int items_per_row = 0; 32 33 double view_offset_y = 0; 34 double view_offset_x = 0; 35 36 public signal void open_new_glyph_signal (unichar c); 37 public signal void open_glyph_signal (GlyphCollection c); 38 39 public GlyphRange glyph_range { 40 get { 41 return _glyph_range; 42 } 43 44 set { 45 _glyph_range = value; 46 } 47 } 48 49 GlyphRange _glyph_range; 50 51 string search_query = ""; 52 53 Gee.ArrayList<OverViewItem> visible_items = new Gee.ArrayList<OverViewItem> (); 54 55 /** List of undo commands. */ 56 public Gee.ArrayList<OverViewUndoItem> undo_items = new Gee.ArrayList<OverViewUndoItem> (); 57 public Gee.ArrayList<OverViewUndoItem> redo_items = new Gee.ArrayList<OverViewUndoItem> (); 58 59 /** Show all characters that has been drawn. */ 60 public bool all_available { 61 set { 62 _all_available = value; 63 update_item_list (); 64 } 65 66 get { 67 return _all_available; 68 } 69 } 70 71 bool _all_available = false; 72 73 /** Show unicode database info. */ 74 CharacterInfo? character_info = null; 75 76 double scroll_size = 1; 77 const double UCD_LINE_HEIGHT = 17 * 1.3; 78 79 public OverView (GlyphRange? range = null, 80 bool open_selected = true, bool default_character_set = true) { 81 82 GlyphRange gr; 83 84 if (range == null) { 85 gr = new GlyphRange (); 86 set_current_glyph_range (gr); 87 } 88 89 if (open_selected) { 90 this.open_glyph_signal.connect ((glyph_collection) => { 91 TabBar tabs = MainWindow.get_tab_bar (); 92 string n = glyph_collection.get_current ().name; 93 bool selected = tabs.select_char (n); 94 GlyphCanvas canvas; 95 Glyph g = glyph_collection.get_current (); 96 97 if (!selected) { 98 canvas = MainWindow.get_glyph_canvas (); 99 tabs.add_tab (g, true, glyph_collection); 100 canvas.set_current_glyph_collection (glyph_collection); 101 set_initial_zoom (); 102 PenTool.update_orientation (); 103 } 104 }); 105 106 this.open_new_glyph_signal.connect ((character) => { 107 // ignore control characters 108 if (character <= 0x1F) { 109 return; 110 } 111 112 create_new_glyph (character); 113 }); 114 } 115 116 if (default_character_set) { 117 IdleSource idle = new IdleSource (); 118 119 idle.set_callback (() => { 120 use_default_character_set (); 121 selected_canvas (); 122 return false; 123 }); 124 125 idle.attach (null); 126 } 127 128 update_item_list (); 129 update_scrollbar (); 130 reset_zoom (); 131 } 132 133 public void select_all_glyphs () { 134 Font f; 135 GlyphCollection? glyphs; 136 137 f = BirdFont.get_current_font (); 138 139 for (int index = 0; index < f.length (); index++) { 140 glyphs = f.get_glyph_collection_indice ((uint32) index); 141 return_if_fail (glyphs != null); 142 143 selected_items.add ((!) glyphs); 144 } 145 146 foreach (OverViewItem item in visible_items) { 147 item.selected = item.glyphs != null; 148 } 149 150 GlyphCanvas.redraw (); 151 } 152 153 public void use_default_character_set () { 154 GlyphRange gr = new GlyphRange (); 155 all_available = false; 156 DefaultCharacterSet.use_default_range (gr); 157 set_current_glyph_range (gr); 158 OverviewTools.update_overview_characterset (); 159 FontDisplay.dirty_scrollbar = true; 160 } 161 162 public GlyphCollection create_new_glyph (unichar character) { 163 StringBuilder name = new StringBuilder (); 164 TabBar tabs = MainWindow.get_tab_bar (); 165 bool selected; 166 Glyph glyph; 167 GlyphCollection glyph_collection = MainWindow.get_current_glyph_collection (); 168 GlyphCanvas canvas; 169 170 name.append_unichar (character); 171 selected = tabs.select_char (name.str); 172 173 if (!selected) { 174 glyph_collection = add_character_to_font (character); 175 176 glyph = glyph_collection.get_current (); 177 glyph.layers.add_layer (new Layer ()); 178 tabs.add_tab (glyph, true, glyph_collection); 179 180 selected_items.add (glyph_collection); 181 182 canvas = MainWindow.get_glyph_canvas (); 183 canvas.set_current_glyph_collection (glyph_collection); 184 185 set_initial_zoom (); 186 } else { 187 warning ("Glyph is already open"); 188 } 189 190 OverviewTools.update_overview_characterset (); 191 return glyph_collection; 192 } 193 194 public GlyphCollection add_empty_character_to_font (unichar character, bool unassigned, string name) { 195 return add_character_to_font (character, true, unassigned); 196 } 197 198 public GlyphCollection add_character_to_font (unichar character, bool empty = false, 199 bool unassigned = false, string glyph_name = "") { 200 StringBuilder name = new StringBuilder (); 201 Font font = BirdFont.get_current_font (); 202 GlyphCollection? fg; 203 Glyph glyph; 204 GlyphCollection glyph_collection; 205 206 if (glyph_name == "") { 207 name.append_unichar (character); 208 } else { 209 name.append (glyph_name); 210 } 211 212 if (all_available) { 213 fg = font.get_glyph_collection_by_name (name.str); 214 } else { 215 fg = font.get_glyph_collection (name.str); 216 } 217 218 if (fg != null) { 219 glyph_collection = (!) fg; 220 } else { 221 glyph_collection = new GlyphCollection (character, name.str); 222 223 if (!empty) { 224 glyph = new Glyph (name.str, (!unassigned) ? character : '\0'); 225 glyph_collection.insert_glyph (glyph, true); 226 } 227 228 font.add_glyph_collection (glyph_collection); 229 } 230 231 glyph_collection.set_unassigned (unassigned); 232 233 return glyph_collection; 234 } 235 236 public static void search () { 237 OverView ow = MainWindow.get_overview (); 238 TextListener listener = new TextListener (t_("Search"), ow.search_query, t_("Filter")); 239 240 listener.signal_text_input.connect ((text) => { 241 OverView o = MainWindow.get_overview (); 242 o.search_query = text; 243 }); 244 245 listener.signal_submit.connect (() => { 246 OverView o = MainWindow.get_overview (); 247 string q = o.search_query; 248 249 if (q.char_count () > 1) { 250 q = q.down (); 251 } 252 253 GlyphRange r = CharDatabase.search (q); 254 o.set_current_glyph_range (r); 255 MainWindow.get_tab_bar ().select_tab_name ("Overview"); 256 257 TextListener tl = new TextListener (t_("Search"), o.search_query, t_("Filter")); 258 TabContent.show_text_input (tl); 259 }); 260 261 TabContent.show_text_input (listener); 262 } 263 264 public Glyph? get_current_glyph () { 265 OverViewItem oi = selected_item; 266 if (oi.glyphs != null) { 267 return ((!) oi.glyphs).get_current (); 268 } 269 return null; 270 } 271 272 public void set_initial_zoom () { 273 Toolbox tools = MainWindow.get_toolbox (); 274 ZoomTool z = (ZoomTool) tools.get_tool ("zoom_tool"); 275 z.store_current_view (); 276 MainWindow.get_current_glyph ().default_zoom (); 277 z.store_current_view (); 278 OverViewItem.reset_label (); 279 } 280 281 public double get_height () { 282 double l; 283 Font f; 284 285 if (rows == 0) { 286 return 0; 287 } 288 289 if (all_available) { 290 f = BirdFont.get_current_font (); 291 l = f.length (); 292 } else { 293 l = glyph_range.length (); 294 } 295 296 return 2.0 * OverViewItem.height * (l / rows); 297 } 298 299 public bool selected_char_is_visible () { 300 return first_visible <= selected <= first_visible + items_per_row * rows; 301 } 302 303 public override bool has_scrollbar () { 304 return true; 305 } 306 307 public override void scroll_wheel (double x, double y, double pixeldelta, double dy) { 308 if (dy > 0) { 309 key_up (); 310 update_scrollbar (); 311 GlyphCanvas.redraw (); 312 hide_menu (); 313 314 selected_item = get_selected_item (); 315 selected_items.clear (); 316 if (selected_item.glyphs != null) { 317 selected_items.add ((!) selected_item.glyphs); 318 } 319 } else { 320 key_down (); 321 update_scrollbar (); 322 GlyphCanvas.redraw (); 323 hide_menu (); 324 325 selected_item = get_selected_item (); 326 selected_items.clear (); 327 if (selected_item.glyphs != null) { 328 selected_items.add ((!) selected_item.glyphs); 329 } 330 } 331 } 332 333 public override void selected_canvas () { 334 OverviewTools.update_overview_characterset (); 335 KeyBindings.set_require_modifier (true); 336 update_scrollbar (); 337 update_zoom_bar (); 338 OverViewItem.glyph_scale = 1; 339 update_item_list (); 340 selected_item = get_selected_item (); 341 GlyphCanvas.redraw (); 342 } 343 344 public void update_zoom_bar () { 345 double z = OverViewItem.width / OverViewItem.DEFAULT_WIDTH - 0.5; 346 Toolbox.overview_tools.zoom_bar.set_zoom (z); 347 Toolbox.redraw_tool_box (); 348 update_item_list (); 349 } 350 351 public void set_zoom (double zoom) { 352 double z = zoom + 0.5; 353 OverViewItem.glyph_scale = 1; 354 OverViewItem.width = OverViewItem.DEFAULT_WIDTH * z; 355 OverViewItem.height = OverViewItem.DEFAULT_HEIGHT * z; 356 OverViewItem.margin = OverViewItem.DEFAULT_MARGIN * z; 357 update_item_list (); 358 OverViewItem.reset_label (); 359 GlyphCanvas.redraw (); 360 } 361 362 public override void zoom_min () { 363 OverViewItem.width = OverViewItem.DEFAULT_WIDTH * 0.5; 364 OverViewItem.height = OverViewItem.DEFAULT_HEIGHT * 0.5; 365 OverViewItem.margin = OverViewItem.DEFAULT_MARGIN * 0.5; 366 update_item_list (); 367 OverViewItem.reset_label (); 368 GlyphCanvas.redraw (); 369 update_zoom_bar (); 370 } 371 372 public override void reset_zoom () { 373 OverViewItem.width = OverViewItem.DEFAULT_WIDTH; 374 OverViewItem.height = OverViewItem.DEFAULT_HEIGHT; 375 OverViewItem.margin = OverViewItem.DEFAULT_MARGIN; 376 update_item_list (); 377 OverViewItem.reset_label (); 378 GlyphCanvas.redraw (); 379 update_zoom_bar (); 380 } 381 382 public override void zoom_max () { 383 OverViewItem.width = allocation.width; 384 OverViewItem.height = allocation.height; 385 update_item_list (); 386 OverViewItem.reset_label (); 387 GlyphCanvas.redraw (); 388 } 389 390 public override void zoom_in () { 391 OverViewItem.width *= 1.1; 392 OverViewItem.height *= 1.1; 393 OverViewItem.margin *= 1.1; 394 update_item_list (); 395 OverViewItem.reset_label (); 396 GlyphCanvas.redraw (); 397 update_zoom_bar (); 398 } 399 400 public override void zoom_out () { 401 OverViewItem.width *= 0.9; 402 OverViewItem.height *= 0.9; 403 OverViewItem.margin *= 0.9; 404 update_item_list (); 405 OverViewItem.reset_label (); 406 GlyphCanvas.redraw (); 407 update_zoom_bar (); 408 } 409 410 public override void store_current_view () { 411 } 412 413 public override void restore_last_view () { 414 } 415 416 public override void next_view () { 417 } 418 419 public override string get_label () { 420 return t_("Overview"); 421 } 422 423 public override string get_name () { 424 return "Overview"; 425 } 426 427 public void display_all_available_glyphs () { 428 all_available = true; 429 430 first_visible = 0; 431 selected = 0; 432 433 update_item_list (); 434 selected_item = get_selected_item (); 435 GlyphCanvas.redraw (); 436 } 437 438 OverViewItem get_selected_item () { 439 if (visible_items.size == 0) { 440 return new OverViewItem (null, '\0', 0, 0); 441 } 442 443 if (unlikely (!(0 <= selected < visible_items.size))) { 444 warning (@"0 <= $selected < $(visible_items.size)"); 445 return new OverViewItem (null, '\0', 0, 0); 446 } 447 448 return visible_items.get (selected); 449 } 450 451 int get_items_per_row () { 452 int i = 1; 453 double tab_with = allocation.width; 454 OverViewItem.margin = OverViewItem.width * 0.1; 455 double l = OverViewItem.margin + OverViewItem.full_width (); 456 457 while (l <= tab_with) { 458 l += OverViewItem.full_width (); 459 i++; 460 } 461 462 return i - 1; 463 } 464 465 public void update_item_list (int item_list_length = -1) { 466 string character_string; 467 Font f = BirdFont.get_current_font (); 468 GlyphCollection? glyphs = null; 469 uint32 index; 470 OverViewItem item; 471 double x, y; 472 unichar character; 473 Glyph glyph; 474 double tab_with; 475 476 tab_with = allocation.width - 30; // scrollbar 477 478 items_per_row = get_items_per_row (); 479 rows = (int) (allocation.height / OverViewItem.full_height ()) + 2; 480 481 if (item_list_length == -1) { 482 item_list_length = items_per_row * rows; 483 } 484 485 visible_items.clear (); 486 visible_items = new Gee.ArrayList<OverViewItem> (); 487 488 // update item list 489 index = (uint32) first_visible; 490 x = OverViewItem.margin; 491 y = OverViewItem.margin; 492 for (int i = 0; i < item_list_length; i++) { 493 if (all_available) { 494 if (! (0 <= index < f.length ())) { 495 break; 496 } 497 498 glyphs = f.get_glyph_collection_indice ((uint32) index); 499 return_if_fail (glyphs != null); 500 501 glyph = ((!) glyphs).get_current (); 502 character_string = glyph.name; 503 character = glyph.unichar_code; 504 } else { 505 if (!(0 <= index < glyph_range.get_length ())) { 506 break; 507 } 508 509 character_string = glyph_range.get_char ((uint32) index); 510 glyphs = f.get_glyph_collection_by_name (character_string); 511 character = character_string.get_char (0); 512 } 513 514 item = new OverViewItem (glyphs, character, x, y); 515 item.adjust_scale (); 516 517 x += OverViewItem.full_width (); 518 519 if (x + OverViewItem.full_width () >= tab_with) { 520 x = OverViewItem.margin; 521 y += OverViewItem.full_height (); 522 } 523 524 item.selected = (i == selected); 525 526 if (glyphs != null) { 527 item.selected |= selected_items.index_of ((!) glyphs) != -1; 528 } 529 530 visible_items.add (item); 531 index++; 532 } 533 534 // offset 535 item = get_selected_item (); 536 if (item.y + OverViewItem.height + view_offset_y > allocation.height) { 537 view_offset_y = allocation.height - (item.y + OverViewItem.height); 538 } 539 540 if (item.y + view_offset_y < 0) { 541 view_offset_y = 0; 542 } 543 544 foreach (OverViewItem i in visible_items) { 545 i.y += view_offset_y; 546 i.x += view_offset_x; 547 } 548 } 549 550 public override void draw (WidgetAllocation allocation, Context cr) { 551 552 if (this.allocation.width == 0) { 553 this.allocation = allocation; 554 update_item_list (); 555 } 556 557 this.allocation = allocation; 558 559 // clear canvas 560 cr.save (); 561 Theme.color (cr, "Background 1"); 562 cr.rectangle (0, 0, allocation.width, allocation.height); 563 cr.fill (); 564 cr.restore (); 565 566 foreach (OverViewItem i in visible_items) { 567 i.draw (allocation, cr); 568 } 569 570 if (unlikely (visible_items.size == 0)) { 571 draw_empty_canvas (allocation, cr); 572 } 573 574 if (unlikely (character_info != null)) { 575 draw_character_info (cr); 576 } 577 } 578 579 void draw_empty_canvas (WidgetAllocation allocation, Context cr) { 580 Text t; 581 582 cr.save (); 583 t = new Text (t_("No glyphs in this view."), 24); 584 Theme.text_color (t, "Text Foreground"); 585 t.widget_x = 40; 586 t.widget_y = 30; 587 t.draw (cr); 588 cr.restore (); 589 } 590 591 public void scroll_rows (int row_adjustment) { 592 for (int i = 0; i < row_adjustment; i++) { 593 scroll (-OverViewItem.height); 594 } 595 596 for (int i = 0; i > row_adjustment; i--) { 597 scroll (OverViewItem.height); 598 } 599 } 600 601 public void scroll_adjustment (double pixel_adjustment) { 602 double l; 603 Font f; 604 605 if (all_available) { 606 f = BirdFont.get_current_font (); 607 l = f.length (); 608 } else { 609 l = glyph_range.length (); 610 } 611 612 if (first_visible <= 0) { 613 return; 614 } 615 616 if (first_visible + rows * items_per_row >= l) { 617 return; 618 } 619 620 scroll ((int64) pixel_adjustment); 621 } 622 623 void default_position () { 624 scroll_top (); 625 scroll_rows (1); 626 } 627 628 void scroll_to_position (int64 r) { 629 if (r < 0) { 630 scroll_top (); 631 return; 632 } 633 634 default_position (); 635 636 first_visible = (int) r; 637 update_item_list (); 638 } 639 640 public override void scroll_to (double position) requires (items_per_row > 0) { 641 double r; 642 int nrows; 643 Font f; 644 645 if (all_available) { 646 f = BirdFont.get_current_font (); 647 nrows = (int) (f.length () / items_per_row); 648 } else { 649 nrows = (int) (glyph_range.length () / items_per_row); 650 } 651 652 view_offset_y = 0; 653 r = (int64) (position * (nrows - rows + 3)); // 3 invisible rows 654 r *= items_per_row; 655 656 scroll_to_position ((int64) r); 657 update_item_list (); 658 GlyphCanvas.redraw (); 659 } 660 661 private void scroll (double pixel_adjustment) { 662 if (first_visible < 0 && pixel_adjustment < 0) { 663 scroll_top (); 664 return; 665 } 666 667 view_offset_y += pixel_adjustment; 668 669 if (view_offset_y >= 0) { 670 while (view_offset_y > OverViewItem.height) { 671 view_offset_y -= OverViewItem.height; 672 first_visible -= items_per_row; 673 } 674 675 first_visible -= items_per_row; 676 view_offset_y -= OverViewItem.height; 677 } else if (view_offset_y < -OverViewItem.height) { 678 view_offset_y = 0; 679 first_visible += items_per_row; 680 } 681 682 update_item_list (); 683 } 684 685 public void scroll_top () { 686 selected = 0; 687 first_visible = 0; 688 689 update_item_list (); 690 691 if (visible_items.size != 0) { 692 selected_item = get_selected_item (); 693 } 694 } 695 696 /** Returns true if the selected glyph is at the last row. */ 697 private bool last_row () { 698 return visible_items.size - selected <= items_per_row; 699 } 700 701 public void key_down () { 702 Font f = BirdFont.get_current_font (); 703 int64 len = (all_available) ? f.length () : glyph_range.length (); 704 705 if (at_bottom () && last_row ()) { 706 return; 707 } 708 709 selected += items_per_row; 710 711 if (selected >= items_per_row * rows) { 712 first_visible += items_per_row; 713 selected -= items_per_row; 714 } 715 716 if (first_visible + selected >= len) { 717 selected = (int) (len - first_visible - 1); 718 719 if (selected < items_per_row * (rows - 1)) { 720 first_visible -= items_per_row; 721 selected += items_per_row; 722 } 723 } 724 725 if (selected >= visible_items.size) { 726 selected = (int) (visible_items.size - 1); 727 } 728 729 selected_item = get_selected_item (); 730 update_item_list (); 731 } 732 733 public void key_right () { 734 Font f = BirdFont.get_current_font (); 735 int64 len = (all_available) ? f.length () : glyph_range.length (); 736 737 if (at_bottom () && first_visible + selected + 1 >= len) { 738 selected = (int) (visible_items.size - 1); 739 selected_item = get_selected_item (); 740 return; 741 } 742 743 selected += 1; 744 745 if (selected >= items_per_row * rows) { 746 first_visible += items_per_row; 747 selected -= items_per_row; 748 selected -= 1; 749 } 750 751 if (first_visible + selected > len) { 752 first_visible -= items_per_row; 753 selected = (int) (len - first_visible - 1); 754 selected_item = get_selected_item (); 755 } 756 update_item_list (); 757 } 758 759 public void key_up () { 760 selected -= items_per_row; 761 762 if (selected < 0) { 763 first_visible -= items_per_row; 764 selected += items_per_row; 765 } 766 767 if (first_visible < 0) { 768 first_visible = 0; 769 } 770 update_item_list (); 771 } 772 773 public void key_left () { 774 selected -= 1; 775 776 if (selected < 0) { 777 first_visible -= items_per_row; 778 selected += items_per_row; 779 selected += 1; 780 } 781 782 if (first_visible < 0) { 783 scroll_top (); 784 } 785 update_item_list (); 786 } 787 788 public string get_selected_char () { 789 Font f; 790 Glyph? g; 791 792 if (all_available) { 793 f = BirdFont.get_current_font (); 794 g = f.get_glyph_indice (selected); 795 return_val_if_fail (g != null, "".dup ()); 796 return ((!) g).get_name (); 797 } 798 799 return glyph_range.get_char (selected); 800 } 801 802 public override void key_press (uint keyval) { 803 hide_menu (); 804 update_item_list (); 805 GlyphCanvas.redraw (); 806 807 if (KeyBindings.modifier == CTRL) { 808 return; 809 } 810 811 switch (keyval) { 812 case Key.ENTER: 813 open_current_glyph (); 814 return; 815 816 case Key.UP: 817 key_up (); 818 selected_item = get_selected_item (); 819 820 selected_items.clear (); 821 if (selected_item.glyphs != null) { 822 selected_items.add ((!) selected_item.glyphs); 823 } 824 825 update_scrollbar (); 826 return; 827 828 case Key.RIGHT: 829 key_right (); 830 selected_item = get_selected_item (); 831 832 selected_items.clear (); 833 if (selected_item.glyphs != null) { 834 selected_items.add ((!) selected_item.glyphs); 835 } 836 837 update_scrollbar (); 838 return; 839 840 case Key.LEFT: 841 key_left (); 842 selected_item = get_selected_item (); 843 844 selected_items.clear (); 845 if (selected_item.glyphs != null) { 846 selected_items.add ((!) selected_item.glyphs); 847 } 848 849 update_scrollbar(); 850 return; 851 852 case Key.DOWN: 853 key_down (); 854 selected_item = get_selected_item (); 855 856 selected_items.clear (); 857 if (selected_item.glyphs != null) { 858 selected_items.add ((!) selected_item.glyphs); 859 } 860 861 update_scrollbar (); 862 return; 863 864 case Key.PG_UP: 865 for (int i = 0; i < rows; i++) { 866 key_up (); 867 } 868 selected_item = get_selected_item (); 869 870 selected_items.clear (); 871 if (selected_item.glyphs != null) { 872 selected_items.add ((!) selected_item.glyphs); 873 } 874 875 update_scrollbar (); 876 return; 877 878 case Key.PG_DOWN: 879 for (int i = 0; i < rows; i++) { 880 key_down (); 881 } 882 selected_item = get_selected_item (); 883 884 selected_items.clear (); 885 if (selected_item.glyphs != null) { 886 selected_items.add ((!) selected_item.glyphs); 887 } 888 889 update_scrollbar (); 890 return; 891 892 case Key.DEL: 893 delete_selected_glyph (); 894 selected_item = get_selected_item (); 895 return; 896 897 case Key.BACK_SPACE: 898 delete_selected_glyph (); 899 selected_item = get_selected_item (); 900 return; 901 } 902 903 if (!KeyBindings.has_ctrl () && !KeyBindings.has_logo ()) { 904 scroll_to_char (keyval); 905 } 906 907 selected_item = get_selected_item (); 908 909 selected_items.clear (); 910 if (selected_item.glyphs != null) { 911 selected_items.add ((!) selected_item.glyphs); 912 } 913 914 update_item_list (); 915 GlyphCanvas.redraw (); 916 } 917 918 public void delete_selected_glyph () { 919 Font font = BirdFont.get_current_font (); 920 OverViewUndoItem undo_item = new OverViewUndoItem (); 921 922 undo_item.alternate_sets = font.alternates.copy (); 923 924 foreach (GlyphCollection g in selected_items) { 925 undo_item.glyphs.add (g.copy ()); 926 } 927 store_undo_items (undo_item); 928 929 foreach (GlyphCollection gc in selected_items) { 930 font.delete_glyph (gc); 931 } 932 933 update_item_list (); 934 GlyphCanvas.redraw (); 935 } 936 937 public override void undo () { 938 Font font = BirdFont.get_current_font (); 939 OverViewUndoItem previous_collection; 940 941 if (undo_items.size == 0) { 942 return; 943 } 944 945 previous_collection = undo_items.get (undo_items.size - 1); 946 redo_items.add (get_current_state (previous_collection)); 947 948 // remove the old glyph and add the new one 949 foreach (GlyphCollection g in previous_collection.glyphs) { 950 font.delete_glyph (g); 951 952 if (g.length () > 0) { 953 font.add_glyph_collection (g); 954 } 955 } 956 957 Font f = BirdFont.get_current_font (); 958 f.alternates = previous_collection.alternate_sets.copy (); 959 960 undo_items.remove_at (undo_items.size - 1); 961 GlyphCanvas.redraw (); 962 } 963 964 public override void redo () { 965 Font font = BirdFont.get_current_font (); 966 OverViewUndoItem previous_collection; 967 968 if (redo_items.size == 0) { 969 return; 970 } 971 972 previous_collection = redo_items.get (redo_items.size - 1); 973 undo_items.add (get_current_state (previous_collection)); 974 975 // remove the old glyph and add the new one 976 foreach (GlyphCollection g in previous_collection.glyphs) { 977 font.delete_glyph (g); 978 font.add_glyph_collection (g); 979 } 980 981 font.alternates = previous_collection.alternate_sets.copy (); 982 983 redo_items.remove_at (redo_items.size - 1); 984 GlyphCanvas.redraw (); 985 } 986 987 public OverViewUndoItem get_current_state (OverViewUndoItem previous_collection) { 988 GlyphCollection? gc; 989 OverViewUndoItem ui = new OverViewUndoItem (); 990 Font font = BirdFont.get_current_font (); 991 992 ui.alternate_sets = font.alternates.copy (); 993 994 foreach (GlyphCollection g in previous_collection.glyphs) { 995 gc = font.get_glyph_collection (g.get_name ()); 996 997 if (gc != null) { 998 ui.glyphs.add (((!) gc).copy ()); 999 } else { 1000 ui.glyphs.add (new GlyphCollection (g.get_unicode_character (), g.get_name ())); 1001 } 1002 } 1003 1004 return ui; 1005 } 1006 1007 public void store_undo_state (GlyphCollection gc) { 1008 OverViewUndoItem i = new OverViewUndoItem (); 1009 Font f = BirdFont.get_current_font (); 1010 i.alternate_sets = f.alternates.copy (); 1011 i.glyphs.add (gc); 1012 store_undo_items (i); 1013 } 1014 1015 public void store_undo_items (OverViewUndoItem i) { 1016 undo_items.add (i); 1017 redo_items.clear (); 1018 } 1019 1020 bool select_visible_glyph (string name) { 1021 int i = 0; 1022 1023 foreach (OverViewItem o in visible_items) { 1024 if (o.get_name () == name) { 1025 selected = i; 1026 selected_item = get_selected_item (); 1027 return true; 1028 } 1029 1030 if (i > 1000) { 1031 warning ("selected character not found"); 1032 return true; 1033 } 1034 1035 i++; 1036 } 1037 1038 return false; 1039 } 1040 1041 public void scroll_to_char (unichar c) { 1042 StringBuilder s = new StringBuilder (); 1043 1044 if (is_modifier_key (c)) { 1045 return; 1046 } 1047 1048 s.append_unichar (c); 1049 scroll_to_glyph (s.str); 1050 } 1051 1052 public void scroll_to_glyph (string name) { 1053 GlyphRange gr = glyph_range; 1054 int i, r, index; 1055 string ch; 1056 Font font = BirdFont.get_current_font (); 1057 GlyphCollection? glyphs = null; 1058 Glyph glyph; 1059 1060 index = -1; 1061 1062 if (items_per_row <= 0) { 1063 return; 1064 } 1065 1066 ch = name; 1067 1068 // selected char is visible 1069 if (select_visible_glyph (ch)) { 1070 return; 1071 } 1072 1073 // scroll to char 1074 if (all_available) { 1075 1076 // don't search for glyphs in huge CJK fonts 1077 if (font.length () > 300) { 1078 r = 0; 1079 } else { 1080 // FIXME: too slow 1081 for (r = 0; r < font.length (); r += items_per_row) { 1082 for (i = 0; i < items_per_row; i++) { 1083 glyphs = font.get_glyph_collection_indice ((uint32) r + i); 1084 return_if_fail (glyphs != null); 1085 glyph = ((!) glyphs).get_current (); 1086 1087 if (glyph.name == ch) { 1088 index = i; 1089 } 1090 } 1091 1092 if (index > -1) { 1093 break; 1094 } 1095 } 1096 } 1097 } else { 1098 1099 if (ch.char_count () > 1) { 1100 warning ("Can't scroll to ligature in this view"); 1101 return; 1102 } 1103 1104 for (r = 0; r < gr.length (); r += items_per_row) { 1105 for (i = 0; i < items_per_row; i++) { 1106 if (gr.get_char (r + i) == ch) { 1107 index = i; 1108 } 1109 } 1110 1111 if (index > -1) { 1112 break; 1113 } 1114 } 1115 } 1116 1117 if (index > -1) { 1118 first_visible = r; 1119 update_item_list (); 1120 select_visible_glyph (ch); 1121 } 1122 } 1123 1124 public override void double_click (uint button, double ex, double ey) 1125 requires (!is_null (visible_items) && !is_null (allocation)) { 1126 1127 return_if_fail (!is_null (this)); 1128 1129 foreach (OverViewItem i in visible_items) { 1130 if (i.double_click (button, ex, ey)) { 1131 open_overview_item (i); 1132 } 1133 } 1134 1135 GlyphCanvas.redraw (); 1136 } 1137 1138 public void open_overview_item (OverViewItem i) { 1139 if (i.glyphs != null) { 1140 open_glyph_signal ((!) i.glyphs); 1141 ((!) i.glyphs).get_current ().close_path (); 1142 } else { 1143 open_new_glyph_signal (i.character); 1144 } 1145 } 1146 1147 public void set_character_info (CharacterInfo i) { 1148 character_info = i; 1149 } 1150 1151 public int get_selected_index () { 1152 GlyphCollection gc; 1153 int index = 0; 1154 1155 if (selected_items.size == 0) { 1156 return 0; 1157 } 1158 1159 gc = selected_items.get (0); 1160 1161 foreach (OverViewItem i in visible_items) { 1162 if (i.glyphs != null && gc == ((!) i.glyphs)) { 1163 break; 1164 } 1165 1166 index++; 1167 } 1168 1169 return index; 1170 } 1171 1172 public void hide_menu () { 1173 foreach (OverViewItem i in visible_items) { 1174 i.hide_menu (); 1175 } 1176 } 1177 1178 public override void button_press (uint button, double x, double y) { 1179 OverViewItem i; 1180 int index = 0; 1181 int selected_index = -1; 1182 bool update = false; 1183 1184 if (character_info != null) { 1185 character_info = null; 1186 GlyphCanvas.redraw (); 1187 return; 1188 } 1189 1190 for (int j = 0; j < visible_items.size; j++) { 1191 i = visible_items.get (j); 1192 1193 if (i.click (button, x, y)) { 1194 selected = index; 1195 selected_item = get_selected_item (); 1196 1197 if (KeyBindings.has_shift ()) { 1198 if (selected_item.glyphs != null) { 1199 1200 selected_index = selected_items.index_of ((!) selected_item.glyphs); 1201 if (selected_index == -1) { 1202 selected_items.add ((!) selected_item.glyphs); 1203 } else { 1204 return_if_fail (0 <= selected_index < selected_items.size); 1205 selected_items.remove_at (selected_index); 1206 selected = get_selected_index (); 1207 selected_item = get_selected_item (); 1208 } 1209 } 1210 } else { 1211 selected_items.clear (); 1212 if (selected_item.glyphs != null) { 1213 selected_items.add ((!) selected_item.glyphs); 1214 } 1215 } 1216 1217 update = !i.version_menu.menu_visible; 1218 } 1219 index++; 1220 } 1221 1222 if (update) { 1223 update_item_list (); 1224 } 1225 1226 GlyphCanvas.redraw (); 1227 } 1228 1229 /** Returns true if overview shows the last character. */ 1230 private bool at_bottom () { 1231 Font f; 1232 double t = rows * items_per_row + first_visible; 1233 1234 if (all_available) { 1235 f = BirdFont.get_current_font (); 1236 return t >= f.length (); 1237 } 1238 1239 return t >= glyph_range.length (); 1240 } 1241 1242 public void set_current_glyph_range (GlyphRange range) { 1243 GlyphRange? current = glyph_range; 1244 string c; 1245 1246 if (current != null) { 1247 c = glyph_range.get_char (selected); 1248 } 1249 1250 all_available = false; 1251 1252 glyph_range = range; 1253 scroll_top (); 1254 1255 // TODO: scroll down to c 1256 update_item_list (); 1257 selected_item = get_selected_item (); 1258 1259 GlyphCanvas.redraw (); 1260 } 1261 1262 public void select_next_glyph () { 1263 key_right (); 1264 } 1265 1266 public void open_current_glyph () { 1267 open_overview_item (selected_item); 1268 } 1269 1270 public override void update_scrollbar () { 1271 Font f; 1272 double nrows = 0; 1273 double pos = 0; 1274 double size; 1275 double visible_rows; 1276 1277 if (rows == 0) { 1278 MainWindow.set_scrollbar_size (0); 1279 MainWindow.set_scrollbar_position (0); 1280 } else { 1281 if (all_available) { 1282 f = BirdFont.get_current_font (); 1283 nrows = Math.floor ((f.length ()) / rows); 1284 size = f.length (); 1285 } else { 1286 nrows = Math.floor ((glyph_range.length ()) / rows); 1287 size = glyph_range.length (); 1288 } 1289 1290 if (nrows <= 0) { 1291 nrows = 1; 1292 } 1293 1294 visible_rows = allocation.height / OverViewItem.height; 1295 scroll_size = visible_rows / nrows; 1296 MainWindow.set_scrollbar_size (scroll_size); 1297 pos = first_visible / (nrows * items_per_row - visible_rows * items_per_row); 1298 MainWindow.set_scrollbar_position (pos); 1299 } 1300 } 1301 1302 /** Display one entry from the Unicode Character Database. */ 1303 void draw_character_info (Context cr) 1304 requires (character_info != null) { 1305 double x, y, w, h; 1306 int i; 1307 string unicode_value, unicode_description; 1308 string[] column; 1309 string entry; 1310 int len = 0; 1311 int length = 0; 1312 bool see_also = false; 1313 WidgetAllocation allocation = MainWindow.get_overview ().allocation; 1314 string name; 1315 string[] lines; 1316 double character_start; 1317 double character_height; 1318 1319 entry = ((!)character_info).get_entry (); 1320 lines = entry.split ("\n"); 1321 1322 foreach (string line in entry.split ("\n")) { 1323 len = line.char_count (); 1324 if (len > length) { 1325 length = len; 1326 } 1327 } 1328 1329 x = allocation.width * 0.1; 1330 y = allocation.height * 0.1; 1331 w = allocation.width * 0.9 - x; 1332 h = allocation.height * 0.9 - y; 1333 1334 if (w < 8 * length) { 1335 w = 8 * length; 1336 x = (allocation.width - w) / 2.0; 1337 } 1338 1339 if (x < 0) { 1340 x = 2; 1341 } 1342 1343 // background 1344 cr.save (); 1345 Theme.color_opacity (cr, "Background 1", 0.98); 1346 cr.rectangle (x, y, w, h); 1347 cr.fill (); 1348 cr.restore (); 1349 1350 cr.save (); 1351 Theme.color_opacity (cr, "Foreground 1", 0.98); 1352 cr.set_line_width (2); 1353 cr.rectangle (x, y, w, h); 1354 cr.stroke (); 1355 cr.restore (); 1356 1357 // database entry 1358 1359 if (((!)character_info).is_ligature ()) { 1360 name = ((!)character_info).get_name (); 1361 draw_info_line (name, cr, x, y, 0); 1362 } else { 1363 i = 0; 1364 foreach (string line in lines) { 1365 if (i == 0) { 1366 column = line.split ("\t"); 1367 return_if_fail (column.length == 2); 1368 unicode_value = "U+" + column[0]; 1369 unicode_description = column[1]; 1370 1371 draw_info_line (unicode_description, cr, x, y, i); 1372 i++; 1373 1374 draw_info_line (unicode_value, cr, x, y, i); 1375 i++; 1376 } else { 1377 1378 if (line.has_prefix ("\t*")) { 1379 draw_info_line (line.replace ("\t*", "•"), cr, x, y, i); 1380 i++; 1381 } else if (line.has_prefix ("\tx (")) { 1382 if (!see_also) { 1383 i++; 1384 draw_info_line (t_("See also:"), cr, x, y, i); 1385 i++; 1386 see_also = true; 1387 } 1388 1389 draw_info_line (line.replace ("\tx (", "•").replace (")", ""), cr, x, y, i); 1390 i++; 1391 } else { 1392 i++; 1393 } 1394 } 1395 } 1396 1397 character_start = y + 10 + i * UCD_LINE_HEIGHT; 1398 character_height = h - character_start; 1399 draw_fallback_character (cr, x, character_start, character_height); 1400 } 1401 } 1402 1403 /** Fallback character in UCD info. */ 1404 void draw_fallback_character (Context cr, double x, double y, double height) 1405 requires (character_info != null) { 1406 unichar c = ((!)character_info).unicode; 1407 1408 cr.save (); 1409 Text character = new Text (); 1410 Theme.text_color (character, "Foreground 1"); 1411 character.set_text ((!) c.to_string ()); 1412 character.set_font_size (height); 1413 character.draw_at_top (cr, x + 10, y); 1414 cr.restore (); 1415 } 1416 1417 void draw_info_line (string line, Context cr, double x, double y, int row) { 1418 Text ucd_entry = new Text (line); 1419 cr.save (); 1420 Theme.text_color (ucd_entry, "Foreground 1"); 1421 ucd_entry.widget_x = 10 + x; 1422 ucd_entry.widget_y = 10 + y + row * UCD_LINE_HEIGHT; 1423 ucd_entry.draw (cr); 1424 cr.restore (); 1425 } 1426 1427 public void paste () { 1428 GlyphCollection gc; 1429 GlyphCollection? c; 1430 Glyph glyph; 1431 uint32 index; 1432 int i; 1433 int skip = 0; 1434 int s; 1435 string character_string; 1436 Gee.ArrayList<GlyphCollection> glyps; 1437 Font f; 1438 OverViewUndoItem undo_item; 1439 1440 f = BirdFont.get_current_font (); 1441 gc = new GlyphCollection ('\0', ""); 1442 glyps = new Gee.ArrayList<GlyphCollection> (); 1443 1444 copied_glyphs.sort ((a, b) => { 1445 return (int) ((GlyphCollection) a).get_unicode_character () 1446 - (int) ((GlyphCollection) b).get_unicode_character (); 1447 }); 1448 1449 index = (uint32) first_visible + selected; 1450 for (i = 0; i < copied_glyphs.size; i++) { 1451 if (all_available) { 1452 if (f.length () == 0) { 1453 c = add_empty_character_to_font (copied_glyphs.get (i).get_unicode_character (), 1454 copied_glyphs.get (i).is_unassigned (), 1455 copied_glyphs.get (i).get_name ()); 1456 } else if (index >= f.length ()) { 1457 // FIXME: duplicated unicodes? 1458 c = add_empty_character_to_font (copied_glyphs.get (i).get_unicode_character (), 1459 copied_glyphs.get (i).is_unassigned (), 1460 copied_glyphs.get (i).get_name ()); 1461 } else { 1462 c = f.get_glyph_collection_indice ((uint32) index); 1463 } 1464 1465 if (c == null) { 1466 c = add_empty_character_to_font (copied_glyphs.get (i).get_unicode_character (), 1467 copied_glyphs.get (i).is_unassigned (), 1468 copied_glyphs.get (i).get_name ()); 1469 } 1470 1471 return_if_fail (c != null); 1472 gc = (!) c; 1473 } else { 1474 if (i != 0) { 1475 s = (int) copied_glyphs.get (i).get_unicode_character (); 1476 s -= (int) copied_glyphs.get (i - 1).get_unicode_character (); 1477 s -= 1; 1478 skip += s; 1479 } 1480 1481 character_string = glyph_range.get_char ((uint32) (index + skip)); 1482 c = f.get_glyph_collection_by_name (character_string); 1483 1484 if (c == null) { 1485 gc = add_empty_character_to_font (character_string.get_char (), 1486 copied_glyphs.get (i).is_unassigned (), 1487 copied_glyphs.get (i).get_name ()); 1488 } else { 1489 gc = (!) c; 1490 } 1491 } 1492 1493 glyps.add (gc); 1494 index++; 1495 } 1496 1497 undo_item = new OverViewUndoItem (); 1498 undo_item.alternate_sets = f.alternates.copy (); 1499 foreach (GlyphCollection g in glyps) { 1500 undo_item.glyphs.add (g.copy ()); 1501 } 1502 store_undo_items (undo_item); 1503 1504 if (glyps.size != copied_glyphs.size) { 1505 warning ("glyps.size != copied_glyphs.size"); 1506 return; 1507 } 1508 1509 if (copied_glyphs.size < i) { 1510 warning ("Array index out of bounds."); 1511 return; 1512 } 1513 1514 i = 0; 1515 foreach (GlyphCollection g in glyps) { 1516 glyph = copied_glyphs.get (i).get_current ().copy (); 1517 glyph.version_id = (glyph.version_id == -1 || g.length () == 0) ? 1 : g.get_last_id () + 1; 1518 glyph.unichar_code = g.get_unicode_character (); 1519 1520 if (!g.is_unassigned ()) { 1521 glyph.name = (!) glyph.unichar_code.to_string (); 1522 } else { 1523 glyph.name = g.get_name (); 1524 } 1525 1526 g.insert_glyph (glyph, true); 1527 i++; 1528 } 1529 1530 f.touch (); 1531 } 1532 1533 public class OverViewUndoItem { 1534 public AlternateSets alternate_sets = new AlternateSets (); 1535 public Gee.ArrayList<GlyphCollection> glyphs = new Gee.ArrayList<GlyphCollection> (); 1536 } 1537 } 1538 1539 } 1540