📄 fstextconstructor.java
字号:
decodeOpenTypeFont(filename);
else
decodeAWTFont(filename);
}
/**
* Creates a new FSTextConstructor object using the specified font.
*
* @param anIdentifier a unique identifier that will be assigned to the
* font definition object generated and referenced by all the text object
* generated.
*
* @param font an AWT Font object.
*/
public FSTextConstructor(int anIdentifier, Font font)
{
identifier = anIdentifier;
for (int i=0; i<65536; i++)
orderTable[i] = -1;
decodeAWTFont(font);
}
/**
* Resets the FSTextConstructor to generate a new set of font and text
* objects.
*
* The character sets defined for the font are cleared so the characters
* that will be used to generate the next font definition should be set with
* the willDisplay() method.
*
* This method is useful when generating objects for more than one Flash
* file as it avoids the penalty of reloading the font definition which
* can be very expensive.
*
* @param anIdentifier a unique identifier that will be assigned to the
* font definition object generated and referenced by all the text object
* generated.
*/
public void reset(int anIdentifier)
{
identifier = anIdentifier;
for (int i=0; i<65536; i++)
orderTable[i] = -1;
}
/**
* Indicates whether or not this FSTextConstructor can display all the
* characters specified in the array. This method returns the index of the
* first character that cannot be displayed using this font. If the Font can
* display all characters, -1 is returned.
*
* @param chars an array containing all the characters to be displayed.
*
* @return the index of the first character that cannot be displayed, -1
* otherwise.
*/
public int canDisplay(char[] chars)
{
int firstMissingChar = -1;
for (int i=0; i<chars.length; i++)
{
if (canDisplay(chars[i]) == false)
{
firstMissingChar = i;
break;
}
}
return firstMissingChar;
}
/**
* Indicates whether or not this FSTextConstructor can display a specified
* Unicode String. This method returns the index of the first character that
* cannot be displayed using this font. If the Font can display all
* characters, -1 is returned.
*
* @param aString the String to be displayed.
*
* @return the index of the first character that cannot be displayed, -1
* otherwise.
*/ public int canDisplay(String aString)
{
int firstMissingChar = -1;
for (int i = 0; i < aString.length(); i++)
{
if (canDisplay(aString.charAt(i)) == false)
{
firstMissingChar = i;
break;
}
}
return firstMissingChar;
}
/**
* willDisplay is used to predefine the set of characters that will be
* used when defining text objects.
*
* @see FSCharacterTable for lists of predefined character sets that can be
* used with different spoken languages.
*
* @param chars an array of characters defining the character set that will
* be used when defining text objects and fonts. The characters must be
* sorted by the code used to represent the character.
*/
public void willDisplay(char[] chars)
{
for (int i=0; i<chars.length; i++)
{
int glyphIndex = characterTable[chars[i]];
for (int index=0; index<65536; index++)
{
if (orderTable[index] == glyphIndex)
break;
else if (orderTable[index] == -1)
{
orderTable[index] = (short)glyphIndex;
break;
}
}
}
}
/**
* Generates a FSDefineFont2 object containing a complete definition of the font.
*
* <P>NOTE: Only the glyphs specified in the array of characters passed to
* the willDisplay method will be shown.</P>
*
* @return a FSDefineFont2 object generated from the font definition.
*/
public FSDefineFont2 defineFont()
{
FSDefineFont2 font = null;
int count = 0;
for (count=0; orderTable[count] != -1 && count<orderTable.length; count++);
ArrayList glyphsArray = new ArrayList(count);
ArrayList codesArray = new ArrayList(count);
ArrayList advancesArray = new ArrayList(count);
ArrayList boundsArray = new ArrayList(count);
for (int i=0; i<count; i++)
{
int glyphIndex = orderTable[i];
int character = 0;
while (characterTable[character] != glyphIndex) character++;
glyphsArray.add(glyphTable[glyphIndex].shape);
codesArray.add(new Integer(character));
advancesArray.add(new Integer(glyphTable[glyphIndex].advance));
boundsArray.add(glyphTable[glyphIndex].bounds);
}
// Use simplest constructor to ensure compatibility between
// different editions of Transform.
font = new FSDefineFont2(identifier, name);
font.setEncoding(encoding);
font.setItalic(isItalic);
font.setBold(isBold);
font.setAscent((int)ascent);
font.setDescent((int)descent);
font.setLeading((int)leading);
font.setShapes(glyphsArray);
font.setCodes(codesArray);
font.setAdvances(advancesArray);
font.setBounds(boundsArray);
font.setKernings(kernings);
return font;
}
/**
* Generates an FSDefineText2 object for the text rendered in the specified font size
* and colour. This method is used to create FSDefineText2 objects for arbitrary
* strings, using the characters defined for the FSTextConstructor object when it is
* constructed.
*
* NOTE: The font size is specified in twips not points or pixels.
*
* <P>An FSDefineText2 object differs from an FSDefineText object in that it supports
* transparent colours.</p>
*
* <P>If any of the Unicode characters in the String cannot be displayed using this font
* then the missing glyph is substituted.</p>
*
* @param anIdentifier a unique identifier for the object.
* @param text the String to be displayed.
* @param fontSize the size of the font in twips.
* @param aColor the colour of the text including transparency.
*
* @return an FSDefineText object representing the text.
*/
public FSDefineText2 defineText(int anIdentifier, String text, int fontSize,
FSColor aColor)
{
FSCoordTransform coordTransform = new FSCoordTransform(0, 0);
float scaleFactor = ((float)fontSize) / 1024.0f;
int[] glyphCodes = glyphIndicesForString(text);
int[] glyphAdvances = advancesForGlyphIndices(glyphCodes, scaleFactor);
FSText textRecord = new FSText(identifier, aColor, 0,
scaleBaseline(scaleFactor), fontSize, charactersForGlyphs(glyphCodes, glyphAdvances));
ArrayList textRecords = new ArrayList();
textRecords.add(textRecord);
return new FSDefineText2(anIdentifier,
boundsForText(glyphCodes, glyphAdvances, fontSize), coordTransform,
textRecords);
}
/**
* Generates an FSDefineText2 object for a block of text.
*
* <P>An FSDefineText2 object differs from an FSDefineText object in that it supports
* transparent colours.</p>
*
* <P>If any of the Unicode characters in the String cannot be displayed using this font
* then the missing glyph is substituted.</p>
*
* @param anIdentifier a unique identifier for the object.
* @param lines an array containing the lines of text to be displayed.
* @param fontSize the size of the font in twips.
* @param aColor the colour of the text including transparency.
* @param lineSpacing the distance, in twips, between successive lines.
*
* @return an FSDefineText object representing the text.
*/
public FSDefineText2 defineTextBlock(int anIdentifier, ArrayList lines, int fontSize,
FSColor aColor, int lineSpacing)
{
FSCoordTransform coordTransform = new FSCoordTransform(0, 0);
float scaleFactor = ((float)fontSize) / 1024.0f;
int xMin = 0;
int yMin = 0;
int xMax = 0;
int yMax = 0;
int xOffset = 0;
int yOffset = scaleBaseline(scaleFactor);
ArrayList textRecords = new ArrayList();
int n = 0;
for (Iterator i = lines.iterator(); i.hasNext(); yOffset += lineSpacing, n++)
{
String text = (String)i.next();
int[] glyphCodes = glyphIndicesForString(text);
int[] glyphAdvances = advancesForGlyphIndices(glyphCodes, scaleFactor);
FSBounds bounds = boundsForText(glyphCodes, glyphAdvances, fontSize);
if (n==0) {
yMin = bounds.getMinY();
yMax = bounds.getMaxY();
}
else {
yMax += lineSpacing;
}
if (n==lines.size()-1)
yMax += bounds.getHeight();
xMin = (xMin < bounds.getMinX()) ? xMin : bounds.getMinX();
xMax = (xMax > bounds.getMaxX()) ? xMax : bounds.getMaxX();
FSText textRecord = new FSText(identifier, aColor, xOffset,
yOffset, fontSize, charactersForGlyphs(glyphCodes, glyphAdvances));
textRecords.add(textRecord);
}
return new FSDefineText2(anIdentifier, new FSBounds(xMin, yMin, xMax, yMax), coordTransform,
textRecords);
}
/**
* Generates an FSBounds object that defines the bounding box that encloses the text,
* rendered in the specified font size.
*
* @param text the String to be displayed.
* @param fontSize the size of the font in twips.
*
* @return an FSBounds object defining the bounding box enclosing the text.
*/
public FSBounds boundsForText(String text, int fontSize)
{
float scaleFactor = ((float)fontSize) / 1024.0f;
int[] glyphCodes = glyphIndicesForString(text);
int[] glyphAdvances = advancesForGlyphIndices(glyphCodes, scaleFactor);
return boundsForText(glyphCodes, glyphAdvances, fontSize);
}
/**
* Returns the advance, in twips, to the next character. This method can be
* used when laying out sequences of individual characters, rather than as
* as single string.
*
* @param c the character that will be displayed.
* @param fontSize the size of the font the character will be rendered in.
* @return the distance to the next character.
*/
public int advanceForChar(char c, int fontSize)
{
float scaleFactor = ((float)fontSize) / 1024.0f;
int index = characterTable[c];
int advance = (int) (glyphTable[index].advance * scaleFactor);
return advance;
}
/**
* defineShape converts a string into an equivalent shape representation. The
* shape is constructed from glyphs used to represent each character in the
* string and filled with the specified colour.
*
* @param anIdentifier an unique identifier for the shape.
* @param text the string to convert into a shape.
* @param fontSize the font size in twips used to render the shape
* @param aColor the colour which will be used to fill the shape.
*
* @return an FSDefineShape3 object4 which contains a shape that represents
* the characters contained in the string argument.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -