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 preview_directory = BirdFont.get_preview_directory (); 117 118 f_ttf = get_child (ExportTool.get_export_dir (), @"$(ExportSettings.get_file_name (font)).ttf"); 119 f_eot = get_child (ExportTool.get_export_dir (), @"$(ExportSettings.get_file_name (font)).eot"); 120 f_svg = get_child (ExportTool.get_export_dir (), @"$(ExportSettings.get_file_name (font)).svg"); 121 122 if (!f_ttf.query_exists ()) { 123 warning ("TTF file does not exist."); 124 } 125 126 if (!f_svg.query_exists ()) { 127 warning ("SVG file does not exist."); 128 } 129 130 while ((line = dis.read_line (null)) != null) { 131 line = ((!) line).replace (@"$(ExportSettings.get_file_name (font)).ttf", @"$(TabContent.path_to_uri ((!) f_ttf.get_path ()))?$rid"); 132 line = ((!) line).replace (@"$(ExportSettings.get_file_name (font)).eot", @"$(TabContent.path_to_uri ((!) f_eot.get_path ()))?$rid"); 133 line = ((!) line).replace (@"$(ExportSettings.get_file_name (font)).svg", @"$(TabContent.path_to_uri ((!) f_svg.get_path ()))?$rid"); 134 sb.append ((!) line); 135 } 136 137 } catch (Error e) { 138 warning (e.message); 139 warning ("Failed to load html into canvas."); 140 } 141 return sb.str; 142 } 143 144 public override bool needs_modifier () { 145 return true; 146 } 147 148 } 149 } 150