Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
0

Embedding font as3

Participant ,
Nov 14, 2010 Nov 14, 2010

Hi!

Im trying to embed a font in a actionScript project in Flash Builder like this:

package {
     import flash.display.*;
     import flash.text.*;
     
     public class HelloWorldEmbedded extends Sprite {
          
          // Embed the font Verdana
          [Embed(source="Verdana.ttf", fontFamily="Verdana")]
          private var verdana:Class;
          
          public function HelloWorldEmbedded() {
               
               var t:TextField = new TextField();
               
               // Tell Flash Player to use embedded fonts when rendering t's content
               t.embedFonts = true;
               
               t.htmlText = "<FONT FACE='Verdana'>Hello world</FONT>";
               t.text = "Hello World";
               
               addChild(t)
          }
     }
}

But when i debug my application i can't see any text!

This is the link of my project http://www.emmelledesign.it/eas3_fonts_embedding.fxp

Thanks so much!

Matteo

TOPICS
ActionScript
12.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 14, 2010 Nov 14, 2010

i believe you need to register the font to use it.  look into Font.registerFont

    Font.registerFont();
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 14, 2010 Nov 14, 2010

registerFont()

add the font to the global font list.

But in this case the font it's Verdana, it's in my global font list because it's a font system, so no need to register!

When you set

TextField.embedFonts = true;

tell Flash Player to use embedded fonts when rendering TextField's content and not operating system's text render!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 14, 2010 Nov 14, 2010

The way you try to embed it assumes that Verdana.ttf is in the same folder as the root application. Also you need to link a static variable to this font.

Try (depending on your directory structure):

[Embed(source = 'C:\WINDOWS\Fonts\Verdana.ttf', fontName = "_verdana", mimeType = "application/x-font-truetype")]
private static var Verdana:Class;

OR

[Embed(systemFont = "Verdana", fontName = "_verdana", mimeType = "application/x-font-truetype")]
private static var Verdana:Class;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 14, 2010 Nov 14, 2010

This line of code in my project:

private var verdana:Class;

it's the variable that will refer to the class that represents the embedded font and it's used only when loading font at runtime.

Of course Verdana.ttf is in the same folder as the root application.

Look at my link, u can download the project!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 14, 2010 Nov 14, 2010

"it's the variable that will refer to the class that represents the  embedded font and it's used only when loading font at runtime."

I am not sure what you mean. Embed metatags are for embedding assets at compilation - not runtime.

Try making variable static.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 14, 2010 Nov 14, 2010

Still not working!

private var verdana:Class;

Font.registerFont(verdana)

I don't need to register because it's a system font!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 14, 2010 Nov 14, 2010

Oh, just noticed.

Actually your code does embed font. The problem is that you rely on font face in html tag. It doesn't work. You need to set TextFormat to the TextField.

The following works perfectly:

package {
     import flash.display.Sprite;
     import flash.text.TextFieldAutoSize;
     import flash.text.TextField;
     import flash.text.TextFormat;
     import flash.text.AntiAliasType;
     
     public class FontTest extends Sprite {
          [Embed(source="Verdana.TTF", fontFamily="Verdana")]
          public var font:String;
          public function FontTest() {
               var format:TextFormat = new TextFormat();
               format.font = "Verdana";
               format.color = 0xFF0000;
               format.size = 50;
               var label:TextField = new TextField();
               label.embedFonts = true;
               label.autoSize = TextFieldAutoSize.LEFT;
               label.antiAliasType = AntiAliasType.ADVANCED;
               label.defaultTextFormat = format;
               label.htmlText = "<p>Hello world!</p>";
               addChild(label);
       }
    }
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 16, 2010 Nov 16, 2010

.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 16, 2010 Nov 16, 2010

Font face works perfectly!

Try if u have time!

package {
     import flash.display.*;
     import flash.text.*;

     public class FontAvailability extends Sprite {
          
          [Embed(source="fontName.ttf", fontFamily="fontName", embedAsCFF="false")]
          private var ClassName:Class;
          
          public function FontAvailability() {
               
               var t:TextField = new TextField();
               t.embedFonts = true;
               t.htmlText = "<FONT SIZE='33' FACE='fontName'>Hello world</FONT>";
               addChild(t)
          }
     }
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 16, 2010 Nov 16, 2010

Thanks. I guess with SDK 4 face works now. Doesn't work with SDK 3.5.

I will play with it (or, at least, remember, hopefully, for the future references).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 17, 2010 Nov 17, 2010

No!

It works in 3.5 to!

Try it!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 17, 2010 Nov 17, 2010
LATEST

You are right. It does.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 14, 2010 Nov 14, 2010

Just got rusty on fonts.

In order to use embedded fonts with html tags you have to use StyleSheet. The following works without setting TextFormat:

package {
     import flash.display.Sprite;
     import flash.text.AntiAliasType;
     import flash.text.StyleSheet;
     import flash.text.TextField;
     import flash.text.TextFieldAutoSize;
     
     public class FontTest extends Sprite {
          [Embed(source="Verdana.TTF", fontFamily="Verdana")]
          public var verdana:String;
          [Embed(source="ARIAL.TTF", fontFamily="Arial")]
          public var arial:String;
          public function FontTest() {
               var style:StyleSheet = new StyleSheet();
               style.setStyle(".redFont", { fontFamily: "Verdana", fontSize: 50, color: "#ff0000" } );
               style.setStyle(".greenFont", { fontFamily: "Arial", fontSize: 30, color: "#008000"} );
               var label:TextField = new TextField();
               label.embedFonts = true;
               label.autoSize = TextFieldAutoSize.LEFT;
               label.antiAliasType = AntiAliasType.ADVANCED;
               label.styleSheet = style;
               label.htmlText = "<p><span class='redFont'>Hello world!</span><span class='greenFont'> It works!</span></p>";
               addChild(label);
       }
    }
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 14, 2010 Nov 14, 2010

Is working the first example that u write?

I tried but still don't work!

If u download my example, i did a method call showEmbeddedFonts () that show the embedded font! And it's embedding the font but i cant see the text!

You can formatt text with emedded font in different way:

// Set the font with a TextFormat object
var format:TextFormat = new TextFormat();
format.font = "Verdana";
var t:TextField = new TextField();
t.embedFonts = true;
t.defaultTextFormat = format;
t.text = "hello world";
               
// Or set the font with HTML
var t:TextField = new TextField();
t.embedFonts = true;
t.htmlText = "<FONT FACE='Verdana'>Hello world</FONT>";


// Or set the font with CSS
var styleSheet:StyleSheet = new StyleSheet();
var pStyle:Object = new Object();
pStyle.fontFamily = "Verdana";
styleSheet.setStyle("p", pStyle);
var t:TextField = new TextField();
t.embedFonts = true;
// Assign styleSheet before assigning htmlText!
t.styleSheet = styleSheet;
t.htmlText = "<p>hello world</p>";

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 14, 2010 Nov 14, 2010

Both my examples work perfectly. Again, <font> tag with font face in htmlText doesn't work.

Sorry, but I don't download projects from the forum.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 14, 2010 Nov 14, 2010

Strage,

because my last post it's from Colin Moock EssentialActionScript 3.0 page 742.

This is my project:

HelloWorld class:

package {
     import flash.display.*;
     import flash.text.*;
     
     // This class demonstrates how to format text using embedded fonts.
     // The fonts, themselves, are embedded in the class FontEmbedder.
     public class HelloWorld extends Sprite {
          // Make a reference to the class that embeds the fonts for this
          // application. This reference causes the class and, by extension, its
          // fonts to be compiled into the .swf file.
          FontEmbedder;
          
          public function HelloWorld () {
               // Create a text field
               var t:TextField = new TextField();
               t.border=true
               
               
               // Tell ActionScript to render this text field using embedded fonts
               t.embedFonts = true;
               
               // Use two variations of Verdana (normal, and bold)
               t.htmlText = "<FONT FACE='Pilgiche'>Hello world</FONT>";
               
               // Enable FlashType (i.e., Saffron) text rendering
               t.antiAliasType = AntiAliasType.ADVANCED;
               
               // Add the text field to the display list
               addChild(t);
               
               showEmbeddedFonts ();
          }
          
          // Outputs a list of the currently available embedded fonts
          public function showEmbeddedFonts ():void {
               trace("========Embedded Fonts========");
               var fonts:Array = Font.enumerateFonts();
               fonts.sortOn("fontName", Array.CASEINSENSITIVE);
               for (var i:int = 0; i < fonts.length; i++) {
                    trace(fonts.fontName + ", " + fonts.fontStyle);
               }
          }
          
     }
}

FontEmbedder class:

package {
  // Embeds the fonts for this application
  public class FontEmbedder {
     
     [Embed(source="assets/Pilgiche.ttf",
           fontFamily="Pilgiche")]
     private var pilgiche:Class;
  }
}

in assets folder i have Pilgiche.ttf file!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 14, 2010 Nov 14, 2010

Moock uses mx.core package. I am not sure why he does it.  Here are classes that use his approach without referencing to mx.core (they work with Flex compiler). Application class is FontTest:

package 
{
     public class Verdana
     {
          [Embed(source="Verdana.TTF", fontFamily="Verdana")]
          public var verdana:String;
     }
}

package 
{
     public class Arial
     {
          [Embed(source="CURLZ___.TTF", fontFamily="Arial")]
          public var arial:String;
     }
}

package 
{
     import flash.display.Sprite;
     import flash.text.AntiAliasType;
     import flash.text.GridFitType;
     import flash.text.StyleSheet;
     import flash.text.TextField;
     import flash.text.TextFieldAutoSize;
     public class FontTestChild extends Sprite
     {
          public function FontTestChild()
          {
               var style:StyleSheet = new StyleSheet();
               style.setStyle(".redFont", { fontFamily: "Verdana", fontSize: 50, color: "#ff0000" } );
               style.setStyle(".greenFont", { fontFamily: "Arial", fontSize: 30, color: "#008000"} );
               style.setStyle("p", { fontFamily: "Arial", fontSize: 100, color: "#000080"} );
               var label:TextField = new TextField();
               label.embedFonts = true;
               label.autoSize = TextFieldAutoSize.LEFT;
               label.antiAliasType = AntiAliasType.ADVANCED;
               label.gridFitType = GridFitType.PIXEL;
               label.sharpness = -200;
               label.styleSheet = style;
               label.htmlText = "<p>AHA!!! <span class='redFont'>Hello world!</span><span class='greenFont'> It works!</span></p>";
               addChild(label);
          }
     }
}

package {
     import flash.display.Sprite;
     public class FontTest extends Sprite {
          Verdana;
          Arial;
          public function FontTest() {
               var fontTest:FontTestChild = new FontTestChild();
               addChild(fontTest);
       }
    }
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 14, 2010 Nov 14, 2010

Im becoming crazy, but still can't see any text!

It's embedded problem for sure because if i comment this line of your code

label.embedFonts = true;

it's working like in my example!

Im using flashBuilder, shoud i do some settings?

Thanks so much for your job!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 14, 2010 Nov 14, 2010

Try to enumerate fonts and see if the fonts you embed are present:

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/text/Font.html#enumerateFonts()

Secons, I did have problems with font embedding when using FlashBuilder. I suggest you attempt to embed system fonts - not from the folder in FlashBUilder project. It worked for me in FlashBuilder

[Embed(source = 'C:\WINDOWS\Fonts\Verdana.ttf', fontName = "_verdana", mimeType = "application/x-font-truetype")]
private static var Verdana:Class;

OR

[Embed(systemFont = "Verdana", fontName = "_verdana", mimeType = "application/x-font-truetype")]
private static var Verdana:Class;

I suspect that there are some settings that influence how FlashBuilder handles some assets. Perhaps, on the settings prevents it for copy right reasons.

Other than FlashBuilder environments do not have these problems/restrictions except for the cases when font is corrupted.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 14, 2010 Nov 14, 2010

in your code i added this method

public function showEmbeddedFonts ():void {


            trace("========Embedded Fonts========");
            var fonts:Array = Font.enumerateFonts();
            fonts.sortOn("fontName", Array.CASEINSENSITIVE);
            for (var i:int = 0; i < fonts.length; i++) {
                trace(fonts.fontName + ", " + fonts.fontStyle);
            }
        }

trace correctly:

========Embedded Fonts========
Arial, regular
Verdana, regular

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 14, 2010 Nov 14, 2010

"

trace correctly:

========Embedded Fonts========
Arial, regular
Verdana, regular"

So, fonts are embedded and you need to figure out how you use them in your TextFields.

By the way, what font and backgroung colors do you use? They are not the same, are they?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 14, 2010 Nov 14, 2010

im using your classes!

like u did!

but still nothing!

It's really strange!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 15, 2010 Nov 15, 2010

look, this one have same problem!

http://forums.adobe.com/message/3276591#3276591

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Nov 15, 2010 Nov 15, 2010

just add this in the embed tag and it works fine!

All the script was correct!

embedAsCFF="false"

[

Embed(source="c:/windows/fonts/verdana.ttf", fontFamily="Verdana", embedAsCFF="false")]

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines