From f6480445df8554b0be59e92f18f8304bca84bd74 Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Mon, 28 Jul 2014 17:08:57 -0400 Subject: [PATCH] Stabilizing the code after the options switch. Getting there... --- lib/squib/api/data.rb | 6 +++--- lib/squib/api/image.rb | 4 ++-- lib/squib/api/text.rb | 9 ++++----- lib/squib/constants.rb | 7 +++++-- lib/squib/deck.rb | 2 +- lib/squib/graphics/save_doc.rb | 14 +++++++------- lib/squib/graphics/text.rb | 17 ++++------------- lib/squib/input_helpers.rb | 12 +++++++----- samples/text_options.rb | 5 +++-- spec/api/api_text_spec.rb | 2 +- spec/samples_run_spec.rb | 16 ++++++++-------- 11 files changed, 45 insertions(+), 49 deletions(-) diff --git a/lib/squib/api/data.rb b/lib/squib/api/data.rb index 66a6d36..f13710c 100644 --- a/lib/squib/api/data.rb +++ b/lib/squib/api/data.rb @@ -16,9 +16,9 @@ module Squib # @param sheet: [Integer] The zero-based index of the sheet from which to read. # @api public def xlsx(opts = {}) - needs(opts, [:file, :sheet]) - s = Roo::Excelx.new(file) - s.default_sheet = s.sheets[sheet] + opts = needs(opts, [:file, :sheet]) + s = Roo::Excelx.new(opts[:file]) + s.default_sheet = s.sheets[opts[:sheet]] data = {} s.first_column.upto(s.last_column) do |col| header = s.cell(s.first_row,col).to_s diff --git a/lib/squib/api/image.rb b/lib/squib/api/image.rb index d81ba48..27793b0 100644 --- a/lib/squib/api/image.rb +++ b/lib/squib/api/image.rb @@ -15,7 +15,7 @@ module Squib 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]) + @cards[i].png(opts[:file][i], opts[:x], opts[:y], opts[:alpha]) end end @@ -37,7 +37,7 @@ module Squib 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]) + @cards[i].svg(p[:file][i], p[:id], p[:x], p[:y], p[:width], p[:height]) end end diff --git a/lib/squib/api/text.rb b/lib/squib/api/text.rb index d4a005a..33c380d 100644 --- a/lib/squib/api/text.rb +++ b/lib/squib/api/text.rb @@ -22,7 +22,6 @@ module Squib # @option height: the height of the box the string will be placed in. Stretches to the content by default. # @option wrap: When height is set, determines the behavior of how the string wraps. The `:word_char` option will break at words, but then fall back to characters when the word cannot fit. # # Options are `:none, :word, :char, :word_char`. Also: `true` is the same as `:word_char`, `false` is the same as `:none`. Default `:word_char` - # @option fitxy: sets the text `width` and `height` to be equal to `width - x` and `height - y` for easy centering # @option align: options `:left, :right, and :center`. Default `:left` # @option justify: [Boolean] toggles whether or not the text is justified or not. Default `false` # @option valign: When width and height are set, align text vertically according to the logical extents of the text. Options are `:top, :middle, :bottom`. Default `:top` @@ -31,11 +30,11 @@ module Squib # @return [nil] Returns nothing # @api public def text(opts = {}) - opts = needs(opts, [:range, :str, :font, :x, :y, :width, :height, :color, :wrap, - :fitxy, :align, :justify, :valign, :ellipsize, :hint]) - str = [opts[:str]] * @cards.size unless str.respond_to? :each + opts = needs(opts, [:range, :str, :font, :x, :y, :width, :height, :text_color, :wrap, + :align, :justify, :valign, :ellipsize, :hint]) + opts[:str] = [opts[:str]] * @cards.size unless opts[:str].respond_to? :each opts[:range].each do |i| - @cards[i].text(str[i], opts[:font], opts[:x], opts[:y], opts[:color], opts) #TODO split this out + @cards[i].text(opts[:str][i], opts[:font], opts[:x], opts[:y], opts[:color], opts) end end diff --git a/lib/squib/constants.rb b/lib/squib/constants.rb index ed8f588..6094cbe 100644 --- a/lib/squib/constants.rb +++ b/lib/squib/constants.rb @@ -9,10 +9,10 @@ module Squib :stroke_color => :black, :stroke_width => 2.0, :font => :use_set, + :default_font => 'Arial 36', :sheet => 0, :x => 0, :y => 0, - :fitxy => false, :align => :left, :valign => :top, :justify => false, @@ -22,7 +22,10 @@ module Squib :alpha => 1.0, :format => :png, :dir => "_output", - :prefix => "card_" + :prefix => "card_", + :margin => 75, + :gap => 0, + :trim => 0 } end \ No newline at end of file diff --git a/lib/squib/deck.rb b/lib/squib/deck.rb index d29f2a6..1241c09 100644 --- a/lib/squib/deck.rb +++ b/lib/squib/deck.rb @@ -24,7 +24,7 @@ module Squib def initialize(width: 825, height: 1125, cards: 1, dpi: 300, config: 'config.yml', &block) @width=width; @height=height @dpi = dpi - @font = Squib::SYSTEM_DEFAULTS[:font] + @font = Squib::SYSTEM_DEFAULTS[:default_font] @cards = [] cards.times{ @cards << Squib::Card.new(self, width, height) } load_config(config) diff --git a/lib/squib/graphics/save_doc.rb b/lib/squib/graphics/save_doc.rb index ca5760c..1f603de 100644 --- a/lib/squib/graphics/save_doc.rb +++ b/lib/squib/graphics/save_doc.rb @@ -1,21 +1,21 @@ module Squib class Deck - def save_pdf(file: 'deck.pdf', dir: '_output', margin: 75, gap: 0, trim: 0) - dir = dirify(dir, allow_create: true) + def save_pdf(opts = {}) + p = needs(opts, [:file_to_save, :prefix, :margin, :gap, :trim]) width = 11 * @dpi ; height = 8.5 * @dpi #TODO: allow this to be specified too cc = Cairo::Context.new(Cairo::PDFSurface.new("#{dir}/#{file}", width, height)) - x = margin ; y = margin + x = p[:margin] ; y = p[:margin] @cards.each_with_index do |card, i| - surface = trim(card.cairo_surface, trim, @width, @height) + surface = trim(card.cairo_surface, p[:trim], @width, @height) cc.set_source(surface, x, y) cc.paint x += surface.width + gap if x > (width - surface.width - margin) - x = margin - y += surface.height + gap + x = p[:margin] + y += surface.height + p[:gap] if y > (height - surface.height - margin) - x = margin ; y = margin + x = p[:margin] ; y = p[:margin] cc.show_page #next page end end diff --git a/lib/squib/graphics/text.rb b/lib/squib/graphics/text.rb index 3729ebd..3e89a28 100644 --- a/lib/squib/graphics/text.rb +++ b/lib/squib/graphics/text.rb @@ -11,7 +11,7 @@ module Squib w = layout.extents[1].width / Pango::SCALE if w < 0 h = layout.height / Pango::SCALE h = layout.extents[1].height / Pango::SCALE if h < 0 - draw_rectangle(x,y,w,h,0,0,'#0000',color, 2.0) + rect(x,y,w,h,0,0,'#0000',color, 2.0) end def ellipsize(layout, options) @@ -65,16 +65,8 @@ module Squib end def setwh(layout, options) - layout.width = options[:width] * Pango::SCALE unless options[:width].nil? - layout.height = options[:height] * Pango::SCALE unless options[:height].nil? - layout - end - - def fitxy(layout, x,y, options) - w = options[:width] ; h = options[:height] - w ||= (@width - 2*x); h ||= (@height - 2*y) # default centers to x,y - w *= Pango::SCALE ; h *= Pango::SCALE - layout.width=w ; layout.height=h + 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 layout end @@ -86,8 +78,7 @@ module Squib layout.font_description = Pango::FontDescription.new(font) layout.text = str.to_s layout.markup = str.to_s if options[:markup] - layout = setwh(layout, options) unless options[:width].nil? && options[:height].nil? - layout = fitxy(layout, x, y , options) if options[:fitxy] + layout = setwh(layout, options) layout = wrap(layout, options) layout = ellipsize(layout, options) layout = align(layout, options) diff --git a/lib/squib/input_helpers.rb b/lib/squib/input_helpers.rb index 1a8e657..57bb4e6 100644 --- a/lib/squib/input_helpers.rb +++ b/lib/squib/input_helpers.rb @@ -7,6 +7,7 @@ module Squib opts = Squib::SYSTEM_DEFAULTS.merge(opts) opts = rangeify(opts) if params.include? :range opts = fileify(opts) if params.include? :file + opts = fileify(opts, false, true) if params.include? :file_to_save opts = fileify(opts, true) if params.include? :files opts = colorify(opts) if params.include? :color opts = colorify(opts, true) if params.include? :nillable_color @@ -39,11 +40,11 @@ module Squib end module_function :rangeify - def fileify(opts, expand_singletons=false) + def fileify(opts, expand_singletons=false, allow_non_exist=false) opts[:file] = [opts[:file]] * @cards.size if expand_singletons files = [opts[:file]].flatten files.each do |file| - unless File.exists? file + unless File.exists? file || allow_non_exist raise "File #{File.expand_path(file)} does not exist!" end end @@ -52,7 +53,8 @@ module Squib module_function :fileify def dirify(opts, allow_create=false) - return opts if Dir.exists? opts[:dir] + puts opts[:dir] + return opts if Dir.exists?(opts[:dir]) if allow_create Squib.logger.warn "Dir #{opts[:dir]} does not exist, creating it." Dir.mkdir opts[:dir] @@ -76,7 +78,7 @@ module Squib def fontify (opts) opts[:font] = @font if opts[:font]==:use_set - opts[:font] = Squib::SYSTEM_DEFAULTS[:font] if opts[:font] ==:default + opts[:font] = Squib::SYSTEM_DEFAULTS[:default_font] if opts[:font] == :default opts end module_function :fontify @@ -92,7 +94,7 @@ module Squib def svgidify(opts) unless opts[:id].nil? - opts[:id] = '#' << opts[:id] unless svgid.start_with? '#' + opts[:id] = '#' << opts[:id] unless opts[:id].start_with? '#' end opts end diff --git a/samples/text_options.rb b/samples/text_options.rb index 7f72152..a4b1604 100644 --- a/samples/text_options.rb +++ b/samples/text_options.rb @@ -3,7 +3,7 @@ require 'squib' data = {'name' => ['Thief', 'Grifter', 'Mastermind'], 'level' => [1,2,3]} -longtext = "This is left-justified text centered on the card based on x and y.\nWhat do you know about tweetle beetles? well... \nWhen tweetle beetles fight, it's called a tweetle beetle battle. And when they battle in a puddle, it's a tweetle beetle puddle battle. AND when tweetle beetles battle with paddles in a puddle, they call it a tweetle beetle puddle paddle battle. AND... When beetles battle beetles in a puddle paddle battle and the beetle battle puddle is a puddle in a bottle... ..they call this a tweetle beetle bottle puddle paddle battle muddle. AND... When beetles fight these battles in a bottle with their paddles and the bottle's on a poodle and the poodle's eating noodles... ...they call this a muddle puddle tweetle poodle beetle noodle bottle paddle battle." +longtext = "This is left-justified text.\nWhat do you know about tweetle beetles? well... \nWhen tweetle beetles fight, it's called a tweetle beetle battle. And when they battle in a puddle, it's a tweetle beetle puddle battle. AND when tweetle beetles battle with paddles in a puddle, they call it a tweetle beetle puddle paddle battle. AND... When beetles battle beetles in a puddle paddle battle and the beetle battle puddle is a puddle in a bottle... ..they call this a tweetle beetle bottle puddle paddle battle muddle. AND... When beetles fight these battles in a bottle with their paddles and the bottle's on a poodle and the poodle's eating noodles... ...they call this a muddle puddle tweetle poodle beetle noodle bottle paddle battle." Squib::Deck.new(width: 825, height: 1125, cards: 3) do background color: :white @@ -46,7 +46,8 @@ Squib::Deck.new(width: 825, height: 1125, cards: 3) do text str: longtext, font: 'Arial 16', x: 65, y: 700, - fitxy: true, justify: true + width: inches(2.25), height: inches(1), + justify: true text str: "Markup is also quite easy awesome", markup: true, diff --git a/spec/api/api_text_spec.rb b/spec/api/api_text_spec.rb index d1c3bb6..7beb513 100644 --- a/spec/api/api_text_spec.rb +++ b/spec/api/api_text_spec.rb @@ -6,7 +6,7 @@ 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 card = instance_double(Squib::Card) - expect(card).to receive(:text).with('a', Squib::SYSTEM_DEFAULTS[:font], anything, anything, anything, anything).once + expect(card).to receive(:text).with('a', 'Arial 36', anything, anything, anything, anything).once Squib::Deck.new do @cards = [card] text str: 'a' diff --git a/spec/samples_run_spec.rb b/spec/samples_run_spec.rb index 50137f2..b56cb41 100644 --- a/spec/samples_run_spec.rb +++ b/spec/samples_run_spec.rb @@ -3,13 +3,13 @@ require 'squib' describe Squib do - # it "should execute all examples with no errors" do - # samples = File.expand_path('../samples', File.dirname(__FILE__)) - # Dir["#{samples}/**/*.rb"].each do |sample| - # Dir.chdir(samples) do #to save to _output - # require_relative "../samples/#{File.basename(sample)}" - # end - # end - # end + it "should execute all examples with no errors" do + samples = File.expand_path('../samples', File.dirname(__FILE__)) + Dir["#{samples}/**/*.rb"].each do |sample| + Dir.chdir(samples) do #to save to _output + require_relative "../samples/#{File.basename(sample)}" + end + end + end end \ No newline at end of file