Skip to content

Commit a6ddf72

Browse files
committed
Remove dependency of SVG fragment to native fragments
1 parent ae58361 commit a6ddf72

File tree

10 files changed

+52
-101
lines changed

10 files changed

+52
-101
lines changed

binaries/.classpath_cocoa

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@
2929
<classpathentry kind="src" path="Eclipse SWT WebKit/cocoa"/>
3030
<classpathentry kind="src" path="Eclipse SWT OpenGL/cocoa"/>
3131
<classpathentry kind="src" path="Eclipse SWT OpenGL/common"/>
32+
<classpathentry combineaccessrules="false" kind="src" path="/org.eclipse.swt"/>
3233
<classpathentry kind="output" path="bin"/>
3334
</classpath>

binaries/.classpath_gtk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@
3131
<classpathentry kind="src" path="Eclipse SWT OpenGL/glx"/>
3232
<classpathentry kind="src" path="Eclipse SWT OpenGL/common"/>
3333
<classpathentry kind="src" path="Eclipse SWT WebKit/gtk"/>
34+
<classpathentry combineaccessrules="false" kind="src" path="/org.eclipse.swt"/>
3435
<classpathentry kind="output" path="bin"/>
3536
</classpath>

binaries/.classpath_win32

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@
3131
</attributes>
3232
</classpathentry>
3333
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
34+
<classpathentry combineaccessrules="false" kind="src" path="/org.eclipse.swt"/>
3435
<classpathentry kind="output" path="bin"/>
3536
</classpath>

bundles/org.eclipse.swt.svg/META-INF/p2.inf

Lines changed: 0 additions & 32 deletions
This file was deleted.

bundles/org.eclipse.swt.svg/build.properties

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,3 @@ bin.includes = META-INF/,\
2020
fragment.properties,\
2121
about.html
2222
src.includes = about.html
23-
24-
jars.extra.classpath = platform:/plugin/org.eclipse.swt.cocoa.macosx.aarch64,\
25-
platform:/plugin/org.eclipse.swt.cocoa.macosx.x86_64,\
26-
platform:/plugin/org.eclipse.swt.gtk.linux.aarch64,\
27-
platform:/plugin/org.eclipse.swt.gtk.linux.ppc64le,\
28-
platform:/plugin/org.eclipse.swt.gtk.linux.riscv64,\
29-
platform:/plugin/org.eclipse.swt.gtk.linux.x86_64,\
30-
platform:/plugin/org.eclipse.swt.win32.win32.aarch64,\
31-
platform:/plugin/org.eclipse.swt.win32.win32.x86_64

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

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,9 @@
3434
import java.awt.Graphics2D;
3535
import java.awt.RenderingHints.Key;
3636
import java.awt.image.BufferedImage;
37-
import java.awt.image.DataBufferInt;
3837
import java.io.InputStream;
3938
import java.util.Map;
4039

41-
import org.eclipse.swt.SWT;
42-
import org.eclipse.swt.graphics.ImageData;
43-
import org.eclipse.swt.graphics.PaletteData;
4440
import org.eclipse.swt.internal.image.SVGRasterizer;
4541

4642
import com.github.weisj.jsvg.SVGDocument;
@@ -70,26 +66,21 @@ public class JSVGRasterizer implements SVGRasterizer {
7066
);
7167

7268
@Override
73-
public ImageData rasterizeSVG(InputStream inputStream, int zoom) {
74-
if (zoom < 0) {
75-
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
76-
}
69+
public BufferedImage rasterizeSVG(InputStream inputStream, int zoom) {
7770
SVGDocument svgDocument = loadAndValidateSVG(inputStream);
78-
BufferedImage rasterizedImage = renderSVG(svgDocument, zoom);
79-
return convertToSWTImageData(rasterizedImage);
71+
return renderSVG(svgDocument, zoom);
8072
}
8173

8274
@Override
83-
public ImageData rasterizeSVG(InputStream inputStream, int width, int height) {
75+
public BufferedImage rasterizeSVG(InputStream inputStream, int width, int height) {
8476
SVGDocument svgDocument = loadAndValidateSVG(inputStream);
85-
BufferedImage rasterizedImage = renderSVG(svgDocument, width, height);
86-
return convertToSWTImageData(rasterizedImage);
77+
return renderSVG(svgDocument, width, height);
8778
}
8879

8980
private SVGDocument loadAndValidateSVG(InputStream inputStream) {
9081
SVGDocument svgDocument = SVG_LOADER.load(inputStream, null, LoaderContext.createDefault());
9182
if (svgDocument == null) {
92-
SWT.error(SWT.ERROR_INVALID_IMAGE);
83+
throw new IllegalArgumentException();
9384
}
9485
return svgDocument;
9586
}
@@ -103,9 +94,6 @@ private BufferedImage renderSVG(SVGDocument svgDocument, int zoom) {
10394
}
10495

10596
private BufferedImage renderSVG(SVGDocument svgDocument, int width, int height) {
106-
if (width <= 0 || height <= 0) {
107-
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
108-
}
10997
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
11098
float widthScalingFactor = width / svgDocument.size().width;
11199
float heightScalingFactor = height / svgDocument.size().height;
@@ -133,20 +121,5 @@ private Graphics2D configureRenderingOptions(float widthScalingFactor, float hei
133121
return g;
134122
}
135123

136-
private ImageData convertToSWTImageData(BufferedImage rasterizedImage) {
137-
int width = rasterizedImage.getWidth();
138-
int height = rasterizedImage.getHeight();
139-
int[] pixels = ((DataBufferInt) rasterizedImage.getRaster().getDataBuffer()).getData();
140-
PaletteData paletteData = new PaletteData(0xFF0000, 0x00FF00, 0x0000FF);
141-
ImageData imageData = new ImageData(width, height, 24, paletteData);
142-
int index = 0;
143-
for (int y = 0; y < imageData.height; y++) {
144-
for (int x = 0; x < imageData.width; x++) {
145-
int alpha = (pixels[index] >> 24) & 0xFF;
146-
imageData.setAlpha(x, y, alpha);
147-
imageData.setPixel(x, y, pixels[index++] & 0x00FFFFFF);
148-
}
149-
}
150-
return imageData;
151-
}
124+
152125
}

bundles/org.eclipse.swt/.classpath

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
<classpath>
33
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21"/>
44
<classpathentry kind="src" output="bin_build" path="build-scripts"/>
5+
<classpathentry kind="src" path="Eclipse SWT Common"/>
6+
<classpathentry kind="output" path="bin"/>
57
</classpath>

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/SVGRasterizer.java renamed to bundles/org.eclipse.swt/Eclipse SWT Common/org/eclipse/swt/internal/image/SVGRasterizer.java

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
*******************************************************************************/
1313
package org.eclipse.swt.internal.image;
1414

15+
import java.awt.image.*;
1516
import java.io.*;
1617

17-
import org.eclipse.swt.*;
18-
import org.eclipse.swt.graphics.*;
19-
2018
/**
2119
* Defines the interface for an SVG rasterizer, responsible for converting SVG
2220
* data into rasterized images.
@@ -29,18 +27,11 @@ public interface SVGRasterizer {
2927
* @param stream the SVG image as an {@link InputStream}.
3028
* @param zoom the scaling percentage (e.g., 100 = original size, 200 = double size).
3129
* This value must be greater zero.
32-
* @return the {@link ImageData} for the rasterized image.
30+
* @return the {@link BufferedImage} for the rasterized image.
3331
*
34-
* @exception SWTException
35-
* <ul>
36-
* <li>ERROR_INVALID_IMAGE - if the SVG cannot be loaded</li>
37-
* </ul>
38-
* @exception IllegalArgumentException
39-
* <ul>
40-
* <li>ERROR_INVALID_ARGUMENT - if the zoom is less than zero</li>
41-
* </ul>
32+
* @exception IllegalArgumentException if the SVG cannot be loaded
4233
*/
43-
public ImageData rasterizeSVG(InputStream stream, int zoom);
34+
public BufferedImage rasterizeSVG(InputStream stream, int zoom);
4435

4536
/**
4637
* Rasterizes an SVG image from the provided {@code InputStream} into a raster
@@ -49,16 +40,9 @@ public interface SVGRasterizer {
4940
* @param stream the SVG image as an {@link InputStream}.
5041
* @param width the width of the rasterized image in pixels (must be positive).
5142
* @param height the height of the rasterized image in pixels (must be positive).
52-
* @return the {@link ImageData} for the rasterized image.
43+
* @return the {@link BufferedImage} for the rasterized image.
5344
*
54-
* @exception SWTException
55-
* <ul>
56-
* <li>ERROR_INVALID_IMAGE - if the SVG cannot be loaded</li>
57-
* </ul>
58-
* @exception IllegalArgumentException
59-
* <ul>
60-
* <li>ERROR_INVALID_ARGUMENT - if the width or height is less than zero</li>
61-
* </ul>
45+
* @exception IllegalArgumentException if the SVG cannot be loaded
6246
*/
63-
public ImageData rasterizeSVG(InputStream stream, int width, int height);
47+
public BufferedImage rasterizeSVG(InputStream stream, int width, int height);
6448
}

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*******************************************************************************/
1313
package org.eclipse.swt.internal.image;
1414

15+
import java.awt.image.*;
1516
import java.io.*;
1617
import java.nio.charset.*;
1718
import java.util.*;
@@ -61,8 +62,13 @@ List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom)
6162
if (targetZoom <= 0) {
6263
SWT.error(SWT.ERROR_INVALID_ARGUMENT, null, " [Cannot rasterize SVG for zoom <= 0]");
6364
}
64-
ImageData rasterizedImageData = RASTERIZER.rasterizeSVG(inputStream, 100 * targetZoom / fileZoom);
65-
return List.of(new ElementAtZoom<>(rasterizedImageData, targetZoom));
65+
try {
66+
ImageData rasterizedImageData = convertToSWTImageData(RASTERIZER.rasterizeSVG(inputStream, 100 * targetZoom / fileZoom));
67+
return List.of(new ElementAtZoom<>(rasterizedImageData, targetZoom));
68+
} catch (IllegalArgumentException e) {
69+
SWT.error(SWT.ERROR_INVALID_IMAGE);
70+
return null;
71+
}
6672
}
6773

6874
@Override
@@ -73,8 +79,30 @@ ImageData loadFromByteStreamBySize(int width, int height) {
7379
if (width <= 0 || height <= 0) {
7480
SWT.error(SWT.ERROR_INVALID_ARGUMENT, null, " [Cannot rasterize SVG for width or height <= 0]");
7581
}
76-
ImageData rasterizedImageData = RASTERIZER.rasterizeSVG(inputStream, width, height);
77-
return rasterizedImageData;
82+
try {
83+
ImageData rasterizedImageData = convertToSWTImageData(RASTERIZER.rasterizeSVG(inputStream, width, height));
84+
return rasterizedImageData;
85+
} catch (IllegalArgumentException e) {
86+
SWT.error(SWT.ERROR_INVALID_IMAGE);
87+
return null;
88+
}
89+
}
90+
91+
private ImageData convertToSWTImageData(BufferedImage rasterizedImage) {
92+
int width = rasterizedImage.getWidth();
93+
int height = rasterizedImage.getHeight();
94+
int[] pixels = ((DataBufferInt) rasterizedImage.getRaster().getDataBuffer()).getData();
95+
PaletteData paletteData = new PaletteData(0xFF0000, 0x00FF00, 0x0000FF);
96+
ImageData imageData = new ImageData(width, height, 24, paletteData);
97+
int index = 0;
98+
for (int y = 0; y < imageData.height; y++) {
99+
for (int x = 0; x < imageData.width; x++) {
100+
int alpha = (pixels[index] >> 24) & 0xFF;
101+
imageData.setAlpha(x, y, alpha);
102+
imageData.setPixel(x, y, pixels[index++] & 0x00FFFFFF);
103+
}
104+
}
105+
return imageData;
78106
}
79107

80108
@Override

bundles/org.eclipse.swt/build.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ bin.includes = plugin.properties,about.html,about_files/,META-INF/
1717
# force update
1818
# Don't want to generate an org.eclipse.swt.source bundle, all the source will be in fragment source bundles
1919
generateSourceBundle=false
20+
21+
source.. = Eclipse SWT Common/

0 commit comments

Comments
 (0)