Skip to content

Commit dceecc6

Browse files
committed
Introduce SVG Rasterization for Icons
Feature Proposal: Rasterization of SVGs at Runtime for Eclipse Icons Fixes #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.
1 parent 9f60279 commit dceecc6

File tree

27 files changed

+1291
-17
lines changed

27 files changed

+1291
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21"/>
4+
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
5+
<classpathentry kind="src" path="JUnit Tests">
6+
<attributes>
7+
<attribute name="test" value="true"/>
8+
</attributes>
9+
</classpathentry>
10+
<classpathentry kind="output" path="bin"/>
11+
</classpath>
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>org.eclipse.swt.svg.tests</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.pde.ManifestBuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.pde.SchemaBuilder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
</buildSpec>
24+
<natures>
25+
<nature>org.eclipse.pde.PluginNature</nature>
26+
<nature>org.eclipse.jdt.core.javanature</nature>
27+
</natures>
28+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
3+
org.eclipse.jdt.core.compiler.compliance=21
4+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
5+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
6+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
7+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
8+
org.eclipse.jdt.core.compiler.release=enabled
9+
org.eclipse.jdt.core.compiler.source=21
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Manifest-Version: 1.0
2+
Bundle-ManifestVersion: 2
3+
Bundle-Name: Tests
4+
Bundle-SymbolicName: org.eclipse.swt.svg.tests
5+
Bundle-Version: 1.0.0.qualifier
6+
Require-Bundle: org.eclipse.swt,
7+
org.eclipse.jface,
8+
org.eclipse.swt.tests,
9+
org.junit,
10+
org.eclipse.core.runtime
11+
Automatic-Module-Name: org.eclipse.swt.svg.tests
12+
Bundle-ClassPath: .
13+
Bundle-RequiredExecutionEnvironment: JavaSE-21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source.. = JUnit Tests/,
2+
output.. = bin/
3+
bin.includes = .,\
4+
META-INF/
Loading

0 commit comments

Comments
 (0)