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
Drop old icons handler heads/master
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 </style> 407 """); 408 409 os.put_string ( 410 """ 411 </head> 412 <body> 413 <section> 414 <div> 415 <h1 class="bigger">Lorem ipsum</h1> 416 <p class="bigger">Dolor sit amet!</p> 417 </div> 418 419 <div> 420 <h2 class="big">Hamburgerfonstiv</h2> 421 <p class="big">""" + name + """</p> 422 </div> 423 424 <div> 425 <h4 class="smaller">Headline 16pt</h4> 426 <p class="smaller">The quick brown fox jumps over the lazy dog</p> 427 </div> 428 429 <div> 430 <h4 class="smaller">""" + t_("Alphabet") + """</h4> 431 <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> 432 <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> 433 <p class="smaller">0 1 2 3 4 5 6 7 8 9</p> 434 </div> 435 </section> 436 </body> 437 </html> 438 """); 439 440 } catch (GLib.Error e) { 441 warning (e.message); 442 } 443 } 444 445 public static void generate_html_document (string html_file, Font font) { 446 File config_dir = BirdFont.get_settings_directory (); 447 File template = get_child (config_dir, "preview.html"); 448 449 if (!template.query_exists ()) { 450 generate_html_template (); 451 get_child (config_dir, "preview.html"); 452 } else { 453 print ("HTML template exits."); 454 } 455 456 if (!template.query_exists ()) { 457 warning ("Preview template does not exists."); 458 return; 459 } 460 461 string html_data; 462 FileUtils.get_contents ((!) template.get_path (), out html_data); 463 464 string name; 465 #if MAC 466 name = ExportSettings.get_file_name_mac (font); 467 #else 468 name = ExportSettings.get_file_name (font); 469 #endif 470 471 html_data = html_data.replace ("_NAME_", name); 472 473 try { 474 print(html_data); 475 FileUtils.set_contents (html_file, html_data); 476 } catch (FileError e) { 477 warning (e.message); 478 } 479 } 480 481 482 public static string get_export_folder () { 483 Font font = BirdFont.get_current_font (); 484 string? d = font.get_export_directory (); 485 486 if (d == null) { 487 warning ("No export path is not set"); 488 return ""; 489 } 490 491 return (!) d; 492 } 493 494 public static File get_export_dir () { 495 return File.new_for_path (get_export_folder ()); 496 } 497 498 public static bool export_ttf_font () { 499 File f = get_export_dir (); 500 Font font = BirdFont.get_current_font (); 501 502 try { 503 if (!f.query_exists ()) { 504 f.make_directory (); 505 } 506 } catch (GLib.Error e) { 507 warning(e.message); 508 } 509 510 printd (@"export_ttf_font:\n"); 511 printd (@"get_export_folder (): $(get_export_folder ())\n"); 512 printd (@"font.get_path (): $(font.get_path ())\n"); 513 printd (@"font.get_folder_path (): $(font.get_folder_path ())\n"); 514 printd (@"font.get_folder (): $((!) f.get_path ())\n"); 515 516 return export_ttf_font_path (f); 517 } 518 519 public static bool export_ttf_font_path (File folder, bool use_export_settings = true) { 520 Font current_font = BirdFont.get_current_font (); 521 File ttf_file; 522 File ttf_file_mac; 523 File eot_file; 524 bool done = true; 525 string ttf_name; 526 string ttf_name_mac; 527 528 try { 529 ttf_name = ExportSettings.get_file_name (current_font) + ".ttf"; 530 ttf_name_mac = ExportSettings.get_file_name_mac (current_font) + ".ttf"; 531 532 if (ttf_name == ttf_name_mac) { 533 warning ("Same file name for the two ttf files."); 534 ttf_name_mac = ExportSettings.get_file_name_mac (current_font) + " Mac.ttf"; 535 } 536 537 ttf_file = get_child (folder, ttf_name); 538 ttf_file_mac = get_child (folder, ttf_name_mac); 539 eot_file = get_child (folder, ExportSettings.get_file_name (current_font) + ".eot"); 540 541 printd (@"Writing TTF fonts to $((!) ttf_file.get_path ())\n"); 542 543 if (ttf_file.query_exists ()) { 544 ttf_file.delete (); 545 } 546 547 if (ttf_file_mac.query_exists ()) { 548 ttf_file_mac.delete (); 549 } 550 551 if (eot_file.query_exists ()) { 552 eot_file.delete (); 553 } 554 555 write_ttf ((!) ttf_file.get_path (), (!) ttf_file_mac.get_path ()); 556 557 if (!use_export_settings || ExportSettings.export_eot_setting (current_font)) { 558 write_eot ((!) ttf_file.get_path (), (!) eot_file.get_path ()); 559 } 560 561 if (use_export_settings && !ExportSettings.export_ttf_setting (current_font)) { 562 if (ttf_file.query_exists ()) { 563 ttf_file.delete (); 564 } 565 } 566 } catch (Error e) { 567 critical (@"$(e.message)"); 568 done = false; 569 } 570 571 return done; 572 } 573 574 public static bool export_svg_font () { 575 return export_svg_font_path (get_export_dir ()); 576 } 577 578 public static bool export_svg_font_path (File folder) { 579 Font font = BirdFont.get_current_font (); 580 string file_name = @"$(ExportSettings.get_file_name (font)).svg"; 581 File file; 582 SvgFontFormatWriter fo; 583 584 try { 585 file = get_child (folder, file_name); 586 587 if (file.query_exists ()) { 588 file.delete (); 589 } 590 591 fo = new SvgFontFormatWriter (); 592 fo.open (file); 593 fo.write_font_file (font); 594 fo.close (); 595 } catch (Error e) { 596 critical (@"$(e.message)"); 597 return false; 598 } 599 600 return true; 601 } 602 603 static void write_ttf (string ttf, string ttf_mac) { 604 Font f = BirdFont.get_current_font (); 605 OpenFontFormatWriter fo = new OpenFontFormatWriter (f.units_per_em); 606 607 File file = (!) File.new_for_path (ttf); 608 File file_mac = (!) File.new_for_path (ttf_mac); 609 610 error_message = null; 611 612 try { 613 fo.open (file, file_mac); 614 fo.write_ttf_font (f); 615 fo.close (); 616 } catch (Error e) { 617 warning (@"Can't create TTF font to $ttf"); 618 critical (@"$(e.message)"); 619 error_message = e.message; 620 f.export_directory = null; 621 } 622 } 623 624 static void write_eot (string ttf, string eot) { 625 EotWriter fo = new EotWriter (ttf, eot); 626 627 try { 628 fo.write (); 629 } catch (Error e) { 630 warning (@"EOF conversion falied, $ttf -> $eot"); 631 critical (@"$(e.message)"); 632 } 633 } 634 635 static bool can_write (File folder) { 636 File test = get_child (folder, "text.tmp"); 637 bool writable = false; 638 639 try { 640 writable = FileUtils.set_contents ((!) test.get_path (), "test"); 641 642 if (writable) { 643 FileUtils.remove ((!) test.get_path ()); 644 } 645 } catch (GLib.Error e) { 646 writable = false; 647 } 648 649 return writable; 650 } 651 } 652 653 } 654