From 824e26d2c29b576dd4340992ed3c5b733cb514cd Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Wed, 13 May 2015 09:42:14 -0400 Subject: [PATCH] Hacked together a basic example to work from --- lib/squib/graphics/cairo_context_wrapper.rb | 2 +- lib/squib/graphics/hand.rb | 1 + samples/hand.rb | 51 +++++++++++++++------ 3 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 lib/squib/graphics/hand.rb diff --git a/lib/squib/graphics/cairo_context_wrapper.rb b/lib/squib/graphics/cairo_context_wrapper.rb index f5c26ba..d53782b 100644 --- a/lib/squib/graphics/cairo_context_wrapper.rb +++ b/lib/squib/graphics/cairo_context_wrapper.rb @@ -23,7 +23,7 @@ module Squib :set_source, :scale, :render_rsvg_handle, :circle, :triangle, :line_to, :operator=, :show_page, :clip, :transform, :mask, :create_pango_layout, :antialias=, :curve_to, :matrix, :matrix=, :identity_matrix, :pango_layout_path, - :stroke_preserve + :stroke_preserve, :target # :nodoc: # @api private diff --git a/lib/squib/graphics/hand.rb b/lib/squib/graphics/hand.rb new file mode 100644 index 0000000..29c98f6 --- /dev/null +++ b/lib/squib/graphics/hand.rb @@ -0,0 +1 @@ +hand.rb \ No newline at end of file diff --git a/samples/hand.rb b/samples/hand.rb index fc3d4df..c794693 100644 --- a/samples/hand.rb +++ b/samples/hand.rb @@ -2,19 +2,44 @@ require 'squib' # Built-in layouts are easy to use and extend Squib::Deck.new(cards: 8, layout: 'playing-card.yml') do - # background color: :white - # rect x: 37, y: 37, width: 750, height: 1050, fill_color: :black - # rect x: 75, y: 75, width: 675, height: 975, fill_color: :white - # text str: ('A'..'H').to_a, layout: :bonus_ul, font: 'Sans bold 100' - # def helper(a) - # puts a - # end - # helper('abc') - # helper('def') - # # hand range: :all, - # # center_x: :auto, center_y: :auto, - # # angle_start: 0, angle_end: Math::PI, - # # width: :auto, height: :auto, + background color: :white + 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 + + cxt.target.write_to_png('_output/hand.png') + end