Skip to content

Commit 3a26755

Browse files
committed
change functionality according to SVG Feature PR
This commit introduces much new API to bring the disablement flag to the Rasterizer. ImageDataProvider is ignored for now.
1 parent 548d586 commit 3a26755

File tree

7 files changed

+89
-27
lines changed

7 files changed

+89
-27
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ public static ElementAtZoom<ImageData> load(String filename, int fileZoom, int t
4949
return data.get(0);
5050
}
5151

52+
public static ElementAtZoom<ImageData> load(String filename, int fileZoom, int targetZoom, int flag) {
53+
List<ElementAtZoom<ImageData>> data = new ImageLoader().load(filename, fileZoom, targetZoom, flag);
54+
if (data.isEmpty()) SWT.error(SWT.ERROR_INVALID_IMAGE);
55+
return data.get(0);
56+
}
57+
5258
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +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-
}
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+
// }
5959
}

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

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

165+
List<ElementAtZoom<ImageData>> load(InputStream stream, int fileZoom, int targetZoom, int flag) {
166+
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
167+
reset();
168+
List<ElementAtZoom<ImageData>> images = InternalImageLoader.load(stream, this, fileZoom, targetZoom, flag);
169+
data = images.stream().map(ElementAtZoom::element).toArray(ImageData[]::new);
170+
return images;
171+
}
172+
165173
/**
166174
* Loads an array of <code>ImageData</code> objects from the
167175
* file with the specified name. Throws an error if either
@@ -195,6 +203,16 @@ List<ElementAtZoom<ImageData>> load(String filename, int fileZoom, int targetZoo
195203
return null;
196204
}
197205

206+
List<ElementAtZoom<ImageData>> load(String filename, int fileZoom, int targetZoom, int flag) {
207+
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
208+
try (InputStream stream = new FileInputStream(filename)) {
209+
return load(stream, fileZoom, targetZoom, flag);
210+
} catch (IOException e) {
211+
SWT.error(SWT.ERROR_IO, e);
212+
}
213+
return null;
214+
}
215+
198216
/**
199217
* Saves the image data in this ImageLoader to the specified stream.
200218
* The format parameter can have one of the following values:

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ static abstract class StaticImageFileFormat extends FileFormat {
8585
List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom) {
8686
return Arrays.stream(loadFromByteStream()).map(d -> new ElementAtZoom<>(d, fileZoom)).toList();
8787
}
88+
89+
@Override
90+
List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom, int flag) {
91+
return Arrays.stream(loadFromByteStream()).map(d -> new ElementAtZoom<>(d, fileZoom)).toList();
92+
}
8893
}
8994

9095
LEDataInputStream inputStream;
@@ -104,6 +109,12 @@ List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom)
104109
*/
105110
abstract List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom);
106111

112+
/**
113+
* Format that do not implement {@link StaticImageFileFormat} MUST return
114+
* {@link ImageData} with the specified {@code targetZoom}.
115+
*/
116+
abstract List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom, int flag);
117+
107118
/**
108119
* Read the specified input stream, and return the
109120
* device independent image array represented by the stream.
@@ -122,6 +133,20 @@ public List<ElementAtZoom<ImageData>> loadFromStream(LEDataInputStream stream, i
122133
}
123134
}
124135

136+
public List<ElementAtZoom<ImageData>> loadFromStream(LEDataInputStream stream, int fileZoom, int targetZoom, int flag) {
137+
try {
138+
inputStream = stream;
139+
return loadFromByteStream(fileZoom, targetZoom, flag);
140+
} catch (Exception e) {
141+
if (e instanceof IOException) {
142+
SWT.error(SWT.ERROR_IO, e);
143+
} else {
144+
SWT.error(SWT.ERROR_INVALID_IMAGE, e);
145+
}
146+
return null;
147+
}
148+
}
149+
125150
/**
126151
* Read the specified input stream using the specified loader, and
127152
* return the device independent image array represented by the stream.
@@ -136,6 +161,20 @@ public static List<ElementAtZoom<ImageData>> load(InputStream is, ImageLoader lo
136161
return fileFormat.loadFromStream(stream, fileZoom, targetZoom);
137162
}
138163

164+
/**
165+
* Read the specified input stream using the specified loader, and
166+
* return the device independent image array represented by the stream.
167+
*/
168+
public static List<ElementAtZoom<ImageData>> load(InputStream is, ImageLoader loader, int fileZoom, int targetZoom, int flag) {
169+
LEDataInputStream stream = new LEDataInputStream(is);
170+
FileFormat fileFormat = determineFileFormat(stream).orElseGet(() -> {
171+
SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
172+
return null;
173+
});
174+
fileFormat.loader = loader;
175+
return fileFormat.loadFromStream(stream, fileZoom, targetZoom, flag);
176+
}
177+
139178
/**
140179
* Write the device independent image array stored in the specified loader
141180
* to the specified output stream using the specified file format.

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,19 @@ boolean isFileFormat(LEDataInputStream stream) throws IOException {
4444

4545
@Override
4646
List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom) {
47+
return loadFromByteStream(fileZoom, targetZoom, SWT.IMAGE_COPY);
48+
}
49+
50+
@Override
51+
List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom, int flag) {
4752
if (RASTERIZER == null) {
4853
SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
4954
}
5055
if (targetZoom <= 0) {
5156
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
5257
}
5358
try {
54-
ImageData rasterizedImageData = RASTERIZER.rasterizeSVG(inputStream, 100 * targetZoom / fileZoom);
59+
ImageData rasterizedImageData = RASTERIZER.rasterizeSVG(inputStream, 100 * targetZoom / fileZoom, flag);
5560
return List.of(new ElementAtZoom<>(rasterizedImageData, targetZoom));
5661
} catch (IOException e) {
5762
SWT.error(SWT.ERROR_INVALID_IMAGE, e);

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -302,24 +302,14 @@ public Image(Device device, Image srcImage, int flag) {
302302
}
303303

304304
private boolean createWithSVG(Device device, int flag) {
305-
ImageData data = null;
306-
Image customizedImage = null;
307305
if (imageProvider.getProvider() instanceof ImageFileNameProvider imageFileNameProvider) {
308306
ElementAtZoom<String> fileName = DPIUtil.validateAndGetImagePathAtZoom(imageFileNameProvider, getZoom());
309307
if (fileName.element().endsWith(".svg")) {
310-
customizedImage = new Image(device, imageFileNameProvider, flag);
311-
}
312-
} else if (imageProvider.getProvider() instanceof ImageDataProvider imageDataProvider) {
313-
if (imageDataProvider.supportsRasterizationFlag(flag)) {
314-
customizedImage = new Image(device, imageDataProvider, flag);
308+
ElementAtZoom<ImageData> imageData = ImageDataLoader.load(fileName.element(), fileName.zoom(), getZoom(), flag);
309+
init(imageData.element(), getZoom());
310+
return true;
315311
}
316312
}
317-
if(customizedImage != null) {
318-
data = customizedImage.getImageData(customizedImage.getZoom());
319-
init(data, getZoom());
320-
customizedImage.dispose();
321-
return true;
322-
}
323313
return false;
324314
}
325315

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/InternalImageLoader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ static List<ElementAtZoom<ImageData>> load(InputStream stream, ImageLoader image
2525
return FileFormat.load(stream, imageLoader, fileZoom, targetZoom);
2626
}
2727

28+
static List<ElementAtZoom<ImageData>> load(InputStream stream, ImageLoader imageLoader, int fileZoom, int targetZoom, int flag) {
29+
return FileFormat.load(stream, imageLoader, fileZoom, targetZoom, flag);
30+
}
31+
2832
static void save(OutputStream stream, int format, ImageLoader imageLoader) {
2933
FileFormat.save(stream, format, imageLoader);
3034
}

0 commit comments

Comments
 (0)