DEV Community

Yutaka Hirata
Yutaka Hirata

Posted on

[1/3] What Does “Right to Left” Actually Change? Lessons from My Hex Editor

Adding RTL to YuHex — Part 1 of 3

Japanese does not have just one reading direction, either

Before turning to Arabic, let me start with the language I read every day. Here is a short Japanese sentence, with its pronunciation and meaning:

日本語を読みます。

In hiragana, it is にほんごを よみます; in romanization, Nihongo o yomimasu. It means “I read Japanese.” 日本語 means “Japanese,” を marks the object, and 読みます means “read.” The particle を is pronounced o here.

Modern horizontal Japanese normally runs left to right, like English. That is the familiar arrangement in websites, applications, and technical documents. I will call it LTR, short for left-to-right. But Japanese is not always horizontal.

Many Japanese novels and manga use vertical text. Here is the same sentence split into two columns, with the final punctuation omitted to keep the reading order clear:

Col. 2  Col. 1
  読      日
  み      本
  ま      語
  す      を
Enter fullscreen mode Exit fullscreen mode

Start at the top of the right-hand column: 日本語を. Then move to the top of the left-hand column: 読みます. Characters advance downward; successive columns advance from right to left. That is different from characters advancing horizontally from right to left. W3C’s Japanese layout requirements distinguish vertical and horizontal composition.

Many Japanese manga also guide readers through panels from right to left, with vertical text in speech balloons. Many novels use vertical text too, although neither format is universal. We switch reading habits with the material in front of us.

An old Japanese sign might start on the right

Historically, Japanese also used horizontal writing that began at the right edge. Consider 東京駅, pronounced Tōkyō-eki, meaning “Tokyo Station.” The example below illustrates the direction; it is not a reproduction of a particular sign.

Modern horizontal: 東京駅  → read from the left
Older right-first: 駅京東  ← read from the right
Enter fullscreen mode Exit fullscreen mode

The lower line is still read 東, then 京, then 駅. Its pronunciation and meaning have not been reversed, and the individual characters have not been mirrored.

Not all older Japanese ran horizontally from the right. Both horizontal directions coexisted; there was no single overnight switch after World War II. Japan’s Center for Asian Historical Records explains the history, including left-to-right examples from before the war.

Seen this way, I was never exclusively a left-to-right reader. Arabic interfaces still felt unfamiliar, though.

When I look at an Arabic interface, I have to stop and decide where to begin. Japanese is the only language I know. Even so, I wanted people who read from the right to be able to use YuHex, the Windows hex editor I develop on my own.

A hex editor shows and edits a file one byte at a time. An application full of numbers might seem straightforward to rearrange. Move a few columns, change the alignment, and the job should be done.

That turned out to leave out most of the interesting questions. Some things needed to move. Others had to keep their order. Before writing about the C++, I want to explain the distinction that helped me understand the work.

Let’s read one short sentence

RTL means right-to-left: the basic direction of a passage. Arabic is a familiar example. English generally uses the opposite direction, LTR.

Here is a short example in Modern Standard Arabic:

هذا كتاب جديد

It means “This is a new book.” Begin with the word on the right. The pronunciation hints below omit grammatical case endings.

Reading order Word Pronunciation hint Meaning and role
1, on the right هذا hādhā this; a masculine singular demonstrative
2 كتاب kitāb book; indefinite here
3, on the left جديد jadīd new; an adjective describing the book

English puts new before book. In this Arabic example, the adjective comes after the noun. This kind of present-tense nominal sentence also has no written equivalent of is.

Adding the definite article ال to كتاب gives الكتاب, “the book.” The related sentence هذا الكتاب جديد means “This book is new.” Similar words, but a different construction.

These are just enough grammar notes to read the examples, rather than a general guide to Arabic. The University of California’s teaching material explains the relationship between demonstratives and nouns. Michigan State University’s textbook discusses definiteness and noun–adjective constructions.

Grammar determines the order of words in a sentence. Writing direction determines how that sequence appears on a line. Reversing English would not produce Arabic.

An LTR paragraph does not make Arabic letters run LTR

This was a useful surprise. Setting a paragraph to LTR does not make the Arabic letters themselves behave like English letters.

The figure below was rendered in a browser. It is an educational example, not a YuHex screenshot. The top shows the sentence we just read. The middle shows a label containing different kinds of characters.

An Arabic sentence with word-by-word grammar notes, followed by the same mixed label in LTR and RTL paragraphs

Begin with “this” on the right. In the mixed label below it, ABC and 123 retain their internal order in both paragraph directions.

The label was entered as the Arabic word ملف, meaning “file,” followed by a space, ABC, a space, and 123. It is a display test label, rather than an example of sentence grammar.

In the LTR paragraph, the Arabic word appears on the left, with ABC 123 to its right. In the RTL paragraph, the Arabic word appears on the right, with ABC 123 to its left. In both cases, the Arabic letters progress RTL within their own group. ABC does not become CBA, and 123 does not become 321.

Text that mixes directions this way is called bidirectional text, or BiDi. W3C’s introduction distinguishes runs of characters with a shared direction from the base direction of the paragraph containing them. The figure changes the paragraph direction, while keeping the stored characters identical.

What happens to the numbers

The familiar digits 123 and the Arabic-Indic digits ١٢٣ both put the hundreds digit on the left in these examples. Choosing a different set of digit shapes does not reverse place value.

That does not mean a developer can ignore everything containing Latin letters or numbers. Spaces, brackets, and hyphens can interact with surrounding text and paragraph direction. A single ABC may look fine while a longer identifier such as ABC-123 (v2) or a file path needs more care. The Unicode FAQ discusses differences involving numbers and separators.

Arabic also has contextual letter forms. The basic characters in كتاب are ك, ت, ا, and ب. Displaying isolated forms beside one another is not equivalent to displaying the joined word. Choosing the appropriate glyph forms is a separate concern from deciding the display order. The W3C Arabic and Persian layout material helped me understand that distinction.

Persia, a computer game, and an old Japanese sign

I learned during this work that Persian is also written RTL. It uses an Arabic-derived script, but it is a different language.

Persia brought back a much older memory: playing Prince of Persia on a computer. Perhaps I had been reading the title from left to right all along. I no longer remember the exact edition or title screen. A name from a game I played years ago had found its way into my development work through the way people read.

Japanese has its own reminders that writing direction is a convention. Old signs and newspapers sometimes have horizontal text beginning on the right. The history is not a clean switch at the end of World War II; both horizontal directions coexisted. Fukui Prefectural Archives describes that history.

Many Japanese novels still use vertical text, and many manga guide readers through panels from right to left. Technical documents generally run horizontally from left to right. Vertical Japanese is different from horizontal Arabic: characters advance downward, while successive lines advance from right to left.

I was already changing direction depending on what I read. Familiarity had made the change invisible to me.

Now bring those distinctions into a hex editor

YuHex puts human language beside bytes, addresses, and input expressions. Moving columns for someone who reads from the right must not change the meaning of a sequence such as 12 34 56 78.

I struggled to find reference implementations that answered the questions specific to this editor. There may still be awkward decisions in mine. At least I had begun to understand which questions needed separate answers.

YuHex has a Microsoft Store edition and a free edition, including for commercial use. If inspecting binary files is part of your work, I would be happy if you tried it.

In Japanese, I switch between a novel and a technical book without thinking about direction. I wanted someone reading another language to have that same ordinary ease. That was where I started.

Then moving the columns raised another question. If a byte moves to the right side of the screen, will clicking it still select that byte? A plausible-looking layout is not enough if an edit changes the wrong location. Part 2 follows the actual C++ behind keeping those two promises: move the layout, preserve the data order.

Top comments (0)