org.apache.batik.apps.rasterizer

Class SVGConverter

public class SVGConverter extends Object

This application can be used to convert SVG images to raster images.
Possible result raster image formats are PNG, JPEG, TIFF, and PDF. The Batik Transcoder API is used to execute the conversion. FOP is needed to be able to transcode to the PDF format
The source has to be list of files or URL (set by the setSources method).
The destination can be:

There are a number of options which control the way the image is converted to the destination format:
Nested Class Summary
static classSVGConverter.SVGFileFilter
Convenience class to filter svg files
Field Summary
protected StringallowedScriptTypes
Set of allowed script types.
protected StringalternateStylesheet
Alternate stylesheet for which should be applied to the SVG
protected Rectangle2Darea
Output AOI area.
protected ColorbackgroundColor
Background color for the output images.
protected booleanconstrainScriptOrigin
Controls whether scripts can only have the same origin as the document which references them.
protected SVGConverterControllercontroller
Controls some aspects of the converter's operation, such as whether or not it should proceed in some error situations.
protected StringdefaultFontFamily
Default value for the font-family when it is unspecified
protected DestinationTypedestinationType
Result type
protected Filedst
Destination image path.
protected static floatDEFAULT_HEIGHT
Default height
protected static floatDEFAULT_QUALITY
Default quality value.
protected static DestinationTypeDEFAULT_RESULT_TYPE
Default result type
protected static floatDEFAULT_WIDTH
Default width
protected booleanexecuteOnload
Execute the 'onload' scripts flag
static StringERROR_CANNOT_ACCESS_TRANSCODER
static StringERROR_CANNOT_COMPUTE_DESTINATION
static StringERROR_CANNOT_OPEN_OUTPUT_FILE
static StringERROR_CANNOT_OPEN_SOURCE
static StringERROR_CANNOT_READ_SOURCE
static StringERROR_CANNOT_USE_DST_FILE
static StringERROR_NO_SOURCES_SPECIFIED
static StringERROR_OUTPUT_NOT_WRITEABLE
static StringERROR_SOURCE_SAME_AS_DESTINATION
static StringERROR_UNABLE_TO_CREATE_OUTPUT_DIR
static StringERROR_WHILE_RASTERIZING_FILE
protected Listfiles
Contents of fileset elements.
protected floatheight
Output image height.
protected intindexed
Should output Image be indexed .
protected Stringlanguage
Language
protected floatmaxHeight
Maximum output image height.
protected floatmaxWidth
Maximum output image width.
protected StringmediaType
Media type for which the SVG image should be rendered
protected static floatMAXIMUM_QUALITY
Maximum quality value
protected floatpixelUnitToMillimeter
Millimeters Per Pixel
protected floatquality
Output image quality.
protected booleansecurityOff
Controls whether scripts should be run securely or not
protected floatsnapshotTime
Document time to seek to.
protected Listsources
Sources files or URLs
protected static StringSVG_EXTENSION
SVG file extension
protected StringuserStylesheet
User stylesheet
protected booleanvalidate
Validation flag
protected floatwidth
Output image width.
Constructor Summary
SVGConverter()
SVGConverter(SVGConverterController controller)
Method Summary
protected ListcomputeDstFiles(List sources)
Populates a vector with destination files names computed from the names of the files in the sources vector and the value of the dst property
protected ListcomputeSources()
Populates a vector with the set of SVG files from the srcDir if it is not null and with the sources (files or URLs) if any.
protected MapcomputeTranscodingHints()
Computes the set of transcoding hints to use for the operation
protected voidcreateOutputDir(File output)
Creates directories for output files if needed.
voidexecute()
Starts the conversion process.
StringgetAllowedScriptTypes()
Returns the list of allowed script types.
StringgetAlternateStylesheet()
Rectangle2DgetArea()
ColorgetBackgroundColor()
booleangetConstrainScriptOrigin()
Returns whether scripts can only be loaded from the same origin as the documents referencing them.
StringgetDefaultFontFamily()
protected StringgetDestinationFile(String file)
Get the name of the result image file.
DestinationTypegetDestinationType()
FilegetDst()
booleangetExecuteOnload()
Returns true if the scripts attached to the DOM using 'onload' event attribute is going to be executed before rasterizing, false otherwise.
String[]getFileNRef(String fileName)
floatgetHeight()
intgetIndexed()
StringgetLanguage()
floatgetMaxHeight()
floatgetMaxWidth()
StringgetMediaType()
floatgetPixelUnitToMillimeter()
floatgetQuality()
booleangetSecurityOff()
Returns whether or not scripts will be run securely
floatgetSnapshotTime()
Returns the document to to seek to before rasterizing.
ListgetSources()
StringgetUserStylesheet()
booleangetValidate()
floatgetWidth()
protected booleanisFile(File f)
Returns true if f is a File.
protected booleanisWriteable(File file)
Checks if the application is allowed to write to the file.
voidsetAllowedScriptTypes(String allowedScriptTypes)
Sets the set of allowed script types (i.e., the set of possible values for the type attribute in the <script> element), as a comma separated list of allowed values.
voidsetAlternateStylesheet(String alternateStylesheet)
Sets the alternateStyleSheet value.
voidsetArea(Rectangle2D area)
Sets the area as a Rectangle.
voidsetBackgroundColor(Color backgroundColor)
Sets the backgroundColor value.
voidsetConstrainScriptOrigin(boolean constrainScriptOrigin)
Sets whether scripts should only be loaded from the same location as the documents referencing them.
voidsetDefaultFontFamily(String defaultFontFamily)
Sets the defaultFontFamily value.
voidsetDestinationType(DestinationType destinationType)
Sets the destinationType attribute value.
voidsetDst(File dst)
When converting a single source, dst can be a file.
voidsetExecuteOnload(boolean b)
Sets whether or not scripts attached to the DOM using 'onload' event attribute must be executed before rasterizing.
voidsetHeight(float height)
In less than or equal to zero, the height is not constrained on the output image.
voidsetIndexed(int bits)
Tells the PNG encoder to reduce the image to 256 colors, so the PNG file is indexed.
voidsetLanguage(String language)
Sets the user language.
voidsetMaxHeight(float height)
If less than or equal to zero, the maximum height does not have any effect on the output image.
voidsetMaxWidth(float width)
If less than or equal to zero, the maximum width does not have any effect on the output image.
voidsetMediaType(String mediaType)
Sets the mediaType value.
voidsetPixelUnitToMillimeter(float pixelUnitToMillimeter)
Sets the millimeters per pixel constant.
voidsetQuality(float quality)
Sets the JPEG encoding quality.
voidsetSecurityOff(boolean securityOff)
Sets whether or not scripts should be run securely
voidsetSnapshotTime(float t)
Sets the document time to seek to before rasterizing.
voidsetSources(String[] sources)
Sets the list of individual SVG sources.
voidsetUserStylesheet(String userStylesheet)
Sets the user stylesheet.
voidsetValidate(boolean validate)
Defines whether or not input sources should be validated in the conversion process
voidsetWidth(float width)
In less than or equal to zero, the width is not constrained on the output image.
protected voidtranscode(SVGConverterSource inputFile, File outputFile, Transcoder transcoder)
Converts the input image to the result image. with the given transcoder.

Field Detail

allowedScriptTypes

protected String allowedScriptTypes
Set of allowed script types.

alternateStylesheet

protected String alternateStylesheet
Alternate stylesheet for which should be applied to the SVG

area

protected Rectangle2D area
Output AOI area.

backgroundColor

protected Color backgroundColor
Background color for the output images.

constrainScriptOrigin

protected boolean constrainScriptOrigin
Controls whether scripts can only have the same origin as the document which references them.

controller

protected SVGConverterController controller
Controls some aspects of the converter's operation, such as whether or not it should proceed in some error situations. See SVGConverterController

defaultFontFamily

protected String defaultFontFamily
Default value for the font-family when it is unspecified

destinationType

protected DestinationType destinationType
Result type

dst

protected File dst
Destination image path. Can be a file (for single source) or a directory

DEFAULT_HEIGHT

protected static final float DEFAULT_HEIGHT
Default height

DEFAULT_QUALITY

protected static final float DEFAULT_QUALITY
Default quality value. A value of -1 means disabled.

DEFAULT_RESULT_TYPE

protected static final DestinationType DEFAULT_RESULT_TYPE
Default result type

DEFAULT_WIDTH

protected static final float DEFAULT_WIDTH
Default width

executeOnload

protected boolean executeOnload
Execute the 'onload' scripts flag

ERROR_CANNOT_ACCESS_TRANSCODER

public static final String ERROR_CANNOT_ACCESS_TRANSCODER

ERROR_CANNOT_COMPUTE_DESTINATION

public static final String ERROR_CANNOT_COMPUTE_DESTINATION

ERROR_CANNOT_OPEN_OUTPUT_FILE

public static final String ERROR_CANNOT_OPEN_OUTPUT_FILE

ERROR_CANNOT_OPEN_SOURCE

public static final String ERROR_CANNOT_OPEN_SOURCE

ERROR_CANNOT_READ_SOURCE

public static final String ERROR_CANNOT_READ_SOURCE

ERROR_CANNOT_USE_DST_FILE

public static final String ERROR_CANNOT_USE_DST_FILE

ERROR_NO_SOURCES_SPECIFIED

public static final String ERROR_NO_SOURCES_SPECIFIED

ERROR_OUTPUT_NOT_WRITEABLE

public static final String ERROR_OUTPUT_NOT_WRITEABLE

ERROR_SOURCE_SAME_AS_DESTINATION

public static final String ERROR_SOURCE_SAME_AS_DESTINATION

ERROR_UNABLE_TO_CREATE_OUTPUT_DIR

public static final String ERROR_UNABLE_TO_CREATE_OUTPUT_DIR

ERROR_WHILE_RASTERIZING_FILE

public static final String ERROR_WHILE_RASTERIZING_FILE

files

protected List files
Contents of fileset elements.

height

protected float height
Output image height.

indexed

protected int indexed
Should output Image be indexed .

language

protected String language
Language

maxHeight

protected float maxHeight
Maximum output image height.

maxWidth

protected float maxWidth
Maximum output image width.

mediaType

protected String mediaType
Media type for which the SVG image should be rendered

MAXIMUM_QUALITY

protected static final float MAXIMUM_QUALITY
Maximum quality value

pixelUnitToMillimeter

protected float pixelUnitToMillimeter
Millimeters Per Pixel

quality

protected float quality
Output image quality.

securityOff

protected boolean securityOff
Controls whether scripts should be run securely or not

snapshotTime

protected float snapshotTime
Document time to seek to.

sources

protected List sources
Sources files or URLs

SVG_EXTENSION

protected static final String SVG_EXTENSION
SVG file extension

userStylesheet

protected String userStylesheet
User stylesheet

validate

protected boolean validate
Validation flag

width

protected float width
Output image width.

Constructor Detail

SVGConverter

public SVGConverter()

SVGConverter

public SVGConverter(SVGConverterController controller)

Method Detail

computeDstFiles

protected List computeDstFiles(List sources)
Populates a vector with destination files names computed from the names of the files in the sources vector and the value of the dst property

computeSources

protected List computeSources()
Populates a vector with the set of SVG files from the srcDir if it is not null and with the sources (files or URLs) if any.

computeTranscodingHints

protected Map computeTranscodingHints()
Computes the set of transcoding hints to use for the operation

createOutputDir

protected void createOutputDir(File output)
Creates directories for output files if needed.

Parameters: output Output file with path.

Throws: SVGConverterException Output directory doesn't exist and it can't be created.

execute

public void execute()
Starts the conversion process.

Throws: SVGConverterException thrown if parameters are not set correctly.

getAllowedScriptTypes

public String getAllowedScriptTypes()
Returns the list of allowed script types.

See Also: SVGConverter

getAlternateStylesheet

public String getAlternateStylesheet()

getArea

public Rectangle2D getArea()

getBackgroundColor

public Color getBackgroundColor()

getConstrainScriptOrigin

public boolean getConstrainScriptOrigin()
Returns whether scripts can only be loaded from the same origin as the documents referencing them.

getDefaultFontFamily

public String getDefaultFontFamily()

getDestinationFile

protected String getDestinationFile(String file)
Get the name of the result image file.

This method modifies the result filename, it changes the existing suffix to correspong the result file type. It also adds the suffix if the file doesn't have one.

Parameters: file Result file name as a String object.

Returns: Name of the file. The directory of the file is not returned. The returned string is empty if the parameter is not a file.

getDestinationType

public DestinationType getDestinationType()

getDst

public File getDst()

getExecuteOnload

public boolean getExecuteOnload()
Returns true if the scripts attached to the DOM using 'onload' event attribute is going to be executed before rasterizing, false otherwise.

getFileNRef

public String[] getFileNRef(String fileName)

getHeight

public float getHeight()

getIndexed

public int getIndexed()

getLanguage

public String getLanguage()

getMaxHeight

public float getMaxHeight()

getMaxWidth

public float getMaxWidth()

getMediaType

public String getMediaType()

getPixelUnitToMillimeter

public float getPixelUnitToMillimeter()

getQuality

public float getQuality()

getSecurityOff

public boolean getSecurityOff()
Returns whether or not scripts will be run securely

getSnapshotTime

public float getSnapshotTime()
Returns the document to to seek to before rasterizing.

getSources

public List getSources()

getUserStylesheet

public String getUserStylesheet()

getValidate

public boolean getValidate()

getWidth

public float getWidth()

isFile

protected boolean isFile(File f)
Returns true if f is a File. f is found to be a file if it exists and is a file. If it does not exist, it is declared to be a file if it has the same extension as the DestinationType.

isWriteable

protected boolean isWriteable(File file)
Checks if the application is allowed to write to the file.

Parameters: file File to be checked.

Returns: true if the file is writeable and false otherwise.

setAllowedScriptTypes

public void setAllowedScriptTypes(String allowedScriptTypes)
Sets the set of allowed script types (i.e., the set of possible values for the type attribute in the <script> element), as a comma separated list of allowed values.

setAlternateStylesheet

public void setAlternateStylesheet(String alternateStylesheet)
Sets the alternateStyleSheet value. This value controls the CSS alternate stylesheet to select in the rendered SVG file(s). It may be null, in which case no alternate stylesheet will be selected.

setArea

public void setArea(Rectangle2D area)
Sets the area as a Rectangle. This value can be null in which case the whole image will be rendered. If the area is not null, then only the portion of the image it defines will be rendered.

setBackgroundColor

public void setBackgroundColor(Color backgroundColor)
Sets the backgroundColor value. This can be null in which case no color will be used to fill the background before rendering this SVG image.

setConstrainScriptOrigin

public void setConstrainScriptOrigin(boolean constrainScriptOrigin)
Sets whether scripts should only be loaded from the same location as the documents referencing them.

setDefaultFontFamily

public void setDefaultFontFamily(String defaultFontFamily)
Sets the defaultFontFamily value. This value controls the default value for the font-family CSS property when that property is unspecified.

setDestinationType

public void setDestinationType(DestinationType destinationType)
Sets the destinationType attribute value. Should not be null.

setDst

public void setDst(File dst)
When converting a single source, dst can be a file. Othewise, it should be a directory.

setExecuteOnload

public void setExecuteOnload(boolean b)
Sets whether or not scripts attached to the DOM using 'onload' event attribute must be executed before rasterizing.

Parameters: b true means scripts will be executed

setHeight

public void setHeight(float height)
In less than or equal to zero, the height is not constrained on the output image. The height is in user space.

setIndexed

public void setIndexed(int bits)
Tells the PNG encoder to reduce the image to 256 colors, so the PNG file is indexed.

setLanguage

public void setLanguage(String language)
Sets the user language. If the value is null, then the default (see UserAgent) is used.

setMaxHeight

public void setMaxHeight(float height)
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.

setMaxWidth

public void setMaxWidth(float width)
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.

setMediaType

public void setMediaType(String mediaType)
Sets the mediaType value. This value controls the CSS media for which the image should be rendered. It can be null, in which case no specific media selectors will apply. If it is not null, it can contain space separated values of the medias for which the image should be rendered. For example, "screen", "print" or "scree projection" are valid values.

setPixelUnitToMillimeter

public void setPixelUnitToMillimeter(float pixelUnitToMillimeter)
Sets the millimeters per pixel constant. A negative value will cause the default value (see UserAgent) to be used.

setQuality

public void setQuality(float quality)
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.

setSecurityOff

public void setSecurityOff(boolean securityOff)
Sets whether or not scripts should be run securely

setSnapshotTime

public void setSnapshotTime(float t)
Sets the document time to seek to before rasterizing.

Parameters: t the document time, in seconds

setSources

public void setSources(String[] sources)
Sets the list of individual SVG sources. The strings can be either URLs or file names. Note that invalid sources (e.g., read-protected files or invalid URLs) will cause SVGConverterExceptions to be thrown during the transcoding process (see SVGConverter);

setUserStylesheet

public void setUserStylesheet(String userStylesheet)
Sets the user stylesheet. May be null.

setValidate

public void setValidate(boolean validate)
Defines whether or not input sources should be validated in the conversion process

setWidth

public void setWidth(float width)
In less than or equal to zero, the width is not constrained on the output image. The width is in user space.

transcode

protected void transcode(SVGConverterSource inputFile, File outputFile, Transcoder transcoder)
Converts the input image to the result image. with the given transcoder. If a failure happens, the controller is notified and decides whether to proceed or not. If it decides to proceed, the converter will continue processing other files. Otherwise, it will throw an exception.
Copyright B) 2008 Apache Software Foundation. All Rights Reserved.