Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static java.awt.RenderingHints.VALUE_STROKE_PURE;
import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_ON;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints.Key;
import java.awt.image.BufferedImage;
Expand All @@ -41,6 +42,7 @@
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.internal.image.SVGRasterizer;

import com.github.weisj.jsvg.SVGDocument;
Expand Down Expand Up @@ -82,21 +84,31 @@ public class JSVGRasterizer implements SVGRasterizer {

@Override
public ImageData rasterizeSVG(InputStream inputStream, int zoom) {
return rasterizeSVG(inputStream, zoom, null);
}

@Override
public ImageData rasterizeSVG(InputStream inputStream, int width, int height) {
return rasterizeSVG(inputStream, width, height, null);
}

@Override
public ImageData rasterizeSVG(InputStream inputStream, int zoom, RGB foregroundColor) {
if (zoom < 0) {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
SVGDocument svgDocument = loadAndValidateSVG(inputStream);
BufferedImage rasterizedImage = renderSVG(svgDocument, zoom);
BufferedImage rasterizedImage = renderSVG(svgDocument, zoom, foregroundColor);
return convertToSWTImageData(rasterizedImage);
}

@Override
public ImageData rasterizeSVG(InputStream inputStream, int width, int height) {
public ImageData rasterizeSVG(InputStream inputStream, int width, int height, RGB foregroundColor) {
SVGDocument svgDocument = loadAndValidateSVG(inputStream);
BufferedImage rasterizedImage = renderSVG(svgDocument, width, height);
BufferedImage rasterizedImage = renderSVG(svgDocument, width, height, foregroundColor);
return convertToSWTImageData(rasterizedImage);
}

private SVGDocument loadAndValidateSVG(InputStream inputStream) {
SVGDocument svgDocument = SVG_LOADER.load(inputStream, null, LoaderContext.createDefault());
if (svgDocument == null) {
Expand All @@ -105,22 +117,22 @@ private SVGDocument loadAndValidateSVG(InputStream inputStream) {
return svgDocument;
}

private BufferedImage renderSVG(SVGDocument svgDocument, int zoom) {
private BufferedImage renderSVG(SVGDocument svgDocument, int zoom, RGB foregroundColor) {
FloatSize sourceImageSize = svgDocument.size();
float scalingFactor = zoom / 100.0f;
int targetImageWidth = calculateTargetWidth(scalingFactor, sourceImageSize);
int targetImageHeight = calculateTargetHeight(scalingFactor, sourceImageSize);
return renderSVG(svgDocument, targetImageWidth, targetImageHeight);
return renderSVG(svgDocument, targetImageWidth, targetImageHeight, foregroundColor);
}
private BufferedImage renderSVG(SVGDocument svgDocument, int width, int height) {

private BufferedImage renderSVG(SVGDocument svgDocument, int width, int height, RGB foregroundColor) {
if (width <= 0 || height <= 0) {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
float widthScalingFactor = width / svgDocument.size().width;
float heightScalingFactor = height / svgDocument.size().height;
Graphics2D g = configureRenderingOptions(widthScalingFactor, heightScalingFactor, image);
Graphics2D g = configureRenderingOptions(widthScalingFactor, heightScalingFactor, image, foregroundColor);
svgDocument.render(null, g);
g.dispose();
return image;
Expand All @@ -137,8 +149,12 @@ private int calculateTargetHeight(float scalingFactor, FloatSize sourceImageSize
}

private Graphics2D configureRenderingOptions(float widthScalingFactor, float heightScalingFactor,
BufferedImage image) {
BufferedImage image, RGB foregroundColor) {
Graphics2D g = image.createGraphics();
Color fgColor = foregroundColor != null
? new Color(foregroundColor.red, foregroundColor.green, foregroundColor.blue)
: Color.BLACK;
g.setColor(fgColor);
g.setRenderingHints(RENDERING_HINTS);
g.scale(widthScalingFactor, heightScalingFactor);
return g;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.DPIUtil.*;
import org.eclipse.swt.widgets.Display;

/**
* A {@link FileFormat} implementation for handling SVG (Scalable Vector
Expand Down Expand Up @@ -61,7 +62,7 @@ List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom)
if (targetZoom <= 0) {
SWT.error(SWT.ERROR_INVALID_ARGUMENT, null, " [Cannot rasterize SVG for zoom <= 0]");
}
ImageData rasterizedImageData = RASTERIZER.rasterizeSVG(inputStream, 100 * targetZoom / fileZoom);
ImageData rasterizedImageData = RASTERIZER.rasterizeSVG(inputStream, 100 * targetZoom / fileZoom, getWidgetForegroundColor());
return List.of(new ElementAtZoom<>(rasterizedImageData, targetZoom));
}

Expand All @@ -73,8 +74,15 @@ ImageData loadFromByteStreamBySize(int width, int height) {
if (width <= 0 || height <= 0) {
SWT.error(SWT.ERROR_INVALID_ARGUMENT, null, " [Cannot rasterize SVG for width or height <= 0]");
}
ImageData rasterizedImageData = RASTERIZER.rasterizeSVG(inputStream, width, height);
return rasterizedImageData;
return RASTERIZER.rasterizeSVG(inputStream, width, height, getWidgetForegroundColor());
}

private static RGB getWidgetForegroundColor() {
Display display = Display.getDefault();
if (display != null) {
return display.getSystemColor(SWT.COLOR_WIDGET_FOREGROUND).getRGB();
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,46 @@ public interface SVGRasterizer {
* </ul>
*/
public ImageData rasterizeSVG(InputStream stream, int width, int height);

/**
* Rasterizes an SVG image from the provided {@code InputStream} using the
* specified zoom and foreground color.
*
* @param stream the SVG image as an {@link InputStream}.
* @param zoom the scaling percentage (e.g., 100 = original size, 200 = double size).
* This value must be greater zero.
* @param foregroundColor the default foreground color, or {@code null} for black.
* @return the {@link ImageData} for the rasterized image.
*
* @exception SWTException
* <ul>
* <li>ERROR_INVALID_IMAGE - if the SVG cannot be loaded</li>
* </ul>
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_INVALID_ARGUMENT - if the zoom is less than zero</li>
* </ul>
*/
public ImageData rasterizeSVG(InputStream stream, int zoom, RGB foregroundColor);

/**
* Rasterizes an SVG image from the provided {@code InputStream} into a raster
* image of the specified width and height, using the given foreground color.
*
* @param stream the SVG image as an {@link InputStream}.
* @param width the width of the rasterized image in pixels (must be positive).
* @param height the height of the rasterized image in pixels (must be positive).
* @param foregroundColor the default foreground color, or {@code null} for black.
* @return the {@link ImageData} for the rasterized image.
*
* @exception SWTException
* <ul>
* <li>ERROR_INVALID_IMAGE - if the SVG cannot be loaded</li>
* </ul>
* @exception IllegalArgumentException
* <ul>
* <li>ERROR_INVALID_ARGUMENT - if the width or height is less than zero</li>
* </ul>
*/
public ImageData rasterizeSVG(InputStream stream, int width, int height, RGB foregroundColor);
}
Loading