Add :scale shortcut to width/height of png and svg
Allows scaling while keeping the aspect ratio Closes #91
This commit is contained in:
@@ -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|
|
||||
|
||||
Reference in New Issue
Block a user