diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/FileFormat.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/FileFormat.java index dfd5e26519..79e7c7eb6f 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/FileFormat.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/FileFormat.java @@ -116,6 +116,9 @@ ImageData loadFromByteStreamBySize(int width, int height) { * device independent image array represented by the stream. */ public List> loadFromStream(LEDataInputStream stream, int fileZoom, int targetZoom) { + return loadFromStream(stream, fileZoom, targetZoom, null); +} +public List> loadFromStream(LEDataInputStream stream, int fileZoom, int targetZoom, Supplier sizeHintSupplier) { try { inputStream = stream; return loadFromByteStream(fileZoom, targetZoom); @@ -148,13 +151,16 @@ public ImageData loadFromStreamBySize(LEDataInputStream stream, int width, int h * return the device independent image array represented by the stream. */ public static List> load(ElementAtZoom is, ImageLoader loader, int targetZoom) { + return load(is, loader, targetZoom, null); +} +public static List> load(ElementAtZoom is, ImageLoader loader, int targetZoom, Supplier sizeHintSupplier) { LEDataInputStream stream = new LEDataInputStream(is.element()); FileFormat fileFormat = determineFileFormat(stream).orElseGet(() -> { SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT); return null; }); fileFormat.loader = loader; - return fileFormat.loadFromStream(stream, is.zoom(), targetZoom); + return fileFormat.loadFromStream(stream, is.zoom(), targetZoom, sizeHintSupplier); } public static ImageData load(InputStream is, ImageLoader loader, int width, int height) { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/SVGFileFormat.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/SVGFileFormat.java index b985d5cead..7d54ee428e 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/SVGFileFormat.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/SVGFileFormat.java @@ -15,6 +15,7 @@ import java.io.*; import java.nio.charset.*; import java.util.*; +import java.util.function.*; import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; @@ -55,6 +56,29 @@ List> loadFromByteStream(int fileZoom, int targetZoom) return List.of(new ElementAtZoom<>(rasterizedImageData, targetZoom)); } + @Override + public List> loadFromStream(LEDataInputStream stream, int fileZoom, int targetZoom, + Supplier sizeHintSupplier) { + if (RASTERIZER == null) { + SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT, null, " [No SVG rasterizer found]"); + } + if (targetZoom <= 0) { + SWT.error(SWT.ERROR_INVALID_ARGUMENT, null, " [Cannot rasterize SVG for zoom <= 0]"); + } + if (sizeHintSupplier != null) { + Point point = sizeHintSupplier.get(); + if (point != null) { + double factor = targetZoom / 100d; + int w = (int) (factor * point.x); + int h = (int) (factor * point.y); + ImageData imageData = RASTERIZER.rasterizeSVG(inputStream, w, h); + return List.of(new ElementAtZoom<>(imageData, targetZoom)); + } + } + ImageData rasterizedImageData = RASTERIZER.rasterizeSVG(inputStream, 100 * targetZoom / fileZoom); + return List.of(new ElementAtZoom<>(rasterizedImageData, targetZoom)); + } + @Override ImageData loadFromByteStreamBySize(int width, int height) { if (RASTERIZER == null) { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/internal/NativeImageLoader.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/internal/NativeImageLoader.java index 0aa8a25210..44b31097fd 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/internal/NativeImageLoader.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/internal/NativeImageLoader.java @@ -18,6 +18,7 @@ import java.io.*; import java.util.*; import java.util.List; +import java.util.function.*; import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; @@ -34,6 +35,10 @@ public class NativeImageLoader { // --- loading --- public static List> load(ElementAtZoom streamAtZoom, ImageLoader imageLoader, int targetZoom) { + return load(streamAtZoom, imageLoader, targetZoom, null); + } + + public static List> load(ElementAtZoom streamAtZoom, ImageLoader imageLoader, int targetZoom, Supplier sizeHintSupplier) { // 1) Load InputStream into byte array byte[] data_buffer; InputStream stream = streamAtZoom.element(); @@ -53,7 +58,7 @@ public static List> load(ElementAtZoom str } catch (IOException e) { SWT.error(SWT.ERROR_IO); } - return FileFormat.load(new ElementAtZoom<>(stream2, streamAtZoom.zoom()), imageLoader, targetZoom); + return FileFormat.load(new ElementAtZoom<>(stream2, streamAtZoom.zoom()), imageLoader, targetZoom, sizeHintSupplier); } List imgDataList = new ArrayList<>(); long loader = GDK.gdk_pixbuf_loader_new();