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 3e3e53f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
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(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;
}

/**
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 3e3e53f

Please sign in to comment.