Massive rewrite to use DSL conventions
parent
c2f1a9673b
commit
b001da7ba3
|
|
@ -1,19 +1,18 @@
|
||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
require 'squib'
|
require 'squib'
|
||||||
|
|
||||||
deck width: 825, height: 1125, cards: 3
|
|
||||||
background [1.0,1.0,1.0]
|
|
||||||
data = {'name' => ['Thief', 'Grifter', 'Mastermind'],
|
data = {'name' => ['Thief', 'Grifter', 'Mastermind'],
|
||||||
'level' => [1,2,3]}
|
'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
|
save format: :png
|
||||||
rect x: 10, y: 250, width: 100, height: 100
|
end
|
||||||
|
|
||||||
text str: data['level'], x: 10, y: 15
|
|
||||||
|
|
||||||
save format: :png
|
|
||||||
|
|
||||||
puts "Done!"
|
puts "Done!"
|
||||||
72
lib/squib.rb
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/deck'
|
||||||
require 'squib/card'
|
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
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
module Squib
|
||||||
|
class Deck
|
||||||
|
def csv(file: 'deck.csv', header: true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
module Squib
|
||||||
|
class Deck
|
||||||
|
|
||||||
|
def png(range=:all, file: nil, x: 0, y: 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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
|
||||||
|
|
@ -11,6 +11,16 @@ module Squib
|
||||||
@cairo_surface = Cairo::ImageSurface.new(width,height)
|
@cairo_surface = Cairo::ImageSurface.new(width,height)
|
||||||
@cairo_context = Cairo::Context.new(@cairo_surface)
|
@cairo_context = Cairo::Context.new(@cairo_surface)
|
||||||
end
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -1,24 +1,44 @@
|
||||||
require 'squib/card'
|
require 'squib/card'
|
||||||
module Squib
|
|
||||||
|
|
||||||
|
module Squib
|
||||||
class Deck
|
class Deck
|
||||||
include Enumerable
|
include Enumerable
|
||||||
attr_reader :width, :height, :num_cards
|
attr_reader :width, :height
|
||||||
|
attr_reader :cards
|
||||||
|
|
||||||
def initialize(width, height, cards)
|
def initialize(width: 825, height: 1125, cards: 1, &block)
|
||||||
@width=width; @height=height; @num_cards=cards
|
@width=width; @height=height
|
||||||
@cards = []
|
@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
|
end
|
||||||
|
|
||||||
def [](key)
|
def [](key)
|
||||||
@cards[key]
|
@cards[key]
|
||||||
end
|
end
|
||||||
|
|
||||||
#For enumerables
|
#For Enumerable
|
||||||
def each(&block)
|
def each(&block)
|
||||||
@cards.each { |card| block.call(card) }
|
@cards.each { |card| block.call(card) }
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
@ -1,17 +1,11 @@
|
||||||
module Squib
|
module Squib
|
||||||
module Graphics
|
class Card
|
||||||
|
|
||||||
class Background
|
def background(color)
|
||||||
def initialize(card, color)
|
cc = cairo_context
|
||||||
@card=card; @color=color
|
cc.set_source_rgb(*color)
|
||||||
end
|
cc.paint
|
||||||
|
|
||||||
def execute
|
|
||||||
cc = @card.cairo_context
|
|
||||||
cc.set_source_rgb(*@color)
|
|
||||||
cc.paint
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -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
|
|
||||||
|
|
@ -1,25 +1,19 @@
|
||||||
module Squib
|
module Squib
|
||||||
module Graphics
|
class Deck
|
||||||
|
|
||||||
class SaveDoc
|
|
||||||
def initialize(format)
|
|
||||||
@format = format
|
|
||||||
end
|
|
||||||
|
|
||||||
def execute
|
def save_pdf
|
||||||
#paper is 8.5x11
|
#paper is 8.5x11
|
||||||
# PDF points are 1/72 of an inch
|
# PDF points are 1/72 of an inch
|
||||||
width = 8.5 * 72
|
width = 8.5 * 72
|
||||||
height = 11 * 72
|
height = 11 * 72
|
||||||
cc = Cairo::Context.new(Cairo::PDFSurface.new('_img/deck.pdf', width, height))
|
cc = Cairo::Context.new(Cairo::PDFSurface.new('_img/deck.pdf', width, height))
|
||||||
x = 10
|
x = 10
|
||||||
y = 10
|
y = 10
|
||||||
Squib.the_deck.each_with_index do |card, i|
|
@cards.each_with_index do |card, i|
|
||||||
cc.move_to(x,y)
|
cc.move_to(x,y)
|
||||||
x += 825*i
|
x += 825*i
|
||||||
cc.set_source(card.cairo_surface, 0, 300)
|
cc.set_source(card.cairo_surface, 0, 300)
|
||||||
cc.paint
|
cc.paint
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,8 @@
|
||||||
module Squib
|
module Squib
|
||||||
module Graphics
|
class Card
|
||||||
|
|
||||||
class SaveImages
|
def save_png(i, dir: '_img')
|
||||||
def initialize(format)
|
cairo_context.target.write_to_png("#{dir}/img_#{i}.png")
|
||||||
@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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -1,22 +1,13 @@
|
||||||
module Squib
|
module Squib
|
||||||
module Graphics
|
class Card
|
||||||
|
|
||||||
class Text
|
def text(str, font, x, y, options)
|
||||||
def initialize(card, str, font, x, y, options)
|
cc = cairo_context
|
||||||
@card=card
|
cc.set_source_rgb(0.0,0.0,0.0)
|
||||||
@str=str
|
cc.select_font_face ("Helvetica");
|
||||||
@font=font
|
cc.set_font_size(36)
|
||||||
@x=x; @y=y
|
cc.move_to(x,y + cc.text_extents(@str.to_s).height)
|
||||||
end
|
cc.show_text(str.to_s)
|
||||||
|
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue