class SvgSprite

Public Class Methods

get_height() click to toggle source
# File lib/compass-svg-sprite/core.rb, line 143
def self.get_height
  return @@height
end
get_map() click to toggle source
# File lib/compass-svg-sprite/core.rb, line 135
def self.get_map
  return hash_to_map(@@map)
end
get_width() click to toggle source
# File lib/compass-svg-sprite/core.rb, line 139
def self.get_width
  return @@width
end
new(config) click to toggle source
# File lib/compass-svg-sprite/core.rb, line 5
def initialize(config)
  @@map = Hash.new
  @@config = Hash.new
  @@svgs = Hash.new
  @@width = 0
  @@height = 0

  init_config(config)
  init_sprite
  write_sprite
end

Public Instance Methods

change_icon_color(svg, color) click to toggle source
# File lib/compass-svg-sprite/core.rb, line 99
def change_icon_color(svg, color)
  case @@config['color_replace_mode']
    when 'stroke'
      svg = svg.gsub(/(\sstroke=".*?")/im, " stroke=\"#{color}\"")
    when 'fill'
      svg = svg.gsub(/(\sfill=".*?")/im, " fill=\"#{color}\"")
    when 'both'
      svg = svg.gsub(/(\sstroke=".*?")/im, " stroke=\"#{color}\"")
      svg = svg.gsub(/(\sfill=".*?")/im, " fill=\"#{color}\"")
  end
  return svg
end
change_icon_position(svg, width, height, x, y, id) click to toggle source
# File lib/compass-svg-sprite/core.rb, line 54
def change_icon_position(svg, width, height, x, y, id)
  svg = svg.gsub(/(<svg.*?)(\swidth=".*?")(.*?>)/im, '\1\3')
  svg = svg.gsub(/(<svg.*?)(\sheight=".*?")(.*?>)/im, '\1\3')
  svg = svg.gsub(/(<svg.*?)(\sviewbox=".*?")(.*?>)/im, '\1\3')
  svg = svg.gsub(/(<svg.*?)(\sid=".*?")(.*?>)/im, '\1\3')
  svg = svg.gsub(/(<svg.*?)(\sx=".*?")(.*?>)/im, '\1\3')
  svg = svg.gsub(/(<svg.*?)(\sy=".*?")(.*?>)/im, '\1\3')
  svg = svg.gsub(/(<svg.*?)(\senable-background=".*?")(.*?>)/im, '\1\3')
  svg = svg.gsub(/^(<svg)/mi, "<svg width=\"#{width}\" height=\"#{height}\" viewBox=\"0 0 #{width} #{height}\" enable-background=\"new 0 0 #{width} #{height}\" id=\"#{id}\" x=\"#{x}\" y=\"#{y}\"")
  return svg
end
get_contents(real_path) click to toggle source
# File lib/compass-svg-sprite/core.rb, line 112
def get_contents(real_path)
  if File.readable?(real_path)
    File.open(real_path, "rb") {|io| io.read}
  else
    raise Compass::Error, "File not found or cannot be read: #{real_path}"
  end
end
get_icon_color_variants(fileName, svg, width, height) click to toggle source
# File lib/compass-svg-sprite/core.rb, line 81
def get_icon_color_variants(fileName, svg, width, height)
  hash = Hash.new
  @@width += @@config['padding']
  hash['default'] = { 'x' => @@width.to_s, 'y' => @@config['padding'].to_s}
  @@svgs[fileName]['default'] = change_icon_position(svg, width, height, @@width, @@config['padding'], fileName)
  @@width += width + @@config['padding']
  if @@config['icon_variants'].kind_of?(Hash) && @@config['icon_variants'][fileName].kind_of?(Hash)
    @@config['icon_variants'][fileName].each do |name, value|
      @@width += @@config['padding']
      hash[name] = { 'x' => @@width.to_s, 'y' => @@config['padding'].to_s}
      svg = change_icon_color(svg, value)
      @@svgs[fileName][name] = change_icon_position(svg, width, height, @@width, @@config['padding'], fileName + '-' + name)
      @@width += width + @@config['padding']
    end
  end
  return hash
end
get_svg_size(svg, kind) click to toggle source
# File lib/compass-svg-sprite/core.rb, line 66
def get_svg_size(svg, kind)
  size = svg.scan(/<svg.*? #{kind}="(.*?)".*?>/mi)
  if size[0].kind_of?(Array)
    size = size[0][0].to_f
  else
    viewBoxSizes = svg.scan(/<svg.*? viewbox="(.*?)"/mi)[0][0].split(" ")
    if kind === 'width'
      size = viewBoxSizes[2].to_f - viewBoxSizes[0].to_f
    else
      size = viewBoxSizes[3].to_f - viewBoxSizes[1].to_f
    end
  end
  return size.round
end
init_config(config) click to toggle source
# File lib/compass-svg-sprite/core.rb, line 17
def init_config(config)
  @@config = map_to_hash(config)
end
init_sprite() click to toggle source
# File lib/compass-svg-sprite/core.rb, line 21
def init_sprite
  svg_dir = File.join(Compass.configuration.images_path, @@config['icons_dir'], "*.svg")
  files = Dir.glob(svg_dir)
  files.each do |file|
    process_icon(file)
  end
end
optimize_svg(svg) click to toggle source
# File lib/compass-svg-sprite/core.rb, line 44
def optimize_svg(svg)
  svg = svg.gsub(/.*?<svg(.*?)>(.*?)<\/svg>.*?/mi, "<svg\\1>\\2</svg>")
  svg = svg.gsub(/(\sxmlns=".*?")/mi, "")
  svg = svg.gsub(/(\sxmlns:xlink=".*?")/mi, "")
  svg = svg.gsub(/(\sversion=".*?")/mi, "")
  svg = svg.gsub(/\n|\r|\t/mi, "")
  svg = svg.gsub(/\s\s+/mi, " ")
  return svg
end
process_icon(file) click to toggle source
# File lib/compass-svg-sprite/core.rb, line 29
def process_icon(file)
  fileName = File.basename(file, ".svg")
  svg = optimize_svg(get_contents(file))
  width = get_svg_size(svg, 'width')
  height = get_svg_size(svg, 'height')
  svg = change_icon_position(svg, width, height, @@width, @@config['padding'], fileName)
  @@svgs[fileName] = Hash.new
  @@map[fileName] = {
      'width' => width,
      'height' => height,
      'colors' => get_icon_color_variants(fileName, svg, width, height)
  }
  @@height = height + @@config['padding'] > @@height ? height + @@config['padding'] : @@height
end
write_sprite() click to toggle source
# File lib/compass-svg-sprite/core.rb, line 120
def write_sprite
  File.open(File.join(Compass.configuration.images_path, "sprite.svg"), "w") do |file|
    output = "<svg xmlns=\"http://www.w3.org/2000/svg\""
    output += " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
    output += " width=\"#{@@width}\" height=\"#{@@height}\" viewBox=\"0 0 #{@@width} #{@@height}\">\n"
    @@svgs.each do |icon, value|
      value.each do |color, svg|
        output += "\t" + svg + "\n"
      end
    end
    output += "</svg>"
    file.write(output)
  end
end