class Gradient::SVG

Constants

SVGNS

Attributes

maps[R]

Public Class Methods

new() click to toggle source
# File lib/gradient/svg.rb, line 34
def initialize
  @maps = {}
end
open(file) click to toggle source
# File lib/gradient/svg.rb, line 28
def open(file)
  read(file)
end
parse(string_buffer) click to toggle source
# File lib/gradient/svg.rb, line 14
def parse(string_buffer)
  new.tap do |parser|
    parser.parse(string_buffer)
  end.maps
end
read(file) click to toggle source
# File lib/gradient/svg.rb, line 20
def read(file)
  new.tap do |parser|
    File.open(file, 'r') do |file|
      parser.parse(file.read)
    end
  end.maps
end

Public Instance Methods

parse(buffer) click to toggle source
# File lib/gradient/svg.rb, line 40
def parse(buffer)
  xml = Nokogiri::XML(buffer)
  (
    xml.xpath('//linearGradient') +
    xml.xpath('//xmlns:linearGradient', 'xmlns' => SVGNS)
  ).each do |linear_gradient|
    unless (id = linear_gradient['id']) then
      raise SVGError, 'linearGradient has no id'
    end
    unless (map = parse_linear_gradient(linear_gradient)).points.empty?
      @maps[id] = map
    end
  end
end

Private Instance Methods

parse_linear_gradient(linear_gradient) click to toggle source
# File lib/gradient/svg.rb, line 55
        def parse_linear_gradient(linear_gradient)
  map = Gradient::Map.new
  linear_gradient.children.each do |node|
    next unless node.name == 'stop'
    map.points << parse_stop(node)
  end
  map
end
parse_location(offset) click to toggle source
# File lib/gradient/svg.rb, line 92
        def parse_location(offset)
  unless (location = offset.scanf('%f%%')).count == 1 then
    raise SVGError, "failed parse of offset #{offset}"
  end
  location.first / 100.0
end
parse_stop(stop) click to toggle source
# File lib/gradient/svg.rb, line 64
        def parse_stop(stop)
  unless (offset = stop['offset']) then
    raise SVGError, 'stop has no offset'
  end
  location = parse_location(offset)
  if (style = stop['style']) then
    point_from_style(location, style)
  else
    point_from_stop(location, stop)
  end
end
parse_stop_color(stop_color) click to toggle source
# File lib/gradient/svg.rb, line 99
        def parse_stop_color(stop_color)
  parts =
    if (r, g, b = stop_color.scanf('rgb(%d,%d,%d)')).count == 3 then
      [Color::RGB.new(r, g, b), 1.0]
    elsif (r, g, b, a = stop_color.scanf('rgba(%d,%d,%d,%f)')).count == 4 then
      [Color::RGB.new(r, g, b), a]
    elsif (color = Color::RGB.by_css(stop_color)) then
      [color, 1.0]
    end
  unless parts then
    raise SVGError, "failed parse of stop-color #{stop_color}"
  end
  parts
end
parse_stop_opacity(stop_opacity) click to toggle source
# File lib/gradient/svg.rb, line 114
        def parse_stop_opacity(stop_opacity)
  stop_opacity.scanf('%f').first
end
point_from_stop(location, stop) click to toggle source
# File lib/gradient/svg.rb, line 81
        def point_from_stop(location, stop)
  unless (stop_color = stop['stop-color']) then
    raise SVGError, 'stop has no stop-color'
  end
  color, opacity = parse_stop_color(stop_color)
  if (stop_opacity = stop['stop-opacity']) then
    opacity = parse_stop_opacity(stop_opacity)
  end
  Gradient::Point.new(location, color, opacity)
end
point_from_style(location, style) click to toggle source
# File lib/gradient/svg.rb, line 76
        def point_from_style(location, style)
  stop = Hash[ style.split(/;/).map { |item| item.split(/:/) } ]
  point_from_stop(location, stop)
end