Why can't I display embedded fonts in AS3? - actionscript-3

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

Related

actionscript 3 as3 flash AIR font embed external

[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";

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
}

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")]

Embedded Fonts in AS3 and IE: Is the syntax correct?

I spent a good bit of time this morning trying multiple solutions I found throughout the site but unfortunately, none of them truly worked. Here's the problem. I believe that my process of font embedding is working because I purposely removed the fonts from my machine and I see them showing up in the flash player. However, they do not show up on our Windows test machine (in either IE or FF) and I can't seem to figure out why. As I mentioned, I tried several of the solutions found throughout the site but it didn't seem to make a difference. Here is excepts of the class and constructor that I have built:
public class MyClass {
[Embed(source = "../../fonts/DroidSans.ttf",
fontName = "DroidSans",
mimeType = "application/x-font-truetype",
fontWeight = "normal",
fontStyle = "normal",
unicodeRange = "U+0041-U+005A, U+0061-U+007A, U+0030-U+0039, U+002E-U+002E",
embedAsCFF = "false")]
private var droidSansFont:Class;
[Embed(source = "../../fonts/DroidSans-Bold.ttf",
fontName = "DroidSansBold",
mimeType = "application/x-font-truetype",
fontWeight = "normal",
fontStyle = "normal",
unicodeRange = "U+0041-U+005A, U+0061-U+007A, U+0030-U+0039, U+002E-U+002E",
embedAsCFF = "false")]
private var droidSansBoldFont:Class;
.
.
.
public function MyClass() {
_flVersion = String(flash.system.Capabilities.version);
_baseFont = (_flVersion.indexOf("10,") != -1) ? "Arial" : "DroidSans";
_boldFont = (_flVersion.indexOf("10,") != -1) ? "Arial" : "DroidSansBold";
buttonFormat = new TextFormat();
buttonFormat.font = _baseFont;
buttonFormat.size = 10;
buttonFormat.color = 0x000000;
buttonFormat.bold = false;
buttonFormat.align = "center";
.
.
.
}
}
If anyone can offer up a solution or point me in the right direction, it would be most helpful.
Are you setting embedFonts=true on your TextField?
And be careful with version test:
_flVersion.indexOf("10,")!=-1
This could match anywhere in the version String. You negaitve logic is confusing me a little, but thinking carefully, you check will DISABLE embeded fonts in Flash Player versions, for example: 10,1,100 and 12,10,115
Assuming you do intend to disable embedded fonts in FP10, you could possibly change your condition to the following (which won't match 12,10,115):
_flVersion.indexOf("10,")==0
I'd then put this in a commented getter function to make your code more readable:
// No embedded fonts for flash player 10
private static var _flVersion:String = String(flash.system.Capabilities.version);
private function get use_embedded_fonts():Boolean {
return _flVersion.indexOf("10,")!=0;
}
Also, you'll only want to set the embed flag when using the embedded Droid font and not Arial:
_baseFont = (!use_embedded_fonts) ? "Arial" : "DroidSans";
_boldFont = (!use_embedded_fonts) ? "Arial" : "DroidSansBold";
_embed_flag = use_embedded_fonts;
textfield.embedFonts = _embed_flag;
...

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