Browse Source

Implementing rotate for save_png

dev
Andy Meneely 12 years ago
parent
commit
41038178ad
  1. 6
      README.md
  2. 9
      lib/squib/api/save.rb
  3. 1
      lib/squib/constants.rb
  4. 26
      lib/squib/graphics/save_images.rb
  5. 23
      samples/portrait-landscape.rb

6
README.md

@ -181,7 +181,7 @@ See the `custom_config` sample found [here](https://github.com/andymeneely/squib
{include:file:samples/custom_config.rb} {include:file:samples/custom_config.rb}
# Squib Principles and Patterns # Squib Best Practices and Patterns
I am eating my own dogfood and using Squib for prototyping my own games. From that experience, here are some pieces of advice. They are really just re-hashing of common Ruby community and good software engineering practices. I am eating my own dogfood and using Squib for prototyping my own games. From that experience, here are some pieces of advice. They are really just re-hashing of common Ruby community and good software engineering practices.
@ -212,6 +212,10 @@ By default, Squib assumes Git. But it's not dogmatic about it. Tracking your pro
For me, I tend to ignore any auto-generated files in my output folder, but version control everything else. I also try to keep my graphics vector files, so the files stay small. Version control is intended for source code, so large binary files don't usually get checked in unless absolutely necessary. For me, I tend to ignore any auto-generated files in my output folder, but version control everything else. I also try to keep my graphics vector files, so the files stay small. Version control is intended for source code, so large binary files don't usually get checked in unless absolutely necessary.
## Decks with multiple orientations or sizes
If you want to make a deck that has some portrait and some landscape cards, I recommend you use multiple `Squib::Deck`s. The pixel size of a given card is designed to not change thorughout the life of a `Squib::Deck`. To work with landscape cards, there is a `rotate` option on `save_png` so your print-on-demand PNGs can still be in portrait.
# Development # Development
Squib is currently in pre-release alpha, so the API is still maturing. If you are using Squib, however, I'd love to hear about it! Feel free to [file a bug or feature request](https://github.com/andymeneely/squib/issues). Squib is currently in pre-release alpha, so the API is still maturing. If you are using Squib, however, I'd love to hear about it! Feel free to [file a bug or feature request](https://github.com/andymeneely/squib/issues).

9
lib/squib/api/save.rb

@ -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 [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 [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 [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 # @return self
# @api public # @api public
def save(opts = {}) 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_png(opts) if opts[:format].include? :png
save_pdf(opts) if opts[:format].include? :pdf save_pdf(opts) if opts[:format].include? :pdf
self self
@ -24,13 +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.
# @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]) opts = needs(opts,[:range, :creatable_dir, :prefix, :rotate])
@progress_bar.start("Saving PNGs to #{opts[:dir]}/#{opts[:prefix]}*") do |bar| @progress_bar.start("Saving PNGs to #{opts[:dir]}/#{opts[:prefix]}*") do |bar|
opts[:range].each do |i| 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 bar.increment
end end
end end
@ -38,3 +40,4 @@ module Squib
end end
end end

1
lib/squib/constants.rb

@ -20,6 +20,7 @@ module Squib
:prefix => "card_", :prefix => "card_",
:progress_bar => false, :progress_bar => false,
:range => :all, :range => :all,
:rotate => false,
:sheet => 0, :sheet => 0,
:spacing => 0, :spacing => 0,
:str => '', :str => '',

26
lib/squib/graphics/save_images.rb

@ -3,8 +3,30 @@ module Squib
# :nodoc: # :nodoc:
# @api private # @api private
def save_png(i, dir, prefix) def save_png(i, dir, prefix, rotate)
cairo_context.target.write_to_png("#{dir}/#{prefix}#{i}.png") 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
end end

23
samples/portrait-landscape.rb

@ -0,0 +1,23 @@
require 'squib'
# For decks with both landscape and portrait orientations,
# we recommend using two separate decks.
# For print-on-demand, we can rotate all of the images in the final step.
# Normal cards
Squib::Deck.new(width: 825, height: 1125) do
background color: '#aaa'
text str: "This is portrait"
save_png prefix: "portrait_"
end
# Money cards are landscape
Squib::Deck.new(width: 1125, height: 825) do
background color: '#aaa'
text str: "This is landscape"
save_png prefix: "landscape_", rotate: true
end
Loading…
Cancel
Save