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