Last active
September 27, 2020 01:12
-
-
Save raccy/97e0cb066e4cf851d945 to your computer and use it in GitHub Desktop.
SVGファイルをMacのアイコン形式icns(Retina対応)に変換するだけ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# svg2icns.rb | |
# Copyright (c) 2015 IGARASHI Makoto (raccy) | |
# This software is released under the MIT License. | |
# http://opensource.org/licenses/mit-license.php | |
# SVGファイルをMacのアイコン形式であるicnsに変換します。 | |
# 作成されるicnsはRetina対応です。 | |
# SVGファイルはあらかじめ正方形にしておいて下さい。 | |
# そうじゃないと歪みます。 | |
# 実行前に、"gem install rsvg2" しておいてください。 | |
# 既存ファイルは問答無用で上書きされますので注意して下さい。 | |
require "rsvg2" | |
PNG_ICONSET = { | |
16 => ["icon_16x16.png"], | |
32 => ["icon_16x16@2x.png", "icon_32x32.png"], | |
64 => ["icon_32x32@2x.png"], | |
128 => ["icon_128x128.png"], | |
256 => ["icon_128x128@2x.png", "icon_256x256.png"], | |
512 => ["icon_256x256@2x.png", "icon_512x512.png"], | |
1024 => ["icon_512x512@2x.png"], | |
} | |
def svg2iconset(svg_file, iconset_dir) | |
rsvg = RSVG::Handle.new_from_data(IO.read(svg_file)) | |
PNG_ICONSET.each_pair do |size, png_list| | |
png_list.each do |png_file| | |
surface = Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, size, size) | |
context = Cairo::Context.new(surface) | |
context.scale(size.to_f / rsvg.width, size.to_f / rsvg.height) | |
context.render_rsvg_handle(rsvg) | |
File.open(File.join(iconset_dir, png_file), "wb") do |io| | |
surface.write_to_png(io) | |
end | |
end | |
end | |
end | |
def iconset2icns(iconset_dir) | |
system("iconutil -c icns '#{iconset_dir}'") | |
end | |
if $0 == __FILE__ | |
if ARGV.size < 1 | |
puts "svg2icns.rb - SVGファイルをICNSに変換するツール" | |
puts "使い方: ruby #{$0} svg_file" | |
exit 1 | |
end | |
svg_file = ARGV[0] | |
svg_file_ext = File.extname(svg_file) | |
iconset_dir = File.join(File.dirname(svg_file), | |
File.basename(svg_file, svg_file_ext) + ".iconset") | |
unless svg_file_ext.downcase == ".svg" | |
warn "指定したファイルがSVGではないようです。" | |
exit 2 | |
end | |
unless FileTest.file?(svg_file) | |
warn "ファイルが見つかりません。" | |
exit 2 | |
end | |
unless FileTest.directory?(iconset_dir) | |
Dir.mkdir(iconset_dir) | |
end | |
svg2iconset(svg_file, iconset_dir) | |
iconset2icns(iconset_dir) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment