.
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 = font.get_folder ();
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 Font font = BirdFont.get_current_font ();
53 string path = get_html_file_name ();
54 File dir = font.get_folder ();
55 File file = get_child (dir, path);
56 return file.query_exists ();
57 }
58
59 public static void generate_html_document () {
60 Font font = BirdFont.get_current_font ();
61 string path = get_html_file_name ();
62 File dir = font.get_folder ();
63 File file = get_child (dir, path);
64 ExportTool.generate_html_document ((!)file.get_path (), font);
65 }
66
67 public static void delete_html_document () {
68 Font font = BirdFont.get_current_font ();
69 string path = get_html_file_name ();
70 File dir = font.get_folder ();
71 File file = get_child (dir, path);
72 try {
73 file.delete ();
74 } catch (Error e) {
75 warning (e.message);
76 }
77 }
78
79 static string get_html_file_name () {
80 Font font = BirdFont.get_current_font ();
81 return @"$(ExportSettings.get_file_name (font)).html";
82 }
83
84 public static File get_html_file () {
85 return get_file ();
86 }
87
88 public static string get_uri () {
89 return TabContent.path_to_uri ((!) get_html_file ().get_path ());
90 }
91
92 public static string get_windows_uri () {
93 Font font = BirdFont.get_current_font ();
94 string html = get_html_file_name ();
95 File dir = font.get_folder ();
96 File file = get_child (dir, html);
97 return "file:///" + (!) file.get_path ();
98 }
99
100 public static string get_html_with_absolute_paths () {
101 // hack: force webkit to ignore cache in preview
102 StringBuilder sb = new StringBuilder ();
103 DataInputStream dis;
104 string? line;
105
106 uint rid = Random.next_int ();
107 Font font = BirdFont.get_current_font ();
108
109 File preview_directory;
110 File f_ttf;
111 File f_eot;
112 File f_svg;
113
114 try {
115 dis = new DataInputStream (get_file ().read ());
116
117 preview_directory = BirdFont.get_preview_directory ();
118
119 f_ttf = get_child (font.get_folder (), @"$(ExportSettings.get_file_name (font)).ttf");
120 f_eot = get_child (font.get_folder (), @"$(ExportSettings.get_file_name (font)).eot");
121 f_svg = get_child (font.get_folder (), @"$(ExportSettings.get_file_name (font)).svg");
122
123 if (!f_ttf.query_exists ()) {
124 warning ("TTF file does not exist.");
125 }
126
127 if (!f_svg.query_exists ()) {
128 warning ("SVG file does not exist.");
129 }
130
131 while ((line = dis.read_line (null)) != null) {
132 line = ((!) line).replace (@"$(ExportSettings.get_file_name (font)).ttf", @"$(TabContent.path_to_uri ((!) f_ttf.get_path ()))?$rid");
133 line = ((!) line).replace (@"$(ExportSettings.get_file_name (font)).eot", @"$(TabContent.path_to_uri ((!) f_eot.get_path ()))?$rid");
134 line = ((!) line).replace (@"$(ExportSettings.get_file_name (font)).svg", @"$(TabContent.path_to_uri ((!) f_svg.get_path ()))?$rid");
135 sb.append ((!) line);
136 }
137
138 } catch (Error e) {
139 warning (e.message);
140 warning ("Failed to load html into canvas.");
141 }
142 return sb.str;
143 }
144
145 public override bool needs_modifier () {
146 return true;
147 }
148
149 }
150 }
151