8208638: Instead of circle rendered in appl window, but ellipse is produced JEditor Pane
Reviewed-by: serb, psadhukhan
--- a/src/java.desktop/share/classes/javax/swing/text/html/ImageView.java Wed Sep 26 18:36:55 2018 +0100
+++ b/src/java.desktop/share/classes/javax/swing/text/html/ImageView.java Thu Sep 27 14:36:33 2018 +0530
@@ -776,32 +776,10 @@
if (newWidth > 0) {
newState |= WIDTH_FLAG;
- if (newHeight <= 0) {
- newHeight = newWidth;
- newState |= HEIGHT_FLAG;
- }
}
if (newHeight > 0) {
newState |= HEIGHT_FLAG;
- if (newWidth <= 0) {
- newWidth = newHeight;
- newState |= WIDTH_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:
@@ -965,6 +943,35 @@
changed |= 2;
}
+ /**
+ * If the image properties (height and width) have been loaded,
+ * then 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/jdk/javax/swing/JEditorPane/8195095/ImageViewTest.java Wed Sep 26 18:36:55 2018 +0100
+++ b/test/jdk/javax/swing/JEditorPane/8195095/ImageViewTest.java Thu Sep 27 14:36:33 2018 +0530
@@ -24,7 +24,7 @@
/**
* @test
* @key headful
- * @bug 8195095 8206238
+ * @bug 8195095 8206238 8208638
* @summary Tests if Images are scaled correctly in JEditorPane.
* @run main ImageViewTest
*/
@@ -51,8 +51,11 @@
f.add(editorPane);
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);
});