1 /*
2  * Copyright (c) 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  * @bug 8139176
27  * @summary Test layout uses correct styled font.
28  * @run main StyledFontLayoutTest
29  */
30 
31 import java.awt.Color;
32 import java.awt.Font;
33 import java.awt.Graphics;
34 import java.awt.Graphics2D;
35 import java.awt.RenderingHints;
36 import java.awt.font.FontRenderContext;
37 import java.awt.font.GlyphVector;
38 import java.awt.geom.Rectangle2D;
39 import java.awt.image.BufferedImage;
40 
41 import javax.swing.JFrame;
42 import javax.swing.JPanel;
43 import javax.swing.SwingUtilities;
44 import javax.swing.WindowConstants;
45 
46 public class StyledFontLayoutTest extends JPanel {
47 
48     static final int W=600, H=400;
49     static boolean interactive;
50     static BufferedImage im;
main(String[] args)51     public static void main(String[] args) {
52 
53         interactive = args.length > 0;
54 
55         runTest();
56 
57         if (!interactive) {
58             return;
59         }
60         SwingUtilities.invokeLater(() -> {
61             JFrame frame = new JFrame("Styled Font Layout Test");
62             frame.add(new StyledFontLayoutTest());
63             frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
64             frame.setSize(W, H);
65             frame.setLocationRelativeTo(null);
66             frame.setVisible(true);
67         });
68     }
69 
70     @Override
paintComponent(Graphics g)71     protected void paintComponent(Graphics g) {
72         g.drawImage(im, 0, 0, null);
73     }
74 
runTest()75     private static void runTest() {
76         im = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);
77         Graphics2D g2d = im.createGraphics();
78         g2d.setColor(Color.white);
79         g2d.fillRect(0, 0, W, H);
80         g2d.setColor(Color.black);
81         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
82                              RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
83         char[] chs = "Sample Text.".toCharArray();
84         int len = chs.length;
85 
86         int x = 50, y = 100;
87 
88         FontRenderContext frc = g2d.getFontRenderContext();
89         Font plain = new Font("Serif", Font.PLAIN, 48);
90         GlyphVector pgv = plain.layoutGlyphVector(frc, chs, 0, len, 0);
91         g2d.setFont(plain);
92         g2d.drawChars(chs, 0, len, x, y); y +=50;
93 
94         g2d.drawGlyphVector(pgv, x, y); y += 50;
95         Rectangle2D plainStrBounds = plain.getStringBounds(chs, 0, len, frc);
96         Rectangle2D plainGVBounds = pgv.getLogicalBounds();
97         Font bold = new Font("Serif", Font.BOLD, 48);
98         GlyphVector bgv = bold.layoutGlyphVector(frc, chs, 0, len, 0);
99         Rectangle2D boldStrBounds = bold.getStringBounds(chs, 0, len, frc);
100         Rectangle2D boldGVBounds = bgv.getLogicalBounds();
101         g2d.setFont(bold);
102         g2d.drawChars(chs, 0, len, x, y); y +=50;
103         g2d.drawGlyphVector(bgv, x, y);
104         System.out.println("Plain String Bounds = " + plainStrBounds);
105         System.out.println("Bold String Bounds = " + boldStrBounds);
106         System.out.println("Plain GlyphVector Bounds = " + plainGVBounds);
107         System.out.println("Bold GlyphVector Bounds = " + boldGVBounds);
108         if (!plainStrBounds.equals(boldStrBounds) &&
109              plainGVBounds.equals(boldGVBounds))
110         {
111             System.out.println("Test failed: Plain GV bounds same as Bold");
112             if (!interactive) {
113                 throw new RuntimeException("Plain GV bounds same as Bold");
114             }
115         }
116 
117     };
118 }
119