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/* @test 25 * @summary verify bounds enclose rendering of decorations. 26 * @bug 6751621 27 */ 28 29import java.awt.*; 30import java.awt.font.*; 31import java.awt.geom.*; 32import java.awt.image.*; 33import java.util.*; 34 35public class DecorationBoundsTest { 36 37 public static void main(String[] args) { 38 BufferedImage bi = 39 new BufferedImage(600, 300, BufferedImage.TYPE_INT_RGB); 40 Graphics2D g2d = bi.createGraphics(); 41 g2d.setColor(Color.white); 42 g2d.fillRect(0, 0, 600, 300); 43 44 float x = 10; 45 float y = 90; 46 Map map = new HashMap(); 47 map.put(TextAttribute.STRIKETHROUGH, 48 TextAttribute.STRIKETHROUGH_ON); 49 map.put(TextAttribute.SIZE, new Float(80)); 50 51 FontRenderContext frc = g2d.getFontRenderContext(); 52 53 String text = "Welcome to "; 54 TextLayout tl = new TextLayout(text, map, frc); 55 g2d.translate(x, y); 56 g2d.setColor(Color.RED); 57 tl.draw(g2d, 0, 0); 58 g2d.setColor(Color.GREEN); 59 Rectangle2D bds = tl.getBounds(); 60 /* Since due to pixelisation the glyphs may touch above 61 * or below the theoretical outline bounds, pad in the 62 * y direction to avoid spurious failures. 63 */ 64 bds.setRect(bds.getX(), bds.getY()-1, 65 bds.getWidth(), bds.getHeight()+2); 66 g2d.fill(bds); 67 68 map = new HashMap(); 69 map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON); 70 map.put(TextAttribute.SIZE, new Float(80)); 71 tl = new TextLayout(text, map, frc); 72 g2d.translate(0, 100); 73 g2d.setColor(Color.RED); 74 tl.draw(g2d, 0, 0); 75 76 g2d.setColor(Color.GREEN); 77 bds = tl.getBounds(); 78 bds.setRect(bds.getX(), bds.getY()-1, 79 bds.getWidth(), bds.getHeight()+2); 80 g2d.fill(bds); 81 82 checkBI(bi, Color.RED); 83 } 84 85 static void checkBI(BufferedImage bi, Color badColor) { 86 int badrgb = badColor.getRGB(); 87 int w = bi.getWidth(null); 88 int h = bi.getHeight(null); 89 for (int x=0; x<w; x++) { 90 for (int y=0; y<h; y++) { 91 int col = bi.getRGB(x, y); 92 if (col == badrgb) { 93 throw new RuntimeException("Got " + col); 94 } 95 } 96 } 97 } 98} 99