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 This tests differences between a font derived by an 0.75, 0.75
  27  @summary affine transform of a 16 point font and a simple 12 point font.
  28  @summary Basically, there should be no differences...
  29  @summary com.apple.junit.java.awt.Font;
  30  @library ../../regtesthelpers
  31  @build VisibilityValidator
  32  @build Waypoint
  33  @run junit FontTransform
  34  */
  35 
  36 import junit.framework.*;
  37 
  38 import javax.swing.*;
  39 import java.awt.*;
  40 import java.awt.font.FontRenderContext;
  41 import java.awt.font.LineMetrics;
  42 import java.awt.geom.AffineTransform;
  43 import java.awt.geom.Rectangle2D;
  44 
  45 import test.java.awt.regtesthelpers.VisibilityValidator;
  46 
  47 public class FontTransform extends TestCase {
  48 
  49     static final int PULSE = 5;
  50     static final int HEATBEAT = 250;
  51 
  52     static String[] getty = {
  53         "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.\n\n",
  54         "Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.\n\n",
  55         "But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.\n",
  56         "\nThis tests differences between a font derived by an 0.75, 0.75 affine transform of a 16 point font and a simple 12 point font.",
  57         "Basically, there should be no differences...\n"
  58 
  59     };
  60 
  61     private AffineTransform a;
  62     private Font base = null;
  63     private Font derived = null;
  64     private Font similar = null;
  65 
  66 
  67     // debugging
  68     static public void dump(LineMetrics lm) {
  69         System.out.println( "getAscent " + lm.getAscent() );
  70         System.out.println( "getHeight " + lm.getHeight() );
  71         System.out.println( "getNumChars " + lm.getNumChars() );
  72         System.out.println( "getBaselineIndex " + lm.getBaselineIndex() );
  73         System.out.println( "getDescent " + lm.getDescent() );
  74         System.out.println( "getLeading " + lm.getLeading() );
  75         System.out.println( "getStrikethroughOffset " + lm.getStrikethroughOffset() );
  76         System.out.println( "getStrikethroughThickness " + lm.getStrikethroughThickness() );
  77         System.out.println( "getUnderlineOffset " + lm.getUnderlineOffset() );
  78         System.out.println( "getUnderlineThickness " + lm.getUnderlineThickness() );
  79     }
  80 
  81     protected void setUp() {
  82         base = new Font( "Serif", Font.PLAIN, 16 );
  83         assertNotNull( base );
  84 
  85         a = AffineTransform.getScaleInstance(0.75,0.75);
  86         assertNotNull( a );
  87         derived = base.deriveFont(a);
  88         assertNotNull( derived );
  89 
  90         similar = new Font( "Serif", Font.PLAIN, 12 );
  91         assertNotNull( similar );
  92 
  93     }
  94 
  95     public void testSimpleDerivedFont()
  96     {
  97         assertEquals( "Unexpected base size", 16, base.getSize() );
  98         assertFalse( "base isTransformed() should be false", base.isTransformed() );
  99         derived = base.deriveFont(a);
 100         assertNotNull( derived );
 101         assertEquals( "Unexpected derived size", 16, derived.getSize() );
 102         assertTrue( "derived isTransformed() should be true", derived.isTransformed() );
 103     }
 104 
 105     public void testDisplayDerivedFont() throws Exception
 106     {
 107         final JTextArea text = new JTextArea();
 108         assertNotNull( text );
 109 
 110         text.setLineWrap(true);
 111         text.setWrapStyleWord(true);
 112         text.setFont(derived);
 113         JScrollPane scroll = new JScrollPane(text);
 114 
 115         for (int i = 0; i< getty.length; i++) {
 116             text.append(getty[i]);
 117         }
 118 
 119         JFrame f = new JFrame();
 120         assertNotNull( f );
 121 
 122         try {
 123             f.getContentPane().add(scroll, java.awt.BorderLayout.CENTER);
 124             f.setSize(600,600);
 125 
 126             VisibilityValidator.setVisibleAndConfirm(f);
 127 
 128             text.setFont( derived );
 129             for( int i=0; i < PULSE; i++) {
 130                 SwingUtilities.invokeAndWait(new Runnable() {
 131                     public void run() {
 132                         text.selectAll();
 133                     }
 134                 });
 135                 Thread.sleep(HEATBEAT);
 136                 SwingUtilities.invokeAndWait(new Runnable() {
 137                     public void run() {
 138                         text.select(0,1);
 139                     }
 140                 });
 141                 Thread.sleep(HEATBEAT);
 142             }
 143 
 144             text.setFont( similar );
 145             for( int i=0; i < PULSE; i++) {
 146                 SwingUtilities.invokeAndWait(new Runnable() {
 147                     public void run() {
 148                         text.selectAll();
 149                     }
 150                 });
 151                 Thread.sleep(HEATBEAT);
 152                 SwingUtilities.invokeAndWait(new Runnable() {
 153                     public void run() {
 154                         text.select(0,1);
 155                     }
 156                 });
 157                 Thread.sleep(HEATBEAT);
 158             }
 159 
 160             Graphics2D g = (Graphics2D) text.getGraphics();
 161             assertNotNull( g );
 162 
 163             FontRenderContext frc =  g.getFontRenderContext();
 164             assertNotNull( frc );
 165 
 166             LineMetrics derived_lm = derived.getLineMetrics( getty[2], 0, getty[2].length(), frc);
 167             LineMetrics similar_lm = similar.getLineMetrics( getty[2], 0, getty[2].length(), frc);
 168 
 169 
 170             String descr = "12pt derived font and 12pt simple font should have similar ";
 171             double b_val, s_val, diff;
 172 
 173 
 174             //
 175             //    Check a few basic line metrics
 176             //
 177 
 178             b_val = derived_lm.getAscent();
 179             s_val = similar_lm.getAscent();
 180             diff = Math.abs(b_val - s_val);
 181             assertTrue(descr + "ascent", diff < b_val/100);
 182 
 183             b_val = derived_lm.getHeight();
 184             s_val = similar_lm.getHeight();
 185             diff = Math.abs(b_val - s_val);
 186             assertTrue(descr + "height", diff < b_val/100);
 187 
 188             b_val = derived_lm.getDescent();
 189             s_val = similar_lm.getDescent();
 190             diff = Math.abs(b_val - s_val);
 191             assertTrue(descr + "descent", diff < b_val/100);
 192 
 193 
 194             //
 195             //    Check string bounds
 196             //
 197             Rectangle2D derived_b = derived.getStringBounds( getty[2], frc );
 198             Rectangle2D similar_b = similar.getStringBounds( getty[2], frc );
 199 
 200 
 201             b_val = derived_b.getX();
 202             s_val = similar_b.getX();
 203             diff = Math.abs(b_val - s_val);
 204             assertTrue(descr + "bounds X coord", diff < 0.01);
 205 
 206             b_val = derived_b.getY();
 207             s_val = similar_b.getY();
 208             diff = Math.abs(b_val - s_val);
 209             assertTrue(descr + "bounds Y coord", diff < 0.01);
 210 
 211             b_val = derived_b.getWidth();
 212             s_val = similar_b.getWidth();
 213             diff = Math.abs(b_val - s_val);
 214             assertTrue(descr + "bounds width", diff < b_val/100);
 215 
 216             b_val = derived_b.getHeight();
 217             s_val = similar_b.getHeight();
 218             diff = Math.abs(b_val - s_val);
 219             assertTrue(descr + "bounds width", diff < b_val/100);
 220         }
 221         finally {
 222             f.dispose();
 223         }
 224 
 225     }
 226 
 227     public static Test suite() {
 228         return new TestSuite(FontTransform.class);
 229     }
 230 
 231     public static void main (String[] args) throws RuntimeException {
 232         TestResult tr = junit.textui.TestRunner.run(suite());
 233         if((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
 234             throw new RuntimeException("### Unexpected JUnit errors or failures.");
 235         }
 236     }
 237 }
 238