Skip to content

Commit

Permalink
Introduce SVG Rasterization for Icons
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Michael5601 committed Jan 24, 2025
1 parent a0a0485 commit 36312c2
Show file tree
Hide file tree
Showing 27 changed files with 1,297 additions and 17 deletions.
11 changes: 11 additions & 0 deletions bundles/org.eclipse.swt.svg.tests/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="JUnit Tests">
<attributes>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
28 changes: 28 additions & 0 deletions bundles/org.eclipse.swt.svg.tests/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.eclipse.swt.svg.tests</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
org.eclipse.jdt.core.compiler.compliance=21
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=21
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*******************************************************************************
* Copyright (c) 2025 Vector Informatik GmbH and others.
*
* This program and the accompanying materials are made available under the terms of the Eclipse
* Public License 2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors: Michael Bangas (Vector Informatik GmbH) - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.svg.tests.junit;

import static org.eclipse.swt.tests.junit.SwtTestUtil.assertSWTProblem;
import static org.junit.Assert.fail;

import java.io.File;
import java.nio.file.Path;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageDataProvider;
import org.eclipse.swt.graphics.ImageFileNameProvider;
import org.eclipse.swt.tests.junit.SwtTestUtil;
import org.eclipse.swt.widgets.Display;
import org.junit.Before;
import org.junit.Test;

public class Test_org_eclipse_swt_internal_SVGRasterizer {

Display display;

ImageFileNameProvider imageFileNameProvider = zoom -> {
String fileName = "collapseall.svg";
return getPath(fileName);
};

ImageDataProvider imageDataProvider = zoom -> {
String fileName = "collapseall.svg";
return new ImageData(getPath(fileName), zoom);
};

@Before
public void setUp() {
display = Display.getDefault();
}

String getPath(String fileName) {
String urlPath = "";
String pluginPath = System.getProperty("PLUGIN_PATH");
if (pluginPath == null) {
urlPath = Path.of("data/" + fileName).toAbsolutePath().toString();
} else {
urlPath = pluginPath + "/data/" + fileName;
}
if (File.separatorChar != '/')
urlPath = urlPath.replace('/', File.separatorChar);
if (SwtTestUtil.isWindows && urlPath.indexOf(File.separatorChar) == 0)
urlPath = urlPath.substring(1);
urlPath = urlPath.replaceAll("%20", " ");
return urlPath;
}

@Test
public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageFileNameProvider() {
// Valid provider
Image image = new Image(display, imageFileNameProvider);
image.dispose();
// Corrupt Image provider
ImageFileNameProvider provider = zoom -> {
String fileName = "corrupt.svg";
return getPath(fileName);
};
try {
image = new Image(display, provider);
image.dispose();
fail("No exception thrown for corrupt image file.");
} catch (SWTException e) {
assertSWTProblem("Incorrect exception thrown for provider with corrupt images", SWT.ERROR_INVALID_IMAGE, e);
}
}

@Test
public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageDataProvider() {
// Valid provider
Image image = new Image(display, imageDataProvider);
image.dispose();
// Corrupt Image provider
ImageDataProvider provider = zoom -> {
String fileName = "corrupt.svg";
return new ImageData(getPath(fileName), zoom);
};
try {
image = new Image(display, provider);
image.dispose();
fail("No exception thrown for corrupt image file.");
} catch (SWTException e) {
assertSWTProblem("Incorrect exception thrown for provider with corrupt images", SWT.ERROR_INVALID_IMAGE, e);
}
}
}
13 changes: 13 additions & 0 deletions bundles/org.eclipse.swt.svg.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Tests
Bundle-SymbolicName: org.eclipse.swt.svg.tests
Bundle-Version: 1.0.0.qualifier
Require-Bundle: org.eclipse.swt,
org.eclipse.jface,
org.eclipse.swt.tests,
org.junit,
org.eclipse.core.runtime
Automatic-Module-Name: org.eclipse.swt.svg.tests
Bundle-ClassPath: .
Bundle-RequiredExecutionEnvironment: JavaSE-21
4 changes: 4 additions & 0 deletions bundles/org.eclipse.swt.svg.tests/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source.. = JUnit Tests/,
output.. = bin/
bin.includes = .,\
META-INF/
223 changes: 223 additions & 0 deletions bundles/org.eclipse.swt.svg.tests/data/collapseall.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 36312c2

Please sign in to comment.