Skip to content

Commit 3bc3621

Browse files
committed
move SVG-File check out of JSVGRasterizer and adjust check
The caller needs to make sure the method is called with an SVG file. The check now only checks the first byte.
1 parent 9648549 commit 3bc3621

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

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

Lines changed: 21 additions & 28 deletions
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
}
@@ -155,16 +153,11 @@ else if (bufferedImage.getColorModel() instanceof ComponentColorModel) {
155153
return null;
156154
}
157155

158-
private boolean isSVGFile(byte[] data) throws IOException {
159-
String content = new String(data, 0, Math.min(data.length, 512), StandardCharsets.UTF_8);
160-
return content.contains("<svg");
161-
}
162-
163156
public boolean isSVGFile(InputStream inputStream) throws IOException {
164-
if (inputStream == null) {
165-
throw new IllegalArgumentException("InputStream cannot be null");
166-
}
167-
byte[] data = inputStream.readNBytes(512);
168-
return isSVGFile(data);
157+
if (inputStream == null) {
158+
throw new IllegalArgumentException("InputStream cannot be null");
159+
}
160+
int firstByte = inputStream.read();
161+
return firstByte == '<';
169162
}
170163
}

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

Lines changed: 6 additions & 4 deletions
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)