Starling Font Fallback - actionscript-3

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);

Related

Dynamic font embedding and formatting textflow with them

I am loading multiple swf files containing the different fonts at run-time. I am having problem when I am applying it to textflow in Rich Editable Text. Any Idea how to apply those custom fonts to the textflow ?
Here is the part of my code.
For registering the fonts
var FontClass:*;
var dom:ApplicationDomain = response.target.applicationDomain;
FontClass = dom.getDefinition(fileName) as Class;
Font.registerFont(FontClass);
Works!
For applying the font to textflow.
var cf:TextLayoutFormat = new TextLayoutFormat();
cf.fontFamily = mFontName; //Font Name
cf.fontLookup = FontLookup.EMBEDDED_CFF;
var editManager: IEditManager = IEditManager(textflow.interactionManager);
editManager.applyLeafFormat(cf);
No crash! Works!
But the applied font is not correct. Any suggestion please.
IEditManager is an interface, it doesn't do anything. You should be using EditManager.
Interfaces are not working classes, they used to define a common set of functions, any class that implements an interface, has to create working versions of all functions defined in the interface, or the compiler will fail. Further reading here

How do I get a list of available fonts to select from in the TextField component?

In textfield component ,there's a class named TextFormat. TextFormat has a property named font which can let you set the font, but the help document doesn't give any information about the font, it only tells you the default was "Times New Roman", so I can only set 'Times New Roman'?
Is there a way to list all the fonts available so I can pick up one from the list?
Have a look at Font.enumerateFonts() this function will give you a list of all available embedded and device fonts.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/Font.html#enumerateFonts()
This example first calls the static method Font.enumerateFonts() to
get a list of all device and embedded fonts. Then it sorts the
resulting Array of Font objects by the fontName property.
Next the example shows how to call the Font.enumerateFonts() method
with the enumerateDeviceFonts parameter set to false. The resulting
Array only includes embedded Font objects. (If you run this code
within an application that does not contain any embedded fonts, the
embeddedFonts array will be empty.)
import flash.text.Font;
var allFonts:Array = Font.enumerateFonts(true);
allFonts.sortOn("fontName", Array.CASEINSENSITIVE);
var embeddedFonts:Array = Font.enumerateFonts(false);
embeddedFonts.sortOn("fontName", Array.CASEINSENSITIVE);

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.

AS3: Arabic Embed Fonts

I am using as3 and trying to use embed fonts with Arabic text but it doesn't work
So is there a way that I can access from .tff file the shape of the character or make it works using embed fonts
As using system fonts works but at same time its bad as on some machines it is reverted so I want to use embed font
Hope anyone can help
I found code by some one to change the input arabic text to the correct shape based on the position in text and I modified it to work perfectly with my code :)
here the AS3 code file: http://www.akhalifa.com/testing/ArabicAS3File/ArabicStringUtils.as
and here an example on how to use it: http://www.akhalifa.com/testing/ArabicAS3File/Main.as
You can use any arabic/persian input language like Maryam4
then write your sentence in this program after that copy it and paste in your code
also as #Goran Mottram said embed your font (for this situation if you use Maryam you must select F_fonts)
but noticed that in your code if you paste your arabic language it shows you wired character like ÂMoø , but dont worry if you publish you see a correct arabic word with rtl support
If you have the font installed on your system, make sure to import the font into your library and tick the box "Export for ActionScript" and give it an appropriate name, say MyArabicFont, then bind it to textfield using the following code:
// create the font
var myFont:Font = new MyArabicFont();
// assign to textformat
var myTextFormat:TextFormat = new TextFormat();
myTextFormat.font = myFont.fontName;
// assign to textfield
var myTextField:TextField = new TextField();
myTextField.defaultTextFormat = myTextFormat;
myTextField.embedFonts = true;
myTextField.text = "Hello, world";
this.addChild(myTextField);

ActionScript 3 & font embedding

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.