/*
* ShowFont.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.io.IOException;
import java.util.zip.DataFormatException;
import java.util.*;
import java.awt.*;
/*
* This example shows how to display a block of text in a movie.
*
* To run this example, type the following on a command line:
*
* java -cp cookbook.jar BasicTextBlock file-out
*
* where
*
* font-name is the name of a Font available in Java such as Arial or
*
* 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.
*
* This example is almost identical to the ShowFontFile example which displays
* a block of text using a font definition loaded from a Flash file or a TrueType/
* OpenType font definition.
*/
public class BasicTextBlock
{
public static void main(String[] args)
{
try
{
String out = args.length == 1 ? "BasicTextBlock.swf" : args[1];
BasicTextBlock example = new BasicTextBlock();
FSMovie movie = new FSMovie();
example.createMovie(movie, args[0]);
movie.encodeToFile(out);
}
catch (Exception e)
{
e.printStackTrace();
}
}
void createMovie(FSMovie movie, String fontName) throws DataFormatException, IOException
{
Font awtFont = new Font("Arial", Font.PLAIN, 12);
FSTextConstructor textGenerator = new FSTextConstructor(movie.newIdentifier(), awtFont);
int layer = 0; // Starting layer for objects in the display list.
int fontSize = 280; // Font size in twips 1 point = 20 twips
int lineSpacing = fontSize; // The font size defines a suitable line spacing
/*
* 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;
/* Create the strings that will be used to display the text. The first
* 256 characters available in the font will be shown as a block of 4
* lines each containing 64 characters.
*/
ArrayList lines = new ArrayList();
char c = 0;
for (int i=0; i<4; i++)
{
StringBuffer line = new StringBuffer();
for (int j=0; j<64; j++, c++) {
line.append((char)c);
}
lines.add(line.toString());
}
/* The block of 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.
*
* The FSDefineText2 object contains an array of FSText objects, each
* specifying an offset relative to the origin of the entire block. These
* can easily be changed if a line needs to be indented or the line spacing
* adjusted.
*/
FSDefineText2 text = textGenerator.defineTextBlock(movie.newIdentifier(),
lines, fontSize, FSColorTable.black(), lineSpacing);
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());
}
}