Crop PNGs, now with more rounded corners!

Contributes to #11
This commit is contained in:
Andy Meneely
2015-10-26 22:35:56 -04:00
parent 7126afbc9c
commit 541c8d49ca
13 changed files with 175 additions and 4 deletions
+9 -2
View File
@@ -25,6 +25,13 @@ module Squib
# @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}
# @option opts mask [String] (nil) If specified, the image will be used as a mask for the given color/gradient. Transparent pixels are ignored, opaque pixels are the given color. Note: the origin for gradient coordinates is at the given x,y, not at 0,0 as it is most other places.
# @option opts crop_x [Integer] (0) Crop the loaded image at this x coordinate. Supports Unit Conversion, see {file:README.md#Units Units}.
# @option opts crop_y [Integer] (0) Crop the loaded image at this y coordinate. Supports Unit Conversion, see {file:README.md#Units Units}.
# @option opts crop_corner_radius [Integer] (0): Radius for rounded corners, both x and y. When set, overrides crop_corner_x_radius and crop_corner_y_radius. Supports Unit Conversion, see {file:README.md#Units Units}.
# @option opts crop_corner_x_radius [Integer] (0): x radius for rounded corners of cropped image. Supports Unit Conversion, see {file:README.md#Units Units}.
# @option opts crop_corner_y_radius [Integer] (0): y radius for rounded corners of cropped image. Supports Unit Conversion, see {file:README.md#Units Units}.
# @option opts crop_width [Integer] (:native): Width of the cropped image. Supports Unit Conversion, see {file:README.md#Units Units}.
# @option opts crop_height [Integer] (:native): Height of the cropped image. Supports Unit Conversion, see {file:README.md#Units Units}.
# @return [nil] Returns nil
# @api public
def png(opts = {})
@@ -32,7 +39,7 @@ module Squib
range = Args::CardRange.new(opts[:range], deck_size: size)
paint = Args::Paint.new(custom_colors).load!(opts, expand_by: size, layout: layout)
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)
trans = Args::Transform.new(self).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|
range.each do |i|
@@ -72,7 +79,7 @@ module Squib
range = Args::CardRange.new(opts[:range], deck_size: size)
paint = Args::Paint.new(custom_colors).load!(opts, expand_by: size, layout: layout)
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)
trans = Args::Transform.new(self).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)
@progress_bar.start('Loading SVG(s)', range.size) do |bar|
+35 -1
View File
@@ -7,8 +7,20 @@ module Squib
class Transform
include ArgLoader
def initialize(deck = nil)
@deck = deck
end
def self.parameters
{ angle: 0 }
{ angle: 0,
crop_x: 0,
crop_y: 0,
crop_width: :native,
crop_height: :native,
crop_corner_radius: nil,
crop_corner_x_radius: 0,
crop_corner_y_radius: 0,
}
end
def self.expanding_parameters
@@ -19,6 +31,28 @@ module Squib
parameters.keys # all of them
end
def validate_crop_width(arg, _i)
return arg if @deck.nil?
return @deck.width if arg == :deck
arg
end
def validate_crop_height(arg, _i)
return arg if @deck.nil?
return @deck.height if arg == :deck
arg
end
def validate_crop_corner_x_radius(arg, i)
return crop_corner_radius[i] unless crop_corner_radius[i].nil?
arg
end
def validate_crop_corner_y_radius(arg, i)
return crop_corner_radius[i] unless crop_corner_radius[i].nil?
arg
end
end
end
+8
View File
@@ -30,6 +30,14 @@ module Squib
end
cc.rotate(trans.angle)
cc.translate(-box.x, -box.y)
# cc.translate(trans.crop_x, trans.crop_y)
trans.crop_width = png.width.to_f if trans.crop_width == :native
trans.crop_height = png.height.to_f if trans.crop_height == :native
cc.rounded_rectangle(box.x, box.y, trans.crop_width, trans.crop_height, trans.crop_corner_x_radius, trans.crop_corner_y_radius)
cc.clip
cc.translate(-trans.crop_x, -trans.crop_y)
cc.set_source(png, box.x, box.y)
cc.operator = paint.blend unless paint.blend == :none
if paint.mask.empty?