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
Lines changed: 8 additions & 0 deletions
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

Lines changed: 28 additions & 0 deletions
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>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/<project>=UTF-8
Lines changed: 9 additions & 0 deletions
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
Lines changed: 10 additions & 0 deletions
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
Lines changed: 5 additions & 0 deletions
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.
Lines changed: 73 additions & 0 deletions
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+
}
Lines changed: 27 additions & 0 deletions
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

Lines changed: 63 additions & 2 deletions
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(

0 commit comments

Comments
 (0)