Path: blob/master/test/jdk/java/awt/Graphics2D/DrawString/RotTransText.java
41153 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 6683472 6687298 819779726* @summary Transformed fonts using drawString and TextLayout should be in27* the same position.28*/2930import java.awt.*;31import java.awt.font.*;32import java.awt.geom.*;33import java.awt.image.*;34import java.util.HashMap;3536public class RotTransText {3738public static void main(String[] args) {3940testIt(false);41testIt(true);4243}4445public static void testIt(boolean fmOn) {4647int wid=400, hgt=400;48BufferedImage bi =49new BufferedImage(wid, hgt, BufferedImage.TYPE_INT_RGB);5051Graphics2D g2d = bi.createGraphics();5253if (fmOn) {54g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,55RenderingHints.VALUE_FRACTIONALMETRICS_ON);56}5758int x=130, y=130;59String s = "Text";6061int xt=90, yt=50;62for (int angle=0;angle<360;angle+=30) {6364g2d.setColor(Color.white);65g2d.fillRect(0, 0, wid, hgt);6667AffineTransform aff = AffineTransform.getTranslateInstance(50,90);68aff.rotate(angle * Math.PI/180.0);6970Font fnt = new Font("SansSerif", Font.PLAIN, 60);71fnt = fnt.deriveFont(Font.PLAIN, aff);72g2d.setFont(fnt);73g2d.setColor(Color.blue);74g2d.drawString(s, x, y);7576g2d.setColor(Color.red);77FontRenderContext frc = g2d.getFontRenderContext();78HashMap attrMap = new HashMap();79attrMap.put(TextAttribute.STRIKETHROUGH,80TextAttribute.STRIKETHROUGH_ON);81fnt = fnt.deriveFont(attrMap);82TextLayout tl = new TextLayout(s, fnt, frc);83tl.draw(g2d, (float)x, (float)y);8485// Test BI: should be minimal blue relative to red.86int redCount = 0;87int blueCount = 0;88int red = Color.red.getRGB();89int blue = Color.blue.getRGB();90for (int px=0;px<wid;px++) {91for (int py=0;py<hgt;py++) {92int rgb = bi.getRGB(px, py);93if (rgb == red) {94redCount++;95} else if (rgb == blue) {96blueCount++;97}98}99}100if (redCount == 0 || (blueCount/(double)redCount) > 0.1) {101throw new102RuntimeException("Ratio of blue to red is too great: " +103(blueCount/(double)redCount));104}105}106}107}108109110