Class: ResizeCss

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

Overview

use ImageMagick to Resize, using 32px version as base convert flags32.png -resize 75% flags24.png convert flags32_semi.png -resize 75% flags24_semi.png

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_size, new_size, options) ⇒ ResizeCss

Returns a new instance of ResizeCss.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/world_flags/tools/resize_css.rb', line 10

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]
  
  command1 = "convert #{sprite_name}#{base_size}.png -resize #{factor * 100}% #{sprite_name}#{new_size}.png"
  command2 = "convert #{sprite_name}#{base_size}_semi.png -resize #{factor * 100}% #{sprite_name}#{new_size}_semi.png"

  if auto_exec?
    %x[command1]
    %x[command2]
  else
    puts "Use ImageMagick to resize #{sprite_name} sprites:"  
    puts command1
    puts command2
  end
end

Instance Attribute Details

#base_sizeObject (readonly)

Returns the value of attribute base_size.



7
8
9
# File 'lib/world_flags/tools/resize_css.rb', line 7

def base_size
  @base_size
end

#new_sizeObject (readonly)

Returns the value of attribute new_size.



7
8
9
# File 'lib/world_flags/tools/resize_css.rb', line 7

def new_size
  @new_size
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/world_flags/tools/resize_css.rb', line 8

def options
  @options
end

#size_prefixObject (readonly)

Returns the value of attribute size_prefix.



8
9
10
# File 'lib/world_flags/tools/resize_css.rb', line 8

def size_prefix
  @size_prefix
end

#sprite_nameObject (readonly)

Returns the value of attribute sprite_name.



7
8
9
# File 'lib/world_flags/tools/resize_css.rb', line 7

def sprite_name
  @sprite_name
end

#stylesheet_folderObject (readonly)

Returns the value of attribute stylesheet_folder.



7
8
9
# File 'lib/world_flags/tools/resize_css.rb', line 7

def stylesheet_folder
  @stylesheet_folder
end

#stylesheet_nameObject (readonly)

Returns the value of attribute stylesheet_name.



7
8
9
# File 'lib/world_flags/tools/resize_css.rb', line 7

def stylesheet_name
  @stylesheet_name
end

#template_langObject (readonly)

Returns the value of attribute template_lang.



8
9
10
# File 'lib/world_flags/tools/resize_css.rb', line 8

def template_lang
  @template_lang
end

Instance Method Details

#auto_exec?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/world_flags/tools/resize_css.rb', line 34

def auto_exec?
  options[:auto_exec]
end

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/world_flags/tools/resize_css.rb', line 70

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}/flags/flags#{new_size}#{semi}.#{ext}#{template_ext}", File.dirname(__FILE__)

  lines = File.open(src_file_path).readlines

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

  repositioned_css = lines.map do |line|
    pos_match = line.match /.+position:0 -(\d+)px.*/
    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
    new_line ||= line
    new_line
  end.join("")

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

#factorObject



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

def factor
  @factor ||= new_size / base_size
end

#replace_pos(pos_match, line) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/world_flags/tools/resize_css.rb', line 99

def replace_pos pos_match, line
  pos_y = pos_match[1].to_i        
  new_pos_y = (pos_y * factor).to_i.to_s
  # puts "pos: #{pos_y} -> #{new_pos_y}"
  rest = line[10..-1]
  rest = rest.sub /#{pos_y}/, new_pos_y.to_s
  # first = line[0..10].sub(".f#{base_size}", ".f#{newsize}") 
  line[0..10] + rest
end

#stylesheet_full_pathObject



50
51
52
# File 'lib/world_flags/tools/resize_css.rb', line 50

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

#stylesheet_pathObject



42
43
44
# File 'lib/world_flags/tools/resize_css.rb', line 42

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

#template_extObject



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

def template_ext
  template_lang ? ".#{template_lang}" : ''
end