ActionScript 3 & font embedding - actionscript-3

I have a problem with doing proper font embedding in a actionscript 3 project (flash CS4, not flex).
I followed this Adobe guide to do font embedding:
http://www.adobe.com/devnet/flash/quickstart/embedding_fonts/
the guide tells to set the Textfield.embedFonts property to true. if I do so and try to display a text with another font in this textfield, then nothing is displayed - that's fine, i expect it like this.
But now I have this particular problem:
i embed the font "Arial" in Regular style and create two input Textfields on the stage. one of them i set the embedFonts property to true (as described in the guide) the other i leave as is. Now I publish the thing as swf and try to enter the following (Turkish) string into the textfield
Yeni Yılın Barış ve Mutluluk Getirmesini Dileriz.
now the problem is, that the untouched textfield displays the string correctly - but the one with embedFonts set to true is missing some of the letters (for example ş is not displayed). But the Arial font does have this letter since it is displayed correctly in static - so why does it not render correctly when i set this property (as told in the guide)?
in my final app there shall be a single textField, but multiple embedded fonts and a way to switch between them (for example the user must be able to choose another font for entering chinese text).
can someone tell me how to do it properly?
thanks!

Embedding fonts in Flash is not as straight forward as it should be, and there are a bunch of special cases... One way to ensure that you are embedding the correct characters of your font is enabling "Generate size report" in the Flash Publish Settings.... there you will see all characters of all fonts that are being embedded. The only exception is that fonts embedded using the [EMBED tag do not show there.
Adding the font to the library doesn't embed the whole set of characters of that font (Arial for instance is about 8mb)... it only embeds a subset of them... I'm not sure if its always the standard occidental Latin set, or if it depends on the computer language.
You can extend this set manually using any textfield in your movie (with the "Character embedding..." dialog) as long as you use the actual Library Font name (it shows in the font list with an asterisc at the end... in your case it would be "Arial*").
You can also use the [EMBED tag with the unicodeRange to declare de character set, but bare in mind that the fonts you declare there wont be available in the Flash IDE during editing time... you need to set them at runtime with ActionScript (TextFormat, StyleSheet, etc...), which is not very practical when working with Flash.

I use the system fonts, and avoid the embedded ones like Arial.
public function get availableFonts(): Array
{
var font: Font = null;
var allFonts: Array = Font.enumerateFonts(true).sortOn("fontName", Array.CASEINSENSITIVE);
var embeddedFonts: Array = Font.enumerateFonts(false);
var excludeList: Object = {}
for each(font in embeddedFonts)
{
excludeList[font.fontName] = '';
}
var ourFonts: Array = [];
for each(font in allFonts)
{
if (!excludeList.hasOwnProperty(font.fontName))
{
ourFonts.push(font);
}
}
return ourFonts;
}
The list of fonts that this returns are going to have all their letters.
(wouldn't it be nice if ActionScript had some easier way of doing set difference built in?)

If you're looking for a more robust font loading solution that can load the different fonts at runtime, I've posted some code here (http://labs.tomasino.org/2009/07/16/flash-as3-runtime-font-manager/) and explanations of how to use it.

Related

if statement skipped , uppercase letters not displaying

I'm making a mini game for my multimedia class using Adobe Animate with ActionScript 3.
The first question requires the user to fill in the blanks to print "hi" like in python.
the question looks like this
_____("hi")
then you would fill in the blanks and click on a check answer button.
Then to check if the answer is correct I use an if-else statement like so :
import flash.events.MouseEvent;
stop();
checkBtn1.addEventListener(MouseEvent.CLICK, checkClick);
function checkClick(event:MouseEvent): void
{
if(input1.text == "print")
{
ans1.text = "Correct!";
}
else
{
ans1.text = "Wrong answer!";
}
}
However, even if the input is correct, it will skip the if part of the code and run straight into else.
It also won't display uppercase letters?
It'll display "rong answer!" ignoring the uppercase "W".
Any ideas?
#Organis is right; The font needs to be embedded.
Flash Professional CS5 (and later) automatically embeds any characters used by text fields on the Stage. However, if you are using Dynamic or Input text fields, the project may also need to include characters for the font that aren't specifically contained in instances on the Stage during authoring. If you do not embed the necessary characters and text elements are programmatically displayed or entered by the user, the characters may be missing and the application may appear broken.
Common Mistakes Working with Fonts and Text Objects in Flash
For help embedding fonts, refer to the Adobe documentation on it.
Embedding Fonts with ActionScript
Embedding Fonts in Adobe Animate (Flash)

Starling Font Fallback

I'm using bitmap fonts to display text.
In case of special characters such as Chinese, I'd like to be able to fall back to system default instead of having all possible characters in the world in the bitmap font.
Is that possible?
Starling's TextField does not provide a way to specify fallback fonts. You will need to check which language you are using beforehand and pass in the font name (in your case system default) that you know will work for the current language.
How to embed a font:
[Embed(source="../embeds/fonts/FuturaLTPro-Book.ttf", embedAsCFF="false", fontFamily="FuturaLTPro-Book")]
public static const Font_FuturaLTPro_Book:Class;
// create a TextFormat instance and use it instead of your bitmap font
var textFormat:TextFormat = new TextFormat(Font_FuturaLTPro_Book:Class, 18, 0x000000);

How to get the "style" from a classic textField when loaded by code?

I need to get the style used in a textField placed in the stage, and doing this by code.
The thing is, bold and italic are not enough in my case, since I have a style that is semi-bold.
And I can't find the way to get that style when I analyze the textField in my code.
Do you know of any way to do this?.
In other words, I need to know what is the style of the font used in Flash when I get the textFormat.
For example, if I have a font that is "Signika", I need to know that the style is Semibold. Right now, I only get "Signika". It works if I check the bold style, becuase bold is true. But thats all the information I can get from it.
Thanks.
Behold TextField API Reference and the getTextFormat() method. (yay!)
It returns a TextFormat object which contains a complete definition of that TextField, which can then be applied to another TextField via setTextFormat().
That said, font families work a little differently in AS3 (sadly, not Adobe's fault, but rather the OS). For example, Myriad Pro has Regular, Light, Semibold, and Bold. Yet, once embedded, only two actual font names are displayed, and the only way to differentiate them is by setting the TextFormat.bold = true.
Interestingly, omitting the embed of one of these companion fonts will not throw an error, but rather, in the case that you were to embed only Myriad Pro Regular and not Myriad Pro Bold, setting the TextFormat.bold = true will perform faux bold on regular (which will naturally be much lower quality than the actual bold font, but... it works).
Hope that clears things up.
Update
I looked into the Signika, and it apparently suffers from an acute identity crisis. To exemplify this, embed the entire font family and run the following:
var fontList:Array = Font.enumerateFonts();
for( var i:int = 0; i < fontList.length; i++ ) {
trace("Font Name: " + fontList[i].fontName + ", " + fontList[i].fontStyle )
}
This will trace a list of each font that's present in the environment. Sadly, regardless of how you embed the font (programmatically, or via library import), the font overwrites other family entries (hypothetically because they report the same way without distinction to the environment).
// Myriad Pro Results:
Font Name: Myriad Pro, regular
Font Name: Myriad Pro, bold
Font Name: Myriad Pro Light, regular
Font Name: Myriad Pro Light, bold
Running the same query against a the Myriad Pro family actually reports four entries, without overlap, whereas Signika will only report 2 of 4.
// Signika Results:
Font Name: Signika, regular
Font Name: Signika, bold
What I stated previously still stands: using the API methods above will result in a TextFormat object that fully describes the fonts in use. These are distinguished by both name:String, and bold:Boolean properties on the object. However, since Flash overwrites the embedded object with the same reported properties (or simply doesn't overwrite), you only have 2 of the 4 family styles available at any time: a regular version, and a bold version.
Hope that clarifies things.

What fonts are built in flash player/AIR?

I'm porting my AIR app to iPad. I'm using embeded fonts in textfields in my app. But when user starts editing text (after double click the textfield.type is set TextFieldType.INPUT), the text is geting huge... Embeded font is DejaVuSans. When I do textfield.embedFonts=false the huge font problem disappears. I'm not even trying to ask why the font can get huge (looks like it's specific for my app). The question is a bit more simplier. When I set textfield.embedFonts to false Times font is used. But I need DejaVuSans. Heres the question: how is called in flash player (so that I can do textFormat.font=NAME) and where are all available fontfaces are listed?
Thank you in advance!
No fonts are built into Flash Player or AIR. Besides embedding fonts yourself, you can use the fonts that are installed on a user's computer (and some fonts are available on almost all OS'es like Verdana and Times).
You can get a list of all fonts on the user's machine via the enumerateFonts method of Font like this:
(copied from http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/Font.html#enumerateFonts%28%29)
var allFonts:Array = Font.enumerateFonts(true);
from the manual:
enumerateDeviceFonts:Boolean (default = false) — Indicates whether you want to limit the list to only the currently available embedded fonts. If this is set to true then a list of all fonts, both device fonts and embedded fonts, is returned. If this is set to false then only a list of embedded fonts is returned.

Can I convert a library font into a sprite sheet in AS3?

In Flash I am able to create a font asset and add it to the library:
I want to convert this asset into some BitmapData that will contain all of the characters with the correct letter spacing/line height etc.
Is there an inbuilt way of doing this other than manually creating text fields, adding a character, using BitmapData.draw() and then adding the result to a sprite sheet?
If I need to do it manually like above, is there a way to retrieve all of the embedded characters? For example, in the above screenshot I'd expect only a-z, A-Z. Or will I need to note these manually as well?
If you're going to go with your own solution as I mentioned in my comment (like drawing out each supported character and then batching them together) then here is how you can look up every possible glyph supported by the embedded font. (Psuedo Code)
//Build out a loop that will generate char codes for all possible glyphs
for(etc, etc, etc) {
String glyph = String.fromCharCode(%26);
if(myFont.hasGlyphs(glyph) == true) {
myTextField.text = glyph;
myBitmap.draw(myTextField);
//Save image and repeat
}
}
Reference material:
List of supported char codes.
Font.hasGlyphs() documentation.
String.fromCharCode() documentation.