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.
Use table in recent files 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 218 if (BirdFont.logging) { 219 init_logfile (); 220 } 221 222 for (int i = 1; i < arg.length; i++) { 223 224 if (arg[i] == "-f" || arg[i] == "--fatal-warnings") { 225 BirdFont.fatal_wanings = true; 226 return 0; 227 } 228 229 if (arg[i] == "-h" || arg[i] == "--help") { 230 print_export_help (arg); 231 return 0; 232 } 233 234 if ((arg[i] == "-o" || arg[i] == "--output") && i + 1 < arg.length) { 235 output_directory = arg[i + 1]; 236 i++; 237 continue; 238 } 239 240 if (arg[i] == "-s" || arg[i] == "--svg") { 241 write_svg = true; 242 specific_formats = true; 243 continue; 244 } 245 246 if (arg[i] == "-t" || arg[i] == "--ttf") { 247 write_ttf = true; 248 specific_formats = true; 249 continue; 250 } 251 252 if (arg[i].has_prefix ("-")) { 253 print_export_help (arg); 254 return 1; 255 } 256 257 if (!arg[i].has_prefix ("-")) { 258 file_name = arg[i]; 259 260 if (i != arg.length - 1) { 261 print_export_help (arg); 262 return 1; 263 } 264 265 break; 266 } 267 } 268 269 if (BirdFont.fatal_wanings) { 270 LogLevelFlags levels = LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL | LogLevelFlags.LEVEL_WARNING; 271 Log.set_handler (null, levels, BirdFont.fatal_warning); 272 } 273 274 Preferences.load (); 275 276 BirdFont.args = new Argument (""); 277 BirdFont.current_glyph_collection = new GlyphCollection.with_glyph ('\0', ""); 278 279 file_name = build_absoulute_path (file_name); 280 281 font = BirdFont.get_current_font (); 282 font.set_file (file_name); 283 if (!font.load ()) { 284 warning (@"Failed to load font $file_name.\n"); 285 286 if (!file_name.has_suffix (".bf")) { 287 warning (@"Is it a .bf file?\n"); 288 } 289 290 return 1; 291 } 292 293 directory = File.new_for_path (output_directory); 294 295 if (!directory.query_exists ()) { 296 stderr.printf (t_("Can't find output directory") + @"$((!)directory.get_path ())\n"); 297 return 1; 298 } 299 300 if (!specific_formats || write_svg) { 301 print (@"Writing $(BirdFont.current_font.get_full_name ()).svg to $output_directory\n"); 302 ExportTool.export_svg_font_path (File.new_for_path (output_directory)); 303 } 304 305 if (!specific_formats || write_ttf) { 306 print (@"Writing $(BirdFont.current_font.get_full_name ()).ttf to $output_directory\n"); 307 ExportTool.export_ttf_font_path (File.new_for_path (output_directory)); 308 } 309 310 return 0; 311 } 312 313 public static void set_logging (bool log) { 314 BirdFont.logging = log; 315 } 316 317 public static string wine_to_unix_path (string exec_path) { 318 bool drive_c, drive_z; 319 int i; 320 string p, q; 321 322 p = exec_path; 323 p = p.replace ("\\", "/"); 324 325 drive_c = exec_path.index_of ("C:") == 0; 326 drive_z = exec_path.index_of ("Z:") == 0; 327 328 i = p.index_of (":"); 329 330 if (i != -1) { 331 p = p.substring (i + 2); 332 } 333 334 if (drive_c) { 335 q = @"/home/$(Environment.get_user_name ())/.wine/drive_c/" + p; 336 337 if (File.new_for_path (q).query_exists ()) { 338 return q; 339 } else { 340 return p; 341 } 342 } 343 344 if (drive_z) { 345 return ("/" + p).dup (); 346 } 347 348 return exec_path.dup (); 349 } 350 351 public bool is_null (void* n) { 352 return n == null; 353 } 354 355 public bool has_flag (uint32 flag, uint32 mask) { 356 return (flag & mask) > 0; 357 } 358 359 public class BirdFont { 360 public static Argument args; 361 public static bool experimental = false; 362 public static bool show_coordinates = false; 363 public static bool fatal_wanings = false; 364 public static bool win32 = false; 365 public static bool mac = false; 366 public static bool android = false; 367 public static string exec_path = ""; 368 public static string bundle_path = ""; 369 370 public static bool logging = false; 371 public static DataOutputStream? logstream = null; 372 373 public static Font current_font; 374 public static GlyphCollection current_glyph_collection; 375 376 public static Drawing? drawing = null; 377 378 public BirdFont () { 379 set_defaul_drawing_callbacks (); 380 } 381 382 void set_defaul_drawing_callbacks () { 383 if (drawing == null) { 384 drawing = new Drawing (); 385 } 386 } 387 388 /** 389 * @param arg command line arguments 390 * @param program path 391 */ 392 public void init (string[] arg, string? program_path) { 393 int err_arg; 394 int i; 395 File font_file; 396 string exec_path; 397 string theme; 398 399 args = new Argument.command_line (arg); 400 401 #if ANDROID 402 BirdFont.logging = true; 403 404 __android_log_print (ANDROID_LOG_WARN, "BirdFont", @"libbirdfont version $VERSION"); 405 LogLevelFlags log_levels = LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL | LogLevelFlags.LEVEL_WARNING; 406 Log.set_handler (null, log_levels, android_warning); 407 408 android = true; 409 #else 410 stdout.printf ("birdfont version %s\n", VERSION); 411 stdout.printf ("built on %s\n", BUILD_TIMESTAMP); 412 413 android = args.has_argument ("--android"); 414 415 if (!BirdFont.logging) { 416 BirdFont.logging = args.has_argument ("--log"); 417 } 418 #endif 419 420 if (BirdFont.logging) { 421 init_logfile (); 422 } 423 424 if (!args.has_argument ("--no-translation")) { 425 init_gettext (); 426 } 427 428 if (args.has_argument ("--help")) { 429 args.print_help (); 430 Process.exit (0); 431 } 432 433 err_arg = args.validate (); 434 if (err_arg != 0) { 435 stdout.printf (@"Unknown parameter $(arg [err_arg])\n\n"); 436 args.print_help (); 437 Process.exit (0); 438 } 439 440 Preferences.load (); 441 442 Theme.set_default_colors (); 443 444 theme = Preferences.get ("theme"); 445 446 if (theme != "") { 447 Theme.load_theme (theme); 448 } else { 449 Theme.load_theme ("default.theme"); 450 } 451 452 current_font = new Font (); 453 current_font.set_name (""); 454 current_font.initialised = false; 455 current_glyph_collection = new GlyphCollection.with_glyph ('\0', ""); 456 457 experimental = args.has_argument ("--test"); 458 show_coordinates = args.has_argument ("--show-coordinates") || experimental; 459 fatal_wanings = args.has_argument ("--fatal-warning"); 460 win32 = (arg[0].index_of (".exe") > -1) 461 || arg[0] == "wine" 462 || args.has_argument ("--windows"); 463 464 #if MAC 465 mac = true; 466 #else 467 mac = args.has_argument ("--mac"); 468 #endif 469 470 if (program_path == null) { 471 exec_path = ""; 472 473 if (win32) { 474 // wine hack to get "." folder in win32 environment 475 i = arg[0].last_index_of ("\\"); 476 477 if (i != -1) { 478 exec_path = arg[0]; 479 exec_path = exec_path.substring (0, i); 480 exec_path = wine_to_unix_path (exec_path); 481 } 482 } else { 483 exec_path = "./"; 484 } 485 } else { 486 exec_path = (!) program_path; 487 } 488 489 if (args.get_file () != "") { 490 font_file = File.new_for_path (args.get_file ()); 491 492 if (!font_file.query_exists ()) { 493 stderr.printf (@"The file \"$(args.get_file ())\" was not found.\n"); 494 Process.exit (-1); 495 } 496 } 497 498 if (fatal_wanings) { 499 LogLevelFlags levels = LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL | LogLevelFlags.LEVEL_WARNING; 500 Log.set_handler (null, levels, fatal_warning); 501 } 502 503 Preferences.set_last_file (get_current_font ().get_path ()); 504 505 DefaultCharacterSet.create_default_character_sets (); 506 DefaultCharacterSet.get_characters_for_prefered_language (); 507 508 HeadTable.init (); 509 510 if (TestBirdFont.get_singleton ().test_cases_to_run != "All") { 511 TestBirdFont.run_tests (); 512 } 513 } 514 515 public static Argument get_arguments () { 516 return args; 517 } 518 519 public static void set_bundle_path (string path) { 520 bundle_path = path; 521 } 522 523 public static void init_gettext () { 524 // FIXME: android, this should be OK now 525 #if !ANDROID 526 string locale_directory = SearchPaths.get_locale_directory (); 527 Intl.setlocale (LocaleCategory.MESSAGES, ""); 528 Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "utf-8"); 529 Intl.bindtextdomain (GETTEXT_PACKAGE, locale_directory); 530 #endif 531 } 532 533 public static void load_font_from_command_line () { 534 string file = args.get_file (); 535 if (file != "") { 536 FileTab.load_font (file); 537 } 538 } 539 540 public static Font get_current_font () { 541 return current_font; 542 } 543 544 internal static void fatal_warning (string? log_domain, LogLevelFlags log_levels, string message) { 545 bool fatal = true; 546 547 if (log_domain != null) { 548 stderr.printf ("%s: \n", (!) log_domain); 549 } 550 551 stderr.printf ("\n%s\n\n", message); 552 assert (!fatal); 553 } 554 555 #if ANDROID 556 internal static void android_warning (string? log_domain, LogLevelFlags log_levels, string message) { 557 __android_log_print (ANDROID_LOG_WARN, "BirdFont", message); 558 } 559 #endif 560 561 public static Font new_font () { 562 current_font = new Font (); 563 564 if (!is_null (MainWindow.tools)) { 565 MainWindow.get_drawing_tools ().remove_all_grid_buttons (); 566 MainWindow.get_drawing_tools ().add_new_grid (1); 567 MainWindow.get_drawing_tools ().add_new_grid (2); 568 MainWindow.get_drawing_tools ().add_new_grid (4); 569 } 570 571 if (!is_null (Toolbox.background_tools)) { 572 Toolbox.background_tools.remove_images (); 573 } 574 575 KerningTools.update_kerning_classes (); 576 577 return current_font; 578 } 579 580 public static void set_settings_directory (string directory) { 581 settings_directory = directory; 582 } 583 584 public static File get_preview_directory () { 585 File settings = get_settings_directory (); 586 File backup = get_child(settings, "preview"); 587 588 if (!backup.query_exists ()) { 589 DirUtils.create ((!) backup.get_path (), 0755); 590 } 591 592 return backup; 593 } 594 595 internal static File get_thumbnail_directory () { 596 File thumbnails = get_child (get_settings_directory (), "thumbnails"); 597 598 if (!thumbnails.query_exists ()) { 599 DirUtils.create ((!) thumbnails.get_path (), 0755); 600 } 601 602 return thumbnails; 603 } 604 605 internal static File get_settings_directory () { 606 string home_path; 607 File home; 608 File settings; 609 610 #if ANDROID 611 home_path = "/data/data/org.birdfont.sefyr/files"; 612 home = File.new_for_path (home_path); 613 614 if (!home.query_exists ()) { 615 printd ("Create settings directory."); 616 DirUtils.create ((!) home.get_path (),0755); 617 } 618 #else 619 home_path = (settings_directory != null) 620 ? (!) settings_directory : Environment.get_user_config_dir (); 621 622 if (is_null (home_path)) { 623 warning ("No home directory set."); 624 home_path = "."; 625 } 626 627 home = File.new_for_path (home_path); 628 #endif 629 settings = get_child(home, "birdfont"); 630 631 if (!settings.query_exists ()) { 632 DirUtils.create ((!) settings.get_path (), 0755); 633 } 634 635 return settings; 636 } 637 638 internal static File get_backup_directory () { 639 File settings = get_settings_directory (); 640 File backup = get_child (settings, "backup"); 641 642 if (!backup.query_exists ()) { 643 DirUtils.create ((!) backup.get_path (), 0755); 644 } 645 646 return backup; 647 } 648 649 public static bool has_argument (string param) { 650 if (is_null (args)) { 651 return false; 652 } 653 654 return args.has_argument (param); 655 } 656 657 internal static string? get_argument (string param) { 658 return args.get_argument (param); 659 } 660 } 661 662 void init_logfile () { 663 DateTime t; 664 File settings; 665 string s; 666 File log; 667 668 try { 669 t = new DateTime.now_local (); 670 settings = BirdFont.get_settings_directory (); 671 s = t.to_string ().replace (":", "_"); 672 log = get_child (settings, @"birdfont_$s.log"); 673 674 BirdFont.logstream = new DataOutputStream (log.create (FileCreateFlags.REPLACE_DESTINATION)); 675 ((!)BirdFont.logstream).put_string ((!) log.get_path ()); 676 ((!)BirdFont.logstream).put_string ("\n"); 677 678 warning ("Logging to " + (!) log.get_path ()); 679 } catch (GLib.Error e) { 680 warning (e.message); 681 warning ((!) log.get_path ()); 682 } 683 684 LogLevelFlags levels = LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL | LogLevelFlags.LEVEL_WARNING | LogLevelFlags.LEVEL_DEBUG; 685 Log.set_handler (null, levels, log_warning); 686 687 BirdFont.logging = true; 688 689 printd (@"Program version: $(VERSION)\n"); 690 printd (@"built on $(BUILD_TIMESTAMP)\n"); 691 } 692 693 internal static void log_warning (string? log_domain, LogLevelFlags log_levels, string message) { 694 if (log_domain != null) { 695 printd ((!) log_domain); 696 } 697 698 printd ("\n"); 699 printd (message); 700 printd ("\n"); 701 printd ("\n"); 702 } 703 704 /** Write debug output to logfile. */ 705 public static void printd (string s) { 706 #if ANDROID 707 __android_log_print (ANDROID_LOG_WARN, "BirdFont", s); 708 #else 709 if (unlikely (BirdFont.logging)) { 710 try { 711 if (BirdFont.logstream != null) { 712 ((!)BirdFont.logstream).put_string (s); 713 } else { 714 warning ("No logstream."); 715 } 716 717 stderr.printf (s); 718 } catch (GLib.Error e) { 719 warning (e.message); 720 } 721 } 722 #endif 723 } 724 725 /** Translate string */ 726 public string t_ (string t) { 727 return _(t); 728 } 729 730 /** Translate mac menu items */ 731 public static string translate_mac (string t) { 732 string s = t_(t); 733 return s.replace ("_", ""); 734 } 735 736 /** Print a warning if Birdfont was started with the --test argument. */ 737 public static void warn_if_test (string message) { 738 if (BirdFont.has_argument ("--test")) { 739 warning (message); 740 } 741 } 742 743 /** Obtain a handle to a file in a folder. */ 744 public static File get_child (File folder, string file_name) { 745 string f; 746 string s; 747 string n; 748 749 // avoid drive letter problems on windows 750 751 f = (!) folder.get_path (); 752 s = (BirdFont.win32) ? "\\" : "/"; 753 754 n = file_name; 755 if (unlikely (BirdFont.win32 && file_name.index_of ("\\") != -1)) { 756 warning (@"File name contains path separator: $file_name, Directory: $f"); 757 n = n.substring (n.last_index_of ("\\")).replace ("\\", ""); 758 } 759 760 if (!f.has_suffix (s)) { 761 f += s; 762 } 763 764 printd (@"File: Directory: $f Name: $n\n"); 765 766 return File.new_for_path (f + n); 767 } 768 769 public static void set_drawing_callbacks (Drawing callbacks) { 770 BirdFont.drawing = callbacks; 771 } 772 773 } 774