1 /* 2 * Copyright (c) 2008, 2016, 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 /** 25 * @test 26 * @key headful 27 * @bug 6684056 28 * @summary Super-scripted text needs to be positioned the same with 29 * drawString and TextLayout. 30 */ 31 import java.awt.*; 32 import java.awt.event.*; 33 import java.awt.font.*; 34 import static java.awt.font.TextAttribute.*; 35 import java.awt.geom.AffineTransform; 36 import java.awt.image.BufferedImage; 37 import java.util.HashMap; 38 39 40 public class DrawStrSuper extends Component { 41 42 int angle = 0; 43 static boolean interactive = false; 44 45 int wid=400, hgt=400; 46 BufferedImage bi = null; 47 48 void paintImage() { 49 50 if (bi == null) { 51 bi = new BufferedImage(wid, hgt, BufferedImage.TYPE_INT_RGB); 52 } 53 Graphics2D g2d = bi.createGraphics(); 54 g2d.setColor(Color.white); 55 g2d.fillRect(0, 0, wid, hgt); 56 g2d.translate(200, 200); 57 58 Font fnt = new Font("Arial", Font.PLAIN, 20); 59 fnt = fnt.deriveFont(60.0f); 60 HashMap attrMap = new HashMap(); 61 AffineTransform aff = 62 AffineTransform.getRotateInstance(angle * Math.PI/180.0); 63 attrMap.put(SUPERSCRIPT, SUPERSCRIPT_SUPER); 64 attrMap.put(TRANSFORM, aff); 65 fnt = fnt.deriveFont(attrMap); 66 67 g2d.setFont(fnt); 68 g2d.setColor(Color.yellow); 69 TextLayout tl = new TextLayout("Text", fnt,g2d.getFontRenderContext()); 70 g2d.fill(tl.getBounds()); 71 72 g2d.setColor(Color.black); 73 g2d.drawLine(-3, 0, 3, 0); 74 g2d.drawLine(0, -3, 0, 3); 75 76 g2d.setColor(Color.blue); 77 g2d.drawString("Text", 0, 0); 78 79 g2d.setColor(Color.red); 80 tl.draw(g2d,0f,0f); 81 82 // Test BI: should be no blue 83 int blue = Color.blue.getRGB(); 84 for (int px=0;px<wid;px++) { 85 for (int py=0;py<hgt;py++) { 86 int rgb = bi.getRGB(px, py); 87 if (rgb == blue) { 88 throw new RuntimeException 89 ("Unexpected color : " + Integer.toHexString(rgb) + 90 " at x=" + px + " y="+ py); 91 } 92 } 93 } 94 } 95 96 @Override 97 public void paint(Graphics g) { 98 paintImage(); 99 g.drawImage(bi, 0,0, null); 100 } 101 102 103 static class Runner extends Thread { 104 105 DrawStrSuper dss; 106 107 Runner(DrawStrSuper dss) { 108 this.dss = dss; 109 } 110 111 public void run() { 112 while (true) { 113 if (!interactive && dss.angle > 360) { 114 return; 115 } 116 try { 117 Thread.sleep(100); 118 } catch (InterruptedException e) { 119 return; 120 } 121 122 dss.angle += 10; 123 dss.repaint(); 124 } 125 } 126 } 127 128 @Override 129 public Dimension getPreferredSize() { 130 return new Dimension(400, 400); 131 } 132 133 public static void main(String argv[]) throws InterruptedException { 134 if (argv.length > 0) interactive = true; 135 136 Frame f = new Frame("Text bounds test"); 137 f.addWindowListener(new WindowAdapter() { 138 @Override 139 public void windowClosing(WindowEvent e) { 140 System.exit(0); 141 } 142 }); 143 DrawStrSuper dss = new DrawStrSuper(); 144 f.add(dss, BorderLayout.CENTER); 145 f.pack(); 146 f.setLocationRelativeTo(null); 147 f.setVisible(true); 148 Runner runner = new Runner(dss); 149 runner.start(); 150 runner.join(); 151 } 152 }