2 Replies Latest reply: Sep 24, 2010 5:33 AM by midget35 RSS

    Get global x y of characters in TextLines

    midget35

      Hi,

       

      I'm hoping you can help me. I am trying to get the global coordinates of very character in a RichEditableText (which uses TLF).

       

      I'm working with TLF and going through each textline in a flow composer trying to get the exact start x& y of every character based on atom position. The code below is what I have so far. Suffice to say the multiple columns are being ignored (each line appears one after the other rather than to the right of the previous colum after reaching the bottom).

       

      Also - the character placement starts out OK but the atom coordinates seem to go out of sync with the characters after a few words - too close or far from its character neighbours - and new lines seem to be ignored.

       

      I'd greatly appreciate any help with this.

       

       

      var

       

       

       

      text:String = richEditableText.getTextFlow().getText();

      for

       

      (var i:int =0; i < richEditableText.getTextFlow().flowComposer.numLines; i++){

       

       

           var flowLine:TextFlowLine = te.getTextFlow().flowComposer.findLineAtPosition(i);

       

       

           var textLine:TextLine = flowLine.getTextLine(true);

          

           var

       

      currentChar:int = 0;

       

       

       

           var rawCount:int = textLine.rawTextLength;

       

       

           for (var j:int =0; j < textLine.rawTextLength; j++){

       

         

       

       

       

                var x:int = textLine.getAtomBounds(j).x + richEditableText.x;

       

                var y:int = textLine.getAtomBounds(j).y + (textLine.height *i)+ richEditableText.y;

       

       

                trace(text.charAt(currentChar)+", "+x+", "+y+", "+textLine.getAtomIndexAtCharIndex(j));

       

                currentChar++;

       

       

                }

      // end j

       

           }

       

       

      }

      // end i

       

           }

       

       

      }

      // end i

       

           }

       

       

      }

      // end i

       

       

        • 1. Re: Get global x y of characters in TextLines
          robin.briggs Adobe Employee

          The rawTextLength is not the same as the atomCount -- you could have more than one Unicode character in an atom. So to iterate over the atoms you should use the line's atomCount. If you want to map the atoms back to characters in the line, you need to use the getAtomTextBlockBeginIndex and getAtomTextBlockEndIndex functions.

           

          The atom bounds are expressed in units that relative to the TextLine. If you want them in container or stage coordinates, you will have to either translate them yourself, or call localToGlobal to do it for you. In your code, it looks like you are not taking the origin of the line into account when you do the transform.

           

          Below is a small code snippet for iterating through the atoms and drawing their bounds:

           

          public class ShowAtomBounds extends Sprite

          {

          public function ShowAtomBounds()

          {

          super();

          var textSprite:Sprite = buildFTEExample("IJ Now is the final time for fish to flagellate ");

          addChild(textSprite);

          }

           

          /** build FTE data each time */

          public function buildFTEExample(sampleText:String):Sprite

          {

          var r:Rectangle;

          var elementFormat:ElementFormat = new ElementFormat();

          elementFormat.fontDescription = new FontDescription("Minion Pro");

          // elementFormat.fontDescription = new FontDescription("Arial Black");

          elementFormat.fontSize = 48;

          var textElement:TextElement = new TextElement(sampleText, elementFormat)

          var textBlock:TextBlock = new TextBlock(textElement);

          var textLine:TextLine = textBlock.createTextLine();

          var sprite:Sprite = new Sprite();

          sprite.addChild(textLine);

          sprite.x = 100;

          sprite.y = 100;

          sprite.graphics.lineStyle(1, 0xFF0000);

          trace("line contains", textLine.atomCount, "atoms");

          for (var i:int = 0; i < textLine.atomCount; i++)

          {

          r = textLine.getAtomBounds(i);

          sprite.graphics.moveTo(r.left, r.top);

          sprite.graphics.lineTo(r.right, r.top);

          sprite.graphics.lineTo(r.right, r.bottom);

          sprite.graphics.lineTo(r.left, r.bottom);

          sprite.graphics.lineTo(r.left, r.top);

          trace("\t atom", i, "bounds is", r.toString());

          }

          return sprite;

          }

           

          }

          }

           

          If there was more than one line, it would have to do the coordinate transform from TextLine to container Sprite coordinates.

           

          Here's a snippet that converts from TextLine to container coords:

           

          pt = textLine.localToGlobal(pt);

          if (textLine.parent)

          pt = textLine.parent.globalToLocal(pt);

           

          If the TextLine hasn't yet been added to the display list, you can just take the local coordinates and translate:

               localX += textLine.x

               localY += textLine.y

           

          Hope this helps,

           

          - robin

          • 2. Re: Get global x y of characters in TextLines
            midget35 Community Member

            Hi Robin,

             

            Thank you so much for taking the time out to respond - and deciphering my poorly marked-up code!

             

            This is exactly what I was looking for. The thing is I was going through rawtext rather than atomCount because I believed that more than one character could fit into an atom. I guess I was getting ahead of myself.

             

            So with the exact atom placement I could then go through the text string and re-insert the characters - exactly one per atom. This seems to work perfectly even with complicated flows, regardless of font. But I wonder - is there ever a scenario where one character could take up 2 atoms, or 2 charactors fit in the same atom? Is my solution fallible in this sense?

             

            Once again Robin - I can't thank you enough for this. Really helped me out