class Prawn::SVG::Document
Constants
- DEFAULT_FALLBACK_FONT_NAME
- Error
- InvalidSVGData
Attributes
An Array
of warnings that occurred while parsing the SVG data.
Public Class Methods
Source
# 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
Public Instance Methods
Source
# 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
Private Instance Methods
Source
# File lib/prawn/svg/document.rb, line 59 def load_color_mode case @options[:color_mode] when nil, :rgb then :rgb when :cmyk then :cmyk else raise ArgumentError, ':color_mode must be set to :rgb (default) or :cmyk' end end
Source
# File lib/prawn/svg/document.rb, line 68 def raise_parse_error(data, exception_message = nil) message = 'The data supplied is not a valid SVG document.' if data.respond_to?(:end_with?) && data.end_with?('.svg') message += " It looks like you've supplied a filename instead; use File.read(filename) to get the data from the file before you pass it to prawn-svg." end message += "\n#{exception_message}" if exception_message raise InvalidSVGData, message end