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 .eclipse .swt .graphics .SVGRasterizerRegistry ;
35
+ import org .w3c .dom .Document ;
36
+ import org .w3c .dom .Element ;
37
+ import org .xml .sax .SAXException ;
38
+
24
39
import com .github .weisj .jsvg .*;
25
40
import com .github .weisj .jsvg .geometry .size .*;
26
41
import com .github .weisj .jsvg .parser .*;
@@ -45,17 +60,39 @@ public class JSVGRasterizer implements SVGRasterizer {
45
60
KEY_STROKE_CONTROL , VALUE_STROKE_PURE , //
46
61
KEY_TEXT_ANTIALIASING , VALUE_TEXT_ANTIALIAS_ON //
47
62
);
48
-
63
+
49
64
@ Override
50
65
public ImageData rasterizeSVG (InputStream stream , float scalingFactor ) throws IOException {
51
66
if (stream == null ) {
52
67
throw new IllegalArgumentException ("InputStream cannot be null" );
53
68
}
54
- stream .mark (Integer .MAX_VALUE );
55
69
if (svgLoader == null ) {
56
70
svgLoader = new SVGLoader ();
57
71
}
72
+ return rasterize (stream , scalingFactor );
73
+ }
74
+
75
+ @ Override
76
+ public ImageData rasterizeDisabledSVG (InputStream stream , float scalingFactor ) throws IOException {
77
+ if (svgLoader == null ) {
78
+ svgLoader = new SVGLoader ();
79
+ }
80
+ InputStream disabledStream = applyDisabledLook (stream );
81
+ return rasterize (disabledStream , scalingFactor );
82
+ }
83
+
84
+ @ Override
85
+ public ImageData rasterizeGraySVG (InputStream stream , float scalingFactor ) throws IOException {
86
+ if (svgLoader == null ) {
87
+ svgLoader = new SVGLoader ();
88
+ }
89
+ InputStream disabledStream = applyGrayLook (stream );
90
+ return rasterize (disabledStream , scalingFactor );
91
+ }
92
+
93
+ private ImageData rasterize (InputStream stream , float scalingFactor ) throws IOException {
58
94
SVGDocument svgDocument = null ;
95
+ stream .mark (Integer .MAX_VALUE );
59
96
InputStream nonClosingStream = new FilterInputStream (stream ) {
60
97
@ Override
61
98
public void close () throws IOException {
@@ -81,6 +118,82 @@ public void close() throws IOException {
81
118
return null ;
82
119
}
83
120
121
+ private static InputStream applyDisabledLook (InputStream svgInputStream ) throws IOException {
122
+ Document svgDocument = parseSVG (svgInputStream );
123
+ addDisabledFilter (svgDocument );
124
+ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ()) {
125
+ writeSVG (svgDocument , outputStream );
126
+ return new ByteArrayInputStream (outputStream .toByteArray ());
127
+ }
128
+ }
129
+
130
+ private static InputStream applyGrayLook (InputStream svgInputStream ) throws IOException {
131
+ Document svgDocument = parseSVG (svgInputStream );
132
+ addGrayFilter (svgDocument );
133
+ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ()) {
134
+ writeSVG (svgDocument , outputStream );
135
+ return new ByteArrayInputStream (outputStream .toByteArray ());
136
+ }
137
+ }
138
+
139
+ private static Document parseSVG (InputStream inputStream ) throws IOException {
140
+ DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
141
+ DocumentBuilder builder ;
142
+ try {
143
+ builder = factory .newDocumentBuilder ();
144
+ return builder .parse (inputStream );
145
+ } catch (SAXException | IOException | ParserConfigurationException e ) {
146
+ throw new IOException (e .getMessage ());
147
+ }
148
+ }
149
+
150
+ private static void addDisabledFilter (Document document ) {
151
+ addFilter (document , 0.64f , 0.4f );
152
+ }
153
+
154
+ private static void addGrayFilter (Document document ) {
155
+ addFilter (document , 0.64f , 0.1f );
156
+ }
157
+
158
+ private static void addFilter (Document document , float slope , float intercept ) {
159
+ Element defs = (Element ) document .getElementsByTagName ("defs" ).item (0 );
160
+ if (defs == null ) {
161
+ defs = document .createElement ("defs" );
162
+ document .getDocumentElement ().appendChild (defs );
163
+ }
164
+
165
+ Element filter = document .createElement ("filter" );
166
+ filter .setAttribute ("id" , "customizedLook" );
167
+
168
+ Element colorMatrix = document .createElement ("feColorMatrix" );
169
+ colorMatrix .setAttribute ("type" , "saturate" );
170
+ colorMatrix .setAttribute ("values" , "0" );
171
+ filter .appendChild (colorMatrix );
172
+
173
+ Element componentTransfer = document .createElement ("feComponentTransfer" );
174
+ for (String channel : new String [] { "R" , "G" , "B" }) {
175
+ Element func = document .createElement ("feFunc" + channel );
176
+ func .setAttribute ("type" , "linear" );
177
+ func .setAttribute ("slope" , Float .toString (slope ));
178
+ func .setAttribute ("intercept" , Float .toString (intercept ));
179
+ componentTransfer .appendChild (func );
180
+ }
181
+ filter .appendChild (componentTransfer );
182
+ defs .appendChild (filter );
183
+ document .getDocumentElement ().setAttribute ("filter" , "url(#customizedLook)" );
184
+ }
185
+
186
+ private static void writeSVG (Document document , OutputStream outputStream ) throws IOException {
187
+ TransformerFactory transformerFactory = TransformerFactory .newInstance ();
188
+ Transformer transformer ;
189
+ try {
190
+ transformer = transformerFactory .newTransformer ();
191
+ transformer .transform (new DOMSource (document ), new StreamResult (outputStream ));
192
+ } catch (TransformerException e ) {
193
+ throw new IOException (e .getMessage ());
194
+ }
195
+ }
196
+
84
197
private ImageData convertToSWT (BufferedImage bufferedImage ) {
85
198
if (bufferedImage .getColorModel () instanceof DirectColorModel ) {
86
199
DirectColorModel colorModel = (DirectColorModel )bufferedImage .getColorModel ();
0 commit comments