Path: blob/master/test/jdk/java/awt/FontClass/HelvLtOblTest.java
41149 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key headful26* @bug 806483327* @summary Test correct font is obtained via famil+style28* @run main HelvLtOblTest29*/3031import 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;4445public class HelvLtOblTest extends JComponent {4647static Font helvFont = null;4849static int[] codes = { 0x23, 0x4a, 0x48, 0x3, 0x4a, 0x55, 0x42, 0x4d,500x4a, 0x44, 0x3,510x53, 0x46, 0x45, 0x3, 0x55, 0x46, 0x59, 0x55, };5253static String str = "Big italic red text";5455public static void main(String[] args) throws Exception {56String os = System.getProperty("os.name");57if (!os.startsWith("Mac")) {58return;59}60GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();61Font[] fonts = ge.getAllFonts();62for (int i=0; i<fonts.length; i++) {63if (fonts[i].getPSName().equals("Helvetica-LightOblique")) {64helvFont = fonts[i];65break;66}67}68if (helvFont == null) {69return;70}71final HelvLtOblTest test = new HelvLtOblTest();72SwingUtilities.invokeLater(() -> {73JFrame f = new JFrame();74f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);75f.add("Center", test);76f.pack();77f.setVisible(true);78});79test.compareImages();80}8182public Dimension getPreferredSize() {83return new Dimension(400,400);84}8586public void paintComponent(Graphics g) {87super.paintComponent(g);88Graphics2D g2 = (Graphics2D)g;89FontRenderContext frc = new FontRenderContext(null, true, true);90Font f = helvFont.deriveFont(Font.PLAIN, 40);91System.out.println("font = " +f.getFontName());92GlyphVector gv = f.createGlyphVector(frc, codes);93g.setFont(f);94g.setColor(Color.white);95g.fillRect(0,0,400,400);96g.setColor(Color.black);97g2.drawGlyphVector(gv, 5,200);98g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,99RenderingHints.VALUE_TEXT_ANTIALIAS_ON);100g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,101RenderingHints.VALUE_FRACTIONALMETRICS_ON);102g2.drawString(str, 5, 250);103}104105void compareImages() {106BufferedImage bi0 = drawText(false);107BufferedImage bi1 = drawText(true);108compare(bi0, bi1);109}110111BufferedImage drawText(boolean doGV) {112int w = 400;113int h = 50;114BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);115Graphics2D g = bi.createGraphics();116g.setColor(Color.white);117g.fillRect(0,0,w,h);118g.setColor(Color.black);119Font f = helvFont.deriveFont(Font.PLAIN, 40);120g.setFont(f);121int x = 5;122int y = h - 10;123if (doGV) {124FontRenderContext frc = new FontRenderContext(null, true, true);125GlyphVector gv = f.createGlyphVector(frc, codes);126g.drawGlyphVector(gv, 5, y);127} else {128g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,129RenderingHints.VALUE_TEXT_ANTIALIAS_ON);130g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,131RenderingHints.VALUE_FRACTIONALMETRICS_ON);132g.drawString(str, x, y);133}134return bi;135}136137// Need to allow for minimal rounding error, so allow each component138// to differ by 1.139void compare(BufferedImage bi0, BufferedImage bi1) {140int wid = bi0.getWidth();141int hgt = bi0.getHeight();142for (int x=0; x<wid; x++) {143for (int y=0; y<hgt; y++) {144int rgb0 = bi0.getRGB(x, y);145int rgb1 = bi1.getRGB(x, y);146if (rgb0 == rgb1) continue;147int r0 = (rgb0 & 0xff0000) >> 16;148int r1 = (rgb1 & 0xff0000) >> 16;149int rdiff = r0-r1; if (rdiff<0) rdiff = -rdiff;150int g0 = (rgb0 & 0x00ff00) >> 8;151int g1 = (rgb1 & 0x00ff00) >> 8;152int gdiff = g0-g1; if (gdiff<0) gdiff = -gdiff;153int b0 = (rgb0 & 0x0000ff);154int b1 = (rgb1 & 0x0000ff);155int bdiff = b0-b1; if (bdiff<0) bdiff = -bdiff;156if (rdiff > 1 || gdiff > 1 || bdiff > 1) {157throw new RuntimeException(158"Images differ at x=" + x + " y="+ y + " " +159Integer.toHexString(rgb0) + " vs " +160Integer.toHexString(rgb1));161}162}163}164}165166}167168169