|
| 1 | +/* |
| 2 | + * Copyright (c) 2001, 2025, 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.Color; |
| 25 | +import java.awt.Dimension; |
| 26 | +import java.awt.Font; |
| 27 | +import java.awt.FontMetrics; |
| 28 | +import java.awt.Frame; |
| 29 | +import java.awt.Graphics; |
| 30 | +import java.awt.Graphics2D; |
| 31 | +import java.awt.Insets; |
| 32 | +import java.awt.Panel; |
| 33 | +import java.awt.ScrollPane; |
| 34 | +import java.awt.font.FontRenderContext; |
| 35 | +import java.awt.font.TextLayout; |
| 36 | + |
| 37 | +/* |
| 38 | + * @test |
| 39 | + * @bug 4517298 |
| 40 | + * @summary Display special control characters using both TextLayout.draw and |
| 41 | + * Graphics.drawString. In no case should a missing glyph appear. |
| 42 | + * Also display the advance of the control characters, in all cases |
| 43 | + * these should be 0. The space character is also displayed as a reference. |
| 44 | + * Note, the character is rendered between '><' but owing to the directional |
| 45 | + * properties of two of the characters, the second '<' is rendered as '>'. |
| 46 | + * This is correct behavior. |
| 47 | + * @library /java/awt/regtesthelpers |
| 48 | + * @build PassFailJFrame |
| 49 | + * @run main/manual TestControls |
| 50 | + */ |
| 51 | + |
| 52 | +public class TestControls { |
| 53 | + private static String fontName = Font.DIALOG; |
| 54 | + |
| 55 | + public static void main(String[] args) throws Exception { |
| 56 | + final String INSTRUCTIONS = """ |
| 57 | + A number of control characters are displayed, one per line. |
| 58 | + Each line displays the hex value of the character, the character |
| 59 | + between '><' as rendered by TextLayout, the character between '><' |
| 60 | + as rendered by drawString, and the advance of the character. |
| 61 | + The first line renders the space character, as a reference. |
| 62 | + The following lines all render the controls. |
| 63 | + All controls should not render (even as space) and report a zero advance. |
| 64 | +
|
| 65 | + Pass the test if this is true. |
| 66 | +
|
| 67 | + Note: two of the control characters have the effect of changing the '<' |
| 68 | + following the control character so that it renders as '>'. |
| 69 | + This is not an error."""; |
| 70 | + |
| 71 | + PassFailJFrame.builder() |
| 72 | + .title("TestControls Instruction") |
| 73 | + .instructions(INSTRUCTIONS) |
| 74 | + .columns(45) |
| 75 | + .testUI(TestControls::createUI) |
| 76 | + .build() |
| 77 | + .awaitAndCheck(); |
| 78 | + } |
| 79 | + |
| 80 | + private static Frame createUI() { |
| 81 | + Frame f = new Frame("TestControls Test UI"); |
| 82 | + Panel panel = new ControlPanel(fontName); |
| 83 | + ScrollPane sp = new ScrollPane(); |
| 84 | + sp.add("Center", panel); |
| 85 | + f.add(sp); |
| 86 | + f.setSize(450, 400); |
| 87 | + return f; |
| 88 | + } |
| 89 | + |
| 90 | + static class ControlPanel extends Panel { |
| 91 | + |
| 92 | + static final char[] chars = { |
| 93 | + (char)0x0020, (char)0x0009, |
| 94 | + (char)0x000A, (char)0x000D, (char)0x200C, (char)0x200D, (char)0x200E, |
| 95 | + (char)0x200F, (char)0x2028, (char)0x2029, (char)0x202A, (char)0x202B, |
| 96 | + (char)0x202C, (char)0x202D, (char)0x202E, (char)0x206A, (char)0x206B, |
| 97 | + (char)0x206C, (char)0x206D, (char)0x206E, (char)0x206F |
| 98 | + }; |
| 99 | + |
| 100 | + ControlPanel(String fontName) { |
| 101 | + Font font = new Font(fontName, Font.PLAIN, 24); |
| 102 | + System.out.println("using font: " + font); |
| 103 | + setFont(font); |
| 104 | + setForeground(Color.BLACK); |
| 105 | + setBackground(Color.WHITE); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public Dimension getPreferredSize() { |
| 110 | + return new Dimension(400, 750); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public Dimension getMaximumSize() { |
| 115 | + return getPreferredSize(); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void paint(Graphics g) { |
| 120 | + Graphics2D g2d = (Graphics2D)g; |
| 121 | + FontRenderContext frc = g2d.getFontRenderContext(); |
| 122 | + Font font = g2d.getFont(); |
| 123 | + FontMetrics fm = g2d.getFontMetrics(); |
| 124 | + Insets insets = getInsets(); |
| 125 | + |
| 126 | + String jvmString = System.getProperty("java.version"); |
| 127 | + String osString = System.getProperty("os.name") + " / " + |
| 128 | + System.getProperty("os.arch") + " / " + |
| 129 | + System.getProperty("os.version"); |
| 130 | + |
| 131 | + int x = insets.left + 10; |
| 132 | + int y = insets.top; |
| 133 | + |
| 134 | + y += 30; |
| 135 | + g2d.drawString("jvm: " + jvmString, x, y); |
| 136 | + |
| 137 | + y += 30; |
| 138 | + g2d.drawString("os: " + osString, x, y); |
| 139 | + |
| 140 | + y += 30; |
| 141 | + g2d.drawString("font: " + font.getFontName(), x, y); |
| 142 | + |
| 143 | + for (int i = 0; i < chars.length; ++i) { |
| 144 | + String s = ">" + chars[i] + "<"; |
| 145 | + x = insets.left + 10; |
| 146 | + y += 30; |
| 147 | + |
| 148 | + g2d.drawString(Integer.toHexString(chars[i]), x, y); |
| 149 | + x += 100; |
| 150 | + |
| 151 | + new TextLayout(s, font, frc).draw(g2d, x, y); |
| 152 | + x += 100; |
| 153 | + |
| 154 | + g2d.drawString(s, x, y); |
| 155 | + x += 100; |
| 156 | + |
| 157 | + g2d.drawString(Integer.toString(fm.charWidth(chars[i])), x, y); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments