Skip to content

Commit 714fd07

Browse files
committed
disabled icon logic
disabled icon logic
1 parent 9f27765 commit 714fd07

File tree

9 files changed

+310
-7
lines changed

9 files changed

+310
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
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.130.0.qualifier
6+
Bundle-Version: 4.0.0.qualifier
77
Bundle-ManifestVersion: 2
88
Bundle-Localization: fragment
99
Export-Package:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Vector Informatik GmbH and others.
3+
*
4+
* This program and the accompanying materials are made available under the terms of the Eclipse
5+
* Public License 2.0 which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors: Michael Bangas (Vector Informatik GmbH) - initial API and implementation
11+
*******************************************************************************/
12+
package org.eclipse.swt.svg.tests.junit;
13+
14+
import static org.eclipse.swt.tests.junit.SwtTestUtil.assertSWTProblem;
15+
import static org.junit.Assert.fail;
16+
17+
import java.io.File;
18+
import java.nio.file.Path;
19+
import org.eclipse.swt.SWT;
20+
import org.eclipse.swt.SWTException;
21+
import org.eclipse.swt.graphics.Image;
22+
import org.eclipse.swt.graphics.ImageData;
23+
import org.eclipse.swt.graphics.ImageDataProvider;
24+
import org.eclipse.swt.graphics.ImageFileNameProvider;
25+
import org.eclipse.swt.tests.junit.SwtTestUtil;
26+
import org.eclipse.swt.widgets.Display;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
30+
public class Test_org_eclipse_swt_internal_SVGRasterizer {
31+
32+
Display display;
33+
34+
ImageFileNameProvider imageFileNameProvider = zoom -> {
35+
String fileName = "collapseall.svg";
36+
return getPath(fileName);
37+
};
38+
39+
ImageDataProvider imageDataProvider = zoom -> {
40+
String fileName = "collapseall.svg";
41+
return new ImageData(getPath(fileName), zoom, SWT.IMAGE_COPY);
42+
};
43+
44+
@Before
45+
public void setUp() {
46+
display = Display.getDefault();
47+
}
48+
49+
String getPath(String fileName) {
50+
String urlPath = "";
51+
String pluginPath = System.getProperty("PLUGIN_PATH");
52+
if (pluginPath == null) {
53+
urlPath = Path.of("data/" + fileName).toAbsolutePath().toString();
54+
} else {
55+
urlPath = pluginPath + "/data/" + fileName;
56+
}
57+
if (File.separatorChar != '/')
58+
urlPath = urlPath.replace('/', File.separatorChar);
59+
if (SwtTestUtil.isWindows && urlPath.indexOf(File.separatorChar) == 0)
60+
urlPath = urlPath.substring(1);
61+
urlPath = urlPath.replaceAll("%20", " ");
62+
return urlPath;
63+
}
64+
65+
@Test
66+
public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageFileNameProvider() {
67+
// Valid provider
68+
Image image = new Image(display, imageFileNameProvider);
69+
image.dispose();
70+
// Corrupt Image provider
71+
ImageFileNameProvider provider = zoom -> {
72+
String fileName = "corrupt.svg";
73+
return getPath(fileName);
74+
};
75+
try {
76+
image = new Image(display, provider);
77+
image.dispose();
78+
fail("No exception thrown for corrupt image file.");
79+
} catch (SWTException e) {
80+
assertSWTProblem("Incorrect exception thrown for provider with corrupt images", SWT.ERROR_INVALID_IMAGE, e);
81+
}
82+
}
83+
84+
@Test
85+
public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageDataProvider() {
86+
// Valid provider
87+
Image image = new Image(display, imageDataProvider);
88+
image.dispose();
89+
// Corrupt Image provider
90+
ImageDataProvider provider = zoom -> {
91+
String fileName = "corrupt.svg";
92+
return new ImageData(getPath(fileName), zoom, SWT.IMAGE_COPY);
93+
};
94+
try {
95+
image = new Image(display, provider);
96+
image.dispose();
97+
fail("No exception thrown for corrupt image file.");
98+
} catch (SWTException e) {
99+
assertSWTProblem("Incorrect exception thrown for provider with corrupt images", SWT.ERROR_INVALID_IMAGE, e);
100+
}
101+
}
102+
}
Lines changed: 1 addition & 0 deletions
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

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,19 @@
4242
import org.eclipse.swt.SWT;
4343
import org.eclipse.swt.graphics.ImageData;
4444
import org.eclipse.swt.graphics.PaletteData;
45+
import org.eclipse.swt.graphics.RGB;
4546
import org.eclipse.swt.internal.image.SVGRasterizer;
47+
import javax.xml.parsers.DocumentBuilder;
48+
import javax.xml.parsers.DocumentBuilderFactory;
49+
import javax.xml.parsers.ParserConfigurationException;
50+
import javax.xml.transform.Transformer;
51+
import javax.xml.transform.TransformerException;
52+
import javax.xml.transform.TransformerFactory;
53+
import javax.xml.transform.dom.DOMSource;
54+
import javax.xml.transform.stream.StreamResult;
55+
import org.w3c.dom.Document;
56+
import org.w3c.dom.Element;
57+
import org.xml.sax.SAXException;
4658

4759
import com.github.weisj.jsvg.SVGDocument;
4860
import com.github.weisj.jsvg.geometry.size.FloatSize;
@@ -73,7 +85,19 @@ public class JSVGRasterizer implements SVGRasterizer {
7385
);
7486

7587
@Override
76-
public ImageData[] rasterizeSVG(InputStream inputStream, int zoom) throws IOException {
88+
public ImageData[] rasterizeSVG(InputStream inputStream, int zoom, int flag) throws IOException {
89+
switch(flag) {
90+
case SWT.IMAGE_DISABLE:
91+
inputStream = applyDisabledLook(inputStream);
92+
break;
93+
case SWT.IMAGE_GRAY:
94+
inputStream = applyGrayLook(inputStream);
95+
break;
96+
case SWT.IMAGE_COPY:
97+
break;
98+
default:
99+
SWT.error(SWT.ERROR_INVALID_IMAGE);
100+
}
77101
SVGDocument svgDocument = loadSVG(inputStream);
78102
if (svgDocument != null) {
79103
return generateRasterizedImageData(svgDocument, zoom);
@@ -141,4 +165,80 @@ private ImageData[] convertToSWTImageData(BufferedImage rasterizedImage) {
141165
}
142166
return new ImageData[]{imageData};
143167
}
168+
169+
private static InputStream applyDisabledLook(InputStream svgInputStream) throws IOException {
170+
Document svgDocument = parseSVG(svgInputStream);
171+
addDisabledFilter(svgDocument);
172+
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
173+
writeSVG(svgDocument, outputStream);
174+
return new ByteArrayInputStream(outputStream.toByteArray());
175+
}
176+
}
177+
178+
private static InputStream applyGrayLook(InputStream svgInputStream) throws IOException {
179+
Document svgDocument = parseSVG(svgInputStream);
180+
addGrayFilter(svgDocument);
181+
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
182+
writeSVG(svgDocument, outputStream);
183+
return new ByteArrayInputStream(outputStream.toByteArray());
184+
}
185+
}
186+
187+
private static Document parseSVG(InputStream inputStream) throws IOException {
188+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
189+
DocumentBuilder builder;
190+
try {
191+
builder = factory.newDocumentBuilder();
192+
return builder.parse(inputStream);
193+
} catch (SAXException | IOException | ParserConfigurationException e) {
194+
throw new IOException(e.getMessage());
195+
}
196+
}
197+
198+
private static void addDisabledFilter(Document document) {
199+
addFilter(document, 0.64f, 0.4f);
200+
}
201+
202+
private static void addGrayFilter(Document document) {
203+
addFilter(document, 0.64f, 0.1f);
204+
}
205+
206+
private static void addFilter(Document document, float slope, float intercept) {
207+
Element defs = (Element) document.getElementsByTagName("defs").item(0);
208+
if (defs == null) {
209+
defs = document.createElement("defs");
210+
document.getDocumentElement().appendChild(defs);
211+
}
212+
213+
Element filter = document.createElement("filter");
214+
filter.setAttribute("id", "customizedLook");
215+
216+
Element colorMatrix = document.createElement("feColorMatrix");
217+
colorMatrix.setAttribute("type", "saturate");
218+
colorMatrix.setAttribute("values", "0");
219+
filter.appendChild(colorMatrix);
220+
221+
Element componentTransfer = document.createElement("feComponentTransfer");
222+
for (String channel : new String[] { "R", "G", "B" }) {
223+
Element func = document.createElement("feFunc" + channel);
224+
func.setAttribute("type", "linear");
225+
func.setAttribute("slope", Float.toString(slope));
226+
func.setAttribute("intercept", Float.toString(intercept));
227+
componentTransfer.appendChild(func);
228+
}
229+
filter.appendChild(componentTransfer);
230+
defs.appendChild(filter);
231+
document.getDocumentElement().setAttribute("filter", "url(#customizedLook)");
232+
}
233+
234+
private static void writeSVG(Document document, OutputStream outputStream) throws IOException {
235+
TransformerFactory transformerFactory = TransformerFactory.newInstance();
236+
Transformer transformer;
237+
try {
238+
transformer = transformerFactory.newTransformer();
239+
transformer.transform(new DOMSource(document), new StreamResult(outputStream));
240+
} catch (TransformerException e) {
241+
throw new IOException(e.getMessage());
242+
}
243+
}
144244
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,4 @@ public static ElementAtZoom<ImageData> load(String filename, int fileZoom, int t
4848
if (data.isEmpty()) SWT.error(SWT.ERROR_INVALID_IMAGE);
4949
return data.get(0);
5050
}
51-
52-
public static ImageData[] load(String filename, int zoom) {
53-
return new ImageLoader().load(filename, zoom);
54-
}
55-
5651
}

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

Lines changed: 13 additions & 0 deletions
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/graphics/ImageLoader.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ List<ElementAtZoom<ImageData>> load(InputStream stream, int fileZoom, int target
162162
return images;
163163
}
164164

165+
//TODO: JavaDocs flag parameter
165166
/**
166167
* Loads an array of <code>ImageData</code> objects from the
167168
* file with the specified name. Throws an error if either
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Vector Informatik GmbH and others.
3+
*
4+
* This program and the accompanying materials are made available under the terms of the Eclipse
5+
* Public License 2.0 which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors: Michael Bangas (Vector Informatik GmbH) - initial API and implementation
11+
*******************************************************************************/
12+
package org.eclipse.swt.internal;
13+
14+
import java.io.*;
15+
16+
import org.eclipse.swt.graphics.*;
17+
18+
/**
19+
* Defines the interface for an SVG rasterizer, responsible for converting SVG
20+
* data into rasterized images.
21+
*
22+
* @since 3.129
23+
*/
24+
public interface SVGRasterizer {
25+
/**
26+
* Rasterizes an SVG image from the provided {@code InputStream} using the specified
27+
* zoom factor.
28+
*
29+
* @param stream the SVG image as an {@link InputStream}.
30+
* @param zoom the scaling factor e.g. 200 for doubled size. This value must no be 0.
31+
* @return the {@link ImageData} for the rasterized image, or {@code null} if
32+
* the input is not a valid SVG file or cannot be processed.
33+
* @throws IOException if an error occurs while reading the SVG data.
34+
*/
35+
public ImageData[] rasterizeSVG(InputStream stream, int zoom, int flag) throws IOException;
36+
}

0 commit comments

Comments
 (0)