Check if font is embedded? - actionscript-3

Can I check if I'm using an actual font when I create TextFormat/TextField ? If I specify a not embedded font or use any random string, no text is displayed and I don't know why.
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class SimpleSprite extends Sprite
{
//[Embed(source="C:/Windows/Fonts/Arial.ttf",fontName="default_font",mimeType="application/x-font",fontWeight="normal",fontStyle="normal",advancedAntiAliasing="true",embedAsCFF="false")]
//private static var defaultFont: String;
public function SimpleSprite()
{
var t: TextField = new TextField;
t.autoSize = TextFieldAutoSize.LEFT;
t.defaultTextFormat = new TextFormat("default_font", 16, 0xff0000);
t.embedFonts = true;
t.text = "hello world";
addChild(t);
}
}
It doesn't display any text when the embed lines are missing.
Important: My package that creates TextFields does not embed anything and I wish to keep it that way. The embedding must be done by the programmer who uses the package. I want to check if the font is embedded and throw an error if not.

You can use Font.enumerateFonts which will return an array of available embedded fonts. You could use that to create a function like the following:
private function hasEmbeddedFont(fontName:String):Boolean
{
var fonts:Array = Font.enumerateFonts();
for each(var font:Font in fonts)
{
if (font.fontName == fontName)
{
return true;
}
}
return false;
}
And then use it something like this:
t.autoSize = TextFieldAutoSize.LEFT;
t.defaultTextFormat = new TextFormat("default_font", 16, 0xff0000);
t.embedFonts = hasEmbeddedFont("default_font");
t.text = "hello world";
If you're building a library for others to use, you might consider abstracting it into your own custom subclass of TextField so it's all handled automatically.

Related

Dynamically display text in Flex, ActionScript 3

I've been having problems displaying text in ActionScript 3 using Flex SDK 4.6. Any method I try results in no change or a black screen; here is my project code and attempts.
MyProject.mxml is simply and mx:Application tag with the relevant parts being
<mx:Script>
<![CDATA[
include "MyProject.as"; //simply running my project file
]]>
</mx:Script>
and
<mx:Canvas id="gamePanel" x="0" y="0" width="100%" height="100%"/> //defining the canvas
In MyProject.as I have
import flash.display.*;
import flash.events.*;
import mx.events.*;
import mx.controls.*;
import Game;
public static const SCREEN_WIDTH:int = 960;
public static const SCREEN_HEIGHT:int = 720;
private var initializationCompleted:Boolean = false;
public var screenBuffer:BitmapData;
public var game:Game;
public function setup():void {
screenBuffer = new BitmapData(SCREEN_WIDTH, SCREEN_HEIGHT, false, 0x00000000);
game = new Game(SCREEN_WIDTH, SCREEN_HEIGHT, screenBuffer);
initializationCompleted = true;
}
private function updateFrame():void {
if (!initializationCompleted) {
return;
}
draw();
gamePanel.graphics.clear();
gamePanel.graphics.beginBitmapFill(screenBuffer, null, false, false);
gamePanel.graphics.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
gamePanel.graphics.endFill();
}
private function draw():void {
game.update();
}
And in Game.as, I simply draw everything using the BitmapData class, and then copy everything to the screenBuffer:
screenBuffer.copyPixels(myBitmap, new Rectangle(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), new Point(0,0));
(This is only the relevant code - I trimmed as much as possible to leave a "Minimal, Complete, and Verifiable example")
Now I have been having problems displaying text in my project. I know that TextField is a subclass of flash.display.Sprite which can be added to the canvas. Whenever I try using something like
var txtHello:TextField = new TextField();
txtHello.text = "Hello World";
gamePanel.addChild(txtHello)
this either changes nothing (if used in setup(), I'm assuming I'm drawing over it or else it is never displayed) or causes a black screen (if used anywhere in updateFrame(), I'm assuming I'm creating infinite sprites).
I have tried instead, creating a new file named "TextWithImage.as" with the contents
//this is ripped off the adobe help page
package {
import flash.display.Sprite;
import flash.text.*;
public class TextWithImage extends Sprite {
private var myTextBox:TextField = new TextField();
private var myText:String = "Hello World";
public function TextWithImage() {
addChild(myTextBox);
myTextBox.text = myText;
}
}
}
importing it in MyProject.as, and then using it as
gamePanel.addChild(new TextWithImage());
to the same effect as my previous attempt.
What is the simplest way to display text in Flex/AS3? Any help is appreciated, and thank you in advance!
There's a trick. Flex components, albeit having the same addChild method derived from DisplayObjectContainer class, cannot actually add regular Flash content - Shape, Sprite, MovieClip, TextField, Bitmap - directly. More to that, they don't produce any runtime error, which I personally think they totally could to not confuse new people.
Flex component can only addChild classes that extend the basic UIComponent class. At the same time, UIComponent can addChild regular Flash content. Thus you do it as following:
var proxyContainer:UIComponent = new UIComponent;
var txtHello:TextField = new TextField;
txtHello.text = "Hello World";
proxyContainer.addChild(txtHello);
gamePanel.addChild(proxyContainer);

embedded font flash.text.engine

I've completely run out of ideas on this. It follows, and is part of my previous question:
embedding a font in a swf using as3
I just don't seem to be able to get the flash.text.engine to use my embedded font. NB the font has to be loaded into the application (as embedded in swf) after the user has chosen the two languages (for translation to and from). There seems to be a little info implying that it is now necessary to use the fontswf application which is in the sdk. I have tried this and produced loadable swf files but I can't find any info on how these are then loaded (i.e. the getDefinition and registerFont bits below don't work as there are no classes in these swf) and applied to text.engine objects. The source for the embedding is in my answer to my question above. This is a test as3 which demonstrates how it doesn't work!
package
{
import flash.display.Loader;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.text.engine.ElementFormat;
import flash.text.engine.FontDescription;
import flash.text.engine.TextBlock;
import flash.text.engine.TextLine;
import flash.text.engine.TextElement;
import flash.net.URLRequest;
import flash.text.Font;
public class Main extends Sprite
{
private var loader:Loader;
private var tl:TextLine;
public function Main():void
{
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,fontLoaded);
loader.load(new URLRequest("EnglishF.swf"));
}
private function fontLoaded(evt:Event):void {
var FontClass:Class
FontClass = evt.target.applicationDomain.getDefinition("EnglishF") as Class;
try {
Font.registerFont(FontClass.myFont);
trace("successfully loaded " + FontClass);
// gives 'successfully loaded EnglishF'
} catch (err:Error) {}
var fontList:Array = Font.enumerateFonts();
for (var i:int = 0; i < fontList.length; i++) {
trace(fontList[i].fontName, fontList[i].fontType);
// gives 'EnglishF embeddedCFF'
}
var block:TextBlock = new TextBlock();
var font:FontDescription = new FontDescription("EnglishF");
var formt:ElementFormat = new ElementFormat(font, 30);
trace(FontDescription.isFontCompatible("EnglishF","normal","normal"), formt.fontDescription.fontName);
// gives 'true EnglishF'
formt.color = 0x882233;
var span:TextElement = new TextElement("Hello World. This is certainly NOT in the Font provided!", formt);
block.content = span;
tl = block.createTextLine();
tl.x = 10;
tl.y = tl.ascent + 10;
addChild(tl);
}
}
}
Am I doing anything wrong, or is this impossible?
Hopefully this will help. Under this line:
var font:FontDescription = new FontDescription("EnglishF");
add the following line:
font.fontLookup = FontLookup.EMBEDDED_CFF;
This will let the text framework know that you're using a CFF font (used in the new text framework), instead of a regular embedded font (used in TextFields).
Hope this helps.
Jordan

AS3 Text Input accents

I create an AS3 TextField set to Input mode dynamically through code, but when the user tries to input some special characters (eg. á à é è í ì ó ò ú ù ç) they simply do not appear on the TextInput.
Copying and pasting them to the textfield works, but I'd prefer if the user could directly type them.
Here's a quick test demonstrating this:
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
public class TextInput extends Sprite
{
public function TextInput()
{
super();
var t:TextField = new TextField;
t.type = TextFieldType.INPUT;
addChild(t);
}
}
}
This creates a textfield where the user can type, but I can't type special characters like à.
Many thanks.
If you can paste it into the Input field, you should be able to type it.
If you start a new Flash document, using the same font as you are above create an Input textfield on the stage with the following settings:
Embed the normal glyphs
Embed the extended latin glyphs
And this should work, as:
Now if all this works, it might have something to do with the way the class is written.
Writing classes that embeds fonts is frankly a pain. Make sure you embed the font in the library and export it for action script:
Following that, you need to use the following code:
// The name of the font class
var _font:Calibri = new Calibri();
var _textFormat:TextFormat = new TextFormat();
_textFormat.font = _font.fontName;
_textFormat.size = 16;
// For some weird reason the ordering here is important. I remember mucking around with this for ages for an old project. EmbedFonts must come last
var _textField:TextField = new TextField();
_textField.defaultTextFormat = _textFormat;
_textField.type = TextFieldType.INPUT;
_textField.embedFonts = true;
addChild(_textField);
And that should have it all working:
** EDIT **
To those using FlashDevelop, etc you can use the following method:
public class Main extends MovieClip {
[Embed(source='assets/HOBOSTD.OTF', fontName='_hobo', embedAsCFF="false")] public static const HOBO:Class;
public function Main() {
var _font:Font = new HOBO() as Font;
var _textFormat:TextFormat = new TextFormat();
_textFormat.font = _font.fontName;
_textFormat.size = 22;
var _textField:TextField = new TextField();
_textField.embedFonts = true;
_textField.defaultTextFormat = _textFormat;
_textField.autoSize = TextFieldAutoSize.LEFT;
_textField.antiAliasType = AntiAliasType.ADVANCED;
_textField.type = TextFieldType.INPUT;
addChild(_textField);
}
}
And you will get the following:
Now note, the font file must be either relative to your project, or the source can point to the C:\windows\font folder if you choose. In the above example, I copied the font to my assets folder.

Flash/Flex error 1067: can't make a custom TextFormat object?

Because I want to avoid repetitive code, and I'm using a lot of text formats, I created a CustomTextFormat class in Flex Builder.
Another class, called CustomInputBox.as is using this object to create a format:
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
public class CustomInputBox extends Sprite
{
public function CustomInputBox(xLoc:int, yLoc:int, width:uint, height:uint, password:Boolean = false, text:String = "", font:String = "Arial", fontColor:uint = 0x000000, fontSize:uint = 18, fontBold:Boolean = false)
{
var inputBox:TextField = new TextField();
inputBox.type = TextFieldType.INPUT;
inputBox.mouseEnabled = true;
inputBox.selectable = true;
inputBox.multiline = false;
inputBox.x = xLoc;
inputBox.y = yLoc;
inputBox.width = width;
inputBox.height = height;
inputBox.displayAsPassword = password;
var format:CustomTextFormat = new CustomTextFormat();
inputBox.defaultTextFormat = format;
inputBox.text = text;
addChild(inputBox);
}
}
}
The code of CustomTextFormat is as follows:
package
{
import flash.display.Sprite;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
public class CustomTextFormat extends Sprite
{
public function CustomTextFormat(font:String = "Arial", fontColor:uint = 0x000000, fontSize:uint = 18, fontBold:Boolean = false, fontAlign:String = TextFormatAlign.LEFT)
{
var format:TextFormat = new TextFormat();
format.font = font;
format.color = fontColor;
format.size = fontSize;
format.bold = fontBold;
format.align = fontAlign;
}
}
}
Now, I'm getting error 1067 in the CustomInputBox.as file, it's a Dutch error unfortunately (any way to set flex errors to english?):
1067: Impliciete afgedwongen omzetting van een waarde van het type
CustomTextFormat in een niet-gerelateerd type flash.text:TextFormat.
CustomInputBox.as
It's difficult to translate, but hopefuly the error number and the code are enough to identify my problem. I'm new to Flash, and searched but couldn't find out what I am doing wrong.
Thanks in advance.
Something's wacky here. If you want to assign your custom format like this:
var format:CustomTextFormat = new CustomTextFormat();
inputBox.defaultTextFormat = format;
Then CustomTextFormat needs to extend TextFormat, and the code in CustomTextFormat's constructor should be modifying the inherited TF properties. Alternately, if you want to leave CustomTextFormat extending Sprite, then you need to change CustomTextFormat's "format" property to be a public property, and change your assignment to something like:
var customFormat:CustomTextFormat = new CustomTextFormat();
inputBox.defaultTextFormat = customFormat.format;
Does that make sense? Right now you're trying to set the input's default text format to a class object that extends Sprite. And the inputBox doesn't know anything about CustomTextFormat's internal "format" property, which is both private and temporary.
(Incidentally none of this precisely explains the error message you're getting, but in my experience it's somewhat rare for Flash compiler errors to really tell you what's wrong... they tend to claim you're using classes illegally when all you did is leave out a semicolon. I tend not to trust the error messages too much.)

ActionScript 3 Error 1037: Packages cannot be nested

I am new to AS3. When learning AS3, I get the below code from an Adobe example and trying to run it gives an error like
"1037: Packages cannot be nested."
What does this mean?
Please tell me how to execute? or any problem in this code?
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
public class TextField_alwaysShowSelection extends Sprite {
public function TextField_alwaysShowSelection() {
var label1:TextField = createTextField(0, 20, 200, 20);
label1.text = "This text is selected.";
label1.setSelection(0, 9);
label1.alwaysShowSelection = true;
var label2:TextField = createTextField(0, 50, 200, 20);
label2.text = "Drag to select some of this text.";
}
private function createTextField(x:Number, y:Number, width:Number, height:Number):TextField {
var result:TextField = new TextField();
result.x = x; result.y = y;
result.width = width; result.height = height;
addChild(result);
return result;
}
}
}
You need to create an action script file and then add that class to document class in your fla file property then it would not give you an error
Your code should compile, provided that it is in the root source folder ("src" in flex builder). Are you sure that's the entire file?
The error means that you have nested a package {} statement inside another package {} statement.
If you want to include the AS3 in the timeline itself, use this:
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
function TextField_alwaysShowSelection() {
var label1:TextField = createTextField(0, 20, 200, 20);
label1.text = "This text is selected.";
label1.setSelection(0, 9);
label1.alwaysShowSelection = true;
var label2:TextField = createTextField(0, 50, 200, 20);
label2.text = "Drag to select some of this text.";
}
function createTextField(x:Number, y:Number, width:Number, height:Number):TextField {
var result:TextField = new TextField();
result.x = x; result.y = y;
result.width = width; result.height = height;
addChild(result);
return result;
}
How are you running this file? This is not a complete file. If you are working with flex then you need the supported MXML code. However the error indicates that you are working with src folder. It would be good if you give the complete procedure.
If you are using Flash, put that code in a file named "TextField_alwaysShowSelection.as", place it next to your FLA and set that class name as the DocumentClass in the "properties" panel of your FLA... so where it says "Class:" write "TextField_alwaysShowSelection".