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.
- Loading branch information
1 parent
2ce8542
commit c79c7a8
Showing
14 changed files
with
328 additions
and
12 deletions.
There are no files selected for viewing
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,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"/> | ||
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="lib" path="libs/jsvg-1.6.1.jar"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
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,28 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>org.eclipse.swt.svg</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.pde.ManifestBuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.pde.SchemaBuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.pde.PluginNature</nature> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
2 changes: 2 additions & 0 deletions
2
bundles/org.eclipse.swt.svg/.settings/org.eclipse.core.resources.prefs
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,2 @@ | ||
eclipse.preferences.version=1 | ||
encoding/<project>=UTF-8 |
9 changes: 9 additions & 0 deletions
9
bundles/org.eclipse.swt.svg/.settings/org.eclipse.jdt.core.prefs
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,9 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 | ||
org.eclipse.jdt.core.compiler.compliance=17 | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning | ||
org.eclipse.jdt.core.compiler.release=enabled | ||
org.eclipse.jdt.core.compiler.source=17 |
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,10 @@ | ||
Manifest-Version: 1.0 | ||
Bundle-ManifestVersion: 2 | ||
Bundle-Name: SvgPlugin | ||
Bundle-SymbolicName: org.eclipse.swt.svg | ||
Bundle-Version: 1.0.0.qualifier | ||
Automatic-Module-Name: org.eclipse.swt.svgPlugin | ||
Bundle-RequiredExecutionEnvironment: JavaSE-17 | ||
Export-Package: org.eclipse.swt.svg | ||
Import-Package: org.eclipse.swt.graphics | ||
Bundle-ClassPath: ., libs/jsvg-1.6.1.jar |
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,5 @@ | ||
source.. = src/ | ||
output.. = bin/ | ||
bin.includes = META-INF/,\ | ||
.,\ | ||
libs/jsvg-1.6.1.jar |
Binary file not shown.
79 changes: 79 additions & 0 deletions
79
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,79 @@ | ||
package org.eclipse.swt.svg; | ||
|
||
import static java.awt.RenderingHints.*; | ||
|
||
import java.awt.*; | ||
import java.awt.image.*; | ||
import java.io.*; | ||
import java.nio.charset.*; | ||
import java.util.*; | ||
import org.eclipse.swt.graphics.ISVGRasterizer; | ||
import org.eclipse.swt.graphics.SVGRasterizerRegistry; | ||
|
||
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 3.128 | ||
*/ | ||
public class SVGRasterizer implements ISVGRasterizer { | ||
|
||
/** | ||
* 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<Object, 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, int zoom) throws IOException { | ||
SVGLoader loader = new SVGLoader(); | ||
SVGDocument svgDocument = null; | ||
if (this.isSVGFile(bytes)) { | ||
try (InputStream stream = new ByteArrayInputStream(bytes)) { | ||
svgDocument = loader.load(stream, null, LoaderContext.createDefault()); | ||
} | ||
if (svgDocument != null) { | ||
double scalingFactor = zoom / 100.0; | ||
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; | ||
} | ||
|
||
private boolean isSVGFile(byte[] data) throws IOException { | ||
String content = new String(data, 0, Math.min(data.length, 512), StandardCharsets.UTF_8); | ||
return content.contains("<svg"); | ||
} | ||
|
||
@Override | ||
public boolean isSVGFile(InputStream inputStream) throws IOException { | ||
if (inputStream == null) { | ||
throw new IllegalArgumentException("InputStream cannot be null"); | ||
} | ||
byte[] data = inputStream.readNBytes(512); | ||
return isSVGFile(data); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
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,34 @@ | ||
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 zoom the zoom factor. | ||
* @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, int zoom) throws IOException; | ||
|
||
/** | ||
* 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 boolean isSVGFile(InputStream inputStream) throws IOException; | ||
} |
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
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
38 changes: 38 additions & 0 deletions
38
...es/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/SVGRasterizerRegistry.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,38 @@ | ||
package org.eclipse.swt.graphics; | ||
|
||
/** | ||
* A registry for managing the instance of an {@link ISVGRasterizer} implementation. | ||
* This allows for the registration and retrieval of a single rasterizer instance. | ||
* | ||
* @since 3.129 | ||
*/ | ||
public class SVGRasterizerRegistry { | ||
|
||
/** | ||
* The instance of the registered {@link ISVGRasterizer}. | ||
*/ | ||
private static ISVGRasterizer rasterizer; | ||
|
||
/** | ||
* Registers the provided implementation of {@link ISVGRasterizer}. | ||
* If a rasterizer has already been registered, subsequent calls to this method | ||
* will have no effect. | ||
* | ||
* @param implementation the {@link ISVGRasterizer} implementation to register. | ||
*/ | ||
public static void register(ISVGRasterizer implementation) { | ||
if (rasterizer == null) { | ||
rasterizer = implementation; | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves the currently registered {@link ISVGRasterizer} implementation. | ||
* | ||
* @return the registered {@link ISVGRasterizer}, or {@code null} if no implementation | ||
* has been registered. | ||
*/ | ||
public static ISVGRasterizer getRasterizer() { | ||
return rasterizer; | ||
} | ||
} |
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
Oops, something went wrong.