diff --git a/lib/squib/api/image.rb b/lib/squib/api/image.rb index ca214ea..fd786b1 100644 --- a/lib/squib/api/image.rb +++ b/lib/squib/api/image.rb @@ -19,10 +19,12 @@ module Squib # @api public def png(opts = {}) opts = needs(opts, [:range, :files, :x, :y, :alpha, :layout, :blend]) - @progress_bar.start("Loading PNG(s)", opts[:range].size) do |bar| - opts[:range].each do |i| - @cards[i].png(opts[:file][i], opts[:x][i], opts[:y][i], opts[:alpha][i], opts[:blend][i]) - bar.increment + Dir.chdir(@img_dir) do + @progress_bar.start("Loading PNG(s)", opts[:range].size) do |bar| + opts[:range].each do |i| + @cards[i].png(opts[:file][i], opts[:x][i], opts[:y][i], opts[:alpha][i], opts[:blend][i]) + bar.increment + end end end end @@ -48,13 +50,15 @@ module Squib # @api public def svg(opts = {}) p = needs(opts,[:range, :files, :svgid, :force_svgid, :x, :y, :width, :height, :layout, :alpha, :blend]) - @progress_bar.start("Loading SVG(s)", p[:range].size) do |bar| - p[:range].each do |i| - unless p[:force_id][i] && p[:id][i].to_s.empty? - @cards[i].svg(p[:file][i], p[:id][i], p[:x][i], p[:y][i], - p[:width][i], p[:height][i], p[:alpha][i], p[:blend][i]) + Dir.chdir(@img_dir) do + @progress_bar.start("Loading SVG(s)", p[:range].size) do |bar| + p[:range].each do |i| + unless p[:force_id][i] && p[:id][i].to_s.empty? + @cards[i].svg(p[:file][i], p[:id][i], p[:x][i], p[:y][i], + p[:width][i], p[:height][i], p[:alpha][i], p[:blend][i]) + end + bar.increment end - bar.increment end end end diff --git a/lib/squib/api/settings.rb b/lib/squib/api/settings.rb index 9461aab..12a3425 100644 --- a/lib/squib/api/settings.rb +++ b/lib/squib/api/settings.rb @@ -17,7 +17,7 @@ module Squib @text_hint = text end - # Sets various defaults for this deck. Defaults can be overriden by the commands themselves + # Sets various defaults for this deck. Defaults can be overriden by the commands themselves when that command supports it. # @example # set font: 'Arial 26' # text 'blah' # in Arial 26 @@ -25,11 +25,13 @@ module Squib # set font: :default # Back to Squib-wide default # # @option opts font: the font string to set as default. Can also be set to `:default` to use the Squib-wide default. + # @option opts img_dir: the default directory to READ images from. Default is `.`. Useful for switching from bw to color images. # @return [nil] Returns nothing # @api public def set(opts = {}) - opts = needs(opts, [:font]) + opts = needs(opts, [:font, :img_dir]) @font = opts[:font][0] #was expanded - just need the first + @img_dir = opts[:img_dir] end end diff --git a/lib/squib/constants.rb b/lib/squib/constants.rb index e8b505b..a9f9482 100644 --- a/lib/squib/constants.rb +++ b/lib/squib/constants.rb @@ -17,6 +17,7 @@ module Squib :gap => 0, :height => :native, :hint => :off, + :img_dir => '.', :justify => false, :margin => 75, :markup => false, @@ -53,6 +54,7 @@ module Squib 'dpi' => 300, 'hint' => :none, 'progress_bar' => false, + 'img_dir' => '.', } # These are parameters that are intended to be "expanded" across diff --git a/lib/squib/deck.rb b/lib/squib/deck.rb index 88b74ba..461a8d1 100644 --- a/lib/squib/deck.rb +++ b/lib/squib/deck.rb @@ -96,6 +96,7 @@ module Squib @text_hint = config['text_hint'] @progress_bar.enabled = config['progress_bars'] @custom_colors = config['custom_colors'] + @img_dir = config['img_dir'] end end diff --git a/lib/squib/input_helpers.rb b/lib/squib/input_helpers.rb index fdaee21..1e10f48 100644 --- a/lib/squib/input_helpers.rb +++ b/lib/squib/input_helpers.rb @@ -16,7 +16,8 @@ module Squib opts = fileify(opts) if params.include? :file opts = fileify(opts, false) if params.include? :file_to_save opts = colorify(opts, true) if params.include? :nillable_color - opts = dirify(opts, true) if params.include? :creatable_dir + opts = dirify(opts, :dir, true) if params.include? :creatable_dir + opts = dirify(opts, :img_dir, false) if params.include? :img_dir opts = fileify(opts, false) if params.include? :files opts = colorify(opts) if params.include? :color opts = colorify(opts, false, :fill_color) if params.include? :fill_color @@ -107,14 +108,14 @@ module Squib # :nodoc: # @api private - def dirify(opts, allow_create=false) - return opts if Dir.exists?(opts[:dir]) + def dirify(opts, key, allow_create=false) + return opts if Dir.exists?(opts[key]) if allow_create - Squib.logger.warn {"Dir #{opts[:dir]} does not exist, creating it."} - Dir.mkdir opts[:dir] + Squib.logger.warn {"Dir #{opts[key]} does not exist, creating it."} + Dir.mkdir opts[key] return opts else - raise "'#{opts[:dir]}' does not exist!" + raise "'#{opts[key]}' does not exist!" end end module_function :dirify diff --git a/lib/squib/project_template/config.yml b/lib/squib/project_template/config.yml index 33c8bec..55cefc0 100644 --- a/lib/squib/project_template/config.yml +++ b/lib/squib/project_template/config.yml @@ -13,4 +13,8 @@ #Enable some custom colors that can be used in any color #custom_colors: -# foo: '#abc' \ No newline at end of file +# foo: '#abc' + +#For reading image file command (e.g. png and svg), read from this directory instead +#img_dir: img-color +#img_dir: img-bw \ No newline at end of file diff --git a/samples/custom-config.yml b/samples/custom-config.yml index e593144..d23a052 100644 --- a/samples/custom-config.yml +++ b/samples/custom-config.yml @@ -2,4 +2,5 @@ dpi: 300 progress_bars: true text_hint: '#FF0000' custom_colors: - foo: '#ccc' \ No newline at end of file + foo: '#ccc' +img_dir: customconfig-imgdir \ No newline at end of file diff --git a/samples/custom_config.rb b/samples/custom_config.rb index 95a652b..5637381 100644 --- a/samples/custom_config.rb +++ b/samples/custom_config.rb @@ -10,8 +10,9 @@ Squib::Deck.new(config: 'custom-config.yml') do font: 'Arial 72', align: :center # Progress bars are shown for these commands - png file: 'shiny-purse.png', x: 620, y: 75 - svg file: 'spanner.svg', x: 620, y: 218 + # And images are taken from config-imgdir + png file: 'shiny-purse2.png', x: 620, y: 75 + svg file: 'spanner2.svg', x: 620, y: 218 save_png prefix: 'custom-config_' save_pdf file: "custom-config-out.pdf" diff --git a/samples/customconfig-imgdir/shiny-purse2.png b/samples/customconfig-imgdir/shiny-purse2.png new file mode 100644 index 0000000..477ef93 Binary files /dev/null and b/samples/customconfig-imgdir/shiny-purse2.png differ diff --git a/samples/customconfig-imgdir/spanner2.svg b/samples/customconfig-imgdir/spanner2.svg new file mode 100644 index 0000000..1f3a747 --- /dev/null +++ b/samples/customconfig-imgdir/spanner2.svg @@ -0,0 +1,91 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + diff --git a/spec/input_helpers_spec.rb b/spec/input_helpers_spec.rb index 32bde75..c475724 100644 --- a/spec/input_helpers_spec.rb +++ b/spec/input_helpers_spec.rb @@ -85,7 +85,7 @@ describe Squib::InputHelpers do context "#dir" do it "should raise an error if the directory does not exist" do - expect{@deck.send(:dirify, {dir: 'nonexist'}, false)}.to \ + expect{@deck.send(:dirify, {dir: 'nonexist'}, :dir, false)}.to \ raise_error(RuntimeError,"'nonexist' does not exist!") end end