Class: ResizeCss

Inherits:
Object
  • Object
show all
Defined in:
lib/pictos-icons/tools/resize_css.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_size, new_size, options = {}) ⇒ ResizeCss

Returns a new instance of ResizeCss.



12
13
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
# File 'lib/pictos-icons/tools/resize_css.rb', line 12

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]

  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.



9
10
11
# File 'lib/pictos-icons/tools/resize_css.rb', line 9

def base_size
  @base_size
end

#new_sizeObject (readonly)

Returns the value of attribute new_size.



9
10
11
# File 'lib/pictos-icons/tools/resize_css.rb', line 9

def new_size
  @new_size
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/pictos-icons/tools/resize_css.rb', line 10

def options
  @options
end

#size_prefixObject (readonly)

Returns the value of attribute size_prefix.



10
11
12
# File 'lib/pictos-icons/tools/resize_css.rb', line 10

def size_prefix
  @size_prefix
end

#sprite_nameObject (readonly)

Returns the value of attribute sprite_name.



9
10
11
# File 'lib/pictos-icons/tools/resize_css.rb', line 9

def sprite_name
  @sprite_name
end

#stylesheet_folderObject (readonly)

Returns the value of attribute stylesheet_folder.



9
10
11
# File 'lib/pictos-icons/tools/resize_css.rb', line 9

def stylesheet_folder
  @stylesheet_folder
end

#stylesheet_nameObject (readonly)

Returns the value of attribute stylesheet_name.



9
10
11
# File 'lib/pictos-icons/tools/resize_css.rb', line 9

def stylesheet_name
  @stylesheet_name
end

#template_langObject (readonly)

Returns the value of attribute template_lang.



10
11
12
# File 'lib/pictos-icons/tools/resize_css.rb', line 10

def template_lang
  @template_lang
end

Instance Method Details

#auto_exec?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/pictos-icons/tools/resize_css.rb', line 74

def auto_exec?
  options[:auto_exec]
end

#blank?(txt) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/pictos-icons/tools/resize_css.rb', line 110

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

#darkenObject



66
67
68
# File 'lib/pictos-icons/tools/resize_css.rb', line 66

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

#darken?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/pictos-icons/tools/resize_css.rb', line 54

def darken?
  options[:dark]
end

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/pictos-icons/tools/resize_css.rb', line 114

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



149
150
151
# File 'lib/pictos-icons/tools/resize_css.rb', line 149

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

#gray?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/pictos-icons/tools/resize_css.rb', line 58

def gray?
  options[:gray]
end

#grayscaleObject



70
71
72
# File 'lib/pictos-icons/tools/resize_css.rb', line 70

def grayscale
  "-colorspace Gray"
end

#lightenObject



62
63
64
# File 'lib/pictos-icons/tools/resize_css.rb', line 62

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

#lighten?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/pictos-icons/tools/resize_css.rb', line 50

def lighten?
  options[:light]
end

#replace_pos(pos_match, line) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/pictos-icons/tools/resize_css.rb', line 153

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



90
91
92
# File 'lib/pictos-icons/tools/resize_css.rb', line 90

def stylesheet_full_path
  @stylesheet_full_path ||= File.join(stylesheet_path, stylesheet_folder)
end

#stylesheet_pathObject



82
83
84
# File 'lib/pictos-icons/tools/resize_css.rb', line 82

def stylesheet_path
  "../../../vendor/assets/stylesheets"
end

#template_extObject



106
107
108
# File 'lib/pictos-icons/tools/resize_css.rb', line 106

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