9 changed files with 97 additions and 75 deletions
@ -1,8 +1,14 @@ |
|||||||
module Squib |
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 |
end |
||||||
@ -1,73 +1,97 @@ |
|||||||
|
require 'squib/constants' |
||||||
|
|
||||||
module Squib |
module Squib |
||||||
module InputHelpers |
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? |
raise 'Range cannot be nil' if range.nil? |
||||||
range = 0..(@cards.size-1) if range == :all |
range = 0..(@cards.size-1) if range == :all |
||||||
range = range..range if range.is_a? Integer |
range = range..range if range.is_a? Integer |
||||||
if range.max > (@cards.size-1) |
if range.max > (@cards.size-1) |
||||||
raise "#{range} is outside of deck range of 0..#{@cards.size-1}" |
raise "#{range} is outside of deck range of 0..#{@cards.size-1}" |
||||||
end |
end |
||||||
return range |
opts[:range] = range |
||||||
|
opts |
||||||
end |
end |
||||||
module_function :rangeify |
module_function :rangeify |
||||||
|
|
||||||
def fileify(file) |
def fileify(opts, expand_singletons=false) |
||||||
raise "File #{File.expand_path(file)} does not exist!" unless File.exists? file |
opts[:file] = [opts[:file]] * @cards.size if expand_singletons |
||||||
file |
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 |
end |
||||||
module_function :fileify |
module_function :fileify |
||||||
|
|
||||||
def dirify(dir, allow_create: false) |
def dirify(opts, allow_create=false) |
||||||
return dir if Dir.exists? dir |
return opts if Dir.exists? opts[:dir] |
||||||
if allow_create |
if allow_create |
||||||
Squib.logger.warn "PDF output dir #{dir} does not exist... attempting to create it" |
Squib.logger.warn "Dir #{opts[:dir]} does not exist, creating it." |
||||||
Dir.mkdir dir |
Dir.mkdir opts[:dir] |
||||||
return dir |
return opts |
||||||
else |
else |
||||||
raise "#{dir} does not exist!" |
raise "#{opts[:dir]} does not exist!" |
||||||
end |
end |
||||||
end |
end |
||||||
module_function :dirify |
module_function :dirify |
||||||
|
|
||||||
|
|
||||||
def colorify(color, nillable: false) |
def colorify(opts, nillable=false) |
||||||
if nillable # for optional color arguments like text hints |
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 |
else |
||||||
color ||= :black |
opts[:color] = Cairo::Color.parse(opts[:color]) |
||||||
color = Cairo::Color.parse(color) |
|
||||||
end |
end |
||||||
color |
opts |
||||||
end |
end |
||||||
module_function :colorify |
module_function :colorify |
||||||
|
|
||||||
def fontify (font) |
def fontify (opts) |
||||||
font = @font if font==:use_set |
opts[:font] = @font if opts[:font]==:use_set |
||||||
font = Squib::DEFAULT_FONT if font==:default |
opts[:font] = Squib::SYSTEM_DEFAULTS[:font] if opts[:font] ==:default |
||||||
font |
opts |
||||||
end |
end |
||||||
module_function :fontify |
module_function :fontify |
||||||
|
|
||||||
def radiusify(radius, x_radius, y_radius) |
def radiusify(opts) |
||||||
if radius.nil? |
unless opts[:radius].nil? |
||||||
return x_radius, y_radius |
opts[:x_radius] = opts[:radius] |
||||||
else |
opts[:y_radius] = opts[:radius] |
||||||
return radius,radius |
|
||||||
end |
end |
||||||
|
opts |
||||||
end |
end |
||||||
module_function :radiusify |
module_function :radiusify |
||||||
|
|
||||||
def idify(svgid) |
def svgidify(opts) |
||||||
unless svgid.nil? |
unless opts[:id].nil? |
||||||
svgid = '#' << svgid unless svgid.start_with? '#' |
opts[:id] = '#' << opts[:id] unless svgid.start_with? '#' |
||||||
end |
end |
||||||
svgid |
opts |
||||||
end |
end |
||||||
module_function :idify |
module_function :idify |
||||||
|
|
||||||
def xyify |
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 |
||||||
|
|
||||||
end |
end |
||||||
|
|||||||
Loading…
Reference in new issue