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