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 translation
1 /* 2 Copyright (C) 2012 2014 2015 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 internal static string build_absoulute_path (string file_name) { 23 File f = File.new_for_path (file_name); 24 return (!) f.get_path (); 25 } 26 27 public static string get_version () { 28 return VERSION; 29 } 30 31 public static void set_logging (bool log) { 32 BirdFont.logging = log; 33 } 34 35 public static string wine_to_unix_path (string exec_path) { 36 bool drive_c, drive_z; 37 int i; 38 string p, q; 39 40 p = exec_path; 41 p = p.replace ("\\", "/"); 42 43 drive_c = exec_path.index_of ("C:") == 0; 44 drive_z = exec_path.index_of ("Z:") == 0; 45 46 i = p.index_of (":"); 47 48 if (i != -1) { 49 p = p.substring (i + 2); 50 } 51 52 if (drive_c) { 53 q = @"/home/$(Environment.get_user_name ())/.wine/drive_c/" + p; 54 55 if (File.new_for_path (q).query_exists ()) { 56 return q; 57 } else { 58 return p; 59 } 60 } 61 62 if (drive_z) { 63 return ("/" + p).dup (); 64 } 65 66 return exec_path.dup (); 67 } 68 69 public bool is_null (void* n) { 70 return n == null; 71 } 72 73 public bool has_flag (uint32 flag, uint32 mask) { 74 return (flag & mask) > 0; 75 } 76 77 public class BirdFont { 78 public static Argument args; 79 public static bool experimental = false; 80 public static bool show_coordinates = false; 81 public static bool fatal_wanings = false; 82 public static bool win32 = false; 83 public static bool mac = false; 84 public static bool android = false; 85 public static string exec_path = ""; 86 public static string? bundle_path = null; 87 88 internal static bool logging = false; 89 public static DataOutputStream? logstream = null; 90 91 public static Font current_font; 92 public static GlyphCollection current_glyph_collection; 93 94 public static Drawing? drawing = null; 95 96 public static string? settings_subdirectory = null; 97 public static string? sandbox_directory = null; 98 99 public BirdFont () { 100 set_defaul_drawing_callbacks (); 101 } 102 103 void set_defaul_drawing_callbacks () { 104 if (drawing == null) { 105 drawing = new Drawing (); 106 } 107 } 108 109 /** 110 * @param arg command line arguments 111 * @param program path 112 * @param setting subdirectory 113 */ 114 public void init (string[] arg, string? program_path, string? settings_subdir, string? sandbox_path) { 115 int i; 116 File font_file; 117 string exec_path; 118 string theme; 119 int default_theme_version; 120 string theme_version; 121 CharDatabaseParser parser; 122 CodePageBits codepage_bits; 123 124 set_settings_subdir (settings_subdir); 125 sandbox_directory = sandbox_path; 126 127 args = new Argument.command_line (arg); 128 Font.empty = new Font (); 129 130 #if ANDROID 131 BirdFont.logging = true; 132 133 __android_log_print (ANDROID_LOG_WARN, "BirdFont", @"libbirdfont version $VERSION"); 134 LogLevelFlags log_levels = LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL | LogLevelFlags.LEVEL_WARNING; 135 Log.set_handler (null, log_levels, android_warning); 136 137 android = true; 138 #else 139 stdout.printf ("birdfont version %s\n", VERSION); 140 141 android = args.has_argument ("--android"); 142 143 if (!BirdFont.logging) { 144 BirdFont.logging = args.has_argument ("--log"); 145 } 146 #endif 147 148 if (BirdFont.logging) { 149 init_logfile (); 150 } 151 152 if (!args.has_argument ("--no-translation")) { 153 init_gettext (); 154 } 155 156 if (args.has_argument ("--help")) { 157 args.print_help (); 158 Process.exit (0); 159 } 160 161 #if !MAC 162 int err_arg = args.validate (); 163 if (err_arg != 0) { 164 stdout.printf (@"Unknown parameter $(arg [err_arg])\n\n"); 165 args.print_help (); 166 Process.exit (0); 167 } 168 #endif 169 Preferences.load (); 170 171 // always load default theme when names in theme does change 172 default_theme_version = 1; 173 theme = Preferences.get ("theme"); 174 theme_version = Preferences.get ("theme_version"); 175 176 Theme.set_default_colors (); 177 178 if (theme_version == "" || int.parse (theme_version) < default_theme_version) { 179 Theme.load_theme ("dark.theme"); 180 Preferences.set ("theme", "dark.theme"); 181 } else { 182 if (theme != "") { 183 Theme.load_theme (theme); 184 } else { 185 Theme.load_theme ("dark.theme"); 186 } 187 } 188 189 Preferences.set ("theme_version", @"$default_theme_version"); 190 191 current_font = new Font (); 192 current_font.set_name (""); 193 current_font.initialised = false; 194 current_glyph_collection = new GlyphCollection.with_glyph ('\0', ""); 195 196 experimental = args.has_argument ("--test"); 197 show_coordinates = args.has_argument ("--show-coordinates") || experimental; 198 fatal_wanings = args.has_argument ("--fatal-warning"); 199 win32 = (arg[0].index_of (".exe") > -1) 200 || arg[0] == "wine" 201 || args.has_argument ("--windows"); 202 203 #if MAC 204 mac = true; 205 #else 206 mac = args.has_argument ("--mac"); 207 #endif 208 209 if (program_path == null) { 210 exec_path = ""; 211 212 if (win32) { 213 // wine hack to get "." folder in win32 environment 214 i = arg[0].last_index_of ("\\"); 215 216 if (i != -1) { 217 exec_path = arg[0]; 218 exec_path = exec_path.substring (0, i); 219 exec_path = wine_to_unix_path (exec_path); 220 } 221 } else { 222 exec_path = "./"; 223 } 224 } else { 225 exec_path = (!) program_path; 226 } 227 228 if (args.get_file () != "") { 229 font_file = File.new_for_path (args.get_file ()); 230 231 if (!font_file.query_exists ()) { 232 stderr.printf (@"The file \"$(args.get_file ())\" was not found.\n"); 233 Process.exit (-1); 234 } 235 } 236 237 if (fatal_wanings) { 238 LogLevelFlags levels = LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL | LogLevelFlags.LEVEL_WARNING; 239 Log.set_handler (null, levels, fatal_warning); 240 } 241 242 Preferences.set_last_file (get_current_font ().get_path ()); 243 244 DefaultCharacterSet.create_default_character_sets (); 245 DefaultCharacterSet.get_characters_for_prefered_language (); 246 247 HeadTable.init (1024); 248 249 if (TestBirdFont.get_singleton ().test_cases_to_run != "All") { 250 TestBirdFont.run_tests (); 251 } 252 253 if (has_argument ("--parse-ucd")) { 254 parser = new CharDatabaseParser (); 255 parser.regenerate_database (); 256 } 257 258 if (has_argument ("--codepages")) { 259 codepage_bits = new CodePageBits (); 260 codepage_bits.generate_codepage_database (); 261 } 262 263 ExportTool.generate_html_template (); 264 } 265 266 public static bool has_logging () { 267 bool log; 268 269 lock (BirdFont.logging) { 270 log = BirdFont.logging; 271 } 272 273 return log; 274 } 275 276 public static Argument get_arguments () { 277 return args; 278 } 279 280 public static void set_bundle_path (string path) { 281 bundle_path = path; 282 } 283 284 public static void init_gettext () { 285 // FIXME: android, this should be OK now 286 #if !ANDROID 287 string locale_directory = SearchPaths.get_locale_directory (); 288 Intl.setlocale (LocaleCategory.MESSAGES, ""); 289 Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "utf-8"); 290 Intl.bindtextdomain (GETTEXT_PACKAGE, locale_directory); 291 #endif 292 } 293 294 public static void load_font_from_command_line () { 295 string file = args.get_file (); 296 if (file != "") { 297 RecentFiles.load_font (file); 298 } 299 } 300 301 public static Font get_current_font () { 302 return current_font; 303 } 304 305 internal static void fatal_warning (string? log_domain, LogLevelFlags log_levels, string message) { 306 bool fatal = true; 307 308 if (log_domain != null) { 309 stderr.printf ("%s: \n", (!) log_domain); 310 } 311 312 stderr.printf ("\n%s\n\n", message); 313 assert (!fatal); 314 } 315 316 #if ANDROID 317 internal static void android_warning (string? log_domain, LogLevelFlags log_levels, string message) { 318 __android_log_print (ANDROID_LOG_WARN, "BirdFont", message); 319 } 320 #endif 321 322 public static Font new_font () { 323 current_font = new Font (); 324 325 if (!is_null (MainWindow.tools)) { 326 MainWindow.get_drawing_tools ().remove_all_grid_buttons (); 327 DrawingTools.add_new_grid (1, false); 328 DrawingTools.add_new_grid (2, false); 329 DrawingTools.add_new_grid (4, false); 330 } 331 332 if (!is_null (Toolbox.background_tools)) { 333 Toolbox.background_tools.remove_images (); 334 } 335 336 KerningTools.update_kerning_classes (); 337 338 return current_font; 339 } 340 341 public static string? get_sandbox_directory () { 342 return sandbox_directory; 343 } 344 345 public static void set_settings_directory (string directory) { 346 settings_subdirectory = directory; 347 } 348 349 public static File get_preview_directory () { 350 string? export = BirdFont.get_current_font ().get_export_directory (); 351 352 if (export == null) { 353 warning ("No export directory is set."); 354 export = ""; 355 } 356 357 File e = File.new_for_path ((!) export); 358 File p = get_child(e, "preview"); 359 360 return p; 361 } 362 363 public static void set_settings_subdir (string? subdir) { 364 settings_subdirectory = subdir; 365 } 366 367 internal static File get_settings_directory () { 368 string home_path; 369 File home; 370 File settings; 371 372 #if ANDROID 373 home_path = "/data/data/org.birdfont.sefyr/files"; 374 home = File.new_for_path (home_path); 375 376 if (!home.query_exists ()) { 377 printd ("Create settings directory."); 378 DirUtils.create ((!) home.get_path (),0755); 379 } 380 #else 381 if (sandbox_directory != null) { 382 home = File.new_for_path ((!) sandbox_directory); 383 } else { 384 home_path = (settings_directory != null) 385 ? (!) settings_directory : Environment.get_user_config_dir (); 386 387 if (is_null (home_path)) { 388 warning ("No home directory set."); 389 home_path = "."; 390 } 391 392 home = File.new_for_path (home_path); 393 } 394 #endif 395 396 if (settings_subdirectory != null) { 397 settings = get_child(home, (!) settings_subdirectory); 398 } else { 399 settings = get_child(home, "birdfont"); 400 } 401 402 if (!settings.query_exists ()) { 403 DirUtils.create ((!) settings.get_path (), 0755); 404 } 405 406 return settings; 407 } 408 409 internal static File get_backup_directory () { 410 File settings = get_settings_directory (); 411 File backup = get_child (settings, "backup"); 412 413 if (!backup.query_exists ()) { 414 DirUtils.create ((!) backup.get_path (), 0755); 415 } 416 417 return backup; 418 } 419 420 public static bool has_argument (string param) { 421 if (is_null (args)) { 422 return false; 423 } 424 425 return args.has_argument (param); 426 } 427 428 internal static string? get_argument (string param) { 429 return args.get_argument (param); 430 } 431 432 public static void debug_message (string s) { 433 if (unlikely (has_logging ())) { 434 try { 435 if (BirdFont.logstream != null) { 436 ((!)BirdFont.logstream).put_string (s); 437 ((!)BirdFont.logstream).flush (); 438 } else { 439 warning ("No logstream."); 440 } 441 442 stderr.printf (s); 443 } catch (GLib.Error e) { 444 warning (e.message); 445 } 446 } 447 } 448 } 449 450 void init_logfile () { 451 DateTime t; 452 File settings; 453 string s; 454 File log; 455 456 try { 457 t = new DateTime.now_local (); 458 settings = BirdFont.get_settings_directory (); 459 s = t.to_string ().replace (":", "_"); 460 log = get_child (settings, @"birdfont_$s.log"); 461 462 BirdFont.logstream = new DataOutputStream (log.create (FileCreateFlags.REPLACE_DESTINATION)); 463 ((!)BirdFont.logstream).put_string ((!) log.get_path ()); 464 ((!)BirdFont.logstream).put_string ("\n"); 465 466 warning ("Logging to " + (!) log.get_path ()); 467 } catch (GLib.Error e) { 468 warning (e.message); 469 warning ((!) log.get_path ()); 470 } 471 472 LogLevelFlags levels = LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL | LogLevelFlags.LEVEL_WARNING | LogLevelFlags.LEVEL_DEBUG; 473 Log.set_handler (null, levels, log_warning); 474 475 BirdFont.logging = true; 476 477 printd (@"Program version: $(VERSION)\n"); 478 } 479 480 internal static void log_warning (string? log_domain, LogLevelFlags log_levels, string message) { 481 if (log_domain != null) { 482 printd ((!) log_domain); 483 } 484 485 printd ("\n"); 486 printd (message); 487 printd ("\n"); 488 printd ("\n"); 489 } 490 491 /** Write debug output to logfile. */ 492 public static void printd (string s) { 493 #if ANDROID 494 __android_log_print (ANDROID_LOG_WARN, "BirdFont", s); 495 #else 496 BirdFont.debug_message (s); 497 #endif 498 } 499 500 /** Translate string */ 501 public string t_ (string t) { 502 #if ANDROID 503 return t; 504 #else 505 string translate = Preferences.get ("translate"); 506 507 if (translate == "" || translate == "true") { 508 return _(t); 509 } else { 510 return t; 511 } 512 #endif 513 } 514 515 /** Translate mac menu items */ 516 public static string translate_mac (string t) { 517 string s = t_(t); 518 return s.replace ("_", ""); 519 } 520 521 /** Print a warning if Birdfont was started with the --test argument. */ 522 public static void warn_if_test (string message) { 523 if (BirdFont.has_argument ("--test")) { 524 warning (message); 525 } 526 } 527 528 /** Obtain a handle to a file in a folder. */ 529 public static File get_child (File folder, string file_name) { 530 string f; 531 string s; 532 string n; 533 534 // avoid drive letter problems on windows 535 536 f = (!) folder.get_path (); 537 538 #if LINUX 539 s = "/"; 540 #else 541 s = (BirdFont.win32) ? "\\" : "/"; 542 #endif 543 544 n = file_name; 545 if (unlikely (BirdFont.win32 && file_name.index_of ("\\") != -1)) { 546 warning (@"File name contains path separator: $file_name, Directory: $f"); 547 n = n.substring (n.last_index_of ("\\")).replace ("\\", ""); 548 } 549 550 if (!f.has_suffix (s)) { 551 f += s; 552 } 553 554 printd (@"File in Directory: $f Name: $n\n"); 555 556 return File.new_for_path (f + n); 557 } 558 559 public static void set_drawing_callbacks (Drawing callbacks) { 560 BirdFont.drawing = callbacks; 561 } 562 563 } 564