5 Replies Latest reply on Dec 12, 2009 10:49 PM by Flex harUI

    Embed font dynamically at compile time.

    Adam York Level 1

      Hey everyone. I have been using the embed feature of flex to embed fonts at compile time. This works great I have used both these syntax :

       

      [Embed(systemFont='Arial'
      [Embed(source='somefolder/somefont.tty'
      I realize this isnt the entire line. Im just putting in the pertinent parts.

       

      Either one works fine. My question is , what if I dont know what font i need where this line exists. For example let say i want to instantiate a new object of type SomeField. And as an arguement i want to pass in a string , an associative id of the system font, like "Arial" or "Tahoma". Then inside the SomeField class where the :

       

      [Embed(systemFont='Arial'

       

      line exists , i want to assign the variable as the system font used.

       

      so something like

       

      [Embed(systemFont=$SomeVar

       

      where $Some var is a string assigned by the caller of the child. The idea is to have the developer be able to import a class , create a new instance of that class , and pass in a string , being the name of the font theyd like to dynamically embed. All this could be done without ever having to go into the acutal class defintion for the object and manually chaning the 'systemFont' or 'source' linakge.

       

      Thanks !

        • 1. Re: Embed font dynamically at compile time.
          paul.williams Level 4

          If you want to give developers control of a component's font at compile time then just expose a style that allows them to set the font. This is how all the Flex controls do it. The standard name for this style is "fontFamily".

           

          You can embed fonts in your application using stylesheets, and you can also compile stylesheets as independent swf files. I imagine this is what you would have to do if you wanted to load an embedded font dynamically at runtime.

           

          http://livedocs.adobe.com/flex/3/html/help.html?content=fonts_04.html

           

          http://livedocs.adobe.com/flex/3/html/help.html?content=styles_10.html

          1 person found this helpful
          • 2. Re: Embed font dynamically at compile time.
            Adam York Level 1

            Thanks for reply paul. Let me clarify a point a might have failed to convery in my initial post. My flex project is essentially just a framework , an mx:canvas , and some basic flex components. The flex app actually is loading swf's that are compiled from various actionscript projects I have set up in flex builder. I think my main area of confusion is this :

             

            Lets say i have some class, for use in an actionscript project whos public constructor is this :

             

            public function SomeClass() {

             

                  [Embed(systemFont="Arial" , fontName='_UserFont', mimeType='application/x-font')]

                  var fntAppMain : Class;

                  Font.registerFont( fntAppMain );

             

            };

             

            Right so this works , and works fine , but I dont want to them to have to use Arial of course , but i also dont want them to have to physically go into this class and change it. so what i want to do is this :

             

            public function SomeClass( theFontName : String ) {

                  [Embed(systemFont=theFontName , fontName='_UserFont', mimeType='application/x-font')]

                  var fntAppMain : Class;

                  Font.registerFont( fntAppMain );

            };

             

            This way the developer will never need to see whats going on the class , he or she can just , instantiate the object and pass the font name in as argument in the contructor.

             

            like

             

            var sc : SomeClass = new SomeClass( "Tahoma" );

            addChild( sc );

             

            when i try to make either the systemFont , or source , attribute or the Embed tag dynamic , and not a hardcoded string , i get errors like :

             

            exception during transcoding: Font for alias '_UserFont' with plain weight and style was not found by family name 'theFontName'

             

            Is there a way to setup some process to check for a token like

             

            public function SomeClass( theToken: *) {

                  [Embed(systemFont=$theToken$, fontName='_UserFont', mimeType='application/x-font')]

                  var fntAppMain : Class;

                  Font.registerFont( fntAppMain );

            };

             

            So when the application is compiled that value will be correct. Thanks !

            • 3. Re: Embed font dynamically at compile time.
              Flex harUI Adobe Employee

              Embedding is a compile time thing, not a runtime thing.  It is better to embed once, maybe in a font module, and simply tell the other controls which fontName to use.

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

              Blog: http://blogs.adobe.com/aharui

              • 4. Re: Embed font dynamically at compile time.
                Adam York Level 1

                Thanks for the response Alex. I agree with you completely. The intentions are not to be able to generate new textfields with various fonts at run time. Here is the dilemma , this tool is for use in a UI Toolset lib that i am building. I am planning on creating a swc from the whole library once finished. I really need to be able to give the user the ability to specify which font he or she would like to embed , but i wanted to try to keep everything as encpasulated as possible in the one class. Now i know i could very simply have the user create a font symbol in the library , export for actionscript , and use that. But i wanted a more elegant way of handling it, without creating all these dependencies. It seems really odd to me that the compiler wouldnt be able to do this , seeing as if the compiling of flash to bytecode should be happening in a linear way , the variable would exists at the time the constructor is called to create the class. Would it be possible to do this :

                 

                [Embed(source='/assets/fonts/font.locationfile', fontName="Font", mimeType="application/x-font-truetype")]

                 

                where font.locationfile , is a binary file with a path inside of it ? Maybe just one line like , C:\WINDOWS\Fonts\FONT.ttf. That way , i can just make a developers note that , the assets/fonts folder must exist in the workspace , and that the font.locationfile must exist , otherwise i would just embed a default font for them. If the want to use a different font , the would just open up the locationfile in text edit , change the font and save. Done.

                • 5. Re: Embed font dynamically at compile time.
                  Flex harUI Adobe Employee

                  That sounds like pre-processor/directive stuff, which there is no such thing in Flex.  You can try to find some other tool to auto-generate actionscript and metadata.  ANT scripts, Unix shell scripts, AIR apps can possibly be used to auto-generate what you want.

                   

                  Alex Harui

                  Flex SDK Developer

                  Adobe Systems Inc.

                  Blog: http://blogs.adobe.com/aharui