Class: Prawn::SVG::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/svg/interface.rb

Constant Summary collapse

VALID_OPTIONS =
[:at, :position, :vposition, :width, :height, :cache_images, :enable_web_requests, :enable_file_requests_with_root, :fallback_font_name, :color_mode]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, prawn, options, &block) ⇒ Interface

Creates a Prawn::SVG object.

data is the SVG data to convert. prawn is your Prawn::Document object.

See README.md for the options that can be passed to this method.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/prawn/svg/interface.rb', line 19

def initialize(data, prawn, options, &block)
  Prawn.verify_options VALID_OPTIONS, options

  @data = data
  @prawn = prawn
  @options = options

  font_registry = Prawn::SVG::FontRegistry.new(prawn.font_families)

  @document = Document.new(data, [prawn.bounds.width, prawn.bounds.height], options, font_registry: font_registry, &block)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



10
11
12
# File 'lib/prawn/svg/interface.rb', line 10

def data
  @data
end

#documentObject (readonly)

Returns the value of attribute document.



10
11
12
# File 'lib/prawn/svg/interface.rb', line 10

def document
  @document
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/prawn/svg/interface.rb', line 10

def options
  @options
end

#prawnObject (readonly)

Returns the value of attribute prawn.



10
11
12
# File 'lib/prawn/svg/interface.rb', line 10

def prawn
  @prawn
end

Class Method Details

.font_pathObject

backwards support for when the font_path used to be stored on this class



69
70
71
# File 'lib/prawn/svg/interface.rb', line 69

def self.font_path # backwards support for when the font_path used to be stored on this class
  Prawn::SVG::FontRegistry.font_path
end

Instance Method Details

#drawObject

Draws the SVG to the Prawn::Document object.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/prawn/svg/interface.rb', line 34

def draw
  if @document.sizing.invalid?
    @document.warnings << "Zero or negative sizing data means this SVG cannot be rendered"
    return
  end

  @document.warnings.clear

  prawn.save_font do
    prawn.bounding_box(position, :width => @document.sizing.output_width, :height => @document.sizing.output_height) do
      prawn.save_graphics_state do
        clip_rectangle 0, 0, @document.sizing.output_width, @document.sizing.output_height

        calls = []
        root_element = Prawn::SVG::Elements::Root.new(@document, @document.root, calls)
        root_element.process

        proc_creator(prawn, calls).call
      end
    end
  end
end

#positionObject



65
66
67
# File 'lib/prawn/svg/interface.rb', line 65

def position
  @options[:at] || [x_based_on_requested_alignment, y_based_on_requested_alignment]
end

#resize(width: nil, height: nil) ⇒ Object



61
62
63
# File 'lib/prawn/svg/interface.rb', line 61

def resize(width: nil, height: nil)
  document.calculate_sizing(requested_width: width, requested_height: height)
end

#sizingObject



57
58
59
# File 'lib/prawn/svg/interface.rb', line 57

def sizing
  document.sizing
end