Starting work on the new options

This commit is contained in:
Andy Meneely
2014-07-25 13:10:04 -04:00
parent a017330d0b
commit 6c60476e42
9 changed files with 97 additions and 75 deletions
+7 -6
View File
@@ -2,13 +2,14 @@ module Squib
class Deck
# Fills the background with the given color
#
# @param range: the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
# @param color: the color the font will render to. See {file:API.md#label-Specifying+Colors Specifying Colors}
# @params opts: the parameter hash.
# @option range: the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
# @option color: the color the font will render to. See {file:API.md#label-Specifying+Colors Specifying Colors}
# @return [nil]
# @api public
def background(range: :all, color: :white)
range = rangeify(range)
color = colorify(color)
range.each { |i| @cards[i].background(color) }
def background(opts = {})
opts = needs(opts,[:range, :color])
opts[:range].each { |i| @cards[i].background(opts[:color]) }
end
end
+2 -1
View File
@@ -15,7 +15,8 @@ module Squib
# @param file: [String] the file to open. Must end in `.xlsx`. Opens relative to the current directory.
# @param sheet: [Integer] The zero-based index of the sheet from which to read.
# @api public
def xlsx(file: 'deck.xlsx', sheet: 0)
def xlsx(opts = {})
needs(opts, [:file, :sheet])
s = Roo::Excelx.new(file)
s.default_sheet = s.sheets[sheet]
data = {}
+12 -10
View File
@@ -6,15 +6,17 @@ module Squib
# Note: scaling not currently supported for PNGs.
#
# @param range: the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
# @param file: the . See {file:API.md#Specifying+Files Specifying Files}
# @param file: file(s) to read in. If it's a single file, then it's use for every card. If the parameter is an Array of files, then each file is looked up for each card. See {file:API.md#Specifying+Files Specifying Files}
# @param x: the x-coordinate to place
# @param y: the y-coordinate to place
# @param alpha: the alpha-transparency percentage used to blend this image
# @return [nil] intended to be void
# @api public
def png(range: :all, file: nil, x: 0, y: 0, alpha: 1.0)
range = rangeify(range)
file = fileify(file)
range.each{ |i| @cards[i].png(file, x, y, alpha) }
def png(opts = {})
opts = needs(opts, [:range, :files, :x, :y, :alpha])
opts[:range].each do |i|
@cards[i].png(opts[:file][i], opts[:x], opts[:y], opts:[alpha])
end
end
# Renders an entire svg file at the given location. Uses the SVG-specified units and DPI to determine the pixel width and height.
@@ -32,11 +34,11 @@ module Squib
# @param height: the pixel width that the image should scale to. SVG scaling is done with vectors, so the scaling should be smooth. When set to `:native`, uses the DPI and units of the loaded SVG document.
# @return [nil] essentially a void method
# @api public
def svg(range: :all, file: nil, id: nil, x: 0, y: 0, width: :native, height: :native)
range = rangeify(range)
file = fileify(file)
id = idify(id)
range.each{ |i| @cards[i].svg(file, id, x, y, width, height) }
def svg(opts = {})
p = needs(opts,[:range, :files, :svgid, :x, :y, :width, :height])
p[:range].each do |i|
@cards[i].svg(p[:file], p[:id], p[:x], p[:y], p[:width], p[:height])
end
end
end
+11 -5
View File
@@ -1,8 +1,14 @@
module Squib
module Constants
#@api public
DEFAULT_FONT = 'Arial 36 B'
end
#@api public
SYSTEM_DEFAULTS = {
:range => :all,
:color => :white,
:font => 'Arial, Sans 36',
:sheet => 0,
:x => 0,
:y => 0,
:alpha => 1.0
}
end
+54 -30
View File
@@ -1,73 +1,97 @@
require 'squib/constants'
module Squib
module InputHelpers
def rangeify (range)
def needs(opts, params)
opts = Squib::SYSTEM_DEFAULTS.merge(opts)
opts = rangeify(opts) if params.include? :range
opts = fileify(opts) if params.include? :file
opts = fileify(opts, true) if params.include? :files
opts = colorify(opts) if params.include? :color
opts = colorify(opts, true) if params.include? :nillable_color
opts = dirify(opts) if params.include? :dir
opts = dirify(opts, true) if params.include? :creatable_dir
opts = fontify(opts) if params.include? :font
opts = radiusify(opts) if params.include? :radius
opts = svgidify(opts) if params.include? :svgid
end
module_function :needs
def rangeify (opts)
range = opts[:range]
raise 'Range cannot be nil' if range.nil?
range = 0..(@cards.size-1) if range == :all
range = range..range if range.is_a? Integer
if range.max > (@cards.size-1)
raise "#{range} is outside of deck range of 0..#{@cards.size-1}"
end
return range
opts[:range] = range
opts
end
module_function :rangeify
def fileify(file)
raise "File #{File.expand_path(file)} does not exist!" unless File.exists? file
file
def fileify(opts, expand_singletons=false)
opts[:file] = [opts[:file]] * @cards.size if expand_singletons
files = [opts[:file]].flatten
files.each do |file|
unless File.exists? file
raise "File #{File.expand_path(file)} does not exist!"
end
end
opts
end
module_function :fileify
def dirify(dir, allow_create: false)
return dir if Dir.exists? dir
def dirify(opts, allow_create=false)
return opts if Dir.exists? opts[:dir]
if allow_create
Squib.logger.warn "PDF output dir #{dir} does not exist... attempting to create it"
Dir.mkdir dir
return dir
Squib.logger.warn "Dir #{opts[:dir]} does not exist, creating it."
Dir.mkdir opts[:dir]
return opts
else
raise "#{dir} does not exist!"
raise "#{opts[:dir]} does not exist!"
end
end
module_function :dirify
def colorify(color, nillable: false)
def colorify(opts, nillable=false)
if nillable # for optional color arguments like text hints
color = Cairo::Color.parse(color) unless color.nil?
opts[:color] = Cairo::Color.parse(opts[:color]) unless opts[:color].nil?
else
color ||= :black
color = Cairo::Color.parse(color)
opts[:color] = Cairo::Color.parse(opts[:color])
end
color
opts
end
module_function :colorify
def fontify (font)
font = @font if font==:use_set
font = Squib::DEFAULT_FONT if font==:default
font
def fontify (opts)
opts[:font] = @font if opts[:font]==:use_set
opts[:font] = Squib::SYSTEM_DEFAULTS[:font] if opts[:font] ==:default
opts
end
module_function :fontify
def radiusify(radius, x_radius, y_radius)
if radius.nil?
return x_radius, y_radius
else
return radius,radius
def radiusify(opts)
unless opts[:radius].nil?
opts[:x_radius] = opts[:radius]
opts[:y_radius] = opts[:radius]
end
opts
end
module_function :radiusify
def idify(svgid)
unless svgid.nil?
svgid = '#' << svgid unless svgid.start_with? '#'
def svgidify(opts)
unless opts[:id].nil?
opts[:id] = '#' << opts[:id] unless svgid.start_with? '#'
end
svgid
opts
end
module_function :idify
def xyify
#TODO: Allow negative numbers that subtract from the card width & height
#TODO: Allow negative numbers that subtract from the card width & height.
end
end