1717import java .awt .image .*;
1818import java .io .*;
1919import 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+
2030import org .eclipse .swt .graphics .SVGRasterizer ;
2131import org .eclipse .swt .graphics .ImageData ;
2232import org .eclipse .swt .graphics .PaletteData ;
2333import org .eclipse .swt .graphics .RGB ;
34+ import org .w3c .dom .Document ;
35+ import org .w3c .dom .Element ;
36+ import org .xml .sax .SAXException ;
37+
2438import com .github .weisj .jsvg .*;
2539import com .github .weisj .jsvg .geometry .size .*;
2640import com .github .weisj .jsvg .parser .*;
@@ -45,17 +59,39 @@ public class JSVGRasterizer implements SVGRasterizer {
4559 KEY_STROKE_CONTROL , VALUE_STROKE_PURE , //
4660 KEY_TEXT_ANTIALIASING , VALUE_TEXT_ANTIALIAS_ON //
4761 );
48-
62+
4963 @ Override
5064 public ImageData rasterizeSVG (InputStream stream , float scalingFactor ) throws IOException {
5165 if (stream == null ) {
5266 throw new IllegalArgumentException ("InputStream cannot be null" );
5367 }
54- stream .mark (Integer .MAX_VALUE );
5568 if (svgLoader == null ) {
5669 svgLoader = new SVGLoader ();
5770 }
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 {
5893 SVGDocument svgDocument = null ;
94+ stream .mark (Integer .MAX_VALUE );
5995 InputStream nonClosingStream = new FilterInputStream (stream ) {
6096 @ Override
6197 public void close () throws IOException {
@@ -81,6 +117,82 @@ public void close() throws IOException {
81117 return null ;
82118 }
83119
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+
84196 private ImageData convertToSWT (BufferedImage bufferedImage ) {
85197 if (bufferedImage .getColorModel () instanceof DirectColorModel ) {
86198 DirectColorModel colorModel = (DirectColorModel )bufferedImage .getColorModel ();
0 commit comments