Browse Source

Initial implementation of curves

Contributes to #37
dev
Andy Meneely 11 years ago
parent
commit
2cc9cc8e93
  1. 21
      lib/squib/api/shapes.rb
  2. 8
      lib/squib/constants.rb
  3. 2
      lib/squib/graphics/cairo_context_wrapper.rb
  4. 16
      lib/squib/graphics/shapes.rb
  5. 5
      samples/draw_shapes.rb
  6. 11
      spec/data/samples/draw_shapes.rb.txt
  7. 2
      spec/spec_helper.rb
  8. 6
      squib.sublime-project

21
lib/squib/api/shapes.rb

@ -113,5 +113,26 @@ module Squib
end end
end end
# Draw a curve using the given coordinates
#
# @option opts range [Enumerable, :all] (:all) the range of cards over which this will be rendered. See {file:README.md#Specifying_Ranges Specifying Ranges}
# @option opts x1 [Integer] (0) the x-coordinate to place. Supports Unit Conversion, see {file:README.md#Units Units}.
# @option opts y1 [Integer] (0) the y-coordinate to place. Supports Unit Conversion, see {file:README.md#Units Units}.
# @option opts x2 [Integer] (50) the x-coordinate to place. Supports Unit Conversion, see {file:README.md#Units Units}.
# @option opts y2 [Integer] (50) the y-coordinate to place. Supports Unit Conversion, see {file:README.md#Units Units}.
# @option opts stroke_color [String] (:black) the color with which to stroke the line. See {file:README.md#Specifying_Colors___Gradients Specifying Colors & Gradients}.
# @option opts stroke_width [Decimal] (2.0) the width of the outside stroke. Supports Unit Conversion, see {file:README.md#Units Units}.
# @return [nil] intended to be void
# @api public
def curve(opts = {})
opts = needs(opts, [:range, :x1, :y1, :dx1, :dy1, :x2, :y2, :dx2, :dy2,
:layout, :fill_color, :stroke_color, :stroke_width])
opts[:range].each do |i|
@cards[i].curve(opts[:x1][i], opts[:y1][i], opts[:dx1][i], opts[:dy1][i],
opts[:x2][i], opts[:y2][i], opts[:dx2][i], opts[:dy2][i],
opts[:fill_color][i], opts[:stroke_color][i], opts[:stroke_width][i])
end
end
end end
end end

8
lib/squib/constants.rb

@ -12,6 +12,10 @@ module Squib
:count_format => '%02d', :count_format => '%02d',
:default_font => 'Arial 36', :default_font => 'Arial 36',
:dir => '_output', :dir => '_output',
:dx1 => 0,
:dx2 => 0,
:dy1 => 0,
:dy2 => 0,
:ellipsize => :end, :ellipsize => :end,
:face => :left, :face => :left,
:fill_color => '#0000', :fill_color => '#0000',
@ -97,6 +101,10 @@ module Squib
:blend => :blend, :blend => :blend,
:circle_radius => :radius, :circle_radius => :radius,
:color => :color, :color => :color,
:dx1 => :dx1,
:dx2 => :dx2,
:dy1 => :dy1,
:dy2 => :dy2,
:ellipsize => :ellipsize, :ellipsize => :ellipsize,
:files => :file, :files => :file,
:fill_color => :fill_color, :fill_color => :fill_color,

2
lib/squib/graphics/cairo_context_wrapper.rb

@ -22,7 +22,7 @@ module Squib
:show_pango_layout, :rounded_rectangle, :set_line_width, :stroke, :fill, :show_pango_layout, :rounded_rectangle, :set_line_width, :stroke, :fill,
:set_source, :scale, :render_rsvg_handle, :circle, :triangle, :line_to, :set_source, :scale, :render_rsvg_handle, :circle, :triangle, :line_to,
:operator=, :show_page, :clip, :transform, :mask, :create_pango_layout, :operator=, :show_page, :clip, :transform, :mask, :create_pango_layout,
:antialias= :antialias=, :curve_to
# :nodoc: # :nodoc:
# @api private # @api private

16
lib/squib/graphics/shapes.rb

@ -58,5 +58,21 @@ module Squib
end end
end end
# :nodoc:
# @api private
def curve(x1, y1, dx1, dy1, x2, y2, dx2, dy2, fill_color, stroke_color, stroke_width)
use_cairo do |cc|
cc.move_to(x1, y1)
cc.curve_to(dx1, dy1, dx2, dy2, x2, y2)
cc.set_line_width(stroke_width)
cc.set_source_squibcolor(stroke_color)
cc.stroke
cc.move_to(x1, y1)
cc.curve_to(dx1, dy1, dx2, dy2, x2, y2)
cc.set_source_squibcolor(fill_color)
cc.fill
end
end
end end
end end

5
samples/draw_shapes.rb

@ -15,5 +15,10 @@ Squib::Deck.new do
x2: 150, y2: 650, x2: 150, y2: 650,
stroke_width: 25.0 stroke_width: 25.0
curve x1: 50, y1: 850, dx1: 150, dy1: 700,
x2: 625, y2: 900, dx2: 150, dy2: 700,
stroke_width: 12.0, stroke_color: :cyan,
fill_color: :burgundy
save_png prefix: 'shape_' save_png prefix: 'shape_'
end end

11
spec/data/samples/draw_shapes.rb.txt

@ -34,4 +34,15 @@ cairo: set_source_color([:black])
cairo: set_line_width([25.0]) cairo: set_line_width([25.0])
cairo: stroke([]) cairo: stroke([])
cairo: restore([]) cairo: restore([])
cairo: save([])
cairo: move_to([50, 850])
cairo: curve_to([150, 700, 150, 700, 625, 900])
cairo: set_line_width([12.0])
cairo: set_source_color([:cyan])
cairo: stroke([])
cairo: move_to([50, 850])
cairo: curve_to([150, 700, 150, 700, 625, 900])
cairo: set_source_color([:burgundy])
cairo: fill([])
cairo: restore([])
surface: write_to_png(["_output/shape_00.png"]) surface: write_to_png(["_output/shape_00.png"])

2
spec/spec_helper.rb

@ -84,7 +84,7 @@ def mock_cairo(strio)
update_pango_layout width height show_pango_layout rounded_rectangle update_pango_layout width height show_pango_layout rounded_rectangle
set_line_width stroke fill set_source scale render_rsvg_handle circle set_line_width stroke fill set_source scale render_rsvg_handle circle
triangle line_to operator= show_page clip transform mask rectangle triangle line_to operator= show_page clip transform mask rectangle
reset_clip antialias=).each do |m| reset_clip antialias= curve_to).each do |m|
allow(cxt).to receive(m) { |*args| strio << scrub_hex("cairo: #{m}(#{args})\n") } allow(cxt).to receive(m) { |*args| strio << scrub_hex("cairo: #{m}(#{args})\n") }
end end

6
squib.sublime-project vendored

@ -20,6 +20,10 @@
"name": "rake run[embed_text]", "name": "rake run[embed_text]",
"shell_cmd": "rake run[embed_text]", "shell_cmd": "rake run[embed_text]",
}, },
{
"name": "rake run[draw_shapes]",
"shell_cmd": "rake run[draw_shapes]",
},
{ {
"name": "rspec spec/samples/samples_regression_spec.rb", "name": "rspec spec/samples/samples_regression_spec.rb",
"shell_cmd": "rspec spec/samples/samples_regression_spec.rb", "shell_cmd": "rspec spec/samples/samples_regression_spec.rb",
@ -29,7 +33,7 @@
"name": "rspec spec/graphics/graphics_text_spec.rb", "name": "rspec spec/graphics/graphics_text_spec.rb",
"shell_cmd": "rspec spec/graphics/graphics_text_spec.rb", "shell_cmd": "rspec spec/graphics/graphics_text_spec.rb",
"working_dir": "${project_path:${folder}}" "working_dir": "${project_path:${folder}}"
} },
], ],
} }

Loading…
Cancel
Save