4

画像(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)に収めるために自動的に行を分割することはありません(ワードラップ)。

私は何が間違っているのですか?

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

4

3 に答える 3

14

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

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

画像に描画したときにテキストが指定された幅に収まるかどうかを確認する機能があります

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
于 2014-10-03T15:46:28.313 に答える