From b001da7ba31ad04699ff44376370148a041f7d17 Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Wed, 16 Jul 2014 10:39:10 -0400 Subject: [PATCH] Massive rewrite to use DSL conventions --- bin/squib | 0 bin/squib-eg | 17 ++++---- lib/squib.rb | 72 +------------------------------ lib/squib/api/background.rb | 12 ++++++ lib/squib/api/data.rb | 6 +++ lib/squib/api/image.rb | 8 ++++ lib/squib/api/save.rb | 16 +++++++ lib/squib/api/shapes.rb | 13 ++++++ lib/squib/api/text.rb | 19 ++++++++ lib/squib/card.rb | 10 +++++ lib/squib/context.rb | 11 ----- lib/squib/deck.rb | 32 +++++++++++--- lib/squib/graphics/background.rb | 18 +++----- lib/squib/graphics/rectangle.rb | 21 --------- lib/squib/graphics/save_doc.rb | 34 ++++++--------- lib/squib/graphics/save_images.rb | 14 ++---- lib/squib/graphics/shapes.rb | 12 ++++++ lib/squib/graphics/text.rb | 27 ++++-------- 18 files changed, 163 insertions(+), 179 deletions(-) mode change 100644 => 100755 bin/squib mode change 100644 => 100755 bin/squib-eg create mode 100644 lib/squib/api/background.rb create mode 100644 lib/squib/api/data.rb create mode 100644 lib/squib/api/image.rb create mode 100644 lib/squib/api/save.rb create mode 100644 lib/squib/api/shapes.rb create mode 100644 lib/squib/api/text.rb delete mode 100644 lib/squib/context.rb delete mode 100644 lib/squib/graphics/rectangle.rb create mode 100644 lib/squib/graphics/shapes.rb diff --git a/bin/squib b/bin/squib old mode 100644 new mode 100755 diff --git a/bin/squib-eg b/bin/squib-eg old mode 100644 new mode 100755 index bd911e6..80f9388 --- a/bin/squib-eg +++ b/bin/squib-eg @@ -1,19 +1,18 @@ #!/usr/bin/env ruby require 'squib' -deck width: 825, height: 1125, cards: 3 -background [1.0,1.0,1.0] data = {'name' => ['Thief', 'Grifter', 'Mastermind'], 'level' => [1,2,3]} -rect x: 0, y: 0, width: 825, height: 1125, x_radius: 30, y_radius: 30 +Squib::Deck.new(width: 825, height: 1125, cards: 3) do + background color: [1.0,1.0,1.0] + rect x: 15, y: 15, width: 795, height: 1095, x_radius: 50, y_radius: 50 + rect x: 30, y: 30, width: 150, height: 150, x_radius: 25, y_radius: 25 + text str: data['name'], x: 250, y: 55 + text str: data['level'], x: 65, y: 65 -text str: data['name'], x: 10, y: 250 -rect x: 10, y: 250, width: 100, height: 100 - -text str: data['level'], x: 10, y: 15 - -save format: :png + save format: :png +end puts "Done!" \ No newline at end of file diff --git a/lib/squib.rb b/lib/squib.rb index cad39fb..f2f6443 100644 --- a/lib/squib.rb +++ b/lib/squib.rb @@ -1,72 +1,2 @@ -require 'squib/graphics/text' -require 'squib/graphics/save_doc' -require 'squib/graphics/save_images' -require 'squib/graphics/rectangle' -require 'squib/graphics/background' require 'squib/deck' -require 'squib/card' -require 'singleton' - -module Squib - @@the_deck = nil - - def self.the_deck=(d) - @@the_deck = d - end - - def self.the_deck - @@the_deck - end -end - -################## -### PUBLIC API ### -################## - -def deck(width: , height: , cards: 1) - Squib::the_deck = Squib::Deck.new(width, height, cards) -end - -def font(type: , size: 12, **options) -end - -def set_font(type: 'Arial', size: 12, **options) - Squib::queue_command Squib::Graphics::SetFont.new(type,size,options) -end - -def text(range: :all, str: , font: :use_set, x: 0, y: 0, **options) - deck = Squib::the_deck - range = 0..(deck.num_cards-1) if range == :all - str = [str] * deck.num_cards unless str.respond_to? :each - range.each do |i| - Squib::Graphics::Text.new(deck[i], str[i], font, x, y, options).execute - end -end - -def image(range=:all, file: , x: 0, y: 0) -end - -def rect(range: :all, x:, y:, width:, height:, x_radius: 0, y_radius: 0) - deck = Squib::the_deck - range = 0..(deck.num_cards-1) if range == :all - range.each do |i| - Squib::Graphics::Rectangle.new(deck[i], x, y, width, height, x_radius, y_radius).execute - end -end - -def background(color) - Squib::the_deck.each do |card| - Squib::Graphics::Background.new(card, color).execute - end -end - -def load_csv(file:, header: true) -end - -def data(field) -end - -def save(format: :png) - Squib::Graphics::SaveImages.new(format).execute if format==:png - Squib::Graphics::SaveDoc.new(format).execute if format==:pdf -end \ No newline at end of file +require 'squib/card' \ No newline at end of file diff --git a/lib/squib/api/background.rb b/lib/squib/api/background.rb new file mode 100644 index 0000000..806fd9c --- /dev/null +++ b/lib/squib/api/background.rb @@ -0,0 +1,12 @@ +module Squib + class Deck + #module API + + def background(range: :all, color: '#000000') + range = rangeify(range) + range.each { |i| @cards[i].background(color) } + end + + #end + end +end \ No newline at end of file diff --git a/lib/squib/api/data.rb b/lib/squib/api/data.rb new file mode 100644 index 0000000..d921acb --- /dev/null +++ b/lib/squib/api/data.rb @@ -0,0 +1,6 @@ +module Squib + class Deck + def csv(file: 'deck.csv', header: true) + end + end +end \ No newline at end of file diff --git a/lib/squib/api/image.rb b/lib/squib/api/image.rb new file mode 100644 index 0000000..273fa7e --- /dev/null +++ b/lib/squib/api/image.rb @@ -0,0 +1,8 @@ +module Squib + class Deck + + def png(range=:all, file: nil, x: 0, y: 0) + end + + end +end \ No newline at end of file diff --git a/lib/squib/api/save.rb b/lib/squib/api/save.rb new file mode 100644 index 0000000..f5715f1 --- /dev/null +++ b/lib/squib/api/save.rb @@ -0,0 +1,16 @@ +module Squib + class Deck + + def save(range: :all, format: :png) + format = [format].flatten + save_png(range: range) if format.include? :png + save_pdf if format.include? :pdf + end + + def save_png(range: :all) + range = rangeify(range) + range.each { |i| @cards[i].save_png(i) } + end + + end +end \ No newline at end of file diff --git a/lib/squib/api/shapes.rb b/lib/squib/api/shapes.rb new file mode 100644 index 0000000..3aeb8e4 --- /dev/null +++ b/lib/squib/api/shapes.rb @@ -0,0 +1,13 @@ +module Squib + class Deck + + def rect(range: :all, x: 0, y: 0, width: 825, height: 1125, \ + x_radius: 0, y_radius: 0) + range = rangeify(range) + range.each do |i| + @cards[i].draw_rectangle(x, y, width, height, x_radius, y_radius) + end + end + + end +end \ No newline at end of file diff --git a/lib/squib/api/text.rb b/lib/squib/api/text.rb new file mode 100644 index 0000000..afcc5eb --- /dev/null +++ b/lib/squib/api/text.rb @@ -0,0 +1,19 @@ +module Squib + class Deck + + def font(type: 'Arial', size: 12, **options) + end + + def set_font(type: 'Arial', size: 12, **options) + end + + 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 + range.each do |i| + cards[i].text(str[i], font, x, y, options) + end + end + + end +end \ No newline at end of file diff --git a/lib/squib/card.rb b/lib/squib/card.rb index e4652c6..4dc1f1b 100644 --- a/lib/squib/card.rb +++ b/lib/squib/card.rb @@ -11,6 +11,16 @@ module Squib @cairo_surface = Cairo::ImageSurface.new(width,height) @cairo_context = Cairo::Context.new(@cairo_surface) end + + ######################## + ### BACKEND GRAPHICS ### + ######################## + require 'squib/graphics/background' + require 'squib/graphics/save_doc' + require 'squib/graphics/save_images' + require 'squib/graphics/shapes' + require 'squib/graphics/text' + end end \ No newline at end of file diff --git a/lib/squib/context.rb b/lib/squib/context.rb deleted file mode 100644 index 23d03f0..0000000 --- a/lib/squib/context.rb +++ /dev/null @@ -1,11 +0,0 @@ -module Squib - module Render - - class RenderContext - attr_accessor :font #current font we're using - attr_accessor :cur #index of the current card rendering - attr_accessor :cards #total number of cards we're iterating over - end - - end -end \ No newline at end of file diff --git a/lib/squib/deck.rb b/lib/squib/deck.rb index 9343c5d..578d341 100644 --- a/lib/squib/deck.rb +++ b/lib/squib/deck.rb @@ -1,24 +1,44 @@ require 'squib/card' -module Squib +module Squib class Deck include Enumerable - attr_reader :width, :height, :num_cards + attr_reader :width, :height + attr_reader :cards - def initialize(width, height, cards) - @width=width; @height=height; @num_cards=cards + def initialize(width: 825, height: 1125, cards: 1, &block) + @width=width; @height=height @cards = [] - num_cards.times{ @cards << Squib::Card.new(width, height) } + cards.times{ @cards << Squib::Card.new(width, height) } + if block_given? + instance_eval(&block) + end end def [](key) @cards[key] end - #For enumerables + #For Enumerable def each(&block) @cards.each { |card| block.call(card) } end + def rangeify (range) + range = 0..(@cards.size-1) if range == :all + range = range..range if range.is_a? Integer + return range + end + + ################## + ### PUBLIC API ### + ################## + require 'squib/api/background' + require 'squib/api/data' + require 'squib/api/image' + require 'squib/api/save' + require 'squib/api/shapes' + require 'squib/api/text' + end end \ No newline at end of file diff --git a/lib/squib/graphics/background.rb b/lib/squib/graphics/background.rb index 5bf2064..a2458f3 100644 --- a/lib/squib/graphics/background.rb +++ b/lib/squib/graphics/background.rb @@ -1,17 +1,11 @@ module Squib - module Graphics + class Card - class Background - def initialize(card, color) - @card=card; @color=color - end - - def execute - cc = @card.cairo_context - cc.set_source_rgb(*@color) - cc.paint - end + def background(color) + cc = cairo_context + cc.set_source_rgb(*color) + cc.paint end - + end end \ No newline at end of file diff --git a/lib/squib/graphics/rectangle.rb b/lib/squib/graphics/rectangle.rb deleted file mode 100644 index 46bb413..0000000 --- a/lib/squib/graphics/rectangle.rb +++ /dev/null @@ -1,21 +0,0 @@ -module Squib - module Graphics - - class Rectangle - - def initialize(card, x, y, width, height, x_radius, y_radius) - @card=card; @x=x; @y=y; @width=width; @height=height - @x_radius=x_radius; @y_radius=y_radius - end - - def execute - cc = @card.cairo_context - cc.set_source_rgb(0.0,0.0,0.0) - cc.rounded_rectangle(@x, @y, @width, @height, @x_radius, @y_radius) - cc.stroke - end - - end - - end -end \ No newline at end of file diff --git a/lib/squib/graphics/save_doc.rb b/lib/squib/graphics/save_doc.rb index f166f20..0f34ede 100644 --- a/lib/squib/graphics/save_doc.rb +++ b/lib/squib/graphics/save_doc.rb @@ -1,25 +1,19 @@ module Squib - module Graphics - - class SaveDoc - def initialize(format) - @format = format - end + class Deck - def execute - #paper is 8.5x11 - # PDF points are 1/72 of an inch - width = 8.5 * 72 - height = 11 * 72 - cc = Cairo::Context.new(Cairo::PDFSurface.new('_img/deck.pdf', width, height)) - x = 10 - y = 10 - Squib.the_deck.each_with_index do |card, i| - cc.move_to(x,y) - x += 825*i - cc.set_source(card.cairo_surface, 0, 300) - cc.paint - end + def save_pdf + #paper is 8.5x11 + # PDF points are 1/72 of an inch + width = 8.5 * 72 + height = 11 * 72 + cc = Cairo::Context.new(Cairo::PDFSurface.new('_img/deck.pdf', width, height)) + x = 10 + y = 10 + @cards.each_with_index do |card, i| + cc.move_to(x,y) + x += 825*i + cc.set_source(card.cairo_surface, 0, 300) + cc.paint end end diff --git a/lib/squib/graphics/save_images.rb b/lib/squib/graphics/save_images.rb index c880e82..157b91e 100644 --- a/lib/squib/graphics/save_images.rb +++ b/lib/squib/graphics/save_images.rb @@ -1,16 +1,8 @@ module Squib - module Graphics + class Card - class SaveImages - def initialize(format) - @format = format - end - - def execute - Squib.the_deck.each_with_index do |card, i| - card.cairo_context.target.write_to_png("_img/img_#{i}.png") - end - end + def save_png(i, dir: '_img') + cairo_context.target.write_to_png("#{dir}/img_#{i}.png") end end diff --git a/lib/squib/graphics/shapes.rb b/lib/squib/graphics/shapes.rb new file mode 100644 index 0000000..d946f84 --- /dev/null +++ b/lib/squib/graphics/shapes.rb @@ -0,0 +1,12 @@ +module Squib + class Card + + def draw_rectangle(x, y, width, height, x_radius, y_radius) + cc = cairo_context + cc.set_source_rgb(0.0,0.0,0.0) + cc.rounded_rectangle(x, y, width, height, x_radius, y_radius) + cc.stroke + end + + end +end \ No newline at end of file diff --git a/lib/squib/graphics/text.rb b/lib/squib/graphics/text.rb index d2116bc..93ccfa3 100644 --- a/lib/squib/graphics/text.rb +++ b/lib/squib/graphics/text.rb @@ -1,22 +1,13 @@ module Squib - module Graphics - - class Text - def initialize(card, str, font, x, y, options) - @card=card - @str=str - @font=font - @x=x; @y=y - end - - def execute - cc = @card.cairo_context - cc.set_source_rgb(0.0,0.0,0.0) - cc.select_font_face ("Helvetica"); - cc.set_font_size(36) - cc.move_to(@x,@y + cc.text_extents(@str.to_s).height) - cc.show_text(@str.to_s) - end + class Card + + def text(str, font, x, y, options) + cc = cairo_context + cc.set_source_rgb(0.0,0.0,0.0) + cc.select_font_face ("Helvetica"); + cc.set_font_size(36) + cc.move_to(x,y + cc.text_extents(@str.to_s).height) + cc.show_text(str.to_s) end end