1 hosho_bidi
liquibyte edited this page 2026-11-19 16:23:30 -06:00

hosho_bidi.h

Bidirectional Text: the Order in which a Line's Characters Are Shown.

Copyright (c) 2016-2026 Michael Grey (liquibyte) liquibyte@gmail.com SPDX-License-Identifier: Artistic-2.0 Distributed under the Artistic License 2.0; see LICENSE for terms.

Hebrew and Arabic are written from right to left; numbers and Latin words in them go from left to right. A program writes the characters in the order they are read. This gives the order in which a display shows them.

Features:

  • The implicit rules of the Unicode Bidirectional Algorithm (UAX #9): the weak types (W1 to W7), the neutral types (N1, N2), the levels (I1, I2), the line's end (L1) and the reordering (L2)
  • Mirror characters: a bracket in right-to-left text is shown as its mirror image (L4)
  • A line that is left to right, right to left, or as its first strong character says

Internal design notes:

  • A terminal's line is one paragraph. The explicit embedding and isolate characters have no effect: they are read as "boundary neutral". Bracket pairs (N0) are not looked for: a bracket is a neutral character.
  • Characters are not joined into their shapes (Arabic): that is the work of the text drawing of a display
  • The tables come from the Unicode data files (hosho_bidi_tables.h)
#include "hosho_bidi.h"

Types: HoshoBidiDirection

Functions: hosho_bidi_is_right, hosho_bidi_mirror, hosho_bidi_order

Types

HoshoBidiDirection

The direction of a line, for hosho_bidi_order().

Name Value Description
HOSHO_BIDI_LEFT the line is left to right; right-to-left words in it are turned
HOSHO_BIDI_RIGHT the line is right to left
HOSHO_BIDI_AUTO as the line's first character with a direction says; left when there is none

Functions

hosho_bidi_is_right

int hosho_bidi_is_right(uint32_t character);

Say if a character is written from right to left (a Hebrew or an Arabic letter, and such)

Parameter Description
character A Unicode code point

Returns: 1 when it is, 0 when it is not

hosho_bidi_mirror

uint32_t hosho_bidi_mirror(uint32_t character);

Get the mirror image of a character.

Parameter Description
character A Unicode code point

Returns: Its mirror character (for "(" it is ")"), or the character itself when it has none

hosho_bidi_order

int hosho_bidi_order(
    const uint32_t *characters,
    int count,
    HoshoBidiDirection direction,
    int *logical_of,
    uint8_t *levels);

Find the order in which the characters of a line are shown.

Parameter Description
characters The characters in the order they are read; 0 counts as a space
count How many
direction The line's direction
logical_of Room for count numbers: gets, for each place on the display from the left, the index of the character shown there
levels Room for count bytes, or NULL: gets each character's level; a character with an odd level is right to left and is shown as its mirror

Returns: 1 when the order is not the order of reading; 0 when it is (then logical_of has 0, 1, 2 and so on), or when an argument is not correct or memory ran out