Add :scale shortcut to width/height of png and svg

Allows scaling while keeping the aspect ratio

Closes #91
This commit is contained in:
Andy Meneely
2015-07-27 13:25:12 -04:00
parent 6af9ff7501
commit e03a97ce7e
7 changed files with 173 additions and 10 deletions
+71
View File
@@ -0,0 +1,71 @@
require 'spec_helper'
require 'squib/args/scale_box'
describe Squib::Args::ScaleBox do
subject(:box) { Squib::Args::ScaleBox.new({}) }
context 'unit conversion' do
it 'converts units on all args' do
args = {x: ['1in', '2in'], y: 300, width: '1in', height: '1in'}
box.load!(args, expand_by: 2)
expect(box).to have_attributes(
x: [300, 600],
y: [300, 300],
width: [300, 300],
height: [300, 300],
)
end
end
context 'validation' do
it 'replaces with deck width and height' do
args = {width: :deck, height: :deck}
deck = OpenStruct.new(width: 123, height: 456)
box = Squib::Args::Box.new(deck)
box.load!(args, expand_by: 1)
expect(box).to have_attributes(width: [123], height: [456])
end
it 'allows :native' do
args = {width: :native, height: :native}
box.load!(args, expand_by: 1)
expect(box).to have_attributes(width: [:native], height: [:native])
end
it 'allows native to be a string' do
args = {width: 'native'}
box.load!(args, expand_by: 1)
expect(box).to have_attributes(width: [:native], height: [:native])
end
it 'allows :scale on width if height has to_f' do
args = {width: :scale, height: 75}
box.load!(args, expand_by: 1)
expect(box).to have_attributes(width: [:scale], height: [75])
end
it 'allows :scale on width if height has to_f' do
args = {width: 75, height: :scale}
box.load!(args, expand_by: 1)
expect(box).to have_attributes(width: [75], height: [:scale])
end
it 'disallows both :scale' do
args = {width: :scale, height: :scale}
expect { box.load!(args, expand_by: 1) }.to raise_error('if width is :scale, height must be a number')
end
it 'disallows non-to_f on width' do
args = {width: :foo}
expect { box.load!(args, expand_by: 1) }.to raise_error('width must be a number, :scale, :native, or :deck')
end
it 'disallows non-to_f on height' do
args = {height: :foo}
expect { box.load!(args, expand_by: 1) }.to raise_error('height must be a number, :scale, :native, or :deck')
end
end
end
+28
View File
@@ -42,6 +42,34 @@ cairo: set_source([ImageSurface, 305, 50])
cairo: paint([1.0])
cairo: restore([])
cairo: save([])
cairo: translate([200, 350])
cairo: rotate([0])
cairo: scale([0.2734375, 0.2734375])
cairo: render_rsvg_handle([RSVG::Handle, nil])
cairo: restore([])
cairo: save([])
cairo: translate([200, 390])
cairo: rotate([0])
cairo: scale([0.2734375, 0.2734375])
cairo: render_rsvg_handle([RSVG::Handle, nil])
cairo: restore([])
cairo: save([])
cairo: translate([240, 350])
cairo: scale([0.2734375, 0.2734375])
cairo: rotate([0])
cairo: translate([-240, -350])
cairo: set_source([ImageSurface, 240, 350])
cairo: paint([1.0])
cairo: restore([])
cairo: save([])
cairo: translate([240, 390])
cairo: scale([0.2734375, 0.2734375])
cairo: rotate([0])
cairo: translate([-240, -390])
cairo: set_source([ImageSurface, 240, 390])
cairo: paint([1.0])
cairo: restore([])
cairo: save([])
cairo: translate([50, 350])
cairo: rotate([0])
cairo: scale([0.5859375, 0.5859375])