Skip to content

Commit fc3b77a

Browse files
committed
Detect the desired size from the path or Query parameters.
Currently we have the problem that I want to replace some icons with a SVG variant. Currently Icons can be organized in folders like /icons/16x16, /icons/32x32 and so on for high resolution support. Currently if one want to replace such structure with SVG it is required to scale down the SVG to 16x16 pixel document size as otherwise they get rendered at there native size (what usually is much larger). As it is not really desirable to restrict the size of the SVG design for technical reasons, JFace now can detect two cases: 1) the SVG is places in a folder with "classic" folder layout the size is extracted and passed down as a hint for dynamic sizable icons 2) one can additionally add a query parameter, e.g. if I have an icon like /icons/obj16/search.svg and I have two places where I want to use it one for a toolbar (16x16) and once for a Wizard Images (usually 128x128) I can use the url bundle:/example.id/icons/obj16/search.svg?size=16x16 and bundle:/example.id/icons/obj16/search.svg?size=128x128 to accomplish this task without the need to even store two SVGs
1 parent 34e3f47 commit fc3b77a

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Christoph Läubrich and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Christoph Läubrich - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.jface.resource;
15+
16+
import java.net.URL;
17+
import java.util.function.Supplier;
18+
import java.util.regex.Matcher;
19+
import java.util.regex.Pattern;
20+
21+
import org.eclipse.swt.graphics.Point;
22+
23+
class URLHintProvider implements Supplier<Point> {
24+
25+
private static final Pattern QUERY_PATTERN = Pattern.compile("&size=(\\d+)x(\\d+)"); //$NON-NLS-1$
26+
private static final Pattern PATH_PATTERN = Pattern.compile("/(\\d+)x(\\d+)/"); //$NON-NLS-1$
27+
28+
private URL url;
29+
30+
public URLHintProvider(URL url) {
31+
this.url = url;
32+
}
33+
34+
@Override
35+
public Point get() {
36+
String query = url.getQuery();
37+
Matcher matcher;
38+
if (query != null && !query.isEmpty()) {
39+
matcher = QUERY_PATTERN.matcher("&" + query); //$NON-NLS-1$
40+
} else {
41+
String path = url.getPath();
42+
matcher = PATH_PATTERN.matcher(path);
43+
}
44+
if (matcher.find()) {
45+
return new Point(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(1)));
46+
}
47+
return null;
48+
}
49+
50+
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.net.MalformedURLException;
2626
import java.net.URL;
2727
import java.util.function.Function;
28+
import java.util.function.Supplier;
2829
import java.util.regex.Matcher;
2930
import java.util.regex.Pattern;
3031

@@ -42,6 +43,7 @@
4243
import org.eclipse.swt.graphics.ImageDataProvider;
4344
import org.eclipse.swt.graphics.ImageFileNameProvider;
4445
import org.eclipse.swt.graphics.ImageLoader;
46+
import org.eclipse.swt.graphics.Point;
4547
import org.eclipse.swt.internal.DPIUtil.ElementAtZoom;
4648
import org.eclipse.swt.internal.NativeImageLoader;
4749
import org.eclipse.swt.internal.image.FileFormat;
@@ -133,7 +135,7 @@ private static <R> R getZoomedImageSource(URL url, String urlString, int zoom, F
133135
private static ImageData getImageData(URL url, int fileZoom, int targetZoom) {
134136
try (InputStream in = getStream(url)) {
135137
if (in != null) {
136-
return loadImageFromStream(new BufferedInputStream(in), fileZoom, targetZoom);
138+
return loadImageFromStream(new BufferedInputStream(in), fileZoom, targetZoom, new URLHintProvider(url));
137139
}
138140
} catch (SWTException e) {
139141
if (e.code != SWT.ERROR_INVALID_IMAGE) {
@@ -147,8 +149,10 @@ private static ImageData getImageData(URL url, int fileZoom, int targetZoom) {
147149
}
148150

149151
@SuppressWarnings("restriction")
150-
private static ImageData loadImageFromStream(InputStream stream, int fileZoom, int targetZoom) {
151-
return NativeImageLoader.load(new ElementAtZoom<>(stream, fileZoom), new ImageLoader(), targetZoom).get(0)
152+
private static ImageData loadImageFromStream(InputStream stream, int fileZoom, int targetZoom,
153+
Supplier<Point> hintProvider) {
154+
return NativeImageLoader
155+
.load(new ElementAtZoom<>(stream, fileZoom), new ImageLoader(), targetZoom, hintProvider).get(0)
152156
.element();
153157
}
154158

0 commit comments

Comments
 (0)