Module10- QTZoo/Source/TextPresenter.java

import java.awt.Dimension;
 
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.util.Vector;
 
import quicktime.app.image.ImagePresenter; 
 
 
import quicktime.qd.QDFont;
import quicktime.qd.QDDrawer;
import quicktime.qd.QDColor; 
import quicktime.qd.QDConstants;
 
import quicktime.qd.QDGraphics;
import quicktime.qd.QDRect;
 
import quicktime.QTException; 
 
import quicktime.std.image.GraphicsMode;
 
 
 
/**
 * QTZoo Module 3 - Using QuickTime to draw text
 * A graphics utility class that images a block of text loaded from disk 
 * and draws it in an image presenter
 *
 * @author Michael Hopkins
 * @author Levi Brown 
 * @author Apple Computer, Inc.
 * @version 2.0 01/25/2000
 * Copyright:   © Copyright 1999 Apple Computer, Inc. All rights reserved.
 *  
 * Disclaimer:  IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
 *              ("Apple") in consideration of your agreement to the following terms, and your
 *              use, installation, modification or redistribution of this Apple software
 *              constitutes acceptance of these terms.  If you do not agree with these terms,
 *              please do not use, install, modify or redistribute this Apple software.
 *
 *              In consideration of your agreement to abide by the following terms, and subject
 *              to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
 *              copyrights in this original Apple software (the "Apple Software"), to use,
 *              reproduce, modify and redistribute the Apple Software, with or without
 *              modifications, in source and/or binary forms; provided that if you redistribute
 *              the Apple Software in its entirety and without modifications, you must retain
 *              this notice and the following text and disclaimers in all such redistributions of
 *              the Apple Software.  Neither the name, trademarks, service marks or logos of
 *              Apple Computer, Inc. may be used to endorse or promote products derived from the
 *              Apple Software without specific prior written permission from Apple.  Except as
 *              expressly stated in this notice, no other rights or licenses, express or implied,
 *              are granted by Apple herein, including but not limited to any patent rights that
 *              may be infringed by your derivative works or by other works in which the Apple
 *              Software may be incorporated.
 *
 *              The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
 *              WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
 *              WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 *              PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
 *              COMBINATION WITH YOUR PRODUCTS.
 *
 *              IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
 *              CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *              GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *              ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
 *              OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
 *              (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
 *              ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * 
 */
public class TextPresenter
{
    /**
     *  Constructor for the TextPresenter class
     *  @param filePath relative path of file where text is located
     *  @param size size of area where text is to be clipped to
     */
    public TextPresenter( String filePath, Dimension size )  throws FileNotFoundException
    {
        this( TextPresenter.readTextFile(filePath), size );
    }
    
    /**
     *  Constructor for the TextPresenter class
     *  @param filePath relative path of file where text is located
     *  @param size size of area where text is to be clipped to
     */
    public TextPresenter(String[] text, Dimension size)
    {   
        final QDRect area = new QDRect( size.width, size.height );
        theText = text;
        
        try
        {
            gw = new QDGraphics( area );
            
            gw.beginDraw( new QDDrawer()
                            {
                                public void draw( QDGraphics gw ) throws QTException
                                {
                                    gw.textSize( 12 );
                                    gw.textFont( QDFont.getFNum("Times") ); 
                                    gw.textFace( QDConstants.normal );
                                    gw.setForeColor( QDColor.black );
                                      
                                    gw.setBackColor( QDColor.white );
                                    gw.eraseRect( area );
                                    
                                    
                                    String aLine = theText[0];
                                    
                                    if ( gw.textWidth( aLine, 0, aLine.length() ) > area.getWidth() )
                                        gw.textSize( 10 );
                                    
                                      
                                    for ( int i = 0; i < theText.length; ++i )
                                    {
                                        if ( i != 0 )
                                            aLine = theText[ i ];
                                        gw.moveTo( 0, (i+1) * 12 );
                                        gw.drawTextScaled( aLine.length(), aLine, 1.0f, 1.0f );
                                    }
                                }
                            }); 
                              
            imagePres = ImagePresenter.fromGWorld( gw );
            imagePres.setGraphicsMode( new GraphicsMode( QDConstants.transparent, QDColor.white ));
        }
        catch ( QTException e )
        {
            e.printStackTrace();
        }
    }
    
    /**
     * Reads in a text file to a String array.
     * @param filePath the path and name of the text file to read.
     * @return the string array containing the contents of the file,
     * one line per element of the array.
     * @throws java.io.FileNotFoundException if the given file can not
     * be found to be read.
     */
    public static String[] readTextFile( String filePath ) throws FileNotFoundException
    {
        if (filePath != null)
        {
            try
            {
                BufferedReader reader = new BufferedReader( new FileReader( filePath ));
                Vector lines = new Vector();
                
                String aLine = reader.readLine();
                while( aLine != null )
                {
                    lines.addElement(aLine);
                    aLine = reader.readLine();
                }
                
                String[] lineArray = new String[ lines.size() ];
                lines.copyInto(lineArray);
                return lineArray;
            }
            catch (IOException exc)
            {
                exc.printStackTrace();
            }
        }
        
        return new String[0];
    }
    
    /**
     * Returns the image presenter object that contains the rendered text
     */
    public ImagePresenter getPresenter()
    {
        return imagePres;
    }
    
    protected QDGraphics     gw;
    protected String[]       theText;
    protected ImagePresenter imagePres;
}