17
17
import java .awt .image .*;
18
18
import java .io .*;
19
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
+
20
30
import org .eclipse .swt .graphics .SVGRasterizer ;
21
31
import org .eclipse .swt .graphics .ImageData ;
22
32
import org .eclipse .swt .graphics .PaletteData ;
23
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
+
24
38
import com .github .weisj .jsvg .*;
25
39
import com .github .weisj .jsvg .geometry .size .*;
26
40
import com .github .weisj .jsvg .parser .*;
@@ -45,17 +59,39 @@ public class JSVGRasterizer implements SVGRasterizer {
45
59
KEY_STROKE_CONTROL , VALUE_STROKE_PURE , //
46
60
KEY_TEXT_ANTIALIASING , VALUE_TEXT_ANTIALIAS_ON //
47
61
);
48
-
62
+
49
63
@ Override
50
64
public ImageData rasterizeSVG (InputStream stream , float scalingFactor ) throws IOException {
51
65
if (stream == null ) {
52
66
throw new IllegalArgumentException ("InputStream cannot be null" );
53
67
}
54
- stream .mark (Integer .MAX_VALUE );
55
68
if (svgLoader == null ) {
56
69
svgLoader = new SVGLoader ();
57
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 {
58
93
SVGDocument svgDocument = null ;
94
+ stream .mark (Integer .MAX_VALUE );
59
95
InputStream nonClosingStream = new FilterInputStream (stream ) {
60
96
@ Override
61
97
public void close () throws IOException {
@@ -81,6 +117,82 @@ public void close() throws IOException {
81
117
return null ;
82
118
}
83
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
+
84
196
private ImageData convertToSWT (BufferedImage bufferedImage ) {
85
197
if (bufferedImage .getColorModel () instanceof DirectColorModel ) {
86
198
DirectColorModel colorModel = (DirectColorModel )bufferedImage .getColorModel ();
0 commit comments