Browse Source

Implementation of hand

dev
Andy Meneely 11 years ago
parent
commit
31b583d134
  1. 32
      lib/squib/api/save.rb
  2. 1
      lib/squib/constants.rb
  3. 2
      lib/squib/deck.rb
  4. 42
      lib/squib/graphics/hand.rb
  5. 48
      samples/hand.rb
  6. 28
      squib.sublime-project

32
lib/squib/api/save.rb

@ -39,7 +39,7 @@ module Squib
end
end
# Renders a range of files in a showcase as if they are sitting on a reflective surface
# Renders a range of cards in a showcase as if they are sitting in 3D on a reflective surface
# See {file:samples/showcase.rb} for full example
#
# @example
@ -51,7 +51,7 @@ module Squib
# @option opts [Fixnum] margin (75) the margin around the entire showcase
# @option opts [Fixnum] scale (0.8) percentage of original width of each (trimmed) card to scale to. Must be between 0.0 and 1.0, but starts looking bad around 0.6.
# @option opts [Fixnum] offset (1.1) percentage of the scaled width of each card to shift each offset. e.g. 1.1 is a 10% shift, and 0.95 is overlapping by 5%
# @option opts [String, Color] fill_color (:white) backdrop color. Usually black or white.
# @option opts [String, Color] fill_color (:white) backdrop color. Usually black or white. Supports gradients.
# @option opts [Fixnum] reflect_offset (15) the number of pixels between the bottom of the card and the reflection
# @option opts [Fixnum] reflect_strength (0.2) the starting alpha transparency of the reflection (at the top of the card). Percentage between 0 and 1. Looks more realistic at low values since even shiny surfaces lose a lot of light.
# @option opts [Fixnum] reflect_percent (0.25) the length of the reflection in percentage of the card. Larger values tend to make the reflection draw just as much attention as the card, which is not good.
@ -70,11 +70,33 @@ module Squib
opts[:dir], opts[:file])
end
# Renders a range of cards fanned out as if in a hand. Saves as PNG.
# See {file:samples/hand.rb} for full example
#
# @example
# hand range: :all, radius: :auto, margin: 20, fill_color: :white,
# angle_range: (Math::PI / -4.0)..(Math::PI / 2),
# dir: '_output', file: 'hand1.png'
#
# @option opts [Enumerable, :all] range (:all) the range of cards over which this will be rendered. See {file:README.md#Specifying_Ranges Specifying Ranges}
# @option opts [Fixnum] radius (:auto) the distance from the bottom of each card to the center of the fan. If set to `:auto`, then it is computed as 30% of the card's height.
# @option opts [Range] angle_range: ((Math::PI / -4.0)..(Math::PI / 2)). The overall width of the fan, in radians. Angle of zero is a vertical card. Further negative angles widen the fan counter-clockwise and positive angles widen the fan clockwise.
# @option opts [Fixnum] trim (0) the margin around the card to trim before putting into the image
# @option opts [Fixnum] trim_radius (0) the rounded rectangle radius around the card to trim before putting into the showcase
# @option opts [Fixnum] margin (75) the margin around the entire image
# @option opts [String, Color] fill_color (:white) backdrop color. Usually black or white. Supports gradients.
# @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] file ('hand.png') the file to save in dir. Will be overwritten.
# @return [nil] Returns nothing.
# @api public
def hand(opts = {})
opts = {file: 'hand.png', fill_color: :white}.merge(opts)
opts = {file: 'hand.png', fill_color: :white, radius: :auto, trim_radius: 0}
.merge(opts)
opts = needs(opts,[:range, :margin, :trim, :trim_radius, :creatable_dir, :file_to_save])
render_hand()
opts[:radius] = 0.3 * height if opts[:radius] == :auto
render_hand(opts[:range], opts[:radius], opts[:angle_range],
opts[:trim], opts[:trim_radius], opts[:margin],
opts[:fill_color], opts[:dir], opts[:file])
end
end

1
lib/squib/constants.rb

@ -6,6 +6,7 @@ module Squib
:align => :left,
:alpha => 1.0,
:angle => 0,
:angle_range => (Math::PI / -4.0)..(Math::PI / 4),
:blend => :none,
:color => :black,
:columns => 5,

2
lib/squib/deck.rb

@ -9,6 +9,8 @@ require 'squib/constants'
require 'squib/layout_parser'
require 'squib/args/unit_conversion'
require 'squib/conf'
require 'squib/graphics/showcase'
require 'squib/graphics/hand'
# The project module
#

42
lib/squib/graphics/hand.rb

@ -1 +1,41 @@
hand.rb
module Squib
class Deck
# Draw cards in a fan.
# @api private
def render_hand(range, radius, angle_range, trim, trim_radius, margin,
fill_color, dir, file)
cards = range.collect { |i| @cards[i] }
center_x = width / 2.0
center_y = radius + height
out_size = 3.0 * center_y
angle_delta = (angle_range.last - angle_range.first) / cards.size
cxt = Cairo::Context.new(Cairo::RecordingSurface.new(0, 0, out_size, out_size))
cxt.translate(out_size / 2.0, out_size / 2.0)
cxt.rotate(angle_range.first)
cxt.translate(-width, -width)
cards.each_with_index do |card, i|
cxt.translate(center_x, center_y)
cxt.rotate(angle_delta)
cxt.translate(-center_x, -center_y)
card.use_cairo do |card_cxt|
cxt.rounded_rectangle(trim, trim,
width - (2 * trim), height - (2 * trim),
trim_radius, trim_radius)
cxt.clip
cxt.set_source(card_cxt.target)
cxt.paint
cxt.reset_clip
end
end
x, y, w, h = cxt.target.ink_extents # I love Ruby assignment ;)
png_cxt = Cairo::Context.new(Cairo::ImageSurface.new(w + 2*margin, h + 2*margin))
png_cxt.set_source_color(fill_color)
png_cxt.paint
png_cxt.translate(-x + margin, -y + margin)
png_cxt.set_source(cxt.target)
png_cxt.paint
png_cxt.target.write_to_png("#{dir}/#{file}")
end
end
end

48
samples/hand.rb

@ -1,45 +1,21 @@
require 'squib'
# Built-in layouts are easy to use and extend
Squib::Deck.new(cards: 8, layout: 'playing-card.yml') do
background color: :white
background color: :cyan
rect x: 37, y: 37, width: 750, height: 1050, fill_color: :black, radius: 25
rect x: 75, y: 75, width: 675, height: 975, fill_color: :white, radius: 20
text str: ('A'..'H').to_a, layout: :bonus_ul, font: 'Sans bold 100'
# hand range: :all,
# center_x: :auto, center_y: :auto,
# angle_start: 0, angle_end: Math::PI,
# width: :auto, height: :auto
# save_png prefix: 'hand_'
#
# Here's the hacky version
# So much hardcoded magic to get this to work. Math is hard.
# Idea: getting the final image surface extents is hard math for me. Maybe
# just put it on a recording surface, then get the extents.
# Can't seem to create unbounded surfaces in rcairo, so just make it
# massively large enough. Paint the recording surface with proper margins
# at the end
cxt = Cairo::Context.new(Cairo::ImageSurface.new(2 * 1.3 * height, 2 * 1.3 * height))
cxt.translate( 1.3 * height , 1.3 * height)
cxt.rotate(Math::PI / -4.0)
cxt.translate( -1.3 * height, -1.3 * height)
cxt.translate(500, 500) # I don't even know why I need this
angle = (Math::PI / 2.0) / cards.size
radius_x, radius_y = 0 + (width / 2.0), 1.3 * height
each_with_index do |card, i|
cxt.translate(radius_x, radius_y)
cxt.rotate(angle)
cxt.translate(-radius_x, -radius_y)
card.use_cairo do |card_cxt|
cxt.rounded_rectangle(37, 37, 825-75, 1125-75, 25, 25)
cxt.clip
cxt.set_source(card_cxt.target)
cxt.paint
cxt.reset_clip
end
end
text str: ('A'..'Z').to_a, layout: :bonus_ul, font: 'Sans bold 100'
cxt.target.write_to_png('_output/hand.png')
# Defaults are sensible
hand #saves to _output/hand.png
# Here's a prettier version:
# - Each card is trimmed with rounded corners
# - Zero radius means cards rotate about the bottom of the card
# - Cards are shown in reverse order
hand trim: 37.5, trim_radius: 25,
radius: 0,
range: 7.downto(0),
file: 'hand_pretty.png'
end

28
squib.sublime-project vendored

@ -17,20 +17,20 @@
{"name": "rake sanity", "shell_cmd": "rake sanity",},
{"name": "rake spec_fastonly", "shell_cmd": "rake spec_fastonly",},
{"name": "rake run[hand]", "shell_cmd": "rake run[hand]",},
{"name": "rake run[config_text_markup]", "shell_cmd": "rake run[config_text_markup]",},
{"name": "rake run[csv_import]", "shell_cmd": "rake run[csv_import]",},
{"name": "rake run[custom_config]", "shell_cmd": "rake run[custom_config]",},
{"name": "rake run[draw_shapes]", "shell_cmd": "rake run[draw_shapes]",},
{"name": "rake run[embed_text]", "shell_cmd": "rake run[embed_text]",},
{"name": "rake run[load_images]", "shell_cmd": "rake run[load_images]",},
{"name": "rake run[text_options]", "shell_cmd": "rake run[text_options]",},
{"name": "rspec spec/args/typographer_spec.rb", "shell_cmd": "rspec spec/args/typographer_spec.rb", "working_dir": "${project_path:${folder}}"},
{"name": "rspec spec/conf_spec.rb", "shell_cmd": "rspec spec/conf_spec.rb", "working_dir": "${project_path:${folder}}"},
{"name": "rspec spec/graphics/graphics_images_spec.rb", "shell_cmd": "rspec spec/graphics/graphics_images_spec.rb", "working_dir": "${project_path:${folder}}"},
{"name": "rspec spec/graphics/graphics_shapes_spec.rb", "shell_cmd": "rspec spec/graphics/graphics_shapes_spec.rb", "working_dir": "${project_path:${folder}}"},
{"name": "rspec spec/graphics/graphics_shapes_spec.rb", "shell_cmd": "rspec spec/graphics/graphics_shapes_spec.rb", "working_dir": "${project_path:${folder}}"},
{"name": "rspec spec/graphics/graphics_text_spec.rb", "shell_cmd": "rspec spec/graphics/graphics_text_spec.rb", "working_dir": "${project_path:${folder}}"},
{"name": "rspec spec/samples/samples_regression_spec.rb", "shell_cmd": "rspec spec/samples/samples_regression_spec.rb", "working_dir": "${project_path:${folder}}"},
// {"name": "rake run[config_text_markup]", "shell_cmd": "rake run[config_text_markup]",},
// {"name": "rake run[csv_import]", "shell_cmd": "rake run[csv_import]",},
// {"name": "rake run[custom_config]", "shell_cmd": "rake run[custom_config]",},
// {"name": "rake run[draw_shapes]", "shell_cmd": "rake run[draw_shapes]",},
// {"name": "rake run[embed_text]", "shell_cmd": "rake run[embed_text]",},
// {"name": "rake run[load_images]", "shell_cmd": "rake run[load_images]",},
// {"name": "rake run[text_options]", "shell_cmd": "rake run[text_options]",},
// {"name": "rspec spec/args/typographer_spec.rb", "shell_cmd": "rspec spec/args/typographer_spec.rb", "working_dir": "${project_path:${folder}}"},
// {"name": "rspec spec/conf_spec.rb", "shell_cmd": "rspec spec/conf_spec.rb", "working_dir": "${project_path:${folder}}"},
// {"name": "rspec spec/graphics/graphics_images_spec.rb", "shell_cmd": "rspec spec/graphics/graphics_images_spec.rb", "working_dir": "${project_path:${folder}}"},
// {"name": "rspec spec/graphics/graphics_shapes_spec.rb", "shell_cmd": "rspec spec/graphics/graphics_shapes_spec.rb", "working_dir": "${project_path:${folder}}"},
// {"name": "rspec spec/graphics/graphics_shapes_spec.rb", "shell_cmd": "rspec spec/graphics/graphics_shapes_spec.rb", "working_dir": "${project_path:${folder}}"},
// {"name": "rspec spec/graphics/graphics_text_spec.rb", "shell_cmd": "rspec spec/graphics/graphics_text_spec.rb", "working_dir": "${project_path:${folder}}"},
// {"name": "rspec spec/samples/samples_regression_spec.rb", "shell_cmd": "rspec spec/samples/samples_regression_spec.rb", "working_dir": "${project_path:${folder}}"},
]
}

Loading…
Cancel
Save