Skip to content

Commit

Permalink
disabled icon logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael5601 committed Dec 12, 2024
1 parent b5365d8 commit e9fb30d
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Fragment-Host: org.eclipse.swt;bundle-version="[3.128.0,4.0.0)"
Bundle-Name: %fragmentName
Bundle-Vendor: %providerName
Bundle-SymbolicName: org.eclipse.swt.win32.win32.x86_64; singleton:=true
Bundle-Version: 3.129.0.qualifier
Bundle-Version: 4.0.0.qualifier
Bundle-ManifestVersion: 2
Bundle-Localization: fragment
Export-Package:
Expand Down
3 changes: 2 additions & 1 deletion bundles/org.eclipse.swt.svg/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ Bundle-Version: 1.0.0.qualifier
Automatic-Module-Name: org.eclipse.swt.svgPlugin
Bundle-RequiredExecutionEnvironment: JavaSE-17
Export-Package: org.eclipse.swt.svg
Import-Package: org.eclipse.swt.graphics
Import-Package: org.eclipse.swt,
org.eclipse.swt.graphics
Bundle-ClassPath: ., libs/jsvg-1.6.1.jar
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,24 @@
import java.awt.image.*;
import java.io.*;
import java.util.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.eclipse.swt.graphics.SVGRasterizer;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.SVGRasterizerRegistry;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import com.github.weisj.jsvg.*;
import com.github.weisj.jsvg.geometry.size.*;
Expand Down Expand Up @@ -55,17 +68,39 @@ public static void intializeJSVGRasterizer() {
KEY_STROKE_CONTROL, VALUE_STROKE_PURE, //
KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_ON //
);

@Override
public ImageData rasterizeSVG(InputStream stream, float scalingFactor) throws IOException {
if (stream == null) {
throw new IllegalArgumentException("InputStream cannot be null");
}
stream.mark(Integer.MAX_VALUE);
if(svgLoader == null) {
svgLoader = new SVGLoader();
}
return rasterize(stream, scalingFactor);
}

@Override
public ImageData rasterizeDisabledSVG(InputStream stream, float scalingFactor) throws IOException {
if(svgLoader == null) {
svgLoader = new SVGLoader();
}
InputStream disabledStream = applyDisabledLook(stream);
return rasterize(disabledStream, scalingFactor);
}

@Override
public ImageData rasterizeGraySVG(InputStream stream, float scalingFactor) throws IOException {
if(svgLoader == null) {
svgLoader = new SVGLoader();
}
InputStream disabledStream = applyGrayLook(stream);
return rasterize(disabledStream, scalingFactor);
}

private ImageData rasterize(InputStream stream, float scalingFactor) throws IOException {
SVGDocument svgDocument = null;
stream.mark(Integer.MAX_VALUE);
InputStream nonClosingStream = new FilterInputStream(stream) {
@Override
public void close() throws IOException {
Expand All @@ -91,6 +126,82 @@ public void close() throws IOException {
return null;
}

private static InputStream applyDisabledLook(InputStream svgInputStream) throws IOException {
Document svgDocument = parseSVG(svgInputStream);
addDisabledFilter(svgDocument);
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
writeSVG(svgDocument, outputStream);
return new ByteArrayInputStream(outputStream.toByteArray());
}
}

private static InputStream applyGrayLook(InputStream svgInputStream) throws IOException {
Document svgDocument = parseSVG(svgInputStream);
addGrayFilter(svgDocument);
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
writeSVG(svgDocument, outputStream);
return new ByteArrayInputStream(outputStream.toByteArray());
}
}

private static Document parseSVG(InputStream inputStream) throws IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
return builder.parse(inputStream);
} catch (SAXException | IOException | ParserConfigurationException e) {
throw new IOException(e.getMessage());
}
}

private static void addDisabledFilter(Document document) {
addFilter(document, 0.64f, 0.4f);
}

private static void addGrayFilter(Document document) {
addFilter(document, 0.64f, 0.1f);
}

private static void addFilter(Document document, float slope, float intercept) {
Element defs = (Element) document.getElementsByTagName("defs").item(0);
if (defs == null) {
defs = document.createElement("defs");
document.getDocumentElement().appendChild(defs);
}

Element filter = document.createElement("filter");
filter.setAttribute("id", "customizedLook");

Element colorMatrix = document.createElement("feColorMatrix");
colorMatrix.setAttribute("type", "saturate");
colorMatrix.setAttribute("values", "0");
filter.appendChild(colorMatrix);

Element componentTransfer = document.createElement("feComponentTransfer");
for (String channel : new String[] { "R", "G", "B" }) {
Element func = document.createElement("feFunc" + channel);
func.setAttribute("type", "linear");
func.setAttribute("slope", Float.toString(slope));
func.setAttribute("intercept", Float.toString(intercept));
componentTransfer.appendChild(func);
}
filter.appendChild(componentTransfer);
defs.appendChild(filter);
document.getDocumentElement().setAttribute("filter", "url(#customizedLook)");
}

private static void writeSVG(Document document, OutputStream outputStream) throws IOException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = transformerFactory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult(outputStream));
} catch (TransformerException e) {
throw new IOException(e.getMessage());
}
}

private ImageData convertToSWT(BufferedImage bufferedImage) {
if (bufferedImage.getColorModel() instanceof DirectColorModel) {
DirectColorModel colorModel = (DirectColorModel)bufferedImage.getColorModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ scanlinePad, checkData(data), 0, null,
* @see ImageLoader#load(InputStream)
*/
public ImageData(InputStream stream) {
this(stream, 0);
this(stream, 0, SWT.IMAGE_COPY);
}

/**
Expand Down Expand Up @@ -360,10 +360,10 @@ public ImageData(InputStream stream) {
* </ul>
*
* @see ImageLoader#load(InputStream)
* @since 3.129
* @since 4.0
*/
public ImageData(InputStream stream, int zoom) {
ImageData[] data = ImageDataLoader.load(stream, zoom);
public ImageData(InputStream stream, int zoom, int flag) {
ImageData[] data = ImageDataLoader.load(stream, zoom, flag);
if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
ImageData i = data[0];
setAllFields(
Expand Down Expand Up @@ -409,7 +409,7 @@ public ImageData(InputStream stream, int zoom) {
* </ul>
*/
public ImageData(String filename) {
this(filename, 0);
this(filename, 0, SWT.IMAGE_COPY);
}

/**
Expand All @@ -435,10 +435,10 @@ public ImageData(String filename) {
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
* </ul>
*
* @since 3.129
* @since 4.0
*/
public ImageData(String filename, int zoom) {
ImageData[] data = ImageDataLoader.load(filename, zoom);
public ImageData(String filename, int zoom, int flag) {
ImageData[] data = ImageDataLoader.load(filename, zoom, flag);
if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
ImageData i = data[0];
setAllFields(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public static ImageData[] load(InputStream stream) {
return new ImageLoader().load(stream);
}

public static ImageData[] load(InputStream stream, int zoom) {
return new ImageLoader().load(stream, zoom);
public static ImageData[] load(InputStream stream, int zoom, int flag) {
return new ImageLoader().load(stream, zoom, flag);
}

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

public static ImageData[] load(String filename, int zoom) {
return new ImageLoader().load(filename, zoom);
public static ImageData[] load(String filename, int zoom, int flag) {
return new ImageLoader().load(filename, zoom, flag);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
*/
public interface ImageDataProvider {


/**
* Returns the image data for the given zoom level.
* <p>
Expand All @@ -43,4 +42,17 @@ public interface ImageDataProvider {
*/
ImageData getImageData (int zoom);

/**
* @since 4.0
*/
default ImageData getCustomizedImageData(int zoom, int flag) {
throw new UnsupportedOperationException();
}

/**
* @since 4.0
*/
default boolean supportsRasterizationFlag(int flag) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Defines the interface for an SVG rasterizer, responsible for converting SVG
* data into rasterized images.
*
* @since 3.129
* @since 4.0
*/
public interface SVGRasterizer {

Expand All @@ -34,6 +34,10 @@ public interface SVGRasterizer {
*/
public ImageData rasterizeSVG(InputStream stream, float scalingFactor) throws IOException;

public ImageData rasterizeDisabledSVG(InputStream stream, float scalingFactor) throws IOException;

public ImageData rasterizeGraySVG(InputStream stream, float scalingFactor) throws IOException;

/**
* Determines whether the given {@link InputStream} contains a SVG file.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* A registry for managing the instance of an {@link SVGRasterizer} implementation.
* This allows for the registration and retrieval of a single rasterizer instance.
*
* @since 3.129
* @since 4.0
*/
public class SVGRasterizerRegistry {

Expand Down
Loading

0 comments on commit e9fb30d

Please sign in to comment.