lib/yamg/icon.rb in yamg-0.0.9 vs lib/yamg/icon.rb in yamg-0.3.0
- old
+ new
@@ -2,34 +2,45 @@
#
# ICONS
#
#
class Icon
- attr_accessor :src, :size, :rounded, :icons
+ attr_accessor :src, :size, :dpi, :rounded, :icons
def initialize(src, size, rounded = false)
fail if src.nil? || src.empty?
@src = src
@size = size
@rounded = rounded
@icons = YAMG.load_images(src)
+ YAMG.puts_and_exit("No sources in '#{src}'") if icons.empty?
+ @path = File.join(src, find_closest_gte_icon)
+ @dpi = 90
end
+ alias_method :rounded?, :rounded
def find_closest_gte_icon
return icons.max_by(&:to_i) if icons.map(&:to_i).max < size
icons.min_by do |f|
# n = x.match(/\d+/).to_s.to_i
n = f.to_i
size > n ? Float::INFINITY : n
end
end
- def image
- path = File.join(src, find_closest_gte_icon)
- img = MiniMagick::Image.open(path)
- img.resize size # "NxN"
- rounded ? round(img) : img
+ def image(out)
+ if File.extname(@path) =~ /svg/
+ pixels = dpi ? "-d #{dpi} -p #{dpi}" : nil
+ args = "#{pixels} -w #{size} -h #{size} -f png"
+ YAMG.run_rsvg(@path, out, args)
+ img = MiniMagick::Image.open(out)
+ else
+ img = MiniMagick::Image.open(@path)
+ img.resize size # "NxN"
+ write_out(img, out)
+ end
+ write_out(round(img), out) if rounded?
end
#
# Maybe this can be smaller, terminal equivalent:
# convert
@@ -70,8 +81,18 @@
masked.composite(overlay, 'png') do |i|
i.compose 'Over'
end
masked
+ end
+
+ #
+ # Writes image to disk
+ #
+ def write_out(img, out)
+ FileUtils.mkdir_p File.dirname(out)
+ img.write(out)
+ rescue Errno::ENOENT
+ puts_and_exit("Path not found '#{out}'")
end
end
end