Now the bigger part is integrating reshaping without messing up things.
Integration of Arabic Reshaping
First, lots of the logic in StaticLayout should be replaced.
When StaticLayout breaks text into lines it simply measures each character alone and sums individual character widths up in each word, applying rules from the Unicode Text Segmentation standard. Lines are sequentially extended to contain max number of words that fit.
After breaking words, Reshaping should be performed where necessary so that Arabic (or Arabic-like) words are measured correctly. This is especially important for lam-alef and other combinations that significantly affect displayed size of words (different shapes of the same Arabic letter might have different sizes). By that we successfully patch StaticLayout to break lines correctly. I removed the whole line-breaking logic and rewrote it in a separate class android.text.LineBreaker (not ready yet).
The next and not less important part of the job lefts off from Layout. Methods that inform cursor positions should be also modified to work according to Unicode Text Segmentation (currently they don’t), specifically letter boundaries (Unicode calls them Grapheme Boundaries). They should handle special cases like not stopping inside lam-alef sequences. They should coordinate real text indexes with indexes in reshaped forms.
Next, we should modify android.text.Styled to perform Arabic reshaping when necessary. Styled is used by layouts to measure text and draw it; it is the medium between Layout and the abstract graphics layer.
Performance Considerations
The text itself is always stored in its bare form in layouts. Reshaped form is necessary when doing measurements (cursor movement or placement) or drawing. Since the same layout might be redrawn several times and cursor movements are performed often, caching is logically favorable. I can think about big caches in Styled for general strings, or a cache in each Layout object that stores the shaped form of the text (only reshaped parts are stored).
Work Progress
- Patch StaticLayout to break lines correctly. done
- Patch Layout and Styled to measure and draw Arabic reshaped. pending
- Patch Layout to do mark selections and move cursors correctly. pending
I haven’t uploaded any code changes regarding this step. I’ll do so incrementally.