Skip to content

Commit 3cb61c5

Browse files
committed
move SVG-File check out of JSVGRasterizer
The caller needs to make sure the method is called with an SVG file.
1 parent 9648549 commit 3cb61c5

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java

+16-18
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,22 @@ public ImageData rasterizeSVG(byte[] bytes, float scalingFactor) throws IOExcept
6363
svgLoader = new SVGLoader();
6464
}
6565
SVGDocument svgDocument = null;
66-
if (isSVGFile(bytes)) {
67-
try (InputStream stream = new ByteArrayInputStream(bytes)) {
68-
svgDocument = svgLoader.load(stream, null, LoaderContext.createDefault());
69-
}
70-
if (svgDocument != null) {
71-
FloatSize size = svgDocument.size();
72-
double originalWidth = size.getWidth();
73-
double originalHeight = size.getHeight();
74-
int scaledWidth = (int) Math.round(originalWidth * scalingFactor);
75-
int scaledHeight = (int) Math.round(originalHeight * scalingFactor);
76-
BufferedImage image = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
77-
Graphics2D g = image.createGraphics();
78-
g.setRenderingHints(RENDERING_HINTS);
79-
g.scale(scalingFactor, scalingFactor);
80-
svgDocument.render(null, g);
81-
g.dispose();
82-
return convertToSWT(image);
83-
}
66+
try (InputStream stream = new ByteArrayInputStream(bytes)) {
67+
svgDocument = svgLoader.load(stream, null, LoaderContext.createDefault());
68+
}
69+
if (svgDocument != null) {
70+
FloatSize size = svgDocument.size();
71+
double originalWidth = size.getWidth();
72+
double originalHeight = size.getHeight();
73+
int scaledWidth = (int) Math.round(originalWidth * scalingFactor);
74+
int scaledHeight = (int) Math.round(originalHeight * scalingFactor);
75+
BufferedImage image = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
76+
Graphics2D g = image.createGraphics();
77+
g.setRenderingHints(RENDERING_HINTS);
78+
g.scale(scalingFactor, scalingFactor);
79+
svgDocument.render(null, g);
80+
g.dispose();
81+
return convertToSWT(image);
8482
}
8583
return null;
8684
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,18 @@ public ImageData[] load(InputStream stream, int zoom) {
186186
}
187187
SVGRasterizer rasterizer = SVGRasterizerRegistry.getRasterizer();
188188
if (rasterizer != null && zoom != 0) {
189-
float scalingFactor = zoom / 100.0f;
190-
try {
189+
try (InputStream imageStream = new ByteArrayInputStream(bytes)) {
190+
if (rasterizer.isSVGFile(imageStream)) {
191+
float scalingFactor = zoom / 100.0f;
191192
ImageData rasterizedData = rasterizer.rasterizeSVG(bytes, scalingFactor);
192193
if (rasterizedData != null) {
193194
data = new ImageData[]{rasterizedData};
194195
return data;
195196
}
196-
} catch (IOException e) {
197-
//ignore.
198197
}
198+
} catch (IOException e) {
199+
//ignore.
200+
}
199201
}
200202
try (InputStream fallbackStream = new ByteArrayInputStream(bytes)) {
201203
return loadDefault(fallbackStream);

0 commit comments

Comments
 (0)