lib/prawn/svg/document.rb in prawn-svg-0.22.1 vs lib/prawn/svg/document.rb in prawn-svg-0.23.0
- old
+ new
@@ -1,6 +1,9 @@
class Prawn::SVG::Document
+ Error = Class.new(StandardError)
+ InvalidSVGData = Class.new(Error)
+
begin
require 'css_parser'
CSS_PARSER_LOADED = true
rescue LoadError
CSS_PARSER_LOADED = false
@@ -8,28 +11,44 @@
DEFAULT_FALLBACK_FONT_NAME = "Times-Roman"
# An +Array+ of warnings that occurred while parsing the SVG data.
attr_reader :warnings
- attr_writer :url_cache
attr_reader :root,
:sizing,
- :cache_images, :fallback_font_name,
+ :fallback_font_name,
+ :font_registry,
+ :url_loader,
:css_parser, :elements_by_id, :gradients
- def initialize(data, bounds, options)
+ def initialize(data, bounds, options, font_registry: nil)
@css_parser = CssParser::Parser.new if CSS_PARSER_LOADED
@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 = {}
- @cache_images = options[:cache_images]
@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)
sizing.requested_width = options[:width]
sizing.requested_height = options[:height]
sizing.calculate
@@ -50,11 +69,7 @@
value && points(value, axis)
end
def points(value, axis = nil)
Prawn::SVG::Calculators::Pixels.to_pixels(value, @axis_to_size.fetch(axis, sizing.viewport_diagonal))
- end
-
- def url_loader
- @url_loader ||= Prawn::SVG::UrlLoader.new(:enable_cache => cache_images)
end
end