summaryrefslogtreecommitdiffhomepage
path: root/ios/convert-assets.rb
blob: 0d49431304f8a4f66994f185d8531c1a87ff297a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
80
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/ruby

require 'optparse'

SCRIPT_DIR = File.expand_path(File.dirname(__FILE__))
ROOT_DIR = File.dirname(SCRIPT_DIR)

# assets catalogue root
XCASSETS_DIR = File.join(SCRIPT_DIR, "MullvadVPN/Assets.xcassets")
XCASSETS_APPICON_DIR = File.join(XCASSETS_DIR, "AppIcon.appiconset")

# graphical assets sources
APPICON_PATH = File.join(ROOT_DIR, "graphics/icon-square.svg")
GRAPHICAL_ASSETS_DIR = File.join(ROOT_DIR, "gui/assets/images")
ADDITIONAL_ASSETS_DIR = File.join(SCRIPT_DIR, "AdditionalAssets")

# graphical assets to import
GRAPHICAL_ASSETS=[
  "icon-alert.svg",
  "icon-arrow.svg",
  "icon-back.svg",
  "icon-chevron-down.svg",
  "icon-chevron-up.svg",
  "icon-chevron.svg",
  "icon-close-sml.svg",
  "icon-close.svg",
  "icon-extLink.svg",
  "icon-fail.svg",
  "icon-fastest.svg",
  "icon-nearest.svg",
  "icon-reload.svg",
  "icon-settings.svg",
  "icon-spinner.svg",
  "icon-success.svg",
  "icon-tick.svg",
  "location-marker-secure.svg",
  "location-marker-unsecure.svg",
  "logo-icon.svg"
]

# App icon sizes
APP_ICON_SIZES=[
  # iphone-notification 20pt at 2x, 3x
  ["AppIconPhoneNotification", 20, 2, 3],

  # iphone-settings at 29pt, 2x, 3x
  ["AppIconPhoneSettings", 29, 2, 3],

  # iphone-spotlight at 40pt, 2x, 3x
  ["AppIconPhoneSpotlight", 40, 2, 3],

  # iphone-app at 60pt, 2x, 3x
  ["AppIconPhone", 60, 2, 3],

  # ipad-notifications at 20pt, 1x, 2x
  ["AppIconPadNotifications", 20, 1, 2],

  # ipad-settings at 29pt, 1x, 2x
  ["AppIconPadSettings", 29, 1, 2],

  # ipad-spotlight at 40pt, 1x, 2x
  ["AppIconPadSpotlight", 40, 1, 2],

  # ipad-app at 76pt, 1x, 2x
  ["AppIconPad", 76, 1, 2],

  # ipad-pro-app at 83.5pt, 2x
  ["AppIconPadPro", 83.5, 2],

  # appstore-ios (marketing) at 1024pt, 1x
  ["AppStoreIosMarketing", 1024, 1],
]

# Additional assets generated from SVG -> vector PDF
ADDITIONAL_ASSETS = [
  "DefaultButton.svg",
  "SuccessButton.svg",
  "DangerButton.svg",
  "TranslucentDangerButton.svg",
  "TranslucentNeutralButton.svg",
  "TranslucentDangerSplitLeftButton.svg",
  "TranslucentDangerSplitRightButton.svg"
]

# Functions

def generate_graphical_assets()
  for asset_name in GRAPHICAL_ASSETS do
    svg_file = File.join(GRAPHICAL_ASSETS_DIR, asset_name)
    image_name = pascal_case(File.basename(svg_file, ".svg"))
    output_dir = File.join(XCASSETS_DIR, "#{image_name}.imageset")

    if !Dir.exists?(output_dir)
      puts "Create directory #{output_dir}"
      Dir.mkdir(output_dir)
    end

    output_file = File.join(output_dir, "#{image_name}.pdf")

    puts "Convert #{svg_file} -> #{output_file}"
    system("rsvg-convert", "--format=pdf", svg_file, "--output", output_file)
  end
end

def genereate_app_icon()
  for (icon_name, nominal_size, *retina_scales) in APP_ICON_SIZES do
    for retina_scale in retina_scales do
      scale_suffix = retina_scale_suffix(retina_scale)
      output_file = File.join(XCASSETS_APPICON_DIR, "#{icon_name}#{scale_suffix}.png")
      actual_size = (nominal_size * retina_scale).to_i

      puts "Generate #{icon_name}: #{nominal_size} (#{retina_scale}x) -> #{output_file}"
      system("rsvg-convert", "--width=#{actual_size}", "--height=#{actual_size}", "--format=png", APPICON_PATH, "--output", output_file)
    end
  end
end

def generate_additional_assets()
  # Fix PDF "CreationDate" for reproducible output
  environment_variables = { "SOURCE_DATE_EPOCH" => "1596022781" }

  for asset_name in ADDITIONAL_ASSETS do
    svg_file = File.join(ADDITIONAL_ASSETS_DIR, asset_name)
    image_name = File.basename(svg_file, ".svg")
    output_dir = File.join(XCASSETS_DIR, "#{image_name}.imageset")
    output_file = File.join(output_dir, "#{image_name}.pdf")

    if !Dir.exists?(output_dir)
      puts "Create directory #{output_dir}"
      Dir.mkdir(output_dir)
    end

    puts "Generate #{image_name} -> #{output_file}"
    system(environment_variables, "rsvg-convert", "-f", "pdf", "-o", output_file, svg_file)
  end
end

def pascal_case(str)
  return str.split('-').collect(&:capitalize).join
end

def retina_scale_suffix(retina_scale)
  return retina_scale == 1 ? "" : "@#{retina_scale}x"
end

def command?(name)
  `which #{name}`
  $?.success?
end

# Check requirements

if !command?("rsvg-convert")
  puts "rsvg-convert is not installed."
  exit
end

# Parse program arguments

ARGV << '-h' if ARGV.empty?

OptionParser.new do |opts|
  opts.banner = "Usage: convert-assets.rb [options]"

  opts.on("--app-icon", "Generate application icon assets") do |v|
    genereate_app_icon
  end

  opts.on("--import-desktop-assets", "Import assets from the desktop app") do |v|
    generate_graphical_assets
  end

  opts.on("--additional-assets", "Generate additional assets") do |v|
    generate_additional_assets
  end

  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end
end.parse!