From fc3b77acf1b33fd7d15366adf996cc2acaeadbac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Wed, 22 Oct 2025 18:46:14 +0200 Subject: [PATCH] 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 --- .../jface/resource/URLHintProvider.java | 50 +++++++++++++++++++ .../jface/resource/URLImageDescriptor.java | 10 ++-- 2 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLHintProvider.java diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLHintProvider.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLHintProvider.java new file mode 100644 index 00000000000..daa087dfbc9 --- /dev/null +++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLHintProvider.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2025 Christoph Läubrich and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Christoph Läubrich - initial API and implementation + *******************************************************************************/ +package org.eclipse.jface.resource; + +import java.net.URL; +import java.util.function.Supplier; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.eclipse.swt.graphics.Point; + +class URLHintProvider implements Supplier { + + private static final Pattern QUERY_PATTERN = Pattern.compile("&size=(\\d+)x(\\d+)"); //$NON-NLS-1$ + private static final Pattern PATH_PATTERN = Pattern.compile("/(\\d+)x(\\d+)/"); //$NON-NLS-1$ + + private URL url; + + public URLHintProvider(URL url) { + this.url = url; + } + + @Override + public Point get() { + String query = url.getQuery(); + Matcher matcher; + if (query != null && !query.isEmpty()) { + matcher = QUERY_PATTERN.matcher("&" + query); //$NON-NLS-1$ + } else { + String path = url.getPath(); + matcher = PATH_PATTERN.matcher(path); + } + if (matcher.find()) { + return new Point(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(1))); + } + return null; + } + +} diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java index 646ca5cb90f..c03d6ec4bba 100644 --- a/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java +++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/resource/URLImageDescriptor.java @@ -25,6 +25,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.function.Function; +import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -42,6 +43,7 @@ import org.eclipse.swt.graphics.ImageDataProvider; import org.eclipse.swt.graphics.ImageFileNameProvider; import org.eclipse.swt.graphics.ImageLoader; +import org.eclipse.swt.graphics.Point; import org.eclipse.swt.internal.DPIUtil.ElementAtZoom; import org.eclipse.swt.internal.NativeImageLoader; import org.eclipse.swt.internal.image.FileFormat; @@ -133,7 +135,7 @@ private static R getZoomedImageSource(URL url, String urlString, int zoom, F private static ImageData getImageData(URL url, int fileZoom, int targetZoom) { try (InputStream in = getStream(url)) { if (in != null) { - return loadImageFromStream(new BufferedInputStream(in), fileZoom, targetZoom); + return loadImageFromStream(new BufferedInputStream(in), fileZoom, targetZoom, new URLHintProvider(url)); } } catch (SWTException e) { if (e.code != SWT.ERROR_INVALID_IMAGE) { @@ -147,8 +149,10 @@ private static ImageData getImageData(URL url, int fileZoom, int targetZoom) { } @SuppressWarnings("restriction") - private static ImageData loadImageFromStream(InputStream stream, int fileZoom, int targetZoom) { - return NativeImageLoader.load(new ElementAtZoom<>(stream, fileZoom), new ImageLoader(), targetZoom).get(0) + private static ImageData loadImageFromStream(InputStream stream, int fileZoom, int targetZoom, + Supplier hintProvider) { + return NativeImageLoader + .load(new ElementAtZoom<>(stream, fileZoom), new ImageLoader(), targetZoom, hintProvider).get(0) .element(); }