Skip to content
Merged
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 @@ -74,34 +74,47 @@ public ImageData rasterizeSVG(InputStream inputStream, int zoom) {
if (zoom < 0) {
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
}
SVGDocument svgDocument = loadSVG(inputStream);
if (svgDocument == null) {
SWT.error(SWT.ERROR_INVALID_IMAGE);
}
SVGDocument svgDocument = loadAndValidateSVG(inputStream);
BufferedImage rasterizedImage = renderSVG(svgDocument, zoom);
return convertToSWTImageData(rasterizedImage);
}

private SVGDocument loadSVG(InputStream inputStream) {
return SVG_LOADER.load(inputStream, null, LoaderContext.createDefault());
@Override
public ImageData rasterizeSVG(InputStream inputStream, int width, int height) {
SVGDocument svgDocument = loadAndValidateSVG(inputStream);
BufferedImage rasterizedImage = renderSVG(svgDocument, width, height);
return convertToSWTImageData(rasterizedImage);
}

private SVGDocument loadAndValidateSVG(InputStream inputStream) {
SVGDocument svgDocument = SVG_LOADER.load(inputStream, null, LoaderContext.createDefault());
if (svgDocument == null) {
SWT.error(SWT.ERROR_INVALID_IMAGE);
}
return svgDocument;
}

private BufferedImage renderSVG(SVGDocument svgDocument, int zoom) {
FloatSize sourceImageSize = svgDocument.size();
float scalingFactor = zoom / 100.0f;
BufferedImage image = createImageBase(svgDocument, scalingFactor);
Graphics2D g = configureRenderingOptions(scalingFactor, image);
int targetImageWidth = calculateTargetWidth(scalingFactor, sourceImageSize);
int targetImageHeight = calculateTargetHeight(scalingFactor, sourceImageSize);
return renderSVG(svgDocument, targetImageWidth, targetImageHeight);
}

private BufferedImage renderSVG(SVGDocument svgDocument, int width, int height) {
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);
svgDocument.render(null, g);
g.dispose();
return image;
}

private BufferedImage createImageBase(SVGDocument svgDocument, float scalingFactor) {
FloatSize sourceImageSize = svgDocument.size();
int targetImageWidth = calculateTargetWidth(scalingFactor, sourceImageSize);
int targetImageHeight = calculateTargetHeight(scalingFactor, sourceImageSize);
return new BufferedImage(targetImageWidth, targetImageHeight, BufferedImage.TYPE_INT_ARGB);
}

private int calculateTargetWidth(float scalingFactor, FloatSize sourceImageSize) {
double sourceImageWidth = sourceImageSize.getWidth();
return (int) Math.round(sourceImageWidth * scalingFactor);
Expand All @@ -112,10 +125,11 @@ private int calculateTargetHeight(float scalingFactor, FloatSize sourceImageSize
return (int) Math.round(sourceImageHeight * scalingFactor);
}

private Graphics2D configureRenderingOptions(float scalingFactor, BufferedImage image) {
private Graphics2D configureRenderingOptions(float widthScalingFactor, float heightScalingFactor,
BufferedImage image) {
Graphics2D g = image.createGraphics();
g.setRenderingHints(RENDERING_HINTS);
g.scale(scalingFactor, scalingFactor);
g.scale(widthScalingFactor, heightScalingFactor);
return g;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,24 @@ public interface SVGRasterizer {
* </ul>
*/
public ImageData rasterizeSVG(InputStream stream, int zoom);

/**
* Rasterizes an SVG image from the provided {@code InputStream} into a raster
* image of the specified width and height.
*
* @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).
* @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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,33 @@ void testRasterizeWithZoomWithInvalidSVG() {
assertEquals(SWT.ERROR_INVALID_IMAGE, exception.code);
}

@Test
void testRasterizeWithTargetSize() {
ImageData data = rasterizer.rasterizeSVG(svgStream(svgString), 300, 150);
assertEquals(300, data.width);
assertEquals(150, data.height);
}

@Test
void testRasterizeWithTargetSizeHavingInvalidHeight() {
assertThrows(IllegalArgumentException.class, () -> {
rasterizer.rasterizeSVG(svgStream(svgString), -1, 150);
});
}

@Test
void testRasterizeWithTargetSizeHavingInvalidWidth() {
assertThrows(IllegalArgumentException.class, () -> {
rasterizer.rasterizeSVG(svgStream(svgString), 150, -1);
});
}

@Test
void testRasterizeWithTargetSizeWithInvalidSVG() {
SWTException exception = assertThrows(SWTException.class, () -> {
rasterizer.rasterizeSVG(invalidSvg, 150, 150);
});
assertEquals(SWT.ERROR_INVALID_IMAGE, exception.code);
}

}
Loading