Finished porting of save_png over to args classes
parent
4aee48dada
commit
f8a18cdbce
|
|
@ -6,6 +6,7 @@ module Squib
|
||||||
|
|
||||||
# Saves the given range of cards to either PNG or PDF
|
# Saves the given range of cards to either PNG or PDF
|
||||||
#
|
#
|
||||||
|
#
|
||||||
# @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 [Symbol] format (:png) the format that this will be rendered too. Options `:pdf, :png`. Array of both is allowed: `[:pdf, :png]`
|
# @option opts [Symbol] format (:png) the format that this will be rendered too. Options `:pdf, :png`. Array of both is allowed: `[:pdf, :png]`
|
||||||
|
|
@ -24,6 +25,8 @@ module Squib
|
||||||
# @example
|
# @example
|
||||||
# save range: 1..8, dir: '_pnp', prefix: 'bw_'
|
# save range: 1..8, dir: '_pnp', prefix: 'bw_'
|
||||||
#
|
#
|
||||||
|
# Options support Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
|
||||||
|
#
|
||||||
# @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.
|
||||||
|
|
@ -34,10 +37,9 @@ module Squib
|
||||||
def save_png(opts = {})
|
def save_png(opts = {})
|
||||||
range = Args::CardRange.new(opts[:range], deck_size: size)
|
range = Args::CardRange.new(opts[:range], deck_size: size)
|
||||||
batch = Args::SaveBatch.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
batch = Args::SaveBatch.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
||||||
opts = needs(opts,[:range, :creatable_dir, :prefix, :count_format, :rotate])
|
@progress_bar.start("Saving PNGs to #{batch.dir}/#{batch.count_format}*", size) do |bar|
|
||||||
@progress_bar.start("Saving PNGs to #{opts[:dir]}/#{opts[:prefix]}*", @cards.size) do |bar|
|
|
||||||
range.each do |i|
|
range.each do |i|
|
||||||
@cards[i].save_png(i, opts[:dir], opts[:prefix], opts[:count_format], opts[:rotate], opts[:angle])
|
@cards[i].save_png(batch[i])
|
||||||
bar.increment
|
bar.increment
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ module Squib
|
||||||
class SaveBatch
|
class SaveBatch
|
||||||
include ArgLoader
|
include ArgLoader
|
||||||
|
|
||||||
def initialize #TODO DSL method default for prefix
|
def initialize
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.parameters
|
def self.parameters
|
||||||
|
|
@ -14,18 +14,19 @@ module Squib
|
||||||
prefix: 'card_',
|
prefix: 'card_',
|
||||||
count_format: '%02d',
|
count_format: '%02d',
|
||||||
rotate: false,
|
rotate: false,
|
||||||
|
angle: 0,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.expanding_parameters
|
def self.expanding_parameters
|
||||||
[] # none of them
|
self.parameters.keys # all of them
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.params_with_units
|
def self.params_with_units
|
||||||
[] # none of them
|
[] # none of them
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_dir(arg)
|
def validate_dir(arg, _i)
|
||||||
unless Dir.exists?(arg)
|
unless Dir.exists?(arg)
|
||||||
Squib.logger.warn("Dir '#{arg}' does not exist, creating it.")
|
Squib.logger.warn("Dir '#{arg}' does not exist, creating it.")
|
||||||
Dir.mkdir arg
|
Dir.mkdir arg
|
||||||
|
|
@ -33,6 +34,21 @@ module Squib
|
||||||
return arg
|
return arg
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def validate_rotate(arg, i)
|
||||||
|
case arg
|
||||||
|
when true, :clockwise
|
||||||
|
angle[i] = 0.5 * Math::PI
|
||||||
|
return true
|
||||||
|
when :counterclockwise
|
||||||
|
angle[i] = 1.5 * Math::PI
|
||||||
|
return true
|
||||||
|
when false
|
||||||
|
false
|
||||||
|
else
|
||||||
|
raise 'invalid option to rotate: only [true, false, :clockwise, :counterclockwise]'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,13 @@ module Squib
|
||||||
|
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
# @api private
|
# @api private
|
||||||
def save_png(i, dir, prefix, count_format, do_rotate, angle)
|
def save_png(batch)
|
||||||
if [true, :clockwise, :counterclockwise].include?(do_rotate)
|
surface = if batch.rotate
|
||||||
surface = rotated_image(angle)
|
rotated_image(batch.angle)
|
||||||
else
|
else
|
||||||
surface = @cairo_surface
|
surface = @cairo_surface
|
||||||
end
|
end
|
||||||
write_png(surface, i, dir, prefix, count_format)
|
write_png(surface, index, batch.dir, batch.prefix, batch.count_format)
|
||||||
end
|
end
|
||||||
|
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
|
|
|
||||||
|
|
@ -5,17 +5,47 @@ describe Squib::Args::SaveBatch do
|
||||||
subject(:save_batch) {Squib::Args::SaveBatch.new}
|
subject(:save_batch) {Squib::Args::SaveBatch.new}
|
||||||
|
|
||||||
context 'dir' do
|
context 'dir' do
|
||||||
|
|
||||||
it 'is created if not exists (and warns)' do
|
it 'is created if not exists (and warns)' do
|
||||||
opts = {dir: 'tocreate'}
|
opts = {dir: 'tocreate'}
|
||||||
Dir.chdir(output_dir) do
|
Dir.chdir(output_dir) do
|
||||||
FileUtils.rm_rf('tocreate', secure: true)
|
FileUtils.rm_rf('tocreate', secure: true)
|
||||||
expect(Squib.logger).to receive(:warn).with("Dir 'tocreate' does not exist, creating it.").once
|
expect(Squib.logger).to receive(:warn).with("Dir 'tocreate' does not exist, creating it.").once
|
||||||
save_batch.load! opts
|
save_batch.load! opts
|
||||||
expect(save_batch).to have_attributes({dir: 'tocreate'})
|
expect(save_batch).to have_attributes({dir: ['tocreate']})
|
||||||
expect(Dir.exists? 'tocreate').to be true
|
expect(Dir.exists? 'tocreate').to be true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'rotate' do
|
||||||
|
it 'does nothing by default' do
|
||||||
|
opts = {}
|
||||||
|
save_batch.load! opts
|
||||||
|
expect(save_batch[0]).to have_attributes({rotate: false, angle: 0})
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'rotates by pi/2 with true' do
|
||||||
|
opts = {rotate: true}
|
||||||
|
save_batch.load! opts
|
||||||
|
expect(save_batch[0]).to have_attributes({rotate: true, angle: Math::PI / 2})
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'rotates by pi/2' do
|
||||||
|
opts = {rotate: :clockwise}
|
||||||
|
save_batch.load! opts
|
||||||
|
expect(save_batch[0]).to have_attributes({rotate: true, angle: Math::PI / 2})
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'rotates by pi/2 with counterclockwise' do
|
||||||
|
opts = {rotate: :counterclockwise}
|
||||||
|
save_batch.load! opts
|
||||||
|
expect(save_batch[0]).to have_attributes({rotate: true, angle: 3 * Math::PI / 2})
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'raises error on a number' do
|
||||||
|
opts = {rotate: 5.0}
|
||||||
|
expect { save_batch.load!(opts) }.to raise_error('invalid option to rotate: only [true, false, :clockwise, :counterclockwise]')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue