Skip to content

Commit 734bccd

Browse files
committed
Enable Loading of SVGs in arbitrary sized for ImageDataProvider
1 parent 1e6e9a9 commit 734bccd

File tree

3 files changed

+67
-36
lines changed

3 files changed

+67
-36
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: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
import org.eclipse.swt.graphics.Image;
4040
import org.eclipse.swt.graphics.ImageData;
4141
import org.eclipse.swt.graphics.ImageFileNameProvider;
42+
import org.eclipse.swt.graphics.ImageLoader;
43+
import org.eclipse.swt.internal.DPIUtil.ElementAtZoom;
44+
import org.eclipse.swt.internal.NativeImageLoader;
4245

4346
/**
4447
* An image descriptor that loads its image information from a file.
@@ -47,22 +50,23 @@ class FileImageDescriptor extends ImageDescriptor implements IAdaptable {
4750

4851
private class ImageProvider implements ImageFileNameProvider {
4952

53+
@SuppressWarnings("restriction")
5054
@Override
5155
public String getImagePath(int zoom) {
5256
final boolean logIOException = zoom == 100;
5357
if (zoom == 100) {
5458
return getFilePath(name, logIOException);
5559
}
56-
String xName = getxName(name, zoom);
60+
ElementAtZoom<String> xName = getxName(name, zoom);
5761
if (xName != null) {
58-
String xResult = getFilePath(xName, logIOException);
62+
String xResult = getFilePath(xName.element(), logIOException);
5963
if (xResult != null) {
6064
return xResult;
6165
}
6266
}
63-
String xPath = getxPath(name, zoom);
67+
ElementAtZoom<String> xPath = getxPath(name, zoom);
6468
if (xPath != null) {
65-
String xResult = getFilePath(xPath, logIOException);
69+
String xResult = getFilePath(xPath.element(), logIOException);
6670
if (xResult != null) {
6771
return xResult;
6872
}
@@ -121,12 +125,15 @@ public boolean equals(Object o) {
121125
* {@link ImageDescriptor#createImage(boolean, Device)} as of version
122126
* 3.4 so that the SWT OS optimized loading can be used.
123127
*/
128+
@SuppressWarnings("restriction")
124129
@Override
125130
public ImageData getImageData(int zoom) {
126-
InputStream in = getStream(zoom);
127-
if (in != null) {
128-
try (BufferedInputStream stream = new BufferedInputStream(in)) {
129-
return new ImageData(stream);
131+
ElementAtZoom<InputStream> inputStreamAtZoom = getStream(zoom);
132+
if (inputStreamAtZoom != null) {
133+
try (BufferedInputStream stream = new BufferedInputStream(inputStreamAtZoom.element())) {
134+
ElementAtZoom<ImageData> imageData = NativeImageLoader
135+
.load(new ElementAtZoom<>(stream, inputStreamAtZoom.zoom()), new ImageLoader(), zoom).get(0);
136+
return imageData.element();
130137
} catch (SWTException e) {
131138
if (e.code != SWT.ERROR_INVALID_IMAGE) {
132139
throw e;
@@ -147,17 +154,18 @@ public ImageData getImageData(int zoom) {
147154
* @return the buffered stream on the file or <code>null</code> if the
148155
* file cannot be found
149156
*/
150-
private InputStream getStream(int zoom) {
157+
@SuppressWarnings("restriction")
158+
private ElementAtZoom<InputStream> getStream(int zoom) {
151159
if (zoom == 100) {
152-
return getStream(name);
160+
return getStream(new ElementAtZoom<>(name, 100));
153161
}
154162

155-
InputStream xstream = getStream(getxName(name, zoom));
163+
ElementAtZoom<InputStream> xstream = getStream(getxName(name, zoom));
156164
if (xstream != null) {
157165
return xstream;
158166
}
159167

160-
InputStream xpath = getStream(getxPath(name, zoom));
168+
ElementAtZoom<InputStream> xpath = getStream(getxPath(name, zoom));
161169
if (xpath != null) {
162170
return xpath;
163171
}
@@ -173,21 +181,24 @@ private InputStream getStream(int zoom) {
173181
* @return an {@link InputStream} to read from, or <code>null</code> if fileName
174182
* does not denotes an existing resource
175183
*/
176-
private InputStream getStream(String fileName) {
184+
@SuppressWarnings("restriction")
185+
private ElementAtZoom<InputStream> getStream(ElementAtZoom<String> fileName) {
177186
if (fileName != null) {
187+
// TODO DO we need to close these?
178188
if (location != null) {
179-
return location.getResourceAsStream(fileName);
189+
return new ElementAtZoom<>(location.getResourceAsStream(fileName.element()), fileName.zoom());
180190
}
181191
try {
182-
return new FileInputStream(fileName);
192+
return new ElementAtZoom<>(new FileInputStream(fileName.element()), fileName.zoom());
183193
} catch (FileNotFoundException e) {
184194
return null;
185195
}
186196
}
187197
return null;
188198
}
189199

190-
static String getxPath(String name, int zoom) {
200+
@SuppressWarnings("restriction")
201+
static ElementAtZoom<String> getxPath(String name, int zoom) {
191202
Matcher matcher = XPATH_PATTERN.matcher(name);
192203
if (matcher.find()) {
193204
try {
@@ -197,24 +208,30 @@ static String getxPath(String name, int zoom) {
197208
int desiredHeight = Math.round((zoom / 100f) * currentHeight);
198209
String lead = name.substring(0, matcher.start(1));
199210
String tail = name.substring(matcher.end(2));
200-
return lead + desiredWidth + "x" + desiredHeight + tail; //$NON-NLS-1$
211+
String xPath = lead + desiredWidth + "x" + desiredHeight + tail; //$NON-NLS-1$
212+
return new ElementAtZoom<>(xPath, desiredHeight);
201213
} catch (RuntimeException e) {
202214
// should never happen but if then we can't use the alternative name...
203215
}
204216
}
205217
return null;
206218
}
207219

208-
static String getxName(String name, int zoom) {
220+
@SuppressWarnings("restriction")
221+
static ElementAtZoom<String> getxName(String name, int zoom) {
209222
int dot = name.lastIndexOf('.');
210223
if (dot != -1 && (zoom == 150 || zoom == 200)) {
211224
String lead = name.substring(0, dot);
212225
String tail = name.substring(dot);
213226
if (InternalPolicy.DEBUG_LOAD_URL_IMAGE_DESCRIPTOR_2x_PNG_FOR_GIF && ".gif".equalsIgnoreCase(tail)) { //$NON-NLS-1$
214227
tail = ".png"; //$NON-NLS-1$
215228
}
216-
String x = zoom == 150 ? "@1.5x" : "@2x"; //$NON-NLS-1$ //$NON-NLS-2$
217-
return lead + x + tail;
229+
String x = "@2x";//$NON-NLS-1$
230+
if (zoom == 150) {
231+
x = "@1.5x"; //$NON-NLS-1$
232+
return new ElementAtZoom<>(lead + x + tail, 150);
233+
}
234+
return new ElementAtZoom<>(lead + x + tail, 200);
218235
}
219236
return null;
220237
}

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

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
import org.eclipse.swt.graphics.ImageData;
4040
import org.eclipse.swt.graphics.ImageDataProvider;
4141
import org.eclipse.swt.graphics.ImageFileNameProvider;
42+
import org.eclipse.swt.graphics.ImageLoader;
43+
import org.eclipse.swt.internal.DPIUtil.ElementAtZoom;
44+
import org.eclipse.swt.internal.NativeImageLoader;
4245

4346
/**
4447
* An ImageDescriptor that gets its information from a URL. This class is not
@@ -55,6 +58,7 @@ public URLImageFileNameProvider(String url) {
5558
this.url = url;
5659
}
5760

61+
@SuppressWarnings("restriction")
5862
@Override
5963
public String getImagePath(int zoom) {
6064
URL tempURL = getURL(url);
@@ -63,16 +67,16 @@ public String getImagePath(int zoom) {
6367
if (zoom == 100) {
6468
return getFilePath(tempURL, logIOException);
6569
}
66-
URL xUrl = getxURL(tempURL, zoom);
70+
ElementAtZoom<URL> xUrl = getxURL(tempURL, zoom);
6771
if (xUrl != null) {
68-
String xResult = getFilePath(xUrl, logIOException);
72+
String xResult = getFilePath(xUrl.element(), logIOException);
6973
if (xResult != null) {
7074
return xResult;
7175
}
7276
}
73-
String xpath = FileImageDescriptor.getxPath(url, zoom);
77+
ElementAtZoom<String> xpath = FileImageDescriptor.getxPath(url, zoom);
7478
if (xpath != null) {
75-
URL xPathUrl = getURL(xpath);
79+
URL xPathUrl = getURL(xpath.element());
7680
if (xPathUrl != null) {
7781
return getFilePath(xPathUrl, logIOException);
7882
}
@@ -128,43 +132,49 @@ public boolean equals(Object o) {
128132
@Deprecated
129133
@Override
130134
public ImageData getImageData() {
131-
return getImageData(getURL(url));
135+
// TODO What to do with this? targetZoom and fileZoom is unknown.
136+
// the first can be extracted from the url but the latter?
137+
return getImageData(getURL(url), 100, 100);
132138
}
133139

134140
@Override
135141
public ImageData getImageData(int zoom) {
136142
return getImageData(url, zoom);
137143
}
138144

145+
@SuppressWarnings("restriction")
139146
private static ImageData getImageData(String url, int zoom) {
140147
URL tempURL = getURL(url);
141148
if (tempURL != null) {
142149
if (zoom == 100) {
143-
return getImageData(tempURL);
150+
return getImageData(tempURL, 100, zoom);
144151
}
145-
URL xUrl = getxURL(tempURL, zoom);
152+
ElementAtZoom<URL> xUrl = getxURL(tempURL, zoom);
146153
if (xUrl != null) {
147-
ImageData xdata = getImageData(xUrl);
154+
ImageData xdata = getImageData(xUrl.element(), xUrl.zoom(), zoom);
148155
if (xdata != null) {
149156
return xdata;
150157
}
151158
}
152-
String xpath = FileImageDescriptor.getxPath(url, zoom);
159+
ElementAtZoom<String> xpath = FileImageDescriptor.getxPath(url, zoom);
153160
if (xpath != null) {
154-
URL xPathUrl = getURL(xpath);
161+
URL xPathUrl = getURL(xpath.element());
155162
if (xPathUrl != null) {
156-
return getImageData(xPathUrl);
163+
return getImageData(xPathUrl, xpath.zoom(), zoom);
157164
}
158165
}
159166
}
160167
return null;
161168
}
162169

163-
private static ImageData getImageData(URL url) {
170+
@SuppressWarnings("restriction")
171+
private static ImageData getImageData(URL url, int fileZoom, int targetZoom) {
164172
ImageData result = null;
165173
try (InputStream in = getStream(url)) {
166174
if (in != null) {
167-
result = new ImageData(in);
175+
result = NativeImageLoader
176+
.load(new ElementAtZoom<>(in, fileZoom), new ImageLoader(), targetZoom).get(0)
177+
.element();
168178
}
169179
} catch (SWTException e) {
170180
if (e.code != SWT.ERROR_INVALID_IMAGE) {
@@ -227,7 +237,8 @@ public String toString() {
227237
return "URLImageDescriptor(" + url + ")"; //$NON-NLS-1$ //$NON-NLS-2$
228238
}
229239

230-
private static URL getxURL(URL url, int zoom) {
240+
@SuppressWarnings("restriction")
241+
private static ElementAtZoom<URL> getxURL(URL url, int zoom) {
231242
String path = url.getPath();
232243
int dot = path.lastIndexOf('.');
233244
if (dot != -1 && (zoom == 150 || zoom == 200)) {
@@ -242,7 +253,10 @@ private static URL getxURL(URL url, int zoom) {
242253
if (url.getQuery() != null) {
243254
file += '?' + url.getQuery();
244255
}
245-
return new URL(url.getProtocol(), url.getHost(), url.getPort(), file);
256+
if (x == "@1.5x") { //$NON-NLS-1$
257+
return new ElementAtZoom<>(new URL(url.getProtocol(), url.getHost(), url.getPort(), file), 150);
258+
}
259+
return new ElementAtZoom<>(new URL(url.getProtocol(), url.getHost(), url.getPort(), file), 200);
246260
} catch (MalformedURLException e) {
247261
Policy.getLog().log(new Status(IStatus.ERROR, Policy.JFACE, e.getLocalizedMessage(), e));
248262
}

0 commit comments

Comments
 (0)