<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Arabic on Abdullah Diab&#39;s Blog</title>
    <link>https://mpcabd.xyz/tags/arabic/</link>
    <description>Recent content in Arabic on Abdullah Diab&#39;s Blog</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Thu, 10 May 2012 17:40:11 +0000</lastBuildDate>
    <atom:link href="https://mpcabd.xyz/tags/arabic/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Python Arabic Text Reshaper</title>
      <link>https://mpcabd.xyz/python-arabic-text-reshaper/</link>
      <pubDate>Thu, 10 May 2012 17:40:11 +0000</pubDate>
      <guid>https://mpcabd.xyz/python-arabic-text-reshaper/</guid>
      <description>&lt;p&gt;I was trying today to generate PDF reports using &lt;a href=&#34;http://www.geraldoreports.org/&#34; target=&#34;_blank&#34;&gt;Geraldo Reports&lt;/a&gt; and I needed to generate reports with Arabic text in them. Arabic is a very special script language with two essential features:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;It is written from right to left.&lt;/li&gt;
&lt;li&gt;The characters change shape according to their surrounding characters.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;So when you try to print Arabic text in an application – or a library – that doesn’t support Arabic you’re pretty likely to end up with something that looks like this:&lt;/p&gt;
&lt;figure class=&#34;auto center&#34;&gt;
    &lt;img loading=&#34;lazy&#34; src=&#34;https://mpcabd.xyz/wp-content/uploads/2012/05/arabic-1.png&#34;
         alt=&#34;Arabic text, broken, left to right&#34;/&gt; &lt;figcaption&gt;
            &lt;p&gt;Arabic text, broken, left to right&lt;/p&gt;
        &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;We have two problems here, first, the characters are in the isolated form, which means that every character is rendered regardless of its surroundings, and second is that the text is written from left to right.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>I was trying today to generate PDF reports using <a href="http://www.geraldoreports.org/" target="_blank">Geraldo Reports</a> and I needed to generate reports with Arabic text in them. Arabic is a very special script language with two essential features:</p>
<ol>
<li>It is written from right to left.</li>
<li>The characters change shape according to their surrounding characters.</li>
</ol>
<p>So when you try to print Arabic text in an application – or a library – that doesn’t support Arabic you’re pretty likely to end up with something that looks like this:</p>
<figure class="auto center">
    <img loading="lazy" src="/wp-content/uploads/2012/05/arabic-1.png"
         alt="Arabic text, broken, left to right"/> <figcaption>
            <p>Arabic text, broken, left to right</p>
        </figcaption>
</figure>

<p>We have two problems here, first, the characters are in the isolated form, which means that every character is rendered regardless of its surroundings, and second is that the text is written from left to right.</p>
<p>To solve the latter issue all we have to do is to use the <a title="Unicode Bidirectional Algorithm" href="http://unicode.org/reports/tr9/" target="_blank">Unicode bidirectional algorithm</a>, which is implemented purely in Python in <a title="python-bidi; Unicode Bidirectional Algorithm" href="https://github.com/MeirKriheli/python-bidi" target="_blank">python-bidi</a>. If you use it you’ll end up with something that looks like this:</p>
<figure class="auto center">
    <img loading="lazy" src="/wp-content/uploads/2012/05/arabic-6.png"
         alt="Arabic text, broken, right to left"/> <figcaption>
            <p>Arabic text, broken, right to left</p>
        </figcaption>
</figure>

<p>The only issue left to solve is to reshape those characters and replace them with their correct shapes according to their surroundings.</p>
<p>I solved this issue more than four years ago in a small application that I wrote in Visual Basic, my solution was naive but it solved it well, anyway, a few days ago I faced the same problem – rendering Arabic text correctly – but on Android, and I searched and used the solution in <a title="Stack Overflow answer by breceivemail that solves Arabic rendering in Android" href="http://stackoverflow.com/a/7962813/99557" target="_blank">this SO answer</a>, which is pretty similar to the solution provided in <a title="Better Arabic Reshaper, Java class for Android" href="https://github.com/agawish/Better-Arabic-Reshaper/" target="_blank">Better Arabic Reshaper</a>.</p>
<p>Today I ported the solution in Better Arabic Reshaper from Java to Python, tweaked it a little bit, and used it to successfully render Arabic text in PDF, and the result was:</p>
<figure class="auto center">
    <img loading="lazy" src="/wp-content/uploads/2012/05/arabic-3.png"
         alt="Arabic text, correctly shaped, right to left"/> <figcaption>
            <p>Arabic text, correctly shaped, right to left</p>
        </figcaption>
</figure>

<p>Pretty cool right? Here is another test with English text in it some diacritics:</p>
<figure class="auto center">
    <img loading="lazy" src="/wp-content/uploads/2012/05/arabic-4.png"
         alt="Arabic text, with English text, correctly shaped, correctly directed, with diacritics"/> <figcaption>
            <p>Arabic text, with English text, correctly shaped, correctly directed, with diacritics</p>
        </figcaption>
</figure>

<p>It looks fine! in Word the same text looks like this:</p>
<figure class="auto center">
    <img loading="lazy" src="/wp-content/uploads/2012/05/arabic-5.png"
         alt="Arabic text, with English text, correctly shaped, correctly directed, with diacritics, in Microsoft Word"/> <figcaption>
            <p>Arabic text, with English text, correctly shaped, correctly directed, with diacritics, in Microsoft Word</p>
        </figcaption>
</figure>

<p>Amazing, now it is time for you to use the ported library along with python-bidi to solve those issues.</p>
<h3 id="usage">Usage</h3>
<pre class="lang:python decode:1">import arabic_reshaper
from bidi.algorithm import get_display

#...
reshaped_text = arabic_reshaper.reshape(u'اللغة العربية رائعة')
bidi_text = get_display(reshaped_text)
pass_arabic_text_to_render(bidi_text)  # &lt;-- This function does not really exist
#...
</pre>
<p>The <code>pass_arabic_text_to_render</code> function here is an imaginary function, it is just here to say that the variable bidi_text is the variable that you would need to use in your code afterwards, for example to print it in PDF, or to write it in an Image, etc.</p>
<h3 id="demo">Demo</h3>
<p>You can try an online demo of this script on my page here: <a href="http://reshaper.mpcabd.xyz/">Arabic Reshaper Online</a>.</p>
<h3 id="download">Download</h3>
<p>The source code is licensed under the <a href="https://opensource.org/licenses/MIT">MIT License</a>.</p>
<p>You can install it into your Python installation with pip:</p>
<pre class="lang:bash decode:1">$ pip install arabic-reshaper</pre>
<p><a href="https://github.com/mpcabd/python-arabic-reshaper" target="_blank">Project on GitHub</a></p>
<p><a href="https://github.com/mpcabd/python-arabic-reshaper/tarball/master" target="_blank">Source code download from GitHub</a></p>
<p>Have fun واستمتع! 🙂</p>]]></content:encoded>
    </item>
  </channel>
</rss>
