Techioz Blog

RMagick と Word Wrap を使用して画像上にテキストをレンダリングするにはどうすればよいですか?

概要

画像 (1024x768) 上にテキスト (Unicode、helvetica、白、22px、太字) をレンダリングする必要があります。

これはこれまでの私のコードです:

img = Magick::ImageList.new("my_bg_img.jpg")
txt = Magick::Draw.new

img.annotate(txt, 800, 600, 0, 0, "my super long text that needs to be auto line breaked and cropped") {
      txt.gravity = Magick::NorthGravity
      txt.pointsize = 22
      txt.fill = "#ffffff"
      txt.font_family = 'helvetica'
      txt.font_weight = Magick::BoldWeight
}

img.format = "jpeg"

return img.to_blob

それはすべて問題ありませんが、すべてのテキストを定義した領域( 800x600 )に収めるために自動的に改行(ワードラップ)されません。

私の何が間違っているのでしょうか?

助けてくれてありがとう:)

解決策

Draw.annotate メソッドでは、width パラメータはレンダリング テキストに影響を与えていないようです。

私も同じ問題に直面したため、新しい行を追加することでテキストを指定された幅に合わせるこの関数を開発しました。

画像上に描画する際に文字が指定した幅に収まるかどうかをチェックする機能があります

def text_fit?(text, width)
  tmp_image = Image.new(width, 500)
  drawing = Draw.new
  drawing.annotate(tmp_image, 0, 0, 0, 0, text) { |txt|
    txt.gravity = Magick::NorthGravity
    txt.pointsize = 22
    txt.fill = "#ffffff"
    txt.font_family = 'helvetica'
    txt.font_weight = Magick::BoldWeight
  }
  metrics = drawing.get_multiline_type_metrics(tmp_image, text)
  (metrics.width < width)
end

そして、新しい行を追加して、指定された幅に収まるようにテキストを変換する別の関数があります

def fit_text(text, width)
  separator = ' '
  line = ''

  if not text_fit?(text, width) and text.include? separator
    i = 0
    text.split(separator).each do |word|
      if i == 0
        tmp_line = line + word
      else
        tmp_line = line + separator + word
      end

      if text_fit?(tmp_line, width)
        unless i == 0
          line += separator
        end
        line += word
      else
        unless i == 0
          line +=  '\n'
        end
        line += word
      end
      i += 1
    end
    text = line
  end
  text
end