8260687: Inherited font size is smaller than expected when using StyleSheet to add styles

Co-authored-by: Alexey Ivanov <aivanov@openjdk.org>
Reviewed-by: psadhukhan, aivanov, kizune
This commit is contained in:
Stanimir Stamenkov 2021-02-15 16:16:50 +00:00 committed by Alexey Ivanov
parent 3882fda83b
commit 2e610f53c1
3 changed files with 211 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2021, 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
@ -2813,7 +2813,10 @@ public class StyleSheet extends StyleContext {
((StyleConstants)key);
if (cssKey != null) {
Object value = doGetAttribute(cssKey);
if (value instanceof CSS.CssValue) {
if (value instanceof CSS.FontSize) {
return ((CSS.FontSize)value)
.getValue(this, StyleSheet.this);
} else if (value instanceof CSS.CssValue) {
return ((CSS.CssValue)value).toStyleConstants
((StyleConstants)key, host);
}

View File

@ -0,0 +1,169 @@
/*
* Copyright (c) 2021, 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.
*/
import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.text.AbstractDocument.AbstractElement;
import javax.swing.text.Document;
import javax.swing.text.GlyphView;
import javax.swing.text.View;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;
/*
* @test
* @bug 8260687
* @summary Tests inherited font-size is the same as explicitly specified
* @run main BodyInheritedFontSize
*/
public class BodyInheritedFontSize {
private static final String HTML_TEXT = """
<html>
<body>
<p style="font-size: 100%">100% from body</p>
<p>16pt inherited from body</p>
<p style="font-size: 16pt">16pt paragraph</p>
</body>
</html>
""";
private static JEditorPane createEditorPane(boolean w3cUnits, boolean showFrame) {
JEditorPane htmlPane = new JEditorPane();
htmlPane.setEditable(false);
if (w3cUnits) {
htmlPane.putClientProperty(JEditorPane.W3C_LENGTH_UNITS, Boolean.TRUE);
}
HTMLEditorKit kit = new HTMLEditorKit();
htmlPane.setEditorKit(kit);
StyleSheet styleSheet = kit.getStyleSheet();
styleSheet.addRule("body { font-family: sans-serif; font-size: 16pt; }");
Document doc = kit.createDefaultDocument();
htmlPane.setDocument(doc);
htmlPane.setText(HTML_TEXT);
if (showFrame) {
JFrame frame = new JFrame("HtmlFontSizeGUITest: "
+ (w3cUnits ? "w3c" : "std"));
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(new JScrollPane(htmlPane), BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
// Ignore the result but perform layout
htmlPane.getPreferredSize();
return htmlPane;
}
public static void main(String[] args) throws Exception {
final List<String> argsList = Arrays.asList(args);
final boolean showFrame = toShowFrame(argsList);
final boolean debugPrint = toDebugPrint(argsList);
final List<Exception> exceptions = new ArrayList<>(2);
SwingUtilities.invokeAndWait(() -> {
for (boolean w3cUnits : new boolean[] {true, false}) {
JEditorPane htmlPane = createEditorPane(w3cUnits, showFrame);
try {
checkFontSize(htmlPane, w3cUnits, debugPrint);
} catch (Exception e) {
exceptions.add(e);
}
}
});
if (exceptions.size() > 0) {
exceptions.forEach(System.err::println);
throw new RuntimeException(
"Test failed: " + exceptions.get(0).getMessage(),
exceptions.get(0));
}
}
private static boolean toShowFrame(final List<String> argsList) {
return argsList.contains("-show");
}
private static boolean toDebugPrint(final List<String> argsList) {
return argsList.contains("-print");
}
private static void checkFontSize(JEditorPane htmlPane,
boolean w3cUnits,
boolean debugPrint) {
final View rootView = htmlPane.getUI().getRootView(htmlPane);
final View boxView = rootView.getView(0);
final View bodyView = boxView.getView(1);
int fontSizePercentage = getViewFontSize(bodyView.getView(0), debugPrint);
int fontSizeInherited = getViewFontSize(bodyView.getView(1), debugPrint);
int fontSizeExplicit = getViewFontSize(bodyView.getView(2), debugPrint);
if (debugPrint) {
System.out.println("w3cUnits: " + w3cUnits + "\n"
+ "Percentage: " + fontSizePercentage + "\n"
+ "Inherited: " + fontSizeInherited + "\n"
+ "Explicit: " + fontSizeExplicit + "\n");
}
if (fontSizeInherited != fontSizeExplicit
|| fontSizePercentage != fontSizeExplicit) {
throw new RuntimeException("The font size is different with "
+ (w3cUnits ? "w3cUnits" : "stdUnits") + ": "
+ "Percentage: " + fontSizePercentage + " vs. "
+ "Inherited: " + fontSizeInherited + " vs. "
+ "Explicit: " + fontSizeExplicit);
}
}
private static int getViewFontSize(View paragraphView, boolean debugPrint) {
GlyphView inlineView = findFirstTextRun(paragraphView);
int fontSize = inlineView.getFont().getSize();
if (debugPrint) {
((AbstractElement) inlineView.getElement()).dump(System.out, 1);
}
return fontSize;
}
private static GlyphView findFirstTextRun(View view) {
if (view instanceof GlyphView) {
return (GlyphView) view;
}
for (int i = 0; i < view.getViewCount(); i++) {
GlyphView textRun = findFirstTextRun(view.getView(i));
if (textRun != null) {
return textRun;
}
}
return null;
}
}

View File

@ -26,6 +26,7 @@ import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import javax.imageio.ImageIO;
@ -39,10 +40,11 @@ import javax.swing.text.View;
* @bug 8257664
* @summary Tests inherited font-size with parent percentage specification.
* @run main TestWrongCSSFontSize
* @run main TestWrongCSSFontSize -w3cUnits
*/
public class TestWrongCSSFontSize {
private static String text =
private static final String TEXT =
"<html><head><style>" +
"body { font-size: 14 }" +
"div span { font-size: 150% }" +
@ -61,15 +63,24 @@ public class TestWrongCSSFontSize {
"</body></html>";
private static int expectedFontSize = 21;
private static int expectedAssertions = 8;
private static final int expectedFontSize = 21;
private static final int expectedAssertions = 8;
private final boolean w3cUnits;
private JEditorPane editor;
public TestWrongCSSFontSize(boolean w3cUnits) {
this.w3cUnits = w3cUnits;
}
public void setUp() {
editor = new JEditorPane();
editor.setContentType("text/html");
editor.setText(text);
if (w3cUnits) {
editor.putClientProperty(JEditorPane.W3C_LENGTH_UNITS, Boolean.TRUE);
}
editor.setText(TEXT);
editor.setSize(editor.getPreferredSize()); // layout
}
@ -77,7 +88,8 @@ public class TestWrongCSSFontSize {
int count = forEachTextRun(editor.getUI()
.getRootView(editor), this::assertFontSize);
if (count != expectedAssertions) {
throw new AssertionError("assertion count expected ["
throw new AssertionError((w3cUnits ? "w3cUnits - " : "")
+ "assertion count expected ["
+ expectedAssertions + "] but found [" + count + "]");
}
}
@ -104,7 +116,8 @@ public class TestWrongCSSFontSize {
printSource(child);
int actualFontSize = child.getFont().getSize();
if (actualFontSize != expectedFontSize) {
throw new AssertionError("font size expected ["
throw new AssertionError((w3cUnits ? "w3cUnits - " : "")
+ "font size expected ["
+ expectedFontSize + "] but found [" + actualFontSize +"]");
}
}
@ -119,7 +132,7 @@ public class TestWrongCSSFontSize {
}
}
private static void captureImage(Component comp, String path) {
private static void captureImage(Component comp, String suffix) {
try {
BufferedImage capture = new BufferedImage(comp.getWidth(),
comp.getHeight(), BufferedImage.TYPE_INT_ARGB);
@ -127,14 +140,16 @@ public class TestWrongCSSFontSize {
comp.paint(g);
g.dispose();
ImageIO.write(capture, "png", new File(path));
ImageIO.write(capture, "png",
new File(TestWrongCSSFontSize.class
.getSimpleName() + suffix + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Throwable {
TestWrongCSSFontSize test = new TestWrongCSSFontSize();
TestWrongCSSFontSize test = new TestWrongCSSFontSize(argW3CUnits(args));
AtomicReference<Throwable> failure = new AtomicReference<>();
SwingUtilities.invokeAndWait(() -> {
try {
@ -143,8 +158,11 @@ public class TestWrongCSSFontSize {
} catch (Throwable e) {
failure.set(e);
} finally {
if (args.length == 1) {
captureImage(test.editor, args[0]);
String suffix = test.w3cUnits ? "-w3cUnits" : "";
if (failure.get() != null) {
captureImage(test.editor, suffix + "-failure");
} else if (argCapture(args)) {
captureImage(test.editor, suffix + "-success");
}
}
});
@ -153,4 +171,12 @@ public class TestWrongCSSFontSize {
}
}
private static boolean argW3CUnits(String[] args) {
return Arrays.asList(args).contains("-w3cUnits");
}
private static boolean argCapture(String[] args) {
return Arrays.asList(args).contains("-capture");
}
}