Hi,
today i wanted to share the final and absolute way to embedded fonts in as3 for flashdevelop or flex, why is something that is not real clear when you search in internet and like you usually don’t get warning and that stuff when something is wrong, is hard to diagnostic the problem.
this is an example class using a embebbed font:
package{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
public class test extends Sprite{
[Embed(source = "C:/7theb.ttf", fontFamily = "7th Service",fontStyle="normal", fontWeight = "bold", mimeType = "application/x-font")]
private var theboldfont:Class;
public var txtFormat:TextFormat = new TextFormat();
public var textTest:TextField = new TextField();
public function test(){
txtFormat.font = new theboldfont().fontName;
txtFormat.color = 0xFFFFFF;
textTest.embedFonts = true;
textTest.defaultTextFormat = txtFormat;
textTest.text=”Testing font”;
}
}
}
now, let analyze the code:
1)the EMBED tag:
maybe this will be the most common source of the problems when you need to embebbed a font. in the example, the font have 5 parameters:
source: this is where the font file is located, as you see, even it’s windows. is needed to use / and not \ as is commonly use for this OS
fontFamily: this is basically the fontFamily that the font belongs, to know it just open the font and you will see it
fontStyle: this is as the word say, the style of the font, can be normal, italic etc..
fontWeight: this could be bold or normal
mimeType: for fonts is always: application/x-font
also there are other properties that can be defined:
fontName: this is the name of the font, if you open the font you will see it and you can define it (some times is necessary but other times broke the code and the font is now showing, like the example i put here)
usually the use of this tag is something of try and fail thing with each font, some fonts will work with all parameters, others you just need to ignore them or you put the wrong family name, weight or style.
then following with the code, we are setting the textFormat.font instancing a new object from the class “theboldfont” that is the one that keeps the properties of the embebed font. in the same line we access to the fontName property that the class have to assign this value to the textFormat.font property.
them, line below, we set the property embebedFonts to true in the textField object and the defaultTextFormat to the textFormat previously defined. i tried to use the function setTextFormat for this also but didn’t work, maybe is why i’m using flash develop but i do not know why
and we are done ! you have embebbed your font and should be working
if not, check and play with the properties of the embed tag, adding/changing/erasing the attributes.
i hope it help you to save some hours of work
Regards,
Shadow.