| Register
Sunday, May 11, 2008   

Using Shared Fonts

Created By  Benjamin Mace, at  11/21/2005 - 28 comments.

Click to view this author's website.

In keeping with my optimization theme over the last few articles I thought I would focus on fonts. Fonts are the hidden killers to otherwise small file sized projects and can add on extra weight fast. By using Flash's Shared Font Symbols we can share a font outline across multiple SWFs reducing file size. Once a shared resource is cached on user's machine, they no longer need to download that asset. This means that something that is used over and over again can just be pulled from the cache and not the server.

Designers love Flash because SWF files can include fonts outlines allowing the designer to use any font without the fear of the end user not having that font installed on their machine as with HTML. Flash is great for this reason but, it's not great in the sense that every font you use and every weight you use it in (bold/italic/regular) can add anywhere to 20kb to 40kb a pop.

If you use 2 fonts and 2 weights you can easily tack on 100kb to any project. Most of the time, it's something we have to live with, yet there are times where we can reduce the repeated size increase. Repeated font use can occur across large sites or in something like training material over the web. For example, say an e-learning lesson on conduct in the work place has 10 lessons. Each lesson uses CorporateCaps Regular as their callout font for bullets. Let's say this fictional font with all its characters weighs 35kb. Over 10 lessons we've added on 350kb of file size. Using shared fonts within Flash we can reduce that 350kb of loading down to 35kb again.

Shared fonts are easy to create and easy to deploy but they aren't used often because there isn't a lot of documentation on them and there is a misconception that it's more trouble than it's worth. Like other runtime sharing symbols in Flash, all you need to do is create a symbol and export it to a SWF that can be linked to by other SWF files. I have included 2 files for you to take a look at and see how this is setup. You can download them here.

To start out you need to open a new file and then open the library panel (F11). From the drop down button in the library panel's top bar, select "New Font.". Here you will enter a name and select the font you want to use across your files. In the example provided I have named my font "_ArialRegular". There are 2 things to note here.

Over the years I have had trouble with the font names when they include special characters, spaces or numbers. There is no rhyme or reason to it. It seems to be totally random. The "_" simply makes my font available in the properties box at the top of the list. I'm lazy and I know where all my fonts will be. Plus, you don't want to name it something like Arial and not know which Arial in the font list is your shared font or the machine font. Name the new font something different to distiguish it.

Last, I tack on Regular, Bold or Italic to the name so I know the style. There are several options for the font you wish to export below that. Choose the weight you want and if it should be italic or aliased etc. The size field is used in conjunction with the alias feature because pixel fonts are mormally created to be displayed a specific height.

After setting the specifics up, right click on the new symbol in the library and select "Linkage". Here you will need to check off "Export for ActionScript" if you intend on using it with AS (in a CSS or TextFormat Object for example), "Export for runtime sharing" which is what we are describing here, and "Export in the first from" since we won't be using it on the timeline. Last, enter the name of the SWF the symbol will reside in. After that we can export our SWF file with the same name we entered in the URL field.

Now in a new file, all we have to do is drag the font symbol from our original file's library to the new one's library. Right click on it in the new library and view the linkage. You'll notice that the options have changed and only "Import for runtime sharing" is checked.

It's checked because this file will load in the font symbol from the other library at runtime. Here we assume that both the source library SWF and the SWF using the fonts from that library are in the same directory. If not, you would need to specify the location of the SWF in the URL field. Our libraries are all set now to use the font on the stage.

In our new file create a textfield. In your properties box, choose the new font ie: "_ArialRegular" in the drop down for that field. You'll notice that there is an asterisk by the name noting the font symbol. Now export your file. It's 0kb because we are pulling the font information form the other file. That's it! You can do this with other symbols as well. Something like a company logo at 10kb can add up over multiple files. As with the shared font, sharing the logo can shed extra weight as well. Font symbols and shared assets are best when use in large, multi-file projects. For smaller projects with single files shared assets offer no real savings in file size so use this technique accordingly.

Need Professional Help For Your ActionScript Project?
ActionScript.com Consulting Services provide top quality professional ActionScript consulting to businesses around the globe. If you have a professional project in need to world-class talent, tell us about your project by requesting a quote today.

Reader Comments

  1. Anthony Graddy  Replied:
    ( 11/22/2005 At 11:06 AM)

    Thanks for the tips. That is some good, pratical information about sharing assets (that is not commonly known).

  2. Ivan Andonov  Replied:
    ( 11/22/2005 At 1:26 PM)

    Hi Benjamin,
    This way to use fonts is good for not very complex projects... for others i prefer dembicki way and getNewTextFormat method to get needed font.

  3. ubaldo  Replied:
    ( 11/22/2005 At 2:52 PM)

    hi
    shared fonts doesnt work with "createTextField" and a CSS "styleSheet"
    apparently it works only when you create a textfield manually.
    Ubaldo

  4. Max Ziebell  Replied:
    ( 11/22/2005 At 3:47 PM)

    Is there a way to Link these shared font with actionscript on Runtime. So I could load them on demand from a selection of multiply fonts? I read something once but I forgot about it again... any clou?

  5. MOLOKO  Replied:
    ( 11/22/2005 At 6:10 PM)

    well the only problem with shared fonts and library fonts is that you can only apply one shared/library font to a textfield - and you can only embed one 'weight' of the font into the symbol. So this technique is no good if you wanted to be able to use a mixture of bold, italic and regular in the same textfield - something I find is required by my designers 99% of the time.
    Wish Macromedia would change this so that you can embed all the different weights into the same font symbol, that would solve the problem.

  6. Ivan Andonov  Replied:
    ( 11/22/2005 At 7:16 PM)

    replie to Max Ziebell:
    http://sharedfonts.com/
    replie to MOLOKO:
    you can use two or more fonts linked in the library and apply them to a textfield with setNewTextFormat or you can create two or more textfields at one level with different 'weight'(bold, italic etc..) and you can set html text.

  7. nfrank  Replied:
    ( 11/22/2005 At 7:39 PM)

    I've found that it is possible to 1. use multiple typefaces in the same textfield* and 2. that having a font object alone wasn't enough in some cases.*
    If anyone finds a way to dynamically switch what font objects get loaded in or are available at runtime, let me know; I'm totally interested in that.
    *note 1: using multiple typefaces in the same textfield before flash8 was odd and produced differing results. One common result was to find blurry text starting midline especially when using multiple typefaces of differing heights at different sizes. The new flash8 text renderer is pretty cool and I haven't tried working it out in flash8. I've only done it using css, but I'm sure you could use textformats to work it out.
    *note 2: I've found that When making my shared font files I've also created a movieclip that has a sole textfield in it. The textfield gets the font object font applied to it and the embed characters is turned on for the characters that I need. You have to then set up the export/import for both the clip and the font object and import into your file. On the first frame of the importing movie drop a copy of the imported mc on the timeline (I've found some odd things when using this inside a clip with a class definition, so put it on the root timeline)
    *you can also do switching of embedded and non embedded fonts at runtime this has proved beneficial for some multilingual projects.

  8. BJ  Replied:
    ( 11/22/2005 At 7:49 PM)

    Here is a link to an interesting article to give you some more information about shared libraries.
    http://www.quasimondo.com/archives/000227.php

  9. bunnyhero  Replied:
    ( 11/23/2005 At 6:20 AM)

    i haven't tried this myself, yet, but in this article (archive.org copy linked to below, the original is now missing), it says that there are potential problems with this on non-PC flash players:
    "Dynamic and input textfields that utilize shared fonts, when viewed any where other than IE/PC, will show any text that they originally had in them, but if you set their text property while the font was being loaded (i.e. in the first few frames) that text will not show up. However, anything done to the textfields after that point will show up properly. This is obviously a latency issue, and as such a preloader will solve the problem."
    like i said, i haven't played with any of this myself yet, so i don't know if this is still true with more recent versions of the flash player...
    http://web.archive.org/web/20030205231143/http://www.waxpraxis.org/archives/000062.html

  10. BJ  Replied:
    ( 11/23/2005 At 8:53 AM)

    I should probably ammend this article later but the #1 thing you need to do when using this technique is to 110% sure that the shared library is fully loaded into the requesting swf before making any attempts to use the resources from it. In all honesty, you should make sure that all of your resources are fully loaded with respect to anything you do in any program. You can't use something if it's not there or not ready.

  11. Metah  Replied:
    ( 11/23/2005 At 12:43 PM)

    Great tips, i never used shared fonts be4, now i'm going to use it.
    cheers,
    Ahmet

  12. fatu  Replied:
    ( 11/29/2005 At 9:01 PM)

    hi!
    i haven't got good english, but i hope i will be understandable.
    i got some problems with shared library. i made a font.swf, i have shared it's library, so the main movie uses this library for read embed fonts. becouse i use dinamical created text box, i have made empty text boxes to every movie to made de embeding. it works ok, but only under firefox!
    my question is: how does work shared library if i use dinamical create text boxes?
    thanks
    fatu

  13. iv  Replied:
    ( 12/2/2005 At 6:49 AM)

    [q]From the drop down button in the library panel's top bar, select "New Font." ....[/q]
    - it is bad way. really. in this case management of symbols collections is impossible.
    I have an article about my practice of fonts embedding but now in russian only:
    http://www.sharedfonts.com/ru/faq.html#include
    Please help if anybody can translate it to english. My english is very bad.

  14. tim  Replied:
    ( 12/6/2005 At 12:33 AM)

    i've tried ya method, iv, but i still can't correctly work with the fonts. i have main clip and child clips, which i load into the parent via loadMovie(). On the main timeline of parent clip i have textfield with embeded font outlines. The getNewTxtFormat() from child clips is working, but i can't see the text in my textfields though i'm tracing .font, .bold, etc in child movies.
    So i'm using shared font assets now as macromedia recommends (though i've cutted off all the garbage (i mean outlines, that i don't need) from my ttf using apropriate ttf-editor). this way it's working.

  15. Axl Laruse  Replied:
    ( 12/13/2005 At 3:41 AM)

    Could someone post a solution for this shared fonts issue that do not involve going to the website "http://sharedfonts.com/"?
    I went there and the solution showed there didn't help me. Also there are thinks hide in those files.
    Thank,
    Axl

  16. Damian  Replied:
    ( 12/13/2005 At 6:53 AM)

    Thks!, This is very useful information for use in my Flash projects!

  17. Jimmy Pauwaert  Replied:
    ( 12/14/2005 At 2:28 PM)

    Thanks, even my teacher Flash didn't know this was possible.

  18. Yasuo  Replied:
    ( 12/16/2005 At 7:18 AM)

    I guess that we can create shared pics as well, because we have been doing that. it just puts all the pics on library and publish for shared pics. so that we can pull out all the pics from shared pics swf form main swf. Is that pretty normal?

  19. BJ  Replied:
    ( 12/16/2005 At 11:27 PM)

    Yeah you can also share images, clips etc. A company logo, watermarks, anything reused could be done in the same manner.

  20. iv  Replied:
    ( 12/19/2005 At 8:29 PM)

    http://www.sharedfonts.com/eng/faq.html#include
    - article is now in english. Thanx 2 Nox Noctis.

  21. Zareh Boghozian  Replied:
    ( 12/22/2005 At 7:17 PM)

    This is a good way i have used this way but this is not the way i am looking for maybe you could help me
    is it possible to load a embedded font into swf but not everytime for example if a user want to use this kind then flash would start to load it (and could i have a progress bar for this) if i use a dynamic textfield and place it into an external swf file when i load it by loadmovie the embeddef font doesnt import into my swf .
    Please mail me if you can solve this problem
    Zareh Boghozian

  22. iv  Replied:
    ( 12/23/2005 At 3:41 AM)


    [...]is it possible to load a embedded font into swf [...]

    - of course. all http://www.sharedfonts.com about it.

  23. Okan Ozkan  Replied:
    ( 12/27/2005 At 2:22 AM)

    thanks...

  24. Bryan  Replied:
    ( 12/28/2005 At 2:08 AM)

    Has anyone been able to make "shared fonts" work in Flash 8? I've had absolutely not luck. I am trying to do something similar to Zareh Boghozian.

  25. Pascal  Replied:
    ( 12/30/2005 At 11:03 AM)

    Bryan:
    It works for me, but bold styled fonts won't show up, even when embedding all of the glyphs.
    Just create a shared library swf/symbol, then drag it to your content file and set the font of the field to the "*" font. It should work.. but make sure both files are exporting to the same Flash version.

  26. Ivan Dembicki  Replied:
    ( 1/10/2006 At 10:47 AM)

    bold, italic, bold-italic it's different font for flash and his own library required.

  27. Josh Cairney  Replied:
    ( 1/20/2006 At 7:21 PM)

    Rather than calling a "_ArialRegular" (or whatever it may be) by choosing that font in the properties box, is it somehow possible to use ActionScript and say:

    myTextFormat.font = "_ArialRegular";

    Or something to that extent?
    I can only get this to work if I have the font symbol in my current file's library, not from a shared library. Any help would be much appreciated!

  28. Bjarne Grönnevik  Replied:
    ( 3/13/2007 At 11:52 AM)

    This way of sharing Fonts is awsome in all ways except one: I can not for the life of me find a way of including more than the standard 256 ( or close to ) character glyhps into the shared font. I need a way to include character sets such as Greek, Hebrew, Thai and simplified Chineese... At first I thought it would just be solved by including a text field with the needed characters embedded into the resource *.swf but having a Font object in the library seems to actively prevent including any other glyhps in the *.swf file...

    Any ideas how to solve this?

Login to post your comments. If you do not have an account with us please Register.
Copyright 2005 by ActionScript, Inc.   |  Privacy Statement  |  Terms Of Use  |  ActionScript Client Extranet