The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

BirdFont.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/BirdFont.vala.
Store order for theme colors in setting tab
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 using BirdFont; 15 public const string GETTEXT_PACKAGE = "birdfont"; 16 17 namespace BirdFont { 18 19 public static string? settings_directory = null; 20 21 static void print_import_help (string[] arg) { 22 stdout.printf (t_("Usage:")); 23 stdout.printf (arg[0]); 24 stdout.printf (" " + t_("BF-FILE") + " " + t_("SVG-FILES ...") +"\n"); 25 stdout.printf ("\n"); 26 } 27 28 public static int run_import (string[] arg) { 29 string bf_file = ""; 30 Gee.ArrayList<string> svg_files = new Gee.ArrayList<string> (); 31 File bf; 32 File svg; 33 Font font; 34 bool imported; 35 36 Preferences.load (); 37 BirdFont.args = new Argument (""); 38 BirdFont.current_font = new Font (); 39 BirdFont.current_glyph_collection = new GlyphCollection.with_glyph ('\0', ""); 40 MainWindow.init (); 41 42 if (arg.length < 3) { 43 print_import_help (arg); 44 return -1; 45 } 46 47 bf_file = build_absoulute_path (arg[1]); 48 49 for (int i = 2; i < arg.length; i++) { 50 svg_files.add (arg[i]); 51 } 52 53 bf = File.new_for_path (bf_file); 54 foreach (string f in svg_files) { 55 svg = File.new_for_path (f); 56 57 if (!svg.query_exists ()) { 58 stdout.printf (@"$f " + t_("does not exist.") + "\n"); 59 return -1; 60 } 61 } 62 63 font = BirdFont.get_current_font (); 64 65 if (!bf.query_exists ()) { 66 stdout.printf (@"$bf_file " + t_("does not exist.") + " "); 67 stdout.printf (t_("A new font will be created.") + "\n"); 68 font.set_file (bf_file); 69 } else { 70 font.set_file (bf_file); 71 if (!font.load ()) { 72 warning (@"Failed to load font $bf_file.\n"); 73 74 if (!bf_file.has_suffix (".bf")) { 75 warning (@"Is it a .bf file?\n"); 76 } 77 78 return -1; 79 } 80 } 81 82 font.save_backup (); 83 84 foreach (string f in svg_files) { 85 svg = File.new_for_path (f); 86 imported = import_svg_file (font, svg); 87 88 if (!imported) { 89 stdout.printf (t_("Failed to import") + " " + f + "\n"); 90 stdout.printf (t_("Aborting") + "\n"); 91 return -1; 92 } 93 } 94 95 font.save_bf (); 96 97 return 0; 98 } 99 100 internal static string build_absoulute_path (string file_name) { 101 File f = File.new_for_path (file_name); 102 return (!) f.get_path (); 103 } 104 105 static bool import_svg_file (Font font, File svg_file) { 106 string file_name = (!) svg_file.get_basename (); 107 string glyph_name; 108 StringBuilder n; 109 Glyph glyph; 110 GlyphCollection? gc = null; 111 GlyphCollection glyph_collection; 112 unichar character; 113 GlyphCanvas canvas; 114 115 glyph_name = file_name.replace (".svg", ""); 116 glyph_name = glyph_name.replace (".SVG", ""); 117 118 if (glyph_name.char_count () > 1) { 119 if (glyph_name.has_prefix ("U+")) { 120 n = new StringBuilder (); 121 n.append_unichar (Font.to_unichar (glyph_name)); 122 glyph_name = n.str; 123 gc = font.get_glyph_collection (glyph_name); 124 } else { 125 gc = font.get_glyph_collection_by_name (glyph_name); 126 127 if (gc == null) { 128 stdout.printf (file_name + " " + t_("is not the name of a glyph or a Unicode value.") + "\n"); 129 stdout.printf (t_("Unicode values must start with U+.") + "\n"); 130 return false; 131 } 132 } 133 } else { 134 gc = font.get_glyph_collection (glyph_name); 135 } 136 137 if (gc != null) { 138 glyph_collection = (!) gc; 139 character = glyph_collection.get_unicode_character (); 140 glyph = new Glyph (glyph_collection.get_name (), character); 141 glyph.version_id = glyph_collection.get_last_id () + 1; 142 glyph_collection.insert_glyph (glyph, true); 143 } else { 144 return_val_if_fail (glyph_name.char_count () == 1, false); 145 character = glyph_name.get_char (0); 146 glyph_collection = new GlyphCollection (character, glyph_name); 147 glyph = new Glyph (glyph_name, character); 148 glyph_collection.insert_glyph (glyph, true); 149 font.add_glyph_collection (glyph_collection); 150 } 151 152 canvas = MainWindow.get_glyph_canvas (); 153 canvas.set_current_glyph_collection (glyph_collection); 154 155 stdout.printf (t_("Adding")); 156 stdout.printf (" "); 157 stdout.printf ((!) svg_file.get_basename ()); 158 stdout.printf (" "); 159 stdout.printf (t_("to")); 160 stdout.printf (" "); 161 stdout.printf (t_("Glyph")); 162 stdout.printf (": "); 163 stdout.printf (glyph.get_name ()); 164 stdout.printf (" "); 165 stdout.printf (t_("Version")); 166 stdout.printf (": "); 167 stdout.printf (@"$(glyph.version_id)"); 168 stdout.printf ("\n"); 169 170 SvgParser.import_svg ((!) svg_file.get_path ()); 171 172 return true; 173 } 174 175 static void print_export_help (string[] arg) { 176 stdout.printf (t_("Usage:")); 177 stdout.printf (arg[0]); 178 stdout.printf (" [" + t_("OPTION") + "...] " + t_("FILE") +"\n"); 179 stdout.printf ("-h, --help " + t_("print this message\n")); 180 stdout.printf ("-o, --output [DIRECTORY] " + t_("write files to this directory\n")); 181 stdout.printf ("-s, --svg " + t_("write svg file\n")); 182 stdout.printf ("-t, --ttf " + t_("write ttf and eot files\n")); 183 stdout.printf ("\n"); 184 } 185 186 public static string get_version () { 187 return VERSION; 188 } 189 190 public static string get_build_stamp () { 191 return BUILD_TIMESTAMP; 192 } 193 194 public static int run_export (string[] arg) { 195 string output_directory = "."; 196 string file_name = ""; 197 bool specific_formats = false; 198 bool write_ttf = false; 199 bool write_svg = false; 200 File directory; 201 Font font; 202 MainWindow main_window; 203 204 stdout.printf ("birdfont-export version %s\n", VERSION); 205 stdout.printf ("built on %s\n", BUILD_TIMESTAMP); 206 207 if (arg.length < 2) { 208 print_export_help (arg); 209 return -1; 210 } 211 212 BirdFont.current_font = BirdFont.new_font (); 213 BirdFont.current_glyph_collection = new GlyphCollection.with_glyph ( '\0', "null"); 214 main_window = new MainWindow (); 215 216 // FIXME: create a option for this and add structure the log messages 217 // init_logfile (); 218 219 for (int i = 1; i < arg.length; i++) { 220 221 if (arg[i] == "-f" || arg[i] == "--fatal-warnings") { 222 BirdFont.fatal_wanings = true; 223 return 0; 224 } 225 226 if (arg[i] == "-h" || arg[i] == "--help") { 227 print_export_help (arg); 228 return 0; 229 } 230 231 if ((arg[i] == "-o" || arg[i] == "--output") && i + 1 < arg.length) { 232 output_directory = arg[i + 1]; 233 i++; 234 continue; 235 } 236 237 if (arg[i] == "-s" || arg[i] == "--svg") { 238 write_svg = true; 239 specific_formats = true; 240 continue; 241 } 242 243 if (arg[i] == "-t" || arg[i] == "--ttf") { 244 write_ttf = true; 245 specific_formats = true; 246 continue; 247 } 248 249 if (arg[i].has_prefix ("-")) { 250 print_export_help (arg); 251 return 1; 252 } 253 254 if (!arg[i].has_prefix ("-")) { 255 file_name = arg[i]; 256 257 if (i != arg.length - 1) { 258 print_export_help (arg); 259 return 1; 260 } 261 262 break; 263 } 264 } 265 266 if (BirdFont.fatal_wanings) { 267 LogLevelFlags levels = LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL | LogLevelFlags.LEVEL_WARNING; 268 Log.set_handler (null, levels, BirdFont.fatal_warning); 269 } 270 271 Preferences.load (); 272 273 BirdFont.args = new Argument (""); 274 BirdFont.current_glyph_collection = new GlyphCollection.with_glyph ('\0', ""); 275 276 file_name = build_absoulute_path (file_name); 277 278 font = BirdFont.get_current_font (); 279 font.set_file (file_name); 280 if (!font.load ()) { 281 warning (@"Failed to load font $file_name.\n"); 282 283 if (!file_name.has_suffix (".bf")) { 284 warning (@"Is it a .bf file?\n"); 285 } 286 287 return 1; 288 } 289 290 directory = File.new_for_path (output_directory); 291 292 if (!directory.query_exists ()) { 293 stderr.printf (t_("Can't find output directory") + @"$((!)directory.get_path ())\n"); 294 return 1; 295 } 296 297 if (!specific_formats || write_svg) { 298 print (@"Writing $(BirdFont.current_font.get_full_name ()).svg to $output_directory\n"); 299 ExportTool.export_svg_font_path (File.new_for_path (output_directory)); 300 } 301 302 if (!specific_formats || write_ttf) { 303 print (@"Writing $(BirdFont.current_font.get_full_name ()).ttf to $output_directory\n"); 304 ExportTool.export_ttf_font_path (File.new_for_path (output_directory)); 305 } 306 307 return 0; 308 } 309 310 public static string wine_to_unix_path (string exec_path) { 311 bool drive_c, drive_z; 312 int i; 313 string p, q; 314 315 p = exec_path; 316 p = p.replace ("\\", "/"); 317 318 drive_c = exec_path.index_of ("C:") == 0; 319 drive_z = exec_path.index_of ("Z:") == 0; 320 321 i = p.index_of (":"); 322 323 if (i != -1) { 324 p = p.substring (i + 2); 325 } 326 327 if (drive_c) { 328 q = @"/home/$(Environment.get_user_name ())/.wine/drive_c/" + p; 329 330 if (File.new_for_path (q).query_exists ()) { 331 return q; 332 } else { 333 return p; 334 } 335 } 336 337 if (drive_z) { 338 return ("/" + p).dup (); 339 } 340 341 return exec_path.dup (); 342 } 343 344 public bool is_null (void* n) { 345 return n == null; 346 } 347 348 public bool has_flag (uint32 flag, uint32 mask) { 349 return (flag & mask) > 0; 350 } 351 352 public class BirdFont { 353 public static Argument args; 354 public static bool experimental = false; 355 public static bool show_coordinates = false; 356 public static bool fatal_wanings = false; 357 public static bool win32 = false; 358 public static bool mac = false; 359 public static bool android = false; 360 public static string exec_path = ""; 361 public static string bundle_path = ""; 362 363 public static bool logging = false; 364 public static DataOutputStream? logstream = null; 365 366 public static Font current_font; 367 public static GlyphCollection current_glyph_collection; 368 369 public static Drawing? drawing = null; 370 371 public BirdFont () { 372 set_defaul_drawing_callbacks (); 373 } 374 375 void set_defaul_drawing_callbacks () { 376 if (drawing == null) { 377 drawing = new Drawing (); 378 } 379 } 380 381 /** 382 * @param arg command line arguments 383 * @param program path 384 */ 385 public void init (string[] arg, string? program_path) { 386 int err_arg; 387 int i; 388 File font_file; 389 string exec_path; 390 391 args = new Argument.command_line (arg); 392 393 #if ANDROID 394 BirdFont.logging = true; 395 396 __android_log_print (ANDROID_LOG_WARN, "BirdFont", @"libbirdfont version $VERSION"); 397 LogLevelFlags log_levels = LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL | LogLevelFlags.LEVEL_WARNING; 398 Log.set_handler (null, log_levels, android_warning); 399 400 android = true; 401 #else 402 stdout.printf ("birdfont version %s\n", VERSION); 403 stdout.printf ("built on %s\n", BUILD_TIMESTAMP); 404 405 android = args.has_argument ("--android"); 406 BirdFont.logging = args.has_argument ("--log"); 407 #endif 408 409 if (BirdFont.logging) { 410 init_logfile (); 411 } 412 413 if (!args.has_argument ("--no-translation")) { 414 init_gettext (); 415 } 416 417 if (args.has_argument ("--help")) { 418 args.print_help (); 419 Process.exit (0); 420 } 421 422 err_arg = args.validate (); 423 if (err_arg != 0) { 424 stdout.printf (@"Unknown parameter $(arg [err_arg])\n\n"); 425 args.print_help (); 426 Process.exit (0); 427 } 428 429 Preferences.load (); 430 431 Theme.set_default_colors (); 432 Theme.load_theme (); 433 434 current_font = new Font (); 435 current_font.set_name (""); 436 current_font.initialised = false; 437 current_glyph_collection = new GlyphCollection.with_glyph ('\0', ""); 438 439 experimental = args.has_argument ("--test"); 440 show_coordinates = args.has_argument ("--show-coordinates") || experimental; 441 fatal_wanings = args.has_argument ("--fatal-warning"); 442 win32 = (arg[0].index_of (".exe") > -1) 443 || arg[0] == "wine" 444 || args.has_argument ("--windows"); 445 446 #if MAC 447 mac = true; 448 #else 449 mac = args.has_argument ("--mac"); 450 #endif 451 452 if (program_path == null) { 453 exec_path = ""; 454 455 if (win32) { 456 // wine hack to get "." folder in win32 environment 457 i = arg[0].last_index_of ("\\"); 458 459 if (i != -1) { 460 exec_path = arg[0]; 461 exec_path = exec_path.substring (0, i); 462 exec_path = wine_to_unix_path (exec_path); 463 } 464 } else { 465 exec_path = "./"; 466 } 467 } else { 468 exec_path = (!) program_path; 469 } 470 471 if (args.get_file () != "") { 472 font_file = File.new_for_path (args.get_file ()); 473 474 if (!font_file.query_exists ()) { 475 stderr.printf (@"The file \"$(args.get_file ())\" was not found.\n"); 476 Process.exit (-1); 477 } 478 } 479 480 if (fatal_wanings) { 481 LogLevelFlags levels = LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL | LogLevelFlags.LEVEL_WARNING; 482 Log.set_handler (null, levels, fatal_warning); 483 } 484 485 Preferences.set_last_file (get_current_font ().get_path ()); 486 487 DefaultCharacterSet.create_default_character_sets (); 488 DefaultCharacterSet.get_characters_for_prefered_language (); 489 490 HeadTable.init (); 491 492 if (TestBirdFont.get_singleton ().test_cases_to_run != "All") { 493 TestBirdFont.run_tests (); 494 } 495 } 496 497 public static Argument get_arguments () { 498 return args; 499 } 500 501 public static void set_bundle_path (string path) { 502 bundle_path = path; 503 } 504 505 public static void init_gettext () { 506 // FIXME: android, this should be OK now 507 #if !ANDROID 508 string locale_directory = SearchPaths.get_locale_directory (); 509 Intl.setlocale (LocaleCategory.MESSAGES, ""); 510 Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "utf-8"); 511 Intl.bindtextdomain (GETTEXT_PACKAGE, locale_directory); 512 #endif 513 } 514 515 public static void load_font_from_command_line () { 516 string file = args.get_file (); 517 if (file != "") { 518 FileTab.load_font (file); 519 } 520 } 521 522 public static Font get_current_font () { 523 return current_font; 524 } 525 526 internal static void fatal_warning (string? log_domain, LogLevelFlags log_levels, string message) { 527 bool fatal = true; 528 529 if (log_domain != null) { 530 stderr.printf ("%s: \n", (!) log_domain); 531 } 532 533 stderr.printf ("\n%s\n\n", message); 534 assert (!fatal); 535 } 536 537 #if ANDROID 538 internal static void android_warning (string? log_domain, LogLevelFlags log_levels, string message) { 539 __android_log_print (ANDROID_LOG_WARN, "BirdFont", message); 540 } 541 #endif 542 543 public static Font new_font () { 544 current_font = new Font (); 545 546 if (!is_null (MainWindow.tools)) { 547 MainWindow.get_drawing_tools ().remove_all_grid_buttons (); 548 MainWindow.get_drawing_tools ().add_new_grid (1); 549 MainWindow.get_drawing_tools ().add_new_grid (2); 550 MainWindow.get_drawing_tools ().add_new_grid (4); 551 } 552 553 if (!is_null (Toolbox.background_tools)) { 554 Toolbox.background_tools.remove_images (); 555 } 556 557 KerningTools.update_kerning_classes (); 558 559 return current_font; 560 } 561 562 public static void set_settings_directory (string directory) { 563 settings_directory = directory; 564 } 565 566 public static File get_preview_directory () { 567 File settings = get_settings_directory (); 568 File backup = get_child(settings, "preview"); 569 570 if (!backup.query_exists ()) { 571 DirUtils.create ((!) backup.get_path (), 0755); 572 } 573 574 return backup; 575 } 576 577 internal static File get_thumbnail_directory () { 578 File thumbnails = get_child (get_settings_directory (), "thumbnails"); 579 580 if (!thumbnails.query_exists ()) { 581 DirUtils.create ((!) thumbnails.get_path (), 0755); 582 } 583 584 return thumbnails; 585 } 586 587 internal static File get_settings_directory () { 588 string home_path; 589 File home; 590 File settings; 591 592 #if ANDROID 593 home_path = "/data/data/org.birdfont.sefyr/files"; 594 home = File.new_for_path (home_path); 595 596 if (!home.query_exists ()) { 597 printd ("Create settings directory."); 598 DirUtils.create ((!) home.get_path (),0755); 599 } 600 #else 601 home_path = (settings_directory != null) 602 ? (!) settings_directory : Environment.get_user_config_dir (); 603 604 if (is_null (home_path)) { 605 warning ("No home directory set."); 606 home_path = "."; 607 } 608 609 home = File.new_for_path (home_path); 610 #endif 611 settings = get_child(home, "birdfont"); 612 613 if (!settings.query_exists ()) { 614 DirUtils.create ((!) settings.get_path (), 0755); 615 } 616 617 return settings; 618 } 619 620 internal static File get_backup_directory () { 621 File settings = get_settings_directory (); 622 File backup = get_child (settings, "backup"); 623 624 if (!backup.query_exists ()) { 625 DirUtils.create ((!) backup.get_path (), 0755); 626 } 627 628 return backup; 629 } 630 631 public static bool has_argument (string param) { 632 if (is_null (args)) { 633 return false; 634 } 635 636 return args.has_argument (param); 637 } 638 639 internal static string? get_argument (string param) { 640 return args.get_argument (param); 641 } 642 } 643 644 void init_logfile () { 645 DateTime t; 646 File settings; 647 string s; 648 File log; 649 650 try { 651 t = new DateTime.now_local (); 652 settings = BirdFont.get_settings_directory (); 653 s = t.to_string ().replace (":", "_"); 654 log = get_child (settings, @"birdfont_$s.log"); 655 656 BirdFont.logstream = new DataOutputStream (log.create (FileCreateFlags.REPLACE_DESTINATION)); 657 ((!)BirdFont.logstream).put_string ((!) log.get_path ()); 658 ((!)BirdFont.logstream).put_string ("\n"); 659 660 warning ("Logging to " + (!) log.get_path ()); 661 } catch (GLib.Error e) { 662 warning (e.message); 663 warning ((!) log.get_path ()); 664 } 665 666 LogLevelFlags levels = LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL | LogLevelFlags.LEVEL_WARNING | LogLevelFlags.LEVEL_DEBUG; 667 Log.set_handler (null, levels, log_warning); 668 669 BirdFont.logging = true; 670 671 printd (@"Program version: $(VERSION)\n"); 672 printd (@"built on $(BUILD_TIMESTAMP)\n"); 673 } 674 675 internal static void log_warning (string? log_domain, LogLevelFlags log_levels, string message) { 676 if (log_domain != null) { 677 printd ((!) log_domain); 678 } 679 680 printd ("\n"); 681 printd (message); 682 printd ("\n"); 683 printd ("\n"); 684 } 685 686 /** Write debug output to logfile. */ 687 void printd (string s) { 688 #if ANDROID 689 __android_log_print (ANDROID_LOG_WARN, "BirdFont", s); 690 #else 691 if (unlikely (BirdFont.logging)) { 692 try { 693 if (BirdFont.logstream != null) { 694 ((!)BirdFont.logstream).put_string (s); 695 } else { 696 warning ("No logstream."); 697 } 698 699 stderr.printf (s); 700 } catch (GLib.Error e) { 701 warning (e.message); 702 } 703 } 704 #endif 705 } 706 707 /** Translate string */ 708 public string t_ (string t) { 709 return _(t); 710 } 711 712 /** Translate mac menu items */ 713 public static string translate_mac (string t) { 714 string s = t_(t); 715 return s.replace ("_", ""); 716 } 717 718 /** Print a warning if Birdfont was started with the --test argument. */ 719 public static void warn_if_test (string message) { 720 if (BirdFont.has_argument ("--test")) { 721 warning (message); 722 } 723 } 724 725 /** Obtain a handle to a file in a folder. */ 726 public static File get_child (File folder, string file_name) { 727 string f; 728 string s; 729 string n; 730 731 // avoid drive letter problems on windows 732 733 f = (!) folder.get_path (); 734 s = (BirdFont.win32) ? "\\" : "/"; 735 736 n = file_name; 737 if (unlikely (BirdFont.win32 && file_name.index_of ("\\") != -1)) { 738 warning (@"File name contains path separator: $file_name, Directory: $f"); 739 n = n.substring (n.last_index_of ("\\")).replace ("\\", ""); 740 } 741 742 if (!f.has_suffix (s)) { 743 f += s; 744 } 745 746 printd (@"File: Directory: $f Name: $n\n"); 747 748 return File.new_for_path (f + n); 749 } 750 751 public static void set_drawing_callbacks (Drawing callbacks) { 752 BirdFont.drawing = callbacks; 753 } 754 755 } 756