Skip to content

Commit

Permalink
move SVG-File check out of JSVGRasterizer and adjust check
Browse files Browse the repository at this point in the history
The caller needs to make sure the method is called with an SVG file.
The check now only checks the first byte.
  • Loading branch information
Michael5601 committed Dec 11, 2024
1 parent 9648549 commit 3bc3621
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,22 @@ public ImageData rasterizeSVG(byte[] bytes, float scalingFactor) throws IOExcept
svgLoader = new SVGLoader();
}
SVGDocument svgDocument = null;
if (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 convertToSWT(image);
}
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 convertToSWT(image);
}
return null;
}
Expand Down Expand Up @@ -155,16 +153,11 @@ else if (bufferedImage.getColorModel() instanceof ComponentColorModel) {
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");
}

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);
if (inputStream == null) {
throw new IllegalArgumentException("InputStream cannot be null");
}
int firstByte = inputStream.read();
return firstByte == '<';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,18 @@ public ImageData[] load(InputStream stream, int zoom) {
}
SVGRasterizer rasterizer = SVGRasterizerRegistry.getRasterizer();
if (rasterizer != null && zoom != 0) {
float scalingFactor = zoom / 100.0f;
try {
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;
}
} catch (IOException e) {
//ignore.
}
} catch (IOException e) {
//ignore.
}
}
try (InputStream fallbackStream = new ByteArrayInputStream(bytes)) {
return loadDefault(fallbackStream);
Expand Down

0 comments on commit 3bc3621

Please sign in to comment.