|
23 | 23 | import org.eclipse.swt.graphics.PaletteData;
|
24 | 24 | import org.eclipse.swt.graphics.RGB;
|
25 | 25 | import org.eclipse.swt.internal.SVGRasterizer;
|
| 26 | +import javax.xml.parsers.DocumentBuilder; |
| 27 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 28 | +import javax.xml.parsers.ParserConfigurationException; |
| 29 | +import javax.xml.transform.Transformer; |
| 30 | +import javax.xml.transform.TransformerException; |
| 31 | +import javax.xml.transform.TransformerFactory; |
| 32 | +import javax.xml.transform.dom.DOMSource; |
| 33 | +import javax.xml.transform.stream.StreamResult; |
| 34 | +import org.w3c.dom.Document; |
| 35 | +import org.w3c.dom.Element; |
| 36 | +import org.xml.sax.SAXException; |
| 37 | + |
26 | 38 | import com.github.weisj.jsvg.*;
|
27 | 39 | import com.github.weisj.jsvg.geometry.size.*;
|
28 | 40 | import com.github.weisj.jsvg.parser.*;
|
@@ -50,7 +62,19 @@ public class JSVGRasterizer implements SVGRasterizer {
|
50 | 62 | );
|
51 | 63 |
|
52 | 64 | @Override
|
53 |
| - public ImageData[] rasterizeSVG(InputStream stream, int zoom) throws IOException { |
| 65 | + public ImageData[] rasterizeSVG(InputStream stream, int zoom, int flag) throws IOException { |
| 66 | + switch(flag) { |
| 67 | + case SWT.IMAGE_DISABLE: |
| 68 | + stream = applyDisabledLook(stream); |
| 69 | + break; |
| 70 | + case SWT.IMAGE_GRAY: |
| 71 | + stream = applyGrayLook(stream); |
| 72 | + break; |
| 73 | + case SWT.IMAGE_COPY: |
| 74 | + break; |
| 75 | + default: |
| 76 | + SWT.error(SWT.ERROR_INVALID_IMAGE); |
| 77 | + } |
54 | 78 | SVGDocument svgDocument = null;
|
55 | 79 | svgDocument = SVG_LOADER.load(stream, null, LoaderContext.createDefault());
|
56 | 80 | if (svgDocument != null) {
|
@@ -136,4 +160,80 @@ private ImageData convertToSWT(BufferedImage bufferedImage) {
|
136 | 160 | }
|
137 | 161 | return null;
|
138 | 162 | }
|
| 163 | + |
| 164 | + private static InputStream applyDisabledLook(InputStream svgInputStream) throws IOException { |
| 165 | + Document svgDocument = parseSVG(svgInputStream); |
| 166 | + addDisabledFilter(svgDocument); |
| 167 | + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { |
| 168 | + writeSVG(svgDocument, outputStream); |
| 169 | + return new ByteArrayInputStream(outputStream.toByteArray()); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + private static InputStream applyGrayLook(InputStream svgInputStream) throws IOException { |
| 174 | + Document svgDocument = parseSVG(svgInputStream); |
| 175 | + addGrayFilter(svgDocument); |
| 176 | + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { |
| 177 | + writeSVG(svgDocument, outputStream); |
| 178 | + return new ByteArrayInputStream(outputStream.toByteArray()); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + private static Document parseSVG(InputStream inputStream) throws IOException { |
| 183 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 184 | + DocumentBuilder builder; |
| 185 | + try { |
| 186 | + builder = factory.newDocumentBuilder(); |
| 187 | + return builder.parse(inputStream); |
| 188 | + } catch (SAXException | IOException | ParserConfigurationException e) { |
| 189 | + throw new IOException(e.getMessage()); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + private static void addDisabledFilter(Document document) { |
| 194 | + addFilter(document, 0.64f, 0.4f); |
| 195 | + } |
| 196 | + |
| 197 | + private static void addGrayFilter(Document document) { |
| 198 | + addFilter(document, 0.64f, 0.1f); |
| 199 | + } |
| 200 | + |
| 201 | + private static void addFilter(Document document, float slope, float intercept) { |
| 202 | + Element defs = (Element) document.getElementsByTagName("defs").item(0); |
| 203 | + if (defs == null) { |
| 204 | + defs = document.createElement("defs"); |
| 205 | + document.getDocumentElement().appendChild(defs); |
| 206 | + } |
| 207 | + |
| 208 | + Element filter = document.createElement("filter"); |
| 209 | + filter.setAttribute("id", "customizedLook"); |
| 210 | + |
| 211 | + Element colorMatrix = document.createElement("feColorMatrix"); |
| 212 | + colorMatrix.setAttribute("type", "saturate"); |
| 213 | + colorMatrix.setAttribute("values", "0"); |
| 214 | + filter.appendChild(colorMatrix); |
| 215 | + |
| 216 | + Element componentTransfer = document.createElement("feComponentTransfer"); |
| 217 | + for (String channel : new String[] { "R", "G", "B" }) { |
| 218 | + Element func = document.createElement("feFunc" + channel); |
| 219 | + func.setAttribute("type", "linear"); |
| 220 | + func.setAttribute("slope", Float.toString(slope)); |
| 221 | + func.setAttribute("intercept", Float.toString(intercept)); |
| 222 | + componentTransfer.appendChild(func); |
| 223 | + } |
| 224 | + filter.appendChild(componentTransfer); |
| 225 | + defs.appendChild(filter); |
| 226 | + document.getDocumentElement().setAttribute("filter", "url(#customizedLook)"); |
| 227 | + } |
| 228 | + |
| 229 | + private static void writeSVG(Document document, OutputStream outputStream) throws IOException { |
| 230 | + TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
| 231 | + Transformer transformer; |
| 232 | + try { |
| 233 | + transformer = transformerFactory.newTransformer(); |
| 234 | + transformer.transform(new DOMSource(document), new StreamResult(outputStream)); |
| 235 | + } catch (TransformerException e) { |
| 236 | + throw new IOException(e.getMessage()); |
| 237 | + } |
| 238 | + } |
139 | 239 | }
|
0 commit comments