Skip to content

Commit

Permalink
Specs passing, mid-refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
andymeneely committed Apr 18, 2015
1 parent a134cc2 commit 8d7bc67
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 35 deletions.
4 changes: 2 additions & 2 deletions lib/squib/api/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Deck
# @api public
def png(opts = {})
opts = needs(opts, [:range, :files, :x, :y, :width, :height, :alpha, :layout, :blend, :angle, :mask])
Dir.chdir(@img_dir) do
Dir.chdir(conf['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],
Expand Down Expand Up @@ -57,7 +57,7 @@ def png(opts = {})
# @api public
def svg(opts = {})
p = needs(opts,[:range, :files, :svgid, :force_svgid, :x, :y, :width, :height, :layout, :alpha, :blend, :angle, :mask])
Dir.chdir(@img_dir) do
Dir.chdir(conf['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?
Expand Down
6 changes: 3 additions & 3 deletions lib/squib/args/typographer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ module Args
class Typographer

def initialize(config = Conf::DEFAULTS)
[:lsquote, :ldquote, :rsquote, :rdquote, :smart_quotes,
:em_dash, :en_dash, :ellipsis ].each do |var|
%w(lsquote ldquote rsquote rdquote smart_quotes
em_dash en_dash ellipsis).each do |var|
instance_variable_set("@#{var}", config[var])
end
end

def process(str)
str = explicit_replacements(str)
str = explicit_replacements(str.to_s)
str = smart_quotes(str) if @smart_quotes
str
end
Expand Down
8 changes: 4 additions & 4 deletions lib/squib/card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ def initialize(deck, width, height, index=-1)
@deck = deck
@width = width
@height = height
@backend = deck.conf[:backend]
@svgfile = "#{deck.conf[:dir]}/#{deck.conf[:prefix]}#{deck.conf[:count_format] % index}.svg"
@backend = deck.conf['backend']
@svgfile = "#{deck.conf['dir']}/#{deck.conf['prefix']}#{deck.conf['count_format'] % index}.svg"
@cairo_surface = make_surface(@svgfile, @backend)
@cairo_context = Squib::Graphics::CairoContextWrapper.new(Cairo::Context.new(@cairo_surface))
@cairo_context.antialias = deck.conf[:antialias]
@cairo_context.antialias = deck.conf['antialias']
end

# :nodoc:
Expand All @@ -34,7 +34,7 @@ def make_surface(svgfile, backend)
when :memory
Cairo::ImageSurface.new(@width, @height)
when :svg
Dir.mkdir @deck.conf[:dir] unless Dir.exists?(@deck.conf[:dir])
Dir.mkdir @deck.conf['dir'] unless Dir.exists?(@deck.conf['dir'])
Cairo::SVGSurface.new(svgfile, @width, @height)
else
Squib.logger.fatal "Back end not recognized: '#{backend}'"
Expand Down
42 changes: 23 additions & 19 deletions lib/squib/conf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ module Squib
class Conf

DEFAULTS = {
:antialias => 'best',
:backend => 'memory',
:count_format => SYSTEM_DEFAULTS[:count_format],
:custom_colors => {},
:dir => SYSTEM_DEFAULTS[:dir],
:dpi => 300,
:hint => :none,
:img_dir => '.',
:progress_bar => false,
:ldquote => "\u201C", # UTF8 chars
:rdquote => "\u201D",
:lsquote => "\u2018",
:rsquote => "\u2019",
:em_dash => "\u2014",
:en_dash => "\u2013",
:ellipsis => "\u2026",
:smart_quotes => true,
:text_hint => 'off',
'antialias' => 'best',
'backend' => 'memory',
'count_format' => SYSTEM_DEFAULTS[:count_format],
'custom_colors' => {},
'dir' => SYSTEM_DEFAULTS[:dir],
'dpi' => 300,
'hint' => :none,
'img_dir' => '.',
'progress_bar' => false,
'ldquote' => "\u201C", # UTF8 chars
'rdquote' => "\u201D",
'lsquote' => "\u2018",
'rsquote' => "\u2019",
'em_dash' => "\u2014",
'en_dash' => "\u2013",
'ellipsis' => "\u2026",
'smart_quotes' => true,
'text_hint' => 'off',
}

#Translate the hints to the methods.
Expand Down Expand Up @@ -58,10 +58,14 @@ def self.load(file)
Conf.new(DEFAULTS.merge(yaml))
end

def to_s
"Conf: #{@config_hash.to_s}"
end

private

def normalize_antialias
@config_hash[:antialias] = ANTIALIAS_OPTS[@config_hash[:antialias].downcase.strip]
@config_hash['antialias'] = ANTIALIAS_OPTS[@config_hash['antialias'].downcase.strip]
end


Expand Down
2 changes: 1 addition & 1 deletion lib/squib/deck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def initialize(width: 825, height: 1125, cards: 1, dpi: 300, config: 'config.yml
@prefix = SYSTEM_DEFAULTS[:prefix]
@count_format = SYSTEM_DEFAULTS[:count_format]
@quote_chars = Conf::DEFAULTS.select {|k,v| %w(lsquote rsquote ldquote rdquote em_dash en_dash ellipsis smart_quotes).include?(k) }
show_info(config, layout)
@conf = Conf.load(config)
show_info(config, layout)
@width = Args::UnitConversion.parse width, dpi
@height = Args::UnitConversion.parse height, dpi
cards.times{ |i| @cards << Squib::Card.new(self, @width, @height, i) }
Expand Down
2 changes: 1 addition & 1 deletion lib/squib/graphics/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def text(embed,str, font, font_size, color,
layout.font_description = font_desc
layout.text = str
if markup
str = Args::Typographer.new(@deck.quote_chars).process(layout.text)
str = Args::Typographer.new(@deck.conf).process(layout.text)
layout.markup = str
end

Expand Down
4 changes: 2 additions & 2 deletions lib/squib/input_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ def dirify(opts, key, allow_create=false)
def colorify(opts, nillable=false, key=:color)
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]
if @conf['custom_colors'].key? color.to_s
color = @conf['custom_colors'][color.to_s]
end
opts[key][i] = color
end
Expand Down
7 changes: 4 additions & 3 deletions spec/input_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class DummyDeck
include Squib::InputHelpers
attr_accessor :layout, :cards, :custom_colors, :width, :height, :dpi
attr_accessor :layout, :cards, :custom_colors, :width, :height, :dpi, :conf
end

describe Squib::InputHelpers do
Expand All @@ -21,6 +21,7 @@ class DummyDeck
@deck.width = 100
@deck.height = 200
@deck.dpi = 300
@deck.conf = Squib::Conf.new
end

context '#layoutify' do
Expand Down Expand Up @@ -101,13 +102,13 @@ class DummyDeck
end

it 'pulls from custom colors in the config' do
@deck.custom_colors['foo'] = '#abc'
@deck.conf['custom_colors']['foo'] = '#abc'
expect(@deck.send(:colorify, {color: [:foo]}, false)[:color][0].to_s).to \
eq('#abc')
end

it 'pulls custom colors even when a string' do
@deck.custom_colors['foo'] = '#abc'
@deck.conf['custom_colors']['foo'] = '#abc'
expect(@deck.send(:colorify, {color: ['foo']}, false)[:color][0].to_s).to \
eq('#abc')
end
Expand Down

0 comments on commit 8d7bc67

Please sign in to comment.