United States Power Squadrons® Flag Generator

Gem Version Build Status Test Coverage Maintainability

This gem allows you to generate precise SVG and PNG flag images based on official specifications.

Installation

Rails

Add to your Gemfile:

gem 'usps_flags'

Create the file config/initializers/usps_flags.rb:

USPSFlags.configure do |config|
  config.flags_dir = "#{Rails.root}/app/assets/images/flags"
end

Available flags

  • US Ensign
  • USPS Ensign
  • USPS Ensign Wheel logo
  • Officer flags
  • Officer insignia
  • Official pennants

Testing

Tests are written in Rspec.

To run all specs, run rake or rspec.

To run most specs, but skip the static file generators (which can be slow), run rspec --tag '~slow'.

Generation

All files

To generate all static files, run:

USPSFlags::Generate.all svg: true, png: true, zips: true
  • Boolean arguments specify whether to process that set of files.

Zip archives

To re-generate zip files from current static files, run:

USPSFlags::Generate.zips svg: true, png: true
  • Boolean arguments specify whether to process that set of files.

Individual files

To generate an individual SVG file, run:

USPSFlags::Generate.svg "flag", outfile: nil, scale: nil, field: true
  • outfile specifies where to save the file. If left as nil, this method will puts the generated SVG. Either way, the SVG code is returned.
  • scale is a divisor scaling factor – the larger it is, the smaller the resulting SVG will be rendered. Accepted values are floats between 0 and 1, and integers above that.
  • field specifies whether to render the field of a flag, or to only render the insignia. Setting this to false will invert some colors for visibility.

Trident spec sheet

To generate the trident spec sheet, run:

USPSFlags::Generate.spec outfile: nil, scale: nil, fly: 24, unit: "in"
  • outfile specifies where to save the file. If left as nil, this method will puts the generated SVG. Either way, the SVG code is returned.
  • scale is a divisor scaling factor – the larger it is, the smaller the resulting SVG will be rendered. Accepted values are floats between 0 and 1, and integers above that.
  • fly specifies the custom fly measurement to scale all trident labels to.
  • unit specifies the custom fly measurement unit to append to all trident labels.

Convert SVG to PNG

To convert SVG data to a PNG image, run:

USPSFlags::Generate.png svg_data, outfile: nil, trim: false
  • outfile is required, and specifies where to save the file.
  • trim specifies whether to trim blank space from around the image. (This is ideal for generating insignia.)

Constructing

You can also construct individual flags using the following syntax:

flag = USPSFlags.new do |f|
  f.type = "LtC"
  f.scale = 3
  f.field = false
  f.trim = true
  f.svg_file = "/path/to/svg/output.svg"
  f.png_file = "/path/to/png/output.png"
end

flag = USPSFlags.new(
  type: "LtC", scale: 3, field: false, trim: true,
  svg_file: "/path/to/svg/output.svg",
  png_file: "/path/to/png/output.png"
)

flag.svg #=> Generates SVG file at "/path/to/svg/output.svg"
flag.png #=> Generates PNG file at "/path/to/png/output.png"
  • You can explicitly set svg_file to "" to suppress printing the SVG content to console/log.
  • Calling .png requires png_file to be set.

Extensions

There is an extension to this gem for handling squadron burgees: USPSFlags::Burgees

There is an extension to this gem for handling grade insignia: USPSFlags::Grades

License

Actual images generated (other than the US Ensign) are registered trademarks of United States Power Squadrons.

This project is released under the GPLv3.