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.
Drop Firefox warning
1 /* 2 Copyright (C) 2012 2014 2015 2019 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_template () { 237 File config_dir = BirdFont.get_settings_directory (); 238 File file = get_child (config_dir, "preview.html"); 239 string name = "_NAME_"; 240 DataOutputStream os; 241 242 if (file.query_exists ()) { 243 printd("Preview template exists.\n"); 244 return; 245 } 246 247 try { 248 os = new DataOutputStream (file.create(FileCreateFlags.REPLACE_DESTINATION)); 249 250 os.put_string ( 251 """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 252 <html> 253 <head> 254 255 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 256 <title>Lorem ipsum</title> 257 258 <style type="text/css"> 259 260 body { 261 text-rendering: optimizeLegibility; 262 font-feature-settings: "kern"; 263 margin: 0 0 0 0; 264 } 265 266 @font-face { 267 font-family: '"""); os.put_string (@"$(name)"); os.put_string ("""SVG'; 268 src: url('"""); os.put_string (@"$(name).svg#$(name)"); os.put_string ("""') format('svg'); 269 } 270 """); 271 272 os.put_string (""" 273 @font-face { 274 font-family: '"""); os.put_string (@"$(name)"); os.put_string ("""OTF'; 275 src: url('"""); os.put_string (@"$(name).otf"); os.put_string ("""') format('opentype'); 276 } 277 """); 278 279 os.put_string (""" 280 @font-face { 281 font-family: '"""); os.put_string (@"$(name)"); os.put_string ("""TTF'; 282 src: url('"""); os.put_string (@"$(name).ttf"); os.put_string ("""') format('truetype'); 283 } 284 """); 285 286 os.put_string (""" 287 @font-face { 288 font-family: '"""); os.put_string (@"$(name)"); os.put_string ("""EOT'; 289 src: url('"""); os.put_string (@"$(name).eot"); os.put_string ("""'); 290 } 291 292 """); 293 294 os.put_string (""" 295 296 section { 297 padding-left: 25px; 298 } 299 300 h1 { 301 font-weight:normal; 302 margin: 10px 0 5px 0; 303 color: #afafaf; 304 } 305 306 h2 { 307 font-weight:normal; 308 margin: 0 0 5px 0; 309 color: #afafaf; 310 } 311 312 h3 { 313 font-weight:normal; 314 margin: 0 0 5px 0; 315 color: #afafaf; 316 } 317 318 h4 { 319 font-weight:normal; 320 margin: 0 0 5px 0; 321 color: #afafaf; 322 } 323 324 p { 325 margin: 0 0 5px 0; 326 color: #000000; 327 } 328 329 div { 330 font-family: """); 331 332 os.put_string ("'"); 333 os.put_string (@"$(name)"); 334 os.put_string ("EOT'"); 335 336 os.put_string (", '"); 337 os.put_string (@"$(name)"); 338 os.put_string ("OTF'"); 339 340 os.put_string (", '"); 341 os.put_string (@"$(name)"); 342 os.put_string ("TTF'"); 343 344 os.put_string (", '"); 345 os.put_string (@"$(name)"); 346 os.put_string ("SVG'"); 347 348 os.put_string (";"); 349 os.put_string (""" 350 margin: 0 0 30px 0; 351 } 352 353 h1.bigger { 354 font-size: 58pt; 355 } 356 357 h2.big { 358 font-size: 40pt; 359 } 360 361 h3.small { 362 font-size: 32pt; 363 } 364 365 h4.smaller { 366 font-size: 18pt; 367 } 368 369 h1.percent { 370 font-size: 100%; 371 } 372 373 p.bigger { 374 font-size: 32pt; 375 } 376 377 p.big { 378 font-size: 24pt; 379 } 380 381 p.small { 382 font-size: 18pt; 383 } 384 385 p.smaller { 386 font-size: 12pt; 387 } 388 389 span.swashes { 390 font-feature-settings: "swsh"; 391 } 392 393 span.alternates { 394 font-feature-settings: "salt" 1; 395 } 396 397 span.smallcaps { 398 font-variant-caps: small-caps; 399 font-feature-settings: "smcp"; 400 } 401 402 span.capstosmallcaps { 403 font-variant-caps: all-small-caps; 404 font-feature-settings: "c2sc", "smcp"; 405 } 406 407 div.firefox_message { 408 background-color: #00A000; 409 font-size: 10pt; 410 padding: 10px 10px 10px 33px; 411 margin: 0 0 0 0; 412 width: 100%; 413 font-family: sans-serif; 414 } 415 </style> 416 417 <script> 418 window.onload = function() { 419 if (navigator.userAgent.indexOf("Firefox") > -1 && window.location.protocol == "file:") { 420 var parent = document.createElement("div"); 421 var firefoxMessageBox = document.createElement("div"); 422 firefoxMessageBox.classList.add("firefox_message"); 423 var message = document.createTextNode("Firefox will not let you load fonts on your " 424 + "local machine. Ways around it include uploading your files to a server " 425 + "or usning Chrome."); 426 firefoxMessageBox.appendChild(message); 427 parent.appendChild(firefoxMessageBox); 428 document.body.innerHTML = parent.innerHTML + document.body.innerHTML; 429 } 430 } 431 </script> 432 """); 433 434 os.put_string ( 435 """ 436 </head> 437 <body> 438 <section> 439 <div> 440 <h1 class="bigger">Lorem ipsum</h1> 441 <p class="bigger">Dolor sit amet!</p> 442 </div> 443 444 <div> 445 <h2 class="big">Hamburgerfonstiv</h2> 446 <p class="big">""" + name + """</p> 447 </div> 448 449 <div> 450 <h4 class="smaller">Headline 16pt</h4> 451 <p class="smaller">The quick brown fox jumps over the lazy dog</p> 452 </div> 453 454 <div> 455 <h4 class="smaller">""" + t_("Alphabet") + """</h4> 456 <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> 457 <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> 458 <p class="smaller">0 1 2 3 4 5 6 7 8 9</p> 459 </div> 460 </section> 461 </body> 462 </html> 463 """); 464 465 } catch (GLib.Error e) { 466 warning (e.message); 467 } 468 } 469 470 public static void generate_html_document (string html_file, Font font) { 471 File config_dir = BirdFont.get_settings_directory (); 472 File template = get_child (config_dir, "preview.html"); 473 474 if (!template.query_exists ()) { 475 generate_html_template (); 476 get_child (config_dir, "preview.html"); 477 } else { 478 print ("HTML template exits."); 479 } 480 481 if (!template.query_exists ()) { 482 warning ("Preview template does not exists."); 483 return; 484 } 485 486 string html_data; 487 FileUtils.get_contents ((!) template.get_path (), out html_data); 488 489 string name; 490 #if MAC 491 name = ExportSettings.get_file_name_mac (font); 492 #else 493 name = ExportSettings.get_file_name (font); 494 #endif 495 496 html_data = html_data.replace ("_NAME_", name); 497 498 try { 499 print(html_data); 500 FileUtils.set_contents (html_file, html_data); 501 } catch (FileError e) { 502 warning (e.message); 503 } 504 } 505 506 507 public static string get_export_folder () { 508 Font font = BirdFont.get_current_font (); 509 string? d = font.get_export_directory (); 510 511 if (d == null) { 512 warning ("No export path is not set"); 513 return ""; 514 } 515 516 return (!) d; 517 } 518 519 public static File get_export_dir () { 520 return File.new_for_path (get_export_folder ()); 521 } 522 523 public static bool export_ttf_font () { 524 File f = get_export_dir (); 525 Font font = BirdFont.get_current_font (); 526 527 try { 528 if (!f.query_exists ()) { 529 f.make_directory (); 530 } 531 } catch (GLib.Error e) { 532 warning(e.message); 533 } 534 535 printd (@"export_ttf_font:\n"); 536 printd (@"get_export_folder (): $(get_export_folder ())\n"); 537 printd (@"font.get_path (): $(font.get_path ())\n"); 538 printd (@"font.get_folder_path (): $(font.get_folder_path ())\n"); 539 printd (@"font.get_folder (): $((!) f.get_path ())\n"); 540 541 return export_ttf_font_path (f); 542 } 543 544 public static bool export_ttf_font_path (File folder, bool use_export_settings = true) { 545 Font current_font = BirdFont.get_current_font (); 546 File ttf_file; 547 File ttf_file_mac; 548 File eot_file; 549 bool done = true; 550 string ttf_name; 551 string ttf_name_mac; 552 553 try { 554 ttf_name = ExportSettings.get_file_name (current_font) + ".ttf"; 555 ttf_name_mac = ExportSettings.get_file_name_mac (current_font) + ".ttf"; 556 557 if (ttf_name == ttf_name_mac) { 558 warning ("Same file name for the two ttf files."); 559 ttf_name_mac = ExportSettings.get_file_name_mac (current_font) + " Mac.ttf"; 560 } 561 562 ttf_file = get_child (folder, ttf_name); 563 ttf_file_mac = get_child (folder, ttf_name_mac); 564 eot_file = get_child (folder, ExportSettings.get_file_name (current_font) + ".eot"); 565 566 printd (@"Writing TTF fonts to $((!) ttf_file.get_path ())\n"); 567 568 if (ttf_file.query_exists ()) { 569 ttf_file.delete (); 570 } 571 572 if (ttf_file_mac.query_exists ()) { 573 ttf_file_mac.delete (); 574 } 575 576 if (eot_file.query_exists ()) { 577 eot_file.delete (); 578 } 579 580 write_ttf ((!) ttf_file.get_path (), (!) ttf_file_mac.get_path ()); 581 582 if (!use_export_settings || ExportSettings.export_eot_setting (current_font)) { 583 write_eot ((!) ttf_file.get_path (), (!) eot_file.get_path ()); 584 } 585 586 if (use_export_settings && !ExportSettings.export_ttf_setting (current_font)) { 587 if (ttf_file.query_exists ()) { 588 ttf_file.delete (); 589 } 590 } 591 } catch (Error e) { 592 critical (@"$(e.message)"); 593 done = false; 594 } 595 596 return done; 597 } 598 599 public static bool export_svg_font () { 600 return export_svg_font_path (get_export_dir ()); 601 } 602 603 public static bool export_svg_font_path (File folder) { 604 Font font = BirdFont.get_current_font (); 605 string file_name = @"$(ExportSettings.get_file_name (font)).svg"; 606 File file; 607 SvgFontFormatWriter fo; 608 609 try { 610 file = get_child (folder, file_name); 611 612 if (file.query_exists ()) { 613 file.delete (); 614 } 615 616 fo = new SvgFontFormatWriter (); 617 fo.open (file); 618 fo.write_font_file (font); 619 fo.close (); 620 } catch (Error e) { 621 critical (@"$(e.message)"); 622 return false; 623 } 624 625 return true; 626 } 627 628 static void write_ttf (string ttf, string ttf_mac) { 629 Font f = BirdFont.get_current_font (); 630 OpenFontFormatWriter fo = new OpenFontFormatWriter (f.units_per_em); 631 632 File file = (!) File.new_for_path (ttf); 633 File file_mac = (!) File.new_for_path (ttf_mac); 634 635 error_message = null; 636 637 try { 638 fo.open (file, file_mac); 639 fo.write_ttf_font (f); 640 fo.close (); 641 } catch (Error e) { 642 warning (@"Can't create TTF font to $ttf"); 643 critical (@"$(e.message)"); 644 error_message = e.message; 645 f.export_directory = null; 646 } 647 } 648 649 static void write_eot (string ttf, string eot) { 650 EotWriter fo = new EotWriter (ttf, eot); 651 652 try { 653 fo.write (); 654 } catch (Error e) { 655 warning (@"EOF conversion falied, $ttf -> $eot"); 656 critical (@"$(e.message)"); 657 } 658 } 659 660 static bool can_write (File folder) { 661 File test = get_child (folder, "text.tmp"); 662 bool writable = false; 663 664 try { 665 writable = FileUtils.set_contents ((!) test.get_path (), "test"); 666 667 if (writable) { 668 FileUtils.remove ((!) test.get_path ()); 669 } 670 } catch (GLib.Error e) { 671 writable = false; 672 } 673 674 return writable; 675 } 676 } 677 678 } 679