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.
Fix simplify path
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 return true; 156 } 157 158 public override string get_label () { 159 return t_("Menu"); 160 } 161 162 public override string get_name () { 163 return "Menu"; 164 } 165 166 public static void select_overview () { 167 if (suppress_event) { 168 warn_if_test ("Event suppressed"); 169 return; 170 } 171 172 if (BirdFont.get_current_font ().is_empty ()) { 173 show_default_characters (); 174 } else { 175 MainWindow.get_tab_bar ().add_unique_tab (new OverView ()); 176 MainWindow.get_tab_bar ().select_tab_name ("Overview"); 177 } 178 } 179 180 public static void signal_file_exported () { 181 IdleSource idle = new IdleSource (); 182 idle.set_callback (() => { 183 export_callback.file_exported (); 184 return false; 185 }); 186 idle.attach (null); 187 } 188 189 public static void signal_file_saved () { 190 IdleSource idle = new IdleSource (); 191 idle.set_callback (() => { 192 save_callback.file_saved (); 193 return false; 194 }); 195 idle.attach (null); 196 } 197 198 public static void signal_file_loaded () { 199 IdleSource idle = new IdleSource (); 200 idle.set_callback (() => { 201 load_callback.file_loaded (); 202 MainWindow.native_window.font_loaded (); 203 return false; 204 }); 205 idle.attach (null); 206 } 207 208 public static void apply_font_setting (Font f) { 209 DrawingTools.background_scale.set_value (f.background_scale); 210 211 DrawingTools.grid_expander.tool.clear (); 212 213 if (f.grid_width.size == 0) { 214 f.grid_width.add ("1"); 215 f.grid_width.add ("2"); 216 f.grid_width.add ("4"); 217 } 218 219 foreach (string grid in f.grid_width) { 220 DrawingTools.add_new_grid (double.parse (grid), false); 221 } 222 223 string sw = f.settings.get_setting ("stroke_width"); 224 if (sw != ""){ 225 StrokeTool.stroke_width = double.parse (sw); 226 DrawingTools.object_stroke.set_value_round (StrokeTool.stroke_width); 227 } 228 229 string pt = f.settings.get_setting ("point_type"); 230 DrawingTools.set_default_point_type (pt); 231 232 string stroke = f.settings.get_setting ("apply_stroke"); 233 bool s = bool.parse (stroke); 234 DrawingTools.add_stroke.set_selected (s); 235 StrokeTool.add_stroke = s; 236 237 string lc = f.settings.get_setting ("line_cap"); 238 239 if (lc == "butt") { 240 StrokeTool.line_cap = LineCap.BUTT; 241 } else if (lc == "square") { 242 StrokeTool.line_cap = LineCap.SQUARE; 243 } else if (lc == "round") { 244 StrokeTool.line_cap = LineCap.ROUND; 245 } 246 247 DrawingTools.set_stroke_tool_visibility (); 248 249 string lock_grid = f.settings.get_setting ("lock_grid"); 250 bool lg = bool.parse (lock_grid); 251 GridTool.lock_grid = lg; 252 DrawingTools.lock_grid.selected = GridTool.lock_grid; 253 254 string skew_overview = f.settings.get_setting ("skew_overview"); 255 if (skew_overview != "") { 256 double so = double.parse (skew_overview); 257 OverviewTools.skew.set_value_round (so); 258 } 259 } 260 261 // FIXME: background thread 262 public static void save_as_bfp () { 263 FileChooser fc = new FileChooser (); 264 265 if (suppress_event) { 266 warn_if_test ("Event suppressed"); 267 return; 268 } 269 270 if (!set_suppress_event (true)) { 271 return; 272 } 273 274 fc.file_selected.connect((fn) => { 275 Font f = BirdFont.get_current_font (); 276 277 if (fn != null) { 278 f.init_bfp ((!) fn); 279 } 280 281 set_suppress_event (false); 282 }); 283 284 MainWindow.file_chooser (t_("Save"), fc, FileChooser.SAVE); 285 } 286 287 public static void new_file () { 288 Font font; 289 SaveDialogListener dialog = new SaveDialogListener (); 290 291 if (suppress_event) { 292 warn_if_test ("Event suppressed"); 293 return; 294 } 295 296 font = BirdFont.get_current_font (); 297 298 dialog.signal_discard.connect (() => { 299 MainWindow.close_all_tabs (); 300 301 BirdFont.new_font (); 302 MainWindow.native_window.font_loaded (); 303 304 show_default_characters (); 305 306 GlyphCanvas.redraw (); 307 }); 308 309 dialog.signal_save.connect (() => { 310 MenuTab.save_callback = new SaveCallback (); 311 MenuTab.save_callback.file_saved.connect (() => { 312 dialog.signal_discard (); 313 }); 314 save_callback.save (); 315 }); 316 317 dialog.signal_cancel.connect (() => { 318 MainWindow.hide_dialog (); 319 }); 320 321 if (!font.is_modified ()) { 322 dialog.signal_discard (); 323 } else { 324 MainWindow.show_dialog (new SaveDialog (dialog)); 325 } 326 327 return; 328 } 329 330 public static void quit () { 331 if (suppress_event) { 332 warn_if_test ("Event suppressed"); 333 return; 334 } 335 336 TabContent.hide_text_input (); 337 338 SaveDialogListener dialog = new SaveDialogListener (); 339 Font font = BirdFont.get_current_font (); 340 341 Preferences.save (); 342 343 dialog.signal_discard.connect (() => { 344 ensure_main_loop_is_empty (); 345 MainWindow.native_window.quit (); 346 }); 347 348 dialog.signal_save.connect (() => { 349 MenuTab.set_save_callback (new SaveCallback ()); 350 MenuTab.save_callback.file_saved.connect (() => { 351 ensure_main_loop_is_empty (); 352 MainWindow.native_window.quit (); 353 }); 354 save_callback.save (); 355 }); 356 357 dialog.signal_cancel.connect (() => { 358 MainWindow.hide_dialog (); 359 }); 360 361 if (!font.is_modified ()) { 362 dialog.signal_discard (); 363 } else { 364 MainWindow.show_dialog (new SaveDialog (dialog)); 365 } 366 } 367 368 public static void show_export_settings_tab () { 369 MainWindow.get_tab_bar ().add_unique_tab (new ExportSettings ()); 370 } 371 372 public static void show_description () { 373 MainWindow.get_tab_bar ().add_unique_tab (new DescriptionDisplay ()); 374 } 375 376 public static void show_kerning_context () { 377 if (suppress_event) { 378 warn_if_test ("Event suppressed"); 379 return; 380 } 381 382 KerningDisplay kd = MainWindow.get_kerning_display (); 383 MainWindow.get_tab_bar ().add_unique_tab (kd); 384 } 385 386 public static void show_spacing_tab () { 387 if (suppress_event) { 388 warn_if_test ("Event suppressed"); 389 return; 390 } 391 392 SpacingTab s = MainWindow.get_spacing_tab (); 393 MainWindow.get_tab_bar ().add_unique_tab (s); 394 } 395 396 public static void show_ligature_tab () { 397 if (suppress_event) { 398 warn_if_test ("Event suppressed"); 399 return; 400 } 401 402 LigatureList d = MainWindow.get_ligature_display (); 403 MainWindow.get_tab_bar ().add_unique_tab (d); 404 } 405 406 public static void preview () { 407 Font font = BirdFont.get_current_font (); 408 409 if (suppress_event) { 410 warn_if_test ("Event suppressed"); 411 return; 412 } 413 414 if (font.font_file == null) { 415 save_callback = new SaveCallback (); 416 save_callback.file_saved.connect (() => { 417 show_preview_tab (); 418 }); 419 save_callback.save (); 420 } else { 421 show_preview_tab (); 422 } 423 } 424 425 public static void show_preview_tab () { 426 OverWriteDialogListener dialog = new OverWriteDialogListener (); 427 TabBar tab_bar = MainWindow.get_tab_bar (); 428 FontFormat format = BirdFont.get_current_font ().format; 429 430 if (suppress_event) { 431 warn_if_test ("Event suppressed"); 432 return; 433 } 434 435 dialog.overwrite_signal.connect (() => { 436 KeyBindings.set_modifier (NONE); 437 tab_bar.add_unique_tab (new Preview (), true); 438 PreviewTools.update_preview (); 439 }); 440 441 if ((format == FontFormat.SVG || format == FontFormat.FREETYPE) && !OverWriteDialogListener.dont_ask_again) { 442 MainWindow.native_window.set_overwrite_dialog (dialog); 443 } else { 444 dialog.overwrite (); 445 } 446 } 447 448 /** Display the language selection tab. */ 449 public static void select_language () { 450 if (suppress_event) { 451 warn_if_test ("Event suppressed"); 452 return; 453 } 454 455 MainWindow.get_tab_bar ().add_unique_tab (new LanguageSelectionTab ()); 456 } 457 458 public static void use_current_glyph_as_background () { 459 if (suppress_event) { 460 warn_if_test ("Event suppressed"); 461 return; 462 } 463 464 Glyph.background_glyph = MainWindow.get_current_glyph (); 465 466 if (MainWindow.get_current_display () is OverView) { 467 Glyph.background_glyph = MainWindow.get_overview ().get_current_glyph (); 468 } 469 } 470 471 public static void reset_glyph_background () { 472 Glyph.background_glyph = null; 473 } 474 475 public static void remove_all_kerning_pairs () { 476 if (suppress_event) { 477 warn_if_test ("Event suppressed"); 478 return; 479 } 480 481 KerningClasses classes = BirdFont.get_current_font ().get_kerning_classes (); 482 classes.remove_all_pairs (); 483 KerningTools.update_kerning_classes (); 484 } 485 486 public static void list_all_kerning_pairs () { 487 if (suppress_event) { 488 warn_if_test ("Event suppressed"); 489 return; 490 } 491 492 MainWindow.get_tab_bar ().add_unique_tab (new KerningList ()); 493 } 494 495 public static void ensure_main_loop_is_empty () { 496 unowned MainContext context; 497 bool acquired; 498 499 context = MainContext.default (); 500 acquired = context.acquire (); 501 502 if (unlikely (!acquired)) { 503 warning ("Failed to acquire main loop.\n"); 504 return; 505 } 506 507 while (context.pending ()) { 508 context.iteration (true); 509 } 510 context.release (); 511 } 512 513 public static void save_as () { 514 if (MenuTab.suppress_event || !save_callback.is_done) { 515 warn_if_test ("Event suppressed"); 516 return; 517 } 518 519 MenuTab.set_save_callback (new SaveCallback ()); 520 MenuTab.save_callback.save_as(); 521 } 522 523 public static void save () { 524 if (MenuTab.suppress_event && !save_callback.is_done) { 525 warn_if_test ("Event suppressed"); 526 return; 527 } 528 529 MenuTab.set_save_callback (new SaveCallback ()); 530 MenuTab.save_callback.save (); 531 } 532 533 public static void load () { 534 if (MenuTab.suppress_event) { 535 warn_if_test ("Event suppressed"); 536 return; 537 } 538 539 MenuTab.load_callback = new LoadCallback (); 540 MenuTab.load_callback.load (); 541 542 MenuTab.load_callback.file_loaded.connect (() => { 543 Font f = BirdFont.get_current_font (); 544 MenuTab.apply_font_setting (f); 545 }); 546 } 547 548 public static void move_to_baseline () { 549 if (suppress_event) { 550 warn_if_test ("Event suppressed"); 551 return; 552 } 553 554 DrawingTools.move_tool.move_to_baseline (); 555 } 556 557 public static void show_file_dialog_tab (string title, FileChooser action, bool folder) { 558 FileDialogTab ft = new FileDialogTab (title, action, folder); 559 MainWindow.get_tab_bar ().add_tab (ft); 560 } 561 562 public static void simplify_path () { 563 if (suppress_event) { 564 warn_if_test ("Event suppressed"); 565 return; 566 } 567 568 Task t = new Task (); 569 t.task.connect (simplify); 570 MainWindow.native_window.run_background_thread (t); 571 } 572 573 private static void simplify () { 574 Glyph g = MainWindow.get_current_glyph (); 575 Gee.ArrayList<Path> paths = new Gee.ArrayList<Path> (); 576 577 // selected objects 578 foreach (Path p in g.active_paths) { 579 paths.add (PenTool.simplify (p, false, PenTool.simplification_threshold)); 580 } 581 582 // selected segments 583 if (paths.size == 0) { 584 foreach (Path p in g.get_all_paths ()) { 585 g.add_active_path (null, p); 586 } 587 588 foreach (Path p in g.active_paths) { 589 paths.add (PenTool.simplify (p, true, PenTool.simplification_threshold)); 590 } 591 } 592 593 g.store_undo_state (); 594 595 foreach (Path p in g.active_paths) { 596 g.layers.remove_path (p); 597 } 598 599 foreach (Path p in g.active_paths) { 600 g.layers.remove_path (p); 601 } 602 603 foreach (Path p in paths) { 604 g.add_path (p); 605 g.add_active_path (null, p); 606 } 607 608 g.active_paths.clear (); 609 g.update_view (); 610 } 611 612 public static void show_spacing_class_tab () { 613 SpacingClassTab t = MainWindow.get_spacing_class_tab (); 614 MainWindow.get_tab_bar ().add_unique_tab (t); 615 } 616 617 public static void add_ligature () { 618 TextListener listener; 619 string ligature_name = ""; 620 621 if (suppress_event) { 622 warn_if_test ("Event suppressed"); 623 return; 624 } 625 626 listener = new TextListener (t_("Name"), "", t_("Add ligature")); 627 628 listener.signal_text_input.connect ((text) => { 629 ligature_name = text; 630 }); 631 632 listener.signal_submit.connect (() => { 633 Font font = BirdFont.get_current_font (); 634 GlyphCollection? fg; 635 Glyph glyph; 636 GlyphCollection glyph_collection; 637 OverView o = MainWindow.get_overview (); 638 639 fg = font.get_glyph_collection_by_name (ligature_name); 640 641 if (fg == null) { 642 glyph_collection = new GlyphCollection ('\0', ligature_name); 643 644 glyph = new Glyph (ligature_name, '\0'); 645 glyph_collection.set_unassigned (true); 646 glyph_collection.insert_glyph (glyph, true); 647 648 font.add_glyph_collection (glyph_collection); 649 } 650 651 o.display_all_available_glyphs (); 652 o.scroll_to_glyph (ligature_name); 653 654 TabContent.hide_text_input (); 655 show_all_available_characters (); 656 }); 657 658 TabContent.show_text_input (listener); 659 } 660 661 public static void show_default_characters () { 662 MainWindow.get_tab_bar ().add_unique_tab (new OverView ()); 663 OverView o = MainWindow.get_overview (); 664 GlyphRange gr = new GlyphRange (); 665 666 if (!BirdFont.get_current_font ().initialised) { 667 MenuTab.new_file (); 668 } 669 670 DefaultCharacterSet.use_default_range (gr); 671 o.set_current_glyph_range (gr); 672 673 MainWindow.get_tab_bar ().select_tab_name ("Overview"); 674 } 675 676 public static void show_all_available_characters () { 677 MainWindow.get_tab_bar ().add_unique_tab (new OverView ()); 678 679 if (!BirdFont.get_current_font ().initialised) { 680 MenuTab.new_file (); 681 } 682 683 MainWindow.get_tab_bar ().select_tab_name ("Overview"); 684 OverviewTools.show_all_available_characters (); 685 } 686 687 public static void show_background_tab () { 688 BackgroundTab bt; 689 690 if (suppress_event) { 691 warn_if_test ("Event suppressed"); 692 return; 693 } 694 695 bt = BackgroundTab.get_instance (); 696 MainWindow.get_tab_bar ().add_unique_tab (bt); 697 } 698 699 public static void show_settings_tab () { 700 MainWindow.get_tab_bar ().add_unique_tab (new SettingsTab ()); 701 } 702 703 public static void show_theme_tab () { 704 MainWindow.get_tab_bar ().add_unique_tab (new ThemeTab ()); 705 } 706 707 public static void show_guide_tab () { 708 MainWindow.get_tab_bar ().add_unique_tab (new GuideTab ()); 709 } 710 } 711 712 } 713