Class: USPSFlags::Generate

Inherits:
Object
  • Object
show all
Defined in:
lib/usps_flags/generate.rb

Overview

Controller class for generating files.

Since:

  • 0.1.5

Class Method Summary collapse

Class Method Details

.all(svg: true, png: true, zips: true, reset: true) ⇒ Object

Generate all static SVG and PNG files, and automaticall generates zip archives for download.

Parameters:

  • svg (Boolean) (defaults to: true)

    Whether to generate SVG images.

  • png (Boolean) (defaults to: true)

    Whether to generate PNG images.

  • zips (Boolean) (defaults to: true)

    Whether to create zip archives for all images created. Does not create a zip for skipped formats.

  • reset (Boolean) (defaults to: true)

    Whether to delete all previous files before generating new files.

Since:

  • 0.1.5



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/usps_flags/generate.rb', line 54

def self.all(svg: true, png: true, zips: true, reset: true)
  if reset
    ::FileUtils.rm_rf(Dir.glob("#{USPSFlags::Config.flags_dir}/SVG/*"))
    ::FileUtils.rm_rf(Dir.glob("#{USPSFlags::Config.flags_dir}/PNG/*"))
    ::FileUtils.rm_rf(Dir.glob("#{USPSFlags::Config.flags_dir}/ZIP/*"))
    ::FileUtils.mkdir_p("#{USPSFlags::Config.flags_dir}/SVG/insignia")
    ::FileUtils.mkdir_p("#{USPSFlags::Config.flags_dir}/PNG/insignia")
    puts "Cleared previous files."
  end

  flags = USPSFlags::Helpers.valid_flags(:all)
  insignia_flags = USPSFlags::Helpers.valid_flags(:insignia)

  max_length = flags.map(&:length).max
  puts "\nSVGs generate a single file.",
    "PNGs generate full-res, 1500w, 1000w, 500w, and thumbnail files.",
    "Corresponding rank insignia (including smaller sizes) are also generated, as appropriate."
  USPSFlags::Helpers.log "\n#{Time.now.strftime('%Y%m%d.%H%M%S%z')} – Generating static files...\n\n"
  USPSFlags::Helpers.log "Flag | SVG | PNG        | Run time\n".rjust(max_length+31),
    "\n".rjust(max_length+32, "-")

  overall_start_time = Time.now

  flags.each do |flag|
    start_time = Time.now
    USPSFlags::Helpers.log " |     |  _ _ _ _ _  |         \r".rjust(max_length+31, " ")
    flag = flag.upcase
    USPSFlags::Helpers.log "#{flag.rjust(max_length)} |"

    svg_file = "#{USPSFlags::Config.flags_dir}/SVG/#{flag}.svg"
    png_file = "#{USPSFlags::Config.flags_dir}/PNG/#{flag}.png"

    svg_ins_file = "#{USPSFlags::Config.flags_dir}/SVG/insignia/#{flag}.svg"
    png_ins_file = "#{USPSFlags::Config.flags_dir}/PNG/insignia/#{flag}.png"

    past = (flag[0] == "P" && flag != "PORTCAP")

    if svg
      begin
        USPSFlags::Helpers.log " "
        self.get flag, outfile: svg_file, scale: 1
        USPSFlags::Helpers.log "S"
        if past || !insignia_flags.include?(flag)
          USPSFlags::Helpers.log "-"
        else
          self.get flag, field: false, outfile: svg_ins_file, scale: 1
          USPSFlags::Helpers.log "I"
        end
      rescue => e
        USPSFlags::Helpers.log "x -> #{e.message}"
      end
    else
      USPSFlags::Helpers.log "-"
    end

    if png
      USPSFlags::Helpers.log "  | "
      begin
        USPSFlags::Helpers.log "…\b"
        USPSFlags::Generate.png(File.read(svg_file), outfile: png_file) unless ::File.exists?(png_file)
        USPSFlags::Helpers.log "F"
        if past || !insignia_flags.include?(flag)
          USPSFlags::Helpers.log "-"
        else
          USPSFlags::Helpers.log "…\b"
          USPSFlags::Generate.png(File.read(svg_ins_file), outfile: png_ins_file, trim: true) unless ::File.exists?(png_ins_file)
          USPSFlags::Helpers.log "I"
        end
        sizes = {1500 => "H", 1000 => "K", 500 => "D", "thumb" => "T"}
        sizes.keys.each do |size|
          if ::File.exists?("#{USPSFlags::Config.flags_dir}/PNG/#{flag}.#{size}.png")
            USPSFlags::Helpers.log "."
          else
            USPSFlags::Helpers.log "…\b"
            if size == "thumb"
              size_key = size
              size = case flag
              when "ENSIGN"
                200
              when "US"
                300
              else
                150
              end
            else
              size_key = size
            end
            MiniMagick::Tool::Convert.new do |convert|
              convert << "-background" << "none"
              convert << "-format" << "png"
              convert << "-resize" << "#{size}"
              convert << png_file
              convert << "#{USPSFlags::Config.flags_dir}/PNG/#{flag}.#{size_key}.png"
            end
            USPSFlags::Helpers.log sizes[size_key]

            if ::File.exists?(png_ins_file)
              if ::File.exists?("#{USPSFlags::Config.flags_dir}/PNG/insignia/#{flag}.#{size}.png")
                USPSFlags::Helpers.log "."
              elsif MiniMagick::Image.open(png_ins_file)[:width] > size
                USPSFlags::Helpers.log "…\b"
                MiniMagick::Tool::Convert.new do |convert|
                  convert << "-background" << "none"
                  convert << "-format" << "png"
                  convert << "-resize" << "#{size}"
                  convert << png_ins_file
                  convert << "#{USPSFlags::Config.flags_dir}/PNG/insignia/#{flag}.#{size_key}.png"
                end
                USPSFlags::Helpers.log "i"
              else
                USPSFlags::Helpers.log "+"
              end
            else
              USPSFlags::Helpers.log "-"
            end
          end
        end
      rescue => e
        USPSFlags::Helpers.log "x -> #{e.message}"
      end
    else
      USPSFlags::Helpers.log "- "
    end
    run_time = (Time.now - start_time).round(4).to_s[(0..5)].ljust(6, "0")
    USPSFlags::Helpers.log " | #{run_time} s\n"
  end

  self.zips(svg: svg, png: png) if zips

  USPSFlags::Helpers.log "\nTotal run time: #{Time.now - overall_start_time} s\n\n"

  nil
end

.get(flag, outfile: nil, scale: nil, field: true) ⇒ String

The primary controller method. Generates an SVG file or SVG data.

Parameters:

  • flag (String)

    The flag type to generate.

  • outfile (String) (defaults to: nil)

    The path to save the SVG file to. If not set, prints to console.

  • field (Boolean) (defaults to: true)

    Whether to generate the flag field (including any border).

  • scale (String) (defaults to: nil)

    The image scale divisor factor.

Returns:

  • (String)

    Returns the SVG data.

Since:

  • 0.1.5



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/usps_flags/generate.rb', line 10

def self.get(flag, outfile: nil, scale: nil, field: true)
  flag = flag.upcase
  if ["CRUISE", "OIC"].include?(flag)
    self.pennant(type: flag, outfile: outfile, scale: scale)
  elsif flag.upcase == "ENSIGN"
    self.ensign(outfile: outfile, scale: scale)
  elsif flag.upcase == "US"
    self.us(outfile: outfile, scale: scale)
  elsif flag.upcase == "WHEEL"
    self.wheel(outfile: outfile, scale: scale)
  else
    self.flag(rank: flag, outfile: outfile, scale: scale, field: field)
  end
end

.png(svg, outfile: nil, trim: false) ⇒ Object

Convert SVG data into a PNG file.

Parameters:

  • svg (String)

    The SVG data.

  • outfile (String) (defaults to: nil)

    The path to save the PNG file to. (File is not accessible if this is left blank.)

  • trim (Boolean) (defaults to: false)

    Whether to trim the generated PNG file of excess transparency.

Since:

  • 0.1.5



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/usps_flags/generate.rb', line 30

def self.png(svg, outfile: nil, trim: false)
  outfile = "temp.png" if outfile.nil?
  temp_svg = ::File.new("temp.svg", "w+")
  temp_svg.write(svg)
  temp_svg.flush

  MiniMagick::Tool::Convert.new do |convert|
    convert << "-background" << "none"
    convert << "-format" << "png"
    convert << "-trim" if trim
    convert << temp_svg.path
    convert << outfile
  end
ensure
  ::File.delete(temp_svg) if ::File.exists?(temp_svg)
  ::File.delete("temp.png") if ::File.exists?("temp.png")
end

.spec(outfile: nil, fly: nil, unit: nil, scale: nil) ⇒ String

Generate trident spec sheet as an SVG image.

Parameters:

  • outfile (String) (defaults to: nil)

    The path to save the SVG file to. If not set, prints to console.

  • fly (Integer) (defaults to: nil)

    The nominal fly length of an appropriate flag field for the generated tridents. Size labels scale to this size.

  • outfile (String) (defaults to: nil)

    The unit to append to all trident measurements.

  • scale (String) (defaults to: nil)

    The image scale divisor factor.

Returns:

  • (String)

    Returns the SVG data.

Since:

  • 0.1.5



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/usps_flags/generate.rb', line 219

def self.spec(outfile: nil, fly: nil, unit: nil, scale: nil)
  fly = fly.nil? ? USPSFlags::Config::BASE_FLY : fly
  final_svg = ""
  final_svg << USPSFlags::Core.headers(scale: scale)
  final_svg << USPSFlags::Core.trident_spec(fly: fly, unit: unit)
  final_svg << USPSFlags::Core.footer

  if outfile.nil?
    puts final_svg, "\n"
  else
    f = ::File.new(outfile, "w+")
    f.write(final_svg)
    f.close
  end
  final_svg
end

.zips(svg: true, png: true) ⇒ Object

Generate zip archives of current static image files.

Parameters:

  • svg (Boolean) (defaults to: true)

    Generate zip archive of SVG images.

  • png (Boolean) (defaults to: true)

    Generate zip archive of PNG images.

Since:

  • 0.1.5



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/usps_flags/generate.rb', line 192

def self.zips(svg: true, png: true)
  ["svg", "png"].each do |format|
    if eval(format)
      zip = "#{USPSFlags::Config.flags_dir}/ZIP/USPS_Flags.#{format}.zip"
      ::File.delete(zip) if ::File.exists?(zip)
      Zip::File.open(zip, Zip::File::CREATE) do |z|
        Dir.glob("#{USPSFlags::Config.flags_dir}/#{format.upcase}/**/*").each do |f|
          if f.split("/").last(2).first == "insignia"
            filename = "insignia/#{f.split("/").last}"
            z.add(filename, f)
          else
            z.add(f.split("/").last, f)
          end
        end
      end
      puts "Generated #{format.upcase} Zip"
    end
  end
end