More exploration spikes
* Playing with PDF support * Added background * Added rectangle * Split save to save_pdf and save_images since PDFs are way different * Working on text rendering (non-pango for now, but that'll change)dev
parent
6fd2ab8684
commit
e036d36737
16
bin/squib-eg
16
bin/squib-eg
|
|
@ -2,11 +2,19 @@
|
|||
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]}
|
||||
|
||||
text str: data['name'], x: 10, y: 250
|
||||
text str: data['level'], x: 10, y:10
|
||||
rect x: 0, y: 0, width: 825, height: 1125, x_radius: 30, y_radius: 30
|
||||
|
||||
save format: :png
|
||||
|
||||
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: :pdf
|
||||
|
||||
puts "Done!"
|
||||
22
lib/squib.rb
22
lib/squib.rb
|
|
@ -1,5 +1,8 @@
|
|||
require 'squib/graphics/text'
|
||||
require 'squib/graphics/save'
|
||||
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'
|
||||
|
|
@ -31,7 +34,7 @@ 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)
|
||||
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
|
||||
|
|
@ -43,8 +46,18 @@ end
|
|||
def image(range=:all, file: , x: 0, y: 0)
|
||||
end
|
||||
|
||||
def rect(x:, y:, width:, height:, x_radius: 0, y_radius: 0)
|
||||
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)
|
||||
|
|
@ -54,5 +67,6 @@ def data(field)
|
|||
end
|
||||
|
||||
def save(format: :png)
|
||||
Squib::Graphics::Save.new(format).execute
|
||||
Squib::Graphics::SaveImages.new(format).execute if format==:png
|
||||
Squib::Graphics::SaveDoc.new(format).execute if format==:pdf
|
||||
end
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
module Squib
|
||||
module Graphics
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
module Squib
|
||||
module Graphics
|
||||
|
||||
class SaveDoc
|
||||
def initialize(format)
|
||||
@format = format
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
@ -1,13 +1,12 @@
|
|||
module Squib
|
||||
module Graphics
|
||||
|
||||
class Save
|
||||
class SaveImages
|
||||
def initialize(format)
|
||||
@format = format
|
||||
end
|
||||
|
||||
def execute
|
||||
puts "Here!"
|
||||
Squib.the_deck.each_with_index do |card, i|
|
||||
card.cairo_context.target.write_to_png("_img/img_#{i}.png")
|
||||
end
|
||||
|
|
@ -14,7 +14,7 @@ module Squib
|
|||
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.move_to(@x,@y + cc.text_extents(@str.to_s).height)
|
||||
cc.show_text(@str.to_s)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in New Issue