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