Wednesday, May 14, 2025

Arabic and Others: How can you present Right to Left Text in WebXR? | by Muadh Al Kalbani | Samsung Internet Developers

Share

Samsung Internet Developers
A white left-pointing arrow painted on a green brick wall
Photo by Nick Fewings on Unsplash

WebXR developers are constantly pushing boundaries of what’s possible in the immersive web, not only through crafting experiences that are captivating in terms of visuals and interactions, but also inclusive and accessible to a wider range of users. Presentation of text is a big part of immersive experiences over the web, however text directionality that is not Left-to-Right (LTR) is often not supported and in many cases problematic. This leads to wrong (and in many cases confusing) presentation of Right-to-Left (RTL) text and information to end users. In this blog post I provide a short historical context of LTR and RTL text, presentation of RTL text using HTML and finally discuss the main focus of the blog: RTL text in WebXR.

Two tiny disclaimers that I need to make before we jump in:

  1. The definition of “text” can vary slightly throughout this post. When referring to HTML/CSS pages or elements, text as the one you are reading now is designed to be displayed in web browsers using the standard HTML Markup Language. In the Immersive Web however, hold your breath for some serious oversimplification, text is essentially bitmap fonts/images that are graphically rendered and presented via browsers in a form that is “readable text” for end users. The focus of this article is on how to present RTL text in an accurate/readable manner in immersive environments, but we also touch on how you can make use of HTML text elements within your immersive scene using DOM Overlays.
  2. Due to my native language being Arabic — As-salamu alaykum السلام عليكم (peace be upon you) everyone! — you may find that I mention Arabic more than other RTL languages, this is simply because I know a little more about it. That being said, all solutions and workaround presented should be generally applicable.

Early computer systems were primarily developed to support LTR writing systems based on the Latin alphabet that is widely prevalent in Western languages. Accordingly the Latin alphabet became the standard for digital encoding leading to a natural bias towards accommodating LTR scripts.

This LTR-centric approach posed significant challenges for languages with RTL scripts, such as Arabic, Persian/Farsi and Hebrew. RTL text presented unique difficulties in digital environments, including issues with rendering, cursor placement and text mixing. These challenges stemmed from the fundamental differences in directionality, ligatures, joining control (and more) between LTR and RTL scripts, requiring specialised support to ensure accurate display and manipulation. Then along came the hero that is Unicode — this marked a significant moment in addressing problems linked to RTL scripts in digital systems by providing a comprehensive framework for encoding characters from diverse writing systems, including RTL scripts. Unicode facilitated greater interoperability and compatibility across digital platforms by providing standardised character encoding and directionality guidelines. You can find much more detailed information online around this topic — see the very last section on this page for some cool resources.

Despite these advancements, the full integration of RTL support in digital tools remains a work in progress. With the web now becoming more immersive using more complex rendering systems, there is still an ongoing need for continued development and collaboration to ensure inclusivity and accessibility for all writing styles.

The Internalization Working Group at W3C have done a brilliant job in addressing the issue of RTL text presentation for “flat webpages” (which we define as web pages that are not immersive and mostly 2D in nature, like the one you are reading this blog post on right now). You can now easily display RTL text in your HTML pages using the dir attribute. For your HTML pages, you can make use of the dir attribute to handle text directionality. The dir attribute ensures your text is flowing in the intended direction saving you the headache of worrying about the presentation of text on your web page.

For example you can set the whole page to be RTL if the content is predominantly RTL in nature:



. . .
. . .

Or only set certain elements within your page to be RTL if you’re intending to mix and match text directions:

You can also set dir to auto, which can be useful in cases where you’re not sure what direction the text will be (for example users input text in forms in which ever language they prefer):

See the W3C’s Inline markup and bidirectional text in HTML document for more RTL text scenarios such as usage in tables, mixing both RTL and LTR text and others.

But what does this whole section have to do with WebXR? DOM Overlays is the short and sweet answer. The WebXR Device API supports DOM Overlays that allow placing HTML elements on top of an immersive scene. Thus even if you are faced with challenges in displaying RTL text within the WebXR environment, DOM Overlays can be a good option to fall back on using the dir attribute as you would for a flat web page (the “flat web” term — there it is again, gaining traction already!). If you are interested to learn more about subtitles over the DOM, I encourage you to read over this report by the Immersive Captions Community Group that provides great guidelines and best practices for immersive subtitles.

Before jumping into how you can present RTL text accurately in your WebXR experiences, it’s worth mentioning that presenting RTL text correctly is a complex problem that is not exclusive to WebXR. In previous experiences I found this to be the case in other tools such as image editing software and native game engines. To better understand why this is a complex problem, I would advise having a look at the W3C’s Arabic & Persian Layout Requirements document which is pretty epic in its level of detail. I say epic because even as a native Arabic speaker I was in awe of some of the details and requirements covered in this document.

So back to solutions for the Immersive Web… I usually use A-Frame and Three.js for WebXR development, and to my knowledge neither provide built in support. I played around with both tools to attempt to present Arabic text and I am happy to share my failed attempts with you:

The 3D Arabic word for “peace” rendered in a solid light blue colour over a 3D environment of the Egyptian pyramids. The Arabic word is split into separate letters.
Failed attempt at displaying the Arabic word for “Peace” using A-Frame custom fonts (with the help of Don McCurdy’s useful BMfont tool). The Arabic word is split into separate letters that are flipped in order.
The 3D Arabic word for “peace” rendered in a gradient blue colour over a solid light mint background. The Arabic words is split into separate letters.
Failed attempt at displaying the Arabic word for “Peace” using default TextGeometry in Three.js. The Arabic word is split into separate letters that are flipped in order.

Both methods result in what I like to call “broken flipped Arabic” where a word gets flipped (defaulting to the left to right direction) and broken into separate letters with no lines connecting them correctly. You can make out what the separate letters are but because the whole word/phrase is flipped it becomes challenging to read and make sense of it as connecting ligatures are crucial to understand the meaning of Arabic text.

Because rendering 3D RTL text accurately is not supported out of the box in these engines, community solutions and efforts are usually king. troika-text is usually my go to component — it’s available for both A-Frame and Three.js:

troika-text offers a wide range of customisation and manipulation options to the appearance of RTL text.

To use troika-text in A-Frame

All you have to do is add the following in the of your page:

Trending News