Class: Prawn::SVG::Elements::Image

Inherits:
Base
  • Object
show all
Defined in:
lib/prawn/svg/elements/image.rb

Defined Under Namespace

Classes: FakeIO

Constant Summary

Constants inherited from Base

Base::COMMA_WSP_REGEXP, Base::MissingAttributesError, Base::PAINT_TYPES, Base::SVG_NAMESPACE, Base::SkipElementError, Base::SkipElementQuietly

Constants included from Attributes::Stroke

Attributes::Stroke::CAP_STYLE_TRANSLATIONS, Attributes::Stroke::JOIN_STYLE_TRANSLATIONS

Instance Attribute Summary

Attributes inherited from Base

#attributes, #base_calls, #calls, #document, #parent_calls, #properties, #source, #state

Instance Method Summary collapse

Methods inherited from Base

#initialize, #name, #parse_and_apply, #process

Methods included from TransformParser

#parse_transform_attribute

Methods included from Attributes::Space

#parse_xml_space_attribute

Methods included from Attributes::Stroke

#parse_stroke_attributes_and_call

Methods included from Attributes::ClipPath

#parse_clip_path_attribute_and_call

Methods included from Attributes::Opacity

#parse_opacity_attributes_and_call

Methods included from Attributes::Transform

#parse_transform_attribute_and_call

Constructor Details

This class inherits a constructor from Prawn::SVG::Elements::Base

Instance Method Details

#applyObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/prawn/svg/elements/image.rb', line 50

def apply
  if @aspect.slice?
    add_call "save"
    add_call "rectangle", [@clip_x, @clip_y], @clip_width, @clip_height
    add_call "clip"
  end

  options = {:width => @width, :height => @height, :at => [@x, @y]}

  add_call "image", FakeIO.new(@image), options
  add_call "restore" if @aspect.slice?
end

#bounding_boxObject



63
64
65
# File 'lib/prawn/svg/elements/image.rb', line 63

def bounding_box
  [@x, @y, @x + @width, @y - @height]
end

#parseObject

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/prawn/svg/elements/image.rb', line 13

def parse
  require_attributes 'width', 'height'

  raise SkipElementQuietly if state.computed_properties.display == "none"

  @url = href_attribute
  if @url.nil?
    raise SkipElementError, "image tag must have an href or xlink:href"
  end

  x = x(attributes['x'] || 0)
  y = y(attributes['y'] || 0)
  width = x_pixels(attributes['width'])
  height = y_pixels(attributes['height'])

  raise SkipElementQuietly if width.zero? || height.zero?
  require_positive_value width, height

  @image = begin
    @document.url_loader.load(@url)
  rescue Prawn::SVG::UrlLoader::Error => e
    raise SkipElementError, "Error retrieving URL #{@url}: #{e.message}"
  end

  @aspect = Prawn::SVG::Calculators::AspectRatio.new(attributes['preserveAspectRatio'], [width, height], image_dimensions(@image))

  @clip_x = x
  @clip_y = y
  @clip_width = width
  @clip_height = height

  @width = @aspect.width
  @height = @aspect.height
  @x = x + @aspect.x
  @y = y - @aspect.y
end