/* Copyright (C) 2012 2014 2015 2019 Johan Mattsson This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ using B; namespace BirdFont { public class ExportTool : GLib.Object { public static string? error_message = null; public ExportTool (string n) { } public static void set_output_directory () { #if MAC Font font = BirdFont.get_current_font (); string? path = font.get_export_directory (); FileChooser fc = new FileChooser (); fc.file_selected.connect ((p) => { path = p; }); if (path == null) { File export_path_handle = File.new_for_path (path); if (!can_write (export_path_handle)) { MainWindow.file_chooser (t_("Export"), fc, FileChooser.LOAD | FileChooser.DIRECTORY); } } font.export_directory = path; #endif } public static string export_selected_paths_to_svg () { return export_current_glyph_to_string (true); } public static string export_selected_paths_to_inkscape_clipboard () { return export_current_glyph_to_inkscape_clipboard (true); } public static string export_current_glyph_to_string (bool only_selected_paths = false) { return export_to_string (MainWindow.get_current_glyph (), only_selected_paths); } public static string export_to_string (Glyph glyph, bool only_selected_paths) { string name; StringBuilder s; name = BirdFontFile.encode (glyph.get_name ()); s = new StringBuilder (); s.append (""" """); s.append (@"\n"); s.append (get_svg_path_elements (glyph, only_selected_paths)); s.append ("\n"); s.append (""); return s.str; } public static string export_current_glyph_to_inkscape_clipboard (bool only_selected_paths = false) { return export_to_inkscape_clipboard (MainWindow.get_current_glyph (), only_selected_paths); } public static string export_to_inkscape_clipboard (Glyph glyph, bool only_selected_paths = false) { StringBuilder s; s = new StringBuilder (); s.append (""""""); s.append ("\n"); s.append ("\n"); s.append (""" """); s.append (get_svg_path_elements (glyph, only_selected_paths)); s.append (""); return s.str; } private static string get_svg_path_elements (Glyph glyph, bool only_selected_paths) { string glyph_svg; StringBuilder s; string name; int id = 0; name = glyph.get_name (); Gee.ArrayList pl; s = new StringBuilder (); glyph_svg = ""; pl = only_selected_paths ? glyph.active_paths : glyph.get_visible_paths (); foreach (Path p in pl) { if (p.stroke > 0) { s.append (@"\n"); id++; } } if (only_selected_paths) { foreach (Path p in glyph.active_paths) { if (p.stroke == 0) { glyph_svg += Svg.to_svg_path (p, glyph); } } } else { foreach (Path p in glyph.get_visible_paths ()) { if (p.stroke == 0) { glyph_svg += Svg.to_svg_path (p, glyph); } } } if (glyph_svg != "") { s.append (@"\n"); id++; } return s.str; } public static void export_current_glyph () { FileChooser fc = new FileChooser (); fc.file_selected.connect ((selected_file) => { Glyph glyph = MainWindow.get_current_glyph (); FontDisplay fd = MainWindow.get_current_display (); string glyph_svg; string svg_file; File file; DataOutputStream os; string name; string fn; int i; if (fd is GlyphTab || fd is Glyph) { glyph = MainWindow.get_current_glyph (); } else if (fd is Overview) { Overview overview = MainWindow.get_overview (); Glyph? g = overview.get_selected_glyph (); if (g == null) { warning("No glyph selected in overview."); return; } glyph = (!) g; } else { return; } name = glyph.get_name (); if (selected_file == null) { warning ("No selected file."); return; } svg_file = (!) selected_file; try { #if MAC file = File.new_for_path (svg_file); #else i = 1; fn = svg_file.replace (".svg", ""); file = File.new_for_path (fn + ".svg"); while (file.query_exists ()) { file = File.new_for_path (fn + @"$i.svg"); i++; } #endif glyph_svg = export_to_string (glyph, false); os = new DataOutputStream (file.create(FileCreateFlags.REPLACE_DESTINATION)); os.put_string (glyph_svg); } catch (Error e) { stderr.printf (@"Export \"$svg_file\" \n"); critical (@"$(e.message)"); } }); fc.add_extension ("svg"); MainWindow.file_chooser (t_("Save"), fc, FileChooser.SAVE); } public static void generate_html_template () { File config_dir = BirdFont.get_settings_directory (); File file = get_child (config_dir, "preview.html"); string name = "_NAME_"; DataOutputStream os; if (file.query_exists ()) { printd("Preview template exists.\n"); return; } try { os = new DataOutputStream (file.create(FileCreateFlags.REPLACE_DESTINATION)); os.put_string ( """ Lorem ipsum """); os.put_string ( """

Lorem ipsum

Dolor sit amet!

Hamburgefonstiv

""" + name + """

Headline 16pt

The quick brown fox jumps over the lazy dog

""" + t_("Alphabet") + """

""" + 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") + """

""" + 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") + """

0 1 2 3 4 5 6 7 8 9

"""); } catch (GLib.Error e) { warning (e.message); } } public static void generate_html_document (string html_file, Font font) { File config_dir = BirdFont.get_settings_directory (); File template = get_child (config_dir, "preview.html"); if (!template.query_exists ()) { generate_html_template (); get_child (config_dir, "preview.html"); } else { print ("HTML template exits."); } if (!template.query_exists ()) { warning ("Preview template does not exists."); return; } string html_data; try { FileUtils.get_contents ((!) template.get_path (), out html_data); } catch (FileError e) { warning (e.message); } string name; #if MAC name = ExportSettings.get_file_name_mac (font); #else name = ExportSettings.get_file_name (font); #endif html_data = html_data.replace ("_NAME_", name); try { print(html_data); FileUtils.set_contents (html_file, html_data); } catch (FileError e) { warning (e.message); } } public static string get_export_folder () { Font font = BirdFont.get_current_font (); string? d = font.get_export_directory (); if (d == null) { warning ("No export path is not set"); return ""; } return (!) d; } public static File get_export_dir () { return File.new_for_path (get_export_folder ()); } public static bool export_ttf_font () { File f = get_export_dir (); Font font = BirdFont.get_current_font (); try { if (!f.query_exists ()) { f.make_directory (); } } catch (GLib.Error e) { warning(e.message); } printd (@"export_ttf_font:\n"); printd (@"get_export_folder (): $(get_export_folder ())\n"); printd (@"font.get_path (): $(font.get_path ())\n"); printd (@"font.get_folder_path (): $(font.get_folder_path ())\n"); printd (@"font.get_folder (): $((!) f.get_path ())\n"); return export_ttf_font_path (f); } public static bool export_ttf_font_path (File folder, bool use_export_settings = true) { Font current_font = BirdFont.get_current_font (); File ttf_file; File ttf_file_mac; File eot_file; bool done = true; string ttf_name; string ttf_name_mac; try { ttf_name = ExportSettings.get_file_name (current_font) + ".ttf"; ttf_name_mac = ExportSettings.get_file_name_mac (current_font) + ".ttf"; if (ttf_name == ttf_name_mac) { warning ("Same file name for the two ttf files."); ttf_name_mac = ExportSettings.get_file_name_mac (current_font) + " Mac.ttf"; } ttf_file = get_child (folder, ttf_name); ttf_file_mac = get_child (folder, ttf_name_mac); eot_file = get_child (folder, ExportSettings.get_file_name (current_font) + ".eot"); printd (@"Writing TTF fonts to $((!) ttf_file.get_path ())\n"); if (ttf_file.query_exists ()) { ttf_file.delete (); } if (ttf_file_mac.query_exists ()) { ttf_file_mac.delete (); } if (eot_file.query_exists ()) { eot_file.delete (); } write_ttf ((!) ttf_file.get_path (), (!) ttf_file_mac.get_path ()); if (!use_export_settings || ExportSettings.export_eot_setting (current_font)) { write_eot ((!) ttf_file.get_path (), (!) eot_file.get_path ()); } if (use_export_settings && !ExportSettings.export_ttf_setting (current_font)) { if (ttf_file.query_exists ()) { ttf_file.delete (); } } } catch (Error e) { critical (@"$(e.message)"); done = false; } return done; } public static bool export_svg_font () { return export_svg_font_path (get_export_dir ()); } public static bool export_svg_font_path (File folder) { Font font = BirdFont.get_current_font (); string file_name = @"$(ExportSettings.get_file_name (font)).svg"; File file; SvgFontFormatWriter fo; try { file = get_child (folder, file_name); if (file.query_exists ()) { file.delete (); } fo = new SvgFontFormatWriter (); fo.open (file); fo.write_font_file (font); fo.close (); } catch (Error e) { critical (@"$(e.message)"); return false; } return true; } static void write_ttf (string ttf, string ttf_mac) { Font f = BirdFont.get_current_font (); OpenFontFormatWriter fo = new OpenFontFormatWriter (f.units_per_em); File file = (!) File.new_for_path (ttf); File file_mac = (!) File.new_for_path (ttf_mac); error_message = null; try { fo.open (file, file_mac); fo.write_ttf_font (f); fo.close (); } catch (Error e) { warning (@"Can't create TTF font to $ttf"); critical (@"$(e.message)"); error_message = e.message; f.export_directory = null; } } static void write_eot (string ttf, string eot) { EotWriter fo = new EotWriter (ttf, eot); try { fo.write (); } catch (Error e) { warning (@"EOF conversion falied, $ttf -> $eot"); critical (@"$(e.message)"); } } /* static bool can_write (File folder) { File test = get_child (folder, "text.tmp"); bool writable = false; try { writable = FileUtils.set_contents ((!) test.get_path (), "test"); if (writable) { FileUtils.remove ((!) test.get_path ()); } } catch (GLib.Error e) { writable = false; } return writable; } */ } }