Skip to content

Commit

Permalink
improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael5601 committed Dec 6, 2024
1 parent 1efaf36 commit 5bbddf5
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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<Object, Object> 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<Key, Object> 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();
Expand All @@ -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("<svg");
}

@Override
public boolean isSVGFile(InputStream inputStream) throws IOException {
if (inputStream == null) {
throw new IllegalArgumentException("InputStream cannot be null");
}
byte[] data = inputStream.readNBytes(512);
return isSVGFile(data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,24 @@
import java.io.*;

/**
* Defines the interface for an SVG rasterizer, responsible for converting
* SVG data into rasterized images.
* Defines the interface for an SVG rasterizer, responsible for converting SVG
* data into rasterized images.
*
* @since 3.129
*/
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.
* @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;
* Rasterizes an SVG image from the provided byte array, using the specified
* zoom factor.
*
* @param bytes the SVG image as a byte array.
* @param scalingFactor the scaling ratio 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, float scalingFactor) throws IOException;

/**
* Determines whether the given {@link InputStream} contains a SVG file.
*
* @param inputStream the input stream to check.
* @return {@code true} if the input stream contains SVG content; {@code false} otherwise.
* @throws IOException if an error occurs while reading the stream.
* @throws IllegalArgumentException if the input stream is {@code null}.
*/
public boolean isSVGFile(InputStream inputStream) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.eclipse.swt.graphics;

import java.io.*;
import java.nio.charset.*;

/**
* Utility class for handling SVG-related operations.
*
* @since 3.129
*/
public class SVGUtil {

/**
* Determines whether the given {@link InputStream} contains a SVG file.
*
* @param data byte array to check.
* @return {@code true} if the input stream contains SVG content; {@code false}
* otherwise.
* @throws IOException if an error occurs while reading the stream.
* @throws IllegalArgumentException if the input stream is {@code null}.
*/
public static boolean isSVGFile(byte[] data) throws IOException {
String content = new String(data, 0, Math.min(data.length, 512), StandardCharsets.UTF_8);
return content.contains("<svg");
}

/**
* Determines whether the given {@link InputStream} contains a SVG file.
*
* @param inputStream the input stream to check.
* @return {@code true} if the input stream contains SVG content; {@code false}
* otherwise.
* @throws IOException if an error occurs while reading the stream.
* @throws IllegalArgumentException if the input stream is {@code null}.
*/
public static boolean isSVGFile(InputStream inputStream) throws IOException {
if (inputStream == null) {
throw new IllegalArgumentException("InputStream cannot be null");
}
byte[] data = inputStream.readNBytes(512);
return isSVGFile(data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void reset() {
* </ul>
*/
public ImageData[] load(InputStream stream) {
return load(stream, 0);
return loadDefault(stream);
}

/**
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

/**
Expand All @@ -231,8 +242,15 @@ public ImageData[] load(InputStream stream, int zoom) {
* </ul>
*/
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 <code>ImageData</code> objects from the
* file with the specified name. If the filename is a SVG File and zoom is not 0,
Expand Down

0 comments on commit 5bbddf5

Please sign in to comment.