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