diff --git a/lib/squib/api/shapes.rb b/lib/squib/api/shapes.rb index cbdca9b..2f51c1d 100644 --- a/lib/squib/api/shapes.rb +++ b/lib/squib/api/shapes.rb @@ -166,10 +166,11 @@ module Squib def curve(opts = {}) opts = needs(opts, [:range, :x1, :y1, :cx1, :cy1, :x2, :y2, :cx2, :cy2, :layout, :fill_color, :stroke_color, :stroke_width]) + draw = Args::Draw.new.load!(opts, expand_by: size, layout: layout, dpi: dpi) opts[:range].each do |i| @cards[i].curve(opts[:x1][i], opts[:y1][i], opts[:cx1][i], opts[:cy1][i], opts[:x2][i], opts[:y2][i], opts[:cx2][i], opts[:cy2][i], - opts[:fill_color][i], opts[:stroke_color][i], opts[:stroke_width][i]) + draw[i]) end end diff --git a/lib/squib/args/arg_loader.rb b/lib/squib/args/arg_loader.rb index 9954f5f..bf99f9f 100644 --- a/lib/squib/args/arg_loader.rb +++ b/lib/squib/args/arg_loader.rb @@ -1,5 +1,6 @@ require 'squib/constants' require 'squib/conf' +require 'ostruct' module Squib # @api private diff --git a/lib/squib/args/draw.rb b/lib/squib/args/draw.rb index 3d48cae..6fe0b63 100644 --- a/lib/squib/args/draw.rb +++ b/lib/squib/args/draw.rb @@ -1,3 +1,4 @@ +require 'cairo' require 'squib/args/arg_loader' module Squib @@ -8,7 +9,12 @@ module Squib include ArgLoader def self.parameters - { fill_color: '#0000', stroke_color: :black, stroke_width: 2.0 } + { fill_color: '#0000', + stroke_color: :black, + stroke_width: 2.0, + join: :miter, + cap: 'butt' + } end def self.expanding_parameters @@ -19,6 +25,28 @@ module Squib [:stroke_width] end + def validate_join(arg, _i) + case arg.to_s.strip.downcase + when 'miter' + Cairo::LINE_JOIN_MITER + when 'round' + Cairo::LINE_JOIN_ROUND + when 'bevel' + Cairo::LINE_JOIN_BEVEL + end + end + + def validate_cap(arg, _i) + case arg.to_s.strip.downcase + when 'butt' + Cairo::LINE_CAP_BUTT + when 'round' + Cairo::LINE_CAP_ROUND + when 'square' + Cairo::LINE_CAP_SQUARE + end + end + end end diff --git a/lib/squib/graphics/cairo_context_wrapper.rb b/lib/squib/graphics/cairo_context_wrapper.rb index cd53b09..78662b9 100644 --- a/lib/squib/graphics/cairo_context_wrapper.rb +++ b/lib/squib/graphics/cairo_context_wrapper.rb @@ -24,7 +24,8 @@ 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, :target, :new_path, :fill_preserve, :close_path + :stroke_preserve, :target, :new_path, :fill_preserve, :close_path, + :set_line_join, :set_line_cap # :nodoc: # @api private @@ -51,13 +52,16 @@ module Squib # Convenience method for a common task # @api private - def fill_n_stroke(fill_color, stroke_color, stroke_width) + def fill_n_stroke(fill_color, stroke_color, stroke_width, line_join = 0, line_cap = Cairo::LINE_CAP_BUTT) set_source_squibcolor(fill_color) fill_preserve set_source_squibcolor(stroke_color) set_line_width(stroke_width) + set_line_join(line_join) + set_line_cap(line_cap) stroke end + end end end \ No newline at end of file diff --git a/lib/squib/graphics/shapes.rb b/lib/squib/graphics/shapes.rb index 26f4373..25c3219 100644 --- a/lib/squib/graphics/shapes.rb +++ b/lib/squib/graphics/shapes.rb @@ -7,7 +7,7 @@ module Squib def rect(box, draw) use_cairo do |cc| cc.rounded_rectangle(box.x, box.y, box.width, box.height, box.x_radius, box.y_radius) - cc.fill_n_stroke(draw.fill_color, draw.stroke_color, draw.stroke_width) + cc.fill_n_stroke(draw.fill_color, draw.stroke_color, draw.stroke_width, draw.join) end end @@ -68,11 +68,11 @@ module Squib # :nodoc: # @api private - def curve(x1, y1, cx1, cy1, x2, y2, cx2, cy2, fill_color, stroke_color, stroke_width) + def curve(x1, y1, cx1, cy1, x2, y2, cx2, cy2, draw) use_cairo do |cc| cc.move_to(x1, y1) cc.curve_to(cx1, cy1, cx2, cy2, x2, y2) - cc.fill_n_stroke(fill_color, stroke_color, stroke_width) + cc.fill_n_stroke(draw.fill_color, draw.stroke_color, draw.stroke_width, draw.join, draw.cap) end end diff --git a/samples/draw_shapes.rb b/samples/draw_shapes.rb index 546b98c..fe350c1 100644 --- a/samples/draw_shapes.rb +++ b/samples/draw_shapes.rb @@ -2,8 +2,12 @@ require 'squib' Squib::Deck.new do background color: :white + + rect x: 300, y: 100, width: 200, height: 50 + rect x: 300, y: 300, width: 400, height: 400, - fill_color: :blue, stroke_color: :red, stroke_width: 50.0 + fill_color: :blue, stroke_color: :red, stroke_width: 50.0, + join: 'bevel' circle x: 600, y: 600, radius: 75, fill_color: :gray, stroke_color: :green, stroke_width: 8.0 @@ -13,7 +17,6 @@ Squib::Deck.new do x3: 75, y3: 250, fill_color: :gray, stroke_color: :green, stroke_width: 3.0 - line x1: 50, y1: 550, x2: 150, y2: 650, stroke_width: 25.0 @@ -21,7 +24,7 @@ Squib::Deck.new do curve x1: 50, y1: 850, cx1: 150, cy1: 700, x2: 625, y2: 900, cx2: 150, cy2: 700, stroke_width: 12.0, stroke_color: :cyan, - fill_color: :burgundy + fill_color: :burgundy, cap: 'round' ellipse x: 50, y: 925, width: 200, height: 100, stroke_width: 5.0, stroke_color: :cyan, diff --git a/spec/args/draw_spec.rb b/spec/args/draw_spec.rb new file mode 100644 index 0000000..555536e --- /dev/null +++ b/spec/args/draw_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' +require 'squib/args/box' + +describe Squib::Args::Draw do + subject(:draw) { Squib::Args::Draw.new } + + context 'unit conversion' do + + it 'converts units on stroke width' do + args = {stroke_width: '2in'} + draw.load!(args, expand_by: 2) + expect(draw).to have_attributes(stroke_width: [600, 600]) + end + + end + + context 'validation' do + + it 'converts to Cairo options' do + args = {join: 'bevel', cap: 'round'} + draw.load!(args) + expect(draw).to have_attributes(join: [Cairo::LINE_JOIN_BEVEL]) + end + + end + +end \ No newline at end of file diff --git a/spec/data/samples/draw_shapes.rb.txt b/spec/data/samples/draw_shapes.rb.txt index 1c73548..518e8c1 100644 --- a/spec/data/samples/draw_shapes.rb.txt +++ b/spec/data/samples/draw_shapes.rb.txt @@ -4,6 +4,14 @@ cairo: set_source_color([:white]) cairo: paint([]) cairo: restore([]) cairo: save([]) +cairo: rounded_rectangle([300, 100, 200, 50, 0, 0]) +cairo: set_source_color(["#0000"]) +cairo: fill_preserve([]) +cairo: set_source_color([:black]) +cairo: set_line_width([2.0]) +cairo: stroke([]) +cairo: restore([]) +cairo: save([]) cairo: rounded_rectangle([300, 300, 400, 400, 0, 0]) cairo: set_source_color([:blue]) cairo: fill_preserve([])