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