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

Class Attribute Summary collapse

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:



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/prawn/svg/document.rb', line 26

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)
  @external_svg_cache = {}
  @fallback_font_name = options.fetch(:fallback_font_name, DEFAULT_FALLBACK_FONT_NAME)
  @font_registry = font_registry
  @color_mode = load_color_mode
  @font_face_tempfiles = []

  if !options.key?(:enable_web_requests) && !self.class.enable_web_requests_warned
    self.class.enable_web_requests_warned = true
    warn '[prawn-svg] WARNING: :enable_web_requests is not set and currently defaults to true. ' \
         'In prawn-svg 1.0, this will default to false. ' \
         'Please explicitly pass enable_web_requests: true or enable_web_requests: false to suppress this warning.'
  end

  @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])

  stylesheets = Prawn::SVG::CSS::Stylesheets.new(css_parser, root, url_loader: url_loader, warnings: warnings)
  @element_styles = stylesheets.load
  process_font_face_rules(stylesheets.font_face_rules)

  yield self if block_given?
end

Class Attribute Details

.enable_web_requests_warnedObject

Returns the value of attribute enable_web_requests_warned.



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

def enable_web_requests_warned
  @enable_web_requests_warned
end

Instance Attribute Details

#color_modeObject (readonly)

Returns the value of attribute color_mode.



16
17
18
# File 'lib/prawn/svg/document.rb', line 16

def color_mode
  @color_mode
end

#element_stylesObject (readonly)

Returns the value of attribute element_styles.



16
17
18
# File 'lib/prawn/svg/document.rb', line 16

def element_styles
  @element_styles
end

#elements_by_idObject (readonly)

Returns the value of attribute elements_by_id.



16
17
18
# File 'lib/prawn/svg/document.rb', line 16

def elements_by_id
  @elements_by_id
end

#external_svg_cacheObject (readonly)

Returns the value of attribute external_svg_cache.



16
17
18
# File 'lib/prawn/svg/document.rb', line 16

def external_svg_cache
  @external_svg_cache
end

#fallback_font_nameObject (readonly)

Returns the value of attribute fallback_font_name.



16
17
18
# File 'lib/prawn/svg/document.rb', line 16

def fallback_font_name
  @fallback_font_name
end

#font_registryObject (readonly)

Returns the value of attribute font_registry.



16
17
18
# File 'lib/prawn/svg/document.rb', line 16

def font_registry
  @font_registry
end

#gradientsObject (readonly)

Returns the value of attribute gradients.



16
17
18
# File 'lib/prawn/svg/document.rb', line 16

def gradients
  @gradients
end

#rootObject (readonly)

Returns the value of attribute root.



16
17
18
# File 'lib/prawn/svg/document.rb', line 16

def root
  @root
end

#sizingObject (readonly)

Returns the value of attribute sizing.



16
17
18
# File 'lib/prawn/svg/document.rb', line 16

def sizing
  @sizing
end

#url_loaderObject (readonly)

Returns the value of attribute url_loader.



16
17
18
# File 'lib/prawn/svg/document.rb', line 16

def url_loader
  @url_loader
end

#warningsObject (readonly)

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



14
15
16
# File 'lib/prawn/svg/document.rb', line 14

def warnings
  @warnings
end

Instance Method Details

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



69
70
71
72
73
# File 'lib/prawn/svg/document.rb', line 69

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

#with_sizing(temporary_sizing) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/prawn/svg/document.rb', line 75

def with_sizing(temporary_sizing)
  original = @sizing
  @sizing = temporary_sizing
  yield
ensure
  @sizing = original
end