Rmagick Rvg hebrew issue

65 views
Skip to first unread message

alexey

unread,
Nov 25, 2009, 7:25:50 AM11/25/09
to Israel.rb - the Israel Ruby & Rails Developers group
Hi,
I am generating images with multilingual annotations (english,
russian, hebrew, and more)
Problem is in rtl languages like hebrew.

Writing order are reversed, if string = "שלום" its outputted to image
like "םולש"

title.tspan(string,10, 200).styles
(:font_size=>font_size, :fill=>color, :font_weight => fw)

any ideas?
(except detect string language and resort it:))

Vladimir Tkach

unread,
Nov 25, 2009, 8:54:02 AM11/25/09
to isra...@googlegroups.com
To create images from the unicode text, we can use the "encoding" method for the Draw object.

Example:

require "RMagick"

def show_textimg
bg = Magick::Image.new(120,20){self.background_color = "#9E9E9E"}
text = Magick::Draw.new
text.encoding = "Unicode"
text.text(23,14,"ドの半角⇔全角")
text.draw(bg)
bg.write "#{RAILS_ROOT}/public/images/text.jpg"
end


2009/11/25 alexey <alex.cr...@gmail.com>

--

You received this message because you are subscribed to the Google Groups "Israel.rb - the Israel Ruby & Rails Developers group" group.
To post to this group, send email to isra...@googlegroups.com.
To unsubscribe from this group, send email to israelrb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/israelrb?hl=en.





--
Best Regards,
Vladimir Tkach

+972-54-7574166
tea...@gmail.com

http://teamco-anthill.blogspot.com/

http://www.google.com/search?q=teamco-anthill&pov=113485037116411478549&usg=__MJmWqpqtgaTxQA8ZMBvE3etiB1Q=

Elad Meidar

unread,
Nov 25, 2009, 9:40:09 AM11/25/09
to isra...@googlegroups.com
<whisper>RAILS_ROOT is dead, Rails.root</whisper>

Elad Meidar

Zohar Arad

unread,
Nov 25, 2009, 1:30:28 PM11/25/09
to isra...@googlegroups.com
Hey

Here's a snippet I use for generating buttons with Hebrew text using RMagick.

# string reversal method

def reverse(orig)
src = orig.force_encoding(Encoding::UTF_8).reverse.to_s
tgt = ''
tmp = []
src.each_char do |c|
if c =~ /[a-zA-Z0-9]/
tmp << c
else
if tmp.length > 0
tgt += tmp.reverse.join('')
tmp.clear
end
tgt += c
end
end
if tmp.length > 0
tgt += tmp.reverse.join('')
tmp.clear
end
tgt
end
end

# button generation method

def generate_button(text)
str = reverse(text)
base_path = File.join(File.dirname(__FILE__),'/../assets/')
canvas = Magick::Image.read(base_path+'button_bg.png').first
text = Magick::Draw.new
text.encoding = 'utf-8'
text.font = base_path+'goldfingerbold.ttf'
text.pointsize = 14
text.gravity = Magick::EastGravity
text.annotate(canvas, 0,0,10,-13, str) {
self.fill = 'white'
}
text.annotate(canvas, 0,0,10,12, str) {
self.fill = 'white'
}
canvas.format = 'PNG'
return canvas
end

Naturally, button and font assets may vary depending on your needs.

The main principle is to reverse the Hebrew string (and then un-reverse alpha-numeric characters) and use Magick::EastGravity to align text from right to left.

Also, if you can use a relatively recent version of ImageMagick (6.5) i recommend it. Using the above with ImageMagick 6.3 produces slightly worse results (font is rendered less clearly).

Hope this helps
Zohar
Reply all
Reply to author
Forward
0 new messages