Sha256: 2840f6b34e9104bc9ae19825d4a83f8fdaef6fec2dad64952f6dd1de6236605c

Contents?: true

Size: 1.82 KB

Versions: 6

Compression:

Stored size: 1.82 KB

Contents

class Prawn::SVG::Document
  Error = Class.new(StandardError)
  InvalidSVGData = Class.new(Error)

  DEFAULT_FALLBACK_FONT_NAME = "Times-Roman"

  # An +Array+ of warnings that occurred while parsing the SVG data.
  attr_reader :warnings

  attr_reader :root,
    :sizing,
    :fallback_font_name,
    :font_registry,
    :url_loader,
    :elements_by_id, :gradients,
    :element_styles

  def initialize(data, bounds, options, font_registry: nil, css_parser: CssParser::Parser.new)
    @root = REXML::Document.new(data).root

    if @root.nil?
      if data.respond_to?(:end_with?) && data.end_with?(".svg")
        raise InvalidSVGData, "The data supplied is not a valid SVG document.  It looks like you've supplied a filename instead; use IO.read(filename) to get the data before you pass it to prawn-svg."
      else
        raise InvalidSVGData, "The data supplied is not a valid SVG document."
      end
    end

    @warnings = []
    @options = options
    @elements_by_id = {}
    @gradients = {}
    @fallback_font_name = options.fetch(:fallback_font_name, DEFAULT_FALLBACK_FONT_NAME)
    @font_registry = font_registry

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

    @sizing = Prawn::SVG::Calculators::DocumentSizing.new(bounds, @root.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

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
prawn-svg-0.32.0 lib/prawn/svg/document.rb
prawn-svg-0.31.0 lib/prawn/svg/document.rb
prawn-svg-0.30.0 lib/prawn/svg/document.rb
prawn-svg-0.29.1 lib/prawn/svg/document.rb
prawn-svg-0.29.0 lib/prawn/svg/document.rb
prawn-svg-0.28.0 lib/prawn/svg/document.rb