|
42 | 42 | import org.eclipse.swt.SWT;
|
43 | 43 | import org.eclipse.swt.graphics.ImageData;
|
44 | 44 | import org.eclipse.swt.graphics.PaletteData;
|
| 45 | +import org.eclipse.swt.graphics.RGB; |
45 | 46 | import org.eclipse.swt.internal.image.SVGRasterizer;
|
| 47 | +import javax.xml.parsers.DocumentBuilder; |
| 48 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 49 | +import javax.xml.parsers.ParserConfigurationException; |
| 50 | +import javax.xml.transform.Transformer; |
| 51 | +import javax.xml.transform.TransformerException; |
| 52 | +import javax.xml.transform.TransformerFactory; |
| 53 | +import javax.xml.transform.dom.DOMSource; |
| 54 | +import javax.xml.transform.stream.StreamResult; |
| 55 | +import org.w3c.dom.Document; |
| 56 | +import org.w3c.dom.Element; |
| 57 | +import org.xml.sax.SAXException; |
46 | 58 |
|
47 | 59 | import com.github.weisj.jsvg.SVGDocument;
|
48 | 60 | import com.github.weisj.jsvg.geometry.size.FloatSize;
|
@@ -73,7 +85,19 @@ public class JSVGRasterizer implements SVGRasterizer {
|
73 | 85 | );
|
74 | 86 |
|
75 | 87 | @Override
|
76 |
| - public ImageData[] rasterizeSVG(InputStream inputStream, int zoom) throws IOException { |
| 88 | + public ImageData[] rasterizeSVG(InputStream inputStream, int zoom, int flag) throws IOException { |
| 89 | + switch(flag) { |
| 90 | + case SWT.IMAGE_DISABLE: |
| 91 | + inputStream = applyDisabledLook(inputStream); |
| 92 | + break; |
| 93 | + case SWT.IMAGE_GRAY: |
| 94 | + inputStream = applyGrayLook(inputStream); |
| 95 | + break; |
| 96 | + case SWT.IMAGE_COPY: |
| 97 | + break; |
| 98 | + default: |
| 99 | + SWT.error(SWT.ERROR_INVALID_IMAGE); |
| 100 | + } |
77 | 101 | SVGDocument svgDocument = loadSVG(inputStream);
|
78 | 102 | if (svgDocument != null) {
|
79 | 103 | return generateRasterizedImageData(svgDocument, zoom);
|
@@ -141,4 +165,80 @@ private ImageData[] convertToSWTImageData(BufferedImage rasterizedImage) {
|
141 | 165 | }
|
142 | 166 | return new ImageData[]{imageData};
|
143 | 167 | }
|
| 168 | + |
| 169 | + private static InputStream applyDisabledLook(InputStream svgInputStream) throws IOException { |
| 170 | + Document svgDocument = parseSVG(svgInputStream); |
| 171 | + addDisabledFilter(svgDocument); |
| 172 | + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { |
| 173 | + writeSVG(svgDocument, outputStream); |
| 174 | + return new ByteArrayInputStream(outputStream.toByteArray()); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + private static InputStream applyGrayLook(InputStream svgInputStream) throws IOException { |
| 179 | + Document svgDocument = parseSVG(svgInputStream); |
| 180 | + addGrayFilter(svgDocument); |
| 181 | + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { |
| 182 | + writeSVG(svgDocument, outputStream); |
| 183 | + return new ByteArrayInputStream(outputStream.toByteArray()); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + private static Document parseSVG(InputStream inputStream) throws IOException { |
| 188 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 189 | + DocumentBuilder builder; |
| 190 | + try { |
| 191 | + builder = factory.newDocumentBuilder(); |
| 192 | + return builder.parse(inputStream); |
| 193 | + } catch (SAXException | IOException | ParserConfigurationException e) { |
| 194 | + throw new IOException(e.getMessage()); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + private static void addDisabledFilter(Document document) { |
| 199 | + addFilter(document, 0.64f, 0.4f); |
| 200 | + } |
| 201 | + |
| 202 | + private static void addGrayFilter(Document document) { |
| 203 | + addFilter(document, 0.64f, 0.1f); |
| 204 | + } |
| 205 | + |
| 206 | + private static void addFilter(Document document, float slope, float intercept) { |
| 207 | + Element defs = (Element) document.getElementsByTagName("defs").item(0); |
| 208 | + if (defs == null) { |
| 209 | + defs = document.createElement("defs"); |
| 210 | + document.getDocumentElement().appendChild(defs); |
| 211 | + } |
| 212 | + |
| 213 | + Element filter = document.createElement("filter"); |
| 214 | + filter.setAttribute("id", "customizedLook"); |
| 215 | + |
| 216 | + Element colorMatrix = document.createElement("feColorMatrix"); |
| 217 | + colorMatrix.setAttribute("type", "saturate"); |
| 218 | + colorMatrix.setAttribute("values", "0"); |
| 219 | + filter.appendChild(colorMatrix); |
| 220 | + |
| 221 | + Element componentTransfer = document.createElement("feComponentTransfer"); |
| 222 | + for (String channel : new String[] { "R", "G", "B" }) { |
| 223 | + Element func = document.createElement("feFunc" + channel); |
| 224 | + func.setAttribute("type", "linear"); |
| 225 | + func.setAttribute("slope", Float.toString(slope)); |
| 226 | + func.setAttribute("intercept", Float.toString(intercept)); |
| 227 | + componentTransfer.appendChild(func); |
| 228 | + } |
| 229 | + filter.appendChild(componentTransfer); |
| 230 | + defs.appendChild(filter); |
| 231 | + document.getDocumentElement().setAttribute("filter", "url(#customizedLook)"); |
| 232 | + } |
| 233 | + |
| 234 | + private static void writeSVG(Document document, OutputStream outputStream) throws IOException { |
| 235 | + TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
| 236 | + Transformer transformer; |
| 237 | + try { |
| 238 | + transformer = transformerFactory.newTransformer(); |
| 239 | + transformer.transform(new DOMSource(document), new StreamResult(outputStream)); |
| 240 | + } catch (TransformerException e) { |
| 241 | + throw new IOException(e.getMessage()); |
| 242 | + } |
| 243 | + } |
144 | 244 | }
|
0 commit comments