From c8849f4cb80f232ffc1c2d2954db1d2a60143732 Mon Sep 17 00:00:00 2001 From: kaddepalli Date: Thu, 9 May 2019 21:02:50 +0000 Subject: [PATCH] 8218674: HTML Tooltip with "img=src" on component doesn't show Summary: Make sure image is scaled appropriately if synchronous loading flag is set. Reviewed-by: serb, psadhukhan --- .../javax/swing/text/html/ImageView.java | 72 ++++++++++++----- .../text/html/8218674/TooltipImageTest.java | 76 ++++++++++++++++++ test/javax/swing/text/html/8218674/circle.png | Bin 0 -> 1573 bytes 3 files changed, 128 insertions(+), 20 deletions(-) create mode 100644 test/javax/swing/text/html/8218674/TooltipImageTest.java create mode 100644 test/javax/swing/text/html/8218674/circle.png diff --git a/src/share/classes/javax/swing/text/html/ImageView.java b/src/share/classes/javax/swing/text/html/ImageView.java index 7ac148920..e2b2bde11 100644 --- a/src/share/classes/javax/swing/text/html/ImageView.java +++ b/src/share/classes/javax/swing/text/html/ImageView.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2019, 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 @@ -734,6 +734,20 @@ public class ImageView extends View { newState |= HEIGHT_FLAG; } + /* + If synchronous loading flag is set, then make sure that the image is + scaled appropriately. + Otherwise, the ImageHandler::imageUpdate takes care of scaling the image + appropriately. + */ + if (getLoadsSynchronously()) { + Dimension d = adjustWidthHeight(image.getWidth(imageObserver), + image.getHeight(imageObserver)); + newWidth = d.width; + newHeight = d.height; + newState |= (WIDTH_FLAG | HEIGHT_FLAG); + } + // Make sure the image starts loading: if ((newState & (WIDTH_FLAG | HEIGHT_FLAG)) != 0) { Toolkit.getDefaultToolkit().prepareImage(newImage, newWidth, @@ -837,6 +851,40 @@ public class ImageView extends View { } } + private Dimension adjustWidthHeight(int newWidth, int newHeight) { + Dimension d = new Dimension(); + 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) { + newWidth = specifiedWidth; + newHeight = specifiedHeight; + } else if (specifiedWidth != -1 ^ specifiedHeight != -1) { + if (specifiedWidth <= 0) { + proportion = specifiedHeight / ((double)newHeight); + newWidth = (int)(proportion * newWidth); + newHeight = specifiedHeight; + } + + if (specifiedHeight <= 0) { + proportion = specifiedWidth / ((double)newWidth); + newHeight = (int)(proportion * newHeight); + newWidth = specifiedWidth; + } + } + + d.width = newWidth; + d.height = newHeight; + + return d; + } + /** * ImageHandler implements the ImageObserver to correctly update the * display as new parts of the image become available. @@ -902,26 +950,10 @@ public class ImageView extends View { */ 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); - } + Dimension d = adjustWidthHeight(newWidth, newHeight); + newWidth = d.width; + newHeight = d.height; changed |= 3; - } } synchronized(ImageView.this) { if ((changed & 1) == 1 && (state & HEIGHT_FLAG) == 0) { diff --git a/test/javax/swing/text/html/8218674/TooltipImageTest.java b/test/javax/swing/text/html/8218674/TooltipImageTest.java new file mode 100644 index 000000000..1a1b76838 --- /dev/null +++ b/test/javax/swing/text/html/8218674/TooltipImageTest.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2019, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * @test + * @key headful + * @bug 8218674 + * @summary Tests if Images are rendered and scaled correctly in JToolTip. + * @run main TooltipImageTest + */ +import java.awt.Dimension; +import java.awt.Insets; +import javax.swing.JToolTip; +import javax.swing.SwingUtilities; + +public class TooltipImageTest { + + private static void checkSize(JToolTip tip, int width, int height) { + Dimension d = tip.getPreferredSize(); + Insets insets = tip.getInsets(); + //6 seems to be the extra width being allocated for some reason + //for a tooltip window. + if (!((d.width - insets.right - insets.left - 6) == width) && + !((d.height - insets.top - insets.bottom) == height)) { + throw new RuntimeException("Test case fails: Expected width, height is " + width + ", " + height + + " whereas actual width, height are " + (d.width - insets.right - insets.left - 6) + " " + + (d.height - insets.top - insets.bottom)); + } + } + + public static void main(String[] args) throws Exception { + String PATH = TooltipImageTest.class.getResource("circle.png").getPath(); + SwingUtilities.invokeAndWait(() -> { + JToolTip tip = new JToolTip(); + tip.setTipText(""); + checkSize(tip, 100, 100); + + tip.setTipText(""); + checkSize(tip, 100, 100); + + tip.setTipText(""); + checkSize(tip, 200, 200); + + tip.setTipText(""); + checkSize(tip, 50, 50); + + tip.setTipText(""); + checkSize(tip, 50, 50); + + tip.setTipText(""); + checkSize(tip, 100, 50); + }); + + System.out.println("Test case passed."); + } +} diff --git a/test/javax/swing/text/html/8218674/circle.png b/test/javax/swing/text/html/8218674/circle.png new file mode 100644 index 0000000000000000000000000000000000000000..614f942dc5887617d274f4afe9094dc7ecc56fd5 GIT binary patch literal 1573 zcmbVMdo+}393GR~$UVlmRG6hQ6Eg{u7O`T+gqmiiZ`5kOhG>vWMs%^c4Kn3OMn|Yz z!kD8^n@bL1WELIDEfZNg2@MLNi`sTS{l9zm>^bl6ecs=Dp7TEM`}>{u$vo)it)*e2 zfj}U%C}d9>j9u%Yj)Z4R=RMspD4n5sdmw81%oK2<9OCZljzH9AZ~O|Xz&R?4%sPWW zXm_lK(r=NKK?sCO6UEb=9v}F2NFGydZh(+~`DQns1&&c}h8`qoG)`=-D6npOu{4oy z&<1^5JCbzoS#O7BI{T}v_hs0aV2y?kj$2;HO^D01>I)rhAvQ%r?^p*KZnf85S@ONV zNxDnV(NSFM)TVn^%JjCMZ;IqcWOLdKCotZGZ85k#q+t9Lk-BNMx%#oGlqf`*YP~C zmZvh=_>}i^zq4L?VhJvhW&|QCNPF@1m01LS{0vj9O%SlRS&L5-u8~7ltNREy2o;a3 zZHdL0r700dbH~1@`q?d|+@1GXTFFTchG;QgA{))DVr)1Oe?_@W1QI3afU}}@Vi(sJ zJ8XanDT+(uT58rMg9?}FbM(@W#dkYGv$;zHgJqaSi(!z<6(qQI44|dW$H(XVoZ6tX zA~LO^z3U@swDdH;-N+tq2rhPNw@k%S1+4YSdTzJVq%zc}I;Y1D*zB`yH5PJno<#D?175!$;{soE>6b!_F+vmb0$4~jW4ikb z^r?|9h<#)%95pYBTnIcdV4=Li2Dto7TQsS?y3bO;Dr4liPp0UtIocN34%*S6Ab^`C z(4^xuNww=-^+U+9jv{5Ke81@us?UgnB+?wJzlzoQHb3u73lk7Zn{-$POAd0L%S z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