Implementing rotate for save_png
This commit is contained in:
@@ -7,10 +7,11 @@ module Squib
|
||||
# @option opts [String] dir (_output) the directory for the output to be sent to. Will be created if it doesn't exist.
|
||||
# @option opts [Symbol] format (:png) the format that this will be rendered too. Options `:pdf, :png`. Array of both is allowed: `[:pdf, :png]`
|
||||
# @option opts [String] prefix (card_) the prefix of the file name to be printed
|
||||
# @option opts [Boolean] rotate (false) PNG saving only. If true, the saved cards will be rotated 90 degrees clockwise. Intended to rendering landscape instead of portrait.
|
||||
# @return self
|
||||
# @api public
|
||||
def save(opts = {})
|
||||
opts = needs(opts, [:range, :creatable_dir, :formats, :prefix])
|
||||
opts = needs(opts, [:range, :creatable_dir, :formats, :prefix, :rotate])
|
||||
save_png(opts) if opts[:format].include? :png
|
||||
save_pdf(opts) if opts[:format].include? :pdf
|
||||
self
|
||||
@@ -24,17 +25,19 @@ module Squib
|
||||
# @option opts [Enumerable] range (:all) the range of cards over which this will be rendered. See {file:README.md#Specifying_Ranges Specifying Ranges}
|
||||
# @option opts [String] dir (_output) the directory for the output to be sent to. Will be created if it doesn't exist.
|
||||
# @option opts [String] prefix (card_) the prefix of the file name to be printed.
|
||||
# @option opts [Boolean] rotate (false) if true, the saved cards will be rotated 90 degrees clockwise. Intended to rendering landscape instead of portrait.
|
||||
# @return [nil] Returns nothing
|
||||
# @api public
|
||||
def save_png(opts = {})
|
||||
opts = needs(opts,[:range, :creatable_dir, :prefix])
|
||||
opts = needs(opts,[:range, :creatable_dir, :prefix, :rotate])
|
||||
@progress_bar.start("Saving PNGs to #{opts[:dir]}/#{opts[:prefix]}*") do |bar|
|
||||
opts[:range].each do |i|
|
||||
@cards[i].save_png(i, opts[:dir], opts[:prefix])
|
||||
@cards[i].save_png(i, opts[:dir], opts[:prefix], opts[:rotate])
|
||||
bar.increment
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ module Squib
|
||||
:prefix => "card_",
|
||||
:progress_bar => false,
|
||||
:range => :all,
|
||||
:rotate => false,
|
||||
:sheet => 0,
|
||||
:spacing => 0,
|
||||
:str => '',
|
||||
|
||||
@@ -3,8 +3,30 @@ module Squib
|
||||
|
||||
# :nodoc:
|
||||
# @api private
|
||||
def save_png(i, dir, prefix)
|
||||
cairo_context.target.write_to_png("#{dir}/#{prefix}#{i}.png")
|
||||
def save_png(i, dir, prefix, rotate)
|
||||
if rotate
|
||||
surface = rotated_image
|
||||
else
|
||||
surface = @cairo_surface
|
||||
end
|
||||
write_png(surface, i, dir, prefix)
|
||||
end
|
||||
|
||||
# :nodoc:
|
||||
# @api private
|
||||
def rotated_image
|
||||
rotated_cc = Cairo::Context.new(Cairo::ImageSurface.new(@height, @width) )
|
||||
rotated_cc.translate(@height * 0.5, @width * 0.5)
|
||||
rotated_cc.rotate(0.5 * Math::PI)
|
||||
rotated_cc.translate(@width * -0.5, @height * -0.5)
|
||||
rotated_cc.set_source(@cairo_surface)
|
||||
rotated_cc.paint
|
||||
rotated_cc.target
|
||||
end
|
||||
# :nodoc:
|
||||
# @api private
|
||||
def write_png(surface, i, dir, prefix)
|
||||
surface.write_to_png("#{dir}/#{prefix}#{i}.png")
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user