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 outline and stroking of underline match.
26  * @bug 6751616
27  */
28 
29 import java.awt.*;
30 import java.awt.font.*;
31 import java.awt.geom.*;
32 import java.awt.image.*;
33 import java.util.*;
34 
35 public class UnderlinePositionTest {
36 
main(String[] args)37     public static void main(String[] args) {
38         BufferedImage bi =
39            new BufferedImage(600, 150, BufferedImage.TYPE_INT_RGB);
40        Graphics2D g2d = bi.createGraphics();
41        g2d.setColor(Color.white);
42        g2d.fillRect(0, 0, 600, 150);
43 
44        float x = 10;
45        float y = 90;
46        Map map = new HashMap();
47        map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
48        map.put(TextAttribute.SIZE, new Float(80));
49 
50        FontRenderContext frc = g2d.getFontRenderContext();
51 
52        // Use all spaces for the text so we know we are dealing
53        // only with pixels from the underline.
54        String text = "           ";
55        TextLayout tl = new TextLayout(text, map, frc);
56        Shape outline = tl.getOutline(null);
57        Rectangle2D bounds = outline.getBounds();
58 
59        g2d.translate(x, y);
60        g2d.setColor(Color.RED);
61        tl.draw(g2d, 0, 0);
62 
63        /* By getting the outline, then its bounds, then filling
64         * according to the same pixelisation rules, this ought to
65         * match the position of the original underline. If any
66         * red pixels are left, then the test will fail.
67         */
68        g2d.setColor(Color.BLUE);
69        g2d.fill(bounds);
70        g2d.dispose();
71 
72        checkBI(bi, Color.RED);
73    }
74 
checkBI(BufferedImage bi, Color badColor)75    static void checkBI(BufferedImage bi, Color badColor) {
76       int badrgb = badColor.getRGB();
77       int w = bi.getWidth(null);
78       int h = bi.getHeight(null);
79       for (int x=0; x<w; x++) {
80           for (int y=0; y<h; y++) {
81              int col = bi.getRGB(x, y);
82              if (col == badrgb) {
83                   throw new RuntimeException("Got " + col);
84              }
85           }
86       }
87    }
88 }
89