changeset 13149:21056af0d1ea

8208638: Instead of circle rendered in appl window, but ellipse is produced JEditor Pane Reviewed-by: serb, psadhukhan
author rpatil
date Thu, 04 Oct 2018 05:55:01 -0400
parents 122a5802ac68
children de57c0414adf
files src/share/classes/javax/swing/text/html/ImageView.java test/javax/swing/JEditorPane/8195095/ImageViewTest.java
diffstat 2 files changed, 90 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/javax/swing/text/html/ImageView.java	Thu Oct 04 10:16:53 2018 +0100
+++ b/src/share/classes/javax/swing/text/html/ImageView.java	Thu Oct 04 05:55:01 2018 -0400
@@ -734,20 +734,6 @@
                 newState |= HEIGHT_FLAG;
             }
 
-            if (newWidth <= 0) {
-                newWidth = newImage.getWidth(imageObserver);
-                if (newWidth <= 0) {
-                    newWidth = DEFAULT_WIDTH;
-                }
-            }
-
-            if (newHeight <= 0) {
-                newHeight = newImage.getHeight(imageObserver);
-                if (newHeight <= 0) {
-                    newHeight = DEFAULT_HEIGHT;
-                }
-            }
-
             // Make sure the image starts loading:
             if ((newState & (WIDTH_FLAG | HEIGHT_FLAG)) != 0) {
                 Toolkit.getDefaultToolkit().prepareImage(newImage, newWidth,
@@ -909,6 +895,34 @@
                     changed |= 2;
                 }
 
+                /**
+                 * If the image properties (height and width) have been loaded,
+                 * tehn figure out if scaling is necessary based on the
+                 * specified HTML attributes.
+                 */
+                if (((flags & ImageObserver.HEIGHT) != 0) &&
+                     ((flags & ImageObserver.WIDTH) != 0)) {
+                    double proportion = 0.0;
+                    final int specifiedWidth = getIntAttr(HTML.Attribute.WIDTH, -1 );
+                    final int specifiedHeight = getIntAttr(HTML.Attribute.HEIGHT, -1);
+                    /**
+                     * If either of the attributes are not specified, then calculate the
+                     * proportion for the specified dimension wrt actual value, and then
+                     * apply the same proportion to the unspecified dimension as well,
+                     * so that the aspect ratio of the image is maintained.
+                     */
+                    if (specifiedWidth != -1 ^ specifiedHeight != -1) {
+                        if (specifiedWidth <= 0) {
+                            proportion = specifiedHeight / ((double)newHeight);
+                            newWidth = (int)(proportion * newWidth);
+                        }
+                        if (specifiedHeight <= 0) {
+                            proportion = specifiedWidth / ((double)newWidth);
+                            newHeight = (int)(proportion * newHeight);
+                        }
+                        changed |= 3;
+                    }
+                }
                 synchronized(ImageView.this) {
                     if ((changed & 1) == 1 && (state & HEIGHT_FLAG) == 0) {
                         height = newHeight;
--- a/test/javax/swing/JEditorPane/8195095/ImageViewTest.java	Thu Oct 04 10:16:53 2018 +0100
+++ b/test/javax/swing/JEditorPane/8195095/ImageViewTest.java	Thu Oct 04 05:55:01 2018 -0400
@@ -23,7 +23,7 @@
 
 /**
  * @test
- * @bug 8195095
+ * @bug 8195095 8208638
  * @summary Tests if Images are scaled correctly in JEditorPane.
  * @run main ImageViewTest
  */
@@ -39,25 +39,23 @@
 
 public class ImageViewTest {
 
-    private static final int WIDTH = 200;
-    private static final int HEIGHT = 200;
     private static JFrame f;
 
-    private static JEditorPane editorPane1;
-    private static JEditorPane editorPane2;
-    private static JEditorPane editorPane3;
-    private static JEditorPane editorPane4;
-
-    private static void test(Robot r, JEditorPane editorPane)  throws Exception {
+    private static void test(Robot r, JEditorPane editorPane,
+                            final int WIDTH, final int HEIGHT )  throws Exception {
 
         SwingUtilities.invokeAndWait(() -> {
             f = new JFrame();
             editorPane.setEditable(false);
             f.add(editorPane);
-            f.setSize(220,240);
+            f.setSize(WIDTH + 20, HEIGHT + 40);
             f.setLocationRelativeTo(null);
 
             f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
+            //This line will trigger the imageupdate, and consequently, the view
+            //will be populated with the appropriate color when the pixel color
+            //is queried by robot.
+            editorPane.getUI().getPreferredSize(editorPane);
             f.setVisible(true);
         });
 
@@ -108,34 +106,78 @@
 
         Robot r = new Robot();
 
+        final JEditorPane[] editorPanes = new JEditorPane[11];
+
         SwingUtilities.invokeAndWait(() -> {
-            editorPane1 = new JEditorPane("text/html",
+            editorPanes[0] = new JEditorPane("text/html",
                     "<img height=\"200\" src=\"file:///" + ABSOLUTE_FILE_PATH + "\"");
 
-            editorPane2 = new JEditorPane("text/html",
+            editorPanes[1] = new JEditorPane("text/html",
                     "<img width=\"200\" src=\"file:///" + ABSOLUTE_FILE_PATH + "\"");
 
-            editorPane3 = new JEditorPane("text/html",
+            editorPanes[2] = new JEditorPane("text/html",
                     "<img width=\"200\" height=\"200\" src=\"file:///" + ABSOLUTE_FILE_PATH + "\"");
 
-            editorPane4 = new JEditorPane("text/html",
+            editorPanes[3] = new JEditorPane("text/html",
                     "<img src=\"file:///" + ABSOLUTE_FILE_PATH + "\"");
 
+            editorPanes[4] = new JEditorPane("text/html",
+                    "<img width=\"100\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
+
+            editorPanes[5] = new JEditorPane("text/html",
+                    "<img height=\"100\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
+
+            editorPanes[6] = new JEditorPane("text/html",
+                    "<img width=\"100\" height=\"100\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
+
+            editorPanes[7] = new JEditorPane("text/html",
+                    "<img width=\"50\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
+
+            editorPanes[8] = new JEditorPane("text/html",
+                    "<img height=\"50\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
+
+            editorPanes[9] = new JEditorPane("text/html",
+                    "<img width=\"300\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
+
+            editorPanes[10] = new JEditorPane("text/html",
+                    "<img height=\"300\" src =\"file:///" + ABSOLUTE_FILE_PATH + "\"");
+
         });
 
         r.waitForIdle();
 
         System.out.println("Test with only height set to 200");
-        test(r, editorPane1);
+        test(r, editorPanes[0], 200, 200);
 
         System.out.println("Test with only width set to 200");
-        test(r, editorPane2);
+        test(r, editorPanes[1], 200, 200);
 
-        System.out.println("Test with none of them set");
-        test(r, editorPane3);
+        System.out.println("Test with both of them set");
+        test(r, editorPanes[2], 200, 200);
 
-        System.out.println("Test with both of them set to 200");
-        test(r, editorPane4);
+        System.out.println("Test with none of them set to 200");
+        test(r, editorPanes[3], 200, 200);
+
+        System.out.println("Test with only width set to 100");
+        test(r, editorPanes[4], 100, 100);
+
+        System.out.println("Test with only height set to 100");
+        test(r, editorPanes[5], 100, 100);
+
+        System.out.println("Test with both width and height set to 100");
+        test(r, editorPanes[6], 100, 100);
+
+        System.out.println("Test with only width set to 50");
+        test(r, editorPanes[7], 50, 50);
+
+        System.out.println("Test with only height set to 50");
+        test(r, editorPanes[8], 50, 50);
+
+        System.out.println("Test with only width set to 300");
+        test(r, editorPanes[9], 300, 300);
+
+        System.out.println("Test with only height set to 300");
+        test(r, editorPanes[10], 300, 300);
 
         System.out.println("Test Passed.");
     }