Add :scale shortcut to width/height of png and svg
Allows scaling while keeping the aspect ratio Closes #91dev
parent
6af9ff7501
commit
e03a97ce7e
|
|
@ -13,6 +13,7 @@ Features
|
|||
* The `text` method will throw a warning when it needs to ellipsize text (i.e. too much text for a fixed-size text box). Can be turned off in `config.yml`. (#80)
|
||||
* Upgraded roo (Excel parsing) to 2.1.0. Macro-enabled Excel files can be parsed now (i.e. `xlsm`), although I've only mildly tested this. (cddea47ba56add286639e493d5cc0146245eca68)
|
||||
* New built-in layouts: `fantasy.yml` and `economy.yml`. Demonstrated in new sample `layouts_builtin.rb` (#97)
|
||||
* Added `:scale` shortcut to `width` and `height` options for `png` and `svg`. Allows you to set the width and the image will scale while keeping its aspect ratio. (e.g. `svg width: 500, height: :scale`) (#91)
|
||||
|
||||
Compatibility:
|
||||
* All drawn shapes (e.g. circle, triangle, star) will now draw their stroke on top of the fill. This was not consistent before, and now it is (because Squib is more DRY about it!). This means that your `stroke_width` might render wider than before, but now it's accurate.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
require 'squib/args/card_range'
|
||||
require 'squib/args/paint'
|
||||
require 'squib/args/box'
|
||||
require 'squib/args/scale_box'
|
||||
require 'squib/args/transform'
|
||||
require 'squib/args/input_file'
|
||||
require 'squib/args/svg_special'
|
||||
|
|
@ -18,8 +18,8 @@ module Squib
|
|||
# @option opts file [String] ((empty)) file(s) to read in. If it's a single file, then it's use for every card in range. If the parameter is an Array of files, then each file is looked up for each card. If any of them are nil or '', nothing is done. See {file:README.md#Specifying_Files Specifying Files}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
|
||||
# @option opts x [Integer] (0) the x-coordinate to place. Supports Arrays, see {file:README#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}. Supports Unit Conversion, see {file:README.md#Units Units}.
|
||||
# @option opts y [Integer] (0) the y-coordinate to place. Supports Arrays, see {file:README#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}. Supports Unit Conversion, see {file:README.md#Units Units}.
|
||||
# @option opts width [Integer] (:native) the pixel width that the image should scale to. Scaling PNGs is not recommended for professional-looking cards. When set to `:native`, uses the DPI and units of the loaded SVG document. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}. Supports Unit Conversion, see {file:README.md#Units Units}.
|
||||
# @option opts height [Integer] (:native) the pixel width that the image should scale to. Scaling PNGs is not recommended for professional-looking cards. When set to `:native`, uses the DPI and units of the loaded SVG document. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}. Supports Unit Conversion, see {file:README.md#Units Units}.
|
||||
# @option opts width [Integer, :native, :scale, :deck] (:native) the pixel width that the image should scale to. :deck will scale to the deck width. :scale will use the height to scale and keep native the aspect ratio. Scaling PNGs is not recommended for professional-looking cards. When set to `:native`, uses the DPI and units of the loaded SVG document. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}. Supports Unit Conversion, see {file:README.md#Units Units}.
|
||||
# @option opts height [Integer, :native, :scale, :deck] (:native) the pixel width that the image should scale to. :deck will scale to the deck height. :scale will use the width to scale and keep native the aspect ratio. Scaling PNGs is not recommended for professional-looking cards. When set to `:native`, uses the DPI and units of the loaded SVG document. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}. Supports Unit Conversion, see {file:README.md#Units Units}.
|
||||
# @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:README.md#Custom_Layouts Custom Layouts}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
|
||||
# @option opts alpha [Decimal] (1.0) the alpha-transparency percentage used to blend this image. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
|
||||
# @option opts blend [:none, :multiply, :screen, :overlay, :darken, :lighten, :color_dodge, :color_burn, :hard_light, :soft_light, :difference, :exclusion, :hsl_hue, :hsl_saturation, :hsl_color, :hsl_luminosity] (:none) the composite blend operator used when applying this image. See Blend Modes at http://cairographics.org/operators. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
|
||||
|
|
@ -31,7 +31,7 @@ module Squib
|
|||
Dir.chdir(img_dir) do
|
||||
range = Args::CardRange.new(opts[:range], deck_size: size)
|
||||
paint = Args::Paint.new(custom_colors).load!(opts, expand_by: size, layout: layout)
|
||||
box = Args::Box.new(self, {width: :native, height: :native}).load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
||||
box = Args::ScaleBox.new(self).load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
||||
trans = Args::Transform.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
||||
ifile = Args::InputFile.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
||||
@progress_bar.start('Loading PNG(s)', range.size) do |bar|
|
||||
|
|
@ -59,8 +59,8 @@ module Squib
|
|||
# @option opts force_id [Boolean] (false) if set, then this svg will not be rendered at all if the id is empty or nil. If not set, the entire SVG is rendered. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
|
||||
# @option opts x [Integer] (0) the x-coordinate to place. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}. Supports Unit Conversion, see {file:README.md#Units Units}.
|
||||
# @option opts y [Integer] (0) the y-coordinate to place. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}. Supports Unit Conversion, see {file:README.md#Units Units}.
|
||||
# @option opts width [Integer] (:native) the pixel width that the image should scale to. SVG scaling is done with vectors, so the scaling should be smooth. When set to `:native`, uses the DPI and units of the loaded SVG document. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}. Supports Unit Conversion, see {file:README.md#Units Units}.
|
||||
# @option opts height [Integer] (:native) the pixel width that the image should scale to. SVG scaling is done with vectors, so the scaling should be smooth. When set to `:native`, uses the DPI and units of the loaded SVG document. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}. Supports Unit Conversion, see {file:README.md#Units Units}.
|
||||
# @option opts width [Integer] (:native) the pixel width that the image should scale to. :deck will scale to the deck height. :scale will use the width to scale and keep native the aspect ratio. SVG scaling is done with vectors, so the scaling should be smooth. When set to `:native`, uses the DPI and units of the loaded SVG document. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}. Supports Unit Conversion, see {file:README.md#Units Units}.
|
||||
# @option opts height [Integer] (:native) the pixel width that the image should scale to. :deck will scale to the deck height. :scale will use the width to scale and keep native the aspect ratio. SVG scaling is done with vectors, so the scaling should be smooth. When set to `:native`, uses the DPI and units of the loaded SVG document. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}. Supports Unit Conversion, see {file:README.md#Units Units}.
|
||||
# @option opts layout [String, Symbol] (nil) entry in the layout to use as defaults for this command. See {file:README.md#Custom_Layouts Custom Layouts}. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
|
||||
# @option opts blend [:none, :multiply, :screen, :overlay, :darken, :lighten, :color_dodge, :color_burn, :hard_light, :soft_light, :difference, :exclusion, :hsl_hue, :hsl_saturation, :hsl_color, :hsl_luminosity] (:none) the composite blend operator used when applying this image. See Blend Modes at http://cairographics.org/operators. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
|
||||
# @option opts angle [FixNum] (0) Rotation of the in radians. Note that this rotates around the upper-left corner, making the placement of x-y coordinates slightly tricky. Supports Arrays, see {file:README.md#Arrays_and_Singleton_Expansion Arrays and Singleon Expansion}
|
||||
|
|
@ -71,7 +71,7 @@ module Squib
|
|||
Dir.chdir(img_dir) do
|
||||
range = Args::CardRange.new(opts[:range], deck_size: size)
|
||||
paint = Args::Paint.new(custom_colors).load!(opts, expand_by: size, layout: layout)
|
||||
box = Args::Box.new(self, {width: :native, height: :native}).load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
||||
box = Args::ScaleBox.new(self).load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
||||
trans = Args::Transform.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
||||
ifile = Args::InputFile.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
||||
svg_args = Args::SvgSpecial.new.load!(opts, expand_by: size, layout: layout, dpi: dpi)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,53 @@
|
|||
require 'squib/args/arg_loader'
|
||||
|
||||
module Squib
|
||||
# @api private
|
||||
module Args
|
||||
|
||||
class ScaleBox
|
||||
include ArgLoader
|
||||
|
||||
def initialize(deck)
|
||||
@deck = deck
|
||||
end
|
||||
|
||||
def self.parameters
|
||||
{ x: 0, y: 0,
|
||||
width: :native, height: :native
|
||||
}
|
||||
end
|
||||
|
||||
def self.expanding_parameters
|
||||
parameters.keys # all of them
|
||||
end
|
||||
|
||||
def self.params_with_units
|
||||
parameters.keys # all of them
|
||||
end
|
||||
|
||||
def validate_width(arg, i)
|
||||
return @deck.width if arg.to_s == 'deck'
|
||||
return :native if arg.to_s == 'native'
|
||||
return arg if arg.respond_to? :to_f
|
||||
if arg.to_s == 'scale'
|
||||
raise 'if width is :scale, height must be a number' unless height[i].respond_to? :to_f
|
||||
return arg
|
||||
end
|
||||
raise 'width must be a number, :scale, :native, or :deck'
|
||||
end
|
||||
|
||||
def validate_height(arg, i)
|
||||
return @deck.height if arg.to_s == 'deck'
|
||||
return :native if arg.to_s == 'native'
|
||||
return arg if arg.respond_to? :to_f
|
||||
if arg.to_s == 'scale'
|
||||
raise 'if height is \'scale\', width must be a number' unless width[i].respond_to? :to_f
|
||||
return arg
|
||||
end
|
||||
raise 'height must be a number, :scale, :native, or :deck'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
@ -21,9 +21,11 @@ module Squib
|
|||
use_cairo do |cc|
|
||||
cc.translate(box.x, box.y)
|
||||
if box.width != :native || box.height != :native
|
||||
box.width == :native && box.width = png.width.to_f
|
||||
box.height == :native && box.height = png.height.to_f
|
||||
Squib.logger.warn "PNG scaling results in antialiasing."
|
||||
box.width = png.width.to_f if box.width == :native
|
||||
box.height = png.height.to_f if box.height == :native
|
||||
box.width = png.width.to_f * box.height.to_f / png.height.to_f if box.width == :scale
|
||||
box.height = png.height.to_f * box.width.to_f / png.width.to_f if box.height == :scale
|
||||
Squib.logger.warn "PNG scaling results in aliasing."
|
||||
cc.scale(box.width.to_f / png.width.to_f, box.height.to_f / png.height.to_f)
|
||||
end
|
||||
cc.rotate(trans.angle)
|
||||
|
|
@ -49,6 +51,8 @@ module Squib
|
|||
svg = RSVG::Handle.new_from_data(svg_args.data)
|
||||
box.width = svg.width if box.width == :native
|
||||
box.height = svg.height if box.height == :native
|
||||
box.width = svg.width.to_f * box.height.to_f / svg.height.to_f if box.width == :scale
|
||||
box.height = svg.height.to_f * box.width.to_f / svg.width.to_f if box.height == :scale
|
||||
scale_width = box.width.to_f / svg.width.to_f
|
||||
scale_height = box.height.to_f / svg.height.to_f
|
||||
use_cairo do |cc|
|
||||
|
|
|
|||
|
|
@ -12,6 +12,12 @@ Squib::Deck.new(width: 825, height: 1125, cards: 1) do
|
|||
png file: 'shiny-purse.png', x: 305, y: 50, width: 250, height: 250
|
||||
#...but PNGs will warn if it's an upscale
|
||||
|
||||
# Can be scaled using just width or height, if one of them is set to :scale
|
||||
svg file: 'spanner.svg', x: 200, y: 350, width: 35, height: :scale
|
||||
svg file: 'spanner.svg', x: 200, y: 390, width: :scale, height: 35
|
||||
png file: 'shiny-purse.png', x: 240, y: 350, width: 35, height: :scale
|
||||
png file: 'shiny-purse.png', x: 240, y: 390, width: :scale, height: 35
|
||||
|
||||
# We can also limit our rendering to a single object, if the SVG ID is set
|
||||
svg file: 'spanner.svg', id: '#backdrop', x: 50, y: 350, width: 75, height: 75
|
||||
# Squib prepends a #-sign if one is not specified
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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])
|
||||
|
|
|
|||
Loading…
Reference in New Issue