Skip to content

Commit e40e2a8

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 e40e2a8

File tree

17 files changed

+625
-35
lines changed

17 files changed

+625
-35
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+
}

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/ImageLoader.java

Lines changed: 101 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
package org.eclipse.swt.graphics;
1515

1616

17+
import java.awt.image.BufferedImage;
1718
import java.io.*;
1819
import java.util.*;
1920

21+
import javax.imageio.ImageIO;
22+
2023
import org.eclipse.swt.*;
2124
import org.eclipse.swt.internal.image.*;
2225

@@ -149,10 +152,72 @@ void reset() {
149152
* </ul>
150153
*/
151154
public ImageData[] load(InputStream stream) {
155+
return loadDefault(stream);
156+
}
157+
158+
private ImageData[] loadDefault(InputStream stream) {
152159
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
153160
reset();
154161
data = FileFormat.load(stream, this);
155-
return data;
162+
return data;
163+
}
164+
165+
/**
166+
* Loads an array of <code>ImageData</code> objects from the
167+
* specified input stream. If the stream is a SVG File and zoom is not 0,
168+
* this method will try to rasterize the SVG.
169+
* Throws an error if either an error occurs while loading the images, or if the images are not
170+
* of a supported type. Returns the loaded image data array.
171+
*
172+
* @param stream the input stream to load the images from
173+
* @param zoom the zoom factor to apply when rasterizing a SVG.
174+
* A value of 0 means that the standard method for loading should be used.
175+
* @return an array of <code>ImageData</code> objects loaded from the specified input stream
176+
*
177+
* @exception IllegalArgumentException <ul>
178+
* <li>ERROR_NULL_ARGUMENT - if the stream is null</li>
179+
* </ul>
180+
* @exception SWTException <ul>
181+
* <li>ERROR_IO - if an IO error occurs while reading from the stream</li>
182+
* <li>ERROR_INVALID_IMAGE - if the image stream contains invalid data</li>
183+
* <li>ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format</li>
184+
* </ul>
185+
*
186+
* @since 3.129
187+
*/
188+
public ImageData[] load(InputStream stream, int zoom) {
189+
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
190+
reset();
191+
byte[] bytes = null;
192+
try {
193+
bytes = stream.readAllBytes();
194+
} catch (IOException e) {
195+
SWT.error(SWT.ERROR_IO, e);
196+
}
197+
ISVGRasterizer rasterizer = SVGRasterizerRegistry.getRasterizer();
198+
if (rasterizer != null && zoom != 0) {
199+
try {
200+
float scalingFactor = zoom / 100.0f;
201+
BufferedImage image = rasterizer.rasterizeSVG(bytes, scalingFactor);
202+
if(image != null) {
203+
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
204+
ImageIO.write(image, "png", baos);
205+
try (InputStream in = new ByteArrayInputStream(baos.toByteArray())) {
206+
data = FileFormat.load(in, this);
207+
return data;
208+
}
209+
}
210+
}
211+
} catch (IOException e) {
212+
// try standard method
213+
}
214+
}
215+
try (InputStream fallbackStream = new ByteArrayInputStream(bytes)) {
216+
return loadDefault(fallbackStream);
217+
} catch (IOException e) {
218+
SWT.error(SWT.ERROR_IO, e);
219+
}
220+
return null;
156221
}
157222

158223
/**
@@ -175,18 +240,43 @@ public ImageData[] load(InputStream stream) {
175240
*/
176241
public ImageData[] load(String filename) {
177242
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
178-
InputStream stream = null;
179-
try {
180-
stream = new FileInputStream(filename);
181-
return load(stream);
243+
try (InputStream stream = new FileInputStream(filename)) {
244+
return loadDefault(stream);
245+
} catch (IOException e) {
246+
SWT.error(SWT.ERROR_IO, e);
247+
}
248+
return null;
249+
}
250+
251+
/**
252+
* Loads an array of <code>ImageData</code> objects from the
253+
* file with the specified name. If the filename is a SVG File and zoom is not 0,
254+
* this method will try to rasterize the SVG. Throws an error if either
255+
* an error occurs while loading the images, or if the images are
256+
* not of a supported type. Returns the loaded image data array.
257+
*
258+
* @param filename the name of the file to load the images from
259+
* @param zoom the zoom factor to apply when rasterizing a SVG.
260+
* A value of 0 means that the standard method for loading should be used.
261+
* @return an array of <code>ImageData</code> objects loaded from the specified file
262+
*
263+
* @exception IllegalArgumentException <ul>
264+
* <li>ERROR_NULL_ARGUMENT - if the file name is null</li>
265+
* </ul>
266+
* @exception SWTException <ul>
267+
* <li>ERROR_IO - if an IO error occurs while reading from the file</li>
268+
* <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li>
269+
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
270+
* </ul>
271+
*
272+
* @since 3.129
273+
*/
274+
public ImageData[] load(String filename, int zoom) {
275+
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
276+
try (InputStream stream = new FileInputStream(filename)) {
277+
return load(stream, zoom);
182278
} catch (IOException e) {
183279
SWT.error(SWT.ERROR_IO, e);
184-
} finally {
185-
try {
186-
if (stream != null) stream.close();
187-
} catch (IOException e) {
188-
// Ignore error
189-
}
190280
}
191281
return null;
192282
}
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+
}

0 commit comments

Comments
 (0)