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 1e3db4a commit 9d1a0f1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ protected boolean supportsZoomLevel(int zoom) {
// Currently only support integer zoom levels, because getZoomedImageData(..)
// suffers from Bug 97506: [HiDPI] ImageData.scaledTo() should use a
// better interpolation method.
return zoom > 0 && zoom % 100 == 0;
return true;
}

private ImageData getZoomedImageData(ImageDataProvider srcProvider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public ImageData getImageData(int zoom) {
InputStream in = getStream(zoom);
if (in != null) {
try (BufferedInputStream stream = new BufferedInputStream(in)) {
return new ImageData(stream);
return new ImageData(stream, zoom);
} catch (SWTException e) {
if (e.code != SWT.ERROR_INVALID_IMAGE) {
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public URLImageFileNameProvider(String url) {
public String getImagePath(int zoom) {
URL tempURL = getURL(url);
if (tempURL != null) {
if (tempURL.toString().endsWith(".svg")) { //$NON-NLS-1$
return getFilePath(tempURL, false);
}
final boolean logIOException = zoom == 100;
if (zoom == 100) {
return getFilePath(tempURL, logIOException);
Expand Down Expand Up @@ -139,6 +142,9 @@ public ImageData getImageData(int zoom) {
private static ImageData getImageData(String url, int zoom) {
URL tempURL = getURL(url);
if (tempURL != null) {
if (tempURL.toString().endsWith(".svg")) { //$NON-NLS-1$
return getImageData(tempURL, zoom);
}
if (zoom == 100) {
return getImageData(tempURL);
}
Expand All @@ -161,10 +167,14 @@ private static ImageData getImageData(String url, int zoom) {
}

private static ImageData getImageData(URL url) {
return getImageData(url, 0);
}

private static ImageData getImageData(URL url, int zoom) {
ImageData result = null;
try (InputStream in = getStream(url)) {
if (in != null) {
result = new ImageData(in);
result = new ImageData(in, zoom);
}
} catch (SWTException e) {
if (e.code != SWT.ERROR_INVALID_IMAGE) {
Expand Down
4 changes: 4 additions & 0 deletions features/org.eclipse.e4.rcp/feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@
arch="aarch64"
version="0.0.0"/>

<plugin
id="org.eclipse.swt.svg"
version="0.0.0"/>

<plugin
id="org.eclipse.jface"
version="0.0.0"/>
Expand Down

0 comments on commit 9d1a0f1

Please sign in to comment.