Skip to content

Commit b632c14

Browse files
committed
disabled icon logic
disabled icon logic
1 parent 945ee0c commit b632c14

File tree

13 files changed

+205
-29
lines changed

13 files changed

+205
-29
lines changed

binaries/org.eclipse.swt.win32.win32.x86_64/META-INF/MANIFEST.MF

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Fragment-Host: org.eclipse.swt;bundle-version="[3.128.0,4.0.0)"
33
Bundle-Name: %fragmentName
44
Bundle-Vendor: %providerName
55
Bundle-SymbolicName: org.eclipse.swt.win32.win32.x86_64; singleton:=true
6-
Bundle-Version: 3.129.0.qualifier
6+
Bundle-Version: 4.0.0.qualifier
77
Bundle-ManifestVersion: 2
88
Bundle-Localization: fragment
99
Export-Package:

bundles/org.eclipse.swt.svg.tests/JUnit Tests/org/eclipse/swt/svg/tests/junit/Test_org_eclipse_swt_internal_SVGRasterizer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class Test_org_eclipse_swt_internal_SVGRasterizer {
3838

3939
ImageDataProvider imageDataProvider = zoom -> {
4040
String fileName = "collapseall.svg";
41-
return new ImageData(getPath(fileName), zoom);
41+
return new ImageData(getPath(fileName), zoom, SWT.IMAGE_COPY);
4242
};
4343

4444
@Before
@@ -89,7 +89,7 @@ public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageDataProvider()
8989
// Corrupt Image provider
9090
ImageDataProvider provider = zoom -> {
9191
String fileName = "corrupt.svg";
92-
return new ImageData(getPath(fileName), zoom);
92+
return new ImageData(getPath(fileName), zoom, SWT.IMAGE_COPY);
9393
};
9494
try {
9595
image = new Image(display, provider);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.eclipse.swt.svg.JSVGRasterizer

bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java

+101-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@
2323
import org.eclipse.swt.graphics.PaletteData;
2424
import org.eclipse.swt.graphics.RGB;
2525
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+
2638
import com.github.weisj.jsvg.*;
2739
import com.github.weisj.jsvg.geometry.size.*;
2840
import com.github.weisj.jsvg.parser.*;
@@ -50,7 +62,19 @@ public class JSVGRasterizer implements SVGRasterizer {
5062
);
5163

5264
@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+
}
5478
SVGDocument svgDocument = null;
5579
svgDocument = SVG_LOADER.load(stream, null, LoaderContext.createDefault());
5680
if (svgDocument != null) {
@@ -136,4 +160,80 @@ private ImageData convertToSWT(BufferedImage bufferedImage) {
136160
}
137161
return null;
138162
}
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+
}
139239
}

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/ImageLoader.java

+3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
package org.eclipse.swt.graphics;
1515

1616

17+
import java.awt.image.BufferedImage;
1718
import java.io.*;
1819
import java.util.*;
1920

21+
import javax.imageio.ImageIO;
22+
2023
import org.eclipse.swt.*;
2124
import org.eclipse.swt.internal.image.*;
2225

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageData.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,10 @@ public ImageData(InputStream stream) {
384384
* </ul>
385385
*
386386
* @see ImageLoader#load(InputStream)
387-
* @since 3.129
387+
* @since 4.0
388388
*/
389-
public ImageData(InputStream stream, int zoom) {
390-
ImageData[] data = ImageDataLoader.load(stream, zoom);
389+
public ImageData(InputStream stream, int zoom, int flag) {
390+
ImageData[] data = ImageDataLoader.load(stream, zoom, flag);
391391
if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
392392
ImageData i = data[0];
393393
setAllFields(
@@ -483,10 +483,10 @@ public ImageData(String filename) {
483483
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
484484
* </ul>
485485
*
486-
* @since 3.129
486+
* @since 4.0
487487
*/
488-
public ImageData(String filename, int zoom) {
489-
ImageData[] data = ImageDataLoader.load(filename, zoom);
488+
public ImageData(String filename, int zoom, int flag) {
489+
ImageData[] data = ImageDataLoader.load(filename, zoom, flag);
490490
if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
491491
ImageData i = data[0];
492492
setAllFields(

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageDataLoader.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ public static ImageData[] load(InputStream stream) {
2525
return new ImageLoader().load(stream);
2626
}
2727

28-
public static ImageData[] load(InputStream stream, int zoom) {
29-
return new ImageLoader().load(stream, zoom);
28+
public static ImageData[] load(InputStream stream, int zoom, int flag) {
29+
return new ImageLoader().load(stream, zoom, flag);
3030
}
3131

3232
public static ImageData[] load(String filename) {
3333
return new ImageLoader().load(filename);
3434
}
3535

36-
public static ImageData[] load(String filename, int zoom) {
37-
return new ImageLoader().load(filename, zoom);
36+
public static ImageData[] load(String filename, int zoom, int flag) {
37+
return new ImageLoader().load(filename, zoom, flag);
3838
}
3939

4040
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/ImageDataProvider.java

+13
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,17 @@ public interface ImageDataProvider {
4343
*/
4444
ImageData getImageData (int zoom);
4545

46+
/**
47+
* @since 4.0
48+
*/
49+
default ImageData getCustomizedImageData(int zoom, int flag) {
50+
throw new UnsupportedOperationException();
51+
}
52+
53+
/**
54+
* @since 4.0
55+
*/
56+
default boolean supportsRasterizationFlag(int flag) {
57+
return false;
58+
}
4659
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/SVGRasterizer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ public interface SVGRasterizer {
3232
* the input is not a valid SVG file or cannot be processed.
3333
* @throws IOException if an error occurs while reading the SVG data.
3434
*/
35-
public ImageData[] rasterizeSVG(InputStream stream, int zoom) throws IOException;
35+
public ImageData[] rasterizeSVG(InputStream stream, int zoom, int flag) throws IOException;
3636
}

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/FileFormat.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static ImageData[] load(InputStream is, ImageLoader loader) {
128128
*
129129
* @since 3.129
130130
*/
131-
public static ImageData[] load(InputStream stream, int zoom, ImageLoader loader) {
131+
public static ImageData[] load(InputStream stream, int zoom, int flag, ImageLoader loader) {
132132
if (stream == null) {
133133
throw new IllegalArgumentException("InputStream cannot be null");
134134
}
@@ -137,7 +137,7 @@ public static ImageData[] load(InputStream stream, int zoom, ImageLoader loader)
137137
}
138138
try {
139139
if (RASTERIZER != null && zoom != 0 && isSVGFile(stream)) {
140-
return RASTERIZER.rasterizeSVG(stream, zoom);
140+
return RASTERIZER.rasterizeSVG(stream, zoom, flag);
141141
} else {
142142
return load(stream, loader);
143143
}

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/ImageLoader.java

+3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
package org.eclipse.swt.graphics;
1616

1717

18+
import java.awt.image.BufferedImage;
1819
import java.io.*;
1920
import java.util.*;
2021
import java.util.List;
2122

23+
import javax.imageio.ImageIO;
24+
2225
import org.eclipse.swt.*;
2326
import org.eclipse.swt.internal.*;
2427
import org.eclipse.swt.internal.gtk.*;

0 commit comments

Comments
 (0)