|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import java.awt.BorderLayout; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.List; |
| 28 | +import javax.swing.JEditorPane; |
| 29 | +import javax.swing.JFrame; |
| 30 | +import javax.swing.JScrollPane; |
| 31 | +import javax.swing.SwingUtilities; |
| 32 | +import javax.swing.text.AbstractDocument.AbstractElement; |
| 33 | +import javax.swing.text.Document; |
| 34 | +import javax.swing.text.GlyphView; |
| 35 | +import javax.swing.text.View; |
| 36 | +import javax.swing.text.html.HTMLEditorKit; |
| 37 | +import javax.swing.text.html.StyleSheet; |
| 38 | + |
| 39 | +/* |
| 40 | + * @test |
| 41 | + * @bug 8260687 |
| 42 | + * @summary Tests inherited font-size is the same as explicitly specified |
| 43 | + * @run main BodyInheritedFontSize |
| 44 | + */ |
| 45 | +public class BodyInheritedFontSize { |
| 46 | + private static final String HTML_TEXT = """ |
| 47 | + <html> |
| 48 | + <body> |
| 49 | + <p style="font-size: 100%">100% from body</p> |
| 50 | + <p>16pt inherited from body</p> |
| 51 | + <p style="font-size: 16pt">16pt paragraph</p> |
| 52 | + </body> |
| 53 | + </html> |
| 54 | + """; |
| 55 | + |
| 56 | + private static JEditorPane createEditorPane(boolean w3cUnits, boolean showFrame) { |
| 57 | + JEditorPane htmlPane = new JEditorPane(); |
| 58 | + htmlPane.setEditable(false); |
| 59 | + |
| 60 | + if (w3cUnits) { |
| 61 | + htmlPane.putClientProperty(JEditorPane.W3C_LENGTH_UNITS, Boolean.TRUE); |
| 62 | + } |
| 63 | + |
| 64 | + HTMLEditorKit kit = new HTMLEditorKit(); |
| 65 | + htmlPane.setEditorKit(kit); |
| 66 | + |
| 67 | + StyleSheet styleSheet = kit.getStyleSheet(); |
| 68 | + styleSheet.addRule("body { font-family: sans-serif; font-size: 16pt; }"); |
| 69 | + |
| 70 | + Document doc = kit.createDefaultDocument(); |
| 71 | + htmlPane.setDocument(doc); |
| 72 | + htmlPane.setText(HTML_TEXT); |
| 73 | + |
| 74 | + if (showFrame) { |
| 75 | + JFrame frame = new JFrame("HtmlFontSizeGUITest: " |
| 76 | + + (w3cUnits ? "w3c" : "std")); |
| 77 | + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 78 | + frame.add(new JScrollPane(htmlPane), BorderLayout.CENTER); |
| 79 | + frame.setLocationRelativeTo(null); |
| 80 | + frame.pack(); |
| 81 | + frame.setVisible(true); |
| 82 | + } |
| 83 | + |
| 84 | + // Ignore the result but perform layout |
| 85 | + htmlPane.getPreferredSize(); |
| 86 | + |
| 87 | + return htmlPane; |
| 88 | + } |
| 89 | + |
| 90 | + public static void main(String[] args) throws Exception { |
| 91 | + final List<String> argsList = Arrays.asList(args); |
| 92 | + final boolean showFrame = toShowFrame(argsList); |
| 93 | + final boolean debugPrint = toDebugPrint(argsList); |
| 94 | + |
| 95 | + final List<Exception> exceptions = new ArrayList<>(2); |
| 96 | + SwingUtilities.invokeAndWait(() -> { |
| 97 | + for (boolean w3cUnits : new boolean[] {true, false}) { |
| 98 | + JEditorPane htmlPane = createEditorPane(w3cUnits, showFrame); |
| 99 | + try { |
| 100 | + checkFontSize(htmlPane, w3cUnits, debugPrint); |
| 101 | + } catch (Exception e) { |
| 102 | + exceptions.add(e); |
| 103 | + } |
| 104 | + } |
| 105 | + }); |
| 106 | + if (exceptions.size() > 0) { |
| 107 | + exceptions.forEach(System.err::println); |
| 108 | + throw new RuntimeException( |
| 109 | + "Test failed: " + exceptions.get(0).getMessage(), |
| 110 | + exceptions.get(0)); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private static boolean toShowFrame(final List<String> argsList) { |
| 115 | + return argsList.contains("-show"); |
| 116 | + } |
| 117 | + |
| 118 | + private static boolean toDebugPrint(final List<String> argsList) { |
| 119 | + return argsList.contains("-print"); |
| 120 | + } |
| 121 | + |
| 122 | + private static void checkFontSize(JEditorPane htmlPane, |
| 123 | + boolean w3cUnits, |
| 124 | + boolean debugPrint) { |
| 125 | + final View rootView = htmlPane.getUI().getRootView(htmlPane); |
| 126 | + final View boxView = rootView.getView(0); |
| 127 | + final View bodyView = boxView.getView(1); |
| 128 | + |
| 129 | + int fontSizePercentage = getViewFontSize(bodyView.getView(0), debugPrint); |
| 130 | + int fontSizeInherited = getViewFontSize(bodyView.getView(1), debugPrint); |
| 131 | + int fontSizeExplicit = getViewFontSize(bodyView.getView(2), debugPrint); |
| 132 | + if (debugPrint) { |
| 133 | + System.out.println("w3cUnits: " + w3cUnits + "\n" |
| 134 | + + "Percentage: " + fontSizePercentage + "\n" |
| 135 | + + "Inherited: " + fontSizeInherited + "\n" |
| 136 | + + "Explicit: " + fontSizeExplicit + "\n"); |
| 137 | + } |
| 138 | + if (fontSizeInherited != fontSizeExplicit |
| 139 | + || fontSizePercentage != fontSizeExplicit) { |
| 140 | + throw new RuntimeException("The font size is different with " |
| 141 | + + (w3cUnits ? "w3cUnits" : "stdUnits") + ": " |
| 142 | + + "Percentage: " + fontSizePercentage + " vs. " |
| 143 | + + "Inherited: " + fontSizeInherited + " vs. " |
| 144 | + + "Explicit: " + fontSizeExplicit); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private static int getViewFontSize(View paragraphView, boolean debugPrint) { |
| 149 | + GlyphView inlineView = findFirstTextRun(paragraphView); |
| 150 | + int fontSize = inlineView.getFont().getSize(); |
| 151 | + if (debugPrint) { |
| 152 | + ((AbstractElement) inlineView.getElement()).dump(System.out, 1); |
| 153 | + } |
| 154 | + return fontSize; |
| 155 | + } |
| 156 | + |
| 157 | + private static GlyphView findFirstTextRun(View view) { |
| 158 | + if (view instanceof GlyphView) { |
| 159 | + return (GlyphView) view; |
| 160 | + } |
| 161 | + for (int i = 0; i < view.getViewCount(); i++) { |
| 162 | + GlyphView textRun = findFirstTextRun(view.getView(i)); |
| 163 | + if (textRun != null) { |
| 164 | + return textRun; |
| 165 | + } |
| 166 | + } |
| 167 | + return null; |
| 168 | + } |
| 169 | +} |
0 commit comments