Sha256: cde98e819017d0d1fbf7355374bbccd324cc75ff8593ef8e923367bb8ae992ef

Contents?: true

Size: 1.31 KB

Versions: 31

Compression:

Stored size: 1.31 KB

Contents

#! /usr/local/bin/ruby -w
require 'RMagick'

include Magick

TEXT = 'get\nmultiline\ntype\nmetrics'

background = Image.new(200, 200)
gc = Draw.new

# Draw the text centered on the background
gc.annotate(background, 0, 0, 0, 0, TEXT) do |gc|
    gc.font_family = 'Verdana'
    gc.pointsize = 36
    gc.gravity = CenterGravity
    gc.stroke = 'none'
end

begin

    # Get the metrics
    metrics = gc.get_multiline_type_metrics(background, TEXT)

    # Compute the corners for a rectangle surrounding the text
    x = (background.columns - metrics.width) / 2
    y = (background.rows - metrics.height) / 2

    # Draw 2 rectangles over the text.
    gc = Draw.new
    gc.stroke('red')
    gc.stroke_width(5)
    gc.stroke_linejoin('round')
    gc.fill('cyan')
    gc.fill_opacity(0.10)
    gc.rectangle(x, y, x+metrics.width, y+metrics.height)

    gc.stroke('white')
    gc.stroke_width(1)
    gc.fill('none')
    gc.rectangle(x, y, x+metrics.width, y+metrics.height)
    gc.draw(background)

    background.border!(1,1, 'blue')

# Substitute the standard "Not Implemented" image
rescue NotImplementedError
    not_implemented = Magick::Image.read("images/notimplemented.gif").first
    not_implemented.resize!(background.columns, background.rows)
    background = not_implemented
end

background.write('get_multiline_type_metrics.gif')

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
rmagick-1.10.0 doc/ex/get_multiline_type_metrics.rb
rmagick-1.10.1 doc/ex/get_multiline_type_metrics.rb
rmagick-1.13.0 doc/ex/get_multiline_type_metrics.rb
rmagick-1.11.0 doc/ex/get_multiline_type_metrics.rb
rmagick-1.11.1 doc/ex/get_multiline_type_metrics.rb
rmagick-1.12.0 doc/ex/get_multiline_type_metrics.rb
rmagick-1.15.1 doc/ex/get_multiline_type_metrics.rb
rmagick-1.14.0 doc/ex/get_multiline_type_metrics.rb
rmagick-1.14.1 doc/ex/get_multiline_type_metrics.rb
rmagick-1.15.0 doc/ex/get_multiline_type_metrics.rb
rmagick-1.15.10 doc/ex/get_multiline_type_metrics.rb
rmagick-1.15.3 doc/ex/get_multiline_type_metrics.rb
rmagick-1.15.2 doc/ex/get_multiline_type_metrics.rb
rmagick-1.15.4 doc/ex/get_multiline_type_metrics.rb
rmagick-1.15.8 doc/ex/get_multiline_type_metrics.rb
rmagick-1.15.5 doc/ex/get_multiline_type_metrics.rb
rmagick-1.15.6 doc/ex/get_multiline_type_metrics.rb
rmagick-1.15.7 doc/ex/get_multiline_type_metrics.rb
rmagick-1.15.9 doc/ex/get_multiline_type_metrics.rb
rmagick-1.7.1 doc/ex/get_multiline_type_metrics.rb