Browse Source

Implementing format strings for filename counts on multisaves

dev
Andy Meneely 11 years ago
parent
commit
4ecbaa4e31
  1. 1
      CHANGELOG.md
  2. 5
      lib/squib/api/save.rb
  3. 1
      lib/squib/constants.rb
  4. 8
      lib/squib/graphics/save_doc.rb
  5. 8
      lib/squib/graphics/save_images.rb
  6. 6
      spec/data/samples/autoscale_font.rb.txt
  7. 6
      spec/data/samples/basic.rb.txt
  8. 4
      spec/data/samples/cairo_access.rb.txt
  9. 4
      spec/data/samples/csv_import.rb.txt
  10. 2
      spec/data/samples/custom_config.rb.txt
  11. 2
      spec/data/samples/draw_shapes.rb.txt
  12. 6
      spec/data/samples/excel.rb.txt
  13. 2
      spec/data/samples/gradients.rb.txt
  14. 4
      spec/data/samples/hello_world.rb.txt
  15. 2
      spec/data/samples/load_images.rb.txt
  16. 4
      spec/data/samples/portrait-landscape.rb.txt
  17. 6
      spec/data/samples/ranges.rb.txt
  18. 12
      spec/data/samples/saves.rb.txt
  19. 8
      spec/data/samples/showcase.rb.txt
  20. 6
      spec/data/samples/text_options.rb.txt
  21. 2
      spec/data/samples/tgc_proofs.rb.txt
  22. 2
      spec/data/samples/units.rb.txt

1
CHANGELOG.md

@ -3,6 +3,7 @@
## v0.3.0a ## v0.3.0a
* Masks! The `png` and `svg` commands can be used as if they are a mask, so you can color the icon with any color you like. Can be handy for switching to black-and-white, or for reusing the same image but different colors across cards. * Masks! The `png` and `svg` commands can be used as if they are a mask, so you can color the icon with any color you like. Can be handy for switching to black-and-white, or for reusing the same image but different colors across cards.
* Gradients! Can now specify linear or radial gradients anywhere you specify colors. See README and `samples/gradients.rb` for more details. * Gradients! Can now specify linear or radial gradients anywhere you specify colors. See README and `samples/gradients.rb` for more details.
* Number padding! `save_png` will now pad zeros on the filenames for friendlier sorting. You can also specify your own with `count_format` using the classical format string from Ruby's `Kernel::sprintf` (mostly just C-style format strings). Default: `'%02d'. The `prefix:` option is still there too.
* Special thanks to Shalom Craimer for the idea and proof-of-concept on the above two features! * Special thanks to Shalom Craimer for the idea and proof-of-concept on the above two features!
* Added new sample table for color constants in `samples/colors.rb` * Added new sample table for color constants in `samples/colors.rb`

5
lib/squib/api/save.rb

@ -25,14 +25,15 @@ module Squib
# @option opts [Enumerable] range (:all) the range of cards over which this will be rendered. See {file:README.md#Specifying_Ranges Specifying Ranges} # @option opts [Enumerable] range (:all) the range of cards over which this will be rendered. See {file:README.md#Specifying_Ranges Specifying Ranges}
# @option opts [String] dir (_output) the directory for the output to be sent to. Will be created if it doesn't exist. # @option opts [String] dir (_output) the directory for the output to be sent to. Will be created if it doesn't exist.
# @option opts [String] prefix (card_) the prefix of the file name to be printed. # @option opts [String] prefix (card_) the prefix of the file name to be printed.
# @option opts [String] count_format (%02d) the format string used for formatting the card count (e.g. padding zeros). Uses a Ruby format string (see the Ruby doc for Kernel::sprintf for specifics)
# @option opts [Boolean, :clockwise, :counterclockwise] rotate (false) if true, the saved cards will be rotated 90 degrees clockwise. Or, rotate by the number of radians. Intended to rendering landscape instead of portrait. # @option opts [Boolean, :clockwise, :counterclockwise] rotate (false) if true, the saved cards will be rotated 90 degrees clockwise. Or, rotate by the number of radians. Intended to rendering landscape instead of portrait.
# @return [nil] Returns nothing # @return [nil] Returns nothing
# @api public # @api public
def save_png(opts = {}) def save_png(opts = {})
opts = needs(opts,[:range, :creatable_dir, :prefix, :rotate]) opts = needs(opts,[:range, :creatable_dir, :prefix, :count_format, :rotate])
@progress_bar.start("Saving PNGs to #{opts[:dir]}/#{opts[:prefix]}*", @cards.size) do |bar| @progress_bar.start("Saving PNGs to #{opts[:dir]}/#{opts[:prefix]}*", @cards.size) do |bar|
opts[:range].each do |i| opts[:range].each do |i|
@cards[i].save_png(i, opts[:dir], opts[:prefix], opts[:rotate], opts[:angle]) @cards[i].save_png(i, opts[:dir], opts[:prefix], opts[:count_format], opts[:rotate], opts[:angle])
bar.increment bar.increment
end end
end end

1
lib/squib/constants.rb

@ -9,6 +9,7 @@ module Squib
:blend => :none, :blend => :none,
:color => :black, :color => :black,
:columns => 1, :columns => 1,
:count_format => '%02d',
:default_font => 'Arial 36', :default_font => 'Arial 36',
:dir => '_output', :dir => '_output',
:ellipsize => :end, :ellipsize => :end,

8
lib/squib/graphics/save_doc.rb

@ -49,6 +49,7 @@ module Squib
# @option opts colulmns [Integer] (1) the number of columns in the grid # @option opts colulmns [Integer] (1) the number of columns in the grid
# @option opts rows [Integer] (:infinite) the number of rows in the grid. When set to :infinite, the sheet scales to the rows needed. If there are more cards than rows*columns, new sheets are started. # @option opts rows [Integer] (:infinite) the number of rows in the grid. When set to :infinite, the sheet scales to the rows needed. If there are more cards than rows*columns, new sheets are started.
# @option opts [String] prefix (card_) the prefix of the file name(s) # @option opts [String] prefix (card_) the prefix of the file name(s)
# @option opts [String] count_format (%02d) the format string used for formatting the card count (e.g. padding zeros). Uses a Ruby format string (see the Ruby doc for Kernel::sprintf for specifics)
# @option opts dir [String] (_output) the directory to save to. Created if it doesn't exist. # @option opts dir [String] (_output) the directory to save to. Created if it doesn't exist.
# @option opts margin [Integer] (0) the margin around the outside of the page # @option opts margin [Integer] (0) the margin around the outside of the page
# @option opts gap [Integer] (0) the space in pixels between the cards # @option opts gap [Integer] (0) the space in pixels between the cards
@ -57,7 +58,7 @@ module Squib
# @api public # @api public
def save_sheet(opts = {}) def save_sheet(opts = {})
opts = {margin: 0}.merge(opts) # overriding the non-system default opts = {margin: 0}.merge(opts) # overriding the non-system default
p = needs(opts, [:range, :prefix, :creatable_dir, :margin, :gap, :trim, :rows, :columns]) p = needs(opts, [:range, :prefix, :count_format, :creatable_dir, :margin, :gap, :trim, :rows, :columns])
# EXTRACT METHOD HERE # EXTRACT METHOD HERE
sheet_width = (p[:columns] * (@width + 2 * p[:gap] - 2 * p[:trim])) + (2 * p[:margin]) sheet_width = (p[:columns] * (@width + 2 * p[:gap] - 2 * p[:trim])) + (2 * p[:margin])
sheet_height = (p[:rows] * (@height + 2 * p[:gap] - 2 * p[:trim])) + (2 * p[:margin]) sheet_height = (p[:rows] * (@height + 2 * p[:gap] - 2 * p[:trim])) + (2 * p[:margin])
@ -68,7 +69,8 @@ module Squib
@progress_bar.start("Saving PNG sheet to #{p[:dir]}/#{p[:prefix]}_*", @cards.size + 1) do |bar| @progress_bar.start("Saving PNG sheet to #{p[:dir]}/#{p[:prefix]}_*", @cards.size + 1) do |bar|
p[:range].each do |i| p[:range].each do |i|
if num_this_sheet >= (p[:columns] * p[:rows]) # new sheet if num_this_sheet >= (p[:columns] * p[:rows]) # new sheet
cc.target.write_to_png("#{p[:dir]}/#{p[:prefix]}#{sheet_num}.png") filename = "#{p[:dir]}/#{p[:prefix]}#{p[:count_format] % sheet_num}.png"
cc.target.write_to_png(filename)
new_sheet = false new_sheet = false
num_this_sheet = 0 num_this_sheet = 0
sheet_num += 1 sheet_num += 1
@ -87,7 +89,7 @@ module Squib
end end
bar.increment bar.increment
end end
cc.target.write_to_png("#{p[:dir]}/#{p[:prefix]}#{sheet_num}.png") cc.target.write_to_png("#{p[:dir]}/#{p[:prefix]}#{p[:count_format] % sheet_num}.png")
end end
end end

8
lib/squib/graphics/save_images.rb

@ -3,13 +3,13 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def save_png(i, dir, prefix, do_rotate, angle) def save_png(i, dir, prefix, count_format, do_rotate, angle)
if [true, :clockwise, :counterclockwise].include?(do_rotate) if [true, :clockwise, :counterclockwise].include?(do_rotate)
surface = rotated_image(angle) surface = rotated_image(angle)
else else
surface = @cairo_surface surface = @cairo_surface
end end
write_png(surface, i, dir, prefix) write_png(surface, i, dir, prefix, count_format)
end end
# :nodoc: # :nodoc:
@ -25,8 +25,8 @@ module Squib
end end
# :nodoc: # :nodoc:
# @api private # @api private
def write_png(surface, i, dir, prefix) def write_png(surface, i, dir, prefix, count_format)
surface.write_to_png("#{dir}/#{prefix}#{i}.png") surface.write_to_png("#{dir}/#{prefix}#{count_format % i}.png")
end end
end end

6
spec/data/samples/autoscale_font.rb.txt

@ -76,6 +76,6 @@ cairo: set_source_color([:red])
cairo: set_line_width([2.0]) cairo: set_line_width([2.0])
cairo: stroke([]) cairo: stroke([])
cairo: restore([]) cairo: restore([])
surface: write_to_png(["_output/autoscale_0.png"]) surface: write_to_png(["_output/autoscale_00.png"])
surface: write_to_png(["_output/autoscale_1.png"]) surface: write_to_png(["_output/autoscale_01.png"])
surface: write_to_png(["_output/autoscale_2.png"]) surface: write_to_png(["_output/autoscale_02.png"])

6
spec/data/samples/basic.rb.txt

@ -201,6 +201,6 @@ cairo: translate([-620, -218])
cairo: set_source([MockDouble, 620, 218]) cairo: set_source([MockDouble, 620, 218])
cairo: paint([1.0]) cairo: paint([1.0])
cairo: restore([]) cairo: restore([])
surface: write_to_png(["_output/basic_0.png"]) surface: write_to_png(["_output/basic_00.png"])
surface: write_to_png(["_output/basic_1.png"]) surface: write_to_png(["_output/basic_01.png"])
surface: write_to_png(["_output/basic_2.png"]) surface: write_to_png(["_output/basic_02.png"])

4
spec/data/samples/cairo_access.rb.txt

@ -21,5 +21,5 @@ cairo: circle([50, 50, 50])
cairo: set_source_color([:red]) cairo: set_source_color([:red])
cairo: fill([]) cairo: fill([])
cairo: restore([]) cairo: restore([])
surface: write_to_png(["_output/cairo_access_0.png"]) surface: write_to_png(["_output/cairo_access_00.png"])
surface: write_to_png(["_output/cairo_access_1.png"]) surface: write_to_png(["_output/cairo_access_01.png"])

4
spec/data/samples/csv_import.rb.txt

@ -108,5 +108,5 @@ cairo: update_pango_layout([MockDouble])
cairo: update_pango_layout([MockDouble]) cairo: update_pango_layout([MockDouble])
cairo: show_pango_layout([MockDouble]) cairo: show_pango_layout([MockDouble])
cairo: restore([]) cairo: restore([])
surface: write_to_png(["_output/sample_csv_0.png"]) surface: write_to_png(["_output/sample_csv_00.png"])
surface: write_to_png(["_output/sample_csv_1.png"]) surface: write_to_png(["_output/sample_csv_01.png"])

2
spec/data/samples/custom_config.rb.txt

@ -40,6 +40,6 @@ cairo: translate([-620, -218])
cairo: set_source([MockDouble, 620, 218]) cairo: set_source([MockDouble, 620, 218])
cairo: paint([1.0]) cairo: paint([1.0])
cairo: restore([]) cairo: restore([])
surface: write_to_png(["_output/custom-config_0.png"]) surface: write_to_png(["_output/custom-config_00.png"])
cairo: set_source([MockDouble, 75, 75]) cairo: set_source([MockDouble, 75, 75])
cairo: paint([]) cairo: paint([])

2
spec/data/samples/draw_shapes.rb.txt

@ -32,4 +32,4 @@ cairo: set_source_color([:black])
cairo: set_line_width([25.0]) cairo: set_line_width([25.0])
cairo: stroke([]) cairo: stroke([])
cairo: restore([]) cairo: restore([])
surface: write_to_png(["_output/shape_0.png"]) surface: write_to_png(["_output/shape_00.png"])

6
spec/data/samples/excel.rb.txt

@ -163,6 +163,6 @@ cairo: update_pango_layout([MockDouble])
cairo: update_pango_layout([MockDouble]) cairo: update_pango_layout([MockDouble])
cairo: show_pango_layout([MockDouble]) cairo: show_pango_layout([MockDouble])
cairo: restore([]) cairo: restore([])
surface: write_to_png(["_output/sample_excel_0.png"]) surface: write_to_png(["_output/sample_excel_00.png"])
surface: write_to_png(["_output/sample_excel_1.png"]) surface: write_to_png(["_output/sample_excel_01.png"])
surface: write_to_png(["_output/sample_excel_2.png"]) surface: write_to_png(["_output/sample_excel_02.png"])

2
spec/data/samples/gradients.rb.txt

@ -62,4 +62,4 @@ cairo: update_pango_layout([MockDouble])
cairo: update_pango_layout([MockDouble]) cairo: update_pango_layout([MockDouble])
cairo: show_pango_layout([MockDouble]) cairo: show_pango_layout([MockDouble])
cairo: restore([]) cairo: restore([])
surface: write_to_png(["_output/gradient_0.png"]) surface: write_to_png(["_output/gradient_00.png"])

4
spec/data/samples/hello_world.rb.txt

@ -32,5 +32,5 @@ cairo: update_pango_layout([MockDouble])
cairo: update_pango_layout([MockDouble]) cairo: update_pango_layout([MockDouble])
cairo: show_pango_layout([MockDouble]) cairo: show_pango_layout([MockDouble])
cairo: restore([]) cairo: restore([])
surface: write_to_png(["_output/card_0.png"]) surface: write_to_png(["_output/card_00.png"])
surface: write_to_png(["_output/card_1.png"]) surface: write_to_png(["_output/card_01.png"])

2
spec/data/samples/load_images.rb.txt

@ -135,4 +135,4 @@ cairo: set_source([ImageSurface, 650, 950])
cairo: set_source_color([:magenta]) cairo: set_source_color([:magenta])
cairo: mask([ImageSurface, 650, 950]) cairo: mask([ImageSurface, 650, 950])
cairo: restore([]) cairo: restore([])
surface: write_to_png(["_output/load_images_0.png"]) surface: write_to_png(["_output/load_images_00.png"])

4
spec/data/samples/portrait-landscape.rb.txt

@ -19,7 +19,7 @@ cairo: update_pango_layout([MockDouble])
cairo: update_pango_layout([MockDouble]) cairo: update_pango_layout([MockDouble])
cairo: show_pango_layout([MockDouble]) cairo: show_pango_layout([MockDouble])
cairo: restore([]) cairo: restore([])
surface: write_to_png(["_output/portrait_0.png"]) surface: write_to_png(["_output/portrait_00.png"])
cairo: save([]) cairo: save([])
cairo: set_source_color(["#aaa"]) cairo: set_source_color(["#aaa"])
cairo: paint([]) cairo: paint([])
@ -46,4 +46,4 @@ cairo: rotate([1.5707963267948966])
cairo: translate([-562.5, -412.5]) cairo: translate([-562.5, -412.5])
cairo: set_source([MockDouble]) cairo: set_source([MockDouble])
cairo: paint([]) cairo: paint([])
surface: write_to_png(["_output/landscape_0.png"]) surface: write_to_png(["_output/landscape_00.png"])

6
spec/data/samples/ranges.rb.txt

@ -421,6 +421,6 @@ cairo: update_pango_layout([MockDouble])
cairo: update_pango_layout([MockDouble]) cairo: update_pango_layout([MockDouble])
cairo: show_pango_layout([MockDouble]) cairo: show_pango_layout([MockDouble])
cairo: restore([]) cairo: restore([])
surface: write_to_png(["_output/ranges_0.png"]) surface: write_to_png(["_output/ranges_00.png"])
surface: write_to_png(["_output/ranges_1.png"]) surface: write_to_png(["_output/ranges_01.png"])
surface: write_to_png(["_output/ranges_2.png"]) surface: write_to_png(["_output/ranges_02.png"])

12
spec/data/samples/saves.rb.txt

@ -542,7 +542,7 @@ cairo: set_source([MockDouble, -37, -37])
cairo: paint([]) cairo: paint([])
cairo: set_source([MockDouble, 1650, 75]) cairo: set_source([MockDouble, 1650, 75])
cairo: paint([]) cairo: paint([])
surface: write_to_png(["_output/saves_notrim_1.png"]) surface: write_to_png(["_output/saves_notrim_01.png"])
cairo: set_source([MockDouble, -37, -37]) cairo: set_source([MockDouble, -37, -37])
cairo: paint([]) cairo: paint([])
cairo: set_source([MockDouble, 75, 75]) cairo: set_source([MockDouble, 75, 75])
@ -607,7 +607,7 @@ cairo: set_source([MockDouble, -37, -37])
cairo: paint([]) cairo: paint([])
cairo: set_source([MockDouble, 180, 817]) cairo: set_source([MockDouble, 180, 817])
cairo: paint([]) cairo: paint([])
surface: write_to_png(["_output/save_single_sheet_0.png"]) surface: write_to_png(["_output/save_single_sheet_00.png"])
cairo: set_source([MockDouble, -37, -37]) cairo: set_source([MockDouble, -37, -37])
cairo: paint([]) cairo: paint([])
cairo: set_source([MockDouble, 75, 75]) cairo: set_source([MockDouble, 75, 75])
@ -640,7 +640,7 @@ cairo: set_source([MockDouble, -37, -37])
cairo: paint([]) cairo: paint([])
cairo: set_source([MockDouble, 390, 181]) cairo: set_source([MockDouble, 390, 181])
cairo: paint([]) cairo: paint([])
surface: write_to_png(["_output/save_sheet_0.png"]) surface: write_to_png(["_output/save_sheet_00.png"])
cairo: set_source([MockDouble, -37, -37]) cairo: set_source([MockDouble, -37, -37])
cairo: paint([]) cairo: paint([])
cairo: set_source([MockDouble, 75, 75]) cairo: set_source([MockDouble, 75, 75])
@ -673,7 +673,7 @@ cairo: set_source([MockDouble, -37, -37])
cairo: paint([]) cairo: paint([])
cairo: set_source([MockDouble, 390, 181]) cairo: set_source([MockDouble, 390, 181])
cairo: paint([]) cairo: paint([])
surface: write_to_png(["_output/save_sheet_1.png"]) surface: write_to_png(["_output/save_sheet_01.png"])
cairo: set_source([MockDouble, -37, -37]) cairo: set_source([MockDouble, -37, -37])
cairo: paint([]) cairo: paint([])
cairo: set_source([MockDouble, 75, 75]) cairo: set_source([MockDouble, 75, 75])
@ -690,7 +690,7 @@ cairo: set_source([MockDouble, -37, -37])
cairo: paint([]) cairo: paint([])
cairo: set_source([MockDouble, 180, 181]) cairo: set_source([MockDouble, 180, 181])
cairo: paint([]) cairo: paint([])
surface: write_to_png(["_output/save_sheet_range_0.png"]) surface: write_to_png(["_output/save_sheet_range_00.png"])
cairo: set_source([MockDouble, -37, -37]) cairo: set_source([MockDouble, -37, -37])
cairo: paint([]) cairo: paint([])
cairo: set_source([MockDouble, 75, 75]) cairo: set_source([MockDouble, 75, 75])
@ -699,4 +699,4 @@ cairo: set_source([MockDouble, -37, -37])
cairo: paint([]) cairo: paint([])
cairo: set_source([MockDouble, 180, 75]) cairo: set_source([MockDouble, 180, 75])
cairo: paint([]) cairo: paint([])
surface: write_to_png(["_output/save_sheet_range_1.png"]) surface: write_to_png(["_output/save_sheet_range_01.png"])

8
spec/data/samples/showcase.rb.txt

@ -5908,7 +5908,7 @@ cairo: restore([])
cairo: set_source([MockDouble, 385.0, 100]) cairo: set_source([MockDouble, 385.0, 100])
cairo: paint([]) cairo: paint([])
surface: write_to_png(["_output/showcase2.png"]) surface: write_to_png(["_output/showcase2.png"])
surface: write_to_png(["_output/showcase_individual_0.png"]) surface: write_to_png(["_output/showcase_individual_00.png"])
surface: write_to_png(["_output/showcase_individual_1.png"]) surface: write_to_png(["_output/showcase_individual_01.png"])
surface: write_to_png(["_output/showcase_individual_2.png"]) surface: write_to_png(["_output/showcase_individual_02.png"])
surface: write_to_png(["_output/showcase_individual_3.png"]) surface: write_to_png(["_output/showcase_individual_03.png"])

6
spec/data/samples/text_options.rb.txt

@ -844,6 +844,6 @@ cairo: set_source_color([:cyan])
cairo: set_line_width([2.0]) cairo: set_line_width([2.0])
cairo: stroke([]) cairo: stroke([])
cairo: restore([]) cairo: restore([])
surface: write_to_png(["_output/text_0.png"]) surface: write_to_png(["_output/text_00.png"])
surface: write_to_png(["_output/text_1.png"]) surface: write_to_png(["_output/text_01.png"])
surface: write_to_png(["_output/text_2.png"]) surface: write_to_png(["_output/text_02.png"])

2
spec/data/samples/tgc_proofs.rb.txt

@ -78,4 +78,4 @@ cairo: translate([0, 0])
cairo: set_source([ImageSurface, 0, 0]) cairo: set_source([ImageSurface, 0, 0])
cairo: paint([0.5]) cairo: paint([0.5])
cairo: restore([]) cairo: restore([])
surface: write_to_png(["_output/tgc_sample_0.png"]) surface: write_to_png(["_output/tgc_sample_00.png"])

2
spec/data/samples/units.rb.txt

@ -29,4 +29,4 @@ cairo: rounded_rectangle([472.4409444, 472.4409444, 100, 100, 0, 0])
cairo: set_source_color(["#0000"]) cairo: set_source_color(["#0000"])
cairo: fill([]) cairo: fill([])
cairo: restore([]) cairo: restore([])
surface: write_to_png(["_output/units_0.png"]) surface: write_to_png(["_output/units_00.png"])

Loading…
Cancel
Save