views:

113

answers:

4

Hi,

I am trying to get the following example to work in Flash Builder 4:

http://developer.yahoo.com/flash/articles/runtime-fonts-as3.html

The Actionscript project compiles but all I get on screen is a tiny rotated square with no text in it.

Does anyone know why this might be happening? My code is identical to the example above - I have compiled the first class into _Arial.swf.

Many thanks,

EDIT:

Ive also tried this...

package {  
    import flash.display.Sprite;  
    import flash.display.Loader;  
    import flash.events.Event;  
    import flash.net.URLRequest;  
    import flash.text.*;  

    public class _Arial extends Sprite {
        [Embed(source='C:/WINDOWS/Fonts/ARIAL.TTF', fontName='_Arial', fontFamily='myFont', mimeType='application/x-font')]  
        public static var _Arial:Class;  

        public function _Arial():void {  
            drawText();
        }

        public function drawText():void {  
            var tf:TextField = new TextField();  
            tf.defaultTextFormat = new TextFormat("_Arial",60,0);
            tf.embedFonts = true;  
            tf.antiAliasType = AntiAliasType.ADVANCED;  
            tf.autoSize = TextFieldAutoSize.LEFT;  
            tf.border = true;  
            tf.text = "Scott was here\nScott was here too\nblah scott...:;*&^% ";  
            tf.rotation =  15;
            addChild(tf);
            trace(Font.enumerateFonts());
        }  
    }
}


var fontList:Array = Font.enumerateFonts(false);
for (var i:uint=0; i<fontList.length; i++) {
    trace("font: "+fontList[i].fontName);
}

The trace displays: font: _Arial

+1  A: 

Although your font SWF compiles, you still could check that the font is correctly embedded by simply adding a textfield in that class , using that font, making sure that embedFonts is set to true, rotate the Textfield if you wish and make sure that the text displays. If it doesn't work at that level, no need to go any further...

After that stage , the code is pretty straightforward and I can't see where it could go wrong

EDIT

As mentioned in my comment I wasn't able to replicate the code example , I get the same result as you.

The only way I could get it to work was by embedding the font directly , which is something I often do.

[Embed(source='fonts/Arial.ttf', fontName='_Arial', 
    mimeType="application/x-font-truetype",
    unicodeRange='U+0020-U+002F,U+0030-U+0039,U+003A-U+0040,U+0041-U+005A,U+005B-   U+0060,U+0061-U+007A,U+007B-U+007E',
    embedAsCFF= "false")]
    public class Main extends Sprite
    {
        public function Main():void
        {
          drawText();
        }
    }

I will eventually try to replicate the example code and will check back soon with an answer...

PatrickS
tried that and got the same result. Any idea what is going on?
codecowboy
if the text doesn't display in the first SWF, then you've narrowed down the problem to a font loading issue. what's your coding environment, what IDE do you use?
PatrickS
Its Flash Builder 4 on mac (10.6) or PC(WinXP). Same problem on either. I haven't done any AS in a while so might be doing something dumb. I'm creating a new actionscript Project in FB4 and compiling it against the Flex SDK.
codecowboy
Nothing wrong on your side, I have tried it and this example just doesn't work. As Poke suggests, it probably is a naming convention issue.
PatrickS
A: 

For your embed statement in your code above, I wonder if the compiler is trying to look for the font relative to your source path, rather than the root of your file system. I would try copying the font file into an assets folder under the src folder of your project. Then refer to it in the embed statement as "/assets/ARIAL.TTF". Hope that helps.

Wade Mueller
Thanks for the reply. It was finding the font ok
codecowboy
A: 

You have naming problems. The class is called _Arial, but you give the font you embed the same class name. That's causing problems to begin with.

Second, to use embedded fonts, you just use them like this:

// embed the font
[Embed(source='C:/WINDOWS/Fonts/ARIAL.TTF', fontName='_Arial', mimeType='application/x-font')]  
public static var ArialFont:Class;


// use the font
var someTextFormat:TextFormat = new TextFormat( '_Arial', 12 );
poke
Thanks - can you explain why it is necessary to include 'public static var ArialFont:class'? Where would I reference 'ArialFont'? The example from PatrickS works with the class var 'public static var _Arial:Class; '
codecowboy
Well, you can also make it private. The point is that the `Embed` metatag (as well as any other metatag) refers to some object that has to be listed directly after the tag. With all other embedded types it makes sense to have it (for example when you embed an image), for fonts it does not, as the font is automatically made available by using its fontName or fontFamily.
poke
+1  A: 

Ok, I got it to work... I started with this

   public class _Arial extends Sprite
{

    [Embed(source='fonts/Arial.ttf', fontName='_Arial',
    mimeType="application/x-font-truetype",
        unicodeRange='U+0020-U+002F,U+0030-U+0039,U+003A-U+0040,U+0041-U+005A,U+005B-U+0060,U+0061-U+007A,U+007B-U+007E',
    embedAsCFF= "false")]
    public static var _Arial:Class; 

}

and to test it , I added that

  public function _Arial():void
  {
    var tf:TextField = new TextField();
    tf.defaultTextFormat = new TextFormat ( "_Arial" , 24 , 0 );
    tf.autoSize = TextFieldAutoSize.LEFT;           
            tf.embedFonts = true;
    tf.text = "This is some text to test!";
    tf.rotation = 20;
    addChild(tf);

  }

The text did display , so I got rid of the constructor and tried the code example again, and it worked!!!

PatrickS
Thanks sooo much for your help! This was driving me mental and its now working. I salute you ;-)
codecowboy
Happy it's working now! It had me a bit confused too ;)
PatrickS