blob: 80f976a76f4e6f7602041048cf48ff67626ae7da [file] [log] [blame]
prr401b1ec2015-06-05 12:02:01 -07001/*
2 * Copyright (c) 2015, 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 8064833
27 * @summary Test correct font is obtained via family+style
28 * @run main HelvLtOblTest
29 */
30
31import javax.swing.JComponent;
32import javax.swing.JFrame;
33import javax.swing.SwingUtilities;
34import java.awt.Color;
35import java.awt.Dimension;
36import java.awt.Font;
37import java.awt.Graphics;
38import java.awt.Graphics2D;
39import java.awt.GraphicsEnvironment;
40import java.awt.RenderingHints;
41import java.awt.font.FontRenderContext;
42import java.awt.font.GlyphVector;
43import java.awt.image.BufferedImage;
44
45public class HelvLtOblTest extends JComponent {
46
47 static Font helvFont = null;
48
49 static int[] codes = { 0x23, 0x4a, 0x48, 0x3, 0x4a, 0x55, 0x42, 0x4d,
50 0x4a, 0x44, 0x3,
51 0x53, 0x46, 0x45, 0x3, 0x55, 0x46, 0x59, 0x55, };
52
53 static String str = "Big italic red text";
54
55 public static void main(String[] args) throws Exception {
56 String os = System.getProperty("os.name");
57 if (!os.startsWith("Mac")) {
58 return;
59 }
60 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
61 Font[] fonts = ge.getAllFonts();
62 for (int i=0; i<fonts.length; i++) {
63 if (fonts[i].getPSName().equals("Helvetica-LightOblique")) {
64 helvFont = fonts[i];
65 break;
66 }
67 }
68 if (helvFont == null) {
69 return;
70 }
71 final HelvLtOblTest test = new HelvLtOblTest();
72 SwingUtilities.invokeLater(() -> {
73 JFrame f = new JFrame();
74 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
75 f.add("Center", test);
76 f.pack();
77 f.setVisible(true);
78 });
79 test.compareImages();
80 }
81
82 public Dimension getPreferredSize() {
83 return new Dimension(400,400);
84 }
85
86 public void paintComponent(Graphics g) {
87 super.paintComponent(g);
88 Graphics2D g2 = (Graphics2D)g;
89 FontRenderContext frc = new FontRenderContext(null, true, true);
90 Font f = helvFont.deriveFont(Font.PLAIN, 40);
91 System.out.println("font = " +f.getFontName());
92 GlyphVector gv = f.createGlyphVector(frc, codes);
93 g.setFont(f);
94 g.setColor(Color.white);
95 g.fillRect(0,0,400,400);
96 g.setColor(Color.black);
97 g2.drawGlyphVector(gv, 5,200);
98 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
99 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
100 g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
101 RenderingHints.VALUE_FRACTIONALMETRICS_ON);
102 g2.drawString(str, 5, 250);
103 }
104
105 void compareImages() {
106 BufferedImage bi0 = drawText(false);
107 BufferedImage bi1 = drawText(true);
108 compare(bi0, bi1);
109 }
110
111 BufferedImage drawText(boolean doGV) {
112 int w = 400;
113 int h = 50;
114 BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
115 Graphics2D g = bi.createGraphics();
116 g.setColor(Color.white);
117 g.fillRect(0,0,w,h);
118 g.setColor(Color.black);
119 Font f = helvFont.deriveFont(Font.PLAIN, 40);
120 g.setFont(f);
121 int x = 5;
122 int y = h - 10;
123 if (doGV) {
124 FontRenderContext frc = new FontRenderContext(null, true, true);
125 GlyphVector gv = f.createGlyphVector(frc, codes);
126 g.drawGlyphVector(gv, 5, y);
127 } else {
128 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
129 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
130 g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
131 RenderingHints.VALUE_FRACTIONALMETRICS_ON);
132 g.drawString(str, x, y);
133 }
134 return bi;
135 }
136
137 // Need to allow for minimal rounding error, so allow each component
138 // to differ by 1.
139 void compare(BufferedImage bi0, BufferedImage bi1) {
140 int wid = bi0.getWidth();
141 int hgt = bi0.getHeight();
142 for (int x=0; x<wid; x++) {
143 for (int y=0; y<hgt; y++) {
144 int rgb0 = bi0.getRGB(x, y);
145 int rgb1 = bi1.getRGB(x, y);
146 if (rgb0 == rgb1) continue;
147 int r0 = (rgb0 & 0xff0000) >> 16;
148 int r1 = (rgb1 & 0xff0000) >> 16;
149 int rdiff = r0-r1; if (rdiff<0) rdiff = -rdiff;
150 int g0 = (rgb0 & 0x00ff00) >> 8;
151 int g1 = (rgb1 & 0x00ff00) >> 8;
152 int gdiff = g0-g1; if (gdiff<0) gdiff = -gdiff;
153 int b0 = (rgb0 & 0x0000ff);
154 int b1 = (rgb1 & 0x0000ff);
155 int bdiff = b0-b1; if (bdiff<0) bdiff = -bdiff;
156 if (rdiff > 1 || gdiff > 1 || bdiff > 1) {
157 throw new RuntimeException(
158 "Images differ at x=" + x + " y="+ y + " " +
159 Integer.toHexString(rgb0) + " vs " +
160 Integer.toHexString(rgb1));
161 }
162 }
163 }
164 }
165
166}