我希望通过命令行界面将阿拉伯文本绘制到位图(bmp或png)中。我尝试过ImageMagick和RMagick,但在RTL语言方面存在问题,所有我的渲染都是从左到右的。谷歌也没有给予帮助。
要求:
要求:
./render "لوحة المفاتيح" out.png
会给出一个位图:
有人可以给我一些成功的结果吗?
键盘
./render "لوحة المفاتيح" out.png
会给出一个位图:
有人可以给我一些成功的结果吗?
键盘
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
一些注意事项:
更新 我没有注意到问题中的RTL部分;我认为使用pango获得了更好的结果 (参考资料):
# On Ubuntu
sudo apt-get install libpango1.0-dev
echo -n لوحة المفاتيح > text.txt
pango-view text.txt --no-display --output text.png
如果您需要进行阿拉伯文排版,可以按照以下步骤使用Inkscape 0.9(在Windows7命令提示符中):
inkscape "ar.svg" --export-png="C:\Users\Path\TO\output\arabicthabit.png" --export-dpi="900,900" --export-background="rgb(100%,100%,100%)"
ar.svg 是指:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
width="400"
height="100"
version="1.1">
<text
xml:space="preserve"
style="font-size:30px;font-style:normal;fill:#000000;fill-opacity:1;stroke:none;font-family:'Droid Arabic Naskh';font-weight:bold;"
x="10" y="90"
><tspan>لغةٌ عربيّةٌ</tspan>
</text>
</svg>
您可以使用系统中的任何字体替换font-family:'Droid Arabic Naskh'
。此外,您可以根据Inkscape手册中的说明更改输出格式。此外,您可以从Inkscape GUI开始,并保存SVG,在任何脚本语言中更改SVG XML中想要的内容。
Inkscape 0.48返回了有缺陷的阿拉伯文版式。
至于ImageMagick 6.8.9-7,我使用Python+Wand(Python Lib)+arabic_reshaper(Python Lib)+bidi.algorithme(Python Lib)来在图像中生成正确的阿拉伯文版式:
from wand.image import Image as wImage
from wand.display import display as wdiplay
from wand.drawing import Drawing
from wand.color import Color
import arabic_reshaper
from bidi.algorithm import get_display
reshaped_text = arabic_reshaper.reshape(u'لغةٌ عربيّة')
artext = get_display(reshaped_text)
fonts = ['C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\DroidNaskh-Bold.ttf',
'C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\Thabit.ttf',
'C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\Thabit-Bold-Oblique.ttf',
'C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\Thabit-Bold.ttf',
'C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\Thabit-Oblique.ttf',
'C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\majalla.ttf',
'C:\\Users\\PATH\\TO\\FONT\\Thabit-0.02\\majallab.ttf',
]
draw = Drawing()
img = wImage(width=1200,height=(len(fonts)+2)*60,background=Color('#ffffff'))
#draw.fill_color(Color('#000000'))
draw.text_alignment = 'right';
draw.text_antialias = True
draw.text_encoding = 'utf-8'
#draw.text_interline_spacing = 1
#draw.text_interword_spacing = 15.0
draw.text_kerning = 0.0
for i in range(len(fonts)):
font = fonts[i]
draw.font = font
draw.font_size = 40
draw.text(img.width / 2, 40+(i*60),artext)
print draw.get_font_metrics(img,artext)
draw(img)
draw.text(img.width / 2, 40+((i+1)*60),u'ناصر test')
draw(img)
img.save(filename='C:\\PATH\\OUTPUT\\arabictest.png'.format(r))
wdiplay(img)