From 086fd5fad9a4d3453a469ce52a994c013551b75e Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Mon, 2 Feb 2015 00:20:25 -0500 Subject: [PATCH] Unit conversion on save_pdf, and arbitrary paper sizes Closes #21 and #20 --- lib/squib/api/save.rb | 2 +- lib/squib/constants.rb | 5 +++++ lib/squib/graphics/save_doc.rb | 21 +++++++++++---------- lib/squib/input_helpers.rb | 10 +++++++--- samples/saves.rb | 5 +++++ spec/data/samples/saves.rb.txt | 4 ++++ spec/input_helpers_spec.rb | 7 +++++++ 7 files changed, 40 insertions(+), 14 deletions(-) diff --git a/lib/squib/api/save.rb b/lib/squib/api/save.rb index e6b64b9..7ad7999 100644 --- a/lib/squib/api/save.rb +++ b/lib/squib/api/save.rb @@ -62,7 +62,7 @@ module Squib # @api public def showcase(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], opts[:scale], opts[:offset], opts[:fill_color], opts[:reflect_offset], opts[:reflect_percent], opts[:reflect_strength], diff --git a/lib/squib/constants.rb b/lib/squib/constants.rb index 1514fc4..80ae9f2 100644 --- a/lib/squib/constants.rb +++ b/lib/squib/constants.rb @@ -128,10 +128,15 @@ module Squib # value: the user-facing API key (e.g. radius: '1in') UNIT_CONVERSION_PARAMS = { :circle_radius => :radius, + :gap => :gap, :height => :height, + :margin => :margin, + :paper_width => :width, + :paper_height => :height, :rect_radius => :radius, :spacing => :spacing, :stroke_width => :stroke_width, + :trim => :trim, :width => :width, :x => :x, :x1 => :x1, diff --git a/lib/squib/graphics/save_doc.rb b/lib/squib/graphics/save_doc.rb index ef0b00f..7788fc1 100644 --- a/lib/squib/graphics/save_doc.rb +++ b/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 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) + # @option opts width [Integer] (3300) the height of the page in pixels. Default is 11in * 300dpi. Supports unit conversion. + # @option opts height [Integer] (2550) the height of the page in pixels. Default is 8.5in * 300dpi. Supports unit conversion. + # @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] # @api public def save_pdf(opts = {}) - p = needs(opts, [:range, :file_to_save, :creatable_dir, :margin, :gap, :trim]) - width = 11 * @dpi - height = 8.5 * @dpi #TODO: allow this to be specified too - cc = Cairo::Context.new(Cairo::PDFSurface.new("#{p[:dir]}/#{p[:file]}", width, height)) + opts = {width: 3300, height: 2550}.merge(opts) + p = needs(opts, [:range, :paper_width, :paper_height, :file_to_save, :creatable_dir, :margin, :gap, :trim]) + cc = Cairo::Context.new(Cairo::PDFSurface.new("#{p[:dir]}/#{p[:file]}", p[:width], p[:height])) x = p[:margin] y = p[:margin] @progress_bar.start("Saving PDF to #{p[:dir]}/#{p[:file]}", p[:range].size) do |bar| @@ -27,10 +28,10 @@ module Squib cc.paint bar.increment x += surface.width + p[:gap] - if x > (width - surface.width - p[:margin]) + if x > (p[:width] - surface.width - p[:margin]) x = p[:margin] y += surface.height + p[:gap] - if y > (height - surface.height - p[:margin]) + if y > (p[:height] - surface.height - p[:margin]) x = p[:margin] y = p[:margin] 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] 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 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 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] diff --git a/lib/squib/input_helpers.rb b/lib/squib/input_helpers.rb index 2e6d4bd..ccfe8b6 100644 --- a/lib/squib/input_helpers.rb +++ b/lib/squib/input_helpers.rb @@ -198,10 +198,14 @@ module Squib # :nodoc: # @api private 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 - opts[api_param].each_with_index do |arg, i| - opts[api_param][i] = Args::UnitConversion.parse(arg, @dpi) + if EXPANDING_PARAMS.include? param_name + 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 diff --git a/samples/saves.rb b/samples/saves.rb index 7d080a2..0d7addc 100644 --- a/samples/saves.rb +++ b/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 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 save_png range: 1, prefix: 'saves_notrim_' diff --git a/spec/data/samples/saves.rb.txt b/spec/data/samples/saves.rb.txt index 6dfd00b..a5c4d16 100644 --- a/spec/data/samples/saves.rb.txt +++ b/spec/data/samples/saves.rb.txt @@ -542,6 +542,10 @@ cairo: set_source([MockDouble, -37, -37]) cairo: paint([]) cairo: set_source([MockDouble, 1650, 75]) 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"]) cairo: set_source([MockDouble, -37, -37]) cairo: paint([]) diff --git a/spec/input_helpers_spec.rb b/spec/input_helpers_spec.rb index e7b7f40..67e608c 100644 --- a/spec/input_helpers_spec.rb +++ b/spec/input_helpers_spec.rb @@ -176,6 +176,13 @@ describe Squib::InputHelpers do expect(opts).to eq({:x => [236.2204722] }) #assume 300dpi default 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 context '#rowify' do