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