Skip to content

Commit e005cf9

Browse files
committed
change functionality to utilize FileFormat changes of PR1828
1 parent a9ae7e1 commit e005cf9

File tree

3 files changed

+74
-15
lines changed

3 files changed

+74
-15
lines changed

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,8 @@ public ImageData(InputStream stream) {
361361
* code is still responsible for closing the input stream.
362362
*
363363
* @param stream the input stream to load the image from (must not be null)
364-
* @param zoom the zoom factor to apply when rasterizing an SVG.
365-
*
366-
* A value of 0 means that the standard method for loading should be used.
367-
* This case is equivalent to calling {@link ImageLoader#load(InputStream)}.
364+
* @param fileZoom the zoom of the file from which the <code>ImageData</code> is loaded
365+
* @param targetZoom the zoom for the returned <code>ImageData</code> (must be grater than 0)
368366
*
369367
* A value above 0 specifies a scaling factor for the output image. For example:
370368
* <ul>
@@ -386,10 +384,8 @@ public ImageData(InputStream stream) {
386384
* @see ImageLoader#load(InputStream)
387385
* @since 3.130
388386
*/
389-
public ImageData(InputStream stream, int zoom) {
390-
ImageData[] data = ImageDataLoader.load(stream, zoom);
391-
if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
392-
ImageData i = data[0];
387+
public ImageData(InputStream stream, int fileZoom, int targetZoom) {
388+
ImageData i = ImageDataLoader.load(stream, fileZoom, targetZoom).element();
393389
setAllFields(
394390
i.width,
395391
i.height,
@@ -462,9 +458,8 @@ public ImageData(String filename) {
462458
* image, or if the image has an unsupported type.
463459
*
464460
* @param filename the name of the file to load the image from (must not be null)
465-
* @param zoom the zoom factor to apply when rasterizing a SVG.
466-
* A value of 0 means that the standard method for loading should be used.
467-
* This case is equivalent to calling {@link ImageLoader#load(String)}.
461+
* @param fileZoom the zoom of the file from which the <code>ImageData</code> is loaded
462+
* @param targetZoom the zoom for the returned <code>ImageData</code> (must be grater than 0)
468463
*
469464
* A value above 0 specifies a scaling factor for the output image. For example:
470465
* <ul>
@@ -485,10 +480,8 @@ public ImageData(String filename) {
485480
*
486481
* @since 3.130
487482
*/
488-
public ImageData(String filename, int zoom) {
489-
ImageData[] data = ImageDataLoader.load(filename, zoom);
490-
if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE);
491-
ImageData i = data[0];
483+
public ImageData(String filename, int fileZoom, int targetZoom) {
484+
ImageData i = ImageDataLoader.load(filename, fileZoom, targetZoom).element();
492485
setAllFields(
493486
i.width,
494487
i.height,

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public abstract class FileFormat {
5353
try {
5454
FORMAT_FACTORIES.add(OS2BMPFileFormat::new);
5555
} catch (NoClassDefFoundError e) { } // ignore format
56+
try {
57+
FORMAT_FACTORIES.add(SVGFileFormat::new);
58+
} catch (NoClassDefFoundError e) { } // ignore format
5659
}
5760

5861
public static final int DEFAULT_ZOOM = 100;
@@ -84,6 +87,16 @@ List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom)
8487
}
8588
}
8689

90+
static abstract class DynamicImageFileFormat extends FileFormat {
91+
92+
abstract ImageData[] loadFromByteStream(int targetZoom);
93+
94+
@Override
95+
List<ElementAtZoom<ImageData>> loadFromByteStream(int fileZoom, int targetZoom) {
96+
return Arrays.stream(loadFromByteStream(targetZoom)).map(d -> new ElementAtZoom<>(d, fileZoom)).toList();
97+
}
98+
}
99+
87100
LEDataInputStream inputStream;
88101
LEDataOutputStream outputStream;
89102
ImageLoader loader;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Vector Informatik GmbH and others.
3+
*
4+
* This program and the accompanying materials are made available under the terms of the Eclipse
5+
* Public License 2.0 which accompanies this distribution, and is available at
6+
* https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors: Michael Bangas (Vector Informatik GmbH) - initial API and implementation
11+
*******************************************************************************/
12+
package org.eclipse.swt.internal.image;
13+
14+
import java.io.*;
15+
import java.nio.charset.*;
16+
import java.util.*;
17+
18+
import org.eclipse.swt.*;
19+
import org.eclipse.swt.graphics.*;
20+
import org.eclipse.swt.internal.*;
21+
import org.eclipse.swt.internal.image.FileFormat.*;
22+
23+
public class SVGFileFormat extends DynamicImageFileFormat {
24+
25+
/** The instance of the registered {@link SVGRasterizer}. */
26+
private static final SVGRasterizer RASTERIZER = ServiceLoader.load(SVGRasterizer.class).findFirst().orElse(null);
27+
28+
@Override
29+
boolean isFileFormat(LEDataInputStream stream) throws IOException {
30+
byte[] firstBytes = new byte[5];
31+
int bytesRead = stream.read(firstBytes);
32+
stream.unread(firstBytes);
33+
String header = new String(firstBytes, 0, bytesRead, StandardCharsets.UTF_8).trim();
34+
return header.startsWith("<?xml") || header.startsWith("<svg");
35+
}
36+
37+
@Override
38+
ImageData[] loadFromByteStream(int targetZoom) {
39+
try {
40+
if (RASTERIZER != null && targetZoom != 0) {
41+
return RASTERIZER.rasterizeSVG(inputStream, targetZoom);
42+
}
43+
} catch (IOException e) {
44+
SWT.error(SWT.ERROR_INVALID_IMAGE, e);
45+
}
46+
return null;
47+
}
48+
49+
@Override
50+
void unloadIntoByteStream(ImageLoader loader) {
51+
throw new UnsupportedOperationException();
52+
}
53+
}

0 commit comments

Comments
 (0)