1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 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;
}
}
|