package
{
import
flash.display.GradientType;
import
flash.display.Sprite;
import
flash.display.StageAlign;
import
flash.display.StageScaleMode;
import
flash.geom.Matrix;
import
flash.text.AntiAliasType;
import
flash.text.Font;
import
flash.text.TextField;
import
flash.text.TextFieldAutoSize;
import
flash.text.TextFormat;
import
flash.text.engine.*;
[SWF(width=
"760"
, height=
"450"
, frameRate=
"12"
, backgroundColor=
"#FFFFFF"
)]
public
class
FontTest
extends
Sprite {
//Embed single font typeface from ttf file,
//advancedAntialiasing=true, all font glyphs included,
//embedAsCFF=true - FTE will be used
[Embed(source=
"./font/enliven_design_element/Element.ttf"
, fontName=
"Element"
)]
// Do not use this variable directly.
//It exists so that the compiler will link in the font.
private
static
var
Fontclass1:Class;
//Embed multiple typefaces from otf files using the same font alias
//advancedAntialiasing=true, Basic Latin unicode range included
//Notice that the same fontName alias used for each font-file embedded
[Embed(source=
"./font/backpacker_bpreplay/BPreplay.otf"
, fontName=
"BPreplay"
,
fontStyle=
"normal"
, fontWeight=
"normal"
, embedAsCFF=
"false"
, unicodeRange=
"U+0020-007E"
)]
private
static
var
Fontclass2:Class;
[Embed(source=
"./font/backpacker_bpreplay/BPreplayBold.otf"
, fontName=
"BPreplay"
,
fontStyle=
"normal"
, fontWeight=
"bold"
, embedAsCFF=
"false"
, unicodeRange=
"U+0020-007E"
)]
private
static
var
Fontclass3:Class;
[Embed(source=
"./font/backpacker_bpreplay/BPreplayBoldItalics.otf"
, fontName=
"BPreplay"
,
fontStyle=
"italic"
, fontWeight=
"bold"
, embedAsCFF=
"false"
, unicodeRange=
"U+0020-007E"
)]
private
static
var
Fontclass4:Class;
[Embed(source=
"./font/backpacker_bpreplay/BPreplayItalics.otf"
, fontName=
"BPreplay"
,
fontStyle=
"italic"
, fontWeight=
"normal"
, embedAsCFF=
"false"
, unicodeRange=
"U+0020-007E"
)]
private
static
var
Fontclass5:Class;
// Embed System font
[Embed(systemFont=
"Tahoma"
, fontName=
"Tahoma"
, mimeType=
"application/x-font-truetype"
,
embedAsCFF=
"false"
, unicodeRange=
"U+0020-007E"
)]
private
static
var
Fontclass7:Class;
// Embed System font
// advancedAntialiasing=false
[Embed(systemFont=
"Tahoma"
, fontName=
"TahomaNoAdvancedAA"
, mimeType=
"application/x-font-truetype"
,
embedAsCFF=
"false"
, advancedAntiAliasing=
"false"
, unicodeRange=
"U+0020-007E"
)]
private
static
var
Fontclass8:Class;
/* This embed from system font collection should work, but it did not for me
on Mac OS 10.6, flex 4.
I expected to embed custom typeface "Futura Condensed Medium" from system font collection,
however this code results in embedding "Futura" regular typeface instead.
*/
[Embed(systemFont=
"Futura"
, fontName=
"Futura"
, fontFamily=
"Futura Condensed Medium"
,
mimeType=
"application/x-font-truetype-collection"
,embedAsCFF=
"false"
,unicodeRange=
"U+0020-007E"
)]
private
static
var
Fontclass9:Class;
/* When embedding system-font this property should include the font name and font face.
In this case the fontName property is generated automatically.
Notice that font name used for TextFormat.font property assignment is "Trebuchet MS",
not "Trebuchet MS Bold"
*/
[Embed(systemFont=
"Trebuchet MS"
, fontFamily=
"Trebuchet MS Bold"
, fontWeight=
"bold"
,
mimeType=
"application/x-font-truetype"
, embedAsCFF=
"false"
, unicodeRange=
"U+0020-007E"
)]
private
static
var
Fontclass10:Class;
// embedding movieClip, created in Flash, containing bitmap font
[Embed(source=
"bitmapFontTest.swf#bitmapFontContainer"
)]
private
static
var
MyFontHolder:Class;
public
function
FontTest()
{
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
makeBackground();
var
topBar:TopBar=
new
TopBar(
"Embedding fonts sample"
);
addChild(topBar);
var
str:
String
=
"Flash Text Engine Line, embedAsCFF=\"true\""
;
var
fd:FontDescription=
new
FontDescription(
"Element"
,
"normal"
,
"normal"
,
FontLookup.EMBEDDED_CFF);
var
format:ElementFormat =
new
ElementFormat(fd,
30
);
format.color=
0x8F360E
;
var
textElement:TextElement =
new
TextElement(str, format);
var
textBlock:TextBlock =
new
TextBlock();
textBlock.content = textElement;
var
textLine1:TextLine = textBlock.createTextLine(
null
,
750
);
addChild(textLine1);
textLine1.x =
30
;
textLine1.y =
70
;
var
str0:
String
=
"Multiple typafaces fontName=\"BPreplay\". Normal typeface"
;
var
str1:
String
=
"Multiple typafaces fontName=\"BPreplay\". Bold typeface"
;
var
str2:
String
=
"Multiple typafaces fontName=\"BPreplay\". Italic typeface"
;
var
str3:
String
=
"Multiple typafaces fontName=\"BPreplay\". Bold italic typeface"
;
var
textFormatBPreplay0:TextFormat=
new
TextFormat();
textFormatBPreplay0.font=
"BPreplay"
;
textFormatBPreplay0.size=
22
;
var
textFormatBPreplay1:TextFormat=
new
TextFormat();
textFormatBPreplay1.font=
"BPreplay"
;
textFormatBPreplay1.bold=
true
;
textFormatBPreplay1.size=
22
;
var
textFormatBPreplay2:TextFormat=
new
TextFormat();
textFormatBPreplay2.font=
"BPreplay"
;
textFormatBPreplay2.italic=
true
;
textFormatBPreplay2.size=
22
;
var
textFormatBPreplay3:TextFormat=
new
TextFormat();
textFormatBPreplay3.font=
"BPreplay"
;
textFormatBPreplay3.italic=
true
;
textFormatBPreplay3.bold=
true
;
textFormatBPreplay3.size=
22
;
var
textFieldBPreplay0:TextField=
new
TextField();
textFieldBPreplay0.embedFonts=
true
;
textFieldBPreplay0.antiAliasType=flash.text.AntiAliasType.ADVANCED;
textFieldBPreplay0.defaultTextFormat=textFormatBPreplay0;
textFieldBPreplay0.selectable=
false
;
textFieldBPreplay0.autoSize=TextFieldAutoSize.LEFT;
textFieldBPreplay0.x=
30
;
textFieldBPreplay0.y=
90
;
textFieldBPreplay0.text=str0;
var
textFieldBPreplay1:TextField=
new
TextField();
textFieldBPreplay1.embedFonts=
true
;
textFieldBPreplay1.antiAliasType=flash.text.AntiAliasType.ADVANCED;
textFieldBPreplay1.defaultTextFormat=textFormatBPreplay1;
textFieldBPreplay1.selectable=
false
;
textFieldBPreplay1.autoSize=TextFieldAutoSize.LEFT;
textFieldBPreplay1.x=
30
;
textFieldBPreplay1.y=
120
;
textFieldBPreplay1.text=str1;
var
textFieldBPreplay2:TextField=
new
TextField();
textFieldBPreplay2.embedFonts=
true
;
textFieldBPreplay2.antiAliasType=flash.text.AntiAliasType.ADVANCED;
textFieldBPreplay2.defaultTextFormat=textFormatBPreplay2;
textFieldBPreplay2.selectable=
false
;
textFieldBPreplay2.autoSize=TextFieldAutoSize.LEFT;
textFieldBPreplay2.x=
30
;
textFieldBPreplay2.y=
150
;
textFieldBPreplay2.text=str2;
var
textFieldBPreplay3:TextField=
new
TextField();
textFieldBPreplay3.embedFonts=
true
;
textFieldBPreplay3.antiAliasType=flash.text.AntiAliasType.ADVANCED;
textFieldBPreplay3.defaultTextFormat=textFormatBPreplay3;
textFieldBPreplay3.selectable=
false
;
textFieldBPreplay3.autoSize=TextFieldAutoSize.LEFT;
textFieldBPreplay3.x=
30
;
textFieldBPreplay3.y=
180
;
textFieldBPreplay3.text=str3;
addChild(textFieldBPreplay0);
addChild(textFieldBPreplay1);
addChild(textFieldBPreplay2);
addChild(textFieldBPreplay3);
var
str4:
String
=
"Tahoma system font embedded, 10px, advanced anti-alising"
;
var
str5:
String
=
"Tahoma system font embedded, 10px, NO advanced anti-alising"
;
var
textFormatTahoma0:TextFormat=
new
TextFormat();
textFormatTahoma0.font=
"Tahoma"
;
textFormatTahoma0.size=
10
;
var
textFormatTahoma1:TextFormat=
new
TextFormat();
textFormatTahoma1.font=
"TahomaNoAdvancedAA"
;
textFormatTahoma1.size=
10
;
var
textFieldTahoma0:TextField=
new
TextField();
textFieldTahoma0.embedFonts=
true
;
textFieldTahoma0.antiAliasType=flash.text.AntiAliasType.ADVANCED;
textFieldTahoma0.sharpness=
250
;
textFieldTahoma0.defaultTextFormat=textFormatTahoma0;
textFieldTahoma0.selectable=
false
;
textFieldTahoma0.autoSize=TextFieldAutoSize.LEFT;
textFieldTahoma0.x=
30
;
textFieldTahoma0.y=
220
;
textFieldTahoma0.text=str4;
var
textFieldTahoma1:TextField=
new
TextField();
textFieldTahoma1.embedFonts=
true
;
textFieldTahoma1.defaultTextFormat=textFormatTahoma1;
textFieldTahoma1.selectable=
false
;
textFieldTahoma1.autoSize=TextFieldAutoSize.LEFT;
textFieldTahoma1.x=
30
;
textFieldTahoma1.y=
232
;
textFieldTahoma1.text=str5;
addChild(textFieldTahoma0);
addChild(textFieldTahoma1);
var
str6:
String
=
"Expected to be Futura Condensed Medium typeface, "
+
"but resulted in Futura (regular)"
;
var
textFormatFutura:TextFormat=
new
TextFormat();
textFormatFutura.font=
"Futura"
;
textFormatFutura.size=
18
;
var
textFieldFutura:TextField=
new
TextField();
textFieldFutura.embedFonts=
true
;
textFieldFutura.antiAliasType=flash.text.AntiAliasType.ADVANCED;
textFieldFutura.defaultTextFormat=textFormatFutura;
textFieldFutura.selectable=
false
;
textFieldFutura.autoSize=TextFieldAutoSize.LEFT;
textFieldFutura.x=
30
;
textFieldFutura.y=
255
;
textFieldFutura.text=str6;
addChild(textFieldFutura);
var
str7:
String
=
"TextFormat.font=\"Trebuchet MS\" fontFamily=\"Trebuchet MS Bold\""
;
var
textFormatTrebuchetMS:TextFormat=
new
TextFormat();
textFormatTrebuchetMS.font=
"Trebuchet MS"
;
textFormatTrebuchetMS.size=
18
;
var
textFieldTrebuchetMS:TextField=
new
TextField();
textFieldTrebuchetMS.embedFonts=
true
;
textFieldTrebuchetMS.antiAliasType=flash.text.AntiAliasType.ADVANCED;
textFieldTrebuchetMS.defaultTextFormat=textFormatTrebuchetMS;
textFieldTrebuchetMS.selectable=
false
;
textFieldTrebuchetMS.autoSize=TextFieldAutoSize.LEFT;
textFieldTrebuchetMS.x=
30
;
textFieldTrebuchetMS.y=
280
;
textFieldTrebuchetMS.text=str7;
addChild(textFieldTrebuchetMS);
var
textFormatPixel:TextFormat=
new
TextFormat();
textFormatPixel.font=
"Arial Unicode MS_9pt_st"
;
textFormatPixel.size=
9
;
var
textFieldPix:TextField=
new
TextField();
textFieldPix.embedFonts=
true
;
textFieldPix.antiAliasType=flash.text.AntiAliasType.NORMAL;
textFieldPix.defaultTextFormat=textFormatPixel;
textFieldPix.y=
210
; textFieldPix.x=
350
;
textFieldPix.width=
340
;
textFieldPix.wordWrap=
true
;
textFieldPix.text=
"Bitmap font support is missing in Flex. This is WEIRD - small "
+
"bitmap fonts are much easear to read than anti-aliased fonts. This font was "
+
"created in Flash IDE, and embedded as an asset from swf file. Note the font"
+
"name in summary below"
;
addChild(textFieldPix);
var
textFieldStats:TextField=
new
TextField();
textFieldStats.embedFonts=
true
;
textFieldStats.antiAliasType=flash.text.AntiAliasType.ADVANCED;
textFieldStats.sharpness=
400
;
textFieldStats.defaultTextFormat=textFormatTahoma0;
textFieldStats.selectable=
false
;
textFieldStats.autoSize=TextFieldAutoSize.LEFT;
textFieldStats.multiline=
true
;
textFieldStats.x=
30
;
textFieldStats.y=
310
;
textFieldStats.htmlText=
"<font color=\"#216633\" face=\"BPreplay\" size=\"12\">"
+
"<b>Embedded fonts summary:</b></font>\n"
+getFontsListing();
addChild(textFieldStats);
}
private
function
getFontsListing():
String
{
var
fontsListing:
String
=
""
;
var
fontArray:
Array
= Font.enumerateFonts(
false
);
fontArray.sortOn([
'fontName'
]);
for
(
var
i:
int
=
0
; i < fontArray.length; i++) {
var
thisFont:Font = fontArray[i];
fontsListing+=thisFont.fontName+
" "
+ thisFont.fontStyle +
"\n"
;
}
return
fontsListing;
}
private
function
makeBackground():
void
{
var
rectCont:Sprite =
new
Sprite();
addChild(rectCont);
var
matrix:Matrix =
new
Matrix();
matrix.createGradientBox(
1
, stage.stageHeight, (Math.PI/
180
)*
90
,
0
,
00
);
rectCont.graphics.beginGradientFill(GradientType.LINEAR,[
0xB0C0C4
,
0xFFFFFF
],
[
1
,
1
],[
0
,
255
],matrix);
rectCont.graphics.drawRect(
0
,
0
,stage.stageWidth,stage.stageHeight);
}
}
}