001/* =================================================== 002 * JFreeSVG : an SVG library for the Java(tm) platform 003 * =================================================== 004 * 005 * (C)opyright 2013, 2014, by Object Refinery Limited. All rights reserved. 006 * 007 * Project Info: http://www.jfree.org/jfreesvg/index.html 008 * 009 * This program is free software: you can redistribute it and/or modify 010 * it under the terms of the GNU General Public License as published by 011 * the Free Software Foundation, either version 3 of the License, or 012 * (at your option) any later version. 013 * 014 * This program is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public License 020 * along with this program. If not, see <http://www.gnu.org/licenses/>. 021 * 022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 023 * Other names may be trademarks of their respective owners.] 024 * 025 * If you do not wish to be bound by the terms of the GPL, an alternative 026 * commercial license can be purchased. For details, please see visit the 027 * JFreeSVG home page: 028 * 029 * http://www.jfree.org/jfreesvg 030 * 031 */ 032 033package org.jfree.graphics2d.canvas; 034 035import java.io.BufferedWriter; 036import java.io.File; 037import java.io.FileWriter; 038import java.io.IOException; 039import java.util.logging.Level; 040import java.util.logging.Logger; 041 042/** 043 * Utility methods related to the {@link CanvasGraphics2D} implementation. 044 */ 045public class CanvasUtils { 046 047 private CanvasUtils() { 048 // no need to instantiate 049 } 050 051 /** 052 * Writes HTML output containing a script (usually generated by 053 * {@link CanvasGraphics2D}) to the specified file. This method is used 054 * in the demo applications. 055 * 056 * @param f the file. 057 * @param title the title for the HTML. 058 * @param canvasID the canvas ID. 059 * @param width the canvas width. 060 * @param height the canvas height. 061 * @param canvasScript the canvas script. 062 * 063 * @throws IOException if there is an IO error. 064 */ 065 public static void writeToHTML(File f, String title, String canvasID, 066 int width, int height, String canvasScript) throws IOException { 067 BufferedWriter writer = null; 068 try { 069 writer = new BufferedWriter(new FileWriter(f)); 070 writer.write("<!DOCTYPE html>\n"); 071 writer.write("<html>\n"); 072 writer.write("<head>\n"); 073 writer.write("<title>" +title + "</title>\n"); 074 writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"); 075 writer.write("<script>\n"); 076 writer.write("window.onload = function() {\n"); 077 writer.write("var canvas = document.getElementById(\"" + canvasID 078 + "\");\n"); 079 writer.write("var ctx = canvas.getContext(\"2d\");\n"); 080 writer.write("if (!ctx.setLineDash) {\n"); 081 writer.write("ctx.setLineDash = function() {};\n"); 082 writer.write("}\n"); 083 writer.write(canvasScript + "\n"); 084 writer.write("}\n"); 085 writer.write("</script>\n"); 086 writer.write("</head>\n"); 087 writer.write("<body>\n"); 088 089 writer.write("<canvas id=\"" + canvasID + "\" width=\"" + width 090 + "\" height=\"" + height + "\"></canvas>"); 091 writer.write("</body>\n"); 092 writer.write("</html>\n"); 093 writer.flush(); 094 } finally { 095 try { 096 if (writer != null) { 097 writer.close(); 098 } 099 } catch (IOException ex) { 100 Logger.getLogger(CanvasUtils.class.getName()).log(Level.SEVERE, 101 null, ex); 102 } 103 } 104 } 105 106}