actionscript 3 as3 flash AIR font embed external - actionscript-3

[Embed(source='/assets/calibri.ttf', fontName="Font", mimeType="application/x-font-truetype" embedAsCFF="false")]
private static var calibri:String;
I Looking from tutorial and get code like above and look like it's must on Class?
can I use it without class? or maybe an example for font loaded class please?

Use the embed param fontFamily. This string you can use to set the font.
[Embed(source='/assets/calibri.ttf', fontFamily="calibri", mimeType="application/x-font-truetype" embedAsCFF="false")]
var CalibriFont:Class;
var tf:TextField = new TextField();
tf.embedFonts = true;
addChild(tf);
var tff:TextFormat = new TextFormat();
tff.font = "calibri";
tf.defaultTextFormat = tff;
tf.htmlText = "HELLO";

Related

AS3 - setTextFormat(), defaultTextFormat not working as designed?

UPDATED with simpler snippet
I am building a Flash swf to check through some XML files using AS3.
I only have access to the Flex compiler, not the actual Flash IDE or Flashdevelop, so all my xml checking code is a single AS file, compiled into an swf. In order to display information, I create and add a TextField and call appendText() (I use += in this snippet).
The snippet below seems to not set the new format on existing text. It appears to not have any effect whatsoever on the text.
I was under the impression that setTextFormat would change the existing text and that any new text added after calling setTextFormat would use the defaultTextFormat object. This does not appear to be the case in my code. Am I incorrect in my understanding?
public function AS3Tester()
{
display_txt = new TextField();
display_txt.multiline = true;
display_txt.width = 1024;
display_txt.height = 768;
var tf:TextFormat = new TextFormat();
tf.size = 12;
tf.font = "Lucida Console";
display_txt.defaultTextFormat = tf;
display_txt.text = "Text Before setTextFormat\n"; //Should use default style tf
addChild(display_txt);
var tf2:TextFormat = new TextFormat();
tf2.size = 18;
tf2.color = 0xFF0000;
display_txt.setTextFormat(tf2);
display_txt.text += "Text after setTextFormat\n"; //previous line should use tf2, this new line should use default tf
}

Try to mask a textfield in actionscript

I trying to mask a textField with the following code, but it's not working.
var Blue_clip:blue_img = new blue_img()
addChild(Blue_clip)
var field:TextField=new TextField();
var format:TextFormat=new TextFormat();
format.size = 40;
format.font = "Arial";
field.defaultTextFormat = format;
field.text = "Hello World";
addChild(field);
Blue_clip.mask = field;
Please guide me.
Method 1) - Use runtime AS mask
_root.attachMovie("mask", "mask_instance", 3 ,{_x:10, _y:20});
_root.masked_content_mc.setMask(mask_instance);
where mask is AS exported movie clip in lybrary (simple shape 100x100px for example)
Method 2) - Embed font.
You have to embed the font using Text->Font Embedding and then set it to the text field:
var style_tf:TextFormat = new TextFormat();
style_tf.font = "ArialEmbed";
style_tf.size = 16;
_root.my_text.setTextFormat(style_tf);

AS3 TextField Fonts

I have problems setting a font in AS3. I have tried several different things using ressources from forums and questions but I can't get it to work.
Here's the code I use:
private function addContentToMovieClips(Text:String, MC:MovieClip):void
{
var myFont = new Trebuchet();
var tFormat:TextFormat = new TextFormat();
tFormat.font = myFont.fontName;
tFormat.color = 0x000000;
trace(tFormat.font);
var tf:TextField = new TextField();
tf.defaultTextFormat = tFormat;
tf.embedFonts = true;
//tf.antiAliasType = AntiAliasType.ADVANCED;
tf.text = Text;
MC.addChild(tf);
tf.width = 300;
}
In the library, I have a Font named "font2" with AS linkage "Trebuchet". I get no compiler errors and text seems to be created on the screen but nothing is displayed.
The following line is for debugging:
trace(tFormat.font);
And returns "Trebuchet MS" as expected.
I'd be very grateful if you can help me understand why this does not work!
Cheers,
Patrick
Edit: when removing
tf.embedFonts = true;
the text is displayed with the correct font. Not sure why but this does the trick for now.
You should have pasted the code where you embed the font. But since I've had the same problem, I guess you missed the embedAsCFF directive:
[Embed(source="../someFont.ttf",
fontName = "myFont",
mimeType = "application/x-font",
fontWeight="normal",
fontStyle="normal",
unicodeRange="englishRange",
advancedAntiAliasing="true",
embedAsCFF="false")]

Why can't I display embedded fonts in AS3?

I have gone through all topics on Embedding fonts in AS3 I could find,a nd tried all solutions. I'm probably missing something obvious, but I don't fully understand what I'm doing so please guide me in the right direction. Many of the answers involve Flash Builder or another tool but I use FlashDevelop. No idea whether that matters.
I have this line in my Main.as:
[Embed(source = "assets/SKA_75_marul_CE_extended.ttf",
fontName = "SKA_75_marul_CE_extended",
fontWeight = "bold",
advancedAntiAliasing = "true",
mimeType = "application/x-font")]
public static var SKA_75_marul_CE_extended:String;
And this exists in the constructor of an extended Sprite called Pointer.as:
var format:TextFormat = new TextFormat();
format.font = "SKA_75_marul_CE_extended";
format.color = 0xFFCCCC;
format.size = 20;
var label:TextField = new TextField();
label.defaultTextFormat = format;
label.text = "test";
label.embedFonts = true;
label.antiAliasType = AntiAliasType.ADVANCED;
//label.setTextFormat(format); --> I tried this too, didn't work...
label.defaultTextFormat = format;
label.x += img.width + 50;
this.addChild(label);
The only way I've found to get it to display anything is if I turn off embedFonts. I've tried embedding C:/windows/fonts/arial.ttf without success.
It seems that embedding fonts is a dark art like no other and I must concede after 1 hour of struggling. Please send help.
UPDATE:
Here's the working code, turns out it was due to having the correct order of operations...:
[Embed(source="assets/SKA_75_marul_CE_extended.ttf",
fontName = "myFont",
mimeType = "application/x-font",
fontWeight="normal",
fontStyle="normal",
unicodeRange="U+0020-U+007E",
advancedAntiAliasing="true",
embedAsCFF="false")]
private var myEmbeddedFont:Class;
var tf:TextFormat = new TextFormat( "myFont", 20,0xffffff );
var t:TextField = new TextField;
t.embedFonts = true; // very important to set
t.defaultTextFormat = tf;
t.text = text;
t.x += img.width + 50;
t.width = 700;
this.addChild( t );
It's most DEFINITIVELY a "dark art" to get embedded fonts to work right. I would first check if "SKA_75_marul_CE_extended" is the actual name the font has in its metadata (I used Suitcase Fusion to extract the name). I've also seen TTF fonts that Flash simply refuses to embed (perhaps invalid metadata causes the embed system to fault). I would continue testing with a known working font until you find the actual problem in case it is a font file problem.
One thing I noticed is "public static var SKA_75_marul_CE_extended:String;"... shouldn't this be of type Class?
FlashDevelop font embed reference from someone who had issues:
http://www.flashdevelop.org/community/viewtopic.php?p=28301

Rendering text in AS3

I'm having a bit of confusion about how to render text in a pure AS3 project. There are classes like flash.text.StaticText but these are designer-only, you can't create them in code. I was half-expecting the Graphics class to have text-rendering options but alas, no.
Specifically I was going to add a label above each player's sprite with their name, health %, etc. So I expected to add a child text-element or draw text using Graphics in some way... it's read-only and should not support user-input, I just want to draw text on-screen.
You can use TextField class for this. Please check the reference. All fields and methods are self explanatory.
A possible example.
var myField:TextField = new TextField();
myField.text = "my text";
myField.x = 200;
myField.y = 200;
addChild(myField); // assuming you are in a container class
If TextField doesn't work, you can create text using this method:
var format:ElementFormat = new ElementFormat();
format.fontSize = 26;
format.color = 0x0000FF;
var textElement:TextElement = new TextElement('Hello World!', format);
var textBlock:TextBlock = new TextBlock();
textBlock.content = textElement;
var textLine:TextLine = textBlock.createTextLine(null, 500);
textLine.x = (stage.stageWidtht - textLine.width) / 2;
textLine.y = (stage.stageHeight - textLine.height) / 2;
addChild(textLine);
Look at:
Creating and displaying text in ActionScript 3.0 Developer’s Guide