From 3dd870e1bfc569221885457a6a72946da604bb14 Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Tue, 5 Nov 2024 14:53:45 +0100 Subject: [PATCH] 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. --- .../jface/resource/FileImageDescriptor.java | 2 +- .../jface/resource/URLImageDescriptor.java | 23 +++++++++++++++++++ features/org.eclipse.e4.rcp/feature.xml | 4 ++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FileImageDescriptor.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FileImageDescriptor.java index f07f6b6cfc4..a1b3cdd39e8 100644 --- a/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FileImageDescriptor.java +++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FileImageDescriptor.java @@ -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; diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java index e00a363bfe0..e0cb6271557 100644 --- a/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java +++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java @@ -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); @@ -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); } @@ -177,6 +183,23 @@ private static ImageData getImageData(URL url) { return result; } + private static ImageData getImageData(URL url, int zoom) { + ImageData result = null; + try (InputStream in = getStream(url)) { + if (in != null) { + result = new ImageData(in, zoom); + } + } catch (SWTException e) { + if (e.code != SWT.ERROR_INVALID_IMAGE) { + throw e; + // fall through otherwise + } + } catch (IOException e) { + Policy.getLog().log(new Status(IStatus.ERROR, Policy.JFACE, e.getLocalizedMessage(), e)); + } + return result; + } + /** * Returns a stream on the image contents. Returns null if a stream could * not be opened. diff --git a/features/org.eclipse.e4.rcp/feature.xml b/features/org.eclipse.e4.rcp/feature.xml index 0b345ae6218..e39c34889db 100644 --- a/features/org.eclipse.e4.rcp/feature.xml +++ b/features/org.eclipse.e4.rcp/feature.xml @@ -294,6 +294,10 @@ arch="aarch64" version="0.0.0"/> + +