Search code examples
ruby-on-railsrubyimagemagickrmagick

ruby Rmagick text width


Im using Rmagick in a ruby project to generate a title, All is working fine as below but i need to put an image after the title and i was wondering if there is any way to find out the width of the text i have just drawn? Thanks

    canvas = Magick::Image.new(600, 18){ self.background_color = '#212121' }
    gc = Magick::Draw.new
    gc.fill('white')
    gc.font = ("lib/fonts/AvenirLTStd-Book.otf")
    gc.pointsize = 18.0
    @title = "hello world"
    gc.text(0, 14, @title)
    gc.draw(canvas)
    canvas.format = 'png'
    canvas.to_blob 

Solution

  • Have a look at get_type_metrics:

    http://www.simplesystems.org/RMagick/doc/draw.html#get_type_metrics

    In practice you have to ask how big the text will be, then draw it, as two separate operations.