Browse Source

Allowing for save_png to rotate either direction

dev
Andy Meneely 11 years ago
parent
commit
d4bde92d89
  1. 4
      lib/squib/api/save.rb
  2. 10
      lib/squib/graphics/save_images.rb
  3. 15
      lib/squib/input_helpers.rb

4
lib/squib/api/save.rb

@ -25,14 +25,14 @@ 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 [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] 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 [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. # @option opts [Boolean, :clockwise, :counterclockwise] rotate (false) if true, the saved cards will be rotated 90 degrees clockwise. Or, rotate by the number of radians. Intended to rendering landscape instead of portrait.
# @return [nil] Returns nothing # @return [nil] Returns nothing
# @api public # @api public
def save_png(opts = {}) def save_png(opts = {})
opts = needs(opts,[:range, :creatable_dir, :prefix, :rotate]) opts = needs(opts,[:range, :creatable_dir, :prefix, :rotate])
@progress_bar.start("Saving PNGs to #{opts[:dir]}/#{opts[:prefix]}*", @cards.size) do |bar| @progress_bar.start("Saving PNGs to #{opts[:dir]}/#{opts[:prefix]}*", @cards.size) do |bar|
opts[:range].each do |i| opts[:range].each do |i|
@cards[i].save_png(i, opts[:dir], opts[:prefix], opts[:rotate]) @cards[i].save_png(i, opts[:dir], opts[:prefix], opts[:rotate], opts[:angle])
bar.increment bar.increment
end end
end end

10
lib/squib/graphics/save_images.rb

@ -3,9 +3,9 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def save_png(i, dir, prefix, rotate) def save_png(i, dir, prefix, do_rotate, angle)
if rotate if [true, :clockwise, :counterclockwise].include?(do_rotate)
surface = rotated_image surface = rotated_image(angle)
else else
surface = @cairo_surface surface = @cairo_surface
end end
@ -14,10 +14,10 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def rotated_image def rotated_image(angle)
rotated_cc = Cairo::Context.new(Cairo::ImageSurface.new(@height, @width) ) rotated_cc = Cairo::Context.new(Cairo::ImageSurface.new(@height, @width) )
rotated_cc.translate(@height * 0.5, @width * 0.5) rotated_cc.translate(@height * 0.5, @width * 0.5)
rotated_cc.rotate(0.5 * Math::PI) rotated_cc.rotate(angle)
rotated_cc.translate(@width * -0.5, @height * -0.5) rotated_cc.translate(@width * -0.5, @height * -0.5)
rotated_cc.set_source(@cairo_surface) rotated_cc.set_source(@cairo_surface)
rotated_cc.paint rotated_cc.paint

15
lib/squib/input_helpers.rb

@ -26,6 +26,7 @@ module Squib
opts = radiusify(opts) if params.include? :rect_radius opts = radiusify(opts) if params.include? :rect_radius
opts = svgidify(opts) if params.include? :svgid opts = svgidify(opts) if params.include? :svgid
opts = formatify(opts) if params.include? :formats opts = formatify(opts) if params.include? :formats
opts = rotateify(opts) if params.include? :rotate
opts opts
end end
module_function :needs module_function :needs
@ -175,5 +176,19 @@ module Squib
end end
module_function :svgidify module_function :svgidify
# :nodoc:
# @api private
def rotateify(opts)
case opts[:rotate]
when true, :clockwise
opts[:angle] = 0.5 * Math::PI
when :counterclockwise
opts[:angle] = 1.5 * Math::PI
end
Squib.logger.debug {"After rotateify: #{opts}"}
opts
end
module_function :svgidify
end end
end end
Loading…
Cancel
Save