From 56905b08bc5c93970187efc939dd18872fc7cbf0 Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Thu, 21 Aug 2014 15:44:33 -0400 Subject: [PATCH] Implementing an img_dir configuration setting --- lib/squib/api/image.rb | 24 +++-- lib/squib/api/settings.rb | 6 +- lib/squib/constants.rb | 2 + lib/squib/deck.rb | 1 + lib/squib/input_helpers.rb | 13 +-- lib/squib/project_template/config.yml | 6 +- samples/custom-config.yml | 3 +- samples/custom_config.rb | 5 +- samples/customconfig-imgdir/shiny-purse2.png | Bin 0 -> 3430 bytes samples/customconfig-imgdir/spanner2.svg | 91 +++++++++++++++++++ spec/input_helpers_spec.rb | 2 +- 11 files changed, 130 insertions(+), 23 deletions(-) create mode 100644 samples/customconfig-imgdir/shiny-purse2.png create mode 100644 samples/customconfig-imgdir/spanner2.svg 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 0000000000000000000000000000000000000000..477ef93263039167c288100ed46d4c6bfb9bd500 GIT binary patch literal 3430 zcmV-s4Vm(ZP)y?!LEg%)kRelX}TmC4$dH0|NH6MkL!aV-$W*{jppWRwu!_a_X+j9O z3D^3mfx=XT@v|kbe==$O>>|l)!Q)Cy%>7>w{#2TnTdAXfe54o@(yT$NAcgs=3b-+3 zR>a%}t^)AJkb(k6JYh zYUY_t@~$7T7FcNeb!j6>vaLprBvLz%-M+xl^9LtKUx8m zMgw1Tx;!k-az*^!tb;MW*jI6i#X+&stpGb_&zTW%u2;g}=TN{=AKX-uqpg}o4h0m! z7TG$;@Wq`)H~dA4T}JW}t35$codWFH{BK0WIpBlYlCf}(5PK0x@_fs4gs=iiVUOq{ zhdAq{m}@;0utR`5z*j+LsT4#au*~8F^i9X_!MI(BHyFb_r>~Pdo+jhf@UJ75r_0Mu zPKQGlb=ooNoAwgm9i%tZvXP`&C0VWkAre*_fV&$c$zx3Rz`2iNSQdpl+wso6NmL3e z29Pto8zs=YF&Jr-gR`sa=6Yja0r^PW7P8x}-iZ3Y7aojyvf78j3y$Ag9KVmp!D+TF z6lnhEm9F>rrh}b>5B*_g7Vf*vu?GH^hXwMH>d4lCy! zh$OtGgjtQm6Pj$T15t`cFa}K&j3JwF$d|WaScvT)HleL@JwuRjejoqNUo$ldxjC{jM)=ZYM%1St)M_HehClwia(7N4rhf=u(@m9dK3QTP0S#j~` zx8$q7WzUYc0?zbhVL@D%HE!`Ky^j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#4-;Lc5;v! zkc$ez7PMpyxo0?Y1#=sg6AY}AJX3Z8gF<|5UKt?rt3F>xHTB{8}Hl>~FI6QY|{I|@2uQ|R;cM0Bci{D@D zh->58WM3~^W`?>(hXjqKbyJX?AhO$m*Ry^UFyF=8tHv&B8y%q2jlW)B7BdBGjbiUk zA?6e-;x87`zsiNTuf7xz3*sXb*9R8=L#4PtllWyr1@!V{^D-g+u0Z1Nm27{Dg;AQa z7!8BoCw#mjoE~+@cH_bCEu;%WD2kPNvpfv0Ratb5N{<_T{dI z#6`MaOZcslMc65Z_M#O9bo60-3r~}TTYju0e1T|>7eVk@ligwV6foJB{qLlQ81&>r zQ5#;JP{^C50T}7N^=Mwa4^UW+s}-=G#AJsX-Rqcxi_sOGkcy@N4&h6b;zN}BP+fpo zz9ko8P<%VVD#bQj!)vVuKn~+!3$w<+O`xUb82&3O!VisXepZ3*I(C4r=Wd7=b_YC- zDohPD4Za%XD9~DxSGNTkLHkhk#nM=wH-zt==P>Q>-k-}nY2D9^spCl+_ABv` zQ>1yX#1N9Te1*3Q91q(VKN#W5T~|)$3p}n7OmSmK^I+xfJ~4s^Tl|$9be#6yJ5tj;)K9>Q?3bE?w&>P{2s(*#JYVi63*(lAWuzPQfY#+Dyu4;N39s8Df?c>c zmgE68*2v+U(|=J<;!7`F3|0$nV~u7dKF6Veem}z37q|pX`+~=Ko_3y7XH~7N_qH$f ziJD!zd%6)*M@f=R;x4?#wYw+ng8I66c{IssaW6&l;$?;n + + + + + 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