-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
62a9959
commit e0a180c
Showing
10 changed files
with
194 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'forwardable' | ||
require 'squib/graphics/gradient_regex' | ||
|
||
module Squib | ||
module Graphics | ||
class CairoContextWrapper | ||
extend Forwardable | ||
attr_accessor :cairo_cxt | ||
|
||
def initialize(cairo_cxt) | ||
@cairo_cxt = cairo_cxt | ||
end | ||
|
||
def_delegators :cairo_cxt, :save, :set_source_color, :paint, :restore, | ||
:translate, :rotate, :move_to, :update_pango_layout, :width, :height, | ||
:show_pango_layout, :rounded_rectangle, :set_line_width, :stroke, :fill, | ||
:set_source, :scale, :render_rsvg_handle, :circle, :triangle, :line_to, | ||
:operator=, :show_page, :clip, :transform, :mask, :create_pango_layout | ||
|
||
def set_source_squibcolor(arg) | ||
if match = arg.match(LINEAR_GRADIENT) | ||
x1, y1, x2, y2 = match.captures | ||
linear = Cairo::LinearPattern.new(x1.to_f, y1.to_f, x2.to_f, y2.to_f) | ||
arg.scan(STOPS).each do |color, offset| | ||
linear.add_color_stop(offset.to_f, color) | ||
end | ||
@cairo_cxt.set_source(linear) | ||
elsif match = arg.match(RADIAL_GRADIENT) | ||
x1, y1, r1, x2, y2, r2 = match.captures | ||
linear = Cairo::RadialPattern.new(x1.to_f, y1.to_f, r1.to_f, | ||
x2.to_f, y2.to_f, r2.to_f) | ||
arg.scan(STOPS).each do |color, offset| | ||
linear.add_color_stop(offset.to_f, color) | ||
end | ||
@cairo_cxt.set_source(linear) | ||
else | ||
@cairo_cxt.set_source_color(arg) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module Squib | ||
module Graphics | ||
STOPS = / # used to capture the stops | ||
\s* # leading whitespace is ok | ||
(\#?[\w]+) # color | ||
@ # no spaces here | ||
(\d+\.?\d*) # offset number | ||
/x | ||
|
||
LINEAR_GRADIENT = / | ||
\( \s* # coordinate 1 | ||
(\d+\.?\d*) \s* # x1 number | ||
,\s* # whitespace after comma is ok | ||
(\d+\.?\d*) \s* # y1 number | ||
\) | ||
\s* # space between coordinates is ok | ||
\( \s* # coordinate 2 | ||
(\d+\.?\d*) \s* # x2 number | ||
,\s* # whitespace after comma is ok | ||
(\d+\.?\d*) \s* # y2 number | ||
\) | ||
(#{STOPS})+ # stops | ||
\s* # trailing whitespace is ok | ||
/x | ||
|
||
RADIAL_GRADIENT = / | ||
\( \s* # coordinate 1 | ||
(\d+\.?\d*) \s* # x1 number | ||
,\s* # whitespace after comma is ok | ||
(\d+\.?\d*) \s* # y1 number | ||
,\s* # whitespace after comma is ok | ||
(\d+\.?\d*) \s* # r1 number | ||
\) | ||
\s* # space between coordinates is ok | ||
\( \s* # coordinate 2 | ||
(\d+\.?\d*) \s* # x2 number | ||
,\s* # whitespace after comma is ok | ||
(\d+\.?\d*) \s* # y2 number | ||
,\s* # whitespace after comma is ok | ||
(\d+\.?\d*) \s* # r2 number | ||
\) | ||
(#{STOPS})+ # stops | ||
\s* # trailing whitespace is ok | ||
/x | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'squib' | ||
|
||
Squib::Deck.new do | ||
# Just about anywhere Squib takes in a color it can also take in a gradient too | ||
# Note that we need to provide xy coordinates. | ||
# These are relative to the xy provided the command. | ||
background color: '(0,0)(0,1125) #ccc@0.0 #111@1.0' | ||
|
||
# Radial gradients look like this | ||
circle x: 415, y: 415, radius: 100, stroke_color: '#0000', | ||
fill_color: '(425,400,2)(425,400,120) #ccc@0.0 #111@1.0' | ||
|
||
save_png prefix: 'gradient_' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require 'spec_helper' | ||
require 'squib/graphics/cairo_context_wrapper' | ||
|
||
describe Squib::Graphics::CairoContextWrapper do | ||
|
||
let(:cairo) { double(Cairo::Context) } | ||
subject { Squib::Graphics::CairoContextWrapper.new(cairo) } | ||
|
||
it 'passes on colors as normal' do | ||
expect(cairo).to receive(:set_source_color).with('blue') | ||
subject.set_source_squibcolor('blue') | ||
end | ||
|
||
it 'passes on color symbols as normal' do | ||
expect(cairo).to receive(:set_source_color).with(:blue) | ||
subject.set_source_squibcolor(:blue) | ||
end | ||
|
||
it 'passes on color hashes' do | ||
expect(cairo).to receive(:set_source_color) | ||
.with('#aabbccdd') | ||
subject.set_source_squibcolor('#aabbccdd') | ||
end | ||
|
||
context 'regex variations for linear gradients' do | ||
before(:each) do | ||
dbl = double(Cairo::LinearPattern) | ||
expect(Cairo::LinearPattern).to receive(:new).with(1,2,3,4).and_return(dbl) | ||
expect(dbl).to receive(:add_color_stop).with(0.0, 'blue') | ||
expect(dbl).to receive(:add_color_stop).with(1.0, 'red') | ||
expect(cairo).to receive(:set_source).with(dbl) | ||
end | ||
|
||
it('handles no decimals' ) { subject.set_source_squibcolor('(1,2) (3,4) blue@0 red@1') } | ||
it('handles decimals' ) { subject.set_source_squibcolor('(1.0,2.0) (3.0,4.0) blue@0 red@1') } | ||
it('handles no whitespace') { subject.set_source_squibcolor('(1,2)(3,4)blue@0red@1') } | ||
it('handles whitespace' ) { subject.set_source_squibcolor(' ( 1 , 2 ) ( 3 , 4 ) blue@0 red@1 ') } | ||
end | ||
|
||
context 'regex variations for radial gradients' do | ||
before(:each) do | ||
dbl = double(Cairo::RadialPattern) | ||
expect(Cairo::RadialPattern).to receive(:new).with(1,2,5,3,4,6).and_return(dbl) | ||
expect(dbl).to receive(:add_color_stop).with(0.0, 'blue') | ||
expect(dbl).to receive(:add_color_stop).with(1.0, 'red') | ||
expect(cairo).to receive(:set_source).with(dbl) | ||
end | ||
|
||
it('handles no decimals' ) { subject.set_source_squibcolor('(1,2,5) (3,4,6) blue@0 red@1') } | ||
it('handles decimals' ) { subject.set_source_squibcolor('(1.0,2.0,5.0) (3.0,4.0,6.0) blue@0 red@1') } | ||
it('handles no whitespace') { subject.set_source_squibcolor('(1,2,5)(3,4,6)blue@0red@1') } | ||
it('handles whitespace' ) { subject.set_source_squibcolor(' ( 1 , 2 , 5 ) ( 3 , 4 , 6 ) blue@0 red@1 ') } | ||
end | ||
|
||
context 'regex handles hash notation' do | ||
it 'on radial patterns' do | ||
dbl = double(Cairo::RadialPattern) | ||
expect(Cairo::RadialPattern).to receive(:new).with(1,2,5,3,4,6).and_return(dbl) | ||
expect(dbl).to receive(:add_color_stop).with(0.0, '#def') | ||
expect(dbl).to receive(:add_color_stop).with(1.0, '#112233') | ||
expect(cairo).to receive(:set_source).with(dbl) | ||
subject.set_source_squibcolor('(1,2,5) (3,4,6) #def@0 #112233@1') | ||
end | ||
|
||
it 'on linear patterns' do | ||
dbl = double(Cairo::LinearPattern) | ||
expect(Cairo::LinearPattern).to receive(:new).with(1,2,3,4).and_return(dbl) | ||
expect(dbl).to receive(:add_color_stop).with(0.0, '#def') | ||
expect(dbl).to receive(:add_color_stop).with(1.0, '#112233') | ||
expect(cairo).to receive(:set_source).with(dbl) | ||
subject.set_source_squibcolor('(1,2) (3,4) #def@0 #112233@1') | ||
end | ||
end | ||
|
||
end |