From 091a1e0179cb264cc2cab6e3b11ea31045c8536d Mon Sep 17 00:00:00 2001 From: kitfox Date: Tue, 29 May 2007 23:33:23 +0000 Subject: Restoring SVG Salamander to it's original code base, and updating build scripts. git-svn-id: https://svn.java.net/svn/svgsalamander~svn/trunk/svg-core@36 7dc7fa77-23fb-e6ad-8e2e-c86bd48ed22b --- .../com/kitfox/svg/app/ant/SVGToImageAntTask.java | 251 +++++++++++++++++++++ 1 file changed, 251 insertions(+) create mode 100644 src/main/java/com/kitfox/svg/app/ant/SVGToImageAntTask.java (limited to 'src/main/java/com/kitfox/svg/app/ant/SVGToImageAntTask.java') diff --git a/src/main/java/com/kitfox/svg/app/ant/SVGToImageAntTask.java b/src/main/java/com/kitfox/svg/app/ant/SVGToImageAntTask.java new file mode 100644 index 0000000..f4cd85a --- /dev/null +++ b/src/main/java/com/kitfox/svg/app/ant/SVGToImageAntTask.java @@ -0,0 +1,251 @@ +/* + * IndexLoadObjectsAntTask.java + * + * Created on January 22, 2005, 10:30 AM + */ + +package com.kitfox.svg.app.ant; + +import java.awt.*; +import java.awt.image.*; +import java.util.*; +import java.util.regex.*; +import java.io.*; +import javax.imageio.*; + +//import com.kitfox.util.*; +//import com.kitfox.util.indexedObject.*; + +import org.apache.tools.ant.*; +import org.apache.tools.ant.types.*; + +import com.kitfox.svg.app.beans.*; +import com.kitfox.svg.*; +import com.kitfox.svg.xml.ColorTable; + +/** + *

Translates a group of SVG files into images.

+ * + *

Parameters:

+ *

+ * + * Example: + * <SVGToImage destDir="${index.java}" format="jpg" verbose="true"> + * <fileset dir="${dir1}"> + * <include name="*.svg"/> + * </fileset> + * <fileset dir="${dir2}"> + * <include name="*.svg"/> + * </fileset> + * </SVGToImage> + * + * + * + * @author kitfox + */ +public class SVGToImageAntTask extends Task +{ + private Vector filesets = new Vector(); + boolean verbose = false; + File destDir; + private String format = "png"; + Color backgroundColor = null; + int width = -1; + int height = -1; + boolean antiAlias = true; + String interpolation = "bicubic"; + boolean clipToViewBox = false; + boolean sizeToFit = true; + + /** Creates a new instance of IndexLoadObjectsAntTask */ + public SVGToImageAntTask() + { + } + + + public String getFormat() + { + return format; + } + + public void setFormat(String format) + { + this.format = format; + } + + public void setBackgroundColor(String bgColor) + { + this.backgroundColor = ColorTable.parseColor(bgColor); + } + + public void setHeight(int height) + { + this.height = height; + } + + public void setWidth(int width) + { + this.width = width; + } + + public void setAntiAlias(boolean antiAlias) + { + this.antiAlias = antiAlias; + } + + public void setInterpolation(String interpolation) + { + this.interpolation = interpolation; + } + + public void setSizeToFit(boolean sizeToFit) + { + this.sizeToFit = sizeToFit; + } + + public void setClipToViewBox(boolean clipToViewBox) + { + this.clipToViewBox = clipToViewBox; + } + + public void setVerbose(boolean verbose) + { + this.verbose = verbose; + } + + public void setDestDir(File destDir) + { + this.destDir = destDir; + } + + /** + * Adds a set of files. + */ + public void addFileset(FileSet set) + { + filesets.add(set); + } + + + + public void execute() + { + if (verbose) log("Building SVG images"); + + for (Iterator it = filesets.iterator(); it.hasNext();) + { + FileSet fs = (FileSet)it.next(); + FileScanner scanner = fs.getDirectoryScanner(getProject()); + String[] files = scanner.getIncludedFiles(); + + try + { + File basedir = scanner.getBasedir(); + + if (verbose) log("Scaning " + basedir); + + for (int i = 0; i < files.length; i++) + { +//System.out.println("File " + files[i]); +//System.out.println("BaseDir " + basedir); + translate(basedir, files[i]); + } + } + catch (Exception e) + { + throw new BuildException(e); + } + } + } + + private void translate(File baseDir, String shortName) throws BuildException + { + File source = new File(baseDir, shortName); + + if (verbose) log("Reading file: " + source); + + Matcher matchName = Pattern.compile("(.*)\\.svg", Pattern.CASE_INSENSITIVE).matcher(shortName); + if (matchName.matches()) + { + shortName = matchName.group(1); + } + shortName += "." + format; + + SVGIcon icon = new SVGIcon(); + icon.setSvgURI(source.toURI()); + icon.setAntiAlias(antiAlias); + if (interpolation.equals("nearest neighbor")) + { + icon.setInterpolation(SVGIcon.INTERP_NEAREST_NEIGHBOR); + } + else if (interpolation.equals("bilinear")) + { + icon.setInterpolation(SVGIcon.INTERP_BILINEAR); + } + else if (interpolation.equals("bicubic")) + { + icon.setInterpolation(SVGIcon.INTERP_BICUBIC); + } + + int iconWidth = width > 0 ? width : icon.getIconWidth(); + int iconHeight = height > 0 ? height : icon.getIconHeight(); + icon.setClipToViewbox(clipToViewBox); + icon.setPreferredSize(new Dimension(iconWidth, iconHeight)); + icon.setScaleToFit(sizeToFit); + BufferedImage image = new BufferedImage(iconWidth, iconHeight, BufferedImage.TYPE_INT_ARGB); + Graphics2D g = image.createGraphics(); + + if (backgroundColor != null) + { + g.setColor(backgroundColor); + g.fillRect(0, 0, iconWidth, iconHeight); + } + + g.setClip(0, 0, iconWidth, iconHeight); +// g.fillRect(10, 10, 100, 100); + icon.paintIcon(null, g, 0, 0); + g.dispose(); + + File outFile = destDir == null ? new File(baseDir, shortName) : new File(destDir, shortName); + if (verbose) log("Writing file: " + outFile); + + try + { + ImageIO.write(image, format, outFile); + } + catch (IOException e) + { + log("Error writing image: " + e.getMessage()); + throw new BuildException(e); + } + + + SVGCache.getSVGUniverse().clear(); + } + +} -- cgit v1.2.3-55-g7522