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