-
1. Re: Get global x y of characters in TextLines
robin.briggs Sep 22, 2010 8:41 PM (in response to midget35)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 Sep 24, 2010 5:33 AM (in response to robin.briggs)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


