Skip to content

Commit cf7f9db

Browse files
ShahzaibIbrahimHeikoKlare
authored andcommitted
Delegating Image(Display, String) to ImageFileNameProvider to init Image
In GTK and Cocoa, an image based on an SVG passed as filename to Image(Device, String) will be drawn now sharply with this change. Contributes to #2917
1 parent 8efbd10 commit cf7f9db

File tree

2 files changed

+30
-27
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT

2 files changed

+30
-27
lines changed

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

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -740,11 +740,7 @@ public Image(Device device, String filename) {
740740
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
741741
try {
742742
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
743-
initNative(filename);
744-
if (this.handle == null) {
745-
initWithSupplier(zoom -> ImageDataLoader.canLoadAtZoom(filename, FileFormat.DEFAULT_ZOOM, zoom),
746-
zoom -> ImageDataLoader.loadByZoom(filename, FileFormat.DEFAULT_ZOOM, zoom).element());
747-
}
743+
initUsingFileNameProvider(zoom -> zoom == 100 ? filename : null);
748744
init();
749745
} finally {
750746
if (pool != null) pool.release();
@@ -783,34 +779,42 @@ public Image(Device device, String filename) {
783779
public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
784780
super(device);
785781
if (imageFileNameProvider == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
786-
this.imageFileNameProvider = imageFileNameProvider;
787-
String filename = imageFileNameProvider.getImagePath(100);
788-
if (filename == null) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
789782
NSAutoreleasePool pool = null;
790783
if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
791784
try {
792-
initNative(filename);
793-
if (this.handle == null) init(ImageDataLoader.loadByZoom(filename, 100, 100).element(), 100);
785+
initUsingFileNameProvider(imageFileNameProvider);
794786
init();
795-
String filename2x = imageFileNameProvider.getImagePath(200);
796-
if (filename2x != null) {
797-
alphaInfo_200 = new AlphaInfo();
798-
id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(filename2x));
799-
NSImageRep rep = new NSImageRep(id);
800-
handle.addRepresentation(rep);
801-
} else if (ImageDataLoader.canLoadAtZoom(filename, 100, 200)) {
802-
// Try to natively scale up the image (e.g. possible if it's an SVG)
803-
ImageData imageData2x = ImageDataLoader.loadByZoom(filename, 100, 200).element();
804-
alphaInfo_200 = new AlphaInfo();
805-
NSBitmapImageRep rep = createRepresentation (imageData2x, alphaInfo_200);
806-
handle.addRepresentation(rep);
807-
rep.release();
808-
}
809787
} finally {
810788
if (pool != null) pool.release();
811789
}
812790
}
813791

792+
private void initUsingFileNameProvider(ImageFileNameProvider imageFileNameProvider) {
793+
this.imageFileNameProvider = imageFileNameProvider;
794+
String filename = imageFileNameProvider.getImagePath(100);
795+
if (filename == null) {
796+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
797+
}
798+
initNative(filename);
799+
if (this.handle == null) {
800+
init(ImageDataLoader.loadByZoom(filename, 100, 100).element(), 100);
801+
}
802+
String filename2x = imageFileNameProvider.getImagePath(200);
803+
if (filename2x != null) {
804+
alphaInfo_200 = new AlphaInfo();
805+
id id = NSImageRep.imageRepWithContentsOfFile(NSString.stringWith(filename2x));
806+
NSImageRep rep = new NSImageRep(id);
807+
handle.addRepresentation(rep);
808+
} else if (ImageDataLoader.canLoadAtZoom(filename, 100, 200)) {
809+
// Try to natively scale up the image (e.g. possible if it's an SVG)
810+
ImageData imageData2x = ImageDataLoader.loadByZoom(filename, 100, 200).element();
811+
alphaInfo_200 = new AlphaInfo();
812+
NSBitmapImageRep rep = createRepresentation(imageData2x, alphaInfo_200);
813+
handle.addRepresentation(rep);
814+
rep.release();
815+
}
816+
}
817+
814818
/**
815819
* Constructs an instance of this class by loading its representation
816820
* from the ImageData retrieved from the ImageDataProvider. Throws an

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,10 +596,9 @@ public Image(Device device, InputStream stream) {
596596
public Image(Device device, String filename) {
597597
super(device);
598598
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
599+
this.imageFileNameProvider = zoom -> zoom == 100 ? filename : null;
599600
currentDeviceZoom = DPIUtil.getDeviceZoom();
600-
ElementAtZoom<ImageData> image = ImageDataLoader.loadByZoom(filename, FileFormat.DEFAULT_ZOOM, currentDeviceZoom);
601-
ImageData data = DPIUtil.scaleImageData(device, image, currentDeviceZoom);
602-
init(data);
601+
initFromFileNameProvider(currentDeviceZoom);
603602
init();
604603
}
605604

0 commit comments

Comments
 (0)