diff --git a/bin/squib-eg b/bin/squib-eg index e5f7276..04f379f 100644 --- a/bin/squib-eg +++ b/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]} +rect x: 0, y: 0, width: 825, height: 1125, x_radius: 30, y_radius: 30 + + text str: data['name'], x: 10, y: 250 -text str: data['level'], x: 10, y:10 +rect x: 10, y: 250, width: 100, height: 100 + +text str: data['level'], x: 10, y: 15 + +save format: :png +save format: :pdf -save format: :png \ No newline at end of file +puts "Done!" \ No newline at end of file diff --git a/lib/squib.rb b/lib/squib.rb index 7a6086f..cad39fb 100644 --- a/lib/squib.rb +++ b/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 \ No newline at end of file diff --git a/lib/squib/graphics/background.rb b/lib/squib/graphics/background.rb new file mode 100644 index 0000000..5bf2064 --- /dev/null +++ b/lib/squib/graphics/background.rb @@ -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 \ No newline at end of file diff --git a/lib/squib/graphics/rectangle.rb b/lib/squib/graphics/rectangle.rb new file mode 100644 index 0000000..46bb413 --- /dev/null +++ b/lib/squib/graphics/rectangle.rb @@ -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 \ No newline at end of file diff --git a/lib/squib/graphics/save_doc.rb b/lib/squib/graphics/save_doc.rb new file mode 100644 index 0000000..f166f20 --- /dev/null +++ b/lib/squib/graphics/save_doc.rb @@ -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 \ No newline at end of file diff --git a/lib/squib/graphics/save.rb b/lib/squib/graphics/save_images.rb similarity index 87% rename from lib/squib/graphics/save.rb rename to lib/squib/graphics/save_images.rb index e0a9667..c880e82 100644 --- a/lib/squib/graphics/save.rb +++ b/lib/squib/graphics/save_images.rb @@ -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 diff --git a/lib/squib/graphics/text.rb b/lib/squib/graphics/text.rb index 939cf1e..d2116bc 100644 --- a/lib/squib/graphics/text.rb +++ b/lib/squib/graphics/text.rb @@ -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