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 18, 2024
1 parent 8cb089b commit 67d7433
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 61 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +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.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.*;
import com.github.weisj.jsvg.parser.*;
Expand All @@ -45,17 +59,39 @@ public class JSVGRasterizer implements SVGRasterizer {
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 @@ -81,6 +117,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 @@ -176,26 +176,37 @@ public ImageData[] load(InputStream stream) {
* <li>ERROR_UNSUPPORTED_FORMAT - if the image stream contains an unrecognized format</li>
* </ul>
*
* @since 3.129
* @since 4.0
*/
public ImageData[] load(InputStream stream, int zoom) {
public ImageData[] load(InputStream stream, int zoom, int flag) {
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
reset();
if (!stream.markSupported()) {
stream = new BufferedInputStream(stream);
}
ImageData rasterizedData = null;
SVGRasterizer rasterizer = SVGRasterizerRegistry.getRasterizer();
if (rasterizer != null && zoom != 0) {
try {
try {
if (rasterizer.isSVGFile(stream)) {
float scalingFactor = zoom / 100.0f;
ImageData rasterizedData = rasterizer.rasterizeSVG(stream, scalingFactor);
if (rasterizedData != null) {
float scalingFactor = zoom / 100.0f;
switch(flag) {
case SWT.IMAGE_DISABLE:
rasterizedData = rasterizer.rasterizeDisabledSVG(stream, scalingFactor);
break;
case SWT.IMAGE_GRAY:
rasterizedData = rasterizer.rasterizeGraySVG(stream, scalingFactor);
break;
case SWT.IMAGE_COPY:
rasterizedData = rasterizer.rasterizeSVG(stream, scalingFactor);
break;
}
if (rasterizedData != null) {
data = new ImageData[]{rasterizedData};
return data;
return data;
}
}
} catch (IOException e) {
} catch (IOException e) {
//ignore.
}
}
Expand Down Expand Up @@ -258,12 +269,12 @@ public ImageData[] load(String filename) {
* <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li>
* </ul>
*
* @since 3.129
* @since 4.0
*/
public ImageData[] load(String filename, int zoom) {
public ImageData[] load(String filename, int zoom, int flag) {
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
try (InputStream stream = new FileInputStream(filename)) {
return load(stream, zoom);
return load(stream, zoom, flag);
} catch (IOException e) {
SWT.error(SWT.ERROR_IO, e);
}
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 @@ -17,7 +17,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
*/
class SVGRasterizerRegistry {

Expand Down
Loading

0 comments on commit 67d7433

Please sign in to comment.