1 /*
   2  * Copyright (c) 2011, 2013, 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  @summary Simple test for using derivedFonts
  27  @summary com.apple.junit.java.awt.Font;
  28  @run main FractionalFont01
  29  */
  30 
  31 import junit.framework.*;
  32 
  33 import javax.swing.*;
  34 import java.awt.*;
  35 import java.awt.font.GlyphVector;
  36 
  37 public class FractionalFont01 extends TestCase {
  38     static final int numSizes = 10;
  39     volatile boolean painted = false;
  40     volatile float[] sizes = new float[numSizes];
  41 
  42     public static Test suite() {
  43         return new TestSuite(FractionalFont01.class);
  44     }
  45 
  46     public static void main (String[] args) throws RuntimeException {
  47         String name = System.getProperty("os.name");
  48         if (name.equals("Mac OS X")) {
  49             // This test uses a font that may not exist on other platforms
  50             TestResult tr = junit.textui.TestRunner.run(suite());
  51             if((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  52                 throw new RuntimeException("### Unexpected JUnit errors or failures.");
  53             }
  54         }
  55     }
  56 
  57     public void testFractionalMetrics() throws Exception {
  58         JFrame f = new JFrame("FontTest");
  59         // front and center
  60         f.setLocation(f.getToolkit().getScreenSize().width / 4, f.getToolkit().getScreenSize().height / 4);
  61         f.setSize(new Dimension(f.getToolkit().getScreenSize().width / 2, f.getToolkit().getScreenSize().height / 2));
  62 
  63         try {
  64             f.getContentPane().setLayout(new BorderLayout());
  65             FontTestPanel fontC = new FontTestPanel();
  66             fontC.setBackground(Color.white);
  67             f.getContentPane().add(BorderLayout.CENTER, fontC);
  68             f.setVisible(true);
  69 
  70             // wait for a paint call
  71             int count = 0;
  72             while (painted == false && count < 10) {
  73                 Thread.sleep(500);
  74                 count += 1;
  75             }
  76 
  77             assertTrue("Should have painted by now", painted);
  78             // loop through the sizes and confirm that each is larger than the last
  79             for (int i = 1; i < numSizes; i++) {
  80                 assertTrue("Expected a non-zero size string", sizes[i] > 0);
  81                 assertTrue("Expected a wider string when using a wider fractional font", sizes[i] > sizes[i - 1]);
  82             }
  83         } finally {
  84             f.dispose();
  85         }
  86     }
  87 
  88     class FontTestPanel extends Canvas {
  89         Font thisFont;
  90 
  91         public FontTestPanel() {
  92             thisFont = new Font("Arial", Font.PLAIN, 12);
  93             thisFont = thisFont.deriveFont(Font.PLAIN, 12.5f);
  94         }
  95 
  96         public void paint(Graphics g) {
  97             super.paint(g);
  98             Graphics2D g2 = (Graphics2D) g;
  99             g2.setColor(Color.blue);
 100             g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
 101             int x = 10;
 102             int y = 20;
 103             float currY = y;
 104             for (int i = 0; i < numSizes; i++) {
 105                 float fontSize = 12f + (float) i / 10;
 106                 currY += fontSize + 5;
 107                 String fullText = "Text width with " + fontSize + " pixel font = ";
 108 
 109                 thisFont = thisFont.deriveFont(Font.PLAIN, fontSize);
 110                 GlyphVector msg = thisFont.createGlyphVector(g2.getFontRenderContext(), fullText);
 111                 g2.drawGlyphVector(msg, x, currY);
 112 
 113                 float width = (float) msg.getLogicalBounds().getBounds2D().getWidth();
 114                 msg = thisFont.createGlyphVector(g2.getFontRenderContext(), Float.toString(width));
 115                 g2.drawGlyphVector(msg, x + width, currY);
 116                 sizes[i] = width;
 117                 painted = true;
 118             }
 119         }
 120     }
 121 }