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