Skip to content

Commit

Permalink
Toying around with a single sheet idea.
Browse files Browse the repository at this point in the history
  • Loading branch information
andymeneely committed Sep 2, 2014
1 parent 38e9252 commit 9d5f13f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/squib/constants.rb
Expand Up @@ -7,6 +7,7 @@ module Squib
:alpha => 1.0,
:blend => :none,
:color => :black,
:cols => 1,
:default_font => 'Arial 36',
:dir => "_output",
:ellipsize => :end,
Expand Down
35 changes: 35 additions & 0 deletions lib/squib/graphics/save_doc.rb
Expand Up @@ -35,7 +35,42 @@ def save_pdf(opts = {})
end
end
end
end
end

# Lays out the cards in range and renders a single PNG
#
# @example
# save_png_sheet file: 'deck.png', margin: 75, gap: 5, trim: 37
#
# @option opts cols [Integer] the number of columns in the grid
# @option opts file [String] the name of the PNG file to save. See {file:README.md#Specifying_Files Specifying Files}
# @option opts dir [String] (_output) the directory to save to. Created if it doesn't exist.
# @option opts margin [Integer] (75) the margin around the outside of the page
# @option opts gap [Integer] (0) the space in pixels between the cards
# @option opts trim [Integer] (0) the space around the edge of each card to trim (e.g. to cut off the bleed margin for print-and-play)
# @return [nil]
# @api public
def save_png_sheet(opts = {})
p = needs(opts, [:file_to_save, :creatable_dir, :margin, :gap, :trim, :cols])
width = p[:cols] * @width ; height = (((@cards.size) / p[:cols]) + 1) * @height
cc = Cairo::Context.new(Cairo::ImageSurface.new(width,height))
x = p[:margin] ; y = p[:margin]
@progress_bar.start("Saving PNG sheet to #{p[:dir]}/#{p[:file]}", @cards.size + 1) do |bar|
@cards.each_with_index do |card, i|
surface = trim(card.cairo_surface, p[:trim], @width, @height)
cc.set_source(surface, x, y)
cc.paint
bar.increment
x += surface.width + p[:gap]
if x > (width - surface.width - p[:margin])
x = p[:margin]
y += surface.height + p[:gap]
end
end
cc.target.write_to_png("#{p[:dir]}/#{p[:file]}.png")
bar.increment
end
end

# :nodoc:
Expand Down
3 changes: 3 additions & 0 deletions samples/save_pdf.rb
Expand Up @@ -11,4 +11,7 @@

#Note that our PNGs still are not trimmed even though the pdf ones are
save_png range: 1, prefix: "save_pdf_"

# We can also save our PNGs into a single sheet, with two columns
save_png_sheet file: 'save_single_sheet.png', cols: 2, margin: 75, gap: 5, trim: 37
end

0 comments on commit 9d5f13f

Please sign in to comment.