Browse Source

PDF support more stable now

* Lays out cards with possiblity of margin, gap, and trim
* Some new directory and file validation
* Added a logger
dev
Andy Meneely 12 years ago
parent
commit
11d2730b44
  1. 14
      lib/squib.rb
  2. 1
      lib/squib/deck.rb
  3. 39
      lib/squib/graphics/save_doc.rb
  4. 17
      lib/squib/input_helpers.rb
  5. 0
      samples/_output/gitkeep.txt
  6. 16
      samples/save_pdf.rb

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

1
lib/squib/deck.rb

@ -13,6 +13,7 @@ module Squib
def initialize(width: 825, height: 1125, cards: 1, &block)
@width=width; @height=height
@dpi = 300
@font = 'Sans 36'
@cards = []
cards.times{ @cards << Squib::Card.new(self, width, height) }

39
lib/squib/graphics/save_doc.rb

@ -1,22 +1,37 @@
module Squib
class Deck
def save_pdf
#paper is 8.5x11
# PDF points are 1/72 of an inch
raise "Not implemented yet!"
width = 8.5 * 72
height = 11 * 72
cc = Cairo::Context.new(Cairo::PDFSurface.new('_img/deck.pdf', width, height))
x = 10
y = 10
def save_pdf(file: 'deck.pdf', dir: '_output', margin: 75, gap: 0, trim: 0)
dir = dirify(dir, allow_create: true)
width = 11 * @dpi ; height = 8.5 * @dpi #TODO: allow this to be specified too
cc = Cairo::Context.new(Cairo::PDFSurface.new("#{dir}/#{file}", width, height))
x = margin ; y = margin
@cards.each_with_index do |card, i|
cc.move_to(x,y)
x += 825*i
cc.set_source(card.cairo_surface, 0, 300)
surface = trim(card.cairo_surface, trim, @width, @height)
cc.set_source(surface, x, y)
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
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

17
lib/squib/input_helpers.rb

@ -13,11 +13,24 @@ module Squib
module_function :rangeify
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
end
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)
if nillable # for optional color arguments like text hints
color = Cairo::Color.parse(color) unless color.nil?
@ -34,7 +47,7 @@ module Squib
font = Squib::DEFAULT_FONT if font==:default
font
end
module_function :fontify
module_function :fontify
def xyify
#TODO: Allow negative numbers that subtract from the card width & height

0
samples/_output/gitkeep.txt

16
samples/save_pdf.rb

@ -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…
Cancel
Save