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.
Fix self intersecting stroke
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 public static void generate_html_document (string html_file, Font font) { 164 File file = File.new_for_path (html_file); 165 DataOutputStream os; 166 string name = ExportSettings.get_file_name (font); 167 168 try { 169 os = new DataOutputStream (file.create(FileCreateFlags.REPLACE_DESTINATION)); 170 171 os.put_string ( 172 """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 173 <html> 174 <head> 175 176 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 177 <title>Lorem ipsum</title> 178 179 <style type="text/css"> 180 181 body { 182 text-rendering: optimizeLegibility; 183 font-feature-settings: "kern"; 184 -moz-font-feature-settings: "kern=1"; 185 -ms-font-feature-settings: "kern"; 186 -webkit-font-feature-settings: "kern"; 187 -o-font-feature-settings: "kern"; 188 } 189 190 @font-face { 191 font-family: '"""); os.put_string (@"$(name)"); os.put_string ("""SVG'; 192 src: url('"""); os.put_string (@"$(name).svg#$(name)"); os.put_string ("""') format('svg'); 193 } 194 """); 195 196 os.put_string (""" 197 @font-face { 198 font-family: '"""); os.put_string (@"$(name)"); os.put_string ("""TTF'; 199 src: url('"""); os.put_string (@"$(name).ttf"); os.put_string ("""') format('truetype'); 200 } 201 """); 202 203 os.put_string (""" 204 @font-face { 205 font-family: '"""); os.put_string (@"$(name)"); os.put_string ("""EOT'; 206 src: url('"""); os.put_string (@"$(name).eot"); os.put_string ("""'); 207 } 208 209 """); 210 211 os.put_string (""" 212 h1 { 213 font-weight:normal; 214 margin: 0 0 5px 0; 215 color: #afafaf; 216 } 217 218 h2 { 219 font-weight:normal; 220 margin: 0 0 5px 0; 221 color: #afafaf; 222 } 223 224 h3 { 225 font-weight:normal; 226 margin: 0 0 5px 0; 227 color: #afafaf; 228 } 229 230 h4 { 231 font-weight:normal; 232 margin: 0 0 5px 0; 233 color: #afafaf; 234 } 235 236 p { 237 margin: 0 0 5px 0; 238 color: #000000; 239 } 240 241 body { 242 margin: 30px 0 0 30px; 243 } 244 245 div { 246 font-family: """); 247 248 os.put_string ("'"); 249 os.put_string (@"$(name)"); 250 os.put_string ("EOT'"); 251 252 os.put_string (", '"); 253 os.put_string (@"$(name)"); 254 os.put_string ("TTF'"); 255 256 os.put_string (", '"); 257 os.put_string (@"$(name)"); 258 os.put_string ("SVG'"); 259 260 os.put_string (";"); 261 os.put_string (""" 262 margin: 0 0 30px 0; 263 } 264 265 h1.bigger { 266 font-size: 58pt; 267 } 268 269 h2.big { 270 font-size: 40pt; 271 } 272 273 h3.small { 274 font-size: 32pt; 275 } 276 277 h4.smaller { 278 font-size: 18pt; 279 } 280 281 h1.percent { 282 font-size: 100%; 283 } 284 285 p.bigger { 286 font-size: 32pt; 287 } 288 289 p.big { 290 font-size: 24pt; 291 } 292 293 p.small { 294 font-size: 18pt; 295 } 296 297 p.smaller { 298 font-size: 12pt; 299 } 300 301 </style> 302 """); 303 304 os.put_string ( 305 """ 306 </head> 307 <body> 308 309 <div> 310 <h1 class="bigger">Lorem ipsum</h1> 311 <p class="bigger">Dolor sit amet!</p> 312 </div> 313 314 <div> 315 <h2 class="big">Hamburgerfonstiv</h2> 316 <p class="big">""" + name + """</p> 317 </div> 318 319 <div> 320 <h3 class="big">Handgloves & Mittoms</h3> 321 <p class="big">Bibliography</p> 322 </p> 323 </div> 324 325 <div> 326 <h4 class="smaller">Headline 16pt</h4> 327 <p class="smaller">Ett litet stycke svalhjärta.</p> 328 </div> 329 330 <div> 331 <h4 class="smaller">""" + t_("Alphabet") + """</h4> 332 <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> 333 <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> 334 <p class="smaller">0 1 2 3 4 5 6 7 8 9</p> 335 </div> 336 337 </body> 338 </html> 339 """); 340 341 } catch (GLib.Error e) { 342 warning (e.message); 343 } 344 } 345 346 public static bool export_ttf_font () { 347 Font font = BirdFont.get_current_font (); 348 File f = font.get_folder (); 349 350 printd (@"export_ttf_font:\n"); 351 printd (@"font.get_path (): $(font.get_path ())\n"); 352 printd (@"font.get_folder_path (): $(font.get_folder_path ())\n"); 353 printd (@"font.get_folder (): $((!) f.get_path ())\n"); 354 355 return export_ttf_font_path (f); 356 } 357 358 public static bool export_ttf_font_path (File folder, bool use_export_settings = true) { 359 Font current_font = BirdFont.get_current_font (); 360 File ttf_file; 361 File eot_file; 362 bool done = true; 363 364 try { 365 ttf_file = get_child (folder, ExportSettings.get_file_name (current_font) + ".ttf"); 366 eot_file = get_child (folder, ExportSettings.get_file_name (current_font) + ".eot"); 367 368 printd (@"Writing TTF fonts to $((!) ttf_file.get_path ())\n"); 369 370 if (ttf_file.query_exists ()) { 371 ttf_file.delete (); 372 } 373 374 if (eot_file.query_exists ()) { 375 eot_file.delete (); 376 } 377 378 write_ttf ((!) ttf_file.get_path ()); 379 380 if (!use_export_settings || ExportSettings.export_eot_setting (current_font)) { 381 write_eot ((!) ttf_file.get_path (), (!) eot_file.get_path ()); 382 } 383 384 if (use_export_settings && !ExportSettings.export_ttf_setting (current_font)) { 385 if (ttf_file.query_exists ()) { 386 ttf_file.delete (); 387 } 388 } 389 } catch (Error e) { 390 critical (@"$(e.message)"); 391 done = false; 392 } 393 394 return done; 395 } 396 397 public static bool export_svg_font () { 398 Font font = BirdFont.get_current_font (); 399 return export_svg_font_path (font.get_folder ()); 400 } 401 402 public static bool export_svg_font_path (File folder) { 403 Font font = BirdFont.get_current_font (); 404 string file_name = @"$(ExportSettings.get_file_name (font)).svg"; 405 File file; 406 SvgFontFormatWriter fo; 407 408 try { 409 file = get_child (folder, file_name); 410 411 if (file.query_exists ()) { 412 file.delete (); 413 } 414 415 fo = new SvgFontFormatWriter (); 416 fo.open (file); 417 fo.write_font_file (font); 418 fo.close (); 419 } catch (Error e) { 420 critical (@"$(e.message)"); 421 return false; 422 } 423 424 return true; 425 } 426 427 static void write_ttf (string ttf) { 428 OpenFontFormatWriter fo = new OpenFontFormatWriter (); 429 Font f = BirdFont.get_current_font (); 430 File file = (!) File.new_for_path (ttf); 431 432 try { 433 fo.open (file); 434 fo.write_ttf_font (f); 435 fo.close (); 436 } catch (Error e) { 437 warning (@"Can't write TTF font to $ttf"); 438 critical (@"$(e.message)"); 439 } 440 } 441 442 static void write_eot (string ttf, string eot) { 443 EotWriter fo = new EotWriter (ttf, eot); 444 445 try { 446 fo.write (); 447 } catch (Error e) { 448 warning (@"EOF conversion falied, $ttf -> $eot"); 449 critical (@"$(e.message)"); 450 } 451 } 452 } 453 454 } 455