1 /* 2 * Copyright (c) 2007, 2015, 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.*; 25 import java.awt.font.TextAttribute; 26 import java.awt.geom.AffineTransform; 27 import java.text.AttributedCharacterIterator; 28 import java.text.StringCharacterIterator; 29 import java.util.HashMap; 30 import java.util.Map; 31 32 /* 33 * @test 34 * @summary Check that Font constructors and methods do not throw 35 * unexpected exceptions in headless mode 36 * @run main/othervm -Djava.awt.headless=true HeadlessFont 37 */ 38 39 public class HeadlessFont { 40 41 public static void main(String args[]) { 42 HashMap attMap = new HashMap(); 43 attMap.put(TextAttribute.FAMILY, "Helvetica Bold"); 44 attMap.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_LIGHT); 45 attMap.put(TextAttribute.WIDTH, TextAttribute.WIDTH_REGULAR); 46 attMap.put(TextAttribute.SIZE, new Float(20)); 47 attMap.put(TextAttribute.FOREGROUND, Color.white); 48 attMap.put(TextAttribute.BACKGROUND, Color.black); 49 50 for (String font : Toolkit.getDefaultToolkit().getFontList()) { 51 for (int i = 8; i < 17; i++) { 52 Font f1 = new Font(font, Font.PLAIN, i); 53 Font f2 = new Font(font, Font.BOLD, i); 54 Font f3 = new Font(font, Font.ITALIC, i); 55 Font f4 = new Font(font, Font.BOLD | Font.ITALIC, i); 56 57 FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(f1); 58 metrics = Toolkit.getDefaultToolkit().getFontMetrics(f2); 59 metrics = Toolkit.getDefaultToolkit().getFontMetrics(f3); 60 metrics = Toolkit.getDefaultToolkit().getFontMetrics(f4); 61 62 AffineTransform trans = f1.getTransform(); 63 trans = f2.getTransform(); 64 trans = f3.getTransform(); 65 trans = f4.getTransform(); 66 67 String str; 68 str = f1.getFamily(); 69 str = f2.getFamily(); 70 str = f3.getFamily(); 71 str = f4.getFamily(); 72 73 str = f1.getPSName(); 74 str = f2.getPSName(); 75 str = f3.getPSName(); 76 str = f4.getPSName(); 77 78 str = f1.getName(); 79 str = f2.getName(); 80 str = f3.getName(); 81 str = f4.getName(); 82 83 str = f1.getFontName(); 84 str = f2.getFontName(); 85 str = f3.getFontName(); 86 str = f4.getFontName(); 87 88 str = f1.toString(); 89 str = f2.toString(); 90 str = f3.toString(); 91 str = f4.toString(); 92 93 int s; 94 s = f1.getStyle(); 95 s = f2.getStyle(); 96 s = f3.getStyle(); 97 s = f4.getStyle(); 98 99 s = f1.getSize(); 100 s = f2.getSize(); 101 s = f3.getSize(); 102 s = f4.getSize(); 103 104 s = f1.hashCode(); 105 s = f2.hashCode(); 106 s = f3.hashCode(); 107 s = f4.hashCode(); 108 109 s = f1.getNumGlyphs(); 110 s = f2.getNumGlyphs(); 111 s = f3.getNumGlyphs(); 112 s = f4.getNumGlyphs(); 113 114 s = f1.getMissingGlyphCode(); 115 s = f2.getMissingGlyphCode(); 116 s = f3.getMissingGlyphCode(); 117 s = f4.getMissingGlyphCode(); 118 119 float f; 120 f = f1.getSize2D(); 121 f = f2.getSize2D(); 122 f = f3.getSize2D(); 123 f = f4.getSize2D(); 124 125 126 byte b; 127 b = f1.getBaselineFor('c'); 128 b = f2.getBaselineFor('c'); 129 b = f3.getBaselineFor('c'); 130 b = f4.getBaselineFor('c'); 131 132 Map m = f1.getAttributes(); 133 m = f2.getAttributes(); 134 m = f3.getAttributes(); 135 m = f4.getAttributes(); 136 137 AttributedCharacterIterator.Attribute[] a; 138 a = f1.getAvailableAttributes(); 139 a = f2.getAvailableAttributes(); 140 a = f3.getAvailableAttributes(); 141 a = f4.getAvailableAttributes(); 142 143 144 Font fnt; 145 fnt = f1.deriveFont(Font.BOLD | Font.ITALIC, (float) 80); 146 fnt = f2.deriveFont(Font.BOLD | Font.ITALIC, (float) 80); 147 fnt = f3.deriveFont(Font.BOLD | Font.ITALIC, (float) 80); 148 fnt = f4.deriveFont(Font.BOLD | Font.ITALIC, (float) 80); 149 150 fnt = f1.deriveFont(80f); 151 fnt = f2.deriveFont(80f); 152 fnt = f3.deriveFont(80f); 153 fnt = f4.deriveFont(80f); 154 155 fnt = f1.deriveFont(Font.BOLD | Font.ITALIC); 156 fnt = f2.deriveFont(Font.BOLD | Font.ITALIC); 157 fnt = f3.deriveFont(Font.BOLD | Font.ITALIC); 158 fnt = f4.deriveFont(Font.BOLD | Font.ITALIC); 159 160 fnt = f1.deriveFont(attMap); 161 fnt = f2.deriveFont(attMap); 162 fnt = f3.deriveFont(attMap); 163 fnt = f4.deriveFont(attMap); 164 165 166 if (!f1.isPlain()) 167 throw new RuntimeException("Plain font " + f1.getName() + " says it's not plain"); 168 if (f2.isPlain()) 169 throw new RuntimeException("Bold font " + f1.getName() + " says it is plain"); 170 if (f3.isPlain()) 171 throw new RuntimeException("Italic font " + f1.getName() + " says it is plain"); 172 if (f4.isPlain()) 173 throw new RuntimeException("Bold|Italic font " + f1.getName() + " says it is plain"); 174 175 if (f1.isBold()) 176 throw new RuntimeException("Plain font " + f1.getName() + " says it is bold"); 177 if (!f2.isBold()) 178 throw new RuntimeException("Bold font " + f1.getName() + " says it's not bold"); 179 if (f3.isBold()) 180 throw new RuntimeException("Italic font " + f1.getName() + " says it is bold"); 181 if (!f4.isBold()) 182 throw new RuntimeException("Bold|Italic font " + f1.getName() + " says it's not bold"); 183 184 if (f1.isItalic()) 185 throw new RuntimeException("Plain font " + f1.getName() + " says it is italic"); 186 if (f2.isItalic()) 187 throw new RuntimeException("Bold font " + f1.getName() + " says it is italic"); 188 if (!f3.isItalic()) 189 throw new RuntimeException("Italic font " + f1.getName() + " says it's not italic"); 190 if (!f4.isItalic()) 191 throw new RuntimeException("Bold|Italic font " + f1.getName() + " says it's not italic"); 192 193 f1.canDisplay('~'); 194 f2.canDisplay('~'); 195 f3.canDisplay('~'); 196 f4.canDisplay('~'); 197 f1.canDisplay('c'); 198 f2.canDisplay('c'); 199 f3.canDisplay('c'); 200 f4.canDisplay('c'); 201 202 f1.canDisplayUpTo("canDisplayUpTo"); 203 f2.canDisplayUpTo("canDisplayUpTo"); 204 f3.canDisplayUpTo("canDisplayUpTo"); 205 f4.canDisplayUpTo("canDisplayUpTo"); 206 207 str = "canDisplayUpTo"; 208 f1.canDisplayUpTo(str.toCharArray(), 0, str.length()); 209 f2.canDisplayUpTo(str.toCharArray(), 0, str.length()); 210 f3.canDisplayUpTo(str.toCharArray(), 0, str.length()); 211 f4.canDisplayUpTo(str.toCharArray(), 0, str.length()); 212 213 f1.canDisplayUpTo(new StringCharacterIterator(str), 0, str.length()); 214 f2.canDisplayUpTo(new StringCharacterIterator(str), 0, str.length()); 215 f3.canDisplayUpTo(new StringCharacterIterator(str), 0, str.length()); 216 f4.canDisplayUpTo(new StringCharacterIterator(str), 0, str.length()); 217 218 f1.getItalicAngle(); 219 f2.getItalicAngle(); 220 f3.getItalicAngle(); 221 f4.getItalicAngle(); 222 223 f1.hasUniformLineMetrics(); 224 f2.hasUniformLineMetrics(); 225 f3.hasUniformLineMetrics(); 226 f4.hasUniformLineMetrics(); 227 } 228 } 229 230 Font f = new Font(attMap); 231 f = Font.getFont(attMap); 232 f = Font.decode(null); 233 } 234 }