changeset 7649:063c15dcc93f

RT-37577: [Mac, Retina] Image loading for @2x images may try to append @2x suffix to wrong part of URL Reviewed-by: flar, snorthov
author vadim
date Sat, 09 Aug 2014 05:36:49 +0400
parents c1c737ec7c60
children de9dbcbc9885 c96c17dc9a3f
files modules/graphics/src/main/java/com/sun/javafx/iio/ImageStorage.java modules/graphics/src/main/java/com/sun/javafx/iio/common/ImageTools.java modules/graphics/src/test/java/com/sun/javafx/iio/ImageStorageTest.java
diffstat 3 files changed, 42 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/modules/graphics/src/main/java/com/sun/javafx/iio/ImageStorage.java	Fri Aug 08 14:28:26 2014 -0400
+++ b/modules/graphics/src/main/java/com/sun/javafx/iio/ImageStorage.java	Sat Aug 09 05:36:49 2014 +0400
@@ -299,14 +299,9 @@
             try {
                 if (pixelScale > 1.9f) {
                     // Use Mac Retina conventions for > 1.9f
-                    StringBuilder new_input = new StringBuilder();
-                    int last_dot_idx = input.lastIndexOf(".");
-                    if (last_dot_idx < 0) last_dot_idx = input.length();
-                    new_input.append(input.substring(0, last_dot_idx));
-                    new_input.append("@2x");
-                    new_input.append(input.substring(last_dot_idx));
                     try {
-                        theStream = ImageTools.createInputStream(new_input.toString());
+                        String name2x = ImageTools.getScaledImageName(input);
+                        theStream = ImageTools.createInputStream(name2x);
                     } catch (IOException e) {
                     }
                 }
--- a/modules/graphics/src/main/java/com/sun/javafx/iio/common/ImageTools.java	Fri Aug 08 14:28:26 2014 -0400
+++ b/modules/graphics/src/main/java/com/sun/javafx/iio/common/ImageTools.java	Sat Aug 09 05:36:49 2014 +0400
@@ -427,6 +427,23 @@
         return output;
     }
 
+    public static String getScaledImageName(String path) {
+        StringBuilder result = new StringBuilder();
+        int slash = path.lastIndexOf('/');
+        String name = (slash < 0) ? path : path.substring(slash + 1);
+        int dot = name.lastIndexOf(".");
+        if (dot < 0) {
+            dot = name.length();
+        }
+        if (slash >= 0) {
+            result.append(path.substring(0, slash + 1));
+        }
+        result.append(name.substring(0, dot));
+        result.append("@2x");
+        result.append(name.substring(dot));
+        return result.toString();
+    }
+
     public static InputStream createInputStream(String input) throws IOException {
         InputStream stream = null;
 
--- a/modules/graphics/src/test/java/com/sun/javafx/iio/ImageStorageTest.java	Fri Aug 08 14:28:26 2014 -0400
+++ b/modules/graphics/src/test/java/com/sun/javafx/iio/ImageStorageTest.java	Sat Aug 09 05:36:49 2014 +0400
@@ -25,7 +25,9 @@
 
 package com.sun.javafx.iio;
 
+import com.sun.javafx.iio.common.ImageTools;
 import static org.junit.Assert.assertNotNull;
+import org.junit.ComparisonFailure;
 import org.junit.Test;
 
 public class ImageStorageTest {
@@ -34,4 +36,25 @@
         String path = this.getClass().getResource("testpngnoextension").toString();
         assertNotNull(ImageStorage.loadAll(path, null, 0, 0, true, 2.0f, true));
     }
+
+    @Test
+    public void testImageNames() {
+        String [][]imageNames = new String[][] {
+            { "image", "image@2x" },
+            { "image.ext", "image@2x.ext" },
+            { "dir/image", "dir/image@2x" },
+            { "/dir.ext/image.ext", "/dir.ext/image@2x.ext" },
+            { "file:image", "file:image@2x" },
+            { "file:image.ext", "file:image@2x.ext" },
+            { "http://test.com/image", "http://test.com/image@2x" },
+            { "http://test.com/dir.ext/image", "http://test.com/dir.ext/image@2x" },
+            { "http://test.com/image.ext", "http://test.com/image@2x.ext" },
+            { "http://test.com/dir.ext/image.ext", "http://test.com/dir.ext/image@2x.ext" },
+        };
+        for (String[] names : imageNames) {
+            String name2x = ImageTools.getScaledImageName(names[0]);
+            if (name2x.equals(names[1])) continue;
+            throw new ComparisonFailure("Scaled image names don't match", names[1], name2x);
+        }
+    }
 }