Skip to main content
Participating Frequently
November 23, 2009
Question

getAtomIndexAtCharIndex Fails?

  • November 23, 2009
  • 1 reply
  • 882 views

I am looking for the bounding box around a string. I know that the string exists on a given line, i checked using TextLine.textBlock.content.text.

here is what I am doing

var tokens:Array = editor.text.split(myPattern); //myPatter is a RegExp,

//editor is a RichEditableText

var pos:int = 0;

for (var i:int=0;i<tokens.length-1;i++)

{

     var t:String = tokens;

     pos += t.length;

     var span:SpanElement = editor.textFlow.findLeaf(pos) as SpanElement;

     var relativePosition:int = pos - span.getAbsoluteStart();

     //var length:int = pos+searchInput.text.length;

     var bbox:Rectangle = spanBBox(span,pos,relativePosition,search.length);

     /// the rectangle is then used to place sprites  etc,,,

     pos += searchInput.text.length; // searchInput is TextInpu

}

private function spanBBox(abs:int,rel:int,len:int):Rectangle

{

     var line:TextFlowLine = editor.textFlow.flowComposer.findLineAtPosition(abs);

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

     if(textLine.textBlock.content.text.substring(rel,rel+len) != searchInput.text)

     {

          //this does happen, yet the line does contain the searchInput!

          rel = textLine.textBlock.content.text.indexOf(searchInput.text);

     }

     var atomIndex1:int = textLine.getAtomIndexAtCharIndex(rel);

     var atomIndex2:int = textLine.getAtomIndexAtCharIndex(rel+len);

     var r1:Rectangle = textLine.getBounds(editor);

     var bbox1:Rectangle = textLine.getAtomBounds(atomIndex1);

     var bbox2:Rectangle  = textLine.getAtomBounds(atomIndex2);

     bbox1.y = r1.y; bbox1.height = r1.height; bbox1.width = bbox2.x - bbox1.x;

     bbox1.x += textLine.x;

     return bbox1;

}

50% of the time atomIndex1,2 are -1. Any clues?

Thank you

Raed

This topic has been closed for replies.

1 reply

Participating Frequently
November 24, 2009

No feedback on this issue?

The purpose for this is to add sprites that highlight various segments of text in cases like Search, Spell Checking, etc which is by far much faster than using textFlow.setFormatOfRange.

Specifically in spell checking, highlighting, say 20-30 misspelled words, is very memory intensive, whereas adding sprites is much much faster. Granted, that resizing would be an issue if opting for sprites, but i am willing to bet to calculating the coordinates of 30 words and moving sprites around the screen will be faster than using setFormatOfRange.

We have seen people adding nice functionality and experimenting with TLF, but i would like to hear their experience in, say, spell checking real text, with 800+ words and 50 misspelled words

thanks for any feedback

Raed

Adobe Employee
November 24, 2009

TextLine.getAtomIndexAtCharIndex needs a position that is relative to the paragraph. So if pos is an absolute position, then you need to convert it to a position relative to the paragraph start, e.g.:

var paragraph:ParagraphElement = span.getParagraph();

var relativePosition:int = pos - parapgraph.getAbsoluteStart();

var atomIndx:int = textLine.getAtomIndexAtCharIndex(relativePosition);

The relative position you are passing in now is relative to the span, which will work fine for the first span in the paragraph, but not for any following span.

- robin

Participating Frequently
November 24, 2009

thank you! that works