class Magick::RVG::Image

Public Class Methods

new(image, width = nil, height = nil, x = 0, y = 0) click to toggle source

Composite a raster image in the viewport defined by [x,y] and width and height. Use the RVG::ImageConstructors#image method to create Text objects in a container.

Calls superclass method Magick::RVG::Describable::new
# File lib/rvg/embellishable.rb, line 220
def initialize(image, width = nil, height = nil, x = 0, y = 0)
  super()             # run module initializers
  @image = image.copy # use a copy of the image in case app. re-uses the argument
  @x, @y, @width, @height = Magick::RVG.convert_to_float(x, y, width || @image.columns, height || @image.rows)
  raise ArgumentError, 'width, height must be >= 0' if @width < 0 || @height < 0

  init_viewbox
end

Public Instance Methods

add_primitives(gc) click to toggle source

@private

# File lib/rvg/embellishable.rb, line 230
def add_primitives(gc)
  # Do not render if width or height is 0
  return if @width.zero? || @height.zero?

  gc.push
  add_transform_primitives(gc)
  add_style_primitives(gc)
  add_composite_primitive(gc)
  gc.pop
end

Private Instance Methods

add_composite_primitive(gc) click to toggle source
# File lib/rvg/embellishable.rb, line 182
def add_composite_primitive(gc)
  if @align == 'none'
    # Let RMagick do the scaling
    scale = 1.0
    width = @width
    height = @height
  elsif @meet_or_slice == 'meet'
    scale = [@width / @image.columns, @height / @image.rows].min || 1.0
    width = @image.columns
    height = @image.rows
  else
    # Establish clipping path around the current viewport
    name = __id__.to_s
    gc.define_clip_path(name) do
      gc.path("M#{@x},#{@y} l#{@width},0 l0,#{@height} l-#{@width},0 l0,-#{@height}z")
    end

    gc.clip_path(name)
    scale = [@width / @image.columns, @height / @image.rows].max || 1.0
    width = @image.columns
    height = @image.rows
  end
  tx, ty = align_to_viewport(scale)
  gc.composite(@x + tx, @y + ty, width * scale, height * scale, @image)
end
align_to_viewport(scale) click to toggle source
# File lib/rvg/embellishable.rb, line 157
def align_to_viewport(scale)
  tx = case @align
       when 'none', /\AxMin/
         0
       when NilClass, /\AxMid/
         (@width - @image.columns * scale) / 2.0
       when /\AxMax/
         @width - @image.columns * scale
       else
         0
       end

  ty = case @align
       when 'none', /YMin\z/
         0
       when NilClass, /YMid\z/
         (@height - @image.rows * scale) / 2.0
       when /YMax\z/
         @height - @image.rows * scale
       else
         0
       end
  [tx, ty]
end
init_viewbox() click to toggle source
# File lib/rvg/embellishable.rb, line 208
def init_viewbox
  @align = nil
  @vbx_width = @image.columns
  @vbx_height = @image.rows
  @meet_or_slice = 'meet'
end