8206238: Aspect ratio is not maintained when Image is scaled in JEditorPane
authorkaddepalli
Thu, 12 Jul 2018 17:34:36 +0530
changeset 51067 0961485fc686
parent 51066 5bf28fee65c1
child 51068 2e675859332a
8206238: Aspect ratio is not maintained when Image is scaled in JEditorPane Reviewed-by: prr, sveerabhadra
src/java.desktop/share/classes/javax/swing/text/html/ImageView.java
test/jdk/javax/swing/JEditorPane/8195095/ImageViewTest.java
--- a/src/java.desktop/share/classes/javax/swing/text/html/ImageView.java	Thu Jul 12 13:40:20 2018 +0200
+++ b/src/java.desktop/share/classes/javax/swing/text/html/ImageView.java	Thu Jul 12 17:34:36 2018 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,13 +24,41 @@
  */
 package javax.swing.text.html;
 
-import java.awt.*;
+import java.awt.Rectangle;
+import java.awt.Image;
+import java.awt.Container;
+import java.awt.Color;
+import java.awt.Shape;
+import java.awt.Graphics;
+import java.awt.Toolkit;
+
 import java.awt.image.ImageObserver;
-import java.net.*;
+import java.net.URL;
+import java.net.MalformedURLException;
+
 import java.util.Dictionary;
-import javax.swing.*;
-import javax.swing.text.*;
-import javax.swing.event.*;
+
+import javax.swing.GrayFilter;
+import javax.swing.ImageIcon;
+import javax.swing.Icon;
+import javax.swing.UIManager;
+import javax.swing.SwingUtilities;
+
+import javax.swing.text.JTextComponent;
+import javax.swing.text.StyledDocument;
+import javax.swing.text.View;
+import javax.swing.text.AttributeSet;
+import javax.swing.text.Element;
+import javax.swing.text.ViewFactory;
+import javax.swing.text.Position;
+import javax.swing.text.Segment;
+import javax.swing.text.Highlighter;
+import javax.swing.text.LayeredHighlighter;
+import javax.swing.text.AbstractDocument;
+import javax.swing.text.Document;
+import javax.swing.text.BadLocationException;
+
+import javax.swing.event.DocumentEvent;
 
 /**
  * View of an Image, intended to support the HTML <IMG> tag.
@@ -744,12 +772,22 @@
             // anything that might cause the image to be loaded, and thus the
             // ImageHandler to be called.
             newWidth = getIntAttr(HTML.Attribute.WIDTH, -1);
+            newHeight = getIntAttr(HTML.Attribute.HEIGHT, -1);
+
             if (newWidth > 0) {
                 newState |= WIDTH_FLAG;
+                if (newHeight <= 0) {
+                    newHeight = newWidth;
+                    newState |= HEIGHT_FLAG;
+                }
             }
-            newHeight = getIntAttr(HTML.Attribute.HEIGHT, -1);
+
             if (newHeight > 0) {
                 newState |= HEIGHT_FLAG;
+                if (newWidth <= 0) {
+                    newWidth = newHeight;
+                    newState |= WIDTH_FLAG;
+                }
             }
 
             if (newWidth <= 0) {
--- a/test/jdk/javax/swing/JEditorPane/8195095/ImageViewTest.java	Thu Jul 12 13:40:20 2018 +0200
+++ b/test/jdk/javax/swing/JEditorPane/8195095/ImageViewTest.java	Thu Jul 12 17:34:36 2018 +0530
@@ -24,7 +24,7 @@
 /**
  * @test
  * @key headful
- * @bug 8195095
+ * @bug 8195095 8206238
  * @summary Tests if Images are scaled correctly in JEditorPane.
  * @run main ImageViewTest
  */
@@ -40,22 +40,16 @@
 
 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);
@@ -109,34 +103,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 both of them set");
+        test(r, editorPanes[2], 200, 200);
+
+        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 none of them set");
-        test(r, editorPane3);
+        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 both of them set to 200");
-        test(r, editorPane4);
+        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.");
     }