001 /*
002 * IndexLoadObjectsAntTask.java
003 *
004 * Created on January 22, 2005, 10:30 AM
005 */
006
007 package com.kitfox.svg.app.ant;
008
009 import java.awt.*;
010 import java.awt.image.*;
011 import java.util.*;
012 import java.util.regex.*;
013 import java.io.*;
014 import javax.imageio.*;
015
016 //import com.kitfox.util.*;
017 //import com.kitfox.util.indexedObject.*;
018
019 import org.apache.tools.ant.*;
020 import org.apache.tools.ant.types.*;
021
022 import com.kitfox.svg.app.beans.*;
023 import com.kitfox.svg.*;
024 import com.kitfox.svg.xml.ColorTable;
025
026 /**
027 * <p>Translates a group of SVG files into images.</p>
028 *
029 * <p>Parameters:</p>
030 * <p><ul>
031 * <li/>destDir - If present, specifices a directory to write SVG files to. Otherwise
032 * writes images to directory SVG file was found in
033 * verbose - If true, prints processing information to the console
034 * <li/>format - File format for output images. The java core javax.imageio.ImageIO
035 * class is used for creating images, so format strings will depend on what
036 * files your system is configured to handle. By default, "gif", "jpg" and "png"
037 * files are guaranteed to be present. If omitted, "png" is used by default.
038 * <li/>backgroundColor - Optional background color. Color can be specified as a standard
039 * HTML color. That is, as the name of a standard color such as "blue" or
040 * "limegreen", using the # notaion as in #ff00ff for magenta, or in rgb format
041 * listing the components as in rgb(255, 192, 192) for pink. If omitted,
042 * background is transparent.
043 * <li/>antiAlias - If set, shapes are drawn using antialiasing. Defaults to true.
044 * <li/>interpolation - String describing image interpolation alrogithm. Can
045 * be one of "nearest neighbor", "bilinear" or "bicubic". Defaults to "bicubic".
046 * <li/>width - If greater than 0, determines the width of the written image. Otherwise,
047 * the width is obtained from the SVG document. Defaults to -1;
048 * <li/>height - If greater than 0, determines the height of the written image. Otherwise,
049 * the height is obtained from the SVG document. Defaults to -1.
050 * <li/>sizeToFit - If true and the width and height of the output image differ
051 * from that of the SVG image, the valid area of the SVG image will be resized
052 * to fit the specified size.
053 * <li/>verbose - IF true, prints out diagnostic infromation about processing.
054 * Defaults to false.
055 * </ul></p>
056 *
057 * Example:
058 * <SVGToImage destDir="${index.java}" format="jpg" verbose="true">
059 * <fileset dir="${dir1}">
060 * <include name="*.svg"/>
061 * </fileset>
062 * <fileset dir="${dir2}">
063 * <include name="*.svg"/>
064 * </fileset>
065 * </SVGToImage>
066 *
067 *
068 *
069 * @author kitfox
070 */
071 public class SVGToImageAntTask extends Task
072 {
073 private ArrayList filesets = new ArrayList();
074 boolean verbose = false;
075 File destDir;
076 private String format = "png";
077 Color backgroundColor = null;
078 int width = -1;
079 int height = -1;
080 boolean antiAlias = true;
081 String interpolation = "bicubic";
082 boolean clipToViewBox = false;
083 boolean sizeToFit = true;
084
085 /** Creates a new instance of IndexLoadObjectsAntTask */
086 public SVGToImageAntTask()
087 {
088 }
089
090
091 public String getFormat()
092 {
093 return format;
094 }
095
096 public void setFormat(String format)
097 {
098 this.format = format;
099 }
100
101 public void setBackgroundColor(String bgColor)
102 {
103 this.backgroundColor = ColorTable.parseColor(bgColor);
104 }
105
106 public void setHeight(int height)
107 {
108 this.height = height;
109 }
110
111 public void setWidth(int width)
112 {
113 this.width = width;
114 }
115
116 public void setAntiAlias(boolean antiAlias)
117 {
118 this.antiAlias = antiAlias;
119 }
120
121 public void setInterpolation(String interpolation)
122 {
123 this.interpolation = interpolation;
124 }
125
126 public void setSizeToFit(boolean sizeToFit)
127 {
128 this.sizeToFit = sizeToFit;
129 }
130
131 public void setClipToViewBox(boolean clipToViewBox)
132 {
133 this.clipToViewBox = clipToViewBox;
134 }
135
136 public void setVerbose(boolean verbose)
137 {
138 this.verbose = verbose;
139 }
140
141 public void setDestDir(File destDir)
142 {
143 this.destDir = destDir;
144 }
145
146 /**
147 * Adds a set of files.
148 */
149 public void addFileset(FileSet set)
150 {
151 filesets.add(set);
152 }
153
154
155
156 public void execute()
157 {
158 if (verbose) log("Building SVG images");
159
160 for (Iterator it = filesets.iterator(); it.hasNext();)
161 {
162 FileSet fs = (FileSet)it.next();
163 FileScanner scanner = fs.getDirectoryScanner(getProject());
164 String[] files = scanner.getIncludedFiles();
165
166 try
167 {
168 File basedir = scanner.getBasedir();
169
170 if (verbose) log("Scaning " + basedir);
171
172 for (int i = 0; i < files.length; i++)
173 {
174 //System.out.println("File " + files[i]);
175 //System.out.println("BaseDir " + basedir);
176 translate(basedir, files[i]);
177 }
178 }
179 catch (Exception e)
180 {
181 throw new BuildException(e);
182 }
183 }
184 }
185
186 private void translate(File baseDir, String shortName) throws BuildException
187 {
188 File source = new File(baseDir, shortName);
189
190 if (verbose) log("Reading file: " + source);
191
192 Matcher matchName = Pattern.compile("(.*)\\.svg", Pattern.CASE_INSENSITIVE).matcher(shortName);
193 if (matchName.matches())
194 {
195 shortName = matchName.group(1);
196 }
197 shortName += "." + format;
198
199 SVGIcon icon = new SVGIcon();
200 icon.setSvgURI(source.toURI());
201 icon.setAntiAlias(antiAlias);
202 if (interpolation.equals("nearest neighbor"))
203 {
204 icon.setInterpolation(SVGIcon.INTERP_NEAREST_NEIGHBOR);
205 }
206 else if (interpolation.equals("bilinear"))
207 {
208 icon.setInterpolation(SVGIcon.INTERP_BILINEAR);
209 }
210 else if (interpolation.equals("bicubic"))
211 {
212 icon.setInterpolation(SVGIcon.INTERP_BICUBIC);
213 }
214
215 int iconWidth = width > 0 ? width : icon.getIconWidth();
216 int iconHeight = height > 0 ? height : icon.getIconHeight();
217 icon.setClipToViewbox(clipToViewBox);
218 icon.setPreferredSize(new Dimension(iconWidth, iconHeight));
219 icon.setScaleToFit(sizeToFit);
220 BufferedImage image = new BufferedImage(iconWidth, iconHeight, BufferedImage.TYPE_INT_ARGB);
221 Graphics2D g = image.createGraphics();
222
223 if (backgroundColor != null)
224 {
225 g.setColor(backgroundColor);
226 g.fillRect(0, 0, iconWidth, iconHeight);
227 }
228
229 g.setClip(0, 0, iconWidth, iconHeight);
230 // g.fillRect(10, 10, 100, 100);
231 icon.paintIcon(null, g, 0, 0);
232 g.dispose();
233
234 File outFile = destDir == null ? new File(baseDir, shortName) : new File(destDir, shortName);
235 if (verbose) log("Writing file: " + outFile);
236
237 try
238 {
239 ImageIO.write(image, format, outFile);
240 }
241 catch (IOException e)
242 {
243 log("Error writing image: " + e.getMessage());
244 throw new BuildException(e);
245 }
246
247
248 SVGCache.getSVGUniverse().clear();
249 }
250
251 }