.
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
38 if (arg.length < 3) {
39 print_import_help (arg);
40 return -1;
41 }
42
43 bf_file = build_absoulute_path (arg[1]);
44
45 for (int i = 2; i < arg.length; i++) {
46 svg_files.add (arg[i]);
47 }
48
49 bf = File.new_for_path (bf_file);
50 foreach (string f in svg_files) {
51 svg = File.new_for_path (f);
52
53 if (!svg.query_exists ()) {
54 stdout.printf (@"$f " + t_("does not exist.") + "\n");
55 return -1;
56 }
57 }
58
59 font = BirdFont.get_current_font ();
60
61 if (!bf.query_exists ()) {
62 stdout.printf (@"$bf_file " + t_("does not exist.") + " ");
63 stdout.printf (t_("A new font will be created.") + "\n");
64 font.set_file (bf_file);
65 } else {
66 font.set_file (bf_file);
67 if (!font.load ()) {
68 warning (@"Failed to load font $bf_file.\n");
69
70 if (!bf_file.has_suffix (".bf") && !bf_file.has_suffix (".birdfont")) {
71 warning (@"Is it a .bf file?\n");
72 }
73
74 return -1;
75 }
76 }
77
78 font.save_backup ();
79
80 foreach (string f in svg_files) {
81 svg = File.new_for_path (f);
82 imported = import_svg_file (font, svg);
83
84 if (!imported) {
85 stdout.printf (t_("Failed to import") + " " + f + "\n");
86 stdout.printf (t_("Aborting") + "\n");
87 return -1;
88 }
89 }
90
91 font.save_bf ();
92
93 return 0;
94 }
95
96 public static bool import_svg_file (Font font, File svg_file) {
97 string file_name = (!) svg_file.get_basename ();
98 string glyph_name;
99 StringBuilder n;
100 Glyph glyph;
101 GlyphCollection? gc = null;
102 GlyphCollection glyph_collection;
103 unichar character;
104 GlyphCanvas canvas;
105
106 glyph_name = file_name.replace (".svg", "");
107 glyph_name = glyph_name.replace (".SVG", "");
108
109 if (glyph_name.char_count () > 1) {
110 if (glyph_name.has_prefix ("U+")) {
111 n = new StringBuilder ();
112 n.append_unichar (Font.to_unichar (glyph_name));
113 glyph_name = n.str;
114 gc = font.get_glyph_collection (glyph_name);
115 } else {
116 gc = font.get_glyph_collection_by_name (glyph_name);
117
118 if (gc == null) {
119 stdout.printf (file_name + " " + t_("is not the name of a glyph or a Unicode value.") + "\n");
120 stdout.printf (t_("Unicode values must start with U+.") + "\n");
121 return false;
122 }
123 }
124 } else {
125 gc = font.get_glyph_collection (glyph_name);
126 }
127
128 if (gc != null) {
129 glyph_collection = (!) gc;
130 character = glyph_collection.get_unicode_character ();
131 glyph = new Glyph (glyph_collection.get_name (), character);
132 glyph.version_id = glyph_collection.get_last_id () + 1;
133 glyph_collection.insert_glyph (glyph, true);
134 } else {
135 return_val_if_fail (glyph_name.char_count () == 1, false);
136 character = glyph_name.get_char (0);
137 glyph_collection = new GlyphCollection (character, glyph_name);
138 glyph = new Glyph (glyph_name, character);
139 glyph_collection.insert_glyph (glyph, true);
140 font.add_glyph_collection (glyph_collection);
141 }
142
143 canvas = MainWindow.get_glyph_canvas ();
144 canvas.set_current_glyph_collection (glyph_collection);
145
146 stdout.printf (t_("Adding"));
147 stdout.printf (" ");
148 stdout.printf ((!) svg_file.get_basename ());
149 stdout.printf (" ");
150 stdout.printf (t_("to"));
151 stdout.printf (" ");
152 stdout.printf (t_("Glyph"));
153 stdout.printf (": ");
154 stdout.printf (glyph.get_name ());
155 stdout.printf (" ");
156 stdout.printf (t_("Version"));
157 stdout.printf (": ");
158 stdout.printf (@"$(glyph.version_id)");
159 stdout.printf ("\n");
160
161 SvgParser.import_svg ((!) svg_file.get_path ());
162
163 return true;
164 }
165
166 }
167