Skip to content

Commit 6c37f04

Browse files
Michael5601HannesWell
authored andcommitted
Enable Loading of SVGs in arbitrary sized for ImageDataProvider
1 parent 037c5d7 commit 6c37f04

File tree

3 files changed

+50
-33
lines changed

3 files changed

+50
-33
lines changed

bundles/org.eclipse.jface/.settings/org.eclipse.jdt.core.prefs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ org.eclipse.jdt.core.compiler.problem.deadCode=warning
4646
org.eclipse.jdt.core.compiler.problem.deprecation=warning
4747
org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled
4848
org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled
49-
org.eclipse.jdt.core.compiler.problem.discouragedReference=error
49+
org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
5050
org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
5151
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
5252
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error

bundles/org.eclipse.jface/src/org/eclipse/jface/resource/FileImageDescriptor.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2022 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -16,7 +16,8 @@
1616
*******************************************************************************/
1717
package org.eclipse.jface.resource;
1818

19-
import java.io.BufferedInputStream;
19+
import static org.eclipse.jface.resource.URLImageDescriptor.loadImageData;
20+
2021
import java.io.FileInputStream;
2122
import java.io.FileNotFoundException;
2223
import java.io.IOException;
@@ -32,7 +33,6 @@
3233
import org.eclipse.core.runtime.Status;
3334
import org.eclipse.jface.internal.InternalPolicy;
3435
import org.eclipse.jface.util.Policy;
35-
import org.eclipse.swt.SWT;
3636
import org.eclipse.swt.SWTException;
3737
import org.eclipse.swt.graphics.Device;
3838
import org.eclipse.swt.graphics.Image;
@@ -122,20 +122,7 @@ public boolean equals(Object o) {
122122
*/
123123
@Override
124124
public ImageData getImageData(int zoom) {
125-
InputStream in = getStream(zoom);
126-
if (in != null) {
127-
try (BufferedInputStream stream = new BufferedInputStream(in)) {
128-
return new ImageData(stream);
129-
} catch (SWTException e) {
130-
if (e.code != SWT.ERROR_INVALID_IMAGE) {
131-
throw e;
132-
// fall through otherwise
133-
}
134-
} catch (IOException ioe) {
135-
// fall through
136-
}
137-
}
138-
return null;
125+
return getImageData(name, zoom);
139126
}
140127

141128
/**
@@ -146,19 +133,20 @@ public ImageData getImageData(int zoom) {
146133
* @return the buffered stream on the file or <code>null</code> if the
147134
* file cannot be found
148135
*/
149-
private InputStream getStream(int zoom) {
150-
if (zoom == 100) {
151-
return getStream(name);
136+
@SuppressWarnings("resource")
137+
private ImageData getImageData(String name, int zoom) {
138+
if (zoom == 100 || URLImageDescriptor.canLoadAtZoom(() -> getStream(name), zoom)) {
139+
return loadImageData(getStream(name), 100, zoom);
152140
}
153141

154142
InputStream xstream = getStream(getxName(name, zoom));
155143
if (xstream != null) {
156-
return xstream;
144+
return loadImageData(xstream, zoom, zoom);
157145
}
158146

159147
InputStream xpath = getStream(getxPath(name, zoom));
160148
if (xpath != null) {
161-
return xpath;
149+
return loadImageData(xpath, zoom, zoom);
162150
}
163151

164152
return null;

bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2022 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -17,13 +17,15 @@
1717
*******************************************************************************/
1818
package org.eclipse.jface.resource;
1919

20+
2021
import java.io.BufferedInputStream;
2122
import java.io.IOException;
2223
import java.io.InputStream;
2324
import java.net.MalformedURLException;
2425
import java.net.URL;
2526
import java.nio.file.Files;
2627
import java.nio.file.Path;
28+
import java.util.function.Supplier;
2729

2830
import org.eclipse.core.runtime.FileLocator;
2931
import org.eclipse.core.runtime.IAdaptable;
@@ -38,6 +40,10 @@
3840
import org.eclipse.swt.graphics.ImageData;
3941
import org.eclipse.swt.graphics.ImageDataProvider;
4042
import org.eclipse.swt.graphics.ImageFileNameProvider;
43+
import org.eclipse.swt.graphics.ImageLoader;
44+
import org.eclipse.swt.internal.DPIUtil.ElementAtZoom;
45+
import org.eclipse.swt.internal.NativeImageLoader;
46+
import org.eclipse.swt.internal.image.FileFormat;
4147

4248
/**
4349
* An ImageDescriptor that gets its information from a URL. This class is not
@@ -127,7 +133,7 @@ public boolean equals(Object o) {
127133
@Deprecated
128134
@Override
129135
public ImageData getImageData() {
130-
return getImageData(getURL(url));
136+
return getImageData(getURL(url), 100, 100);
131137
}
132138

133139
@Override
@@ -138,12 +144,12 @@ public ImageData getImageData(int zoom) {
138144
private static ImageData getImageData(String url, int zoom) {
139145
URL tempURL = getURL(url);
140146
if (tempURL != null) {
141-
if (zoom == 100) {
142-
return getImageData(tempURL);
147+
if (zoom == 100 || canLoadAtZoom(() -> getStream(tempURL), zoom)) {
148+
return getImageData(tempURL, 100, zoom);
143149
}
144150
URL xUrl = getxURL(tempURL, zoom);
145151
if (xUrl != null) {
146-
ImageData xdata = getImageData(xUrl);
152+
ImageData xdata = getImageData(xUrl, zoom, zoom);
147153
if (xdata != null) {
148154
return xdata;
149155
}
@@ -152,18 +158,23 @@ private static ImageData getImageData(String url, int zoom) {
152158
if (xpath != null) {
153159
URL xPathUrl = getURL(xpath);
154160
if (xPathUrl != null) {
155-
return getImageData(xPathUrl);
161+
return getImageData(xPathUrl, zoom, zoom);
156162
}
157163
}
158164
}
159165
return null;
160166
}
161167

162-
private static ImageData getImageData(URL url) {
168+
@SuppressWarnings("resource")
169+
private static ImageData getImageData(URL url, int fileZoom, int targetZoom) {
170+
return loadImageData(getStream(url), fileZoom, targetZoom);
171+
}
172+
173+
static ImageData loadImageData(InputStream stream, int fileZoom, int targetZoom) {
163174
ImageData result = null;
164-
try (InputStream in = getStream(url)) {
175+
try (InputStream in = stream) {
165176
if (in != null) {
166-
result = new ImageData(in);
177+
return loadImageFromStream(new BufferedInputStream(in), fileZoom, targetZoom);
167178
}
168179
} catch (SWTException e) {
169180
if (e.code != SWT.ERROR_INVALID_IMAGE) {
@@ -176,6 +187,24 @@ private static ImageData getImageData(URL url) {
176187
return result;
177188
}
178189

190+
@SuppressWarnings("restriction")
191+
private static ImageData loadImageFromStream(InputStream stream, int fileZoom, int targetZoom) {
192+
return NativeImageLoader.load(new ElementAtZoom<>(stream, fileZoom), new ImageLoader(), targetZoom).get(0)
193+
.element();
194+
}
195+
196+
@SuppressWarnings("restriction")
197+
static boolean canLoadAtZoom(Supplier<InputStream> stream, int zoom) {
198+
try (InputStream in = stream.get()) {
199+
if (in != null) {
200+
return FileFormat.canLoadAtZoom(new ElementAtZoom<>(in, 100), zoom);
201+
}
202+
} catch (IOException e) {
203+
Policy.getLog().log(Status.error(e.getLocalizedMessage(), e));
204+
}
205+
return false;
206+
}
207+
179208
/**
180209
* Returns a stream on the image contents. Returns null if a stream could
181210
* not be opened.
@@ -198,7 +227,7 @@ private static InputStream getStream(URL url) {
198227
url = platformURL;
199228
}
200229
}
201-
return new BufferedInputStream(url.openStream());
230+
return url.openStream();
202231
} catch (IOException e) {
203232
if (InternalPolicy.DEBUG_LOG_URL_IMAGE_DESCRIPTOR_MISSING_2x) {
204233
String path = url.getPath();

0 commit comments

Comments
 (0)