Browse Source

Unit conversion on save_pdf, and arbitrary paper sizes

Closes #21 and #20
dev
Andy Meneely 11 years ago
parent
commit
086fd5fad9
  1. 2
      lib/squib/api/save.rb
  2. 5
      lib/squib/constants.rb
  3. 21
      lib/squib/graphics/save_doc.rb
  4. 10
      lib/squib/input_helpers.rb
  5. 5
      samples/saves.rb
  6. 4
      spec/data/samples/saves.rb.txt
  7. 7
      spec/input_helpers_spec.rb

2
lib/squib/api/save.rb

@ -62,7 +62,7 @@ module Squib
# @api public # @api public
def showcase(opts = {}) def showcase(opts = {})
opts = {file: 'showcase.png', fill_color: :white}.merge(opts) opts = {file: 'showcase.png', fill_color: :white}.merge(opts)
opts = needs(opts,[:range, :trim, :trim_radius, :creatable_dir, :file_to_save, :face]) opts = needs(opts,[:range, :margin, :trim, :trim_radius, :creatable_dir, :file_to_save, :face])
render_showcase(opts[:range], opts[:trim], opts[:trim_radius], render_showcase(opts[:range], opts[:trim], opts[:trim_radius],
opts[:scale], opts[:offset], opts[:fill_color], opts[:scale], opts[:offset], opts[:fill_color],
opts[:reflect_offset], opts[:reflect_percent], opts[:reflect_strength], opts[:reflect_offset], opts[:reflect_percent], opts[:reflect_strength],

5
lib/squib/constants.rb

@ -128,10 +128,15 @@ module Squib
# value: the user-facing API key (e.g. radius: '1in') # value: the user-facing API key (e.g. radius: '1in')
UNIT_CONVERSION_PARAMS = { UNIT_CONVERSION_PARAMS = {
:circle_radius => :radius, :circle_radius => :radius,
:gap => :gap,
:height => :height, :height => :height,
:margin => :margin,
:paper_width => :width,
:paper_height => :height,
:rect_radius => :radius, :rect_radius => :radius,
:spacing => :spacing, :spacing => :spacing,
:stroke_width => :stroke_width, :stroke_width => :stroke_width,
:trim => :trim,
:width => :width, :width => :width,
:x => :x, :x => :x,
:x1 => :x1, :x1 => :x1,

21
lib/squib/graphics/save_doc.rb

@ -8,16 +8,17 @@ module Squib
# #
# @option opts file [String] the name of the PDF file to save. See {file:README.md#Specifying_Files Specifying Files} # @option opts file [String] the name of the PDF 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 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 width [Integer] (3300) the height of the page in pixels. Default is 11in * 300dpi. Supports unit conversion.
# @option opts gap [Integer] (0) the space in pixels between the cards # @option opts height [Integer] (2550) the height of the page in pixels. Default is 8.5in * 300dpi. Supports unit conversion.
# @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) # @option opts margin [Integer] (75) the margin around the outside of the page. Supports unit conversion.
# @option opts gap [Integer] (0) the space in pixels between the cards. Supports unit conversion.
# @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). Supports unit conversion.
# @return [nil] # @return [nil]
# @api public # @api public
def save_pdf(opts = {}) def save_pdf(opts = {})
p = needs(opts, [:range, :file_to_save, :creatable_dir, :margin, :gap, :trim]) opts = {width: 3300, height: 2550}.merge(opts)
width = 11 * @dpi p = needs(opts, [:range, :paper_width, :paper_height, :file_to_save, :creatable_dir, :margin, :gap, :trim])
height = 8.5 * @dpi #TODO: allow this to be specified too cc = Cairo::Context.new(Cairo::PDFSurface.new("#{p[:dir]}/#{p[:file]}", p[:width], p[:height]))
cc = Cairo::Context.new(Cairo::PDFSurface.new("#{p[:dir]}/#{p[:file]}", width, height))
x = p[:margin] x = p[:margin]
y = p[:margin] y = p[:margin]
@progress_bar.start("Saving PDF to #{p[:dir]}/#{p[:file]}", p[:range].size) do |bar| @progress_bar.start("Saving PDF to #{p[:dir]}/#{p[:file]}", p[:range].size) do |bar|
@ -27,10 +28,10 @@ module Squib
cc.paint cc.paint
bar.increment bar.increment
x += surface.width + p[:gap] x += surface.width + p[:gap]
if x > (width - surface.width - p[:margin]) if x > (p[:width] - surface.width - p[:margin])
x = p[:margin] x = p[:margin]
y += surface.height + p[:gap] y += surface.height + p[:gap]
if y > (height - surface.height - p[:margin]) if y > (p[:height] - surface.height - p[:margin])
x = p[:margin] x = p[:margin]
y = p[:margin] y = p[:margin]
cc.show_page #next page cc.show_page #next page
@ -51,7 +52,7 @@ module Squib
# @option opts [String] prefix (card_) the prefix of the file name(s) # @option opts [String] prefix (card_) the prefix of the file name(s)
# @option opts [String] count_format (%02d) the format string used for formatting the card count (e.g. padding zeros). Uses a Ruby format string (see the Ruby doc for Kernel::sprintf for specifics) # @option opts [String] count_format (%02d) the format string used for formatting the card count (e.g. padding zeros). Uses a Ruby format string (see the Ruby doc for Kernel::sprintf for specifics)
# @option opts dir [String] (_output) the directory to save to. Created if it doesn't exist. # @option opts dir [String] (_output) the directory to save to. Created if it doesn't exist.
# @option opts margin [Integer] (0) the margin around the outside of the page # @option opts margin [Integer] (0) the margin around the outside of the page.
# @option opts gap [Integer] (0) the space in pixels between the cards # @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) # @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] # @return [nil]

10
lib/squib/input_helpers.rb

@ -198,10 +198,14 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def convert_units(opts, needed_params) def convert_units(opts, needed_params)
Squib::UNIT_CONVERSION_PARAMS.each_pair do |param_name, api_param| UNIT_CONVERSION_PARAMS.each_pair do |param_name, api_param|
if needed_params.include? param_name if needed_params.include? param_name
opts[api_param].each_with_index do |arg, i| if EXPANDING_PARAMS.include? param_name
opts[api_param][i] = Args::UnitConversion.parse(arg, @dpi) opts[api_param].each_with_index do |arg, i|
opts[api_param][i] = Args::UnitConversion.parse(arg, @dpi)
end
else #not an expanding param
opts[api_param] = Args::UnitConversion.parse(opts[api_param], @dpi)
end end
end end
end end

5
samples/saves.rb

@ -11,6 +11,11 @@ Squib::Deck.new(width: 825, height: 1125, cards: 16) do
# Place on multiple pages over the PDF, with bleed beeing trimmed off # Place on multiple pages over the PDF, with bleed beeing trimmed off
save_pdf file: 'save-pdf.pdf', margin: 75, gap: 5, trim: 37 save_pdf file: 'save-pdf.pdf', margin: 75, gap: 5, trim: 37
# PDFs also support arbitrary paper sizes, in pixels or any other supported units
save_pdf file: 'save-pdf-small.pdf',
width: '7in', height: '5in',
range: 0..1
# Note that our PNGs still are not trimmed even though the pdf ones were # Note that our PNGs still are not trimmed even though the pdf ones were
save_png range: 1, prefix: 'saves_notrim_' save_png range: 1, prefix: 'saves_notrim_'

4
spec/data/samples/saves.rb.txt

@ -542,6 +542,10 @@ cairo: set_source([MockDouble, -37, -37])
cairo: paint([]) cairo: paint([])
cairo: set_source([MockDouble, 1650, 75]) cairo: set_source([MockDouble, 1650, 75])
cairo: paint([]) cairo: paint([])
cairo: set_source([MockDouble, 75, 75])
cairo: paint([])
cairo: set_source([MockDouble, 175, 75])
cairo: paint([])
surface: write_to_png(["_output/saves_notrim_01.png"]) surface: write_to_png(["_output/saves_notrim_01.png"])
cairo: set_source([MockDouble, -37, -37]) cairo: set_source([MockDouble, -37, -37])
cairo: paint([]) cairo: paint([])

7
spec/input_helpers_spec.rb

@ -176,6 +176,13 @@ describe Squib::InputHelpers do
expect(opts).to eq({:x => [236.2204722] }) #assume 300dpi default expect(opts).to eq({:x => [236.2204722] }) #assume 300dpi default
end end
it 'handles non-expading singletons' do
args = {margin: '1in', trim: '1in', gap: '1in'}
needed_params = [:margin, :trim, :gap]
opts = @deck.send(:convert_units, args, needed_params)
expect(opts).to eq({margin: 300, trim: 300, gap: 300}) #assume 300dpi default
end
end end
context '#rowify' do context '#rowify' do

Loading…
Cancel
Save