|
| 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 | + * @test |
| 25 | + * @bug 8231286 |
| 26 | + * @summary Verifies if HTML font size too large with high-DPI scaling and W3C_LENGTH_UNITS |
| 27 | + * @run main/othervm -Dsun.java2d.uiScale=1.0 HtmlFontSizeTest |
| 28 | + */ |
| 29 | + |
| 30 | +import java.awt.Dimension; |
| 31 | +import java.util.Locale; |
| 32 | + |
| 33 | +import javax.swing.JEditorPane; |
| 34 | +import javax.swing.SwingUtilities; |
| 35 | +import javax.swing.text.Document; |
| 36 | +import javax.swing.text.html.HTMLEditorKit; |
| 37 | + |
| 38 | +public class HtmlFontSizeTest { |
| 39 | + static volatile Dimension w3cFrameSize; |
| 40 | + static volatile Dimension stdFrameSize; |
| 41 | + |
| 42 | + private static Dimension test(boolean w3ccheck) { |
| 43 | + JEditorPane htmlPane = new JEditorPane(); |
| 44 | + htmlPane.setEditable(false); |
| 45 | + |
| 46 | + if (w3ccheck) { |
| 47 | + htmlPane.putClientProperty(JEditorPane.W3C_LENGTH_UNITS, Boolean.TRUE); |
| 48 | + } |
| 49 | + |
| 50 | + HTMLEditorKit kit = new HTMLEditorKit(); |
| 51 | + htmlPane.setEditorKit(kit); |
| 52 | + |
| 53 | + String htmlString = "<html>\n" |
| 54 | + + "<body style=\"font-family:SansSerif; font-size:16pt\">\n" |
| 55 | + + "<p>This should be 16 pt.</p>\n" |
| 56 | + + "</body>\n" |
| 57 | + + "</html>"; |
| 58 | + |
| 59 | + Document doc = kit.createDefaultDocument(); |
| 60 | + htmlPane.setDocument(doc); |
| 61 | + htmlPane.setText(htmlString); |
| 62 | + |
| 63 | + return htmlPane.getPreferredSize(); |
| 64 | + } |
| 65 | + |
| 66 | + public static void main(String[] args) throws Exception { |
| 67 | + SwingUtilities.invokeAndWait(() -> { |
| 68 | + w3cFrameSize = test(true); |
| 69 | + stdFrameSize = test(false); |
| 70 | + }); |
| 71 | + System.out.println("size with W3C:" + w3cFrameSize); |
| 72 | + System.out.println("size without W3C:" + stdFrameSize); |
| 73 | + |
| 74 | + float ratio = (float)w3cFrameSize.width / (float)stdFrameSize.width; |
| 75 | + System.out.println("w3cFrameSize.width/stdFrameSize.width " + ratio); |
| 76 | + |
| 77 | + if (!"1.3".equals(String.format(Locale.ENGLISH, "%.1f", ratio))) { |
| 78 | + throw new RuntimeException("HTML font size too large with high-DPI scaling and W3C_LENGTH_UNITS"); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments