The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

ImportUtils.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
Circle boundaries heads/master
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 namespace BirdFont { 15 16 static void print_import_help (string[] arg) { 17 stdout.printf (t_("Usage:")); 18 stdout.printf (arg[0]); 19 stdout.printf (" " + t_("BF-FILE") + " " + t_("SVG-FILES ...") +"\n"); 20 stdout.printf ("\n"); 21 } 22 23 public static int run_import (string[] arg) { 24 string bf_file = ""; 25 Gee.ArrayList<string> svg_files = new Gee.ArrayList<string> (); 26 File bf; 27 File svg; 28 Font font; 29 bool imported; 30 31 Theme.set_default_colors (); 32 Preferences.load (); 33 BirdFont.args = new Argument (""); 34 BirdFont.current_font = new Font (); 35 BirdFont.current_glyph_collection = new GlyphCollection.with_glyph ('\0', ""); 36 MainWindow.init (); 37 SvgType type = SvgType.REGULAR; 38 39 if (arg.length < 3) { 40 print_import_help (arg); 41 return -1; 42 } 43 44 bf_file = build_absoulute_path (arg[1]); 45 46 for (int i = 2; i < arg.length; i++) { 47 if (arg[i] == "--color") { 48 type = SvgType.COLOR; 49 } else { 50 svg_files.add (arg[i]); 51 } 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, type); 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 public static bool import_svg_file (Font font, File svg_file, SvgType type) { 102 string file_name = (!) svg_file.get_basename (); 103 string glyph_name; 104 StringBuilder n; 105 Glyph glyph; 106 GlyphCollection? gc = null; 107 GlyphCollection glyph_collection; 108 unichar character; 109 GlyphCanvas canvas; 110 111 glyph_name = file_name.replace (".svg", ""); 112 glyph_name = glyph_name.replace (".SVG", ""); 113 114 if (glyph_name.char_count () > 1) { 115 if (glyph_name.has_prefix ("U+")) { 116 n = new StringBuilder (); 117 n.append_unichar (Font.to_unichar (glyph_name)); 118 glyph_name = n.str; 119 gc = font.get_glyph_collection (glyph_name); 120 } else { 121 gc = font.get_glyph_collection_by_name (glyph_name); 122 123 if (gc == null) { 124 stdout.printf (file_name + " " + t_("is not the name of a glyph or a Unicode value.") + "\n"); 125 stdout.printf (t_("Unicode values must start with U+.") + "\n"); 126 return false; 127 } 128 } 129 } else { 130 gc = font.get_glyph_collection (glyph_name); 131 } 132 133 if (gc != null) { 134 glyph_collection = (!) gc; 135 character = glyph_collection.get_unicode_character (); 136 glyph = new Glyph (glyph_collection.get_name (), character); 137 glyph.version_id = glyph_collection.get_last_id () + 1; 138 glyph_collection.insert_glyph (glyph, true); 139 } else { 140 return_val_if_fail (glyph_name.char_count () == 1, false); 141 character = glyph_name.get_char (0); 142 glyph_collection = new GlyphCollection (character, glyph_name); 143 glyph = new Glyph (glyph_name, character); 144 glyph_collection.insert_glyph (glyph, true); 145 font.add_glyph_collection (glyph_collection); 146 } 147 148 canvas = MainWindow.get_glyph_canvas (); 149 canvas.set_current_glyph_collection (glyph_collection); 150 151 stdout.printf (t_("Adding")); 152 stdout.printf (" "); 153 stdout.printf ((!) svg_file.get_basename ()); 154 stdout.printf (" "); 155 stdout.printf (t_("to")); 156 stdout.printf (" "); 157 stdout.printf (t_("Glyph")); 158 stdout.printf (": "); 159 stdout.printf (glyph.get_name ()); 160 stdout.printf (" "); 161 stdout.printf (t_("Version")); 162 stdout.printf (": "); 163 stdout.printf (@"$(glyph.version_id)"); 164 stdout.printf ("\n"); 165 166 if (type == SvgType.COLOR) { 167 SvgParser.import_color_svg (glyph, (!) svg_file.get_path ()); 168 } else { 169 SvgParser.import_svg ((!) svg_file.get_path ()); 170 } 171 172 return true; 173 } 174 175 } 176