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

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

Defined Under Namespace

Classes: FakeIO, ImageData

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 PDFMatrix

#load_matrix, #matrix_for_pdf, #rotation_matrix, #scale_matrix, #translation_matrix

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/prawn/svg/elements/image.rb', line 55

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

  if (document = @image_data.document)
    add_call_and_enter 'translate', @x, @y
    add_call 'svg:render_sub_document', document
  else
    options = { width: @width, height: @height, at: [@x, @y] }

    add_call 'image', FakeIO.new(@image), options
  end

  add_call 'restore' if @aspect.slice?
end

#bounding_boxObject



74
75
76
# File 'lib/prawn/svg/elements/image.rb', line 74

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

#parseObject

Raises:



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
49
50
51
52
53
# File 'lib/prawn/svg/elements/image.rb', line 16

def parse
  require_attributes 'width', 'height'

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

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

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

  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

  @image_data = process_image(@image, width, height, preserve_aspect_ratio)

  @aspect = Prawn::SVG::Calculators::AspectRatio.new(preserve_aspect_ratio, [width, height], @image_data.dimensions)

  @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