Arabic Letter Connector
Arabic is not always well-supported in all libraries. There are two problems that often occur when attempting to present Arabic text:
- The letters appear in reverse order (often because there is no right-to-left support).
- The letters appear disconnected.
This gem deals with the second problem. If you are this problem, it normally means your string use the generic form of every Arabic letter (that is, without any attributed form, for example a Qaf not a Qaf at the beginning of a word), and the library you are using to present this string doesn't do anything about it.
What this gem does is replace each such generic character to a character with form.
This gem is a refactored version of
Arabic-Prawn by Dynamix Solutions (Ahmed Nasser).
gem install arabic-letter-connector
Then require it with:
The gem provides a
ArabicLetterConnector.transform(string) method, and also monkey-patches
to include a
Below is an example. In the browser, it might appear that this library is doing nothing (since the browser does the work of converting the characters from their generic form considering their correct form). Try it in IRB to get a sense of what is happening.
x = "مرحبا يا العالم" x.unpack("C*") # [217, 133] y = x.connect_arabic_letters # "ﻣﺮﺣﺒﺎ ﻳﺎ ﺍﻟﻌﺎﻟﻢ" y.unpack("C*") # [239, 187, 163]
This gem is particular useful if you are using
prawn to generate PDF files.
require 'prawn' require 'arabic-letter-connector' Prawn::Document.generate("arabic.pdf") do text_direction :rtl font("/path/to/arabic/font.ttf") do # For example: http://www.amirifont.org/ text "مرحبا يا العالم".connect_arabic_letters end end