Skip to content

Commit

Permalink
JSVGRasterizer now only accepts InputStream instead of byte array
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael5601 committed Dec 11, 2024
1 parent 3bc3621 commit 703913e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import org.eclipse.swt.graphics.SVGRasterizer;
import org.eclipse.swt.graphics.ImageData;
Expand Down Expand Up @@ -58,14 +57,12 @@ public static void intializeJSVGRasterizer() {
);

@Override
public ImageData rasterizeSVG(byte[] bytes, float scalingFactor) throws IOException {
public ImageData rasterizeSVG(InputStream stream, float scalingFactor) throws IOException {
if(svgLoader == null) {
svgLoader = new SVGLoader();
}
SVGDocument svgDocument = null;
try (InputStream stream = new ByteArrayInputStream(bytes)) {
svgDocument = svgLoader.load(stream, null, LoaderContext.createDefault());
}
svgDocument = svgLoader.load(stream, null, LoaderContext.createDefault());
if (svgDocument != null) {
FloatSize size = svgDocument.size();
double originalWidth = size.getWidth();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public interface SVGRasterizer {
* Rasterizes an SVG image from the provided byte array, using the specified
* zoom factor.
*
* @param bytes the SVG image as a byte array.
* @param stream the SVG image as an {@link InputStream}.
* @param scalingFactor the scaling ratio e.g. 2.0 for doubled size.
* @return the {@link ImageData} for 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 ImageData rasterizeSVG(byte[] bytes, float scalingFactor) throws IOException;
public ImageData rasterizeSVG(InputStream stream, float scalingFactor) throws IOException;

/**
* Determines whether the given {@link InputStream} contains a SVG file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,12 @@ public ImageData[] load(InputStream stream, int zoom) {
try (InputStream imageStream = new ByteArrayInputStream(bytes)) {
if (rasterizer.isSVGFile(imageStream)) {
float scalingFactor = zoom / 100.0f;
ImageData rasterizedData = rasterizer.rasterizeSVG(bytes, scalingFactor);
if (rasterizedData != null) {
data = new ImageData[]{rasterizedData};
return data;
try (InputStream svgFileStream = new ByteArrayInputStream(bytes)) {
ImageData rasterizedData = rasterizer.rasterizeSVG(svgFileStream, scalingFactor);
if (rasterizedData != null) {
data = new ImageData[]{rasterizedData};
return data;
}
}
}
} catch (IOException e) {
Expand Down

0 comments on commit 703913e

Please sign in to comment.