PDF support more stable now
* Lays out cards with possiblity of margin, gap, and trim * Some new directory and file validation * Added a loggerdev
parent
1ebb148e94
commit
11d2730b44
12
lib/squib.rb
12
lib/squib.rb
|
|
@ -1,2 +1,14 @@
|
||||||
|
require 'logger'
|
||||||
|
|
||||||
|
module Squib
|
||||||
|
|
||||||
|
def logger
|
||||||
|
@logger ||= Logger.new(STDOUT)
|
||||||
|
end
|
||||||
|
module_function :logger
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
require 'squib/deck'
|
require 'squib/deck'
|
||||||
require 'squib/card'
|
require 'squib/card'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ module Squib
|
||||||
|
|
||||||
def initialize(width: 825, height: 1125, cards: 1, &block)
|
def initialize(width: 825, height: 1125, cards: 1, &block)
|
||||||
@width=width; @height=height
|
@width=width; @height=height
|
||||||
|
@dpi = 300
|
||||||
@font = 'Sans 36'
|
@font = 'Sans 36'
|
||||||
@cards = []
|
@cards = []
|
||||||
cards.times{ @cards << Squib::Card.new(self, width, height) }
|
cards.times{ @cards << Squib::Card.new(self, width, height) }
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,37 @@
|
||||||
module Squib
|
module Squib
|
||||||
class Deck
|
class Deck
|
||||||
|
|
||||||
def save_pdf
|
def save_pdf(file: 'deck.pdf', dir: '_output', margin: 75, gap: 0, trim: 0)
|
||||||
#paper is 8.5x11
|
dir = dirify(dir, allow_create: true)
|
||||||
# PDF points are 1/72 of an inch
|
width = 11 * @dpi ; height = 8.5 * @dpi #TODO: allow this to be specified too
|
||||||
raise "Not implemented yet!"
|
cc = Cairo::Context.new(Cairo::PDFSurface.new("#{dir}/#{file}", width, height))
|
||||||
width = 8.5 * 72
|
x = margin ; y = margin
|
||||||
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|
|
@cards.each_with_index do |card, i|
|
||||||
cc.move_to(x,y)
|
surface = trim(card.cairo_surface, trim, @width, @height)
|
||||||
x += 825*i
|
cc.set_source(surface, x, y)
|
||||||
cc.set_source(card.cairo_surface, 0, 300)
|
|
||||||
cc.paint
|
cc.paint
|
||||||
|
x += surface.width + gap
|
||||||
|
if x > (width - surface.width - margin)
|
||||||
|
x = margin
|
||||||
|
y += surface.height + gap
|
||||||
|
if y > (height - surface.height - margin)
|
||||||
|
x = margin ; y = margin
|
||||||
|
cc.show_page #next page
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def trim(surface, trim, width, height)
|
||||||
|
if trim > 0
|
||||||
|
tmp = Cairo::ImageSurface.new(width-2*trim, height-2*trim)
|
||||||
|
cc = Cairo::Context.new(tmp)
|
||||||
|
cc.set_source(surface,-1*trim, -1*trim)
|
||||||
|
cc.paint
|
||||||
|
surface = tmp
|
||||||
|
end
|
||||||
|
surface
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -13,11 +13,24 @@ module Squib
|
||||||
module_function :rangeify
|
module_function :rangeify
|
||||||
|
|
||||||
def fileify(file)
|
def fileify(file)
|
||||||
raise 'File #{file} does not exist!' unless File.exists? file
|
raise "File #{File.expand_path(file)} does not exist!" unless File.exists? file
|
||||||
file
|
file
|
||||||
end
|
end
|
||||||
module_function :fileify
|
module_function :fileify
|
||||||
|
|
||||||
|
def dirify(dir, allow_create: false)
|
||||||
|
return dir if Dir.exists? dir
|
||||||
|
if allow_create
|
||||||
|
Squib.logger.warn "PDF output dir #{dir} does not exist... attempting to create it"
|
||||||
|
Dir.mkdir dir
|
||||||
|
return dir
|
||||||
|
else
|
||||||
|
raise "#{dir} does not exist!"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
module_function :dirify
|
||||||
|
|
||||||
|
|
||||||
def colorify(color, nillable: false)
|
def colorify(color, nillable: false)
|
||||||
if nillable # for optional color arguments like text hints
|
if nillable # for optional color arguments like text hints
|
||||||
color = Cairo::Color.parse(color) unless color.nil?
|
color = Cairo::Color.parse(color) unless color.nil?
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
#!/usr/bin/env ruby
|
||||||
|
require 'squib'
|
||||||
|
|
||||||
|
Squib::Deck.new(width: 825, height: 1125, cards: 16) do
|
||||||
|
background color: :gray
|
||||||
|
rect x: 38, y: 38, width: 750, height: 1050, x_radius: 38, y_radius: 38
|
||||||
|
|
||||||
|
text str: (1..16).to_a, x: 220, y: 78, font: 'Arial 54'
|
||||||
|
|
||||||
|
save_pdf file: "sample-save-pdf.pdf", margin: 75, gap: 5, trim: 37
|
||||||
|
|
||||||
|
#Note that our PNGs still are not trimmed even though the pdf ones are
|
||||||
|
save_png range: 1, prefix: "save_pdf_"
|
||||||
|
end
|
||||||
|
|
||||||
|
puts "Done!"
|
||||||
Loading…
Reference in New Issue