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 MainWindow.get_toolbox ().update_expanders (); 285 MainWindow.get_toolbox ().update_all_expanders (); 286 Toolbox.redraw_tool_box (); 287 } 288 289 // FIXME: background thread 290 public static void save_as_bfp () { 291 FileChooser fc = new FileChooser (); 292 293 if (suppress_event) { 294 warn_if_test ("Event suppressed"); 295 return; 296 } 297 298 if (!set_suppress_event (true)) { 299 return; 300 } 301 302 fc.file_selected.connect((fn) => { 303 Font f = BirdFont.get_current_font (); 304 305 if (fn != null) { 306 f.init_bfp ((!) fn); 307 } 308 309 set_suppress_event (false); 310 }); 311 312 MainWindow.file_chooser (t_("Save"), fc, FileChooser.SAVE); 313 } 314 315 public static void new_file () { 316 Font font; 317 SaveDialogListener dialog = new SaveDialogListener (); 318 319 if (suppress_event) { 320 warn_if_test ("Event suppressed"); 321 return; 322 } 323 324 font = BirdFont.get_current_font (); 325 326 dialog.signal_discard.connect (() => { 327 MainWindow.close_all_tabs (); 328 329 BirdFont.new_font (); 330 MainWindow.native_window.font_loaded (); 331 332 show_default_characters (); 333 334 GlyphCanvas.redraw (); 335 }); 336 337 dialog.signal_save.connect (() => { 338 MenuTab.save_callback = new SaveCallback (); 339 MenuTab.save_callback.file_saved.connect (() => { 340 dialog.signal_discard (); 341 }); 342 save_callback.save (); 343 }); 344 345 dialog.signal_cancel.connect (() => { 346 MainWindow.hide_dialog (); 347 }); 348 349 if (!font.is_modified ()) { 350 dialog.signal_discard (); 351 } else { 352 MainWindow.show_dialog (new SaveDialog (dialog)); 353 } 354 355 return; 356 } 357 358 public static void quit () { 359 if (suppress_event) { 360 warn_if_test ("Event suppressed"); 361 return; 362 } 363 364 TabContent.hide_text_input (); 365 366 SaveDialogListener dialog = new SaveDialogListener (); 367 Font font = BirdFont.get_current_font (); 368 369 Preferences.save (); 370 371 dialog.signal_discard.connect (() => { 372 ensure_main_loop_is_empty (); 373 MainWindow.native_window.quit (); 374 }); 375 376 dialog.signal_save.connect (() => { 377 MenuTab.set_save_callback (new SaveCallback ()); 378 MenuTab.save_callback.file_saved.connect (() => { 379 ensure_main_loop_is_empty (); 380 MainWindow.native_window.quit (); 381 }); 382 save_callback.save (); 383 }); 384 385 dialog.signal_cancel.connect (() => { 386 MainWindow.hide_dialog (); 387 }); 388 389 if (!font.is_modified ()) { 390 dialog.signal_discard (); 391 } else { 392 MainWindow.show_dialog (new SaveDialog (dialog)); 393 } 394 } 395 396 public static void show_export_settings_tab () { 397 MainWindow.get_tab_bar ().add_unique_tab (new ExportSettings ()); 398 } 399 400 public static void show_description () { 401 MainWindow.get_tab_bar ().add_unique_tab (new DescriptionDisplay ()); 402 } 403 404 public static void show_kerning_context () { 405 if (suppress_event) { 406 warn_if_test ("Event suppressed"); 407 return; 408 } 409 410 KerningDisplay kd = MainWindow.get_kerning_display (); 411 MainWindow.get_tab_bar ().add_unique_tab (kd); 412 } 413 414 public static void show_spacing_tab () { 415 if (suppress_event) { 416 warn_if_test ("Event suppressed"); 417 return; 418 } 419 420 SpacingTab s = MainWindow.get_spacing_tab (); 421 MainWindow.get_tab_bar ().add_unique_tab (s); 422 } 423 424 public static void show_ligature_tab () { 425 if (suppress_event) { 426 warn_if_test ("Event suppressed"); 427 return; 428 } 429 430 LigatureList d = MainWindow.get_ligature_display (); 431 MainWindow.get_tab_bar ().add_unique_tab (d); 432 } 433 434 public static void preview () { 435 Font font = BirdFont.get_current_font (); 436 437 if (suppress_event) { 438 warn_if_test ("Event suppressed"); 439 return; 440 } 441 442 if (font.font_file == null) { 443 save_callback = new SaveCallback (); 444 save_callback.file_saved.connect (() => { 445 show_preview_tab (); 446 }); 447 save_callback.save (); 448 } else { 449 show_preview_tab (); 450 } 451 } 452 453 public static void show_preview_tab () { 454 OverWriteDialogListener dialog = new OverWriteDialogListener (); 455 TabBar tab_bar = MainWindow.get_tab_bar (); 456 FontFormat format = BirdFont.get_current_font ().format; 457 458 if (suppress_event) { 459 warn_if_test ("Event suppressed"); 460 return; 461 } 462 463 dialog.overwrite_signal.connect (() => { 464 KeyBindings.set_modifier (NONE); 465 tab_bar.add_unique_tab (new Preview (), true); 466 PreviewTools.update_preview (); 467 }); 468 469 if ((format == FontFormat.SVG || format == FontFormat.FREETYPE) && !OverWriteDialogListener.dont_ask_again) { 470 MainWindow.native_window.set_overwrite_dialog (dialog); 471 } else { 472 dialog.overwrite (); 473 } 474 } 475 476 /** Display the language selection tab. */ 477 public static void select_language () { 478 if (suppress_event) { 479 warn_if_test ("Event suppressed"); 480 return; 481 } 482 483 MainWindow.get_tab_bar ().add_unique_tab (new LanguageSelectionTab ()); 484 } 485 486 public static void use_current_glyph_as_background () { 487 if (suppress_event) { 488 warn_if_test ("Event suppressed"); 489 return; 490 } 491 492 Glyph.background_glyph = MainWindow.get_current_glyph (); 493 494 if (MainWindow.get_current_display () is OverView) { 495 Glyph.background_glyph = MainWindow.get_overview ().get_current_glyph (); 496 } 497 } 498 499 public static void reset_glyph_background () { 500 Glyph.background_glyph = null; 501 } 502 503 public static void remove_all_kerning_pairs () { 504 if (suppress_event) { 505 warn_if_test ("Event suppressed"); 506 return; 507 } 508 509 KerningClasses classes = BirdFont.get_current_font ().get_kerning_classes (); 510 classes.remove_all_pairs (); 511 KerningTools.update_kerning_classes (); 512 } 513 514 public static void list_all_kerning_pairs () { 515 if (suppress_event) { 516 warn_if_test ("Event suppressed"); 517 return; 518 } 519 520 MainWindow.get_tab_bar ().add_unique_tab (new KerningList ()); 521 } 522 523 public static void ensure_main_loop_is_empty () { 524 unowned MainContext context; 525 bool acquired; 526 527 context = MainContext.default (); 528 acquired = context.acquire (); 529 530 if (unlikely (!acquired)) { 531 warning ("Failed to acquire main loop.\n"); 532 return; 533 } 534 535 while (context.pending ()) { 536 context.iteration (true); 537 } 538 context.release (); 539 } 540 541 public static void save_as () { 542 if (MenuTab.suppress_event || !save_callback.is_done) { 543 warn_if_test ("Event suppressed"); 544 return; 545 } 546 547 MenuTab.set_save_callback (new SaveCallback ()); 548 MenuTab.save_callback.save_as(); 549 } 550 551 public static void save () { 552 if (MenuTab.suppress_event && !save_callback.is_done) { 553 warn_if_test ("Event suppressed"); 554 return; 555 } 556 557 MenuTab.set_save_callback (new SaveCallback ()); 558 MenuTab.save_callback.save (); 559 } 560 561 public static void load () { 562 if (MenuTab.suppress_event) { 563 warn_if_test ("Event suppressed"); 564 return; 565 } 566 567 MenuTab.load_callback = new LoadCallback (); 568 MenuTab.load_callback.load (); 569 570 MenuTab.load_callback.file_loaded.connect (() => { 571 Font f = BirdFont.get_current_font (); 572 MenuTab.apply_font_setting (f); 573 }); 574 } 575 576 public static void move_to_baseline () { 577 if (suppress_event) { 578 warn_if_test ("Event suppressed"); 579 return; 580 } 581 582 DrawingTools.move_tool.move_to_baseline (); 583 } 584 585 public static void show_file_dialog_tab (string title, FileChooser action, bool folder) { 586 FileDialogTab ft = new FileDialogTab (title, action, folder); 587 MainWindow.get_tab_bar ().add_tab (ft); 588 } 589 590 public static void simplify_path () { 591 if (suppress_event) { 592 warn_if_test ("Event suppressed"); 593 return; 594 } 595 596 Task t = new Task (); 597 t.task.connect (simplify); 598 MainWindow.native_window.run_background_thread (t); 599 } 600 601 private static void simplify () { 602 Glyph g = MainWindow.get_current_glyph (); 603 Gee.ArrayList<Path> paths = new Gee.ArrayList<Path> (); 604 605 // selected objects 606 foreach (Path p in g.active_paths) { 607 paths.add (PenTool.simplify (p, false, PenTool.simplification_threshold)); 608 } 609 610 // selected segments 611 if (paths.size == 0) { 612 foreach (Path p in g.get_all_paths ()) { 613 g.add_active_path (null, p); 614 } 615 616 foreach (Path p in g.active_paths) { 617 paths.add (PenTool.simplify (p, true, PenTool.simplification_threshold)); 618 } 619 } 620 621 g.store_undo_state (); 622 623 foreach (Path p in g.active_paths) { 624 g.layers.remove_path (p); 625 } 626 627 foreach (Path p in g.active_paths) { 628 g.layers.remove_path (p); 629 } 630 631 foreach (Path p in paths) { 632 g.add_path (p); 633 g.add_active_path (null, p); 634 } 635 636 g.active_paths.clear (); 637 g.update_view (); 638 } 639 640 public static void show_spacing_class_tab () { 641 SpacingClassTab t = MainWindow.get_spacing_class_tab (); 642 MainWindow.get_tab_bar ().add_unique_tab (t); 643 } 644 645 public static void add_ligature () { 646 TextListener listener; 647 string ligature_name = ""; 648 649 if (suppress_event) { 650 warn_if_test ("Event suppressed"); 651 return; 652 } 653 654 listener = new TextListener (t_("Name"), "", t_("Add ligature")); 655 656 listener.signal_text_input.connect ((text) => { 657 ligature_name = text; 658 }); 659 660 listener.signal_submit.connect (() => { 661 Font font = BirdFont.get_current_font (); 662 GlyphCollection? fg; 663 Glyph glyph; 664 GlyphCollection glyph_collection; 665 OverView o = MainWindow.get_overview (); 666 667 fg = font.get_glyph_collection_by_name (ligature_name); 668 669 if (fg == null) { 670 glyph_collection = new GlyphCollection ('\0', ligature_name); 671 672 glyph = new Glyph (ligature_name, '\0'); 673 glyph_collection.set_unassigned (true); 674 glyph_collection.insert_glyph (glyph, true); 675 676 font.add_glyph_collection (glyph_collection); 677 } 678 679 o.display_all_available_glyphs (); 680 o.scroll_to_glyph (ligature_name); 681 682 TabContent.hide_text_input (); 683 show_all_available_characters (); 684 }); 685 686 TabContent.show_text_input (listener); 687 } 688 689 public static void show_default_characters () { 690 MainWindow.get_tab_bar ().add_unique_tab (new OverView ()); 691 OverView o = MainWindow.get_overview (); 692 GlyphRange gr = new GlyphRange (); 693 694 if (!BirdFont.get_current_font ().initialised) { 695 MenuTab.new_file (); 696 } 697 698 DefaultCharacterSet.use_default_range (gr); 699 o.set_current_glyph_range (gr); 700 701 MainWindow.get_tab_bar ().select_tab_name ("Overview"); 702 } 703 704 public static void show_all_available_characters () { 705 MainWindow.get_tab_bar ().add_unique_tab (new OverView ()); 706 707 if (!BirdFont.get_current_font ().initialised) { 708 MenuTab.new_file (); 709 } 710 711 MainWindow.get_tab_bar ().select_tab_name ("Overview"); 712 OverviewTools.show_all_available_characters (); 713 } 714 715 public static void show_background_tab () { 716 BackgroundTab bt; 717 718 if (suppress_event) { 719 warn_if_test ("Event suppressed"); 720 return; 721 } 722 723 bt = BackgroundTab.get_instance (); 724 MainWindow.get_tab_bar ().add_unique_tab (bt); 725 } 726 727 public static void show_settings_tab () { 728 MainWindow.get_tab_bar ().add_unique_tab (new SettingsTab ()); 729 } 730 731 public static void show_theme_tab () { 732 MainWindow.get_tab_bar ().add_unique_tab (new ThemeTab ()); 733 } 734 735 public static void show_guide_tab () { 736 MainWindow.get_tab_bar ().add_unique_tab (new GuideTab ()); 737 } 738 } 739 740 } 741