开发者

How to use fonts in external SWF files

What I would like to do is a flex app, that uses fonts that are available in external swf. What I have succeded so far is:

  • to create a AS class hat holds the embedded font:

    package { import flash.display.Sprite;

    public class _Arial extends Sprite
    {
        [Embed(source='C:/WINDOWS/Fonts/ARIAL.TTF', fontName='_Arial', 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')]
        public static var _MyArial:Class;
    }
    

    }

  • compiled this into a swf with following command: mxmlc.exe -static-link-runtime-shared-libraries=true _Arial.as

When I try to load the font from my flex app, this fails with following error message:

ArgumentError: Error #1508: The value specified for argument font is invalid.
    at flash.text::Font$/registerFont()
    at valueObjects::FontLoader/fontLoaded()[C:\Documents and Settings\nutrina\Adobe Flash Builder 4\flex_pdf\src\valueObjects\FontLoader.as:33]

This is the code where I try to load the SWF file:

   package 
   {

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

    import mx.collections.ArrayCollection;
    import mx.core.FontAsset;

    public class FontLoader extends Sprite {

        public function FontLoader(url:String) {
            super();
            loadFont(url);
        }

        private function loadFont(url:String):void {
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fontLoaded);
            loader.load(new URLRequest(url));
        }

        private function fontLoaded(event:Event):void {
            var fontList:ArrayCollection = new ArrayCollection(Font.enumerateFonts(true));
            var FontLibrary:Class = event.target.applicationDomain.getDefinition("_Arial") as Class;
            trace("FontList: " + fontList)
            trace("FontLibrary: " + FontLibrary)
            trace("FontLibrary._Arial: " + FontLibrary._MyArial)
            Font.registerFont(FontLibrary._MyArial);

            fontList = new Arra开发者_运维技巧yCollection(Font.enumerateFonts(true));
            trace("Font list: " + fontList)
        }
    }
   }

The font file is definitely not corrupt because if I put the _Arial class from the code above into the Flex application, the embedding works. So my guess is that probably I missed some compilation options?

I am using Adobe Flash Builder 4.

I would appreciate any help on this matter.

Thanks, Gerald


Could be a problem with class name conflicts. I wrestled with an external font loading issue for hours. Turns out my font class was called "Main", and so was the application that was trying to load it. The font never got registered correctly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜