The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

MenuTab.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/MenuTab.vala.
Store font size in kerning display
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 namespace BirdFont { 16 17 public class MenuTab : FontDisplay { 18 19 /** Ignore input events when the background thread is running. 20 * 21 * Do always check the return value of set_suppress_event when this 22 * variable is updated. 23 * 24 * This variable is used only in the gui thread. 25 */ 26 public static bool suppress_event; 27 28 /** True if the background thread is running. */ 29 public static bool background_thread; 30 31 /** A notification sent when the file has been saved. */ 32 public static SaveCallback save_callback; 33 34 /** A notification sent when the file has been loaded. */ 35 public static LoadCallback load_callback; 36 37 /** A notification sent when the file has been loaded. */ 38 public static ExportCallback export_callback; 39 40 public MenuTab () { 41 save_callback = new SaveCallback (); 42 save_callback.is_done = true; 43 44 load_callback = new LoadCallback (); 45 export_callback = new ExportCallback (); 46 47 suppress_event = false; 48 background_thread = false; 49 } 50 51 public static void set_save_callback (SaveCallback c) { 52 if (!save_callback.is_done) { 53 warning ("Prevoius save command has not finished"); 54 } 55 56 save_callback = c; 57 } 58 59 public static void start_background_thread () { 60 if (!set_suppress_event (true)) { 61 warning ("suppressed event"); 62 return; 63 } 64 65 TabBar.start_wheel (); 66 } 67 68 public static void stop_background_thread () { 69 IdleSource idle = new IdleSource (); 70 idle.set_callback (() => { 71 set_suppress_event (false); 72 TabBar.stop_wheel (); 73 GlyphCanvas.redraw (); 74 return false; 75 }); 76 idle.attach (null); 77 } 78 79 public static bool validate_metadata () { 80 Font font = BirdFont.get_current_font (); 81 string m = t_("Missing metadata in font:") + "\n"; 82 83 if (font.postscript_name == "") { 84 MainWindow.show_message (m + t_("PostScript Name")); 85 return false; 86 } 87 88 if (font.name == "") { 89 MainWindow.show_message (m + t_("Name")); 90 return false; 91 } 92 93 if (font.subfamily == "") { 94 MainWindow.show_message (m + t_("Style")); 95 return false; 96 } 97 98 if (font.full_name == "") { 99 MainWindow.show_message (m + t_("Full Name (Name and Style)")); 100 return false; 101 } 102 103 if (font.unique_identifier == "") { 104 MainWindow.show_message (m + t_("Unique Identifier")); 105 return false; 106 } 107 108 Font current_font = BirdFont.get_current_font (); 109 string ttf_name = ExportSettings.get_file_name (current_font) + ".ttf"; 110 string ttf_name_mac = ExportSettings.get_file_name_mac (current_font) + ".ttf"; 111 112 print (@"$ttf_name == $ttf_name_mac"); 113 if (ttf_name == ttf_name_mac) { 114 MainWindow.show_message (t_("You need to choose a different name for the TTF file with Mac adjustmets.")); 115 ttf_name_mac = ExportSettings.get_file_name_mac (current_font) + " Mac.ttf"; 116 return false; 117 } 118 119 return true; 120 } 121 122 public static void export_fonts_in_background () { 123 Font f; 124 125 if (suppress_event || !MainWindow.native_window.can_export ()) { 126 return; 127 } 128 129 f = BirdFont.get_current_font (); 130 131 if (f.font_file == null) { 132 MainWindow.show_message (t_("You need to save your font before exporting it.")); 133 return; 134 } 135 136 if (!validate_metadata ()) { 137 return; 138 } 139 140 if (!ExportSettings.has_export_settings (f)) { 141 show_export_settings_tab (); 142 } else { 143 MenuTab.export_callback = new ExportCallback (); 144 MenuTab.export_callback.export_fonts_in_background (); 145 } 146 } 147 148 public static bool set_suppress_event (bool e) { 149 if (suppress_event && e) { 150 warning ("suppress_event is already set"); 151 return false; 152 } 153 background_thread = e; 154 suppress_event = e; 155 156 // key up for all modifiers will be ignored if events are suppressed 157 if (suppress_event) { 158 KeyBindings.reset (); 159 } 160 161 return true; 162 } 163 164 public override string get_label () { 165 return t_("Menu"); 166 } 167 168 public override string get_name () { 169 return "Menu"; 170 } 171 172 public static void select_overview () { 173 if (suppress_event) { 174 warn_if_test ("Event suppressed"); 175 return; 176 } 177 178 if (BirdFont.get_current_font ().is_empty ()) { 179 show_default_characters (); 180 } else { 181 MainWindow.get_tab_bar ().add_unique_tab (new OverView ()); 182 MainWindow.get_tab_bar ().select_tab_name ("Overview"); 183 } 184 } 185 186 public static void signal_file_exported () { 187 IdleSource idle = new IdleSource (); 188 idle.set_callback (() => { 189 export_callback.file_exported (); 190 return false; 191 }); 192 idle.attach (null); 193 } 194 195 public static void signal_file_saved () { 196 IdleSource idle = new IdleSource (); 197 idle.set_callback (() => { 198 save_callback.file_saved (); 199 return false; 200 }); 201 idle.attach (null); 202 } 203 204 public static void signal_file_loaded () { 205 IdleSource idle = new IdleSource (); 206 idle.set_callback (() => { 207 load_callback.file_loaded (); 208 MainWindow.native_window.font_loaded (); 209 return false; 210 }); 211 idle.attach (null); 212 } 213 214 public static void apply_font_setting (Font f) { 215 DrawingTools.background_scale.set_value (f.background_scale); 216 217 DrawingTools.grid_expander.tool.clear (); 218 219 if (f.grid_width.size == 0) { 220 f.grid_width.add ("1"); 221 f.grid_width.add ("2"); 222 f.grid_width.add ("4"); 223 } 224 225 foreach (string grid in f.grid_width) { 226 DrawingTools.add_new_grid (double.parse (grid), false); 227 } 228 229 string sw = f.settings.get_setting ("stroke_width"); 230 if (sw != ""){ 231 StrokeTool.stroke_width = double.parse (sw); 232 DrawingTools.object_stroke.set_value_round (StrokeTool.stroke_width); 233 } 234 235 string pt = f.settings.get_setting ("point_type"); 236 DrawingTools.set_default_point_type (pt); 237 238 string stroke = f.settings.get_setting ("apply_stroke"); 239 bool s = bool.parse (stroke); 240 DrawingTools.add_stroke.set_selected (s); 241 StrokeTool.add_stroke = s; 242 243 string lc = f.settings.get_setting ("line_cap"); 244 245 if (lc == "butt") { 246 StrokeTool.line_cap = LineCap.BUTT; 247 } else if (lc == "square") { 248 StrokeTool.line_cap = LineCap.SQUARE; 249 } else if (lc == "round") { 250 StrokeTool.line_cap = LineCap.ROUND; 251 } 252 253 DrawingTools.set_stroke_tool_visibility (); 254 255 string lock_grid = f.settings.get_setting ("lock_grid"); 256 bool lg = bool.parse (lock_grid); 257 GridTool.lock_grid = lg; 258 DrawingTools.lock_grid.selected = GridTool.lock_grid; 259 260 string skew_overview = f.settings.get_setting ("skew_overview"); 261 if (skew_overview != "") { 262 double so = double.parse (skew_overview); 263 OverviewTools.skew.set_value_round (so); 264 } 265 266 string autotrace_resolution = f.settings.get_setting ("autotrace_resolution"); 267 if (autotrace_resolution != "") { 268 double ar = double.parse (autotrace_resolution); 269 DrawingTools.background_threshold.set_value_round (ar); 270 } 271 272 string autotrace_threshold = f.settings.get_setting ("autotrace_threshold"); 273 if (autotrace_threshold != "") { 274 double at = double.parse (autotrace_threshold); 275 DrawingTools.auto_trace_resolution.set_value_round (at); 276 } 277 278 string autotrace_simplification = f.settings.get_setting ("autotrace_simplification"); 279 if (autotrace_simplification != "") { 280 double asi = double.parse (autotrace_simplification); 281 DrawingTools.auto_trace_simplify.set_value_round (asi); 282 } 283 284 string kerning_zoom = f.settings.get_setting ("kerning_zoom"); 285 if (kerning_zoom != "") { 286 double k = double.parse (kerning_zoom); 287 if (!is_null (KerningTools.zoom_bar)) { 288 KerningTools.zoom_bar.zoom_level = k; 289 KerningTools.zoom_bar.new_zoom (k); 290 } 291 } 292 293 MainWindow.get_toolbox ().update_expanders (); 294 MainWindow.get_toolbox ().update_all_expanders (); 295 Toolbox.redraw_tool_box (); 296 } 297 298 // FIXME: background thread 299 public static void save_as_bfp () { 300 FileChooser fc = new FileChooser (); 301 302 if (suppress_event) { 303 warn_if_test ("Event suppressed"); 304 return; 305 } 306 307 if (!set_suppress_event (true)) { 308 return; 309 } 310 311 fc.file_selected.connect((fn) => { 312 Font f = BirdFont.get_current_font (); 313 314 if (fn != null) { 315 f.init_bfp ((!) fn); 316 } 317 318 set_suppress_event (false); 319 }); 320 321 MainWindow.file_chooser (t_("Save"), fc, FileChooser.SAVE); 322 } 323 324 public static void new_file () { 325 Font font; 326 SaveDialogListener dialog = new SaveDialogListener (); 327 328 if (suppress_event) { 329 warn_if_test ("Event suppressed"); 330 return; 331 } 332 333 font = BirdFont.get_current_font (); 334 335 dialog.signal_discard.connect (() => { 336 MainWindow.close_all_tabs (); 337 338 BirdFont.new_font (); 339 MainWindow.native_window.font_loaded (); 340 341 show_default_characters (); 342 343 GlyphCanvas.redraw (); 344 }); 345 346 dialog.signal_save.connect (() => { 347 MenuTab.save_callback = new SaveCallback (); 348 MenuTab.save_callback.file_saved.connect (() => { 349 dialog.signal_discard (); 350 }); 351 save_callback.save (); 352 }); 353 354 dialog.signal_cancel.connect (() => { 355 MainWindow.hide_dialog (); 356 }); 357 358 if (!font.is_modified ()) { 359 dialog.signal_discard (); 360 } else { 361 MainWindow.show_dialog (new SaveDialog (dialog)); 362 } 363 364 return; 365 } 366 367 public static void quit () { 368 if (suppress_event) { 369 warn_if_test ("Event suppressed"); 370 return; 371 } 372 373 TabContent.hide_text_input (); 374 375 SaveDialogListener dialog = new SaveDialogListener (); 376 Font font = BirdFont.get_current_font (); 377 378 Preferences.save (); 379 380 dialog.signal_discard.connect (() => { 381 ensure_main_loop_is_empty (); 382 MainWindow.native_window.quit (); 383 }); 384 385 dialog.signal_save.connect (() => { 386 MenuTab.set_save_callback (new SaveCallback ()); 387 MenuTab.save_callback.file_saved.connect (() => { 388 ensure_main_loop_is_empty (); 389 MainWindow.native_window.quit (); 390 }); 391 save_callback.save (); 392 }); 393 394 dialog.signal_cancel.connect (() => { 395 MainWindow.hide_dialog (); 396 }); 397 398 if (!font.is_modified ()) { 399 dialog.signal_discard (); 400 } else { 401 MainWindow.show_dialog (new SaveDialog (dialog)); 402 } 403 } 404 405 public static void show_export_settings_tab () { 406 MainWindow.get_tab_bar ().add_unique_tab (new ExportSettings ()); 407 } 408 409 public static void show_description () { 410 MainWindow.get_tab_bar ().add_unique_tab (new DescriptionDisplay ()); 411 } 412 413 public static void show_kerning_context () { 414 if (suppress_event) { 415 warn_if_test ("Event suppressed"); 416 return; 417 } 418 419 KerningDisplay kd = MainWindow.get_kerning_display (); 420 MainWindow.get_tab_bar ().add_unique_tab (kd); 421 } 422 423 public static void show_spacing_tab () { 424 if (suppress_event) { 425 warn_if_test ("Event suppressed"); 426 return; 427 } 428 429 SpacingTab s = MainWindow.get_spacing_tab (); 430 MainWindow.get_tab_bar ().add_unique_tab (s); 431 } 432 433 public static void show_ligature_tab () { 434 if (suppress_event) { 435 warn_if_test ("Event suppressed"); 436 return; 437 } 438 439 LigatureList d = MainWindow.get_ligature_display (); 440 MainWindow.get_tab_bar ().add_unique_tab (d); 441 } 442 443 public static void preview () { 444 Font font = BirdFont.get_current_font (); 445 446 if (suppress_event) { 447 warn_if_test ("Event suppressed"); 448 return; 449 } 450 451 if (font.font_file == null) { 452 save_callback = new SaveCallback (); 453 save_callback.file_saved.connect (() => { 454 show_preview_tab (); 455 }); 456 save_callback.save (); 457 } else { 458 show_preview_tab (); 459 } 460 } 461 462 public static void show_preview_tab () { 463 OverWriteDialogListener dialog = new OverWriteDialogListener (); 464 TabBar tab_bar = MainWindow.get_tab_bar (); 465 FontFormat format = BirdFont.get_current_font ().format; 466 467 if (suppress_event) { 468 warn_if_test ("Event suppressed"); 469 return; 470 } 471 472 dialog.overwrite_signal.connect (() => { 473 KeyBindings.set_modifier (NONE); 474 tab_bar.add_unique_tab (new Preview (), true); 475 PreviewTools.update_preview (); 476 }); 477 478 if ((format == FontFormat.SVG || format == FontFormat.FREETYPE) && !OverWriteDialogListener.dont_ask_again) { 479 MainWindow.native_window.set_overwrite_dialog (dialog); 480 } else { 481 dialog.overwrite (); 482 } 483 } 484 485 /** Display the language selection tab. */ 486 public static void select_language () { 487 if (suppress_event) { 488 warn_if_test ("Event suppressed"); 489 return; 490 } 491 492 MainWindow.get_tab_bar ().add_unique_tab (new LanguageSelectionTab ()); 493 } 494 495 public static void use_current_glyph_as_background () { 496 if (suppress_event) { 497 warn_if_test ("Event suppressed"); 498 return; 499 } 500 501 Glyph.background_glyph = MainWindow.get_current_glyph (); 502 503 if (MainWindow.get_current_display () is OverView) { 504 Glyph.background_glyph = MainWindow.get_overview ().get_current_glyph (); 505 } 506 } 507 508 public static void reset_glyph_background () { 509 Glyph.background_glyph = null; 510 } 511 512 public static void remove_all_kerning_pairs () { 513 if (suppress_event) { 514 warn_if_test ("Event suppressed"); 515 return; 516 } 517 518 KerningClasses classes = BirdFont.get_current_font ().get_kerning_classes (); 519 classes.remove_all_pairs (); 520 KerningTools.update_kerning_classes (); 521 } 522 523 public static void list_all_kerning_pairs () { 524 if (suppress_event) { 525 warn_if_test ("Event suppressed"); 526 return; 527 } 528 529 MainWindow.get_tab_bar ().add_unique_tab (new KerningList ()); 530 } 531 532 public static void ensure_main_loop_is_empty () { 533 unowned MainContext context; 534 bool acquired; 535 536 context = MainContext.default (); 537 acquired = context.acquire (); 538 539 if (unlikely (!acquired)) { 540 warning ("Failed to acquire main loop.\n"); 541 return; 542 } 543 544 while (context.pending ()) { 545 context.iteration (true); 546 } 547 context.release (); 548 } 549 550 public static void save_as () { 551 if (MenuTab.suppress_event || !save_callback.is_done) { 552 warn_if_test ("Event suppressed"); 553 return; 554 } 555 556 MenuTab.set_save_callback (new SaveCallback ()); 557 MenuTab.save_callback.save_as(); 558 } 559 560 public static void save () { 561 if (MenuTab.suppress_event && !save_callback.is_done) { 562 warn_if_test ("Event suppressed"); 563 return; 564 } 565 566 MenuTab.set_save_callback (new SaveCallback ()); 567 MenuTab.save_callback.save (); 568 } 569 570 public static void load () { 571 if (MenuTab.suppress_event) { 572 warn_if_test ("Event suppressed"); 573 return; 574 } 575 576 MenuTab.load_callback = new LoadCallback (); 577 MenuTab.load_callback.load (); 578 579 MenuTab.load_callback.file_loaded.connect (() => { 580 Font f = BirdFont.get_current_font (); 581 MenuTab.apply_font_setting (f); 582 }); 583 } 584 585 public static void move_to_baseline () { 586 if (suppress_event) { 587 warn_if_test ("Event suppressed"); 588 return; 589 } 590 591 DrawingTools.move_tool.move_to_baseline (); 592 } 593 594 public static void show_file_dialog_tab (string title, FileChooser action, bool folder) { 595 FileDialogTab ft = new FileDialogTab (title, action, folder); 596 MainWindow.get_tab_bar ().add_tab (ft); 597 } 598 599 public static void simplify_path () { 600 if (suppress_event) { 601 warn_if_test ("Event suppressed"); 602 return; 603 } 604 605 Task t = new Task (); 606 t.task.connect (simplify); 607 MainWindow.native_window.run_background_thread (t); 608 } 609 610 private static void simplify () { 611 Glyph g = MainWindow.get_current_glyph (); 612 Gee.ArrayList<Path> paths = new Gee.ArrayList<Path> (); 613 614 // selected objects 615 foreach (Path p in g.active_paths) { 616 paths.add (PenTool.simplify (p, false, PenTool.simplification_threshold)); 617 } 618 619 // selected segments 620 if (paths.size == 0) { 621 foreach (Path p in g.get_all_paths ()) { 622 g.add_active_path (null, p); 623 } 624 625 foreach (Path p in g.active_paths) { 626 paths.add (PenTool.simplify (p, true, PenTool.simplification_threshold)); 627 } 628 } 629 630 g.store_undo_state (); 631 632 foreach (Path p in g.active_paths) { 633 g.layers.remove_path (p); 634 } 635 636 foreach (Path p in g.active_paths) { 637 g.layers.remove_path (p); 638 } 639 640 foreach (Path p in paths) { 641 g.add_path (p); 642 g.add_active_path (null, p); 643 } 644 645 g.active_paths.clear (); 646 g.update_view (); 647 } 648 649 public static void show_spacing_class_tab () { 650 SpacingClassTab t = MainWindow.get_spacing_class_tab (); 651 MainWindow.get_tab_bar ().add_unique_tab (t); 652 } 653 654 public static void add_ligature () { 655 TextListener listener; 656 string ligature_name = ""; 657 658 if (suppress_event) { 659 warn_if_test ("Event suppressed"); 660 return; 661 } 662 663 listener = new TextListener (t_("Name"), "", t_("Add ligature")); 664 665 listener.signal_text_input.connect ((text) => { 666 ligature_name = text; 667 }); 668 669 listener.signal_submit.connect (() => { 670 Font font = BirdFont.get_current_font (); 671 GlyphCollection? fg; 672 Glyph glyph; 673 GlyphCollection glyph_collection; 674 OverView o = MainWindow.get_overview (); 675 676 fg = font.get_glyph_collection_by_name (ligature_name); 677 678 if (fg == null) { 679 glyph_collection = new GlyphCollection ('\0', ligature_name); 680 681 glyph = new Glyph (ligature_name, '\0'); 682 glyph_collection.set_unassigned (true); 683 glyph_collection.insert_glyph (glyph, true); 684 685 font.add_glyph_collection (glyph_collection); 686 } 687 688 o.display_all_available_glyphs (); 689 o.scroll_to_glyph (ligature_name); 690 691 TabContent.hide_text_input (); 692 show_all_available_characters (); 693 }); 694 695 TabContent.show_text_input (listener); 696 } 697 698 public static void show_default_characters () { 699 MainWindow.get_tab_bar ().add_unique_tab (new OverView ()); 700 OverView o = MainWindow.get_overview (); 701 GlyphRange gr = new GlyphRange (); 702 703 if (!BirdFont.get_current_font ().initialised) { 704 MenuTab.new_file (); 705 } 706 707 DefaultCharacterSet.use_default_range (gr); 708 o.set_current_glyph_range (gr); 709 710 MainWindow.get_tab_bar ().select_tab_name ("Overview"); 711 } 712 713 public static void show_all_available_characters () { 714 MainWindow.get_tab_bar ().add_unique_tab (new OverView ()); 715 716 if (!BirdFont.get_current_font ().initialised) { 717 MenuTab.new_file (); 718 } 719 720 MainWindow.get_tab_bar ().select_tab_name ("Overview"); 721 OverviewTools.show_all_available_characters (); 722 } 723 724 public static void show_background_tab () { 725 BackgroundTab bt; 726 727 if (suppress_event) { 728 warn_if_test ("Event suppressed"); 729 return; 730 } 731 732 bt = BackgroundTab.get_instance (); 733 MainWindow.get_tab_bar ().add_unique_tab (bt); 734 } 735 736 public static void show_settings_tab () { 737 MainWindow.get_tab_bar ().add_unique_tab (new SettingsTab ()); 738 } 739 740 public static void show_theme_tab () { 741 MainWindow.get_tab_bar ().add_unique_tab (new ThemeTab ()); 742 } 743 744 public static void show_guide_tab () { 745 MainWindow.get_tab_bar ().add_unique_tab (new GuideTab ()); 746 } 747 } 748 749 } 750