/*
 *  BasicText.java
 *  Cookbook
 *
 *  Copyright (c) 2001-2009 Flagstone Software Ltd. All rights reserved.
 *
 *  This code is distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 *  EXPRESS OR IMPLIED, AND Flagstone HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING
 *  WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 *  PURPOSE, AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
 */

import com.flagstone.transform.*;
import com.flagstone.transform.util.*;

import java.awt.Font;
import java.util.*;

/*
 * This example shows how to display static text fields in a movie.
 *
 * To run this example, type the following on a command line:
 *
 *     java -cp cookbook.jar BasicText string file-out
 *
 * where
 *
 *     string, the text that will be displayed - enclose in quotes if the string 
 *     contains spaces.
 *
 *     file-out is the path where the file will be written. If no output file 
 *     is specified then a file named after the example will be written to the 
 *     current directory.

 */
public class BasicText
{
	public static void main(String[] args)
	{
        try
        {
        	String str = args[0];
        	String out = args.length == 1 ? "BasicText.swf" : args[1];
        	BasicText example = new BasicText();         
            FSMovie movie = new FSMovie();            
            example.createMovie(movie, str);
            movie.encodeToFile(out);
        }
        catch (Exception e)
        {
        	e.printStackTrace();
        }
	}
    
	void createMovie(FSMovie movie, String str)
	{
		Font awtFont = new Font("Arial", Font.PLAIN, 12);
		
    	FSTextConstructor textGenerator = new FSTextConstructor(movie.newIdentifier(), awtFont);

        int layer = 1;                // Starting layer for objects in the display list.
        int fontSize = 280;           // Font size in twips 1 point = 20 twips
        
        /*
         * The movie will be sized to match the block of text generated.
         * The margins are defined so the text is displayed without touching
         * the edge of the Flash Player screen.
         */
        int leftMargin = fontSize;
        int rightMargin = fontSize;
        int topMargin = fontSize;
        int bottomMargin = fontSize;
        
        int fontId = movie.newIdentifier();

		/* 
		 * The text is generated before the font definition. This ensures that
		 * only the characters displayed will be included in the font. Unused
		 * characters will be omitted, greatly reducing the size of the Flash file
		 * generated. Once the FSDefineFont2 object has been generated any text 
		 * objects created with characters not previously used, will not be 
		 * displayed correctly - a new font definition would need to be generated
		 * to include the new characters.
		 */
        FSDefineText2 text = textGenerator.defineText(movie.newIdentifier(), 
        	str, fontSize, FSColorTable.black());
        	
        FSDefineFont2 font = textGenerator.defineFont();

		/* 
		 * Define the size of the Flash Player screen using the bounding 
		 * rectangle defined for the block of text plus a suitable margin so 
		 * the text does not touch the edge of the screen.
		 */
        int screenWidth = text.getBounds().getWidth() + leftMargin + rightMargin;
        int screenHeight = text.getBounds().getHeight() + topMargin + bottomMargin;
        
        /* 
         * Add all the objects together to create the movie. The origin of the 
         * block of text (0,0) is the top left corner as viewed on the Flash 
         * Player screen. The left and top margins offsets the text correctly
         * from the edge of the screen.
         */
        movie.setFrameSize(new FSBounds(0, 0, screenWidth, screenHeight));
        movie.setFrameRate(1.0f);
        movie.add(new FSSetBackgroundColor(FSColorTable.lightblue()));
        movie.add(font);
        movie.add(text);
        movie.add(new FSPlaceObject2(text.getIdentifier(), layer++, leftMargin , topMargin));
        movie.add(new FSShowFrame());
	}
}