the-architect-sprite_generator 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -72,19 +72,17 @@ protected
72
72
 
73
73
  @analyzed.keys.sort.each do |key|
74
74
  value = @analyzed[key]
75
- if @tile
76
- context['left'] = @images.length > 0 ? @tile.columns : 0
77
- else
78
- context['left'] = @images.length > 0 ? @images.append(false).columns : 0
79
- end
75
+
80
76
  context['top'] = 0
81
77
  context['basename'] = key
82
78
  context['overall'] = @images.length
83
-
84
- value.size > 1 ? build_image_list(context, value.sort) : build_single_image(context, value.flatten.first)
79
+ if value.size > 1
80
+ context = build_image_list(context, value.sort)
81
+ else
82
+ context = build_single_image(context, value.flatten.first)
83
+ end
85
84
  @css << build_css(context)
86
85
  end
87
-
88
86
  [@images.append(false), @css.join("\n")]
89
87
  end
90
88
 
@@ -94,12 +92,12 @@ protected
94
92
  image = Image.read(filename){ self.background_color = background }.flatten.first
95
93
 
96
94
  context.merge!(build_context_for_single_image(image, filename, context['basename']))
97
-
98
95
  if @tile
99
96
  @images.from_blob(@tile.composite(image, @alignment, Magick::OverCompositeOp).to_blob){ self.background_color = background }
100
97
  else
101
98
  @images.from_blob(image.to_blob){ self.background_color = background }
102
99
  end
100
+ context
103
101
  end
104
102
 
105
103
 
@@ -114,14 +112,21 @@ protected
114
112
  'variation_name' => file_basename.gsub(/^#{basename}#{@delimiter}/, ''),
115
113
  'width' => @tile ? @tile.columns : image.columns,
116
114
  'height' => @tile ? @tile.rows : image.rows,
117
- 'type' => :image
115
+ 'type' => :image,
116
+ 'left' => left_value_for_context
118
117
  }
119
118
  end
120
119
 
121
120
 
121
+ def left_value_for_context
122
+ @tile ? (@images.length * @tile.columns) : (@images.any? ? @images.append(false).columns : 0)
123
+ end
124
+
125
+
122
126
  def build_image_list(context, image_filenames)
123
127
  background = @background
124
128
  image_list = ImageList.new(*image_filenames){ self.background_color = background }
129
+ context.merge!(build_context_for_image_list(image_list, image_filenames))
125
130
  if @tile
126
131
  tiles = ImageList.new{ self.background_color = background }
127
132
  image_list.each do |image|
@@ -131,7 +136,7 @@ protected
131
136
  else
132
137
  append_to_sprite(image_list)
133
138
  end
134
- context.merge!(build_context_for_image_list(image_list, image_filenames))
139
+ context
135
140
  end
136
141
 
137
142
 
@@ -146,7 +151,8 @@ protected
146
151
  'variations' => image_list.length,
147
152
  'type' => :list,
148
153
  'images' => image_list,
149
- 'filenames' => image_filenames
154
+ 'filenames' => image_filenames,
155
+ 'left' => left_value_for_context
150
156
  }
151
157
  end
152
158
 
@@ -161,7 +167,7 @@ protected
161
167
 
162
168
 
163
169
  def build_css(context = {})
164
- type = context.delete('type')
170
+ type = context['type']
165
171
  case type
166
172
  when :list
167
173
  css = build_css_for_list(context)
@@ -185,9 +191,9 @@ protected
185
191
  new_context['filename'] = File.basename(new_context['full_filename'])
186
192
  new_context['file_basename'] = File.basename(new_context['full_filename'], '.*')
187
193
  new_context['variation_name'] = new_context['file_basename'].gsub(/^#{new_context['basename']}#{@delimiter}/, '')
188
-
189
194
  css << build_css(new_context.dup)
190
- new_context['top'] += @tile ? @tile.columns : new_context['height']
195
+
196
+ new_context['top'] += @tile ? @tile.rows : new_context['height']
191
197
  css
192
198
  end.join("\n")
193
199
  end
@@ -26,4 +26,10 @@ class SpriteBatchGeneratorTest < Test::Unit::TestCase
26
26
  assert 6, output_files.size
27
27
  end
28
28
 
29
+ protected
30
+
31
+ def this_method
32
+ caller[0] =~ /`([^']*)'/ and $1
33
+ end
34
+
29
35
  end
@@ -17,17 +17,75 @@ class SpriteGeneratorTest < Test::Unit::TestCase
17
17
  # delete test output
18
18
  Dir.glob('test/output/*').each{|f| File.delete f }
19
19
  end
20
+
21
+ def test_should_create_correct_sprite_for_tile_with_vertical_distribution
22
+ options = {
23
+ :distribution => 'vertical',
24
+ :tile => '40x300',
25
+ :alignment => 'north_west',
26
+ :template => "a.{{file_basename}} { padding-left:2em; background: transparent url(#{this_method}.png) -{{left}}px -{{top}}px no-repeat; }"
27
+ }
28
+
29
+ generator = SpriteGenerator.new(@all_images_path, "test/output/#{this_method}.png", nil, options)
30
+ css = generator.create
31
+ assert css.include?('-4500px')
32
+
33
+ page = Liquid::Template.parse(File.open('test/templates/link.html').read).render('css' => css, 'image' => "#{this_method}.png")
34
+ output_file = File.join('test', 'output', "#{this_method}.html")
35
+ File.open(output_file, 'w+'){ |f| f.puts page }
36
+ assert File.exists?(output_file)
37
+ end
38
+
39
+
40
+ def test_should_create_correct_sprite_for_tile_with_vertical_distribution
41
+ options = {
42
+ :distribution => 'vertical',
43
+ :tile => '40x300',
44
+ :alignment => 'north_west',
45
+ :template => "a.{{file_basename}} { padding-left:2em; background: transparent url(#{this_method}.png) -{{left}}px -{{top}}px no-repeat; }"
46
+ }
47
+
48
+ generator = SpriteGenerator.new(@all_images_path, "test/output/#{this_method}.png", nil, options)
49
+ css = generator.create
50
+ assert css.include?('-4500px')
51
+
52
+ page = Liquid::Template.parse(File.open('test/templates/link.html').read).render('css' => css, 'image' => "#{this_method}.png")
53
+ output_file = File.join('test', 'output', "#{this_method}.html")
54
+ File.open(output_file, 'w+'){ |f| f.puts page }
55
+ assert File.exists?(output_file)
56
+ end
57
+
58
+
59
+
60
+ def test_should_create_correct_sprite_for_tile_with_horizontal_distribution
61
+ options = {
62
+ :distribution => 'horizontal',
63
+ :tile => '400x50',
64
+ :alignment => 'north_west',
65
+ :template => "a.{{file_basename}} { padding-left:2em; background: transparent url(#{this_method}.png) -{{left}}px -{{top}}px no-repeat; }"
66
+ }
67
+
68
+ generator = SpriteGenerator.new(@all_images_path, "test/output/#{this_method}.png", nil, options)
69
+ css = generator.create
70
+ assert css.include?('-6000px')
71
+
72
+ page = Liquid::Template.parse(File.open('test/templates/link.html').read).render('css' => css, 'image' => "#{this_method}.png")
73
+ output_file = File.join('test', 'output', "#{this_method}.html")
74
+ File.open(output_file, 'w+'){ |f| f.puts page }
75
+ assert File.exists?(output_file)
76
+ end
77
+
20
78
 
21
79
  def test_should_user_horizontal_distribution
22
80
  template = %q{ {{left}} }
23
- generator = SpriteGenerator.new(@all_images_path, @output, nil, { :template => template, :distribution => :horizontal })
81
+ generator = SpriteGenerator.new(@all_images_path, @output, nil, { :template => template, :distribution => 'horizontal' })
24
82
  css = generator.create
25
83
  assert css.include?('240')
26
84
  end
27
85
 
28
86
  def test_should_user_vertical_distribution
29
87
  template = %q{ {{top}} }
30
- generator = SpriteGenerator.new(@all_images_path, @output, nil, { :template => template, :distribution => :vertical })
88
+ generator = SpriteGenerator.new(@all_images_path, @output, nil, { :template => template, :distribution => 'vertical' })
31
89
  css = generator.create
32
90
  assert css.include?('240')
33
91
  end
@@ -151,6 +209,11 @@ class SpriteGeneratorTest < Test::Unit::TestCase
151
209
  files = @generator.instance_variable_get(:@files)
152
210
  assert_equal 0, files.size
153
211
  end
154
-
155
-
212
+
213
+ protected
214
+
215
+ def this_method
216
+ caller[0] =~ /`([^']*)'/ and $1
217
+ end
218
+
156
219
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: the-architect-sprite_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcel Scherf
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-22 00:00:00 -07:00
12
+ date: 2009-04-23 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency