Browse Source

Implementing an img_dir configuration setting

dev
Andy Meneely 11 years ago
parent
commit
56905b08bc
  1. 24
      lib/squib/api/image.rb
  2. 6
      lib/squib/api/settings.rb
  3. 2
      lib/squib/constants.rb
  4. 1
      lib/squib/deck.rb
  5. 13
      lib/squib/input_helpers.rb
  6. 6
      lib/squib/project_template/config.yml
  7. 3
      samples/custom-config.yml
  8. 5
      samples/custom_config.rb
  9. BIN
      samples/customconfig-imgdir/shiny-purse2.png
  10. 91
      samples/customconfig-imgdir/spanner2.svg
  11. 2
      spec/input_helpers_spec.rb

24
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

6
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

2
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

1
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

13
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

6
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'
# 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

3
samples/custom-config.yml

@ -2,4 +2,5 @@ dpi: 300
progress_bars: true
text_hint: '#FF0000'
custom_colors:
foo: '#ccc'
foo: '#ccc'
img_dir: customconfig-imgdir

5
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"

BIN
samples/customconfig-imgdir/shiny-purse2.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

91
samples/customconfig-imgdir/spanner2.svg

@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
viewBox="0 0 128 128"
id="svg2"
version="1.1"
inkscape:version="0.48.3.1 r9886"
width="100%"
height="100%"
sodipodi:docname="spanner.svg">
<metadata
id="metadata18">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
</cc:Work>
</rdf:RDF>
</metadata>
<defs
id="defs16" />
<sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:window-width="1280"
inkscape:window-height="1002"
id="namedview14"
showgrid="false"
inkscape:zoom="2.8284271"
inkscape:cx="98.928938"
inkscape:cy="62.194589"
inkscape:window-x="1912"
inkscape:window-y="-8"
inkscape:window-maximized="1"
inkscape:current-layer="g3767"
inkscape:document-units="in"
units="px"
showguides="true"
inkscape:guide-bbox="true" />
<g
id="g3767"
transform="matrix(0.24961486,0,0,0.24961486,0.197194,96.049335)">
<rect
ry="67.108368"
y="-378.68747"
x="5.3126979"
height="501.3746"
width="501.3746"
id="backdrop"
style="fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:10.62539577;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0"
inkscape:label="#rect3765" />
<path
style="fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none"
d="M 36.267098,-7.709219 C 16.393265,5.1318032 3.2161811,27.462365 3.2161811,52.884128 c 0,39.825874 32.2852369,72.111092 72.1110909,72.111092 23.870397,0 44.964938,-11.67699 58.089488,-29.545518 -11.26063,7.275808 -24.65607,11.517748 -39.060173,11.517748 -39.825853,0 -72.11109,-32.285221 -72.11109,-72.111095 0,-15.95546 5.248882,-30.6218942 14.021601,-42.565574 z"
id="path3790"
inkscape:connector-curvature="0" />
<g
id="g6"
transform="matrix(18.68858,0,0,18.68858,-3385.6437,-2608.3849)">
<path
style="fill:#ffffff"
inkscape:connector-curvature="0"
id="path8"
d="m 192.2325,129.3203 -10,10 v 5.5625 h 5.375 l 10.0938,-10.0938 -5.4688,-5.4688 z" />
<path
style="fill:#ffffff;stroke:#000000"
inkscape:connector-curvature="0"
id="path10"
d="m 181.7796,141.8591 15.6196,-15.6178 m 3.349,3.3486 -15.8051,15.8032" />
<path
style="fill:#ffffff"
inkscape:connector-curvature="0"
id="path12"
d="m 201.2023,119.7104 c -2.8283,-0.73 -5.9623,-0.004 -8.1759,2.2097 -3.3293,3.3292 -3.3293,8.7358 -2e-5,12.065 3.32928,3.3292 8.7358,3.3292 12.065,0 2.216,-2.216 2.9436,-5.3452 2.2097,-8.1759 l -5.6348,5.6348 -4.8172,-1.2816 -1.2816,-4.8171 5.6348,-5.6348 z" />
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 3.4 KiB

2
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

Loading…
Cancel
Save