SelectionUTF16ToUnit

Forms, interaction, unicode

Description

Converts a zero-based UTF-16 position within the active edit value into a count of complete selection units

With fsuUTF16CodeUnit the index is returned unchanged after clamping

With fsuUnicodeCodePoint or fsuGraphemeCluster the boundary table for that unit is built on demand and searched, so a position inside a surrogate pair maps to its containing code point and a position inside a cluster maps to the preceding cluster boundary

Boundary tables are cached for the current edit value and discarded whenever that value changes

Declaration

function SelectionUTF16ToUnit(Index: Integer;
  IndexUnit: TPDFlibFormSelectionUnit): Integer;

Parameters

IndexZero-based UTF-16 position, clamped into the range 0 through the current value length
IndexUnitSelection unit the result is counted in

Return value

Returns the number of complete code points or grapheme clusters that precede the UTF-16 position, or the clamped index itself for fsuUTF16CodeUnit

Example

// A surrogate pair counts as one code point unit
Session.SetEditValue('A' + WideChar($D83D) + WideChar($DE00) + 'B');
CaretUnits := Session.SelectionUTF16ToUnit(3, fsuUnicodeCodePoint);
// CaretUnits = 2

See also

SelectionUnitToUTF16, GetSelectionRange, SetSelectionRange, TPDFlibFormSelectionUnit, TPDFlibFormSession