create_sprite(images, width, height)
click to toggle source
def create_sprite(images, width, height)
library.create(output_image_file, images, width, height)
pngcrush(output_image_file)
end
css_url()
click to toggle source
def css_url
base = File.basename(output_image_file)
custom = config[:cssurl]
if custom
if custom.is_a?(Proc)
custom.call(base)
elsif custom.include?('$IMAGE')
custom.sub('$IMAGE', base)
else
"url(#{File.join(custom, base)})"
end
else
"url(#{base})"
end
end
custom_style_file()
click to toggle source
def custom_style_file
File.join(input, File.basename(input) + ".#{style_name}")
end
height()
click to toggle source
def height
config[:height]
end
hmargin()
click to toggle source
def hmargin
config[:hmargin] || config[:margin] || 0
end
hpadding()
click to toggle source
def hpadding
config[:hpadding] || config[:padding] || 0
end
image_files()
click to toggle source
def image_files
return [] if input.nil?
valid_extensions = library::VALID_EXTENSIONS
expansions = Array(valid_extensions).map{|ext| File.join(input, "**", "*.#{ext}")}
SpriteFactory.find_files(*expansions)
end
image_name_without_pseudo_class(image)
click to toggle source
def image_name_without_pseudo_class(image)
image[:name].split(':').first
end
image_pseudo_class(image)
click to toggle source
def image_pseudo_class(image)
image[:name].slice(/:.*?\Z/)
end
image_pseudo_class_priority(image)
click to toggle source
def image_pseudo_class_priority(image)
PSEUDO_CLASS_ORDER.index(image_pseudo_class(image))
end
layout_images(images)
click to toggle source
def layout_images(images)
layout_strategy.layout(images, :width => width, :height => height, :hpadding => hpadding, :vpadding => vpadding, :hmargin => hmargin, :vmargin => vmargin)
end
layout_name()
click to toggle source
def layout_name
config[:layout]
end
layout_strategy()
click to toggle source
def layout_strategy
@layout_strategy ||= Layout.send(layout_name)
end
library()
click to toggle source
def library
@library ||= Library.send(library_name)
end
library_name()
click to toggle source
def library_name
config[:library]
end
load_images()
click to toggle source
def load_images
input_path = Pathname.new(input)
images = library.load(image_files)
images.each do |i|
i[:name], i[:ext] = map_image_filename(i[:filename], input_path)
raise RuntimeError, "image #{i[:name]} does not fit within a fixed width of #{width}" if width && (width < i[:width])
raise RuntimeError, "image #{i[:name]} does not fit within a fixed height of #{height}" if height && (height < i[:height])
end
images.sort_by {|i| [image_name_without_pseudo_class(i), image_pseudo_class_priority(i)] }
end
map_image_filename(filename, input_path)
click to toggle source
def map_image_filename(filename, input_path)
name = Pathname.new(filename).relative_path_from(input_path).to_s.gsub(File::SEPARATOR, "_")
name = name.gsub('--', ':')
name = name.gsub('__', ' ')
ext = File.extname(name)
name = name[0...-ext.length] unless ext.empty?
[name, ext]
end
nocss?()
click to toggle source
def nocss?
config[:nocss]
end
output()
click to toggle source
def output
config[:output] || input
end
output_image_file()
click to toggle source
def output_image_file
config[:output_image] || "#{output}.png"
end
output_style_file()
click to toggle source
def output_style_file
config[:output_style] || "#{output}.#{style_name}"
end
pngcrush(image)
click to toggle source
def pngcrush(image)
if SUPPORTS_PNGCRUSH && config[:pngcrush]
crushed = "#{image}.crushed"
%xpngcrush -rem alla -reduce -brute #{image} #{crushed}`
FileUtils.mv(crushed, image)
end
end
report(msg)
click to toggle source
def report(msg)
puts msg if config[:report]
end
report_path(path)
click to toggle source
def report_path(path)
@cwd ||= Pathname.new(File.expand_path('.'))
path = Pathname.new(path)
path = path.relative_path_from(@cwd) if path.absolute?
path.to_s
end
selector()
click to toggle source
def selector
config[:selector]
end
style(selector, url, images) { |inject({}) {|h,i| h[i = i; h}| ... }
click to toggle source
def style(selector, url, images, &block)
defaults = Style.generate(style_name, selector, url, images)
if block_given?
yield images.inject({}) {|h,i| h[i[:name].to_sym] = i; h}
else
defaults
end
end
style_name()
click to toggle source
def style_name
config[:style]
end
summary(images, max)
click to toggle source
def summary(images, max)
return <<-EOF
Creating a sprite from following images:
\n#{images.map{|i| " #{report_path(i[:filename])} (#{i[:width]}x#{i[:height]})" }.join("\n")}
Output files:
#{report_path(output_image_file)}
#{report_path(output_style_file)}
Output size:
#{max[:width]}x#{max[:height]}
EOF
end
vmargin()
click to toggle source
def vmargin
config[:vmargin] || config[:margin] || 0
end
vpadding()
click to toggle source
def vpadding
config[:vpadding] || config[:padding] || 0
end
width()
click to toggle source
def width
config[:width]
end