Class: ResizeCss

Inherits:
Object
  • Object
show all
Defined in:
lib/world_flags/tools/resize_css.rb

Overview

TODO: Needs some heavy refactoring and cleaning up!

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_size, new_size, options) ⇒ ResizeCss

Returns a new instance of ResizeCss.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/world_flags/tools/resize_css.rb', line 14

def initialize base_size, new_size, options
  @base_size  = base_size
  @new_size   = new_size
  @options    = options

  @stylesheet_folder = options[:stylesheet_folder]  if options[:stylesheet_folder]
  @stylesheet_name   = options[:stylesheet_name]    if options[:stylesheet_name]
  @sprite_name       = options[:sprite_name]        if options[:sprite_name]
  @template_lang     = options[:template_lang]      if options[:template_lang]
  @size_prefix       = options[:size_prefix]        if options[:size_prefix]
  
  resize = "convert #{sprite_name}#{base_size}.png -resize #{factor * 100}%"
  new_file_name = "#{sprite_name}#{new_size}"

  command_resize = "#{resize} #{new_file_name}.png"
  command_resize_light = "#{resize} #{lighten} #{new_file_name}_light.png"
  command_resize_dark  = "#{resize} #{darken} #{new_file_name}_dark.png"
  command_resize_gray  = "#{resize} #{grayscale} #{new_file_name}_gray.png"

  if auto_exec?
    Open3.popen3("pwd") do |stdin, stdout, e, t|
      base_dir = stdout
      Open3.popen3 "cd #{images_dir}"

      Open3.popen3 command_resize
      Open3.popen3 command_resize_light if lighten?
      Open3.popen3 command_resize_dark if darken?
      Open3.popen3 command_resize_gray if gray? 

      Open3.popen3 "cd #{base_dir}"
    end
  else
    puts "Use ImageMagick to resize #{sprite_name} sprites:"  
    puts command_resize
    puts command_resize_light if lighten?
    puts command_resize_dark if darken?
    puts command_resize_gray if gray? 
  end
end

Instance Attribute Details

#base_sizeObject (readonly)

Returns the value of attribute base_size.



11
12
13
# File 'lib/world_flags/tools/resize_css.rb', line 11

def base_size
  @base_size
end

#new_sizeObject (readonly)

Returns the value of attribute new_size.



11
12
13
# File 'lib/world_flags/tools/resize_css.rb', line 11

def new_size
  @new_size
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/world_flags/tools/resize_css.rb', line 12

def options
  @options
end

#size_prefixObject (readonly)

Returns the value of attribute size_prefix.



12
13
14
# File 'lib/world_flags/tools/resize_css.rb', line 12

def size_prefix
  @size_prefix
end

#sprite_nameObject (readonly)

Returns the value of attribute sprite_name.



11
12
13
# File 'lib/world_flags/tools/resize_css.rb', line 11

def sprite_name
  @sprite_name
end

#stylesheet_folderObject (readonly)

Returns the value of attribute stylesheet_folder.



11
12
13
# File 'lib/world_flags/tools/resize_css.rb', line 11

def stylesheet_folder
  @stylesheet_folder
end

#stylesheet_nameObject (readonly)

Returns the value of attribute stylesheet_name.



11
12
13
# File 'lib/world_flags/tools/resize_css.rb', line 11

def stylesheet_name
  @stylesheet_name
end

#template_langObject (readonly)

Returns the value of attribute template_lang.



12
13
14
# File 'lib/world_flags/tools/resize_css.rb', line 12

def template_lang
  @template_lang
end

Instance Method Details

#auto_exec?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/world_flags/tools/resize_css.rb', line 78

def auto_exec?
  options[:auto_exec]
end

#blank?(txt) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/world_flags/tools/resize_css.rb', line 122

def blank? txt
  !txt || txt.empty?
end

#darkenObject



70
71
72
# File 'lib/world_flags/tools/resize_css.rb', line 70

def darken
  "-level 0%,100%,0.5"
end

#darken?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/world_flags/tools/resize_css.rb', line 58

def darken?
  options[:dark]
end

#execute(options = {:ext => 'css', :semi => false}) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/world_flags/tools/resize_css.rb', line 126

def execute options = {:ext => 'css', :semi => false}
  semi = options[:semi] ? '-semi': ''
  ext = options[:ext] ? options[:ext] : 'css'
  src_file_path = File.expand_path "#{stylesheet_full_path}/#{stylesheet_name}#{base_size}#{semi}.#{ext}#{template_ext}", File.dirname(__FILE__)
  target_file_path = File.expand_path "#{stylesheet_full_path}/#{stylesheet_name}#{new_size}#{semi}.#{ext}#{template_ext}", File.dirname(__FILE__)

  puts "src: #{src_file_path} -> #{target_file_path}"

  lines = File.open(src_file_path).readlines

  img_exp = "#{sprite_name}-#{base_size}"
  replace_img = "#{sprite_name}-#{new_size}"

  # puts "img_exp: #{img_exp}"
  # puts "replace_img: #{replace_img}"

  repositioned_css = lines.map do |line|
    pos_match = line.match /.+position:\s-?(\d+)(px)?\s-?(\d+)(px)?/
    # puts "pos_match: #{line} - #{pos_match.inspect}"
    img_match = line.match /#{img_exp}/
    new_line = line.sub(/#{img_exp}/, replace_img) if img_match
    new_line = replace_pos pos_match, line if pos_match
    # puts "line: #{line} -> #{new_line}"
    new_line ||= line
    new_line
  end.join("")

  puts "Write new CSS:"
  puts repositioned_css
  
  File.open(target_file_path, 'w') do |f|
    f.puts repositioned_css.gsub ".#{size_prefix}#{base_size}", ".#{size_prefix}#{new_size}"
  end
end

#factorObject



161
162
163
# File 'lib/world_flags/tools/resize_css.rb', line 161

def factor
  @factor ||= new_size / base_size.to_f
end

#gray?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/world_flags/tools/resize_css.rb', line 62

def gray?
  options[:gray]
end

#grayscaleObject



74
75
76
# File 'lib/world_flags/tools/resize_css.rb', line 74

def grayscale
  "-colorspace Gray"
end

#images_dirObject



90
91
92
# File 'lib/world_flags/tools/resize_css.rb', line 90

def images_dir
  @images_dirĀ ||= File.join(vendor_assets_path, 'images')
end

#lightenObject



66
67
68
# File 'lib/world_flags/tools/resize_css.rb', line 66

def lighten
  "-level 0%,100%,2.0"
end

#lighten?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/world_flags/tools/resize_css.rb', line 54

def lighten?
  options[:light]
end

#replace_pos(pos_match, line) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/world_flags/tools/resize_css.rb', line 165

def replace_pos pos_match, line
  pos_x = pos_match[1].to_i        
  new_pos_x = (pos_x * factor).to_i.to_s

  pos_y = pos_match[3].to_i        
  new_pos_y = (pos_y * factor).to_i.to_s
  
  # puts "new pos: #{new_pos_x}, #{new_pos_y}"
  
  rest = line[6..-1]
  rest = rest.sub /#{pos_x}/, new_pos_x.to_s
  rest = rest.sub /#{pos_y}/, new_pos_y.to_s

  rest = rest.gsub /#{base_size}px;/, "#{new_size}px;"

  # first = line[0..10].sub(".f#{base_size}", ".f#{newsize}") 
  line[0..5] + rest
end

#stylesheet_full_pathObject



102
103
104
# File 'lib/world_flags/tools/resize_css.rb', line 102

def stylesheet_full_path
  @stylesheet_full_path ||= File.join(stylesheets_dir, stylesheets_folder)
end

#stylesheets_dirObject



94
95
96
# File 'lib/world_flags/tools/resize_css.rb', line 94

def stylesheets_dir
  @stylesheets_dir ||= File.join(vendor_assets_path, 'stylesheets')
end

#stylesheets_folderObject



98
99
100
# File 'lib/world_flags/tools/resize_css.rb', line 98

def stylesheets_folder
  @stylesheets_folder ||= "flags"
end

#template_extObject



118
119
120
# File 'lib/world_flags/tools/resize_css.rb', line 118

def template_ext
  !blank?(template_lang) ? ".#{template_lang}" : ''
end

#vendor_assets_pathObject



86
87
88
# File 'lib/world_flags/tools/resize_css.rb', line 86

def vendor_assets_path
  "../../../vendor/assets/"
end