Singleton expansion has... expanded!

Closes #13, hopefully
This commit is contained in:
Andy Meneely
2014-08-11 00:22:04 -04:00
parent 91bcfb06e2
commit 3e08a0d604
12 changed files with 222 additions and 126 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ module Squib
# @api public
def background(opts = {})
opts = needs(opts,[:range, :color])
opts[:range].each { |i| @cards[i].background(opts[:color]) }
opts[:range].each { |i| @cards[i].background(opts[:color][i]) }
end
end
+3 -2
View File
@@ -20,7 +20,7 @@ module Squib
opts = needs(opts, [:range, :files, :x, :y, :alpha, :layout])
@progress_bar.start("Loading PNGs #{location(opts)}", opts[:range].size) do |bar|
opts[:range].each do |i|
@cards[i].png(opts[:file][i], opts[:x], opts[:y], opts[:alpha])
@cards[i].png(opts[:file][i], opts[:x][i], opts[:y][i], opts[:alpha][i])
bar.increment
end
end
@@ -46,7 +46,8 @@ module Squib
p = needs(opts,[:range, :files, :svgid, :x, :y, :width, :height, :layout])
@progress_bar.start("Loading SVGs #{location(opts)}", p[:range].size) do |bar|
p[:range].each do |i|
@cards[i].svg(p[:file][i], p[:id], p[:x], p[:y], p[:width], p[:height])
@cards[i].svg(p[:file][i], p[:id][i], p[:x][i], p[:y][i],
p[:width][i], p[:height][i])
bar.increment
end
end
+1 -1
View File
@@ -30,7 +30,7 @@ module Squib
# @api public
def set(opts = {})
opts = needs(opts, [:font])
@font = opts[:font]
@font = opts[:font][0] #was expanded - just need the first
end
end
+14 -10
View File
@@ -21,12 +21,13 @@ module Squib
# @return [nil] intended to be void
# @api public
def rect(opts = {})
opts = needs(opts, [:range, :x, :y, :width, :height, :radius,
opts = needs(opts, [:range, :x, :y, :width, :height, :rect_radius, :x_radius, :y_radius,
:fill_color, :stroke_color, :stroke_width, :layout])
opts[:range].each do |i|
@cards[i].rect(opts[:x], opts[:y], opts[:width], opts[:height],
opts[:x_radius], opts[:y_radius],
opts[:fill_color], opts[:stroke_color], opts[:stroke_width])
@cards[i].rect(opts[:x][i], opts[:y][i], opts[:width][i], opts[:height][i],
opts[:x_radius][i], opts[:y_radius][i],
opts[:fill_color][i], opts[:stroke_color][i],
opts[:stroke_width][i])
end
end
@@ -49,8 +50,8 @@ module Squib
opts = needs(opts, [:range, :x, :y, :circle_radius, :layout,
:fill_color, :stroke_color, :stroke_width])
opts[:range].each do |i|
@cards[i].circle(opts[:x], opts[:y], opts[:radius],
opts[:fill_color], opts[:stroke_color], opts[:stroke_width])
@cards[i].circle(opts[:x][i], opts[:y][i], opts[:radius][i],
opts[:fill_color][i], opts[:stroke_color][i], opts[:stroke_width][i])
end
end
@@ -75,8 +76,11 @@ module Squib
opts = needs(opts, [:range, :x1, :y1, :x2, :y2, :x3, :y3, :layout,
:fill_color, :stroke_color, :stroke_width])
opts[:range].each do |i|
@cards[i].triangle(opts[:x1], opts[:y1], opts[:x2], opts[:y2],opts[:x3], opts[:y3],
opts[:fill_color], opts[:stroke_color], opts[:stroke_width])
@cards[i].triangle(opts[:x1][i], opts[:y1][i],
opts[:x2][i], opts[:y2][i],
opts[:x3][i], opts[:y3][i],
opts[:fill_color][i], opts[:stroke_color][i],
opts[:stroke_width][i])
end
end
@@ -98,8 +102,8 @@ module Squib
opts = needs(opts, [:range, :x1, :y1, :x2, :y2, :layout,
:stroke_color, :stroke_width])
opts[:range].each do |i|
@cards[i].line(opts[:x1], opts[:y1], opts[:x2], opts[:y2],
opts[:stroke_color], opts[:stroke_width])
@cards[i].line(opts[:x1][i], opts[:y1][i], opts[:x2][i], opts[:y2][i],
opts[:stroke_color][i], opts[:stroke_width][i])
end
end
+6 -3
View File
@@ -35,10 +35,13 @@ module Squib
# @api public
def text(opts = {})
opts = needs(opts, [:range, :str, :font, :x, :y, :width, :height, :color, :wrap,
:align, :justify, :valign, :ellipsize, :hint, :layout])
opts[:str] = [opts[:str]] * @cards.size unless opts[:str].respond_to? :each
:align, :justify, :valign, :markup, :ellipsize, :hint, :layout])
opts[:range].each do |i|
@cards[i].text(opts[:str][i], opts[:font], opts[:x], opts[:y], opts[:color], opts)
@cards[i].text(opts[:str][i], opts[:font][i], opts[:color][i],
opts[:x][i], opts[:y][i], opts[:width][i], opts[:height][i],
opts[:markup][i], opts[:justify][i], opts[:wrap][i],
opts[:ellipsize][i], opts[:spacing][i], opts[:align][i],
opts[:valign][i], opts[:hint][i])
end
end
+76 -32
View File
@@ -3,51 +3,95 @@ module Squib
#
# @api public
SYSTEM_DEFAULTS = {
:range => :all,
:align => :left,
:alpha => 1.0,
:color => :black,
:str => '',
:default_font => 'Arial 36',
:dir => "_output",
:ellipsize => :end,
:fill_color => '#0000',
:font => :use_set,
:format => :png,
:gap => 0,
:height => :native,
:justify => false,
:margin => 75,
:markup => false,
:prefix => "card_",
:progress_bar => false,
:range => :all,
:sheet => 0,
:spacing => 0,
:str => '',
:stroke_color => :black,
:stroke_width => 2.0,
:font => :use_set,
:default_font => 'Arial 36',
:sheet => 0,
:x => 0,
:y => 0,
:x1 => 100,
:y1 => 100,
:x2 => 150,
:y2 => 150,
:x3 => 100,
:y3 => 150,
:x_radius => 0,
:y_radius => 0,
:align => :left,
:valign => :top,
:justify => false,
:ellipsize => :end,
:wrap => true,
:width => :native,
:height => :native,
:spacing => 0,
:alpha => 1.0,
:format => :png,
:dir => "_output",
:prefix => "card_",
:margin => 75,
:gap => 0,
:trim => 0,
:progress_bar => false
:valign => :top,
:width => :native,
:wrap => true,
:x => 0,
:x1 => 100,
:x2 => 150,
:x3 => 100,
:x_radius => 0,
:y => 0,
:y1 => 100,
:y2 => 150,
:y3 => 150,
:y_radius => 0,
}
# Squib's configuration defaults
#
# @api public
CONFIG_DEFAULTS = {
'custom_colors' => {},
'dpi' => 300,
'progress_bar' => false,
'hint' => nil,
'custom_colors' => {}
'progress_bar' => false,
}
# These are parameters that are intended to be "expanded" across
# range if they are singletons.
#
# For example, using a different font for each card, using one `text`
#
# key: the internal name of the param (e.g. :files)
# value: the user-facing API key (e.g. file: 'abc.png')
#
# @api private
EXPANDING_PARAMS = {
:align => :align,
:alpha => :alpha,
:circle_radius => :radius,
:color => :color,
:ellipsize => :ellipsize,
:files => :file,
:fill_color => :fill_color,
:font => :font,
:height => :height,
:hint => :hint,
:justify => :justify,
:layout => :layout,
:markup => :markup,
:rect_radius => :radius,
:spacing => :spacing,
:str => :str,
:stroke_color => :stroke_color,
:stroke_width => :stroke_width,
:svgid => :id,
:valign => :valign,
:width => :width,
:wrap => :wrap,
:x => :x,
:x1 => :x1,
:x2 => :x2,
:x3 => :x3,
:x_radius => :x_radius,
:y => :y,
:y1 => :y1,
:y2 => :y2,
:y2 => :y3,
:y_radius => :y_radius,
}
end
+27 -24
View File
@@ -8,7 +8,7 @@ module Squib
def draw_text_hint(x,y,layout, color)
return if color.nil? && @deck.text_hint.nil?
color ||= @deck.text_hint
# when w,h < 0, it was never set. extents[0] are ink extents
# when w,h < 0, it was never set. extents[1] are ink extents
w = layout.width / Pango::SCALE
w = layout.extents[1].width / Pango::SCALE if w < 0
h = layout.height / Pango::SCALE
@@ -18,8 +18,8 @@ module Squib
# :nodoc:
# @api private
def ellipsize(layout, options)
unless options[:ellipsize].nil?
def ellipsize(layout, ellipsize)
unless ellipsize.nil?
h = { :none => Pango::Layout::ELLIPSIZE_NONE,
:start => Pango::Layout::ELLIPSIZE_START,
:middle => Pango::Layout::ELLIPSIZE_MIDDLE,
@@ -27,15 +27,15 @@ module Squib
true => Pango::Layout::ELLIPSIZE_END,
false => Pango::Layout::ELLIPSIZE_NONE
}
layout.ellipsize = h[options[:ellipsize]]
layout.ellipsize = h[ellipsize]
end
layout
end
# :nodoc:
# @api private
def wrap(layout, options)
unless options[:wrap].nil?
def wrap(layout, wrap)
unless wrap.nil?
h = { :word => Pango::Layout::WRAP_WORD,
:char => Pango::Layout::WRAP_CHAR,
:word_char => Pango::Layout::WRAP_WORD_CHAR,
@@ -43,20 +43,20 @@ module Squib
false => nil,
:none => nil
}
layout.wrap = h[options[:wrap]]
layout.wrap = h[wrap]
end
layout
end
# :nodoc:
# @api private
def align(layout, options)
unless options[:align].nil?
def align(layout, align)
unless align.nil?
h = { :left => Pango::ALIGN_LEFT,
:right => Pango::ALIGN_RIGHT,
:center => Pango::ALIGN_CENTER
}
layout.alignment = h[options[:align]]
layout.alignment = h[align]
end
layout
end
@@ -77,33 +77,36 @@ module Squib
# :nodoc:
# @api private
def setwh(layout, options)
layout.width = options[:width] * Pango::SCALE unless options[:width].nil? || options[:width] == :native
layout.height = options[:height] * Pango::SCALE unless options[:height].nil? || options[:height] == :native
def setwh(layout, width, height)
layout.width = width * Pango::SCALE unless width.nil? || width == :native
layout.height = height * Pango::SCALE unless height.nil? || height == :native
layout
end
# :nodoc:
# @api private
def text(str, font, x, y, color, options)
Squib.logger.debug {"Placing '#{str}'' with font '#{font}' @ #{x}, #{y}, color: #{color}, and options: #{options}"}
def text(str, font, color,
x, y, width, height,
markup, justify, wrap, ellipsize,
spacing, align, valign, hint)
Squib.logger.debug {"Placing '#{str}'' with font '#{font}' @ #{x}, #{y}, color: #{color}, etc. (TODO FILL THIS IN WITH METAPROGRAMMING)"}
cc = cairo_context
cc.set_source_color(color)
cc.move_to(x,y)
layout = cc.create_pango_layout
layout.font_description = Pango::FontDescription.new(font)
layout.text = str.to_s
layout.markup = str.to_s if options[:markup]
layout = setwh(layout, options)
layout = wrap(layout, options)
layout = ellipsize(layout, options)
layout = align(layout, options)
layout.justify = options[:justify] unless options[:justify].nil?
layout.spacing = options[:spacing] * Pango::SCALE unless options[:spacing].nil?
layout.markup = str.to_s if markup
layout = setwh(layout, width, height)
layout = wrap(layout, wrap)
layout = ellipsize(layout, ellipsize)
layout = align(layout, align)
layout.justify = justify unless justify.nil?
layout.spacing = spacing * Pango::SCALE unless spacing.nil?
cc.update_pango_layout(layout)
valign(cc, layout, x,y, options[:valign])
valign(cc, layout, x,y, valign)
cc.update_pango_layout(layout) ; cc.show_pango_layout(layout)
draw_text_hint(x,y,layout,options[:hint])
draw_text_hint(x,y,layout,hint)
end
end
+61 -29
View File
@@ -1,4 +1,5 @@
require 'squib/constants'
require 'pp'
module Squib
# :nodoc:
@@ -8,20 +9,22 @@ module Squib
# :nodoc:
# @api private
def needs(opts, params)
Squib.logger.debug {"Pre input-helper opts: #{opts}"}
opts = layoutify(opts) if params.include? :layout
opts = Squib::SYSTEM_DEFAULTS.merge(opts)
opts = expand_singletons(opts, params)
Squib.logger.debug {"Post expand opts: #{opts}"}
opts = rangeify(opts) if params.include? :range
opts = fileify(opts) if params.include? :file
opts = fileify(opts, false, false) if params.include? :file_to_save
opts = fileify(opts, true, false) if params.include? :files
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 = 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
opts = colorify(opts, false, :stroke_color) if params.include? :stroke_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 = radiusify(opts) if params.include? :rect_radius
opts = svgidify(opts) if params.include? :svgid
opts = formatify(opts) if params.include? :formats
opts
@@ -30,15 +33,35 @@ module Squib
# :nodoc:
# @api private
def expand_singletons(opts, needed_params)
Squib::EXPANDING_PARAMS.each_pair do |param_name, api_param|
if needed_params.include? param_name
unless opts[api_param].respond_to?(:each)
opts[api_param] = [opts[api_param]] * @cards.size
end
end
end
opts
end
module_function :expand_singletons
# Layouts have to come before, so we repeat expand_singletons here
# :nodoc:
# @api private
def layoutify(opts)
unless opts[:layout].nil?
entry = @layout[opts[:layout].to_s]
unless entry.nil?
entry.each do |key, value|
opts[key.to_sym] ||= entry[key]
unless opts[:layout].respond_to?(:each)
opts[:layout] = [opts[:layout]] * @cards.size
end
opts[:layout].each_with_index do |layout, i|
unless layout.nil?
entry = @layout[layout.to_s]
unless entry.nil?
entry.each do |key, value|
opts[key.to_sym] ||= entry[key]
end
else
Squib.logger.warn ("Layout entry '#{layout}' does not exist." )
end
else
Squib.logger.warn "Layout entry '#{opts[:layout]}' does not exist."
end
end
opts
@@ -70,10 +93,8 @@ module Squib
# :nodoc:
# @api private
def fileify(opts, expand_singletons=false, file_must_exist=true)
opts[:file] = [opts[:file]] * @cards.size if expand_singletons && !(opts[:file].respond_to? :each)
files = [opts[:file]].flatten
files.each do |file|
def fileify(opts, file_must_exist=true)
[opts[:file]].flatten.each do |file|
if file_must_exist and !File.exists?(file)
raise "File #{File.expand_path(file)} does not exist!"
end
@@ -87,7 +108,7 @@ module Squib
def dirify(opts, allow_create=false)
return opts if Dir.exists?(opts[:dir])
if allow_create
Squib.logger.warn "Dir #{opts[:dir]} does not exist, creating it."
Squib.logger.warn {"Dir #{opts[:dir]} does not exist, creating it."}
Dir.mkdir opts[:dir]
return opts
else
@@ -99,11 +120,16 @@ module Squib
# :nodoc:
# @api private
def colorify(opts, nillable=false, key=:color)
return opts if nillable && opts[key].nil?
if @custom_colors.key? opts[key].to_s
opts[key] = @custom_colors[opts[key].to_s]
opts[key].each_with_index do |color, i|
unless nillable && color.nil?
if @custom_colors.key? color.to_s
color = @custom_colors[color.to_s]
end
opts[key][i] = Cairo::Color.parse(color)
end
end
opts[key] = Cairo::Color.parse(opts[key])
# pp "===Colorified opts==="
# pp opts
opts
end
module_function :colorify
@@ -111,8 +137,10 @@ module Squib
# :nodoc:
# @api private
def fontify (opts)
opts[:font] = @font if opts[:font]==:use_set
opts[:font] = Squib::SYSTEM_DEFAULTS[:default_font] if opts[:font] == :default
opts[:font].each_with_index do |font, i|
opts[:font][i] = @font if font==:use_set
opts[:font][i] = Squib::SYSTEM_DEFAULTS[:default_font] if font == :default
end
opts
end
module_function :fontify
@@ -120,9 +148,11 @@ module Squib
# :nodoc:
# @api private
def radiusify(opts)
unless opts[:radius].nil?
opts[:x_radius] = opts[:radius]
opts[:y_radius] = opts[:radius]
opts[:radius].each_with_index do |radius, i|
unless radius.nil?
opts[:x_radius][i] = radius
opts[:y_radius][i] = radius
end
end
opts
end
@@ -131,8 +161,10 @@ module Squib
# :nodoc:
# @api private
def svgidify(opts)
unless opts[:id].nil?
opts[:id] = '#' << opts[:id] unless opts[:id].start_with? '#'
opts[:id].each_with_index do |id, i|
unless id.nil?
opts[:id][i] = '#' << id unless id.start_with? '#'
end
end
opts
end