Skip to content

Commit 5d2665d

Browse files
arunjose696HeikoKlare
authored andcommitted
Fix Javadoc of SVGRasterizer
This commits fixes Javadoc of SVGRasterizer and adds tests to verify the contract Fixes eclipse-platform#2515
1 parent 7d4190a commit 5d2665d

File tree

5 files changed

+93
-15
lines changed

5 files changed

+93
-15
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import java.awt.RenderingHints.Key;
3636
import java.awt.image.BufferedImage;
3737
import java.awt.image.DataBufferInt;
38-
import java.io.IOException;
3938
import java.io.InputStream;
4039
import java.util.Map;
4140

@@ -71,7 +70,10 @@ public class JSVGRasterizer implements SVGRasterizer {
7170
);
7271

7372
@Override
74-
public ImageData rasterizeSVG(InputStream inputStream, int zoom) throws IOException {
73+
public ImageData rasterizeSVG(InputStream inputStream, int zoom) {
74+
if (zoom < 0) {
75+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
76+
}
7577
SVGDocument svgDocument = loadSVG(inputStream);
7678
if (svgDocument == null) {
7779
SWT.error(SWT.ERROR_INVALID_IMAGE);

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,8 @@ List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom)
5151
if (targetZoom <= 0) {
5252
SWT.error(SWT.ERROR_INVALID_ARGUMENT, null, " [Cannot rasterize SVG for zoom <= 0]");
5353
}
54-
try {
55-
ImageData rasterizedImageData = RASTERIZER.rasterizeSVG(inputStream, 100 * targetZoom / fileZoom);
56-
return List.of(new ElementAtZoom<>(rasterizedImageData, targetZoom));
57-
} catch (IOException e) {
58-
SWT.error(SWT.ERROR_INVALID_IMAGE, e);
59-
return List.of();
60-
}
54+
ImageData rasterizedImageData = RASTERIZER.rasterizeSVG(inputStream, 100 * targetZoom / fileZoom);
55+
return List.of(new ElementAtZoom<>(rasterizedImageData, targetZoom));
6156
}
6257

6358
@Override

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

Lines changed: 14 additions & 5 deletions
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
/**
@@ -26,10 +27,18 @@ public interface SVGRasterizer {
2627
* specified zoom.
2728
*
2829
* @param stream the SVG image as an {@link InputStream}.
29-
* @param zoom the scaling factor (in percent) e.g. {@code 200} for doubled
30-
* size. This value must be greater zero.
31-
* @return the {@link ImageData} for the rasterized image, or {@code null} if
32-
* the input is not a valid SVG file or cannot be processed.
30+
* @param zoom the scaling percentage (e.g., 100 = original size, 200 = double size).
31+
* This value must be greater zero.
32+
* @return the {@link ImageData} for the rasterized image.
33+
*
34+
* @exception SWTException
35+
* <ul>
36+
* <li>ERROR_INVALID_IMAGE - if the SVG cannot be loaded</li>
37+
* </ul>
38+
* @exception IllegalArgumentException
39+
* <ul>
40+
* <li>ERROR_INVALID_ARGUMENT - if the zoom is less than zero</li>
41+
* </ul>
3342
*/
34-
public ImageData rasterizeSVG(InputStream stream, int zoom) throws IOException;
43+
public ImageData rasterizeSVG(InputStream stream, int zoom);
3544
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
Test_org_eclipse_swt_accessibility_AccessibleEvent.class,
4747
Test_org_eclipse_swt_accessibility_AccessibleTextEvent.class,
4848
Test_org_eclipse_swt_internal_SVGRasterizer.class,
49-
DPIUtilTests.class})
49+
DPIUtilTests.class,
50+
JSVGRasterizerTest.class})
5051
public class AllNonBrowserTests {
5152
private static List<Error> leakedResources;
5253

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*******************************************************************************
2+
* Copyright (c) 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.eclipse.swt.tests.junit.SwtTestUtil.assertSWTProblem;
17+
import static org.junit.Assert.assertThrows;
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
20+
import java.io.ByteArrayInputStream;
21+
import java.nio.charset.StandardCharsets;
22+
23+
import org.eclipse.swt.SWT;
24+
import org.eclipse.swt.SWTException;
25+
import org.eclipse.swt.graphics.ImageData;
26+
import org.eclipse.swt.svg.JSVGRasterizer;
27+
import org.junit.jupiter.api.Test;
28+
29+
class JSVGRasterizerTest {
30+
31+
private final JSVGRasterizer rasterizer = new JSVGRasterizer();
32+
private String svgString = """
33+
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
34+
<rect width="100%" height="100%"/>
35+
</svg>
36+
""";
37+
38+
private ByteArrayInputStream svgStream(String svg) {
39+
return new ByteArrayInputStream(svg.getBytes(StandardCharsets.UTF_8));
40+
}
41+
42+
private ByteArrayInputStream invalidSvg = new ByteArrayInputStream(new byte[0]);
43+
44+
@Test
45+
void testRasterizeWithZoom() {
46+
ImageData data = rasterizer.rasterizeSVG(svgStream(svgString), 200);
47+
assertEquals(200, data.width);
48+
assertEquals(200, data.height);
49+
}
50+
51+
@Test
52+
void testRasterizeWithZoomNegative() {
53+
try {
54+
rasterizer.rasterizeSVG(svgStream(svgString), -100);
55+
56+
} catch (IllegalArgumentException e) {
57+
assertSWTProblem("Incorrect exception thrown for negative zoom", SWT.ERROR_INVALID_ARGUMENT, e);
58+
}
59+
60+
}
61+
62+
@Test
63+
void testRasterizeWithZoomWithInvalidSVG() {
64+
65+
SWTException exception = assertThrows(SWTException.class, () -> {
66+
rasterizer.rasterizeSVG(invalidSvg, 100);
67+
});
68+
assertEquals(SWT.ERROR_INVALID_IMAGE, exception.code);
69+
}
70+
71+
}

0 commit comments

Comments
 (0)