summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/kitfox/svg/app/ant/SVGToImageAntTask.java
blob: 53f8687b0e78a5022e8f9ea256413177587c1974 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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;

/**
 * <p>Translates a group of SVG files into images.</p>
 * 
 * <p>Parameters:</p>
 * <p><ul>
 * <li/>destDir - If present, specifices a directory to write SVG files to.  Otherwise
 * writes images to directory SVG file was found in
 * verbose - If true, prints processing information to the console
 * <li/>format - File format for output images.  The java core javax.imageio.ImageIO
 * class is used for creating images, so format strings will depend on what
 * files your system is configured to handle.  By default, "gif", "jpg" and "png"
 * files are guaranteed to be present.  If omitted, "png" is used by default.
 * <li/>backgroundColor - Optional background color.  Color can be specified as a standard 
 * HTML color.  That is, as the name of a standard color such as "blue" or 
 * "limegreen", using the # notaion as in #ff00ff for magenta, or in rgb format
 * listing the components as in rgb(255, 192, 192) for pink.  If omitted,
 * background is transparent.
 * <li/>antiAlias - If set, shapes are drawn using antialiasing.  Defaults to true.
 * <li/>interpolation - String describing image interpolation alrogithm.  Can
 * be one of "nearest neighbor", "bilinear" or "bicubic".  Defaults to "bicubic".
 * <li/>width - If greater than 0, determines the width of the written image.  Otherwise,
 * the width is obtained from the SVG document.  Defaults to -1;
 * <li/>height - If greater than 0, determines the height of the written image.  Otherwise,
 * the height is obtained from the SVG document.  Defaults to -1.
 * <li/>sizeToFit - If true and the width and height of the output image differ
 * from that of the SVG image, the valid area of the SVG image will be resized 
 * to fit the specified size.
 * <li/>verbose - IF true, prints out diagnostic infromation about processing.  
 * Defaults to false.
 * </ul></p>
 * 
 * Example:
 * &lt;SVGToImage destDir="${index.java}" format="jpg" verbose="true"&gt;
 *    &lt;fileset dir="${dir1}"&gt;
 *        &lt;include name="*.svg"/&gt;
 *    &lt;/fileset&gt;
 *    &lt;fileset dir="${dir2}"&gt;
 *        &lt;include name="*.svg"/&gt;
 *    &lt;/fileset&gt;
 * &lt;/SVGToImage&gt;
 * 
 * 
 * 
 * @author kitfox
 */
public class SVGToImageAntTask extends Task
{
    private ArrayList filesets = new ArrayList();
    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();
    }
    
}