# Right-to-left text

**URL:** https://discourse.processing.org/t/right-to-left-text/3527
**Category:** Coding Questions
**Created:** [September 13, 2018, 10:20pm UTC](https://discourse.processing.org/t/right-to-left-text/3527 "2018-09-13T22:20:33Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Miguel](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@Miguel](https://discourse.processing.org/u/Miguel)
#### Post date: [September 13, 2018, 10:20pm UTC](https://discourse.processing.org/t/right-to-left-text/3527/1 "2018-09-13T22:20:33Z")

</div>

Hey folks, long time no see. I’ve been away from processing for a while but now I’ve opened it again for a small project: I am trying to render some Persian text within processing. The text looks fine when the result is rendered using P2d and exported as a PNG. However when the renderer is switched to PDF the text looks all broken like the characters are being flipped. I believe this has to do with how PDF mode handles RTL text. Unfortunately a simple scale to flip the whole text does not fix the problem.

Is there a way to handle this issue?

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [September 14, 2018, 3:01am UTC](https://discourse.processing.org/t/right-to-left-text/3527/2 "2018-09-14T03:01:07Z")

</div>

Can you provide a small code sample reproducing the problem?

Kf

---

<div class="post-metadata">

### Author: ![WakeMeAtThree](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wakemeatthree/32/38_2.png) [@WakeMeAtThree](https://discourse.processing.org/u/WakeMeAtThree)
#### Post date: [September 14, 2018, 6:08am UTC](https://discourse.processing.org/t/right-to-left-text/3527/3 "2018-09-14T06:08:33Z")

</div>

> [@Miguel](#):
>
> . The text looks fine when the result is rendered using P2d and exported as a PNG. However when the renderer is switched to PDF the text looks all broken like the characters are being flipped. I believe this has to do with how PDF mode handles RTL text. Unfortunately a simple scale to flip the whole text does not fix the problem.
> 
> Is there a way to handle this issue?

Hi @Miguel!  
I run into this issue a lot in Arabic, and not just in processing, but also in illustrator, photoshop, etc. One quick fix I tend to use is to flip input text (you can do that on your own or use [this online tool to flip it](http://www.arabic-keyboard.org/photoshop-arabic/))

For example, اهلا وسهلا in Arabic (It means “Welcome”) appears correctly to you right now, but then it appears like this when rendered to pdf:  
 ![flippedincorrect](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/99012b1b3bf31079f62e5d17e31456a42f0a46f0.jpeg)

So then, I change اهلا وسهلا to its flipped version using the previous tool ﻼﻬﺳﻭ ﻼﻫﺍ, and then it appears correctly.  
 ![flippedcorrect](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4827065ee52c9db9b809d2b35737b16107cd4704.jpeg)

If anyone needs a simple example to test this out, here it is:

```java
import processing.pdf.*;

void setup() {
  size(400, 400, PDF, "test.pdf");
  textAlign(CENTER, CENTER);
  fill(0);
  textSize(64);
}

void draw() {
  background(255);  
  text("ﻼﻬﺳﻭ ﻼﻫﺍ", width/2, height/2);
  exit(); 
}

```

---

<div class="post-metadata">

### Author: ![Miguel](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@Miguel](https://discourse.processing.org/u/Miguel)
#### Post date: [September 14, 2018, 10:14am UTC](https://discourse.processing.org/t/right-to-left-text/3527/4 "2018-09-14T10:14:20Z")

</div>

WakeMeAtThree:  
I tried a similar approach to what this tool is doing, mainly reversing the string with StringBuilder:

```auto
import processing.pdf.*;

void setup() {
  size(400, 400, PDF, "test.pdf");
  textAlign(CENTER, CENTER);
  fill(0);
  textSize(64);
}

void draw() {
  background(255);
  String welcome = "اهلا وسهلا";
  String emoclew = new StringBuilder(welcome).reverse().toString();
  text(emoclew, width/2, height/2);
  exit(); 
}

```

I am not a Persian speaker, but I believe by looking at the results that even though the string was reversed, the end results were definitely not accurate, still did not look legible. Maybe I missed something.

I have also tried to use unicode characters to set the text direction ([https://en.wikipedia.org/wiki/Universal\_Character\_Set\_characters#Bidirectional\_General\_Formatting](https://en.wikipedia.org/wiki/Universal_Character_Set_characters#Bidirectional_General_Formatting)) but it looks like they are being ignored by Processing.

---

<div class="post-metadata">

### Author: ![WakeMeAtThree](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wakemeatthree/32/38_2.png) [@WakeMeAtThree](https://discourse.processing.org/u/WakeMeAtThree)
#### Post date: [September 14, 2018, 11:41am UTC](https://discourse.processing.org/t/right-to-left-text/3527/5 "2018-09-14T11:41:55Z")

</div>

Hi @Miguel,  
It is not exactly a reversed string (I wish if it was as easy as that). I would encourage you to use the online tool I suggested previously to flip your texts instead rather than implement it yourself. This should help make your text legible, and if it still isn’t, post your code + sample text here so we can see what’s the issue.

---

<div class="post-metadata">

### Author: ![Miguel](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@Miguel](https://discourse.processing.org/u/Miguel)
#### Post date: [September 14, 2018, 12:06pm UTC](https://discourse.processing.org/t/right-to-left-text/3527/6 "2018-09-14T12:06:59Z")

</div>

The problem is I am pulling text from a database, so this tool wont help much ;(

---

<div class="post-metadata">

### Author: ![TheWizardBear](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thewizardbear/32/3552_2.png) [@TheWizardBear](https://discourse.processing.org/u/TheWizardBear)
#### Post date: [September 14, 2018, 2:28pm UTC](https://discourse.processing.org/t/right-to-left-text/3527/7 "2018-09-14T14:28:32Z")

</div>

Does this work?

```auto
String oldString = "hello world";
String newString = "";

void setup(){
  size(400, 400);
  background(255);
  fill(0);
  for(int chr = oldString.length()-1; chr >= 0; chr--){
    newString += oldString.charAt(chr);    
  }
  text("original string: " + oldString, 50, 50);
  text("new string: " + newString, 50, 100);
}

```

---

<div class="post-metadata">

### Author: ![Miguel](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@Miguel](https://discourse.processing.org/u/Miguel)
#### Post date: [September 14, 2018, 2:52pm UTC](https://discourse.processing.org/t/right-to-left-text/3527/8 "2018-09-14T14:52:43Z")

</div>

Unfortunately not. Tried that, it is just another form of doing StringBuilder().rever() or convert a string to a char array and looping it. I am not so knowledgeable about Arabic, but I believe it has to do with the fact that some characters take more memory allocation than other.

---

<div class="post-metadata">

### Author: ![WakeMeAtThree](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wakemeatthree/32/38_2.png) [@WakeMeAtThree](https://discourse.processing.org/u/WakeMeAtThree)
#### Post date: [September 14, 2018, 3:10pm UTC](https://discourse.processing.org/t/right-to-left-text/3527/9 "2018-09-14T15:10:54Z")

</div>

Here’s a [blog post](http://mpcabd.xyz/python-arabic-text-reshaper/) that addresses the Arabic text shape issue. You will also find mentioned a Python port of [Better Arabic Reshaper](https://github.com/agawish/Better-Arabic-Reshaper) which is written in Java. So there are two approaches you can take:

- Either incorporate Better Arabic Reshaper (or an similar set of tools) into your processing sketch so that you transform your data as you render it.
- Or transform the dataset first (maybe in python with `arabic_reshaper` or a similar library) and then do your pdf generation as normal in processing.

Also, one more issue you might have is that the set of letters in your text-script may have 3 or 4 more distinct letters than Arabic, so if you don’t find any dedicated library to it, you may have to include it yourself in whatever library you end up using.

@TheWizardBear I wish that would work, but it’s not just reversed string. Take a look at [this code to get an idea](https://github.com/agawish/Better-Arabic-Reshaper/blob/master/src/org/amr/arabic/ArabicReshaper.java) from the Arabic reshaper set of tools I linked to earlier.

---

<div class="post-metadata">

### Author: ![Miguel](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@Miguel](https://discourse.processing.org/u/Miguel)
#### Post date: [September 14, 2018, 3:37pm UTC](https://discourse.processing.org/t/right-to-left-text/3527/10 "2018-09-14T15:37:15Z")

</div>

> [@WakeMeAtThree](#):
>
> Better Arabic Reshaper

Thank you very much, WakeMeAtThree. This is the kind of information I needed to understand the issue and implement my own solution. I really appreciate you taking the time to dig this. As a side note, maybe something like that could be incorporated in Processing core, like a direction parameter on the text() or a TextDirection(), like we have TextMode().
