Text positions
CRDTTextHandler, CRDTFugueTextHandler and CRDTFugueRichTextHandler count
positions in runes (Unicode code points), not in UTF-16 code units. insert,
delete, update, length, the rich text handler's format and
attributesAt, and the Fugue handlers' stablePositionAt /
indexOfStablePosition all agree on this: one element is one rune.
For ASCII and most Latin text, a rune and a UTF-16 code unit are the same thing.
They differ for emoji and other characters outside the Basic Multilingual Plane:
'😀' is one rune and two code units.
Flutter counts code units​
Flutter's RenderEditable, TextSelection and TextPosition count UTF-16 code
units. CrdtTextFieldBuilder, CrdtTextCursorsOverlay and the Quill binding
already convert for you. You only need to convert yourself when you build a
custom text binding.
The single conversion point is RuneOffsets, defined in crdt_lf:
RuneOffsets.utf16Offset('a😀b', 1); // 1 — start of the emoji
RuneOffsets.utf16Offset('a😀b', 2); // 3 — start of 'b'
RuneOffsets.runeIndex('a😀b', 3); // 2 — the rune that starts at code unit 3
utf16Offset converts a handler rune index into a UTF-16 offset for
TextSelection / TextPosition. runeIndex converts the other way. Both clamp
out-of-range input to the ends of the string instead of throwing.