The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

ExportTool.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/ExportTool.vala.
Merge branch 'master' of https://github.com/johanmattssonm/birdfont into HEAD
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 15 using Bird; 16 17 namespace BirdFont { 18 19 public class ExportTool : GLib.Object { 20 21 public ExportTool (string n) { 22 } 23 24 public static string export_selected_paths_to_svg () { 25 return export_current_glyph_to_string (true); 26 } 27 28 public static string export_selected_paths_to_inkscape_clipboard () { 29 return export_current_glyph_to_inkscape_clipboard (true); 30 } 31 32 public static string export_current_glyph_to_string (bool only_selected_paths = false) { 33 return export_to_string (MainWindow.get_current_glyph (), only_selected_paths); 34 } 35 36 public static string export_to_string (Glyph glyph, bool only_selected_paths) { 37 string name; 38 StringBuilder s; 39 40 name = XmlParser.encode (glyph.get_name ()); 41 s = new StringBuilder (); 42 43 s.append ("""<?xml version="1.0" encoding="utf-8"?> 44 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> 45 <svg version="1.0" 46 id="glyph_""" + name + """" 47 mlns="http://www.w3.org/2000/svg" 48 xmlns:xlink="http://www.w3.org/1999/xlink" 49 x="0px" 50 y="0px" 51 width="""" + @"$(glyph.get_width ())" + """px" 52 height="""" + @"$(glyph.get_height ())" + """px"> 53 """); 54 55 s.append (@"<g id=\"$(name)\">\n"); 56 57 s.append (get_svg_path_elements (glyph, only_selected_paths)); 58 59 s.append ("</g>\n"); 60 s.append ("</svg>"); 61 62 return s.str; 63 } 64 65 public static string export_current_glyph_to_inkscape_clipboard (bool only_selected_paths = false) { 66 return export_to_inkscape_clipboard (MainWindow.get_current_glyph (), only_selected_paths); 67 } 68 69 public static string export_to_inkscape_clipboard (Glyph glyph, bool only_selected_paths = false) { 70 StringBuilder s; 71 72 s = new StringBuilder (); 73 s.append ("""<?xml version="1.0" encoding="UTF-8" standalone="no"?>"""); 74 s.append ("\n"); 75 s.append ("<svg>\n"); 76 77 s.append ("""<inkscape:clipboard 78 id="clipboard3009" 79 style="color:#000000;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate" 80 min="0,0" 81 max="0,0" /> 82 """); 83 84 s.append (get_svg_path_elements (glyph, only_selected_paths)); 85 s.append ("</svg>"); 86 87 return s.str; 88 } 89 90 private static string get_svg_path_elements (Glyph glyph, bool only_selected_paths) { 91 string glyph_svg; 92 StringBuilder s; 93 string name; 94 name = glyph.get_name (); 95 96 s = new StringBuilder (); 97 glyph_svg = ""; 98 99 if (only_selected_paths) { 100 foreach (Path p in glyph.active_paths) { 101 glyph_svg += Svg.to_svg_path (p, glyph); 102 } 103 } else { 104 foreach (Path p in glyph.path_list) { 105 glyph_svg += Svg.to_svg_path (p, glyph); 106 } 107 } 108 109 s.append (@"<path "); 110 s.append (@"style=\"fill:#000000;stroke-width:0px\" "); 111 s.append (@"d=\"$(glyph_svg)\" id=\"path_$(name)\" />\n"); 112 113 return s.str; 114 } 115 116 public static void export_current_glyph () { 117 FileChooser fc = new FileChooser (); 118 fc.file_selected.connect ((f) => { 119 Glyph glyph = MainWindow.get_current_glyph (); 120 FontDisplay fd = MainWindow.get_current_display (); 121 string glyph_svg; 122 string svg_file; 123 File file; 124 DataOutputStream os; 125 string name; 126 string fn; 127 int i; 128 129 name = glyph.get_name (); 130 131 if (f == null) { 132 return; 133 } 134 135 svg_file = (!) f; 136 137 if (!(fd is Glyph)) { 138 return; 139 } 140 141 try { 142 i = 1; 143 fn = svg_file.replace (".svg", ""); 144 file = File.new_for_path (fn + ".svg"); 145 while (file.query_exists ()) { 146 file = File.new_for_path (fn + @"$i.svg"); 147 i++; 148 } 149 150 glyph_svg = export_current_glyph_to_string (); 151 os = new DataOutputStream (file.create(FileCreateFlags.REPLACE_DESTINATION)); 152 os.put_string (glyph_svg); 153 154 } catch (Error e) { 155 stderr.printf (@"Export \"$svg_file\" \n"); 156 critical (@"$(e.message)"); 157 } 158 }); 159 160 MainWindow.file_chooser (t_("Save"), fc, FileChooser.SAVE); 161 } 162 163 /* Font must be saved before export in order to know where the 164 * generated files should be stored. 165 */ 166 internal static void export_all () { 167 Font font = BirdFont.get_current_font (); 168 169 printd ("Exporting all fonts.\n"); 170 171 if (font.font_file == null) { 172 warning ("Font is not saved."); 173 } else { 174 do_export (); 175 } 176 } 177 178 static void do_export () { 179 bool f; 180 181 f = export_ttf_font (); 182 if (!f) { 183 warning ("Failed to export ttf font"); 184 } 185 186 f = export_svg_font (); 187 if (!f) { 188 warning ("Failed to export svg font"); 189 } 190 } 191 192 public static void generate_html_document (string html_file, Font font) { 193 File file = File.new_for_path (html_file); 194 DataOutputStream os; 195 string name = font.get_full_name (); 196 197 try { 198 os = new DataOutputStream (file.create(FileCreateFlags.REPLACE_DESTINATION)); 199 200 os.put_string ( 201 """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 202 <html> 203 <head> 204 205 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 206 <title>Lorem ipsum</title> 207 208 <style type="text/css"> 209 210 body { 211 text-rendering: optimizeLegibility; 212 font-feature-settings: "kern"; 213 -moz-font-feature-settings: "kern=1"; 214 -ms-font-feature-settings: "kern"; 215 -webkit-font-feature-settings: "kern"; 216 -o-font-feature-settings: "kern"; 217 } 218 219 @font-face { 220 font-family: '"""); os.put_string (@"$(name)"); os.put_string ("""SVG'; 221 src: url('"""); os.put_string (@"$(name).svg#$(name)"); os.put_string ("""') format('svg'); 222 } 223 """); 224 225 os.put_string (""" 226 @font-face { 227 font-family: '"""); os.put_string (@"$(name)"); os.put_string ("""TTF'; 228 src: url('"""); os.put_string (@"$(name).ttf"); os.put_string ("""') format('truetype'); 229 } 230 """); 231 232 os.put_string (""" 233 @font-face { 234 font-family: '"""); os.put_string (@"$(name)"); os.put_string ("""EOT'; 235 src: url('"""); os.put_string (@"$(name).eot"); os.put_string ("""'); 236 } 237 238 """); 239 240 os.put_string (""" 241 h1 { 242 font-weight:normal; 243 margin: 0 0 5px 0; 244 color: #afafaf; 245 } 246 247 h2 { 248 font-weight:normal; 249 margin: 0 0 5px 0; 250 color: #afafaf; 251 } 252 253 h3 { 254 font-weight:normal; 255 margin: 0 0 5px 0; 256 color: #afafaf; 257 } 258 259 h4 { 260 font-weight:normal; 261 margin: 0 0 5px 0; 262 color: #afafaf; 263 } 264 265 p { 266 margin: 0 0 5px 0; 267 color: #000000; 268 } 269 270 body { 271 margin: 30px 0 0 30px; 272 } 273 274 div { 275 font-family: """); 276 277 os.put_string ("'"); 278 os.put_string (@"$(name)"); 279 os.put_string ("EOT'"); 280 281 os.put_string (", '"); 282 os.put_string (@"$(name)"); 283 os.put_string ("TTF'"); 284 285 os.put_string (", '"); 286 os.put_string (@"$(name)"); 287 os.put_string ("SVG'"); 288 289 os.put_string (";"); 290 os.put_string (""" 291 margin: 0 0 30px 0; 292 } 293 294 h1.bigger { 295 font-size: 58pt; 296 } 297 298 h2.big { 299 font-size: 40pt; 300 } 301 302 h3.small { 303 font-size: 32pt; 304 } 305 306 h4.smaller { 307 font-size: 18pt; 308 } 309 310 h1.percent { 311 font-size: 100%; 312 } 313 314 p.bigger { 315 font-size: 32pt; 316 } 317 318 p.big { 319 font-size: 24pt; 320 } 321 322 p.small { 323 font-size: 18pt; 324 } 325 326 p.smaller { 327 font-size: 12pt; 328 } 329 330 </style> 331 """); 332 333 os.put_string ( 334 """ 335 </head> 336 <body> 337 338 <div> 339 <h1 class="bigger">Lorem ipsum</h1> 340 <p class="bigger">Dolor sit amet!</p> 341 </div> 342 343 <div> 344 <h2 class="big">Hamburgerfonstiv</h2> 345 <p class="big">""" + name + """</p> 346 </div> 347 348 <div> 349 <h3 class="big">Handgloves & Mittoms</h3> 350 <p class="big">Bibliography</p> 351 </p> 352 </div> 353 354 <div> 355 <h4 class="smaller">Headline 16pt</h4> 356 <p class="smaller">Ett litet stycke svalhjärta.</p> 357 </div> 358 359 <div> 360 <h4 class="smaller">""" + t_("Alphabet") + """</h4> 361 <p class="smaller">""" + t_("a b c d e f g h i j k l m n o p q r s t u v w x y z") + """</p> 362 <p class="smaller">""" + t_("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z") + """</p> 363 <p class="smaller">0 1 2 3 4 5 6 7 8 9</p> 364 </div> 365 366 </body> 367 </html> 368 """); 369 370 } catch (GLib.Error e) { 371 warning (e.message); 372 } 373 } 374 375 public static bool export_ttf_font () { 376 Font font = BirdFont.get_current_font (); 377 File f = font.get_folder (); 378 379 printd (@"export_ttf_font:\n"); 380 printd (@"font.get_path (): $(font.get_path ())\n"); 381 printd (@"font.get_folder_path (): $(font.get_folder_path ())\n"); 382 printd (@"font.get_folder (): $((!) f.get_path ())\n"); 383 384 return export_ttf_font_path (f); 385 } 386 387 public static bool export_ttf_font_path (File folder) { 388 Font current_font = BirdFont.get_current_font (); 389 File ttf_file; 390 File eot_file; 391 bool done = true; 392 393 try { 394 ttf_file = get_child (folder, current_font.get_full_name () + ".ttf"); 395 eot_file = get_child (folder, current_font.get_full_name () + ".eot"); 396 397 printd (@"Writing TTF fonts to $((!) ttf_file.get_path ())\n"); 398 399 if (ttf_file.query_exists ()) { 400 ttf_file.delete (); 401 } 402 403 if (eot_file.query_exists ()) { 404 eot_file.delete (); 405 } 406 407 write_ttf ((!) ttf_file.get_path ()); 408 write_eot ((!) ttf_file.get_path (), (!) eot_file.get_path ()); 409 } catch (Error e) { 410 critical (@"$(e.message)"); 411 done = false; 412 } 413 414 return done; 415 } 416 417 public static bool export_svg_font () { 418 Font font = BirdFont.get_current_font (); 419 return export_svg_font_path (font.get_folder ()); 420 } 421 422 public static bool export_svg_font_path (File folder) { 423 Font font = BirdFont.get_current_font (); 424 string file_name = @"$(font.get_full_name ()).svg"; 425 File file; 426 SvgFontFormatWriter fo; 427 428 try { 429 file = get_child (folder, file_name); 430 431 if (file.query_exists ()) { 432 file.delete (); 433 } 434 435 fo = new SvgFontFormatWriter (); 436 fo.open (file); 437 fo.write_font_file (font); 438 fo.close (); 439 } catch (Error e) { 440 critical (@"$(e.message)"); 441 return false; 442 } 443 444 return true; 445 } 446 447 static void write_ttf (string ttf) { 448 OpenFontFormatWriter fo = new OpenFontFormatWriter (); 449 Font f = BirdFont.get_current_font (); 450 File file = (!) File.new_for_path (ttf); 451 452 try { 453 fo.open (file); 454 fo.write_ttf_font (f); 455 fo.close (); 456 } catch (Error e) { 457 warning (@"Can't write TTF font to $ttf"); 458 critical (@"$(e.message)"); 459 } 460 } 461 462 static void write_eot (string ttf, string eot) { 463 EotWriter fo = new EotWriter (ttf, eot); 464 465 try { 466 fo.write (); 467 } catch (Error e) { 468 warning (@"EOF conversion falied, $ttf -> $eot"); 469 critical (@"$(e.message)"); 470 } 471 } 472 } 473 474 } 475