From 14e04a89c74ddf5b824a8a73ac7a05d474f6ec89 Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Tue, 19 May 2015 17:56:01 -0400 Subject: [PATCH] Started the star feature Contributes to #72 [skip ci] --- lib/squib/api/shapes.rb | 27 +++++++++++++++++++++++++++ lib/squib/constants.rb | 3 +++ lib/squib/graphics/shapes.rb | 22 ++++++++++++++++++++++ samples/draw_shapes.rb | 3 +++ squib.sublime-project | 2 +- 5 files changed, 56 insertions(+), 1 deletion(-) diff --git a/lib/squib/api/shapes.rb b/lib/squib/api/shapes.rb index cffb436..40ed624 100644 --- a/lib/squib/api/shapes.rb +++ b/lib/squib/api/shapes.rb @@ -154,6 +154,7 @@ module Squib # @option opts cy2 [Integer] (50) the y-coordinate of the second control point. 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}. + # @option opts fill_color [String] ('#0000') the color with which to fill the triangle. See {file:README.md#Specifying_Colors___Gradients Specifying Colors & Gradients} # @return [nil] intended to be void # @api public def curve(opts = {}) @@ -166,5 +167,31 @@ module Squib end end + # Draw a star at the given x,y + # @example + # star x: 10, y: 10, n: 5, angle: Math::PI / 4, inner_radius: 50, outer_radius: 100, + # fill_color: :green, stroke_color: :burgundy, :stroke_width: 3 + # + # @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 x [Fixnum] (0) the x-coordinate of the center. Supports Unit Conversion, see {file:README.md#Units Units}. + # @option opts y [Fixnum] (0) the y-coordinate of the center. Supports Unit Conversion, see {file:README.md#Units Units}. + # @option opts n [Integer] (5) the number of points on the star + # @option opts angle [Fixnum] (0) the angle at which to rotate + # @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}. + # @option opts fill_color [String] ('#0000') the color with which to fill the triangle. See {file:README.md#Specifying_Colors___Gradients Specifying Colors & Gradients} + # @return [nil] intended to be void + # @api public + def star(opts = {}) + opts = needs(opts, [:range, :x, :y, :n, :angle, :inner_radius, :outer_radius, + :layout, :fill_color, :stroke_color, :stroke_width]) + opts[:range].each do |i| + @cards[i].star(opts[:x][i], opts[:y][i], opts[:n][i], opts[:angle][i], + opts[:inner_radius][i], opts[:outer_radius][i], + opts[:fill_color][i], opts[:stroke_color][i], + opts[:stroke_width][i]) + end + end + end end diff --git a/lib/squib/constants.rb b/lib/squib/constants.rb index 9f93e86..924376e 100644 --- a/lib/squib/constants.rb +++ b/lib/squib/constants.rb @@ -30,13 +30,16 @@ module Squib :gap => 0, :height => :native, :hint => :off, + :inner_radius => 50, :img_dir => '.', :justify => false, :key => '*', :margin => 75, :markup => false, :mask => nil, + :n => 5, :offset => 1.1, + :outer_radius => 100, :prefix => 'card_', :progress_bar => false, :quotes => :dumb, diff --git a/lib/squib/graphics/shapes.rb b/lib/squib/graphics/shapes.rb index 1d6b68a..1f76734 100644 --- a/lib/squib/graphics/shapes.rb +++ b/lib/squib/graphics/shapes.rb @@ -102,5 +102,27 @@ module Squib end end + # :nodoc: + # @api private + def star(x, y, n, angle, inner_radius, outer_radius, fill_color, stroke_color, stroke_width) + use_cairo do |cc| + cc.translate(x, y) + cc.rotate(angle) + cc.translate(-x, -y) + + # coords = [] + # coords << [x, y - outer_radius] + # coords << [x, y - outer_radius] + # cc.move_to(*coords.first) + # cc.line_to(*coords[1..-1]) + + cc.set_source_squibcolor(stroke_color) + cc.set_line_width(stroke_width) + cc.stroke_preserve + cc.set_source_squibcolor(fill_color) + cc.fill + end + end + end end diff --git a/samples/draw_shapes.rb b/samples/draw_shapes.rb index 31fcb36..09688e4 100644 --- a/samples/draw_shapes.rb +++ b/samples/draw_shapes.rb @@ -25,5 +25,8 @@ Squib::Deck.new do stroke_width: 5.0, stroke_color: :cyan, fill_color: :burgundy + star x: 50, y: 1000, n: 5, inner_radius: 50, outer_radius: 100, + fill_color: :burgundy, stroke_color: :cyan, stroke_width: 8 + save_png prefix: 'shape_' end diff --git a/squib.sublime-project b/squib.sublime-project index b79cf70..e44a8cb 100644 --- a/squib.sublime-project +++ b/squib.sublime-project @@ -20,7 +20,7 @@ // {"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[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]",},