Posted on by & filed under ebooks, epub.

Languages such as Arabic and Hebrew are written right-to-left (RTL) rather than left-to-right (LTR), as in European languages. When dealing with only one of those scripts at a time, computers generally handle the directionality well by just falling back on the user’s general language setting. But what if you have to render text in multiple directions?

This Project Gutenberg edition of Hunger by Knut Hamsun has text in Hebrew. It also has text in English. Ideally we’d like it to run in the correct direction for each language, but Bookworm shows it all running LTR (note the ragged-right margin):

Picture 51

Maybe it’s a Bookworm bug; if I open the XHTML file directly in my browser, I get the Hebrew text RTL, but the English text is aligned right, which looks weird:

Picture 52

Adobe Digital Editions doesn’t have a Hebrew font, and also the directionality is wrong:

Picture 50

(This is a case where font embedding is appropriate.)

This particular ebook is coded with the directionality marked using the dir attribute in the <body>:

&lt;body dir=&quot;rtl&quot; xml:lang=&quot;he&quot;&gt;

But that’s not really accurate, since the first block of content is in English (it’s the Project Gutenberg boilerplate text). We need to mark up the ebook to properly describe the directionality of all parts of the text.

CSS provides the direction property for this. It’s pretty simple; you can have:

direction: ltr; /* Left to right */
direction: rtl; /* Right to left */

So a better way to mark up Hunger might be to define the default as rtl and then mark up any English text with a .english class:

body {
.english {

And add the .english class where necessary:

&lt;div class=&quot;pgheader english&quot; id=&quot;pgepubid00000&quot;&gt;&lt;p&gt;
&lt;br/&gt;The Project Gutenberg EBook of Hunger, by Knut Hamsun

Now Bookworm is happy:

Picture 54

And later on that same page…

Picture 53

(The fact that Bookworm didn’t respect the initial dir attribute is a bug, which I opened with a test case.)


2 Responses to “Bidirectional text in ePub”

  1. Ben

    I may be mistaken, but if ADE on your computer doesn’t show Hebrew text (is lacking a Hebrew font) then it’s likely that your computer actually lacks one. I would imagine that a program gets it’s fonts from the underlying OS.

    Of course, if you want to absolutely ensure a font is available, then you can embed…at the cost of bloating your file by about 15x, and duplicating that font file in each ePub that requires it.