Search code examples
rubylinuximagemagickarabicrmagick

linux CLI: how to render Arabic text into bitmap


I would like to draw Arabic text into bitmap (bmp or png) with command line interface.

I have tried imagemagick and RMagick, but I have problem with RTL language issue. all my rendering are left to right. And google is not helping.

require:

./render "لوحة المفاتيح" out.png

would give a bitmap:

لوحة المفاتيح

Anyone can give me some successful results?


Solution

  • 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:

    Text to image result

    Some notes:

    1. You need to use a font which defines the used characters (in your case an arabic font)
    2. 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:

    Text to image result using pango