Skip to content

Commit

Permalink
Naive implementation of padding for the :packed layout.
Browse files Browse the repository at this point in the history
We simply add padding to the bottom and right of every image,
without trying to figure out if we are at the edge of the image
or not. This solves my own need for padding: getting rid of
display bugs on touch devices due to scaling (zooming) the
document. This is resolved by adding 1px of padding around all
images.
  • Loading branch information
nedbaldessin committed Mar 9, 2012
1 parent dd4bb49 commit acc40e9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/sprite_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class << self
attr_accessor :csspath
attr_accessor :pngcrush
attr_accessor :nocomments
attr_accessor :padding
end

#----------------------------------------------------------------------------
Expand Down
11 changes: 6 additions & 5 deletions lib/sprite_factory/layout/packed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ module Packed

def self.layout(images, options = {})

raise NotImplementedError, ":packed layout does not support the :padding option" if (options[:padding].to_i > 0) || (options[:hpadding].to_i > 0) || (options[:vpadding].to_i > 0)
raise NotImplementedError, ":packed layout does not support fixed :width/:height option" if options[:width] || options[:height]

options[:padding] ||= options[:hpadding]

return { :width => 0, :height => 0 } if images.empty?

images.sort! do |a,b|
Expand All @@ -19,14 +20,14 @@ def self.layout(images, options = {})
diff
end

root = { :x => 0, :y => 0, :w => images[0][:width], :h => images[0][:height] }
root = { :x => 0, :y => 0, :w => images[0][:width] + options[:padding], :h => images[0][:height] + options[:padding] }

images.each do |i|
if (node = findNode(root, i[:width], i[:height]))
if (node = findNode(root, i[:width] + options[:padding], i[:height] + options[:padding]))
placeImage(i, node)
splitNode(node, i[:width], i[:height])
splitNode(node, i[:width] + options[:padding], i[:height] + options[:padding])
else
root = grow(root, i[:width], i[:height])
root = grow(root, i[:width] + options[:padding], i[:height] + options[:padding])
redo
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/sprite_factory/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def initialize(input, config = {})
@config[:layout] ||= SpriteFactory.layout || :horizontal
@config[:library] ||= SpriteFactory.library || :rmagick
@config[:selector] ||= SpriteFactory.selector || 'img.'
@config[:padding] ||= SpriteFactory.padding || 0
@config[:csspath] ||= SpriteFactory.csspath
@config[:report] ||= SpriteFactory.report
@config[:pngcrush] ||= SpriteFactory.pngcrush
Expand Down

0 comments on commit acc40e9

Please sign in to comment.