Skip to content

Feature Proposal: Rasterization of SVGs at Runtime for Eclipse Icons #2593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ org.eclipse.jdt.core.compiler.problem.deadCode=warning
org.eclipse.jdt.core.compiler.problem.deprecation=warning
org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled
org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled
org.eclipse.jdt.core.compiler.problem.discouragedReference=error
org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2022 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -16,7 +16,8 @@
*******************************************************************************/
package org.eclipse.jface.resource;

import java.io.BufferedInputStream;
import static org.eclipse.jface.resource.URLImageDescriptor.loadImageData;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -32,7 +33,6 @@
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.internal.InternalPolicy;
import org.eclipse.jface.util.Policy;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Image;
Expand Down Expand Up @@ -122,20 +122,7 @@ public boolean equals(Object o) {
*/
@Override
public ImageData getImageData(int zoom) {
InputStream in = getStream(zoom);
if (in != null) {
try (BufferedInputStream stream = new BufferedInputStream(in)) {
return new ImageData(stream);
} catch (SWTException e) {
if (e.code != SWT.ERROR_INVALID_IMAGE) {
throw e;
// fall through otherwise
}
} catch (IOException ioe) {
// fall through
}
}
return null;
return getImageData(name, zoom);
}

/**
Expand All @@ -146,19 +133,20 @@ public ImageData getImageData(int zoom) {
* @return the buffered stream on the file or <code>null</code> if the
* file cannot be found
*/
private InputStream getStream(int zoom) {
if (zoom == 100) {
return getStream(name);
@SuppressWarnings("resource")
private ImageData getImageData(String name, int zoom) {
if (zoom == 100 || URLImageDescriptor.canLoadAtZoom(() -> getStream(name), zoom)) {
return loadImageData(getStream(name), 100, zoom);
}

InputStream xstream = getStream(getxName(name, zoom));
if (xstream != null) {
return xstream;
return loadImageData(xstream, zoom, zoom);
}

InputStream xpath = getStream(getxPath(name, zoom));
if (xpath != null) {
return xpath;
return loadImageData(xpath, zoom, zoom);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2022 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -17,13 +17,15 @@
*******************************************************************************/
package org.eclipse.jface.resource;


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.function.Supplier;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IAdaptable;
Expand All @@ -38,6 +40,10 @@
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageDataProvider;
import org.eclipse.swt.graphics.ImageFileNameProvider;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.internal.DPIUtil.ElementAtZoom;
import org.eclipse.swt.internal.NativeImageLoader;
import org.eclipse.swt.internal.image.FileFormat;

/**
* An ImageDescriptor that gets its information from a URL. This class is not
Expand Down Expand Up @@ -127,7 +133,7 @@
@Deprecated
@Override
public ImageData getImageData() {
return getImageData(getURL(url));
return getImageData(getURL(url), 100, 100);
}

@Override
Expand All @@ -138,12 +144,12 @@
private static ImageData getImageData(String url, int zoom) {
URL tempURL = getURL(url);
if (tempURL != null) {
if (zoom == 100) {
return getImageData(tempURL);
if (zoom == 100 || canLoadAtZoom(() -> getStream(tempURL), zoom)) {
return getImageData(tempURL, 100, zoom);
}
URL xUrl = getxURL(tempURL, zoom);
if (xUrl != null) {
ImageData xdata = getImageData(xUrl);
ImageData xdata = getImageData(xUrl, zoom, zoom);
if (xdata != null) {
return xdata;
}
Expand All @@ -152,18 +158,23 @@
if (xpath != null) {
URL xPathUrl = getURL(xpath);
if (xPathUrl != null) {
return getImageData(xPathUrl);
return getImageData(xPathUrl, zoom, zoom);
}
}
}
return null;
}

private static ImageData getImageData(URL url) {
@SuppressWarnings("resource")
private static ImageData getImageData(URL url, int fileZoom, int targetZoom) {
return loadImageData(getStream(url), fileZoom, targetZoom);
}

static ImageData loadImageData(InputStream stream, int fileZoom, int targetZoom) {
ImageData result = null;
try (InputStream in = getStream(url)) {
try (InputStream in = stream) {
if (in != null) {
result = new ImageData(in);
return loadImageFromStream(new BufferedInputStream(in), fileZoom, targetZoom);
}
} catch (SWTException e) {
if (e.code != SWT.ERROR_INVALID_IMAGE) {
Expand All @@ -176,6 +187,24 @@
return result;
}

@SuppressWarnings("restriction")

Check warning on line 190 in bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java

View check run for this annotation

Jenkins - Eclipse Platform / Compiler

Unnecessary Code

NORMAL: Unnecessary @SuppressWarnings("restriction")
private static ImageData loadImageFromStream(InputStream stream, int fileZoom, int targetZoom) {
return NativeImageLoader.load(new ElementAtZoom<>(stream, fileZoom), new ImageLoader(), targetZoom).get(0)
.element();
}

@SuppressWarnings("restriction")

Check warning on line 196 in bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java

View check run for this annotation

Jenkins - Eclipse Platform / Compiler

Unnecessary Code

NORMAL: Unnecessary @SuppressWarnings("restriction")
static boolean canLoadAtZoom(Supplier<InputStream> stream, int zoom) {
try (InputStream in = stream.get()) {
if (in != null) {
return FileFormat.canLoadAtZoom(new ElementAtZoom<>(in, 100), zoom);
}
} catch (IOException e) {
Policy.getLog().log(Status.error(e.getLocalizedMessage(), e));
}
return false;
}

/**
* Returns a stream on the image contents. Returns null if a stream could
* not be opened.
Expand All @@ -198,7 +227,7 @@
url = platformURL;
}
}
return new BufferedInputStream(url.openStream());
return url.openStream();
} catch (IOException e) {
if (InternalPolicy.DEBUG_LOG_URL_IMAGE_DESCRIPTOR_MISSING_2x) {
String path = url.getPath();
Expand Down
Loading