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.
Merge ../birdfont-2.x
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 public static Drawing? drawing = null; 94 95 public static string? settings_subdirectory = null; 96 public static string? sandbox_directory = null; 97 98 public BirdFont () { 99 set_defaul_drawing_callbacks (); 100 } 101 102 void set_defaul_drawing_callbacks () { 103 if (drawing == null) { 104 drawing = new Drawing (); 105 } 106 } 107 108 /** 109 * @param arg command line arguments 110 * @param program path 111 * @param setting subdirectory 112 */ 113 public void init (string[] arg, string? program_path, string? settings_subdir, string? sandbox_path) { 114 int i; 115 File font_file; 116 string exec_path; 117 string theme; 118 int default_theme_version; 119 string theme_version; 120 CharDatabaseParser parser; 121 CodePageBits codepage_bits; 122 123 set_settings_subdir (settings_subdir); 124 sandbox_directory = sandbox_path; 125 126 args = new Argument.command_line (arg); 127 Font.empty = new Font (); 128 129 #if ANDROID 130 BirdFont.logging = true; 131 132 __android_log_print (ANDROID_LOG_WARN, "BirdFont", @"libbirdfont version $VERSION"); 133 LogLevelFlags log_levels = LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL | LogLevelFlags.LEVEL_WARNING; 134 Log.set_handler (null, log_levels, android_warning); 135 136 android = true; 137 #else 138 stdout.printf ("birdfont version %s\n", VERSION); 139 140 android = args.has_argument ("--android"); 141 142 if (!BirdFont.logging) { 143 BirdFont.logging = args.has_argument ("--log"); 144 } 145 #endif 146 147 if (BirdFont.logging) { 148 init_logfile (); 149 } 150 151 if (!args.has_argument ("--no-translation")) { 152 init_gettext (); 153 } 154 155 if (args.has_argument ("--help")) { 156 args.print_help (); 157 Process.exit (0); 158 } 159 160 #if !MAC 161 int err_arg = args.validate (); 162 if (err_arg != 0) { 163 stdout.printf (@"Unknown parameter $(arg [err_arg])\n\n"); 164 args.print_help (); 165 Process.exit (0); 166 } 167 #endif 168 169 CanvasSettings.init (); 170 Preferences.load (); 171 172 // always load default theme when names in theme does change 173 default_theme_version = 1; 174 theme = Preferences.get ("theme"); 175 theme_version = Preferences.get ("theme_version"); 176 177 Theme.set_default_colors (); 178 179 if (theme_version == "" || int.parse (theme_version) < default_theme_version) { 180 181 Theme.load_theme ("dark.theme"); 182 Preferences.set ("theme", "dark.theme"); 183 } else { 184 if (theme != "") { 185 Theme.load_theme (theme); 186 } else { 187 Theme.load_theme ("dark.theme"); 188 } 189 } 190 191 Preferences.set ("theme_version", @"$default_theme_version"); 192 193 current_font = new Font (); 194 current_font.set_name (""); 195 current_font.initialised = false; 196 current_glyph_collection = new GlyphCollection.with_glyph ('\0', ""); 197 198 experimental = args.has_argument ("--test"); 199 show_coordinates = args.has_argument ("--show-coordinates") || experimental; 200 fatal_wanings = args.has_argument ("--fatal-warning"); 201 win32 = (arg[0].index_of (".exe") > -1) 202 || arg[0] == "wine" 203 || args.has_argument ("--windows"); 204 205 #if MAC 206 mac = true; 207 #else 208 mac = args.has_argument ("--mac"); 209 #endif 210 211 if (program_path == null) { 212 exec_path = ""; 213 214 if (win32) { 215 // wine hack to get "." folder in win32 environment 216 i = arg[0].last_index_of ("\\"); 217 218 if (i != -1) { 219 exec_path = arg[0]; 220 exec_path = exec_path.substring (0, i); 221 exec_path = wine_to_unix_path (exec_path); 222 } 223 } else { 224 exec_path = "./"; 225 } 226 } else { 227 exec_path = (!) program_path; 228 } 229 230 if (args.get_file () != "") { 231 font_file = File.new_for_path (args.get_file ()); 232 233 if (!font_file.query_exists ()) { 234 stderr.printf (@"The file \"$(args.get_file ())\" was not found.\n"); 235 Process.exit (-1); 236 } 237 } 238 239 if (fatal_wanings) { 240 LogLevelFlags levels = LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL | LogLevelFlags.LEVEL_WARNING; 241 Log.set_handler (null, levels, fatal_warning); 242 } 243 244 Preferences.set_last_file (get_current_font ().get_path ()); 245 246 DefaultCharacterSet.create_default_character_sets (); 247 DefaultCharacterSet.get_characters_for_prefered_language (); 248 249 HeadTable.init (1024); 250 251 if (TestBirdFont.get_singleton ().test_cases_to_run != "All") { 252 TestBirdFont.run_tests (); 253 } 254 255 if (has_argument ("--parse-ucd")) { 256 parser = new CharDatabaseParser (); 257 parser.regenerate_database (); 258 } 259 260 if (has_argument ("--codepages")) { 261 codepage_bits = new CodePageBits (); 262 codepage_bits.generate_codepage_database (); 263 } 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 File settings = get_settings_directory (); 351 File backup = get_child(settings, "preview"); 352 353 if (!backup.query_exists ()) { 354 DirUtils.create ((!) backup.get_path (), 0755); 355 } 356 357 return backup; 358 } 359 360 public static void set_settings_subdir (string? subdir) { 361 settings_subdirectory = subdir; 362 } 363 364 internal static File get_settings_directory () { 365 string home_path; 366 File home; 367 File settings; 368 369 #if ANDROID 370 home_path = "/data/data/org.birdfont.sefyr/files"; 371 home = File.new_for_path (home_path); 372 373 if (!home.query_exists ()) { 374 printd ("Create settings directory."); 375 DirUtils.create ((!) home.get_path (),0755); 376 } 377 #else 378 if (sandbox_directory != null) { 379 home = File.new_for_path ((!) sandbox_directory); 380 } else { 381 home_path = (settings_directory != null) 382 ? (!) settings_directory : Environment.get_user_config_dir (); 383 384 if (is_null (home_path)) { 385 warning ("No home directory set."); 386 home_path = "."; 387 } 388 389 home = File.new_for_path (home_path); 390 } 391 #endif 392 393 if (settings_subdirectory != null) { 394 settings = get_child(home, (!) settings_subdirectory); 395 } else { 396 settings = get_child(home, "birdfont"); 397 } 398 399 if (!settings.query_exists ()) { 400 DirUtils.create ((!) settings.get_path (), 0755); 401 } 402 403 return settings; 404 } 405 406 internal static File get_backup_directory () { 407 File settings = get_settings_directory (); 408 File backup = get_child (settings, "backup"); 409 410 if (!backup.query_exists ()) { 411 DirUtils.create ((!) backup.get_path (), 0755); 412 } 413 414 return backup; 415 } 416 417 public static bool has_argument (string param) { 418 if (is_null (args)) { 419 return false; 420 } 421 422 return args.has_argument (param); 423 } 424 425 internal static string? get_argument (string param) { 426 return args.get_argument (param); 427 } 428 429 public static void debug_message (string s) { 430 if (unlikely (has_logging ())) { 431 try { 432 if (BirdFont.logstream != null) { 433 ((!)BirdFont.logstream).put_string (s); 434 ((!)BirdFont.logstream).flush (); 435 } else { 436 warning ("No logstream."); 437 } 438 439 stderr.printf (s); 440 } catch (GLib.Error e) { 441 warning (e.message); 442 } 443 } 444 } 445 } 446 447 void init_logfile () { 448 DateTime t; 449 File settings; 450 string s; 451 File log; 452 453 try { 454 t = new DateTime.now_local (); 455 settings = BirdFont.get_settings_directory (); 456 s = t.to_string ().replace (":", "_"); 457 log = get_child (settings, @"birdfont_$s.log"); 458 459 BirdFont.logstream = new DataOutputStream (log.create (FileCreateFlags.REPLACE_DESTINATION)); 460 ((!)BirdFont.logstream).put_string ((!) log.get_path ()); 461 ((!)BirdFont.logstream).put_string ("\n"); 462 463 warning ("Logging to " + (!) log.get_path ()); 464 } catch (GLib.Error e) { 465 warning (e.message); 466 warning ((!) log.get_path ()); 467 } 468 469 LogLevelFlags levels = LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL | LogLevelFlags.LEVEL_WARNING | LogLevelFlags.LEVEL_DEBUG; 470 Log.set_handler (null, levels, log_warning); 471 472 BirdFont.logging = true; 473 474 printd (@"Program version: $(VERSION)\n"); 475 } 476 477 internal static void log_warning (string? log_domain, LogLevelFlags log_levels, string message) { 478 if (log_domain != null) { 479 printd ((!) log_domain); 480 } 481 482 printd ("\n"); 483 printd (message); 484 printd ("\n"); 485 printd ("\n"); 486 } 487 488 /** Write debug output to logfile. */ 489 public static void printd (string s) { 490 #if ANDROID 491 __android_log_print (ANDROID_LOG_WARN, "BirdFont", s); 492 #else 493 BirdFont.debug_message (s); 494 #endif 495 } 496 497 /** Translate string */ 498 public string t_ (string t) { 499 #if ANDROID 500 return t; 501 #else 502 return _(t); 503 #endif 504 } 505 506 /** Translate mac menu items */ 507 public static string translate_mac (string t) { 508 string s = t_(t); 509 return s.replace ("_", ""); 510 } 511 512 /** Print a warning if Birdfont was started with the --test argument. */ 513 public static void warn_if_test (string message) { 514 if (BirdFont.has_argument ("--test")) { 515 warning (message); 516 } 517 } 518 519 /** Obtain a handle to a file in a folder. */ 520 public static File get_child (File folder, string file_name) { 521 string f; 522 string s; 523 string n; 524 525 // avoid drive letter problems on windows 526 527 f = (!) folder.get_path (); 528 529 #if LINUX 530 s = "/"; 531 #else 532 s = (BirdFont.win32) ? "\\" : "/"; 533 #endif 534 535 n = file_name; 536 if (unlikely (BirdFont.win32 && file_name.index_of ("\\") != -1)) { 537 warning (@"File name contains path separator: $file_name, Directory: $f"); 538 n = n.substring (n.last_index_of ("\\")).replace ("\\", ""); 539 } 540 541 if (!f.has_suffix (s)) { 542 f += s; 543 } 544 545 printd (@"File in Directory: $f Name: $n\n"); 546 547 return File.new_for_path (f + n); 548 } 549 550 public static void set_drawing_callbacks (Drawing callbacks) { 551 BirdFont.drawing = callbacks; 552 } 553 554 } 555