|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Vector Informatik GmbH and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the terms of the Eclipse |
| 5 | + * Public License 2.0 which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Michael Bangas (Vector Informatik GmbH) - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | +package org.eclipse.swt.svg; |
| 14 | + |
| 15 | +import static java.awt.RenderingHints.KEY_ALPHA_INTERPOLATION; |
| 16 | +import static java.awt.RenderingHints.KEY_ANTIALIASING; |
| 17 | +import static java.awt.RenderingHints.KEY_COLOR_RENDERING; |
| 18 | +import static java.awt.RenderingHints.KEY_DITHERING; |
| 19 | +import static java.awt.RenderingHints.KEY_FRACTIONALMETRICS; |
| 20 | +import static java.awt.RenderingHints.KEY_INTERPOLATION; |
| 21 | +import static java.awt.RenderingHints.KEY_RENDERING; |
| 22 | +import static java.awt.RenderingHints.KEY_STROKE_CONTROL; |
| 23 | +import static java.awt.RenderingHints.KEY_TEXT_ANTIALIASING; |
| 24 | +import static java.awt.RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY; |
| 25 | +import static java.awt.RenderingHints.VALUE_ANTIALIAS_ON; |
| 26 | +import static java.awt.RenderingHints.VALUE_COLOR_RENDER_QUALITY; |
| 27 | +import static java.awt.RenderingHints.VALUE_DITHER_DISABLE; |
| 28 | +import static java.awt.RenderingHints.VALUE_FRACTIONALMETRICS_ON; |
| 29 | +import static java.awt.RenderingHints.VALUE_INTERPOLATION_BICUBIC; |
| 30 | +import static java.awt.RenderingHints.VALUE_RENDER_QUALITY; |
| 31 | +import static java.awt.RenderingHints.VALUE_STROKE_PURE; |
| 32 | +import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_ON; |
| 33 | + |
| 34 | +import java.awt.Graphics2D; |
| 35 | +import java.awt.RenderingHints.Key; |
| 36 | +import java.awt.image.BufferedImage; |
| 37 | +import java.awt.image.DataBufferInt; |
| 38 | +import java.io.IOException; |
| 39 | +import java.io.InputStream; |
| 40 | +import java.util.Map; |
| 41 | + |
| 42 | +import org.eclipse.swt.SWT; |
| 43 | +import org.eclipse.swt.graphics.ImageData; |
| 44 | +import org.eclipse.swt.graphics.PaletteData; |
| 45 | +import org.eclipse.swt.internal.image.SVGRasterizer; |
| 46 | + |
| 47 | +import com.github.weisj.jsvg.SVGDocument; |
| 48 | +import com.github.weisj.jsvg.geometry.size.FloatSize; |
| 49 | +import com.github.weisj.jsvg.parser.LoaderContext; |
| 50 | +import com.github.weisj.jsvg.parser.SVGLoader; |
| 51 | + |
| 52 | +/** |
| 53 | + * A rasterizer implementation for converting SVG data into rasterized images. |
| 54 | + * This class uses the third party library JSVG for the raterization of SVG |
| 55 | + * images. |
| 56 | + */ |
| 57 | +public class JSVGRasterizer implements SVGRasterizer { |
| 58 | + |
| 59 | + private static final SVGLoader SVG_LOADER = new SVGLoader(); |
| 60 | + |
| 61 | + private final static Map<Key, Object> RENDERING_HINTS = Map.of( // |
| 62 | + KEY_ANTIALIASING, VALUE_ANTIALIAS_ON, // |
| 63 | + KEY_ALPHA_INTERPOLATION, VALUE_ALPHA_INTERPOLATION_QUALITY, // |
| 64 | + KEY_COLOR_RENDERING, VALUE_COLOR_RENDER_QUALITY, // |
| 65 | + KEY_DITHERING, VALUE_DITHER_DISABLE, // |
| 66 | + KEY_FRACTIONALMETRICS, VALUE_FRACTIONALMETRICS_ON, // |
| 67 | + KEY_INTERPOLATION, VALUE_INTERPOLATION_BICUBIC, // |
| 68 | + KEY_RENDERING, VALUE_RENDER_QUALITY, // |
| 69 | + KEY_STROKE_CONTROL, VALUE_STROKE_PURE, // |
| 70 | + KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_ON // |
| 71 | + ); |
| 72 | + |
| 73 | + @Override |
| 74 | + public ImageData rasterizeSVG(InputStream inputStream, int zoom) throws IOException { |
| 75 | + SVGDocument svgDocument = loadSVG(inputStream); |
| 76 | + if (svgDocument != null) { |
| 77 | + return generateRasterizedImageData(svgDocument, zoom); |
| 78 | + } else { |
| 79 | + SWT.error(SWT.ERROR_INVALID_IMAGE); |
| 80 | + } |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + private SVGDocument loadSVG(InputStream inputStream) { |
| 85 | + return SVG_LOADER.load(inputStream, null, LoaderContext.createDefault()); |
| 86 | + } |
| 87 | + |
| 88 | + private ImageData generateRasterizedImageData(SVGDocument svgDocument, int zoom) { |
| 89 | + BufferedImage rasterizedImage = renderSVG(svgDocument, zoom); |
| 90 | + return convertToSWTImageData(rasterizedImage); |
| 91 | + } |
| 92 | + |
| 93 | + private BufferedImage renderSVG(SVGDocument svgDocument, int zoom) { |
| 94 | + float scalingFactor = zoom / 100.0f; |
| 95 | + BufferedImage image = createImageBase(svgDocument, scalingFactor); |
| 96 | + Graphics2D g = configureRenderingOptions(scalingFactor, image); |
| 97 | + svgDocument.render(null, g); |
| 98 | + g.dispose(); |
| 99 | + return image; |
| 100 | + } |
| 101 | + |
| 102 | + private BufferedImage createImageBase(SVGDocument svgDocument, float scalingFactor) { |
| 103 | + FloatSize sourceImageSize = svgDocument.size(); |
| 104 | + int targetImageWidth = calculateTargetWidth(scalingFactor, sourceImageSize); |
| 105 | + int targetImageHeight = calculateTargetHeight(scalingFactor, sourceImageSize); |
| 106 | + return new BufferedImage(targetImageWidth, targetImageHeight, BufferedImage.TYPE_INT_ARGB); |
| 107 | + } |
| 108 | + |
| 109 | + private int calculateTargetWidth(float scalingFactor, FloatSize sourceImageSize) { |
| 110 | + double sourceImageWidth = sourceImageSize.getWidth(); |
| 111 | + return (int) Math.round(sourceImageWidth * scalingFactor); |
| 112 | + } |
| 113 | + |
| 114 | + private int calculateTargetHeight(float scalingFactor, FloatSize sourceImageSize) { |
| 115 | + double sourceImageHeight = sourceImageSize.getHeight(); |
| 116 | + return (int) Math.round(sourceImageHeight * scalingFactor); |
| 117 | + } |
| 118 | + |
| 119 | + private Graphics2D configureRenderingOptions(float scalingFactor, BufferedImage image) { |
| 120 | + Graphics2D g = image.createGraphics(); |
| 121 | + g.setRenderingHints(RENDERING_HINTS); |
| 122 | + g.scale(scalingFactor, scalingFactor); |
| 123 | + return g; |
| 124 | + } |
| 125 | + |
| 126 | + private ImageData convertToSWTImageData(BufferedImage rasterizedImage) { |
| 127 | + int width = rasterizedImage.getWidth(); |
| 128 | + int height = rasterizedImage.getHeight(); |
| 129 | + int[] pixels = ((DataBufferInt) rasterizedImage.getRaster().getDataBuffer()).getData(); |
| 130 | + PaletteData paletteData = new PaletteData(0x00FF0000, 0x0000FF00, 0x000000FF); |
| 131 | + ImageData imageData = new ImageData(width, height, 32, paletteData); |
| 132 | + int index = 0; |
| 133 | + for (int y = 0; y < imageData.height; y++) { |
| 134 | + for (int x = 0; x < imageData.width; x++) { |
| 135 | + int alpha = (pixels[index] >> 24) & 0xFF; |
| 136 | + imageData.setAlpha(x, y, alpha); |
| 137 | + imageData.setPixel(x, y, pixels[index++]); |
| 138 | + } |
| 139 | + } |
| 140 | + return imageData; |
| 141 | + } |
| 142 | +} |
0 commit comments