2 Replies Latest reply on Jun 23, 2010 12:04 PM by FotoMeister

    unloadStyleDeclarations not unloading Font

    FotoMeister

      We are developing an app that will allow the user to choose from any number (30+ and counting) fonts. Each font is being put in its own SWF using a CSS that is compiled to a SWF. Each CSS has no styling, just @font-face entries to include the various weights and styles. But after the user samples ~15 font, the player and browser crash.

       

      So I added some code to only keep the last 5 SWF by calling unloadStyleDeclarations on the others. But after calling that, the font is still available and usable (if I set the fontFamily, I can still see the "unloaded" font). If I call Font.enumerateFonts(false) the font is still listed there. And after ~15 different fonts are sampled, it still crashes.

       

      No where in the app do I call Font.registerFont.

       

      Thinking there was something in the application. I made the following, simple as you get, example, and it does not unload the fonts.

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                     xmlns:s="library://ns.adobe.com/flex/spark" 
                     xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="400" minHeight="600">
          
          <fx:Script>
              <![CDATA[
                  import mx.events.StyleEvent;
                  private function LoadFont(swfName:String):void {
                      var myEvent:IEventDispatcher = styleManager.loadStyleDeclarations(swfName, true, false, null, null);
                      //var myEvent:IEventDispatcher = styleManager.loadStyleDeclarations(swfName, true, false, ApplicationDomain.currentDomain, null);
                      myEvent.addEventListener(StyleEvent.COMPLETE, function(evt:StyleEvent):void { FontSWFLoadComplete(swfName, evt); } );
                      lblStatus.text = "Loading " + swfName;
                  }
                  private function UnLoadFont(swfName:String):void {
                      styleManager.unloadStyleDeclarations(swfName, true);
                  }
                  protected function FontSWFLoadComplete(swfFilename:String, evt:StyleEvent):void {
                      lblStatus.text = "Finished loading " + swfFilename;
                      ListLoadedFonts();
                  }
                  private function ListLoadedFonts():void {
                      loadedFonts.text = "";
                      for each (var curFont:* in Font.enumerateFonts(false)) {
                          loadedFonts.text += curFont.fontName + "\r\n";
                      }
                  }
                  
                  [Bindable]
                  private var fontName:String = "";
              ]]>
          </fx:Script>
          <s:VGroup>
              <s:HGroup>
                  <s:VGroup>
                      <s:Button click="LoadFont('BPdiet.swf');" label="Load BPDiet"/>
                      <s:Button click="UnLoadFont('BPdiet.swf');" label="UnLoad BPDiet"/>
                      <s:Button click="fontName = 'BPDiet';" label="Apply"/>
                  </s:VGroup>
                  <s:VGroup>
                      <s:Button click="LoadFont('UglyQua.swf');" label="Load UglyQua"/>
                      <s:Button click="UnLoadFont('UglyQua.swf');" label="UnLoad UglyQua"/>            
                      <s:Button click="fontName = 'UglyQua';" label="Apply"/>
                  </s:VGroup>
                  <s:VGroup>
                      <s:Button click="LoadFont('kids.swf');" label="Load kids"/>
                      <s:Button click="UnLoadFont('kids.swf');" label="UnLoad kids"/>            
                      <s:Button click="fontName = 'Kids';" label="Apply"/>
                  </s:VGroup>
                  <s:VGroup>
                      <s:Button click="LoadFont('NoFontsStyle.swf');" label="Load NoFonts"/>
                      <s:Button click="UnLoadFont('NoFontsStyle.swf');" label="UnLoad NoFonts"/>            
                      <s:Button click="fontName = '';" label="Apply"/>
                  </s:VGroup>                
              </s:HGroup>
                  <s:Label id="lblStatus" text="Status Area"/>
              <s:TextArea id="SampleTextArea" width="320" heightInLines="5" fontFamily="{fontName}" fontSize="25" >
                  Some sample text to think about
              </s:TextArea>
              <s:Button click="ListLoadedFonts();" label="Reload list"/>
              <s:TextArea id="loadedFonts" width="320" heightInLines="15"/>
          </s:VGroup>    
      </s:Application>
      
      

       

      Interestingly, I will sometimes see [Unload SWF] on the console, and that SWF's font is still able to be used.

       

      After that didnt work, I did add the following style to one of the CSS files, and when I call unload, that style is removed, but the font is still available as mentioned.

      s|Label {
          fontSize:    15;
          fontFamily: BPDiet;
          color: #FF9933;
      }

      (that made all of the text on the buttons change to the BPDiet font and be orange)

       

      I'm at a loss on this tacked.  I am now trying something based on info I gleaned from here: Flex and Embedded Fonts by making a SWF that has the font embedded in an .as file and then creating a textfield (text area, heck rendering a bitmap) that uses that embedded font, but thats not working either. I'll bug you all about that if we dont find an answer to unloading the fonts that are in the CSS files.