2 Replies Latest reply: Aug 9, 2012 6:04 AM by Brian Vaughn RSS

    Reliably calculating character bounds for Right-To-Left languages using flash.text.engine.* classes?

    Brian Vaughn Community Member

      I just filed a bug in the Adobe bugbase about this, but I thought maybe the community could help with a workaround (or point me in another direction if I'm looking at it wrong).

       

      I don't know an elegant way to describe the defect, but it's something like this:

       

      1. Create a Vector of ContentElements and add a TextElement containing a right-to-left string in a language such as Hindi (ex. "वह बाग़ में दोपहर का खाना ")
      2. Create a TextBlock with a GroupElement and use it to create a TextLine to display the right-to-left string
      3. Walk through each character in the right-to-left string and try to calculate its boundaries using the TextLine's "getAtomBounds" method.

       

      What I'm seeing in my test project is that the number of characters (atoms?) the TextLine reports is fewer than the number of characters that were in the original string. I was under the impression that TextLine would offer some method of automatically converting from a logical character index into a display index (to keep things simpler for the end user). That doesn't seem to be the case, and I don't see any way to use TextLine's methods to do that conversion.

       

      Am I overlooking something obvious?

        • 1. Re: Reliably calculating character bounds for Right-To-Left languages using flash.text.engine.* classes?
          Brian Vaughn Community Member

          Note to myself (and others) - this post seems very promising:

          http://forums.adobe.com/message/3155597

           

          More to follow soon, hopefully!

          • 2. Re: Reliably calculating character bounds for Right-To-Left languages using flash.text.engine.* classes?
            Brian Vaughn Community Member

            Update: I was missing something obvious!

             

            It seems like TextLine's getAtomTextBlockBeginIndex and getAtomTextBlockEndIndex methods were exactly what I was looking for. It just took me a while to find them and realize that. So to convert from a logical (unicode) character index to a display (atom) index, you can do this...

             

            private function getCharBounds( charIndex:int ):Rectangle {
                 for ( var childIndex:int = 0; childIndex < textContainer.numChildren; childIndex++ ) {
                      var textLine:TextLine = textContainer.getChildAt( childIndex ) as TextLine;
                      
                      for ( var atomIndex:int = 0; atomIndex < textLine.atomCount; atomIndex++ ) {
                           var charIndexRangeStart:int = textLine.getAtomTextBlockBeginIndex( atomIndex );
                           var charIndexRangeStop:int = textLine.getAtomTextBlockEndIndex( atomIndex );
                           
                           if ( charIndex >= charIndexRangeStart && charIndex <= charIndexRangeStop ) {
                                var rectangle:Rectangle =
                                     textLine.getAtomBounds(
                                          atomIndex );
                                
                                var point:Point =
                                     textContainer.globalToLocal(
                                          textLine.localToGlobal(
                                               new Point( rectangle.x, rectangle.y ) ) );
                                
                                rectangle.x = point.x;
                                rectangle.y = point.y;
                                
                                return rectangle;
                           }
                      }
                 }
                 
                 return null;
            }