From 03d9e25265bef4fffd5e1bfdc0f0924b46350647 Mon Sep 17 00:00:00 2001 From: Krishna Addepalli Date: Thu, 12 Jul 2018 17:34:36 +0530 Subject: [PATCH] 8206238: Aspect ratio is not maintained when Image is scaled in JEditorPane Reviewed-by: prr, sveerabhadra --- .../javax/swing/text/html/ImageView.java | 52 +++++++++++-- .../JEditorPane/8195095/ImageViewTest.java | 78 ++++++++++++++----- 2 files changed, 103 insertions(+), 27 deletions(-) diff --git a/src/java.desktop/share/classes/javax/swing/text/html/ImageView.java b/src/java.desktop/share/classes/javax/swing/text/html/ImageView.java index 45bbe42f395..eab1a7e1507 100644 --- a/src/java.desktop/share/classes/javax/swing/text/html/ImageView.java +++ b/src/java.desktop/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, 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 @@ public class ImageView extends View { // 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) { diff --git a/test/jdk/javax/swing/JEditorPane/8195095/ImageViewTest.java b/test/jdk/javax/swing/JEditorPane/8195095/ImageViewTest.java index 7c98a882a83..4a23e8995b1 100644 --- a/test/jdk/javax/swing/JEditorPane/8195095/ImageViewTest.java +++ b/test/jdk/javax/swing/JEditorPane/8195095/ImageViewTest.java @@ -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 @@ import javax.swing.WindowConstants; 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 @@ public class ImageViewTest { Robot r = new Robot(); + final JEditorPane[] editorPanes = new JEditorPane[11]; + SwingUtilities.invokeAndWait(() -> { - editorPane1 = new JEditorPane("text/html", + editorPanes[0] = new JEditorPane("text/html", "