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