我正在使用rmagick以编程方式为图像添加文本注释。文本将需要支持一系列语言,包括中文,韩语,英语等。我正在处理的字体要求非常具体,为英语选择的字体支持各种各样的西方语言,但它不支持中文或韩语。我会有其他的字体为这些语言。
我考虑的方法是将字符范围映射到特定的字体,并以编程方式告诉rmagick使用哪种字体。我是否遗漏了什么明显的东西,或者这是一个很好的方法?
发布于 2013-01-15 06:33:27
以下是我最终解决这个问题的方法:
def font_for(verb)
return "#{Rails.root}/app/uploaders/fonts/Gotham-Bold.ttf" if verb =~ /\p{Latin}/
return "#{Rails.root}/app/uploaders/fonts/ArialUnicode.ttf"
end该方法将获取一些文本,并将路径返回到适当的字体。Regex的character property matching在这里派上了用场!然后,我可以在我的rmagick脚本中使用font_for方法来注释图像。
def create_image_with_text
canvas = Magick::ImageList.new
canvas.new_image(640, 480) {self.background_color = "white"}
text = Magick::Draw.new
text.font = font_for "english"
text.pointsize = 23
text.gravity = ::Magick::NorthGravity
text.annotate(canvas, 0,0,0,28, "ENGLISH") { self.fill = '#343434' }
text.font = font_for self.verb
text.pointsize = 65
text.gravity = ::Magick::CenterGravity
text.annotate(canvas, 0,0,0,18, self.verb.upcase) { self.fill = '#343434' }
tempfile = Tempfile.new(['new_center_stripe', '.jpg'])
canvas.write tempfile.path
self.image.store!(tempfile)
end值得注意的是,这种简单的方法不能处理混合语言的输入。
https://stackoverflow.com/questions/13480189
复制相似问题