Skip to content

Commit 3612a63

Browse files
committed
Introduce SVG rasterizer logic
Feature Proposal: Rasterization of SVGs at Runtime for Eclipse Icons Fixes #1438 Eclipse currently loads icons exclusively as raster graphics (e.g., `.png`), without support for vector formats like `.svg`. A major drawback of raster graphics is their inability to scale without degrading image quality. Additionally, generating icons of different sizes requires manually rasterizing SVGs outside Eclipse, leading to unnecessary effort and many icon files. This PR introduces support for vector graphics in Eclipse, enabling SVGs to be used for icons. Existing PNG icons will continue to be loaded alongside SVGs, allowing the use of the new functionality without the need to replace all PNG files at once. --- - **How It Works**: - To use SVG icons, simply place the SVG file in the bundle and reference it in the `plugin.xml` and other necessary locations, as is done for PNGs. No additional configuration is required. - At runtime, Eclipse uses the library JSVG to rasterize the SVG into a raster image of the desired size, eliminating the need for scaling. My analysis shows that JSVG is the most suitable Java library for this purpose. - You need to write the flag `-Dswt.autoScale=quarter` into your `eclipse.ini` file or into the run arguments of a new configuration.
1 parent 2ce8542 commit 3612a63

File tree

15 files changed

+424
-11
lines changed

15 files changed

+424
-11
lines changed
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"/>
4+
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
5+
<classpathentry kind="src" path="src"/>
6+
<classpathentry kind="lib" path="libs/jsvg-1.6.1.jar"/>
7+
<classpathentry kind="output" path="bin"/>
8+
</classpath>

bundles/org.eclipse.swt.svg/.project

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>org.eclipse.swt.svg</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.pde.ManifestBuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.pde.SchemaBuilder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
</buildSpec>
24+
<natures>
25+
<nature>org.eclipse.pde.PluginNature</nature>
26+
<nature>org.eclipse.jdt.core.javanature</nature>
27+
</natures>
28+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/<project>=UTF-8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
3+
org.eclipse.jdt.core.compiler.compliance=17
4+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
5+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
6+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
7+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
8+
org.eclipse.jdt.core.compiler.release=enabled
9+
org.eclipse.jdt.core.compiler.source=17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Manifest-Version: 1.0
2+
Bundle-ManifestVersion: 2
3+
Bundle-Name: SvgPlugin
4+
Bundle-SymbolicName: org.eclipse.swt.svg
5+
Bundle-Version: 1.0.0.qualifier
6+
Automatic-Module-Name: org.eclipse.swt.svgPlugin
7+
Bundle-RequiredExecutionEnvironment: JavaSE-17
8+
Export-Package: org.eclipse.swt.svg
9+
Import-Package: org.eclipse.swt.graphics
10+
Bundle-ClassPath: ., libs/jsvg-1.6.1.jar
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
source.. = src/
2+
output.. = bin/
3+
bin.includes = META-INF/,\
4+
.,\
5+
libs/jsvg-1.6.1.jar
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.eclipse.swt.svg;
2+
3+
import static java.awt.RenderingHints.*;
4+
5+
import java.awt.*;
6+
import java.awt.image.*;
7+
import java.io.*;
8+
import java.util.*;
9+
import org.eclipse.swt.graphics.ISVGRasterizer;
10+
import org.eclipse.swt.graphics.SVGRasterizerRegistry;
11+
import org.eclipse.swt.graphics.SVGUtil;
12+
13+
import com.github.weisj.jsvg.*;
14+
import com.github.weisj.jsvg.geometry.size.*;
15+
import com.github.weisj.jsvg.parser.*;
16+
17+
/**
18+
* A rasterizer implementation for converting SVG data into rasterized images.
19+
* This class implements the {@code ISVGRasterizer} interface.
20+
*
21+
* @since 1.0.0
22+
*/
23+
public class SVGRasterizer implements ISVGRasterizer {
24+
25+
private SVGLoader svgLoader;
26+
27+
/**
28+
* Initializes the SVG rasterizer by registering an instance of this rasterizer
29+
* with the {@link SVGRasterizerRegistry}.
30+
*/
31+
public static void intializeSVGRasterizer() {
32+
SVGRasterizerRegistry.register(new SVGRasterizer());
33+
}
34+
35+
private final static Map<Key, Object> RENDERING_HINTS = Map.of(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON, //
36+
KEY_ALPHA_INTERPOLATION, VALUE_ALPHA_INTERPOLATION_QUALITY, //
37+
KEY_COLOR_RENDERING, VALUE_COLOR_RENDER_QUALITY, //
38+
KEY_DITHERING, VALUE_DITHER_DISABLE, //
39+
KEY_FRACTIONALMETRICS, VALUE_FRACTIONALMETRICS_ON, //
40+
KEY_INTERPOLATION, VALUE_INTERPOLATION_BICUBIC, //
41+
KEY_RENDERING, VALUE_RENDER_QUALITY, //
42+
KEY_STROKE_CONTROL, VALUE_STROKE_PURE, //
43+
KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_ON //
44+
);
45+
46+
@Override
47+
public BufferedImage rasterizeSVG(byte[] bytes, float scalingFactor) throws IOException {
48+
if(svgLoader == null) {
49+
svgLoader = new SVGLoader();
50+
}
51+
SVGDocument svgDocument = null;
52+
if (SVGUtil.isSVGFile(bytes)) {
53+
try (InputStream stream = new ByteArrayInputStream(bytes)) {
54+
svgDocument = svgLoader.load(stream, null, LoaderContext.createDefault());
55+
}
56+
if (svgDocument != null) {
57+
FloatSize size = svgDocument.size();
58+
double originalWidth = size.getWidth();
59+
double originalHeight = size.getHeight();
60+
int scaledWidth = (int) Math.round(originalWidth * scalingFactor);
61+
int scaledHeight = (int) Math.round(originalHeight * scalingFactor);
62+
BufferedImage image = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
63+
Graphics2D g = image.createGraphics();
64+
g.setRenderingHints(RENDERING_HINTS);
65+
g.scale(scalingFactor, scalingFactor);
66+
svgDocument.render(null, g);
67+
g.dispose();
68+
return image;
69+
}
70+
}
71+
return null;
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.eclipse.swt.graphics;
2+
3+
import java.awt.image.*;
4+
import java.io.*;
5+
6+
/**
7+
* Defines the interface for an SVG rasterizer, responsible for converting SVG
8+
* data into rasterized images.
9+
*
10+
* @since 3.129
11+
*/
12+
public interface ISVGRasterizer {
13+
14+
/**
15+
* Rasterizes an SVG image from the provided byte array, using the specified
16+
* zoom factor.
17+
*
18+
* @param bytes the SVG image as a byte array.
19+
* @param scalingFactor the scaling ratio e.g. 2.0 for doubled size.
20+
* @return a {@link BufferedImage} containing the rasterized image, or
21+
* {@code null} if the input is not a valid SVG file or cannot be
22+
* processed.
23+
* @throws IOException if an error occurs while reading the SVG data.
24+
*/
25+
public BufferedImage rasterizeSVG(byte[] bytes, float scalingFactor) throws IOException;
26+
27+
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageData.java

+63-2
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,39 @@ scanlinePad, checkData(data), 0, null,
331331
* @see ImageLoader#load(InputStream)
332332
*/
333333
public ImageData(InputStream stream) {
334-
ImageData[] data = ImageDataLoader.load(stream);
334+
this(stream, 0);
335+
}
336+
337+
/**
338+
* Constructs an <code>ImageData</code> loaded from the specified
339+
* input stream. Throws an error if an error occurs while loading
340+
* the image, or if the image has an unsupported type. Application
341+
* code is still responsible for closing the input stream.
342+
* <p>
343+
* This constructor is provided for convenience when loading a single
344+
* image only. If the stream contains multiple images, only the first
345+
* one will be loaded. To load multiple images, use
346+
* <code>ImageLoader.load()</code>.
347+
* </p>
348+
*
349+
* @param stream the input stream to load the image from (must not be null)
350+
* @param zoom the zoom factor to apply when rasterizing a SVG.
351+
* A value of 0 means that the standard method for loading should be used.
352+
*
353+
* @exception IllegalArgumentException <ul>
354+
* <li>ERROR_NULL_ARGUMENT - if the stream is null</li>
355+
* </ul>
356+
* @exception SWTException <ul>
357+
* <li>ERROR_IO - if an IO error occurs while reading from the stream</li>
358+
* <li>ERROR_INVALID_IMAGE - if the image stream contains invalid data</li>
359+
* <li>ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format</li>
360+
* </ul>
361+
*
362+
* @see ImageLoader#load(InputStream)
363+
* @since 3.129
364+
*/
365+
public ImageData(InputStream stream, int zoom) {
366+
ImageData[] data = ImageDataLoader.load(stream, zoom);
335367
if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
336368
ImageData i = data[0];
337369
setAllFields(
@@ -377,7 +409,36 @@ public ImageData(InputStream stream) {
377409
* </ul>
378410
*/
379411
public ImageData(String filename) {
380-
ImageData[] data = ImageDataLoader.load(filename);
412+
this(filename, 0);
413+
}
414+
415+
/**
416+
* Constructs an <code>ImageData</code> loaded from a file with the
417+
* specified name. Throws an error if an error occurs loading the
418+
* image, or if the image has an unsupported type.
419+
* <p>
420+
* This constructor is provided for convenience when loading a single
421+
* image only. If the file contains multiple images, only the first
422+
* one will be loaded. To load multiple images, use
423+
* <code>ImageLoader.load()</code>.
424+
* </p>
425+
*
426+
* @param filename the name of the file to load the image from (must not be null)
427+
* @param zoom the zoom factor to apply when rasterizing a SVG.
428+
* A value of 0 means that the standard method for loading should be used.
429+
* @exception IllegalArgumentException <ul>
430+
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
431+
* </ul>
432+
* @exception SWTException <ul>
433+
* <li>ERROR_IO - if an IO error occurs while reading from the file</li>
434+
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li>
435+
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
436+
* </ul>
437+
*
438+
* @since 3.129
439+
*/
440+
public ImageData(String filename, int zoom) {
441+
ImageData[] data = ImageDataLoader.load(filename, zoom);
381442
if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
382443
ImageData i = data[0];
383444
setAllFields(

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageDataLoader.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,16 @@ public static ImageData[] load(InputStream stream) {
2525
return new ImageLoader().load(stream);
2626
}
2727

28-
public static ImageData[] load(String filename) {
28+
public static ImageData[] load(InputStream stream, int zoom) {
29+
return new ImageLoader().load(stream, zoom);
30+
}
31+
32+
public static ImageData[] load(String filename) {
2933
return new ImageLoader().load(filename);
3034
}
3135

36+
public static ImageData[] load(String filename, int zoom) {
37+
return new ImageLoader().load(filename, zoom);
38+
}
39+
3240
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.eclipse.swt.graphics;
2+
3+
/**
4+
* A registry for managing the instance of an {@link ISVGRasterizer} implementation.
5+
* This allows for the registration and retrieval of a single rasterizer instance.
6+
*
7+
* @since 3.129
8+
*/
9+
public class SVGRasterizerRegistry {
10+
11+
/**
12+
* The instance of the registered {@link ISVGRasterizer}.
13+
*/
14+
private static ISVGRasterizer rasterizer;
15+
16+
/**
17+
* Registers the provided implementation of {@link ISVGRasterizer}.
18+
* If a rasterizer has already been registered, subsequent calls to this method
19+
* will have no effect.
20+
*
21+
* @param implementation the {@link ISVGRasterizer} implementation to register.
22+
*/
23+
public static void register(ISVGRasterizer implementation) {
24+
if (rasterizer == null) {
25+
rasterizer = implementation;
26+
}
27+
}
28+
29+
/**
30+
* Retrieves the currently registered {@link ISVGRasterizer} implementation.
31+
*
32+
* @return the registered {@link ISVGRasterizer}, or {@code null} if no implementation
33+
* has been registered.
34+
*/
35+
public static ISVGRasterizer getRasterizer() {
36+
return rasterizer;
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.eclipse.swt.graphics;
2+
3+
import java.io.*;
4+
import java.nio.charset.*;
5+
6+
/**
7+
* Utility class for handling SVG-related operations.
8+
*
9+
* @since 3.129
10+
*/
11+
public class SVGUtil {
12+
13+
/**
14+
* Determines whether the given {@link InputStream} contains a SVG file.
15+
*
16+
* @param data byte array to check.
17+
* @return {@code true} if the input stream contains SVG content; {@code false}
18+
* otherwise.
19+
* @throws IOException if an error occurs while reading the stream.
20+
* @throws IllegalArgumentException if the input stream is {@code null}.
21+
*/
22+
public static boolean isSVGFile(byte[] data) throws IOException {
23+
String content = new String(data, 0, Math.min(data.length, 512), StandardCharsets.UTF_8);
24+
return content.contains("<svg");
25+
}
26+
27+
/**
28+
* Determines whether the given {@link InputStream} contains a SVG file.
29+
*
30+
* @param inputStream the input stream to check.
31+
* @return {@code true} if the input stream contains SVG content; {@code false}
32+
* otherwise.
33+
* @throws IOException if an error occurs while reading the stream.
34+
* @throws IllegalArgumentException if the input stream is {@code null}.
35+
*/
36+
public static boolean isSVGFile(InputStream inputStream) throws IOException {
37+
if (inputStream == null) {
38+
throw new IllegalArgumentException("InputStream cannot be null");
39+
}
40+
byte[] data = inputStream.readNBytes(512);
41+
return isSVGFile(data);
42+
}
43+
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ public Image(Device device, ImageData source, ImageData mask) {
468468
public Image (Device device, InputStream stream) {
469469
super(device);
470470
initialNativeZoom = DPIUtil.getNativeDeviceZoom();
471-
ImageData data = DPIUtil.autoScaleUp(device, new ElementAtZoom<>(new ImageData (stream), 100));
471+
ImageData data = DPIUtil.autoScaleUp(device, new ElementAtZoom<>(new ImageData (stream, getZoom()), 100));
472472
init(data, getZoom());
473473
init();
474474
this.device.registerResourceWithZoomSupport(this);
@@ -510,7 +510,7 @@ public Image (Device device, String filename) {
510510
super(device);
511511
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
512512
initialNativeZoom = DPIUtil.getNativeDeviceZoom();
513-
ImageData data = DPIUtil.autoScaleUp(device, new ElementAtZoom<>(new ImageData (filename), 100));
513+
ImageData data = DPIUtil.autoScaleUp(device, new ElementAtZoom<>(new ImageData (filename, getZoom()), 100));
514514
init(data, getZoom());
515515
init();
516516
this.device.registerResourceWithZoomSupport(this);
@@ -553,10 +553,10 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
553553
if (fileName.zoom() == getZoom()) {
554554
ImageHandle imageMetadata = initNative (fileName.element(), getZoom());
555555
if (imageMetadata == null) {
556-
init(new ImageData (fileName.element()), getZoom());
556+
init(new ImageData (fileName.element(), getZoom()), getZoom());
557557
}
558558
} else {
559-
ImageData resizedData = DPIUtil.autoScaleImageData (device, new ImageData (fileName.element()), fileName.zoom());
559+
ImageData resizedData = DPIUtil.autoScaleImageData (device, new ImageData (fileName.element(), getZoom()), fileName.zoom());
560560
init(resizedData, getZoom());
561561
}
562562
init();
@@ -753,7 +753,7 @@ private ImageHandle getImageMetadata(int zoom) {
753753

754754
if (imageFileNameProvider != null) {
755755
ElementAtZoom<String> imageCandidate = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, zoom);
756-
ImageData imageData = new ImageData (imageCandidate.element());
756+
ImageData imageData = new ImageData (imageCandidate.element(), zoom);
757757
if (imageCandidate.zoom() == zoom) {
758758
/* Release current native resources */
759759
ImageHandle imageMetadata = initNative(imageCandidate.element(), zoom);
@@ -1389,7 +1389,7 @@ public ImageData getImageData (int zoom) {
13891389
return DPIUtil.scaleImageData (device, data.element(), zoom, data.zoom());
13901390
} else if (imageFileNameProvider != null) {
13911391
ElementAtZoom<String> fileName = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, zoom);
1392-
return DPIUtil.scaleImageData (device, new ImageData (fileName.element()), zoom, fileName.zoom());
1392+
return DPIUtil.scaleImageData (device, new ImageData (fileName.element(), zoom), zoom, fileName.zoom());
13931393
}
13941394

13951395
// if a GC is initialized with an Image (memGC != null), the image data must not be resized, because it would

0 commit comments

Comments
 (0)