6 Replies Latest reply on Mar 2, 2012 5:37 AM by Jayesh Sharma

    TextSnapshot problem

    Yevhen Runts

      I'm using textSnapshot property of DisplayObjectContainer to select static text in loaded in runtime SWF.

      The interesting issue is if call  _textSnapshot = getTextSnapshot(_swfLoder); twice static text in swf will be not selected.

      Please comment/uncoment row after "       //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"and test application.

       

      Test SWF file you can download at http://dl.dropbox.com/u/2147606/test.swf

       

      Are there any ideas why this happens? There are several SWFs and I can not read all textSnapshots at once so this problem is very critical for me.

       

      Sample code:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application
          xmlns:mx="http://www.adobe.com/2006/mxml"
          layout="absolute"
          minWidth="955" minHeight="600"
          creationComplete="init()">
         
          <mx:Script>
              <![CDATA[
                  import mx.controls.SWFLoader;
                 
                  private var  _textSnapshot: TextSnapshot;
                 
                  private var _swfLoder: SWFLoader;
                 
                  private function init(): void
                  {
                      var url: String = "test.swf";
                      _swfLoder = new SWFLoader();
                      _swfLoder.addEventListener(Event.COMPLETE , completeHandler);       
                      _swfLoder.load(url);
                  }
             
                 
                  private function completeHandler(event: Event): void
                  {
                      _swfLoder = event.target as SWFLoader;
                      _swfLoder.y = 0;
                      _swfLoder.x = 0;
                      addChild(_swfLoder);


                      _textSnapshot = getTextSnapshot(_swfLoder);
                     
                      //Comment to see highlighting

                      //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                      //_textSnapshot = getTextSnapshot(_swfLoder);
                     
                      var searchText: String = "a";
                      var startPosition: int = 0;
                      startPosition = _textSnapshot.findText(startPosition, searchText, false);
                     
                      _textSnapshot.setSelected(0, _textSnapshot.charCount, false);
                     
                      while (startPosition >= 0)
                      {
                          _textSnapshot.setSelected(startPosition, startPosition + searchText.length, true);
                         
                          startPosition += searchText.length;
                          startPosition = _textSnapshot.findText(startPosition, searchText, true);
                         
                      }
                  }
                 
                  private function getTextSnapshot(loader: SWFLoader): TextSnapshot
                  {
                      var textSnapshot: TextSnapshot;
                     
                      if (loader.content != null)
                      {
                          var movieClip: MovieClip = loader.content as MovieClip;
                         
                          if (movieClip != null)
                          {
                              textSnapshot = (movieClip as DisplayObjectContainer).textSnapshot;
                          } 
                      }
                     
                      return textSnapshot;
                  }
              ]]>
          </mx:Script>
         
      </mx:Application>

        • 1. Re: TextSnapshot problem
          Volodymyr Styranivskyy

          Hi,

           

          I fixed this issue by storing previous snapshots into array. I'm not sure why you should do this maybe it is connected to GC. But anyway following example works fine.

           

          <?xml version="1.0" encoding="utf-8"?>

          <mx:Application

          xmlns:mx="http://www.adobe.com/2006/mxml"

          layout="absolute"

          minWidth="955" minHeight="600"

          creationComplete="init()">

           

          <mx:Script>

          <![CDATA[

          import mx.controls.SWFLoader;

           

          private var  _textSnapshot: TextSnapshot;

           

          // SArray of snapshots

          private var  textSnapshots: Array = new Array();

           

          private var _swfLoder: SWFLoader;

           

          private function init(): void

          {

          var url: String = "http://dl.dropbox.com/u/2147606/test.swf";

          _swfLoder = new SWFLoader();

          _swfLoder.addEventListener(Event.COMPLETE , completeHandler);       

          _swfLoder.load(url);

          }

           

           

          private function completeHandler(event: Event): void

          {

          _swfLoder = event.target as SWFLoader;

          _swfLoder.y = 0;

          _swfLoder.x = 0;

          addChild(_swfLoder);

           

          _textSnapshot = getTextSnapshot(_swfLoder);

           

          // Store previous snapshot

          textSnapshots.push(_textSnapshot);

           

          //Comment to see highlighting

          //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

          _textSnapshot = getTextSnapshot(_swfLoder);

           

          var searchText: String = "a";

          var startPosition: int = 0;

          startPosition = _textSnapshot.findText(startPosition, searchText, false);

           

          _textSnapshot.setSelected(0, _textSnapshot.charCount, false);

           

          while (startPosition >= 0)

          {

          _textSnapshot.setSelected(startPosition, startPosition + searchText.length, true);

           

          startPosition += searchText.length;

          startPosition = _textSnapshot.findText(startPosition, searchText, true);

           

          }

          }

           

          private function getTextSnapshot(loader: SWFLoader): TextSnapshot

          {

          var textSnapshot: TextSnapshot;

           

          if (loader.content != null)

          {

          var movieClip: MovieClip = loader.content as MovieClip;

           

          if (movieClip != null)

          {

          textSnapshot = (movieClip as DisplayObjectContainer).textSnapshot;

          }

           

          return textSnapshot;

          }

          ]]>

          </mx:Script>

           

          </mx:Application>

          • 2. Re: TextSnapshot problem
            Yevhen Runts Level 1

            Thanks a lot) It helps me to solve problems i had.

            • 3. Re: TextSnapshot problem
              JSujubala-nZao4n

              Hi Yevhen Runts

              i have using your code but my swf file is locally loaded in my application, although i could not get textSnapshot, error is occured

              The Error is:Error #1009: Cannot access a property or method of a null object reference.

              the error occured in this line(startPosition = _textSnapshot.findText(startPosition, searchText, false)).

              Coding is

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application
              xmlns:mx="http://www.adobe.com/2006/mxml"
              layout="absolute" minWidth="955" minHeight="600"
              creationComplete="init()">
                  <mx:Script>   
                  <![CDATA[   
                      import mx.controls.SWFLoader;
                      private var  _textSnapshot: TextSnapshot;
                      // SArray of snapshots   
                      private var  textSnapshots: Array = new Array();
                      private var _swfLoder: SWFLoader;
                      private function init(): void   
                      {   
                          var url: String = "test.swf";   
                          _swfLoder = new SWFLoader();   
                          _swfLoder.addEventListener(Event.COMPLETE , completeHandler);
                          _swfLoder.load(url);   
                      }
                      private function completeHandler(event: Event): void   
                      {   
                          _swfLoder = event.target as SWFLoader;       
                          _swfLoder.y = 0;       
                          _swfLoder.x = 0;       
                          addChild(_swfLoder);               
                          _textSnapshot = getTextSnapshot(_swfLoder);
                          // Store previous snapshot       
                          textSnapshots.push(_textSnapshot);
                          //Comment to see highlighting
                          //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!       
                          _textSnapshot = getTextSnapshot(_swfLoder);
                          var searchText: String = "S";       
                          var startPosition: int = 0;       
                         startPosition = _textSnapshot.findText(startPosition, searchText, false);
                          _textSnapshot.setSelected(0, _textSnapshot.charCount, false);
                          while (startPosition >= 0)       
                          {       
                              _textSnapshot.setSelected(startPosition, startPosition + searchText.length, true);
                              startPosition += searchText.length;       
                              startPosition = _textSnapshot.findText(startPosition, searchText, true);       
                          }       
                      }               
                      private function getTextSnapshot(loader: SWFLoader): TextSnapshot       
                      {       
                          var textSnapshot: TextSnapshot;       
                          if (loader.content != null)       
                          {       
                              var movieClip: MovieClip = loader.content as MovieClip;       
                              if (movieClip != null)       
                              {
                                  textSnapshot = (movieClip as DisplayObjectContainer).textSnapshot;
                              }        
                          }           
                          return textSnapshot;       
                      }   
                  ]]>
                  </mx:Script>
              </mx:Application>

              Please help me

              • 4. Re: TextSnapshot problem
                JSujubala-nZao4n Level 1

                Hi Yevhen Runts

                 

                First of all i m very sorry, my mistake is i am using as2 swf files, but another problem is the setselected color is not working, so please help me

                • 5. Re: TextSnapshot problem
                  JSujubala-nZao4n Level 1

                  Hi Yevhen Runts

                   

                  Hi sorry i solve this problem its working fine, my mistake is the swf text is not embeded, anyway thanks lot

                  • 6. Re: TextSnapshot problem
                    Jayesh Sharma

                    Hi Volodymyr,

                     

                    Thanks a lot for the posting the answer it worked for me

                    My only concern is that you are not removing textsnapshots you are adding into the array. So after some time will it not cause memory leaking?