Logical text order (source code)

= Logical text order

<Logical text order> is the order in which <text characters> are represented in a stored <sequence> for interpretation. A bidirectional renderer derives <visual text order> from that sequence; it does not require the stored characters to be prearranged in screen-position order.<Unicode Bidirectional Algorithm (Revision 51) by the Unicode Consortium>[section1: logical order and display order]{ref}

An original indexing example uses six abstract items. Write the stored positions in order as
$$
\begin{array}{c|rrrrrr}
\text{position}&0&1&2&3&4&5\\
\text{item}&a&R_1&1&2&R_2&b\\
\text{resolved level}&0&1&2&2&1&0
\end{array}
$$
The $R_i$ labels each stand for a single right-to-left <text character>. All displayed arrays here are read physically from left to right. Applying <level-based reordering of bidirectional text> to these supplied levels gives the visual list $a,R_2,1,2,R_1,b$, whose source-position list is $0,4,2,3,1,5$. Stored position1 remains position1 even though its item is drawn near the right end.

This distinction makes a text-processing task precise. An operation on logical positions1 through4 selects the contiguous stored subsequence $R_1,1,2,R_2$. A renderer may need a position map to relate that range to its displayed cells. Sorting characters by screen coordinate before searching or serializing the text would change the sequence being processed.

Logical order does not determine a font's glyph geometry. <Text shaping>, line layout and the <Unicode Bidirectional Algorithm> contribute different information. For example, a different line break can change which items are reordered together while the underlying stored sequence remains the same.<Unicode Bidirectional Algorithm (Revision 51) by the Unicode Consortium>[section3.4, per-line reordering; section3.5, shaping]{ref}