diff --git a/lib/squib.rb b/lib/squib.rb index f2f6443..b0257da 100644 --- a/lib/squib.rb +++ b/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' \ No newline at end of file +require 'squib/card' + diff --git a/lib/squib/deck.rb b/lib/squib/deck.rb index 7de03b2..fb63f34 100644 --- a/lib/squib/deck.rb +++ b/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) } diff --git a/lib/squib/graphics/save_doc.rb b/lib/squib/graphics/save_doc.rb index 8add2e5..ca5760c 100644 --- a/lib/squib/graphics/save_doc.rb +++ b/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 \ No newline at end of file diff --git a/lib/squib/input_helpers.rb b/lib/squib/input_helpers.rb index de53419..1d76196 100644 --- a/lib/squib/input_helpers.rb +++ b/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 diff --git a/samples/_output/gitkeep.txt b/samples/_output/gitkeep.txt new file mode 100644 index 0000000..e69de29 diff --git a/samples/save_pdf.rb b/samples/save_pdf.rb new file mode 100644 index 0000000..c224718 --- /dev/null +++ b/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!" \ No newline at end of file