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