Class: Prawn::SVG::Document

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

Constant Summary collapse

Error =
Class.new(StandardError)
InvalidSVGData =
Class.new(Error)
DEFAULT_FALLBACK_FONT_NAME =
'Times-Roman'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, bounds, options, font_registry: nil, css_parser: CssParser::Parser.new, attribute_overrides: {}) {|_self| ... } ⇒ Document

Returns a new instance of Document.

Yields:

  • (_self)

Yield Parameters:



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
# File 'lib/prawn/svg/document.rb', line 19

def initialize(data, bounds, options, font_registry: nil, css_parser: CssParser::Parser.new, attribute_overrides: {})
  begin
    @root = REXML::Document.new(data).root or raise_parse_error(data)
  rescue REXML::ParseException => e
    raise_parse_error(data, e.message)
  end

  @warnings = []
  @options = options
  @elements_by_id = {}
  @gradients = Prawn::SVG::Gradients.new(self)
  @fallback_font_name = options.fetch(:fallback_font_name, DEFAULT_FALLBACK_FONT_NAME)
  @font_registry = font_registry
  @color_mode = load_color_mode

  @url_loader = Prawn::SVG::UrlLoader.new(
    enable_cache:          options[:cache_images],
    enable_web:            options.fetch(:enable_web_requests, true),
    enable_file_with_root: options[:enable_file_requests_with_root]
  )

  attributes = @root.attributes.dup
  attribute_overrides.each { |key, value| attributes.add(REXML::Attribute.new(key, value)) }

  @sizing = Prawn::SVG::Calculators::DocumentSizing.new(bounds, attributes)
  calculate_sizing(requested_width: options[:width], requested_height: options[:height])

  @element_styles = Prawn::SVG::CSS::Stylesheets.new(css_parser, root).load

  yield self if block_given?
end

Instance Attribute Details

#color_modeObject (readonly)

Returns the value of attribute color_mode.



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

def color_mode
  @color_mode
end

#element_stylesObject (readonly)

Returns the value of attribute element_styles.



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

def element_styles
  @element_styles
end

#elements_by_idObject (readonly)

Returns the value of attribute elements_by_id.



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

def elements_by_id
  @elements_by_id
end

#fallback_font_nameObject (readonly)

Returns the value of attribute fallback_font_name.



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

def fallback_font_name
  @fallback_font_name
end

#font_registryObject (readonly)

Returns the value of attribute font_registry.



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

def font_registry
  @font_registry
end

#gradientsObject (readonly)

Returns the value of attribute gradients.



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

def gradients
  @gradients
end

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

#sizingObject (readonly)

Returns the value of attribute sizing.



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

def sizing
  @sizing
end

#url_loaderObject (readonly)

Returns the value of attribute url_loader.



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

def url_loader
  @url_loader
end

#warningsObject (readonly)

An Array of warnings that occurred while parsing the SVG data.



8
9
10
# File 'lib/prawn/svg/document.rb', line 8

def warnings
  @warnings
end

Instance Method Details

#calculate_sizing(requested_width: nil, requested_height: nil) ⇒ Object



51
52
53
54
55
# File 'lib/prawn/svg/document.rb', line 51

def calculate_sizing(requested_width: nil, requested_height: nil)
  sizing.requested_width = requested_width
  sizing.requested_height = requested_height
  sizing.calculate
end