Browse Source

Singleton expansion has... expanded!

Closes #13, hopefully
dev
Andy Meneely 12 years ago
parent
commit
3e08a0d604
  1. 2
      lib/squib/api/background.rb
  2. 5
      lib/squib/api/image.rb
  3. 2
      lib/squib/api/settings.rb
  4. 24
      lib/squib/api/shapes.rb
  5. 9
      lib/squib/api/text.rb
  6. 98
      lib/squib/constants.rb
  7. 51
      lib/squib/graphics/text.rb
  8. 82
      lib/squib/input_helpers.rb
  9. 1
      samples/colors.rb
  10. 1
      samples/custom_config.rb
  11. 16
      spec/api/api_text_spec.rb
  12. 39
      spec/input_helpers_spec.rb

2
lib/squib/api/background.rb

@ -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

5
lib/squib/api/image.rb

@ -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

2
lib/squib/api/settings.rb

@ -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

24
lib/squib/api/shapes.rb

@ -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

9
lib/squib/api/text.rb

@ -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

98
lib/squib/constants.rb

@ -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',
:stroke_color => :black,
:stroke_width => 2.0,
:font => :use_set,
:default_font => 'Arial 36',
: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,
:trim => 0,
:valign => :top,
:width => :native,
:wrap => true,
:x => 0,
:y => 0,
:x1 => 100,
:y1 => 100,
:x2 => 150,
:y2 => 150,
:x3 => 100,
:y3 => 150,
:x_radius => 0,
:y => 0,
:y1 => 100,
:y2 => 150,
:y3 => 150,
: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
}
# 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

51
lib/squib/graphics/text.rb

@ -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

82
lib/squib/input_helpers.rb

@ -1,4 +1,5 @@
require 'squib/constants'
require 'pp'
module Squib
# :nodoc:
@ -8,37 +9,59 @@ 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
end
module_function :needs
# :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 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 '#{opts[:layout]}' does not exist."
Squib.logger.warn ("Layout entry '#{layout}' does not exist." )
end
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
opts[key] = Cairo::Color.parse(opts[key])
end
# 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

1
samples/colors.rb

@ -11,7 +11,6 @@ Squib::Deck.new(width: 825, height: 1125, cards: 1) do
text color: '#ffff00000000', str: '12-hex', x: 50, y: y+=50
text color: '#ffff000000009999', str: '12-hex (alpha)', x: 50, y: y+=50
text color: :burnt_orange, str: 'Symbols of constants too', x: 50, y: y+=50
text color: [1.0, 0.0, 0.0], str: 'Array of percentages', x: 50, y: y+=50
save_png prefix: "colors_"
end

1
samples/custom_config.rb

@ -2,7 +2,6 @@
require 'squib'
Squib::Deck.new(config: 'custom-config.yml') do
# Custom color defined in our config
background color: :foo

16
spec/api/api_text_spec.rb

@ -3,10 +3,12 @@ require 'squib'
describe Squib::Deck, '#text' do
context "when working with fonts" do
it"should use the default font when #text and #set_font don't specify" do
context "fonts" do
it "should use the default font when #text and #set_font don't specify" do
card = instance_double(Squib::Card)
expect(card).to receive(:text).with('a', 'Arial 36', anything, anything, anything, anything).once
expect(card).to receive(:text).with('a', 'Arial 36',
anything, anything, anything, anything,anything, anything, anything, anything,anything, anything, anything, anything, anything
).once
Squib::Deck.new do
@cards = [card]
text str: 'a'
@ -15,7 +17,9 @@ describe Squib::Deck, '#text' do
it "should use the #set_font when #text doesn't specify" do
card = instance_double(Squib::Card)
expect(card).to receive(:text).with('a', 'Times New Roman 16', anything, anything, anything, anything).once
expect(card).to receive(:text).with('a', 'Times New Roman 16',
anything, anything, anything, anything,anything, anything, anything, anything,anything, anything, anything, anything, anything
).once
Squib::Deck.new do
@cards = [card]
set font: 'Times New Roman 16'
@ -25,7 +29,9 @@ describe Squib::Deck, '#text' do
it "should use the specified font no matter what" do
card = instance_double(Squib::Card)
expect(card).to receive(:text).with('a', 'Arial 18', anything, anything, anything, anything).once
expect(card).to receive(:text).with('a', 'Arial 18',
anything, anything, anything, anything,anything, anything, anything, anything,anything, anything, anything, anything, anything
).once
Squib::Deck.new do
@cards = [card]
set font: 'Times New Roman 16'

39
spec/input_helpers_spec.rb

@ -27,27 +27,31 @@ describe Squib::InputHelpers do
it "should warn on the logger when the layout doesn't exist" do
@old_logger = Squib.logger
Squib.logger = instance_double(Logger)
expect(Squib.logger).to receive(:warn).with("Layout entry 'foo' does not exist.")
expect(@deck.send(:layoutify, {layout: :foo})).to eq({layout: :foo})
expect(Squib.logger).to receive(:warn).with("Layout entry 'foo' does not exist.").twice
expect(@deck.send(:layoutify, {layout: :foo})).to eq({layout: [:foo,:foo]})
Squib.logger = @old_logger
end
it "should apply the layout in a normal situation" do
expect(@deck.send(:layoutify, {layout: :blah})).to eq({layout: :blah, x: 25})
expect(@deck.send(:layoutify, {layout: :blah})).to \
eq({layout: [:blah, :blah], x: 25})
end
it "also look up based on strings" do
expect(@deck.send(:layoutify, {layout: 'blah'})).to eq({layout: 'blah', x: 25})
expect(@deck.send(:layoutify, {layout: 'blah'})).to \
eq({layout: ['blah','blah'], x: 25})
end
end
context '#rangeify' do
it "must be within the card size range" do
expect{@deck.send(:rangeify, {range: 2..3})}.to raise_error(ArgumentError, '2..3 is outside of deck range of 0..1')
expect{@deck.send(:rangeify, {range: 2..3})}.to \
raise_error(ArgumentError, '2..3 is outside of deck range of 0..1')
end
it "cannot be nil" do
expect{@deck.send(:rangeify, {range: nil})}.to raise_error(RuntimeError, 'Range cannot be nil')
expect{@deck.send(:rangeify, {range: nil})}.to \
raise_error(RuntimeError, 'Range cannot be nil')
end
it "defaults to a range of all cards if :all" do
@ -57,38 +61,39 @@ describe Squib::InputHelpers do
context "#fileify" do
it "should throw an error if the file doesn't exist" do
expect{@deck.send(:fileify, {file: 'nonexist.txt'}, false, true)}.to raise_error(RuntimeError,"File #{File.expand_path('nonexist.txt')} does not exist!")
end
it "should expand singletons when asked" do
expect(@deck.send(:fileify, {file: 'foo.txt'}, true, false)).to eq({file: ['foo.txt', 'foo.txt']})
expect{@deck.send(:fileify, {file: 'nonexist.txt'}, true)}.to \
raise_error(RuntimeError,"File #{File.expand_path('nonexist.txt')} does not exist!")
end
end
context "#dir" do
it "should raise an error if the directory does not exist" do
expect{@deck.send(:dirify, {dir: 'nonexist'}, false)}.to raise_error(RuntimeError,"'nonexist' does not exist!")
expect{@deck.send(:dirify, {dir: 'nonexist'}, false)}.to \
raise_error(RuntimeError,"'nonexist' does not exist!")
end
end
context "#colorify" do
it "should parse if nillable" do
color = @deck.send(:colorify, {color: '#fff'}, true)[:color]
expect(color.to_a).to eq([1.0, 1.0, 1.0, 1.0])
color = @deck.send(:colorify, {color: ['#fff']}, true)[:color]
expect(color.to_a[0].to_a).to eq([1.0, 1.0, 1.0, 1.0])
end
it "raises and error if the color doesn't exist" do
expect{ @deck.send(:colorify, {color: :nonexist}, false) }.to raise_error(ArgumentError, "unknown color name: nonexist")
expect{ @deck.send(:colorify, {color: [:nonexist]}, false) }.to \
raise_error(ArgumentError, "unknown color name: nonexist")
end
it "pulls from config's custom colors" do
@deck.custom_colors['foo'] = "#abc"
expect(@deck.send(:colorify, {color: :foo}, false)[:color].to_s).to eq('#AABBCCFF')
expect(@deck.send(:colorify, {color: [:foo]}, false)[:color][0].to_s).to \
eq('#AABBCCFF')
end
it "pulls from config's custom colors even when a string" do
@deck.custom_colors['foo'] = "#abc"
expect(@deck.send(:colorify, {color: 'foo'}, false)[:color].to_s).to eq('#AABBCCFF')
expect(@deck.send(:colorify, {color: ['foo']}, false)[:color][0].to_s).to \
eq('#AABBCCFF')
end
end

Loading…
Cancel
Save