4.2. The Bidirectional Algorithm

Nowadays we often speak of "culture shock". This shock has been troubling typographers for centuries because one of its most trivial aspects (and, alas, one of the easiest to resolve) is the difference in the direction in which scripts read. Suppose that we are writing a line of text in English and suddenly decide to switch to Arabic when we reach the middle of the line. Arabic is written from right to left; thus we cannot simply stay where we are and start writing in the opposite direction, since the space is already occupied. Thus we have to move, but where should we go? The less daring among us will change paragraphs at this point: that is a way to start from scratch. In the new paragraph, we start at the right, with the usual indention, and everything is perfect.

But suppose that the nature of the text does not permit a change of paragraph. Ideally we would set aside the space needed to write the few words of Arabic and begin at that point. But what happens if the Arabic text runs for several lines? And how can we go back to writing in English?

Fortunately, we are not the first to raise these questions; they have been tormenting typographers, and subsequently computer scientists, for some time now. In this chapter, we shall discuss the solution that Unicode offers for these problems, which are as old as the hills (or, at a minimum, as old as our cultures).

So as not to favor one of the scripts that are written from right to left (Arabic, Hebrew, ...

Get Fonts & Encodings now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.