Skip to content

Commit c04bcdf

Browse files
committed
Add internal API in SVGrasterizer to rasterize images at given height and width
Adding internal API to SVGRasterizer and simple tests to see if the svgs are loaded at right sizes by the rasterizer
1 parent c6a29a9 commit c04bcdf

File tree

3 files changed

+101
-20
lines changed

3 files changed

+101
-20
lines changed

bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,35 +71,48 @@ public class JSVGRasterizer implements SVGRasterizer {
7171
);
7272

7373
@Override
74-
public ImageData rasterizeSVG(InputStream inputStream, int zoom) throws IOException {
75-
SVGDocument svgDocument = loadSVG(inputStream);
76-
if (svgDocument == null) {
77-
SWT.error(SWT.ERROR_INVALID_IMAGE);
78-
}
74+
public ImageData rasterizeSVG(InputStream inputStream, int zoom){
75+
SVGDocument svgDocument = loadAndValidateSVG(inputStream);
7976
BufferedImage rasterizedImage = renderSVG(svgDocument, zoom);
8077
return convertToSWTImageData(rasterizedImage);
8178
}
82-
83-
private SVGDocument loadSVG(InputStream inputStream) {
84-
return SVG_LOADER.load(inputStream, null, LoaderContext.createDefault());
79+
80+
@Override
81+
public ImageData rasterizeSVG(InputStream inputStream, int width, int height){
82+
SVGDocument svgDocument = loadAndValidateSVG(inputStream);
83+
BufferedImage rasterizedImage = renderSVG(svgDocument, width, height);
84+
return convertToSWTImageData(rasterizedImage);
85+
}
86+
87+
private SVGDocument loadAndValidateSVG(InputStream inputStream) {
88+
SVGDocument svgDocument = SVG_LOADER.load(inputStream, null, LoaderContext.createDefault());
89+
if (svgDocument == null) {
90+
SWT.error(SWT.ERROR_INVALID_IMAGE);
91+
}
92+
return svgDocument;
8593
}
8694

8795
private BufferedImage renderSVG(SVGDocument svgDocument, int zoom) {
96+
FloatSize sourceImageSize = svgDocument.size();
8897
float scalingFactor = zoom / 100.0f;
89-
BufferedImage image = createImageBase(svgDocument, scalingFactor);
90-
Graphics2D g = configureRenderingOptions(scalingFactor, image);
98+
int targetImageWidth = calculateTargetWidth(scalingFactor, sourceImageSize);
99+
int targetImageHeight = calculateTargetHeight(scalingFactor, sourceImageSize);
100+
return renderSVG(svgDocument, targetImageWidth, targetImageHeight);
101+
}
102+
103+
private BufferedImage renderSVG(SVGDocument svgDocument, int width, int height) {
104+
if (width <= 0 || height <= 0) {
105+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
106+
}
107+
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
108+
float widthScalingFactor = width / svgDocument.size().width;
109+
float heightScalingFactor = height / svgDocument.size().height;
110+
Graphics2D g = configureRenderingOptions(widthScalingFactor, heightScalingFactor, image);
91111
svgDocument.render(null, g);
92112
g.dispose();
93113
return image;
94114
}
95115

96-
private BufferedImage createImageBase(SVGDocument svgDocument, float scalingFactor) {
97-
FloatSize sourceImageSize = svgDocument.size();
98-
int targetImageWidth = calculateTargetWidth(scalingFactor, sourceImageSize);
99-
int targetImageHeight = calculateTargetHeight(scalingFactor, sourceImageSize);
100-
return new BufferedImage(targetImageWidth, targetImageHeight, BufferedImage.TYPE_INT_ARGB);
101-
}
102-
103116
private int calculateTargetWidth(float scalingFactor, FloatSize sourceImageSize) {
104117
double sourceImageWidth = sourceImageSize.getWidth();
105118
return (int) Math.round(sourceImageWidth * scalingFactor);
@@ -110,10 +123,10 @@ private int calculateTargetHeight(float scalingFactor, FloatSize sourceImageSize
110123
return (int) Math.round(sourceImageHeight * scalingFactor);
111124
}
112125

113-
private Graphics2D configureRenderingOptions(float scalingFactor, BufferedImage image) {
126+
private Graphics2D configureRenderingOptions(float widthScalingFactor, float heightScalingFactor, BufferedImage image) {
114127
Graphics2D g = image.createGraphics();
115128
g.setRenderingHints(RENDERING_HINTS);
116-
g.scale(scalingFactor, scalingFactor);
129+
g.scale(widthScalingFactor, heightScalingFactor);
117130
return g;
118131
}
119132

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/SVGRasterizer.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.io.*;
1616

17+
import org.eclipse.swt.*;
1718
import org.eclipse.swt.graphics.*;
1819

1920
/**
@@ -31,5 +32,20 @@ public interface SVGRasterizer {
3132
* @return the {@link ImageData} for the rasterized image, or {@code null} if
3233
* the input is not a valid SVG file or cannot be processed.
3334
*/
34-
public ImageData rasterizeSVG(InputStream stream, int zoom) throws IOException;
35+
public ImageData rasterizeSVG(InputStream stream, int zoom);
36+
37+
/**
38+
* Rasterizes an SVG image from the provided {@code InputStream} into a
39+
* raster image of the specified width and height.
40+
*
41+
* If {@code width} or {@code height}
42+
* is zero or negative, this method throws an {@link SWT#ERROR_INVALID_ARGUMENT}.
43+
*
44+
* @param stream the SVG image as an {@link InputStream}.
45+
* @param width the width of the rasterized image in pixels (must be positive).
46+
* @param height the height of the rasterized image in pixels (must be positive).
47+
* @return the {@link ImageData} for the rasterized image, or {@code null} if
48+
* the input is not a valid SVG file or cannot be processed.
49+
*/
50+
public ImageData rasterizeSVG(InputStream stream, int width, int height);
3551
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024, 2025 Yatta Solutions and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Yatta Solutions - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.swt.tests.junit;
15+
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
18+
import java.io.ByteArrayInputStream;
19+
import java.nio.charset.StandardCharsets;
20+
21+
import org.eclipse.swt.graphics.ImageData;
22+
import org.eclipse.swt.svg.JSVGRasterizer;
23+
import org.junit.jupiter.api.Test;
24+
25+
class JSVGRasterizerTest {
26+
27+
private final JSVGRasterizer rasterizer = new JSVGRasterizer();
28+
private String svgString = """
29+
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
30+
<rect width="100%" height="100%"/>
31+
</svg>
32+
""";
33+
private ByteArrayInputStream svgStream(String svg) {
34+
return new ByteArrayInputStream(svg.getBytes(StandardCharsets.UTF_8));
35+
}
36+
37+
@Test
38+
void testRasterizeWithZoom(){
39+
ImageData data = rasterizer.rasterizeSVG(svgStream(svgString), 200);
40+
assertEquals(200, data.width);
41+
assertEquals(200, data.height);
42+
}
43+
44+
@Test
45+
void testRasterizeWithWidthHeight(){
46+
ImageData data = rasterizer.rasterizeSVG(svgStream(svgString), 300, 150);
47+
assertEquals(300, data.width);
48+
assertEquals(150, data.height);
49+
}
50+
51+
52+
}

0 commit comments

Comments
 (0)