diff --git a/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java b/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java index efa87970d90..65e85f9eaea 100644 --- a/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java +++ b/bundles/org.eclipse.swt.svg/src/org/eclipse/swt/svg/JSVGRasterizer.java @@ -31,6 +31,7 @@ import static java.awt.RenderingHints.VALUE_STROKE_PURE; import static java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_ON; +import java.awt.Color; import java.awt.Graphics2D; import java.awt.RenderingHints.Key; import java.awt.image.BufferedImage; @@ -41,6 +42,7 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.PaletteData; +import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.internal.image.SVGRasterizer; import com.github.weisj.jsvg.SVGDocument; @@ -82,21 +84,31 @@ public class JSVGRasterizer implements SVGRasterizer { @Override public ImageData rasterizeSVG(InputStream inputStream, int zoom) { + return rasterizeSVG(inputStream, zoom, null); + } + + @Override + public ImageData rasterizeSVG(InputStream inputStream, int width, int height) { + return rasterizeSVG(inputStream, width, height, null); + } + + @Override + public ImageData rasterizeSVG(InputStream inputStream, int zoom, RGB foregroundColor) { if (zoom < 0) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } SVGDocument svgDocument = loadAndValidateSVG(inputStream); - BufferedImage rasterizedImage = renderSVG(svgDocument, zoom); + BufferedImage rasterizedImage = renderSVG(svgDocument, zoom, foregroundColor); return convertToSWTImageData(rasterizedImage); } @Override - public ImageData rasterizeSVG(InputStream inputStream, int width, int height) { + public ImageData rasterizeSVG(InputStream inputStream, int width, int height, RGB foregroundColor) { SVGDocument svgDocument = loadAndValidateSVG(inputStream); - BufferedImage rasterizedImage = renderSVG(svgDocument, width, height); + BufferedImage rasterizedImage = renderSVG(svgDocument, width, height, foregroundColor); return convertToSWTImageData(rasterizedImage); } - + private SVGDocument loadAndValidateSVG(InputStream inputStream) { SVGDocument svgDocument = SVG_LOADER.load(inputStream, null, LoaderContext.createDefault()); if (svgDocument == null) { @@ -105,22 +117,22 @@ private SVGDocument loadAndValidateSVG(InputStream inputStream) { return svgDocument; } - private BufferedImage renderSVG(SVGDocument svgDocument, int zoom) { + private BufferedImage renderSVG(SVGDocument svgDocument, int zoom, RGB foregroundColor) { FloatSize sourceImageSize = svgDocument.size(); float scalingFactor = zoom / 100.0f; int targetImageWidth = calculateTargetWidth(scalingFactor, sourceImageSize); int targetImageHeight = calculateTargetHeight(scalingFactor, sourceImageSize); - return renderSVG(svgDocument, targetImageWidth, targetImageHeight); + return renderSVG(svgDocument, targetImageWidth, targetImageHeight, foregroundColor); } - - private BufferedImage renderSVG(SVGDocument svgDocument, int width, int height) { + + private BufferedImage renderSVG(SVGDocument svgDocument, int width, int height, RGB foregroundColor) { if (width <= 0 || height <= 0) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); float widthScalingFactor = width / svgDocument.size().width; float heightScalingFactor = height / svgDocument.size().height; - Graphics2D g = configureRenderingOptions(widthScalingFactor, heightScalingFactor, image); + Graphics2D g = configureRenderingOptions(widthScalingFactor, heightScalingFactor, image, foregroundColor); svgDocument.render(null, g); g.dispose(); return image; @@ -137,8 +149,12 @@ private int calculateTargetHeight(float scalingFactor, FloatSize sourceImageSize } private Graphics2D configureRenderingOptions(float widthScalingFactor, float heightScalingFactor, - BufferedImage image) { + BufferedImage image, RGB foregroundColor) { Graphics2D g = image.createGraphics(); + Color fgColor = foregroundColor != null + ? new Color(foregroundColor.red, foregroundColor.green, foregroundColor.blue) + : Color.BLACK; + g.setColor(fgColor); g.setRenderingHints(RENDERING_HINTS); g.scale(widthScalingFactor, heightScalingFactor); return g; diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/SVGFileFormat.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/SVGFileFormat.java index 396d838aa2c..48eb26469e1 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/SVGFileFormat.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/SVGFileFormat.java @@ -19,6 +19,7 @@ import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.internal.DPIUtil.*; +import org.eclipse.swt.widgets.Display; /** * A {@link FileFormat} implementation for handling SVG (Scalable Vector @@ -61,7 +62,7 @@ List> loadFromByteStream(int fileZoom, int targetZoom) if (targetZoom <= 0) { SWT.error(SWT.ERROR_INVALID_ARGUMENT, null, " [Cannot rasterize SVG for zoom <= 0]"); } - ImageData rasterizedImageData = RASTERIZER.rasterizeSVG(inputStream, 100 * targetZoom / fileZoom); + ImageData rasterizedImageData = RASTERIZER.rasterizeSVG(inputStream, 100 * targetZoom / fileZoom, getWidgetForegroundColor()); return List.of(new ElementAtZoom<>(rasterizedImageData, targetZoom)); } @@ -73,8 +74,15 @@ ImageData loadFromByteStreamBySize(int width, int height) { if (width <= 0 || height <= 0) { SWT.error(SWT.ERROR_INVALID_ARGUMENT, null, " [Cannot rasterize SVG for width or height <= 0]"); } - ImageData rasterizedImageData = RASTERIZER.rasterizeSVG(inputStream, width, height); - return rasterizedImageData; + return RASTERIZER.rasterizeSVG(inputStream, width, height, getWidgetForegroundColor()); + } + + private static RGB getWidgetForegroundColor() { + Display display = Display.getDefault(); + if (display != null) { + return display.getSystemColor(SWT.COLOR_WIDGET_FOREGROUND).getRGB(); + } + return null; } @Override diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/SVGRasterizer.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/SVGRasterizer.java index c99413dbd9e..81e98e46887 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/SVGRasterizer.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/SVGRasterizer.java @@ -61,4 +61,46 @@ public interface SVGRasterizer { * */ public ImageData rasterizeSVG(InputStream stream, int width, int height); + + /** + * Rasterizes an SVG image from the provided {@code InputStream} using the + * specified zoom and foreground color. + * + * @param stream the SVG image as an {@link InputStream}. + * @param zoom the scaling percentage (e.g., 100 = original size, 200 = double size). + * This value must be greater zero. + * @param foregroundColor the default foreground color, or {@code null} for black. + * @return the {@link ImageData} for the rasterized image. + * + * @exception SWTException + * + * @exception IllegalArgumentException + * + */ + public ImageData rasterizeSVG(InputStream stream, int zoom, RGB foregroundColor); + + /** + * Rasterizes an SVG image from the provided {@code InputStream} into a raster + * image of the specified width and height, using the given foreground color. + * + * @param stream the SVG image as an {@link InputStream}. + * @param width the width of the rasterized image in pixels (must be positive). + * @param height the height of the rasterized image in pixels (must be positive). + * @param foregroundColor the default foreground color, or {@code null} for black. + * @return the {@link ImageData} for the rasterized image. + * + * @exception SWTException + * + * @exception IllegalArgumentException + * + */ + public ImageData rasterizeSVG(InputStream stream, int width, int height, RGB foregroundColor); }