From 093b2cf15b7cd49b35d99ae04f2939ec822f1ea8 Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Fri, 25 Jul 2014 17:38:23 -0400 Subject: [PATCH] More work on the conversion --- lib/squib/api/save.rb | 16 +++++++++------- lib/squib/api/settings.rb | 2 +- lib/squib/api/shapes.rb | 15 +++++++-------- lib/squib/api/text.rb | 14 ++++++-------- lib/squib/constants.rb | 15 ++++++++++++++- lib/squib/deck.rb | 2 -- lib/squib/graphics/shapes.rb | 2 +- lib/squib/input_helpers.rb | 9 ++++++++- 8 files changed, 46 insertions(+), 29 deletions(-) diff --git a/lib/squib/api/save.rb b/lib/squib/api/save.rb index 79e3531..9665dcd 100644 --- a/lib/squib/api/save.rb +++ b/lib/squib/api/save.rb @@ -7,10 +7,10 @@ module Squib # @param dir: the directory for the output to be sent to. Will be created if it doesn't exist # @param format: the format that this will be rendered too. Options `:pdf, :png`. Array of both is allowed: `[:pdf, :png]` # @param prefix: the prefix of the file name to be printed - def save(range: :all, dir: "_output", format: :png, prefix: "card_") - format = [format].flatten - save_png(range: range, dir: dir, prefix: prefix) if format.include? :png - save_pdf if format.include? :pdf + def save(opts = {}) + opts = needs(opts, [:range, :creatable_dir, :formats, :prefix]) + save_png(opts) if opts[:format].include? :png + save_pdf(opts) if opts[:format].include? :pdf end # Saves the range of cards to PNG @@ -18,9 +18,11 @@ module Squib # @param range: the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges} # @param dir: the directory for the output to be sent to. Will be created if it doesn't exist # @param prefix: the prefix of the file name to be printed - def save_png(range: :all, dir: "_output", prefix: 'card_') - range = rangeify(range); dir = dirify(dir, allow_create: true) - range.each { |i| @cards[i].save_png(i, dir, prefix) } + def save_png(opts = {}) + opts = needs(opts,[:range, :creatable_dir, :prefix]) + opts[:range].each do |i| + @cards[i].save_png(i, opts[:dir], opts[:prefix]) + end end end diff --git a/lib/squib/api/settings.rb b/lib/squib/api/settings.rb index f50d3ac..9245542 100644 --- a/lib/squib/api/settings.rb +++ b/lib/squib/api/settings.rb @@ -10,7 +10,7 @@ module Squib # @api public def hint(text: nil) text = nil if text == :off - @text_hint = colorify(text, nillable: true) + @text_hint = text end end diff --git a/lib/squib/api/shapes.rb b/lib/squib/api/shapes.rb index e77f5d6..c10179f 100644 --- a/lib/squib/api/shapes.rb +++ b/lib/squib/api/shapes.rb @@ -14,14 +14,13 @@ module Squib # @param stroke_color: the color with which to stroke the outside of the rectangle # @param stroke_width: the width of the outside stroke # @api public - def rect(range: :all, x: 0, y: 0, width: 825, height: 1125, \ - radius: nil, x_radius: 0, y_radius: 0, \ - fill_color: '#0000', stroke_color: :black, stroke_width: 2.0) - range = rangeify(range) - color = colorify(color) - x_radius,y_radius = radiusify(radius, x_radius, y_radius) - range.each do |i| - @cards[i].draw_rectangle(x, y, width, height, x_radius, y_radius, fill_color, stroke_color, stroke_width) + def rect(opts = {}) + opts = needs(opts, [:range, :x, :y, :width, :height, :radius, + :fill_color, :stroke_color, :stroke_width]) + 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]) end end diff --git a/lib/squib/api/text.rb b/lib/squib/api/text.rb index 129da2f..8a78fe0 100644 --- a/lib/squib/api/text.rb +++ b/lib/squib/api/text.rb @@ -40,14 +40,12 @@ module Squib # @option hint: show a text hint with the given color. Overrides global hints (see {Deck#hint}). # @return [nil] Returns nothing # @api public - def text(range: :all, str: '', font: :use_set, x: 0, y: 0, **options) - range = rangeify(range) - str = [str] * @cards.size unless str.respond_to? :each - font = fontify(font) - color = colorify(options[:color], nillable: false) - options[:hint] = colorify(options[:hint]) unless options[:hint].nil? - range.each do |i| - cards[i].text(str[i], font, x, y, color, options) + 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[:range].each do |i| + @cards[i].text(str[i], opts[:font], opts[:x], opts[:y], opts[:color], opts) #TODO split this out end end diff --git a/lib/squib/constants.rb b/lib/squib/constants.rb index 34af675..f9de5c9 100644 --- a/lib/squib/constants.rb +++ b/lib/squib/constants.rb @@ -4,11 +4,24 @@ module Squib SYSTEM_DEFAULTS = { :range => :all, :color => :white, + :str => '', + :fill_color => '#0000', + :stroke_color => :black, :font => 'Arial, Sans 36', :sheet => 0, :x => 0, :y => 0, - :alpha => 1.0 + :fitxy => false, + :align => :left, + :valign => :top, + :justify => false, + :ellipsize => :end, + :width => :native, + :height => :native, + :alpha => 1.0, + :format => :png, + :dir => "_output", + :prefix => "card_" } end \ No newline at end of file diff --git a/lib/squib/deck.rb b/lib/squib/deck.rb index 1cd18b2..f787fad 100644 --- a/lib/squib/deck.rb +++ b/lib/squib/deck.rb @@ -3,7 +3,6 @@ require 'squib/card' require 'squib/input_helpers' require 'squib/constants' - # The project module # # @api public @@ -15,7 +14,6 @@ module Squib class Deck include Enumerable include Squib::InputHelpers - include Squib::Constants attr_reader :width, :height attr_reader :cards attr_reader :text_hint diff --git a/lib/squib/graphics/shapes.rb b/lib/squib/graphics/shapes.rb index d3105bc..c2d37f4 100644 --- a/lib/squib/graphics/shapes.rb +++ b/lib/squib/graphics/shapes.rb @@ -1,7 +1,7 @@ module Squib class Card - def draw_rectangle(x, y, width, height, x_radius, y_radius, fill_color, stroke_color, stroke_width) + def rect(x, y, width, height, x_radius, y_radius, fill_color, stroke_color, stroke_width) cc = cairo_context cc.rounded_rectangle(x, y, width, height, x_radius, y_radius) cc.set_source_color(stroke_color) diff --git a/lib/squib/input_helpers.rb b/lib/squib/input_helpers.rb index b83cfca..7b5c74a 100644 --- a/lib/squib/input_helpers.rb +++ b/lib/squib/input_helpers.rb @@ -15,9 +15,16 @@ module Squib opts = fontify(opts) if params.include? :font opts = radiusify(opts) if params.include? :radius opts = svgidify(opts) if params.include? :svgid + opts = formatify(opts) if params.include? :formats end module_function :needs + def formatify(opts) + opts[:format] = [opts[:format]].flatten + opts + end + module_function :formatify + def rangeify (opts) range = opts[:range] raise 'Range cannot be nil' if range.nil? @@ -88,7 +95,7 @@ module Squib end opts end - module_function :idify + module_function :svgidify def xyify #TODO: Allow negative numbers that subtract from the card width & height.