Skip to content

Commit c451620

Browse files
committed
Introduce SVG Rasterization for Icons
Feature Proposal: Rasterization of SVGs at Runtime for Eclipse Icons Fixes eclipse-platform#1438 Eclipse currently loads icons exclusively as raster graphics (e.g., `.png`), without support for vector formats like `.svg`. A major drawback of raster graphics is their inability to scale without degrading image quality. Additionally, generating icons of different sizes requires manually rasterizing SVGs outside Eclipse, leading to unnecessary effort and many icon files. This PR introduces support for vector graphics in Eclipse, enabling SVGs to be used for icons. Existing PNG icons will continue to be loaded alongside SVGs, allowing the use of the new functionality without the need to replace all PNG files at once. --- - **How It Works**: - To use SVG icons, simply place the SVG file in the bundle and reference it in the `plugin.xml` and other necessary locations, as is done for PNGs. No additional configuration is required. - At runtime, Eclipse uses the library JSVG to rasterize the SVG into a raster image of the desired size, eliminating the need for scaling. My analysis shows that JSVG is the most suitable Java library for this purpose. - You need to write the flag `-Dswt.autoScale=quarter` into your `eclipse.ini` file or into the run arguments of a new configuration. revert changes
1 parent 682e9ab commit c451620

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Vector Informatik GmbH and others.
3+
*
4+
* This program and the accompanying materials are made available under the terms of the Eclipse
5+
* Public License 2.0 which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors: Michael Bangas (Vector Informatik GmbH) - initial API and implementation
11+
*******************************************************************************/
12+
package org.eclipse.swt.svg.tests.junit;
13+
14+
import static org.eclipse.swt.tests.junit.SwtTestUtil.assertSWTProblem;
15+
import static org.junit.Assert.fail;
16+
17+
import java.io.File;
18+
import java.nio.file.Path;
19+
import org.eclipse.swt.SWT;
20+
import org.eclipse.swt.SWTException;
21+
import org.eclipse.swt.graphics.Image;
22+
import org.eclipse.swt.graphics.ImageData;
23+
import org.eclipse.swt.graphics.ImageDataProvider;
24+
import org.eclipse.swt.graphics.ImageFileNameProvider;
25+
import org.eclipse.swt.tests.junit.SwtTestUtil;
26+
import org.eclipse.swt.widgets.Display;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
30+
public class Test_org_eclipse_swt_internal_SVGRasterizer {
31+
32+
Display display;
33+
34+
ImageFileNameProvider imageFileNameProvider = zoom -> {
35+
String fileName = "collapseall.svg";
36+
return getPath(fileName);
37+
};
38+
39+
ImageDataProvider imageDataProvider = zoom -> {
40+
String fileName = "collapseall.svg";
41+
return new ImageData(getPath(fileName), zoom);
42+
};
43+
44+
@Before
45+
public void setUp() {
46+
display = Display.getDefault();
47+
}
48+
49+
String getPath(String fileName) {
50+
String urlPath = "";
51+
String pluginPath = System.getProperty("PLUGIN_PATH");
52+
if (pluginPath == null) {
53+
urlPath = Path.of("data/" + fileName).toAbsolutePath().toString();
54+
} else {
55+
urlPath = pluginPath + "/data/" + fileName;
56+
}
57+
if (File.separatorChar != '/')
58+
urlPath = urlPath.replace('/', File.separatorChar);
59+
if (SwtTestUtil.isWindows && urlPath.indexOf(File.separatorChar) == 0)
60+
urlPath = urlPath.substring(1);
61+
urlPath = urlPath.replaceAll("%20", " ");
62+
return urlPath;
63+
}
64+
65+
@Test
66+
public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageFileNameProvider() {
67+
// Valid provider
68+
Image image = new Image(display, imageFileNameProvider);
69+
image.dispose();
70+
// Corrupt Image provider
71+
ImageFileNameProvider provider = zoom -> {
72+
String fileName = "corrupt.svg";
73+
return getPath(fileName);
74+
};
75+
try {
76+
image = new Image(display, provider);
77+
image.dispose();
78+
fail("No exception thrown for corrupt image file.");
79+
} catch (SWTException e) {
80+
assertSWTProblem("Incorrect exception thrown for provider with corrupt images", SWT.ERROR_INVALID_IMAGE, e);
81+
}
82+
}
83+
84+
@Test
85+
public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageDataProvider() {
86+
// Valid provider
87+
Image image = new Image(display, imageDataProvider);
88+
image.dispose();
89+
// Corrupt Image provider
90+
ImageDataProvider provider = zoom -> {
91+
String fileName = "corrupt.svg";
92+
return new ImageData(getPath(fileName), zoom);
93+
};
94+
try {
95+
image = new Image(display, provider);
96+
image.dispose();
97+
fail("No exception thrown for corrupt image file.");
98+
} catch (SWTException e) {
99+
assertSWTProblem("Incorrect exception thrown for provider with corrupt images", SWT.ERROR_INVALID_IMAGE, e);
100+
}
101+
}
102+
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageDataLoader.java

+4
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,8 @@ public static ElementAtZoom<ImageData> load(String filename, int fileZoom, int t
4949
return data.get(0);
5050
}
5151

52+
public static ImageData[] load(String filename, int zoom) {
53+
return new ImageLoader().load(filename, zoom);
54+
}
55+
5256
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Vector Informatik GmbH and others.
3+
*
4+
* This program and the accompanying materials are made available under the terms of the Eclipse
5+
* Public License 2.0 which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors: Michael Bangas (Vector Informatik GmbH) - initial API and implementation
11+
*******************************************************************************/
12+
package org.eclipse.swt.internal;
13+
14+
import java.io.*;
15+
16+
import org.eclipse.swt.graphics.*;
17+
18+
/**
19+
* Defines the interface for an SVG rasterizer, responsible for converting SVG
20+
* data into rasterized images.
21+
*
22+
* @since 3.129
23+
*/
24+
public interface SVGRasterizer {
25+
/**
26+
* Rasterizes an SVG image from the provided {@code InputStream} using the specified
27+
* zoom factor.
28+
*
29+
* @param stream the SVG image as an {@link InputStream}.
30+
* @param zoom the scaling factor e.g. 200 for doubled size. This value must no be 0.
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.
33+
* @throws IOException if an error occurs while reading the SVG data.
34+
*/
35+
public ImageData[] rasterizeSVG(InputStream stream, int zoom) throws IOException;
36+
}

0 commit comments

Comments
 (0)