Massive rewrite to use DSL conventions

This commit is contained in:
Andy Meneely
2014-07-16 10:39:10 -04:00
parent c2f1a9673b
commit b001da7ba3
18 changed files with 163 additions and 179 deletions
+12
View File
@@ -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
+6
View File
@@ -0,0 +1,6 @@
module Squib
class Deck
def csv(file: 'deck.csv', header: true)
end
end
end
+8
View File
@@ -0,0 +1,8 @@
module Squib
class Deck
def png(range=:all, file: nil, x: 0, y: 0)
end
end
end
+16
View File
@@ -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
+13
View File
@@ -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
+19
View File
@@ -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
+10
View File
@@ -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
-11
View File
@@ -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
+26 -6
View File
@@ -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
+6 -12
View File
@@ -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
-21
View File
@@ -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
+14 -20
View File
@@ -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
+3 -11
View File
@@ -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
+12
View File
@@ -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
+9 -18
View File
@@ -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