📄 constructtext.java
字号:
/*
* ShowAWTFonts.java
* Examples
*
* Created by Stuart MacKay on Wed Sep 03 2003.
* Copyright (c) 2001-2004 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.
*/
package com.flagstone.transform.examples;
import com.flagstone.transform.*;
import com.flagstone.transform.util.*;
import java.io.*;
import java.util.*;
import java.awt.*;
/*
* This example shows how the FSTextConstructor class can be used to generate a
* font defintion using the fonts built into the Java AWT toolkit. The example
* loads the font definition and generates a movie displaying the first 2048
* characters as a block of text containing 32 rows of 64 characters - enough
* to display all the characters displayed in Latin and Cyrillic fonts along
* with the majority of special characters and symbols.
*
* To run this example, type the following on a command line:
*
* java com.flagstone.transform.examples.ConstructText [--resultDir path]
*
* where
*
* resultDir is the directory where the Flash files generated by the example
* is written to. The files will be written to the directory where the example
* was run if omitted.
*
* The example will generate one Flash file for each of the fonts available. The
* names of the font will be used as the filename with a '.swf' suffix added.
*/
public class ConstructText extends Example
{
private FSTextConstructor textGenerator = null;
public static void main(String[] args)
{
new ConstructText(args);
}
public ConstructText(String[] args)
{
super(args);
String fontName = null;
String fileOut = null;
try
{
Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for (int i=0; i<fonts.length; i++)
{
movie = new FSMovie();
fontName = fonts[i].getFontName();
fileOut = resultDir + File.separator + fontName + ".swf";
/*
* Font definitions for AWT fonts are created by specifying the name
* of the font. Flash and OpenType/TrueType fonts are generated by
* specifying the name of the file containing the font definition.
*/
textGenerator = new FSTextConstructor(movie.newIdentifier(), fontName);
createMovie();
movie.encodeToFile(fileOut);
}
}
catch (Exception e)
{
/* Several exceptions could be thrown to get to this point. FSTextConstructor
* will throw an IllegalArgumentException font cannot be loaded from the
* graphics environment. The constructor will also throw a FileNotFoundException,
* IOException or DataFormatException if the font is being loaded from a file.
*
* The FSMovie will throw a FileNotFoundException or IOException if there is a
* problem creating the file.
*
* The exceptions are all caught in a single handler to make the code more
* readable.
*/
System.err.println("Cannot create movie for the font: " + fontName);
e.printStackTrace();
}
}
void createMovie()
{
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
* 2048 characters availalbe in the font will be shown as a block of
* 32 lines each containing 64 characters.
*/
ArrayList lines = new ArrayList();
char c = 0;
for (int i=0; i<32; 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());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -