linux CLI: how to render Arabic text into bitmap
Anyone can give me some successful results?
I can. I use convert
from ImageMagick:
echo لوحة المفاتيح > text.txt && \
convert -size 300x200 \
-background white \
-fill black \
-font /usr/share/fonts/truetype/droid/DroidNaskh-Regular.ttf \
-pointsize 24 \
-gravity Center \
label:@text.txt \
text.png
This is the result:
Some notes:
- You need to use a font which defines the used characters (in your case an arabic font)
- Always use a temporary file with the text inside instead of provide the text directly from the command line: it would lead to weird results
UPDATE I didn't notice the RTL part of the question; I think I got better results using pango (reference):
# On Ubuntu
sudo apt-get install libpango1.0-dev
echo -n لوحة المفاتيح > text.txt
pango-view text.txt --no-display --output text.png
Result:
phantomjs screenshot font missing, boxes rendered instead
I had a similar problem with Japanese fonts. (PhantomJS 1.9.1, Redhat on Amazon EC2)
English characters showed up fine, but Japanese characters were rendered as boxes.
How I fixed it:
1) Installed the (Japanese) IPA fonts (Mincho and Gothic) using yum install
.
(Use yum list
to check the exact package names.)
2) The IPA .ttf
files were installed to:
/usr/share/fonts/IPA-Gothic/
/usr/share/fonts/IPA-Mincho/
3) Move the two downloaded .ttf
files to this directory: (Create it)
/usr/share/fonts/ipa/
4) Make a backup of /etc/fonts/fonts.conf
5) Edit the original /etc/fonts/fonts.conf
and fill it with this:
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/usr/share/fonts/ipa</dir>
<cachedir>/var/cache/fontconfig</cachedir>
<cachedir>~/.fontconfig</cachedir>
<alias>
<family>serif</family>
<prefer>
<family>IPAP Mincho</family>
</prefer>
</alias>
<alias>
<family>sans serif</family>
<prefer>
<family>IPAP Gothic</family>
</prefer>
</alias>
<alias>
<family>monospace</family>
<prefer>
<family>IPA Gothic</family>
</prefer>
</alias>
</fontconfig>
6) Refresh your font cache with fc-cache -vf
7) Enjoy your new working fonts.
Gotchas:
If you're getting no characters (blank space), your font cache is probably out of date.
Tryfc-cache -vf
to regenerate it.There's a fix for Japanese/Chinese/Korean characters in the 1.9.1 release. Not sure if it makes a difference, but probably worth upgrading from 1.9.0.
Related Topics
Heroku: Pg::Connectionbad: Could Not Connect to Server: Connection Refused
Ruby - Problems with Expect and Pty
How to Create a Jwt for Use with Apple Music
How to Refer a Local Gem in Ruby
"Ruby.Exe Is Not Recognized as an Internal or External Command" in Windows 7
Colon in the Front: Yaml Syntax
Rails - Link_To Helper with Data-* Attribute
Append Row to CSV File Ruby 1.9 CSV Lib
How to Use Factorygirl to Create an Attribute Called "Alias"
Dynamically Get Object's Attribute
How to Return a Fixed Length Binary Representation of an Integer in Ruby
Ruby Inserting Key, Value Elements in Hash
Robustly Call a Flaky API: Proper Error Handling with Net::Http
Can You Create/Write/Append a String to a File in a Single Line in Ruby