The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Preview.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/Preview.vala.
Merge ../birdfont-2.x
1 /* 2 Copyright (C) 2012 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 namespace BirdFont { 16 17 public class Preview : FontDisplay { 18 19 public Preview () { 20 } 21 22 public override string get_name () { 23 return "Preview"; 24 } 25 26 public override string get_label () { 27 return t_("Preview"); 28 } 29 30 public override void selected_canvas () { 31 MainWindow.set_scrollbar_size (0); 32 } 33 34 public static string get_html_path () { 35 return (!) get_file ().get_path (); 36 } 37 38 public static File get_file () { 39 Font font = BirdFont.get_current_font (); 40 string fn = get_html_file_name (); 41 File dir = ExportTool.get_export_dir (); 42 File file = get_child (dir, fn); 43 44 if (!file.query_exists ()) { 45 ExportTool.generate_html_document ((!)file.get_path (), font); 46 } 47 48 return file; 49 } 50 51 public static bool has_html_document () { 52 string path = get_html_file_name (); 53 File dir = ExportTool.get_export_dir (); 54 File file = get_child (dir, path); 55 return file.query_exists (); 56 } 57 58 public static void generate_html_document () { 59 Font font = BirdFont.get_current_font (); 60 string path = get_html_file_name (); 61 File dir = ExportTool.get_export_dir (); 62 File file = get_child (dir, path); 63 ExportTool.generate_html_document ((!)file.get_path (), font); 64 } 65 66 public static void delete_html_document () { 67 Font font = BirdFont.get_current_font (); 68 string path = get_html_file_name (); 69 File dir = font.get_folder (); 70 File file = get_child (dir, path); 71 try { 72 file.delete (); 73 } catch (Error e) { 74 warning (e.message); 75 } 76 } 77 78 static string get_html_file_name () { 79 Font font = BirdFont.get_current_font (); 80 return @"$(ExportSettings.get_file_name (font)).html"; 81 } 82 83 public static File get_html_file () { 84 return get_file (); 85 } 86 87 public static string get_uri () { 88 return TabContent.path_to_uri ((!) get_html_file ().get_path ()); 89 } 90 91 public static string get_windows_uri () { 92 Font font = BirdFont.get_current_font (); 93 string html = get_html_file_name (); 94 File dir = font.get_folder (); 95 File file = get_child (dir, html); 96 return "file:///" + (!) file.get_path (); 97 } 98 99 public static string get_html_with_absolute_paths () { 100 // hack: force webkit to ignore cache in preview 101 StringBuilder sb = new StringBuilder (); 102 DataInputStream dis; 103 string? line; 104 105 uint rid = Random.next_int (); 106 Font font = BirdFont.get_current_font (); 107 108 File preview_directory; 109 File f_ttf; 110 File f_eot; 111 File f_svg; 112 113 try { 114 dis = new DataInputStream (get_file ().read ()); 115 116 string? d = font.get_export_directory (); 117 118 if (d == null) { 119 warning ("Export dir is not set."); 120 ExportTool.set_output_directory (); 121 d = font.get_export_directory (); 122 } 123 124 preview_directory = File.new_for_path ((!) d); 125 126 warning (@"previwdir $((!) d)"); 127 128 if (ExportTool.get_export_dir () == null) { 129 ExportTool.set_output_directory (); 130 } 131 132 File dir = File.new_for_path ((!) d); 133 f_ttf = get_child (dir, @"$(ExportSettings.get_file_name (font)).ttf"); 134 f_eot = get_child (dir, @"$(ExportSettings.get_file_name (font)).eot"); 135 f_svg = get_child (dir, @"$(ExportSettings.get_file_name (font)).svg"); 136 137 if (!f_ttf.query_exists ()) { 138 warning ("TTF file does not exist."); 139 } 140 141 if (!f_svg.query_exists ()) { 142 warning ("SVG file does not exist."); 143 } 144 145 string name = ExportSettings.get_file_name_mac (font); 146 147 while ((line = dis.read_line (null)) != null) { 148 warning (@"PRE $line name $name rep $(TabContent.path_to_uri ((!) f_ttf.get_path ()))"); 149 line = ((!) line).replace (@"$name.ttf", @"$(TabContent.path_to_uri ((!) f_ttf.get_path ()))?$rid"); 150 line = ((!) line).replace (@"$name.eot", @"$(TabContent.path_to_uri ((!) f_eot.get_path ()))?$rid"); 151 line = ((!) line).replace (@"$name.svg", @"$(TabContent.path_to_uri ((!) f_svg.get_path ()))?$rid"); 152 warning (@"POST $line"); 153 sb.append ((!) line); 154 } 155 156 } catch (Error e) { 157 warning (e.message); 158 warning ("Failed to load html into canvas."); 159 } 160 return sb.str; 161 } 162 163 public override bool needs_modifier () { 164 return true; 165 } 166 167 } 168 } 169