Browse Source

Massive rewrite to use DSL conventions

dev
Andy Meneely 12 years ago
parent
commit
b001da7ba3
  1. 0
      bin/squib
  2. 17
      bin/squib-eg
  3. 72
      lib/squib.rb
  4. 12
      lib/squib/api/background.rb
  5. 6
      lib/squib/api/data.rb
  6. 8
      lib/squib/api/image.rb
  7. 16
      lib/squib/api/save.rb
  8. 13
      lib/squib/api/shapes.rb
  9. 19
      lib/squib/api/text.rb
  10. 10
      lib/squib/card.rb
  11. 11
      lib/squib/context.rb
  12. 32
      lib/squib/deck.rb
  13. 18
      lib/squib/graphics/background.rb
  14. 21
      lib/squib/graphics/rectangle.rb
  15. 34
      lib/squib/graphics/save_doc.rb
  16. 14
      lib/squib/graphics/save_images.rb
  17. 12
      lib/squib/graphics/shapes.rb
  18. 27
      lib/squib/graphics/text.rb

17
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!"

72
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
require 'squib/card'

12
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

6
lib/squib/api/data.rb

@ -0,0 +1,6 @@
module Squib
class Deck
def csv(file: 'deck.csv', header: true)
end
end
end

8
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

16
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

13
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

19
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

10
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

11
lib/squib/context.rb

@ -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

32
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

18
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

21
lib/squib/graphics/rectangle.rb

@ -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

34
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

14
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

12
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

27
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

Loading…
Cancel
Save