Jump to content
Search Community

Loading Fonts

ambatya test
Moderator Tag

Recommended Posts

I'm using Flash CS5 "new" font embedding to create a font swf and than I'm trying to get access to the shared font in the loaded swf.

Using Adobe Loader class I just access the:

evt.currentTarget.applicationDomain.getDefinition("FontName")

and all is fine.

 

But I can't do it with the SWFLoader.

When I'm using

evt.currentTarget.applicationDomain.getDefinition("FontName")

I get Property applicationDomain not found on com.greensock.loading.LoaderMax

 

And when I'm using :

LoaderMax.getContent("FontName")

I get Parameter must be non-null.

 

so my wrong code is:

...
queue.append( new SWFLoader("fontFile.swf", {estimatedBytes:15000, container:this}) );
queue.load();
...
private function onFontLoaded (evt:LoaderEvent):void {
   var FontLib:Class = LoaderMax.getContent("FontName") as Class;
   ...
}

* fontFIle.swf contains a font with a linkage name of "FontName".

 

What am I doing wrong ?

Link to comment
Share on other sites

That's exactly what the getClass() method of SWFLoader is for.

 

var loader:SWFLoader = new SWFLoader("myFonts.swf", {onComplete:completeHandler});
loader.load();
function completeHandler(event:LoaderEvent):void {
   var fontClass:Class = loader.getClass("FontClassName");
}

Link to comment
Share on other sites

I use this bit of code after the fonts.swf loads ... just to trace all available fonts. Might be useful.

 

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

Link to comment
Share on other sites

  • 2 months later...

I'm stuck here. Using FB 4 with Flash CS5.

 

I've tried several different solutions of dynamically loading fonts. It seems like you know how to do it. This is how I've done in CS5:

post-6304-133152001175_thumb.png

 

I then load this SWF with the greensock loader class, and when load is complete, I use the examplecode you submitted:

var fontClass:Class = loadFont.getClass("HelveticaBoldCondensed");
Font.registerFont(fontClass);

 

And this is what I get:

argumentError: Error #1508: The value specified for argument font is invalid. at flash.text::Font$/registerFont()

 

I can't figure out how to do this right. So disturbing! Please help!

Link to comment
Share on other sites

Thanks for your quick reply!

If I don't export it, my .swf get 0kb.. It's an empty .fla only for holding fonts..

 

I've also tried adding a textfield onto stage, and using the Font I've created as font family, still no go.

I now I get this error, when using:

var fontClass:Class = loadFont.getClass("TestFont");
Font.registerFont(fontClass);

Gives me:

ReferenceError: Error #1065: Variable 'TestFont' is not defined.

post-6304-133152001178_thumb.png

Link to comment
Share on other sites

Oh, sorry - I forgot to mention that I always have a (dynamic) TextField on the stage for each font and I click the "embed" button in the properties palette next to the font info. That's where I select those options I mentioned. Having it on the stage ensures that it gets compiled in the swf.

 

I'm not saying this is the only (or ideal) way of embedding fonts - it's what I have done. I remember running into bugs/problems with other ways that I tried in the past although I haven't tried other ways with CS5.

Link to comment
Share on other sites

Two problems: your TextField was the wrong type - you were using a TLF one instead of a dynamic "classic" TextField. Also, you do need to export the font for ActionScript so that it has a class name that you use to retrieve it from the getClass() method of SWFLoader. I've attached a super-simple working example (I didn't have the font you used, so I just did Arial).

Link to comment
Share on other sites

Oh, sorry - I forgot to mention that I always have a (dynamic) TextField on the stage for each font and I click the "embed" button in the properties palette next to the font info. That's where I select those options I mentioned. Having it on the stage ensures that it gets compiled in the swf.

 

I'm not saying this is the only (or ideal) way of embedding fonts - it's what I have done. I remember running into bugs/problems with other ways that I tried in the past although I haven't tried other ways with CS5.

 

 

Instead of doing it this way, you can simply write this for each font you use > "Font.registerFont(Georgia);"

 

I attached any example of my fonts.swf file that I use regularly. Then I just load fonts.swf into any other swf that needs it.

Link to comment
Share on other sites

Ohh..

Thank you!

 

Now it works locally atleast, but not If I point it at an http://... request. Crossdomain.xml is there.. Have you encountered that before?

 

I've dealt with this before. I built an embeddable video player that needed to load fonts.swf from another domain. This code was written before I started using GreenSock loading. But it should point you in the right direction.

 

Pay attention to the following:

 

import flash.system.LoaderContext;

import flash.system.SecurityDomain;

import flash.system.ApplicationDomain;

 

var context:LoaderContext = new LoaderContext();

context.checkPolicyFile = true;

context.securityDomain = SecurityDomain.currentDomain;

 

package com.blueion {
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Bitmap;
import flash.text.Font;
import flash.net.URLRequest;
import flash.events.*
import flash.system.LoaderContext;
import flash.system.SecurityDomain;
import flash.system.ApplicationDomain;

public class LoadDisplayObject extends MovieClip 
{

	private var _loader              :Loader = new Loader();
	private var _path                :String;
	private var _filetype	         :String;

	//- CONSTRUCTOR -------------------------------------------------------------------------------------------

	public function LoadDisplayObject(path:String,type:String):void 
	{
		_path = path;
		_filetype = type;
		addChild(_loader);
		addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);	
		addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
	}

	//- PRIVATE METHODS ---------------------------------------------------------------------------------------

	private function init():void
	{
		removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteListener, false, 100);
		_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loadProgressListener, false, 100);
		_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loadIOErrorListener, false, 100);

		var _localmode = new RegExp("file://").test(loaderInfo.url);

		if (_localmode){
			this._loader.load(new URLRequest(_path));
		} else {
			var context:LoaderContext = new LoaderContext();
			context.checkPolicyFile = true;
			context.securityDomain = SecurityDomain.currentDomain;
			_loader.load(new URLRequest(_path),context);
		}

	}


	private function loadProgressListener(e:ProgressEvent):void 
	{
		this.dispatchEvent(e);
	}

	private function loadCompleteListener(e:Event):void 
	{
		e.target.removeEventListener(Event.COMPLETE, loadCompleteListener);
		e.target.removeEventListener(ProgressEvent.PROGRESS,loadProgressListener);
		e.target.removeEventListener(IOErrorEvent.IO_ERROR,loadIOErrorListener);

		if (_filetype == "font"){
			showEmbeddedFonts();
		}
		if (_filetype == "image"){
			var bitmap = Bitmap(_loader.content);
			bitmap.smoothing = true;
		}
		if (_filetype == "swf"){

		}

		this.dispatchEvent(e);
	}
	private function showEmbeddedFonts():void 
	{
		var fonts:Array = Font.enumerateFonts();
		fonts.sortOn("fontName",Array.CASEINSENSITIVE);
		for (var i:int=0;i				trace("Embedded fonts = " + fonts[i].fontName + ", " + fonts[i].fontStyle);
		}

	}

	private function loadIOErrorListener(e:IOErrorEvent):void 
	{
		// Data did not load.
		trace("LoadDisplayObject: loadIOErrorListener > error");
		this.dispatchEvent(e);
		this.destroy();
	}

	private function onAddedToStage(e:Event):void 
	{
		this.init();
	}

	private function onRemovedFromStage(e:Event):void 
	{
		this.destroy();
	}

	private function destroy():void 
	{
		this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);	
		this.removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);

		try{
			removeChild(this._loader);
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,loadCompleteListener);
			_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,loadProgressListener);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,loadIOErrorListener);
			_loader.close();
			_loader = null;
		}catch(e:Error){
			// Do nothing.
			_loader = null;
		}

	}
	//- PUBLIC METHODS ----------------------------------------------------------------------------------------

	//- GETTERS & SETTERS ----------------------------------------------------------------------------------------

	public function get filetype():String
	{
		return _filetype;
	}
}
}

Link to comment
Share on other sites

Thanks for the info, ukla. I know you mentioned that the code was built before you started using LoaderMax, but for other folks out there I figured I'd point out that LoaderMax automatically handles setting the SecurityDomain to currentDomain for you (even adjusting it when running locally to avoid errors) and it works around some other common bugs/hassles in Flash related to loading and unloading swfs so I'd definitely recommend just using the SWFLoader that's part of LoaderMax to do this sort of thing. But again, thanks ukla for sharing the code because it can be helpful for folks to see some of the important inner workings and concepts.

Link to comment
Share on other sites

LoaderMax automatically handles setting the SecurityDomain to currentDomain for you (even adjusting it when running locally to avoid errors) and it works around some other common bugs/hassles in Flash related to loading and unloading swfs so I'd definitely recommend just using the SWFLoader that's part of LoaderMax to do this sort of thing. But again, thanks ukla for sharing the code because it can be helpful for folks to see some of the important inner workings and concepts.

 

That doesn't surprise me! You are always incredibly thorough!

 

Which reminds me ... my membership expired a couple of weeks ago. Time to renew.

Link to comment
Share on other sites

  • 8 months later...

I'm having trouble getting this to work. I'm using the attached font_loader file that loads the font.swf that has Arial embedded in it. I'm trying to use the embedded font with a textField and a textFormat object. but it's not working what am i doing wrong?

 

import com.greensock.loading.*;
import com.greensock.events.*;
import flash.text.TextField;
import flash.text.TextFormat;

var fontLoader:SWFLoader = new SWFLoader("font.swf", {onComplete:completeHandler});
fontLoader.load();

function completeHandler(event:LoaderEvent):void {
var fontClass:Class = fontLoader.getClass("TestFont");
Font.registerFont(fontClass);
trace("loaded successfully");

var tfm:TextFormat = new TextFormat();
tfm.font = "Arial";
var textfield:TextField = new TextField;
textfield.setTextFormat(tfm);
textfield.text = "this is arial";
addChild(textfield);
}

 

It's not working

Link to comment
Share on other sites

  • 5 months later...

I apologize for thread necromancy, but I'm just writing to express my conviction that TweenMax/LoaderMax are indisputably awesome.

 

I'd been banging my head around trying to figure out how to register a font loaded from an external SWF via a LoaderMax SWFLoader instance and kept coming up blank. Until I searched the forums and found one thread that directed me to this thread.

 

That's my long-winded way of saying "thank you very much!" I'm so grateful, I'll say it again: thank you very much!!! :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...