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