/* Copyright (C) 2012 2014 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 Bird; namespace BirdFont { public class ExportTool : GLib.Object { public ExportTool (string n) { } 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 = XmlParser.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.path_list; 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.path_list) { 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 ((f) => { 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; name = glyph.get_name (); if (f == null) { return; } svg_file = (!) f; if (!(fd is Glyph)) { return; } try { 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++; } glyph_svg = export_current_glyph_to_string (); 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)"); } }); MainWindow.file_chooser (t_("Save"), fc, FileChooser.SAVE); } public static void generate_html_document (string html_file, Font font) { File file = File.new_for_path (html_file); DataOutputStream os; string name = ExportSettings.get_file_name (font); try { os = new DataOutputStream (file.create(FileCreateFlags.REPLACE_DESTINATION)); os.put_string ( """ Lorem ipsum """); os.put_string ( """

Lorem ipsum

Dolor sit amet!

Hamburgerfonstiv

""" + name + """

Handgloves & Mittoms

Bibliography

Headline 16pt

Ett litet stycke svalhjÀrta.

""" + 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 bool export_ttf_font () { Font font = BirdFont.get_current_font (); File f = font.get_folder (); printd (@"export_ttf_font:\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 eot_file; bool done = true; try { ttf_file = get_child (folder, ExportSettings.get_file_name (current_font) + ".ttf"); 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 (eot_file.query_exists ()) { eot_file.delete (); } write_ttf ((!) ttf_file.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 () { Font font = BirdFont.get_current_font (); return export_svg_font_path (font.get_folder ()); } 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) { OpenFontFormatWriter fo = new OpenFontFormatWriter (); Font f = BirdFont.get_current_font (); File file = (!) File.new_for_path (ttf); try { fo.open (file); fo.write_ttf_font (f); fo.close (); } catch (Error e) { warning (@"Can't write TTF font to $ttf"); critical (@"$(e.message)"); } } 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)"); } } } }