Skip to content

Commit

Permalink
move SVG-File check out of JSVGRasterizer
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.
  • Loading branch information
Michael5601 committed Dec 11, 2024
1 parent 9648549 commit 3cb61c5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 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
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 3cb61c5

Please sign in to comment.