|
| 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: Michael Bangas (Vector Informatik GmbH) - initial API and implementation |
| 11 | + *******************************************************************************/ |
| 12 | +package org.eclipse.swt.svg; |
| 13 | + |
| 14 | +import static java.awt.RenderingHints.*; |
| 15 | + |
| 16 | +import java.awt.*; |
| 17 | +import java.awt.image.*; |
| 18 | +import java.io.*; |
| 19 | +import java.util.*; |
| 20 | + |
| 21 | +import javax.xml.parsers.DocumentBuilder; |
| 22 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 23 | +import javax.xml.parsers.ParserConfigurationException; |
| 24 | +import javax.xml.transform.Transformer; |
| 25 | +import javax.xml.transform.TransformerException; |
| 26 | +import javax.xml.transform.TransformerFactory; |
| 27 | +import javax.xml.transform.dom.DOMSource; |
| 28 | +import javax.xml.transform.stream.StreamResult; |
| 29 | + |
| 30 | +import org.eclipse.swt.graphics.SVGRasterizer; |
| 31 | +import org.eclipse.swt.graphics.ImageData; |
| 32 | +import org.eclipse.swt.graphics.PaletteData; |
| 33 | +import org.eclipse.swt.graphics.RGB; |
| 34 | +import org.w3c.dom.Document; |
| 35 | +import org.w3c.dom.Element; |
| 36 | +import org.xml.sax.SAXException; |
| 37 | + |
| 38 | +import com.github.weisj.jsvg.*; |
| 39 | +import com.github.weisj.jsvg.geometry.size.*; |
| 40 | +import com.github.weisj.jsvg.parser.*; |
| 41 | + |
| 42 | +/** |
| 43 | + * A rasterizer implementation for converting SVG data into rasterized images. |
| 44 | + * This class implements the {@code ISVGRasterizer} interface. |
| 45 | + * |
| 46 | + * @since 1.0.0 |
| 47 | + */ |
| 48 | +public class JSVGRasterizer implements SVGRasterizer { |
| 49 | + |
| 50 | + private SVGLoader svgLoader; |
| 51 | + |
| 52 | + private final static Map<Key, Object> RENDERING_HINTS = Map.of(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON, // |
| 53 | + KEY_ALPHA_INTERPOLATION, VALUE_ALPHA_INTERPOLATION_QUALITY, // |
| 54 | + KEY_COLOR_RENDERING, VALUE_COLOR_RENDER_QUALITY, // |
| 55 | + KEY_DITHERING, VALUE_DITHER_DISABLE, // |
| 56 | + KEY_FRACTIONALMETRICS, VALUE_FRACTIONALMETRICS_ON, // |
| 57 | + KEY_INTERPOLATION, VALUE_INTERPOLATION_BICUBIC, // |
| 58 | + KEY_RENDERING, VALUE_RENDER_QUALITY, // |
| 59 | + KEY_STROKE_CONTROL, VALUE_STROKE_PURE, // |
| 60 | + KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_ON // |
| 61 | + ); |
| 62 | + |
| 63 | + @Override |
| 64 | + public ImageData rasterizeSVG(InputStream stream, float scalingFactor) throws IOException { |
| 65 | + if (stream == null) { |
| 66 | + throw new IllegalArgumentException("InputStream cannot be null"); |
| 67 | + } |
| 68 | + if(svgLoader == null) { |
| 69 | + svgLoader = new SVGLoader(); |
| 70 | + } |
| 71 | + return rasterize(stream, scalingFactor); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public ImageData rasterizeDisabledSVG(InputStream stream, float scalingFactor) throws IOException { |
| 76 | + if(svgLoader == null) { |
| 77 | + svgLoader = new SVGLoader(); |
| 78 | + } |
| 79 | + InputStream disabledStream = applyDisabledLook(stream); |
| 80 | + return rasterize(disabledStream, scalingFactor); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public ImageData rasterizeGraySVG(InputStream stream, float scalingFactor) throws IOException { |
| 85 | + if(svgLoader == null) { |
| 86 | + svgLoader = new SVGLoader(); |
| 87 | + } |
| 88 | + InputStream disabledStream = applyGrayLook(stream); |
| 89 | + return rasterize(disabledStream, scalingFactor); |
| 90 | + } |
| 91 | + |
| 92 | + private ImageData rasterize(InputStream stream, float scalingFactor) throws IOException { |
| 93 | + SVGDocument svgDocument = null; |
| 94 | + stream.mark(Integer.MAX_VALUE); |
| 95 | + InputStream nonClosingStream = new FilterInputStream(stream) { |
| 96 | + @Override |
| 97 | + public void close() throws IOException { |
| 98 | + // Do nothing to prevent closing the underlying stream |
| 99 | + } |
| 100 | + }; |
| 101 | + svgDocument = svgLoader.load(nonClosingStream, null, LoaderContext.createDefault()); |
| 102 | + stream.reset(); |
| 103 | + if (svgDocument != null) { |
| 104 | + FloatSize size = svgDocument.size(); |
| 105 | + double originalWidth = size.getWidth(); |
| 106 | + double originalHeight = size.getHeight(); |
| 107 | + int scaledWidth = (int) Math.round(originalWidth * scalingFactor); |
| 108 | + int scaledHeight = (int) Math.round(originalHeight * scalingFactor); |
| 109 | + BufferedImage image = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB); |
| 110 | + Graphics2D g = image.createGraphics(); |
| 111 | + g.setRenderingHints(RENDERING_HINTS); |
| 112 | + g.scale(scalingFactor, scalingFactor); |
| 113 | + svgDocument.render(null, g); |
| 114 | + g.dispose(); |
| 115 | + return convertToSWT(image); |
| 116 | + } |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + private static InputStream applyDisabledLook(InputStream svgInputStream) throws IOException { |
| 121 | + Document svgDocument = parseSVG(svgInputStream); |
| 122 | + addDisabledFilter(svgDocument); |
| 123 | + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { |
| 124 | + writeSVG(svgDocument, outputStream); |
| 125 | + return new ByteArrayInputStream(outputStream.toByteArray()); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static InputStream applyGrayLook(InputStream svgInputStream) throws IOException { |
| 130 | + Document svgDocument = parseSVG(svgInputStream); |
| 131 | + addGrayFilter(svgDocument); |
| 132 | + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { |
| 133 | + writeSVG(svgDocument, outputStream); |
| 134 | + return new ByteArrayInputStream(outputStream.toByteArray()); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private static Document parseSVG(InputStream inputStream) throws IOException { |
| 139 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 140 | + DocumentBuilder builder; |
| 141 | + try { |
| 142 | + builder = factory.newDocumentBuilder(); |
| 143 | + return builder.parse(inputStream); |
| 144 | + } catch (SAXException | IOException | ParserConfigurationException e) { |
| 145 | + throw new IOException(e.getMessage()); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private static void addDisabledFilter(Document document) { |
| 150 | + addFilter(document, 0.64f, 0.4f); |
| 151 | + } |
| 152 | + |
| 153 | + private static void addGrayFilter(Document document) { |
| 154 | + addFilter(document, 0.64f, 0.1f); |
| 155 | + } |
| 156 | + |
| 157 | + private static void addFilter(Document document, float slope, float intercept) { |
| 158 | + Element defs = (Element) document.getElementsByTagName("defs").item(0); |
| 159 | + if (defs == null) { |
| 160 | + defs = document.createElement("defs"); |
| 161 | + document.getDocumentElement().appendChild(defs); |
| 162 | + } |
| 163 | + |
| 164 | + Element filter = document.createElement("filter"); |
| 165 | + filter.setAttribute("id", "customizedLook"); |
| 166 | + |
| 167 | + Element colorMatrix = document.createElement("feColorMatrix"); |
| 168 | + colorMatrix.setAttribute("type", "saturate"); |
| 169 | + colorMatrix.setAttribute("values", "0"); |
| 170 | + filter.appendChild(colorMatrix); |
| 171 | + |
| 172 | + Element componentTransfer = document.createElement("feComponentTransfer"); |
| 173 | + for (String channel : new String[] { "R", "G", "B" }) { |
| 174 | + Element func = document.createElement("feFunc" + channel); |
| 175 | + func.setAttribute("type", "linear"); |
| 176 | + func.setAttribute("slope", Float.toString(slope)); |
| 177 | + func.setAttribute("intercept", Float.toString(intercept)); |
| 178 | + componentTransfer.appendChild(func); |
| 179 | + } |
| 180 | + filter.appendChild(componentTransfer); |
| 181 | + defs.appendChild(filter); |
| 182 | + document.getDocumentElement().setAttribute("filter", "url(#customizedLook)"); |
| 183 | + } |
| 184 | + |
| 185 | + private static void writeSVG(Document document, OutputStream outputStream) throws IOException { |
| 186 | + TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
| 187 | + Transformer transformer; |
| 188 | + try { |
| 189 | + transformer = transformerFactory.newTransformer(); |
| 190 | + transformer.transform(new DOMSource(document), new StreamResult(outputStream)); |
| 191 | + } catch (TransformerException e) { |
| 192 | + throw new IOException(e.getMessage()); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + private ImageData convertToSWT(BufferedImage bufferedImage) { |
| 197 | + if (bufferedImage.getColorModel() instanceof DirectColorModel) { |
| 198 | + DirectColorModel colorModel = (DirectColorModel)bufferedImage.getColorModel(); |
| 199 | + PaletteData palette = new PaletteData( |
| 200 | + colorModel.getRedMask(), |
| 201 | + colorModel.getGreenMask(), |
| 202 | + colorModel.getBlueMask()); |
| 203 | + ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), |
| 204 | + colorModel.getPixelSize(), palette); |
| 205 | + for (int y = 0; y < data.height; y++) { |
| 206 | + for (int x = 0; x < data.width; x++) { |
| 207 | + int rgb = bufferedImage.getRGB(x, y); |
| 208 | + int pixel = palette.getPixel(new RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF)); |
| 209 | + data.setPixel(x, y, pixel); |
| 210 | + if (colorModel.hasAlpha()) { |
| 211 | + data.setAlpha(x, y, (rgb >> 24) & 0xFF); |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + return data; |
| 216 | + } |
| 217 | + else if (bufferedImage.getColorModel() instanceof IndexColorModel) { |
| 218 | + IndexColorModel colorModel = (IndexColorModel)bufferedImage.getColorModel(); |
| 219 | + int size = colorModel.getMapSize(); |
| 220 | + byte[] reds = new byte[size]; |
| 221 | + byte[] greens = new byte[size]; |
| 222 | + byte[] blues = new byte[size]; |
| 223 | + colorModel.getReds(reds); |
| 224 | + colorModel.getGreens(greens); |
| 225 | + colorModel.getBlues(blues); |
| 226 | + RGB[] rgbs = new RGB[size]; |
| 227 | + for (int i = 0; i < rgbs.length; i++) { |
| 228 | + rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF); |
| 229 | + } |
| 230 | + PaletteData palette = new PaletteData(rgbs); |
| 231 | + ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), |
| 232 | + colorModel.getPixelSize(), palette); |
| 233 | + data.transparentPixel = colorModel.getTransparentPixel(); |
| 234 | + WritableRaster raster = bufferedImage.getRaster(); |
| 235 | + int[] pixelArray = new int[1]; |
| 236 | + for (int y = 0; y < data.height; y++) { |
| 237 | + for (int x = 0; x < data.width; x++) { |
| 238 | + raster.getPixel(x, y, pixelArray); |
| 239 | + data.setPixel(x, y, pixelArray[0]); |
| 240 | + } |
| 241 | + } |
| 242 | + return data; |
| 243 | + } |
| 244 | + else if (bufferedImage.getColorModel() instanceof ComponentColorModel) { |
| 245 | + ComponentColorModel colorModel = (ComponentColorModel)bufferedImage.getColorModel(); |
| 246 | + //ASSUMES: 3 BYTE BGR IMAGE TYPE |
| 247 | + PaletteData palette = new PaletteData(0x0000FF, 0x00FF00,0xFF0000); |
| 248 | + ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), |
| 249 | + colorModel.getPixelSize(), palette); |
| 250 | + //This is valid because we are using a 3-byte Data model with no transparent pixels |
| 251 | + data.transparentPixel = -1; |
| 252 | + WritableRaster raster = bufferedImage.getRaster(); |
| 253 | + int[] pixelArray = new int[3]; |
| 254 | + for (int y = 0; y < data.height; y++) { |
| 255 | + for (int x = 0; x < data.width; x++) { |
| 256 | + raster.getPixel(x, y, pixelArray); |
| 257 | + int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2])); |
| 258 | + data.setPixel(x, y, pixel); |
| 259 | + } |
| 260 | + } |
| 261 | + return data; |
| 262 | + } |
| 263 | + return null; |
| 264 | + } |
| 265 | + |
| 266 | + public boolean isSVGFile(InputStream stream) throws IOException { |
| 267 | + if (stream == null) { |
| 268 | + throw new IllegalArgumentException("InputStream cannot be null"); |
| 269 | + } |
| 270 | + stream.mark(Integer.MAX_VALUE); |
| 271 | + try { |
| 272 | + int firstByte = stream.read(); |
| 273 | + return firstByte == '<'; |
| 274 | + } finally { |
| 275 | + stream.reset(); |
| 276 | + } |
| 277 | + } |
| 278 | +} |
0 commit comments