Browse Source

Added line cap and join settings for rect and curve

More forthcoming - these are proof-of-concept
dev
Andy Meneely 11 years ago
parent
commit
12c5cd675b
  1. 3
      lib/squib/api/shapes.rb
  2. 1
      lib/squib/args/arg_loader.rb
  3. 30
      lib/squib/args/draw.rb
  4. 8
      lib/squib/graphics/cairo_context_wrapper.rb
  5. 6
      lib/squib/graphics/shapes.rb
  6. 9
      samples/draw_shapes.rb
  7. 27
      spec/args/draw_spec.rb
  8. 8
      spec/data/samples/draw_shapes.rb.txt

3
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

1
lib/squib/args/arg_loader.rb

@ -1,5 +1,6 @@
require 'squib/constants'
require 'squib/conf'
require 'ostruct'
module Squib
# @api private

30
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

8
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

6
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

9
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,

27
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

8
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([])

Loading…
Cancel
Save