Skip to content

Commit 6a23f97

Browse files
committed
remove check for svg by using new FileFormat.canLoadAtZoom API
1 parent 53825d5 commit 6a23f97

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

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

+17-4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.eclipse.swt.graphics.ImageLoader;
4444
import org.eclipse.swt.internal.DPIUtil.ElementAtZoom;
4545
import org.eclipse.swt.internal.NativeImageLoader;
46+
import org.eclipse.swt.internal.image.FileFormat;
4647

4748
/**
4849
* An image descriptor that loads its image information from a file.
@@ -54,7 +55,7 @@ private class ImageProvider implements ImageFileNameProvider {
5455
@Override
5556
public String getImagePath(int zoom) {
5657
final boolean logIOException = zoom == 100;
57-
if (zoom == 100) {
58+
if (canLoadAtZoom(zoom) || zoom == 100) {
5859
return getFilePath(name, logIOException);
5960
}
6061
SourceAtZoom<String> xName = getxName(name, zoom);
@@ -155,8 +156,8 @@ public ImageData getImageData(int zoom) {
155156
* file cannot be found
156157
*/
157158
private SourceAtZoom<InputStream> getStream(int zoom) {
158-
if (zoom == 100) {
159-
return getStream(new SourceAtZoom<>(name, 100));
159+
if (canLoadAtZoom(zoom) || zoom == 100) {
160+
return getStream(new SourceAtZoom<>(name, zoom));
160161
}
161162

162163
SourceAtZoom<InputStream> xstream = getStream(getxName(name, zoom));
@@ -172,6 +173,18 @@ private SourceAtZoom<InputStream> getStream(int zoom) {
172173
return null;
173174
}
174175

176+
@SuppressWarnings("restriction")
177+
private boolean canLoadAtZoom(int zoom) {
178+
try (InputStream in = getStream(new SourceAtZoom<>(name, zoom)).source()) {
179+
if (in != null) {
180+
return FileFormat.canLoadAtZoom(new ElementAtZoom<>(in, 100), zoom);
181+
}
182+
} catch (IOException e) {
183+
Policy.getLog().log(new Status(IStatus.ERROR, Policy.JFACE, e.getLocalizedMessage(), e));
184+
}
185+
return false;
186+
}
187+
175188
/**
176189
* try to obtain a stream for a given name, if the name does not match a valid
177190
* resource null is returned
@@ -180,9 +193,9 @@ private SourceAtZoom<InputStream> getStream(int zoom) {
180193
* @return an {@link InputStream} to read from, or <code>null</code> if fileName
181194
* does not denotes an existing resource
182195
*/
196+
@SuppressWarnings("resource")
183197
private SourceAtZoom<InputStream> getStream(SourceAtZoom<String> fileName) {
184198
if (fileName != null) {
185-
// TODO DO we need to close these?
186199
if (location != null) {
187200
return new SourceAtZoom<>(location.getResourceAsStream(fileName.source()), fileName.zoom());
188201
}

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

+14-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.eclipse.swt.graphics.ImageLoader;
4343
import org.eclipse.swt.internal.DPIUtil.ElementAtZoom;
4444
import org.eclipse.swt.internal.NativeImageLoader;
45+
import org.eclipse.swt.internal.image.FileFormat;
4546

4647
/**
4748
* An ImageDescriptor that gets its information from a URL. This class is not
@@ -161,10 +162,7 @@ public ImageData getImageData(int zoom) {
161162
private static ImageData getImageData(String url, int zoom) {
162163
URL tempURL = getURL(url);
163164
if (tempURL != null) {
164-
if (tempURL.toString().endsWith(".svg")) { //$NON-NLS-1$
165-
return getImageData(tempURL, 100, zoom);
166-
}
167-
if (zoom == 100) {
165+
if (canLoadAtZoom(zoom, tempURL) || zoom == 100) {
168166
return getImageData(tempURL, 100, zoom);
169167
}
170168
SourceAtZoom<URL> xUrl = getxURL(tempURL, zoom);
@@ -185,6 +183,18 @@ private static ImageData getImageData(String url, int zoom) {
185183
return null;
186184
}
187185

186+
@SuppressWarnings("restriction")
187+
private static boolean canLoadAtZoom(int zoom, URL tempURL) {
188+
try (InputStream in = getStream(tempURL)) {
189+
if (in != null) {
190+
return FileFormat.canLoadAtZoom(new ElementAtZoom<>(in, 100), zoom);
191+
}
192+
} catch (IOException e) {
193+
Policy.getLog().log(new Status(IStatus.ERROR, Policy.JFACE, e.getLocalizedMessage(), e));
194+
}
195+
return false;
196+
}
197+
188198
@SuppressWarnings("restriction")
189199
private static ImageData getImageData(URL url, int fileZoom, int targetZoom) {
190200
ImageData result = null;

0 commit comments

Comments
 (0)