vhardy 2002/09/18 02:23:12 Modified: contrib/rasterizertask README build.xml contrib/rasterizertask/sources/org/apache/tools/ant/taskdefs/optional RasterizerTask.java RasterizerTaskSVGConverterController.java resources/org/apache/batik/apps/rasterizer/resources Messages.properties sources/org/apache/batik/apps/rasterizer Main.java SVGConverter.java sources/org/apache/batik/transcoder SVGAbstractTranscoder.java test-resources/org/apache/batik/transcoder/image unitTesting.xml test-sources/org/apache/batik/apps/rasterizer MainTest.java SVGConverterTest.java xdocs svgrasterizer.xml Added: test-references/org/apache/batik/transcoder/image anneMaxH200.png anneMaxW200.png anneMaxWH200.png Log: Contribution from Henri Ruini ([EMAIL PROTECTED]). Henri added max-width and max-height hints to the rasterizer infrastructure. He also cleaned-up some unncecessary import statements in the rasterizer app code. Tests have been added to the infrastructre. Revision Changes Path 1.2 +10 -2 xml-batik/contrib/rasterizertask/README Index: README =================================================================== RCS file: /home/cvs/xml-batik/contrib/rasterizertask/README,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- README 28 Nov 2001 08:59:12 -0000 1.1 +++ README 18 Sep 2002 09:23:11 -0000 1.2 @@ -6,7 +6,7 @@ Content of this directory ========================= -This directory contains rasterizer task for Ant. Th task can be used to +This directory contains rasterizer task for Ant. The task can be used to convert SVG files to raster format. This has been contributed by Henri Ruini ([EMAIL PROTECTED]). @@ -16,3 +16,11 @@ in this directory. Type .\build.bat help to display other available targets. These commands work only in Windows. + +NOTE: Due the internal changes in Ant itself the rasterizer task may not +work if the Ant in use is a different version from the Ant used to build +the task. Use the same Ant version to build and to execute task to avoid +strange problems. + +At least JDK 1.3 is needed to build and execute the task. + 1.2 +29 -24 xml-batik/contrib/rasterizertask/build.xml Index: build.xml =================================================================== RCS file: /home/cvs/xml-batik/contrib/rasterizertask/build.xml,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- build.xml 28 Nov 2001 08:59:12 -0000 1.1 +++ build.xml 18 Sep 2002 09:23:11 -0000 1.2 @@ -26,10 +26,14 @@ <!-- lib: Directory where the library files (jars etc.) are located. --> <property name="src" value="${root}/sources" /> <!-- src: Directory for source files. --> - <property name="build" value="${root}/classes" /> - <!-- build: Directory for compiles class files. --> - <property name="javadoc" value="${root}/javadoc" /> - <!-- javadocs: Directory for javadocs. --> + <property name="build" value="${root}/build" /> + <!-- build: Directory for build results. --> + <property name="build.classes" value="${build}/classes" /> + <!-- build.classes: Directory for compiled classes. --> + <property name="doc" value="${root}/doc" /> + <!-- doc: Directory for documentation. --> + <property name="doc.api" value="${doc}/javadoc" /> + <!-- doc.api: Directory for javadocs. --> @@ -39,37 +43,47 @@ <echo>SVG Rasterizer Task build file.</echo> <echo>Available targets are:</echo> <echo> compile --> Compiles the source code.</echo> - <echo> javadoc --> Generates Javadoc documentation.</echo> <echo> jar --> Builds the JAR distribution.</echo> - <echo> clean --> Deletes the generated directories.</echo> + <echo> javadoc --> Generates Javadoc documentation.</echo> + <echo> clean --> Deletes generated directories.</echo> </target> <target name="prepare"> + <echo>Using ${ant.version}.</echo> <tstamp/> </target> <target name="compile" depends="prepare" description="Compiles source files."> - <!-- Clean and (re)create the build directory. --> - <delete dir="${build}" /> - <mkdir dir="${build}" /> + <mkdir dir="${build.classes}" /> <!-- Compile code. --> - <javac srcdir="${src}" destdir="${build}" /> + <javac srcdir="${src}" destdir="${build.classes}" /> + </target> + + <target name="jar" depends="prepare, compile" + description="Creates a JAR package."> + + <mkdir dir="${build}/lib" /> + <jar jarfile="${build}/lib/RasterizerTask.jar" + basedir="${build.classes}" + excludes="**/CVS" + includes="**/*.class"> + </jar> </target> <target name="javadoc" depends="prepare" description="Generates Javadoc documentation."> <!-- Clean and (re)create the javadocs directory. --> - <delete dir="${javadoc}" /> - <mkdir dir="${javadoc}" /> + <delete dir="${doc.api}" /> + <mkdir dir="${doc.api}" /> <!-- Create javadocs. --> - <javadoc destdir="${javadoc}" + <javadoc destdir="${doc.api}" packagenames="org.apache.tools.ant.*" version="false" author="false" windowtitle="Rasterizer Task Javadoc" doctitle="Rasterizer Task API Specification" - bottom="Copyright © 2001 Apache Software Foundation. All Rights Reserved."> + bottom="Copyright © 2001-2002 Apache Software Foundation. All Rights Reserved."> <sourcepath> <pathelement path="${src}" /> <pathelement path="${root}/../../sources" /> @@ -78,18 +92,9 @@ </javadoc> </target> - <target name="jar" depends="prepare, compile" - description="Creates a JAR package."> - <jar jarfile="${root}/RasterizerTask.jar" - basedir="${build}" - excludes="**/CVS" - includes="**/*.class"> - </jar> - </target> - <target name="clean" description="Deletes directories created by this script."> - <delete dir="${javadoc}" /> + <delete dir="${doc}" /> <delete dir="${build}" /> </target> 1.2 +142 -59 xml-batik/contrib/rasterizertask/sources/org/apache/tools/ant/taskdefs/optional/RasterizerTask.java Index: RasterizerTask.java =================================================================== RCS file: /home/cvs/xml-batik/contrib/rasterizertask/sources/org/apache/tools/ant/taskdefs/optional/RasterizerTask.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- RasterizerTask.java 28 Nov 2001 08:59:12 -0000 1.1 +++ RasterizerTask.java 18 Sep 2002 09:23:11 -0000 1.2 @@ -16,13 +16,17 @@ import org.apache.tools.ant.taskdefs.MatchingTask; import org.apache.tools.ant.types.EnumeratedAttribute; import org.apache.tools.ant.types.FileSet; +import org.apache.tools.ant.util.JAXPUtils; // -- Batik classes ---------------------------------------------------------- import org.apache.batik.apps.rasterizer.SVGConverter; import org.apache.batik.apps.rasterizer.DestinationType; import org.apache.batik.apps.rasterizer.SVGConverterException; - import org.apache.batik.transcoder.image.JPEGTranscoder; +import org.apache.batik.util.XMLResourceDescriptor; + +// -- SAX classes ------------------------------------------------------------ +import org.xml.sax.XMLReader; // -- Java SDK classes ------------------------------------------------------- import java.awt.geom.Rectangle2D; @@ -52,14 +56,19 @@ */ public class RasterizerTask extends MatchingTask { - // -- Class constants ---------------------------------------------------- + // -- Constants ---------------------------------------------------------- /** * Default quality value for JPEGs. This value is used when * the user doesn't set the quality. */ - protected static final float DEFAULT_QUALITY = 0.99f; + private static final float DEFAULT_QUALITY = 0.99f; + /** + * Magic string indicating that any JAXP conforming XML parser can + * be used. + */ + private static final String JAXP_PARSER = "jaxp"; - // -- Class variables ---------------------------------------------------- + // -- Variables ---------------------------------------------------------- /** Result image type. The default is PNG. */ protected DestinationType resultType = DestinationType.PNG; @@ -67,6 +76,10 @@ protected float height = Float.NaN; /** Output image width. */ protected float width = Float.NaN; + /** Maximum output image height. */ + protected float maxHeight = Float.NaN; + /** Maximum output image width. */ + protected float maxWidth = Float.NaN; /** Output image quality. */ protected float quality = Float.NaN; /** Output Area of Interest (AOI) area. */ @@ -79,23 +92,21 @@ protected float dpi = Float.NaN; /** Output image language. */ protected String language = null; + /** XML parser class currently in use. */ + protected String readerClassName = XMLResourceDescriptor.getXMLParserClassName(); + /** Source image path. */ protected File srcFile = null; - /** Destination image path. */ protected File destFile = null; - /** Source directory of images. */ protected File srcDir = null; - /** Destination directory for output images. */ protected File destDir = null; - /** Contents of <code>fileset</code> elements. */ protected Vector filesets = new Vector(); - /** Converter object used to convert SVG images to raster images. */ protected SVGConverter converter; @@ -146,6 +157,28 @@ } /** + * Gets <code>maxheight</code> attribute value. + * + * <p>The attribute is optional.</p> + * + * @param height Attribute value. + */ + public void setMaxheight(float height) { + this.maxHeight = height; + } + + /** + * Gets <code>maxwidth</code> attribute value. + * + * <p>The attribute is optional.</p> + * + * @param width Attribute value. + */ + public void setMaxwidth(float width) { + this.maxWidth = width; + } + + /** * Gets <code>quality</code> attribute value. * * <p>The value have to be a float between 0 and 1 excluded. @@ -219,6 +252,16 @@ this.language = language; } + /** + * Sets classname of an XML parser. + * The attribute is optional. + * + * @param value Java classname of an XML parser. + */ + public void setClassname(String value) { + this.readerClassName = value; + } + /** * Gets <code>src</code> attribute value. * @@ -294,45 +337,55 @@ String[] sources; // Array of input files. - // Check file and directory values. - if(this.srcFile != null) { - if(this.destFile == null) { - throw new BuildException("dest attribute is not set."); - } - } else { - if((this.srcDir == null) && (filesets.size() == 0)) { - throw new BuildException("No input files! Either srcdir or fileset have to be set."); - } - if(this.destDir == null) { - throw new BuildException("destdir attribute is not set!"); - } - } - + // Store default XML parser information and set user class. + String defaultParser = XMLResourceDescriptor.getXMLParserClassName(); // Throws BuildException. - setRasterizingParameters(); + XMLResourceDescriptor.setXMLParserClassName(getParserClassName(readerClassName)); - // Get and set source(s). - sources = getSourceFiles(); - converter.setSources(sources); + try { + // Check file and directory values. + if(this.srcFile != null) { + if(this.destFile == null) { + throw new BuildException("dest attribute is not set."); + } + } else { + if((this.srcDir == null) && (filesets.size() == 0)) { + throw new BuildException("No input files! Either srcdir or fileset have to be set."); + } + if(this.destDir == null) { + throw new BuildException("destdir attribute is not set!"); + } + } - // Set destination. - if(this.srcFile != null) { - converter.setDst(this.destFile); - } else { - converter.setDst(this.destDir); - } + // Throws BuildException. + setRasterizingParameters(); - // Input filenames are stored in the converter and - // everything is ready for the conversion. + // Get and set source(s). + sources = getSourceFiles(); + converter.setSources(sources); + + // Set destination. + if(this.srcFile != null) { + converter.setDst(this.destFile); + } else { + converter.setDst(this.destDir); + } - log("Rasterizing " + sources.length + - (sources.length == 1 ? " image " : " images ") + - "from SVG to " + this.resultType.toString() + "."); + // Input filenames are stored in the converter and + // everything is ready for the conversion. - try { - converter.execute(); - } catch(SVGConverterException sce) { - throw new BuildException(sce.getMessage()); + log("Rasterizing " + sources.length + + (sources.length == 1 ? " image " : " images ") + + "from SVG to " + this.resultType.toString() + "."); + + try { + converter.execute(); + } catch(SVGConverterException sce) { + throw new BuildException(sce.getMessage()); + } + } finally { + // Restore default XML parser for the next execute. + XMLResourceDescriptor.setXMLParserClassName(defaultParser); } } @@ -357,18 +410,32 @@ } else { throw new BuildException("Unknown value in result parameter."); } + // Set size values. if(!Float.isNaN(this.width)) { if(this.width < 0) { - throw new BuildException("Value in width parameter must positive."); + throw new BuildException("Value of width parameter must positive."); } converter.setWidth(this.width); } if(!Float.isNaN(this.height)) { if(this.height < 0) { - throw new BuildException("Value in height parameter must positive."); + throw new BuildException("Value of height parameter must positive."); } converter.setHeight(this.height); } + // Set maximum size values. + if(!Float.isNaN(this.maxWidth)) { + if(this.maxWidth < 0) { + throw new BuildException("Value of maxwidth parameter must positive."); + } + converter.setMaxWidth(this.maxWidth); + } + if(!Float.isNaN(this.maxHeight)) { + if(this.maxHeight < 0) { + throw new BuildException("Value of maxheight parameter must positive."); + } + converter.setMaxHeight(this.maxHeight); + } // The quality is just swallowed if the result type is not correct. if(allowedToSetQuality(resultType)) { if(!Float.isNaN(this.quality)) { @@ -394,10 +461,10 @@ } if(!Float.isNaN(this.dpi)) { if(this.dpi < 0) { - throw new BuildException("Value in dpi parameter must positive."); + throw new BuildException("Value of dpi parameter must positive."); } // The calculation is the same as 2.54/dpi*10 where - converter.setPixelToMillimeter(25.4f/this.dpi); + converter.setPixelUnitToMillimeter(25.4f/this.dpi); } if(this.language != null) { converter.setLanguage(this.language); @@ -412,8 +479,7 @@ */ protected String[] getSourceFiles() { - String[] list; // Input files in array. - ArrayList inputFiles = new ArrayList(); // Input files in temp list. + List inputFiles = new ArrayList(); // Input files in temp list. if(this.srcFile != null) { // Only one source and destination file have been set. @@ -454,14 +520,8 @@ } } - // Copy items from list object to array. - list = new String[inputFiles.size()]; - Iterator iter = inputFiles.iterator(); - for(int i = 0 ; iter.hasNext() ; i++) { - list[i] = (String)iter.next(); - } - - return list; + // Convert List to array and return the array. + return (String[])inputFiles.toArray(new String[0]); } /** @@ -599,6 +659,29 @@ } return new Color(r, g, b, a); + } + + /** + * Returns name of an XML parser. + * Magic string {@link #JAXP_PARSER} is also accepted. + * + * @param className Name of the XML parser class or a magic string. + * + * @return Name of an XML parser. + * + * @throws BuildException Unable to get the name of JAXP parser. + */ + private String getParserClassName(final String className) { + String name = className; + if(className.equals(JAXP_PARSER)) { + // Set first JAXP parser. + // Throws BuildException. + XMLReader reader = JAXPUtils.getXMLReader(); + name = reader.getClass().getName(); + } + + log("Using class '" + name + "' to parse SVG documents.", Project.MSG_VERBOSE); + return name; } 1.2 +3 -4 xml-batik/contrib/rasterizertask/sources/org/apache/tools/ant/taskdefs/optional/RasterizerTaskSVGConverterController.java Index: RasterizerTaskSVGConverterController.java =================================================================== RCS file: /home/cvs/xml-batik/contrib/rasterizertask/sources/org/apache/tools/ant/taskdefs/optional/RasterizerTaskSVGConverterController.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- RasterizerTaskSVGConverterController.java 28 Nov 2001 08:59:12 -0000 1.1 +++ RasterizerTaskSVGConverterController.java 18 Sep 2002 09:23:11 -0000 1.2 @@ -40,7 +40,7 @@ */ public class RasterizerTaskSVGConverterController implements SVGConverterController { - // -- Class variables ---------------------------------------------------- + // -- Variables ---------------------------------------------------------- /** Ant task that is used to log messages. */ protected Task executingTask = null; @@ -63,7 +63,6 @@ // -- Public interface --------------------------------------------------- - public boolean proceedWithComputedTask(Transcoder transcoder, Map hints, Vector sources, @@ -80,7 +79,7 @@ File dest, String errorCode){ if(executingTask != null) { - executingTask.log("Unable to rasterize image from '" + executingTask.log("Unable to rasterize image '" + source.getName() + "' to '" + dest.getAbsolutePath() + "': " + errorCode); } 1.11 +17 -3 xml-batik/resources/org/apache/batik/apps/rasterizer/resources/Messages.properties Index: Messages.properties =================================================================== RCS file: /home/cvs/xml-batik/resources/org/apache/batik/apps/rasterizer/resources/Messages.properties,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- Messages.properties 9 Sep 2002 13:02:06 -0000 1.10 +++ Messages.properties 18 Sep 2002 09:23:11 -0000 1.11 @@ -71,6 +71,10 @@ \toutput width. This is a floating point value. \n \ -h <height> \n \ \toutput height. This is a floating point value. \n \ + -maxw <width> \n \ +\tMaximum output width. This is a floating point value. \n \ + -maxh <height> \n \ +\tMaximum output height. This is a floating point value. \n \ -a <area> \n \ \toutput area. The format for <area> is x,y,w,h, where x, y, w and h \n \ \tare floating point values. \n \ @@ -135,15 +139,25 @@ Default: image/png Main.cl.option.width.description = \ --w <width> Output width. This is a floating point value. \n +-w <width> Output width. This is a floating point value. \n \ Example: -w 455.6 \n \ Default: none (which means that the images's width will be used) Main.cl.option.height.description = \ --h <height> Output height. This is a floating point value. \n +-h <height> Output height. This is a floating point value. \n \ Example: -h 345.67 \n \ Default: none (which means that the image's height will be used) +Main.cl.option.max.width.description = \ +-maxw <width> Maximum output width. This is a floating point value. \n \ +Example: -maxw 455.6 \n \ +Default: none (which means that the option is ignored) + +Main.cl.option.max.height.description = \ +-maxh <height> Maximum output height. This is a floating point value. \n \ +Example: -maxh 345.67 \n \ +Default: none (which means that the option is ignored) + Main.cl.option.area.description = \ -a <area> output area. The format for <area> is x,y,w,h, where x, y, w and h are \n \ floating point values. \n \ @@ -166,7 +180,7 @@ Main.cl.option.default.font.family.description = \ -font-family <defaultValue> Value used as a default when no font-family value \n \ is specified.\n \ -Example: -font-family "Times, Comic Sans MS" +Example: -font-family "Times, Comic Sans MS" \n \ Default: "Arial, Helvetica, sans-serif" Main.cl.option.cssAlternate.description = \ 1.25 +51 -19 xml-batik/sources/org/apache/batik/apps/rasterizer/Main.java Index: Main.java =================================================================== RCS file: /home/cvs/xml-batik/sources/org/apache/batik/apps/rasterizer/Main.java,v retrieving revision 1.24 retrieving revision 1.25 diff -u -r1.24 -r1.25 --- Main.java 9 Sep 2002 13:02:07 -0000 1.24 +++ Main.java 18 Sep 2002 09:23:11 -0000 1.25 @@ -11,36 +11,19 @@ import java.awt.Color; import java.awt.geom.Rectangle2D; -import java.io.BufferedOutputStream; import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; import java.util.Hashtable; import java.util.Map; import java.util.Vector; import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; import java.util.StringTokenizer; -import java.net.URL; -import java.net.MalformedURLException; -import org.apache.batik.transcoder.TranscoderInput; -import org.apache.batik.transcoder.TranscoderOutput; -import org.apache.batik.transcoder.image.ImageTranscoder; import org.apache.batik.transcoder.Transcoder; -import org.apache.batik.transcoder.image.JPEGTranscoder; -import org.apache.batik.transcoder.image.PNGTranscoder; -import org.apache.batik.transcoder.image.TIFFTranscoder; import org.apache.batik.util.ApplicationSecurityEnforcer; -import org.xml.sax.InputSource; - /** * Handles command line parameters to configure the <tt>SVGConverter</tt> * and rasterizer images. <br /> @@ -324,6 +307,24 @@ = Messages.get("Main.cl.option.height.description", "No description"); /** + * Option to specify the output image's maximum width. + */ + public static String CL_OPTION_MAX_WIDTH + = Messages.get("Main.cl.option.max.width", "-maxw"); + + public static String CL_OPTION_MAX_WIDTH_DESCRIPTION + = Messages.get("Main.cl.option.max.width.description", "No description"); + + /** + * Option to specify the output image's maximum height. + */ + public static String CL_OPTION_MAX_HEIGHT + = Messages.get("Main.cl.option.max.height", "-maxh"); + + public static String CL_OPTION_MAX_HEIGHT_DESCRIPTION + = Messages.get("Main.cl.option.max.height.description", "No description"); + + /** * Option to specify the area of interest in the output * image. */ @@ -552,7 +553,38 @@ } }); - + optionMap.put(CL_OPTION_MAX_WIDTH, + new FloatOptionHandler(){ + public void handleOption(float optionValue, + SVGConverter c){ + if (optionValue <= 0){ + throw new IllegalArgumentException(); + } + + c.setMaxWidth(optionValue); + } + + public String getOptionDescription(){ + return CL_OPTION_MAX_WIDTH_DESCRIPTION; + } + }); + + optionMap.put(CL_OPTION_MAX_HEIGHT, + new FloatOptionHandler(){ + public void handleOption(float optionValue, + SVGConverter c){ + if (optionValue <= 0){ + throw new IllegalArgumentException(); + } + + c.setMaxHeight(optionValue); + } + + public String getOptionDescription(){ + return CL_OPTION_MAX_HEIGHT_DESCRIPTION; + } + }); + optionMap.put(CL_OPTION_AOI, new RectangleOptionHandler(){ public void handleOption(Rectangle2D optionValue, 1.18 +48 -10 xml-batik/sources/org/apache/batik/apps/rasterizer/SVGConverter.java Index: SVGConverter.java =================================================================== RCS file: /home/cvs/xml-batik/sources/org/apache/batik/apps/rasterizer/SVGConverter.java,v retrieving revision 1.17 retrieving revision 1.18 diff -u -r1.17 -r1.18 --- SVGConverter.java 9 Sep 2002 13:02:07 -0000 1.17 +++ SVGConverter.java 18 Sep 2002 09:23:11 -0000 1.18 @@ -9,7 +9,6 @@ package org.apache.batik.apps.rasterizer; import org.apache.batik.transcoder.Transcoder; -import org.apache.batik.transcoder.TranscoderException; import org.apache.batik.transcoder.TranscoderInput; import org.apache.batik.transcoder.TranscoderOutput; import org.apache.batik.transcoder.image.ImageTranscoder; @@ -18,7 +17,6 @@ import java.io.File; import java.io.FileFilter; -import java.io.FileInputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.FileOutputStream; @@ -26,14 +24,11 @@ import java.io.FileNotFoundException; import java.awt.Color; -import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; -import java.awt.image.BufferedImage; import java.util.HashMap; import java.util.Vector; import java.util.Map; -import java.util.StringTokenizer; /** * This application can be used to convert SVG images to raster images. @@ -60,9 +55,11 @@ * There are a number of options which control the way the image is * converted to the destination format:<br /><ul> * <li>destinationType: controls the type of conversion which should be done. - * see the {@link DestinationType} documentation.</li> + * see the {@link DestinationType} documentation.</li> * <li>width/height: they control the desired width and height, in user space, - * for the output image.</li> + * for the output image.</li> + * <li>maxWidth/maxHeight: control the maximum width and height, + * in user space, of the output image.</li> * <li>area: controls the specific sub-area of the image which should be * rendered.</li> * <li>backgroundColor: controls the color which is used to fill the @@ -85,8 +82,9 @@ * </ul> * * @version $Id$ - * @author Henri Ruini - * @author <a href="mailto:[EMAIL PROTECTED]">Vincent Hardy</a> */ + * @author <a href="[EMAIL PROTECTED]">Henri Ruini</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Vincent Hardy</a> + */ public class SVGConverter { // // Error codes reported by the SVGConverter @@ -204,6 +202,12 @@ /** Output image width. */ protected float width = DEFAULT_WIDTH; + /** Maximum output image height. */ + protected float maxHeight = DEFAULT_HEIGHT; + + /** Maximum output image width. */ + protected float maxWidth = DEFAULT_WIDTH; + /** Output image quality. */ protected float quality = DEFAULT_QUALITY; @@ -333,6 +337,32 @@ } /** + * If less than or equal to zero, the maximum height + * does not have any effect on the output image. + * The maximum height is in user space. + */ + public void setMaxHeight(float height) { + this.maxHeight = height; + } + + public float getMaxHeight(){ + return maxHeight; + } + + /** + * If less than or equal to zero, the maximum width + * does not have any effect on the output image. + * The maximum width is in user space. + */ + public void setMaxWidth(float width) { + this.maxWidth = width; + } + + public float getMaxWidth(){ + return maxWidth; + } + + /** * Sets the JPEG encoding quality. The value should be strictly * less than 1. If the value is less than zero, then the maximum * encoding quality is used. @@ -792,6 +822,14 @@ } if (width > 0){ map.put(ImageTranscoder.KEY_WIDTH, new Float(this.width)); + } + + // Set maximum height and width --------------------------------------- + if (maxHeight > 0) { + map.put(ImageTranscoder.KEY_MAX_HEIGHT, new Float(this.maxHeight)); + } + if (maxWidth > 0){ + map.put(ImageTranscoder.KEY_MAX_WIDTH, new Float(this.maxWidth)); } // Set CSS Media 1.3 +115 -37 xml-batik/sources/org/apache/batik/transcoder/SVGAbstractTranscoder.java Index: SVGAbstractTranscoder.java =================================================================== RCS file: /home/cvs/xml-batik/sources/org/apache/batik/transcoder/SVGAbstractTranscoder.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- SVGAbstractTranscoder.java 9 Sep 2002 13:02:07 -0000 1.2 +++ SVGAbstractTranscoder.java 18 Sep 2002 09:23:12 -0000 1.3 @@ -8,15 +8,11 @@ package org.apache.batik.transcoder; -import java.awt.Cursor; import java.awt.Dimension; -import java.awt.Point; import java.awt.geom.AffineTransform; import java.awt.geom.Dimension2D; import java.awt.geom.Rectangle2D; -import java.net.URL; - import java.util.StringTokenizer; import java.util.Vector; @@ -26,12 +22,10 @@ import org.apache.batik.dom.util.DocumentFactory; import org.apache.batik.gvt.GraphicsNode; -import org.apache.batik.gvt.event.EventDispatcher; import org.apache.batik.bridge.BaseScriptingEnvironment; import org.apache.batik.bridge.BridgeContext; import org.apache.batik.bridge.BridgeException; -import org.apache.batik.bridge.BridgeExtension; import org.apache.batik.bridge.NoLoadScriptSecurity; import org.apache.batik.bridge.DefaultScriptSecurity; import org.apache.batik.bridge.RelaxedScriptSecurity; @@ -49,13 +43,10 @@ import org.apache.batik.transcoder.image.resources.Messages; import org.apache.batik.util.SVGConstants; -import org.apache.batik.util.XMLResourceDescriptor; import org.apache.batik.util.ParsedURL; import org.w3c.dom.DOMImplementation; import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.svg.SVGAElement; import org.w3c.dom.svg.SVGSVGElement; @@ -95,6 +86,9 @@ */ protected GraphicsNode root; + /** + * Image's width and height. + */ protected float width, height; /** The user agent dedicated to an SVG Transcoder. */ @@ -186,29 +180,8 @@ ctx = null; builder = null; - // compute the image's width and height according the hints - float imgWidth = -1; - if (hints.containsKey(KEY_WIDTH)) { - imgWidth = ((Float)hints.get(KEY_WIDTH)).floatValue(); - } - float imgHeight = -1; - if (hints.containsKey(KEY_HEIGHT)) { - imgHeight = ((Float)hints.get(KEY_HEIGHT)).floatValue(); - } + setImageSize(docWidth, docHeight); - if (imgWidth > 0 && imgHeight > 0) { - width = imgWidth; - height = imgHeight; - } else if (imgHeight > 0) { - width = (docWidth * imgHeight) / docHeight; - height = imgHeight; - } else if (imgWidth > 0) { - width = imgWidth; - height = (docHeight * imgWidth) / docWidth; - } else { - width = docWidth; - height = docHeight; - } // compute the preserveAspectRatio matrix AffineTransform Px; String ref = new ParsedURL(uri).getRef(); @@ -250,6 +223,60 @@ this.root = gvtRoot; } + /** + * Sets document size according to the hints. + * Global variables width and height are modified. + * + * @param docWidth Width of the document. + * @param docHeight Height of the document. + */ + protected void setImageSize(float docWidth, float docHeight) { + + // Compute the image's width and height according the hints + float imgWidth = -1; + if (hints.containsKey(KEY_WIDTH)) { + imgWidth = ((Float)hints.get(KEY_WIDTH)).floatValue(); + } + float imgHeight = -1; + if (hints.containsKey(KEY_HEIGHT)) { + imgHeight = ((Float)hints.get(KEY_HEIGHT)).floatValue(); + } + + if (imgWidth > 0 && imgHeight > 0) { + width = imgWidth; + height = imgHeight; + } else if (imgHeight > 0) { + width = (docWidth * imgHeight) / docHeight; + height = imgHeight; + } else if (imgWidth > 0) { + width = imgWidth; + height = (docHeight * imgWidth) / docWidth; + } else { + width = docWidth; + height = docHeight; + } + + // Limit image size according to the maximuxm size hints. + float imgMaxWidth = -1; + if (hints.containsKey(KEY_MAX_WIDTH)) { + imgMaxWidth = ((Float)hints.get(KEY_MAX_WIDTH)).floatValue(); + } + float imgMaxHeight = -1; + if (hints.containsKey(KEY_MAX_HEIGHT)) { + imgMaxHeight = ((Float)hints.get(KEY_MAX_HEIGHT)).floatValue(); + } + + if ((imgMaxHeight > 0) && (height > imgMaxHeight)) { + width = (docWidth * imgMaxHeight) / docHeight; + height = imgMaxHeight; + } + if ((imgMaxWidth > 0) && (width > imgMaxWidth)) { + width = imgMaxWidth; + height = (docHeight * imgMaxWidth) / docWidth; + } + } + + // -------------------------------------------------------------------- // Keys definition // -------------------------------------------------------------------- @@ -298,6 +325,57 @@ public static final TranscodingHints.Key KEY_HEIGHT = new LengthKey(); + /** + * The maximum width of the image key. + * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1"> + * <TR> + * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH> + * <TD VALIGN="TOP">KEY_MAX_WIDTH</TD></TR> + * <TR> + * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH> + * <TD VALIGN="TOP">Float</TD></TR> + * <TR> + * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH> + * <TD VALIGN="TOP">The width of the top most svg element</TD></TR> + * <TR> + * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH> + * <TD VALIGN="TOP">No</TD></TR> + * <TR> + * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH> + * <TD VALIGN="TOP">Specify the maximum width of the image to create. + * The value will set the maximum width of the image even when + * bigger width is specified in a document or set with KEY_WIDTH. + * </TD></TR> + * </TABLE> + */ + public static final TranscodingHints.Key KEY_MAX_WIDTH + = new LengthKey(); + + /** + * The maximux height of the image key. + * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1"> + * <TR> + * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH> + * <TD VALIGN="TOP">KEY_MAX_HEIGHT</TD></TR> + * <TR> + * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH> + * <TD VALIGN="TOP">Float</TD></TR> + * <TR> + * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH> + * <TD VALIGN="TOP">The height of the top most svg element</TD></TR> + * <TR> + * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH> + * <TD VALIGN="TOP">No</TD></TR> + * <TR> + * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH> + * <TD VALIGN="TOP">Specify the maximum height of the image to create. + * The value will set the maximum height of the image even when + * bigger height is specified in a document or set with KEY_HEIGHT. + * </TD></TR> + * </TABLE> + */ + public static final TranscodingHints.Key KEY_MAX_HEIGHT + = new LengthKey(); /** * The area of interest key. @@ -726,9 +804,9 @@ } /** - * Returns true if the XML parser must be in validation mode, false - * otherwise. - */ + * Returns true if the XML parser must be in validation mode, false + * otherwise. + */ public boolean isXMLParserValidating() { Boolean b = (Boolean)SVGAbstractTranscoder.this.hints.get (KEY_XML_PARSER_VALIDATING); @@ -744,11 +822,11 @@ * * @param scriptType type of script, as found in the * type attribute of the <script> element. - * @param scriptURL url for the script, as defined in + * @param scriptPURL url for the script, as defined in * the script's xlink:href attribute. If that * attribute was empty, then this parameter should * be null - * @param docURL url for the document into which the + * @param docPURL url for the document into which the * script was found. */ public ScriptSecurity getScriptSecurity(String scriptType, 1.1 xml-batik/test-references/org/apache/batik/transcoder/image/anneMaxH200.png <<Binary file>> 1.1 xml-batik/test-references/org/apache/batik/transcoder/image/anneMaxW200.png <<Binary file>> 1.1 xml-batik/test-references/org/apache/batik/transcoder/image/anneMaxWH200.png <<Binary file>> 1.3 +58 -1 xml-batik/test-resources/org/apache/batik/transcoder/image/unitTesting.xml Index: unitTesting.xml =================================================================== RCS file: /home/cvs/xml-batik/test-resources/org/apache/batik/transcoder/image/unitTesting.xml,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- unitTesting.xml 9 Sep 2002 13:02:07 -0000 1.2 +++ unitTesting.xml 18 Sep 2002 09:23:12 -0000 1.3 @@ -133,6 +133,63 @@ </testGroup> +<!-- ================================================================== --> +<!-- KEY_MAX_WIDTH and/or KEY_MAX_HEIGHT tests --> +<!-- ================================================================== --> + +<testGroup id="transcoder.image.hints.maxDimension" class="org.apache.batik.transcoder.image.MaxDimensionTest"> + +<!-- ###### Limit size in the document ###### --> +<test id="transcoder.image.hints.maxWidth200"> + <arg class="java.lang.String" value="samples/anne.svg" /> + <arg class="java.lang.String" value="test-references/org/apache/batik/transcoder/image/anneMaxW200.png" /> + <arg class="java.lang.Float" value="200" /> + <arg class="java.lang.Float" value="-1" /> +</test> + +<test id="transcoder.image.hints.maxHeight200"> + <arg class="java.lang.String" value="samples/anne.svg" /> + <arg class="java.lang.String" value="test-references/org/apache/batik/transcoder/image/anneMaxH200.png" /> + <arg class="java.lang.Float" value="-1" /> + <arg class="java.lang.Float" value="200" /> +</test> + +<test id="transcoder.image.hints.maxWidthHeight200"> + <arg class="java.lang.String" value="samples/anne.svg" /> + <arg class="java.lang.String" value="test-references/org/apache/batik/transcoder/image/anneMaxWH200.png" /> + <arg class="java.lang.Float" value="200" /> + <arg class="java.lang.Float" value="200" /> +</test> + +<!-- ###### Limit size set in hints ###### --> +<test id="transcoder.image.hints.maxWidth200.overrideHints"> + <arg class="java.lang.String" value="samples/anne.svg" /> + <arg class="java.lang.String" value="test-references/org/apache/batik/transcoder/image/anneMaxW200.png" /> + <arg class="java.lang.Float" value="200" /> + <arg class="java.lang.Float" value="-1" /> + <arg class="java.lang.Float" value="300" /> + <arg class="java.lang.Float" value="-1" /> +</test> + +<test id="transcoder.image.hints.maxHeight200.overrideHints"> + <arg class="java.lang.String" value="samples/anne.svg" /> + <arg class="java.lang.String" value="test-references/org/apache/batik/transcoder/image/anneMaxH200.png" /> + <arg class="java.lang.Float" value="-1" /> + <arg class="java.lang.Float" value="200" /> + <arg class="java.lang.Float" value="-1" /> + <arg class="java.lang.Float" value="300" /> +</test> + +<test id="transcoder.image.hints.maxWidthHeight200.overrideHints"> + <arg class="java.lang.String" value="samples/anne.svg" /> + <arg class="java.lang.String" value="test-references/org/apache/batik/transcoder/image/anneMaxWH200.png" /> + <arg class="java.lang.Float" value="200" /> + <arg class="java.lang.Float" value="200" /> + <arg class="java.lang.Float" value="300" /> + <arg class="java.lang.Float" value="300" /> +</test> + +</testGroup> <!-- ================================================================== --> <!-- KEY_BACKGROUND_COLOR --> 1.9 +45 -2 xml-batik/test-sources/org/apache/batik/apps/rasterizer/MainTest.java Index: MainTest.java =================================================================== RCS file: /home/cvs/xml-batik/test-sources/org/apache/batik/apps/rasterizer/MainTest.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- MainTest.java 9 Sep 2002 13:02:06 -0000 1.8 +++ MainTest.java 18 Sep 2002 09:23:12 -0000 1.9 @@ -157,7 +157,7 @@ t = new MainConfigTest("-w 467.69") { public TestReport validate(SVGConverter c){ float width = c.getWidth(); - if(width != 467.69){ + if(width == 467.69f){ return reportSuccess(); } else { return reportError("-w", "" + 467.69, "" + width); @@ -184,6 +184,33 @@ addTest(t); t.setId("MainConfigTest.height"); + t = new MainConfigTest("-maxw 467.69") { + public TestReport validate(SVGConverter c){ + float maxWidth = c.getMaxWidth(); + if(maxWidth == 467.69f){ + return reportSuccess(); + } else { + return reportError("-maxw", "" + 467.69, "" + maxWidth); + } + } + + }; + addTest(t); + t.setId("MainConfigTest.maxWidth"); + + t = new MainConfigTest("-maxh 345.67") { + public TestReport validate(SVGConverter c){ + float maxHeight = c.getMaxHeight(); + if(maxHeight == 345.67f){ + return reportSuccess(); + } else { + return reportError("-maxh", "" + 345.67, "" + maxHeight); + } + } + }; + addTest(t); + t.setId("MainConfigTest.maxHeight"); + t = new MainConfigTest("-a 5,10,20,30") { public TestReport validate(SVGConverter c){ Rectangle2D aoi = c.getArea(); @@ -428,6 +455,14 @@ addTest(t); t.setId("MainConfigErrorTest.height"); + t = new MainConfigErrorTest("-maxw", "hello.svg -maxw"); + addTest(t); + t.setId("MainConfigErrorTest.maxWidth"); + + t = new MainConfigErrorTest("-maxh", "hello.svg -maxh"); + addTest(t); + t.setId("MainConfigErrorTest.maxHeight"); + t = new MainConfigErrorTest("-a", "hello.svg -a"); addTest(t); t.setId("MainConfigErrorTest.area"); @@ -479,6 +514,14 @@ t = new MainIllegalArgTest("-h", "-h abaa"); addTest(t); t.setId("MainIllegalArgTest.height"); + + t = new MainIllegalArgTest("-maxw", "-maxw abd"); + addTest(t); + t.setId("MainIllegalArgTest.maxWidth"); + + t = new MainIllegalArgTest("-maxh", "-maxh abaa"); + addTest(t); + t.setId("MainIllegalArgTest.maxHeight"); t = new MainIllegalArgTest("a", "-a aaaaaa"); addTest(t); 1.16 +19 -1 xml-batik/test-sources/org/apache/batik/apps/rasterizer/SVGConverterTest.java Index: SVGConverterTest.java =================================================================== RCS file: /home/cvs/xml-batik/test-sources/org/apache/batik/apps/rasterizer/SVGConverterTest.java,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- SVGConverterTest.java 9 Sep 2002 13:02:06 -0000 1.15 +++ SVGConverterTest.java 18 Sep 2002 09:23:12 -0000 1.16 @@ -115,6 +115,24 @@ t.setId("HintsConfigTest.KEY_WIDTH"); t = new HintsConfigTest(new Object[][]{ + {ImageTranscoder.KEY_MAX_HEIGHT, new Float(50)}}){ + protected void deltaConfigure(SVGConverter c){ + c.setMaxHeight(50); + } + }; + addTest(t); + t.setId("HintsConfigTest.KEY_MAX_HEIGHT"); + + t = new HintsConfigTest(new Object[][]{ + {ImageTranscoder.KEY_MAX_WIDTH, new Float(50)}}){ + protected void deltaConfigure(SVGConverter c){ + c.setMaxWidth(50); + } + }; + addTest(t); + t.setId("HintsConfigTest.KEY_MAX_WIDTH"); + + t = new HintsConfigTest(new Object[][]{ {ImageTranscoder.KEY_MEDIA, "print"}}){ protected void deltaConfigure(SVGConverter c){ c.setMediaType("print"); 1.13 +41 -13 xml-batik/xdocs/svgrasterizer.xml Index: svgrasterizer.xml =================================================================== RCS file: /home/cvs/xml-batik/xdocs/svgrasterizer.xml,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- svgrasterizer.xml 19 Jun 2002 07:02:19 -0000 1.12 +++ svgrasterizer.xml 18 Sep 2002 09:23:12 -0000 1.13 @@ -66,7 +66,7 @@ <s2 title="Using the binary distribution" > <p>If you downloaded the binary distribution of Batik, you should have - gotten a file called <em>batik-1.5beta3.zip</em>, and, after expanding that + gotten a file called <em>batik-1.5beta4.zip</em>, and, after expanding that file, a JAR (Java ARchive) file called <code>batik-rasterizer.jar</code>. To start the rasterizer, open a console, go to the directory where you expanded the distribution (and where <code>batik-rasterizer.jar</code> is located) and @@ -82,10 +82,12 @@ <p><code>java -jar batik-rasterizer.jar </code><em>[options] [@files]</em></p> <p>Where the options are:</p> <ul> - <li><code>-d</code> <em><dir|file></em>. Output directory. If there is a single input file, this can be a file.</li> + <li><code>-d</code><em> <dir|file></em>. Output directory. If there is a single input file, this can be a file.</li> <li><code>-m</code><em> <mimeType></em>. Output mime type, one of image/png, image/jpeg, application/pdf, image/tiff.</li> <li><code>-w</code><em> <width></em>. Output width. This is a floating point value.</li> <li><code>-h</code><em> <height></em>. Output height. This is a floating point value.</li> + <li><code>-maxw</code><em> <width></em>. Maximum output width. This is a floating point value.</li> + <li><code>-maxh</code><em> <height></em>. Maximum output height. This is a floating point value.</li> <li><code>-a</code><em> <area></em>. Output area. The format for <area> is x,y,w,h, where x, y, w and h are floating point values.</li> <li><code>-bg</code><em> <color></em>. Uuput color. The format for <color> is a.r.g.b, where a, r, g and b @@ -111,7 +113,7 @@ <li><code>java -jar batik-rasterizer.jar -d myDir -m image/jpeg samples/*.svg</code> will generate JPEG images for all the SVG files found in the samples directory.</li> </ul> - <p><strong>NOTE:</strong>to run MIME type <code>application/pdf</code> need to have (see <link href="http://xml.apache.org/fop/index.html">FOP</link>) installed.</p> + <p><strong>NOTE:</strong> to run MIME type <code>application/pdf</code> need to have (see <link href="http://xml.apache.org/fop/index.html">FOP</link>) installed.</p> </s2> <s2 title="Using the source distribution"> @@ -162,12 +164,18 @@ <anchor id="initTask" /> <s2 title="Taking rasterizer task in use"> - <p>The first thing to do before you can use the task in your Ant - projects is to set <code>batik-rasterizer.jar</code> and - classes of the rasterizer task to your + + <p>The first thing to do is to compile rasterizer task classes. + Download <link href="install.html#distributions">source distribution</link> + of Batik and see <code>README</code> file in <code>xml-batik\contrib\rasterizertask</code> + directory for more instructions. The build procedure works the same way as when building + Batik itself.</p> + + <p> After building set <code>batik-rasterizer.jar</code> and + classes (or JAR) of the rasterizer task to your <em>CLASSPATH</em>.</p> - <p>After that you have to define the task in your Ant + <p>Next you have to define the task in your Ant project. To do this, add the following line either after the <code>project</code> start tag or after the <code>target</code> start tag in the target you are using the rasterizer task:</p> @@ -222,6 +230,22 @@ <td>No</td> </tr> <tr> + <td>maxheight</td> + <td>Sets the maximum height of the result image in pixels. + The image won't be higher than defined in this parameter, + regardless of the size set in the image itself or in other parameters. + This is a floating point value.</td> + <td>No</td> + </tr> + <tr> + <td>maxwidth</td> + <td>Sets the maximum width of the result image in pixels. + The image won't be wider than defined in this parameter, + regardless of the size set in the image itself or in other parameters. + This is a floating point value.</td> + <td>No</td> + </tr> + <tr> <td>quality</td> <td>Sets the quality of the produced image. The value have to be greater than 0 but smaller than 1. A bigger @@ -297,11 +321,6 @@ <code>fileset</code> element(s).</td> </tr> <tr> - <td></td> - <td></td> - </tr> - - <tr> <td>dest</td> <td>Name of a one output file. Use this with <code>src</code> parameter only. Output directory is @@ -334,6 +353,15 @@ created if they don't exist.</td> <td>Required if <code>srcdir</code> attribute or <code>fileset</code> elements are used.</td> + </tr> + <tr> + <td>classname</td> + <td>Classname of the XML parser used to parse SVG images. + The value can be either complete classname with package + information included or <code>jaxp</code>, + which means any available parser in the <code>CLASSPATH</code> + that supports JAXP. See the Batik code for the default value.</td> + <td>No</td> </tr> </table> <p>You can use <code>fileset</code> elements to select input
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]