class Phantom::SVG::Parser::SVGReader
SVG
reader.
Public Instance Methods
read(path, options = {})
click to toggle source
Read svg file from path.
# File lib/phantom/parser/svg_reader.rb, line 13 def read(path, options = {}) reset return if path.nil? || path.empty? @path = path @marker = Digest::MD5.hexdigest(open(@path).read) @root = REXML::Document.new(open(path)) make_ids_unique() if options[:unique_ids] == true if @root.elements['svg'].attributes['id'] == 'phantom_svg' read_animation_svg(options) @has_animation = true else read_svg(options) @has_animation = false end end
Private Instance Methods
_replace_id_refs(id, new_id)
click to toggle source
# File lib/phantom/parser/svg_reader.rb, line 49 def _replace_id_refs(id, new_id) @root.elements.each("//*") do |element| element.attributes['style'].gsub!(/url\(\##{id}\)/, "url(\##{new_id})") if element.attributes['style'] != nil element.attributes['fill'].gsub!(/\##{id}$/, "\##{new_id}") if element.attributes['fill'] != nil element.attributes['xlink:href'].gsub!(/\##{id}$/, "\##{new_id}") if element.attributes['xlink:href'] != nil end end
choice_value(val, override)
click to toggle source
Helper method. Return val if override is nil. Return override if override is not nil.
# File lib/phantom/parser/svg_reader.rb, line 160 def choice_value(val, override) override.nil? ? val : override end
make_id_unique(element_type)
click to toggle source
# File lib/phantom/parser/svg_reader.rb, line 40 def make_id_unique(element_type) @root.elements.each("//#{element_type}") do |element| id = element.attributes['id'] new_id = id + @marker element.attributes['id'] = new_id _replace_id_refs(id, new_id) end end
make_ids_unique(element_types = ['linearGradient', 'radialGradient'])
click to toggle source
Make ID's and their references unique so they don't collide with other layers/frames.
# File lib/phantom/parser/svg_reader.rb, line 36 def make_ids_unique(element_types = ['linearGradient', 'radialGradient']) element_types.each { |element_type| make_id_unique(element_type) } end
read_animation_svg(options)
click to toggle source
Read animation svg.
# File lib/phantom/parser/svg_reader.rb, line 65 def read_animation_svg(options) svg = @root.elements['svg'] defs = svg.elements['defs'] read_size(svg, self, options) read_images(defs, options) read_skip_first read_durations(options) read_loops end
read_durations(options)
click to toggle source
Read frame durations.
# File lib/phantom/parser/svg_reader.rb, line 139 def read_durations(options) symbol = @root.elements['svg/defs/symbol'] return if symbol.nil? i = @skip_first ? 1 : 0 symbol.elements.each('use') do |use| duration = use.elements['set'].attributes['dur'].to_f @frames[i].duration = choice_value(duration, options[:duration]) i += 1 end end
read_images(parent_node, options)
click to toggle source
Read images from svg.
# File lib/phantom/parser/svg_reader.rb, line 110 def read_images(parent_node, options) parent_node.elements.each('svg') do |svg| new_frame = Phantom::SVG::Frame.new # Read namespaces. new_frame.namespaces = svg.namespaces.clone new_frame.namespaces.merge!(options[:namespaces]) unless options[:namespaces].nil? # Read image size. read_size(svg, new_frame, options) # Read image surfaces. new_frame.surfaces = choice_value(svg.elements.to_a, options[:surfaces]) # Read frame duration. new_frame.duration = choice_value(new_frame.duration, options[:duration]) # Add frame to array. @frames << new_frame end end
read_loops()
click to toggle source
Read animation loop count.
# File lib/phantom/parser/svg_reader.rb, line 152 def read_loops animate = @root.elements['svg/animate'] @loops = animate.nil? ? 0 : animate.attributes['repeatCount'].to_i end
read_size(node, dest, options = {})
click to toggle source
Read size from node to dest.
# File lib/phantom/parser/svg_reader.rb, line 77 def read_size(node, dest, options = {}) set_view_box(node, dest, options) set_width(node, dest, options) set_height(node, dest, options) end
read_skip_first()
click to toggle source
Read skip_first.
# File lib/phantom/parser/svg_reader.rb, line 133 def read_skip_first use = @root.elements['svg/defs/symbol/use'] @skip_first = use.nil? ? @frames.size > 1 : use.attributes['xlink:href'] != '#frame0' end
read_svg(options)
click to toggle source
Read no animation svg.
# File lib/phantom/parser/svg_reader.rb, line 58 def read_svg(options) read_images(@root, options) @width = @frames.first.width @height = @frames.first.height end
set_height(node, dest, options = {})
click to toggle source
# File lib/phantom/parser/svg_reader.rb, line 99 def set_height(node, dest, options = {}) if node.attributes['height'].nil? dest.instance_variable_set(:@height, choice_value("#{dest.viewbox.height}px", options[:height])) else dest.instance_variable_set(:@height, choice_value(node.attributes['height'], options[:height])) end end
set_view_box(node, dest, options = {})
click to toggle source
# File lib/phantom/parser/svg_reader.rb, line 83 def set_view_box(node, dest, options = {}) return if node.attributes['viewBox'].nil? dest.viewbox.set_from_text(choice_value(node.attributes['viewBox'], options[:viewbox]).to_s) end
set_width(node, dest, options = {})
click to toggle source
# File lib/phantom/parser/svg_reader.rb, line 89 def set_width(node, dest, options = {}) if node.attributes['width'].nil? dest.instance_variable_set(:@width, choice_value("#{dest.viewbox.width}px", options[:width])) else dest.instance_variable_set(:@width, choice_value(node.attributes['width'], options[:width])) end end