Class: Appiconset::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/appiconset/generator.rb

Overview

Generator

Instance Method Summary collapse

Constructor Details

#initializeGenerator

Returns a new instance of Generator.



11
# File 'lib/appiconset/generator.rb', line 11

def initialize; end

Instance Method Details

#any_platformsObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/appiconset/generator.rb', line 81

def any_platforms
  platforms = [
    {
      name: 'universal',
      size: [0, 0],
      contents: [
        { width: @width, height: @height, name: 'Icon@3x.png' },
        { width: @width / 3 * 2, height: @height / 3 * 2, name: 'Icon@2x.png' },
        { width: @width / 3, height: @height / 3, name: 'Icon@1x.png' }
      ]
    },
    {
      name: 'tv',
      size: [4640, 1440],
      contents: [
        { width: 800, height: 480, name: 'Icon@2x.png' },
        { width: 400, height: 240, name: 'Icon@1x.png' }
      ]
    },
    {
      name: 'tv-top-shelf',
      size: [4640, 1440],
      contents: [
        { width: 3840, height: 1440, name: 'Icon@2x.png' },
        { width: 1920, height: 720, name: 'Icon@1x.png' }
      ]
    },
    {
      name: 'tv-top-shelf-wide',
      size: [4640, 1440],
      contents: [
        { width: 4640, height: 1440, name: 'Icon@2x.png' },
        { width: 2320, height: 720, name: 'Icon@1x.png' }
      ]
    },
    {
      name: 'tv-appstore',
      size: [4640, 1440],
      contents: [
        { width: 1280, height: 768, name: 'Icon@1x.png' }
      ]
    },
    {
      name: 'tv-1024x1024',
      size: [4640, 1440],
      contents: [
        { width: 1024, height: 1024, name: 'Icon@1x.png' }
      ]
    }
  ]

  platforms.each do |platform|
    platform_name = platform[:name]
    next unless size_match?(platform[:size])

    show_info(platform_name)

    output_dir = "#{@output}#{platform_name}/"
    FileUtils.mkdir_p(output_dir)

    platform[:contents].each do |content|
      width = content[:width].to_f
      height = content[:height].to_f
      name = content[:name]

      image = Magick::ImageList.new(@input)
      if platform_name.start_with?('tv')
        image = image.scale(height / 1440.0)
        image = image.crop(Magick::CenterGravity, width, height)
      else
        image = image.resize_to_fit(width, height)
      end
      image.format = 'PNG'
      image.write(output_dir + name)
    end
  end
end

#config(input, output) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/appiconset/generator.rb', line 13

def config(input, output)
  raise 'no input file.' unless File.exist?(input)
  raise 'no output <dir>.' if output == ''

  size = FastImage.size(input)
  raise 'Unsupported file.' if size.nil?

  @width = size[0]
  @height = size[1]

  output += '/' unless output.end_with?('/')

  FileUtils.mkdir_p(output) unless FileTest.exist?(output)

  @input = input
  @output = output
end

#square_platformsObject

正方形アイコン



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/appiconset/generator.rb', line 32

def square_platforms
  return unless size_match?([1024, 1024])

  json_dir = "#{__dir__}/settings/*.json"

  Dir.glob(json_dir).each do |path|
    json_data = File.open(path) do |f|
      JSON.parse(f.read)
    end

    # プラットフォームごとのディレクトリ
    platform = File.basename(path).gsub('-Contents.json', '')
    output_dir = "#{@output}#{platform}/"
    FileUtils.mkdir_p(output_dir)

    show_info(platform)

    json_data['images'].each do |image|
      size = image['size'].match(/(.*?)x/)[1].to_f
      scale = image['scale'].gsub('x', '').to_i
      # 実際のサイズ
      real_size = size * scale
      name = image['filename']

      image = Magick::ImageList.new(@input)
      new_image = image.resize_to_fit(real_size, real_size)
      new_image.format = 'PNG'
      new_image.write(output_dir + name)
    end

    # Xcode用にファイルをコピーする
    begin
      FileUtils.cp(path, "#{output_dir}/Contents.json") if json_data['info']['author'] == 'xcode'
    rescue StandardError
      # none
    end

    if platform == 'icns.iconset' && RbConfig::CONFIG['host_os'].match(/darwin|mac os/)
      # macOSで実行可能
      system("iconutil -c icns --output #{output_dir}/Icon.icns #{output_dir}/")
    elsif platform == 'favicon'
      images = Dir.glob("#{output_dir}*")
      image = Magick::ImageList.new(*images)
      image.format = 'ICO'
      image.write("#{output_dir}favicon.ico")
    end
  end
end