SVG Rasterizing API

Last modified by Vincent Massol on 2023/06/26 00:00

vectorAPIs to rasterize SVG images
TypeJAR
CategoryAPI
Developed by

Vincent Massol, XWiki Development Team

Active Installs0
Rating
0 Votes
LicenseGNU Lesser General Public License 2.1
Compatibility

In XWiki 15.6, the SVG Rasterizing API has been removed from XWiki Standard and moved as a Contrib extension (released as version 15.6). Also, if you were using the Java API, you'll need to change the packages from org.xwiki.platform.svg to org.xwiki.contrib.svg.

Installable with the Extension Manager

Description

Should be used as a replacement of the older SVG Plugin (which had no documentation).

Java API

public interface SVGRasterizer
{
   /**
     * Rasterize an image as PNG into a temporary file.
     *
     * @param content the SVG image
     * @param width the desired width of the raster image, in pixels; if 0 or a negative number, the image's native size
     *            is used
     * @param height the desired height of the raster image, in pixels; if 0 or a negative number, the image's native
     *            size is used
     * @return the file where the PNG is stored
     * @throws IOException if temporary files can't be accessed
     */

    File rasterizeToTemporaryFile(String content, int width, int height) throws IOException;

   /**
     * Rasterize an image as PNG as a temporary resource belonging to the current document, which can be accessed with
     * the /temp/ action.
     *
     * @param content the SVG image
     * @param width the desired width of the raster image, in pixels; if 0 or a negative number, the image's native size
     *            is used
     * @param height the desired height of the raster image, in pixels; if 0 or a negative number, the image's native
     *            size is used
     * @return the temporary resource where the PNG is stored
     * @throws IOException if temporary files can't be accessed
     */

    TemporaryResourceReference rasterizeToTemporaryResource(String content, int width, int height) throws IOException;

   /**
     * Rasterize an image as PNG as a temporary resource belonging to the specified document, which can be accessed with
     * the /temp/ action.
     *
     * @param content the SVG image
     * @param width the desired width of the raster image, in pixels; if 0 or a negative number, the image's native size
     *            is used
     * @param height the desired height of the raster image, in pixels; if 0 or a negative number, the image's native
     *            size is used
     * @param targetContext the document which will "own" the new temporary resource
     * @return the temporary resource where the PNG is stored
     * @throws IOException if temporary files can't be accessed
     */

    TemporaryResourceReference rasterizeToTemporaryResource(String content, int width, int height,
        DocumentReference targetContext) throws IOException;

   /**
     * Rasterize an image as PNG into the current response.
     *
     * @param content the SVG image
     * @param width the desired width of the raster image, in pixels; if 0 or a negative number, the image's native size
     *            is used
     * @param height the desired height of the raster image, in pixels; if 0 or a negative number, the image's native
     *            size is used
     * @throws IOException if writing the response fails
     */

   void rasterizeToResponse(String content, int width, int height) throws IOException;
}

Scripting API

    /**
     * Rasterize an image as PNG as a temporary resource belonging to the current document, which can be accessed with
     * the /temp/ action.
     *
     * @param content the SVG image
     * @return URL pointing to the temporary resource where the PNG is stored
     */

   public ExtendedURL rasterizeToTemporaryResource(String content)

   /**
     * Rasterize an image as PNG as a temporary resource belonging to the current document, which can be accessed with
     * the /temp/ action.
     *
     * @param content the SVG image
     * @param width the desired width of the raster image, in pixels; if 0 or a negative number, the image's native size
     *            is used
     * @param height the desired height of the raster image, in pixels; if 0 or a negative number, the image's native
     *            size is used
     * @return URL pointing to the temporary resource where the PNG is stored
     */

   public ExtendedURL rasterizeToTemporaryResource(String content, int width, int height)

   /**
     * Rasterize an image as PNG as a temporary resource belonging to the current document, which can be accessed with
     * the /temp/ action.
     *
     * @param content the SVG image
     * @param targetContext the document which will "own" the new temporary resource
     * @return URL pointing to the temporary resource where the PNG is stored
     */

   public ExtendedURL rasterizeToTemporaryResource(String content, DocumentReference targetContext)

   /**
     * Rasterize an image as PNG as a temporary resource belonging to the current document, which can be accessed with
     * the /temp/ action.
     *
     * @param content the SVG image
     * @param width the desired width of the raster image, in pixels; if 0 or a negative number, the image's native size
     *            is used
     * @param height the desired height of the raster image, in pixels; if 0 or a negative number, the image's native
     *            size is used
     * @param targetContext the document which will "own" the new temporary resource
     * @return URL pointing to the temporary resource where the PNG is stored
     */

   public ExtendedURL rasterizeToTemporaryResource(String content, int width, int height,
        DocumentReference targetContext)

   /**
     * Rasterize an image as PNG into the current response.
     *
     * @param content the SVG image
     * @return {@code true} if the image was successfully rasterized and written to the response, {@code false} in case
     *         of exceptions
     */

   public boolean rasterizeToResponse(String content)

   /**
     * Rasterize an image as PNG into the current response.
     *
     * @param content the SVG image
     * @param width the desired width of the raster image, in pixels; if 0 or a negative number, the image's native size
     *            is used
     * @param height the desired height of the raster image, in pixels; if 0 or a negative number, the image's native
     *            size is used
     * @return {@code true} if the image was successfully rasterized and written to the response, {@code false} in case
     *         of exceptions
     */

   public boolean rasterizeToResponse(String content, int width, int height)
}

For example from Velocity:

$services.svg.rasterizeToResponse("... svg content...")

Prerequisites & Installation Instructions

We recommend using the Extension Manager to install this extension (Make sure that the text "Installable with the Extension Manager" is displayed at the top right location on this page to know if this extension can be installed with the Extension Manager).

You can also use the manual method which involves dropping the JAR file and all its dependencies into the WEB-INF/lib folder and restarting XWiki.

Dependencies

Dependencies for this extension (org.xwiki.contrib:svg-rasterizer 15.6):

Tags:
    

Get Connected