From 5bbddf5d400bbb0c4350348667840971d9e8cf2e Mon Sep 17 00:00:00 2001 From: Michael Bangas Date: Fri, 6 Dec 2024 09:59:44 +0100 Subject: [PATCH] improve code --- .../org/eclipse/swt/svg/SVGRasterizer.java | 44 +++++++------------ .../eclipse/swt/graphics/ISVGRasterizer.java | 33 ++++++-------- .../org/eclipse/swt/graphics/SVGUtil.java | 43 ++++++++++++++++++ .../org/eclipse/swt/graphics/ImageLoader.java | 36 +++++++++++---- 4 files changed, 100 insertions(+), 56 deletions(-) create mode 100644 bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/SVGUtil.java diff --git a/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/SVGRasterizer.java b/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/SVGRasterizer.java index 44c4b7d04c6..555bc02a6d3 100644 --- a/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/SVGRasterizer.java +++ b/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/SVGRasterizer.java @@ -5,10 +5,10 @@ import java.awt.*; import java.awt.image.*; import java.io.*; -import java.nio.charset.*; import java.util.*; import org.eclipse.swt.graphics.ISVGRasterizer; import org.eclipse.swt.graphics.SVGRasterizerRegistry; +import org.eclipse.swt.graphics.SVGUtil; import com.github.weisj.jsvg.*; import com.github.weisj.jsvg.geometry.size.*; @@ -18,34 +18,38 @@ * A rasterizer implementation for converting SVG data into rasterized images. * This class implements the {@code ISVGRasterizer} interface. * - * @since 3.128 + * @since 1.0.0 */ public class SVGRasterizer implements ISVGRasterizer { /** - * Initializes the SVG rasterizer by registering an instance of this rasterizer - * with the {@link SVGRasterizerRegistry}. - */ + * Initializes the SVG rasterizer by registering an instance of this rasterizer + * with the {@link SVGRasterizerRegistry}. + */ public static void intializeSVGRasterizer() { SVGRasterizerRegistry.register(new SVGRasterizer()); } - private final static Map RENDERING_HINTS = Map.of(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON, - KEY_ALPHA_INTERPOLATION, VALUE_ALPHA_INTERPOLATION_QUALITY, KEY_COLOR_RENDERING, VALUE_COLOR_RENDER_QUALITY, - KEY_DITHERING, VALUE_DITHER_DISABLE, KEY_FRACTIONALMETRICS, VALUE_FRACTIONALMETRICS_ON, KEY_INTERPOLATION, - VALUE_INTERPOLATION_BICUBIC, KEY_RENDERING, VALUE_RENDER_QUALITY, KEY_STROKE_CONTROL, VALUE_STROKE_PURE, - KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_ON); + private final static Map RENDERING_HINTS = Map.of(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON, // + KEY_ALPHA_INTERPOLATION, VALUE_ALPHA_INTERPOLATION_QUALITY, // + KEY_COLOR_RENDERING, VALUE_COLOR_RENDER_QUALITY, // + KEY_DITHERING, VALUE_DITHER_DISABLE, // + KEY_FRACTIONALMETRICS, VALUE_FRACTIONALMETRICS_ON, // + KEY_INTERPOLATION, VALUE_INTERPOLATION_BICUBIC, // + KEY_RENDERING, VALUE_RENDER_QUALITY, // + KEY_STROKE_CONTROL, VALUE_STROKE_PURE, // + KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_ON // + ); @Override - public BufferedImage rasterizeSVG(byte[] bytes, int zoom) throws IOException { + public BufferedImage rasterizeSVG(byte[] bytes, float scalingFactor) throws IOException { SVGLoader loader = new SVGLoader(); SVGDocument svgDocument = null; - if (this.isSVGFile(bytes)) { + if (SVGUtil.isSVGFile(bytes)) { try (InputStream stream = new ByteArrayInputStream(bytes)) { svgDocument = loader.load(stream, null, LoaderContext.createDefault()); } if (svgDocument != null) { - double scalingFactor = zoom / 100.0; FloatSize size = svgDocument.size(); double originalWidth = size.getWidth(); double originalHeight = size.getHeight(); @@ -62,18 +66,4 @@ public BufferedImage rasterizeSVG(byte[] bytes, int zoom) throws IOException { } 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(" */ public ImageData[] load(InputStream stream) { - return load(stream, 0); + return loadDefault(stream); } /** @@ -190,7 +190,8 @@ public ImageData[] load(InputStream stream, int zoom) { ISVGRasterizer rasterizer = SVGRasterizerRegistry.getRasterizer(); if (rasterizer != null && zoom != 0) { try { - BufferedImage image = rasterizer.rasterizeSVG(bytes, zoom); + float scalingFactor = zoom / 100.0f; + BufferedImage image = rasterizer.rasterizeSVG(bytes, scalingFactor); if(image != null) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { ImageIO.write(image, "png", baos); @@ -204,12 +205,22 @@ public ImageData[] load(InputStream stream, int zoom) { // try standard method } } - try (InputStream fallbackStream = new ByteArrayInputStream(bytes)) { - data = FileFormat.load(fallbackStream, this); - } catch (IOException e) { - SWT.error(SWT.ERROR_IO, e); - } - return data; + try (InputStream fallbackStream = new ByteArrayInputStream(bytes)) { + return loadDefault(fallbackStream); + } catch (IOException e) { + SWT.error(SWT.ERROR_IO, e); + } + return null; +} + +/** + * @since 3.129 + */ +public ImageData[] loadDefault(InputStream stream) { + if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + reset(); + data = FileFormat.load(stream, this); + return data; } /** @@ -231,8 +242,15 @@ public ImageData[] load(InputStream stream, int zoom) { * */ public ImageData[] load(String filename) { - return load(filename, 0); + if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + try (InputStream stream = new FileInputStream(filename)) { + return loadDefault(stream); + } catch (IOException e) { + SWT.error(SWT.ERROR_IO, e); + } + return null; } + /** * Loads an array of ImageData objects from the * file with the specified name. If the filename is a SVG File and zoom is not 0,