【问题标题】:Multilingual text with rmagick使用 rmagick 的多语言文本
【发布时间】:2012-11-20 18:58:33
【问题描述】:

我正在使用 rmagick 以编程方式使用文本注释图像。文本需要支持多种语言,包括中文、韩文、英文等。我正在处理的字体要求非常具体,为英文选择的字体支持多种西方语言,但不支持中文或韩文。我将为这些语言提供其他字体。

我想到的方法是将字符范围映射到特定字体,并以此为基础以编程方式告诉 rmagick 要使用什么字体。我是否遗漏了任何明显的东西,或者这是一个很好的方法?

【问题讨论】:

    标签: ruby utf-8 fonts rmagick


    【解决方案1】:

    这是我最终解决这个问题的方法:

    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
    

    该方法将获取一些文本并将路径返回到适当的字体。正则表达式的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
    

    值得注意的是,这种简单化的方法无法处理混合语言的输入。

    【讨论】:

      猜你喜欢
      • 2012-07-09
      • 2013-06-08
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多