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.
Xml encoding
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 15 using B; 16 17 namespace BirdFont { 18 19 public class ExportTool : GLib.Object { 20 21 public static string? error_message = null; 22 23 public ExportTool (string n) { 24 } 25 26 public static void set_output_directory () { 27 #if MAC 28 Font font = BirdFont.get_current_font (); 29 string? path = font.get_export_directory (); 30 31 FileChooser fc = new FileChooser (); 32 fc.file_selected.connect ((p) => { 33 path = p; 34 }); 35 36 if (path == null) { 37 File export_path_handle = File.new_for_path (path); 38 39 if (!can_write (export_path_handle)) { 40 MainWindow.file_chooser (t_("Export"), fc, FileChooser.LOAD | FileChooser.DIRECTORY); 41 } 42 } 43 44 font.export_directory = path; 45 #endif 46 } 47 48 public static string export_selected_paths_to_svg () { 49 return export_current_glyph_to_string (true); 50 } 51 52 public static string export_selected_paths_to_inkscape_clipboard () { 53 return export_current_glyph_to_inkscape_clipboard (true); 54 } 55 56 public static string export_current_glyph_to_string (bool only_selected_paths = false) { 57 return export_to_string (MainWindow.get_current_glyph (), only_selected_paths); 58 } 59 60 public static string export_to_string (Glyph glyph, bool only_selected_paths) { 61 string name; 62 StringBuilder s; 63 64 name = BirdFontFile.encode (glyph.get_name ()); 65 s = new StringBuilder (); 66 67 s.append ("""<?xml version="1.0" encoding="utf-8"?> 68 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> 69 <svg version="1.0" 70 id="glyph_""" + name + """" 71 xmlns="http://www.w3.org/2000/svg" 72 xmlns:xlink="http://www.w3.org/1999/xlink" 73 x="0px" 74 y="0px" 75 width=""" + "\"" + @"$(glyph.get_width ())" + """px" 76 height=""" + "\"" + @"$(glyph.get_height ())" + """px"> 77 """); 78 79 s.append (@"<g id=\"$(name)\">\n"); 80 81 s.append (get_svg_path_elements (glyph, only_selected_paths)); 82 83 s.append ("</g>\n"); 84 s.append ("</svg>"); 85 86 return s.str; 87 } 88 89 public static string export_current_glyph_to_inkscape_clipboard (bool only_selected_paths = false) { 90 return export_to_inkscape_clipboard (MainWindow.get_current_glyph (), only_selected_paths); 91 } 92 93 public static string export_to_inkscape_clipboard (Glyph glyph, bool only_selected_paths = false) { 94 StringBuilder s; 95 96 s = new StringBuilder (); 97 s.append ("""<?xml version="1.0" encoding="UTF-8" standalone="no"?>"""); 98 s.append ("\n"); 99 s.append ("<svg>\n"); 100 101 s.append ("""<inkscape:clipboard 102 id="clipboard3009" 103 style="color:#000000;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate" 104 min="0,0" 105 max="0,0" /> 106 """); 107 108 s.append (get_svg_path_elements (glyph, only_selected_paths)); 109 s.append ("</svg>"); 110 111 return s.str; 112 } 113 114 private static string get_svg_path_elements (Glyph glyph, bool only_selected_paths) { 115 string glyph_svg; 116 StringBuilder s; 117 string name; 118 int id = 0; 119 120 name = glyph.get_name (); 121 122 Gee.ArrayList<Path> pl; 123 124 s = new StringBuilder (); 125 glyph_svg = ""; 126 127 pl = only_selected_paths ? glyph.active_paths : glyph.get_visible_paths (); 128 foreach (Path p in pl) { 129 if (p.stroke > 0) { 130 s.append (@"<path "); 131 s.append (@"style=\""); 132 s.append (@"fill:none;"); 133 s.append (@"stroke:#000000;"); 134 s.append (@"stroke-width:$(p.stroke)px;"); 135 136 if (p.line_cap == LineCap.ROUND) { 137 s.append (@"stroke-linecap:round;"); 138 } else if (p.line_cap == LineCap.SQUARE) { 139 s.append (@"stroke-linecap:square;"); 140 } 141 142 s.append (@"\" "); 143 144 s.append (@"d=\"$(Svg.to_svg_path (p, glyph))\" id=\"path_$(name)_$(id)\" />\n"); 145 id++; 146 } 147 } 148 149 if (only_selected_paths) { 150 foreach (Path p in glyph.active_paths) { 151 if (p.stroke == 0) { 152 glyph_svg += Svg.to_svg_path (p, glyph); 153 } 154 } 155 } else { 156 foreach (Path p in glyph.get_visible_paths ()) { 157 if (p.stroke == 0) { 158 glyph_svg += Svg.to_svg_path (p, glyph); 159 } 160 } 161 } 162 163 if (glyph_svg != "") { 164 s.append (@"<path "); 165 s.append (@"style=\"fill:#000000;stroke-width:0px\" "); 166 s.append (@"d=\"$(glyph_svg)\" id=\"path_$(name)_$(id)\" />\n"); 167 id++; 168 } 169 170 return s.str; 171 } 172 173 public static void export_current_glyph () { 174 FileChooser fc = new FileChooser (); 175 fc.file_selected.connect ((selected_file) => { 176 Glyph glyph = MainWindow.get_current_glyph (); 177 FontDisplay fd = MainWindow.get_current_display (); 178 string glyph_svg; 179 string svg_file; 180 File file; 181 DataOutputStream os; 182 string name; 183 string fn; 184 int i; 185 186 if (fd is GlyphTab || fd is Glyph) { 187 glyph = MainWindow.get_current_glyph (); 188 } else if (fd is OverView) { 189 OverView overview = MainWindow.get_overview (); 190 Glyph? g = overview.get_selected_glyph (); 191 192 if (g == null) { 193 warning("No glyph selected in overview."); 194 return; 195 } 196 197 glyph = (!) g; 198 } else { 199 return; 200 } 201 202 name = glyph.get_name (); 203 204 if (selected_file == null) { 205 warning ("No selected file."); 206 return; 207 } 208 209 svg_file = (!) selected_file; 210 211 try { 212 #if MAC 213 file = File.new_for_path (svg_file); 214 #else 215 i = 1; 216 fn = svg_file.replace (".svg", ""); 217 file = File.new_for_path (fn + ".svg"); 218 while (file.query_exists ()) { 219 file = File.new_for_path (fn + @"$i.svg"); 220 i++; 221 } 222 #endif 223 glyph_svg = export_to_string (glyph, false); 224 os = new DataOutputStream (file.create(FileCreateFlags.REPLACE_DESTINATION)); 225 os.put_string (glyph_svg); 226 } catch (Error e) { 227 stderr.printf (@"Export \"$svg_file\" \n"); 228 critical (@"$(e.message)"); 229 } 230 }); 231 232 fc.add_extension ("svg"); 233 MainWindow.file_chooser (t_("Save"), fc, FileChooser.SAVE); 234 } 235 236 public static void generate_html_document (string html_file, Font font) { 237 File file = File.new_for_path (html_file); 238 DataOutputStream os; 239 string name; 240 241 #if MAC 242 name = ExportSettings.get_file_name_mac (font); 243 #else 244 name = ExportSettings.get_file_name (font); 245 #endif 246 try { 247 os = new DataOutputStream (file.create(FileCreateFlags.REPLACE_DESTINATION)); 248 249 os.put_string ( 250 """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 251 <html> 252 <head> 253 254 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 255 <title>Lorem ipsum</title> 256 257 <style type="text/css"> 258 259 body { 260 text-rendering: optimizeLegibility; 261 font-feature-settings: "kern"; 262 -moz-font-feature-settings: "kern=1"; 263 -ms-font-feature-settings: "kern"; 264 -webkit-font-feature-settings: "kern"; 265 -o-font-feature-settings: "kern"; 266 } 267 268 @font-face { 269 font-family: '"""); os.put_string (@"$(name)"); os.put_string ("""SVG'; 270 src: url('"""); os.put_string (@"$(name).svg#$(name)"); os.put_string ("""') format('svg'); 271 } 272 """); 273 274 os.put_string (""" 275 @font-face { 276 font-family: '"""); os.put_string (@"$(name)"); os.put_string ("""TTF'; 277 src: url('"""); os.put_string (@"$(name).ttf"); os.put_string ("""') format('truetype'); 278 } 279 """); 280 281 os.put_string (""" 282 @font-face { 283 font-family: '"""); os.put_string (@"$(name)"); os.put_string ("""EOT'; 284 src: url('"""); os.put_string (@"$(name).eot"); os.put_string ("""'); 285 } 286 287 """); 288 289 os.put_string (""" 290 h1 { 291 font-weight:normal; 292 margin: 0 0 5px 0; 293 color: #afafaf; 294 } 295 296 h2 { 297 font-weight:normal; 298 margin: 0 0 5px 0; 299 color: #afafaf; 300 } 301 302 h3 { 303 font-weight:normal; 304 margin: 0 0 5px 0; 305 color: #afafaf; 306 } 307 308 h4 { 309 font-weight:normal; 310 margin: 0 0 5px 0; 311 color: #afafaf; 312 } 313 314 p { 315 margin: 0 0 5px 0; 316 color: #000000; 317 } 318 319 body { 320 margin: 30px 0 0 30px; 321 } 322 323 div { 324 font-family: """); 325 326 os.put_string ("'"); 327 os.put_string (@"$(name)"); 328 os.put_string ("EOT'"); 329 330 os.put_string (", '"); 331 os.put_string (@"$(name)"); 332 os.put_string ("TTF'"); 333 334 os.put_string (", '"); 335 os.put_string (@"$(name)"); 336 os.put_string ("SVG'"); 337 338 os.put_string (";"); 339 os.put_string (""" 340 margin: 0 0 30px 0; 341 } 342 343 h1.bigger { 344 font-size: 58pt; 345 } 346 347 h2.big { 348 font-size: 40pt; 349 } 350 351 h3.small { 352 font-size: 32pt; 353 } 354 355 h4.smaller { 356 font-size: 18pt; 357 } 358 359 h1.percent { 360 font-size: 100%; 361 } 362 363 p.bigger { 364 font-size: 32pt; 365 } 366 367 p.big { 368 font-size: 24pt; 369 } 370 371 p.small { 372 font-size: 18pt; 373 } 374 375 p.smaller { 376 font-size: 12pt; 377 } 378 379 span.swashes { 380 -moz-font-feature-settings: "swsh"; 381 -ms-font-feature-settings: "swsh"; 382 -webkit-font-feature-settings: "swsh"; 383 font-feature-settings: "swsh"; 384 } 385 386 span.alternates { 387 -moz-font-feature-settings: "salt" 1; 388 -ms-font-feature-settings: "salt" 1; 389 -webkit-font-feature-settings: "salt" 1; 390 font-feature-settings: "salt" 1; 391 } 392 393 span.smallcaps { 394 font-variant-caps: small-caps; 395 -moz-font-feature-settings: "smcp"; 396 -ms-font-feature-settings: "smcp"; 397 -webkit-font-feature-settings: "smcp"; 398 font-feature-settings: "smcp"; 399 } 400 401 span.capstosmallcaps { 402 font-variant-caps: all-small-caps; 403 -moz-font-feature-settings: "c2sc", "smcp"; 404 -ms-font-feature-settings: "c2sc", "smcp"; 405 -webkit-font-feature-settings: "c2sc", "smcp"; 406 font-feature-settings: "c2sc", "smcp"; 407 } 408 </style> 409 """); 410 411 os.put_string ( 412 """ 413 </head> 414 <body> 415 416 <div> 417 <h1 class="bigger">Lorem ipsum</h1> 418 <p class="bigger">Dolor sit amet!</p> 419 </div> 420 421 <div> 422 <h2 class="big">Hamburgerfonstiv</h2> 423 <p class="big">""" + name + """</p> 424 </div> 425 426 <div> 427 <h3 class="big"></h3> 428 <p class="big">OTF features, like swashes alternates &amp; </span> small caps, can be added 429 to the font.</span> 430 </p> 431 </div> 432 433 <div> 434 <h4 class="smaller">Headline 16pt</h4> 435 <p class="smaller">Ett litet stycke svalhjärta.</p> 436 </div> 437 438 <div> 439 <h4 class="smaller">""" + t_("Alphabet") + """</h4> 440 <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> 441 <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> 442 <p class="smaller">0 1 2 3 4 5 6 7 8 9</p> 443 </div> 444 445 </body> 446 </html> 447 """); 448 449 } catch (GLib.Error e) { 450 warning (e.message); 451 } 452 } 453 454 public static string get_export_folder () { 455 Font font = BirdFont.get_current_font (); 456 string? d = font.get_export_directory (); 457 458 if (d == null) { 459 warning ("No export path is not set"); 460 return ""; 461 } 462 463 return (!) d; 464 } 465 466 public static File get_export_dir () { 467 return File.new_for_path (get_export_folder ()); 468 } 469 470 public static bool export_ttf_font () { 471 File f = get_export_dir (); 472 Font font = BirdFont.get_current_font (); 473 474 try { 475 if (!f.query_exists ()) { 476 f.make_directory (); 477 } 478 } catch (GLib.Error e) { 479 warning(e.message); 480 } 481 482 printd (@"export_ttf_font:\n"); 483 printd (@"get_export_folder (): $(get_export_folder ())\n"); 484 printd (@"font.get_path (): $(font.get_path ())\n"); 485 printd (@"font.get_folder_path (): $(font.get_folder_path ())\n"); 486 printd (@"font.get_folder (): $((!) f.get_path ())\n"); 487 488 return export_ttf_font_path (f); 489 } 490 491 public static bool export_ttf_font_path (File folder, bool use_export_settings = true) { 492 Font current_font = BirdFont.get_current_font (); 493 File ttf_file; 494 File ttf_file_mac; 495 File eot_file; 496 bool done = true; 497 string ttf_name; 498 string ttf_name_mac; 499 500 try { 501 ttf_name = ExportSettings.get_file_name (current_font) + ".ttf"; 502 ttf_name_mac = ExportSettings.get_file_name_mac (current_font) + ".ttf"; 503 504 if (ttf_name == ttf_name_mac) { 505 warning ("Same file name for the two ttf files."); 506 ttf_name_mac = ExportSettings.get_file_name_mac (current_font) + " Mac.ttf"; 507 } 508 509 ttf_file = get_child (folder, ttf_name); 510 ttf_file_mac = get_child (folder, ttf_name_mac); 511 eot_file = get_child (folder, ExportSettings.get_file_name (current_font) + ".eot"); 512 513 printd (@"Writing TTF fonts to $((!) ttf_file.get_path ())\n"); 514 515 if (ttf_file.query_exists ()) { 516 ttf_file.delete (); 517 } 518 519 if (ttf_file_mac.query_exists ()) { 520 ttf_file_mac.delete (); 521 } 522 523 if (eot_file.query_exists ()) { 524 eot_file.delete (); 525 } 526 527 write_ttf ((!) ttf_file.get_path (), (!) ttf_file_mac.get_path ()); 528 529 if (!use_export_settings || ExportSettings.export_eot_setting (current_font)) { 530 write_eot ((!) ttf_file.get_path (), (!) eot_file.get_path ()); 531 } 532 533 if (use_export_settings && !ExportSettings.export_ttf_setting (current_font)) { 534 if (ttf_file.query_exists ()) { 535 ttf_file.delete (); 536 } 537 } 538 } catch (Error e) { 539 critical (@"$(e.message)"); 540 done = false; 541 } 542 543 return done; 544 } 545 546 public static bool export_svg_font () { 547 return export_svg_font_path (get_export_dir ()); 548 } 549 550 public static bool export_svg_font_path (File folder) { 551 Font font = BirdFont.get_current_font (); 552 string file_name = @"$(ExportSettings.get_file_name (font)).svg"; 553 File file; 554 SvgFontFormatWriter fo; 555 556 try { 557 file = get_child (folder, file_name); 558 559 if (file.query_exists ()) { 560 file.delete (); 561 } 562 563 fo = new SvgFontFormatWriter (); 564 fo.open (file); 565 fo.write_font_file (font); 566 fo.close (); 567 } catch (Error e) { 568 critical (@"$(e.message)"); 569 return false; 570 } 571 572 return true; 573 } 574 575 static void write_ttf (string ttf, string ttf_mac) { 576 Font f = BirdFont.get_current_font (); 577 OpenFontFormatWriter fo = new OpenFontFormatWriter (f.units_per_em); 578 579 File file = (!) File.new_for_path (ttf); 580 File file_mac = (!) File.new_for_path (ttf_mac); 581 582 error_message = null; 583 584 try { 585 fo.open (file, file_mac); 586 fo.write_ttf_font (f); 587 fo.close (); 588 } catch (Error e) { 589 warning (@"Can't create TTF font to $ttf"); 590 critical (@"$(e.message)"); 591 error_message = e.message; 592 f.export_directory = null; 593 } 594 } 595 596 static void write_eot (string ttf, string eot) { 597 EotWriter fo = new EotWriter (ttf, eot); 598 599 try { 600 fo.write (); 601 } catch (Error e) { 602 warning (@"EOF conversion falied, $ttf -> $eot"); 603 critical (@"$(e.message)"); 604 } 605 } 606 607 static bool can_write (File folder) { 608 File test = get_child (folder, "text.tmp"); 609 bool writable = false; 610 611 try { 612 writable = FileUtils.set_contents ((!) test.get_path (), "test"); 613 614 if (writable) { 615 FileUtils.remove ((!) test.get_path ()); 616 } 617 } catch (GLib.Error e) { 618 writable = false; 619 } 620 621 return writable; 622 } 623 } 624 625 } 626