forked from eclipse-platform/eclipse.platform.swt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature Proposal: Rasterization of SVGs at Runtime for Eclipse Icons Fixes eclipse-platform#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. Introduce SVG rasterizer logic
- Loading branch information
1 parent
703913e
commit 9f2b198
Showing
4 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/SVGRasterizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.eclipse.swt.svg; | ||
|
||
import static java.awt.RenderingHints.*; | ||
|
||
import java.awt.*; | ||
import java.awt.image.*; | ||
import java.io.*; | ||
import java.util.*; | ||
import org.eclipse.swt.graphics.ISVGRasterizer; | ||
import org.eclipse.swt.graphics.SVGRasterizerRegistry; | ||
import org.eclipse.swt.graphics.SVGUtil; | ||
|
||
import com.github.weisj.jsvg.*; | ||
import com.github.weisj.jsvg.geometry.size.*; | ||
import com.github.weisj.jsvg.parser.*; | ||
|
||
/** | ||
* A rasterizer implementation for converting SVG data into rasterized images. | ||
* This class implements the {@code ISVGRasterizer} interface. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
public class SVGRasterizer implements ISVGRasterizer { | ||
|
||
private SVGLoader svgLoader; | ||
|
||
/** | ||
* Initializes the SVG rasterizer by registering an instance of this rasterizer | ||
* with the {@link SVGRasterizerRegistry}. | ||
*/ | ||
public static void intializeSVGRasterizer() { | ||
SVGRasterizerRegistry.register(new SVGRasterizer()); | ||
} | ||
|
||
private final static Map<Key, Object> RENDERING_HINTS = Map.of(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON, // | ||
KEY_ALPHA_INTERPOLATION, VALUE_ALPHA_INTERPOLATION_QUALITY, // | ||
KEY_COLOR_RENDERING, VALUE_COLOR_RENDER_QUALITY, // | ||
KEY_DITHERING, VALUE_DITHER_DISABLE, // | ||
KEY_FRACTIONALMETRICS, VALUE_FRACTIONALMETRICS_ON, // | ||
KEY_INTERPOLATION, VALUE_INTERPOLATION_BICUBIC, // | ||
KEY_RENDERING, VALUE_RENDER_QUALITY, // | ||
KEY_STROKE_CONTROL, VALUE_STROKE_PURE, // | ||
KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_ON // | ||
); | ||
|
||
@Override | ||
public BufferedImage rasterizeSVG(byte[] bytes, float scalingFactor) throws IOException { | ||
if(svgLoader == null) { | ||
svgLoader = new SVGLoader(); | ||
} | ||
SVGDocument svgDocument = null; | ||
if (SVGUtil.isSVGFile(bytes)) { | ||
try (InputStream stream = new ByteArrayInputStream(bytes)) { | ||
svgDocument = svgLoader.load(stream, null, LoaderContext.createDefault()); | ||
} | ||
if (svgDocument != null) { | ||
FloatSize size = svgDocument.size(); | ||
double originalWidth = size.getWidth(); | ||
double originalHeight = size.getHeight(); | ||
int scaledWidth = (int) Math.round(originalWidth * scalingFactor); | ||
int scaledHeight = (int) Math.round(originalHeight * scalingFactor); | ||
BufferedImage image = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB); | ||
Graphics2D g = image.createGraphics(); | ||
g.setRenderingHints(RENDERING_HINTS); | ||
g.scale(scalingFactor, scalingFactor); | ||
svgDocument.render(null, g); | ||
g.dispose(); | ||
return image; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ISVGRasterizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.eclipse.swt.graphics; | ||
|
||
import java.awt.image.*; | ||
import java.io.*; | ||
|
||
/** | ||
* Defines the interface for an SVG rasterizer, responsible for converting SVG | ||
* data into rasterized images. | ||
* | ||
* @since 3.129 | ||
*/ | ||
public interface ISVGRasterizer { | ||
|
||
/** | ||
* Rasterizes an SVG image from the provided byte array, using the specified | ||
* zoom factor. | ||
* | ||
* @param bytes the SVG image as a byte array. | ||
* @param scalingFactor the scaling ratio e.g. 2.0 for doubled size. | ||
* @return a {@link BufferedImage} containing the rasterized image, or | ||
* {@code null} if the input is not a valid SVG file or cannot be | ||
* processed. | ||
* @throws IOException if an error occurs while reading the SVG data. | ||
*/ | ||
public BufferedImage rasterizeSVG(byte[] bytes, float scalingFactor) throws IOException; | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/SVGUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.eclipse.swt.graphics; | ||
|
||
import java.io.*; | ||
import java.nio.charset.*; | ||
|
||
/** | ||
* Utility class for handling SVG-related operations. | ||
* | ||
* @since 3.129 | ||
*/ | ||
public class SVGUtil { | ||
|
||
/** | ||
* Determines whether the given {@link InputStream} contains a SVG file. | ||
* | ||
* @param data byte array to check. | ||
* @return {@code true} if the input stream contains SVG content; {@code false} | ||
* otherwise. | ||
* @throws IOException if an error occurs while reading the stream. | ||
* @throws IllegalArgumentException if the input stream is {@code null}. | ||
*/ | ||
public static boolean isSVGFile(byte[] data) throws IOException { | ||
String content = new String(data, 0, Math.min(data.length, 512), StandardCharsets.UTF_8); | ||
return content.contains("<svg"); | ||
} | ||
|
||
/** | ||
* Determines whether the given {@link InputStream} contains a SVG file. | ||
* | ||
* @param inputStream the input stream to check. | ||
* @return {@code true} if the input stream contains SVG content; {@code false} | ||
* otherwise. | ||
* @throws IOException if an error occurs while reading the stream. | ||
* @throws IllegalArgumentException if the input stream is {@code null}. | ||
*/ | ||
public static boolean isSVGFile(InputStream inputStream) throws IOException { | ||
if (inputStream == null) { | ||
throw new IllegalArgumentException("InputStream cannot be null"); | ||
} | ||
byte[] data = inputStream.readNBytes(512); | ||
return isSVGFile(data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters