views:

313

answers:

1

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
+2  A: 

Have a look at get_type_metrics:

http://www.simplesystems.org/RMagick/doc/draw.html#get%5Ftype%5Fmetrics

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

tadman
thanks this was right on the button ill post code below as to how i extracted the text width using your answer
ADAM
metrics = gc.get_type_metrics(@title) puts metrics[3] # <<< this is my text width
ADAM