Skip to content

Commit 8444ec7

Browse files
arunjose696HeikoKlare
authored andcommitted
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 5d2665d commit 8444ec7

File tree

3 files changed

+80
-17
lines changed

3 files changed

+80
-17
lines changed

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

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,34 +74,47 @@ public ImageData rasterizeSVG(InputStream inputStream, int zoom) {
7474
if (zoom < 0) {
7575
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
7676
}
77-
SVGDocument svgDocument = loadSVG(inputStream);
78-
if (svgDocument == null) {
79-
SWT.error(SWT.ERROR_INVALID_IMAGE);
80-
}
77+
SVGDocument svgDocument = loadAndValidateSVG(inputStream);
8178
BufferedImage rasterizedImage = renderSVG(svgDocument, zoom);
8279
return convertToSWTImageData(rasterizedImage);
8380
}
8481

85-
private SVGDocument loadSVG(InputStream inputStream) {
86-
return SVG_LOADER.load(inputStream, null, LoaderContext.createDefault());
82+
@Override
83+
public ImageData rasterizeSVG(InputStream inputStream, int width, int height) {
84+
SVGDocument svgDocument = loadAndValidateSVG(inputStream);
85+
BufferedImage rasterizedImage = renderSVG(svgDocument, width, height);
86+
return convertToSWTImageData(rasterizedImage);
87+
}
88+
89+
private SVGDocument loadAndValidateSVG(InputStream inputStream) {
90+
SVGDocument svgDocument = SVG_LOADER.load(inputStream, null, LoaderContext.createDefault());
91+
if (svgDocument == null) {
92+
SWT.error(SWT.ERROR_INVALID_IMAGE);
93+
}
94+
return svgDocument;
8795
}
8896

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

98-
private BufferedImage createImageBase(SVGDocument svgDocument, float scalingFactor) {
99-
FloatSize sourceImageSize = svgDocument.size();
100-
int targetImageWidth = calculateTargetWidth(scalingFactor, sourceImageSize);
101-
int targetImageHeight = calculateTargetHeight(scalingFactor, sourceImageSize);
102-
return new BufferedImage(targetImageWidth, targetImageHeight, BufferedImage.TYPE_INT_ARGB);
103-
}
104-
105118
private int calculateTargetWidth(float scalingFactor, FloatSize sourceImageSize) {
106119
double sourceImageWidth = sourceImageSize.getWidth();
107120
return (int) Math.round(sourceImageWidth * scalingFactor);
@@ -112,10 +125,11 @@ private int calculateTargetHeight(float scalingFactor, FloatSize sourceImageSize
112125
return (int) Math.round(sourceImageHeight * scalingFactor);
113126
}
114127

115-
private Graphics2D configureRenderingOptions(float scalingFactor, BufferedImage image) {
128+
private Graphics2D configureRenderingOptions(float widthScalingFactor, float heightScalingFactor,
129+
BufferedImage image) {
116130
Graphics2D g = image.createGraphics();
117131
g.setRenderingHints(RENDERING_HINTS);
118-
g.scale(scalingFactor, scalingFactor);
132+
g.scale(widthScalingFactor, heightScalingFactor);
119133
return g;
120134
}
121135

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,24 @@ public interface SVGRasterizer {
4141
* </ul>
4242
*/
4343
public ImageData rasterizeSVG(InputStream stream, int zoom);
44+
45+
/**
46+
* Rasterizes an SVG image from the provided {@code InputStream} into a raster
47+
* image of the specified width and height.
48+
*
49+
* @param stream the SVG image as an {@link InputStream}.
50+
* @param width the width of the rasterized image in pixels (must be positive).
51+
* @param height the height of the rasterized image in pixels (must be positive).
52+
* @return the {@link ImageData} for the rasterized image.
53+
*
54+
* @exception SWTException
55+
* <ul>
56+
* <li>ERROR_INVALID_IMAGE - if the SVG cannot be loaded</li>
57+
* </ul>
58+
* @exception IllegalArgumentException
59+
* <ul>
60+
* <li>ERROR_INVALID_ARGUMENT - if the width or height is less than zero</li>
61+
* </ul>
62+
*/
63+
public ImageData rasterizeSVG(InputStream stream, int width, int height);
4464
}

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/JSVGRasterizerTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,33 @@ void testRasterizeWithZoomWithInvalidSVG() {
6868
assertEquals(SWT.ERROR_INVALID_IMAGE, exception.code);
6969
}
7070

71+
@Test
72+
void testRasterizeWithTargetSize() {
73+
ImageData data = rasterizer.rasterizeSVG(svgStream(svgString), 300, 150);
74+
assertEquals(300, data.width);
75+
assertEquals(150, data.height);
76+
}
77+
78+
@Test
79+
void testRasterizeWithTargetSizeHavingInvalidHeight() {
80+
assertThrows(IllegalArgumentException.class, () -> {
81+
rasterizer.rasterizeSVG(svgStream(svgString), -1, 150);
82+
});
83+
}
84+
85+
@Test
86+
void testRasterizeWithTargetSizeHavingInvalidWidth() {
87+
assertThrows(IllegalArgumentException.class, () -> {
88+
rasterizer.rasterizeSVG(svgStream(svgString), 150, -1);
89+
});
90+
}
91+
92+
@Test
93+
void testRasterizeWithTargetSizeWithInvalidSVG() {
94+
SWTException exception = assertThrows(SWTException.class, () -> {
95+
rasterizer.rasterizeSVG(invalidSvg, 150, 150);
96+
});
97+
assertEquals(SWT.ERROR_INVALID_IMAGE, exception.code);
98+
}
99+
71100
}

0 commit comments

Comments
 (0)