wezzy
wezzy

Reputation: 5935

Fast way to set text format for a range of Text using TLF

i have to set the text format for some tokens in a plain text. I'm trying to use the Text Layout Framework to improve the speed of the operation but i've founded that TLF is far slower (10X in my tests) than the old setTextFormat(). For each token i call this function:

public function setTextFormat(format:TextLayoutFormat, begin:int, end:int):void{

            var selection:SelectionState = new SelectionState(this._textFlow, begin, end, this._normalFormat);
            IEditManager(_textFlow.interactionManager).applyLeafFormat(format, selection);

        }

is there any faster and clever way to do this operation ?

Thanks

Upvotes: 0

Views: 959

Answers (1)

ZackBeNimble
ZackBeNimble

Reputation: 549

Most of the processing time in these TLF updates is the recalculation and update of the display. The association of particular formats with portions of your text model is much less intensive. Unfortunately, the applyLeafFormat() call does both format-association and redisplay operations. You need to split those two up.

Instead of only dealing with your tokens in terms of their absolute positions, you could split them into separate FlowElement objects (most likely SpanElements), which can be uniquely identified with an "id" property. Once your tokens are in separate elements, it becomes straightforward to iterate through a whole lot of them, change format characteristics, and only force a display update at the end.

for each (var id:String in ids) {
    var element:SpanElement = _textFlow.getElementByID(id) as SpanElement;
    if (element) {
        element.format = getAppropriateFormatForElement(element);
    }
}
_textFlow.flowComposer.updateAllControllers();

As an aside, splitting your tokens into elements also opens the door for storing your token classification in the elements themselves, freeing you from maintaining a separate classification mapping structure.

Upvotes: 1

Related Questions