Friday, May 8, 2015

Experimenting with FMX and Synedit

I've done a small experiment to create simple read-only FMX control which could display highlighted code (Pascal in my case).

To tokenize text SynEdit highlighter was taken (SynHighlighterPas). Also minimal required set of units was used and cut/modified to compile in XE8 FMX.

Highlighter

Highlighter is used on each line to find tokens from text and according attributes (background color, foreground color, font styles).

There's Range property to know what's current block/range type (like comment, directive, asm block). Highlighter need to know what was last range of previous line to continue scanning of new line correctly.

First line obviously needs no Range. So we call ResetRange for first line.
For this simple example range was not set because lines was scanned sequentially w/o gaps.

Then need to feed line to highlighter with SetLine function. Now it has line and calls Next internally to get first token.

Next does search for next token and set token ID, position and token length,.

We can call Next until end of line (which can be tested by function GetEol).

For each token we know attributes and can paint accordingly.

Code

Base SynEdit was taken from https://github.com/SynEdit/SynEdit

Some units were cut and modified in order to compile.

Code of this experiment can be downloaded from https://github.com/vdisasm/FMXTestSynedit

Text Control

Simple FMX text control was created instead of TSynEdit control. Generally it contains vertical scroll bar and Lines. It paints lines using provided highlighter (or uncolored) and allows vertical scrolling. No caret, selection and other.

Here's how sample looks


Notes

Painting is much faster if GlobalUseGPUCanvas used (about 10x max).

I think main speed problem in my code is related to many canvas attribute switches. For example painting uncolored text is fast.

I suppose optimized version should use TTextLayout for text rendering like TMemo.

No comments:

Post a Comment