From 3e3e53ff037d1fcaecc4eb6f24fda8050b0af8a2 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 | 26 ++++++++------ .../eclipse/swt/graphics/ISVGRasterizer.java | 4 +-- .../org/eclipse/swt/graphics/ImageLoader.java | 36 ++++++++++++++----- 3 files changed, 44 insertions(+), 22 deletions(-) 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 44c4b7d04c..0c300381ff 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 @@ -18,26 +18,31 @@ * 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)) { @@ -45,7 +50,6 @@ public BufferedImage rasterizeSVG(byte[] bytes, int zoom) throws IOException { 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(); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ISVGRasterizer.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ISVGRasterizer.java index 0e8e1cc9f8..e9e99b2e2f 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ISVGRasterizer.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ISVGRasterizer.java @@ -15,12 +15,12 @@ public interface ISVGRasterizer { * Rasterizes an SVG image from the provided byte array, using the specified zoom factor. * * @param bytes the SVG image as a byte array. - * @param zoom the zoom factor. + * @param scalingFactor the scaling factor e.g. 2.0 for doubled size. * @return a {@link BufferedImage} containing the rasterized image, or {@code null} if the input * is not a valid SVG file or cannot be processed. * @throws IOException if an error occurs while reading the SVG data. */ - public BufferedImage rasterizeSVG(byte[] bytes, int zoom) throws IOException; + public BufferedImage rasterizeSVG(byte[] bytes, float scalingFactor) throws IOException; /** * Determines whether the given {@link InputStream} contains a SVG file. diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java index 000301c1f1..8afa5b2a6d 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/ImageLoader.java @@ -152,7 +152,7 @@ void reset() { * */ 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(stream); + } 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,